Exemplo n.º 1
0
void decode(char * buffer,
            size_t buffer_len,
            int indent)
{
    lwm2m_tlv_type_t type;
    uint16_t id;
    size_t dataIndex;
    size_t dataLen;
    int length = 0;
    int result;

    while (0 != (result = lwm2m_decodeTLV(buffer + length, buffer_len - length, &type, &id, &dataIndex, &dataLen)))
    {
        print_indent(indent);
        printf("type: ");
        switch (type)
        {
        case LWM2M_TYPE_OBJECT_INSTANCE:
            printf("LWM2M_TYPE_OBJECT_INSTANCE\r\n");
            break;
        case LWM2M_TYPE_RESOURCE_INSTANCE:
            printf("LWM2M_TYPE_RESOURCE_INSTANCE\r\n");
            break;
        case LWM2M_TYPE_MULTIPLE_RESOURCE:
            printf("LWM2M_TYPE_MULTIPLE_RESOURCE\r\n");
            break;
        case LWM2M_TYPE_RESOURCE:
            printf("LWM2M_TYPE_RESOURCE\r\n");
            break;
        default:
            printf("unknown (%d)\r\n", (int)type);
            break;
        }
        print_indent(indent);
        printf("id: %d\r\n", id);
        print_indent(indent);
        printf("data (%d bytes): ", dataLen);
        prv_output_buffer(buffer + length + dataIndex, dataLen);
        if (type == LWM2M_TYPE_OBJECT_INSTANCE || type == LWM2M_TYPE_MULTIPLE_RESOURCE)
        {
            decode(buffer + length + dataIndex, dataLen, indent+1);
        }
        else if (dataLen <= 8)
        {
            int64_t value;
            if (0 != lwm2m_opaqueToInt(buffer + length + dataIndex, dataLen, &value))
            {
                print_indent(indent);
                printf("  as int: %ld\r\n", value);
            }
        }

        length += result;
    }
}
Exemplo n.º 2
0
static uint8_t prv_create(lwm2m_uri_t * uriP,
                          char * buffer,
                          int length,
                          lwm2m_object_t * objectP)
{
    prv_instance_t * targetP;
    lwm2m_tlv_type_t type;
    uint16_t newId;
    uint16_t resID;
    size_t dataIndex;
    size_t dataLen;
    int result;
    int64_t value;

    if (LWM2M_URI_IS_SET_INSTANCE(uriP))
    {
        targetP = (prv_instance_t *)lwm2m_list_find(objectP->instanceList, uriP->instanceId);
        if (targetP != NULL) return COAP_406_NOT_ACCEPTABLE;
        newId = uriP->instanceId;
    }
    else
    {
        // determine a new unique ID
        newId = lwm2m_list_newId(objectP->instanceList);
    }

    result = lwm2m_decodeTLV(buffer, length, &type, &resID, &dataIndex, &dataLen);
    if (result != length)
    {
        // decode failure or too much data for our single ressource object
        return COAP_400_BAD_REQUEST;
    }
    if (type != TLV_RESSOURCE || resID != 1)
    {
        return COAP_400_BAD_REQUEST;
    }
    result = lwm2m_opaqueToInt(buffer + dataIndex, dataLen, &value);
    if (result == 0 || value < 0 || value > 255)
        return COAP_400_BAD_REQUEST;

    targetP = (prv_instance_t *)malloc(sizeof(prv_instance_t));
    if (NULL == targetP) return COAP_500_INTERNAL_SERVER_ERROR;
    memset(targetP, 0, sizeof(prv_instance_t));
    targetP->shortID = newId;
    targetP->test = value;
    objectP->instanceList = LWM2M_LIST_ADD(objectP->instanceList, targetP);

    return COAP_201_CREATED;
}
Exemplo n.º 3
0
static void output_tlv(uint8_t * buffer,
                       size_t buffer_len,
                       int indent)
{
    lwm2m_tlv_type_t type;
    uint16_t id;
    size_t dataIndex;
    size_t dataLen;
    int length = 0;
    int result;

    while (0 != (result = lwm2m_decodeTLV((uint8_t*)buffer + length, buffer_len - length, &type, &id, &dataIndex, &dataLen)))
    {
        print_indent(indent);
        fprintf(stdout, "ID: %d", id);
        fprintf(stdout, "  type: ");
        switch (type)
        {
        case LWM2M_TYPE_OBJECT_INSTANCE:
            fprintf(stdout, "Object Instance");
            break;
        case LWM2M_TYPE_RESOURCE_INSTANCE:
            fprintf(stdout, "Resource Instance");
            break;
        case LWM2M_TYPE_MULTIPLE_RESOURCE:
            fprintf(stdout, "Multiple Instances");
            break;
        case LWM2M_TYPE_RESOURCE:
            fprintf(stdout, "Resource");
            break;
        default:
            printf("unknown (%d)", (int)type);
            break;
        }
        fprintf(stdout, "\n");
        print_indent(indent);
        fprintf(stdout, "{\n");
        if (type == LWM2M_TYPE_OBJECT_INSTANCE || type == LWM2M_TYPE_MULTIPLE_RESOURCE)
        {
            output_tlv(buffer + length + dataIndex, dataLen, indent+2);
        }
        else
        {
            int64_t intValue;
            double floatValue;

            print_indent(indent+2);
            fprintf(stdout, "data (%ld bytes):  ", dataLen);
            if (dataLen >= 16) fprintf(stdout, "\n");
            output_buffer(stdout, (uint8_t*)buffer + length + dataIndex, dataLen);
            if (0 < lwm2m_opaqueToInt(buffer + length + dataIndex, dataLen, &intValue))
            {
                print_indent(indent+2);
                fprintf(stdout, "data as Integer: %" PRId64 "\r\n", intValue);
            }
            if (0 < lwm2m_opaqueToFloat(buffer + length + dataIndex, dataLen, &floatValue))
            {
                print_indent(indent+2);
                fprintf(stdout, "data as Float: %.16g\r\n", floatValue);
            }
        }
        print_indent(indent);
        fprintf(stdout, "}\n");
        length += result;
    }
}