-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnetcomm.c
More file actions
3175 lines (2639 loc) · 71 KB
/
netcomm.c
File metadata and controls
3175 lines (2639 loc) · 71 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
// file description
#include <pthread.h>
#include <time.h>
#include "netcomm.h"
#include "netcommon.h"
#include "loopbuf.h"
#include "CmdProc.h"
#include "remotePreview.h"
#include "remotePlay.h"
#include "remoteDown.h"
#include "remoteUpdate.h"
#include "mydaemon.h"
#include "mail.h"
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include "ddns.h"
//** macro
#define ETH_NAME "eth0"
//** typedef
typedef void (*PTTHREAD_ROUTINE)(void* para);
typedef enum
{
FRAME_TYPE_P = 1, /*PSLICE types*/
FRAME_TYPE_I = 5 /*ISLICE types*/
}venc_frame_type_e;
/*
TCP stream request struct
*/
typedef struct _sNetCommTcpStreamReq
{
u8 chn; //command(0-1)有效
u8 command; //0 预览视频; 1 预览音频;2 文件回放;3 时间回放;4 文件下载;5 主板升级;6 面板升级
u32 id; //对应command: cmd(0-1), id=0; cmd(2-4) id = 发送命令返回的id; cmd(5-6), id = 升级文件大小
} SNCTSReq, *PSNCTSReq;
// cmd data struct
typedef struct _sNCCmdData
{
SOCKHANDLE sock; // new sock for new connect req
ifly_TCP_Stream_Req req; // req struct from ctrlprotocol
void* pContext; // context for current command from ctrlprotocol
} SNCCmdData, *PSNCCmdData;
//** pppoe
#define PPPOE_ETH_NAME "ppp0"
typedef enum
{
EM_PPPOE_Down,
EM_PPPOE_PreUp,
EM_PPPOE_Up,
EM_PPPOE_MAX
} EM_PPPOE_STATUS;
typedef struct _sPppoeCfg
{
char szEthName[10];
u8 nFlag; // enable
EM_PPPOE_STATUS eStatus;
u8 nDisconnNum;
u32 nLastDiscTime;
char szUser[64];
char szPasswd[64];
char szIP[24];
char szSubMask[24];
char szGateway[24];
char szDNS[24];
} SPppoeCfg;
// ddns ctrl
typedef struct _sDdnsCfg
{
EM_NET_DDNS eProt;
u8 nDdnsFlag; // 0 down 1 up
u8 bStart; // 0 no, 1 yes
u8 bInit; // 0 no, 1 yes
SDDNSBaseInfo sInfo;
} SDdnsCfg;
//** local var
static SPppoeCfg sPPPoECfg;
static SDdnsCfg sDdnsCfg;
static SNetConnStatus sNetConnStatus;
//** global var
SNetCommCtrl sNetCommCtrl;
#if 1 //luo
#define AUTYPENUM 13
//#define MAX_ALARM_UPLOAD_NUM 5
static int upload_alarm_fd[2] = {-1, -1};
static pthread_mutex_t upload_alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
//u32 alarmupload[AUTYPENUM];
/*
0 信号量报警
1 硬盘满报警
2 视频丢失报警
3 移动侦测报警
4 硬盘未格式化报警
5 读写硬盘出错报警
6 IPC遮盖报警
7 制式不匹配报警
8 非法访问报警
9 IPC外部报警
10 485扩展报警
*/
//yaogang modify 20141118
/*
0 //硬盘未格式化报警
1 //硬盘丢失报警
2 //硬盘读写出错报警
3 //硬盘满报警
4 //开机检测无硬盘
5 //信号量报警
6 //视频丢失报警
7 //移动侦测报警
8 //IPC遮盖报警
9 //制式不匹配报警
10 //非法访问报警
11 //IPC外部报警
12 //485扩展报警
*/
typedef struct
{
u8 type; //如上
//9-IPCEXT
u8 state; // 1报警 2恢复
u8 id; //通道,硬盘号,报警输入号,取决于type
u16 reserved1; //预留
u32 reserved2; //预留
}ifly_alarmstate_t;
ifly_alarmstate_t m_alarmstate;
ifly_AlarmUploadCenter_t g_AlarmUploadCenter[MAX_ALARM_UPLOAD_NUM];
#endif
//** local functions
static void netcomm_ConfigNetwork(SNetPara* pConfig);
static void netComm_DealStreamCmd(SOCKHANDLE sock, ifly_TCP_Stream_Req* pReq, u32 ip);
static s32 netComm_PPPOE_Start( u8 flag, char *pszUser, char* pszPasswd );
static s32 netComm_NotifyPppoeState( u8 bState );
static BOOL netComm_SetPPPOEUser(char *username,char *passwd);
static s32 netComm_DHCP_Stop();
static s32 netComm_DHCP_Start();
//static BOOL netComm_ReadNetWorkParam(SNetCommCfg* psNCCfg);
static int AddStreamTCPLink(SOCKHANDLE hSock, ifly_TCP_Stream_Req req, void* pContext);
//static void* netCommCmd_ThreadFunc(void* param);
static s32 netComm_Webs_Start( u16 nPort );
//static s32 netComm_Webs_Restart( u16 nPort );
static int netComm_ReplaceWebRow(const char *fname, const char *src, char *dest);
static void netComm_SaveServerPort(u16 server_port);
//static BOOL netComm_CheckNetLink();
//static void *netComm_CheckState(void *param);
static void* netComm_DDNS_Init(void* para);
static void* netComm_AlarmUpload_init(void* para);//luo
static s32 netComm_Webs_ChangeWebJs( u16 nPort );
static int SendMsgToDaemon(my_daemon_param *pDaemon);
s32 NetComm_ReqConnStatus(SNetConnStatus* psNCS);
//** global functions
BOOL CheckNetLink2(char* pIFName);
BOOL CheckNetLink(void);
u32 GetLocalIp();
u32 GetLocalIp2(char * name);
int SetLocalIp(u32 dwIp);
int GetHWAddr(char *pBuf);
int SetHWAddr(char *pBuf);
u32 GetRemoteIP();
u32 GetNetMask();
u32 GetNetMask2(char * name);
int SetNetMask(u32 dwIp);
u32 GetBroadcast();
int SetBroadcast(u32 dwIp);
u32 GetDefaultGateway();
int SetDefaultGateway(u32 dwIp);
u32 GetDNSServer();
int GetDNSServer2(u32 *pDns, u32 *pDns1);
int SetDNSServer(u32 dwIp);
int AddDNSServer(u32 dwIp);
u16 GetNewIdNum();
extern void netcomm_assert( char * file_name, u32 line_no );
extern void cmdPro_Init( PNetCommCommandDeal2 p, u8 nChMax );
void* rtsp_init(void* arg); // rtsp support
/*
void term_exit(int signo)
{
time_t cur;
cur = time(NULL);
//printf("term_exit:system time:%s\n", ctime(&cur));
printf("!!!!!!recv signal(%d),SIGBUS=%d,SIGPIPE=%d,,%s\n",signo,SIGBUS,SIGPIPE,ctime(&cur));
if(signo != 17)//子进程结束
{
//sleep(10);
printf("process quit!!!\n");
exit(-1);
}
}
*/
// init ddns para
typedef struct _DDNSInitPara {
u8 flag;
u16 interval;
SNetDdnsPara sStart;
} DDNSInitPara;
static DDNSInitPara initPara;
/*
初始化netcomm module
*/
disk_manager* pHddMgr = NULL;
void* NetCommInitThxd( void* para )
{
printf("$$$$$$$$$$$$$$$$$$NetCommInitThxd id:%d\n",getpid());
//printf("NetCommInitThxd-1\n");
sleep(5);//why???
//printf("NetCommInitThxd-2\n");
//u8 i;
u32 i;
u8 nSendBufNum, nChnNum;
u16 nServerPort = 0;
u32 nFrameSizeMax = 0;
s32 errCode = 0;
PSNetCommCtrl pNCCIns = NULL;
SMsgCmdHdr pCmd = NULL;
pthread_t cmdThxd = 0;
PSNetCommCfg pCfg = (PSNetCommCfg)para;
PStreamRequestCB pCB = pCfg->pReqStreamCB;
PRequestKeyFrameCB pReqKeyCB = pCfg->pReqKeyCB;
PRemotePlayCB pRpCB = pCfg->pRpCB;
PNetCommVOIPOut pVOIP = pCfg->pVOIPOut;
PNetCommVOIPIn pVOIPIn = pCfg->pVOIPIn;
NETCOMM_DEBUG_STR("netinit", 0);
NETCOMM_ASSERT(pCB==NULL || NULL!=pCfg);
//
pNCCIns = &sNetCommCtrl;
//printf("NetCommInitThxd - 1, CheckNetLink:%d\n",CheckNetLink());
// save serverport to file
netComm_SaveServerPort(pCfg->sParaIns.TCPPort);
// write config
memcpy(&pNCCIns->sCommonCfg, pCfg, sizeof(SNetCommCfg));
//printf("NetCommInitThxd - 1.1, CheckNetLink:%d\n",CheckNetLink());
// set network para at the system beginning..
netcomm_ConfigNetwork(&pCfg->sParaIns);
//printf("NetCommInitThxd - 1.2, CheckNetLink:%d\n",CheckNetLink());
// create a/v stream tx-ctrl
u16 nAudStreamNum = pCfg->nSubStreamMax;
u16 nVidStreamNum = pCfg->sParaIns.TCPMaxConn;
u16 nTotalLinkNum = pCfg->nAllLinkMax;
printf("NetCommInitThxd:nAudStreamNum=%d,nVidStreamNum=%d,nTotalLinkNum=%d,nChnMax=%d\n",nAudStreamNum,nVidStreamNum,nTotalLinkNum,pCfg->nChnMax);
//sNetCommCtrl.sCommonCfg.pDiskMgr = pCfg->pDiskMgr;
pHddMgr = pCfg->pDiskMgr;
pNCCIns->nAudStreamNum = nAudStreamNum;
pNCCIns->nChnTcpStreamNum = pNCCIns->nAudStreamNum;
pNCCIns->nSubStreamNum = pNCCIns->nAudStreamNum;
pNCCIns->nVidStreamNum = nVidStreamNum;
pNCCIns->nTotalLinkNum = nTotalLinkNum;
// main/sub stream lost frame ctrl
for(i=0; i<3; i++)//csp modify 20130423
{
pNCCIns->pnLostFrame[i] = malloc(sizeof(u32)*pCfg->nChnMax);
if(NULL==pNCCIns->pnLostFrame[i])
{
NETCOMM_DEBUG_STR("allocate memory for pnLostFrame failed!!\n", -1);
errCode = -1;
goto NCINIT_ERR;
}
//csp modify 20130423
memset(pNCCIns->pnLostFrame[i],0,sizeof(u32)*pCfg->nChnMax);
}
pNCCIns->psSendCtrl = NULL;
pNCCIns->psSendCtrl = malloc(nTotalLinkNum*sizeof(SCPSSCtrl));
if( NULL == pNCCIns->psSendCtrl )
{
NETCOMM_DEBUG_STR("allocate memory for stream block failed!!\n", -1);
errCode = -1;
goto NCINIT_ERR;
}
// create queue for stream get
nSendBufNum = pCfg->nSendBufNumPerChn;
nChnNum = pCfg->nSubStreamMax;// default chnnum == substreamnum
nFrameSizeMax = pCfg->nFrameSizeMax;
#if 0//csp modify
if( !initMsgQ(&pNCCIns->netsndMsgQ,
nSendBufNum * nChnNum,
nFrameSizeMax * nChnNum,
64 ) )
{
NETCOMM_DEBUG_STR("initMsgQ", -1);
errCode = -1;
goto NCINIT_ERR;
}
// audio buff
if( !initMsgQ(&pNCCIns->netsndAudioMsgQ,
nSendBufNum * nChnNum,
nFrameSizeMax * nChnNum,
64 ) )
{
NETCOMM_DEBUG_STR("initMsgQ for audio", -1);
errCode = -1;
goto NCINIT_ERR;
}
printf("#############################initMsgQ bufnum %d chnnum %d framesizemax %d MsgQLen %d\n", nSendBufNum, nChnNum, nFrameSizeMax, nFrameSizeMax * nChnNum);
#else
if(nChnNum == 8)
{
#if defined(_JMV_) || defined(_JUAN_)//R9508S
//csp modify 20121016//这里需要验证
if(!initMsgQ(&pNCCIns->netsndMsgQ, nSendBufNum * nChnNum * 2, nFrameSizeMax * nChnNum * 3 / 2, 64))
{
NETCOMM_DEBUG_STR("initMsgQ", -1);
errCode = -1;
goto NCINIT_ERR;
}
#else
//csp modify 20121016//这里需要验证
int msgnum = nSendBufNum * nChnNum * 2 * 6;//csp modify 20140315
//if(!initMsgQ(&pNCCIns->netsndMsgQ, nSendBufNum * nChnNum * 2, nFrameSizeMax * nChnNum * 2, 64))
if(!initMsgQ(&pNCCIns->netsndMsgQ, msgnum, nFrameSizeMax * nChnNum * 2, 64))
{
NETCOMM_DEBUG_STR("initMsgQ", -1);
errCode = -1;
goto NCINIT_ERR;
}
#endif
}
else
{
//csp modify 20121016//这里需要验证
int msgnum = nSendBufNum * nChnNum * 2 * 6;//csp modify 20140315
//if(!initMsgQ(&pNCCIns->netsndMsgQ, nSendBufNum * nChnNum * 2, nFrameSizeMax * nChnNum * 2, 64))
if(!initMsgQ(&pNCCIns->netsndMsgQ, msgnum, nFrameSizeMax * nChnNum * 2, 64))
{
NETCOMM_DEBUG_STR("initMsgQ", -1);
errCode = -1;
goto NCINIT_ERR;
}
}
//4SDI:#############################initMsgQ bufnum 6 chnnum 4 framesizemax 1048576 MsgQLen 8388608
printf("#############################initMsgQ bufnum %d chnnum %d framesizemax %d MsgQLen %d\n", nSendBufNum * 2, nChnNum, nFrameSizeMax, nFrameSizeMax * nChnNum * 2);
#endif
if(nChnNum == 8)
{
#if defined(_JMV_) || defined(_JUAN_)//R9508S
if( !initMsgQ(&pNCCIns->netsndMbMsgQ,
nSendBufNum * nChnNum,
nFrameSizeMax * nChnNum * 3 / 4,
64 ) )
{
NETCOMM_DEBUG_STR("initMsgQ for mobile", -1);
errCode = -1;
goto NCINIT_ERR;
}
#else
if( !initMsgQ(&pNCCIns->netsndMbMsgQ,
nSendBufNum * nChnNum,
nFrameSizeMax * nChnNum,
64 ) )
{
NETCOMM_DEBUG_STR("initMsgQ for mobile", -1);
errCode = -1;
goto NCINIT_ERR;
}
#endif
}
else
{
if( !initMsgQ(&pNCCIns->netsndMbMsgQ,
nSendBufNum * nChnNum,
nFrameSizeMax * nChnNum,
64 ) )
{
NETCOMM_DEBUG_STR("initMsgQ for mobile", -1);
errCode = -1;
goto NCINIT_ERR;
}
}
// init cmdproc
#ifndef DEBUG_TEMP
cmdPro_Init(NULL, pCfg->nChnMax);
#endif
//printf("initMsgQ over\n");
//printf("NetCommInitThxd - 2, CheckNetLink:%d\n",CheckNetLink());
// preview init
if( remotePreview_Init( pCfg->yFactor,
pNCCIns->nChnTcpStreamNum,
nVidStreamNum,
nAudStreamNum,
pCfg->nVoipMode,
pCfg->nAudioMediaType,
pCfg->nVideoMediaType,
pCB,
pReqKeyCB,
pVOIP,
pVOIPIn )
)
{
errCode = -1;
goto NCINIT_ERR;
}
//printf("remotePreview_Init over\n");
// init remote play
errCode = remotePlay_Init(
pRpCB,
pCfg->nVideoMediaType,
pCfg->nAudioMediaType,
pCfg->nVideoFrameRate,
pCfg->nAudioFrameRate,
pCfg->nAudioMode,
pCfg->nChnMax
);
if(0!=errCode)
{
errCode = -1; // remotePlay_Init init failed
goto NCINIT_ERR;
}
//printf("remotePlay_Init over\n");
// down init
remoteDownload_Init(pRpCB);
//printf("remoteDownload_Init over\n");
// update init
remoteUpdate_Init(pCfg->pDiskMgr);
//printf("remoteUpdate_Init over\n");
// init psSendCtrl
PSCPSSCtrl pCtrl = pNCCIns->psSendCtrl;
for(i=0; i<nTotalLinkNum; i++)
{
pCtrl[i].sockfd = INVALID_SOCKET;
}
// create netcomm module cmd process thread
// create cmd struct
pCmd = CreateMsgCmd( sizeof(SNCCmdData) );
if(NULL==pCmd)
{
NETCOMM_DEBUG_STR("CreateMsgCmd failed!!!", -1);
errCode = -1; // create cmmand fail
goto NCINIT_ERR;
}
NETCOMM_DEBUG_STR("CreateMsgCmd ", 0);
//printf("CreateMsgCmd over\n");
//set callback
pNCCIns->pCB = pCB;
SetAddStreamLinkCB(AddStreamTCPLink, &pNCCIns->linkIP);
SetMsgCallBack(DealCommand, NULL);
NETCOMM_DEBUG_STR("SetAddStreamLinkCB&SetMsgCallBack ", 0);
//printf("SetAddStreamLinkCB&SetMsgCallBack over\n");
//Alarm upload init
//luo
// 放在监听线程初始化之前,避免资源为初始化就接收到报警上传信令后对无效资源进行操作 byspliang
#if 1
pthread_t thxalarmupload;
errCode = pthread_create(&thxalarmupload,
NULL,
netComm_AlarmUpload_init,
(void*)pHddMgr);
if(0 != errCode)
{
//pthread_exit(&thxalarmupload);
printf("alarmUpload_init error!\n");
errCode = -1;// fail to create rtsp thread
goto NCINIT_ERR;
}
#endif
//printf("NetCommInitThxd - 3, CheckNetLink:%d\n",CheckNetLink());
// init ctrlprotocol
nServerPort = pCfg->sParaIns.TCPPort;
errCode = CPLibInit(nServerPort);
if( CTRL_SUCCESS!=errCode )
{
errCode = -1; // ctrlprotocol init failed
goto NCINIT_ERR;
}
if( pCfg->sParaIns.DhcpFlag )
{
netComm_DHCP_Start();
}
//printf("CPLibInit over\n");
// mobile init
if( 0 != mobile_init(
pCfg->sParaIns.MobilePort,
pCfg->nSubStreamMax,
pCfg->nSubStreamMax,
pCfg->pReqStreamCB,
pCfg->nVideoMediaType,
pCfg->yFactor
) )
{
NETCOMM_DEBUG_STR("mobile_init", -1);
errCode = -1;
goto NCINIT_ERR;
}
#if 0
// rtsp init
pthread_t thxRtsp = 0;
printf("rtsp para nVidStreamNum %d \n", nVidStreamNum);
errCode = pthread_create(&thxRtsp,
NULL,
rtsp_init,
(void*)(u32)nVidStreamNum);
if(0 != errCode)
{
//pthread_exit(&thxRtsp);
errCode = -1;//fail to create rtsp thread
goto NCINIT_ERR;
}
#endif
//printf("NetCommInitThxd - 4, CheckNetLink:%d\n",CheckNetLink());
// Server_Tcp_Daemon??
// nat_fxn ??
NETCOMM_DEBUG_STR("CPLibInit ok, chech the port : ", 0);
NETCOMM_DEBUG(nServerPort);
pNCCIns->pMsgCmd = pCmd;
NETCOMM_DEBUG_STR("netCommCmd_ThreadFunc created, ok! ", 0);
pNCCIns->cmdThxd = cmdThxd;
// register writelog callback
pNCCIns->pWriteLogCB = pCfg->pWriteLogCB;
if(NULL == pNCCIns->pWriteLogCB)
{
NETCOMM_DEBUG_STR("null logwrite callback pointer", -1);
exit(1);
}
// register update & format callback
pNCCIns->pRFormat = pCfg->pRFomat;
pNCCIns->pRUpdate = pCfg->pRUpdate;
if(NULL == pNCCIns->pRFormat || NULL == pNCCIns->pRUpdate)
{
NETCOMM_DEBUG_STR("null remote update/format callback pointer", -1);
exit(1);
}
//printf("NetCommInitThxd - 5, CheckNetLink:%d\n",CheckNetLink());
// change web js
netComm_Webs_ChangeWebJs(nServerPort);
// web start
netComm_Webs_Start(pCfg->sParaIns.HttpPort);
// ddns init
memset(&initPara, 0, sizeof(initPara));
initPara.flag = pCfg->sParaIns.DDNSFlag;
initPara.interval = pCfg->sParaIns.UpdateIntvl;
strcpy(initPara.sStart.szDomain, (char *)pCfg->sParaIns.DDNSDomain);
strcpy(initPara.sStart.szUser, (char *)pCfg->sParaIns.DDNSUser);
strcpy(initPara.sStart.szPasswd, (char *)pCfg->sParaIns.DDNSPasswd);
//printf("NetCommInitThxd - 6, CheckNetLink:%d\n",CheckNetLink());
pthread_t ddnsInitThread;
pthread_create(&ddnsInitThread, NULL, netComm_DDNS_Init, &initPara);
// pppoe auto dial ...
if(pCfg->sParaIns.PPPOEFlag)
{
netComm_PPPOE_Start(1,
(char*)pCfg->sParaIns.PPPOEUser,
(char*)pCfg->sParaIns.PPPOEPasswd
);
}
else
{
//csp modify 20130321
if(pCfg->sParaIns.UPNPFlag)
{
printf("here,start upnp\n");
NetComm_RestartUPnP(pCfg->sParaIns.UPNPFlag);
}
}
//yaogang modify 20150324 //跃天: 1 nvr,2 轮巡解码器,3 切换解码器
if (pCfg->nNVROrDecoder == 1)
{
//yaogang modify 20141209 深广平台
net_sg_init(&pNCCIns->sCommonCfg);
}
NCINIT_ERR:
if(errCode!=0)
{
NETCOMM_DEBUG_STR("NetCommInit failed!!!",errCode);
}
else
{
pNCCIns->bInit = 1;
NETCOMM_DEBUG_STR("NetCommInit success!!!",errCode);
}
return NULL;
}
s32 NetCommInit( PSNetCommCfg pCfg )
{
printf("NetCommInit......\n");
pthread_t netThread;
return pthread_create(&netThread, NULL, NetCommInitThxd, (void*)pCfg);
}
//csp modify 20130423
static SNetThirdStreamProperty g_sNetThirdStreamProperty;
s32 NetComm_SetThirdStreamProperty(SNetThirdStreamProperty *psProperty)
{
if(psProperty == NULL)
{
return -1;
}
g_sNetThirdStreamProperty = *psProperty;
return 0;
}
s32 NetComm_GetThirdStreamProperty(SNetThirdStreamProperty *psProperty)
{
if(psProperty == NULL)
{
return -1;
}
*psProperty = g_sNetThirdStreamProperty;
return 0;
}
#if SEND_DELAY_DBG // def PRINT_SND_TIME
struct timeval start,end;
long span;
#endif
s32 NetCommSendPreviewFrame(PSNetComStmHead pHead, u8* pBuf)
{
u8* pQBuf = NULL;
u32 nDataLen = 0;
PSNetCommCtrl pNCCIns = NULL;
pNCCIns = &sNetCommCtrl;
if(NULL==pHead || NULL==pBuf)
{
NETCOMM_DEBUG_STR("met null pointer", -1);
//printf("~~~~~~~~~null!\n");
return -1;
}
switch(pHead->eFrameType)
{
case EM_TALK:
{
if(pHead->eFrameType == EM_TALK)
{
SVOIPFrame sFIns;
sFIns.nMediaType = pHead->byMediaType;
sFIns.nAudioMode = pHead->nAudioMode;
sFIns.nFrameRate = pHead->nFrameRate;
sFIns.nTStamp = pHead->timeStamp;
sFIns.nSize = pHead->dwlen;
sFIns.pData = pBuf;
return RemoteVoipSendData(&sFIns);
}
} break;
case EM_AUDIO:
{
//fill msg buf
nDataLen = pHead->dwlen+sizeof(SNetComStmHead);
//csp modify
//if(GetMsgQWriteInfo(&pNCCIns->netsndAudioMsgQ, &pQBuf, &nDataLen))
if(GetMsgQWriteInfo(&pNCCIns->netsndMsgQ, &pQBuf, &nDataLen))
{
memcpy(pQBuf, pHead, sizeof(SNetComStmHead));
memcpy(pQBuf+sizeof(SNetComStmHead), pBuf, nDataLen);
//csp modify
//post msg queue
//skipWriteMsgQ(&pNCCIns->netsndAudioMsgQ);
skipWriteMsgQ(&pNCCIns->netsndMsgQ);
return 0;
}
else
{
printf("too many audio frames to send ...\n");
//NETCOMM_DEBUG_STR("audio GetMsgQWriteInfo failed!!!", pHead->eFrameType);
}
} break;
case EM_VIDEO:
{
//fill msg buf
nDataLen = pHead->dwlen+sizeof(SNetComStmHead);
if((pHead->bSub & 0x2) || (pHead->bSub == 0))
{
//csp modify 20140318
if(pHead->bSub)
{
if(pNCCIns->pnLostFrame[1][pHead->byChnIndex])
{
if(pHead->byFrameType)
{
pNCCIns->pnLostFrame[1][pHead->byChnIndex] = 0;
}
else
{
pNCCIns->pnLostFrame[1][pHead->byChnIndex]++;
break;
//return -1;
}
}
}
else
{
if(pNCCIns->pnLostFrame[0][pHead->byChnIndex])
{
if(pHead->byFrameType)
{
pNCCIns->pnLostFrame[0][pHead->byChnIndex] = 0;
}
else
{
pNCCIns->pnLostFrame[0][pHead->byChnIndex]++;
break;
//return -1;
}
}
}
#if 0
int try_count = 1;
if(pHead->byFrameType)
{
try_count = 5;
}
while(1)
{
//send to preview
if(GetMsgQWriteInfo(&pNCCIns->netsndMsgQ, &pQBuf, &nDataLen))
{
//printf("chn%d w:%d, h:%d, len:%d\n", pHead->byChnIndex, pHead->nWidth, pHead->nHeight, pHead->dwlen);
memcpy(pQBuf, pHead, sizeof(SNetComStmHead));
memcpy(pQBuf+sizeof(SNetComStmHead), pBuf, pHead->dwlen/*nDataLen*/);
//post msg queue
skipWriteMsgQ(&pNCCIns->netsndMsgQ);
//printf("len:%d\n", pHead->dwlen);
//NETCOMM_DEBUG_STR("GetMsgQWriteInfo success!!!", 0);
break;
}
try_count--;
if(try_count <= 0)
{
//if(pHead->byChnIndex == 0) printf("chn%d net video get buf failed, len:%d\n", pHead->byChnIndex, pHead->dwlen);
//NETCOMM_DEBUG_STR("video GetMsgQWriteInfo failed!!!", pHead->eFrameType);
//calc lost frames
if(pHead->bSub)
{
pNCCIns->pnLostFrame[1][pHead->byChnIndex]++;
}
else
{
pNCCIns->pnLostFrame[0][pHead->byChnIndex]++;
}
break;
}
usleep(4000);
}
#else
//send to preview
if(GetMsgQWriteInfo(&pNCCIns->netsndMsgQ, &pQBuf, &nDataLen))
{
//printf("chn%d w:%d, h:%d, len:%d\n", pHead->byChnIndex, pHead->nWidth, pHead->nHeight, pHead->dwlen);
memcpy(pQBuf, pHead, sizeof(SNetComStmHead));
memcpy(pQBuf+sizeof(SNetComStmHead), pBuf, pHead->dwlen/*nDataLen*/);
//post msg queue
skipWriteMsgQ(&pNCCIns->netsndMsgQ);
//printf("len:%d\n", pHead->dwlen);
//NETCOMM_DEBUG_STR("GetMsgQWriteInfo success!!!", 0);
}
else
{
//if(pHead->byChnIndex == 0) printf("chn%d net video get buff failed, len:%d\n", pHead->byChnIndex, pHead->dwlen);
//NETCOMM_DEBUG_STR("video GetMsgQWriteInfo failed!!!", pHead->eFrameType);
//calc lost frames
if(pHead->bSub)
{
pNCCIns->pnLostFrame[1][pHead->byChnIndex]++;
}
else
{
pNCCIns->pnLostFrame[0][pHead->byChnIndex]++;
}
return -1;
}
#endif
}
else
{
//printf("error video frame type!\n");
//NETCOMM_DEBUG_STR("useless frame type, pHead->bSub %d!!!", pHead->bSub);
}
} break;
case EM_THIRD://csp modify 20130423
{
//csp modify 20140525
if(pHead->bSub & 0x2)
{
#if 1//csp modify 20140315
//printf("EM_THIRD-1,chn%d w=%d h=%d FrameType=%d\n",pHead->byChnIndex,pHead->nWidth,pHead->nHeight,pHead->byFrameType);
if(pNCCIns->pnLostFrame[2][pHead->byChnIndex])
{
if(pHead->byFrameType)
{
pNCCIns->pnLostFrame[2][pHead->byChnIndex] = 0;
}
else
{
pNCCIns->pnLostFrame[2][pHead->byChnIndex]++;
break;
//return -1;
}
}
//printf("EM_THIRD-2\n");
#endif
//fill msg buf
nDataLen = pHead->dwlen+sizeof(SNetComStmHead);
//send to preview
if(GetMsgQWriteInfo(&pNCCIns->netsndMsgQ, &pQBuf, &nDataLen))
{
memcpy(pQBuf, pHead, sizeof(SNetComStmHead));
memcpy(pQBuf+sizeof(SNetComStmHead), pBuf, pHead->dwlen/*nDataLen*/);
skipWriteMsgQ(&pNCCIns->netsndMsgQ);
}
else
{
pNCCIns->pnLostFrame[2][pHead->byChnIndex]++;
return -1;
}
//printf("EM_THIRD-3\n");
}
}break;
default:
{
//usleep(5000);
return -1;
}
}
if(pHead->bSub & 0x1)
{
//printf("chn%d mobile stream eFrameType=%d\n",pHead->byChnIndex,pHead->eFrameType);
//csp modify 20140525
//fill msg buf
nDataLen = pHead->dwlen+sizeof(SNetComStmHead);
//send to mobile monitor
if(GetMsgQWriteInfo(&pNCCIns->netsndMbMsgQ, &pQBuf, &nDataLen))
{
memcpy(pQBuf, pHead, sizeof(SNetComStmHead));
memcpy(pQBuf+sizeof(SNetComStmHead), pBuf, nDataLen);
//post msg queue
skipWriteMsgQ(&pNCCIns->netsndMbMsgQ);
//NETCOMM_DEBUG_STR("GetMsgQWriteInfo success!!!", 0);
}
else
{
//usleep(5000);
NETCOMM_DEBUG_STR("GetMsgQWriteInfo failed!!!", -1);
return -1;
}
}
return 0;
}
int AddStreamTCPLink(SOCKHANDLE hSock, ifly_TCP_Stream_Req req, void* pContext)
{
s32 errCode = 0;
netComm_DealStreamCmd(hSock, &req, ((struct sockaddr_in*)pContext)->sin_addr.s_addr);
usleep(40*1000);
return errCode;
}
void netcomm_assert( char * file_name, u32 line_no )
{
printf( "\n[netcomm] Assert failed: %s, line %u\n",
file_name, line_no );
abort();
}
int loopsend_ex(SOCKET s, char * buf, unsigned int sndsize)
{
int remian = sndsize;
int sendlen = 0;
int ret = 0;
ifly_TCP_Stream_Ack* pAck;
pAck = (ifly_TCP_Stream_Ack*)buf;
while(remian > 0)
{
ret=send(s,buf+sendlen,remian,0);
if(ret <= 0)
{