コード例 #1
0
ファイル: sol-oic-common.c プロジェクト: wanghongjuan/soletta
SOL_API void
sol_oic_payload_debug(struct sol_coap_packet *pkt)
{
    SOL_NULL_CHECK(pkt);

#ifdef HAVE_STDOUT
    struct sol_buffer *buf;
    CborParser parser;
    CborValue root;
    CborError err;
    size_t offset;

    if (!sol_oic_pkt_has_cbor_content(pkt) ||
        !sol_coap_packet_has_payload(pkt)) {
        return;
    }
    if (sol_coap_packet_get_payload(pkt, &buf, &offset) < 0) {
        SOL_DBG("Failed to get packet payload");
        return;
    }

    err = cbor_parser_init(sol_buffer_at(buf, offset),
        buf->used - offset, 0, &parser, &root);
    if (err != CborNoError) {
        SOL_DBG("Failed to get cbor payload");
        return;
    }

    cbor_value_to_pretty(stdout, &root);
    fprintf(stdout, "\n");
#else
    SOL_DBG("Failed to log oic payload: stdout not available");
#endif
}
コード例 #2
0
ファイル: sol-oic-client.c プロジェクト: cmarcelo/soletta
static bool
_parse_server_info_payload(struct sol_oic_server_information *info,
    uint8_t *payload, uint16_t payload_len)
{
    CborParser parser;
    CborError err;
    CborValue root;

    err = cbor_parser_init(payload, payload_len, 0, &parser, &root);

    if (!cbor_value_is_map(&root))
        return false;

    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_DEVICE_NAME,
        &info->device_name))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_SPEC_VERSION,
        &info->spec_version))
        return false;
    if (!sol_cbor_map_get_bytestr_value(&root, SOL_OIC_KEY_DEVICE_ID,
        &info->device_id))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_DATA_MODEL_VERSION,
        &info->data_model_version))
        return false;

    return err == CborNoError;
}
コード例 #3
0
ファイル: tst_tojson.cpp プロジェクト: Subh1994/iotivity
void compareMetaData(QByteArray data, const QString &expected, int otherFlags = 0)
{
    QString decoded;

    // needs to be in in one map, with the entry called "v"
    data = "\xa1\x61v" + data;

    {
        CborParser parser;
        CborValue first;
        CborError err = cbor_parser_init(reinterpret_cast<const quint8 *>(data.constData()), data.length(), 0, &parser, &first);
        QVERIFY2(!err, QByteArrayLiteral(": Got error \"") + cbor_error_string(err) + "\"");

        err = parseOne(&first, &decoded, CborConvertAddMetadata | otherFlags);
        QVERIFY2(!err, QByteArrayLiteral(": Got error \"") + cbor_error_string(err) +
                 "\"; decoded stream:\n" + decoded.toLatin1());

        // check that we consumed everything
        QCOMPARE((void*)first.ptr, (void*)data.constEnd());
    }

    QVERIFY(decoded.startsWith("{\"v\":"));
    QVERIFY(decoded.endsWith('}'));
//    qDebug() << "was" << decoded;

    // extract just the metadata
    static const char needle[] = "\"v$cbor\":{";
    int pos = decoded.indexOf(needle);
    QCOMPARE(pos == -1, expected.isEmpty());
    if (pos != -1) {
        decoded.chop(2);
        decoded = std::move(decoded).mid(pos + strlen(needle));
        QCOMPARE(decoded, expected);
    }
}
コード例 #4
0
ファイル: sol-oic-cbor.c プロジェクト: anselmolsm/soletta
CborError
sol_oic_decode_cbor_repr(struct sol_coap_packet *pkt, struct sol_vector *reprs)
{
    CborParser parser;
    CborError err;
    CborValue root, array, repr;
    uint8_t *payload;
    uint16_t size;
    int payload_type;

    if (sol_coap_packet_get_payload(pkt, &payload, &size) < 0)
        return CborErrorUnknownLength;

    err = cbor_parser_init(payload, size, 0, &parser, &root);
    if (err != CborNoError)
        return err;

    if (!cbor_value_is_array(&root))
        return CborErrorIllegalType;

    err |= cbor_value_enter_container(&root, &array);

    err |= cbor_value_get_int(&array, &payload_type);
    err |= cbor_value_advance_fixed(&array);
    if (err != CborNoError)
        return err;
    if (payload_type != SOL_OIC_PAYLOAD_REPRESENTATION)
        return CborErrorIllegalType;
    if (cbor_value_map_find_value(&array, SOL_OIC_KEY_REPRESENTATION, &repr) != CborNoError)
        return CborErrorIllegalType;

    /* We're done with this CborParser, no need to close the container. */
    return sol_oic_decode_cbor_repr_map(&repr, reprs);
}
コード例 #5
0
ファイル: ocpayloadparse.c プロジェクト: rajesh89s/iotivity
OCStackResult OCParsePayload(OCPayload **outPayload, OCPayloadType payloadType,
        const uint8_t *payload, size_t payloadSize)
{
    OCStackResult result = OC_STACK_MALFORMED_RESPONSE;
    CborError err;

    VERIFY_PARAM_NON_NULL(TAG, outPayload, "Conversion of outPayload failed");
    VERIFY_PARAM_NON_NULL(TAG, payload, "Invalid cbor payload value");

    OIC_LOG_V(INFO, TAG, "CBOR Parsing size: %zu", payloadSize);

    CborParser parser;
    CborValue rootValue;

    err = cbor_parser_init(payload, payloadSize, 0, &parser, &rootValue);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed initializing init value")

    switch(payloadType)
    {
        case PAYLOAD_TYPE_DISCOVERY:
            result = OCParseDiscoveryPayload(outPayload, &rootValue);
            break;
        case PAYLOAD_TYPE_DEVICE:
            result = OCParseDevicePayload(outPayload, &rootValue);
            break;
        case PAYLOAD_TYPE_PLATFORM:
            result = OCParsePlatformPayload(outPayload, &rootValue);
            break;
        case PAYLOAD_TYPE_REPRESENTATION:
            result = OCParseRepPayload(outPayload, &rootValue);
            break;
        case PAYLOAD_TYPE_PRESENCE:
            result = OCParsePresencePayload(outPayload, &rootValue);
            break;
        case PAYLOAD_TYPE_SECURITY:
            result = OCParseSecurityPayload(outPayload, &rootValue);
            break;
        case PAYLOAD_TYPE_RD:
            result = OCRDCborToPayload(&rootValue, outPayload);
            break;
        default:
            OIC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
            result = OC_STACK_INVALID_PARAM;
            break;
    }

    OIC_LOG_V(INFO, TAG, "Finished parse payload, result is %d", result);

exit:
    return result;
}
コード例 #6
0
OCStackResult OCParsePayload(OCPayload** outPayload, OCPayloadType payloadType,
                             const uint8_t* payload, size_t payloadSize)
{
    CborParser parser;
    CborValue rootValue;
    bool err = false;

    OC_LOG_V(INFO, TAG, "CBOR Parsing size: %d", payloadSize);
    if((err = cbor_parser_init(payload, payloadSize, 0, &parser, &rootValue)) != false)
    {
        OC_LOG_V(ERROR, TAG, "CBOR Parser init failed: %d", err);
        return OC_STACK_ERROR;
    }

    OCStackResult result = OC_STACK_ERROR;
    switch(payloadType)
    {
    case PAYLOAD_TYPE_DISCOVERY:
        result = OCParseDiscoveryPayload(outPayload, &rootValue);
        break;
    case PAYLOAD_TYPE_DEVICE:
        result = OCParseDevicePayload(outPayload, &rootValue);
        break;
    case PAYLOAD_TYPE_PLATFORM:
        result = OCParsePlatformPayload(outPayload, &rootValue);
        break;
    case PAYLOAD_TYPE_REPRESENTATION:
        result = OCParseRepPayload(outPayload, &rootValue);
        break;
    case PAYLOAD_TYPE_PRESENCE:
        result = OCParsePresencePayload(outPayload, &rootValue);
        break;
    case PAYLOAD_TYPE_SECURITY:
        result = OCParseSecurityPayload(outPayload, &rootValue);
        break;
    case PAYLOAD_TYPE_RD:
        result = OCRDCborToPayload(&rootValue, outPayload);
        break;
    default:
        OC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
        result = OC_STACK_ERROR;
        break;
    }

    if(result != OC_STACK_OK)
    {
        OC_LOG_V(INFO, TAG, "Finished parse payload, result is %d", result);
    }

    return result;
}
コード例 #7
0
ファイル: sol-oic-client.c プロジェクト: cmarcelo/soletta
static bool
_parse_platform_info_payload(struct sol_oic_platform_information *info,
    uint8_t *payload, uint16_t payload_len)
{
    CborParser parser;
    CborError err;
    CborValue root;

    err = cbor_parser_init(payload, payload_len, 0, &parser, &root);

    if (!cbor_value_is_map(&root))
        return false;

    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_PLATFORM_ID,
        &info->platform_id))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_MANUF_NAME,
        &info->manufacturer_name))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_MANUF_URL,
        &info->manufacturer_url))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_MODEL_NUM,
        &info->model_number))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_MANUF_DATE,
        &info->manufacture_date))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_PLATFORM_VER,
        &info->platform_version))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_OS_VER,
        &info->os_version))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_HW_VER,
        &info->hardware_version))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_FIRMWARE_VER,
        &info->firmware_version))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_SUPPORT_URL,
        &info->support_url))
        return false;
    if (!sol_cbor_map_get_str_value(&root, SOL_OIC_KEY_SYSTEM_TIME,
        &info->system_time))
        return false;

    return err == CborNoError;
}
コード例 #8
0
ファイル: tst_tojson.cpp プロジェクト: Subh1994/iotivity
void tst_ToJson::nonStringKeyMaps()
{
    QFETCH(QByteArray, data);
    QFETCH(QString, expected);

    data = "\xa1" + data + "\1";
    compareOne(data, "{\"" + expected + "\":1}", CborConvertStringifyMapKeys);

    // and verify that they fail if we use CborConvertRequireMapStringKeys
    CborParser parser;
    CborValue first;
    QString decoded;
    cbor_parser_init(reinterpret_cast<const quint8 *>(data.constData()), data.length(), 0, &parser, &first);
    CborError err = parseOne(&first, &decoded, CborConvertRequireMapStringKeys);
    QCOMPARE(err, CborErrorJsonObjectKeyNotString);
}
コード例 #9
0
/*
 * Read in cbor key/values from os_mbuf pointed by m, and fill them
 * into attrs.
 *
 * @param m		Pointer to os_mbuf containing cbor encoded data
 * @param off		Offset into mbuf where cbor data begins
 * @param len		Number of bytes to decode
 * @param attrs		Array of cbor objects to look for.
 *
 * @return		0 on success; non-zero on failure.
 */
