1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "rpc_service_manager.h"
18
19 #include "chre/util/macros.h"
20 #include "chre/util/nanoapp/log.h"
21
22 #define LOG_TAG "[RpcServiceTest]"
23
24 namespace chre {
25 namespace rpc_service_test {
26
Echo(const pw_rpc_EchoMessage & request,pw_rpc_EchoMessage & response)27 pw::Status EchoService::Echo(const pw_rpc_EchoMessage &request,
28 pw_rpc_EchoMessage &response) {
29 RpcServiceManagerSingleton::get()->setPermissionForNextMessage(
30 CHRE_MESSAGE_PERMISSION_NONE);
31 memcpy(response.msg, request.msg,
32 MIN(ARRAY_SIZE(response.msg), ARRAY_SIZE(request.msg)));
33 return pw::OkStatus();
34 }
35
start()36 bool RpcServiceManager::start() {
37 // Make sure nanoapps support publishing at least
38 // CHRE_MINIMUM_RPC_SERVICE_LIMIT services.
39 RpcServer::Service service{
40 .service = mEchoService, .id = 0xca8f7150a3f05847, .version = 0x01020034};
41
42 bool success = true;
43
44 for (uint64_t i = 0; i < CHRE_MINIMUM_RPC_SERVICE_LIMIT - 1; i++) {
45 struct chreNanoappRpcService chreService = {.id = i, .version = 1};
46 success =
47 success && chrePublishRpcServices(&chreService, 1 /*numServices*/);
48 }
49
50 return success && mServer.registerServices(1 /*numServices*/, &service);
51 }
52
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)53 void RpcServiceManager::handleEvent(uint32_t senderInstanceId,
54 uint16_t eventType, const void *eventData) {
55 if (!mServer.handleEvent(senderInstanceId, eventType, eventData)) {
56 LOGE("An RPC error occurred");
57 }
58 }
59
end()60 void RpcServiceManager::end() {
61 mServer.close();
62 }
63
setPermissionForNextMessage(uint32_t permission)64 void RpcServiceManager::setPermissionForNextMessage(uint32_t permission) {
65 mServer.setPermissionForNextMessage(permission);
66 }
67
68 } // namespace rpc_service_test
69 } // namespace chre
70