diff --git a/examples/lighting-app/nrf5/main/AppTask.cpp b/examples/lighting-app/nrf5/main/AppTask.cpp index adb51aafc33601..39faa86db7a2cf 100644 --- a/examples/lighting-app/nrf5/main/AppTask.cpp +++ b/examples/lighting-app/nrf5/main/AppTask.cpp @@ -41,9 +41,6 @@ #include #include -#include -#include - APP_TIMER_DEF(sFunctionTimer); namespace { @@ -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(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; @@ -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)); diff --git a/examples/lock-app/nrf5/main/AppTask.cpp b/examples/lock-app/nrf5/main/AppTask.cpp index 3820eaf85fdb33..5b80bb8a9f40f5 100644 --- a/examples/lock-app/nrf5/main/AppTask.cpp +++ b/examples/lock-app/nrf5/main/AppTask.cpp @@ -34,8 +34,6 @@ #include #if CHIP_ENABLE_OPENTHREAD -#include -#include #include #include #include @@ -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(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; @@ -296,6 +243,7 @@ void AppTask::AppTaskMain(void * pvParameter) } chip::DeviceLayer::ConnectivityMgr().AddCHIPoBLEConnectionHandler(&AppTask::HandleBLEConnectionOpened); + SetDeviceName("LockDemo._chip._udp.local."); while (true) { diff --git a/examples/platform/nrf528xx/app/Server.cpp b/examples/platform/nrf528xx/app/Server.cpp index 5558576c50c3f9..1a9100e0a5ae28 100644 --- a/examples/platform/nrf528xx/app/Server.cpp +++ b/examples/platform/nrf528xx/app/Server.cpp @@ -15,6 +15,8 @@ * limitations under the License. */ +#include "Server.h" + #include "FreeRTOS.h" #include "nrf_log.h" #include "task.h" @@ -36,7 +38,14 @@ #include #include -#include "Server.h" +#if CHIP_ENABLE_OPENTHREAD +#include +#include +#include +#include +#include +#include +#endif #include "attribute-storage.h" #include "chip-zcl/chip-zcl-zpro-codec.h" @@ -46,6 +55,7 @@ using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::Transport; +using namespace ::chip::DeviceLayer; // Transport Callbacks namespace { @@ -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 }; @@ -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(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(); diff --git a/examples/platform/nrf528xx/app/include/Server.h b/examples/platform/nrf528xx/app/include/Server.h index bba06d4ddc0491..8660b03391ef32 100644 --- a/examples/platform/nrf528xx/app/include/Server.h +++ b/examples/platform/nrf528xx/app/include/Server.h @@ -28,5 +28,7 @@ using DemoSessionManager = chip::SecureSessionMgr; void StartServer(DemoSessionManager * sessions); void InitDataModelHandler(); +void SetDeviceName(const char * newDeviceName); +void SendUDPBroadCast(); #endif // NRF5_COMMON_SERVER_H