コード例 #1
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;
}
コード例 #2
0
/*
 * Function:    _wilddog_coap_ackSend.
 * Description: response an ack .
 * Input:       recv_type: recv type conn or non-con .
 *              ack_type : respond an ack or reset.
 *              mid : recv coap's message id.
 *              tokenLen: recv coap's token len.
 *              recv_token: recv coap's token.
 * Output:      N/A.
 * Return:      N/A.
*/
STATIC int WD_SYSTEM _wilddog_coap_ackSend
    (
    u32 recv_type,
    u32 ack_type,
    u32 mid,
    u32 tokenLen,
    u32 recv_token
    ) 
{
    int returnCode= 0;
    unsigned int id = mid;
    size_t tkl = tokenLen;
    unsigned char* p_tk = (unsigned char*)&recv_token;
    coap_pdu_t  *toSend = NULL;
    /* only con request need tobe respond.*/
    if(recv_type != COAP_MESSAGE_CON)
        return WILDDOG_ERR_NOERR;

    toSend = coap_pdu_init(ack_type, 0, id,WILDDOG_PROTO_MAXSIZE);
    coap_add_token(toSend,tkl,p_tk);
    if (toSend == NULL) 
    {
        wilddog_debug_level(WD_DEBUG_ERROR,"coap_addToken error");
        return WILDDOG_ERR_NULL;
    }
    returnCode = _wilddog_sec_send(toSend->hdr,toSend->length);
    
    coap_delete_pdu(toSend);
    return returnCode;

}
コード例 #3
0
static void
t_error_response1(void) {
  uint8_t teststr[] = {
    0x60, 0x80, 0x12, 0x34, 0xff, 'B', 'a', 'd',
    ' ', 'R', 'e', 'q', 'u', 'e', 's', 't'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->hdr->type = COAP_MESSAGE_CON;
  pdu->hdr->id = htons(0x1234);

  /* result = coap_add_token(pdu, 5, (unsigned char *)"token"); */
  coap_option_filter_clear(opts);
  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(400), opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 0);
  CU_ASSERT(response->hdr->code == 0x80);
  CU_ASSERT(pdu->hdr->id == htons(0x1234));

  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #4
0
static void
t_error_response3(void) {
  const unsigned char code = COAP_RESPONSE_CODE(402);
  uint8_t teststr[] = {
    0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e',
    'n', 0x90, 0xff, 'B', 'a', 'd', ' ', 'O',
    'p', 't', 'i', 'o', 'n'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->hdr->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (unsigned char *)"token");
  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */

  /* unknown critical option 9 */
  coap_add_option(pdu, 9, 0, NULL);

  coap_option_filter_clear(opts);
  coap_option_setb(opts, 9);
  response = coap_new_error_response(pdu, code, opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == code);

  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #5
0
static void
t_error_response2(void) {
  uint8_t teststr[] = {
    0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e',
    'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o',
    'u', 'n', 'd'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->hdr->type = COAP_MESSAGE_NON;
  pdu->hdr->id = htons(0x1234);
  coap_add_token(pdu, 5, (unsigned char *)"token");
  coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time");

  coap_option_filter_clear(opts);
  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_NON);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == 0x84);

  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #6
0
TEST_F(CABlockTransferTests, CAGetBlockSizeOptionFromPduTest)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

    coap_pdu_t *pdu = NULL;
    coap_list_t *options = NULL;
    coap_transport_t transport = COAP_UDP;

    CAToken_t tempToken = NULL;
    CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN);

    CAInfo_t requestData;
    memset(&requestData, 0, sizeof(CAInfo_t));
    requestData.token = tempToken;
    requestData.tokenLength = CA_MAX_TOKEN_LEN;
    requestData.type = CA_MSG_NONCONFIRM;

    pdu = CAGeneratePDU(CA_GET, &requestData, tempRep, &options, &transport);

    size_t totalPayloadLen = 0;
    EXPECT_FALSE(CAIsPayloadLengthInPduWithBlockSizeOption(pdu, COAP_OPTION_SIZE1,
                                                           &totalPayloadLen));

    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
}
コード例 #7
0
static void
t_error_response2(void) {
  uint8_t teststr[] = {
    0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e',
    'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o',
    'u', 'n', 'd'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->type = COAP_MESSAGE_NON;
  pdu->tid = 0x1234;
  coap_add_token(pdu, 5, (const uint8_t *)"token");
  coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time");

  coap_option_filter_clear(opts);
  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
  CU_ASSERT(response->type == COAP_MESSAGE_NON);
  CU_ASSERT(response->token_length == 5);
  CU_ASSERT(response->code == 0x84);
  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #8
0
// response and block option2
TEST_F(CABlockTransferTests, CAAddBlockOption2InResponse)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

    coap_pdu_t *pdu = NULL;
    coap_list_t *options = NULL;
    coap_transport_t transport = COAP_UDP;

    CAToken_t tempToken = NULL;
    CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN);

    CAInfo_t responseData;
    memset(&responseData, 0, sizeof(CAInfo_t));
    responseData.token = tempToken;
    responseData.tokenLength = CA_MAX_TOKEN_LEN;
    responseData.type = CA_MSG_NONCONFIRM;
    responseData.messageId = 1;

    responseData.payload = (CAPayload_t) calloc(LARGE_PAYLOAD_LENGTH, sizeof(char));
    if(!responseData.payload)
    {
        CADestroyToken(tempToken);
        FAIL() << "requestData.payload allocation failed";
    }
    memset(responseData.payload, '1', sizeof(responseData.payload) - 1);
    responseData.payloadSize = sizeof(responseData.payload);

    pdu = CAGeneratePDU(CA_CREATED, &responseData, tempRep, &options, &transport);

    CAData_t *cadata = CACreateNewDataSet(pdu, tempRep);
    EXPECT_TRUE(cadata != NULL);

    CABlockData_t *currData = CACreateNewBlockData(cadata);
    EXPECT_TRUE(currData != NULL);

    if (currData)
    {
        EXPECT_EQ(CA_STATUS_OK, CAUpdateBlockOptionType(currData->blockDataId,
                                                        COAP_OPTION_BLOCK2));

        EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption2(&pdu, &responseData,
                                                  responseData.payloadSize,
                                                  currData->blockDataId, &options));
    }

    CADestroyDataSet(cadata);
    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(responseData.payload);
}
コード例 #9
0
ファイル: camessagehandler.c プロジェクト: hanglei/iotivity
void CAErrorHandler(const CAEndpoint_t *endpoint,
                    const void *data, uint32_t dataLen,
                    CAResult_t result)
{
    OIC_LOG(DEBUG, TAG, "CAErrorHandler IN");

#ifndef SINGLE_THREAD

    VERIFY_NON_NULL_VOID(endpoint, TAG, "remoteEndpoint");
    VERIFY_NON_NULL_VOID(data, TAG, "data");

    uint32_t code = CA_NOT_FOUND;
    //Do not free remoteEndpoint and data. Currently they will be freed in data thread
    //Get PDU data
    coap_pdu_t *pdu = (coap_pdu_t *)CAParsePDU((const char *)data, dataLen, &code, endpoint);
    if (NULL == pdu)
    {
        OIC_LOG(ERROR, TAG, "Parse PDU failed");
        return;
    }

    CAData_t *cadata = CAGenerateHandlerData(endpoint, NULL, pdu, CA_ERROR_DATA);
    if(!cadata)
    {
        OIC_LOG(ERROR, TAG, "CAErrorHandler, CAGenerateHandlerData failed!");
        coap_delete_pdu(pdu);
        return;
    }

    cadata->errorInfo->result = result;

    CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
    coap_delete_pdu(pdu);
#endif

    OIC_LOG(DEBUG, TAG, "CAErrorHandler OUT");
    return;
}
コード例 #10
0
STATIC Wilddog_Return_T WD_SYSTEM _wilddog_coap_destory
    (
    void *p_coap,
    int flag
    )
{
    if(p_coap == 0)
        return WILDDOG_ERR_INVALID;
    
    wilddog_debug_level(WD_DEBUG_LOG,"\tcc\tdestory coap pakge :%p :",p_coap);
    coap_delete_pdu((coap_pdu_t*)p_coap);

    return WILDDOG_ERR_NOERR;
}
コード例 #11
0
TEST_F(CABlockTransferTests, CAGetPayloadFromBlockDataListTest)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

    coap_pdu_t *pdu = NULL;
    coap_list_t *options = NULL;
    coap_transport_t transport = COAP_UDP;

    CAToken_t tempToken = NULL;
    CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN);

    CAInfo_t requestData;
    memset(&requestData, 0, sizeof(CAInfo_t));
    requestData.token = tempToken;
    requestData.tokenLength = CA_MAX_TOKEN_LEN;
    requestData.type = CA_MSG_NONCONFIRM;

    pdu = CAGeneratePDU(CA_GET, &requestData, tempRep, &options, &transport);

    CAData_t *cadata = CACreateNewDataSet(pdu, tempRep);
    EXPECT_TRUE(cadata != NULL);

    CABlockData_t *currData = CACreateNewBlockData(cadata);
    EXPECT_TRUE(currData != NULL);

    if (currData)
    {
        size_t fullPayload = 0;
        CAPayload_t payload = CAGetPayloadFromBlockDataList(currData->blockDataId,
                                                            &fullPayload);

        size_t payloadLen = (payload != NULL) ? strlen((const char*) payload) : 0;
        EXPECT_TRUE(fullPayload == payloadLen);

        CARemoveBlockDataFromList(currData->blockDataId);
    }

    CADestroyDataSet(cadata);
    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
}
コード例 #12
0
ファイル: etsi_iot_01.c プロジェクト: erichkeane/libcoap
void 
check_async(coap_context_t  *ctx, coap_tick_t now) {
  coap_pdu_t *response;
  coap_async_state_t *tmp;
  unsigned char buf[2];
  size_t size = sizeof(coap_hdr_t) + 8;

  if (!async || now < async->created + (unsigned long)async->appdata) 
    return;

  size += async->tokenlen;

  response = coap_pdu_init(async->flags & COAP_ASYNC_CONFIRM 
			   ? COAP_MESSAGE_CON
			   : COAP_MESSAGE_NON, 
			   COAP_RESPONSE_CODE(205), 0, size);
  if (!response) {
    debug("check_async: insufficient memory, we'll try later\n");
    async->appdata = 
      (void *)((unsigned long)async->appdata + 15 * COAP_TICKS_PER_SECOND);
    return;
  }
  
  response->hdr->id = coap_new_message_id(ctx);

  if (async->tokenlen)
    coap_add_token(response, async->tokenlen, async->token);

  coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
		  coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);

  coap_add_data(response, 4, (unsigned char *)"done");

  if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) {
    debug("check_async: cannot send response for message %d\n", 
	  response->hdr->id);
  }
  coap_delete_pdu(response);
  
  coap_remove_async(ctx, async->id, &tmp);
  coap_free_async(async);
  async = NULL;
}
コード例 #13
0
static void
t_error_response7(void) {
  const unsigned char code = COAP_RESPONSE_CODE(402);
  unsigned char optval[] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    0x10, 0x11, 0x12
  };
  uint8_t teststr[] = {
    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
     'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03,
    0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
    0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff,
     'B',  'a',  'd',  ' ',  'O',  'p',  't',  'i',
     'o',  'n'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->hdr->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (unsigned char *)"token");
  /* known option 11 */
  coap_add_option(pdu, 11, 4, (unsigned char *)"time");

  /* unknown critical option 23 */
  coap_add_option(pdu, 23, sizeof(optval), optval);

  coap_option_filter_clear(opts);
  coap_option_setb(opts, 23);
  response = coap_new_error_response(pdu, code, opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == code);

  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #14
