Skip to content

Commit 9cc4f47

Browse files
app: refactor and align code
Refactor and align code across modules. Signed-off-by: Giacomo Dematteis <giacomo.dematteis@nordicsemi.no>
1 parent 232ea16 commit 9cc4f47

File tree

6 files changed

+219
-251
lines changed

6 files changed

+219
-251
lines changed

app/src/modules/cloud/cloud.c

Lines changed: 82 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,9 @@ ZBUS_CHAN_DEFINE(PRIV_CLOUD_CHAN,
9999
static void backoff_timer_work_fn(struct k_work *work);
100100
static K_WORK_DELAYABLE_DEFINE(backoff_timer_work, backoff_timer_work_fn);
101101

102-
/* Forward declarations of state handlers */
103-
static void state_running_entry(void *o);
104-
static void state_running_run(void *o);
105-
106-
static void state_disconnected_entry(void *o);
107-
static void state_disconnected_run(void *o);
108-
109-
static void state_connecting_entry(void *o);
110-
111-
static void state_connecting_attempt_entry(void *o);
112-
113-
static void state_connecting_backoff_entry(void *o);
114-
static void state_connecting_backoff_run(void *o);
115-
static void state_connecting_backoff_exit(void *o);
116-
117-
static void state_connected_entry(void *o);
118-
static void state_connected_exit(void *o);
119-
120-
static void state_connected_ready_entry(void *o);
121-
static void state_connected_ready_run(void *o);
102+
/* State machine */
122103

123-
static void state_connected_paused_entry(void *o);
124-
static void state_connected_paused_run(void *o);
125-
126-
/* Defining the hierarchical cloud module states: */
104+
/* Cloud module states */
127105
enum cloud_module_state {
128106
/* The cloud module has started and is running */
129107
STATE_RUNNING,
@@ -146,7 +124,47 @@ enum cloud_module_state {
146124
STATE_CONNECTED_PAUSED,
147125
};
148126

149-
/* Construct state table */
127+
/* State object.
128+
* Used to transfer context data between state changes.
129+
*/
130+
struct cloud_state_object {
131+
/* This must be first */
132+
struct smf_ctx ctx;
133+
134+
/* Last channel type that a message was received on */
135+
const struct zbus_channel *chan;
136+
137+
/* Last received message */
138+
uint8_t msg_buf[MAX_MSG_SIZE];
139+
140+
/* Network status */
141+
enum network_msg_type nw_status;
142+
143+
/* Connection attempt counter. Reset when entering STATE_CONNECTING */
144+
uint32_t connection_attempts;
145+
146+
/* Connection backoff time */
147+
uint32_t backoff_time;
148+
};
149+
150+
/* Forward declarations of state handlers */
151+
static void state_running_entry(void *obj);
152+
static void state_running_run(void *obj);
153+
static void state_disconnected_entry(void *obj);
154+
static void state_disconnected_run(void *obj);
155+
static void state_connecting_entry(void *obj);
156+
static void state_connecting_attempt_entry(void *obj);
157+
static void state_connecting_backoff_entry(void *obj);
158+
static void state_connecting_backoff_run(void *obj);
159+
static void state_connecting_backoff_exit(void *obj);
160+
static void state_connected_entry(void *obj);
161+
static void state_connected_exit(void *obj);
162+
static void state_connected_ready_entry(void *obj);
163+
static void state_connected_ready_run(void *obj);
164+
static void state_connected_paused_entry(void *obj);
165+
static void state_connected_paused_run(void *obj);
166+
167+
/* State machine definition */
150168
static const struct smf_state states[] = {
151169
[STATE_RUNNING] =
152170
SMF_CREATE_STATE(state_running_entry, state_running_run, NULL,
@@ -190,39 +208,15 @@ static const struct smf_state states[] = {
190208
NULL),
191209
};
192210

193-
/* Cloud module state object.
194-
* Used to transfer data between state changes.
195-
*/
196-
struct cloud_state {
197-
/* This must be first */
198-
struct smf_ctx ctx;
199-
200-
/* Last channel type that a message was received on */
201-
const struct zbus_channel *chan;
202-
203-
/* Last received message */
204-
uint8_t msg_buf[MAX_MSG_SIZE];
205-
206-
/* Network status */
207-
enum network_msg_type nw_status;
208-
209-
/* Connection attempt counter. Reset when entering STATE_CONNECTING */
210-
uint32_t connection_attempts;
211-
212-
/* Connection backoff time */
213-
uint32_t backoff_time;
214-
};
215-
216-
/* Static helper function */
217-
static void task_wdt_callback(int channel_id, void *user_data)
211+
static void cloud_wdt_callback(int channel_id, void *user_data)
218212
{
219213
LOG_ERR("Watchdog expired, Channel: %d, Thread: %s",
220214
channel_id, k_thread_name_get((k_tid_t)user_data));
221215

222216
SEND_FATAL_ERROR_WATCHDOG_TIMEOUT();
223217
}
224218

225-
static void connect_to_cloud(const struct cloud_state *state_object)
219+
static void connect_to_cloud(const struct cloud_state_object *state_object)
226220
{
227221
int err;
228222
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN];
@@ -286,15 +280,13 @@ static void backoff_timer_work_fn(struct k_work *work)
286280
}
287281
}
288282

289-
/* Zephyr State Machine Framework handlers */
283+
/* State handlers */
290284

291-
/* Handler for STATE_RUNNING */
292-
293-
static void state_running_entry(void *o)
285+
static void state_running_entry(void *obj)
294286
{
295287
int err;
296288

297-
ARG_UNUSED(o);
289+
ARG_UNUSED(obj);
298290

299291
LOG_DBG("%s", __func__);
300292

@@ -307,9 +299,9 @@ static void state_running_entry(void *o)
307299
}
308300
}
309301

