Sync while working on OT
All checks were successful
Deploy Docs / build-and-deploy (push) Successful in 12s

This commit is contained in:
2026-02-17 13:44:03 +01:00
parent 37d1bd6db1
commit 5406c9917f
4 changed files with 80 additions and 19 deletions

View File

@@ -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).

View File

@@ -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'");
}
}

View File

@@ -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

View File

@@ -8,6 +8,7 @@
#include <openthread/udp.h>
#include <openthread/coap.h>
#include <openthread/ip6.h>
#include <openthread/network_time.h>
#include <string.h>
#include <stdio.h>
#include <lasertag_utils.h>
@@ -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 <zephyr/shell/shell.h>
#include <openthread/cli.h>
#include <openthread/network_time.h>
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;
}