0
static void
t_error_response8(void) {
  const unsigned char code = COAP_RESPONSE_CODE(503);
  uint8_t teststr[] = {
    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
     'n', 0xe0, 0x02, 0xdc, 0xd0, 0x00, 0xff,  'S',
     'e',  'r',  'v',  'i',  'c',  'e',  ' ',  'U',
     'n',  'a',  'v',  'a',  'i',  'l',  'a',  'b',
     'l',  'e'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->hdr->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (unsigned char *)"token");
  /* known option 1000 */
  coap_add_option(pdu, 1000, 0, NULL);

  /* unknown options 1001 and 1014 */
  coap_add_option(pdu, 1001, 0, NULL);
  coap_add_option(pdu, 1014, 0, NULL);

  /* known option 2000 */
  coap_add_option(pdu, 2000, 0, NULL);

  coap_option_filter_clear(opts);
  coap_option_setb(opts, 1001);
  coap_option_setb(opts, 1014);
  response = coap_new_error_response(pdu, code, opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->length == sizeof(teststr));
  CU_ASSERT(response->hdr->version == 1);
  CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->hdr->token_length == 5);
  CU_ASSERT(response->hdr->code == code);

  CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #15
0
static void
t_error_response6(void) {
  const uint8_t code = COAP_RESPONSE_CODE(402);
  unsigned char optval[] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    0x10, 0x11, 0x12
  };
  uint8_t teststr[] = {
    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
     'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03,
    0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
    0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff,
     'B',  'a',  'd',  ' ',  'O',  'p',  't',  'i',
     'o',  'n'
  };
  coap_pdu_t *response;

  coap_pdu_clear(pdu, pdu->max_size);
  pdu->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (const uint8_t *)"token");
  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); */

  /* unknown critical option 23 */
  coap_add_option(pdu, 23, sizeof(optval), optval);

  coap_option_filter_clear(opts);
  coap_option_setb(opts, 23);
  response = coap_new_error_response(pdu, code, opts);

  CU_ASSERT_PTR_NOT_NULL(response);

  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->token_length == 5);
  CU_ASSERT(response->code == code);
  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
  coap_delete_pdu(response);
}
コード例 #16
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;
    }
}
コード例 #17
0
TEST_F(CABlockTransferTests, CASetNextBlockOption2WithResponse)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

    coap_pdu_t *pdu = NULL;
    coap_list_t *options = NULL;
    coap_transport_t transport = COAP_UDP;

    CAToken_t tempToken = NULL;
    CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN);

    CAInfo_t responseData;
    memset(&responseData, 0, sizeof(CAInfo_t));
    responseData.token = tempToken;
    responseData.tokenLength = CA_MAX_TOKEN_LEN;
    responseData.type = CA_MSG_NONCONFIRM;
    responseData.messageId = 1;

    pdu = CAGeneratePDU(CA_CREATED, &responseData, tempRep, &options, &transport);

    CAData_t *cadata = CACreateNewDataSet(pdu, tempRep);
    EXPECT_TRUE(cadata != NULL);

    CABlockData_t *currData = CACreateNewBlockData(cadata);
    EXPECT_TRUE(currData != NULL);

    coap_block_t block = {0, 0, 0};
    EXPECT_EQ(CA_STATUS_OK, CASetNextBlockOption2(pdu, tempRep, cadata, block, pdu->length));

    CADestroyDataSet(cadata);
    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(responseData.payload);
}
コード例 #18
0
// request and block option1
TEST_F(CABlockTransferTests, CAAddBlockOptionTest)
{
    CAEndpoint_t* tempRep = NULL;
    CACreateEndpoint(CA_DEFAULT_FLAGS, CA_ADAPTER_IP, "127.0.0.1", 5683, &tempRep);

    coap_pdu_t *pdu = NULL;
    coap_list_t *options = NULL;
    coap_transport_t transport = COAP_UDP;

    CAToken_t tempToken = NULL;
    CAGenerateToken(&tempToken, CA_MAX_TOKEN_LEN);

    CAInfo_t requestData;
    memset(&requestData, 0, sizeof(CAInfo_t));
    requestData.token = tempToken;
    requestData.tokenLength = CA_MAX_TOKEN_LEN;

    requestData.payload = (CAPayload_t) calloc(LARGE_PAYLOAD_LENGTH, sizeof(char));
    if(!requestData.payload)
    {
        CADestroyToken(tempToken);
        FAIL() << "requestData.payload allocation failed";
    }
    memset(requestData.payload, '1', sizeof(requestData.payload) - 1);
    requestData.payloadSize = sizeof(requestData.payload);
    requestData.type = CA_MSG_NONCONFIRM;

    pdu = CAGeneratePDU(CA_GET, &requestData, tempRep, &options, &transport);

    EXPECT_EQ(CA_STATUS_OK, CAAddBlockOption(&pdu, &requestData, tempRep, &options));

    coap_delete_list(options);
    coap_delete_pdu(pdu);

    CADestroyToken(tempToken);
    CADestroyEndpoint(tempRep);
    free(requestData.payload);
}
コード例 #19
0
ファイル: server.c プロジェクト: nikosft/libcoap
static void
send_async_response(coap_context_t *ctx, const coap_endpoint_t *local_if) 
{
	coap_pdu_t *response;
	unsigned char buf[3];
	const char* response_data     = "Hello World!";
	size_t size = sizeof(coap_hdr_t) + 20;
	response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size);
	response->hdr->id = coap_new_message_id(ctx);
	if (async->tokenlen)
		coap_add_token(response, async->tokenlen, async->token);
	coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
	coap_add_data  (response, strlen(response_data), (unsigned char *)response_data);

	if (coap_send(ctx, local_if, &async->peer, response) == COAP_INVALID_TID) {
		
	}
	coap_delete_pdu(response);
	coap_async_state_t *tmp;
	coap_remove_async(ctx, async->id, &tmp);
	coap_free_async(async);
	async = NULL;
 }
