OSDN Git Service

Merge commit '9a4b19c' into merge2
[android-x86/hardware-ril.git] / libril / RilSocket.h
1 /*
2 * Copyright (C) 2014 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 RIL_SOCKET_H_INCLUDED
18 #define RIL_SOCKET_H_INCLUDED
19 #include "ril_ex.h"
20 #include "rilSocketQueue.h"
21 #include <ril_event.h>
22
23 using namespace std;
24
25 /**
26  * Abstract socket class representing sockets in rild.
27  * <p>
28  * This class performs the following functions :
29  * <ul>
30  *     <li> Start socket listen.
31  *     <li> Handle socket listen and command callbacks.
32  * </ul>
33  */
34 class RilSocket {
35     protected:
36
37         /**
38          * Socket name.
39          */
40         const char* name;
41
42         /**
43          * Socket id.
44          */
45         RIL_SOCKET_ID id;
46
47        /**
48         * Listen socket file descriptor.
49         */
50         int listenFd = -1;
51
52        /**
53         * Commands socket file descriptor.
54         */
55         int commandFd = -1;
56
57        /**
58         * Socket request loop thread id.
59         */
60         pthread_t socketThreadId;
61
62        /**
63         * Listen event callack. Callback called when the other ends does accept.
64         */
65         ril_event_cb listenCb;
66
67        /**
68         * Commands event callack.Callback called when there are requests from the other side.
69         */
70         ril_event_cb commandCb;
71
72         /**
73          * Listen event to be added to eventloop after socket listen.
74          */
75         struct ril_event listenEvent;
76
77         /**
78          * Commands event to be added to eventloop after accept.
79          */
80         struct ril_event callbackEvent;
81
82         /**
83          * Static socket listen handler. Chooses the socket to call the listen callback
84          * from ril.cpp.
85          *
86          * @param Listen fd.
87          * @param flags.
88          * @param Parameter for the listen handler.
89          */
90         static void sSocketListener(int fd, short flags, void *param);
91
92         /**
93          * Static socket request handler. Chooses the socket to call the request handler on.
94          *
95          * @param Commands fd.
96          * @param flags.
97          * @param Parameter for the request handler.
98          */
99         static void sSocketRequestsHandler(int fd, short flags, void *param);
100
101         /**
102          * Process record from the record stream and push the requests onto the queue.
103          *
104          * @param record data.
105          * @param record length.
106          */
107         virtual void pushRecord(void *record, size_t recordlen) = 0;
108
109         /**
110          * Socket lock for writing data on the socket.
111          */
112         pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
113
114         /**
115          * The loop to process the incoming requests.
116          */
117         virtual void *processRequestsLoop(void) = 0;
118
119     private:
120         friend void *::ril_socket_process_requests_loop(void *arg);
121
122     public:
123
124         /**
125          * Constructor.
126          *
127          * @param Socket name.
128          * @param Socket id.
129          */
130         RilSocket(const char* socketName, RIL_SOCKET_ID socketId) {
131             name = socketName;
132             id = socketId;
133         }
134
135         /**
136          * Clean up function on commands socket close.
137          */
138         virtual void onCommandsSocketClosed(void) = 0;
139
140         /**
141          * Function called on new commands socket connect. Request loop thread is started here.
142          */
143         void onNewCommandConnect(void);
144
145         /**
146          * Set listen socket fd.
147          *
148          * @param Input fd.
149          */
150         void setListenFd(int listenFd);
151
152         /**
153          * Set commands socket fd.
154          *
155          * @param Input fd.
156          */
157         void setCommandFd(int commandFd);
158
159         /**
160          * Get listen socket fd.
161          *
162          * @return Listen fd.
163          */
164         int getListenFd(void);
165
166         /**
167          * Get commands socket fd.
168          *
169          * @return Commands fd.
170          */
171         int getCommandFd(void);
172
173         /**
174          * Set listen event callback.
175          *
176          * @param Input event callback.
177          */
178         void setListenCb(ril_event_cb listenCb);
179
180         /**
181          * Set command event callback.
182          *
183          * @param Input event callback.
184          */
185         void setCommandCb(ril_event_cb commandCb);
186
187         /**
188          * Get listen event callback.
189          *
190          * @return Listen event callback.
191          */
192         ril_event_cb getListenCb(void);
193
194         /**
195          * Gey command event callback.
196          *
197          * @return Command event callback.
198          */
199         ril_event_cb getCommandCb(void);
200
201         /**
202          * Set listen event.
203          *
204          * @param Input event.
205          */
206         void setListenEvent(ril_event listenEvent);
207
208         /**
209          * Set command callback event.
210          *
211          * @param Input event.
212          */
213         void setCallbackEvent(ril_event commandEvent);
214
215         /**
216          * Get listen event.
217          *
218          * @return Listen event.
219          */
220         ril_event* getListenEvent(void);
221
222         /**
223          * Get commands callback event.
224          *
225          * @return Commands callback event.
226          */
227         ril_event* getCallbackEvent(void);
228
229         virtual ~RilSocket(){}
230
231     protected:
232
233         /**
234          * Start listening on the socket and add the socket listen callback event.
235          *
236          * @return Result of the socket listen.
237          */
238         int socketInit(void);
239
240         /**
241          * Socket request handler
242          *
243          * @param Commands fd.
244          * @param flags.
245          * @param Record stream.
246          */
247         void socketRequestsHandler(int fd, short flags, RecordStream *rs);
248 };
249
250 class socketClient {
251     public:
252         RilSocket *socketPtr;
253         RecordStream *rs;
254
255         socketClient(RilSocket *socketPtr, RecordStream *rs) {
256             this->socketPtr = socketPtr;
257             this->rs = rs;
258         }
259 };
260
261 typedef struct MySocketListenParam {
262     SocketListenParam sListenParam;
263     RilSocket *socket;
264 } MySocketListenParam;
265
266 typedef void* (RilSocket::*RilSocketFuncPtr)(void);
267 typedef void (RilSocket::*RilSocketEventPtr)(int fd,short flags, void *param);
268 typedef void* (*PthreadPtr)(void*);
269
270 #endif