2016-10-29 21:24:06 +02:00
|
|
|
/*
|
2017-07-06 13:22:11 +02:00
|
|
|
* Copyright (c) 2015-2017 Ken Bannister. All rights reserved.
|
2016-10-29 21:24:06 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup examples
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief gcoap CLI support
|
|
|
|
*
|
|
|
|
* @author Ken Bannister <kb2ma@runbox.com>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-01-15 12:18:04 +01:00
|
|
|
#include "net/gcoap.h"
|
2016-10-29 21:24:06 +02:00
|
|
|
#include "od.h"
|
|
|
|
#include "fmt.h"
|
|
|
|
|
2017-03-13 05:28:39 +01:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2017-07-25 19:22:45 +02:00
|
|
|
static void _resp_handler(unsigned req_state, coap_pkt_t* pdu,
|
|
|
|
sock_udp_ep_t *remote);
|
2018-02-02 19:02:54 +01:00
|
|
|
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
|
|
|
|
static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
|
2016-10-29 21:24:06 +02:00
|
|
|
|
2018-12-12 02:26:05 +01:00
|
|
|
/* CoAP resources. Must be sorted by path (ASCII order). */
|
2016-10-29 21:24:06 +02:00
|
|
|
static const coap_resource_t _resources[] = {
|
2018-02-02 19:02:54 +01:00
|
|
|
{ "/cli/stats", COAP_GET | COAP_PUT, _stats_handler, NULL },
|
|
|
|
{ "/riot/board", COAP_GET, _riot_board_handler, NULL },
|
2016-10-29 21:24:06 +02:00
|
|
|
};
|
2017-09-14 09:52:28 +02:00
|
|
|
|
2016-10-29 21:24:06 +02:00
|
|
|
static gcoap_listener_t _listener = {
|
2018-07-30 08:01:30 +02:00
|
|
|
&_resources[0],
|
2016-10-29 21:24:06 +02:00
|
|
|
sizeof(_resources) / sizeof(_resources[0]),
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Counts requests sent by CLI. */
|
|
|
|
static uint16_t req_count = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Response callback.
|
|
|
|
*/
|
2017-07-25 19:22:45 +02:00
|
|
|
static void _resp_handler(unsigned req_state, coap_pkt_t* pdu,
|
|
|
|
sock_udp_ep_t *remote)
|
2016-10-29 21:24:06 +02:00
|
|
|
{
|
2017-07-25 19:22:45 +02:00
|
|
|
(void)remote; /* not interested in the source currently */
|
|
|
|
|
2016-10-29 21:24:06 +02:00
|
|
|
if (req_state == GCOAP_MEMO_TIMEOUT) {
|
|
|
|
printf("gcoap: timeout for msg ID %02u\n", coap_get_id(pdu));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (req_state == GCOAP_MEMO_ERR) {
|
|
|
|
printf("gcoap: error in response\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *class_str = (coap_get_code_class(pdu) == COAP_CLASS_SUCCESS)
|
|
|
|
? "Success" : "Error";
|
|
|
|
printf("gcoap: response %s, code %1u.%02u", class_str,
|
|
|
|
coap_get_code_class(pdu),
|
|
|
|
coap_get_code_detail(pdu));
|
|
|
|
if (pdu->payload_len) {
|
2018-05-21 19:01:06 +02:00
|
|
|
unsigned content_type = coap_get_content_type(pdu);
|
|
|
|
if (content_type == COAP_FORMAT_TEXT
|
|
|
|
|| content_type == COAP_FORMAT_LINK
|
2016-10-29 21:24:06 +02:00
|
|
|
|| coap_get_code_class(pdu) == COAP_CLASS_CLIENT_FAILURE
|
|
|
|
|| coap_get_code_class(pdu) == COAP_CLASS_SERVER_FAILURE) {
|
|
|
|
/* Expecting diagnostic payload in failure cases */
|
|
|
|
printf(", %u bytes\n%.*s\n", pdu->payload_len, pdu->payload_len,
|
|
|
|
(char *)pdu->payload);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(", %u bytes\n", pdu->payload_len);
|
|
|
|
od_hex_dump(pdu->payload, pdu->payload_len, OD_WIDTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(", empty payload\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-09-14 09:52:28 +02:00
|
|
|
* 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.
|
2016-10-29 21:24:06 +02:00
|
|
|
*/
|
2018-02-02 19:02:54 +01:00
|
|
|
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
|
2016-10-29 21:24:06 +02:00
|
|
|
{
|
2018-02-02 19:02:54 +01:00
|
|
|
(void)ctx;
|
|
|
|
|
2017-09-14 09:52:28 +02:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 21:24:06 +02:00
|
|
|
|
2017-09-14 09:52:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-10-29 21:24:06 +02:00
|
|
|
|
2018-02-02 19:02:54 +01:00
|
|
|
static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
2017-09-14 09:52:28 +02:00
|
|
|
{
|
2018-02-02 19:02:54 +01:00
|
|
|
(void)ctx;
|
2017-09-14 09:52:28 +02:00
|
|
|
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
|
|
|
/* write the RIOT board name in the response buffer */
|
|
|
|
memcpy(pdu->payload, RIOT_BOARD, strlen(RIOT_BOARD));
|
|
|
|
return gcoap_finish(pdu, strlen(RIOT_BOARD), COAP_FORMAT_TEXT);
|
2016-10-29 21:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t _send(uint8_t *buf, size_t len, char *addr_str, char *port_str)
|
|
|
|
{
|
|
|
|
ipv6_addr_t addr;
|
|
|
|
size_t bytes_sent;
|
2016-11-17 06:03:08 +01:00
|
|
|
sock_udp_ep_t remote;
|
|
|
|
|
|
|
|
remote.family = AF_INET6;
|
2017-12-10 15:50:36 +01:00
|
|
|
|
|
|
|
/* parse for interface */
|
|
|
|
int iface = ipv6_addr_split_iface(addr_str);
|
|
|
|
if (iface == -1) {
|
|
|
|
if (gnrc_netif_numof() == 1) {
|
|
|
|
/* assign the single interface found in gnrc_netif_numof() */
|
|
|
|
remote.netif = (uint16_t)gnrc_netif_iter(NULL)->pid;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
remote.netif = SOCK_ADDR_ANY_NETIF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (gnrc_netif_get_by_pid(iface) == NULL) {
|
|
|
|
puts("gcoap_cli: interface not valid");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
remote.netif = iface;
|
|
|
|
}
|
2016-10-29 21:24:06 +02:00
|
|
|
|
|
|
|
/* parse destination address */
|
|
|
|
if (ipv6_addr_from_str(&addr, addr_str) == NULL) {
|
|
|
|
puts("gcoap_cli: unable to parse destination address");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-10 15:50:36 +01:00
|
|
|
if ((remote.netif == SOCK_ADDR_ANY_NETIF) && ipv6_addr_is_link_local(&addr)) {
|
|
|
|
puts("gcoap_cli: must specify interface for link local target");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-17 06:03:08 +01:00
|
|
|
memcpy(&remote.addr.ipv6[0], &addr.u8[0], sizeof(addr.u8));
|
|
|
|
|
2016-10-29 21:24:06 +02:00
|
|
|
/* parse port */
|
2017-05-13 12:39:48 +02:00
|
|
|
remote.port = atoi(port_str);
|
2016-11-17 06:03:08 +01:00
|
|
|
if (remote.port == 0) {
|
2016-10-29 21:24:06 +02:00
|
|
|
puts("gcoap_cli: unable to parse destination port");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:03:08 +01:00
|
|
|
bytes_sent = gcoap_req_send2(buf, len, &remote, _resp_handler);
|
2016-10-29 21:24:06 +02:00
|
|
|
if (bytes_sent > 0) {
|
|
|
|
req_count++;
|
|
|
|
}
|
|
|
|
return bytes_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-06 13:13:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-06 13:18:29 +02:00
|
|
|
/* if not 'info', must be a method code */
|
2017-07-06 13:13:50 +02:00
|
|
|
int code_pos = -1;
|
2016-10-29 21:24:06 +02:00
|
|
|
for (size_t i = 0; i < sizeof(method_codes) / sizeof(char*); i++) {
|
|
|
|
if (strcmp(argv[1], method_codes[i]) == 0) {
|
2017-07-06 13:13:50 +02:00
|
|
|
code_pos = i;
|
2016-10-29 21:24:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 13:13:50 +02:00
|
|
|
if (code_pos == -1) {
|
|
|
|
goto end;
|
|
|
|
}
|
2016-10-29 21:24:06 +02:00
|
|
|
|
2017-07-06 13:18:29 +02:00
|
|
|
/* parse options */
|
|
|
|
int apos = 2; /* position of address argument */
|
|
|
|
unsigned msg_type = COAP_TYPE_NON;
|
|
|
|
if (argc > apos && strcmp(argv[apos], "-c") == 0) {
|
|
|
|
msg_type = COAP_TYPE_CON;
|
|
|
|
apos++;
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:03:27 +02:00
|
|
|
/*
|
|
|
|
* "get" (code_pos 0) must have exactly apos + 3 arguments
|
|
|
|
* while "post" (code_pos 1) and "put" (code_pos 2) and must have exactly
|
|
|
|
* apos + 4 arguments
|
|
|
|
*/
|
|
|
|
if (((argc == apos + 3) && (code_pos == 0)) ||
|
|
|
|
((argc == apos + 4) && (code_pos != 0))) {
|
2017-07-06 13:18:29 +02:00
|
|
|
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);
|
2017-07-06 13:13:50 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-07-06 13:18:29 +02:00
|
|
|
len = gcoap_finish(&pdu, 0, COAP_FORMAT_NONE);
|
2017-07-06 13:13:50 +02:00
|
|
|
}
|
2017-07-06 13:18:29 +02:00
|
|
|
|
2017-07-06 13:13:50 +02:00
|
|
|
printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu),
|
|
|
|
(unsigned) len);
|
2017-07-06 13:18:29 +02:00
|
|
|
if (!_send(&buf[0], len, argv[apos], argv[apos+1])) {
|
2017-07-06 13:13:50 +02:00
|
|
|
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;
|
|
|
|
}
|
2016-10-29 21:24:06 +02:00
|
|
|
}
|
2017-07-06 13:13:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2017-12-10 15:50:36 +01:00
|
|
|
printf("usage: %s <get|post|put> [-c] <addr>[%%iface] <port> <path> [data]\n",
|
2017-07-06 13:13:50 +02:00
|
|
|
argv[0]);
|
2017-07-06 13:18:29 +02:00
|
|
|
printf("Options\n");
|
|
|
|
printf(" -c Send confirmably (defaults to non-confirmable)\n");
|
2017-07-06 13:13:50 +02:00
|
|
|
return 1;
|
2016-10-29 21:24:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
printf("usage: %s <get|post|put|info>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gcoap_cli_init(void)
|
|
|
|
{
|
|
|
|
gcoap_register_listener(&_listener);
|
|
|
|
}
|