-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecodeMsg.cpp
1686 lines (1611 loc) · 45.8 KB
/
DecodeMsg.cpp
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 "StdAfx.h"
#include "DecodeMsg.h"
#include "SIPCallDlg.h"
static bool s_bExit = false;
static bool s_bRun = false;
#define RECV_BUFFER_SZ (4096)
#define MSG_QUE (0)
static BYTE s_RecvBuf[RECV_BUFFER_SZ] = {0};
static BYTE s_OuterRecvBuf[RECV_BUFFER_SZ] = {0};
static BYTE s_PCRecvBuf[RECV_BUFFER_SZ] = {0};
static BYTE s_PCSendBuf[RECV_BUFFER_SZ] = {0};
typedef bool (*DLLFUN)(HWND hWnd);
DLLFUN dll_Fun = NULL;
#define MSG_OUTER_PORT (54321)
#define MSG_PC_PORT (54320)
#define MSG_SEND_COUNT_NORMAL (1)
#define MSG_SEND_COUNT_RESEND2 (2)
#define BC_SEND_MSG (1)
extern char* g_dwLocalIp;
void Byte2Hex(BYTE *pbSrc, char *szDst, UINT uLen) //?? to ê?áù????
{
char strtemp[256];
//UCHAR b[512] = {0};
for(UINT k = 0; k < uLen; k++)//×a???aBYTEDíêy×é
{
BYTE bt=*(char*)(pbSrc + k); //×?·?Dí
memset(strtemp, 0, 256);
//??×?·?ò?ê?áù????·?ê??íè?áùê±±?á?strtemp′?·?£?×¢òa?aà??óè?ò???????
sprintf(strtemp, "%02x ", bt); //2?1?á???μ??°??210
//strtemp.Format("%02x ", bt);
strcat(szDst, strtemp); //?óè??óê?±à?-?ò??ó|×?·?′?
}
}
CDecodeMsg::CDecodeMsg(void)
{
WORD VersionRequested = MAKEWORD(1, 1);
WSADATA WsaData;
int Error = WSAStartup(VersionRequested, &WsaData);
//g_dwLocalIp = GetHostAddr();
g_dwLocalIp = GetHostIP();
m_vecIPData.reserve(10);
}
CDecodeMsg::~CDecodeMsg(void)
{
s_bExit = true;
s_bRun = false;
if (m_socket != 0xFFFFFFFF)
{
if(SOCKET_ERROR != closesocket(m_socket)){
m_socket = 0xFFFFFFFF;
}
}
if (m_socketPC != 0xFFFFFFFF)
{
if(SOCKET_ERROR != closesocket(m_socketPC)){
m_socketPC = 0xFFFFFFFF;
}
}
s_bExit = false;
if (!m_vecIPData.empty())
{
m_vecIPData.clear();
}
WSACleanup();
}
int CDecodeMsg::CreateSocket(/*char* ip, unsigned short port*/)
{
m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_socket == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
USHORT pcport = g_Config.m_SystemConfig.portPC;
serAddr.sin_port = htons(pcport);
serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(m_socket, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("bind error !");
closesocket(m_socket);
return 0;
}
CreateRecvThread();
return 1;
}
int CDecodeMsg::CreatePCSocket()
{
m_socketPC = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_socketPC == INVALID_SOCKET)
{
printf("socket pc error !");
return 0;
}
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
USHORT pcport = MSG_PC_PORT;
serAddr.sin_port = htons(pcport);
serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(m_socketPC, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("bind pc error !");
closesocket(m_socketPC);
return 0;
}
CreatePCRecvThread();
return 1;
}
int CDecodeMsg::CreateOuterSocket()
{
m_socketOuter = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(m_socketOuter == INVALID_SOCKET)
{
printf("socket outer error !");
return 0;
}
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
USHORT outerport = MSG_OUTER_PORT;
serAddr.sin_port = htons(outerport);
serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(m_socketOuter, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("bind outer error !");
closesocket(m_socketOuter);
return 0;
}
CreateOuterRecvThread();
return 1;
}
UINT RecvMsgProc(LPVOID pParam)
{
g_Log.output(LOG_TYPE, "recv msg init....\r\n");
CDecodeMsg* pDecodeMsg = (CDecodeMsg*)pParam;
if (pDecodeMsg == NULL)
{
return 0;
}
CWinThread* pThread = AfxGetThread();
if (pThread == NULL)
{
return 0;
}
g_Log.output(LOG_TYPE, "recv msg start....\r\n");
// fd_set fds;
// struct timeval timeout={0, 10}; //selectμè′y3??£?3?????ˉ£?òa·?×èè??í??0
while(!g_bExitApp/*!s_bExit*/){
//sockaddr_in sin;
//int sinlen;
//int recvLen = recvfrom(pDecodeMsg->m_socket, (char*)s_RecvBuf, RECV_BUFFER_SZ, 0, (sockaddr *)&sin, &sinlen);
Sleep(1);
int timeout = 1000;
fd_set rdset;
FD_ZERO(&rdset);
FD_SET(pDecodeMsg->m_socket, &rdset);
timeval tm;
tm.tv_sec = timeout / 1000;
tm.tv_usec = timeout % 1000 * 1000;
int ts = select(pDecodeMsg->m_socket + 1, &rdset, NULL, NULL, &tm);
if(ts <= -1){
int iError = WSAGetLastError();
return 0;
}
else if (ts == 0)
{
// 3?ê±
}
else{
#if 0
int recvLen = recv(pDecodeMsg->m_socket, (char*)s_RecvBuf, RECV_BUFFER_SZ, 0);
#else
sockaddr_in sin;
int sinlen = sizeof(struct sockaddr_in);
int recvLen = recvfrom(pDecodeMsg->m_socket, (char*)s_RecvBuf, RECV_BUFFER_SZ, 0, (sockaddr *)&sin, &sinlen);
#endif
if (recvLen > 0)
{
char* recvIP = inet_ntoa(sin.sin_addr);
USHORT recvPort = ntohs(sin.sin_port);
//g_Log.output(LOG_TYPE_8, "recv msg len: %d....ip: %s\r\n", recvLen, recvIP);
if (recvLen == SEND_BUFFER_SZ)
{
pDecodeMsg->DecodeMsg(s_RecvBuf, recvLen, recvIP, recvPort);
}
else if (recvLen == 7)
{
pDecodeMsg->DecodeMsgError7(s_RecvBuf, recvLen, recvIP, recvPort);
}
else if (recvLen == 9)
{
pDecodeMsg->DecodeMsgError9(s_RecvBuf, recvLen, recvIP, recvPort);
}
}
else{
int iError = WSAGetLastError();
g_Log.output(LOG_TYPE_8, "recv msg len[%d], error[%d], socket[%d]!!!!!\r\n", recvLen, iError, pDecodeMsg->m_socket);
}
}
}
return 1;
}
void CDecodeMsg::CreateRecvThread() // ′′?¨ Thread??3ì
{
AfxBeginThread(RecvMsgProc, this);
}
UINT RecvPCMsgProc(LPVOID pParam)
{
g_Log.output(LOG_TYPE, "recv pc msg init....\r\n");
CDecodeMsg* pDecodeMsg = (CDecodeMsg*)pParam;
if (pDecodeMsg == NULL)
{
return 0;
}
CWinThread* pThread = AfxGetThread();
if (pThread == NULL)
{
return 0;
}
g_Log.output(LOG_TYPE, "recv pc msg start....\r\n");
while(!g_bExitApp){
Sleep(1);
int timeout = 1000;
fd_set rdset;
FD_ZERO(&rdset);
FD_SET(pDecodeMsg->m_socketPC, &rdset);
timeval tm;
tm.tv_sec = timeout / 1000;
tm.tv_usec = timeout % 1000 * 1000;
int ts = select(pDecodeMsg->m_socketPC + 1, &rdset, NULL, NULL, &tm);
if(ts <= -1){
int iError = WSAGetLastError();
return 0;
}
else if (ts == 0)
{
// 超时
}
else{
sockaddr_in sin;
int sinlen = sizeof(struct sockaddr_in);
int recvLen = recvfrom(pDecodeMsg->m_socketPC, (char*)s_PCRecvBuf, RECV_BUFFER_SZ, 0, (sockaddr *)&sin, &sinlen);
if (recvLen > 0)
{
if (recvLen == USER_BUFFER_SZ)
{
pDecodeMsg->DecodeUserMsg(s_PCRecvBuf, USER_BUFFER_SZ);
//g_Log.output(LOG_TYPE_8, "@@recv pc--ip: %s data:%s\r\n", inet_ntoa(sin.sin_addr),s_PCRecvBuf);
}
}
else{
int iError = WSAGetLastError();
g_Log.output(LOG_TYPE_8, "recv pc msg len[%d], error[%d], socket[%d]!!!!!\r\n", recvLen, iError, pDecodeMsg->m_socketPC);
}
}
}
return 1;
}
UINT RecvOuterMsgProc(LPVOID pParam)
{
g_Log.output(LOG_TYPE, "recv outer msg init....\r\n");
CDecodeMsg* pDecodeMsg = (CDecodeMsg*)pParam;
if (pDecodeMsg == NULL)
{
return 0;
}
CWinThread* pThread = AfxGetThread();
if (pThread == NULL)
{
return 0;
}
g_Log.output(LOG_TYPE, "recv outer msg start....\r\n");
while(!g_bExitApp){
Sleep(1);
int timeout = 1000;
fd_set rdset;
FD_ZERO(&rdset);
FD_SET(pDecodeMsg->m_socketOuter, &rdset);
timeval tm;
tm.tv_sec = timeout / 1000;
tm.tv_usec = timeout % 1000 * 1000;
int ts = select(pDecodeMsg->m_socketOuter + 1, &rdset, NULL, NULL, &tm);
if(ts <= -1){
int iError = WSAGetLastError();
return 0;
}
else if (ts == 0)
{
// 超时
}
else{
sockaddr_in sin;
int sinlen = sizeof(struct sockaddr_in);
int recvLen = recvfrom(pDecodeMsg->m_socketOuter, (char*)s_OuterRecvBuf, RECV_BUFFER_SZ, 0, (sockaddr *)&sin, &sinlen);
if (recvLen > 0)
{
pDecodeMsg->DecodeOuterMsg(s_OuterRecvBuf,recvLen);
}
else{
int iError = WSAGetLastError();
g_Log.output(LOG_TYPE_8, "recv outer msg len[%d], error[%d], socket[%d]!!!!!\r\n", recvLen, iError, pDecodeMsg->m_socketPC);
}
}
}
return 1;
}
void CDecodeMsg::CreatePCRecvThread()
{
AfxBeginThread(RecvPCMsgProc, this);
}
void CDecodeMsg::CreateOuterRecvThread()
{
AfxBeginThread(RecvOuterMsgProc, this);
}
//----------------------分割线-------------------------
void CDecodeMsg::SendHeartBeat(BYTE subTelNo, char* ip, int port)//巡检
{
m_Lock.Lock();
m_pBuffer[0] = 0xA0;
m_pBuffer[1] = 0x9F;
ModifyHead();
m_pBuffer[2] = subTelNo;
m_pBuffer[3] = 0x33; //巡检
m_pBuffer[4] = 0x00;
m_pBuffer[5] = 0x01;
m_pBuffer[6] = 0x01;
m_pBuffer[7] = GetCheckNum(); //巡检 A09F023300010176
Send8Byte2Device(m_pBuffer, ip, port);
m_Lock.Unlock();
}
void CDecodeMsg::SendCallReq2Branch(BYTE subTelNo,char* szIp,BYTE callOpt)
{
m_LockPC.Lock();
memset(s_PCSendBuf, 0, 16 *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = subTelNo;
s_PCSendBuf[3] = 0xA0; //对分控分机进行操作请求
s_PCSendBuf[4] = callOpt; //0x01 打开分机,0x02 关闭分机,0x03 呼叫转移打开分机
s_PCSendBuf[8] = 0xff;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if (g_Config.m_SystemConfig.nMainSystem) // 主系统->各个分系统
{
sendtomsg(m_socketPC,(const char*)s_PCSendBuf,USER_BUFFER_SZ,MSG_SEND_COUNT_NORMAL,szIp,port);
}
m_LockPC.Unlock();
}
//************************************
// Method: SendBroadcastReq
// FullName: CDecodeMsg::SendBroadcastReq
// Access: public
// Returns: void
// Qualifier:
// Parameter: BYTE subTelNo
// Parameter: char * szIp
// Parameter: BYTE broadcastOpt 1:open 2:close
//************************************
void CDecodeMsg::SendBroadcastReq2Branch(BYTE subTelNo,char* szIp,BYTE broadcastOpt)
{
m_LockPC.Lock();
memset(s_PCSendBuf, 0, 16 *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = subTelNo;
s_PCSendBuf[3] = 0xA3; //发送打开或关闭广播 指令
s_PCSendBuf[4] = broadcastOpt; //1:open 2:close 3:pause 4:resume
s_PCSendBuf[8] = 0xff;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if (g_Config.m_SystemConfig.nMainSystem) // 主系统->各个分系统
{
sendtomsg(m_socketPC,(const char*)s_PCSendBuf,USER_BUFFER_SZ,MSG_SEND_COUNT_NORMAL,szIp,port);
}
else{
}
m_LockPC.Unlock();
}
void CDecodeMsg::DecodeBroadcastReqFromMaster(BYTE* pBuffer,int size)
{
SendMessage(m_pAPP->m_hWnd,RECV_MSG_BC_REQ,pBuffer[2],pBuffer[4]);
}
void CDecodeMsg::SendOpenCall(BYTE subTelNo, char* ip, int port) //通话
{
m_Lock.Lock();
m_pBuffer[0] = 0xA0;
m_pBuffer[1] = 0x9F;
ModifyHead();
m_pBuffer[2] = subTelNo;
m_pBuffer[3] = 0x01;
m_pBuffer[4] = 0x00;
m_pBuffer[5] = 0x01;
m_pBuffer[6] = 0x01;
m_pBuffer[7] = GetCheckNum(); //通话 A09F 02 0100010144
Send8Byte2Device(m_pBuffer, ip, port);
m_Lock.Unlock();
}
void CDecodeMsg::SendDeviceCallStatus2Master(BYTE subTelNo,BYTE status)
{
//branch send device call status 2 master
m_LockPC.Lock();
memset(s_PCSendBuf, 0, 16 *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = subTelNo;
s_PCSendBuf[3] = 0xA1; //send call status
s_PCSendBuf[4] = status; //01: is using 02: is usable 03: is offline 04:om down ,05:sipgw down
s_PCSendBuf[8] = 0xff;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if(!g_Config.m_SystemConfig.nMainSystem)
sendtomsg(m_socketPC,(const char*)s_PCSendBuf,USER_BUFFER_SZ,MSG_SEND_COUNT_NORMAL,data[0].ip,port);
m_LockPC.Unlock();
}
void CDecodeMsg::SendBranchOMDown2Master(BYTE status)
{
//·??úμ??°?y?úê1ó?£?í¨?a·????¢?÷??
m_LockPC.Lock();
memset(s_PCSendBuf, 0, 16 *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = 0;
s_PCSendBuf[3] = 0xA5; //·??úμ??°×′ì?
s_PCSendBuf[4] = status; //01£oonline£?02£odown
s_PCSendBuf[8] = 0xff;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
IP2BYTE(g_dwLocalIp,s_PCSendBuf[9],s_PCSendBuf[10],s_PCSendBuf[11],s_PCSendBuf[12]);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if(!g_Config.m_SystemConfig.nMainSystem)
sendtomsg(m_socketPC,(const char*)s_PCSendBuf,USER_BUFFER_SZ,MSG_SEND_COUNT_NORMAL,data[0].ip,port);
m_LockPC.Unlock();
}
void CDecodeMsg::DecodeBrachOMDown(BYTE* pBuffer,int size)
{
CString ip = BYTE2IP(pBuffer[9],pBuffer[10],pBuffer[11],pBuffer[12]);
SendMessage(m_pAPP->m_hWnd,RECV_BRANCH_OM_STATUS,pBuffer[2],(LPARAM)&ip);
}
void CDecodeMsg::DecodeDeviceCallStatusFromBranch(BYTE* pBuffer,int size)
{
SendMessage(m_pAPP->m_hWnd,RECV_MSG_CALL_STATUS,pBuffer[2],pBuffer[4]);
}
void CDecodeMsg::SendDeviceBroadcastStatus2Master(BYTE subTelNo,char* szIp,BYTE status)
{
//分机广播正在使用,通知分控、主控
m_LockPC.Lock();
memset(s_PCSendBuf, 0, 16 *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = subTelNo;
s_PCSendBuf[3] = 0xA2; //分机广播状态
s_PCSendBuf[4] = status; //01:在使用,02:可用,03:离线
s_PCSendBuf[8] = 0xff;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if (g_Config.m_SystemConfig.nMainSystem) // 主系统->各个分系统
{
//sendtomsg(m_socketPC,(const char*)s_PCSendBuf,USER_BUFFER_SZ,MSG_SEND_COUNT_NORMAL,szIp,port);
}
else{
sendtomsg(m_socketPC,(const char*)s_PCSendBuf,USER_BUFFER_SZ,MSG_SEND_COUNT_NORMAL,data[0].ip,port);
}
m_LockPC.Unlock();
}
void CDecodeMsg::DecodeDeviceBroadcastStatusFromBranch(BYTE* pBuffer,int size)
{
//2 分机号,4广播状态
SendMessage(m_pAPP->m_hWnd,RECV_MSG_BROADCAST_STATUS,pBuffer[2],pBuffer[4]);
}
void CDecodeMsg::SendHangUp(BYTE subTelNo, char* ip, int port) //挂机
{
m_Lock.Lock();
m_pBuffer[0] = 0xA0;
m_pBuffer[1] = 0x9F;
ModifyHead();
m_pBuffer[2] = subTelNo;
m_pBuffer[3] = 0x02;
m_pBuffer[4] = 0x00;
m_pBuffer[5] = 0x01;
m_pBuffer[6] = 0x01;
m_pBuffer[7] = GetCheckNum();
Send8Byte2Device(m_pBuffer, ip, port); //挂机 A09F020200010145
m_Lock.Unlock();
}
void CDecodeMsg::SendOpenBroadcast(BYTE subTelNo, char* ip, int port)//开广播
{
#if BC_SEND_MSG
//g_Log.output(LOG_TYPE, "SendOpenBroadcast...., %d\r\n", port);
m_Lock.Lock();
m_pBuffer[0] = 0xA0;
m_pBuffer[1] = 0x9F;
ModifyHead();
m_pBuffer[2] = subTelNo;
m_pBuffer[3] = 0x05; //广播开 A09F020501010149
m_pBuffer[4] = 0x01;
m_pBuffer[5] = 0x01;
m_pBuffer[6] = 0x01;
m_pBuffer[7] = GetCheckNum();
Send8Byte2Device(m_pBuffer, ip, port);
m_Lock.Unlock();
#endif
}
void CDecodeMsg::SendCloseBroadcast(BYTE subTelNo, char* ip, int port, bool beat)
{
#if BC_SEND_MSG
m_Lock.Lock();
m_pBuffer[0] = 0xA0;
m_pBuffer[1] = 0x9F;
ModifyHead();
m_pBuffer[2] = subTelNo;
m_pBuffer[3] = 0x05;
m_pBuffer[4] = 0x00;
m_pBuffer[5] = 0x01;
m_pBuffer[6] = 0x01;
m_pBuffer[7] = GetCheckNum();
Send8Byte2Device(m_pBuffer, ip, port);
m_Lock.Unlock();
#endif
}
//------------ ??ó| ------------
void CDecodeMsg::SendCallAnswer(BYTE subTelNo, char* ip, int port)
{
// 回应
// 00 00 02 E1 00 00 00 E3
m_Lock.Lock();
m_pBuffer[0] = 0xA0;
m_pBuffer[1] = 0x9F;
ModifyHead();
m_pBuffer[2] = subTelNo;
m_pBuffer[3] = 0xE5; //呼叫回应 A09F02E500000026
m_pBuffer[4] = 0x00;
m_pBuffer[5] = 0x00;
m_pBuffer[6] = 0x00;
m_pBuffer[7] = GetCheckNum();
Send8Byte2Device(m_pBuffer, ip, port);
m_Lock.Unlock();
}
BYTE CDecodeMsg::GetCheckNum(int len) //第八位校验和
{
BYTE checkNum = 0;
for (int i = 0; i < len - 1; i++)
{
checkNum += m_pBuffer[i];
}
checkNum &= 0xFF;
return checkNum;
}
//----------------------分割线-------------------------
void CDecodeMsg::DecodeMsgError7(BYTE* pBuffer, int size, char* szIP, USHORT port)
{
BYTE pErrorBuf[8] = {0}; //错误数据初始化0
if (pBuffer[0] == 0x9F)
{
pErrorBuf[0] = 0xA0; //错误位左移?
memcpy(pErrorBuf + 1, pBuffer, 7 * sizeof(BYTE));
// check
BYTE checkNum = 0; //校验位置0
for (int i = 0; i < SEND_BUFFER_SZ - 1; i++) //(8)
{
checkNum += pErrorBuf[i];
}
checkNum &= 0xFF;
if (pErrorBuf[7] == checkNum)
{
DecodeMsg(pErrorBuf, 8, szIP, port);
}
else{ //错误数据校验位不对,则报错
g_Log.output(LOG_TYPE_8, "recv msg len size(7) check error....\r\n");
}
}
else{ //正常MSG第一位不对,直接报错!!!
g_Log.output(LOG_TYPE_8, "recv msg len size(7) error....\r\n");
}
}
void CDecodeMsg::DecodeMsgError9(BYTE* pBuffer, int size, char* szIP, USHORT port)
{
BYTE pErrorBuf[8] = {0};
if ((pBuffer[0] == 0xA0) && (pBuffer[1] == 0x9F))
{
memcpy(pErrorBuf, pBuffer, 8 * sizeof(BYTE)); //复制到ERRORbuf
// check
BYTE checkNum = 0;
for (int i = 0; i < SEND_BUFFER_SZ - 1; i++)
{
checkNum += pErrorBuf[i];
}
checkNum &= 0xFF;
if (pErrorBuf[7] == checkNum)
{
DecodeMsg(pErrorBuf, 8, szIP, port);
}
else{
g_Log.output(LOG_TYPE_8, "recv msg len size(9) check error....\r\n");
}
}
else{
g_Log.output(LOG_TYPE_8, "recv msg len size(9) error....\r\n");
}
}
//-------------消息处理构造函数-------beginning------------------
void StringSplit(CString source, CStringArray& dest, char division)
{
if(source.IsEmpty())
{
}
else
{
int pos = source.Find(division);
if(pos == -1)
{
dest.Add(source);
}
else
{
dest.Add(source.Left(pos));
source = source.Mid(pos+1);
StringSplit(source,dest,division);
}
}
}
void CDecodeMsg::DecodeOuterMsg(BYTE* pBuffer,int size)
{
CStringArray msgs;
CString msg;
msg.Format(L"%s",pBuffer); //unicode编码 发送
/*wchar_t outStr[MAX_PATH] = {0};
MultiByteToWideChar(CP_ACP, 0, (char*)pBuffer, -1, outStr, MAX_PATH);
msg = outStr;*/
StringSplit(msg,msgs,';');
if(msgs.GetSize()>0)
{
int compareRtn = msgs[0].CompareNoCase(L"GetMusicList");
if(compareRtn == 0)
{
DecodeOuterBCMusicPath();
}else{
CString cmd,path;
vector<int> ets;
cmd = msgs[0];
path = msgs[1];
CStringArray etsStr;
StringSplit(msgs[2],etsStr,',');
for(int i=0;i<etsStr.GetSize();i++)
{
int et = _ttoi(etsStr[i]);
ets.push_back(et);
}
DecodeOuterBCControl(cmd,path,ets);
}
}
}
BOOL IsFileExist(const CString& csFile)
{
DWORD dwAttrib = GetFileAttributes(csFile);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
void CDecodeMsg::DecodeOuterBCMusicPath()
{
char* ip = g_Config.m_SystemConfig.IP_OUTER_BC_CONTROL;
int port = g_Config.m_SystemConfig.portOuterBCControl;
TCHAR pBuf[MAX_PATH] = {0};
int nNum = 0;
SListSong* pListSong = g_data.GetSongList(nNum);
for(int i=0;i<nNum;i++)
{
//sprintf(pBuf, "music:%s;",pListSong[i].szName);
MultiByteToWideChar(CP_ACP,0,pListSong[i].szName,-1,pBuf,MAX_PATH);
sendtomsg(m_socketOuter, (char*)pBuf, wcslen(pBuf)*sizeof(TCHAR), MSG_SEND_COUNT_RESEND2, ip, port);
Sleep(30);
}
}
void CDecodeMsg::DecodeOuterBCControl(const CString& cmd,const CString& path,const vector<int>& ets)
{
if(cmd.CompareNoCase(_T("start")) == 0)
{
if(path.IsEmpty()||!IsFileExist(g_szAPPPath+"music\\"+path))
{
return ;
}
SendMessage(m_pAPP->m_hWnd,RECV_MSG_OUTER_BC_OPT_START,(WPARAM)&(g_szAPPPath+"music\\"+path),(LPARAM)&ets);
}else
{
SendMessage(m_pAPP->m_hWnd,RECV_MSG_OUTER_BC_OPT_CLOSE,NULL,(LPARAM)&ets);
}
}
void CDecodeMsg::DecodeMsg(BYTE* pBuffer, int size, char* szIP, USHORT port)
{
if (size != SEND_BUFFER_SZ) // MSG长度不为8,报错
{
g_Log.output(LOG_TYPE_8, "recv device msg len size error....\r\n");
return;
}
else{
char szHex[256] = {0};
Byte2Hex(pBuffer, szHex, SEND_BUFFER_SZ);
g_Log.output(LOG_TYPE_8, "recv msg: IP:%s,,data:%s..\r\n", szIP,szHex);
//正确则输出MSG!
}
// check 校验BEGIN
BYTE checkNum = 0;
for (int i = 0; i < SEND_BUFFER_SZ - 1; i++)
{
checkNum += pBuffer[i];
}
checkNum &= 0xFF;
// if (pBuffer[7] != checkNum)
// {
// return;
// }
// end
BYTE msgFlag = pBuffer[3]; // 第四位命令标志位(开门关门巡检等~)
switch(msgFlag){
case 0x58: //开门
DecodeDoor(pBuffer, size); //解析门开,调用DecodeDoor(调用窗口句柄回到WINpro触发消息)!!!!!
Send8Byte2PC(pBuffer, szIP, port);
break ;
case 0x57: //关门
DecodeDoorClose(pBuffer, size);
Send8Byte2PC(pBuffer, szIP, port);
break;
case 0x03:
DecodeCall(pBuffer, size);
//Send8Byte2PC(pBuffer, szIP, port);
break ;
case 0x43: //巡检回应
DecodeHeartBeat(pBuffer, size);
Send8Byte2PC(pBuffer, szIP, port);
break ;
case 0xE1: //·??ú???′′ò?aí¨?°£??÷??ê?μ?oó?üD?×′ì??¢????·???£?·???ê?μ?oó?üD?×′ì?
//DecodeE1(pBuffer, size);
//if(g_Config.m_SystemConfig.nMainSystem)
PostMessage(m_pAPP->m_hWnd, RECV_MSG_E1, pBuffer[2], NULL);
Send8Byte2PC(pBuffer, szIP, port);
break ;
case 0xE2:
//DecodeE2(pBuffer, size);
SendMessage(m_pAPP->m_hWnd, RECV_MSG_E2, pBuffer[2], NULL);
Send8Byte2PC(pBuffer, szIP, port);
break;
case 0xE3:
DecodeE3(pBuffer, size);
//×a·¢1?2¥?a???′£?·¢?í??Dèòaò????a1?2¥??á????¢?óáD£??¨ê±?ì2é
Send8Byte2PC(pBuffer, szIP, port);
break ;
case 0xE4:
DecodeE4(pBuffer, size);
////×a·¢1?2¥1????′£?·¢?í??Dèòaò???1?1?2¥??á????¢?óáD£??¨ê±?ì2é
Send8Byte2PC(pBuffer, szIP, port);
break ;
case 0xE9:
Decode_Auto_BC_Recv(pBuffer, size);
break;
case 0x19:
Decode_Auto_BC_ReRecv(pBuffer, size);
break;
}
}
//-------------消息处理构造函数--ending-----------------------
//--------------解析用户MSG-------------------------------
void CDecodeMsg::DecodeUserMsg(BYTE* pBuffer, int size)
{
if (size != USER_BUFFER_SZ) //用户MSG 16
{
g_Log.output(LOG_TYPE, "recv pc msg len size error....\r\n");
return;
}
else{
char szHex[256] = {0};
Byte2Hex(pBuffer, szHex, USER_BUFFER_SZ);
g_Log.output(LOG_TYPE_8, "!!!!recv pc msg:data:%s..\r\n",szHex);
}
// check
BYTE checkNum = 0;
for (int i = 0; i < USER_BUFFER_SZ - 1; i++)
{
checkNum += pBuffer[i];
}
checkNum &= 0xFF;
BYTE msgFlag = pBuffer[3]; //消息标志位
switch(msgFlag){
case 0xBB: //PC心跳
break;
case 0xDB: //手动巡检?
SendMessage(m_pAPP->m_hWnd, RECV_MSG_DEVICE_BEAT, pBuffer[2], NULL);
g_Log.output(LOG_TYPE, "RECV_MSG_DEVICE_BEAT.........\r\n");
break;
case 0xDA: //手动巡检回复?
{
SBeatReply beat;
beat.extAddr = pBuffer[2];
beat.bc = (enDeviceStatus)(pBuffer[8]);
beat.call = (enDeviceStatus)(pBuffer[9]);
SendMessage(m_pAPP->m_hWnd, RECV_MSG_DEVICE_BEAT_REPLY, pBuffer[2], (LPARAM)(&beat));
g_Log.output(LOG_TYPE, "RECV_MSG_DEVICE_BEAT_REPLY[%d].........\r\n", pBuffer[2]);
}
break;
case 0xDC: //更新状态
{
SBeatReply beat;
beat.extAddr = pBuffer[2];
beat.bc = (enDeviceStatus)(pBuffer[8]);
beat.call = (enDeviceStatus)(pBuffer[9]);
SendMessage(m_pAPP->m_hWnd, RECV_MSG_DEVICE_UPDATE_STATUS, pBuffer[2], (LPARAM)(&beat));
g_Log.output(LOG_TYPE, "RECV_MSG_DEVICE_UPDATE_STATUS[%d].........\r\n", pBuffer[2]);
}
break;
case 0x90: //由分控请求改为主控请求
DecodeDeviceStatusReq(pBuffer, size);
break;
case 0x91: //由主控推分控改为分控推给主控
DecodeDeviceStatusResponse(pBuffer, size);
break;
case 0x92: //由分控推主控改为主控推给分控
DecodeDeviceStatusAck(pBuffer, size);
break;
case 0xA0:
DecodeCallReqFromMaster(pBuffer,size);
break;
case 0xA1:
DecodeDeviceCallStatusFromBranch(pBuffer,size);
break;
case 0xA2:
DecodeDeviceBroadcastStatusFromBranch(pBuffer,size);
break;
case 0xA3:
DecodeBroadcastReqFromMaster(pBuffer,size);
break;
case 0xE1:
SendMessage(m_pAPP->m_hWnd,RECV_MSG_E1,pBuffer[2],pBuffer[4]);
break;
case 0xE2:
SendMessage(m_pAPP->m_hWnd,RECV_MSG_E2,pBuffer[2],pBuffer[4]);
break;
case 0xE3:
SendMessage(m_pAPP->m_hWnd,RECV_MSG_E3,pBuffer[2],pBuffer[4]);
break;
case 0xE4:
SendMessage(m_pAPP->m_hWnd,RECV_MSG_E4,pBuffer[2],pBuffer[4]);
break;
case 0x43:
DecodeHeartBeat(pBuffer,size);
break;
case 0xA5:
DecodeBrachOMDown(pBuffer,size);
break;
case 0xB0:
DecodeDeviceBeatReq(pBuffer,size);
break;
}
}
void CDecodeMsg::SendCallResponse2Master(BYTE subTelNo)
{
}
void CDecodeMsg::DecodeCallResponseFromMaster(BYTE* pBuffer,int size)
{}
void CDecodeMsg::DecodeCallReqFromMaster(BYTE* pBuffer,int size)
{
SendMessage(m_pAPP->m_hWnd, RECV_MSG_CALL_REQ, pBuffer[2], pBuffer[4]);
}
void CDecodeMsg::SendPCBeat()
{
m_LockPC.Lock();
memset(s_PCSendBuf, 0, USER_BUFFER_SZ *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = 0x00;
s_PCSendBuf[3] = 0xBB;
s_PCSendBuf[4] = 0x00;
s_PCSendBuf[5] = 0x00;
s_PCSendBuf[6] = 0x00;
s_PCSendBuf[7] = 0x00;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if (g_Config.m_SystemConfig.nMainSystem) // 主系统->各个分系统
{
for (int i = 1; i < nNum; i++)
{
sendtomsg(m_socketPC, (const char*)s_PCSendBuf, USER_BUFFER_SZ, MSG_SEND_COUNT_NORMAL, data[i].ip, port);
}
}
else{
if (nNum > 0)
{
sendtomsg(m_socketPC, (const char*)s_PCSendBuf, USER_BUFFER_SZ, MSG_SEND_COUNT_NORMAL, data[0].ip, port);
}
}
m_LockPC.Unlock();
}
void CDecodeMsg::SendPCBeatReply(char* pIP)
{
m_LockPC.Lock();
memset(s_PCSendBuf, 0, USER_BUFFER_SZ *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = 0x00;
s_PCSendBuf[3] = 0xBA;
s_PCSendBuf[4] = 0x00;
s_PCSendBuf[5] = 0x00;
s_PCSendBuf[6] = 0x00;
s_PCSendBuf[7] = 0x00;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
USHORT port = MSG_PC_PORT;
sendtomsg(m_socketPC, (const char*)s_PCSendBuf, USER_BUFFER_SZ, MSG_SEND_COUNT_NORMAL, pIP, port);
m_LockPC.Unlock();
}
void CDecodeMsg::SendDeviceBeat(BYTE subTelNo)
{
m_LockPC.Lock();
memset(s_PCSendBuf, 0, USER_BUFFER_SZ *sizeof(BYTE));
s_PCSendBuf[0] = 0xA0;
s_PCSendBuf[1] = 0x9F;
s_PCSendBuf[2] = subTelNo;
s_PCSendBuf[3] = 0xDB;
s_PCSendBuf[4] = 0x00;
s_PCSendBuf[5] = 0x00;
s_PCSendBuf[6] = 0x00;
s_PCSendBuf[7] = 0x00;
s_PCSendBuf[15] = GetCheckNum(USER_BUFFER_SZ);
int nNum = 0;
SIPData* data = GetIPData(nNum);
USHORT port = MSG_PC_PORT;
if (g_Config.m_SystemConfig.nMainSystem) // 主系统->各个分系统
{
for (int i = 1; i < nNum; i++)
{
sendtomsg(m_socketPC, (const char*)s_PCSendBuf, USER_BUFFER_SZ, MSG_SEND_COUNT_NORMAL, data[i].ip, port);
}
}
else{
if (nNum > 0)
{
sendtomsg(m_socketPC, (const char*)s_PCSendBuf, USER_BUFFER_SZ, MSG_SEND_COUNT_NORMAL, data[0].ip, port);
}
}
m_LockPC.Unlock();
}
void CDecodeMsg::SendDeviceBeatReply(BYTE subTelNo, enDeviceStatus bc, enDeviceStatus call)
{
m_LockPC.Lock(); //线程,上锁
memset(s_PCSendBuf, 0, USER_BUFFER_SZ *sizeof(BYTE));