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_UIM_SOCKET_H_INCLUDED
     18 #define RIL_UIM_SOCKET_H_INCLUDED
     19 #define RIL_SHLIB
     20 #include "telephony/ril.h"
     21 #include "RilSocket.h"
     22 #include <hardware/ril/librilutils/proto/sap-api.pb.h>
     23 
     24 /**
     25  * RilSapSocket is a derived class, derived from the RilSocket abstract
     26  * class, representing sockets for communication between bluetooth SAP module and
     27  * the ril daemon.
     28  * <p>
     29  * This class performs the following functions :
     30  * <ul>
     31  *     <li>Initialize the socket.
     32  *     <li>Process the requests coming on the socket.
     33  *     <li>Provide handlers for Unsolicited and request responses.
     34  *     <li>Request and pending response queue handling.
     35  * </ul>
     36  */
     37 class RilSapSocket : public RilSocket {
     38     /**
     39      * Function pointer to the ril initialization funtion.
     40      *
     41      * @param Ril environment variable with place request and
     42      *        response handlers and timeout handler.
     43      *
     44      * @param Number of arguements for the initialization function.
     45      *
     46      * @param Arguements to the initialization function used to
     47      *        generate instance id of the ril daemon.
     48      *
     49      * @return Radio functions with handlers for onRequest, onStateRequest,
     50      *         supports, onCancel and getVersion.
     51      */
     52     RIL_RadioFunctions *(*UimInit)(const struct RIL_Env *, int argc, char **argv);
     53 
     54     /**
     55      * Place holder for the radio functions returned by the initialization
     56      * function. Currenty only onRequest handler is being used.
     57      */
     58     RIL_RadioFunctions* uimFuncs;
     59 
     60     /**
     61      * Wrapper struct for handling the requests in the queue.
     62      */
     63     typedef struct SapSocketRequest {
     64         int token;
     65         MsgHeader* curr;
     66         struct SapSocketRequest* p_next;
     67         RIL_SOCKET_ID socketId;
     68     } SapSocketRequest;
     69 
     70     /**
     71      * Queue for requests that are pending dispatch.
     72      */
     73     Ril_queue<SapSocketRequest> dispatchQueue;
     74 
     75     /**
     76      * Queue for requests that are dispatched but are pending response
     77      */
     78     Ril_queue<SapSocketRequest> pendingResponseQueue;
     79 
     80     public:
     81         /**
     82          * Initialize the socket and add the socket to the list.
     83          *
     84          * @param Name of the socket.
     85          * @param Radio functions to be used by the socket.
     86          */
     87         static void initSapSocket(const char *socketName,
     88         RIL_RadioFunctions *uimFuncs);
     89 
     90         /**
     91          * Process requests from the dispatch request queue.
     92          * @param Request to be dispatched.
     93          */
     94         int processRequest(MsgHeader *request);
     95 
     96         /**
     97          * Ril envoronment variable that holds the request and
     98          * unsol response handlers.
     99          */
    100         static struct RIL_Env uimRilEnv;
    101 
    102         /**
    103          * Function to print the socket list.
    104          */
    105         static void printList();
    106 
    107         /**
    108          * Clean up method to be called on command close.
    109          */
    110         void onCommandsSocketClosed(void);
    111 
    112         /**
    113          * Datatype to handle the socket list.
    114          */
    115         typedef struct RilSapSocketList {
    116             RilSapSocket* socket;
    117             RilSapSocketList *next;
    118         } RilSapSocketList;
    119 
    120     protected:
    121         /**
    122          * Process each record read from the socket and
    123          * push a new request created from that record to
    124          * the dispatch request queue.
    125          *
    126          * @param The record data.
    127          * @param The record length.
    128          */
    129         void pushRecord(void *record, size_t recordlen);
    130 
    131         /**
    132          * Socket handler to be called when a request has
    133          * been completed.
    134          *
    135          * @param Token associated with the request.
    136          * @param Error, if any, while processing the request.
    137          * @param The response payload.
    138          * @param Response payload length.
    139          */
    140         void onRequestComplete(RIL_Token t,RIL_Errno e,
    141         void *response, size_t response_len);
    142 
    143         /**
    144          * Socket handler to be called when there is an
    145          * unsolicited response.
    146          *
    147          * @param Message id.
    148          * @param Response data.
    149          * @param Response data length.
    150          */
    151         void onUnsolicitedResponse(int unsolResponse,
    152         void *data, size_t datalen);
    153 
    154         /**
    155          * Class method to get the socket from the socket list.
    156          *
    157          * @param Socket id.
    158          * @return the sap socket.
    159          */
    160         static RilSapSocket* getSocketById(RIL_SOCKET_ID socketId);
    161 
    162         /**
    163          * Method to send response to SAP. It does an atomic write operation on the
    164          * socket.
    165          *
    166          * @param the response header with the payload.
    167          */
    168         void sendResponse(MsgHeader *hdr);
    169 
    170         /**
    171          * A loop for processing the requests in the request dispatch queue.
    172          */
    173         void *processRequestsLoop(void);
    174 
    175         /**
    176          * Class method to add the sap socket to the list of sockets.
    177          * Does nothing if the socket is already present in the list.
    178          * Otherwise, calls the constructor of the parent class(To startlistening)
    179          * and add socket to the socket list.
    180          */
    181         static void addSocketToList(const char *socketName, RIL_SOCKET_ID socketid,
    182         RIL_RadioFunctions *uimFuncs);
    183 
    184         /**
    185          * Check if a socket of the given name exists in the socket list.
    186          *
    187          * @param Socket name.
    188          * @return true if exists, false otherwise.
    189          */
    190         static bool SocketExists(const char *socketName);
    191 
    192         /**
    193          * Send a clean up SAP DISCONNECT if the socket disconnects before doing a SAP
    194          * disconnect.
    195          */
    196         void sendDisconnect(void);
    197 
    198         /**
    199          * Dispatch the clean up disconnect request.
    200          */
    201         void dispatchDisconnect(MsgHeader *req);
    202 
    203 
    204     private:
    205         /**
    206          * Constructor.
    207          *
    208          * @param Socket name.
    209          * @param Socket id.
    210          * @param Radio functions.
    211          */
    212         RilSapSocket(const char *socketName,
    213         RIL_SOCKET_ID socketId,
    214         RIL_RadioFunctions *inputUimFuncs);
    215 
    216         /**
    217          * Called by the processRequest method to dispatch the request to
    218          * the lower layers. It calls the on request function.
    219          *
    220          * @param The request message.
    221          */
    222         void dispatchRequest(MsgHeader *request);
    223 
    224         /**
    225          * Class method that selects the socket on which the onRequestComplete
    226          * is called.
    227          *
    228          * @param Token associated with the request.
    229          * @param Error, if any, while processing the request.
    230          * @param The response payload.
    231          * @param Response payload length.
    232          */
    233         static void sOnRequestComplete(RIL_Token t,
    234         RIL_Errno e, void *response, size_t responselen);
    235 
    236 #if defined(ANDROID_MULTI_SIM)
    237         /**
    238          * Class method that selects the socket on which the onUnsolicitedResponse
    239          * is called.
    240          *
    241          * @param Message id.
    242          * @param Response data.
    243          * @param Response data length.
    244          * @param Socket id.
    245          */
    246         static void sOnUnsolicitedResponse(int unsolResponse, const void *data,
    247         size_t datalen, RIL_SOCKET_ID socket_id);
    248 #else
    249         /**
    250          * Class method that selects the socket on which the onUnsolicitedResponse
    251          * is called.
    252          *
    253          * @param Message id.
    254          * @param Response data.
    255          * @param Response data length.
    256          */
    257         static void sOnUnsolicitedResponse(int unsolResponse, const void *data,
    258         size_t datalen);
    259 #endif
    260 };
    261 
    262 #endif /*RIL_UIM_SOCKET_H_INCLUDED*/
    263