Skip to content

Commit

Permalink
Extract common code
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Aug 18, 2020
1 parent 7baa17f commit 4095528
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 104 deletions.
52 changes: 2 additions & 50 deletions examples/lighting-app/nrf5/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
#include <support/ErrorStr.h>
#include <system/SystemClock.h>

#include <openthread/message.h>
#include <openthread/udp.h>

APP_TIMER_DEF(sFunctionTimer);

namespace {
Expand Down Expand Up @@ -161,53 +158,6 @@ int AppTask::Init()
return ret;
}

void SendUDPBroadCast()
{
// TODO: change to CHIP inet layer
const char * domainName = "LightingDemo._chip._udp.local.";
chip::Inet::IPAddress addr;
if (!ConnectivityMgrImpl().IsThreadAttached())
{
return;
}
ThreadStackMgrImpl().LockThreadStack();
otError error = OT_ERROR_NONE;
otMessageInfo messageInfo;
otUdpSocket mSocket;
otMessage * message = nullptr;

memset(&mSocket, 0, sizeof(mSocket));
memset(&messageInfo, 0, sizeof(messageInfo));

// Select a address to send
const otNetifAddress * otAddrs = otIp6GetUnicastAddresses(ThreadStackMgrImpl().OTInstance());
for (const otNetifAddress * otAddr = otAddrs; otAddr != NULL; otAddr = otAddr->mNext)
{
addr = chip::DeviceLayer::Internal::ToIPAddress(otAddr->mAddress);
if (otAddr->mValid && !otAddr->mRloc &&
(!addr.IsIPv6ULA() ||
::chip::DeviceLayer::Internal::IsOpenThreadMeshLocalAddress(ThreadStackMgrImpl().OTInstance(), addr)))
{
memcpy(&messageInfo.mSockAddr, &(otAddr->mAddress), sizeof(otAddr->mAddress));
break;
}
}

message = otUdpNewMessage(ThreadStackMgrImpl().OTInstance(), nullptr);
otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr);
messageInfo.mPeerPort = 23367;
otMessageAppend(message, domainName, static_cast<uint16_t>(strlen(domainName)));

error = otUdpSend(ThreadStackMgrImpl().OTInstance(), &mSocket, message, &messageInfo);

if (error != OT_ERROR_NONE && message != nullptr)
{
otMessageFree(message);
NRF_LOG_INFO("Failed to otUdpSend: %d", error);
}
ThreadStackMgrImpl().UnlockThreadStack();
}

void AppTask::AppTaskMain(void * pvParameter)
{
ret_code_t ret;
Expand All @@ -221,6 +171,8 @@ void AppTask::AppTaskMain(void * pvParameter)
APP_ERROR_HANDLER(ret);
}

SetDeviceName("LightingDemo._chip._udp.local.");

while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
Expand Down
54 changes: 1 addition & 53 deletions examples/lock-app/nrf5/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

#include <platform/CHIPDeviceLayer.h>
#if CHIP_ENABLE_OPENTHREAD
#include <openthread/message.h>
#include <openthread/udp.h>
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/ThreadStackManager.h>
#include <platform/internal/DeviceNetworkInfo.h>
Expand Down Expand Up @@ -231,57 +229,6 @@ void AppTask::HandleBLEMessageReceived(chip::Ble::BLEEndPoint * endPoint, chip::
chip::System::PacketBuffer::Free(buffer);
}

void SendUDPBroadCast()
{
#if CHIP_ENABLE_OPENTHREAD
// TODO: change to CHIP inet layer
const char * domainName = "LockDemo._chip._udp.local.";
chip::Inet::IPAddress addr;
if (!ConnectivityMgrImpl().IsThreadAttached())
{
return;
}
ThreadStackMgrImpl().LockThreadStack();
otError error = OT_ERROR_NONE;
otMessageInfo messageInfo;
otUdpSocket mSocket;
otMessage * message = nullptr;

memset(&mSocket, 0, sizeof(mSocket));
memset(&messageInfo, 0, sizeof(messageInfo));

// Select a address to send
const otNetifAddress * otAddrs = otIp6GetUnicastAddresses(ThreadStackMgrImpl().OTInstance());
for (const otNetifAddress * otAddr = otAddrs; otAddr != NULL; otAddr = otAddr->mNext)
{
addr = chip::DeviceLayer::Internal::ToIPAddress(otAddr->mAddress);
if (otAddr->mValid && !otAddr->mRloc &&
(!addr.IsIPv6ULA() ||
::chip::DeviceLayer::Internal::IsOpenThreadMeshLocalAddress(ThreadStackMgrImpl().OTInstance(), addr)))
{
memcpy(&messageInfo.mSockAddr, &(otAddr->mAddress), sizeof(otAddr->mAddress));
break;
}
}

message = otUdpNewMessage(ThreadStackMgrImpl().OTInstance(), nullptr);
otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr);
messageInfo.mPeerPort = 23367;
otMessageAppend(message, domainName, static_cast<uint16_t>(strlen(domainName)));

error = otUdpSend(ThreadStackMgrImpl().OTInstance(), &mSocket, message, &messageInfo);

