net/gcoap: Add shell command option to send message confirmably

This commit is contained in:
Ken Bannister 2017-07-06 07:18:29 -04:00
parent 25f40df447
commit 5435e0c7d3

View File

@ -189,7 +189,7 @@ int gcoap_cli_cmd(int argc, char **argv)
return 0; return 0;
} }
/* find method code */ /* if not 'info', must be a method code */
int code_pos = -1; int code_pos = -1;
for (size_t i = 0; i < sizeof(method_codes) / sizeof(char*); i++) { for (size_t i = 0; i < sizeof(method_codes) / sizeof(char*); i++) {
if (strcmp(argv[1], method_codes[i]) == 0) { if (strcmp(argv[1], method_codes[i]) == 0) {
@ -200,19 +200,31 @@ int gcoap_cli_cmd(int argc, char **argv)
goto end; goto end;
} }
if (argc == 5 || argc == 6) { /* parse options */
if (argc == 6) { int apos = 2; /* position of address argument */
gcoap_req_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, code_pos+1, argv[4]); unsigned msg_type = COAP_TYPE_NON;
memcpy(pdu.payload, argv[5], strlen(argv[5])); if (argc > apos && strcmp(argv[apos], "-c") == 0) {
len = gcoap_finish(&pdu, strlen(argv[5]), COAP_FORMAT_TEXT); msg_type = COAP_TYPE_CON;
apos++;
}
if (argc == apos + 3 || argc == apos + 4) {
gcoap_req_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, code_pos+1, argv[apos+2]);
if (argc == apos + 4) {
memcpy(pdu.payload, argv[apos+3], strlen(argv[apos+3]));
}
coap_hdr_set_type(pdu.hdr, msg_type);
if (argc == apos + 4) {
len = gcoap_finish(&pdu, strlen(argv[apos+3]), COAP_FORMAT_TEXT);
} }
else { else {
len = gcoap_request(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, code_pos+1, len = gcoap_finish(&pdu, 0, COAP_FORMAT_NONE);
argv[4]);
} }
printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu), printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu),
(unsigned) len); (unsigned) len);
if (!_send(&buf[0], len, argv[2], argv[3])) { if (!_send(&buf[0], len, argv[apos], argv[apos+1])) {
puts("gcoap_cli: msg send failed"); puts("gcoap_cli: msg send failed");
} }
else { else {
@ -236,8 +248,10 @@ int gcoap_cli_cmd(int argc, char **argv)
return 0; return 0;
} }
else { else {
printf("usage: %s <get|post|put> <addr> <port> <path> [data]\n", printf("usage: %s <get|post|put> [-c] <addr> <port> <path> [data]\n",
argv[0]); argv[0]);
printf("Options\n");
printf(" -c Send confirmably (defaults to non-confirmable)\n");
return 1; return 1;
} }