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); } }
int coap_cmd(int argc, char **argv) { coap_context_t *ctx; struct timeval tv, *timeout; coap_tick_t now; coap_queue_t *nextpdu; coap_log_t log_level = LOG_WARNING; coap_set_log_level(log_level); ctx = get_context(); if (!ctx) return -1; init_resources(ctx); while ( !quit ) { nextpdu = coap_peek_next( ctx ); coap_ticks(&now); while (nextpdu && nextpdu->t <= now - ctx->sendqueue_basetime) { coap_retransmit( ctx, coap_pop_next( ctx ) ); nextpdu = coap_peek_next( ctx ); } if ( nextpdu && nextpdu->t <= COAP_RESOURCE_CHECK_TIME ) { /* set timeout if there is a pdu to send before our automatic timeout occurs */ tv.tv_usec = ((nextpdu->t) % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; tv.tv_sec = (nextpdu->t) / COAP_TICKS_PER_SECOND; timeout = &tv; } else { tv.tv_usec = 0; tv.tv_sec = COAP_RESOURCE_CHECK_TIME; timeout = &tv; } (void) timeout; coap_read(ctx); /* read received data */ coap_dispatch(ctx); /* and dispatch PDUs from receivequeue */ #ifndef WITHOUT_ASYNC /* check if we have to send asynchronous responses */ check_async(ctx, now); #endif /* WITHOUT_ASYNC */ #ifndef WITHOUT_OBSERVE /* check if we have to send observe notifications */ coap_check_notify(ctx); #endif /* WITHOUT_OBSERVE */ } coap_free_context( ctx ); return 0; }
int CoAPWrapper::handle_event() { // FIXME: becareful, dont use ACE_Guard at here // maybe cause thread died locked if ( coap_ctx_ ) { coap_read( coap_ctx_ ); /* read received data */ coap_dispatch( coap_ctx_ ); /* and dispatch PDUs from receivequeue */ } return 0; }
void CoAPContext::perform_read() { coap_read(ctx); }
int main(int argc, char **argv) { coap_context_t *ctx; fd_set readfds; struct timeval tv, *timeout; int result; coap_tick_t now; coap_queue_t *nextpdu; char addr_str[NI_MAXHOST] = "::"; char port_str[NI_MAXSERV] = "5683"; int opt; coap_log_t log_level = LOG_WARN; while ((opt = getopt(argc, argv, "A:p:v:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); addr_str[NI_MAXHOST - 1] = '\0'; break; case 'p' : strncpy(port_str, optarg, NI_MAXSERV-1); port_str[NI_MAXSERV - 1] = '\0'; break; case 'v' : log_level = strtol(optarg, NULL, 10); break; default: usage( argv[0], PACKAGE_VERSION ); exit( 1 ); } } coap_set_log_level(log_level); ctx = get_context(addr_str, port_str); if (!ctx) return -1; coap_register_option(ctx, COAP_OPTION_BLOCK2); init_resources(ctx); signal(SIGINT, handle_sigint); while ( !quit ) { FD_ZERO(&readfds); FD_SET( ctx->sockfd, &readfds ); nextpdu = coap_peek_next( ctx ); coap_ticks(&now); while ( nextpdu && nextpdu->t <= now ) { coap_retransmit( ctx, coap_pop_next( ctx ) ); nextpdu = coap_peek_next( ctx ); } if ( nextpdu && nextpdu->t <= now + COAP_RESOURCE_CHECK_TIME_SEC ) { /* set timeout if there is a pdu to send before our automatic timeout occurs */ tv.tv_usec = ((nextpdu->t - now) % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; tv.tv_sec = (nextpdu->t - now) / COAP_TICKS_PER_SECOND; timeout = &tv; } else { tv.tv_usec = 0; tv.tv_sec = COAP_RESOURCE_CHECK_TIME_SEC; timeout = &tv; } result = select( FD_SETSIZE, &readfds, 0, 0, timeout ); if ( result < 0 ) { /* error */ if (errno != EINTR) perror("select"); } else if ( result > 0 ) { /* read from socket */ if ( FD_ISSET( ctx->sockfd, &readfds ) ) { coap_read( ctx ); /* read received data */ coap_dispatch( ctx ); /* and dispatch PDUs from receivequeue */ } } else { /* timeout */ /* coap_check_resource_list( ctx ); */ } /* check if we have to send asynchronous responses */ check_async(ctx, now); } coap_free_context( ctx ); return 0; }