-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update examples/placeholder to supports an interactive websocket mode (…
- Loading branch information
1 parent
09ea936
commit 621bbde
Showing
7 changed files
with
239 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include "InteractiveServer.h" | ||
|
||
#include <json/json.h> | ||
#include <platform/CHIPDeviceLayer.h> | ||
|
||
using namespace chip::DeviceLayer; | ||
|
||
namespace { | ||
constexpr const char * kClusterIdKey = "clusterId"; | ||
constexpr const char * kEndpointIdKey = "endpointId"; | ||
constexpr const char * kAttributeIdKey = "attributeId"; | ||
constexpr const char * kWaitTypeKey = "waitType"; | ||
constexpr const char * kAttributeWriteKey = "writeAttribute"; | ||
constexpr const char * kAttributeReadKey = "readAttribute"; | ||
constexpr const char * kCommandIdKey = "commandId"; | ||
constexpr const char * kWaitForCommissioningCommand = "WaitForCommissioning"; | ||
|
||
std::string JsonToString(Json::Value & json) | ||
{ | ||
Json::FastWriter writer; | ||
writer.omitEndingLineFeed(); | ||
return writer.write(json); | ||
} | ||
|
||
void OnPlatformEvent(const ChipDeviceEvent * event, intptr_t arg); | ||
|
||
void OnCommissioningComplete(intptr_t context) | ||
{ | ||
PlatformMgr().RemoveEventHandler(OnPlatformEvent); | ||
InteractiveServer::GetInstance().CommissioningComplete(); | ||
} | ||
|
||
void OnPlatformEvent(const ChipDeviceEvent * event, intptr_t arg) | ||
{ | ||
switch (event->Type) | ||
{ | ||
case DeviceEventType::kCommissioningComplete: | ||
PlatformMgr().ScheduleWork(OnCommissioningComplete, arg); | ||
break; | ||
} | ||
} | ||
} // namespace | ||
|
||
InteractiveServer * InteractiveServer::instance = nullptr; | ||
InteractiveServer & InteractiveServer::GetInstance() | ||
{ | ||
if (instance == nullptr) | ||
{ | ||
instance = new InteractiveServer(); | ||
} | ||
return *instance; | ||
} | ||
|
||
void InteractiveServer::Run(const chip::Optional<uint16_t> port) | ||
{ | ||
mIsReady = false; | ||
wsThread = std::thread(&WebSocketServer::Run, &mWebSocketServer, port, this); | ||
} | ||
|
||
bool InteractiveServer::OnWebSocketMessageReceived(char * msg) | ||
{ | ||
ChipLogError(chipTool, "Receive message: %s", msg); | ||
if (strcmp(msg, kWaitForCommissioningCommand) == 0) | ||
{ | ||
mIsReady = false; | ||
PlatformMgr().AddEventHandler(OnPlatformEvent); | ||
} | ||
else | ||
{ | ||
mIsReady = true; | ||
} | ||
return true; | ||
} | ||
|
||
bool InteractiveServer::Command(const chip::app::ConcreteCommandPath & path) | ||
{ | ||
VerifyOrReturnValue(mIsReady, false); | ||
|
||
Json::Value value; | ||
value[kClusterIdKey] = path.mClusterId; | ||
value[kEndpointIdKey] = path.mEndpointId; | ||
value[kCommandIdKey] = path.mCommandId; | ||
|
||
auto valueStr = JsonToString(value); | ||
LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); | ||
return mIsReady; | ||
} | ||
|
||
bool InteractiveServer::ReadAttribute(const chip::app::ConcreteAttributePath & path) | ||
{ | ||
VerifyOrReturnValue(mIsReady, false); | ||
|
||
Json::Value value; | ||
value[kClusterIdKey] = path.mClusterId; | ||
value[kEndpointIdKey] = path.mEndpointId; | ||
value[kAttributeIdKey] = path.mAttributeId; | ||
value[kWaitTypeKey] = kAttributeReadKey; | ||
|
||
auto valueStr = JsonToString(value); | ||
LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); | ||
return mIsReady; | ||
} | ||
|
||
bool InteractiveServer::WriteAttribute(const chip::app::ConcreteAttributePath & path) | ||
{ | ||
VerifyOrReturnValue(mIsReady, false); | ||
|
||
Json::Value value; | ||
value[kClusterIdKey] = path.mClusterId; | ||
value[kEndpointIdKey] = path.mEndpointId; | ||
value[kAttributeIdKey] = path.mAttributeId; | ||
value[kWaitTypeKey] = kAttributeWriteKey; | ||
|
||
auto valueStr = JsonToString(value); | ||
LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); | ||
return mIsReady; | ||
} | ||
|
||
void InteractiveServer::CommissioningComplete() | ||
{ | ||
VerifyOrReturn(!mIsReady); | ||
mIsReady = true; | ||
|
||
Json::Value value = Json::objectValue; | ||
auto valueStr = JsonToString(value); | ||
LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); | ||
} |
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,47 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <app/ConcreteAttributePath.h> | ||
#include <app/ConcreteCommandPath.h> | ||
#include <thread> | ||
#include <websocket-server/WebSocketServer.h> | ||
|
||
class InteractiveServer : public WebSocketServerDelegate | ||
{ | ||
public: | ||
static InteractiveServer & GetInstance(); | ||
void Run(const chip::Optional<uint16_t> port); | ||
|
||
bool Command(const chip::app::ConcreteCommandPath & path); | ||
bool ReadAttribute(const chip::app::ConcreteAttributePath & path); | ||
bool WriteAttribute(const chip::app::ConcreteAttributePath & path); | ||
void CommissioningComplete(); | ||
|
||
/////////// WebSocketServerDelegate Interface ///////// | ||
bool OnWebSocketMessageReceived(char * msg) override; | ||
|
||
private: | ||
InteractiveServer(){}; | ||
static InteractiveServer * instance; | ||
|
||
WebSocketServer mWebSocketServer; | ||
std::thread wsThread; | ||
bool mIsReady; | ||
}; |
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