Example #1
0
int ICACHE_FLASH_ATTR
object_getServers(lwm2m_context_t * contextP)
{
    lwm2m_object_t * securityObjP = NULL;
    lwm2m_object_t * serverObjP = NULL;
    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];
        }
    }

    if (NULL == securityObjP) return -1;

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

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

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

        targetP = (lwm2m_server_t *)lwm2m_malloc(sizeof(lwm2m_server_t));
        if (targetP == NULL) {
            lwm2m_data_free(size, dataP);
            return -1;
        }
        c_memset(targetP, 0, sizeof(lwm2m_server_t));
        targetP->secObjInstID = securityInstP->id;

        if (0 == lwm2m_data_decode_bool(dataP + 0, &isBootstrap))
        {
            lwm2m_free(targetP);
            lwm2m_data_free(size, dataP);
            return -1;
        }

        if (0 == lwm2m_data_decode_int(dataP + 1, &value)
         || value < (isBootstrap ? 0 : 1) || value > 0xFFFF)                // 0 is forbidden as a Short Server ID
        {
            lwm2m_free(targetP);
            lwm2m_data_free(size, dataP);
            return -1;
        }
        targetP->shortID = value;

        if (isBootstrap == true)
        {
            if (0 == lwm2m_data_decode_int(dataP + 2, &value)
             || value < 0 || value > 0xFFFFFFFF)             // This is an implementation limit
            {
                lwm2m_free(targetP);
                lwm2m_data_free(size, dataP);
                return -1;
            }
            // lifetime of a bootstrap server is set to ClientHoldOffTime
            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_data_free(size, dataP);
                return -1;
            }
            if (0 != prv_getMandatoryInfo(serverObjP, serverInstP->id, targetP))
            {
                lwm2m_free(targetP);
                lwm2m_data_free(size, dataP);
                return -1;
            }
            targetP->status = STATE_DEREGISTERED;
            contextP->serverList = (lwm2m_server_t*)LWM2M_LIST_ADD(contextP->serverList, targetP);
        }
        lwm2m_data_free(size, dataP);
        securityInstP = securityInstP->next;
    }

    return 0;
}
Example #2
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;
}