Beispiel #1
0
void init_resources(coap_context_t *ctx) {
    coap_resource_t *r;

    r = coap_resource_init(NULL, 0, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index);

    coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
    coap_add_attr(r, (unsigned char *)"title", 5, (unsigned char *)"\"General Info\"", 14, 0);
    coap_add_resource(ctx, r);

    /* store clock base to use in /time */
    my_clock_base = clock_offset;

    r = coap_resource_init((unsigned char *)"time", 4, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
    coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time);
    coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);

    coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
    coap_add_attr(r, (unsigned char *)"title", 5, (unsigned char *)"\"Internal Clock\"", 16, 0);
    coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"\"Ticks\"", 7, 0);
    r->observable = 1;
    coap_add_attr(r, (unsigned char *)"if", 2, (unsigned char *)"\"clock\"", 7, 0);

    coap_add_resource(ctx, r);
    time_resource = r;

#ifndef WITHOUT_ASYNC
    r = coap_resource_init((unsigned char *)"async", 5, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async);

    coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
    coap_add_resource(ctx, r);
#endif /* WITHOUT_ASYNC */
}
Beispiel #2
0
int main(int argc, char* argv[])
{
	coap_context_t*  ctx;
	coap_address_t   serv_addr;
	coap_resource_t* hello_resource;
	fd_set           readfds;    
	/* Prepare the CoAP server socket */ 
	coap_address_init(&serv_addr);
	serv_addr.addr.sin.sin_family      = AF_INET;
	serv_addr.addr.sin.sin_addr.s_addr = INADDR_ANY;
	serv_addr.addr.sin.sin_port        = htons(5683); //default port
	ctx                                = coap_new_context(&serv_addr);
	if (!ctx) exit(EXIT_FAILURE);
	/* Initialize the hello resource */
	hello_resource = coap_resource_init((unsigned char *)"hello", 5, 0);
	coap_register_handler(hello_resource, COAP_REQUEST_GET, hello_async_handler);
	coap_add_resource(ctx, hello_resource);
	/*Listen for incoming connections*/
	while (1) {
		FD_ZERO(&readfds);
		FD_SET( ctx->sockfd, &readfds );
		int result = select( FD_SETSIZE, &readfds, 0, 0, NULL );
		if ( result < 0 ) /* socket error */
		{
			exit(EXIT_FAILURE);
		} 
		else if ( result > 0 && FD_ISSET( ctx->sockfd, &readfds )) /* socket read*/
		{	 
				coap_read( ctx );       
		} 
		sleep(2);
		if(async)
			send_async_response(ctx, ctx->endpoint);
	}    
}
Beispiel #3
0
void CoAPResource::initialize()
{
    const unsigned char *urip = reinterpret_cast<const unsigned char *>(uri.c_str());
    int flags = 0;
    res = coap_resource_init(urip, uri.length(), flags);
    if (res == 0) {
        // Failed to allocate memory for resource descriptor
        throw std::bad_alloc();
    }
}
Beispiel #4
0
void
init_resources(coap_context_t *ctx) {
  coap_resource_t *r;

  r = coap_resource_init(RD_ROOT_STR, RD_ROOT_SIZE, 0);
  coap_register_handler(r, COAP_REQUEST_GET, hnd_get_rd);
  coap_register_handler(r, COAP_REQUEST_POST, hnd_post_rd);

  coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"40", 2, 0);
  coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"\"core-rd\"", 9, 0);
  coap_add_attr(r, (unsigned char *)"ins", 2, (unsigned char *)"\"default\"", 9, 0);

  coap_add_resource(ctx, r);

}
Beispiel #5
0
static void coap_example_thread(void *p)
{
    coap_context_t *ctx = NULL;
    coap_address_t   serv_addr;
    coap_resource_t *resource = NULL;

    snprintf(espressif_data, sizeof(espressif_data), "no data");
    espressif_data_len = strlen(espressif_data);
    coap_set_log_level(COAP_LOGGING_LEVEL);
    while (1) {
        coap_endpoint_t *ep_udp = NULL;
        coap_endpoint_t *ep_tcp = NULL;
        unsigned wait_ms;

        /* Wait for the callback to set the CONNECTED_BIT in the
           event group.
        */
        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);
        ESP_LOGI(TAG, "Connected to AP");

        /* Prepare the CoAP server socket */
        coap_address_init(&serv_addr);
        serv_addr.addr.sin.sin_family      = AF_INET;
        serv_addr.addr.sin.sin_addr.s_addr = INADDR_ANY;
        serv_addr.addr.sin.sin_port        = htons(COAP_DEFAULT_PORT);

        ctx = coap_new_context(NULL);
        if (!ctx) {
           continue;
        }
        ep_udp = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_UDP);
        if (!ep_udp) {
           goto clean_up;
        }
        ep_tcp = coap_new_endpoint(ctx, &serv_addr, COAP_PROTO_TCP);
        if (!ep_tcp) {
           goto clean_up;
        }
        resource = coap_resource_init(coap_make_str_const("Espressif"), 0);
        if (!resource) {
           goto clean_up;
        }
        coap_register_handler(resource, COAP_REQUEST_GET, hnd_espressif_get);
        coap_register_handler(resource, COAP_REQUEST_PUT, hnd_espressif_put);
        coap_register_handler(resource, COAP_REQUEST_DELETE, hnd_espressif_delete);
        /* We possibly want to Observe the GETs */
        coap_resource_set_get_observable(resource, 1);
        coap_add_resource(ctx, resource);

        wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;

        while (1) {
            int result = coap_run_once(ctx, wait_ms);
            if (result < 0) {
                break;
            } else if (result && (unsigned)result < wait_ms) {
                /* decrement if there is a result wait time returned */
                wait_ms -= result;
            }
            if (result) {
                /* result must have been >= wait_ms, so reset wait_ms */
                wait_ms = COAP_RESOURCE_CHECK_TIME * 1000;
            }
        }
    }
