blob: 75c39654b8cb1003554b815b224d638afc2c85ce [file] [log] [blame]
Dheeraj Shettycc231012014-07-02 21:27:57 +02001/*
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 /**
Dheeraj Shettycc231012014-07-02 21:27:57 +020091 * 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 * Clean up method to be called on command close.
103 */
104 void onCommandsSocketClosed(void);
105
106 /**
107 * Datatype to handle the socket list.
108 */
109 typedef struct RilSapSocketList {
110 RilSapSocket* socket;
111 RilSapSocketList *next;
112 } RilSapSocketList;
113
114 protected:
115 /**
116 * Process each record read from the socket and
117 * push a new request created from that record to
118 * the dispatch request queue.
119 *
120 * @param The record data.
121 * @param The record length.
122 */
123 void pushRecord(void *record, size_t recordlen);
124
125 /**
126 * Socket handler to be called when a request has
127 * been completed.
128 *
129 * @param Token associated with the request.
130 * @param Error, if any, while processing the request.
131 * @param The response payload.
132 * @param Response payload length.
133 */
134 void onRequestComplete(RIL_Token t,RIL_Errno e,
135 void *response, size_t response_len);
136
137 /**
138 * Socket handler to be called when there is an
139 * unsolicited response.
140 *
141 * @param Message id.
142 * @param Response data.
143 * @param Response data length.
144 */
145 void onUnsolicitedResponse(int unsolResponse,
146 void *data, size_t datalen);
147
148 /**
149 * Class method to get the socket from the socket list.
150 *
151 * @param Socket id.
152 * @return the sap socket.
153 */
154 static RilSapSocket* getSocketById(RIL_SOCKET_ID socketId);
155
156 /**
157 * Method to send response to SAP. It does an atomic write operation on the
158 * socket.
159 *
160 * @param the response header with the payload.
161 */
162 void sendResponse(MsgHeader *hdr);
163
164 /**
165 * A loop for processing the requests in the request dispatch queue.
166 */
167 void *processRequestsLoop(void);
168
169 /**
170 * Class method to add the sap socket to the list of sockets.
171 * Does nothing if the socket is already present in the list.
172 * Otherwise, calls the constructor of the parent class(To startlistening)
173 * and add socket to the socket list.
174 */
175 static void addSocketToList(const char *socketName, RIL_SOCKET_ID socketid,
176 RIL_RadioFunctions *uimFuncs);
177
178 /**
179 * Check if a socket of the given name exists in the socket list.
180 *
181 * @param Socket name.
182 * @return true if exists, false otherwise.
183 */
184 static bool SocketExists(const char *socketName);
185
186 /**
187 * Send a clean up SAP DISCONNECT if the socket disconnects before doing a SAP
188 * disconnect.
189 */
190 void sendDisconnect(void);
191
192 /**
193 * Dispatch the clean up disconnect request.
194 */
195 void dispatchDisconnect(MsgHeader *req);
196
197
198 private:
199 /**
200 * Constructor.
201 *
202 * @param Socket name.
203 * @param Socket id.
204 * @param Radio functions.
205 */
206 RilSapSocket(const char *socketName,
207 RIL_SOCKET_ID socketId,
208 RIL_RadioFunctions *inputUimFuncs);
209
210 /**
Pavel Zhamaitsiak6eec8192015-08-18 11:40:01 -0700211 * Dispatches the request to the lower layers.
212 * It calls the on request function.
Dheeraj Shettycc231012014-07-02 21:27:57 +0200213 *
214 * @param The request message.
215 */
216 void dispatchRequest(MsgHeader *request);
217
218 /**
219 * Class method that selects the socket on which the onRequestComplete
220 * is called.
221 *
222 * @param Token associated with the request.
223 * @param Error, if any, while processing the request.
224 * @param The response payload.
225 * @param Response payload length.
226 */
227 static void sOnRequestComplete(RIL_Token t,
228 RIL_Errno e, void *response, size_t responselen);
229
230#if defined(ANDROID_MULTI_SIM)
231 /**
232 * Class method that selects the socket on which the onUnsolicitedResponse
233 * is called.
234 *
235 * @param Message id.
236 * @param Response data.
237 * @param Response data length.
238 * @param Socket id.
239 */
240 static void sOnUnsolicitedResponse(int unsolResponse, const void *data,
241 size_t datalen, RIL_SOCKET_ID socket_id);
242#else
243 /**
244 * Class method that selects the socket on which the onUnsolicitedResponse
245 * is called.
246 *
247 * @param Message id.
248 * @param Response data.
249 * @param Response data length.
250 */
251 static void sOnUnsolicitedResponse(int unsolResponse, const void *data,
252 size_t datalen);
253#endif
254};
255
256#endif /*RIL_UIM_SOCKET_H_INCLUDED*/