lobaro / lobaro-coap

CoAP Implementation in C
MIT License
124 stars 47 forks source link

Get uint value from option #22

Closed NZSmartie closed 6 years ago

NZSmartie commented 6 years ago

A Simple feature to store the uint value of an CoAP_option_t into a pointer argument

niondir commented 6 years ago

So thanks for the PR. Looks straight forward.

May I ask where you needed this? - Just for the purpose of documentation here :)

NZSmartie commented 6 years ago

I use it in a request callback to quickly get the Accept option value and check if the client accepts support content types. main/resources/leds.c:71:

_coap.message_get_option( request, kCoapOptionAccept, &accept_option );
    while( accept_option != NULL )
    {
        if( _coap.option_get_uint( accept_option, &accept ) != kCoapOK )
            break;
        if( accept == kCoapContentTypeApplicationJson )
            break;

        if( _coap.option_get_next( &accept_option ) != kCoapOK )
            break;
}

main/interfaces/lobaro-coap.c:429:

static CoapResult_t coap_option_get_uint( const CoapOption_t option, uint32_t* value )
{
    return CoAP_GetUintFromOption( option, value ) == COAP_OK ? kCoapOK : kCoapError;
}

main/interfaces/lobaro-coap.c:401:

static CoapResult_t coap_message_get_option( const CoapMessage_t message, const uint16_t option_number, CoapOption_t *option ){
    const CoAP_Message_t *pMsg = (CoAP_Message_t*)message;
    *option = NULL;

    CoAP_option_t *pOpt = CoAP_FindOptionByNumber( (CoAP_Message_t*)pMsg, option_number );
    if( pOpt == NULL )
        return kCoapError;

    *option = (CoapOption_t)pOpt;
    return kCoapOK;
}