if (error != OT_ERROR_NONE && message != nullptr)
{
otMessageFree(message);
NRF_LOG_INFO("Failed to otUdpSend: %d", error);
}
ThreadStackMgrImpl().UnlockThreadStack();
#else
NRF_LOG_INFO("OpenThread disabled, not sending UDP broadcasts");
#endif
}

void AppTask::AppTaskMain(void * pvParameter)
{
ret_code_t ret;
Expand All @@ -296,6 +243,7 @@ void AppTask::AppTaskMain(void * pvParameter)
}

chip::DeviceLayer::ConnectivityMgr().AddCHIPoBLEConnectionHandler(&AppTask::HandleBLEConnectionOpened);
SetDeviceName("LockDemo._chip._udp.local.");

while (true)
{
Expand Down
65 changes: 64 additions & 1 deletion examples/platform/nrf528xx/app/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

#include "Server.h"

#include "FreeRTOS.h"
#include "nrf_log.h"
#include "task.h"
Expand All @@ -36,7 +38,14 @@
#include <transport/SecureSessionMgr.h>
#include <transport/UDP.h>

#include "Server.h"
#if CHIP_ENABLE_OPENTHREAD
#include <openthread/message.h>
#include <openthread/udp.h>
#include <platform/OpenThread/OpenThreadUtils.h>
#include <platform/ThreadStackManager.h>
#include <platform/internal/DeviceNetworkInfo.h>
#include <platform/nRF5/ThreadStackManagerImpl.h>
#endif

#include "attribute-storage.h"
#include "chip-zcl/chip-zcl-zpro-codec.h"
Expand All @@ -46,6 +55,7 @@
using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::Transport;
using namespace ::chip::DeviceLayer;

// Transport Callbacks
namespace {
Expand All @@ -55,6 +65,9 @@ namespace {
#define EXAMPLE_SERVER_NODEID 0x3546526e
#endif // EXAMPLE_SERVER_NODEID

char deviceName[128];
constexpr uint16_t kUDPBroadcastPort = 23367;

const uint8_t local_private_key[] = { 0xc6, 0x1a, 0x2f, 0x89, 0x36, 0x67, 0x2b, 0x26, 0x12, 0x47, 0x4f,
0x11, 0x0e, 0x34, 0x15, 0x81, 0x81, 0x12, 0xfc, 0x36, 0xeb, 0x65,
0x61, 0x07, 0xaa, 0x63, 0xe8, 0xc5, 0x22, 0xac, 0x52, 0xa1 };
Expand Down Expand Up @@ -158,6 +171,56 @@ static ServerCallback gCallbacks;

} // namespace

void SetDeviceName(const char * newDeviceName)
{
strncpy(deviceName, newDeviceName, sizeof(deviceName) - 1);
}

void SendUDPBroadCast()
{
chip::Inet::IPAddress addr;
if (!ConnectivityMgrImpl().IsThreadAttached())
{
return;
}
ThreadStackMgrImpl().LockThreadStack();
otError error = OT_ERROR_NONE;
otMessageInfo messageInfo;
otUdpSocket mSocket;
otMessage * message = nullptr;

memset(&mSocket, 0, sizeof(mSocket));
memset(&messageInfo, 0, sizeof(messageInfo));

// Select a address to send
const otNetifAddress * otAddrs = otIp6GetUnicastAddresses(ThreadStackMgrImpl().OTInstance());
for (const otNetifAddress * otAddr = otAddrs; otAddr != NULL; otAddr = otAddr->mNext)
{
addr = chip::DeviceLayer::Internal::ToIPAddress(otAddr->mAddress);
if (otAddr->mValid && !otAddr->mRloc &&
(!addr.IsIPv6ULA() ||
::chip::DeviceLayer::Internal::IsOpenThreadMeshLocalAddress(ThreadStackMgrImpl().OTInstance(), addr)))
{
memcpy(&messageInfo.mSockAddr, &(otAddr->mAddress), sizeof(otAddr->mAddress));
break;
}
}

message = otUdpNewMessage(ThreadStackMgrImpl().OTInstance(), nullptr);
otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr);
messageInfo.mPeerPort = kUDPBroadcastPort;
otMessageAppend(message, deviceName, static_cast<uint16_t>(strlen(deviceName)));

error = otUdpSend(ThreadStackMgrImpl().OTInstance(), &mSocket, message, &messageInfo);

if (error != OT_ERROR_NONE && message != nullptr)
{
otMessageFree(message);
NRF_LOG_INFO("Failed to otUdpSend: %d", error);
}
ThreadStackMgrImpl().UnlockThreadStack();
}

void InitDataModelHandler()
{
emberAfEndpointConfigure();
Expand Down
2 changes: 2 additions & 0 deletions examples/platform/nrf528xx/app/include/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ using DemoSessionManager = chip::SecureSessionMgr<chip::Transport::UDP>;

void StartServer(DemoSessionManager * sessions);
void InitDataModelHandler();
void SetDeviceName(const char * newDeviceName);
void SendUDPBroadCast();

#endif // NRF5_COMMON_SERVER_H

0 comments on commit 4095528

Please sign in to comment.