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
20 #include "rilSocketQueue.h"
21 #include <ril_event.h>
26 * Abstract socket class representing sockets in rild.
28 * This class performs the following functions :
30 * <li> Start socket listen.
31 * <li> Handle socket listen and command callbacks.
48 * Listen socket file descriptor.
53 * Commands socket file descriptor.
58 * Socket request loop thread id.
60 pthread_t socketThreadId;
63 * Listen event callack. Callback called when the other ends does accept.
65 ril_event_cb listenCb;
68 * Commands event callack.Callback called when there are requests from the other side.
70 ril_event_cb commandCb;
73 * Listen event to be added to eventloop after socket listen.
75 struct ril_event listenEvent;
78 * Commands event to be added to eventloop after accept.
80 struct ril_event callbackEvent;
83 * Static socket listen handler. Chooses the socket to call the listen callback
88 * @param Parameter for the listen handler.
90 static void sSocketListener(int fd, short flags, void *param);
93 * Static socket request handler. Chooses the socket to call the request handler on.
97 * @param Parameter for the request handler.
99 static void sSocketRequestsHandler(int fd, short flags, void *param);
102 * Process record from the record stream and push the requests onto the queue.
104 * @param record data.
105 * @param record length.
107 virtual void pushRecord(void *record, size_t recordlen) = 0;
110 * Socket lock for writing data on the socket.
112 pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;
115 * The loop to process the incoming requests.
117 virtual void *processRequestsLoop(void) = 0;
120 friend void *::ril_socket_process_requests_loop(void *arg);
127 * @param Socket name.
130 RilSocket(const char* socketName, RIL_SOCKET_ID socketId) {
136 * Clean up function on commands socket close.
138 virtual void onCommandsSocketClosed(void) = 0;
141 * Function called on new commands socket connect. Request loop thread is started here.
143 void onNewCommandConnect(void);
146 * Set listen socket fd.
150 void setListenFd(int listenFd);
153 * Set commands socket fd.
157 void setCommandFd(int commandFd);
160 * Get listen socket fd.
164 int getListenFd(void);
167 * Get commands socket fd.
169 * @return Commands fd.
171 int getCommandFd(void);
174 * Set listen event callback.
176 * @param Input event callback.
178 void setListenCb(ril_event_cb listenCb);
181 * Set command event callback.
183 * @param Input event callback.
185 void setCommandCb(ril_event_cb commandCb);
188 * Get listen event callback.
190 * @return Listen event callback.
192 ril_event_cb getListenCb(void);
195 * Gey command event callback.
197 * @return Command event callback.
199 ril_event_cb getCommandCb(void);
204 * @param Input event.
206 void setListenEvent(ril_event listenEvent);
209 * Set command callback event.
211 * @param Input event.
213 void setCallbackEvent(ril_event commandEvent);
218 * @return Listen event.
220 ril_event* getListenEvent(void);
223 * Get commands callback event.
225 * @return Commands callback event.
227 ril_event* getCallbackEvent(void);
229 virtual ~RilSocket(){}
234 * Start listening on the socket and add the socket listen callback event.
236 * @return Result of the socket listen.
238 int socketInit(void);
241 * Socket request handler
243 * @param Commands fd.
245 * @param Record stream.
247 void socketRequestsHandler(int fd, short flags, RecordStream *rs);
252 RilSocket *socketPtr;
255 socketClient(RilSocket *socketPtr, RecordStream *rs) {
256 this->socketPtr = socketPtr;
261 typedef struct MySocketListenParam {
262 SocketListenParam sListenParam;
264 } MySocketListenParam;
266 typedef void* (RilSocket::*RilSocketFuncPtr)(void);
267 typedef void (RilSocket::*RilSocketEventPtr)(int fd,short flags, void *param);
268 typedef void* (*PthreadPtr)(void*);