BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
bs_state_machine.c
Go to the documentation of this file.
1/*
2 * Copyright 2025 Tobias Moullet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * File: bs_state_machine.c
17 * Description: Implementation of the state machine for Bluesync
18 *
19 * Project: BlueSync - BLE Time Sync for Zephyr
20 * Repository: https://github.com/Tobi15/zephyr-bluesync-ble
21 */
22#include <zephyr/kernel.h>
23#include <zephyr/logging/log.h>
24
25#include <bluesync/bluesync.h>
26#include "bluesync.h"
27#include "bs_state_machine.h"
28
29
30
31
32LOG_MODULE_REGISTER(bs_state_machine, CONFIG_BLUESYNC_LOG_LEVEL);
33
40
41static struct bs_sm_param sm_param = {
43 .current_state = BS_NONE_STATE,
44 .handlers = NULL,
45 .mutex = Z_MUTEX_INITIALIZER(sm_param.mutex)
46};
47
49 switch (current_state) {
51 if (event == EVENT_NEW_SYNC_RCV){
52 LOG_DBG("Transition: BS_SCAN_WAIT_FOR_SYNC -> BS_SYNC");
53 return BS_SYNC;
54 }
55 break;
56 case BS_SYNC:
57 if (event == EVENT_SYNC_EXPIRED){
58 LOG_DBG("Transition: BS_SYNC -> BS_UPDATE");
59 return BS_UPDATE;
60 }
61 break;
62 case BS_UPDATE:
63 if (event == EVENT_UPDATE_SUCCESS){
64 LOG_DBG("Transition: BS_UPDATE -> BS_ADV");
65 return BS_ADV;
66 }
67 else if (event == EVENT_UPDATE_FAILED){
68 LOG_DBG("Transition: BS_UPDATE -> BS_SCAN_WAIT_FOR_SYNC");
70 }
71 break;
72 case BS_ADV:
73 if (event == EVENT_ADV_EXPIRED){
75 LOG_DBG("Transition: BS_ADV -> BS_STOP");
76 return BS_STOP;
77 }
78 else if (role == BLUESYNC_CLIENT_ROLE){
79 LOG_DBG("Transition: BS_ADV -> BS_WAIT_SYNC");
81 }
82 }
83 break;
84 case BS_STOP:
85 if (event == EVENT_NEW_NET_SYNC){
86 LOG_DBG("Transition: BS_STOP -> BS_ADV");
87 return BS_ADV;
88 }
89 break;
90 default:
91 if (event == EVENT_INIT){
93 LOG_DBG("Transition: INIT -> BS_STOP");
94 return BS_STOP;
95 }
96 else if (role == BLUESYNC_CLIENT_ROLE){
97 LOG_DBG("Transition: INIT -> BS_WAIT_SYNC");
99 }
100 }
101 break;
102 }
103
104 return current_state; // No state change
105}
106
108 if (sm_param.handlers == NULL){
109 LOG_ERR("Error: no handlers set...");
110 return;
111 }
112
113 k_mutex_lock(&sm_param.mutex, K_FOREVER);
114 {
116 }
117 k_mutex_unlock(&sm_param.mutex);
118
119
120 // Call the appropriate handler for the current state
121 switch (sm_param.current_state) {
125 }
126 break;
127 case BS_SYNC:
130 }
131 break;
132 case BS_UPDATE:
135 }
136 break;
137 case BS_ADV:
140 }
141 break;
142 case BS_STOP:
145 }
146 break;
147 default:
148 break;
149 }
150}
151
153 k_mutex_lock(&sm_param.mutex, K_FOREVER);
154 {
156 }
157 k_mutex_unlock(&sm_param.mutex);
158}
159
161 LOG_DBG("set role %d",role );
162 k_mutex_lock(&sm_param.mutex, K_FOREVER);
163 {
164
166 switch (sm_param.role)
167 {
170 break;
171
174 break;
175
176 default:
178 break;
179 }
180 }
181 k_mutex_unlock(&sm_param.mutex);
182}
183
187
struct bs_sm_handlers handlers
Definition bluesync.c:586
static struct bs_sm_param sm_param
bs_sm_state_t transition(bluesync_role_t role, bs_sm_state_t current_state, bs_sm_event_t event)
bs_sm_state_t bs_state_machine_get_state()
Get the current state.
void bs_state_machine_init(struct bs_sm_handlers *handlers)
Init the state machine by passing the list of callbacks.
void bs_state_machine_set_role(bluesync_role_t role)
Indicate the role to the state machine.
bluesync_role_t bs_state_machine_get_role()
Get the node role.
void bs_state_machine_run(bs_sm_event_t event)
Annonce an event to the state machine.
LOG_MODULE_REGISTER(bs_state_machine, CONFIG_BLUESYNC_LOG_LEVEL)
bs_sm_state_t
Type definition representing the states.
@ BS_SCAN_WAIT_FOR_SYNC
@ BS_ADV
@ BS_UPDATE
@ BS_STOP
@ BS_NONE_STATE
@ BS_SYNC
bs_sm_event_t
Type definition representing the events.
@ EVENT_ADV_EXPIRED
@ EVENT_SYNC_EXPIRED
@ EVENT_UPDATE_FAILED
@ EVENT_NEW_SYNC_RCV
@ EVENT_NEW_NET_SYNC
@ EVENT_INIT
@ EVENT_UPDATE_SUCCESS
bluesync_role_t
Defines the operational roles for a BlueSync node.
Definition bluesync.h:44
@ BLUESYNC_CLIENT_ROLE
Definition bluesync.h:47
@ BLUESYNC_NONE_ROLE
Definition bluesync.h:45
@ BLUESYNC_AUTHORITY_ROLE
Definition bluesync.h:46
Public API for the BlueSync time synchronization module.
Struct defining the callbacks of the state machine.
void(* bs_adv_cb)(void)
This callback is called when entering in adv state.
void(* bs_stop_cb)(void)
This callback is called when entering in stop state.
void(* bs_sync_cb)()
This callback is called when entering in sync state.
void(* bs_scan_wait_for_sync_cb)(void)
This callback is called when entering in wait_for_sync state.
void(* bs_update_cb)(void)
This callback is called when entering in update state.
bs_sm_state_t current_state
struct bs_sm_handlers * handlers
bluesync_role_t role
struct k_mutex mutex