예제 #1
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;
}
예제 #2
0
int
main(int argc, char **argv) {
  struct timeval tv;
  coap_pdu_t  *pdu;
  coap_session_t *session;
  struct sigaction sa;

  if ( argc > 1 && strncmp(argv[1], "-h", 2) == 0 ) {
    usage( argv[0] );
    exit( 1 );
  }

  session = get_session(argc > 1 ? argv[1] : "::1");

  if ( !session )
    return -1;

  id = rand() & INT_MAX;

  memset (&sa, 0, sizeof(sa));
  sigemptyset(&sa.sa_mask);
  sa.sa_handler = handle_sigint;
  sa.sa_flags = 0;
  sigaction (SIGINT, &sa, NULL);
  sigaction (SIGTERM, &sa, NULL);

  while ( !quit ) {

    if (! (pdu = make_pdu( rand() & 0xfff ) ) )
      break;

    coap_send(session, pdu);

    tv.tv_sec = 5; tv.tv_usec = 0;

    select( 0, 0, 0, 0, &tv );

  }

  coap_free_context( session->context );

  return 0;
}
예제 #3
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;
 }
예제 #4
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
}