Exemplo n.º 1
0
coap_pdu_t* CAParsePDU(const char* data, uint32_t* outCode)
{
    coap_pdu_t* outpdu = coap_new_pdu();
    coap_pdu_parse((unsigned char *) data, strlen(data), outpdu);
    (*outCode) = (uint32_t) outpdu->hdr->code;

    return outpdu;
}
Exemplo n.º 2
0
coap_pdu_t* CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *options,
        const char* payload)
{
    OIC_LOG(DEBUG, TAG, "CACreatePDUforRequestWithPayload");

    coap_pdu_t *pdu;
    coap_list_t *opt;
    unsigned char _token_data[8];
    str the_token =
    { 0, _token_data };

    if (!(pdu = coap_new_pdu()))
        return NULL;

    /* initialize message id */
    unsigned short message_id;
    prng((unsigned char *)&message_id, sizeof(unsigned short));

    pdu->hdr->type = msgtype;
    pdu->hdr->id = htons(++message_id);
    pdu->hdr->code = code;

    pdu->hdr->token_length = the_token.length;
    if (!coap_add_token(pdu, the_token.length, the_token.s))
    {
        OIC_LOG(DEBUG, TAG,"cannot add token to request");
    }

    for (opt = options; opt; opt = opt->next)
    {
        coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data),
                COAP_OPTION_LENGTH(*(coap_option *)opt->data),
                COAP_OPTION_DATA(*(coap_option *)opt->data));
    }

    if (NULL != payload)
    {
        uint32_t len = strlen(payload);
        if ((flags & CA_FLAGS_BLOCK) == 0)
        {
            OIC_LOG_V(DEBUG, TAG, "coap_add_data, payload: %s", payload);
            coap_add_data(pdu, len, payload);
        }
        else
        {
            OIC_LOG_V(DEBUG, TAG, "coap_add_block, payload: %s", payload);
            coap_add_block(pdu, len, payload, block.num, block.szx);
        }
    }
    return pdu;
}
Exemplo n.º 3
0
coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
{
    OIC_LOG(DEBUG, TAG, "IN");

    if (NULL == data)
    {
        OIC_LOG(ERROR, TAG, "data is null");
        return NULL;
    }

    coap_pdu_t *outpdu = coap_new_pdu();
    if (NULL == outpdu)
    {
        OIC_LOG(ERROR, TAG, "outpdu is null");
        return NULL;
    }

    if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu))
    {
        OIC_LOG(ERROR, TAG, "pdu parse failed");
        coap_delete_pdu(outpdu);
        return NULL;
    }

    if (outpdu->hdr->version != COAP_DEFAULT_VERSION)
    {
        OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
                  outpdu->hdr->version);
        coap_delete_pdu(outpdu);
        return NULL;
    }

    if (outpdu->hdr->token_length > CA_MAX_TOKEN_LEN)
    {
        OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
                  outpdu->hdr->token_length);
        coap_delete_pdu(outpdu);
        return NULL;
    }

    if (outCode)
    {
        (*outCode) = (uint32_t) CA_RESPONSE_CODE(outpdu->hdr->code);
    }

    OIC_LOG(DEBUG, TAG, "OUT");
    return outpdu;
}
Exemplo n.º 4
0
/*
 * Function:    _wilddog_recvCoap
 * Description: Verify the receive the coap 
 * Input:       p_buf: The pointer of the buffer
 *              buflen: The length of the buffer 
 * Output:      N/A
 * Return:      If there's coap packet, return the pointer of the coap pdu, 
 *              else return NULL
*/
STATIC coap_pdu_t * WD_SYSTEM _wilddog_recvCoap
    (
    u8 *p_buf,
    u32 buflen
    )
{
    coap_pdu_t* p_resp = NULL;

    p_resp = coap_new_pdu();
    if(!p_resp)
        return NULL;

    /*  is coap packet */
    if( coap_pdu_parse(p_buf,buflen, p_resp) && 
        (p_resp->hdr->version == COAP_DEFAULT_VERSION)) 
            return p_resp;
    else
    {
        coap_delete_pdu(p_resp);
        return NULL;
    }
}
Exemplo n.º 5
0
coap_pdu_t* CACreatePDUforRequest(const code_t code, coap_list_t *options)
{
    OIC_LOG(DEBUG, TAG, "CACreatePDUforRequest");

    coap_pdu_t *pdu;
    coap_list_t *opt;
    unsigned char _token_data[8];
    str the_token =
    { 0, _token_data };

    if (!(pdu = coap_new_pdu()))
        return NULL;

    /* initialize message id */
    unsigned short message_id;
    prng((unsigned char *)&message_id, sizeof(unsigned short));

    pdu->hdr->type = msgtype;
    pdu->hdr->id = htons(++message_id);
    pdu->hdr->code = code;

    pdu->hdr->token_length = the_token.length;
    if (!coap_add_token(pdu, the_token.length, the_token.s))
    {
        OIC_LOG(DEBUG, TAG, "cannot add token to request");
    }

    for (opt = options; opt; opt = opt->next)
    {
        coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data),
                COAP_OPTION_LENGTH(*(coap_option *)opt->data),
                COAP_OPTION_DATA(*(coap_option *)opt->data));
    }

    return pdu;
}
Exemplo n.º 6
0
void
notify(coap_context_t *context, coap_resource_t *res,
       coap_subscription_t *sub, unsigned int duration, int code) {
  coap_pdu_t *pdu;
  int ls, finished=0;
  unsigned char ct, d;
  unsigned int length;
#ifndef NDEBUG
  char addr[INET6_ADDRSTRLEN];
#endif

  if ( !context || !res || !sub || !(pdu = coap_new_pdu()) )
    return;

  pdu->hdr->type = COAP_MESSAGE_CON;
  pdu->hdr->id = rand();	/* use a random transaction id */
  pdu->hdr->code = code;

  /* FIXME: content-type and data (how about block?) */
  if (res->uri->na.length)
    coap_add_option (pdu, COAP_OPTION_URI_AUTHORITY,
		     res->uri->na.length,
		     res->uri->na.s );

  if (res->uri->path.length)
    coap_add_option (pdu, COAP_OPTION_URI_PATH,
		     res->uri->path.length,
		     res->uri->path.s);

  d = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls);

  coap_add_option ( pdu, COAP_OPTION_SUBSCRIPTION, 1, &d );

  if (sub->token.length) {
    coap_add_option (pdu, COAP_OPTION_TOKEN,
		     sub->token.length,
		     sub->token.s);
  }

  if (res->uri->query.length)
    coap_add_option (pdu, COAP_OPTION_URI_QUERY,
		     res->uri->query.length,
		     res->uri->query.s );

  if (res->data) {
    length = (unsigned char *)pdu->hdr + COAP_MAX_PDU_SIZE - pdu->data;
    ct = res->mediatype;
    res->data(res->uri, &ct, 0, pdu->data, &length, &finished);
    pdu->length += length;

    /* TODO: add block option if not finished */
    /* TODO: add mediatype */
  }

