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 #define LOG_TAG "VtsHalDriverCallbackBase"
17 
18 #include "driver_base/DriverCallbackBase.h"
19 
20 #include <VtsDriverCommUtil.h>
21 #include <android-base/logging.h>
22 
23 #include "component_loader/DllLoader.h"
24 #include "test/vts/proto/AndroidSystemControlMessage.pb.h"
25 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
26 #include "utils/InterfaceSpecUtil.h"
27 
28 using namespace std;
29 
30 namespace android {
31 namespace vts {
32 
33 static std::map<string, string> id_map_;
34 
DriverCallbackBase()35 DriverCallbackBase::DriverCallbackBase() {}
36 
~DriverCallbackBase()37 DriverCallbackBase::~DriverCallbackBase() {}
38 
Register(const VariableSpecificationMessage & message)39 bool DriverCallbackBase::Register(const VariableSpecificationMessage& message) {
40   LOG(DEBUG) << "type = " << message.type();
41   if (!message.is_callback()) {
42     LOG(ERROR) << "ERROR: argument is not a callback.";
43     return false;
44   }
45 
46   if (!message.has_type() || message.type() != TYPE_FUNCTION_POINTER) {
47     LOG(ERROR) << "ERROR: inconsistent message.";
48     return false;
49   }
50 
51   for (const auto& func_pt : message.function_pointer()) {
52     LOG(DEBUG) << "map[" << func_pt.function_name() << "] = " << func_pt.id();
53     id_map_[func_pt.function_name()] = func_pt.id();
54   }
55   return true;
56 }
57 
GetCallbackID(const string & name)58 const char* DriverCallbackBase::GetCallbackID(const string& name) {
59   // TODO: handle when not found.
60   LOG(DEBUG) << "GetCallbackID for " << name << "returns '"
61              << id_map_[name].c_str() << "'";
62   return id_map_[name].c_str();
63 }
64 
RpcCallToAgent(const AndroidSystemCallbackRequestMessage & message,const string & callback_socket_name)65 void DriverCallbackBase::RpcCallToAgent(
66     const AndroidSystemCallbackRequestMessage& message,
67     const string& callback_socket_name) {
68   LOG(DEBUG) << " id = '" << message.id() << "'";
69   if (message.id().empty() || callback_socket_name.empty()) {
70     LOG(DEBUG) << "Abort callback forwarding.";
71     return;
72   }
73   VtsDriverCommUtil util;
74   if (!util.Connect(callback_socket_name)) exit(-1);
75   util.VtsSocketSendMessage(message);
76   util.Close();
77 }
78 
79 }  // namespace vts
80 }  // namespace android
81