Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move some OS-specifics into tox_system. #2397

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,76 @@ load("//tools/project:build_defs.bzl", "project")

project(license = "gpl3-https")

genrule(
name = "toxcore_headers",
srcs = ["//c-toxcore/toxcore:public"],
outs = [
"tox/toxcore/os_log.h",
"tox/toxcore/os_memory.h",
"tox/toxcore/os_network.h",
"tox/toxcore/os_random.h",
"tox/toxcore/os_system.h",
"tox/toxcore/tox.h",
"tox/toxcore/tox_attributes.h",
"tox/toxcore/tox_dispatch.h",
"tox/toxcore/tox_events.h",
"tox/toxcore/tox_log.h",
"tox/toxcore/tox_log_impl.h",
"tox/toxcore/tox_log_level.h",
"tox/toxcore/tox_memory.h",
"tox/toxcore/tox_memory_impl.h",
"tox/toxcore/tox_network.h",
"tox/toxcore/tox_network_impl.h",
"tox/toxcore/tox_options.h",
"tox/toxcore/tox_random.h",
"tox/toxcore/tox_random_impl.h",
"tox/toxcore/tox_system.h",
"tox/toxcore/tox_system_impl.h",
"tox/toxcore/tox_time.h",
"tox/toxcore/tox_time_impl.h",
],
cmd = "cp $(locations //c-toxcore/toxcore:public) $(GENDIR)/c-toxcore/tox/toxcore/",
visibility = ["//visibility:public"],
)

genrule(
name = "public_headers",
srcs = [
"tox.h",
"toxav.h",
"toxencryptsave.h",
"//c-toxcore/toxav:toxav.h",
"//c-toxcore/toxcore:tox.h",
"//c-toxcore/toxcore:tox_dispatch.h",
"//c-toxcore/toxcore:tox_events.h",
"//c-toxcore/toxcore:tox_private.h",
"//c-toxcore/toxencryptsave:toxencryptsave.h",
],
outs = [
"tox/toxav.h",
"tox/tox.h",
"tox/tox_dispatch.h",
"tox/tox_events.h",
"tox/tox_private.h",
"tox/toxav.h",
"tox/toxav/toxav.h",
"tox/toxencryptsave.h",
"tox/toxencryptsave/toxencryptsave.h",
],
cmd = """
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/tox.h
cp $(location //c-toxcore/toxcore:tox_dispatch.h) $(GENDIR)/c-toxcore/tox/tox_dispatch.h
cp $(location //c-toxcore/toxcore:tox_events.h) $(GENDIR)/c-toxcore/tox/tox_events.h
cp $(location //c-toxcore/toxcore:tox_private.h) $(GENDIR)/c-toxcore/tox/tox_private.h
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
cp $(location toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
cp $(location tox.h) $(GENDIR)/c-toxcore/tox/tox.h
cp $(location toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav/toxav.h
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave/toxencryptsave.h
""",
visibility = ["//visibility:public"],
)

filegroup(
name = "public",
srcs = [
":public_headers",
":toxcore_headers",
],
visibility = ["//visibility:public"],
)

cc_library(
name = "c-toxcore",
hdrs = [":public_headers"],
hdrs = [":public"],
includes = ["."],
visibility = ["//visibility:public"],
deps = [
Expand Down
62 changes: 56 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ endif()
set_source_files_properties(
toxcore/mono_time.c
toxcore/network.c
toxcore/os_time.c
toxcore/tox.c
toxcore/util.c
PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)
Expand Down Expand Up @@ -319,6 +320,19 @@ set(toxcore_SOURCES
toxcore/onion_client.c
toxcore/onion_client.h
toxcore/onion.h
toxcore/os_log.c
toxcore/os_log.h
toxcore/os_memory.c
toxcore/os_memory.h
toxcore/os_network.c
toxcore/os_network.h
toxcore/os_network_impl.h
toxcore/os_random.c
toxcore/os_random.h
toxcore/os_system.c
toxcore/os_system.h
toxcore/os_time.c
toxcore/os_time.h
toxcore/ping_array.c
toxcore/ping_array.h
toxcore/ping.c
Expand All @@ -341,13 +355,35 @@ set(toxcore_SOURCES
toxcore/timed_auth.h
toxcore/tox_api.c
toxcore/tox.c
toxcore/tox.h
toxcore/tox_dispatch.c
toxcore/tox_dispatch.h
toxcore/tox_event.c
toxcore/tox_event.h
toxcore/tox_events.c
toxcore/tox_events.h
toxcore/tox.h
toxcore/tox_log.c
toxcore/tox_log.h
toxcore/tox_log_impl.h
toxcore/tox_log_level.c
toxcore/tox_log_level.h
toxcore/tox_memory.c
toxcore/tox_memory.h
toxcore/tox_memory_impl.h
toxcore/tox_network.c
toxcore/tox_network.h
toxcore/tox_network_impl.h
toxcore/tox_options.c
toxcore/tox_options.h
toxcore/tox_random.c
toxcore/tox_random.h
toxcore/tox_random_impl.h
toxcore/tox_system.c
toxcore/tox_system.h
toxcore/tox_system_impl.h
toxcore/tox_time.c
toxcore/tox_time.h
toxcore/tox_time_impl.h
toxcore/tox_private.c
toxcore/tox_private.h
toxcore/tox_pack.c
Expand All @@ -368,13 +404,25 @@ else()
endif()
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium)
set(toxcore_API_HEADERS
${toxcore_SOURCE_DIR}/tox.h^tox
${toxcore_SOURCE_DIR}/toxcore/os_log.h^os
${toxcore_SOURCE_DIR}/toxcore/os_memory.h^os
${toxcore_SOURCE_DIR}/toxcore/os_network.h^os
${toxcore_SOURCE_DIR}/toxcore/os_random.h^os
${toxcore_SOURCE_DIR}/toxcore/os_system.h^os
${toxcore_SOURCE_DIR}/toxcore/tox.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_attributes.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox)
if(EXPERIMENTAL_API)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxcore/tox_private.h^tox)
endif()
${toxcore_SOURCE_DIR}/toxcore/tox_log.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_log_level.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_memory.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_network.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_options.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_random.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_system.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_system_impl.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_time.h^tox)

################################################################################
#
Expand Down Expand Up @@ -403,6 +451,7 @@ if(BUILD_TOXAV)
toxav/video.c
toxav/video.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxav.h^toxav
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)

