diff --git a/firmware/libs/game_mgmt/include/game_mgmt.h b/firmware/libs/game_mgmt/include/game_mgmt.h index 203f1a6..fff174e 100644 --- a/firmware/libs/game_mgmt/include/game_mgmt.h +++ b/firmware/libs/game_mgmt/include/game_mgmt.h @@ -17,6 +17,42 @@ typedef enum { SYS_STATE_POST_GAME = 0x05 /* Match ended: Data collection and review */ } sys_state_t; +/** + * @brief Game control command structure for THREAD communication. + * This structure is used to send control commands (start/end game, set ID) + * over the Thread network. It is designed to be flexible for future + * expansion. + */ +typedef enum +{ + GAME_CTRL_CMD_START_GAME = 0x01, + GAME_CTRL_CMD_END_GAME = 0x02, + GAME_CTRL_CMD_SET_ID = 0x03, + // Future commands can be added here +} game_ctrl_command_t; + +/** + * @brief Payload structure for game control messages sent over Thread. + * The union allows for different data formats based on the command type. + */ +typedef struct __packed +{ + game_ctrl_command_t command; + union + { + struct + { + uint32_t start_time; // lower 32 bits of the start time in the open thread timer + uint32_t duration; // in seconds, maximum 49 days (2^32 seconds), should be sufficient for any match ;) + } start_game; + struct + { + uint64_t game_id; // Unique identifier for the game, can be used for stats tracking or match history + } game_id; + uint8_t raw[16]; // For future expansion or custom commands + } data; +} game_control_payload_t; + /** * @brief Callback for state changes. * Allows apps/modules to react when the system transitions (e.g., UI updates). diff --git a/firmware/libs/game_mgmt/src/game_mgmt.c b/firmware/libs/game_mgmt/src/game_mgmt.c index 2262a15..5e132f6 100644 --- a/firmware/libs/game_mgmt/src/game_mgmt.c +++ b/firmware/libs/game_mgmt/src/game_mgmt.c @@ -9,6 +9,9 @@ LOG_MODULE_REGISTER(game_mgmt, CONFIG_GAME_MGMT_LOG_LEVEL); static sys_state_t current_state = SYS_STATE_IDLE; static uint64_t current_game_id = 0; +// Forward declaration for functions defined later in this file +void game_mgmt_init_coap(void); + int game_mgmt_init(void) { game_mgmt_init_coap(); @@ -67,7 +70,7 @@ void game_mgmt_init_coap(void) struct otInstance *instance = openthread_get_default_instance(); if (instance) { otCoapAddResource(instance, &game_ctrl_res); - LOG_DBG("CoAP Ressource '/g' registriert."); + LOG_DBG("Registrated CoAP Ressource '/g'"); } } diff --git a/firmware/libs/thread_mgmt/include/thread_mgmt.h b/firmware/libs/thread_mgmt/include/thread_mgmt.h index e1ebc21..3fce57e 100644 --- a/firmware/libs/thread_mgmt/include/thread_mgmt.h +++ b/firmware/libs/thread_mgmt/include/thread_mgmt.h @@ -55,10 +55,11 @@ void thread_mgmt_get_network_name(char *dest_str, size_t max_len); typedef void (*thread_mgmt_timeout_cb_t)(void); /** - * @brief Schedule a callback using a 32-bit OpenThread network timer value. - * @param t32_us Target timestamp (lower 32 bits, in microseconds). - * @param cb Callback function to execute at the scheduled time. + * @brief Return the current Thread network time in microseconds. + * This is the time since the Thread network was formed or last reset, and is + * synchronized across the network. + * @return 64-bit network time, or 0 if the stack is not ready. */ -void thread_mgmt_schedule_network_event(uint32_t t32_us, thread_mgmt_timeout_cb_t cb); +uint64_t thread_mgmt_get_network_time(void); #endif \ No newline at end of file diff --git a/firmware/libs/thread_mgmt/src/thread_mgmt.c b/firmware/libs/thread_mgmt/src/thread_mgmt.c index e2d70cc..cec2fcb 100644 --- a/firmware/libs/thread_mgmt/src/thread_mgmt.c +++ b/firmware/libs/thread_mgmt/src/thread_mgmt.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -286,11 +287,30 @@ void thread_mgmt_get_network_name(char *dest_str, size_t max_len) } } +uint64_t thread_mgmt_get_network_time(void) +{ + struct otInstance *instance = openthread_get_default_instance(); + uint64_t networkTime = 0; + + if (!instance) + { + return 0; + } + + otNetworkTimeStatus status = otNetworkTimeGet(instance, &networkTime); + + if (status == OT_NETWORK_TIME_SYNCHRONIZED || status == OT_NETWORK_TIME_RESYNC_NEEDED) + { + return networkTime; + } + + return 0; +} + /* --- Shell Commands --- */ #if IS_ENABLED(CONFIG_THREAD_MGMT_SHELL) #include #include -#include otError ot_time_handler(void *aContext, uint8_t aArgsLength, char **aArgs) { @@ -306,19 +326,20 @@ otError ot_time_handler(void *aContext, uint8_t aArgsLength, char **aArgs) status = otNetworkTimeGet(instance, &networkTime); syncPeriod = otNetworkTimeGetSyncPeriod(instance); - switch (status) { - case OT_NETWORK_TIME_SYNCHRONIZED: - otCliOutputFormat("Synchronized: %llu us, Sync Period: %u ms\n", networkTime, syncPeriod); - break; - case OT_NETWORK_TIME_RESYNC_NEEDED: - otCliOutputFormat("Resync needed. Last time: %llu us, Sync Period: %u ms\n", networkTime, syncPeriod); - break; - case OT_NETWORK_TIME_UNSYNCHRONIZED: - otCliOutputFormat(FORMAT_RED_BOLD("Error: Network time not synchronized. Sync Period: %u ms\n"), syncPeriod); - break; - default: - otCliOutputFormat(FORMAT_RED_BOLD("Error: Unknown network time status. Sync Period: %u ms\n"), syncPeriod); - break; + switch (status) + { + case OT_NETWORK_TIME_SYNCHRONIZED: + otCliOutputFormat("Synchronized: %llu us, Sync Period: %u ms\n", networkTime, syncPeriod); + break; + case OT_NETWORK_TIME_RESYNC_NEEDED: + otCliOutputFormat("Resync needed. Last time: %llu us, Sync Period: %u ms\n", networkTime, syncPeriod); + break; + case OT_NETWORK_TIME_UNSYNCHRONIZED: + otCliOutputFormat(FORMAT_RED_BOLD("Error: Network time not synchronized. Sync Period: %u ms\n"), syncPeriod); + break; + default: + otCliOutputFormat(FORMAT_RED_BOLD("Error: Unknown network time status. Sync Period: %u ms\n"), syncPeriod); + break; } return OT_ERROR_NONE; }