コード例 #20
0
/*  to do 
 * Function:    _wilddog_coap_receive.
 * Description: recv coap packet and handle it.
 *   
 * Input:       N/A.
 * Output:      N/A.
 * Return:      Wilddog_Return_T type
*/
Wilddog_Return_T WD_SYSTEM _wilddog_coap_receive(void *p_arg,int flag)
{
    int res =0;
    u32 recv_type = 0;
    u32 ack_type = COAP_MESSAGE_ACK;
    u32 tmp_tokenLen = 0;
    u32 tmp_token = 0;
    u32 tmp_mid = 0;
    Protocol_recvArg_T recvArg;
    coap_pdu_t *p_pdu = NULL;

    memset(&recvArg,0,sizeof(Protocol_recvArg_T));

    recvArg.p_recvData = _wilddog_coap_mallocRecvBuffer();
    if( recvArg.p_recvData  == NULL)
    {
        wilddog_debug_level(WD_DEBUG_ERROR, "malloc failed!");
        return WILDDOG_ERR_NULL;
    }
    
    res = _wilddog_sec_recv((void*)recvArg.p_recvData,(s32)WILDDOG_PROTO_MAXSIZE);
    /*@ NO enougth space */
    if( res <= 0 || res  > WILDDOG_PROTO_MAXSIZE ) 
        goto _COAPRECV_ERR;

    recvArg.d_recvDataLen = res;

    /* distinguish recv an coap packet. */
    p_pdu = _wilddog_recvCoap(recvArg.p_recvData,recvArg.d_recvDataLen);
    if(p_pdu == NULL)
        goto _COAPRECV_ERR; 
    
#ifdef WILDDOG_SELFTEST                        
    ramtest_caculate_peakRam();             
    performtest_getHandleRecvDtlsTime();
#endif  

#ifdef WILDDOG_DEBUG
#if DEBUG_LEVEL <= WD_DEBUG_LOG
    printf("recv:\n");
    coap_show_pdu(p_pdu);
#endif
#endif
    /* dispatch .*/
    recvArg.d_recvDataLen = WILDDOG_PROTO_MAXSIZE;
    if( _wilddog_recv_dispatch(p_pdu,&recvArg) < 0)
    {
        coap_delete_pdu(p_pdu);
        goto _COAPRECV_ERR;
            
    }
    tmp_mid = p_pdu->hdr->id;
    recv_type = p_pdu->hdr->type;
    tmp_tokenLen = p_pdu->hdr->token_length;
    memcpy(&tmp_token,p_pdu->hdr->token,p_pdu->hdr->token_length);

    coap_delete_pdu(p_pdu);
    /* call back.*/
    if( _wilddog_coap_findRespondNode(&recvArg) != TRUE)
        ack_type = COAP_MESSAGE_RST;
    /* ack */
    _wilddog_coap_ackSend(recv_type,ack_type,tmp_mid,tmp_tokenLen,tmp_token);
    _wilddog_coap_freeRecvBuffer( recvArg.p_recvData );
   
    return  WILDDOG_ERR_NOERR;
   
_COAPRECV_ERR:
    _wilddog_coap_freeRecvBuffer( recvArg.p_recvData );
    return WILDDOG_ERR_NULL;
}
コード例 #21
0
static void CAProcessData(const CAData_t *data)
{
    OIC_LOG(DEBUG, TAG, "IN");
    VERIFY_NON_NULL_VOID(data, TAG, "data");
    VERIFY_NON_NULL_VOID(data->remoteEndpoint, TAG, "remoteEndpoint");

    CAResult_t res = CA_STATUS_FAILED;

    CASendDataType_t type = data->type;

    if (SEND_TYPE_UNICAST == type)
    {
        coap_pdu_t *pdu = NULL;

        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "reqInfo avlbl");

            pdu = (coap_pdu_t *)CAGeneratePDU(data->requestInfo->method, &data->requestInfo->info);
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "resInfo avlbl");

            pdu = (coap_pdu_t *)CAGeneratePDU(data->responseInfo->result, &data->responseInfo->info);
        }
        else
        {
            OIC_LOG(DEBUG, TAG, "request info, response info is empty");
        }

        // interface controller function call.
        if (NULL != pdu)
        {
            CALogPDUInfo(pdu);

            res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                coap_delete_pdu(pdu);
                return;
            }
            // for retransmission
            res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
                                           pdu->length);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG_V(INFO, TAG, "retransmissions will not be working: %d", res);
                coap_delete_pdu(pdu);
                return;
            }

            coap_delete_pdu(pdu);
        }
    }
    else if (SEND_TYPE_MULTICAST == type)
    {
        OIC_LOG(DEBUG, TAG, "both requestInfo & responseInfo is not available");

        CAInfo_t *info = &data->requestInfo->info;

        info->options = data->options;
        info->numOptions = data->numOptions;

        coap_pdu_t *pdu = (coap_pdu_t *)CAGeneratePDU(CA_GET, info);

        if (NULL != pdu)
        {
            CALogPDUInfo(pdu);
            res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
            if(CA_STATUS_OK != res)
            {
                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                coap_delete_pdu(pdu);
                return;
            }
            coap_delete_pdu(pdu);
        }
    }

    OIC_LOG(DEBUG, TAG, "OUT");
}
コード例 #22
0
ファイル: camessagehandler.c プロジェクト: hanglei/iotivity
static void CAReceivedPacketCallback(const CASecureEndpoint_t *sep,
                                     const void *data, uint32_t dataLen)
{
    VERIFY_NON_NULL_VOID(sep, TAG, "remoteEndpoint");
    VERIFY_NON_NULL_VOID(data, TAG, "data");

    OIC_LOG(DEBUG, TAG, "received pdu data :");
    OIC_LOG_BUFFER(DEBUG, TAG,  data, dataLen);

    uint32_t code = CA_NOT_FOUND;
    CAData_t *cadata = NULL;

    coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code,
                                                &(sep->endpoint));
    if (NULL == pdu)
    {
        OIC_LOG(ERROR, TAG, "Parse PDU failed");
        return;
    }

    OIC_LOG_V(DEBUG, TAG, "code = %d", code);
    if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
    {
        cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_REQUEST_DATA);
        if (!cadata)
        {
            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
            coap_delete_pdu(pdu);
            return;
        }
    }
    else
    {
        cadata = CAGenerateHandlerData(&(sep->endpoint), &(sep->identity), pdu, CA_RESPONSE_DATA);
        if (!cadata)
        {
            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, CAGenerateHandlerData failed!");
            coap_delete_pdu(pdu);
            return;
        }

#ifdef TCP_ADAPTER
        if (CA_ADAPTER_TCP == sep->endpoint.adapter)
        {
            OIC_LOG(INFO, TAG, "retransmission is not supported");
        }
        else
#endif
        {
            // for retransmission
            void *retransmissionPdu = NULL;
            CARetransmissionReceivedData(&g_retransmissionContext, cadata->remoteEndpoint, pdu->hdr,
                                         pdu->length, &retransmissionPdu);

            // get token from saved data in retransmission list
            if (retransmissionPdu && CA_EMPTY == code)
            {
                if (cadata->responseInfo)
                {
                    CAInfo_t *info = &cadata->responseInfo->info;
                    CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
                                                       info, &(sep->endpoint));
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
                        OICFree(info->token);
                        info->tokenLength = 0;
                    }
                }
            }
            OICFree(retransmissionPdu);
        }
    }

    cadata->type = SEND_TYPE_UNICAST;

