blob: 8346b3691ea7f7c75f6a561c570e6044aa7ef6a3 [file] [log] [blame]
Andrei Homescu9d8adb12022-08-02 04:38:30 +00001/*
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#define TLOG_TAG "binderRpcTestService"
18
Andrei Homescu9d8adb12022-08-02 04:38:30 +000019#include <binder/RpcServerTrusty.h>
20#include <inttypes.h>
21#include <lib/tipc/tipc.h>
22#include <lk/err_ptr.h>
23#include <stdio.h>
24#include <trusty_log.h>
25#include <vector>
26
27#include "binderRpcTestCommon.h"
28
29using namespace android;
Andrei Homescu9d8adb12022-08-02 04:38:30 +000030using binder::Status;
31
32static int gConnectionCounter = 0;
33
34class MyBinderRpcTestTrusty : public MyBinderRpcTestDefault {
35public:
36 wp<RpcServerTrusty> server;
37
38 Status countBinders(std::vector<int32_t>* out) override {
39 return countBindersImpl(server, out);
40 }
41
42 Status scheduleShutdown() override {
43 // TODO: Trusty does not support shutting down the tipc event loop,
44 // so we just terminate the service app since it is marked
45 // restart_on_exit
46 exit(EXIT_SUCCESS);
47 }
48
49 // TODO(b/242940548): implement echoAsFile and concatFiles
50};
51
52struct ServerInfo {
53 std::unique_ptr<std::string> port;
54 sp<RpcServerTrusty> server;
55};
56
57int main(void) {
58 TLOGI("Starting service\n");
59
60 tipc_hset* hset = tipc_hset_create();
61 if (IS_ERR(hset)) {
62 TLOGE("Failed to create handle set (%d)\n", PTR_ERR(hset));
63 return EXIT_FAILURE;
64 }
65
66 const auto port_acl = RpcServerTrusty::PortAcl{
67 .flags = IPC_PORT_ALLOW_NS_CONNECT | IPC_PORT_ALLOW_TA_CONNECT,
68 };
69
70 std::vector<ServerInfo> servers;
71 for (auto serverVersion : testVersions()) {
72 ServerInfo serverInfo{
73 .port = std::make_unique<std::string>(trustyIpcPort(serverVersion)),
74 };
75 TLOGI("Adding service port '%s'\n", serverInfo.port->c_str());
76
77 // Message size needs to be large enough to cover all messages sent by the
78 // tests: SendAndGetResultBackBig sends two large strings.
79 constexpr size_t max_msg_size = 4096;
Tomasz Wasilczykbe685c02023-11-30 20:20:53 -080080 auto server =
Andrei Homescu9d8adb12022-08-02 04:38:30 +000081 RpcServerTrusty::make(hset, serverInfo.port->c_str(),
82 std::shared_ptr<const RpcServerTrusty::PortAcl>(&port_acl),
83 max_msg_size);
Tomasz Wasilczykbe685c02023-11-30 20:20:53 -080084 if (server == nullptr) {
Andrei Homescu9d8adb12022-08-02 04:38:30 +000085 return EXIT_FAILURE;
86 }
87
Andrei Homescu9d8adb12022-08-02 04:38:30 +000088 serverInfo.server = server;
Steven Morelandca3f6382023-05-11 23:23:26 +000089 if (!serverInfo.server->setProtocolVersion(serverVersion)) {
90 return EXIT_FAILURE;
91 }
Steven Morelanddfb92182023-05-24 17:48:59 +000092 serverInfo.server->setPerSessionRootObject(
93 [=](wp<RpcSession> /*session*/, const void* /*addrPtr*/, size_t /*len*/) {
94 auto service = sp<MyBinderRpcTestTrusty>::make();
95 // Assign a unique connection identifier to service->port so
96 // getClientPort returns a unique value per connection
97 service->port = ++gConnectionCounter;
98 service->server = server;
99 return service;
100 });
Andrei Homescu9d8adb12022-08-02 04:38:30 +0000101
102 servers.push_back(std::move(serverInfo));
103 }
104
105 return tipc_run_event_loop(hset);
106}