Exemple #1
0
// send the registration for a single server
static void prv_register(lwm2m_context_t * contextP,
                         lwm2m_server_t * server)
{
    char query[200];
    int query_length;
    uint8_t payload[512];
    int payload_length;

    lwm2m_transaction_t * transaction;

    payload_length = prv_getRegisterPayload(contextP, payload, sizeof(payload));
    if (payload_length == 0) return;

    query_length = prv_getRegistrationQuery(contextP, server, query, sizeof(query));

    if (query_length == 0) return;

    if (0 != server->lifetime)
    {
        int res;

        res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, QUERY_DELIMITER QUERY_LIFETIME);
        if (res < 0) return;
        query_length += res;
        res = utils_intCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, server->lifetime);
        if (res < 0) return;
        query_length += res;
    }

    if (server->sessionH == NULL)
    {
        server->sessionH = contextP->connectCallback(server->secObjInstID, contextP->userData);
    }

    if (NULL != server->sessionH)
    {
        transaction = transaction_new(COAP_TYPE_CON, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL, ENDPOINT_SERVER, (void *)server);
        if (transaction == NULL) return;

        coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT);
        coap_set_header_uri_query(transaction->message, query);
        coap_set_header_content_type(transaction->message, LWM2M_CONTENT_LINK);
        coap_set_payload(transaction->message, payload, payload_length);

        transaction->callback = prv_handleRegistrationReply;
        transaction->userData = (void *) server;

        contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
        if (transaction_send(contextP, transaction) == 0)
        {
            server->status = STATE_REG_PENDING;
        }
    }
}
Exemple #2
0
// send the registration for a single server
static int prv_register(lwm2m_context_t * contextP, lwm2m_server_t * server)
{
    char query[200];
    int query_length;
    char payload[512];
    int payload_length;

    lwm2m_transaction_t * transaction;

    payload_length = prv_getRegisterPayload(contextP, payload, sizeof(payload));
    if (payload_length == 0) return INTERNAL_SERVER_ERROR_5_00;

    query_length = prv_getRegistrationQuery(contextP, query, sizeof(query));

    if (query_length == 0) return INTERNAL_SERVER_ERROR_5_00;

    if (0 != server->lifetime)
    {
        if (snprintf(query + query_length,
                        PRV_QUERY_BUFFER_LENGTH - query_length,
                        QUERY_DELIMITER QUERY_LIFETIME "%d",
                        server->lifetime) <= 0)
        {
            return INTERNAL_SERVER_ERROR_5_00;
        }
    }

    if (server->sessionH == NULL)
    {
        server->sessionH = contextP->connectCallback(server->shortID, contextP->userData);
    }

    if (server->sessionH != NULL)
    {
        transaction = transaction_new(COAP_POST, NULL, contextP->nextMID++, ENDPOINT_SERVER, (void *)server);
        if (transaction == NULL) return INTERNAL_SERVER_ERROR_5_00;

        coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT);
        coap_set_header_uri_query(transaction->message, query);
        coap_set_payload(transaction->message, payload, payload_length);

        transaction->callback = prv_handleRegistrationReply;
        transaction->userData = (void *) server;

        contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
        if (transaction_send(contextP, transaction) == 0)
        {
            server->status = STATE_REG_PENDING;
            server->mid = transaction->mID;
        }
    }
}
Exemple #3
0
int lwm2m_register(lwm2m_context_t * contextP)
{
    char * query;
    char payload[512];
    int payload_length;
    lwm2m_server_t * targetP;

    payload_length = prv_getRegisterPayload(contextP, payload, sizeof(payload));
    if (payload_length == 0) return INTERNAL_SERVER_ERROR_5_00;

    query = (char*)lwm2m_malloc(QUERY_LENGTH + strlen(contextP->endpointName) + 1);
    if (query == NULL) return INTERNAL_SERVER_ERROR_5_00;
    strcpy(query, QUERY_TEMPLATE);
    strcpy(query + QUERY_LENGTH, contextP->endpointName);

    targetP = contextP->serverList;
    while (targetP != NULL)
    {
        lwm2m_transaction_t * transaction;

        transaction = transaction_new(COAP_POST, NULL, contextP->nextMID++, ENDPOINT_SERVER, (void *)targetP);
        if (transaction == NULL) return INTERNAL_SERVER_ERROR_5_00;

        coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT);
        coap_set_header_uri_query(transaction->message, query);
        coap_set_payload(transaction->message, payload, payload_length);

        transaction->callback = prv_handleRegistrationReply;
        transaction->userData = (void *) contextP;

        contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
        if (transaction_send(contextP, transaction) == 0)
        {
            targetP->status = STATE_REG_PENDING;
            targetP->mid = transaction->mID;
        }

        targetP = targetP->next;
    }

    lwm2m_free(query);

    return 0;
}