Repository navigation
Expand file tree
/
Copy pathsocket_qlinkdatanode.c
More file actions
6521 lines (5196 loc) · 178 KB
/
Copy pathsocket_qlinkdatanode.c
File metadata and controls
6521 lines (5196 loc) · 178 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <pthread.h>
#include "socket_qlinkdatanode.h"
#include "event_qlinkdatanode.h"
#include "protocol_qlinkdatanode.h"
#include "common_qlinkdatanode.h"
#include "qmi_parser_qlinkdatanode.h" // notify_Sender()
#include "qmi_sender_qlinkdatanode.h" // notify_Sender()
#include "at_rsp_qlinkdatanode.h" // process_COPS, process_CGREG_lac
#include "queue_qlinkdatanode.h" // add_node
#include "at_qlinkdatanode.h" // add_node
/******************************External Functions*******************************/
extern void notify_EvtLoop(const int msg);
extern void notify_UimSender(char opt);
extern void notify_NasSender(char opt);
extern void notify_DmsSender(char opt);
#ifdef FEATURE_ENABLE_SYSTEM_RESTORATION
extern void notify_RstMonitor(char msg);
#endif
extern void push_ChannelNum(const int chan_num);
extern void push_flow_state(const rsmp_state_s_type fs);
extern void push_thread_idx(const Thread_Idx_E_Type tid);
extern int msq_qry_pwr_inf(void);
extern void msq_send_pd_rsp(void);
extern void msq_send_dialup_ind(void);
extern void msq_send_qlinkdatanode_run_stat_ind(int evt);
extern void msq_send_mdm9215_usim_rdy_ind(int opt);
extern void msq_send_mdm9215_reg_status_ind(int opt);
extern void msq_send_mdm9215_nw_sys_mode_ind(int val);
extern int one_digit_hex_to_dec(const char digit);
extern void set_ChannelNum(const int val);
extern int check_ChannelNum(const int opt);
extern int get_ChannelNum(void);
extern int check_acq_data_state(Acq_Data_State state);
extern Acq_Data_State get_acq_data_state(void);
extern void print_fmt_data(char * str);
extern rsmp_protocol_type conv_flow_state_to_req_type(rsmp_state_s_type state);
extern int mdmEmodem_check_busy(void);
extern int mdmEmodem_check_if_running(void);
extern void mdmEmodem_shutdown(void);
extern int proto_update_flow_state(rsmp_state_s_type state);
extern void upload_status_info_and_power_up_virt_USIM_later(void);
extern void clr_pdp_check_act_routine_timer(void);
extern int check_rsp_timers(void);
extern void add_timer_mdmEmodem_idle_timeout(char opt);
extern Thread_Idx_E_Type get_thread_idx(void);
#ifdef FEATURE_ENABLE_OUT_OF_TRAFFIC_AND_DATE_0206
extern int msqSendToTrafficAndDateYydaemon(char versionf,long long fluRate);
#endif
/******************************External Variables*******************************/
extern char IP_addr[16];
extern int server_port;
extern bool g_qlinkdatanode_run_status_03;
extern rsmp_transmit_buffer_s_type g_rsmp_transmit_buffer[THD_IDX_MAX+1];
extern int ChannelNum;
#ifdef FEATURE_ENABLE_RWLOCK_CHAN_LCK
extern pthread_rwlock_t ChannelLock;
#else
extern pthread_mutex_t ChannelLock;
#endif
extern At_Config at_config;
extern bool b_enable_softwarecard_ok;
//Description:
//The vars below are all for synchronizing threads in situation that EvtLoop request sock thread
//to do some actions.
pthread_mutex_t el_req_mtx;
pthread_cond_t el_req_cnd;
bool el_reqed = false;
extern rsmp_control_block_type flow_info;
extern Dev_Info sim5360_dev_info;
extern Dev_Info rsmp_dev_info;
extern Suspend_Mdm_State suspend_state;
extern bool isQueued;
extern bool isNeedToSendIDMsg;
extern bool isWaitToSusp;
extern bool isWaitToRcvr;
extern bool isTermSuspNow;
extern bool isTermRcvrNow;
extern bool isUimPwrDwn;
extern bool RcvrMdmEmodemConnWhenSuspTerm;
extern bool PowerUpUsimAfterRecvingUrcRpt;
extern bool isPwrDwnRspAcq;
extern bool isSockListenerRecvPdpActMsg;
extern bool isAutoReprotPowerDown;
extern bool isEnableManualApdu;
extern bool isUimPwrDwnMssage;
// Explanation:
// isEnableManualApdu was set true when ApduRsp ext timer expired, and set false when virt USIM
// was powered up later. So, during this period of setting isEnableAssemblyApdu from true to false, it was
// in process of dealing with APDU rsp timeout.
// Applicable Situation:
// isApduRspTimeout is used for determining if status info upload triggered by reg inds is valid or not. If
// APDU rsp timeout, status info upload triggered by reg inds is invalid then.(Abandoned!!!)
// isApduRspTimeout is used for determining if reg inds are valid for processing.
#define isApduRspTimeout (isEnableManualApdu)
extern pthread_mutex_t evtloop_mtx;
extern bool isUimRstOperCplt;
extern pthread_mutex_t rst_uim_mtx;
extern pthread_cond_t rst_uim_cond;
extern bool isRstingUsim;
extern bool isRstRf;
extern bool isRfRsted;
extern pthread_mutex_t rst_rf_mtx;
extern pthread_cond_t rst_rf_cond;
extern int nas_inf_acqed;
extern pthread_mutex_t acq_nas_inf_mtx;
extern pthread_cond_t acq_nas_inf_cnd;
extern int pwr_inf_acqed;
extern pthread_mutex_t pwr_inf_qry_mtx;
extern pthread_cond_t pwr_inf_qry_cnd;
extern int dms_inf_acqed;
extern pthread_mutex_t acq_dms_inf_mtx;
extern pthread_cond_t acq_dms_inf_cnd;
extern int StatInfReqNum;
extern pthread_mutex_t log_file_mtx;
extern pthread_mutex_t pd_mtx;
extern unsigned long thread_id_array[THD_IDX_MAX];
extern int qlinkdatanode_run_stat;
#ifdef FEATURE_ENABLE_4G3GAUTO_FOR_US
extern bool isneedtoset4G3Gauto;
#endif
extern unsigned char g_qlinkdatanode_run_status_01;
extern unsigned char g_qlinkdatanode_run_status_02;
#ifdef FEATURE_ENABLE_EmodemUDP_LINK
extern bool g_EmodemUdp_link_server_state;
extern bool g_enable_EmodemTCP_link;
extern unsigned char g_enable_TCP_IP01_linkcount;
extern unsigned char g_enable_TCP_IP02_linkcount;
extern bool g_isip1_use;
extern bool g_isip2_use;
extern int server_port;
extern unsigned int tcp_server_port;
extern unsigned int tcp_server_port_2;
extern char tcp_IP_addr[16];
extern char tcp_IP_addr_2[16];
extern char IP_addr[16];
#endif
extern unsigned char g_is7100modemstartfail;
#ifdef FEATURE_ENABLE_CONTRL_EmodemREBOOT_0117DEBUG
extern unsigned char g_is7100modem_register_state;
#endif
#ifdef FEATURE_ENABLE_OUT_OF_TRAFFIC_AND_DATE_0206
extern trafficFromServercontrol trafficDataServer;
extern bool isBookMode;
extern bool isCancelRecoveryNet;
#endif
#ifdef FEATURE_ENABLE_STSRT_TEST_MODE
extern bool isTestMode;
#endif
/******************************Local Variables*******************************/
static int at_fd = -1;
static int listen_fd = -1;
// 1: Success
// 0: Operation failed. e.g. AT command response is ERROR.
// -1: Recv excessive amount of data.
// -2: Internal function failed. e.g. read() failed.
// -3: Unexpected msg recved.
// -4: Wrong param of function found.
// -5: Need to retry. e.g. OK of rsp not found.
// -6: In parse_2nd_param_of_URC_ext(), recv unexpected URC report about registration.
// -7: In parse_2nd_param_of_URC_ext(), recv unexpected URC report about registration rejection.
static int err_num_s = 0xff;
static pthread_t listenerloop_tid;
static pthread_mutex_t listener_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t listener_cond = PTHREAD_COND_INITIALIZER;
static int listenerStarted;
//Function:
//Make sure no conflict of different AT rsp in their hdlr.
static pthread_mutex_t at_mutex = PTHREAD_MUTEX_INITIALIZER;
static char unexp_urc_buf[128] = {0};
//Description:
// When app send AT cmd to mdm9215 but recv unexpected URC report, app need wait to
// recv AT cmd rsp and ignore URC report for time being. At this moment, it was set true, and
// it would be set false when AT cmd rsp is recved.
static bool isWaitForRsp = false; // Add by rjf at 20150713
//Description:
// Once reg was denied for rej cause #15, it would be set true, and app would need to check rej cause once
// recved ind "CXREG: 0,2".
//static bool isRejCause15Recved = false;
//Description:
// Once URC rpt "+REGREJ:..." recved (or rej cause checked by AT cmd rsp), it would be set true. And
// no need to check AT cmd rsp when URC rpt "+CXREG: 3" (or handle the URC rpt "+REGREJ:...").
// It would be set false when virt usim was set LPM for recving unrecoverable reg rej cause.
static bool isUnrcvRejCauseRecved = false;
//Description:
// Once reg ind recved by socket thread, app will save its reg stat val into new_reg_stat.
// And new_reg_stat will replace last_reg_stat with itself before updating new_reg_stat.
// The app will check if new_reg_stat is different to last_reg_stat. If yes, app may
// see it as reg off.
//Reason for adding this:
// Handling the special situation in which signal is poor but "+CXREG: 0" not reported. In this situation,
// you are just acquire reg status with "+CXREG: 2".
// Add by rjf at 201507271601
static int new_reg_stat = -1;
static int last_reg_stat = -1;
static int URC_array_len = 0;
static char URC_array[MAX_URC_ARRAY_LEN+1][MAX_URC_LEN];
//Description:
//For the limitation of SOCK_READ_BYTES, app may read less than what buffer has. Then this URC fragment
//will be stored into URC_frag.
static char urc_fragment[MAX_URC_LEN] = {0};
//Description:
//For the limitation of size of URC_array, app may miss some URCs in read buffer. So this pointer will point to
//the unread URC in the read buffer.
static char *URC_buf_break_point = NULL;
static int valid_URC_prefix_array_len = 0;
static char valid_URC_prefix_array[MAX_URC_ARRAY_LEN+1][MAX_URC_LEN];
//Description:
//In check_valid_URC_array_elem(), the qualified elements of URC array will make the elements of URC_valid_mark_array
//with the corresponding indexes be set 1.
static char URC_valid_mark_array[MAX_URC_ARRAY_LEN] = {0};
//Description:
//It's the array which contains the special URC string you want to monitor.
//Elements:
// Idx 1 : "+SIMCARD: NOT AVAILABLE"
static char special_URC_array[MAX_SPEC_URC_ARR_LEN][MAX_URC_LEN];
static int monitored_special_URC_num = 0;
//Description:
// It is the array of marks which indicates that URCs of corresponding index in special_URC_array is recved.
// The elem in this arr will be reset or initialized depended on different situation.
// Elements:
// Idx 1 : 1 ----- When "+SIMCARD: NOT AVAILABLE" is recved.
// 0 ----- When virtual card is powered up again.
char URC_special_mark_array[MAX_SPEC_URC_ARR_LEN] = {0};
static Reg_URC_State_E_Type reg_urc_state = REG_URC_STATE_IDLE;
static Reg_URC_State_E_Type last_reg_urc_state = REG_URC_STATE_IDLE;
//Description:
//Once reg is rejected for cause #15 and URC "+CXREG: 2" is recved, this will be set true. After checking the rejection cause
//after recving "+CXREG: 2", this bool var will be set false.
//static bool need_to_check_rej_cause = false;
static Reg_Sys_Mode_State_E_Type reg_sys_mode_state = REG_SYS_MODE_MIN;
static Reg_Sys_Mode_State_E_Type last_reg_sys_mode_state = REG_SYS_MODE_MIN;
static bool need_to_upload_stat_inf = false;
//Description:
//When USIM is set powerdown manually, app need to wait for recving "+SIMCARD: NOT AVAILABLE". This var is set for remind app
//of exiting from listener_hdlr() when "+SIMCARD: NOT AVAILABLE" has been recved already.
static bool isSimCardNotAvailRecved = false;
static bool isSessIllegalRecved = false;
/******************************Global Variables*******************************/
strflu_record strGobalstandTime;
bool isRegOn = false;
bool isMifiConnected = false;
bool is1stRunOnMdmEmodem = true;
pthread_mutex_t i1r_mtx;
bool isMdmEmodemRunning = false;
//xiaobin add
bool isUMTSOnlySet = false;
static unsigned int rab_restab_rej_or_fail_counter = 0;
Net_Info rsmp_net_info = {
.basic_val = 0x00,
.cpin_val = 0x02,
.cnsmod_val = 0x00,
.cgreg_val = 0x00,
.cereg_val = 0x00,
.cregrej_val = 0x00,
.cnmp_val = 0x00
};
int SockListener_2_Listener_pipe[2];
int EvtLoop_2_Listener_pipe[2];
//Description:
// This bool var is for indicating if mdm9215 is registered.
// If mdm9215 was deregistered of poor signal, it would be set true. And app would set uim driver LPM and powerdown then.
// When mdmEmodem is reconnecting to the server, (it basically indicate that reg network is available) uim driver will be set Online
// Mode and powerup, then this var will be set false.
// The 3 scenes as below:
// 1. Timeout when send APDU to server.
// 2. Recv "+CIPEVENT: NETWORK CLOSED UNEXPECTEDLY" of mdmEmodem URC rpt.
// 3. Recv "+CXREG: 0" of mdm9215 URC rpt.
// As if these 3 scenes show up one by one, it will trigger the processing handler of latest scene no matter if last executed
// processing handler is still running. It means if that isRegOffForPoorSig = true will not block the execution of new processing
// handler of these 3 scenes.
bool isRegOffForPoorSig = false;
bool isMdm9215CpinReady = false;
//Description:
// StatInfMsgSn:
// This var is marked as the serial num of latest status info msg.
// stat_inf_mtx:
// This mutex is for avoiding that status_info_upload_acq_info() is called by 2 threads at same time.
// StatInfMsgSn may not be accurate.
unsigned int StatInfMsgSn = 0;
pthread_mutex_t stat_inf_mtx = PTHREAD_MUTEX_INITIALIZER;
/******************************Reference Declaration*******************************/
void closeSock(void);
static void FdListener(const int fd, evt_cb cb_func, void *userdata);
void createSock(void);
static int trigger_rsmp_status_report(void);
void get_dev_and_net_info_for_status_report_session(void);
int mdm9215_check_CNSMOD(void);
int mdm9215_check_CSQ(void);
//int sock_check_reg_rej_cause(const int fd);
#ifdef FEATURE_ENABLE_STATUS_REPORT_10_MIN
rsmp_transmit_buffer_s_type req_config_get = {-1, NULL, true};
static pthread_t status_parser_tid;
static pthread_mutex_t status_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t status_cond = PTHREAD_COND_INITIALIZER;
static int statusStarted;
int Status_rw_pipe[2];
Evt statusReport_timeout_evt;
#endif
/******************************Macro And Types*******************************/
#define LISTENER_CHANNEL "/dev/smd11"
#define AT_CHANNEL "/dev/smd8"
#define PRIV_PROT_APDU_MSG_HEAD_IDX_S (0)
#define PRIV_PROT_APDU_MSG_LEN_IDX_S (1)
#define PRIV_PROT_APDU_REQ_TYPE_IDX_S (5)
#define PRIV_PROT_APDU_DATA_TYPE_1_IDX_S (6)
#define PRIV_PROT_APDU_UEID_IDX_S (7)
#define PRIV_PROT_APDU_DATA_TYPE_2_IDX_S (11)
#define PRIV_PROT_APDU_DATA_LEN_IDX_S (12)
#define PRIV_PROT_APDU_DATA_IDX_S (14)
typedef void (*evt_cb)(void *userdata);
typedef struct{
int fd;
char *pre;
int ret;
}Stat_Check_Hdlr_Param_S_Type;
typedef struct {
int fd;
int rej_cause;
}Reg_Rej_Hdlr_Param_S_Type;
//parameters:
// i: The integer value of port number.
// p: The pointer which pointed to storage space of result.
#define ITOA(i, p) \
{ \
int n = i; \
int r; \
int cnt; \
int div = 10000; \
for( cnt=0; cnt<5; cnt++){ \
p[cnt] = '0'; \
} \
p[cnt] = '\0'; \
for( cnt=0; cnt<5; cnt++){ \
if((r = (n)/div) != 0){ \
p[cnt] = r+'0'; \
} \
n -= r*div; \
div /= 10; \
} \
cnt = 0; \
while(p[cnt] == '0'){ \
memmove(p, p+1, 5-(cnt+1)); \
p[5-(cnt+1)] = '\0'; \
} \
}
//function:
// No param examination in this macro.
//Parameters:
// us: The type is unsigned int. It means us microseconds.
// tv: The type is struct timeval. us microseconds will be converted to struct timeval.
#define US_TO_TIMEVAL(us,tv) \
{ \
(tv).tv_usec = (us)%S_TO_US; \
(tv).tv_sec = (us)/S_TO_US; \
}
#define SOCK_READ_OK(fd) \
{ \
char rsp_buf[SOCK_READ_BYTES] = {0}; \
int ret; \
FdListener((fd), NULL, NULL); \
if(err_num_s != 1) \
return -3; \
do{ \
ret = read((fd), rsp_buf, SOCK_READ_BYTES); \
}while((ret < 0 && errno == EINTR) || ret == 0); \
if(ret < 0){ \
LOG("read() failed in reception of rsp \"OK\".\n"); \
return -4; \
} \
}
//Return Values:
// 1: Success
// -2: Writen bytes is inadequate or write() failed.
// -3: FdListener() failed.
// -4: Fail to recv rsp of "OK".
static int send_at_and_check_response(const int fd, const char * at)
{
int ret;
ret = write(fd, at, strlen(at));
if(ret != strlen(at)){
if(ret < 0){
LOG("write() error. errno=%s(%d).\n",
strerror(errno), errno);
}else{
LOG("write() error. ret=%d.\n", ret);
}
return -2;
}else{
SOCK_READ_OK(fd);
}
return 1;
}
/*void yy_popen_call(const char *command,char * data)
{
int result = 0;
FILE *stream = NULL;
LOG("system call: %s \n", command);
stream = popen( command, "r" );
if (stream != NULL)
{
if (fread(data, 1, 511, stream) != NULL)
{
LOG("command succeed\r\n");
LOG("!!!!!!!!!!%s!!!!!!!!!!\n",data);
}
if( 0 > pclose( stream ) )
{
LOG("pclose command failed \n");
}
}
else
{
LOG("system command failed error:%s \n",strerror(errno));
}
}*/
int yy_popen_call( char *command)
{
int result = 0;
FILE *stream = NULL;
char data[512];
LOG("system call: %s \n", command);
memset(data, 0 , sizeof(data));
stream = popen( command, "r" );
if (stream != NULL)
{
if (fread(data, 1, 511, stream) != NULL)
{
LOG("command succeed\r\n");
LOG("!!!!!!!!!!%s!!!!!!!!!!\n",data);
}
if( 0 > pclose( stream ) )
{
LOG("pclose command failed \n");
result = -1;
}
}
else
{
if( 0 > pclose( stream ) )
{
LOG("pclose command failed \n");
}
LOG("system command failed error:%s \n",strerror(errno));
result = -1;
}
return result;
}
//Parameters:
// option=1: Check for err_num_s after calling FdListener().
//Return Values:
// 2: Reg rej report recved.
// 1: Success
// 0: Retry again. Call sock_send_at_rd_cmd() again.
// -2: Writen bytes is inadequate or write() failed.
// -3: FdListener() failed.
int sock_send_at_rd_cmd(const int fd, const char *at, evt_cb hdlr, void *param, int option)
{
int ret;
LOG(" --> %s\n", at);
ret = write(fd, at, strlen(at));
if(ret != strlen(at)){
if(ret < 0){
LOG("ERROR: write() error. errno=%s(%d).\n",
strerror(errno), errno);
}else{
LOG("ERROR: write() error. ret=%d.\n", ret);
}
return -2;
}else{
__SOCK_SEND_AT_RD_CMD_CALL_FDLISTENER__:
FdListener(fd, hdlr, (param));
if((option) == 1){
if(err_num_s != 1 && err_num_s != -5 && err_num_s != -6 && err_num_s != -7){
return -3;
}else if(err_num_s == -5){
LOG("err_num_s=%d. Call FdListener() again...\n", err_num_s);
goto __SOCK_SEND_AT_RD_CMD_CALL_FDLISTENER__;
}else if(err_num_s == -6){
return 0;
}else if(err_num_s == -7){
return 2;
}
} // if((option) == 1)
}
return 1;
}
/*************************Basic Functions**************************/
void sock_update_reg_urc_state(Reg_URC_State_E_Type state)
{
last_reg_urc_state = reg_urc_state;
reg_urc_state = state;
LOG("last_reg_urc_state=%d, reg_urc_state=%d.\n",
last_reg_urc_state, reg_urc_state);
return;
}
//Param:
// tag = 0: +CPIN rsp changed.
// opt1 = 0x01 -> +CPIN: READY
// opt2 = 0x02 -> otherwise
// tag = 1: Registration state changed.
// opt1: cnsmod
// opt2: cgreg
// opt3: cereg
// tag = 2: Dial-up state changed.
// opt1: dial-up state. 0x00 means failed, and 0x01 success.
//Modication Records:
// 2015-9-10: Remove the update of mdmImodem_ni.cond when tag is 1.
void update_net_info(int tag, int opt1, int opt2, int opt3)
{
LOG("Debug: param input. tag=%d, opt1=%d,opt2=%d, opt3=%d .\n",tag, opt1,opt2,opt3);
switch(tag)
{
case 0:
{
if(opt1 != 1 && opt1 != 2){
LOG("ERROR: Wrong param input. tag=%d, opt1=%d.\n",tag, opt1);
return;
}
rsmp_net_info.cpin_val = opt1;
if(opt1 == 2){
rsmp_net_info.basic_val = 0x00;
}
break;
} // case 0
case 1:
{
rsmp_net_info.cnsmod_val = opt1;
if(-1 != opt2)
rsmp_net_info.cgreg_val = opt2;
if(-1 != opt3)
rsmp_net_info.cereg_val = opt3;
//If no status info is about to be uploaded, rsmp_net_info.basic_val may be wrong. (Be 0x01 but should be 0x02.)
if((rsmp_net_info.cnsmod_val == 8 && (opt3 == 1 ||opt3 == 5))
|| (rsmp_net_info.cnsmod_val != 8 && (opt2 == 1 ||opt2 == 5)) )
{
if(rsmp_net_info.basic_val == 0x01)
rsmp_net_info.basic_val = 0x02;
}
break;
}// case 1
case 2:
{
//Note:
//Failed dial-up doesn't need update net_info structure.
if(opt1 != 0x01){
LOG("ERROR: Wrong param input. tag=%d, opt1=%d.\n", tag, opt1);
return;
}
rsmp_net_info.basic_val = 0x04;
break;
} // case 2
default:
{
LOG("ERROR: Wrong param input. tag=%d.\n", tag);
break;
} // default
}
}
//Note:
// If macro FEATURE_ENABLE_STATUS_INFO_UPLOAD_BY_MDMEmodem_ONLY is not defined, please
// acquire ChannelLock when you call this function!
static void sock_net_info_processor(char pre_cond, Net_Info cur_ni)
{
char cur_cond = cur_ni.basic_val;
if(pre_cond < 0x00 || pre_cond > 0x05){
LOG("ERROR: Wrong param input. pre_cond=%d.\n", pre_cond);
return;
}
if(pre_cond != cur_cond)
{
#ifdef FEATURE_ENHANCED_STATUS_INFO_UPLOAD
StatInfReqNum++;
#endif
get_dev_and_net_info_for_status_report_session();
trigger_rsmp_status_report();
}
}
void net_info_processor(char pre_cond, Net_Info cur_ni)
{
sock_net_info_processor(pre_cond, cur_ni);
}
//Param:
// option=0: read rx bytes.
// option=1: read tx bytes.
//Return Values:
// >0: The num of rx/tx bytes.
// -1: Wrong param input.
// -2: open() failed.
// -3: read() failed.
static long long int read_net_traffic_info(int option)
{
char buf[80] = {0}, *file_path = NULL;
int len, fd;
switch(option){
case 0: file_path = NET_RX_FILE; break;
case 1: file_path = NET_TX_FILE; break;
default:{
LOG("ERROR: Wrong param input. option=%d.\n", option);
return -1;
}
}
fd = open(file_path, O_RDONLY);
if (fd < 0) {
LOG("ERROR: Fail to open %s. errno=%s(%d).\n", file_path, strerror(errno), errno);
return -2;
}
len = read(fd, buf, sizeof(buf)-1);
if (len < 0) {
LOG("ERROR: read() failed. errno=%s(%d).\n", strerror(errno), errno);
close(fd);
return -3;
}
close(fd);
buf[len] = '\0';
return atoll(buf);
}
//Function:
//Monitor a fd if it is ready to read.(extensable)
static void FdListener(const int fd, evt_cb cb_func, void *userdata)
{
int ret = -1;
fd_set rfd;
if(fd < 0){
LOG("ERROR: Wong param input. fd=%d.\n", fd);
err_num_s = -4;
return;
}
FD_ZERO(&rfd);
FD_SET(fd, &rfd);
__SELECT_AT_FD_AGAIN__:
ret = select(fd+1, &rfd, NULL, NULL, NULL);
if(ret > 0){
if(cb_func != NULL)
cb_func(userdata);
}else{
LOG("ERROR: select() error. ret=%d.\n", ret);
goto __SELECT_AT_FD_AGAIN__;
}
if(cb_func == NULL)
err_num_s = 1;
return;
}
//Function:
// open() a path and acquire a fd. Then send "ATE0" to this fd.
//Return Value:
// 1: Success
// -1: open() failed.
// -2: Sending AT cmd failed.
// -3: FdListener() failed.
// -4: read() failed.
// -5: Wrong param input. path=NULL.
static int initATChannel(int *p_fd, const char *path)
{
if(path == NULL){
LOG("Wrong param input. path=null.\n");
return -5;
}
*p_fd = open(path, O_RDWR | O_NONBLOCK | O_NOCTTY);
if(*p_fd < 0){
LOG("ERROR: open() failed. errno=%d.\n", errno);
return -1;
}
pthread_mutex_lock(&at_mutex);
send_at_and_check_response(*p_fd, "ATE0\r\n");
pthread_mutex_unlock(&at_mutex);
return 1;
}
static void init_URC_array(void)
{
memset(URC_array, 0, sizeof(URC_array));
URC_array_len = 0;
}
static void init_valid_URC_prefix_array(void)
{
memset(valid_URC_prefix_array, 0, sizeof(valid_URC_prefix_array));
valid_URC_prefix_array_len = 0;
}
//Description:
//Parse out raw data in read buffer, and put them into URC_array by one element for
//one URC report.
//Return Values:
// 1 : Process succeeded.
// 0 : Process terminated for running out of URC_array space.
// -1 : Wrong parameter input.
// -2 : Wrong URC report recved. E.x. URC report is too big!
static int parse_raw_URC_data(char *buf, int read_bytes)
{
char *cur = NULL, buf_idx = 0, orig_URC_array_len = URC_array_len;
if(buf == NULL){
LOG("ERROR: Wrong param input. buf=null.\n");
return -1;
}
if(read_bytes == 0){
return 1;
}
for(cur=buf;;)
{
char *URC_head = NULL, *URC_tail = NULL;
if(*urc_fragment != 0x00)
{
LOG("There is something unread in urc_fragment.\n");
if(MAX_URC_ARRAY_LEN < URC_array_len+1){
LOG("Unable to save more URCs into URC array now! %d URCs has been saved already.\n",
URC_array_len-orig_URC_array_len);
URC_buf_break_point = URC_head;
return 0;
}
if(*cur != '\r' && *cur != '\n' && *cur != '\0'){
int already_saved_len = 0;
URC_head = cur;
for(; (*cur!='\r' && *cur!='\n' && *cur!='\0') && buf_idx<read_bytes; cur++, buf_idx++);
if(read_bytes == buf_idx){
LOG("ERROR: Wrong URC report recved. URC report is too big!\n");
return -2;
}
URC_tail = cur;
*URC_tail = '\0';
URC_array_len ++;
memset(URC_array[URC_array_len-1], 0, sizeof(URC_array[0]));
memcpy(URC_array[URC_array_len-1], urc_fragment, sizeof(urc_fragment));
already_saved_len = strlen(URC_array[URC_array_len-1]);
memcpy(URC_array[URC_array_len-1]+already_saved_len,
URC_head,
(URC_tail-URC_head+already_saved_len>MAX_URC_LEN)?(MAX_URC_LEN-already_saved_len-1):(URC_tail-URC_head)
);
memset(urc_fragment, 0, sizeof(urc_fragment)); // Initialize urc_fragment
}else{
//Situation:
//Last URC fragment is breaked between last valid char and line break chars.
URC_array_len ++;
memset(URC_array[URC_array_len-1], 0, sizeof(URC_array[0]));
memcpy(URC_array[URC_array_len-1], urc_fragment, sizeof(urc_fragment));
memset(urc_fragment, 0, sizeof(urc_fragment)); // Initialize urc_fragment
}
}
for(; (*cur=='\r' || *cur=='\n' || *cur=='\0') && buf_idx<read_bytes; cur++, buf_idx++);
if(read_bytes == buf_idx)
{
memset(urc_fragment, 0, sizeof(urc_fragment));
break;
}
URC_head = cur;
for(; (*cur!='\r' && *cur!='\n' && *cur!='\0') && buf_idx<read_bytes; cur++, buf_idx++);
if(read_bytes == buf_idx)
{
memset(urc_fragment, 0, sizeof(urc_fragment));
memcpy(urc_fragment, URC_head, cur-URC_head);
LOG("NOTICE: Recv URC frag. Content: \"%s\".\n", urc_fragment);
break;
}else
{
URC_tail = cur;
*URC_tail = '\0';
if(MAX_URC_ARRAY_LEN < URC_array_len+1)
{
LOG("Unable to save more URCs into URC array now! %d URCs has been saved already.\n",
URC_array_len-orig_URC_array_len);
URC_buf_break_point = URC_head;
return 0;
}
URC_array_len ++;
memset(URC_array[URC_array_len-1], 0, sizeof(URC_array[0]));
memcpy(URC_array[URC_array_len-1], URC_head, URC_tail-URC_head);
continue;
}
}
return 1;
}
//Description:
// Set the prefix of valid URC into valid_URC_prefix_array.
//Parameter:
// pre:
// It must be C-style string.(Ended with a null char)
//Return Values:
// 1 : Succeed.
// -1 : Wrong param input.
// -2 : Valid URC prefix array is totally occupied.
// -3 : valid_URC_prefix_array_len found.
static int set_valid_prefix_array(const char *pre)
{
if(pre == NULL){
LOG("ERROR: Wrong param input. pre=null.\n");
return -1;
}
if(*pre == '\0'){
LOG("ERROR: Wrong param input. *pre=0x00.\n");
return -1;
}
if(valid_URC_prefix_array_len == MAX_URC_ARRAY_LEN){
LOG("ERROR: No more space for storing new valid URC prefix.\n");
return -2;
}else if(valid_URC_prefix_array_len > MAX_URC_ARRAY_LEN || valid_URC_prefix_array_len < 0){
LOG("ERROR: Wrong valid_URC_prefix_array_len(%d) found!\n",
valid_URC_prefix_array_len);
return -3;
}
valid_URC_prefix_array_len += 1;
memset(valid_URC_prefix_array[valid_URC_prefix_array_len-1], 0, sizeof(valid_URC_prefix_array[valid_URC_prefix_array_len]));
memcpy(valid_URC_prefix_array[valid_URC_prefix_array_len-1], pre, strlen(pre));
return 1;
}
//Description:
// Check URC_array refer to valid_URC_prefix_array if the element of URC_array is valid. If yes, the corresponding element of
// URC_valid_mark_array will be set 0x01. Otherwise, the corresponding element of URC_valid_mark_array will be maintained 0x00.
// Check URC_array to look for the monitored URC specified in special_URC_array.
//Return Value:
// >=0 : The number of valid URCs in URC array.
// -1 : Wrong param input.
static int check_valid_URC_array_elem(char (*valid_URC_pre_arr)[MAX_URC_LEN], const int valid_URC_pre_arr_len)
{
int idx = 0, valid_URC_num = 0;
if(valid_URC_pre_arr == NULL){
LOG("ERROR: Wrong param input. valid_URC_prefix_array=null.\n");
return -1;
}
if(valid_URC_pre_arr_len == 0){
LOG("ERROR: Wrong param input. valid_URC_num=0.\n");
return -1;
}
for(; valid_URC_pre_arr[idx]!=NULL && valid_URC_pre_arr[idx][0]!='\0' && idx<valid_URC_pre_arr_len; idx++);
if(idx != valid_URC_pre_arr_len){
LOG("ERROR: Wrong param input. Empty element (idx:%d) of valid_URC_prefix_array found! valid_URC_num=%d.\n",
idx, valid_URC_pre_arr_len);
return -1;
}
memset(URC_valid_mark_array, INVALID_URC, sizeof(URC_valid_mark_array));
for( idx=0; idx<URC_array_len; idx++)
{
int valid_arr_idx = 0, spec_arr_idx = 0;
for(; valid_arr_idx<valid_URC_pre_arr_len; valid_arr_idx++){
int cmp_result = -1;
cmp_result = strncmp(valid_URC_pre_arr[valid_arr_idx], URC_array[idx], strlen(valid_URC_pre_arr[valid_arr_idx]));
if(0 == cmp_result){
URC_valid_mark_array[idx] = VALID_URC;
valid_URC_num ++;