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;
}
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;
}
/**
 * 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;
}
示例#4
0
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);
}