summaryrefslogtreecommitdiffstats
path: root/ril/libril/RilSocket.h
blob: 053df3e4245bc8fa6be0d5b3df617fc93060bd69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef RIL_SOCKET_H_INCLUDED
#define RIL_SOCKET_H_INCLUDED
#include "ril_ex.h"
#include "rilSocketQueue.h"
#include <ril_event.h>

using namespace std;

/**
 * Abstract socket class representing sockets in rild.
 * <p>
 * This class performs the following functions :
 * <ul>
 *     <li> Start socket listen.
 *     <li> Handle socket listen and command callbacks.
 * </ul>
 */
class RilSocket {
    protected:

        /**
         * Socket name.
         */
        const char* name;

        /**
         * Socket id.
         */
        RIL_SOCKET_ID id;

       /**
        * Listen socket file descriptor.
        */
        int listenFd = -1;

       /**
        * Commands socket file descriptor.
        */
        int commandFd = -1;

       /**
        * Socket request loop thread id.
        */
        pthread_t socketThreadId;

       /**
        * Listen event callack. Callback called when the other ends does accept.
        */
        ril_event_cb listenCb;

       /**
        * Commands event callack.Callback called when there are requests from the other side.
        */
        ril_event_cb commandCb;

        /**
         * Listen event to be added to eventloop after socket listen.
         */
        struct ril_event listenEvent;

        /**
         * Commands event to be added to eventloop after accept.
         */
        struct ril_event callbackEvent;

        /**
         * Static socket listen handler. Chooses the socket to call the listen callback
         * from ril.cpp.
         *
         * @param Listen fd.
         * @param flags.
         * @param Parameter for the listen handler.
         */
        static void sSocketListener(int fd, short flags, void *param);

        /**
         * Static socket request handler. Chooses the socket to call the request handler on.
         *
         * @param Commands fd.
         * @param flags.
         * @param Parameter for the request handler.
         */
        static void sSocketRequestsHandler(int fd, short flags, void *param);

        /**
         * Process record from the record stream and push the requests onto the queue.
         *
         * @param record data.
         * @param record length.
         */
        virtual void pushRecord(void *record, size_t recordlen) = 0;

        /**
         * Socket lock for writing data on the socket.
         */
        pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER;

        /**
         * The loop to process the incoming requests.
         */
        virtual void *processRequestsLoop(void) = 0;

    private:
        friend void *::ril_socket_process_requests_loop(void *arg);

    public:

        /**
         * Constructor.
         *
         * @param Socket name.
         * @param Socket id.
         */
        RilSocket(const char* socketName, RIL_SOCKET_ID socketId) {
            name = socketName;
            id = socketId;
        }

        /**
         * Clean up function on commands socket close.
         */
        virtual void onCommandsSocketClosed(void) = 0;

        /**
         * Function called on new commands socket connect. Request loop thread is started here.
         */
        void onNewCommandConnect(void);

        /**
         * Set listen socket fd.
         *
         * @param Input fd.
         */
        void setListenFd(int listenFd);

        /**
         * Set commands socket fd.
         *
         * @param Input fd.
         */
        void setCommandFd(int commandFd);

        /**
         * Get listen socket fd.
         *
         * @return Listen fd.
         */
        int getListenFd(void);

        /**
         * Get commands socket fd.
         *
         * @return Commands fd.
         */
        int getCommandFd(void);

        /**
         * Set listen event callback.
         *
         * @param Input event callback.
         */
        void setListenCb(ril_event_cb listenCb);

        /**
         * Set command event callback.
         *
         * @param Input event callback.
         */
        void setCommandCb(ril_event_cb commandCb);

        /**
         * Get listen event callback.
         *
         * @return Listen event callback.
         */
        ril_event_cb getListenCb(void);

        /**
         * Gey command event callback.
         *
         * @return Command event callback.
         */
        ril_event_cb getCommandCb(void);

        /**
         * Set listen event.
         *
         * @param Input event.
         */
        void setListenEvent(ril_event listenEvent);

        /**
         * Set command callback event.
         *
         * @param Input event.
         */
        void setCallbackEvent(ril_event commandEvent);

        /**
         * Get listen event.
         *
         * @return Listen event.
         */
        ril_event* getListenEvent(void);

        /**
         * Get commands callback event.
         *
         * @return Commands callback event.
         */
        ril_event* getCallbackEvent(void);

        virtual ~RilSocket(){}

    protected:

        /**
         * Start listening on the socket and add the socket listen callback event.
         *
         * @return Result of the socket listen.
         */
        int socketInit(void);

        /**
         * Socket request handler
         *
         * @param Commands fd.
         * @param flags.
         * @param Record stream.
         */
        void socketRequestsHandler(int fd, short flags, RecordStream *rs);
};

class socketClient {
    public:
        RilSocket *socketPtr;
        RecordStream *rs;

        socketClient(RilSocket *socketPtr, RecordStream *rs) {
            this->socketPtr = socketPtr;
            this->rs = rs;
        }
};

typedef struct MySocketListenParam {
    SocketListenParam sListenParam;
    RilSocket *socket;
} MySocketListenParam;

typedef void* (RilSocket::*RilSocketFuncPtr)(void);
typedef void (RilSocket::*RilSocketEventPtr)(int fd,short flags, void *param);
typedef void* (*PthreadPtr)(void*);

#endif