コード例 #1
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu8(void)
{
    /* PDU with token and data */
    char teststr[] =
    { 0x42, 0x43, 0x12, 0x34, 0x00, 0x01, 0xff, 0x00 };
    int result;
    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_CON;
    pdu->hdr->code = COAP_RESPONSE_CODE(203);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_token(pdu, 2, (unsigned char *) "\x00\x01");

    CU_ASSERT(result > 0);

    result = coap_add_data(pdu, 1, (unsigned char *) "\0");

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 8);
    CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 7);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #2
0
ファイル: block.c プロジェクト: amnox/tinyos-main
int 
coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *data,
	       unsigned int block_num, unsigned char block_szx) {
  size_t start;
  start = block_num << (block_szx + 4);

  if (len <= start)
    return 0;
  
  return coap_add_data(pdu, 
		       min(len - start, (unsigned int)(1 << (block_szx + 4))),
		       data + start);
}
コード例 #3
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;
}
コード例 #4
0
void hnd_get_index(coap_context_t  *ctx, struct coap_resource_t *resource, 
              coap_address_t *peer, coap_pdu_t *request, str *token,
              coap_pdu_t *response) {
    unsigned char buf[3];

    response->hdr->code = COAP_RESPONSE_CODE(205);

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

    coap_add_option(response, COAP_OPTION_MAXAGE,
                    coap_encode_var_bytes(buf, 0x2ffff), buf);

    coap_add_data(response, strlen(INDEX), (unsigned char *)INDEX);
}
コード例 #5
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu11(void)
{
    /* data too long for PDU */
    size_t old_max = pdu->max_size;
    int result;

    coap_pdu_clear(pdu, 8); /* clear PDU, with small maximum */

    CU_ASSERT(pdu->data == NULL);
    result = coap_add_data(pdu, 10, (unsigned char *) "0123456789");

    CU_ASSERT(result == 0);
    CU_ASSERT(pdu->data == NULL);

    pdu->max_size = old_max;
}
コード例 #6
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu9(void)
{
    /* PDU with options and data */
    char teststr[] =
    { 0x60, 0x44, 0x12, 0x34, 0x48, 's', 'o', 'm', 'e', 'e', 't', 'a', 'g', 0x10, 0xdd, 0x11, 0x04,
            's', 'o', 'm', 'e', 'r', 'a', 't', 'h', 'e', 'r', 'l', 'o', 'n', 'g', 'u', 'r', 'i',
            0xff, 'd', 'a', 't', 'a' };
    int result;

    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_ACK;
    pdu->hdr->code = COAP_RESPONSE_CODE(204);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_option(pdu, COAP_OPTION_ETAG, 8, (unsigned char *) "someetag");

    CU_ASSERT(result == 9);
    CU_ASSERT(pdu->max_delta == 4);
    CU_ASSERT(pdu->length == 13);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL);

    CU_ASSERT(result == 1);
    CU_ASSERT(pdu->max_delta == 5);
    CU_ASSERT(pdu->length == 14);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_PROXY_URI, 17, (unsigned char *) "someratherlonguri");

    CU_ASSERT(result == 20);
    CU_ASSERT(pdu->max_delta == 35);
    CU_ASSERT(pdu->length == 34);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_data(pdu, 4, (unsigned char *) "data");

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 39);
    CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 35);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #7
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu6(void)
{
    /* PDU with data */
    char teststr[] =
    { 0x50, 0x02, 0x12, 0x34, 0xff, '1', '2', '3', '4', '5', '6', '7', '8' };
    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_NON;
    pdu->hdr->code = COAP_REQUEST_POST;
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);
    CU_ASSERT_PTR_NULL(pdu->data);

    coap_add_data(pdu, 8, (unsigned char *) "12345678");

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #8
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;
}
コード例 #9
0
/*
 * Function:    _wilddog_coap_addData
 * Description: add data.
 * Input:       p_arg: p_coap/p_payload/d_payloadLen. 
 * Output:      N/A
 * Return:      WILDDOG_ERR_NOERR or WILDDOG_ERR_NULL.
*/
STATIC Wilddog_Return_T WD_SYSTEM _wilddog_coap_addData
    (
    Protocol_Arg_Payload_T *p_arg,
    int flag
    )
{
    int ret;
    if( p_arg == NULL || 
        p_arg->p_pkg == NULL ||
        p_arg->p_payload == NULL)
        return WILDDOG_ERR_NULL;
    ret = coap_add_data((coap_pdu_t*)p_arg->p_pkg,\
                        p_arg->d_payloadLen, \
                        p_arg->p_payload);
   
    if(0 == ret)
        return WILDDOG_ERR_NULL;

    return WILDDOG_ERR_NOERR;
}
コード例 #10
0
ファイル: rd.c プロジェクト: joaopedrotaveira/libcoap
void 
hnd_get_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
	      coap_address_t *peer, coap_pdu_t *request, str *token,
	      coap_pdu_t *response, void *userdata) {
  rd_t *rd = NULL;
  unsigned char buf[3];
  
  HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);

  response->hdr->code = COAP_RESPONSE_CODE(205);

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

  if (rd && rd->etag_len)
    coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag);

  if (rd && rd->data.s)
    coap_add_data(response, rd->data.length, rd->data.s);
}
コード例 #11
0
ファイル: etsi_iot_01.c プロジェクト: erichkeane/libcoap
void 
hnd_get_query(coap_context_t  *ctx, struct coap_resource_t *resource, 
	      coap_address_t *peer, coap_pdu_t *request, str *token,
	      coap_pdu_t *response) {
  coap_opt_iterator_t opt_iter;
  coap_opt_filter_t f;
  coap_opt_t *q;
  size_t len, L;
  unsigned char buf[70];

  (void)ctx;
  (void)resource;
  (void)peer;
  (void)token;

  response->hdr->code = COAP_RESPONSE_CODE(205);

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

  coap_option_filter_clear(f);
  coap_option_setb(f, COAP_OPTION_URI_QUERY);
  
  coap_option_iterator_init(request, &opt_iter, f);
  
  len = 0;
  while ((len < sizeof(buf)) && (q = coap_option_next(&opt_iter))) {
    L = min(sizeof(buf) - len, 11);
    memcpy(buf + len, "Uri-Query: ", L);
    len += L;

    L = min(sizeof(buf) - len, COAP_OPT_LENGTH(q));
    memcpy(buf + len, COAP_OPT_VALUE(q), L);
    len += L;
    
    if (len < sizeof(buf))
      buf[len++] = '\n';
  }
  
  coap_add_data(response, len, buf);
}
コード例 #12
0
static coap_pdu_t *
make_pdu( unsigned int value ) {
  coap_pdu_t *pdu;
  unsigned char enc;
  static unsigned char buf[20];
  int len, ls;

  if (!(pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU)))
    return NULL;

  pdu->type = COAP_MESSAGE_NON;
  pdu->code = COAP_REQUEST_POST;
  pdu->tid = id++;

  enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value,ls);

  len = sprintf((char *)buf, "%c%u", enc, COAP_PSEUDOFP_DECODE_8_4(enc));
  coap_add_data( pdu, len, buf );

  return pdu;
}
コード例 #13
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu7(void)
{
    /* PDU with empty data */
    char teststr[] =
    { 0x40, 0x43, 0x12, 0x34 };
    int result;
    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_CON;
    pdu->hdr->code = COAP_RESPONSE_CODE(203);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_data(pdu, 0, NULL);

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 4);
    CU_ASSERT_PTR_NULL(pdu->data);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #14
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;
 }
