Created
January 10, 2019 21:44
-
-
Save jwhui/f6aa706e12899722fd50619de3562103 to your computer and use it in GitHub Desktop.
UDP Multicast Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017, The OpenThread Authors. | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* 3. Neither the name of the copyright holder nor the | |
* names of its contributors may be used to endorse or promote products | |
* derived from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/** | |
* @file | |
* This file implements a simple CLI for the CoAP service. | |
*/ | |
#include "cli_udp_example.hpp" | |
#include <openthread/message.h> | |
#include <openthread/udp.h> | |
#include "cli/cli.hpp" | |
#include "common/encoding.hpp" | |
using ot::Encoding::BigEndian::HostSwap16; | |
namespace ot { | |
namespace Cli { | |
const struct UdpExample::Command UdpExample::sCommands[] = { | |
{"help", &UdpExample::ProcessHelp}, {"bind", &UdpExample::ProcessBind}, {"close", &UdpExample::ProcessClose}, | |
{"connect", &UdpExample::ProcessConnect}, {"open", &UdpExample::ProcessOpen}, {"send", &UdpExample::ProcessSend}, | |
{"test", &UdpExample::ProcessTest}}; | |
UdpExample::UdpExample(Interpreter &aInterpreter) | |
: mInterpreter(aInterpreter) | |
{ | |
memset(&mSocket, 0, sizeof(mSocket)); | |
} | |
otError UdpExample::ProcessTest(int argc, char *argv[]) | |
{ | |
OT_UNUSED_VARIABLE(argc); | |
OT_UNUSED_VARIABLE(argv); | |
const char buf[100] = "Hallo\0"; | |
otMessageInfo messageInfo; | |
otInstance *myInstance; | |
myInstance = mInterpreter.mInstance; | |
otUdpSocket mySocket; | |
memset(&messageInfo, 0, sizeof(messageInfo)); | |
otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr); | |
messageInfo.mPeerPort = 1994; | |
messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD; | |
otCliUartOutputFormat(otThreadErrorToString(otUdpOpen(myInstance, &mySocket, NULL, NULL))); | |
otMessage *test_Message = otUdpNewMessage(myInstance, NULL); | |
otCliUartOutputFormat(otThreadErrorToString(otMessageAppend(test_Message, buf, (uint16_t)strlen(buf)))); | |
otError errorUdpSend = otUdpSend(&mySocket, test_Message, &messageInfo); | |
otCliUartOutputFormat(otThreadErrorToString(errorUdpSend)); | |
otCliUartOutputFormat("Done\0"); | |
otCliUartOutputFormat(otThreadErrorToString(otUdpClose(&mySocket))); | |
if(errorUdpSend != OT_ERROR_NONE && test_Message != NULL) | |
{ | |
otMessageFree(test_Message); | |
} | |
return OT_ERROR_NONE; | |
} | |
otError UdpExample::ProcessHelp(int argc, char *argv[]) | |
{ | |
OT_UNUSED_VARIABLE(argc); | |
OT_UNUSED_VARIABLE(argv); | |
for (unsigned int i = 0; i < OT_ARRAY_LENGTH(sCommands); i++) | |
{ | |
mInterpreter.mServer->OutputFormat("%s\r\n", sCommands[i].mName); | |
} | |
return OT_ERROR_NONE; | |
} | |
otError UdpExample::ProcessBind(int argc, char *argv[]) | |
{ | |
otError error; | |
otSockAddr sockaddr; | |
long value; | |
VerifyOrExit(argc == 2, error = OT_ERROR_INVALID_ARGS); | |
memset(&sockaddr, 0, sizeof(sockaddr)); | |
error = otIp6AddressFromString(argv[0], &sockaddr.mAddress); | |
SuccessOrExit(error); | |
error = Interpreter::ParseLong(argv[1], value); | |
SuccessOrExit(error); | |
sockaddr.mPort = static_cast<uint16_t>(value); | |
sockaddr.mScopeId = OT_NETIF_INTERFACE_ID_THREAD; | |
error = otUdpBind(&mSocket, &sockaddr); | |
exit: | |
return error; | |
} | |
otError UdpExample::ProcessConnect(int argc, char *argv[]) | |
{ | |
otError error; | |
otSockAddr sockaddr; | |
long value; | |
VerifyOrExit(argc == 2, error = OT_ERROR_INVALID_ARGS); | |
memset(&sockaddr, 0, sizeof(sockaddr)); | |
error = otIp6AddressFromString(argv[0], &sockaddr.mAddress); | |
SuccessOrExit(error); | |
error = Interpreter::ParseLong(argv[1], value); | |
SuccessOrExit(error); | |
sockaddr.mPort = static_cast<uint16_t>(value); | |
sockaddr.mScopeId = OT_NETIF_INTERFACE_ID_THREAD; | |
error = otUdpConnect(&mSocket, &sockaddr); | |
exit: | |
return error; | |
} | |
otError UdpExample::ProcessClose(int argc, char *argv[]) | |
{ | |
OT_UNUSED_VARIABLE(argc); | |
OT_UNUSED_VARIABLE(argv); | |
return otUdpClose(&mSocket); | |
} | |
otError UdpExample::ProcessOpen(int argc, char *argv[]) | |
{ | |
OT_UNUSED_VARIABLE(argc); | |
OT_UNUSED_VARIABLE(argv); | |
return otUdpOpen(mInterpreter.mInstance, &mSocket, HandleUdpReceive, this); | |
} | |
otError UdpExample::ProcessSend(int argc, char *argv[]) | |
{ | |
otError error; | |
otMessageInfo messageInfo; | |
otMessage * message = NULL; | |
int curArg = 0; | |
memset(&messageInfo, 0, sizeof(messageInfo)); | |
VerifyOrExit(argc == 1 || argc == 3, error = OT_ERROR_INVALID_ARGS); | |
if (argc == 3) | |
{ | |
long value; | |
error = otIp6AddressFromString(argv[curArg++], &messageInfo.mPeerAddr); | |
SuccessOrExit(error); | |
error = Interpreter::ParseLong(argv[curArg++], value); | |
SuccessOrExit(error); | |
messageInfo.mPeerPort = static_cast<uint16_t>(value); | |
messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD; | |
} | |
message = otUdpNewMessage(mInterpreter.mInstance, NULL); | |
VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); | |
error = otMessageAppend(message, argv[curArg], static_cast<uint16_t>(strlen(argv[curArg]))); | |
SuccessOrExit(error); | |
error = otUdpSend(&mSocket, message, &messageInfo); | |
exit: | |
if (error != OT_ERROR_NONE && message != NULL) | |
{ | |
otMessageFree(message); | |
} | |
return error; | |
} | |
otError UdpExample::Process(int argc, char *argv[]) | |
{ | |
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 - 1, argv + 1); | |
break; | |
} | |
} | |
} | |
return error; | |
} | |
void UdpExample::HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) | |
{ | |
static_cast<UdpExample *>(aContext)->HandleUdpReceive(aMessage, aMessageInfo); | |
} | |
void UdpExample::HandleUdpReceive(otMessage *aMessage, const otMessageInfo *aMessageInfo) | |
{ | |
uint8_t buf[1500]; | |
int length; | |
mInterpreter.mServer->OutputFormat("%d bytes from ", otMessageGetLength(aMessage) - otMessageGetOffset(aMessage)); | |
mInterpreter.mServer->OutputFormat( | |
"%x:%x:%x:%x:%x:%x:%x:%x %d ", 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]), aMessageInfo->mPeerPort); | |
length = otMessageRead(aMessage, otMessageGetOffset(aMessage), buf, sizeof(buf) - 1); | |
buf[length] = '\0'; | |
mInterpreter.mServer->OutputFormat("%s\r\n", buf); | |
} | |
} // namespace Cli | |
} // namespace ot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017, The OpenThread Authors. | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* 3. Neither the name of the copyright holder nor the | |
* names of its contributors may be used to endorse or promote products | |
* derived from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
* POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/** | |
* @file | |
* This file contains definitions for a simple CLI CoAP server and client. | |
*/ | |
#ifndef CLI_UDP_EXAMPLE_HPP_ | |
#define CLI_UDP_EXAMPLE_HPP_ | |
#include "openthread-core-config.h" | |
#include <openthread/udp.h> | |
namespace ot { | |
namespace Cli { | |
class Interpreter; | |
/** | |
* This class implements a CLI-based UDP example. | |
* | |
*/ | |
class UdpExample | |
{ | |
public: | |
/** | |
* Constructor | |
* | |
* @param[in] aInterpreter The CLI interpreter. | |
* | |
*/ | |
UdpExample(Interpreter &aInterpreter); | |
/** | |
* This method interprets a list of CLI arguments. | |
* | |
* @param[in] argc The number of elements in argv. | |
* @param[in] argv A pointer to an array of command line arguments. | |
* | |
*/ | |
otError Process(int argc, char *argv[]); | |
private: | |
struct Command | |
{ | |
const char *mName; | |
otError (UdpExample::*mCommand)(int argc, char *argv[]); | |
}; | |
otError ProcessHelp(int argc, char *argv[]); | |
otError ProcessBind(int argc, char *argv[]); | |
otError ProcessClose(int argc, char *argv[]); | |
otError ProcessConnect(int argc, char *argv[]); | |
otError ProcessOpen(int argc, char *argv[]); | |
otError ProcessSend(int argc, char *argv[]); | |
otError ProcessTest(int argc, char *argv[]); | |
static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); | |
void HandleUdpReceive(otMessage *aMessage, const otMessageInfo *aMessageInfo); | |
static const Command sCommands[]; | |
Interpreter & mInterpreter; | |
otUdpSocket mSocket; | |
}; | |
} // namespace Cli | |
} // namespace ot | |
#endif // CLI_UDP_EXAMPLE_HPP_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment