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); } }
void CoAPContext::initialize(const coap_address_t& listen_addr) { ctx = coap_new_context(&listen_addr); if (ctx == 0) { // Failed to allocate a new context struct throw std::bad_alloc(); } // Set up Boost socket object socket.assign(boost::asio::ip::udp::v6(), ctx->sockfd); // Put the socket into non-blocking mode. socket.non_blocking(true); // Enqueue an asynchronous socket read perform_operations(); }
static coap_session_t * get_session(const char *group) { coap_context_t *ctx = coap_new_context(NULL); int s; struct addrinfo hints; struct addrinfo *result, *rp; coap_session_t *session; int hops = 16; if (!ctx) return NULL; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */ hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; s = getaddrinfo(group, NULL, &hints, &result); if ( s != 0 ) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); return NULL; } /* iterate through results until success */ for (rp = result; rp != NULL; rp = rp->ai_next) { coap_address_t addr; coap_address_init(&addr); addr.size = rp->ai_addrlen; memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen); session = coap_new_client_session(ctx, NULL, &addr, COAP_PROTO_UDP); if (!session) continue; if (IN6_IS_ADDR_MULTICAST(&addr.addr.sin6.sin6_addr) ) { /* set socket options for multicast */ if ( setsockopt(session->sock.fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&hops, sizeof(hops) ) < 0 ) perror("setsockopt: IPV6_MULTICAST_HOPS"); } freeaddrinfo(result); return session; } fprintf(stderr, "no session available for group '%s'\n", group); freeaddrinfo(result); return NULL; }
coap_context_t *get_context(void) { struct sockaddr_in6 server_addr; coap_context_t *ctx = NULL; server_addr.sin6_family = AF_INET6; memset(&server_addr.sin6_addr, 0, sizeof(server_addr.sin6_addr)); coap_address_t addr; coap_address_init(&addr); addr.size = sizeof(server_addr.sin6_addr); memcpy(&addr.addr, &(server_addr.sin6_addr), addr.size); ctx = coap_new_context(&addr); return ctx; }
coap_context_t * get_context(const char *node, const char *port) { coap_context_t *ctx = NULL; int s; struct addrinfo hints; struct addrinfo *result, *rp; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */ hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; s = getaddrinfo(node, port, &hints, &result); if ( s != 0 ) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); return NULL; } /* iterate through results until success */ for (rp = result; rp != NULL; rp = rp->ai_next) { coap_address_t addr; if (rp->ai_addrlen <= sizeof(addr.addr)) { coap_address_init(&addr); addr.size = rp->ai_addrlen; memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen); ctx = coap_new_context(&addr); if (ctx) { /* TODO: output address:port for successful binding */ goto finish; } } } fprintf(stderr, "no context available for interface '%s'\n", node); finish: freeaddrinfo(result); return ctx; }
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); }