2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef RIL_SOCKET_H_INCLUDED
18 #define RIL_SOCKET_H_INCLUDED
19 #include <libril/ril_ex.h>
20 #include "rilSocketQueue.h"
21 #include <ril_event.h>
25 extern "C" void *ril_socket_process_requests_loop(void *arg);
28 * Abstract socket class representing sockets in rild.
30 * This class performs the following functions :
32 * <li> Start socket listen.
33 * <li> Handle socket listen and command callbacks.
50 * Listen socket file descriptor.
55 * Commands socket file descriptor.
60 * Socket request loop thread id.
62 pthread_t socketThreadId;
65 * Listen event callack. Callback called when the other ends does accept.
67 ril_event_cb listenCb;
70 * Commands event callack.Callback called when there are requests from the other side.
72 ril_event_cb commandCb;
75 * Listen event to be added to eventloop after socket listen.
77 struct ril_event listenEvent;
80 * Commands event to be added to eventloop after accept.
82 struct ril_event callbackEvent;
85 * Static socket listen handler. Chooses the socket to call the listen callback
90 * @param Parameter for the listen handler.
92 static void sSocketListener(int fd, short flags, void *param);
95 * Static socket request handler. Chooses the socket to call the request handler on.
99 * @param Parameter for the request handler.
101 static void sSocketRequestsHandler(int fd, short flags, void *param);
104 * Process record from the record stream and push the requests onto the queue.
106 * @param record data.
107 * @param record length.
109 virtual void pushRecord(void *record, size_t recordlen) = 0;
112 * Socket lock for writing data on the socket.
114 pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
117 * The loop to process the incoming requests.
119 virtual void *processRequestsLoop(void) = 0;
122 friend void *::ril_socket_process_requests_loop(void *arg);
129 * @param Socket name.
132 RilSocket(const char* socketName, RIL_SOCKET_ID socketId) {
138 * Clean up function on commands socket close.
140 virtual void onCommandsSocketClosed(void) = 0;
143 * Function called on new commands socket connect. Request loop thread is started here.
145 void onNewCommandConnect(void);
148 * Set listen socket fd.
152 void setListenFd(int listenFd);
155 * Set commands socket fd.
159 void setCommandFd(int commandFd);
162 * Get listen socket fd.
166 int getListenFd(void);
169 * Get commands socket fd.
171 * @return Commands fd.
173 int getCommandFd(void);
176 * Set listen event callback.
178 * @param Input event callback.
180 void setListenCb(ril_event_cb listenCb);
183 * Set command event callback.
185 * @param Input event callback.
187 void setCommandCb(ril_event_cb commandCb);
190 * Get listen event callback.
192 * @return Listen event callback.
194 ril_event_cb getListenCb(void);
197 * Gey command event callback.
199 * @return Command event callback.
201 ril_event_cb getCommandCb(void);
206 * @param Input event.
208 void setListenEvent(ril_event listenEvent);
211 * Set command callback event.
213 * @param Input event.
215 void setCallbackEvent(ril_event commandEvent);
220 * @return Listen event.
222 ril_event* getListenEvent(void);
225 * Get commands callback event.
227 * @return Commands callback event.
229 ril_event* getCallbackEvent(void);
231 virtual ~RilSocket(){}
236 * Start listening on the socket and add the socket listen callback event.
238 * @return Result of the socket listen.
240 int socketInit(void);
243 * Socket request handler
245 * @param Commands fd.
247 * @param Record stream.
249 void socketRequestsHandler(int fd, short flags, RecordStream *rs);
254 RilSocket *socketPtr;
257 socketClient(RilSocket *socketPtr, RecordStream *rs) {
258 this->socketPtr = socketPtr;
263 typedef struct MySocketListenParam {
264 SocketListenParam sListenParam;
266 } MySocketListenParam;
268 typedef void* (RilSocket::*RilSocketFuncPtr)(void);
269 typedef void (RilSocket::*RilSocketEventPtr)(int fd,short flags, void *param);
270 typedef void* (*PthreadPtr)(void*);