|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Switches |
|
16 * System Includes |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 |
|
22 #include <stdio.h> |
|
23 #include <assert.h> |
|
24 #ifndef WIN32 |
|
25 #include <pthread.h> |
|
26 #include <unistd.h> |
|
27 #include <errno.h> |
|
28 #include <netinet/in.h> |
|
29 #else |
|
30 #include <winsock2.h> |
|
31 #include <windows.h> |
|
32 #endif |
|
33 |
|
34 /******************************************************************************* |
|
35 * |
|
36 * Local Includes |
|
37 * |
|
38 ******************************************************************************/ |
|
39 #include "CPhone.h" |
|
40 |
|
41 /******************************************************************************* |
|
42 * |
|
43 * Definitions |
|
44 * |
|
45 ******************************************************************************/ |
|
46 #define POLLINTERVAL 1000 |
|
47 |
|
48 #define CLEANUP_SOURCE_AIR_INTERFACE_START_FAILED 1 |
|
49 #define CLEANUP_SOURCE_TE_CHANNEL_START_FAILED 2 |
|
50 #define CLEANUP_SOURCE_INTERNAL_RUN_PHONE 3 |
|
51 #define CLEANUP_SOURCE_MAIN_START_FAILED 4 |
|
52 |
|
53 |
|
54 /******************************************************************************* |
|
55 * |
|
56 * Macro Functions |
|
57 * |
|
58 ******************************************************************************/ |
|
59 #ifndef WIN32 |
|
60 #define closesocket(x) (shutdown(x,SHUT_RDWR),close(x)) |
|
61 #endif |
|
62 |
|
63 /******************************************************************************* |
|
64 * |
|
65 * Macro functions |
|
66 * |
|
67 ******************************************************************************/ |
|
68 #ifndef WIN32 |
|
69 #define Sleep(x) sleep(x/1000) |
|
70 #endif |
|
71 |
|
72 /******************************************************************************* |
|
73 * |
|
74 * Prototypes |
|
75 * |
|
76 ******************************************************************************/ |
|
77 |
|
78 |
|
79 /******************************************************************************* |
|
80 * |
|
81 * Construction |
|
82 * |
|
83 ******************************************************************************/ |
|
84 CPhone::CPhone() : iUdpAirInterface(&iPhoneData,&iLog), iTcpTeChannel(&iPhoneData,&iLog), iFilterPpp(&iPhoneData,&iLog), iMainThread("MainThread"), iAirInterfaceThread("AirInterfaceThread"), iTEChannelThread("TeChannelThread") |
|
85 { |
|
86 // just need to set all the pointers to null |
|
87 iFilter = NULL; |
|
88 iDatalinkPacketise = NULL; |
|
89 iDatalinkNull = NULL; |
|
90 iProcessData = NULL; |
|
91 iExitFlag = 0; |
|
92 iStatus = MTS_INIT; |
|
93 |
|
94 // clear the phone state |
|
95 memset( &iPhoneData, 0, sizeof(iPhoneData) ); |
|
96 } |
|
97 |
|
98 CPhone::~CPhone() |
|
99 { |
|
100 // just need to check that all the pointers are freed |
|
101 assert( iFilter == NULL ); |
|
102 assert( iDatalinkPacketise == NULL ); |
|
103 assert( iDatalinkNull == NULL ); |
|
104 assert( iProcessData == NULL ); |
|
105 assert( iStatus != MTS_RUNNING ); |
|
106 } |
|
107 |
|
108 |
|
109 /******************************************************************************* |
|
110 * |
|
111 * PRIVATE METHOD: MAIN-THREAD: InternalInitialisePhone - setup everything - if |
|
112 * this returns an error all resource MUST be cleaned up. |
|
113 * |
|
114 ******************************************************************************/ |
|
115 MTError CPhone::InternalInitialisePhone( int aPhoneID, int aDatalinkConfig, int aFilterConfig, int *aErrCode ) |
|
116 { |
|
117 MTError merr; |
|
118 TThreadError terr; |
|
119 |
|
120 // check the params |
|
121 assert( aPhoneID >= 0 ); |
|
122 assert( aErrCode != NULL ); |
|
123 |
|
124 // init the errors |
|
125 *aErrCode = 0; |
|
126 |
|
127 // setup the phone state |
|
128 iPhoneData.iPhoneID = aPhoneID; |
|
129 |
|
130 // create the appropriate filters |
|
131 merr = CreateFilters( aFilterConfig ); |
|
132 if( merr != MTL_SUCCESS ) { |
|
133 return merr; |
|
134 } |
|
135 |
|
136 // create the data link object |
|
137 merr = CreateDatalinkLayer( aDatalinkConfig ); |
|
138 if( merr != MTL_SUCCESS ) { |
|
139 DeleteFilters(); |
|
140 return merr; |
|
141 } |
|
142 |
|
143 // give the datalink object pointers to the air interface and the uu interface |
|
144 iProcessData->SetAirInterface( &iUdpAirInterface ); |
|
145 iProcessData->SetTEChannel( &iTcpTeChannel ); |
|
146 |
|
147 // give each channel a pointer to the datalink |
|
148 iUdpAirInterface.SetDatalink( iProcessData ); |
|
149 iTcpTeChannel.SetDatalink( iProcessData ); |
|
150 iUdpAirInterface.SetFilter( iFilter ); |
|
151 iTcpTeChannel.SetFilter( iFilter ); |
|
152 |
|
153 // create a thread for the air interface to listen |
|
154 terr = iAirInterfaceThread.StartThread( (void*)AirInterfaceThreadProc, this, aErrCode ); |
|
155 if( terr != TE_NONE ) { |
|
156 CleanupState( CLEANUP_SOURCE_AIR_INTERFACE_START_FAILED ); |
|
157 return MTL_FAILED_TO_CREATE_AIR_INTERFACE_THREAD; |
|
158 } |
|
159 |
|
160 // create a thread for the te channel to listen |
|
161 terr = iTEChannelThread.StartThread( (void*)TEChannelThreadProc, this, aErrCode ); |
|
162 if( terr != TE_NONE ) { |
|
163 CleanupState( CLEANUP_SOURCE_TE_CHANNEL_START_FAILED ); |
|
164 return MTL_FAILED_TO_CREATE_TE_CHANNEL_THREAD; |
|
165 } |
|
166 |
|
167 // cool, we are setup and ready to go |
|
168 return MTL_SUCCESS; |
|
169 } |
|
170 |
|
171 |
|
172 /******************************************************************************* |
|
173 * |
|
174 * PRIVATE METHOD: MAIN-THREAD: InternalRunPhone - the main execution loop for |
|
175 * the main thread of the phone - if this returns error then it MUST be cleaned |
|
176 * up. |
|
177 * |
|
178 ******************************************************************************/ |
|
179 MTError CPhone::InternalRunPhone( int *aErrCode ) |
|
180 { |
|
181 TThreadError terr; |
|
182 |
|
183 // wait for the threads to complete or for a command to stop them |
|
184 while( 1 ) { |
|
185 |
|
186 // perform checks every X milliseconds |
|
187 Sleep( POLLINTERVAL ); |
|
188 |
|
189 // check the state of the air_interface thread |
|
190 terr = iAirInterfaceThread.WaitForThread( 0 ); |
|
191 if( terr == TE_NONE ) { |
|
192 break; |
|
193 } |
|
194 assert( terr == TE_TIMEOUT ); |
|
195 |
|
196 // check the state of the channel thread |
|
197 terr = iTEChannelThread.WaitForThread( 0 ); |
|
198 if( terr == TE_NONE ) { |
|
199 break; |
|
200 } |
|
201 assert( terr == TE_TIMEOUT ); |
|
202 |
|
203 // check whether the external program has requested that we shutdown |
|
204 if( iExitFlag != 0 ) { |
|
205 break; |
|
206 } |
|
207 } |
|
208 |
|
209 // cleanup everything |
|
210 CleanupState( CLEANUP_SOURCE_INTERNAL_RUN_PHONE ); |
|
211 |
|
212 // done |
|
213 return MTL_SUCCESS; |
|
214 } |
|
215 |
|
216 |
|
217 /******************************************************************************* |
|
218 * |
|
219 * PUBLIC METHOD: RPC-THREAD: StartPhone - this wraps up init phone and run phone |
|
220 * |
|
221 ******************************************************************************/ |
|
222 MTError CPhone::StartPhone( int aPhoneID, int aDatalinkConfig, int aFilterConfig, int *aErrCode ) |
|
223 { |
|
224 MTError merr; |
|
225 TThreadError terr; |
|
226 |
|
227 // check params |
|
228 assert( aErrCode != NULL ); |
|
229 *aErrCode = 0; |
|
230 |
|
231 // check the state |
|
232 if( iStatus != MTS_INIT ) { |
|
233 return MTL_INVALID_STATE; |
|
234 } |
|
235 |
|
236 // initialise all the sub-components |
|
237 merr = InternalInitialisePhone( aPhoneID, aDatalinkConfig, aFilterConfig, aErrCode ); |
|
238 if( merr != 0 ) { |
|
239 return merr; |
|
240 } |
|
241 |
|
242 // set the state |
|
243 iStatus = MTS_RUNNING; |
|
244 |
|
245 // start up a new thread that calls InternalRunPhone |
|
246 terr = iMainThread.StartThread( (void*)MainThreadProc, this, aErrCode ); |
|
247 if( terr != TE_NONE ) { |
|
248 CleanupState( CLEANUP_SOURCE_MAIN_START_FAILED ); |
|
249 return MTL_FAILED_TO_CREATE_MAIN_THREAD; |
|
250 } |
|
251 |
|
252 // ok everything is running -- return ok |
|
253 return MTL_SUCCESS; |
|
254 } |
|
255 |
|
256 |
|
257 /******************************************************************************* |
|
258 * |
|
259 * PUBLIC METHOD: RPC-THREAD: StopPhone - signal the main handler to stop |
|
260 * everything |
|
261 * |
|
262 ******************************************************************************/ |
|
263 MTError CPhone::StopPhone() |
|
264 { |
|
265 MTStatus mstatus; |
|
266 |
|
267 // set the exit flag |
|
268 iExitFlag = 1; |
|
269 |
|
270 // depends on our state |
|
271 switch( iStatus ) { |
|
272 case MTS_INIT: |
|
273 return MTL_SUCCESS; |
|
274 |
|
275 case MTS_RUNNING: |
|
276 case MTS_SHUTDOWN_ALL_BUT_MAIN: |
|
277 |
|
278 while( 1 ) { |
|
279 mstatus = GetStatus(); |
|
280 if( mstatus == MTS_SHUTDOWN_ALL ) { |
|
281 return MTL_SUCCESS; |
|
282 } |
|
283 Sleep( 500 ); |
|
284 } |
|
285 break; |
|
286 |
|
287 case MTS_SHUTDOWN_ALL: |
|
288 return MTL_SUCCESS; |
|
289 } |
|
290 |
|
291 // should never get here |
|
292 assert( !"INVALID CODE PATH" ); |
|
293 return MTL_SUCCESS; |
|
294 } |
|
295 |
|
296 |
|
297 /******************************************************************************* |
|
298 * |
|
299 * PUBLIC METHOD: RPC-THREAD: Sets the remote address for the uu interface |
|
300 * |
|
301 ******************************************************************************/ |
|
302 MTError CPhone::SetRemoteUUAddress( struct sockaddr_in sockaddr ) |
|
303 { |
|
304 iUdpAirInterface.SetRemoteAddress( sockaddr ); |
|
305 return MTL_SUCCESS; |
|
306 } |
|
307 |
|
308 |
|
309 /******************************************************************************* |
|
310 * |
|
311 * PUBLIC METHOD: RPC-THREAD: Gets the remote address for the uu interface |
|
312 * |
|
313 ******************************************************************************/ |
|
314 MTError CPhone::GetRemoteUUAddress( struct sockaddr_in *sockaddr ) |
|
315 { |
|
316 iUdpAirInterface.GetRemoteAddress( sockaddr ); |
|
317 return MTL_SUCCESS; |
|
318 } |
|
319 |
|
320 |
|
321 /******************************************************************************* |
|
322 * |
|
323 * PUBLIC METHOD: RPC-THREAD: Gets the local address for the uu interface. |
|
324 * |
|
325 ******************************************************************************/ |
|
326 MTError CPhone::GetLocalUUAddress( struct sockaddr_in *sockaddr ) |
|
327 { |
|
328 assert( sockaddr != NULL ); |
|
329 iUdpAirInterface.GetLocalAddress( sockaddr ); |
|
330 return MTL_SUCCESS; |
|
331 } |
|
332 |
|
333 |
|
334 /******************************************************************************* |
|
335 * |
|
336 * PUBLIC METHOD: SERVER-THREAD: Sets the socket to be used by the TE channel. |
|
337 * |
|
338 ******************************************************************************/ |
|
339 MTError CPhone::SetTeSocket( int aSock ) |
|
340 { |
|
341 TChannelError cerr; |
|
342 cerr = iTcpTeChannel.SetSocket( aSock ); |
|
343 assert( (cerr == CE_NONE) || (cerr == CE_SOCKET_ALREADY_SET) ); |
|
344 return ((cerr == CE_NONE) ? MTL_SUCCESS : MTL_TE_CHANNEL_SOCKET_ALREADY_SET); |
|
345 } |
|
346 |
|
347 |
|
348 /******************************************************************************* |
|
349 * |
|
350 * PUBLIC METHOD: RPC-THREAD: Returns a pointer to the log. |
|
351 * |
|
352 ******************************************************************************/ |
|
353 CLog *CPhone::GetLog() |
|
354 { |
|
355 return &iLog; |
|
356 } |
|
357 |
|
358 |
|
359 /******************************************************************************* |
|
360 * |
|
361 * PUBLIC METHOD: RPC-THREAD: Gets the current status of the MT. |
|
362 * |
|
363 ******************************************************************************/ |
|
364 MTStatus CPhone::GetStatus() |
|
365 { |
|
366 TThreadError terr; |
|
367 |
|
368 // if the status is MTS_SHUTDOWN_ALL_BUT_MAIN then we check to see if the |
|
369 // main thread has exited and then we update it |
|
370 if( iStatus == MTS_SHUTDOWN_ALL_BUT_MAIN ) { |
|
371 terr = iMainThread.WaitForThread( 0 ); |
|
372 if( terr == TE_NONE ) { |
|
373 iStatus = MTS_SHUTDOWN_ALL; |
|
374 } |
|
375 } |
|
376 |
|
377 // return the status |
|
378 return iStatus; |
|
379 } |
|
380 |
|
381 |
|
382 /******************************************************************************* |
|
383 * |
|
384 * Cleanupstate - cleans up everything |
|
385 * |
|
386 ******************************************************************************/ |
|
387 void CPhone::CleanupState( int aRequestSource ) |
|
388 { |
|
389 int err; |
|
390 TThreadState thread_state; |
|
391 TThreadError terr; |
|
392 |
|
393 // if the air interface thread is still listening then stop it |
|
394 thread_state = iAirInterfaceThread.GetThreadState(); |
|
395 if( thread_state == TS_ACTIVE ) { |
|
396 err = iUdpAirInterface.StopInterface(); |
|
397 assert( err == 0 ); |
|
398 terr = iAirInterfaceThread.WaitForThread( INFINITE ); |
|
399 assert( terr == TE_NONE ); |
|
400 } |
|
401 |
|
402 // if the te channel thread is still listening then stop it |
|
403 thread_state = iTEChannelThread.GetThreadState(); |
|
404 if( thread_state == TS_ACTIVE ) { |
|
405 iTcpTeChannel.StopChannel(); |
|
406 terr = iTEChannelThread.WaitForThread( INFINITE ); |
|
407 assert( terr == TE_NONE ); |
|
408 } |
|
409 |
|
410 // Remove the datalink layer |
|
411 DeleteDatalinkLayer(); |
|
412 |
|
413 // Remove the fitler |
|
414 DeleteFilters(); |
|
415 |
|
416 // update the status |
|
417 thread_state = iMainThread.GetThreadState(); |
|
418 iStatus = ((thread_state == TS_ACTIVE) ? MTS_SHUTDOWN_ALL_BUT_MAIN : MTS_SHUTDOWN_ALL); |
|
419 |
|
420 // verification |
|
421 assert( (iStatus == MTS_SHUTDOWN_ALL) || (aRequestSource == CLEANUP_SOURCE_INTERNAL_RUN_PHONE) ); |
|
422 } |
|
423 |
|
424 |
|
425 /******************************************************************************* |
|
426 * |
|
427 * SECTION: Helpers |
|
428 * |
|
429 ******************************************************************************/ |
|
430 |
|
431 /******************************************************************************* |
|
432 * |
|
433 * Create And Delete filters layers |
|
434 * |
|
435 ******************************************************************************/ |
|
436 MTError CPhone::CreateFilters( int aFilterConfig ) |
|
437 { |
|
438 // if the config isn't zero or one then we have an error |
|
439 if( (aFilterConfig != 0) && (aFilterConfig != 1) ) { |
|
440 return MTL_INVALID_FILTER_CONFIG; |
|
441 } |
|
442 |
|
443 // the only filter is the ppp logger filter |
|
444 if( aFilterConfig == FILTER_PPP ) { |
|
445 iFilter = &iFilterPpp; |
|
446 } |
|
447 |
|
448 // done |
|
449 return MTL_SUCCESS; |
|
450 } |
|
451 |
|
452 void CPhone::DeleteFilters() |
|
453 { |
|
454 // just set the pointer to NULL |
|
455 iFilter = NULL; |
|
456 } |
|
457 |
|
458 |
|
459 /******************************************************************************* |
|
460 * |
|
461 * CreateAndDelete Datalink layers - these are the methods who know the class |
|
462 * (rather than the interface) of the datalink layer. |
|
463 * |
|
464 ******************************************************************************/ |
|
465 MTError CPhone::CreateDatalinkLayer( int aDatalinkConfig ) |
|
466 { |
|
467 // create the appropriate datalink layer object and set the process data pointer |
|
468 if( aDatalinkConfig == DL_NULL ) { |
|
469 iDatalinkNull = new CDatalinkNull( &iPhoneData, &iLog ); |
|
470 assert( iDatalinkNull != NULL ); |
|
471 iProcessData = iDatalinkNull; |
|
472 } else if( aDatalinkConfig == DL_PACKETISE ) { |
|
473 iDatalinkPacketise = new CDatalinkPacketise( &iPhoneData, &iLog ); |
|
474 assert( iDatalinkPacketise != NULL ); |
|
475 iProcessData = iDatalinkPacketise; |
|
476 } else { |
|
477 return MTL_INVALID_DATALINK_LAYER; |
|
478 } |
|
479 |
|
480 // success |
|
481 return MTL_SUCCESS; |
|
482 } |
|
483 |
|
484 void CPhone::DeleteDatalinkLayer() |
|
485 { |
|
486 // delete whichever datalink layers are active |
|
487 if( iDatalinkNull != NULL ) { |
|
488 delete iDatalinkNull; |
|
489 iDatalinkNull = NULL; |
|
490 iProcessData = NULL; |
|
491 } |
|
492 |
|
493 if( iDatalinkPacketise != NULL ) { |
|
494 delete iDatalinkPacketise; |
|
495 iDatalinkPacketise = NULL; |
|
496 iProcessData = NULL; |
|
497 } |
|
498 } |
|
499 |
|
500 |
|
501 /******************************************************************************* |
|
502 * |
|
503 * SECTION: Thread entry procedures |
|
504 * |
|
505 ******************************************************************************/ |
|
506 |
|
507 /******************************************************************************* |
|
508 * |
|
509 * Thread Entry Procedures |
|
510 * |
|
511 ******************************************************************************/ |
|
512 int MainThreadProc( CPhone *aPhone ) |
|
513 { |
|
514 MTError merr; |
|
515 CLog *log; |
|
516 int errcode; |
|
517 |
|
518 // check the param |
|
519 assert( aPhone != NULL ); |
|
520 |
|
521 // log |
|
522 log = &(aPhone->iLog); |
|
523 log->WriteLogEntry( SV_INFO, "MainThreadProc", "Started" ); |
|
524 |
|
525 // now call startup |
|
526 merr = aPhone->InternalRunPhone( &errcode ); |
|
527 |
|
528 // the result is logged |
|
529 if( merr != MTL_SUCCESS ) { |
|
530 log->WriteLogEntry( SV_WARNING, "MainThreadProc", "InternalRunPhone returned error", merr, errcode ); |
|
531 } |
|
532 |
|
533 // log |
|
534 log->WriteLogEntry( SV_INFO, "MainThreadProc", "Stopped" ); |
|
535 return MTL_SUCCESS; |
|
536 } |
|
537 |
|
538 |
|
539 int AirInterfaceThreadProc( CPhone *aPhone ) |
|
540 { |
|
541 TAirInterfaceError aerr; |
|
542 int errcode; |
|
543 CUDPAirInterface *air_interface; |
|
544 CLog *log; |
|
545 |
|
546 // check the param |
|
547 assert( aPhone != NULL ); |
|
548 |
|
549 // log |
|
550 log = &(aPhone->iLog); |
|
551 log->WriteLogEntry( SV_INFO, "AirInterfaceThreadProc", "Started" ); |
|
552 |
|
553 // now recv from the air interface |
|
554 air_interface = &(aPhone->iUdpAirInterface); |
|
555 aerr = air_interface->ListenOnInterface( &errcode ); |
|
556 |
|
557 // the result is logged |
|
558 if( aerr != AIE_NONE ) { |
|
559 log->WriteLogEntry( SV_WARNING, "AirInterfaceThreadProc", "ListenOnInterface returned error", aerr, errcode ); |
|
560 } |
|
561 |
|
562 // log |
|
563 log->WriteLogEntry( SV_INFO, "AirInterfaceThreadProc", "Stopped" ); |
|
564 return MTL_SUCCESS; |
|
565 } |
|
566 |
|
567 int TEChannelThreadProc( CPhone *aPhone ) |
|
568 { |
|
569 TChannelError cerr; |
|
570 int errcode; |
|
571 CTcpTeChannel *te_channel; |
|
572 CLog *log; |
|
573 |
|
574 // check the param |
|
575 assert( aPhone != NULL ); |
|
576 |
|
577 // log |
|
578 log = &(aPhone->iLog); |
|
579 log->WriteLogEntry( SV_INFO, "TEChannelThreadProc", "Started" ); |
|
580 |
|
581 // now recv from the channel |
|
582 te_channel = &(aPhone->iTcpTeChannel); |
|
583 cerr = te_channel->ListenOnChannel( &errcode ); |
|
584 |
|
585 // the result is logged |
|
586 if( cerr != CE_NONE ) { |
|
587 log->WriteLogEntry( SV_WARNING, "TEChannelThreadProc", "ListenOnChannel returned error", cerr, errcode ); |
|
588 } |
|
589 |
|
590 // log |
|
591 log->WriteLogEntry( SV_INFO, "TEChannelThreadProc", "Stopped" ); |
|
592 return MTL_SUCCESS; |
|
593 } |