Ejemplo n.º 1
0
//////////////////////////////////////////////////////////////////////////
// Method function:  put_light_blink
//////////////////////////////////////////////////////////////////////////
static int handle_put_light_blink(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
    int i,nblink;
    if (inpkt->payload.len == 0)
    {
        return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
    }
    if(inpkt->payload.len<9)
    {
        strcpy(light,(const char *)&inpkt->payload.p[0]);
    }
    if(numbers_only(light)) {
        nblink = atoi(inpkt->payload);
        blinkLed(nblink); //Blink Led nblink times using non-blocking timer
    }
    return coap_make_response(scratch, outpkt, (const uint8_t *)&light[0], inpkt->payload.len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
Ejemplo n.º 2
0
static uint32_t lwm2m_coap_handler_handle_request(coap_message_t * p_request)
{
    LWM2M_TRC("[LWM2M][CoAP      ]: >> lwm2m_coap_handler_handle_request\r\n");
    
    uint16_t index;
    uint16_t path[3];
    char *   endptr;
    
    bool     is_numbers_only = true;
    uint16_t path_index      = 0;
    uint32_t err_code        = NRF_SUCCESS;
    
    LWM2M_MUTEX_LOCK();
    
    for (index = 0; index < p_request->options_count; index++)
    {
        if (p_request->options[index].number == COAP_OPT_URI_PATH)
        {
            bool numbers = numbers_only((char *)p_request->options[index].p_data, p_request->options[index].length);
            if (numbers)
            {
                path[path_index] = strtol((char *)p_request->options[index].p_data, &endptr, 10);
                ++path_index;

                if (endptr == ((char *)p_request->options[index].p_data))
                {                    
                    err_code = NRF_ERROR_NOT_FOUND;
                    break;
                }

                if (endptr != ((char *)p_request->options[index].p_data +
                               p_request->options[index].length))
                {   
                    err_code = NRF_ERROR_NOT_FOUND;
                    break;
                }
            }
            else
            {
                is_numbers_only = false;
                break;
            }
        }
    }
    
    if (err_code == NRF_SUCCESS)
    {
        if (is_numbers_only == true)
        {
            err_code = internal_request_handle(p_request, path, path_index);
        }
        else
        {
            // If uri path did not consist of numbers only.
            char * requested_uri = NULL;
            for (index = 0; index < p_request->options_count; index++)
            {
                if (p_request->options[index].number == COAP_OPT_URI_PATH)
                {
                    requested_uri = (char *)p_request->options[index].p_data;

                    // Stop on first URI hit.
                    break;
                }
            }
            
            if (requested_uri == NULL)
            {
                err_code = NRF_ERROR_NOT_FOUND;
            }
            else
            {
                // Try to look up if there is a match with object with an alias name.
                for (int i = 0; i < m_num_objects; ++i)
                {
                    if (m_objects[i]->object_id == LWM2M_NAMED_OBJECT)
                    {
                        size_t size = strlen(m_objects[i]->p_alias_name);
                        if ((strncmp(m_objects[i]->p_alias_name, requested_uri, size) == 0))
                        {
                            if (m_objects[i]->callback == NULL)
                            {
                                err_code = NRF_ERROR_NULL;
                                break;
                            }
                            
                            LWM2M_MUTEX_UNLOCK();

                            err_code = m_objects[i]->callback(m_objects[i],
                                                              LWM2M_INVALID_INSTANCE,
                                                              LWM2M_OPERATION_CODE_NONE,
                                                              p_request);
                            
                            LWM2M_MUTEX_LOCK();

                            break;
                        }
                    }
                    else 
                    {
                        // This is not a name object, return error code.
                        err_code = NRF_ERROR_NOT_FOUND;
                        break;
                    }
                }
            }
        }
    }
    
    LWM2M_MUTEX_UNLOCK();
    
    LWM2M_TRC("[LWM2M][CoAP      ]: << lwm2m_coap_handler_handle_request\r\n");
    
    return err_code;
}