1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 06:23:53 +01:00

net/gcoap: Flatten command line handling to prepare for optional arguments

This commit is contained in:
Ken Bannister 2017-07-06 07:13:50 -04:00
parent 0e93566814
commit 25f40df447

View File

@ -180,61 +180,66 @@ int gcoap_cli_cmd(int argc, char **argv)
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) {
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;
}
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;
/* find method code */
int code_pos = -1;
for (size_t i = 0; i < sizeof(method_codes) / sizeof(char*); i++) {
if (strcmp(argv[1], method_codes[i]) == 0) {
code_pos = i;
}
}
if (code_pos == -1) {
goto end;
}
if (argc == 5 || argc == 6) {
if (argc == 6) {
gcoap_req_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, code_pos+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, code_pos+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;
}
end:
printf("usage: %s <get|post|put|info>\n", argv[0]);