int imgr_core_list(struct nmgr_jbuf *njb) { const struct flash_area *fa; struct coredump_header hdr; struct json_encoder *enc; struct json_value jv; int rc; rc = flash_area_open(FLASH_AREA_CORE, &fa); if (rc) { rc = NMGR_ERR_EINVAL; } else { rc = flash_area_read(fa, 0, &hdr, sizeof(hdr)); if (rc != 0) { rc = NMGR_ERR_EINVAL; } else if (hdr.ch_magic != COREDUMP_MAGIC) { rc = NMGR_ERR_ENOENT; } else { rc = 0; } } enc = &njb->njb_enc; json_encode_object_start(enc); JSON_VALUE_INT(&jv, rc); json_encode_object_entry(enc, "rc", &jv); json_encode_object_finish(enc); return 0; }
int imgr_boot_read(struct nmgr_jbuf *njb) { int rc; struct json_encoder *enc; struct image_version ver; struct json_value jv; uint8_t hash[IMGMGR_HASH_LEN]; enc = &njb->njb_enc; json_encode_object_start(enc); rc = boot_vect_read_test(&ver); if (!rc) { imgr_ver_jsonstr(enc, "test", &ver); } rc = boot_vect_read_main(&ver); if (!rc) { imgr_ver_jsonstr(enc, "main", &ver); } rc = imgr_read_info(bsp_imgr_current_slot(), &ver, hash); if (!rc) { imgr_ver_jsonstr(enc, "active", &ver); } JSON_VALUE_INT(&jv, NMGR_ERR_EOK); json_encode_object_entry(enc, "rc", &jv); json_encode_object_finish(enc); return 0; }
/** * Log encode function * @param log structure, the encoder, json_value, * timestamp, index * @return 0 on success; non-zero on failure */ static int log_encode(struct log *log, struct json_encoder *encoder, struct json_value *jv, int64_t ts, uint32_t index) { int rc; json_encode_object_start(encoder); JSON_VALUE_STRING(jv, log->l_name); json_encode_object_entry(encoder, "name", jv); JSON_VALUE_UINT(jv, log->l_log->log_type); json_encode_object_entry(encoder, "type", jv); rc = log_encode_entries(log, encoder, ts, index); json_encode_object_finish(encoder); return rc; }
static void imgr_hash_jsonstr(struct json_encoder *enc, char *key, uint8_t *hash) { struct json_value jv; char hash_str[IMGMGR_HASH_STR + 1]; base64_encode(hash, IMGMGR_HASH_LEN, hash_str, 1); JSON_VALUE_STRING(&jv, hash_str); json_encode_object_entry(enc, key, &jv); }
static void imgr_ver_jsonstr(struct json_encoder *enc, char *key, struct image_version *ver) { struct json_value jv; char ver_str[IMGMGR_NMGR_MAX_VER]; int ver_len; ver_len = imgr_ver_str(ver, ver_str); JSON_VALUE_STRINGN(&jv, ver_str, ver_len); json_encode_object_entry(enc, key, &jv); }
static int imgr_list(struct nmgr_jbuf *njb) { struct image_version ver; int i; int rc; struct json_encoder *enc; struct json_value array; struct json_value versions[4]; struct json_value *version_ptrs[4]; char vers_str[4][IMGMGR_NMGR_MAX_VER]; int ver_len; int cnt = 0; for (i = FLASH_AREA_IMAGE_0; i <= FLASH_AREA_IMAGE_1; i++) { rc = imgr_read_ver(i, &ver); if (rc != 0) { continue; } ver_len = imgr_ver_str(&ver, vers_str[cnt]); JSON_VALUE_STRINGN(&versions[cnt], vers_str[cnt], ver_len); version_ptrs[cnt] = &versions[cnt]; cnt++; } array.jv_type = JSON_VALUE_TYPE_ARRAY; array.jv_len = cnt; array.jv_val.composite.values = version_ptrs; enc = &njb->njb_enc; json_encode_object_start(enc); json_encode_object_entry(enc, "images", &array); json_encode_object_finish(enc); return 0; }
/** * Log encode entry * @param log structure, arg:struct passed locally, dataptr, len * @return 0 on success; non-zero on failure */ static int log_nmgr_encode_entry(struct log *log, void *arg, void *dptr, uint16_t len) { struct encode_off *encode_off = (struct encode_off *)arg; struct log_entry_hdr ueh; char data[128]; int dlen; struct json_value jv; int rc; int rsp_len; rc = log_read(log, dptr, &ueh, 0, sizeof(ueh)); if (rc != sizeof(ueh)) { rc = OS_ENOENT; goto err; } rc = OS_OK; /* Matching timestamps and indices for sending a log entry */ if (ueh.ue_ts < encode_off->eo_ts || (ueh.ue_ts == encode_off->eo_ts && ueh.ue_index <= encode_off->eo_index)) { goto err; } dlen = min(len-sizeof(ueh), 128); rc = log_read(log, dptr, data, sizeof(ueh), dlen); if (rc < 0) { rc = OS_ENOENT; goto err; } data[rc] = 0; rsp_len = encode_off->rsp_len; /* Calculating entry len */ rsp_len += strlen(data); /* Pre calculating MAX length of the json string */ rsp_len += (sizeof(STR(INT64_MAX)) + sizeof("{,ts:") + sizeof(STR(UINT8_MAX)) + sizeof(",level:") + sizeof(STR(UINT32_MAX)) + sizeof(",index:") + sizeof(STR(UINT16_MAX)) + sizeof(",module:}")); if (rsp_len > NMGR_MAX_MTU) { rc = OS_ENOMEM; goto err; } json_encode_object_start(encode_off->eo_encoder); JSON_VALUE_STRINGN(&jv, data, rc); rc = json_encode_object_entry(encode_off->eo_encoder, "msg", &jv); if (rc) { goto err; } JSON_VALUE_INT(&jv, ueh.ue_ts); rc = json_encode_object_entry(encode_off->eo_encoder, "ts", &jv); if (rc) { goto err; } JSON_VALUE_UINT(&jv, ueh.ue_level); rc = json_encode_object_entry(encode_off->eo_encoder, "level", &jv); if (rc) { goto err; } JSON_VALUE_UINT(&jv, ueh.ue_index); rc = json_encode_object_entry(encode_off->eo_encoder, "index", &jv); if (rc) { goto err; } JSON_VALUE_UINT(&jv, ueh.ue_module); rc = json_encode_object_entry(encode_off->eo_encoder, "module", &jv); if (rc) { goto err; } json_encode_object_finish(encode_off->eo_encoder); encode_off->rsp_len += rsp_len; return (0); err: return (rc); }
static int json_encode_value(struct json_encoder *encoder, struct json_value *jv) { int rc; int i; int len; switch (jv->jv_type) { case JSON_VALUE_TYPE_BOOL: len = sprintf(encoder->je_encode_buf, "%s", jv->jv_val.u > 0 ? "true" : "false"); encoder->je_write(encoder->je_arg, encoder->je_encode_buf, len); break; case JSON_VALUE_TYPE_UINT64: len = sprintf(encoder->je_encode_buf, "%lu", (unsigned long)jv->jv_val.u); encoder->je_write(encoder->je_arg, encoder->je_encode_buf, len); break; case JSON_VALUE_TYPE_INT64: len = sprintf(encoder->je_encode_buf, "%ld", (long) jv->jv_val.u); encoder->je_write(encoder->je_arg, encoder->je_encode_buf, len); break; case JSON_VALUE_TYPE_STRING: encoder->je_write(encoder->je_arg, "\"", sizeof("\"")-1); for (i = 0; i < jv->jv_len; i++) { switch (jv->jv_val.str[i]) { case '"': case '/': case '\\': encoder->je_write(encoder->je_arg, "\\", sizeof("\\")-1); encoder->je_write(encoder->je_arg, (char *) &jv->jv_val.str[i], 1); break; case '\t': encoder->je_write(encoder->je_arg, "\\t", sizeof("\\t")-1); break; case '\r': encoder->je_write(encoder->je_arg, "\\r", sizeof("\\r")-1); break; case '\n': encoder->je_write(encoder->je_arg, "\\n", sizeof("\\n")-1); break; case '\f': encoder->je_write(encoder->je_arg, "\\f", sizeof("\\f")-1); break; case '\b': encoder->je_write(encoder->je_arg, "\\b", sizeof("\\b")-1); break; default: encoder->je_write(encoder->je_arg, (char *) &jv->jv_val.str[i], 1); break; } } encoder->je_write(encoder->je_arg, "\"", sizeof("\"")-1); break; case JSON_VALUE_TYPE_ARRAY: JSON_ENCODE_ARRAY_START(encoder); for (i = 0; i < jv->jv_len; i++) { rc = json_encode_value(encoder, jv->jv_val.composite.values[i]); if (rc != 0) { goto err; } if (i != jv->jv_len - 1) { encoder->je_write(encoder->je_arg, ",", sizeof(",")-1); } } JSON_ENCODE_ARRAY_END(encoder); break; case JSON_VALUE_TYPE_OBJECT: JSON_ENCODE_OBJECT_START(encoder); for (i = 0; i < jv->jv_len; i++) { rc = json_encode_object_entry(encoder, jv->jv_val.composite.keys[i], jv->jv_val.composite.values[i]); if (rc != 0) { goto err; } } JSON_ENCODE_OBJECT_END(encoder); break; default: rc = -1; goto err; } return (0); err: return (rc); }