int
cbor_read_mbuf_attrs(struct os_mbuf *m, uint16_t off, uint16_t len,
                     const struct cbor_attr_t *attrs)
{
    struct cbor_mbuf_reader cmr;
    struct CborParser parser;
    struct CborValue value;
    CborError err;

    cbor_mbuf_reader_init(&cmr, m, off);
    err = cbor_parser_init(&cmr.r, 0, &parser, &value);
    if (err != CborNoError) {
        return -1;
    }
    return cbor_read_object(&value, attrs);
}
コード例 #10
0
/*
 * Read in cbor key/values from flat buffer pointed by data, and fill them
 * into attrs.
 *
 * @param data		Pointer to beginning of cbor encoded data
 * @param len		Number of bytes in the buffer
 * @param attrs		Array of cbor objects to look for.
 *
 * @return		0 on success; non-zero on failure.
 */
int
cbor_read_flat_attrs(const uint8_t *data, int len,
                     const struct cbor_attr_t *attrs)
{
    struct cbor_buf_reader reader;
    struct CborParser parser;
    struct CborValue value;
    CborError err;

    cbor_buf_reader_init(&reader, data, len);
    err = cbor_parser_init(&reader.r, 0, &parser, &value);
    if (err != CborNoError) {
        return -1;
    }
    return cbor_read_object(&value, attrs);
}
コード例 #11
0
ファイル: tst_tojson.cpp プロジェクト: Subh1994/iotivity
void compareOne_real(const QByteArray &data, const QString &expected, int flags, int line)
{
    compareFailed = true;
    CborParser parser;
    CborValue first;
    CborError err = cbor_parser_init(reinterpret_cast<const quint8 *>(data.constData()), data.length(), 0, &parser, &first);
    QVERIFY2(!err, QByteArray::number(line) + ": Got error \"" + cbor_error_string(err) + "\"");

    QString decoded;
    err = parseOne(&first, &decoded, flags);
    QVERIFY2(!err, QByteArray::number(line) + ": Got error \"" + cbor_error_string(err) +
                   "\"; decoded stream:\n" + decoded.toLatin1());
    QCOMPARE(decoded, expected);

    // check that we consumed everything
    QCOMPARE((void*)first.ptr, (void*)data.constEnd());

    compareFailed = false;
}
コード例 #12
0
ファイル: oc_rep.c プロジェクト: apache/incubator-mynewt-core
uint16_t
oc_parse_rep(const uint8_t *in_payload, uint16_t payload_size,
             oc_rep_t **out_rep)
{
  CborParser parser;
  CborValue root_value, cur_value, map;
  CborError err = CborNoError;
  struct cbor_buf_reader br;

  cbor_buf_reader_init(&br, in_payload, payload_size);
  err |= cbor_parser_init(&br.r, 0, &parser, &root_value);
  if (cbor_value_is_map(&root_value)) {
    err |= cbor_value_enter_container(&root_value, &cur_value);
    *out_rep = 0;
    oc_rep_t **cur = out_rep;
    while (cbor_value_is_valid(&cur_value)) {
      oc_parse_rep_value(&cur_value, cur, &err);
      err |= cbor_value_advance(&cur_value);
      cur = &(*cur)->next;
    }
  } else if (cbor_value_is_array(&root_value)) {
    err |= cbor_value_enter_container(&root_value, &map);
    err |= cbor_value_enter_container(&map, &cur_value);
    *out_rep = 0;
    oc_rep_t **cur = out_rep;
    while (cbor_value_is_valid(&cur_value)) {
      *cur = _alloc_rep();
      (*cur)->type = OBJECT;
      oc_parse_rep_value(&cur_value, &(*cur)->value_object, &err);
      err |= cbor_value_advance(&cur_value);
      (*cur)->next = 0;
      cur = &(*cur)->next;
    }
  }
  return (uint16_t)err;
}
コード例 #13
0
ファイル: ocpayloadparse.c プロジェクト: rzr/iotivity-1
OCStackResult OCParsePayload(OCPayload** outPayload, OCPayloadType payloadType,
        const uint8_t* payload, size_t payloadSize)
{
    CborParser parser;
    CborValue rootValue;
    bool err = false;

    OC_LOG_V(INFO, TAG, "CBOR Parsing size: %d", payloadSize, payload);
    if((err = cbor_parser_init(payload, payloadSize, 0, &parser, &rootValue)) != false)
    {
        OC_LOG_V(ERROR, TAG, "CBOR Parser init failed: %d", err);
        return OC_STACK_ERROR;
    }

    if(!cbor_value_is_array(&rootValue))
    {
        OC_LOG_V(ERROR, TAG, "CBOR payload root object is not an array :%x", rootValue.type);
        return OC_STACK_MALFORMED_RESPONSE;
    }

    CborValue arrayValue;
    // enter the array
    err = err || cbor_value_enter_container(&rootValue, &arrayValue);

    if(err || arrayValue.type != CborMapType)
    {
        OC_LOG_V(ERROR, TAG, "CBOR payload parse failed :%d", err);
        return OC_STACK_MALFORMED_RESPONSE;
    }

    OCStackResult result = OC_STACK_ERROR;
    switch(payloadType)
    {
        case PAYLOAD_TYPE_DISCOVERY:
            result = OCParseDiscoveryPayload(outPayload, &arrayValue);
            break;
        case PAYLOAD_TYPE_DEVICE:
            result = OCParseDevicePayload(outPayload, &arrayValue);
            break;
        case PAYLOAD_TYPE_PLATFORM:
            result = OCParsePlatformPayload(outPayload, &arrayValue);
            break;
        case PAYLOAD_TYPE_REPRESENTATION:
            result = OCParseRepPayload(outPayload, &arrayValue);
            break;
        case PAYLOAD_TYPE_PRESENCE:
            result = OCParsePresencePayload(outPayload, &arrayValue);
            break;
        case PAYLOAD_TYPE_SECURITY:
            result = OCParseSecurityPayload(outPayload, &arrayValue);
            break;
        default:
            OC_LOG_V(ERROR, TAG, "ParsePayload Type default: %d", payloadType);
            result = OC_STACK_ERROR;
            break;
    }

    if(result == OC_STACK_OK)
    {
        err = err || cbor_value_leave_container(&rootValue, &arrayValue);
        if(err != CborNoError)
        {
            return OC_STACK_MALFORMED_RESPONSE;
        }
    }
    else
    {
        OC_LOG_V(INFO, TAG, "Finished parse payload, result is %d", result);
    }

    return result;
}
コード例 #14
0
ファイル: pstatresource.c プロジェクト: Frank-KunLi/iotivity
OCStackResult CBORPayloadToPstat(const uint8_t *cborPayload, const size_t size,
                                 OicSecPstat_t **secPstat)
{
    if (NULL == cborPayload || NULL == secPstat || NULL != *secPstat || 0 == size)
    {
        return OC_STACK_INVALID_PARAM;
    }

    OCStackResult ret = OC_STACK_ERROR;
    *secPstat = NULL;

    CborValue pstatCbor;
    CborParser parser;
    CborError cborFindResult = CborNoError;
    char *strUuid = NULL;
    size_t len = 0;

    cbor_parser_init(cborPayload, size, 0, &parser, &pstatCbor);
    CborValue pstatMap = { .parser = NULL };

    OicSecPstat_t *pstat = NULL;
    cborFindResult = cbor_value_enter_container(&pstatCbor, &pstatMap);
    VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Map.");

    pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
    VERIFY_NON_NULL(TAG, pstat, ERROR);

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ISOP_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_boolean(&pstatMap))
    {
        cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding isOp Value.");
    }

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_DEVICE_ID_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
    {
        cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
        ret = ConvertStrToUuid(strUuid , &pstat->deviceID);
        VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
        OICFree(strUuid );
        strUuid  = NULL;
    }

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
    {
        cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->cm);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
    }

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_TM_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
    {
        cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->tm);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
    }

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
    {
        cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->om);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
    }

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
    {
        pstat->smLen = 1;
        pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
        cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->sm[0]);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");

    }

    cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
    if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
    {
        cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
        VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
        ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
        VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
        OICFree(strUuid );
        strUuid  = NULL;
    }

    *secPstat = pstat;
    ret = OC_STACK_OK;