#ifdef SINGLE_THREAD
    CAProcessReceivedData(cadata);
#else
#ifdef WITH_BWT
    if (CA_ADAPTER_GATT_BTLE != sep->endpoint.adapter
#ifdef TCP_ADAPTER
            && CA_ADAPTER_TCP != sep->endpoint.adapter
#endif
            )
    {
        CAResult_t res = CAReceiveBlockWiseData(pdu, &(sep->endpoint), cadata, dataLen);
        if (CA_NOT_SUPPORTED == res || CA_REQUEST_TIMEOUT == res)
        {
            OIC_LOG(ERROR, TAG, "this message does not have block option");
            CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
        }
        else
        {
            CADestroyData(cadata, sizeof(CAData_t));
        }
    }
    else
#endif
    {
        CAQueueingThreadAddData(&g_receiveThread, cadata, sizeof(CAData_t));
    }
#endif

    coap_delete_pdu(pdu);
}
コード例 #23
0
ファイル: camessagehandler.c プロジェクト: hanglei/iotivity
static CAResult_t CAProcessSendData(const CAData_t *data)
{
    VERIFY_NON_NULL(data, TAG, "data");
    VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");

    CAResult_t res = CA_STATUS_FAILED;

    CASendDataType_t type = data->type;

    coap_pdu_t *pdu = NULL;
    CAInfo_t *info = NULL;
    coap_list_t *options = NULL;
    coap_transport_type transport;

    if (SEND_TYPE_UNICAST == type)
    {
        OIC_LOG(DEBUG,TAG,"Unicast message");
#ifdef ROUTING_GATEWAY
        /*
         * When forwarding a packet, do not attempt retransmission as its the responsibility of
         * packet originator node
         */
        bool skipRetransmission = false;
#endif

        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
#ifdef ROUTING_GATEWAY
            skipRetransmission = data->requestInfo->info.skipRetransmission;
#endif
            pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint,
                                &options, &transport);
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "responseInfo is available..");

            info = &data->responseInfo->info;
