1 /*
2 * Copyright 2016 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 #ifdef VTS_AGENT_DRIVER_COMM_BINDER // binder
18
19 #include "BinderServer.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include <iostream>
25 #include <string>
26
27 #include <utils/RefBase.h>
28 #define LOG_TAG "VtsFuzzerBinderServer"
29 #include <utils/Log.h>
30 #include <utils/String8.h>
31
32 #include <binder/IBinder.h>
33 #include <binder/IInterface.h>
34 #include <binder/IPCThreadState.h>
35 #include <binder/IServiceManager.h>
36 #include <binder/ProcessState.h>
37 #include <binder/TextOutput.h>
38
39 #include "binder/VtsFuzzerBinderService.h"
40
41 #include <google/protobuf/text_format.h>
42 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
43
44 using namespace std;
45
46 namespace android {
47 namespace vts {
48
49 class BnVtsFuzzer : public BnInterface<IVtsFuzzer> {
50 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
51 uint32_t flags = 0);
52 };
53
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)54 status_t BnVtsFuzzer::onTransact(uint32_t code, const Parcel& data,
55 Parcel* reply, uint32_t flags) {
56 ALOGD("BnVtsFuzzer::%s(%i) %i", __FUNCTION__, code, flags);
57
58 data.checkInterface(this);
59 #ifdef VTS_FUZZER_BINDER_DEBUG
60 alog << data << endl;
61 #endif
62
63 switch (code) {
64 case EXIT:
65 Exit();
66 break;
67 case LOAD_HAL: {
68 const char* path = data.readCString();
69 const int target_class = data.readInt32();
70 const int target_type = data.readInt32();
71 const float target_version = data.readFloat();
72 const char* module_name = data.readCString();
73 int32_t result = LoadHal(string(path), target_class, target_type,
74 target_version, string(module_name));
75 ALOGD("BnVtsFuzzer::%s LoadHal(%s) -> %i", __FUNCTION__, path, result);
76 if (reply == NULL) {
77 ALOGE("reply == NULL");
78 abort();
79 }
80 #ifdef VTS_FUZZER_BINDER_DEBUG
81 alog << reply << endl;
82 #endif
83 reply->writeInt32(result);
84 break;
85 }
86 case STATUS: {
87 int32_t type = data.readInt32();
88 int32_t result = Status(type);
89
90 ALOGD("BnVtsFuzzer::%s status(%i) -> %i", __FUNCTION__, type, result);
91 if (reply == NULL) {
92 ALOGE("reply == NULL");
93 abort();
94 }
95 #ifdef VTS_FUZZER_BINDER_DEBUG
96 alog << reply << endl;
97 #endif
98 reply->writeInt32(result);
99 break;
100 }
101 case CALL: {
102 const char* arg = data.readCString();
103 const string& result = Call(arg);
104
105 ALOGD("BnVtsFuzzer::%s call(%s) = %i", __FUNCTION__, arg, result.c_str());
106 if (reply == NULL) {
107 ALOGE("reply == NULL");
108 abort();
109 }
110 #ifdef VTS_FUZZER_BINDER_DEBUG
111 alog << reply << endl;
112 #endif
113 reply->writeCString(result.c_str());
114 break;
115 }
116 case GET_FUNCTIONS: {
117 const char* result = GetFunctions();
118
119 if (reply == NULL) {
120 ALOGE("reply == NULL");
121 abort();
122 }
123 #ifdef VTS_FUZZER_BINDER_DEBUG
124 alog << reply << endl;
125 #endif
126 reply->writeCString(result);
127 break;
128 }
129 default:
130 return BBinder::onTransact(code, data, reply, flags);
131 }
132 return NO_ERROR;
133 }
134
135 class VtsFuzzerServer : public BnVtsFuzzer {
136 public:
VtsFuzzerServer(android::vts::VtsHalDriverManager * driver_manager,const char * lib_path)137 VtsFuzzerServer(android::vts::VtsHalDriverManager* driver_manager,
138 const char* lib_path)
139 : driver_manager_(driver_manager), lib_path_(lib_path) {}
140
Exit()141 void Exit() { printf("VtsFuzzerServer::Exit\n"); }
142
LoadHal(const string & path,int target_class,int target_type,float target_version,const string & module_name)143 int32_t LoadHal(const string& path, int target_class, int target_type,
144 float target_version, const string& module_name) {
145 printf("VtsFuzzerServer::LoadHal(%s)\n", path.c_str());
146 bool success = driver_manager_->LoadTargetComponent(
147 path.c_str(), lib_path_, target_class, target_type, target_version, "",
148 "", "", module_name.c_str());
149 cout << "Result: " << success << std::endl;
150 if (success) {
151 return 0;
152 } else {
153 return -1;
154 }
155 }
156
Status(int32_t type)157 int32_t Status(int32_t type) {
158 printf("VtsFuzzerServer::Status(%i)\n", type);
159 return 0;
160 }
161
Call(const string & arg)162 string Call(const string& arg) {
163 printf("VtsFuzzerServer::Call(%s)\n", arg.c_str());
164 FunctionCallMessage* call_msg = new FunctionCallMessage();
165 google::protobuf::TextFormat::MergeFromString(arg, call_msg);
166 return driver_manager_->CallFunction(call_msg);
167 }
168
GetFunctions()169 const char* GetFunctions() {
170 printf("Get functions*");
171 vts::ComponentSpecificationMessage* spec =
172 driver_manager_->GetComponentSpecification();
173 if (!spec) {
174 return NULL;
175 }
176 string* output = new string();
177 printf("getfunctions serial1\n");
178 if (google::protobuf::TextFormat::PrintToString(*spec, output)) {
179 printf("getfunctions length %d\n", output->length());
180 return output->c_str();
181 } else {
182 printf("can't serialize the interface spec message to a string.\n");
183 return NULL;
184 }
185 }
186
187 private:
188 android::vts::VtsHalDriverManager* driver_manager_;
189 const char* lib_path_;
190 };
191
StartBinderServer(const string & service_name,android::vts::VtsHalDriverManager * driver_manager,const char * lib_path)192 void StartBinderServer(const string& service_name,
193 android::vts::VtsHalDriverManager* driver_manager,
194 const char* lib_path) {
195 defaultServiceManager()->addService(
196 String16(service_name.c_str()),
197 new VtsFuzzerServer(driver_manager, lib_path));
198 android::ProcessState::self()->startThreadPool();
199 IPCThreadState::self()->joinThreadPool();
200 }
201
202 } // namespace vts
203 } // namespace android
204
205 #endif
206