Example #1
0
/**
  * Implementation (callback-) function of reading object resoures. For whole 
  * object, single resources or a sequence of resources
  * see 3GPP TS 23.032 V11.0.0(2012-09) page 23,24.
  * implemented for: HORIZONTAL_VELOCITY_WITH_UNCERTAINT
  * @param objInstId    in,     instances ID of the location object to read
  * @param numDataP     in/out, ponter to the number of resource to read. 0 is the 
  *                             exeption for all readable resource of onject instance
  * @param tlvArrayP    in/out, TLV data sequnce with intialized resource ID to read
  * @param objectP      in,     private location data structure
  */
static uint8_t prv_location_read(uint16_t objInstId,
                                    int*  numDataP,
                            lwm2m_tlv_t** tlvArrayP,
                         lwm2m_object_t*  objectP) {   
    //------------------------------------------------------------------- JH --
    int     i;
    uint8_t result = COAP_500_INTERNAL_SERVER_ERROR;
    location_data_t* locDataP = (location_data_t*)(objectP->userData);

    // defined as single instance object!
    if (objInstId != 0) return COAP_404_NOT_FOUND;

    if (*numDataP == 0) { // full object, readable resources!
        uint16_t readResIds[] = {0,1,2,3,4,5}; // readable resources!
        *numDataP  = sizeof(readResIds)/sizeof(uint16_t);
        *tlvArrayP = lwm2m_tlv_new(*numDataP);
        if (*tlvArrayP == NULL) 
            return COAP_500_INTERNAL_SERVER_ERROR;
        // init readable resource id's
        for (i=0; i<*numDataP; i++) {
            (*tlvArrayP)[i].id = readResIds[i];
        }
    }
    
    for (i=0; i<*numDataP; i++) {
        result = prv_res2tlv ((*tlvArrayP)+i, locDataP);
        if (result!=COAP_205_CONTENT) 
            break;
    }    
    return result;
}
static uint8_t prv_read(uint16_t instanceId,
                        int * numDataP,
                        lwm2m_tlv_t ** dataArrayP,
                        lwm2m_object_t * objectP)
{
    uint8_t result;
    int i;

    // this is a single instance object
    if (instanceId != 0)
    {
        return COAP_404_NOT_FOUND ;
    }

    // is the server asking for the full object ?
    if (*numDataP == 0)
    {
        uint16_t resList[] = {
                RES_M_NETWORK_BEARER,
                RES_M_AVL_NETWORK_BEARER,
                RES_M_RADIO_SIGNAL_STRENGTH,
                RES_O_LINK_QUALITY,
                RES_M_IP_ADDRESSES,
                RES_O_ROUTER_IP_ADDRESS,
                RES_O_LINK_UTILIZATION,
                RES_O_APN,
                RES_O_CELL_ID,
                RES_O_SMNC,
                RES_O_SMCC
        };
        int nbRes = sizeof(resList) / sizeof(uint16_t);

        *dataArrayP = lwm2m_tlv_new(nbRes);
        if (*dataArrayP == NULL)
            return COAP_500_INTERNAL_SERVER_ERROR ;
        *numDataP = nbRes;
        for (i = 0; i < nbRes; i++)
        {
            (*dataArrayP)[i].id = resList[i];
        }
    }

    i = 0;
    do
    {
        result = prv_set_value((*dataArrayP) + i, (conn_m_data_t*) (objectP->userData));
        i++;
    } while (i < *numDataP && result == COAP_205_CONTENT );

    return result;
}
Example #3
0
static uint8_t prv_security_read(uint16_t instanceId,
                                 int * numDataP,
                                 lwm2m_tlv_t ** dataArrayP,
                                 lwm2m_object_t * objectP)
{
    security_instance_t * targetP;
    uint8_t result;
    int i;

    targetP = (security_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
    if (NULL == targetP) return COAP_404_NOT_FOUND;

    // is the server asking for the full instance ?
    if (*numDataP == 0)
    {
        uint16_t resList[] = {LWM2M_SECURITY_URI_ID,
                              LWM2M_SECURITY_BOOTSTRAP_ID,
                              LWM2M_SECURITY_SECURITY_ID,
                              LWM2M_SECURITY_PUBLIC_KEY_ID,
                              LWM2M_SECURITY_SERVER_PUBLIC_KEY_ID,
                              LWM2M_SECURITY_SECRET_KEY_ID,
                              LWM2M_SECURITY_SMS_SECURITY_ID,
                              LWM2M_SECURITY_SMS_KEY_PARAM_ID,
                              LWM2M_SECURITY_SMS_SECRET_KEY_ID,
                              LWM2M_SECURITY_SMS_SERVER_NUMBER_ID,
                              LWM2M_SECURITY_SHORT_SERVER_ID,
                              LWM2M_SECURITY_HOLD_OFF_ID};
        int nbRes = sizeof(resList)/sizeof(uint16_t);

        *dataArrayP = lwm2m_tlv_new(nbRes);
        if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
        *numDataP = nbRes;
        for (i = 0 ; i < nbRes ; i++)
        {
            (*dataArrayP)[i].id = resList[i];
        }
    }

    i = 0;
    do
    {
        result = prv_get_value((*dataArrayP) + i, targetP);
        i++;
    } while (i < *numDataP && result == COAP_205_CONTENT);

    return result;
}
Example #4
0
static uint8_t prv_server_read(uint16_t instanceId,
                               int * numDataP,
                               lwm2m_tlv_t ** dataArrayP,
                               lwm2m_object_t * objectP)
{
    server_instance_t * targetP;
    uint8_t result;
    int i;

    targetP = (server_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
    if (NULL == targetP) return COAP_404_NOT_FOUND;

    // is the server asking for the full instance ?
    if (*numDataP == 0)
    {
        uint16_t resList[] = {
                LWM2M_SERVER_SHORT_ID_ID,
                LWM2M_SERVER_LIFETIME_ID,
                LWM2M_SERVER_STORING_ID,
                LWM2M_SERVER_BINDING_ID
        };
        int nbRes = sizeof(resList)/sizeof(uint16_t);

        *dataArrayP = lwm2m_tlv_new(nbRes);
        if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
        *numDataP = nbRes;
        for (i = 0 ; i < nbRes ; i++)
        {
            (*dataArrayP)[i].id = resList[i];
        }
    }

    i = 0;
    do
    {
        result = prv_get_value((*dataArrayP) + i, targetP);
        i++;
    } while (i < *numDataP && result == COAP_205_CONTENT);

    return result;
}
Example #5
0
static int prv_getMandatoryInfo(lwm2m_object_t * objectP,
                                uint16_t instanceID,
                                lwm2m_server_t * targetP)
{
    lwm2m_tlv_t * tlvP;
    int size;
    int64_t value;

    size = 2;
    tlvP = lwm2m_tlv_new(size);
    if (tlvP == NULL) return -1;
    tlvP[0].id = LWM2M_SERVER_LIFETIME_ID;
    tlvP[1].id = LWM2M_SERVER_BINDING_ID;


    if (objectP->readFunc(instanceID, &size, &tlvP, objectP) != COAP_205_CONTENT)
    {
        lwm2m_free(tlvP);
        return -1;
    }

    if (0 == lwm2m_tlv_decode_int(tlvP, &value)
     || value < 0 || value >0xFFFFFFFF)             // This is an implementation limit
    {
        lwm2m_free(tlvP);
        return -1;
    }
    targetP->lifetime = value;

    targetP->binding = lwm2m_stringToBinding(tlvP[1].value, tlvP[1].length);

    lwm2m_free(tlvP);

    if (targetP->binding == BINDING_UNKNOWN)
    {
        return -1;
    }

    return 0;
}
static uint8_t prv_accelerometer_read(uint16_t instanceId, int * numDataP, lwm2m_tlv_t ** dataArrayP,
        lwm2m_object_t * objectP) {
    uint8_t result;
    int i;

    // this is a single instance object
    if (instanceId != 0) {
        return COAP_404_NOT_FOUND ;
    }

    // is the server asking for the full object ?
    if (*numDataP == 0) {

        uint16_t resList[] = {
        RES_MIN_RANGE_VALUE,
        RES_MAX_RANCE_VALUE,
        RES_SENSOR_UNITS,
        RES_X_VALUE,
        RES_Y_VALUE,
        RES_Z_VALUE };
        int nbRes = sizeof(resList) / sizeof(uint16_t);

        *dataArrayP = lwm2m_tlv_new(nbRes);
        if (*dataArrayP == NULL)
            return COAP_500_INTERNAL_SERVER_ERROR ;
        *numDataP = nbRes;
        for (i = 0; i < nbRes; i++) {
            (*dataArrayP)[i].id = resList[i];
        }
    }

    i = 0;
    do {
        result = prv_set_value((*dataArrayP) + i, (accelometer_data_t*) (objectP->userData));
        i++;
    } while (i < *numDataP && result == COAP_205_CONTENT );

    return result;
}
Example #7
0
static uint8_t prv_device_read(uint16_t instanceId,
                               int * numDataP,
                               lwm2m_tlv_t ** dataArrayP,
                               lwm2m_object_t * objectP)
{
    uint8_t result;
    int i;

    // this is a single instance object
    if (instanceId != 0)
    {
        return COAP_404_NOT_FOUND;
    }

    // is the server asking for the full object ?
    if (*numDataP == 0)
    {
        uint16_t resList[] = {0, 1, 2, 3, 6, 7, 8, 9, 10, 11, 13, 14, 15};
        int nbRes = sizeof(resList)/sizeof(uint16_t);

        *dataArrayP = lwm2m_tlv_new(nbRes);
        if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
        *numDataP = nbRes;
        for (i = 0 ; i < nbRes ; i++)
        {
            (*dataArrayP)[i].id = resList[i];
        }
    }

    i = 0;
    do
    {
        result = prv_set_value((*dataArrayP) + i, (device_data_t*)(objectP->userData));
        i++;
    } while (i < *numDataP && result == COAP_205_CONTENT);

    return result;
}
Example #8
0
static lwm2m_list_t * prv_findServerInstance(lwm2m_object_t * objectP,
                                             uint16_t shortID)
{
    lwm2m_list_t * instanceP;
    lwm2m_tlv_t * tlvP;
    int size;

    size = 1;
    tlvP = lwm2m_tlv_new(size);
    if (tlvP == NULL) return NULL;
    tlvP->id = LWM2M_SERVER_SHORT_ID_ID;

    instanceP = objectP->instanceList;
    while (instanceP != NULL)
    {
        int64_t value;

        if (objectP->readFunc(instanceP->id, &size, &tlvP, objectP) != COAP_205_CONTENT)
        {
            lwm2m_free(tlvP);
            return NULL;
        }

        if (1 == lwm2m_tlv_decode_int(tlvP, &value))
        {
            if (value == shortID)
            {
                break;
            }
        }
        instanceP = instanceP->next;
    }

    lwm2m_free(tlvP);
    return instanceP;
}
Example #9
0
coap_status_t object_write(lwm2m_context_t * contextP,
                           lwm2m_uri_t * uriP,
                           char * buffer,
                           int length)
{
    coap_status_t result;
    lwm2m_object_t * targetP;
    lwm2m_tlv_t * tlvP = NULL;
    int size = 0;

    targetP = prv_find_object(contextP, uriP->objectId);
    if (NULL == targetP) return NOT_FOUND_4_04;
    if (NULL == targetP->writeFunc) return METHOD_NOT_ALLOWED_4_05;

    if (LWM2M_URI_IS_SET_RESOURCE(uriP))
    {
        size = 1;
        tlvP = lwm2m_tlv_new(size);
        if (tlvP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;

        tlvP->flags = LWM2M_TLV_FLAG_TEXT_FORMAT | LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        tlvP->id = uriP->resourceId;
        tlvP->length = length;
        tlvP->value = buffer;
    }
    else
    {
        size = lwm2m_tlv_parse(buffer, length, &tlvP);
        if (size == 0) return COAP_500_INTERNAL_SERVER_ERROR;
    }
    result = targetP->writeFunc(uriP->instanceId, size, tlvP, targetP);
    lwm2m_tlv_free(size, tlvP);

    return result;
}
Example #10
0
static uint8_t prv_set_value(lwm2m_tlv_t * tlvP,
                             device_data_t * devDataP)
{
    // a simple switch structure is used to respond at the specified resource asked
    switch (tlvP->id)
    {
    case 0:
        tlvP->value = PRV_MANUFACTURER;
        tlvP->length = strlen(PRV_MANUFACTURER);
        tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        return COAP_205_CONTENT;

    case 1:
        tlvP->value = PRV_MODEL_NUMBER;
        tlvP->length = strlen(PRV_MODEL_NUMBER);
        tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        return COAP_205_CONTENT;

    case 2:
        tlvP->value = PRV_SERIAL_NUMBER;
        tlvP->length = strlen(PRV_SERIAL_NUMBER);
        tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        return COAP_205_CONTENT;

    case 3:
        tlvP->value = PRV_FIRMWARE_VERSION;
        tlvP->length = strlen(PRV_FIRMWARE_VERSION);
        tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        return COAP_205_CONTENT;

    case 4:
        return COAP_405_METHOD_NOT_ALLOWED;

    case 5:
        return COAP_405_METHOD_NOT_ALLOWED;

    case 6:
    {
        lwm2m_tlv_t * subTlvP;

        subTlvP = lwm2m_tlv_new(2);

        subTlvP[0].flags = 0;
        subTlvP[0].id = 0;
        subTlvP[0].type = LWM2M_TYPE_RESSOURCE_INSTANCE;
        lwm2m_tlv_encode_int(PRV_POWER_SOURCE_1, subTlvP);
        if (0 == subTlvP[0].length)
        {
            lwm2m_tlv_free(2, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR;
        }

        subTlvP[1].flags = 0;
        subTlvP[1].id = 1;
        subTlvP[1].type = LWM2M_TYPE_RESSOURCE_INSTANCE;
        lwm2m_tlv_encode_int(PRV_POWER_SOURCE_2, subTlvP + 1);
        if (0 == subTlvP[1].length)
        {
            lwm2m_tlv_free(2, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR;
        }

        tlvP->flags = 0;
        tlvP->type = LWM2M_TYPE_MULTIPLE_RESSOURCE;
        tlvP->length = 2;
        tlvP->value = (uint8_t *)subTlvP;

        return COAP_205_CONTENT;
    }

    case 7:
    {
        lwm2m_tlv_t * subTlvP;

        subTlvP = lwm2m_tlv_new(2);

        subTlvP[0].flags = 0;
        subTlvP[0].id = 0;
        subTlvP[0].type = LWM2M_TYPE_RESSOURCE_INSTANCE;
        lwm2m_tlv_encode_int(PRV_POWER_VOLTAGE_1, subTlvP);
        if (0 == subTlvP[0].length)
        {
            lwm2m_tlv_free(2, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR;
        }

        subTlvP[1].flags = 0;
        subTlvP[1].id = 1;
        subTlvP[1].type = LWM2M_TYPE_RESSOURCE_INSTANCE;
        lwm2m_tlv_encode_int(PRV_POWER_VOLTAGE_2, subTlvP + 1);
        if (0 == subTlvP[1].length)
        {
            lwm2m_tlv_free(2, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR;
        }

        tlvP->flags = 0;
        tlvP->type = LWM2M_TYPE_MULTIPLE_RESSOURCE;
        tlvP->length = 2;
        tlvP->value = (uint8_t *)subTlvP;

        return COAP_205_CONTENT;
    }

    case 8:
    {
        lwm2m_tlv_t * subTlvP;

        subTlvP = lwm2m_tlv_new(2);

        subTlvP[0].flags = 0;
        subTlvP[0].id = 0;
        subTlvP[0].type = LWM2M_TYPE_RESSOURCE_INSTANCE;
        lwm2m_tlv_encode_int(PRV_POWER_CURRENT_1, subTlvP);
        if (0 == subTlvP[0].length)
        {
            lwm2m_tlv_free(2, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR;
        }

        subTlvP[1].flags = 0;
        subTlvP[1].id = 1;
        subTlvP[1].type = LWM2M_TYPE_RESSOURCE_INSTANCE;
        lwm2m_tlv_encode_int(PRV_POWER_CURRENT_2, subTlvP + 1);
        if (0 == subTlvP[1].length)
        {
            lwm2m_tlv_free(2, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR;
        }

        tlvP->flags = 0;
        tlvP->type = LWM2M_TYPE_MULTIPLE_RESSOURCE;
        tlvP->length = 2;
        tlvP->value = (uint8_t *)subTlvP;

        return COAP_205_CONTENT;
    }

    case 9:
        lwm2m_tlv_encode_int(PRV_BATTERY_LEVEL, tlvP);
        tlvP->type = LWM2M_TYPE_RESSOURCE;

        if (0 != tlvP->length) return COAP_205_CONTENT;
        else return COAP_500_INTERNAL_SERVER_ERROR;

    case 10:
        lwm2m_tlv_encode_int(PRV_MEMORY_FREE, tlvP);
        tlvP->type = LWM2M_TYPE_RESSOURCE;

        if (0 != tlvP->length) return COAP_205_CONTENT;
        else return COAP_500_INTERNAL_SERVER_ERROR;

    case 11:
        lwm2m_tlv_encode_int(PRV_ERROR_CODE, tlvP);
        tlvP->type = LWM2M_TYPE_RESSOURCE;

        if (0 != tlvP->length) return COAP_205_CONTENT;
        else return COAP_500_INTERNAL_SERVER_ERROR;

    case 12:
        return COAP_405_METHOD_NOT_ALLOWED;

    case 13:
        lwm2m_tlv_encode_int(devDataP->time, tlvP);
        tlvP->type = LWM2M_TYPE_RESSOURCE;

        if (0 != tlvP->length) return COAP_205_CONTENT;
        else return COAP_500_INTERNAL_SERVER_ERROR;

    case 14:
        tlvP->value = devDataP->time_offset;
        tlvP->length = strlen(devDataP->time_offset);
        tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        return COAP_205_CONTENT;

    case 15:
        tlvP->value = PRV_BINDING_MODE;
        tlvP->length = strlen(PRV_BINDING_MODE);
        tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA;
        tlvP->type = LWM2M_TYPE_RESSOURCE;
        return COAP_205_CONTENT;

    default:
        return COAP_404_NOT_FOUND;
    }
}
static uint8_t prv_firmware_read(uint16_t instanceId,
                                 int * numDataP,
                                 lwm2m_tlv_t ** dataArrayP,
                                 lwm2m_object_t * objectP)
{
    int i;
    uint8_t result;
    firmware_data_t * data = (firmware_data_t*)(objectP->userData);

    // this is a single instance object
    if (instanceId != 0)
    {
        return COAP_404_NOT_FOUND;
    }

    // is the server asking for the full object ?
    if (*numDataP == 0)
    {
        *dataArrayP = lwm2m_tlv_new(3);
        if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
        *numDataP = 3;
        (*dataArrayP)[0].id = 3;
        (*dataArrayP)[1].id = 4;
        (*dataArrayP)[2].id = 5;
    }

    i = 0;
    do
    {
        switch ((*dataArrayP)[i].id)
        {
        case 0:
        case 1:
        case 2:
            result = COAP_405_METHOD_NOT_ALLOWED;
            break;

        case 3:
            // firmware update state (int)
            lwm2m_tlv_encode_int(data->state, *dataArrayP + i);
            (*dataArrayP)[i].type = LWM2M_TYPE_RESSOURCE;

            if (0 != (*dataArrayP)[i].length) result = COAP_205_CONTENT;
            else result = COAP_500_INTERNAL_SERVER_ERROR;

            break;

        case 4:
            lwm2m_tlv_encode_int(data->supported, *dataArrayP + i);
            (*dataArrayP)[i].type = LWM2M_TYPE_RESSOURCE;

            if (0 != (*dataArrayP)[i].length) result = COAP_205_CONTENT;
            else result = COAP_500_INTERNAL_SERVER_ERROR;

            break;

        case 5:
            lwm2m_tlv_encode_int(data->result, *dataArrayP + i);
            (*dataArrayP)[i].type = LWM2M_TYPE_RESSOURCE;

            if (0 != (*dataArrayP)[i].length) result = COAP_205_CONTENT;
            else result = COAP_500_INTERNAL_SERVER_ERROR;

            break;

        default:
            result = COAP_404_NOT_FOUND;
        }

        i++;
    } while (i < *numDataP && result == COAP_205_CONTENT);

    return result;
}
Example #12
0
coap_status_t object_read(lwm2m_context_t * contextP,
                          lwm2m_uri_t * uriP,
                          char ** bufferP,
                          int * lengthP)
{
    coap_status_t result;
    lwm2m_object_t * targetP;
    lwm2m_tlv_t * tlvP = NULL;
    int size = 0;

    targetP = prv_find_object(contextP, uriP->objectId);
    if (NULL == targetP) return NOT_FOUND_4_04;
    if (NULL == targetP->readFunc) return METHOD_NOT_ALLOWED_4_05;
    if (targetP->instanceList == NULL)
    {
        // CDCC: Do not need to check the object instance for object which do not need register.
        // this is a single instance object
        if (targetP->needRegister && LWM2M_URI_IS_SET_INSTANCE(uriP) && (uriP->instanceId != 0))
        {
            return COAP_404_NOT_FOUND;
        }
    }
    else
    {
        if (LWM2M_URI_IS_SET_INSTANCE(uriP))
        {
            if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId))
            {
                return COAP_404_NOT_FOUND;
            }
        }
        else
        {
            // multiple object instances read
            lwm2m_list_t * instanceP;
            int i;

            size = 0;
            for (instanceP = targetP->instanceList; instanceP != NULL ; instanceP = instanceP->next)
            {
                size++;
            }

            tlvP = lwm2m_tlv_new(size);
            if (tlvP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;

            result = COAP_205_CONTENT;
            instanceP = targetP->instanceList;
            i = 0;
            while (instanceP != NULL && result == COAP_205_CONTENT)
            {
                result = targetP->readFunc(instanceP->id, (int*)&(tlvP[i].length), (lwm2m_tlv_t **)&(tlvP[i].value), targetP);
                tlvP[i].type = LWM2M_TYPE_OBJECT_INSTANCE;
                tlvP[i].id = instanceP->id;
                i++;
                instanceP = instanceP->next;
            }

            if (result == COAP_205_CONTENT)
            {
                *lengthP = lwm2m_tlv_serialize(size, tlvP, bufferP);
                if (*lengthP == 0) result = COAP_500_INTERNAL_SERVER_ERROR;
            }
            lwm2m_tlv_free(size, tlvP);

            return result;
        }
    }

    // single instance read
    if (LWM2M_URI_IS_SET_RESOURCE(uriP))
    {
        size = 1;
        tlvP = lwm2m_tlv_new(size);
        if (tlvP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;

        tlvP->type = LWM2M_TYPE_RESSOURCE;
        tlvP->flags = LWM2M_TLV_FLAG_TEXT_FORMAT;
        tlvP->id = uriP->resourceId;
    }
    result = targetP->readFunc(uriP->instanceId, &size, &tlvP, targetP);
    if (result == COAP_205_CONTENT)
    {
        if (size == 1
         && tlvP->type == LWM2M_TYPE_RESSOURCE
         && (tlvP->flags && LWM2M_TLV_FLAG_TEXT_FORMAT) != 0 )
        {
            *bufferP = (char *)malloc(tlvP->length);
            if (*bufferP == NULL)
            {
                result = COAP_500_INTERNAL_SERVER_ERROR;
            }
            else
            {
                memcpy(*bufferP, tlvP->value, tlvP->length);
                *lengthP = tlvP->length;
            }
        }
        else
        {
            *lengthP = lwm2m_tlv_serialize(size, tlvP, bufferP);
            if (*lengthP == 0) result = COAP_500_INTERNAL_SERVER_ERROR;
        }
    }
    lwm2m_tlv_free(size, tlvP);

    return result;
}
Example #13
0
int object_getServers(lwm2m_context_t * contextP)
{
    lwm2m_object_t * securityObjP;
    lwm2m_object_t * serverObjP;
    lwm2m_list_t * securityInstP;   // instanceID of the server in the LWM2M Security Object
    int i;

    for (i = 0 ; i < contextP->numObject ; i++)
    {
        if (contextP->objectList[i]->objID == LWM2M_SECURITY_OBJECT_ID)
        {
            securityObjP = contextP->objectList[i];
        }
        else if (contextP->objectList[i]->objID == LWM2M_SERVER_OBJECT_ID)
        {
            serverObjP = contextP->objectList[i];
        }
    }

    securityInstP = securityObjP->instanceList;
    while (securityInstP != NULL)
    {
        lwm2m_tlv_t * tlvP;
        int size;
        lwm2m_server_t * targetP;
        bool isBootstrap;
        int64_t value = 0;

        size = 3;
        tlvP = lwm2m_tlv_new(size);
        if (tlvP == NULL) return -1;
        tlvP[0].id = LWM2M_SECURITY_BOOTSTRAP_ID;
        tlvP[1].id = LWM2M_SECURITY_SHORT_SERVER_ID;
        tlvP[2].id = LWM2M_SECURITY_HOLD_OFF_ID;

        if (securityObjP->readFunc(securityInstP->id, &size, &tlvP, securityObjP) != COAP_205_CONTENT)
        {
            lwm2m_free(tlvP);
            return -1;
        }

        targetP = (lwm2m_server_t *)lwm2m_malloc(sizeof(lwm2m_server_t));
        if (targetP == NULL) return -1;
        memset(targetP, 0, sizeof(lwm2m_server_t));

        if (0 == lwm2m_tlv_decode_bool(tlvP + 0, &isBootstrap))
        {
            lwm2m_free(targetP);
            lwm2m_free(tlvP);
            return -1;
        }

        if (0 == lwm2m_tlv_decode_int(tlvP + 1, &value)
         || value <= 0 || value >0xFFFF)                // 0 is forbidden as a Short Server ID
        {
            lwm2m_free(targetP);
            lwm2m_free(tlvP);
            return -1;
        }
        targetP->shortID = value;

        if (isBootstrap == true)
        {
            if (0 == lwm2m_tlv_decode_int(tlvP + 1, &value)
             || value < 0 || value >0xFFFFFFFF)             // This is an implementation limit
            {
                lwm2m_free(targetP);
                lwm2m_free(tlvP);
                return -1;
            }
            targetP->lifetime = value;

            contextP->bootstrapServerList = (lwm2m_server_t*)LWM2M_LIST_ADD(contextP->bootstrapServerList, targetP);
        }
        else
        {
            lwm2m_list_t * serverInstP;     // instanceID of the server in the LWM2M Server Object

            serverInstP = prv_findServerInstance(serverObjP, targetP->shortID);
            if (serverInstP == NULL)
            {
                lwm2m_free(targetP);
                lwm2m_free(tlvP);
                return -1;
            }
            if (0 != prv_getMandatoryInfo(serverObjP, serverInstP->id, targetP))
            {
                lwm2m_free(targetP);
                lwm2m_free(tlvP);
                return -1;
            }

            contextP->serverList = (lwm2m_server_t*)LWM2M_LIST_ADD(contextP->serverList, targetP);
        }
        lwm2m_free(tlvP);
        securityInstP = securityInstP->next;
    }

    return 0;
}
Example #14
0
void handle_value_changed(lwm2m_context_t * lwm2mH,
                          lwm2m_uri_t * uri,
                          const char * value,
                          size_t valueLength)
{
    lwm2m_object_t * object = prv_find_object(lwm2mH, uri->objectId);

    if (NULL != object)
    {
        if (object->writeFunc != NULL)
        {
            lwm2m_tlv_t * tlvP;
            int result;
            
            tlvP = lwm2m_tlv_new(1);
            if (tlvP == NULL)
            {
                fprintf(stderr, "Internal allocation failure !\n");
                return;
            }
            tlvP->flags = LWM2M_TLV_FLAG_STATIC_DATA | LWM2M_TLV_FLAG_TEXT_FORMAT;
#ifdef LWM2M_BOOTSTRAP
            if (lwm2mH->bsState == BOOTSTRAP_PENDING) {
                tlvP->flags |= LWM2M_TLV_FLAG_BOOTSTRAPPING;
            }
#endif
            tlvP->id = uri->resourceId;
            tlvP->length = valueLength;
            tlvP->value = (uint8_t*) value;

            result = object->writeFunc(uri->instanceId, 1, tlvP, object);
            if (COAP_405_METHOD_NOT_ALLOWED == result)
            {
                switch (uri->objectId)
                {
                case LWM2M_DEVICE_OBJECT_ID:
                    result = device_change(tlvP, object);
                    break;
                default:
                    break;
                }
            }
            
            if (COAP_204_CHANGED != result)
            {
                fprintf(stderr, "Failed to change value!\n");
            }
            else
            {
                fprintf(stderr, "value changed!\n");
                lwm2m_resource_value_changed(lwm2mH, uri);
            }
            lwm2m_tlv_free(1, tlvP);
            return;
        }
        else
        {
            fprintf(stderr, "write not supported for specified resource!\n");
        }
        return;
    }
    else
    {
        fprintf(stderr, "Object not found !\n");
    }
}
Example #15
0
static uint8_t prv_set_value(lwm2m_tlv_t * tlvP,
                             conn_m_data_t * connDataP)
{
    switch (tlvP->id)
    {
    case RES_M_NETWORK_BEARER:
        lwm2m_tlv_encode_int(VALUE_NETWORK_BEARER_GSM, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length) return COAP_205_CONTENT ;
        else return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    case RES_M_AVL_NETWORK_BEARER:
    {
        int riCnt = 1;   // reduced to 1 instance to fit in one block size
        lwm2m_tlv_t * subTlvP;
        subTlvP = lwm2m_tlv_new(riCnt);
        subTlvP[0].flags = 0;
        subTlvP[0].id    = 0;
        subTlvP[0].type  = LWM2M_TYPE_RESOURCE_INSTANCE;
        lwm2m_tlv_encode_int(VALUE_AVL_NETWORK_BEARER_1, subTlvP);
        if (0 == subTlvP[0].length)
        {
            lwm2m_tlv_free(riCnt, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR ;
        }
        lwm2m_tlv_include(subTlvP, riCnt, tlvP);
        return COAP_205_CONTENT ;
    }
        break;

    case RES_M_RADIO_SIGNAL_STRENGTH: //s-int
        lwm2m_tlv_encode_int(connDataP->signalStrength, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length)
            return COAP_205_CONTENT ;
        else
            return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    case RES_O_LINK_QUALITY: //s-int
        lwm2m_tlv_encode_int(connDataP->linkQuality, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length) return COAP_205_CONTENT ;
        else return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    case RES_M_IP_ADDRESSES:
    {
        int ri, riCnt = 1;   // reduced to 1 instance to fit in one block size
        lwm2m_tlv_t* subTlvP = lwm2m_tlv_new(riCnt);
        for (ri=0; ri<riCnt; ri++)
        {
            subTlvP[ri].flags  = LWM2M_TLV_FLAG_STATIC_DATA;
            subTlvP[ri].id     = 0;
            subTlvP[ri].type   = LWM2M_TYPE_RESOURCE_INSTANCE;
            subTlvP[ri].dataType = LWM2M_TYPE_STRING;
            subTlvP[ri].value  = (uint8_t*) connDataP->ipAddresses[ri];
            subTlvP[ri].length = strlen(connDataP->ipAddresses[ri]);
            if (subTlvP[ri].length == 0)
            {
                lwm2m_tlv_free(riCnt, subTlvP);
                return COAP_500_INTERNAL_SERVER_ERROR ;
            }
        }
        lwm2m_tlv_include(subTlvP, riCnt, tlvP);
        return COAP_205_CONTENT ;
    }
        break;

    case RES_O_ROUTER_IP_ADDRESS:
    {
        int ri, riCnt = 1;   // reduced to 1 instance to fit in one block size
        lwm2m_tlv_t* subTlvP = lwm2m_tlv_new(riCnt);
        for (ri=0; ri<riCnt; ri++)
        {
            subTlvP[ri].flags  = LWM2M_TLV_FLAG_STATIC_DATA;
            subTlvP[ri].id     = 0;
            subTlvP[ri].type   = LWM2M_TYPE_RESOURCE_INSTANCE;
            subTlvP[ri].dataType = LWM2M_TYPE_STRING;
            subTlvP[ri].value  = (uint8_t*) connDataP->routerIpAddresses[ri];
            subTlvP[ri].length = strlen(connDataP->routerIpAddresses[ri]);
            if (subTlvP[ri].length == 0)
            {
                lwm2m_tlv_free(riCnt, subTlvP);
                return COAP_500_INTERNAL_SERVER_ERROR ;
            }
        }
        lwm2m_tlv_include(subTlvP, riCnt, tlvP);
        return COAP_205_CONTENT ;
    }
        break;

    case RES_O_LINK_UTILIZATION:
        lwm2m_tlv_encode_int(connDataP->linkUtilization, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length)
            return COAP_205_CONTENT ;
        else
            return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    case RES_O_APN:
    {
        int riCnt = 1;   // reduced to 1 instance to fit in one block size
        lwm2m_tlv_t * subTlvP;
        subTlvP = lwm2m_tlv_new(riCnt);
        subTlvP[0].flags  = LWM2M_TLV_FLAG_STATIC_DATA;
        subTlvP[0].id     = 0;
        subTlvP[0].type   = LWM2M_TYPE_RESOURCE_INSTANCE;
        subTlvP[0].dataType = LWM2M_TYPE_STRING;
        subTlvP[0].value  = (uint8_t*) VALUE_APN_1;
        subTlvP[0].length = strlen(VALUE_APN_1);
        if (0 == subTlvP[0].length)
        {
            lwm2m_tlv_free(riCnt, subTlvP);
            return COAP_500_INTERNAL_SERVER_ERROR ;
        }
        lwm2m_tlv_include(subTlvP, riCnt, tlvP);
        return COAP_205_CONTENT;
    }
        break;

    case RES_O_CELL_ID:
        lwm2m_tlv_encode_int(connDataP->cellId, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length) return COAP_205_CONTENT ;
        else return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    case RES_O_SMNC:
        lwm2m_tlv_encode_int(VALUE_SMNC, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length) return COAP_205_CONTENT ;
        else return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    case RES_O_SMCC:
        lwm2m_tlv_encode_int(VALUE_SMCC, tlvP);
        tlvP->type = LWM2M_TYPE_RESOURCE;
        if (0 != tlvP->length) return COAP_205_CONTENT ;
        else return COAP_500_INTERNAL_SERVER_ERROR ;
        break;

    default:
        return COAP_404_NOT_FOUND ;
    }
}