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 */
37class 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         * Ril envoronment variable that holds the request and
92         * unsol response handlers.
93         */
94        static struct RIL_Env uimRilEnv;
95
96        /**
97         * Function to print the socket list.
98         */
99        static void printList();
100
101        /**
102         * Dispatches the request to the lower layers.
103         * It calls the on request function.
104         *
105         * @param request The request message.
106         */
107        void dispatchRequest(MsgHeader *request);
108
109        /**
110         * Class method to get the socket from the socket list.
111         *
112         * @param socketId Socket id.
113         * @return the sap socket.
114         */
115        static RilSapSocket* getSocketById(RIL_SOCKET_ID socketId);
116
117        /**
118         * Datatype to handle the socket list.
119         */
120        typedef struct RilSapSocketList {
121            RilSapSocket* socket;
122            RilSapSocketList *next;
123        } RilSapSocketList;
124
125    protected:
126        /**
127         * Socket handler to be called when a request has
128         * been completed.
129         *
130         * @param Token associated with the request.
131         * @param Error, if any, while processing the request.
132         * @param The response payload.
133         * @param Response payload length.
134         */
135        void onRequestComplete(RIL_Token t,RIL_Errno e,
136        void *response, size_t response_len);
137
138        /**
139         * Socket handler to be called when there is an
140         * unsolicited response.
141         *
142         * @param Message id.
143         * @param Response data.
144         * @param Response data length.
145         */
146        void onUnsolicitedResponse(int unsolResponse,
147        void *data, size_t datalen);
148
149        /**
150         * Class method to add the sap socket to the list of sockets.
151         * Does nothing if the socket is already present in the list.
152         * Otherwise, calls the constructor of the parent class(To startlistening)
153         * and add socket to the socket list.
154         */
155        static void addSocketToList(const char *socketName, RIL_SOCKET_ID socketid,
156        RIL_RadioFunctions *uimFuncs);
157
158        /**
159         * Check if a socket of the given name exists in the socket list.
160         *
161         * @param Socket name.
162         * @return true if exists, false otherwise.
163         */
164        static bool SocketExists(const char *socketName);
165
166    private:
167        /**
168         * Constructor.
169         *
170         * @param Socket name.
171         * @param Socket id.
172         * @param Radio functions.
173         */
174        RilSapSocket(const char *socketName,
175        RIL_SOCKET_ID socketId,
176        RIL_RadioFunctions *inputUimFuncs);
177
178        /**
179         * Class method that selects the socket on which the onRequestComplete
180         * is called.
181         *
182         * @param Token associated with the request.
183         * @param Error, if any, while processing the request.
184         * @param The response payload.
185         * @param Response payload length.
186         */
187        static void sOnRequestComplete(RIL_Token t,
188        RIL_Errno e, void *response, size_t responselen);
189
190#if defined(ANDROID_MULTI_SIM)
191        /**
192         * Class method that selects the socket on which the onUnsolicitedResponse
193         * is called.
194         *
195         * @param Message id.
196         * @param Response data.
197         * @param Response data length.
198         * @param Socket id.
199         */
200        static void sOnUnsolicitedResponse(int unsolResponse, const void *data,
201        size_t datalen, RIL_SOCKET_ID socket_id);
202#else
203        /**
204         * Class method that selects the socket on which the onUnsolicitedResponse
205         * is called.
206         *
207         * @param Message id.
208         * @param Response data.
209         * @param Response data length.
210         */
211        static void sOnUnsolicitedResponse(int unsolResponse, const void *data,
212        size_t datalen);
213#endif
214};
215
216#endif /*RIL_UIM_SOCKET_H_INCLUDED*/
217