310-
static void state_running_run(void *o)
302+
static void state_running_run(void *obj)
311303
{
312-
const struct cloud_state *state_object = (const struct cloud_state *)o;
304+
struct cloud_state_object const *state_object = obj;
313305

314306
if (state_object->chan == &NETWORK_CHAN) {
315307
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
@@ -322,15 +314,14 @@ static void state_running_run(void *o)
322314
}
323315
}
324316

325-
/* Handlers for STATE_DISCONNECTED. */
326-
static void state_disconnected_entry(void *o)
317+
static void state_disconnected_entry(void *obj)
327318
{
328319
int err;
329320
struct cloud_msg cloud_msg = {
330321
.type = CLOUD_DISCONNECTED,
331322
};
332323

333-
ARG_UNUSED(o);
324+
ARG_UNUSED(obj);
334325

335326
LOG_DBG("%s", __func__);
336327

@@ -343,9 +334,9 @@ static void state_disconnected_entry(void *o)
343334
}
344335
}
345336

346-
static void state_disconnected_run(void *o)
337+
static void state_disconnected_run(void *obj)
347338
{
348-
const struct cloud_state *state_object = (const struct cloud_state *)o;
339+
struct cloud_state_object const *state_object = obj;
349340
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
350341

351342
if ((state_object->chan == &NETWORK_CHAN) && (msg.type == NETWORK_CONNECTED)) {
@@ -355,23 +346,19 @@ static void state_disconnected_run(void *o)
355346
}
356347
}
357348

358-
/* Handlers for STATE_CONNECTING */
359-
360-
static void state_connecting_entry(void *o)
349+
static void state_connecting_entry(void *obj)
361350
{
362351
/* Reset connection attempts counter */
363-
struct cloud_state *state_object = o;
352+
struct cloud_state_object *state_object = obj;
364353

365354
LOG_DBG("%s", __func__);
366355

367356
state_object->connection_attempts = 0;
368357
}
369358

370-
/* Handler for STATE_CONNECTING_ATTEMPT */
371-
372-
static void state_connecting_attempt_entry(void *o)
359+
static void state_connecting_attempt_entry(void *obj)
373360
{
374-
struct cloud_state *state_object = o;
361+
struct cloud_state_object *state_object = obj;
375362

376363
LOG_DBG("%s", __func__);
377364

@@ -380,12 +367,10 @@ static void state_connecting_attempt_entry(void *o)
380367
connect_to_cloud(state_object);
381368
}
382369

383-
/* Handler for STATE_CONNECTING_BACKOFF */
384-
385-
static void state_connecting_backoff_entry(void *o)
370+
static void state_connecting_backoff_entry(void *obj)
386371
{
387372
int err;
388-
struct cloud_state *state_object = o;
373+
struct cloud_state_object *state_object = obj;
389374

390375
LOG_DBG("%s", __func__);
391376

@@ -398,9 +383,9 @@ static void state_connecting_backoff_entry(void *o)
398383
}
399384
}
400385

401-
static void state_connecting_backoff_run(void *o)
386+
static void state_connecting_backoff_run(void *obj)
402387
{
403-
const struct cloud_state *state_object = (const struct cloud_state *)o;
388+
struct cloud_state_object const *state_object = obj;
404389

405390
if (state_object->chan == &PRIV_CLOUD_CHAN) {
406391
const enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf;
@@ -413,19 +398,18 @@ static void state_connecting_backoff_run(void *o)
413398
}
414399
}
415400

