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