tomato/other/docker/esp32/main/tox_main.cc
Green Sky b2ae9530a4 Squashed 'external/toxcore/c-toxcore/' changes from e29e185c03..f1df709b87
f1df709b87 feat: add ngc events
1b6c907235 refactor: Make event dispatch ordered by receive time.
b7f9367f6f test: Upgrade cppcheck, fix some warnings.
766e62bc89 chore: Use `pkg_search_module` directly in cmake.
00ff078f91 cleanup: Use target_link_libraries directly in cmake.
c58928cc89 chore: Add `IMPORTED_TARGET` to pkg-config packages.
895a6af122 cleanup: Remove NaCl support.
41dfb1c1c0 fix: unpack enum function names in event impl generator
447666d1a1 chore: Disable targets for cross-compilation.
572924e924 chore: Build a docker image with coverage info in it.
415cb78f5e cleanup: Some portability/warning fixes for Windows builds.
425216d9ec fix: Correct a use-after-free and fix some memory leaks.
4b1cfa3e08 refactor: Change all enum-like `#define` sequences into enums.
d3c2704fa9 chore: Fix make_single_file to support core-only.
0ce46b644e refactor: Change the `TCP_PACKET_*` defines into an enum.
22cd38ad50 adopt event impl generation tool to #2392
f31ea1088a add the event impl generation tool
4e603bb613 refactor: Use `enum-from-int` rule from tokstyle.
19d8f180d6 chore: Update github actions `uses`.
6a895be0c7 test: Make esp32 build actually try to instantiate tox.
65d09c9bfb cleanup: Remove test net support.
REVERT: e29e185c03 feat: add ngc events

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: f1df709b8792da4c0e946d826b11df77d565064d
2023-12-27 12:37:22 +01:00

99 lines
2.9 KiB
C++

#include "../main/tox_main.h"
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <memory>
#include "../../../../toxcore/ccompat.h"
#include "../../../../toxcore/tox.h"
#include "../../../../toxcore/tox_events.h"
static const char *color(int index)
{
switch (index) {
case 0:
return "\033"
"[35m";
case 1:
return "\033"
"[36m";
}
return "\033"
"[0m";
}
static tox_log_cb log_handler;
static void log_handler(Tox *tox, Tox_Log_Level level, const char *file, uint32_t line,
const char *func, const char *msg, void *user_data)
{
const int *index = static_cast<const int *>(user_data);
const uint16_t udp_port = tox_self_get_udp_port(tox, nullptr);
printf("%s#%d (:%d) [%c] %s:%u(%s): %s\n", color(*index), *index, udp_port,
tox_log_level_to_string(level)[0], file, static_cast<unsigned int>(line), func, msg);
}
using Tox_Options_Ptr = std::unique_ptr<Tox_Options, void (*)(Tox_Options *)>;
using Tox_Ptr = std::unique_ptr<Tox, void (*)(Tox *)>;
void tox_main()
{
printf("Hello Tox!\n");
Tox_Options_Ptr opts(tox_options_new(nullptr), tox_options_free);
assert(opts != nullptr);
tox_options_set_ipv6_enabled(opts.get(), false);
tox_options_set_local_discovery_enabled(opts.get(), false);
tox_options_set_log_callback(opts.get(), log_handler);
Tox_Err_New err;
int index[] = {0, 1};
tox_options_set_log_user_data(opts.get(), &index[0]);
Tox_Ptr tox0(tox_new(opts.get(), &err), tox_kill);
printf("tox_new(#0): %p\n", static_cast<void *>(tox0.get()));
if (err != TOX_ERR_NEW_OK) {
printf("tox_new(#0): %s\n", tox_err_new_to_string(err));
return;
}
tox_options_set_log_user_data(opts.get(), &index[1]);
Tox_Ptr tox1(tox_new(opts.get(), &err), tox_kill);
printf("tox_new(#1): %p\n", static_cast<void *>(tox0.get()));
if (err != TOX_ERR_NEW_OK) {
printf("tox_new(#1): %s\n", tox_err_new_to_string(err));
return;
}
uint8_t pk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox0.get(), pk);
tox_bootstrap(tox1.get(), "localhost", tox_self_get_udp_port(tox0.get(), nullptr), pk, nullptr);
#if 0
tox_self_get_public_key(tox0.get(), pk);
tox_friend_add_norequest(tox1.get(), pk, nullptr);
tox_self_get_public_key(tox1.get(), pk);
tox_friend_add_norequest(tox0.get(), pk, nullptr);
#endif
printf("bootstrapping and connecting 2 toxes\n");
while (tox_self_get_connection_status(tox1.get()) == TOX_CONNECTION_NONE
|| tox_self_get_connection_status(tox0.get()) == TOX_CONNECTION_NONE) {
tox_events_free(tox_events_iterate(tox0.get(), true, nullptr));
tox_events_free(tox_events_iterate(tox1.get(), true, nullptr));
usleep(tox_iteration_interval(tox0.get()) * 1000);
usleep(250); // a bit less noise in the log
}
}