Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / lighting-app / nrfconnect / main / Rpc.cpp
1 /*
2  *
3  *    Copyright (c) 2020 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 "Rpc.h"
20 #include "AppTask.h"
21 #include "PigweedLogger.h"
22 #include "PigweedLoggerMutex.h"
23 #include "pigweed/RpcService.h"
24
25 #include "main/pigweed_lighting.rpc.pb.h"
26 #include "pw_hdlc/rpc_channel.h"
27 #include "pw_hdlc/rpc_packets.h"
28 #include "pw_rpc/server.h"
29 #include "pw_stream/sys_io_stream.h"
30 #include "pw_sys_io/sys_io.h"
31 #include "pw_sys_io_nrfconnect/init.h"
32
33 #include <array>
34 #include <kernel.h>
35 #include <logging/log.h>
36
37 LOG_MODULE_DECLARE(app);
38
39 namespace chip {
40 namespace rpc {
41
42 class LightingService final : public generated::LightingService<LightingService>
43 {
44 public:
45     pw::Status ButtonEvent(ServerContext & ctx, const chip_rpc_Button & request, chip_rpc_Empty & response)
46     {
47         GetAppTask().ButtonEventHandler(request.action << request.idx /* button_state */, 1 << request.idx /* has_changed */);
48         return pw::OkStatus();
49     }
50 };
51
52 namespace {
53
54 using std::byte;
55
56 constexpr size_t kRpcTaskSize = 4096;
57 constexpr int kRpcPriority    = 5;
58
59 K_THREAD_STACK_DEFINE(rpc_stack_area, kRpcTaskSize);
60 struct k_thread rpc_thread_data;
61
62 chip::rpc::LightingService lighting_service;
63
64 void RegisterServices(pw::rpc::Server & server)
65 {
66     server.RegisterService(lighting_service);
67 }
68
69 } // namespace
70
71 k_tid_t Init()
72 {
73     pw_sys_io_Init();
74     k_tid_t tid = k_thread_create(&rpc_thread_data, rpc_stack_area, K_THREAD_STACK_SIZEOF(rpc_stack_area), RunRpcService, NULL,
75                                   NULL, NULL, kRpcPriority, 0, K_NO_WAIT);
76     return tid;
77 }
78
79 void RunRpcService(void *, void *, void *)
80 {
81     Start(RegisterServices, &logger_mutex);
82 }
83
84 } // namespace rpc
85 } // namespace chip