exit:
    if (CborNoError != cborFindResult)
    {
        OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
        DeletePstatBinData(pstat);
        pstat = NULL;
        *secPstat = NULL;
        ret = OC_STACK_ERROR;
    }

    return ret;
}
コード例 #15
0
static void
nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
{
    struct os_mbuf *rsp;
    const struct mgmt_handler *handler;
    struct nmgr_hdr *rsp_hdr;
    struct nmgr_hdr hdr;
    int off;
    uint16_t len;
    int rc;

    rsp_hdr = NULL;

    rsp = os_msys_get_pkthdr(512, OS_MBUF_USRHDR_LEN(req));
    if (!rsp) {
        rc = os_mbuf_copydata(req, 0, sizeof(hdr), &hdr);
        if (rc < 0) {
            goto err_norsp;
        }
        rsp = req;
        req = NULL;
        goto err;
    }

    /* Copy the request packet header into the response. */
    memcpy(OS_MBUF_USRHDR(rsp), OS_MBUF_USRHDR(req), OS_MBUF_USRHDR_LEN(req));

    off = 0;
    len = OS_MBUF_PKTHDR(req)->omp_len;

    while (off < len) {
        rc = os_mbuf_copydata(req, off, sizeof(hdr), &hdr);
        if (rc < 0) {
            rc = MGMT_ERR_EINVAL;
            goto err_norsp;
        }

        hdr.nh_len = ntohs(hdr.nh_len);

        handler = mgmt_find_handler(ntohs(hdr.nh_group), hdr.nh_id);
        if (!handler) {
            rc = MGMT_ERR_ENOENT;
            goto err;
        }

        /* Build response header apriori.  Then pass to the handlers
         * to fill out the response data, and adjust length & flags.
         */
        rsp_hdr = nmgr_init_rsp(rsp, &hdr);
        if (!rsp_hdr) {
            rc = MGMT_ERR_ENOMEM;
            goto err_norsp;
        }

        cbor_mbuf_reader_init(&nmgr_task_cbuf.reader, req, sizeof(hdr));
        cbor_parser_init(&nmgr_task_cbuf.reader.r, 0,
                         &nmgr_task_cbuf.n_b.parser, &nmgr_task_cbuf.n_b.it);

        if (hdr.nh_op == NMGR_OP_READ) {
            if (handler->mh_read) {
                rc = handler->mh_read(&nmgr_task_cbuf.n_b);
            } else {
                rc = MGMT_ERR_ENOENT;
            }
        } else if (hdr.nh_op == NMGR_OP_WRITE) {
            if (handler->mh_write) {
                rc = handler->mh_write(&nmgr_task_cbuf.n_b);
            } else {
                rc = MGMT_ERR_ENOENT;
            }
        } else {
            rc = MGMT_ERR_EINVAL;
        }

        if (rc != 0) {
            goto err;
        }

        rsp_hdr->nh_len +=
            cbor_encode_bytes_written(&nmgr_task_cbuf.n_b.encoder);
        off += sizeof(hdr) + OS_ALIGN(hdr.nh_len, 4);

        rsp_hdr->nh_len = htons(rsp_hdr->nh_len);
        rc = nmgr_rsp_fragment(nt, rsp_hdr, rsp, req);
        if (rc) {
            goto err;
        }
    }

    os_mbuf_free_chain(rsp);
    os_mbuf_free_chain(req);
    return;
err:
    OS_MBUF_PKTHDR(rsp)->omp_len = rsp->om_len = 0;
    nmgr_send_err_rsp(nt, rsp, &hdr, rc);
    os_mbuf_free_chain(req);
    return;
err_norsp:
    os_mbuf_free_chain(rsp);
    os_mbuf_free_chain(req);
    return;
}
コード例 #16
0
ファイル: psinterface.c プロジェクト: esevan/iotivity
/**
 * Gets the Secure Virtual Database from the Persistent Storage
 *
 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
 * @param data - pointer of the returned Secure Virtual Resource(s)
 * @param size - pointer of the returned size of Secure Virtual Resource(s)
 *
 * @return OCStackResult - result of getting Secure Virtual Resource(s)
 */
OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size)
{
    OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS IN");
    if (!data || *data || !size)
    {
        return OC_STACK_INVALID_PARAM;
    }

    FILE *fp = NULL;
    uint8_t *fsData = NULL;
    size_t fileSize = 0;
    OCStackResult ret = OC_STACK_ERROR;

    OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
    VERIFY_NON_NULL(TAG, ps, ERROR);

    fileSize = GetSVRDatabaseSize(ps);
    OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
    if (fileSize)
    {
        fsData = (uint8_t *) OICCalloc(1, fileSize);
        VERIFY_NON_NULL(TAG, fsData, ERROR);

        fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
        VERIFY_NON_NULL(TAG, fp, ERROR);
        if (ps->read(fsData, 1, fileSize, fp) == fileSize)
        {
            if (rsrcName)
            {
                CborParser parser;  // will be initialized in |cbor_parser_init|
                CborValue cbor;     // will be initialized in |cbor_parser_init|
                cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
                CborValue cborValue = {0};
                CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
                if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
                {
                    cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
                    VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
                    ret = OC_STACK_OK;
                }
                // in case of |else (...)|, svr_data not found
            }
            // return everything in case rsrcName is NULL
            else
            {
                *size = fileSize;
                *data = (uint8_t *) OICCalloc(1, fileSize);
                VERIFY_NON_NULL(TAG, *data, ERROR);
                memcpy(*data, fsData, fileSize);
                ret = OC_STACK_OK;
            }
        }
    }
    OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS OUT");

exit:
    if (fp)
    {
        ps->close(fp);
    }
    OICFree(fsData);
    return ret;
}
コード例 #17
0
ファイル: psinterface.c プロジェクト: esevan/iotivity
/**
 * Updates the Secure Virtual Resource(s) into the Persistent Storage.
 * This function stores cbor-payload of each resource by appending resource name,
 * and empty payload implies deleting the value
 *
 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
 * @param psPayload - pointer of the updated Secure Virtual Resource(s)
 * @param psSize - the updated size of Secure Virtual Resource(s)
 *
 * @return OCStackResult - result of updating Secure Virtual Resource(s)
 */