clean_up:
    coap_free_context(ctx);
    coap_cleanup();

    vTaskDelete(NULL);
}
Beispiel #6
0
void
init_resources(coap_context_t *ctx) {
  coap_resource_t *r;
  coap_payload_t *test_payload;

  test_payload = coap_new_payload(200);
  if (!test_payload)
    coap_log(LOG_CRIT, "cannot allocate resource /test");
  else {
    test_payload->length = 13;
    memcpy(test_payload->data, "put data here", test_payload->length);
    /* test_payload->media_type is 0 anyway */

    r = coap_resource_init((unsigned char *)"test", 4, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);
    coap_register_handler(r, COAP_REQUEST_POST, hnd_post_test);
    coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_test);
    coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_test);

    coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
    coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"test", 4, 0);
    coap_add_attr(r, (unsigned char *)"if", 2, (unsigned char *)"core#b", 6, 0);
#if 0
    coap_add_attr(r, (unsigned char *)"obs", 3, NULL, 0, 0);
#endif
    coap_add_resource(ctx, r);
    coap_add_payload(r->key, test_payload, NULL);
  }

  /* TD_COAP_BLOCK_01 
   * TD_COAP_BLOCK_02 */
  test_payload = make_large("etsi_iot_01_largedata.txt");
  if (!test_payload)
    coap_log(LOG_CRIT, "cannot allocate resource /large\n");
  else {
    r = coap_resource_init((unsigned char *)"large", 5, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);

    coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"41", 2, 0);
    coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"large", 5, 0);
    coap_add_resource(ctx, r);

    test_payload->flags |= REQUIRE_ETAG;

    coap_add_payload(r->key, test_payload, NULL);
  }

  /* For TD_COAP_CORE_12 */
  test_payload = coap_new_payload(20);
  if (!test_payload)
    coap_log(LOG_CRIT, "cannot allocate resource /seg1/seg2/seg3\n");
  else {
    test_payload->length = 10;
    memcpy(test_payload->data, "segsegseg!", test_payload->length);
    /* test_payload->media_type is 0 anyway */

    r = coap_resource_init((unsigned char *)"seg1/seg2/seg3", 14, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);

    coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
    coap_add_resource(ctx, r);

    coap_add_payload(r->key, test_payload, NULL);
  }

  /* For TD_COAP_CORE_13 */
  r = coap_resource_init((unsigned char *)"query", 5, 0);
  coap_register_handler(r, COAP_REQUEST_GET, hnd_get_query);
  
  coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
  coap_add_resource(ctx, r);
  
  /* For TD_COAP_CORE_16 */
  r = coap_resource_init((unsigned char *)"separate", 8, 0);
  coap_register_handler(r, COAP_REQUEST_GET, hnd_get_separate);

  coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0);
  coap_add_attr(r, (unsigned char *)"rt", 2, (unsigned char *)"separate", 8, 0);
  coap_add_resource(ctx, r);
}
Beispiel #7
0
void 
hnd_post_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 *test_payload;
  size_t len;
  size_t l = 6 + sizeof(void *);
  coap_dynamic_uri_t *uri;
  unsigned char *data;