#ifdef ROUTING_GATEWAY
            skipRetransmission = data->responseInfo->info.skipRetransmission;
#endif
            pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
                                &options, &transport);
        }
        else
        {
            OIC_LOG(DEBUG, TAG, "request info, response info is empty");
            return CA_STATUS_INVALID_PARAM;
        }

        // interface controller function call.
        if (NULL != pdu)
        {
#ifdef WITH_BWT
            if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
#ifdef TCP_ADAPTER
                    && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
#endif
                    )
            {
                // Blockwise transfer
                if (NULL != info)
                {
                    CAResult_t res = CAAddBlockOption(&pdu, info,
                                                      data->remoteEndpoint,
                                                      &options);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(INFO, TAG, "to write block option has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_list(options);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
            }
#endif
            CALogPDUInfo(pdu, data->remoteEndpoint);

            res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                coap_delete_list(options);
                coap_delete_pdu(pdu);
                return res;
            }

#ifdef TCP_ADAPTER
            if (CA_ADAPTER_TCP == data->remoteEndpoint->adapter)
            {
                OIC_LOG(INFO, TAG, "retransmission will be not worked");
            }
            else
#endif
#ifdef ROUTING_GATEWAY
            if(!skipRetransmission)
#endif
            {
                // for retransmission
                res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint,
                                               pdu->hdr, pdu->length);
                if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
                {
                    //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
                    OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
                    coap_delete_list(options);
                    coap_delete_pdu(pdu);
                    return res;
                }
            }

            coap_delete_list(options);
            coap_delete_pdu(pdu);
        }
        else
        {
            OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
            CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
            return CA_SEND_FAILED;
        }
    }
    else if (SEND_TYPE_MULTICAST == type)
    {
        OIC_LOG(DEBUG,TAG,"Multicast message");
        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
            pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint, &options, &transport);

            if (NULL != pdu)
            {
#ifdef WITH_BWT
                if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
#ifdef TCP_ADAPTER
                        && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
#endif
                        )
                {
                    // Blockwise transfer
                    CAResult_t res = CAAddBlockOption(&pdu, &data->requestInfo->info,
                                                      data->remoteEndpoint,
                                                      &options);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_list(options);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
#endif
            }
            else
            {
                OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
                CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
                return CA_SEND_FAILED;
            }
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "responseInfo is available..");

            info = &data->responseInfo->info;
            pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint,
                                &options, &transport);

            if (NULL != pdu)
            {
#ifdef WITH_BWT
                if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter
#ifdef TCP_ADAPTER
                        && CA_ADAPTER_TCP != data->remoteEndpoint->adapter
#endif
                        )
                {
                    // Blockwise transfer
                    if (NULL != info)
                    {
                        CAResult_t res = CAAddBlockOption(&pdu, info,
                                                          data->remoteEndpoint,
                                                          &options);
                        if (CA_STATUS_OK != res)
                        {
                            OIC_LOG(INFO, TAG, "to write block option has failed");
                            CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                            coap_delete_list(options);
                            coap_delete_pdu(pdu);
                            return res;
                        }
                    }
                }