コード例 #15
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;
}
コード例 #16
0
ファイル: etsi_iot_01.c プロジェクト: erichkeane/libcoap
void 
hnd_get_resource(coap_context_t  *ctx, struct coap_resource_t *resource, 
		 coap_address_t *peer, coap_pdu_t *request, str *token,
		 coap_pdu_t *response) {
  coap_key_t etag;
  unsigned char buf[2];
  coap_payload_t *test_payload;
  coap_block_t block;

  (void)ctx;
  (void)peer;
  (void)token;

  test_payload = coap_find_payload(resource->key);
  if (!test_payload) {
    response->hdr->code = COAP_RESPONSE_CODE(500);
    
    return;
  }

  response->hdr->code = COAP_RESPONSE_CODE(205);

  coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
	  coap_encode_var_bytes(buf, test_payload->media_type), buf);

  /* add etag for the resource */
  if (test_payload->flags & REQUIRE_ETAG) {
    memset(etag, 0, sizeof(etag));
    coap_hash(test_payload->data, test_payload->length, etag);
    coap_add_option(response, COAP_OPTION_ETAG, sizeof(etag), etag);
  }
      
  if (request) {
    int res;

    if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) {
      res = coap_write_block_opt(&block, COAP_OPTION_BLOCK2, response,
				 test_payload->length);

      switch (res) {
      case -2:			/* illegal block */
	response->hdr->code = COAP_RESPONSE_CODE(400);
	goto error;
      case -1:			/* should really not happen */
	assert(0);
	/* fall through if assert is a no-op */
      case -3:			/* cannot handle request */
	response->hdr->code = COAP_RESPONSE_CODE(500);
	goto error;
      default:			/* everything is good */
	;
      }
      
      coap_add_block(response, test_payload->length, test_payload->data,
		     block.num, block.szx);
    } else {
      if (!coap_add_data(response, test_payload->length, test_payload->data)) {
	/* set initial block size, will be lowered by
	 * coap_write_block_opt) automatically */
	block.szx = 6;
	coap_write_block_opt(&block, COAP_OPTION_BLOCK2, response,
			     test_payload->length);
	
	coap_add_block(response, test_payload->length, test_payload->data,
		       block.num, block.szx);	
      }
    }    
  } else {		      /* this is a notification, block is 0 */
    /* FIXME: need to store block size with subscription */
  }
  
  return;

 error:
  coap_add_data(response, 
		strlen(coap_response_phrase(response->hdr->code)),
		(unsigned char *)coap_response_phrase(response->hdr->code));
}
コード例 #17
0
ファイル: test_pdu.c プロジェクト: prajoshpremdas/iotivity
void t_encode_pdu10(void)
{
    /* PDU with token, options and data */
    char teststr[] =
    { 0x62, 0x44, 0x12, 0x34, 0x00, 0x00, 0x8d, 0xf2, 'c', 'o', 'a', 'p', ':', '/', '/', 'e', 'x',
            'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', '/', '1', '2', '3', '4', '5', '/', '%',
            '3', 'F', 'x', 'y', 'z', '/', '3', '0', '4', '8', '2', '3', '4', '2', '3', '4', '/',
            '2', '3', '4', '0', '2', '3', '4', '8', '2', '3', '4', '/', '2', '3', '9', '0', '8',
            '4', '2', '3', '4', '-', '2', '3', '/', '%', 'A', 'B', '%', '3', '0', '%', 'a', 'f',
            '/', '+', '1', '2', '3', '/', 'h', 'f', 'k', 's', 'd', 'h', '/', '2', '3', '4', '8',
            '0', '-', '2', '3', '4', '-', '9', '8', '2', '3', '5', '/', '1', '2', '0', '4', '/',
            '2', '4', '3', '5', '4', '6', '3', '4', '5', '3', '4', '5', '2', '4', '3', '/', '0',
            '1', '9', '8', 's', 'd', 'n', '3', '-', 'a', '-', '3', '/', '/', '/', 'a', 'f', 'f',
            '0', '9', '3', '4', '/', '9', '7', 'u', '2', '1', '4', '1', '/', '0', '0', '0', '2',
            '/', '3', '9', '3', '2', '4', '2', '3', '5', '3', '2', '/', '5', '6', '2', '3', '4',
            '0', '2', '3', '/', '-', '-', '-', '-', '/', '=', '1', '2', '3', '4', '=', '/', '0',
            '9', '8', '1', '4', '1', '-', '9', '5', '6', '4', '6', '4', '3', '/', '2', '1', '9',
            '7', '0', '-', '-', '-', '-', '-', '/', '8', '2', '3', '6', '4', '9', '2', '3', '4',
            '7', '2', 'w', 'e', 'r', 'e', 'r', 'e', 'w', 'r', '0', '-', '9', '2', '1', '-', '3',
            '9', '1', '2', '3', '-', '3', '4', '/', 0x0d, 0x01, '/', '/', '4', '9', '2', '4', '0',
            '3', '-', '-', '0', '9', '8', '/', 0xc1, '*', 0xff, 'd', 'a', 't', 'a' };
    int result;

    coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */

    pdu->hdr->type = COAP_MESSAGE_ACK;
    pdu->hdr->code = COAP_RESPONSE_CODE(204);
    pdu->hdr->id = htons(0x1234);

    CU_ASSERT(pdu->length == 4);

    result = coap_add_token(pdu, 2, (unsigned char *) "\0\0");

    CU_ASSERT(result > 0);
    result =
            coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 255,
                    (unsigned char *) "coap://example.com/12345/%3Fxyz/3048234234/23402348234/239084234-23/%AB%30%af/+123/hfksdh/23480-234-98235/1204/243546345345243/0198sdn3-a-3///aff0934/97u2141/0002/3932423532/56234023/----/=1234=/098141-9564643/21970-----/82364923472wererewr0-921-39123-34/");

    CU_ASSERT(result == 257);
    CU_ASSERT(pdu->max_delta == 8);
    CU_ASSERT(pdu->length == 263);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 14,
            (unsigned char *) "//492403--098/");

    CU_ASSERT(result == 16);
    CU_ASSERT(pdu->max_delta == 8);
    CU_ASSERT(pdu->length == 279);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, 1, (unsigned char *) "*");

    CU_ASSERT(result == 2);
    CU_ASSERT(pdu->max_delta == 20);
    CU_ASSERT(pdu->length == 281);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_data(pdu, 4, (unsigned char *) "data");

    CU_ASSERT(result > 0);
    CU_ASSERT(pdu->length == 286);
    CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 282);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}
