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::Create(std::string &node, int port, int debug_level) { char port_buf[20]; sprintf(port_buf, "%d", port); port_buf[19] = '\0'; coap_set_log_level(debug_level); coap_ctx_ = get_context(node.c_str(), port_buf); if (coap_ctx_ == 0) { ACE_DEBUG((LM_DEBUG, "Failed to allocate CoAP context\n")); return -1; } 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); }
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; }
int main(int argc, char *argv[]) { int opt; str destination_hostname = {.length = 0, .s = NULL}; uint16_t server_port = 8080, destination_port = COAP_DEFAULT_PORT; char *endptr; struct stat s; while((opt = getopt(argc, argv, "D:P:p:f:h")) != EOF) { switch(opt) { case 'D': destination_hostname.s = (unsigned char *)optarg; destination_hostname.length = strlen(optarg); resolve_address(&destination_hostname, (struct sockaddr *)&destination); break; case 'P': destination_port = (uint16_t)strtoul(optarg, &endptr, 10); if(*endptr != '\0') { fprintf(stderr, "error: invalid port number: %s\n", optarg); return EXIT_FAILURE; } break; case 'p': server_port = (uint16_t)strtoul(optarg, &endptr, 10); if(*endptr != '\0') { fprintf(stderr, "error: invalid port number: %s\n", optarg); return EXIT_FAILURE; } break; case 'f': if(stat(optarg, &s) == -1) { if(ENOENT == errno) { /* does not exist */ fprintf(stderr, "error: %s: %s\n", optarg, strerror(errno)); return EXIT_FAILURE; } else { perror("stat"); return EXIT_FAILURE; } } else { if(S_ISDIR(s.st_mode)) { /* it's a dir */ strncpy(static_files_path, optarg, 63); static_files_path[63] = '\0'; fprintf(stderr, "Will serve static files of directory '%s'\n", static_files_path); } else { /* exists but is no dir */ fprintf(stderr, "error: %s is not a directory\n", optarg); return EXIT_FAILURE; } } break; case 'h': fprintf(stderr, "usage: %s -D coap_host [-P coap_port] [-p HTTP_server_port] [-f static_files_dir]\n", basename(argv[0])); return EXIT_SUCCESS; default: return EXIT_FAILURE; } } if(destination_hostname.s == NULL) { fprintf(stderr, "error: please specify the target coap host of the proxy with the -D option\n"); return EXIT_FAILURE; } destination.sin_port = htons(destination_port); // Register the clean function for when the program exists if(atexit(cleanup) != 0) { perror("atexit"); return EXIT_FAILURE; } // Also call it where a SIGINT is received struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = &signal_handler; if(sigaction(SIGINT, &action, &old_action) != 0) { perror("sigaction"); return EXIT_FAILURE; } start_http_server(server_port); if(http_daemon == NULL) { fprintf(stderr, "error: HTTP server failed to start: %s\n", strerror(errno)); return EXIT_FAILURE; } fprintf(stderr, "HTTP server is listening on port %u (using libmicrohttpd %s)\n", server_port, MHD_get_version()); // Create the CoAP context coap_set_log_level(LOG_DEBUG); coap_context = coap_create_context("0.0.0.0", NULL); coap_register_response_handler(coap_context, coap_response_handler); // Now let microhttpd accept HTTP requests and wait for a signal pause(); return EXIT_SUCCESS; }