forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[example/nrfconnect] Added support for sending UDP broadcast messages. (
project-chip#3110) * [example/nrfconnect] Added support for sending UDP broadcast messages. nrfconnect platform doesn't support sending UDP broadcast messages periodically by PublishService as other platforms do (e.h. nrf5) * Added Service component with PublishService implementation to support sending UDP broadcast messages. * Restyled by clang-format Co-authored-by: Restyled.io <commits@restyled.io>
- Loading branch information
1 parent
baeacbe
commit 2b85b54
Showing
6 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file implements the service publishing code for example usage. | ||
*/ | ||
|
||
#include "Service.h" | ||
|
||
#include <string.h> | ||
|
||
#include <logging/log.h> | ||
#include <platform/CHIPDeviceLayer.h> | ||
#include <zephyr.h> | ||
|
||
#if CHIP_ENABLE_OPENTHREAD | ||
#include <openthread/message.h> | ||
#include <openthread/udp.h> | ||
#include <platform/OpenThread/OpenThreadUtils.h> | ||
#include <platform/ThreadStackManager.h> | ||
#include <platform/nrfconnect/ThreadStackManagerImpl.h> | ||
#endif | ||
|
||
LOG_MODULE_DECLARE(serv); | ||
|
||
// Transport Callbacks | ||
namespace { | ||
|
||
char deviceName[CONFIG_BT_DEVICE_NAME_MAX]; | ||
constexpr uint16_t kUDPBroadcastPort = 23367; | ||
|
||
} // namespace | ||
|
||
void PublishService() | ||
{ | ||
chip::Inet::IPAddress addr; | ||
if (!chip::DeviceLayer::ConnectivityMgrImpl().IsThreadAttached()) | ||
{ | ||
return; | ||
} | ||
chip::DeviceLayer::ThreadStackMgrImpl().LockThreadStack(); | ||
otError error = OT_ERROR_NONE; | ||
otMessageInfo messageInfo; | ||
otUdpSocket mSocket; | ||
otMessage * message = nullptr; | ||
|
||
memset(&mSocket, 0, sizeof(mSocket)); | ||
memset(&messageInfo, 0, sizeof(messageInfo)); | ||
|
||
// Use mesh local EID by default, if we have GUA, use that IP address. | ||
memcpy(&messageInfo.mSockAddr, otThreadGetMeshLocalEid(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance()), | ||
sizeof(messageInfo.mSockAddr)); | ||
|
||
// Select a address to send | ||
const otNetifAddress * otAddrs = otIp6GetUnicastAddresses(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance()); | ||
for (const otNetifAddress * otAddr = otAddrs; otAddr != NULL; otAddr = otAddr->mNext) | ||
{ | ||
addr = chip::DeviceLayer::Internal::ToIPAddress(otAddr->mAddress); | ||
if (otAddr->mValid && addr.IsIPv6GlobalUnicast()) | ||
{ | ||
memcpy(&messageInfo.mSockAddr, &(otAddr->mAddress), sizeof(otAddr->mAddress)); | ||
break; | ||
} | ||
} | ||
|
||
message = otUdpNewMessage(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance(), nullptr); | ||
otIp6AddressFromString("ff03::1", &messageInfo.mPeerAddr); | ||
messageInfo.mPeerPort = kUDPBroadcastPort; | ||
otMessageAppend(message, deviceName, static_cast<uint16_t>(strlen(deviceName))); | ||
|
||
error = otUdpSend(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance(), &mSocket, message, &messageInfo); | ||
|
||
if (error != OT_ERROR_NONE && message != nullptr) | ||
{ | ||
otMessageFree(message); | ||
LOG_INF("Failed to otUdpSend: %d", error); | ||
} | ||
chip::DeviceLayer::ThreadStackMgrImpl().UnlockThreadStack(); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef NRFCONNECT_COMMON_SERVICE_H | ||
#define NRFCONNECT_COMMON_SERVICE_H | ||
|
||
void PublishService(); | ||
|
||
#endif // NRFCONNECT_COMMON_SERVICE_H |