#ifndef NDEBUG
  if ( inet_ntop(AF_INET6, &(sub->subscriber.sin6_addr), addr, INET6_ADDRSTRLEN) ) {
    debug("*** notify for %s to [%s]:%d\n", res->uri->path.s, addr, ntohs(sub->subscriber.sin6_port));
  }
#endif
  if ( pdu && coap_send_confirmed(context,
		  &sub->subscriber, pdu ) == COAP_INVALID_TID ) {
    debug("coap_check_resource_list: error sending notification\n");
    coap_delete_pdu(pdu);
  }
}
Exemplo n.º 7
0
coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
                              const CAEndpoint_t *endpoint)
{
    OIC_LOG(DEBUG, TAG, "IN");
    VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
    VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);

    coap_pdu_t *pdu = coap_new_pdu();

    if (NULL == pdu)
    {
        OIC_LOG(ERROR, TAG, "malloc failed");
        return NULL;
    }

    OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
    uint16_t message_id;
    if (0 == info->messageId)
    {
        /* initialize message id */
        prng((uint8_t * ) &message_id, sizeof(message_id));

        OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
    }
    else
    {
        /* use saved message id */
        message_id = info->messageId;
    }
    pdu->hdr->id = message_id;
    OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id);

    pdu->hdr->type = info->type;
    pdu->hdr->code = COAP_RESPONSE_CODE(code);

    if (info->token && CA_EMPTY != code)
    {
        uint32_t tokenLength = info->tokenLength;
        OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
        OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);

        int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token);
        if (0 == ret)
        {
            OIC_LOG(ERROR, TAG, "can't add token");
        }
    }

    if (options)
    {
        for (coap_list_t *opt = options; opt; opt = opt->next)
        {
            OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
                      COAP_OPTION_DATA(*(coap_option *) opt->data));
            coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
                            COAP_OPTION_LENGTH(*(coap_option *) opt->data),
                            COAP_OPTION_DATA(*(coap_option *) opt->data));
        }
    }

    bool enabledPayload = false;
#ifndef WITH_BWT
    enabledPayload = true;
#endif

    if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter)
    {
        if (NULL != info->payload && 0 < info->payloadSize)
        {
            OIC_LOG(DEBUG, TAG, "payload is added");
            coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
        }
    }

    OIC_LOG(DEBUG, TAG, "OUT");
    return pdu;
}
Exemplo n.º 8
0
int
coap_read(coap_context_t *ctx,
	  struct sockaddr_in6 *src, void *buf,
	  uint16_t bytes_read, struct ip6_metadata *meta) {
  coap_queue_t *node;
  coap_opt_t *opt;

  if ( bytes_read < 0 ) {
    return -1;
  }

  if ( bytes_read < sizeof(coap_hdr_t) || ((coap_hdr_t *)buf)->version != COAP_DEFAULT_VERSION ) {
#ifndef NDEBUG
    //fprintf(stderr, "coap_read: discarded invalid frame\n" );
#endif
    return -1;
  }
  node = coap_new_node();
  if ( !node )
    return -1;

  node->pdu = coap_new_pdu();
  if ( !node->pdu ) {
    coap_delete_node( node );
    return -1;
  }

  /*printf("** coap: coap_read pointers %p %p, %p %p\n",
    &node->remote,
    node->remote,
    src,
    *src,
    sizeof(src),
    sizeof(*src));*/

  memcpy( &node->remote, src, sizeof( *src ) );

  /* "parse" received PDU by filling pdu structure */
  memcpy( node->pdu->hdr, buf, bytes_read );
  node->pdu->length = bytes_read;

  /* finally calculate beginning of data block */
  options_end( node->pdu, &opt );

  if ( (unsigned char *)node->pdu->hdr + node->pdu->length <
       (unsigned char *)opt )
    node->pdu->data = (unsigned char *)node->pdu->hdr + node->pdu->length;
  else
    node->pdu->data = (unsigned char *)opt;

  /* and add new node to receive queue */
  coap_insert_node( &ctx->recvqueue, node, order_transaction_id );

#ifndef NDEBUG
  if ( inet_ntop(src.sin6_family, &src.sin6_addr, addr, INET6_ADDRSTRLEN) == 0 ) {
    //perror("coap_read: inet_ntop");
  } else {
    printf( "** received from [%s]:%d:\n  ",addr,ntohs(src.sin6_port));
  }
  coap_show_pdu( node->pdu );
#endif

  return 0;
}