416-
static void state_connecting_backoff_exit(void *o)
401+
static void state_connecting_backoff_exit(void *obj)
417402
{
418-
ARG_UNUSED(o);
403+
ARG_UNUSED(obj);
419404

420405
LOG_DBG("%s", __func__);
421406

422407
(void)k_work_cancel_delayable(&backoff_timer_work);
423408
}
424409

425-
/* Handler for STATE_CONNECTED. */
426-
static void state_connected_entry(void *o)
410+
static void state_connected_entry(void *obj)
427411
{
428-
ARG_UNUSED(o);
412+
ARG_UNUSED(obj);
429413

430414
LOG_DBG("%s", __func__);
431415
LOG_INF("Connected to Cloud");
@@ -444,11 +428,11 @@ static void state_connected_entry(void *o)
444428
#endif /* CONFIG_MEMFAULT */
445429
}
446430

447-
static void state_connected_exit(void *o)
431+
static void state_connected_exit(void *obj)
448432
{
449433
int err;
450434

451-
ARG_UNUSED(o);
435+
ARG_UNUSED(obj);
452436

453437
LOG_DBG("%s", __func__);
454438

@@ -459,8 +443,6 @@ static void state_connected_exit(void *o)
459443
}
460444
}
461445

462-
/* Handlers for STATE_CONNECTED_READY */
463-
464446
static void shadow_get(bool delta_only)
465447
{
466448
int err;
@@ -531,14 +513,14 @@ static void shadow_get(bool delta_only)
531513
}
532514
}
533515

534-
static void state_connected_ready_entry(void *o)
516+
static void state_connected_ready_entry(void *obj)
535517
{
536518
int err;
537519
struct cloud_msg cloud_msg = {
538520
.type = CLOUD_CONNECTED,
539521
};
540522

541-
ARG_UNUSED(o);
523+
ARG_UNUSED(obj);
542524

543525
LOG_DBG("%s", __func__);
544526

@@ -553,10 +535,10 @@ static void state_connected_ready_entry(void *o)
553535
shadow_get(false);
554536
}
555537

556-
static void state_connected_ready_run(void *o)
538+
static void state_connected_ready_run(void *obj)
557539
{
558540
int err;
559-
const struct cloud_state *state_object = (const struct cloud_state *)o;
541+
struct cloud_state_object const *state_object = obj;
560542
bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
561543

562544
if (state_object->chan == &NETWORK_CHAN) {
@@ -701,14 +683,14 @@ static void state_connected_ready_run(void *o)
701683

702684
/* Handlers for STATE_CONNECTED_PAUSED */
703685

704-
static void state_connected_paused_entry(void *o)
686+
static void state_connected_paused_entry(void *obj)
705687
{
706688
int err;
707689
struct cloud_msg cloud_msg = {
708690
.type = CLOUD_DISCONNECTED,
709691
};
710692

711-
ARG_UNUSED(o);
693+
ARG_UNUSED(obj);
712694

713695
LOG_DBG("%s", __func__);
714696

@@ -721,9 +703,9 @@ static void state_connected_paused_entry(void *o)
721703
}
722704
}
723705

724-
static void state_connected_paused_run(void *o)
706+
static void state_connected_paused_run(void *obj)
725707
{
726-
const struct cloud_state *state_object = (const struct cloud_state *)o;
708+
struct cloud_state_object const *state_object = obj;
727709
struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf);
728710

729711
if ((state_object->chan == &NETWORK_CHAN) && (msg.type == NETWORK_CONNECTED)) {
@@ -733,21 +715,19 @@ static void state_connected_paused_run(void *o)
733715
}
734716
}
735717

736-
/* End of state handlers */
737-
738-
static void cloud_thread(void)
718+
static void cloud_module_thread(void)
739719
{
740720
int err;
741721
int task_wdt_id;
742722
const uint32_t wdt_timeout_ms = (CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
743723
const uint32_t execution_time_ms =
744724
(CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC);
745725
const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
746-
struct cloud_state cloud_state = { 0 };
726+
struct cloud_state_object cloud_state = { 0 };
747727

748-
LOG_DBG("cloud module task started");
728+
LOG_DBG("Cloud module task started");
749729

750-
task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get());
730+
task_wdt_id = task_wdt_add(wdt_timeout_ms, cloud_wdt_callback, (void *)k_current_get());
751731
if (task_wdt_id < 0) {
752732
LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id);
753733
SEND_FATAL_ERROR();
@@ -787,6 +767,6 @@ static void cloud_thread(void)
787767
}
788768
}
789769

790-
K_THREAD_DEFINE(cloud_thread_id,
770+
K_THREAD_DEFINE(cloud_module_thread_id,
791771
CONFIG_APP_CLOUD_THREAD_STACK_SIZE,
792-
cloud_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);
772+
cloud_module_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);

0 commit comments

Comments
 (0)