#endif
            }
            else
            {
                OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
                CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
                return CA_SEND_FAILED;
            }
        }
        else
        {
            OIC_LOG(ERROR, TAG, "request or response info is empty");
            return CA_SEND_FAILED;
        }

        CALogPDUInfo(pdu, data->remoteEndpoint);

        OIC_LOG(DEBUG, TAG, "pdu to send :");
        OIC_LOG_BUFFER(DEBUG, TAG,  (uint8_t*)pdu->hdr, pdu->length);

        res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
            CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
            coap_delete_list(options);
            coap_delete_pdu(pdu);
            return res;
        }

        coap_delete_list(options);
        coap_delete_pdu(pdu);
    }

    return CA_STATUS_OK;
}
コード例 #24
0
ファイル: camessagehandler.c プロジェクト: HoTaeWang/iotivity
static CAResult_t CAProcessSendData(const CAData_t *data)
{
    OIC_LOG(DEBUG, TAG, "IN");
    VERIFY_NON_NULL(data, TAG, "data");
    VERIFY_NON_NULL(data->remoteEndpoint, TAG, "remoteEndpoint");

    CAResult_t res = CA_STATUS_FAILED;

    CASendDataType_t type = data->type;

    coap_pdu_t *pdu = NULL;
    CAInfo_t *info = NULL;

    if (SEND_TYPE_UNICAST == type)
    {

        OIC_LOG(DEBUG,TAG,"Unicast message");
        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
            pdu = CAGeneratePDU(data->requestInfo->method, info, data->remoteEndpoint);
        }
        else if (NULL != data->responseInfo)
        {
            OIC_LOG(DEBUG, TAG, "responseInfo is available..");

            info = &data->responseInfo->info;
            pdu = CAGeneratePDU(data->responseInfo->result, info, data->remoteEndpoint);
        }
        else
        {
            OIC_LOG(DEBUG, TAG, "request info, response info is empty");
            return CA_STATUS_INVALID_PARAM;
        }

        // interface controller function call.
        if (NULL != pdu)
        {
#ifdef WITH_BWT
            if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
            {
                // Blockwise transfer
                if (NULL != info)
                {
                    CAResult_t res = CAAddBlockOption(&pdu, *info,
                                                      data->remoteEndpoint);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(INFO, TAG, "to write block option has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
            }
#endif
            CALogPDUInfo(pdu);

            res = CASendUnicastData(data->remoteEndpoint, pdu->hdr, pdu->length);
            if (CA_STATUS_OK != res)
            {
                OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                coap_delete_pdu(pdu);
                return res;
            }
            // for retransmission
            res = CARetransmissionSentData(&g_retransmissionContext, data->remoteEndpoint, pdu->hdr,
                                           pdu->length);
            if ((CA_STATUS_OK != res) && (CA_NOT_SUPPORTED != res))
            {
                //when retransmission not supported this will return CA_NOT_SUPPORTED, ignore
                OIC_LOG_V(INFO, TAG, "retransmission is not enabled due to error, res : %d", res);
                coap_delete_pdu(pdu);
                return res;
            }

            coap_delete_pdu(pdu);
        }
        else
        {
            OIC_LOG(ERROR,TAG,"Failed to generate unicast PDU");
            CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
            return CA_SEND_FAILED;
        }
    }
    else if (SEND_TYPE_MULTICAST == type)
    {
        OIC_LOG(DEBUG,TAG,"Multicast message");
        if (NULL != data->requestInfo)
        {
            OIC_LOG(DEBUG, TAG, "requestInfo is available..");

            info = &data->requestInfo->info;
            pdu = CAGeneratePDU(CA_GET, info, data->remoteEndpoint);
            if (NULL != pdu)
            {
#ifdef WITH_BWT
                if (CA_ADAPTER_GATT_BTLE != data->remoteEndpoint->adapter)
                {
                    // Blockwise transfer
                    CAResult_t res = CAAddBlockOption(&pdu, data->requestInfo->info,
                                                      data->remoteEndpoint);
                    if (CA_STATUS_OK != res)
                    {
                        OIC_LOG(DEBUG, TAG, "CAAddBlockOption has failed");
                        CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                        coap_delete_pdu(pdu);
                        return res;
                    }
                }
#endif
                CALogPDUInfo(pdu);

                res = CASendMulticastData(data->remoteEndpoint, pdu->hdr, pdu->length);
                if (CA_STATUS_OK != res)
                {
                    OIC_LOG_V(ERROR, TAG, "send failed:%d", res);
                    CAErrorHandler(data->remoteEndpoint, pdu->hdr, pdu->length, res);
                    coap_delete_pdu(pdu);
                    return res;
                }

                coap_delete_pdu(pdu);
            }
            else
            {
                OIC_LOG(ERROR,TAG,"Failed to generate multicast PDU");
                CASendErrorInfo(data->remoteEndpoint, info, CA_SEND_FAILED);
                return CA_SEND_FAILED;
            }
        }
        else
        {
            OIC_LOG(ERROR, TAG, "request info is empty");
            return CA_SEND_FAILED;
        }
    }

    OIC_LOG(DEBUG, TAG, "OUT");
    return CA_STATUS_OK;
}
コード例 #25
0
ファイル: rd.c プロジェクト: andreaazzara/pyot
void 
hnd_put_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
		 const coap_endpoint_t *local_interface,
		 coap_address_t *peer, coap_pdu_t *request, str *token,
		 coap_pdu_t *response) {
#if 1
  response->hdr->code = COAP_RESPONSE_CODE(501);
#else /* FIXME */
  coap_opt_iterator_t opt_iter;
  coap_opt_t *token, *etag;
  coap_pdu_t *response;
  size_t size = sizeof(coap_hdr_t);
  int type = (request->hdr->type == COAP_MESSAGE_CON) 
    ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON;
  rd_t *rd = NULL;
  unsigned char code;		/* result code */
  unsigned char *data;
  str tmp;

  HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
  if (rd) {
    /* found resource object, now check Etag */
    etag = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter);
    if (!etag || (COAP_OPT_LENGTH(etag) != rd->etag_len)
	|| memcmp(COAP_OPT_VALUE(etag), rd->etag, rd->etag_len) != 0) {
      
      if (coap_get_data(request, &tmp.length, &data)) {

	tmp.s = (unsigned char *)coap_malloc(tmp.length);
	if (!tmp.s) {
	  debug("hnd_put_rd: cannot allocate storage for new rd\n");
	  code = COAP_RESPONSE_CODE(503);
	  goto finish;
	}

	coap_free(rd->data.s);
	rd->data.s = tmp.s;
	rd->data.length = tmp.length;
	memcpy(rd->data.s, data, rd->data.length);
      }
    }

    if (etag) {
      rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag));
      memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len);
    }

    code = COAP_RESPONSE_CODE(204);
    /* FIXME: update lifetime */
    
    } else {
    
    code = COAP_RESPONSE_CODE(503);
  }

  finish:
  /* FIXME: do not create a new response but use the old one instead */
  response = coap_pdu_init(type, code, request->hdr->id, size);

  if (!response) {
    debug("cannot create response for message %d\n", request->hdr->id);
    return;
  }

  if (request->hdr->token_length)
    coap_add_token(response, request->hdr->token_length, request->hdr->token);

  if (coap_send(ctx, peer, response) == COAP_INVALID_TID) {
    debug("hnd_get_rd: cannot send response for message %d\n", 
	  request->hdr->id);
  }
  coap_delete_pdu(response);
