forked from samuelweiwei/AppServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1104 lines (1013 loc) · 21.6 KB
/
main.c
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
/**********************************
命令示例:
set appeui ff-ff-ff-ff-ff-ff-ff-fe
set appkey 11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00
join <appeui ff-ff-ff-ff-ff-ff-ff-fe>
quit <appeui ff-ff-ff-ff-ff-ff-ff-fe>
sendto <appeui ff-ff-ff-ff-ff-ff-ff-fe> deveui 00-00-00-00-01-02-0a-0b cfm 1 payload 80:12:1f:ab:28:09:21 <port 28>
show
**********************************/
#include "claa_base.h"
#include "claa_cs.h"
#include "claa_ssl.h"
/*
下面关于socket的宏定义是为了统一linux和windows上的Socket api
linux下编译程序时需要定义CLAA_LINUX
*/
enum cmd
{
JOIN=1,
QUIT,
SENDTO,
SET,
SHOW,
HELP
};
enum show_type
{
ALL,
APPEUI,
APPKEY
};
enum msg_type
{
DATA=100,
ACK=200
};
void claa_set_tcp_buffer(st_tcp_buffer *buffer);
uint32 claa_write_bytes(st_tcp_buffer* buf, uint8* data, sint32 len);
sint32 sock_open(schar *addr, uint16 portno);
void sock_close(sint32 *sockfd);
uint8 sock_reopen(sint32 *sockfd);
uint8 tcp_send(sint32 sockfd, const uint8* buffer, uint16 len);
void join_cmd_proc(void);
void quit_cmd_proc(void);
void sendto_cmd_proc(void);
void set_cmd_proc(void);
void show_cmd_proc(uint8 type);
void help_cmd_proc(void);
uint8 set_eui(schar *euistr, uint64 *eui);
uint8 set_payload(schar *payloadstr, uint8 *payload, uint16 *pydlen);
uint8 set_appkey(schar *keystr, uint8 *appkey);
uint8 get_cmd(schar *srcstr, schar *outstr, uint16 len);
uint8 get_para(schar *outstr, uint16 len);
sint32 creat_tcp_recv_thread(void);
void tcp_recv_thread_func(void);
void usage(void);
void hexdump(const unsigned char *buf, uint32 num);
sint32 g_sockfd = -1;
schar g_serip[15+1] = {0};
uint16 g_serport = 0;
schar src_str[1024];
uint32 g_cmdseq=0;
uint8 g_appkey[APPKEY_LEN] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
uint64 g_appeui = 0xfffffffffffffffe;
uint8 g_encrypt=0;
SSL_CTX *g_ctx = NULL;
SSL *g_ssl = NULL;
/*
* buffer按十六进制输出
*/
void hexdump(const unsigned char *buf, uint32 num)
{
uint32 i = 0;
for (; i < num; i++)
{
printf("%02X ", buf[i]);
if ((i+1)%8 == 0)
printf("\n");
}
printf("\n");
}
void join_cmd_proc(void)
{
schar para[256];
schar value[256];
st_cmd_join join;
uint8 senddata[MAX_DATA_LEN];
uint16 datalen=0;
uint16 sendlen=0;
/*
使用默认appeui或者获取键盘输入的appeui
*/
if (!get_para(para, sizeof(para)))
{
printf("Use default appeui[%lx] for 'join' command.\n", g_appeui);
join.appeui = g_appeui;
}
else
{
if (!strcasecmp(para, "appeui"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'appeui' para.\n", src_str);
return;
}
if (!set_eui(value, &join.appeui))
{
printf("Syntax error in command '%s' word 3.\n", src_str);
return;
}
}
else
{
printf("Syntax error in command '%s' word 2.\n", src_str);
return;
}
}
/*
使用默认appkey
*/
memcpy(join.appkey, g_appkey, APPKEY_LEN);
/*
设置命令序列号cmdseq
*/
g_cmdseq++;
join.cmdseq = g_cmdseq;
/*
构造JOIN命令并发送
*/
if (claa_cmd_join(join, senddata, &datalen))
{
if (g_encrypt)
{
sendlen = claa_ssl_send(g_ssl, (schar *)senddata, datalen);
}
else
{
sendlen = tcp_send(g_sockfd ,(const uint8 *)senddata, datalen);
}
if (datalen != sendlen)
{
printf("Send command FAIL.\n");
g_cmdseq--;
return;
}
printf("\nSend command SUCC : len:%s\n",senddata);
}
else
{
g_cmdseq--;
printf("Creat 'join' command Json error.\n");
}
return;
}
void quit_cmd_proc(void)
{
schar para[256];
schar value[256];
st_cmd_quit quit;
uint8 senddata[MAX_DATA_LEN];
uint16 datalen=0;
uint16 sendlen=0;
/*
使用默认appeui或者获取键盘输入的appeui
*/
if (!get_para(para, sizeof(para)))
{
printf("Use default appeui[%lx] for 'quit' command.\n", g_appeui);
quit.appeui = g_appeui;
}
else
{
if (!strcasecmp(para, "appeui"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'appeui' para.\n", src_str);
return;
}
if (!set_eui(value, &quit.appeui))
{
printf("Syntax error in command '%s' word 3.\n", src_str);
return;
}
}
else
{
printf("Syntax error in command '%s' word 2.\n", src_str);
return;
}
}
/*
设置命令序列号cmdseq
*/
g_cmdseq++;
quit.cmdseq = g_cmdseq;
/*
构造QUIT命令并发送
*/
if (claa_cmd_quit(quit, senddata, &datalen))
{
if (g_encrypt)
{
sendlen = claa_ssl_send(g_ssl, (schar *)senddata, datalen);
}
else
{
sendlen = tcp_send(g_sockfd ,(const uint8 *)senddata, datalen);
}
if (datalen != sendlen)
{
printf("Send command FAIL.\n");
g_cmdseq--;
return;
}
printf("\nSend command SUCC : len:%s\n",senddata);
}
else
{
g_cmdseq--;
}
printf("=========================\n");
printf("csdemo stop.\n");
printf("=========================\n");
exit(0);
return;
}
void sendto_cmd_proc(void)
{
schar para[256];
schar value[3 * MAX_DATA_LEN];
uint8 paranum=1;
uint8 cmdflag=0;
st_cmd_sendto sendto;
uint8 senddata[MAX_DATA_LEN];
uint16 datalen=0;
uint16 sendlen=0;
uint32 tmpport=0;
/*将appeui预置为默认appeui*/
sendto.appeui = g_appeui;
/*将port预置为0*/
sendto.port = 0;
for (;;)
{
if (!get_para(para, sizeof(para)))
{
if (cmdflag<3)
{
printf("Syntax error in command 'sendto', the command of 'sendto' lack of parameter.\n");
}
break;
}
else
{
paranum++;
/*
获取键盘输入的appeui
*/
if (!strcasecmp(para, "appeui"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'appeui' para.\n", src_str);
return;
}
paranum++;
if (!set_eui(value, &sendto.appeui))
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
}
/*
获取键盘输入的deveui
*/
else if (!strcasecmp(para, "deveui"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'deveui' para.\n", src_str);
return;
}
paranum++;
if (!set_eui(value, &sendto.deveui))
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
cmdflag++;
}
/*
获取键盘输入的cfm,输入值只能为0或1
*/
else if (!strcasecmp(para, "cfm"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'cfm' para.\n", src_str);
return;
}
paranum++;
if (!strcmp(value, "1"))
{
sendto.cfm = 1;
}
else if (!strcmp(value, "0"))
{
sendto.cfm = 0;
}
else
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
cmdflag++;
}
/*
获取键盘输入的payload数据,
payload用以":"分割的十六进制数字字符串表示每个字节的值
*/
else if (!strcasecmp(para, "payload"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'payload' para.\n", src_str);
return;
}
paranum++;
if (!set_payload(value, sendto.payload, &sendto.pydlen))
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
cmdflag++;
}
/*
获取键盘输入的port值,port值用十进制的字符串表示
*/
else if (!strcasecmp(para, "port"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'port' para.\n", src_str);
return;
}
paranum++;
tmpport = (uint32)atoi(value);
if ( ((tmpport < 21)||(223 < tmpport)) && (tmpport != 10) )
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
sendto.port = tmpport;
printf("tmpport %d.\n", tmpport);
}
}
}
/*
设置命令序列号cmdseq
*/
g_cmdseq++;
sendto.cmdseq = g_cmdseq;
/*
构造SENDTO命令并发送
*/
if (claa_cmd_sendto(sendto, senddata, &datalen))
{
if (g_encrypt)
{
sendlen = claa_ssl_send(g_ssl, (schar *)senddata, datalen);
}
else
{
sendlen = tcp_send(g_sockfd ,(const uint8 *)senddata, datalen);
}
if (datalen != sendlen)
{
printf("Send command FAIL.\n");
g_cmdseq--;
return;
}
printf("\nSend command SUCC : len:%s\n",senddata);
}
else
{
g_cmdseq--;
printf("Creat 'sendto' command Json error.\n");
}
return;
}
void set_cmd_proc(void)
{
schar para[256];
schar value[256];
uint8 cmdflag=0;
uint8 paranum=0;
for (;;)
{
if (!get_para(para, sizeof(para)))
{
if (!cmdflag)
{
printf("Syntax error in command '%s', the command of 'set' lack of parameter.\n", src_str);
}
return;
}
else
{
paranum++;
/*
获取键盘输入的appeui,并将输入值赋值给缺省的appeui
*/
if (!strcasecmp(para, "appeui"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'appeui' para.\n", src_str);
return;
}
paranum++;
if (!set_eui(value, &g_appeui))
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
cmdflag=1;
printf("Set appeui SUCC\n");
show_cmd_proc(APPEUI);
}
/*
获取键盘输入的appkey数据,并将其赋给缺省的appkey,
appkey用以":"分割的十六进制数字字符串表示每个字节的值,
appkey共16个字节,每个字节都需要赋值(值可以为0)
*/
else if (!strcasecmp(para, "appkey"))
{
if (!get_para(value, sizeof(value)))
{
printf("Syntax error in command '%s', can't find the value of 'appkey' para.\n", src_str);
return;
}
paranum++;
if (!set_appkey(value, g_appkey))
{
printf("Syntax error in command '%s' word %d.\n", src_str, paranum);
return;
}
cmdflag=1;
printf("Set appkey SUCC\n");
show_cmd_proc(APPKEY);
}
}
}
return;
}
void show_cmd_proc(uint8 type)
{
uint8 i;
uint8 *p;
uint8 endian_type;
/*
显示缺省的appeui
*/
if ((ALL == type)||(APPEUI == type))
{
endian_type = claa_check_endian();
if(endian_type)
{
p = (uint8 *)(&g_appeui) + sizeof(g_appeui) - 1;
}
else
{
p = (uint8 *)(&g_appeui);
}
printf("APPEUI : ");
for (i=0;i<sizeof(g_appeui);i++)
{
printf("%02lX", *p);
if (i <= (sizeof(g_appeui)-2))
{
printf("-");
}
if(endian_type)
{
p--;
}
else
{
p++;
}
}
printf("\n");
}
/*
显示缺省的appkey
*/
if ((ALL == type)||(APPKEY == type))
{
printf("APPKEY : ");
for (i=0;i<APPKEY_LEN;i++)
{
printf("%02lX", g_appkey[i]);
if (i <= (APPKEY_LEN-2))
{
printf(":");
}
}
printf("\n");
}
return;
}
void help_cmd_proc(void)
{
printf("The follows are the command examples.\n");
printf("\n");
printf("set appeui ff-ff-ff-ff-ff-ff-ff-fe\n");
printf("set appkey 11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00\n");
printf("join <appeui ff-ff-ff-ff-ff-ff-ff-fe>\n");
printf("quit <appeui ff-ff-ff-ff-ff-ff-ff-fe>\n");
printf("sendto <appeui ff-ff-ff-ff-ff-ff-ff-fe> deveui 00-00-00-00-01-02-0a-0b cfm 1 payload 80:12:1f:ab:28:09:21 <port 28>\n");
printf("show\n");
printf("help\n");
return;
}
/**********************************
*函数名称: set_eui
*函数功能: 配置appeui
*参数说明:
* 入参:
* 出参:
*返回值 :
**********************************/
uint8 set_eui(schar *euistr, uint64 *eui)
{
schar tmpeui[EUI_LEN+1];
uint16 i,j;
if (strlen(euistr) > EUI_STR_LEN)
{
return 0;
}
j = 0;
for (i=0;i<strlen(euistr);i++)
{
if (euistr[i] != '-')
{
tmpeui[j] = euistr[i];
j++;
}
}
if (j > EUI_LEN)
{
return 0;
}
tmpeui[j] = '\0';
if (!claa_hexstr_to_uint64(tmpeui, eui))
{
return 0;
}
return 1;
}
/**********************************
*函数名称: set_payload
*函数功能: 设置payload
*参数说明:
* 入参:
* 出参:
*返回值 :
**********************************/
uint8 set_payload(schar *payloadstr, uint8 *payload, uint16 *pydlen)
{
uint16 i,j=0;
uint8 tmp[3] = {0};
uint64 data;
uint16 len=0;
uint8 tmppyd[MAX_FRAME_LEN];
for (i=0; i<strlen(payloadstr); i++)
{
if (len >= MAX_FRAME_LEN)
{
return 0;
}
if (payloadstr[i] != ':')
{
if (j < sizeof(tmp))
{
tmp[j] = payloadstr[i];
j++;
}
else
{
return 0;
}
}
else
{
tmp[j] = '\0';
if (!claa_hexstr_to_uint64(tmp, &data))
{
return 0;
}
tmppyd[len] = (uint8)data;
len++;
j = 0;
tmp[0] = '\0';
}
}
if (strlen(tmp))
{
tmp[j] = '\0';
if (!claa_hexstr_to_uint64(tmp, &data))
{
return 0;
}
tmppyd[len] = (uint8)data;
len++;
}
memcpy(payload, tmppyd, len);
*pydlen = len;
return 1;
}
/**********************************
*函数名称: set_appkey
*函数功能: 设置appkey
*参数说明:
* 入参:
* 出参:
*返回值 :
**********************************/
uint8 set_appkey(schar *keystr, uint8 *appkey)
{
uint16 i,j=0;
uint8 tmp[3] = {0};
uint64 data;
uint16 len=0;
uint8 tmpkey[APPKEY_LEN];
for (i=0; i<strlen(keystr); i++)
{
if (len >= APPKEY_LEN)
{
return 0;
}
if (keystr[i] != ':')
{
if (j < sizeof(tmp))
{
tmp[j] = keystr[i];
j++;
}
else
{
return 0;
}
}
else
{
tmp[j] = '\0';
if (!claa_hexstr_to_uint64(tmp, &data))
{
return 0;
}
tmpkey[len] = (uint8)data;
len++;
j = 0;
tmp[0] = '\0';
}
}
if (strlen(tmp))
{
tmp[j] = '\0';
if (!claa_hexstr_to_uint64(tmp, &data))
{
return 0;
}
tmpkey[len] = (uint8)data;
len++;
}
if(APPKEY_LEN != len)
{
return 0;
}
memcpy(appkey, tmpkey, len);
return 1;
}
uint8 get_cmd(schar *srcstr, schar *outstr, uint16 len)
{
uint8 ret = 0;
schar *pstr = NULL;
schar delim[10] = " ";
pstr = strtok(srcstr, delim);
if ((NULL == pstr)||(strlen(pstr) >= len))
{
return 0;
}
strcpy(outstr, pstr);
outstr[CLAA_MIN(strlen(pstr),len-1)] = '\0';
// printf("CMD[%s]\n",outstr);
return 1;
}
uint8 get_para(schar *outstr, uint16 len)
{
uint8 ret = 0;
schar *pstr = NULL;
schar delim[10] = " ";
pstr = strtok(NULL, delim);
if ((NULL == pstr)||(strlen(pstr) >= len))
{
return 0;
}
strcpy(outstr, pstr);
outstr[CLAA_MIN(strlen(pstr),len-1)] = '\0';
// printf("para[%s]\n",outstr);
return 1;
}
void usage(void)
{
printf("\n");
printf("Usage:csdemo [options]\n");
printf("-h List help document\n");
printf("-i The ip of the edpacc\n");
printf("-p The port of the edpacc\n");
printf("-e Encrypt\n");
return;
}
//extern schar *optarg;
int main(int argc, char *argv[])
{
schar opt;
schar* ip = NULL;
schar* port = NULL;
pthread_t p_id;
schar cmdstr[256];
uint8 cmd_stat;
while ((opt = getopt(argc, argv, "hi:p:e")) != -1)
{
switch (opt){
case 'i':
strcpy(g_serip, optarg);
break;
case 'p':
g_serport = (uint16)(atoi(optarg));
break;
case 'e':
g_encrypt = 1;
break;
case 'h':
default:
usage();
exit(0);
}
}
if (!strlen(g_serip) || !g_serport)
{
usage();
return 0;
}
printf("\n");
/*是否采用ssl加密方式进行数据传输,如果是,则进行ssl的初始化*/
if (g_encrypt)
{
if (!claa_ssl_init(&g_ctx))
{
printf("\n");
printf("Init OpenSSL failed!\n");
exit(0);
}
}
/*创建socket并与MSP服务器建立TCP连接*/
g_sockfd = sock_open(g_serip, g_serport);
if (g_sockfd < 0)
{
printf("\n");
printf("Create a socket and connect to MSP server failed!\n");
exit(0);
}
/*如果采用ssl加密方式进行数据传输,则建立SSL连接*/
if (g_encrypt)
{
if (!claa_ssl_connect(&g_ssl, g_ctx, &g_sockfd))
{
printf("\n");
printf("SSL connection failed!\n");
exit(0);
}
}
if (0 != creat_tcp_recv_thread())
{
printf("\n");
printf("Create tcp receive thread failed!\n");
exit(0);
}
usleep(100*1000);
printf("csdemo is working now ...\n");
printf("=========================\n");
for (;;)
{
usleep(100*1000);
src_str[0] = '\0';
fgets(src_str, sizeof(src_str), stdin);
if (src_str[strlen(src_str)-1] == '\n')
{
src_str[strlen(src_str)-1] = '\0';
}
if ((strlen(src_str))&&('\n' != src_str[0]))
{
printf("-------------------------\n");
if (!get_cmd(src_str, cmdstr, sizeof(cmdstr)))
{
continue;
}
else
{
if (!strcasecmp(cmdstr, "JOIN"))
{
cmd_stat = JOIN;
}
else if (!strcasecmp(cmdstr, "QUIT"))
{
cmd_stat = QUIT;
}
else if (!strcasecmp(cmdstr, "SENDTO"))
{
cmd_stat = SENDTO;
}
else if (!strcasecmp(cmdstr, "SET"))
{
cmd_stat = SET;
}
else if (!strcasecmp(cmdstr, "SHOW"))
{
cmd_stat = SHOW;
}
else if (!strcasecmp(cmdstr, "HELP"))
{
cmd_stat = HELP;
}
else if (!strcasecmp(cmdstr, "QUIT"))
{
break;
}
else
{
printf("Unknow command!\n",cmdstr);
printf("=========================\n");
continue;
}
}
switch (cmd_stat)
{
case JOIN:
join_cmd_proc();
break;
case QUIT:
quit_cmd_proc();
break;
case SENDTO:
sendto_cmd_proc();
break;
case SET:
set_cmd_proc();
break;
case SHOW:
show_cmd_proc(ALL);
break;
case HELP:
help_cmd_proc();
break;
}
printf("=========================\n");
}
}
if (g_encrypt)
{
claa_ssl_close(&g_ssl, &g_ctx);
}
sock_close(&g_sockfd);
printf("Close tcp socket and quit csdemo process!\n");
return 0;
}
sint32 creat_tcp_recv_thread(void)
{
pthread_t p_id;
uint32 stack_size = 1024 * 1024;
pthread_attr_t threadAttr;
pthread_attr_init(&threadAttr);
/* Set the stack size of the thread */
pthread_attr_setstacksize(&threadAttr, stack_size);
/* Set thread to detached state. No need for pthread_join */
pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);
return pthread_create(&p_id, &threadAttr, (void *(*) (void *))tcp_recv_thread_func, NULL);
}
void tcp_recv_thread_func(void)
{
sint32 n;
uint8 recv_buf[BUFFER_SIZE];
st_tcp_buffer tcp_buf;
sint32 json_pos;
uint8 json[MAX_MSG_BUFFER_SIZE];
uint8 msgtype;
st_updata updata;
st_ack ack;
printf("TCP recv thread start ...\n");
for (;;)
{
if (g_sockfd >= 0)
{
claa_set_tcp_buffer(&tcp_buf);
for (;;)
{
/*每次接收1024个字节的数据 */
if (g_encrypt)
{
n = claa_ssl_recv(g_ssl, recv_buf, BUFFER_SIZE);
}
else
{
n = Recv(g_sockfd, recv_buf, BUFFER_SIZE, MSG_NOSIGNAL);
}