|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "InteractiveServer.h" |
| 20 | + |
| 21 | +#include <json/json.h> |
| 22 | +#include <platform/CHIPDeviceLayer.h> |
| 23 | + |
| 24 | +using namespace chip::DeviceLayer; |
| 25 | + |
| 26 | +namespace { |
| 27 | +constexpr const char * kClusterIdKey = "clusterId"; |
| 28 | +constexpr const char * kEndpointIdKey = "endpointId"; |
| 29 | +constexpr const char * kAttributeIdKey = "attributeId"; |
| 30 | +constexpr const char * kWaitTypeKey = "waitType"; |
| 31 | +constexpr const char * kAttributeWriteKey = "writeAttribute"; |
| 32 | +constexpr const char * kAttributeReadKey = "readAttribute"; |
| 33 | +constexpr const char * kCommandIdKey = "commandId"; |
| 34 | +constexpr const char * kWaitForCommissioningCommand = "WaitForCommissioning"; |
| 35 | + |
| 36 | +std::string JsonToString(Json::Value & json) |
| 37 | +{ |
| 38 | + Json::FastWriter writer; |
| 39 | + writer.omitEndingLineFeed(); |
| 40 | + return writer.write(json); |
| 41 | +} |
| 42 | + |
| 43 | +void OnPlatformEvent(const ChipDeviceEvent * event, intptr_t arg); |
| 44 | + |
| 45 | +void OnCommissioningComplete(intptr_t context) |
| 46 | +{ |
| 47 | + PlatformMgr().RemoveEventHandler(OnPlatformEvent); |
| 48 | + InteractiveServer::GetInstance().CommissioningComplete(); |
| 49 | +} |
| 50 | + |
| 51 | +void OnPlatformEvent(const ChipDeviceEvent * event, intptr_t arg) |
| 52 | +{ |
| 53 | + switch (event->Type) |
| 54 | + { |
| 55 | + case DeviceEventType::kCommissioningComplete: |
| 56 | + PlatformMgr().ScheduleWork(OnCommissioningComplete, arg); |
| 57 | + break; |
| 58 | + } |
| 59 | +} |
| 60 | +} // namespace |
| 61 | + |
| 62 | +InteractiveServer * InteractiveServer::instance = nullptr; |
| 63 | +InteractiveServer & InteractiveServer::GetInstance() |
| 64 | +{ |
| 65 | + if (instance == nullptr) |
| 66 | + { |
| 67 | + instance = new InteractiveServer(); |
| 68 | + } |
| 69 | + return *instance; |
| 70 | +} |
| 71 | + |
| 72 | +void InteractiveServer::Run(const chip::Optional<uint16_t> port) |
| 73 | +{ |
| 74 | + mIsReady = false; |
| 75 | + wsThread = std::thread(&WebSocketServer::Run, &mWebSocketServer, port, this); |
| 76 | +} |
| 77 | + |
| 78 | +bool InteractiveServer::OnWebSocketMessageReceived(char * msg) |
| 79 | +{ |
| 80 | + ChipLogError(chipTool, "Receive message: %s", msg); |
| 81 | + if (strcmp(msg, kWaitForCommissioningCommand) == 0) |
| 82 | + { |
| 83 | + mIsReady = false; |
| 84 | + PlatformMgr().AddEventHandler(OnPlatformEvent); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + mIsReady = true; |
| 89 | + } |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +bool InteractiveServer::Command(const chip::app::ConcreteCommandPath & path) |
| 94 | +{ |
| 95 | + VerifyOrReturnValue(mIsReady, false); |
| 96 | + |
| 97 | + Json::Value value; |
| 98 | + value[kClusterIdKey] = path.mClusterId; |
| 99 | + value[kEndpointIdKey] = path.mEndpointId; |
| 100 | + value[kCommandIdKey] = path.mCommandId; |
| 101 | + |
| 102 | + auto valueStr = JsonToString(value); |
| 103 | + LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); |
| 104 | + return mIsReady; |
| 105 | +} |
| 106 | + |
| 107 | +bool InteractiveServer::ReadAttribute(const chip::app::ConcreteAttributePath & path) |
| 108 | +{ |
| 109 | + VerifyOrReturnValue(mIsReady, false); |
| 110 | + |
| 111 | + Json::Value value; |
| 112 | + value[kClusterIdKey] = path.mClusterId; |
| 113 | + value[kEndpointIdKey] = path.mEndpointId; |
| 114 | + value[kAttributeIdKey] = path.mAttributeId; |
| 115 | + value[kWaitTypeKey] = kAttributeReadKey; |
| 116 | + |
| 117 | + auto valueStr = JsonToString(value); |
| 118 | + LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); |
| 119 | + return mIsReady; |
| 120 | +} |
| 121 | + |
| 122 | +bool InteractiveServer::WriteAttribute(const chip::app::ConcreteAttributePath & path) |
| 123 | +{ |
| 124 | + VerifyOrReturnValue(mIsReady, false); |
| 125 | + |
| 126 | + Json::Value value; |
| 127 | + value[kClusterIdKey] = path.mClusterId; |
| 128 | + value[kEndpointIdKey] = path.mEndpointId; |
| 129 | + value[kAttributeIdKey] = path.mAttributeId; |
| 130 | + value[kWaitTypeKey] = kAttributeWriteKey; |
| 131 | + |
| 132 | + auto valueStr = JsonToString(value); |
| 133 | + LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); |
| 134 | + return mIsReady; |
| 135 | +} |
| 136 | + |
| 137 | +void InteractiveServer::CommissioningComplete() |
| 138 | +{ |
| 139 | + VerifyOrReturn(!mIsReady); |
| 140 | + mIsReady = true; |
| 141 | + |
| 142 | + Json::Value value = Json::objectValue; |
| 143 | + auto valueStr = JsonToString(value); |
| 144 | + LogErrorOnFailure(mWebSocketServer.Send(valueStr.c_str())); |
| 145 | +} |
0 commit comments