• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #ifndef android_hardware_automotive_vehicle_V2_0_impl_CommBase_H_
18 #define android_hardware_automotive_vehicle_V2_0_impl_CommBase_H_
19 
20 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 #include "VehicleHalProto.pb.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace vehicle {
31 namespace V2_0 {
32 
33 namespace impl {
34 
35 /**
36  * MessageProcess is an interface implemented by VehicleEmulator to process messages received
37  * over a CommConn.
38  */
39 class MessageProcessor {
40    public:
41     virtual ~MessageProcessor() = default;
42 
43     /**
44      * Process a single message received over a CommConn. Populate the given respMsg with the reply
45      * message we should send.
46      */
47     virtual void processMessage(emulator::EmulatorMessage const& rxMsg,
48                                 emulator::EmulatorMessage& respMsg) = 0;
49 };
50 
51 /**
52  * This is the interface that both PipeComm and SocketComm use to represent a connection. The
53  * connection will listen for commands on a separate 'read' thread.
54  */
55 class CommConn {
56    public:
CommConn(MessageProcessor * messageProcessor)57     CommConn(MessageProcessor* messageProcessor) : mMessageProcessor(messageProcessor) {}
58 
~CommConn()59     virtual ~CommConn() {}
60 
61     /**
62      * Start the read thread reading messages from this connection.
63      */
64     virtual void start();
65 
66     /**
67      * Closes a connection if it is open.
68      */
69     virtual void stop();
70 
71     /**
72      * Returns true if the connection is open and available to send/receive.
73      */
74     virtual bool isOpen() = 0;
75 
76     /**
77      * Blocking call to read data from the connection.
78      *
79      * @return std::vector<uint8_t> Serialized protobuf data received from emulator.  This will be
80      *              an empty vector if the connection was closed or some other error occurred.
81      */
82     virtual std::vector<uint8_t> read() = 0;
83 
84     /**
85      * Transmits a string of data to the emulator.
86      *
87      * @param data Serialized protobuf data to transmit.
88      *
89      * @return int Number of bytes transmitted, or -1 if failed.
90      */
91     virtual int write(const std::vector<uint8_t>& data) = 0;
92 
93     /**
94      * Serialized and send the given message to the other side.
95      */
96     void sendMessage(emulator::EmulatorMessage const& msg);
97 
98    protected:
99     std::unique_ptr<std::thread> mReadThread;
100     MessageProcessor* mMessageProcessor;
101 
102     /**
103      * A thread that reads messages in a loop, and responds. You can stop this thread by calling
104      * stop().
105      */
106     void readThread();
107 };
108 
109 }  // namespace impl
110 
111 }  // namespace V2_0
112 }  // namespace vehicle
113 }  // namespace automotive
114 }  // namespace hardware
115 }  // namespace android
116 
117 #endif  // android_hardware_automotive_vehicle_V2_0_impl_CommBase_H_
118