void CoAPWrapper::clear() { if (coap_ctx_) coap_free_context(coap_ctx_); coap_ctx_ = 0; }
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 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; }
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); }
CoAPContext::~CoAPContext() { // Clean up context coap_free_context(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; }
static void cleanup() { fprintf(stderr, "Exiting...\n"); if(coap_context) coap_free_context(coap_context); coap_context = NULL; if(http_daemon) MHD_stop_daemon(http_daemon); http_daemon = NULL; }