コード例 #18
0
void hnd_get_time(coap_context_t  *ctx, struct coap_resource_t *resource, 
             coap_address_t *peer, coap_pdu_t *request, str *token,
             coap_pdu_t *response) {
    coap_opt_iterator_t opt_iter;
    coap_opt_t *option;
    unsigned char buf[40];
    size_t len;
    time_t now;
    coap_tick_t t;
    coap_subscription_t *subscription;

    /* FIXME: return time, e.g. in human-readable by default and ticks
     * when query ?ticks is given. */

    /* if my_clock_base was deleted, we pretend to have no such resource */
    response->hdr->code = 
        my_clock_base ? COAP_RESPONSE_CODE(205) : COAP_RESPONSE_CODE(404);

    if (request != NULL &&
        coap_check_option(request, COAP_OPTION_OBSERVE, &opt_iter)) {
        subscription = coap_add_observer(resource, peer, token);
        if (subscription) {
            subscription->non = request->hdr->type == COAP_MESSAGE_NON;
            coap_add_option(response, COAP_OPTION_OBSERVE, 0, NULL);
        }
    }
    if (resource->dirty == 1)
        coap_add_option(response, COAP_OPTION_OBSERVE, 
                        coap_encode_var_bytes(buf, ctx->observe), buf);


    if (my_clock_base)
        coap_add_option(response, COAP_OPTION_CONTENT_FORMAT,
                        coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);

    coap_add_option(response, COAP_OPTION_MAXAGE,
                    coap_encode_var_bytes(buf, 0x01), buf);

    if (my_clock_base) {

        /* calculate current time */
        coap_ticks(&t);
        now = my_clock_base + (t / COAP_TICKS_PER_SECOND);

        if (request != NULL
            && (option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))
            && memcmp(COAP_OPT_VALUE(option), "ticks",
                      min(5, COAP_OPT_LENGTH(option))) == 0) {
            /* output ticks */
            len = snprintf((char *)buf, 
                           min(sizeof(buf), response->max_size - response->length),
                           "%u", (unsigned int)now);
            coap_add_data(response, len, buf);

        } else {			/* output human-readable time */
            struct tm *tmp;
            tmp = gmtime(&now);
            len = strftime((char *)buf, 
                           min(sizeof(buf), response->max_size - response->length),
                           "%b %d %H:%M:%S", tmp);
            coap_add_data(response, len, buf);
        }
    }
}
コード例 #19
0
void CoAPRDLookUpEPResource::handler_get(CoAPCallback &callback)
{
    std::string ep_result;
    
    find_ep_result(ep_result);

    if (ep_result.empty())
    {
    
        coap_pdu_t *response = (coap_pdu_t*)callback.response_;
        
        /* create response */
        response->hdr->code = COAP_RESPONSE_404;
    }
    else
    {

        unsigned char buf[3];
        std::string result;
        coap_block_t block_opt;
        
        coap_pdu_t *response = (coap_pdu_t*)callback.response_;
        
        /* create response */
        response->hdr->code = COAP_RESPONSE_CODE(205);
        
        coap_add_option(response, COAP_OPTION_CONTENT_TYPE,
                        coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf);
        
        coap_add_option(response, COAP_OPTION_MAXAGE,
                        coap_encode_var_bytes(buf, 0x2ffff), buf);
                        
        /* 1.if result more than 1024 byte, block wise transfer,
         * otherwise directly transfer
         * 2. if client set block2 option, response must be block-wise transferred
         */
        if (1 == coap_get_block((coap_pdu_t*)callback.request_, COAP_OPTION_BLOCK2, &block_opt))
        {
            ACE_DEBUG((LM_DEBUG, "send data by block from %d(size=%d)\n",block_opt.num,block_opt.szx));

            if (1 == send_data_by_block(response, &block_opt, (unsigned char*)ep_result.c_str(), ep_result.length()))
            {
                return;
            }
        }

        if (ep_result.length() > 1024 )
        {
            block_opt.szx = 6;
            block_opt.m = 1;
            block_opt.num = 0;

            ACE_DEBUG((LM_DEBUG, "send data by block from %d(size=%d)\n",block_opt.num,block_opt.szx));

            if (1 == send_data_by_block(response, &block_opt, (unsigned char*)ep_result.c_str(), ep_result.length()))
            {
                return;
            }            
        }

        /* at here, we send data directly*/
        {
        
            ACE_DEBUG((LM_DEBUG, "send data directly(len=%d)\n", ep_result.length()));
            
            coap_add_data(response, ep_result.length(), (unsigned char*)ep_result.c_str());
        }
    }
    
}