Skip to content

Commit

Permalink
JSON: improve JSON printer
Browse files Browse the repository at this point in the history
add types to json macros, use printing to array for sid_dut

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

#include "sid_error.h"
#include <stdbool.h>
#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/storage/flash_map.h>
Expand All @@ -20,7 +21,6 @@
#include <sid_pal_storage_kv_ifc.h>
#include <sid_pal_crypto_ifc.h>
#include <app_ble_config.h>
#include <json_printer.h>

#if defined(CONFIG_SIDEWALK_SUBGHZ)
#include <app_subGHz_config.h>
Expand All @@ -31,6 +31,23 @@
#include <sid_pal_serial_bus_spi_config.h>
#endif

#include <zephyr/shell/shell.h>
#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 @@ -94,83 +111,35 @@ static void on_sidewalk_event(bool in_isr, void *context)
k_work_submit_to_queue(&sidewalk_dut_work_q, &ctx->sidewalk_event_work);
}

#define sid_msg_type_str(val) \
((char *[]){ "SID_MSG_TYPE_GET", "SID_MSG_TYPE_SET", "SID_MSG_TYPE_NOTIFY", \
"SID_MSG_TYPE_RESPONSE" }[(val)])
#define sid_link_mode_str(val) \
(val) == SID_LINK_MODE_CLOUD ? "SID_LINK_MODE_CLOUD" : \
(val) == SID_LINK_MODE_MOBILE ? "SID_LINK_MODE_MOBILE" : \
"SID_LINK_MODE_INVALID"

void sid_msg_desc_to_JSON(const struct shell *shell, const char *caller,
const struct sid_msg_desc *msg_desc)
{
// clang-format off
JSON_DICT(caller, true, {
JSON_VAL_DICT("sid_msg_desc",
{
JSON_VAL("link_type", msg_desc->link_type, JSON_NEXT);
JSON_VAL("type", msg_desc->type, JSON_NEXT);
JSON_VAL_STR("type_str", sid_msg_type_str(msg_desc->type), JSON_NEXT);
JSON_VAL("link_mode", msg_desc->link_mode, JSON_NEXT);
JSON_VAL_STR("link_mode_str", (sid_link_mode_str(msg_desc->link_mode)), JSON_NEXT);
JSON_VAL("id", msg_desc->id, JSON_NEXT);
JSON_VAL_DICT("msg_desc_attr",
{
JSON_VAL_DICT("rx_attr",
{
JSON_VAL_STR("is_msg_ack", msg_desc->msg_desc_attr.rx_attr.is_msg_ack? "true": "false", JSON_NEXT);
JSON_VAL_STR("is_msg_duplicate", msg_desc->msg_desc_attr.rx_attr.is_msg_duplicate? "true": "false", JSON_NEXT);
JSON_VAL_STR("ack_requested", msg_desc->msg_desc_attr.rx_attr.ack_requested? "true": "false", JSON_NEXT);
JSON_VAL("rssi", msg_desc->msg_desc_attr.rx_attr.rssi, JSON_NEXT);
JSON_VAL("snr", msg_desc->msg_desc_attr.rx_attr.snr, JSON_LAST);
}
, JSON_NEXT);
JSON_VAL_DICT("tx_attr",
{
JSON_VAL_STR("request_ack", msg_desc->msg_desc_attr.tx_attr.request_ack? "True": "False", JSON_NEXT);
JSON_VAL("num_retries", msg_desc->msg_desc_attr.tx_attr.num_retries, JSON_NEXT);
JSON_VAL("ttl_in_seconds", msg_desc->msg_desc_attr.tx_attr.ttl_in_seconds, JSON_LAST);
}
, JSON_LAST);
}
, JSON_LAST);
}
, JSON_LAST);
});
// clang-format on
}

static void on_sidewalk_msg_received(const struct sid_msg_desc *msg_desc, const struct sid_msg *msg,
void *context)
{
struct app_context *ctx = (struct app_context *)context;
if (ctx->shell) {
sid_msg_desc_to_JSON(ctx->shell, "on_msg_received", msg_desc);
}

JSON_PREPARE;
JSON_DICT("on_msg_received", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, true, JSON_LAST); });
JSON_SEND;
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)
{
struct app_context *ctx = (struct app_context *)context;
if (ctx->shell) {
sid_msg_desc_to_JSON(ctx->shell, "on_msg_sent", msg_desc);
}

JSON_PREPARE;
JSON_DICT("on_msg_sent", true,
{ JSON_VAL_sid_msg_desc("sid_msg_desc", msg_desc, false, JSON_LAST); });
JSON_SEND;
LOG_DBG("Sidewalk -> App");
}

static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc *msg_desc,
void *context)
{
struct app_context *ctx = (struct app_context *)context;
if (ctx->shell) {
sid_msg_desc_to_JSON(ctx->shell, "on_send_error", msg_desc);
}

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;
LOG_DBG("Sidewalk -> App: error %d", error);
}

Expand Down
14 changes: 14 additions & 0 deletions samples/template_ble/src/sidewalk_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ static const uint8_t *status_name[] = { "ready", "not ready", "Error", "secure c

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)
{
LOG_DBG("on event, from %s, context %p", in_isr ? "ISR" : "App", context);
Expand All @@ -40,6 +45,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); });
}

static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *context)
Expand All @@ -49,6 +56,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); });
}

static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc *msg_desc,
Expand All @@ -60,6 +69,11 @@ 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);
});
}

static void on_sidewalk_status_changed(const struct sid_status *status, void *context)
Expand Down
13 changes: 13 additions & 0 deletions samples/template_subghz/src/sidewalk_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ static const uint8_t *status_name[] = { "ready", "not ready", "Error", "secure c

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)
{
LOG_DBG("on event, from %s, context %p", in_isr ? "ISR" : "App", context);
Expand All @@ -40,6 +45,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); });
}

static void on_sidewalk_msg_sent(const struct sid_msg_desc *msg_desc, void *context)
Expand All @@ -49,6 +56,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); });
}

static void on_sidewalk_send_error(sid_error_t error, const struct sid_msg_desc *msg_desc,
Expand All @@ -60,6 +69,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);
});
}

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

0 comments on commit f5f16f6

Please sign in to comment.