if(TARGET Opus::opus AND TARGET libvpx::libvpx)
Expand All @@ -428,6 +477,7 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxencryptsave/toxencryptsave.c
toxencryptsave/toxencryptsave.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxencryptsave.h^tox
${toxcore_SOURCE_DIR}/toxencryptsave/toxencryptsave.h^tox)

################################################################################
Expand Down
5 changes: 5 additions & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cc_library(
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
"//c-toxcore/toxcore:tox_time",
],
)

Expand Down Expand Up @@ -75,10 +76,14 @@ extra_data = {
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:onion_client",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_network",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
"//c-toxcore/toxcore:tox_unpack",
"//c-toxcore/toxcore:tox_time",
"//c-toxcore/toxcore:util",
"//c-toxcore/toxencryptsave",
"@libsodium",
Expand Down
16 changes: 9 additions & 7 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/network.h"
#include "../toxcore/os_random.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_memory.h"
#include "auto_test_support.h"

#define NUM_PORTS 3
Expand Down Expand Up @@ -52,7 +54,7 @@ static void test_basic(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);

Expand Down Expand Up @@ -319,7 +321,7 @@ static void test_some(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -518,7 +520,7 @@ static void test_client(void)
ck_assert(mem != nullptr);

Logger *logger = logger_new(mem);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand Down Expand Up @@ -655,7 +657,7 @@ static void test_client_invalid(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -734,7 +736,7 @@ static void test_tcp_connection(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

tcp_data_callback_called = 0;
Expand Down Expand Up @@ -849,7 +851,7 @@ static void test_tcp_connection2(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

tcp_oobdata_callback_called = 0;
Expand Down
5 changes: 4 additions & 1 deletion auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_random.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"
Expand Down Expand Up @@ -60,7 +63,7 @@ static void test_store_data(void)
Logger *log = logger_new(mem);
ck_assert(log != nullptr);
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
ck_assert(mono_time != nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns);
ck_assert(net != nullptr);
Expand Down
18 changes: 15 additions & 3 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include "../toxcore/mono_time.h"
#include "../toxcore/tox_dispatch.h"
#include "../toxcore/tox_events.h"
#include "../toxcore/tox_struct.h"
#include "../toxcore/tox_impl.h"
#include "../toxcore/tox_time_impl.h"

#include "auto_test_support.h"

Expand Down Expand Up @@ -157,17 +158,25 @@ static uint64_t get_state_clock_callback(void *user_data)
return *clock;
}

static const Tox_Time_Funcs autotox_time_funcs = {
get_state_clock_callback,
};

void set_mono_time_callback(AutoTox *autotox)
{
ck_assert(autotox != nullptr);

if (autotox->tm == nullptr) {
autotox->tm = tox_time_new(&autotox_time_funcs, &autotox->clock, autotox->tox->sys.mem);
}

Mono_Time *mono_time = autotox->tox->mono_time;

mono_time_set_current_time_callback(mono_time, nullptr); // set to default first
autotox->clock = current_time_monotonic(mono_time);
ck_assert_msg(autotox->clock >= 1000,
"clock is too low (not initialised?): %lu", (unsigned long)autotox->clock);
mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock);
mono_time_set_current_time_callback(mono_time, autotox->tm);
}

void save_autotox(AutoTox *autotox)
Expand All @@ -194,6 +203,8 @@ void kill_autotox(AutoTox *autotox)
autotox->alive = false;
tox_dispatch_free(autotox->dispatch);
tox_kill(autotox->tox);
tox_time_free(autotox->tm);
autotox->tm = nullptr;
}

void reload(AutoTox *autotox)
Expand Down Expand Up @@ -399,6 +410,7 @@ void run_auto_test(struct Tox_Options *options, uint32_t tox_count, autotox_test
for (uint32_t i = 0; i < tox_count; ++i) {
tox_dispatch_free(autotoxes[i].dispatch);
tox_kill(autotoxes[i].tox);
tox_time_free(autotoxes[i].tm);
free(autotoxes[i].state);
free(autotoxes[i].save_state);
}
Expand Down
1 change: 1 addition & 0 deletions auto_tests/auto_test_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
typedef struct AutoTox {
Tox *tox;
Tox_Dispatch *dispatch;
Tox_Time *tm;

uint32_t index;
uint64_t clock;
Expand Down
1 change: 1 addition & 0 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdint.h>

#include "../toxav/toxav.h"
#include "../toxcore/os_random.h"
#include "check_compat.h"

#define NUM_AV_GROUP_TOX 16
Expand Down
1 change: 1 addition & 0 deletions auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <time.h>
#include <stdint.h>

#include "../toxcore/os_random.h"
#include "../toxcore/util.h"

#include "check_compat.h"
Expand Down
Loading
Loading