[cli] cleanup CoAP example (#3470)

This commit is contained in:
Jonathan Hui
2019-01-18 11:30:32 -08:00
committed by GitHub
parent d57b747c98
commit f4c10c0ae1
3 changed files with 340 additions and 131 deletions
+160
View File
@@ -0,0 +1,160 @@
# OpenThread CLI - CoAP Example
The OpenThread CoAP APIs may be invoked via the OpenThread CLI.
## Quick Start
### Build with CoAP API support
Use the `COAP=1` build switch to enable CoAP API support.
```bash
> ./bootstrap
> make -f examples/Makefile-posix COAP=1
```
### Form Network
Form a network with at least two devices.
### Node 1
On node 1, setup CoAP server with resource `test-resource`.
```bash
> coap start
Done
> coap resource test-resource
Done
```
### Node 2
```bash
> coap start
Done
> coap get fdde:ad00:beef:0:d395:daee:a75:3964 test-resource
Done
coap response from [fdde:ad00:beef:0:2780:9423:166c:1aac] with payload: 30
> coap put fdde:ad00:beef:0:2780:9423:166c:1aac test-resource con payload
Done
coap response from [fdde:ad00:beef:0:2780:9423:166c:1aac]
```
### Result
On node 1, you should see output similar to below:
```bash
coap request from [fdde:ad00:beef:0:b3:e3f6:2dcc:4b79] GET
coap response sent
coap request from [fdde:ad00:beef:0:b3:e3f6:2dcc:4b79] PUT with payload: 7061796c6f6164
coap response sent
```
## Command List
* [help](#help)
* [delete](#delete-address-uri-path-type-payload)
* [get](#get-address-uri-path-type)
* [post](#post-address-uri-path-type-payload)
* [put](#put-address-uri-path-type-payload)
# [resource](#resource-uri-path)
* [start](#start)
* [stop](#stop)
## Command Details
### help
```bash
> coap help
help
delete
get
post
put
resource
start
stop
Done
```
List the CoAP CLI commands.
### delete \<address\> \<uri-path\> \[type\] \[payload\]
* address: IPv6 address of the CoAP server.
* uri-path: URI path of the resource.
* type: "con" for Confirmable or "non-con" for Non-confirmable (default).
* payload: CoAP request payload.
```bash
> coap delete fdde:ad00:beef:0:2780:9423:166c:1aac test-resource con payload
Done
```
### get \<address\> \<uri-path\> \[type\]
* address: IPv6 address of the CoAP server.
* uri-path: URI path of the resource.
* type: "con" for Confirmable or "non-con" for Non-confirmable (default).
```bash
> coap get fdde:ad00:beef:0:2780:9423:166c:1aac test-resource
Done
```
### post \<address\> \<uri-path\> \[type\] \[payload\]
* address: IPv6 address of the CoAP server.
* uri-path: URI path of the resource.
* type: "con" for Confirmable or "non-con" for Non-confirmable (default).
* payload: CoAP request payload.
```bash
> coap post fdde:ad00:beef:0:2780:9423:166c:1aac test-resource con payload
Done
```
### put \<address\> \<uri-path\> \[type\] \[payload\]
* address: IPv6 address of the CoAP server.
* uri-path: URI path of the resource.
* type: "con" for Confirmable or "non-con" for Non-confirmable (default).
* payload: CoAP request payload.
```bash
> coap put fdde:ad00:beef:0:2780:9423:166c:1aac test-resource con payload
Done
```
### resource \[uri-path\]
Sets the URI path for the test resource.
```bash
> coap resource test-resource
Done
> coap resource
test-resource
Done
```
### start
Starts the application coap service.
```bash
> coap start
Done
```
### stop
Stops the application coap service.
```bash
> coap stop
Done
```
+163 -122
View File
@@ -43,6 +43,12 @@
namespace ot {
namespace Cli {
const struct Coap::Command Coap::sCommands[] = {
{"help", &Coap::ProcessHelp}, {"delete", &Coap::ProcessRequest}, {"get", &Coap::ProcessRequest},
{"post", &Coap::ProcessRequest}, {"put", &Coap::ProcessRequest}, {"resource", &Coap::ProcessResource},
{"start", &Coap::ProcessStart}, {"stop", &Coap::ProcessStop},
};
Coap::Coap(Interpreter &aInterpreter)
: mInterpreter(aInterpreter)
{
@@ -75,137 +81,59 @@ void Coap::PrintPayload(otMessage *aMessage) const
mInterpreter.mServer->OutputFormat("\r\n");
}
otError Coap::Process(int argc, char *argv[])
otError Coap::ProcessHelp(int argc, char *argv[])
{
OT_UNUSED_VARIABLE(argc);
OT_UNUSED_VARIABLE(argv);
for (size_t i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
mInterpreter.mServer->OutputFormat("%s\r\n", sCommands[i].mName);
}
return OT_ERROR_NONE;
}
otError Coap::ProcessResource(int argc, char *argv[])
{
otError error = OT_ERROR_NONE;
VerifyOrExit(argc > 0, error = OT_ERROR_INVALID_ARGS);
if (argc > 1)
{
VerifyOrExit(strlen(argv[1]) < kMaxUriLength, error = OT_ERROR_INVALID_ARGS);
if (strcmp(argv[0], "start") == 0)
{
SuccessOrExit(error = otCoapStart(mInterpreter.mInstance, OT_DEFAULT_COAP_PORT));
mInterpreter.mServer->OutputFormat("Coap service started: ");
}
else if (strcmp(argv[0], "stop") == 0)
{
otCoapRemoveResource(mInterpreter.mInstance, &mResource);
SuccessOrExit(error = otCoapStop(mInterpreter.mInstance));
mInterpreter.mServer->OutputFormat("Coap service stopped: ");
}
else if (strcmp(argv[0], "resource") == 0)
{
mResource.mUriPath = mUriPath;
mResource.mContext = this;
mResource.mHandler = &Coap::HandleServerResponse;
mResource.mHandler = &Coap::HandleRequest;
if (argc > 1)
{
strlcpy(mUriPath, argv[1], kMaxUriLength);
SuccessOrExit(error = otCoapAddResource(mInterpreter.mInstance, &mResource));
}
mInterpreter.mServer->OutputFormat("Resource name is '%s': ", mResource.mUriPath);
strlcpy(mUriPath, argv[1], kMaxUriLength);
SuccessOrExit(error = otCoapAddResource(mInterpreter.mInstance, &mResource));
}
else
{
error = ProcessRequest(argc, argv);
mInterpreter.mServer->OutputFormat("%s\r\n", mResource.mUriPath);
}
exit:
return error;
return OT_ERROR_NONE;
}
void OTCALL Coap::HandleServerResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
otError Coap::ProcessStart(int argc, char *argv[])
{
static_cast<Coap *>(aContext)->HandleServerResponse(aMessage, aMessageInfo);
OT_UNUSED_VARIABLE(argc);
OT_UNUSED_VARIABLE(argv);
return otCoapStart(mInterpreter.mInstance, OT_DEFAULT_COAP_PORT);
}
void Coap::HandleServerResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo)
otError Coap::ProcessStop(int argc, char *argv[])
{
otError error = OT_ERROR_NONE;
otMessage *responseMessage = NULL;
otCoapCode responseCode = OT_COAP_CODE_EMPTY;
char responseContent = '0';
OT_UNUSED_VARIABLE(argc);
OT_UNUSED_VARIABLE(argv);
mInterpreter.mServer->OutputFormat(
"Received coap request from [%x:%x:%x:%x:%x:%x:%x:%x]: ", HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[0]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[1]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[2]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[3]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[4]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[5]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[6]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[7]));
otCoapRemoveResource(mInterpreter.mInstance, &mResource);
switch (otCoapMessageGetCode(aMessage))
{
case OT_COAP_CODE_GET:
mInterpreter.mServer->OutputFormat("GET");
break;
case OT_COAP_CODE_DELETE:
mInterpreter.mServer->OutputFormat("DELETE");
break;
case OT_COAP_CODE_PUT:
mInterpreter.mServer->OutputFormat("PUT");
break;
case OT_COAP_CODE_POST:
mInterpreter.mServer->OutputFormat("POST");
break;
default:
mInterpreter.mServer->OutputFormat("Undefined\r\n");
ExitNow(error = OT_ERROR_PARSE);
}
PrintPayload(aMessage);
if (otCoapMessageGetType(aMessage) == OT_COAP_TYPE_CONFIRMABLE ||
otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
responseCode = OT_COAP_CODE_CONTENT;
}
else
{
responseCode = OT_COAP_CODE_VALID;
}
responseMessage = otCoapNewMessage(mInterpreter.mInstance, NULL);
VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS);
otCoapMessageInit(responseMessage, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode);
otCoapMessageSetMessageId(responseMessage, otCoapMessageGetMessageId(aMessage));
otCoapMessageSetToken(responseMessage, otCoapMessageGetToken(aMessage), otCoapMessageGetTokenLength(aMessage));
if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
otCoapMessageSetPayloadMarker(responseMessage);
}
if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
SuccessOrExit(error = otMessageAppend(responseMessage, &responseContent, sizeof(responseContent)));
}
SuccessOrExit(error = otCoapSendResponse(mInterpreter.mInstance, responseMessage, aMessageInfo));
}
exit:
if (error != OT_ERROR_NONE)
{
if (responseMessage != NULL)
{
mInterpreter.mServer->OutputFormat("Cannot send coap response message: Error %d: %s\r\n", error,
otThreadErrorToString(error));
otMessageFree(responseMessage);
}
}
else if (responseCode >= OT_COAP_CODE_RESPONSE_MIN)
{
mInterpreter.mServer->OutputFormat("coap response sent successfully!\r\n");
}
return otCoapStop(mInterpreter.mInstance);
}
otError Coap::ProcessRequest(int argc, char *argv[])
@@ -258,6 +186,7 @@ otError Coap::ProcessRequest(int argc, char *argv[])
// CoAP-URI
if (argc > 2)
{
VerifyOrExit(strlen(argv[2]) < kMaxUriLength, error = OT_ERROR_INVALID_ARGS);
strlcpy(coapUri, argv[2], kMaxUriLength);
}
else
@@ -304,15 +233,13 @@ otError Coap::ProcessRequest(int argc, char *argv[])
if ((coapType == OT_COAP_TYPE_CONFIRMABLE) || (coapCode == OT_COAP_CODE_GET))
{
error = otCoapSendRequest(mInterpreter.mInstance, message, &messageInfo, &Coap::HandleClientResponse, this);
error = otCoapSendRequest(mInterpreter.mInstance, message, &messageInfo, &Coap::HandleResponse, this);
}
else
{
error = otCoapSendRequest(mInterpreter.mInstance, message, &messageInfo, NULL, NULL);
}
mInterpreter.mServer->OutputFormat("Sending coap request: ");
exit:
if ((error != OT_ERROR_NONE) && (message != NULL))
@@ -323,26 +250,140 @@ exit:
return error;
}
void OTCALL Coap::HandleClientResponse(void * aContext,
otMessage * aMessage,
const otMessageInfo *aMessageInfo,
otError aError)
otError Coap::Process(int argc, char *argv[])
{
static_cast<Coap *>(aContext)->HandleClientResponse(aMessage, aMessageInfo, aError);
otError error = OT_ERROR_PARSE;
if (argc < 1)
{
ProcessHelp(0, NULL);
error = OT_ERROR_INVALID_ARGS;
}
else
{
for (size_t i = 0; i < OT_ARRAY_LENGTH(sCommands); i++)
{
if (strcmp(argv[0], sCommands[i].mName) == 0)
{
error = (this->*sCommands[i].mCommand)(argc, argv);
break;
}
}
}
return error;
}
void Coap::HandleClientResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError)
void Coap::HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
OT_UNUSED_VARIABLE(aMessageInfo);
static_cast<Coap *>(aContext)->HandleRequest(aMessage, aMessageInfo);
}
void Coap::HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
otError error = OT_ERROR_NONE;
otMessage *responseMessage = NULL;
otCoapCode responseCode = OT_COAP_CODE_EMPTY;
char responseContent = '0';
mInterpreter.mServer->OutputFormat(
"coap request from [%x:%x:%x:%x:%x:%x:%x:%x] ", HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[0]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[1]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[2]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[3]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[4]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[5]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[6]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[7]));
switch (otCoapMessageGetCode(aMessage))
{
case OT_COAP_CODE_GET:
mInterpreter.mServer->OutputFormat("GET");
break;
case OT_COAP_CODE_DELETE:
mInterpreter.mServer->OutputFormat("DELETE");
break;
case OT_COAP_CODE_PUT:
mInterpreter.mServer->OutputFormat("PUT");
break;
case OT_COAP_CODE_POST:
mInterpreter.mServer->OutputFormat("POST");
break;
default:
mInterpreter.mServer->OutputFormat("Undefined\r\n");
ExitNow(error = OT_ERROR_PARSE);
}
PrintPayload(aMessage);
if (otCoapMessageGetType(aMessage) == OT_COAP_TYPE_CONFIRMABLE ||
otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
responseCode = OT_COAP_CODE_CONTENT;
}
else
{
responseCode = OT_COAP_CODE_VALID;
}
responseMessage = otCoapNewMessage(mInterpreter.mInstance, NULL);
VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS);
otCoapMessageInit(responseMessage, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode);
otCoapMessageSetMessageId(responseMessage, otCoapMessageGetMessageId(aMessage));
otCoapMessageSetToken(responseMessage, otCoapMessageGetToken(aMessage), otCoapMessageGetTokenLength(aMessage));
if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET)
{
otCoapMessageSetPayloadMarker(responseMessage);
SuccessOrExit(error = otMessageAppend(responseMessage, &responseContent, sizeof(responseContent)));
}
SuccessOrExit(error = otCoapSendResponse(mInterpreter.mInstance, responseMessage, aMessageInfo));
}
exit:
if (error != OT_ERROR_NONE)
{
if (responseMessage != NULL)
{
mInterpreter.mServer->OutputFormat("coap send response error %d: %s\r\n", error,
otThreadErrorToString(error));
otMessageFree(responseMessage);
}
}
else if (responseCode >= OT_COAP_CODE_RESPONSE_MIN)
{
mInterpreter.mServer->OutputFormat("coap response sent\r\n");
}
}
void Coap::HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError)
{
static_cast<Coap *>(aContext)->HandleResponse(aMessage, aMessageInfo, aError);
}
void Coap::HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError)
{
if (aError != OT_ERROR_NONE)
{
mInterpreter.mServer->OutputFormat("Error receiving coap response message: Error %d: %s\r\n", aError,
mInterpreter.mServer->OutputFormat("coap receive response error %d: %s\r\n", aError,
otThreadErrorToString(aError));
}
else
{
mInterpreter.mServer->OutputFormat("Received coap response");
mInterpreter.mServer->OutputFormat(
"coap response from [%x:%x:%x:%x:%x:%x:%x:%x]", HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[0]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[1]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[2]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[3]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[4]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[5]), HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[6]),
HostSwap16(aMessageInfo->mPeerAddr.mFields.m16[7]));
PrintPayload(aMessage);
}
}
+17 -9
View File
@@ -76,23 +76,31 @@ private:
kMaxBufferSize = 16
};
struct Command
{
const char *mName;
otError (Coap::*mCommand)(int argc, char *argv[]);
};
void PrintPayload(otMessage *aMessage) const;
otError ProcessHelp(int argc, char *argv[]);
otError ProcessRequest(int argc, char *argv[]);
otError ProcessResource(int argc, char *argv[]);
otError ProcessStart(int argc, char *argv[]);
otError ProcessStop(int argc, char *argv[]);
static void OTCALL HandleServerResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
void HandleServerResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo);
static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
void HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo);
static void OTCALL HandleClientResponse(void * aContext,
otMessage * aMessage,
const otMessageInfo *aMessageInfo,
otError aError);
void HandleClientResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
static void HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
void HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
static const Command sCommands[];
Interpreter & mInterpreter;
otCoapResource mResource;
char mUriPath[kMaxUriLength];
Interpreter &mInterpreter;
};
} // namespace Cli