Skip to content

Commit 9b375fe

Browse files
committed
something
1 parent b1c169e commit 9b375fe

File tree

5 files changed

+101
-16
lines changed

5 files changed

+101
-16
lines changed

app/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,6 @@ CONFIG_TASK_WDT_MIN_TIMEOUT=10000
210210
# Device power management
211211
CONFIG_PM_DEVICE=y
212212
CONFIG_PM_DEVICE_SHELL=y
213+
214+
# JSON library
215+
CONFIG_JSON_LIBRARY=y

app/src/main.c

+61-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#define POWER_MSG_SIZE 0
3333
#endif /* CONFIG_APP_POWER */
3434

35+
#define UPDATE_INTERVAL_STRING_FORMAT "{\"config\": {\"update_interval\": %d}}"
36+
3537
/* Register log module */
3638
LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);
3739

@@ -232,6 +234,54 @@ static const struct smf_state states[] = {
232234
};
233235

234236
/* Static helper function */
237+
static int get_update_interval_from_json(const uint8_t *json, size_t json_len, uint64_t *interval_sec)
238+
{
239+
int err;
240+
241+
// #include <zephyr/kernel.h>
242+
// #include <zephyr/data/json.h>
243+
// #include <stdio.h>
244+
245+
// struct config {
246+
// int update_interval;
247+
// };
248+
249+
// // Define the expected JSON structure
250+
// static const struct json_obj_descr config_descr[] = {
251+
// JSON_OBJ_DESCR_PRIM(struct config, update_interval, JSON_TOK_NUMBER),
252+
// };
253+
254+
// struct root {
255+
// struct config config;
256+
// };
257+
258+
// // Define the full JSON structure
259+
// static const struct json_obj_descr root_descr[] = {
260+
// JSON_OBJ_DESCR_OBJECT(struct root, config, config_descr),
261+
// };
262+
263+
// int main(void) {
264+
// const char *json_string = "{\"config\": {\"update_interval\": 30}}";
265+
266+
// struct root parsed_data = {0}; // Initialize struct
267+
268+
// // Parse JSON string
269+
// int ret = json_obj_parse(json_string, strlen(json_string), root_descr,
270+
// ARRAY_SIZE(root_descr), &parsed_data);
271+
272+
// if (ret >= 0) {
273+
// printf("Update Interval: %d\n", parsed_data.config.update_interval);
274+
// } else {
275+
// printf("Failed to parse JSON, error code: %d\n", ret);
276+
// }
277+
278+
// return 0;
279+
// }
280+
281+
282+
return 0;
283+
}
284+
235285
static void task_wdt_callback(int channel_id, void *user_data)
236286
{
237287
LOG_ERR("Watchdog expired, Channel: %d, Thread: %s",
@@ -441,6 +491,7 @@ static void triggering_entry(void *o)
441491

442492
static void triggering_run(void *o)
443493
{
494+
int err;
444495
struct main_state *state_object = o;
445496

446497
if (state_object->chan == &CLOUD_CHAN) {
@@ -454,13 +505,18 @@ static void triggering_run(void *o)
454505
}
455506

456507
if (msg.type == CLOUD_SHADOW_RESPONSE) {
457-
/* Missing: Parse the interval received in the shadow response,
458-
* write to state object and schedule new interval
459-
*/
460508

461-
int err = k_work_reschedule(&trigger_work,
462-
K_SECONDS(state_object->interval_sec));
509+
err = get_update_interval_from_json(msg.response.buffer,
510+
msg.response.buffer_len,
511+
&state_object->interval_sec);
512+
if (err) {
513+
LOG_ERR("json_parse, error: %d", err);
514+
SEND_FATAL_ERROR();
515+
return;
516+
}
463517

518+
err = k_work_reschedule(&trigger_work,
519+
K_SECONDS(state_object->interval_sec));
464520
if (err < 0) {
465521
LOG_ERR("k_work_reschedule, error: %d", err);
466522
SEND_FATAL_ERROR();

app/src/modules/cloud/cloud_module.c

+31-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,37 @@ static void shadow_get(bool delta_only)
491491
return;
492492
}
493493

494-
/* No further processing of shadow is implemented */
494+
/* Distribute the received shadow configuration and report back the adopted
495+
* configurations thereby clearing the shadow delta
496+
*/
497+
struct cloud_msg msg = {
498+
.type = CLOUD_SHADOW_RESPONSE,
499+
};
500+
501+
__BUILD_ASSERT(sizeof(msg.response.buffer) >= sizeof(recv_buf),
502+
"Buffer size must be large enough to hold the received data");
503+
504+
memcpy(msg.response.buffer, recv_buf, recv_buf_len);
505+
msg.response.buffer_len = recv_buf_len;
506+
507+
err = zbus_chan_pub(&CLOUD_CHAN, &msg, K_SECONDS(1));
508+
if (err) {
509+
LOG_ERR("zbus_chan_pub, error: %d", err);
510+
SEND_FATAL_ERROR();
511+
}
512+
513+
err = nrf_cloud_coap_patch("state/reported", NULL,
514+
msg.response.buffer,
515+
msg.response.buffer_len,
516+
COAP_CONTENT_FORMAT_APP_JSON,
517+
true,
518+
NULL,
519+
NULL);
520+
if (err < 0) {
521+
LOG_ERR("Failed to send PATCH request: %d", err);
522+
} else if (err > 0) {
523+
LOG_ERR("Error from the server: %d", err);
524+
}
495525
}
496526

497527
static void state_connected_ready_entry(void *o)

app/src/modules/cloud/cloud_module.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum cloud_msg_type {
4343
struct cloud_msg {
4444
enum cloud_msg_type type;
4545
struct cloud_payload payload;
46+
struct cloud_shadow_response response;
4647
};
4748

4849
#define MSG_TO_CLOUD_MSG(_msg) (*(const struct cloud_msg *)_msg)

tests/module/main/src/main.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,17 @@ static void send_location_search_done(void)
141141
TEST_ASSERT_EQUAL(0, err);
142142
}
143143

144-
/*
145144
static void send_config(uint64_t interval)
146145
{
147-
const struct configuration config = {
148-
.update_interval_present = true,
149-
.update_interval = interval,
146+
const struct cloud_msg msg = {
147+
.type = CLOUD_SHADOW_RESPONSE,
148+
.response.buffer_len = "sometinhg"
150149
};
151150

152-
int err = zbus_chan_pub(&CONFIG_CHAN, &config, K_SECONDS(1));
151+
int err = zbus_chan_pub(&CLOUD_CHAN, &shadow_response, K_SECONDS(1));
153152

154153
TEST_ASSERT_EQUAL(0, err);
155154
}
156-
*/
157155

158156
static void send_cloud_disconnected(void)
159157
{
@@ -214,7 +212,6 @@ void test_button_press_on_disconnected(void)
214212
check_no_events(7200);
215213
}
216214

217-
/*
218215
void test_trigger_interval_change_in_connected(void)
219216
{
220217
send_cloud_connected_ready_to_send();
@@ -230,9 +227,8 @@ void test_trigger_interval_change_in_connected(void)
230227
send_cloud_disconnected();
231228
check_no_events(WEEK_IN_SECONDS);
232229
}
233-
*/
234230

235-
/*
231+
236232
void test_trigger_disconnect_and_connect_when_triggering(void)
237233
{
238234
send_cloud_connected_ready_to_send();
@@ -255,7 +251,6 @@ void test_trigger_disconnect_and_connect_when_triggering(void)
255251
send_cloud_disconnected();
256252
check_no_events(WEEK_IN_SECONDS);
257253
}
258-
*/
259254

260255
/* This is required to be added to each test. That is because unity's
261256
* main may return nonzero, while zephyr's main currently must

0 commit comments

Comments
 (0)