static int _update_remove(unsigned code, gcoap_resp_handler_t handle) { coap_pkt_t pkt; if (_rd_loc[0] == 0) { return RDCLI_NORD; } /* build CoAP request packet */ int res = gcoap_req_init(&pkt, buf, sizeof(buf), code, _rd_loc); if (res < 0) { return RDCLI_ERR; } coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); ssize_t pkt_len = gcoap_finish(&pkt, 0, COAP_FORMAT_NONE); /* send request */ gcoap_req_send2(buf, pkt_len, &_rd_remote, handle); /* synchronize response */ return _sync(); }
static int _discover_internal(const sock_udp_ep_t *remote, char *regif, size_t maxlen) { coap_pkt_t pkt; /* save pointer to result buffer */ _regif_buf = regif; _regif_buf_len = maxlen; /* do URI discovery for the registration interface */ int res = gcoap_req_init(&pkt, buf, sizeof(buf), COAP_METHOD_GET, "/.well-known/core"); if (res < 0) { return RDCLI_ERR; } coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); gcoap_add_qstring(&pkt, "rt", "core.rd"); size_t pkt_len = gcoap_finish(&pkt, 0, COAP_FORMAT_NONE); res = gcoap_req_send2(buf, pkt_len, remote, _on_discover); if (res < 0) { return RDCLI_ERR; } return _sync(); }
int rdcli_register(const sock_udp_ep_t *remote, const char *regif) { assert(remote); int res; ssize_t pkt_len; int retval; coap_pkt_t pkt; _lock(); /* if no registration interface is given, we will need to trigger a URI * discovery for it first (see section 5.2) */ if (regif == NULL) { retval = _discover_internal(remote, _rd_regif, sizeof(_rd_regif)); if (retval != RDCLI_OK) { goto end; } } else { if (strlen(_rd_regif) >= sizeof(_rd_regif)) { retval = RDCLI_OVERFLOW; goto end; } strncpy(_rd_regif, regif, sizeof(_rd_regif)); } /* build and send CoAP POST request to the RD's registration interface */ res = gcoap_req_init(&pkt, buf, sizeof(buf), COAP_METHOD_POST, _rd_regif); if (res < 0) { retval = RDCLI_ERR; goto end; } /* set some packet options and write query string */ coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON); rdcli_common_add_qstring(&pkt); /* add the resource description as payload */ res = gcoap_get_resource_list(pkt.payload, pkt.payload_len, COAP_FORMAT_LINK); if (res < 0) { retval = RDCLI_ERR; goto end; } /* finish up the packet */ pkt_len = gcoap_finish(&pkt, res, COAP_FORMAT_LINK); /* send out the request */ res = gcoap_req_send2(buf, pkt_len, remote, _on_register); if (res < 0) { retval = RDCLI_ERR; goto end; } retval = _sync(); end: /* if we encountered any error, we mark the client as not connected */ if (retval != RDCLI_OK) { _rd_loc[0] = '\0'; } #ifdef MODULE_RDCLI_STANDALONE else { rdcli_standalone_signal(true); } #endif mutex_unlock(&_mutex); return retval; }
int gcoap_cli_cmd(int argc, char **argv) { /* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */ char *method_codes[] = {"get", "post", "put"}; uint8_t buf[GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; size_t len; if (argc == 1) { /* show help for main commands */ goto end; } for (size_t i = 0; i < sizeof(method_codes) / sizeof(char*); i++) { if (strcmp(argv[1], method_codes[i]) == 0) { if (argc == 5 || argc == 6) { if (argc == 6) { gcoap_req_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, i+1, argv[4]); memcpy(pdu.payload, argv[5], strlen(argv[5])); len = gcoap_finish(&pdu, strlen(argv[5]), COAP_FORMAT_TEXT); } else { len = gcoap_request(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, i+1, argv[4]); } printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu), (unsigned) len); if (!_send(&buf[0], len, argv[2], argv[3])) { puts("gcoap_cli: msg send failed"); } else { /* send Observe notification for /cli/stats */ switch (gcoap_obs_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, &_resources[0])) { case GCOAP_OBS_INIT_OK: DEBUG("gcoap_cli: creating /cli/stats notification\n"); size_t payload_len = fmt_u16_dec((char *)pdu.payload, req_count); len = gcoap_finish(&pdu, payload_len, COAP_FORMAT_TEXT); gcoap_obs_send(&buf[0], len, &_resources[0]); break; case GCOAP_OBS_INIT_UNUSED: DEBUG("gcoap_cli: no observer for /cli/stats\n"); break; case GCOAP_OBS_INIT_ERR: DEBUG("gcoap_cli: error initializing /cli/stats notification\n"); break; } } return 0; } else { printf("usage: %s <get|post|put> <addr> <port> <path> [data]\n", argv[0]); return 1; } } } if (strcmp(argv[1], "info") == 0) { if (argc == 2) { uint8_t open_reqs = gcoap_op_state(); printf("CoAP server is listening on port %u\n", GCOAP_PORT); printf(" CLI requests sent: %u\n", req_count); printf("CoAP open requests: %u\n", open_reqs); return 0; } } end: printf("usage: %s <get|post|put|info>\n", argv[0]); return 1; }