size_t encode_json(char *buf, phydat_t *data, int dim) { strcpy(buf, pl0); size_t pos = strlen(pl0); strcpy(&buf[pos], did); pos += strlen(did); strcpy(&buf[pos], pl1); pos += strlen(pl1); for (int i = 0; i < dim; i++) { pos += fmt_u16_dec(&buf[pos], data->val[i]); buf[pos++] = ','; } if (dim > 0) { --pos; } strcpy(&buf[pos], pl2); pos += strlen(pl2); buf[pos] = '\0'; // printf("test:\n%s\n", buf); // printf("len is %i\n", (int)pos); return pos; }
/* * Server callback for /cli/stats. Accepts either a GET or a PUT. * * GET: Returns the count of packets sent by the CLI. * PUT: Updates the count of packets. Rejects an obviously bad request, but * allows any two byte value for example purposes. Semantically, the only * valid action is to set the value to 0. */ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len) { /* read coap method type in packet */ unsigned method_flag = coap_method2flag(coap_get_code_detail(pdu)); switch(method_flag) { case COAP_GET: gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); /* write the response buffer with the request count value */ size_t payload_len = fmt_u16_dec((char *)pdu->payload, req_count); return gcoap_finish(pdu, payload_len, COAP_FORMAT_TEXT); case COAP_PUT: /* convert the payload to an integer and update the internal value */ if (pdu->payload_len <= 5) { char payload[6] = { 0 }; memcpy(payload, (char *)pdu->payload, pdu->payload_len); req_count = (uint16_t)strtoul(payload, NULL, 10); return gcoap_response(pdu, buf, len, COAP_CODE_CHANGED); } else { return gcoap_response(pdu, buf, len, COAP_CODE_BAD_REQUEST); } } return 0; }
static void test_fmt_u16_dec(void) { char out[5] = "----"; uint16_t val = 6556; uint8_t chars = 0; chars = fmt_u16_dec(out, val); TEST_ASSERT_EQUAL_INT(4, chars); out[chars] = '\0'; TEST_ASSERT_EQUAL_STRING("6556", (char *) out); }
int sock_udp_fmt_endpoint(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *port) { void *addr_ptr; *addr_str = '\0'; if (endpoint->family==AF_INET) { #if defined(SOCK_HAS_IPV4) addr_ptr = (void*)&endpoint->addr.ipv4; #else return -ENOTSUP; #endif } else { #if defined(SOCK_HAS_IPV6) addr_ptr = (void*)&endpoint->addr.ipv6; #else return -ENOTSUP; #endif } if (!inet_ntop(endpoint->family, addr_ptr, addr_str, INET6_ADDRSTRLEN)) { return 0; } #if defined(SOCK_HAS_IPV6) if ((endpoint->family == AF_INET6) && endpoint->netif) { #ifdef RIOT_VERSION char *tmp = addr_str + strlen(addr_str); *tmp++ = '%'; tmp += fmt_u16_dec(tmp, endpoint->netif); *tmp = '0'; #else sprintf(addr_str + strlen(addr_str), "%%%4u", endpoint->netif); #endif } #endif if (port) { *port = endpoint->port; } return strlen(addr_str); }
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; }