#endif
}
コード例 #26
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
int t_pdu_tests_remove(void)
{
    coap_delete_pdu(pdu);
    return 0;
}
コード例 #27
0
int 
t_error_response_tests_remove(void) {
  coap_delete_pdu(pdu);
  return 0;
}
コード例 #28
0
ファイル: subscribe.c プロジェクト: SlimaneChen/nodes
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);
  }
}
コード例 #29
0
static void CAReceivedPacketCallback(CAEndpoint_t *endpoint, void *data, uint32_t dataLen)
{
    OIC_LOG(DEBUG, TAG, "IN");
    VERIFY_NON_NULL_VOID(data, TAG, "data");

    uint32_t code = CA_NOT_FOUND;
    coap_pdu_t *pdu = (coap_pdu_t *) CAParsePDU((const char *) data, dataLen, &code);
    OICFree(data);
    if (NULL == pdu)
    {
        OIC_LOG(ERROR, TAG, "Parse PDU failed");
        return;
    }

    char uri[CA_MAX_URI_LENGTH] = { };

    if (CA_GET == code || CA_POST == code || CA_PUT == code || CA_DELETE == code)
    {
        CARequestInfo_t *ReqInfo = (CARequestInfo_t *) OICCalloc(1, sizeof(CARequestInfo_t));
        if (NULL == ReqInfo)
        {
            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
            coap_delete_pdu(pdu);
            return;
        }

        CAResult_t res = CAGetRequestInfoFromPDU(pdu, ReqInfo);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG_V(ERROR, TAG, "CAGetRequestInfoFromPDU failed : %d", res);
            OICFree(ReqInfo);
            coap_delete_pdu(pdu);
            return;
        }

        if (NULL != ReqInfo->info.options)
        {
            for (uint32_t i = 0; i < ReqInfo->info.numOptions; i++)
            {
                OIC_LOG_V(DEBUG, TAG, "optionID: %d", ReqInfo->info.options[i].optionID);

                OIC_LOG_V(DEBUG, TAG, "list: %s", ReqInfo->info.options[i].optionData);
            }
        }

        if (NULL != ReqInfo->info.payload)
        {
            OIC_LOG_V(DEBUG, TAG, "Request- payload: %s", ReqInfo->info.payload);
        }

        OIC_LOG_V(DEBUG, TAG, "code: %d", ReqInfo->method);
        OIC_LOG(DEBUG, TAG, "token:");
        OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *) ReqInfo->info.token, CA_MAX_TOKEN_LEN);

        if (g_requestHandler)
        {
            g_requestHandler(endpoint, ReqInfo);
        }

        CADestroyRequestInfoInternal(ReqInfo);
    }
    else
    {
        CAResponseInfo_t *ResInfo = (CAResponseInfo_t *) OICCalloc(1, sizeof(CAResponseInfo_t));
        if (NULL == ResInfo)
        {
            OIC_LOG(ERROR, TAG, "CAReceivedPacketCallback, Memory allocation failed!");
            coap_delete_pdu(pdu);
            return;
        }

        CAResult_t res = CAGetResponseInfoFromPDU(pdu, ResInfo);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG_V(ERROR, TAG, "CAGetResponseInfoFromPDU failed : %d", res);
            OICFree(ResInfo);
            coap_delete_pdu(pdu);
            return;
        }

        if (NULL != ResInfo->info.options)
        {
            for (uint32_t i = 0; i < ResInfo->info.numOptions; i++)
            {
                OIC_LOG_V(DEBUG, TAG, "optionID: %d", ResInfo->info.options[i].optionID);

                OIC_LOG_V(DEBUG, TAG, "list: %s", ResInfo->info.options[i].optionData);
            }
        }

        if (NULL != ResInfo->info.payload)
        {
            OIC_LOG_V(DEBUG, TAG, "payload: %s", ResInfo->info.payload);
        }
        OIC_LOG_V(DEBUG, TAG, "code: %d", ResInfo->result);

        // for retransmission
        void *retransmissionPdu = NULL;
        CARetransmissionReceivedData(&g_retransmissionContext, endpoint, pdu->hdr, pdu->length,
                                     &retransmissionPdu);

        // get token from saved data in retransmission list
        if (retransmissionPdu && CA_EMPTY == code)
        {
            CAResult_t res = CAGetTokenFromPDU((const coap_hdr_t *)retransmissionPdu,
                                               &(ResInfo->info));
            if(CA_STATUS_OK != res)
            {
                OIC_LOG(ERROR, TAG, "fail to get Token from retransmission list");
                OICFree(ResInfo->info.token);
            }
        }
        OICFree(retransmissionPdu);

        if (NULL != ResInfo)
        {
            if (g_responseHandler)
            {
                g_responseHandler(endpoint, ResInfo);
            }
            CADestroyResponseInfoInternal(ResInfo);
        }
    }

    if (pdu)
    {
        coap_delete_pdu(pdu);
    }
    OIC_LOG(DEBUG, TAG, "OUT");
}