Debian Codegraph @

Xpjproject/pjlib/src/pj/os_symbian.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #ifndef __OS_SYMBIAN_H__
20 #define __OS_SYMBIAN_H__
21
22 #include <pj/assert.h>
23 #include <pj/errno.h>
24 #include <pj/sock.h>
25 #include <pj/os.h>
26 #include <pj/string.h>
27
28 #include <e32base.h>
29 #include <e32cmn.h>
30 #include <e32std.h>
31 #include <es_sock.h>
32 #include <in_sock.h>
33 #include <charconv.h>
34 #include <utf.h>
35 #include <e32cons.h>
36
37 // Forward declarations
38 class CPjSocketReader;
39
40 #ifndef PJ_SYMBIAN_TIMER_PRIORITY
41 # define PJ_SYMBIAN_TIMER_PRIORITY EPriorityNormal
42 #endif
43
44 //
45 // PJLIB Symbian's Socket
46 //
47 class CPjSocket
48 {
49 public:
50 enum
51 {
52 MAX_LEN = 1500,
53 };
54
55 // Construct CPjSocket
56 CPjSocket(int af, int sock_type, RSocket &sock)
57 : af_(af), sock_(sock), sock_type_(sock_type), connected_(false),
58 sockReader_(NULL)
59 {
60 }
61
62 // Destroy CPjSocket
63 ~CPjSocket();
64
65 // Get address family
66 int GetAf() const
67 {
68 return af_;
69 }
70
71 // Get the internal RSocket
72 RSocket& Socket()
73 {
74 return sock_;
75 }
76
77 // Get socket connected flag.
78 bool IsConnected() const
79 {
80 return connected_;
81 }
82
83 // Set socket connected flag.
84 void SetConnected(bool connected)
85 {
86 connected_ = connected;
87 }
88
89 // Get socket type
90 int GetSockType() const
91 {
92 return sock_type_;
93 }
94
95 // Returns true if socket is a datagram
96 bool IsDatagram() const
97 {
98 return sock_type_ == KSockDatagram;
99 }
100
101 // Get socket reader, if any.
102 // May return NULL.
103 CPjSocketReader *Reader()
104 {
105 return sockReader_;
106 }
107
108 // Create socket reader.
109 CPjSocketReader *CreateReader(unsigned max_len=CPjSocket::MAX_LEN);
110
111 // Delete socket reader when it's not wanted.
112 void DestroyReader();
113
114 private:
115 int af_;
116 RSocket sock_; // Must not be reference, or otherwise
117 // it may point to local variable!
118 unsigned sock_type_;
119
120 bool connected_;
121 CPjSocketReader *sockReader_;
122 };
123
124
125 //
126 // Socket reader, used by select() and ioqueue abstraction
127 //
128 class CPjSocketReader : public CActive
129 {
130 public:
131 // Construct.
132 static CPjSocketReader *NewL(CPjSocket &sock, unsigned max_len=CPjSocket::MAX_LEN);
133
134 // Destroy;
135 ~CPjSocketReader();
136
137 // Start asynchronous read from the socket.
138 void StartRecv(void (*cb)(void *key)=NULL,
139 void *key=NULL,
140 TDes8 *aDesc = NULL,
141 TUint flags = 0);
142
143 // Start asynchronous read from the socket.
144 void StartRecvFrom(void (*cb)(void *key)=NULL,
145 void *key=NULL,
146 TDes8 *aDesc = NULL,
147 TUint flags = 0,
148 TSockAddr *fromAddr = NULL);
149
150 // Cancel asynchronous read.
151 void DoCancel();
152
153 // Implementation: called when read has completed.
154 void RunL();
155
156 // Check if there's pending data.
157 bool HasData() const
158 {
159 return buffer_.Length() != 0;
160 }
161
162 // Append data to aDesc, up to aDesc's maximum size.
163 // If socket is datagram based, buffer_ will be clared.
164 void ReadData(TDes8 &aDesc, TInetAddr *addr=NULL);
165
166 private:
167 CPjSocket &sock_;
168 bool isDatagram_;
169 TPtr8 buffer_;
170 TInetAddr recvAddr_;
171
172 void (*readCb_)(void *key);
173 void *key_;
174
175 //
176 // Constructor
177 //
178 CPjSocketReader(CPjSocket &sock);
179 void ConstructL(unsigned max_len);
180 };
181
182
183
184 //
185 // Time-out Timer Active Object
186 //
187 class CPjTimeoutTimer : public CActive
188 {
189 public:
190 static CPjTimeoutTimer *NewL();
191 ~CPjTimeoutTimer();
192
193 void StartTimer(TUint miliSeconds);
194 bool HasTimedOut() const;
195
196 protected:
197 virtual void RunL();
198 virtual void DoCancel();
199 virtual TInt RunError(TInt aError);
200
201 private:
202 RTimer timer_;
203 pj_bool_t hasTimedOut_;
204
205 CPjTimeoutTimer();
206 void ConstructL();
207 };
208
209
210
211 //
212 // Symbian OS helper for PJLIB
213 //
214 class PjSymbianOS
215 {
216 public:
217 //
218 // Get the singleton instance of PjSymbianOS
219 //
220 static PjSymbianOS *Instance();
221
222 //
223 // Set parameters
224 //
225 void SetParameters(pj_symbianos_params *params);
226
227 //
228 // Initialize.
229 //
230 TInt Initialize();
231
232 //
233 // Shutdown.
234 //
235 void Shutdown();
236
237
238 //
239 // Socket helper.
240 //
241
242 // Get RSocketServ instance to be used by all sockets.
243 RSocketServ &SocketServ()
244 {
245 return appSocketServ_ ? *appSocketServ_ : socketServ_;
246 }
247
248 // Get RConnection instance, if any.
249 RConnection *Connection()
250 {
251 return appConnection_;
252 }
253
254 // Convert TInetAddr to pj_sockaddr_in
255 static inline pj_status_t Addr2pj(const TInetAddr & sym_addr,
256 pj_sockaddr &pj_addr,
257 int *addr_len,
258 pj_bool_t convert_ipv4_mapped_addr = PJ_FALSE)
259 {
260 TUint fam = sym_addr.Family();
261 pj_bzero(&pj_addr, *addr_len);
262 if (fam == PJ_AF_INET ||
263 (convert_ipv4_mapped_addr &&
264 fam == PJ_AF_INET6 &&
265 sym_addr.IsV4Mapped()))
266 {
267 pj_addr.addr.sa_family = PJ_AF_INET;
268 PJ_ASSERT_RETURN(*addr_len>=(int)sizeof(pj_sockaddr_in), PJ_ETOOSMALL);
269 pj_addr.ipv4.sin_addr.s_addr = pj_htonl(sym_addr.Address());
270 pj_addr.ipv4.sin_port = pj_htons((pj_uint16_t) sym_addr.Port());
271 *addr_len = sizeof(pj_sockaddr_in);
272 } else if (fam == PJ_AF_INET6) {
273 PJ_ASSERT_RETURN(*addr_len>=(int)sizeof(pj_sockaddr_in6), PJ_ETOOSMALL);
274 const TIp6Addr & ip6 = sym_addr.Ip6Address();
275 pj_addr.addr.sa_family = PJ_AF_INET6;
276 pj_memcpy(&pj_addr.ipv6.sin6_addr, ip6.u.iAddr8, 16);
277 pj_addr.ipv6.sin6_port = pj_htons((pj_uint16_t) sym_addr.Port());
278 pj_addr.ipv6.sin6_scope_id = pj_htonl(sym_addr.Scope());
279 pj_addr.ipv6.sin6_flowinfo = pj_htonl(sym_addr.FlowLabel());
280 *addr_len = sizeof(pj_sockaddr_in6);
281 } else {
282 pj_assert(!"Unsupported address family");
283 return PJ_EAFNOTSUP;
284 }
285
286 return PJ_SUCCESS;
287 }
288
289
290 // Convert pj_sockaddr_in to TInetAddr
291 static inline pj_status_t pj2Addr(const pj_sockaddr &pj_addr,
292 int addrlen,
293 TInetAddr & sym_addr)
294 {
295 if (pj_addr.addr.sa_family == PJ_AF_INET) {
296 PJ_ASSERT_RETURN(addrlen >= (int)sizeof(pj_sockaddr_in), PJ_EINVAL);
297 sym_addr.Init(KAfInet);
298 sym_addr.SetAddress((TUint32)pj_ntohl(pj_addr.ipv4.sin_addr.s_addr));
299 sym_addr.SetPort(pj_ntohs(pj_addr.ipv4.sin_port));
300 } else if (pj_addr.addr.sa_family == PJ_AF_INET6) {
301 TIp6Addr ip6;
302
303 PJ_ASSERT_RETURN(addrlen>=(int)sizeof(pj_sockaddr_in6), PJ_EINVAL);
304 pj_memcpy(ip6.u.iAddr8, &pj_addr.ipv6.sin6_addr, 16);
305 sym_addr.Init(KAfInet6);
306 sym_addr.SetAddress(ip6);
307 sym_addr.SetScope(pj_ntohl(pj_addr.ipv6.sin6_scope_id));
308 sym_addr.SetFlowLabel(pj_ntohl(pj_addr.ipv6.sin6_flowinfo));
309 } else {
310 pj_assert(!"Unsupported address family");
311 }
312 return PJ_SUCCESS;
313 }
314
315
316 //
317 // Resolver helper
318 //
319
320 // Get RHostResolver instance
321 RHostResolver & GetResolver(int af)
322 {
323 if (af==PJ_AF_INET6) {
324 return appHostResolver6_ ? *appHostResolver6_ : hostResolver6_;
325 } else {
326 return appHostResolver_ ? *appHostResolver_ : hostResolver_;
327 }
328 }
329
330 //
331 // Return true if the access point connection is up
332 //
333 bool IsConnectionUp() const
334 {
335 return isConnectionUp_;
336 }
337
338 //
339 // Set access point connection status
340 //
341 void SetConnectionStatus(bool up)
342 {
343 isConnectionUp_ = up;
344 }
345
346 //
347 // Unicode Converter
348 //
349
350 // Convert to Unicode
351 TInt ConvertToUnicode(TDes16 &aUnicode, const TDesC8 &aForeign);
352
353 // Convert from Unicode
354 TInt ConvertFromUnicode(TDes8 &aForeign, const TDesC16 &aUnicode);
355
356 //
357 // Get console
358 //
359
360 // Get console
361 CConsoleBase *Console()
362 {
363 return console_;
364 }
365
366 //
367 // Get select() timeout timer.
368 //
369 CPjTimeoutTimer *SelectTimeoutTimer()
370 {
371 return selectTimeoutTimer_;
372 }
373
374 //
375 // Wait for any active objects to run.
376 //
377 void WaitForActiveObjects(TInt aPriority = CActive::EPriorityStandard)
378 {
379 TInt aError;
380 CActiveScheduler::Current()->WaitForAnyRequest();
381 CActiveScheduler::RunIfReady(aError, aPriority);
382 }
383
384 private:
385 bool isConnectionUp_;
386
387 bool isSocketServInitialized_;
388 RSocketServ socketServ_;
389
390 bool isResolverInitialized_;
391 RHostResolver hostResolver_;
392 RHostResolver hostResolver6_;
393
394 CConsoleBase* console_;
395
396 CPjTimeoutTimer *selectTimeoutTimer_;
397
398 // App parameters
399 RSocketServ *appSocketServ_;
400 RConnection *appConnection_;
401 RHostResolver *appHostResolver_;
402 RHostResolver *appHostResolver6_;
403
404 private:
405 PjSymbianOS();
406 };
407
408 // This macro is used to check the access point connection status and return
409 // failure if the AP connection is down or unusable. See the documentation
410 // of pj_symbianos_set_connection_status() for more info
411 #define PJ_SYMBIAN_CHECK_CONNECTION() \
412 PJ_SYMBIAN_CHECK_CONNECTION2(PJ_ECANCELLED)
413
414 #define PJ_SYMBIAN_CHECK_CONNECTION2(retval) \
415 do { \
416 if (!PjSymbianOS::Instance()->IsConnectionUp()) \
417 return retval; \
418 } while (0);
419
420 #endif /* __OS_SYMBIAN_H__ */
421