Exemplo n.º 1
0
static void
hnd_espressif_put(coap_context_t *ctx,
                  coap_resource_t *resource,
                  coap_session_t *session,
                  coap_pdu_t *request,
                  coap_binary_t *token,
                  coap_string_t *query,
                  coap_pdu_t *response)
{
    size_t size;
    unsigned char *data;

    coap_resource_notify_observers(resource, NULL);

    if (strcmp (espressif_data, "no data") == 0) {
        response->code = COAP_RESPONSE_CODE(201);
    }
    else {
        response->code = COAP_RESPONSE_CODE(204);
    }

    /* coap_get_data() sets size to 0 on error */
    (void)coap_get_data(request, &size, &data);

    if (size == 0) {      /* re-init */
        snprintf(espressif_data, sizeof(espressif_data), "no data");
        espressif_data_len = strlen(espressif_data);
    } else {
        espressif_data_len = size > sizeof (espressif_data) ? sizeof (espressif_data) : size;
        memcpy (espressif_data, data, espressif_data_len);
    }
}
Exemplo n.º 2
0
void hnd_put_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_tick_t t;
    size_t size;
    unsigned char *data;

    /* FIXME: re-set my_clock_base to clock_offset if my_clock_base == 0
     * and request is empty. When not empty, set to value in request payload
     * (insist on query ?ticks). Return Created or Ok.
     */

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

    resource->dirty = 1;

    coap_get_data(request, &size, &data);

    if (size == 0)		/* re-init */
        my_clock_base = clock_offset;
    else {
        my_clock_base = 0;
        coap_ticks(&t);
        while(size--) 
            my_clock_base = my_clock_base * 10 + *data++;
        my_clock_base -= t / COAP_TICKS_PER_SECOND;
    }
}
Exemplo n.º 3
0
void 
hnd_put_test(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;
  coap_payload_t *payload;
  size_t len;
  unsigned char *data;

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

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

  coap_get_data(request, &len, &data);

  payload = coap_find_payload(resource->key);
  if (payload && payload->max_data < len) { /* need more storage */
    coap_delete_payload(payload);
    payload = NULL;
    /* bug: when subsequent coap_new_payload() fails, our old contents
       is gone */
  }

  if (!payload) {		/* create new payload */
    payload = coap_new_payload(len);
    if (!payload)
      goto error;

    coap_add_payload(resource->key, payload, NULL);
  } 
  payload->length = len;
  memcpy(payload->data, data, len);

  option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter);
  if (option) {
    /* set media type given in request */
    payload->media_type = 
      coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option));
  } else {
    /* set default value */
    payload->media_type = COAP_MEDIATYPE_TEXT_PLAIN;
  }
  /* FIXME: need to change attribute ct of resource. 
     To do so, we need dynamic management of the attribute value
  */

  return;
 error:
  warn("cannot modify resource\n");
  response->hdr->code = COAP_RESPONSE_CODE(500);
}
Exemplo n.º 4
0
void
t_error_response2(void) {
  unsigned char 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);
}
Exemplo n.º 5
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->type = COAP_MESSAGE_CON;
  pdu->tid = 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->used_size == sizeof(teststr) - 4);
  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
  CU_ASSERT(response->token_length == 0);
  CU_ASSERT(response->code == 0x80);
  CU_ASSERT(response->tid == 0x1234);
  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);
}
Exemplo n.º 6
0
void CoAP_RD_Resource::response_ok(CoAPResource *node, CoAPCallback &callback)
{
#define LOCSIZE 68

    coap_pdu_t *response = (coap_pdu_t*)callback.response_;
    
    /* create response */
    response->hdr->code = COAP_RESPONSE_CODE(201);

    {
        /* split path into segments and add Location-Path options */
        unsigned char _b[LOCSIZE];
        unsigned char *b = _b;
        size_t buflen = sizeof(_b);
        int nseg;

        nseg = coap_split_path((unsigned char*)node->uri().c_str(), node->uri().length(), b, &buflen);
        while (nseg--)
        {
            coap_add_option(response, COAP_OPTION_LOCATION_PATH,
                            COAP_OPT_LENGTH(b), COAP_OPT_VALUE(b));
            b += COAP_OPT_SIZE(b);
        }
    }
    
}
Exemplo n.º 7
0
void
t_error_response3(void) {
  const unsigned char code = COAP_RESPONSE_CODE(402);
  unsigned char 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);
}
Exemplo n.º 8
0
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);
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
0
static void
t_error_response3(void) {
  const uint8_t 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->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 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->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);
}
Exemplo n.º 11
0
void hnd_get_async(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 long delay = 5;
    size_t size;

    if (async) {
        if (async->id != request->hdr->id) {
            coap_opt_filter_t f;
            coap_option_filter_clear(f);
            response->hdr->code = COAP_RESPONSE_CODE(503);
        }
        return;
    }

    option = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
    if (option) {
        unsigned char *p = COAP_OPT_VALUE(option);

        delay = 0;
        for (size = COAP_OPT_LENGTH(option); size; --size, ++p)
            delay = delay * 10 + (*p - '0');
    }

    async = coap_register_async(ctx, peer, request, 
                                COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM,
                                (void *)(COAP_TICKS_PER_SECOND * delay));
}
Exemplo n.º 12
0
void
t_error_response1(void) {
  unsigned char 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);
}
Exemplo n.º 13
0
void 
hnd_get_rd(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) {
  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_APPLICATION_LINK_FORMAT), buf);

  coap_add_option(response, COAP_OPTION_MAXAGE,
	  coap_encode_var_bytes(buf, 0x2ffff), buf);
}
Exemplo n.º 14
0
static void
hnd_espressif_delete(coap_context_t *ctx,
                     coap_resource_t *resource,
                     coap_session_t *session,
                     coap_pdu_t *request,
                     coap_binary_t *token,
                     coap_string_t *query,
                     coap_pdu_t *response)
{
    coap_resource_notify_observers(resource, NULL);
    snprintf(espressif_data, sizeof(espressif_data), "no data");
    espressif_data_len = strlen(espressif_data);
    response->code = COAP_RESPONSE_CODE(202);
}
Exemplo n.º 15
0
/* DELETE handler for dynamic resources created by POST /test */
void 
hnd_delete_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_payload_t *payload;

  payload = coap_find_payload(resource->key);

  if (payload)
    coap_delete_payload(payload);

  coap_delete_resource(ctx, resource->key);

  response->hdr->code = COAP_RESPONSE_CODE(202);
}
Exemplo n.º 16
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);
}
Exemplo n.º 17
0
void 
hnd_delete_test(coap_context_t  *ctx, struct coap_resource_t *resource, 
		coap_address_t *peer, coap_pdu_t *request, str *token,
		coap_pdu_t *response) {
  /* the ETSI validation tool does not like empty resources... */
#if 0
  coap_payload_t *payload;
  payload = coap_find_payload(resource->key);

  if (payload)
    payload->length = 0;
#endif

  response->hdr->code = COAP_RESPONSE_CODE(202);
}
Exemplo n.º 18
0
static void
make_bad_request(CoAPCallback &callback, int code)
{
    unsigned char buf[3];
    coap_pdu_t *response = (coap_pdu_t*)callback.response_;

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

    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);   

}
Exemplo n.º 19
0
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);
}
Exemplo n.º 20
0
/* handler for TD_COAP_CORE_16 */
void 
hnd_get_separate(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;
  coap_opt_filter_t f;
  unsigned long delay = 5;

  (void)resource;
  (void)token;

  if (async) {
    if (async->id != request->hdr->id) {
      coap_opt_filter_t f;
      coap_option_filter_clear(f);
      response->hdr->code = COAP_RESPONSE_CODE(503);
    }
    return;
  }

  /* search for option delay in query list */
  coap_option_filter_clear(f);
  coap_option_setb(f, COAP_OPTION_URI_QUERY);
  
  coap_option_iterator_init(request, &opt_iter, f);
  
  while ((option = coap_option_next(&opt_iter))) {
    if (strncmp("delay=", (char *)COAP_OPT_VALUE(option), 6) == 0) {
      int i;
      unsigned long d = 0;
      
      for (i = 6; i < COAP_OPT_LENGTH(option); ++i)
	d = d * 10 + COAP_OPT_VALUE(option)[i] - '0';

      /* don't allow delay to be less than COAP_RESOURCE_CHECK_TIME*/
      delay = d < COAP_RESOURCE_CHECK_TIME_SEC 
	? COAP_RESOURCE_CHECK_TIME_SEC
	: d;
      debug("set delay to %lu\n", delay);
      break;
    }
  }

  async = coap_register_async(ctx, peer, request, COAP_ASYNC_SEPARATE,
			      (void *)(COAP_TICKS_PER_SECOND * delay));
}
Exemplo n.º 21
0
void 
hnd_delete_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;

  HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd);
  if (rd) {
    HASH_DELETE(hh, resources, rd);
    rd_delete(rd);
  }
  /* FIXME: link attributes for resource have been created dynamically
   * using coap_malloc() and must be released. */
  coap_delete_resource(ctx, resource->key);

  response->hdr->code = COAP_RESPONSE_CODE(202);
}
Exemplo n.º 22
0
void
t_error_response8(void) {
  const unsigned char code = COAP_RESPONSE_CODE(503);
  unsigned char 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;

  if (COAP_MAX_OPT < 2000) {
    CU_PASS("WARNING: bypassed due to COAP_MAX_OPT < 2000");
    return;
  }
  
  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);
}
Exemplo n.º 23
0
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;
}
Exemplo n.º 24
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);
}
Exemplo n.º 25
0
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);
}
Exemplo n.º 26
0
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);
}
Exemplo n.º 27
0
static void
t_error_response8(void) {
  const uint8_t 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->type = COAP_MESSAGE_CON;
  coap_add_token(pdu, 5, (const uint8_t *)"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->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);
}
Exemplo n.º 28
0
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;
 }