#define BUFSIZE 20
  int res;
  unsigned char _buf[BUFSIZE];
  unsigned char *buf = _buf;
  size_t buflen = BUFSIZE;

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

  coap_get_data(request, &len, &data);

  /* allocate storage for resource and to hold URI */
  test_payload = coap_new_payload(len);
  uri = (coap_dynamic_uri_t *)coap_malloc(sizeof(coap_dynamic_uri_t) + l);
  if (!(test_payload && uri)) {
    coap_log(LOG_CRIT, "cannot allocate new resource under /test");
    response->hdr->code = COAP_RESPONSE_CODE(500);    
    coap_free(test_payload);
    coap_free(uri);
  } else {
    coap_resource_t *r;

    memset(uri, 0, sizeof(coap_dynamic_uri_t));
    uri->length = min(l, (size_t)snprintf((char *)uri->data, l, "test/%p", (void*)test_payload));
    test_payload->length = len;

    memcpy(test_payload->data, data, len);

    r = coap_resource_init(uri->data, uri->length, 0);
    coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);
    coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource);

    /* set media_type if available */
    option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter);
    if (option) {
      test_payload->media_type = 
	coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option));
    }

    coap_add_resource(ctx, r);
    coap_add_payload(r->key, test_payload, uri);

    /* add Location-Path */
    res = coap_split_path(uri->data, uri->length, buf, &buflen);

    while (res--) {
      coap_add_option(response, COAP_OPTION_LOCATION_PATH,
		      COAP_OPT_LENGTH(buf), COAP_OPT_VALUE(buf));
      
      buf += COAP_OPT_SIZE(buf);      
    }

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

}
Beispiel #8
0
void 
hnd_post_rd(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) {

  size_t len=0;
  unsigned char *databuf;

  unsigned char strBuf[100];
  
  
  unsigned char s[100];

  coap_print_addr(peer, s, 100);

  if (coap_get_data(request, &len, &databuf)){	
    memcpy(strBuf, databuf, len);
    strBuf[len]='\0';
    fprintf(stdout, "%s %s\n", s, strBuf);
  }else{
    fprintf(stdout, "%s\n", s);
  }

  fflush(stdout);
  coap_resource_t *r;
  coap_opt_iterator_t opt_iter;
  coap_opt_t *query;
#define LOCSIZE 68
  unsigned char *loc;
  size_t loc_size;
  str h = {0, NULL}, ins = {0, NULL}, rt = {0, NULL}, lt = {0, NULL};		/* store query parameters */
  unsigned char *buf;

  loc = (unsigned char *)coap_malloc(LOCSIZE);
  if (!loc) {
    response->hdr->code = COAP_RESPONSE_CODE(500);
    return;
  }
  memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE);

  loc_size = RD_ROOT_SIZE;
  loc[loc_size++] = '/';
  
  /* store query parameters for later use */
  query = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter);
  if (query) {
    parse_param((unsigned char *)"h", 1, 
		COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &h);
    parse_param((unsigned char *)"ins", 3, 
		COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &ins);
    parse_param((unsigned char *)"lt", 2, 
		COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &lt);
    parse_param((unsigned char *)"rt", 2, 
		COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &rt);
  } 
  
  if (h.length) {		/* client has specified a node name */
    memcpy(loc + loc_size, h.s, min(h.length, LOCSIZE - loc_size - 1));
    loc_size += min(h.length, LOCSIZE - loc_size - 1);

    if (ins.length && loc_size > 1) {
      loc[loc_size++] = '-';
      memcpy((char *)(loc + loc_size), 
	     ins.s, min(ins.length, LOCSIZE - loc_size - 1));
      loc_size += min(ins.length, LOCSIZE - loc_size - 1);
    }
 
  } else {			/* generate node identifier */
    loc_size += 
      snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, 
	       "%x", request->hdr->id);
    
    if (loc_size > 1) {
      if (ins.length) {
	loc[loc_size++] = '-';
	memcpy((char *)(loc + loc_size), 
	       ins.s, min(ins.length, LOCSIZE - loc_size - 1));
	loc_size += min(ins.length, LOCSIZE - loc_size - 1);
      } else {
	coap_tick_t now;
	coap_ticks(&now);
	
	loc_size += 
	  snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, 
		   "-%x", now);
      }
    }
  }

  /* TODO:
   *   - use lt to check expiration
   */
  
  r = coap_resource_init(loc, loc_size, COAP_RESOURCE_FLAGS_RELEASE_URI);
  coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource);
  coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_resource);
  coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource);

  if (ins.s) {
    buf = (unsigned char *)coap_malloc(ins.length + 2);
    if (buf) {
      /* add missing quotes */
      buf[0] = '"';
      memcpy(buf + 1, ins.s, ins.length);
      buf[ins.length + 1] = '"';
      coap_add_attr(r, (unsigned char *)"ins", 3, buf, ins.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE);
    }
  }

  if (rt.s) {
    buf = (unsigned char *)coap_malloc(rt.length + 2);
    if (buf) {
      /* add missing quotes */
      buf[0] = '"';
      memcpy(buf + 1, rt.s, rt.length);
      buf[rt.length + 1] = '"';
      coap_add_attr(r, (unsigned char *)"rt", 2, buf, rt.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE);
    }
  }

  add_source_address(r, peer);

  {
    rd_t *rd;
    rd = make_rd(peer, request);
    if (rd) {
      coap_hash_path(loc, loc_size, rd->key);
      HASH_ADD(hh, resources, key, sizeof(coap_key_t), rd);
    } else {
      /* FIXME: send error response and delete r */
    }
  }

  coap_add_resource(ctx, r);


  /* 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(loc, loc_size, 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);
    }
  }
}