OCStackResult UpdateSecureResourceInPS(const char *rsrcName, const uint8_t *psPayload, size_t psSize)
{
    OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS IN");
    if (!rsrcName)
    {
        return OC_STACK_INVALID_PARAM;
    }

    size_t dbSize = 0;
    size_t outSize = 0;
    uint8_t *dbData = NULL;
    uint8_t *outPayload = NULL;

    uint8_t *aclCbor = NULL;
    uint8_t *pstatCbor = NULL;
    uint8_t *doxmCbor = NULL;
    uint8_t *amaclCbor = NULL;
    uint8_t *svcCbor = NULL;
    uint8_t *credCbor = NULL;
    uint8_t *pconfCbor = NULL;

    int64_t cborEncoderResult = CborNoError;
    OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
    if (dbData && dbSize)
    {
        size_t aclCborLen = 0;
        size_t pstatCborLen = 0;
        size_t doxmCborLen = 0;
        size_t amaclCborLen = 0;
        size_t svcCborLen = 0;
        size_t credCborLen = 0;
        size_t pconfCborLen = 0;

        // Gets each secure virtual resource from persistent storage
        // this local scoping intended, for destroying large cbor instances after use
        {
            CborParser parser;  // will be initialized in |cbor_parser_init|
            CborValue cbor;     // will be initialized in |cbor_parser_init|
            cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
            CborValue curVal = {0};
            CborError cborFindResult = CborNoError;

            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
            }
            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
            }
            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult,  "Failed Finding DOXM Name Value.");
            }
            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value.");
            }
            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_SVC_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &svcCbor, &svcCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SVC Name Value.");
            }
            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
            }
            cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal);
            if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
            {
                cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL);
                VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value.");
            }
        }

        // Updates the added |psPayload| with the existing secure virtual resource(s)
        // this local scoping intended, for destroying large cbor instances after use
        {
            size_t size = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen
                        + svcCborLen + credCborLen + pconfCborLen + psSize + 255;
            // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending

            outPayload = (uint8_t *) OICCalloc(1, size);
            VERIFY_NON_NULL(TAG, outPayload, ERROR);
            CborEncoder encoder;  // will be initialized in |cbor_parser_init|
            cbor_encoder_init(&encoder, outPayload, size, 0);
            CborEncoder secRsrc;  // will be initialized in |cbor_encoder_create_map|
            cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");

            if (psPayload && psSize)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
            }
            if (strcmp(OIC_JSON_ACL_NAME, rsrcName) && aclCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
            }
            if (strcmp(OIC_JSON_PSTAT_NAME, rsrcName) && pstatCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
            }
            if (strcmp(OIC_JSON_DOXM_NAME, rsrcName) && doxmCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
            }
            if (strcmp(OIC_JSON_AMACL_NAME, rsrcName) && amaclCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, amaclCbor, amaclCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value.");
            }
            if (strcmp(OIC_JSON_SVC_NAME, rsrcName) && svcCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, svcCbor, svcCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
            }
            if (strcmp(OIC_JSON_CRED_NAME, rsrcName) && credCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value.");
            }
            if (strcmp(OIC_JSON_PCONF_NAME, rsrcName) && pconfCborLen)
            {
                cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME));
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name.");
                cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pconfCbor, pconfCborLen);
                VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value.");
            }

            cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
            VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
            outSize = encoder.ptr - outPayload;
        }
    }
    else if (psPayload && psSize)
    {
        size_t size = psSize + 255;
        // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending

        outPayload = (uint8_t *) OICCalloc(1, size);
        VERIFY_NON_NULL(TAG, outPayload, ERROR);
        CborEncoder encoder;  // will be initialized in |cbor_parser_init|
        cbor_encoder_init(&encoder, outPayload, size, 0);
        CborEncoder secRsrc;  // will be initialized in |cbor_encoder_create_map|
        cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");

        cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
        cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");

        cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
        VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
        outSize = encoder.ptr - outPayload;
    }

    if (outPayload && outSize)
    {
        OIC_LOG_V(DEBUG, TAG, "Writting in the file: %zu", outSize);
        OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
        if (ps)
        {
            FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb");
            if (fp)
            {
                size_t numberItems = ps->write(outPayload, 1, outSize, fp);
                if (outSize == numberItems)
                {
                    OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", outSize);
                    ret = OC_STACK_OK;
                }
                else
                {
                    OIC_LOG_V(ERROR, TAG, "Failed writing %zu in the database", numberItems);
                }
                ps->close(fp);
            }
            else
            {
                OIC_LOG(ERROR, TAG, "File open failed.");
            }
        }
    }

    OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS OUT");

