Skip to content

Commit

Permalink
shell: improve JSON module
Browse files Browse the repository at this point in the history
new module is a preprocessor macro that generate printf arguments.

It is lighter because it does not require any aditional buffor to format

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed Oct 24, 2023
1 parent f5f16f6 commit de1124a
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 297 deletions.
45 changes: 18 additions & 27 deletions samples/sid_dut/src/sid_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "sid_error.h"
#include "zephyr/sys/printk.h"
#include <stdbool.h>
#include <stdio.h>
#include <zephyr/kernel.h>
Expand Down Expand Up @@ -35,19 +36,6 @@
#include <json_printer.h>
#include <sidTypes2Json.h>

#undef JSON_RAW_PRINT
#define JSON_MESSAGE_CAPACITY 512
char JSON_MESSAGE[JSON_MESSAGE_CAPACITY + 1] = { 0 };
size_t JSON_CHARS_WRITTEN = 0;
#define JSON_PREPARE memset(JSON_MESSAGE, 0, JSON_MESSAGE_CAPACITY)

#define JSON_SEND \
LOG_RAW("%s", JSON_MESSAGE); \
JSON_CHARS_WRITTEN = 0
#define JSON_RAW_PRINT(format, ...) \
JSON_RAW_PRINTER_STRING(JSON_MESSAGE, JSON_CHARS_WRITTEN, JSON_MESSAGE_CAPACITY, \
format __VA_OPT__(, ) __VA_ARGS__)

#if !FIXED_PARTITION_EXISTS(mfg_storage)
#error "Flash partition is not defined for the Sidewalk manufacturing storage!!"
#endif
Expand Down Expand Up @@ -114,32 +102,35 @@ static void on_sidewalk_event(bool in_isr, void *context)
static void on_sidewalk_msg_received(const struct sid_msg_desc *msg_desc, const struct sid_msg *msg,
void *context)
{
JSON_PREPARE;
JSON_DICT("on_msg_received", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, true, JSON_LAST); });
JSON_SEND;
struct app_context *ctx = (struct app_context *)context;
shell_fprintf(ctx->shell, SHELL_NORMAL,
JSON_NEW_LINE(JSON_OBJ(
JSON_NAME("on_msg_received",
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 1)))));
LOG_DBG("Sidewalk -> App");
LOG_HEXDUMP_INF(msg->data, msg->size, "");
}

static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *context)
{
JSON_PREPARE;
JSON_DICT("on_msg_sent", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST); });
JSON_SEND;
struct app_context *ctx = (struct app_context *)context;
shell_fprintf(ctx->shell, SHELL_NORMAL,
JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_msg_sent",
JSON_OBJ(JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 0))))));
LOG_DBG("Sidewalk -> App");
}

static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc *msg_desc,
void *context)
{
JSON_PREPARE;
JSON_DICT("on_send_error", true, {
JSON_VAL_sid_error_t("error", error, JSON_NEXT);
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST);
});
JSON_SEND;
struct app_context *ctx = (struct app_context *)context;
shell_fprintf(ctx->shell, SHELL_NORMAL,
JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_send_error",
JSON_OBJ(JSON_LIST_2(JSON_VAL_sid_error_t("error", error),
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc,
0)))))));
LOG_DBG("Sidewalk -> App: error %d", error);
}

Expand Down
19 changes: 9 additions & 10 deletions samples/template_ble/src/sidewalk_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ static const uint8_t *link_mode_idx_name[] = { "ble", "fsk", "lora" };

#include <json_printer.h>
#include <sidTypes2Json.h>
#undef JSON_RAW_PRINT
#define JSON_RAW_PRINT(format, ...) LOG_RAW(format __VA_OPT__(, ) __VA_ARGS__)

static void on_sidewalk_event(bool in_isr, void *context)
{
Expand All @@ -45,8 +43,9 @@ static void on_sidewalk_msg_received(const struct sid_msg_desc *msg_desc, const
LOG_DBG("received message(type: %d, link_mode: %d, id: %u size %u)", (int)msg_desc->type,
(int)msg_desc->link_mode, msg_desc->id, msg->size);
LOG_HEXDUMP_INF((uint8_t *)msg->data, msg->size, "Message data: ");
JSON_DICT("on_msg_received", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, true, JSON_LAST); });

printk(JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_msg_received", JSON_OBJ(JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 1))))));
}

static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *context)
Expand All @@ -56,8 +55,8 @@ static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *cont
CLI_register_message_send();
#endif
LOG_INF("sent message(type: %d, id: %u)", (int)msg_desc->type, msg_desc->id);
JSON_DICT("on_msg_sent", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST); });
printk(JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_msg_sent", JSON_OBJ(JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 0))))));
}

static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc *msg_desc,
Expand All @@ -70,10 +69,10 @@ static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc
LOG_ERR("failed to send message(type: %d, id: %u), err:%d", (int)msg_desc->type,
msg_desc->id, (int)error);

JSON_DICT("on_send_error", true, {
JSON_VAL_sid_error_t("error", error, JSON_NEXT);
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST);
});
printk(JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_send_error",
JSON_OBJ(JSON_LIST_2(JSON_VAL_sid_error_t("error", error),
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 0)))))));
}

static void on_sidewalk_status_changed(const struct sid_status *status, void *context)
Expand Down
17 changes: 9 additions & 8 deletions samples/template_subghz/src/sidewalk_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static const uint8_t *link_mode_idx_name[] = { "ble", "fsk", "lora" };

#include <json_printer.h>
#include <sidTypes2Json.h>

#undef JSON_RAW_PRINT
#define JSON_RAW_PRINT(format, ...) LOG_RAW(format __VA_OPT__(, ) __VA_ARGS__)

Expand All @@ -45,8 +46,8 @@ static void on_sidewalk_msg_received(const struct sid_msg_desc *msg_desc, const
LOG_DBG("received message(type: %d, link_mode: %d, id: %u size %u)", (int)msg_desc->type,
(int)msg_desc->link_mode, msg_desc->id, msg->size);
LOG_HEXDUMP_INF((uint8_t *)msg->data, msg->size, "Message data: ");
JSON_DICT("on_msg_received", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, true, JSON_LAST); });
printk(JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_msg_received", JSON_OBJ(JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 1))))));
}

static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *context)
Expand All @@ -56,8 +57,8 @@ static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *cont
CLI_register_message_send();
#endif
LOG_INF("sent message(type: %d, id: %u)", (int)msg_desc->type, msg_desc->id);
JSON_DICT("on_msg_sent", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST); });
printk(JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_msg_sent", JSON_OBJ(JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 0))))));
}

static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc *msg_desc,
Expand All @@ -69,10 +70,10 @@ static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc
#endif
LOG_ERR("failed to send message(type: %d, id: %u), err:%d", (int)msg_desc->type,
msg_desc->id, (int)error);
JSON_DICT("on_send_error", true, {
JSON_VAL_sid_error_t("error", error, JSON_NEXT);
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST);
});
printk(JSON_NEW_LINE(JSON_OBJ(JSON_NAME(
"on_send_error",
JSON_OBJ(JSON_LIST_2(JSON_VAL_sid_error_t("error", error),
JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, 0)))))));
}

static void on_sidewalk_status_changed(const struct sid_status *status, void *context)
Expand Down
Loading

0 comments on commit de1124a

Please sign in to comment.