Exemplo n.º 29
0
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);
}
Exemplo n.º 30
0
void t_encode_pdu5(void)
{
    /* PDU with token and options */
    char teststr[] =
    { 0x68, 0x84, 0x12, 0x34, '1', '2', '3', '4', '5', '6', '7', '8', 0x18, 0x41, 0x42, 0x43, 0x44,
            0x45, 0x46, 0x47, 0x48, 0xd1, 0x03, 0x12 };
    int result;

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

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

    CU_ASSERT(pdu->length == 4);

    result = coap_add_token(pdu, 8, (unsigned char *) "12345678");

    CU_ASSERT(pdu->length == 12);

    result = coap_add_option(pdu, COAP_OPTION_IF_MATCH, 8, (unsigned char *) "ABCDEFGH");

    CU_ASSERT(result == 9);
    CU_ASSERT(pdu->max_delta == 1);
    CU_ASSERT(pdu->length == 21);
    CU_ASSERT_PTR_NULL(pdu->data);

    result = coap_add_option(pdu, COAP_OPTION_ACCEPT, 1, (unsigned char *) "\x12");

    CU_ASSERT(result == 3);
    CU_ASSERT(pdu->max_delta == 17);
    CU_ASSERT(pdu->length == 24);
    CU_ASSERT_PTR_NULL(pdu->data);

    CU_ASSERT(pdu->length == sizeof(teststr));
    CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0);
}