exit:
    OICFree(dbData);
    OICFree(outPayload);
    OICFree(aclCbor);
    OICFree(pstatCbor);
    OICFree(doxmCbor);
    OICFree(amaclCbor);
    OICFree(svcCbor);
    OICFree(credCbor);
    OICFree(pconfCbor);
    return ret;
}
コード例 #18
0
ファイル: sol-oic-client.c プロジェクト: rchiossi/soletta
static bool
_parse_server_info_payload(struct sol_oic_server_information *info,
    uint8_t *payload, uint16_t payload_len)
{
    CborParser parser;
    CborError err;
    CborValue root, array, value, map;
    int payload_type;

    err = cbor_parser_init(payload, payload_len, 0, &parser, &root);
    if (err != CborNoError)
        return false;
    if (!cbor_value_is_array(&root))
        return false;

    err |= cbor_value_enter_container(&root, &array);

    err |= cbor_value_get_int(&array, &payload_type);
    err |= cbor_value_advance_fixed(&array);
    if (err != CborNoError)
        return false;
    if (payload_type != SOL_OIC_PAYLOAD_PLATFORM)
        return false;

    if (!cbor_value_is_map(&array))
        return false;

    /* href is intentionally ignored */

    err |= cbor_value_map_find_value(&map, SOL_OIC_KEY_REPRESENTATION, &value);
    if (!cbor_value_is_map(&value))
        return false;

    if (err != CborNoError)
        return false;

    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_PLATFORM_ID, &info->platform_id))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_MANUF_NAME, &info->manufacturer_name))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_MANUF_URL, &info->manufacturer_url))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_MODEL_NUM, &info->model_number))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_MANUF_DATE, &info->manufacture_date))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_PLATFORM_VER, &info->platform_version))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_OS_VER, &info->os_version))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_HW_VER, &info->hardware_version))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_FIRMWARE_VER, &info->firmware_version))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_SUPPORT_URL, &info->support_url))
        return false;
    if (!_cbor_map_get_str_value(&value, SOL_OIC_KEY_SYSTEM_TIME, &info->system_time))
        return false;

    return err == CborNoError;
}