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 #define LOG_TAG "VtsFuzzerBinderServer"
28 #include <log/log.h>
29 #include <utils/RefBase.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", __func__, 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 int target_version_major = data.readInt32();
72       const int target_version_minor = data.readInt32();
73       const char* module_name = data.readCString();
74       int32_t result =
75           LoadHal(string(path), target_class, target_type, target_version_major,
76                   target_version_minor, string(module_name));
77       ALOGD("BnVtsFuzzer::%s LoadHal(%s) -> %i", __FUNCTION__, path, result);
78       if (reply == NULL) {
79         ALOGE("reply == NULL");
80         abort();
81       }
82 #ifdef VTS_FUZZER_BINDER_DEBUG
83       alog << reply << endl;
84 #endif
85       reply->writeInt32(result);
86       break;
87     }
88     case STATUS: {
89       int32_t type = data.readInt32();
90       int32_t result = Status(type);
91 
92       ALOGD("BnVtsFuzzer::%s status(%i) -> %i", __FUNCTION__, type, result);
93       if (reply == NULL) {
94         ALOGE("reply == NULL");
95         abort();
96       }
97 #ifdef VTS_FUZZER_BINDER_DEBUG
98       alog << reply << endl;
99 #endif
100       reply->writeInt32(result);
101       break;
102     }
103     case CALL: {
104       const char* arg = data.readCString();
105       const string& result = Call(arg);
106 
107       ALOGD("BnVtsFuzzer::%s call(%s) = %i", __FUNCTION__, arg, result.c_str());
108       if (reply == NULL) {
109         ALOGE("reply == NULL");
110         abort();
111       }
112 #ifdef VTS_FUZZER_BINDER_DEBUG
113       alog << reply << endl;
114 #endif
115       reply->writeCString(result.c_str());
116       break;
117     }
118     case GET_FUNCTIONS: {
119       const char* result = GetFunctions();
120 
121       if (reply == NULL) {
122         ALOGE("reply == NULL");
123         abort();
124       }
125 #ifdef VTS_FUZZER_BINDER_DEBUG
126       alog << reply << endl;
127 #endif
128       reply->writeCString(result);
129       break;
130     }
131     default:
132       return BBinder::onTransact(code, data, reply, flags);
133   }
134   return NO_ERROR;
135 }
136 
137 class VtsFuzzerServer : public BnVtsFuzzer {
138  public:
VtsFuzzerServer(android::vts::VtsHalDriverManager * driver_manager,const char * lib_path)139   VtsFuzzerServer(android::vts::VtsHalDriverManager* driver_manager,
140                   const char* lib_path)
141       : driver_manager_(driver_manager), lib_path_(lib_path) {}
142 
Exit()143   void Exit() { printf("VtsFuzzerServer::Exit\n"); }
144 
LoadHal(const string & path,int target_class,int target_type,int target_version_major,int target_version_minor,const string & module_name)145   int32_t LoadHal(const string& path, int target_class, int target_type,
146                   int target_version_major, int target_version_minor,
147                   const string& module_name) {
148     printf("VtsFuzzerServer::LoadHal(%s)\n", path.c_str());
149     bool success = driver_manager_->LoadTargetComponent(
150         path.c_str(), lib_path_, target_class, target_type,
151         target_version_major, target_version_minor, "", "", "",
152         module_name.c_str());
153     cout << "Result: " << success << std::endl;
154     if (success) {
155       return 0;
156     } else {
157       return -1;
158     }
159   }
160 
Status(int32_t type)161   int32_t Status(int32_t type) {
162     printf("VtsFuzzerServer::Status(%i)\n", type);
163     return 0;
164   }
165 
Call(const string & arg)166   string Call(const string& arg) {
167     printf("VtsFuzzerServer::Call(%s)\n", arg.c_str());
168     FunctionCallMessage* call_msg = new FunctionCallMessage();
169     google::protobuf::TextFormat::MergeFromString(arg, call_msg);
170     return driver_manager_->CallFunction(call_msg);
171   }
172 
GetFunctions()173   const char* GetFunctions() {
174     printf("Get functions*");
175     vts::ComponentSpecificationMessage* spec =
176         driver_manager_->GetComponentSpecification();
177     if (!spec) {
178       return NULL;
179     }
180     string* output = new string();
181     printf("getfunctions serial1\n");
182     if (google::protobuf::TextFormat::PrintToString(*spec, output)) {
183       printf("getfunctions length %d\n", output->length());
184       return output->c_str();
185     } else {
186       printf("can't serialize the interface spec message to a string.\n");
187       return NULL;
188     }
189   }
190 
191  private:
192   android::vts::VtsHalDriverManager* driver_manager_;
193   const char* lib_path_;
194 };
195 
StartBinderServer(const string & service_name,android::vts::VtsHalDriverManager * driver_manager,const char * lib_path)196 void StartBinderServer(const string& service_name,
197                        android::vts::VtsHalDriverManager* driver_manager,
198                        const char* lib_path) {
199   defaultServiceManager()->addService(
200       String16(service_name.c_str()),
201       new VtsFuzzerServer(driver_manager, lib_path));
202   android::ProcessState::self()->startThreadPool();
203   IPCThreadState::self()->joinThreadPool();
204 }
205 
206 }  // namespace vts
207 }  // namespace android
208 
209 #endif
210