-
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.
ESP32: Add RPC service to lock-app. (#7584)
- Loading branch information
1 parent
7b64cc4
commit 1573591
Showing
8 changed files
with
388 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include "sdkconfig.h" | ||
#if CONFIG_ENABLE_PW_RPC | ||
#include "AppTask.h" | ||
#include "PigweedLoggerMutex.h" | ||
#include "RpcService.h" | ||
#include "button_service/button_service.rpc.pb.h" | ||
#include "device_service/device_service.rpc.pb.h" | ||
#include "esp_log.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/event_groups.h" | ||
#include "freertos/semphr.h" | ||
#include "freertos/task.h" | ||
#include "locking_service/locking_service.rpc.pb.h" | ||
#include "pw_log/log.h" | ||
#include "pw_rpc/server.h" | ||
#include "pw_sys_io/sys_io.h" | ||
#include <support/logging/CHIPLogging.h> | ||
|
||
const char * TAG = "RPC"; | ||
|
||
using chip::DeviceLayer::ConfigurationMgr; | ||
|
||
static bool uartInitialised; | ||
|
||
extern "C" void __wrap_esp_log_write(esp_log_level_t level, const char * tag, const char * format, ...) | ||
{ | ||
va_list v; | ||
va_start(v, format); | ||
#ifndef CONFIG_LOG_DEFAULT_LEVEL_NONE | ||
if (uartInitialised) | ||
{ | ||
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; | ||
size_t len = vsnprintf(formattedMsg, sizeof formattedMsg, format, v); | ||
if (len >= sizeof formattedMsg) | ||
{ | ||
len = sizeof formattedMsg - 1; | ||
} | ||
PigweedLogger::putString(formattedMsg, len); | ||
} | ||
#endif | ||
va_end(v); | ||
} | ||
|
||
namespace chip { | ||
namespace rpc { | ||
|
||
class Locking final : public generated::Locking<Locking> | ||
{ | ||
|
||
public: | ||
pw::Status Set(ServerContext &, const chip_rpc_LockingState & request, pw_protobuf_Empty & response) | ||
{ | ||
BoltLockMgr().InitiateAction(AppEvent::kEventType_Lock, | ||
request.locked ? BoltLockManager::LOCK_ACTION : BoltLockManager::UNLOCK_ACTION); | ||
return pw::OkStatus(); | ||
} | ||
|
||
pw::Status Get(ServerContext &, const pw_protobuf_Empty & request, chip_rpc_LockingState & response) | ||
{ | ||
response.locked = BoltLockMgr().IsUnlocked(); | ||
return pw::OkStatus(); | ||
} | ||
}; | ||
|
||
class Button final : public generated::Button<Button> | ||
{ | ||
public: | ||
pw::Status Event(ServerContext &, const chip_rpc_ButtonEvent & request, pw_protobuf_Empty & response) | ||
{ | ||
GetAppTask().ButtonEventHandler(request.idx, request.pushed); | ||
return pw::OkStatus(); | ||
} | ||
}; | ||
|
||
class Device final : public generated::Device<Device> | ||
{ | ||
public: | ||
pw::Status FactoryReset(ServerContext & ctx, const pw_protobuf_Empty & request, pw_protobuf_Empty & response) | ||
{ | ||
ConfigurationMgr().InitiateFactoryReset(); | ||
return pw::OkStatus(); | ||
} | ||
pw::Status Reboot(ServerContext & ctx, const pw_protobuf_Empty & request, pw_protobuf_Empty & response) | ||
{ | ||
return pw::OkStatus(); | ||
} | ||
pw::Status TriggerOta(ServerContext & ctx, const pw_protobuf_Empty & request, pw_protobuf_Empty & response) | ||
{ | ||
// TODO: auto err = DeviceLayer::SoftwareUpdateMgr().CheckNow(); | ||
return pw::Status::Unimplemented(); | ||
} | ||
pw::Status GetDeviceInfo(ServerContext &, const pw_protobuf_Empty & request, chip_rpc_DeviceInfo & response) | ||
{ | ||
response.vendor_id = 1234; | ||
response.product_id = 5678; | ||
response.software_version = 0; | ||
return pw::OkStatus(); | ||
} | ||
}; | ||
|
||
constexpr size_t kRpcStackSizeBytes = (4 * 1024); | ||
constexpr uint8_t kRpcTaskPriority = 5; | ||
|
||
TaskHandle_t rpcTaskHandle; | ||
|
||
Button button_service; | ||
Locking locking_service; | ||
Device device_service; | ||
|
||
void RegisterServices(pw::rpc::Server & server) | ||
{ | ||
server.RegisterService(locking_service); | ||
server.RegisterService(button_service); | ||
server.RegisterService(device_service); | ||
} | ||
|
||
void RunRpcService(void *) | ||
{ | ||
Start(RegisterServices, &logger_mutex); | ||
} | ||
|
||
void Init() | ||
{ | ||
PigweedLogger::init(); | ||
uartInitialised = true; | ||
|
||
ESP_LOGI(TAG, "----------- esp32-pigweed-service starting -----------"); | ||
|
||
xTaskCreate(RunRpcService, "RPC", kRpcStackSizeBytes / sizeof(StackType_t), nullptr, kRpcTaskPriority, &rpcTaskHandle); | ||
} | ||
|
||
} // namespace rpc | ||
} // namespace chip | ||
|
||
#endif |
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,27 @@ | ||
/* | ||
* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace chip { | ||
namespace rpc { | ||
|
||
void Init(); | ||
void RunRpcService(void *); | ||
|
||
} // namespace rpc | ||
} // namespace chip |
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,21 @@ | ||
syntax = "proto3"; | ||
|
||
import 'pw_protobuf_protos/common.proto'; | ||
|
||
package chip.rpc; | ||
|
||
message LockingState { | ||
bool locked = 1; | ||
} | ||
|
||
service Locking { | ||
// Set will return OK if all supported fields are successfully applied, any | ||
// unsupported fields will be ignored. | ||
// Get can be used to determine which fields are supported. | ||
rpc Set(LockingState) returns (pw.protobuf.Empty){} | ||
|
||
// Get will populate all of the supported locking state fields with the | ||
// current values. This can be used to discover the devices supported | ||
// locking features. | ||
rpc Get(pw.protobuf.Empty) returns (LockingState){} | ||
} |
Oops, something went wrong.