8000 Restyle [example/nrfconnect] Added support for sending UDP broadcast messages. by restyled-io[bot] · Pull Request #3111 · project-chip/connectedhomeip · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Restyle [example/nrfconnect] Added support for sending UDP broadcast messages. #3111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CHIP_ROOT}/config/nrfconnect/)
include(nrfconnect-app)

project(chip-nrf52840-lighting-example)
target_include_directories(app PRIVATE main/include ${LIGHTING_COMMON} ${NRFCONNECT_COMMON}/util/include ${CHIP_APP_SERVER}/include)
target_include_directories(app PRIVATE main/include ${LIGHTING_COMMON} ${NRFCONNECT_COMMON}/util/include ${NRFCONNECT_COMMON}/app/include ${CHIP_APP_SERVER}/include)
target_sources(app PRIVATE
main/AppTask.cpp
main/LightingManager.cpp
Expand All @@ -37,6 +37,7 @@ target_sources(app PRIVATE
${LIGHTING_COMMON}/gen/znet-bookkeeping.c
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
${NRFCONNECT_COMMON}/util/ThreadUtil.cpp
${NRFCONNECT_COMMON}/app/Service.cpp
${CHIP_APP_SERVER}/DataModelHandler.cpp
${CHIP_APP_SERVER}/Server.cpp
${CHIP_APP_SERVER}/QRCodeUtil.cpp
Expand Down
14 changes: 13 additions & 1 deletion examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "LightingManager.h"
#include "QRCodeUtil.h"
#include "Server.h"
#include "Service.h"
#include "ThreadUtil.h"

#include <platform/CHIPDeviceLayer.h>
Expand All @@ -47,6 +48,7 @@ constexpr int kAppEventQueueSize = 10;
constexpr int kExampleVendorID = 0xabcd;
constexpr uint8_t kButtonPushEvent = 1;
constexpr uint8_t kButtonReleaseEvent = 0;
constexpr uint32_t kPublishServicePeriodUs = 5000000;

K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent));
k_timer sFunctionTimer;
Expand Down Expand Up @@ -104,7 +106,8 @@ int AppTask::Init()

int AppTask::StartApp()
{
int ret = Init();
int ret = Init();
uint64_t mLastPublishServiceTimeUS = 0;

if (ret)
{
Expand Down Expand Up @@ -179,6 +182,15 @@ int AppTask::StartApp()
sStatusLED.Animate();
sUnusedLED.Animate();
sUnusedLED_1.Animate();

uint64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
uint64_t nextChangeTimeUS = mLastPublishServiceTimeUS + kPublishServicePeriodUs;

if (nowUS > nextChangeTimeUS)
{
PublishService();
mLastPublishServiceTimeUS = nowUS;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CHIP_ROOT}/config/nrfconnect/)
include(nrfconnect-app)

project(chip-nrf52840-lock-example)
target_include_directories(app PRIVATE main/include ${LOCK_COMMON} ${NRFCONNECT_COMMON}/util/include ${CHIP_APP_SERVER}/include)
target_include_directories(app PRIVATE main/include ${LOCK_COMMON} ${NRFCONNECT_COMMON}/util/include ${NRFCONNECT_COMMON}/app/include ${CHIP_APP_SERVER}/include)
target_sources(app PRIVATE
main/AppTask.cpp
main/BoltLockManager.cpp
Expand All @@ -37,6 +37,7 @@ target_sources(app PRIVATE
${LOCK_COMMON}/gen/znet-bookkeeping.c
${NRFCONNECT_COMMON}/util/LEDWidget.cpp
${NRFCONNECT_COMMON}/util/ThreadUtil.cpp
${NRFCONNECT_COMMON}/app/Service.cpp
${CHIP_APP_SERVER}/DataModelHandler.cpp
${CHIP_APP_SERVER}/Server.cpp
${CHIP_APP_SERVER}/QRCodeUtil.cpp
Expand Down
15 changes: 14 additions & 1 deletion examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "LEDWidget.h"
#include "QRCodeUtil.h"
#include "Server.h"
#include "Service.h"
#include "ThreadUtil.h"

#include <platform/CHIPDeviceLayer.h>
Expand All @@ -41,6 +42,8 @@
LOG_MODULE_DECLARE(app);
K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), APP_EVENT_QUEUE_SIZE, alignof(AppEvent));

constexpr uint32_t kPublishServicePeriodUs = 5000000;

static LEDWidget sStatusLED;
static LEDWidget sLockLED;
static LEDWidget sUnusedLED;
Expand Down Expand Up @@ -95,7 +98,8 @@ int AppTask::Init()

int AppTask::StartApp()
{
int ret = Init();
int ret = Init();
uint64_t mLastPublishServiceTimeUS = 0;

if (ret)
{
Expand Down Expand Up @@ -171,6 +175,15 @@ int AppTask::StartApp()
sLockLED.Animate();
sUnusedLED.Animate();
sUnusedLED_1.Animate();

uint64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
uint64_t nextChangeTimeUS = mLastPublishServiceTimeUS + kPublishServicePeriodUs;

if (nowUS > nextChangeTimeUS)
{
PublishService();
mLastPublishServiceTimeUS = nowUS;
}
}
}

Expand Down
94 changes: 94 additions & 0 deletions examples/platform/nrfconnect/app/Service.cpp
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();
}
23 changes: 23 additions & 0 deletions examples/platform/nrfconnect/app/include/Service.h
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");
5E1D * 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
0