-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcma_commu.c
1757 lines (1461 loc) · 41.1 KB
/
cma_commu.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <pthread.h>
#include "device.h"
#include "cma_commu.h"
#include "types.h"
#include "socket_lib.h"
#include "sensor_ops.h"
#include "rtc_alarm.h"
#include "file_ops.h"
extern pthread_mutex_t sndMutex;
extern pthread_mutex_t imgMutex;
typedef struct __reponse_data {
byte msg_type;
int res;
}Response_data;
static Response_data resData[16];
static volatile int upgradeLostFlag = 0;
byte imageRbuf[MAX_COMBUF_SIZE];
static volatile int imageRcvLen = 0;
//#define _DEBUG
#ifdef _DEBUG
static void print_message(byte *buf, int len)
{
int i;
logcat("");
for(i = 0; i<len; i++) {
logcat_raw("0x%02x ", buf[i]);
if (((i+1)%16) == 0) {
logcat_raw("\n");
logcat("");
}
}
logcat_raw("\n");
}
#endif
int Commu_GetPacket_Udp(int fd, byte *rbuf, int len, int timeout)
{
int ret = 0;
usint crc16 = 0;
usint size;
if ((rbuf == NULL)) {
logcat("Commu_GetPacket: Invalid parameter.\n");
return -1;
}
// logcat("UDP Socket Begin to receive msg, len = %d\n", len);
memset(rbuf, 0, len);
if (fd == -1) {
return -1;
}
else {
ret = socket_recv(fd, rbuf, len, timeout, 1);
if (ret < 0)
return ret;
}
memcpy(&size, (rbuf + 2), 2);
logcat("size = %d, ret = %d \n", size, ret);
if ((size < 0) || (ret < (size + sizeof(frame_head_t) + 2)))
return -1;
if ((rbuf[0] != 0xA5) && (rbuf[1] != 0x5A)) {
logcat("Invalid package head.\n");
return -1;
}
memcpy(&crc16, (rbuf + size + sizeof(frame_head_t)), 2);
if (crc16 != RTU_CRC(rbuf, (size + sizeof(frame_head_t)))) {
logcat("Data packagre crc error!\n");
return -1;
}
#ifdef _DEBUG
logcat("Receive MSG, len = %d:\n", ret);
print_message(rbuf, ret);
#endif
return ret;
}
int Commu_GetPacket(int fd, byte *rbuf, int len, int timeout)
{
int ret = 0;
usint crc16 = 0;
usint size;
if ((rbuf == NULL)) {
logcat("Commu_GetPacket: Invalid parameter.\n");
return -1;
}
if (len < (sizeof(frame_head_t) + 3))
return -1;
memset(rbuf, 0, len);
if (CMA_Env_Parameter.s_protocal == 1)
return Commu_GetPacket_Udp(fd, rbuf, len, timeout);
// logcat("Begin to receive msg, len = %d\n", len);
ret = socket_recv(fd, rbuf, sizeof(frame_head_t), timeout, 0);
if (ret < 0)
return ret;
memcpy(&size, (rbuf + 2), 2);
// logcat("size = %d \n", size);
if ((size < 0) || (ret != sizeof(frame_head_t))) {
return -1;
}
if ((rbuf[0] != 0xA5) && (rbuf[1] != 0x5A)) {
logcat("Invalid package head.\n");
return -1;
}
ret = socket_recv(fd, (rbuf + sizeof(frame_head_t)), (size + 2), timeout, 0);
if (ret < 0)
return ret;
if (ret != (size + 2))
return -1;
memcpy(&crc16, (rbuf + size + sizeof(frame_head_t)), 2);
if (crc16 != RTU_CRC(rbuf, (size + sizeof(frame_head_t)))) {
logcat("Data packagre crc error!\n");
return -1;
}
#ifdef _DEBUG
logcat("Receive MSG, len = %d:\n", (size + 25));
print_message(rbuf, (size + 25));
#endif
return (size + 25);
}
int Commu_SendPacket(int fd, frame_head_t *head, byte *data)
{
int ret = 0;
byte sbuf[MAX_COMBUF_SIZE];
int timeout = 5;
usint crc16 = 0;
int size = 0;
if ((head == NULL) || (data == NULL)) {
logcat("Commu_SendPacket: Invalid parameter.\n");
return -1;
}
memset(sbuf, 0, MAX_COMBUF_SIZE);
memcpy(sbuf, head, sizeof(frame_head_t));
size += sizeof(frame_head_t);
memcpy((sbuf + size), data, head->pack_len);
size += head->pack_len;
crc16 = RTU_CRC(sbuf, size);
memcpy((sbuf + size), &crc16, 2);
size += 2;
#ifdef _DEBUG
logcat("Send MSG, len = %d:\n", size);
print_message(sbuf, size);
#endif
if (fd == -1) {
// ret = socket_send_udp(CMA_Env_Parameter.cma_ip, CMA_Env_Parameter.cma_port, sbuf, size);
return -1;
}
pthread_mutex_lock(&sndMutex);
ret = socket_send(fd, sbuf, size, timeout);
pthread_mutex_unlock(&sndMutex);
logcat("Socket Send Data: ret = %d, size = %d\n", ret, size);
if (ret <= 0) {
logcat("Send Package: send error.\n");
return -1;
}
return 0;
}
static int CMD_GetMsgTypeIndex(byte type)
{
int index = 0;
switch(type) {
case CMA_MSG_TYPE_DATA_QXENV:
index = 1;
break;
case CMA_MSG_TYPE_DATA_TGQXIE:
index = 2;
break;
case CMA_MSG_TYPE_DATA_DDXWFTZ:
index = 3;
break;
case CMA_MSG_TYPE_DATA_DDXWFBX:
index = 4;
break;
case CMA_MSG_TYPE_DATA_DXHCH:
index = 5;
break;
case CMA_MSG_TYPE_DATA_DXWD:
index = 6;
break;
case CMA_MSG_TYPE_DATA_FUBING:
index = 7;
break;
case CMA_MSG_TYPE_DATA_DXFP:
index = 8;
break;
case CMA_MSG_TYPE_DATA_DXWDTZH:
index = 9;
break;
case CMA_MSG_TYPE_DATA_DXWDGJ:
index = 10;
break;
case CMA_MSG_TYPE_DATA_XCHWS:
index = 11;
break;
case CMA_MSG_TYPE_STATUS_INFO:
index = 12;
break;
case CMA_MSG_TYPE_STATUS_WORK:
index = 13;
break;
case CMA_MSG_TYPE_STATUS_ERROR:
index = 14;
break;
case CMA_MSG_TYPE_STATUS_HEART:
index = 15;
break;
default:
logcat("Invalid Sensor tyep.\n");
break;
}
return index;
}
static int CMD_WaitStatus_Res(byte msg_type, int timeout)
{
while ((resData[CMD_GetMsgTypeIndex(msg_type)].res == -1) && timeout) {
sleep(1);
timeout--;
}
if (timeout == 0)
return -1;
else
return resData[CMD_GetMsgTypeIndex(msg_type)].res;
}
int CMA_Server_Process(int fd, byte *rbuf)
{
frame_head_t *f_head = (frame_head_t *)rbuf;
byte frame_type = f_head->frame_type;
byte msg_type = f_head->msg_type;
byte id[18] = {0};
byte status = 0;
int ret = 0;
logcat("Enter func: %s \n", __func__);
memcpy(id, f_head->id, 17);
logcat("CMD: Receive Message, id = %s, frame type = 0x%x, msg type = 0x%x\n",
id, f_head->frame_type, f_head->msg_type);
if (CMA_MSG_TYPE_CTL_DEV_ID != msg_type) {
if (memcmp(f_head->id, CMA_Env_Parameter.id, 17) != 0) {
logcat("Device ID: %s, MSG ID: %s, Miss Match.\n", f_head->id, CMA_Env_Parameter.id);
return -1;
}
}
if (frame_type == CMA_FRAME_TYPE_CONTROL) {
switch (msg_type) {
case CMA_MSG_TYPE_CTL_TIME_CS:
if (CMA_Time_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_CY_PAR:
if (CMA_SamplePar_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_TIME_AD:
if (CMA_NetAdapter_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_MODEL_PAR:
if (CMA_ModelPar_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_ALARM:
if (CMA_Alarm_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_TOCMA_INFO:
if (CMA_UpDevice_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_DEV_ID:
if (CMA_DeviceId_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_BASIC_INFO:
if (CMA_BasicInfo_SetReq_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_DEV_RESET:
if (CMA_DeviceRset_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_REQ_DATA:
if (CMA_RequestData_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_UPGRADE_DATA:
case CMA_MSG_TYPE_CTL_UPGRADE_END:
if (CMA_SoftWare_Update_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_CTL_DEV_WAKE:
if (CMA_WakeupTime_Response(fd, rbuf) < 0)
ret = -1;
break;
default:
ret = -1;
logcat("CMA: Invalid MSG type.\n");
}
}
else if (frame_type == CMA_FRAME_TYPE_IMAGE_CTRL) {
switch (msg_type) {
case CMA_MSG_TYPE_IMAGE_VIDEO_SET:
if (CMA_Video_StopStart_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_IMAGE_CAP_MANUAL:
if (CMA_ManualCapture_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_IMAGE_CAP_TIME:
if (CMA_CaptureTimetable_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_IMAGE_CAP_PAR:
if (CMA_SetImagePar_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_IMAGE_CAM_ADJ:
if (CMA_CameraControl_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_IMAGE_DATA_REP:
pthread_mutex_lock(&imgMutex);
memcpy(imageRbuf, rbuf, MAX_COMBUF_SIZE);
imageRcvLen = f_head->pack_len;
pthread_mutex_unlock(&imgMutex);
break;
case CMA_MSG_TYPE_IMAGE_GET_PAR:
if (CMA_GetImagePar_Response(fd, rbuf) < 0)
ret = -1;
break;
case CMA_MSG_TYPE_IMAGE_GET_TIME:
if (CMA_GetImageTimeTable_Response(fd, rbuf) < 0)
ret = -1;
break;
default:
ret = -1;
logcat("CMA: Invalid MSG type.\n");
}
}
else if (frame_type == CMA_FRAME_TYPE_IMAGE) {
pthread_mutex_lock(&imgMutex);
memcpy(imageRbuf, rbuf, MAX_COMBUF_SIZE);
imageRcvLen = f_head->pack_len;
pthread_mutex_unlock(&imgMutex);
}
else if ((frame_type == CMA_FRAME_TYPE_DATA_RES) || (frame_type == CMA_FRAME_TYPE_STATUS_RES)) {
status = *(rbuf + sizeof(frame_head_t));
logcat("CMD: Receive Send data response 0x%x.\n", status);
resData[CMD_GetMsgTypeIndex(msg_type)].msg_type = msg_type;
resData[CMD_GetMsgTypeIndex(msg_type)].res = status;
}
else if (msg_type == CMA_MSG_TYPE_STATUS_HEART) {
logcat("CMD: Receive Hearbeat response.\n");
resData[CMD_GetMsgTypeIndex(msg_type)].msg_type = msg_type;
resData[CMD_GetMsgTypeIndex(msg_type)].res = 0xff;
}
return ret;
}
int CMA_Send_SensorData(int fd, int type, void *data, int waitRes)
{
char *id = CMA_Env_Parameter.id;
frame_head_t f_head;
byte data_buf[MAX_DATA_BUFSIZE];
logcat("CMA Send Sensor Data.\n");
memset(&f_head, 0, sizeof(frame_head_t));
f_head.head = 0x5aa5;
f_head.frame_type = CMA_FRAME_TYPE_DATA;
memcpy(f_head.id, id, 17);
memset(data_buf, 0, MAX_DATA_BUFSIZE);
switch (type) {
case CMA_MSG_TYPE_DATA_QXENV:
case CMA_MSG_TYPE_CTL_QX_PAR:
f_head.pack_len = sizeof(Data_qixiang_t);
f_head.msg_type = CMA_MSG_TYPE_DATA_QXENV;
break;
case CMA_MSG_TYPE_DATA_TGQXIE:
case CMA_MSG_TYPE_CTL_TGQX_PAR:
f_head.pack_len = sizeof(Data_incline_t);
f_head.msg_type = CMA_MSG_TYPE_DATA_TGQXIE;
break;
case CMA_MSG_TYPE_DATA_DDXWFTZ:
f_head.pack_len = sizeof(Data_vibration_f_t);
break;
case CMA_MSG_TYPE_DATA_DDXWFBX:
// f_head.pack_len = sizeof(Data_vibration_w_t); // ? sample number: n
break;
case CMA_MSG_TYPE_DATA_DXHCH:
f_head.pack_len = sizeof(Data_conductor_sag_t);
break;
case CMA_MSG_TYPE_DATA_DXWD:
f_head.pack_len = sizeof(Data_line_temperature_t);
break;
case CMA_MSG_TYPE_DATA_FUBING:
case CMA_MSG_TYPE_CTL_FUBING_PAR:
f_head.pack_len = sizeof(Data_ice_thickness_t);
f_head.msg_type = CMA_MSG_TYPE_DATA_FUBING;
break;
case CMA_MSG_TYPE_DATA_DXFP:
f_head.pack_len = sizeof(Data_windage_yaw_t);
break;
case CMA_MSG_TYPE_DATA_DXWDTZH:
f_head.pack_len = sizeof(Data_line_gallop_f_t);
break;
case CMA_MSG_TYPE_DATA_DXWDGJ:
f_head.pack_len = sizeof(Data_gallop_w_t);
break;
case CMA_MSG_TYPE_DATA_XCHWS:
// f_head.pack_len = sizeof(Data_dirty_t); // ? sample num: n
break;
default:
logcat("Invalid Sensor tyep.\n");
break;
}
if (Commu_SendPacket(fd, &f_head, (byte *)data) < 0) {
logcat("CMD: Socket Send Data error.\n");
return -1;
}
if (waitRes) {
resData[CMD_GetMsgTypeIndex(type)].res = -1;
return CMD_WaitStatus_Res(type, 30);
}
else
return 0;
}
int CMA_Check_Send_SensorData(int fd, int type)
{
byte record[256];
int total = 0;
int record_len;
char *filename = NULL;
int i, flag = 0;
time_t now, t;
int ret = 0;
switch (type) {
case CMA_MSG_TYPE_DATA_QXENV:
case CMA_MSG_TYPE_CTL_QX_PAR:
filename = RECORD_FILE_QIXIANG;
record_len = sizeof(struct record_qixiang);
break;
case CMA_MSG_TYPE_DATA_TGQXIE:
case CMA_MSG_TYPE_CTL_TGQX_PAR:
filename = RECORD_FILE_TGQXIE;
record_len = sizeof(struct record_incline);
break;
case CMA_MSG_TYPE_DATA_FUBING:
case CMA_MSG_TYPE_CTL_FUBING_PAR:
filename = RECORD_FILE_FUBING;
record_len = sizeof(struct record_fubing);
break;
case CMA_MSG_TYPE_DATA_DDXWFTZ:
// break;
case CMA_MSG_TYPE_DATA_DDXWFBX:
// break;
case CMA_MSG_TYPE_DATA_DXHCH:
// break;
case CMA_MSG_TYPE_DATA_DXWD:
// break;
case CMA_MSG_TYPE_DATA_DXFP:
case CMA_MSG_TYPE_CTL_DXFP_PAR:
// break;
case CMA_MSG_TYPE_DATA_DXWDTZH:
// break;
case CMA_MSG_TYPE_DATA_DXWDGJ:
// break;
case CMA_MSG_TYPE_DATA_XCHWS:
// break;
default:
logcat("Invalid Sensor tyep.\n");
return -1;
}
total = File_GetNumberOfRecords(filename, record_len);
now = rtc_get_time();
while (total > 0) {
memset(&record, 0, record_len);
memset(&t, 0, sizeof(time_t));
if (File_GetRecordByIndex(filename, &record, record_len, 0) == record_len) {
memcpy(&t, &record, sizeof(time_t));
if ((now - t) > 50*24*60*60) {
File_DeleteRecordByIndex(filename, record_len, 0);
total = total -1;
}
else
break;
}
else
break;
}
if (total == 0) {
return 0;
}
for (i = (total - 1); i >= 0; i--) {
// logcat("total = %d, i = %d\n", total, i);
memset(&record, 0, record_len);
if (File_GetRecordByIndex(filename, &record, record_len, i) == record_len) {
memcpy(&flag, ((byte *)&record + (record_len - 4)), 4);
memcpy(&t, &record, sizeof(time_t));
if (flag == 0) {
logcat("CMD Send Data: filename = %s, record_len = %d, i = %d, total = %d\n", filename, record_len, i, total);
logcat("CMD Send Data: flag = %d, time: %s", flag, ctime(&t));
ret = CMA_Send_SensorData(fd, type, (record + sizeof(time_t)), 1);
if (ret < 0)
continue;
else if (ret == 0xff) {
logcat("CMD: Send Data reponse OK.\n");
flag = 0xff;
memcpy(((byte *)&record + record_len - 4), &flag, 4);
File_UpdateRecordByIndex(filename, &record, record_len, i);
system("sync");
/*
File_GetRecordByIndex(filename, &record, record_len, i);
if (type == CMA_MSG_TYPE_DATA_TGQXIE) {
struct record_incline *p = (struct record_incline *)&record;
logcat("After Send flag = %d \n", p->send_flag);
}
*/
}
}
// else
// break;
}
}
return 0;
}
int CMA_Time_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
byte config_type = *(rbuf + sizeof(frame_head_t));
time_t cur_time;
int Clocktime_Stamp;
byte sbuf[MAX_DATA_BUFSIZE];
struct timeval tv;
struct timezone tz;
memcpy(&cur_time, (rbuf + sizeof(frame_head_t) + 1), sizeof(int));
if (config_type == 0x01) {
gettimeofday (&tv , &tz);
// logcat("Now time: %d, %s \n", (int)tv.tv_sec, asctime(gmtime(&tv.tv_sec)));
tv.tv_sec = mktime(gmtime(&cur_time));
// logcat("Set time: %d, %s \n", (int)tv.tv_sec, asctime(gmtime(&cur_time)));
if (settimeofday(&tv, &tz) < 0)
logcat("CMA: Set time error.\n");
rtc_set_time(gmtime(&cur_time));
}
cur_time = time((time_t*)NULL);
Clocktime_Stamp = mktime_k(localtime(&cur_time));
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = 5;
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
memcpy((sbuf + 1), &Clocktime_Stamp, sizeof(int));
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
return 0;
}
int CMA_NetAdapter_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
byte config_type = *(rbuf + sizeof(frame_head_t));
Ctl_net_adap_t *adap = (Ctl_net_adap_t *)(rbuf + sizeof(frame_head_t) + 1);
byte sbuf[MAX_DATA_BUFSIZE];
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
if (config_type == 0x00) {
Device_getNet_info(adap);
}
else if (config_type == 0x01) {
Device_setNet_info(adap);
}
else
sbuf[0] = 0x00;
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = sizeof(Ctl_net_adap_t) + 1;
memcpy((sbuf + 1), adap, sizeof(Ctl_net_adap_t));
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
return 0;
}
static int Request_data_type[] = {
CMA_MSG_TYPE_DATA_QXENV,
CMA_MSG_TYPE_DATA_TGQXIE,
CMA_MSG_TYPE_DATA_DDXWFTZ,
CMA_MSG_TYPE_DATA_DDXWFBX,
CMA_MSG_TYPE_DATA_DXHCH,
CMA_MSG_TYPE_DATA_DXWD,
CMA_MSG_TYPE_DATA_FUBING,
CMA_MSG_TYPE_DATA_DXFP,
CMA_MSG_TYPE_DATA_DXWDTZH,
CMA_MSG_TYPE_DATA_DXWDGJ,
CMA_MSG_TYPE_DATA_XCHWS,
};
static int CMA_Send_RecordingData(int fd, byte type, time_t start, time_t end)
{
byte record[256];
int total = 0;
int record_len;
char *filename = NULL;
int i;
time_t t_max, t_min, t;
switch (type) {
case CMA_MSG_TYPE_DATA_QXENV:
case CMA_MSG_TYPE_CTL_QX_PAR:
filename = RECORD_FILE_QIXIANG;
record_len = sizeof(struct record_qixiang);
break;
case CMA_MSG_TYPE_DATA_TGQXIE:
case CMA_MSG_TYPE_CTL_TGQX_PAR:
filename = RECORD_FILE_TGQXIE;
record_len = sizeof(struct record_incline);
break;
case CMA_MSG_TYPE_DATA_FUBING:
case CMA_MSG_TYPE_CTL_FUBING_PAR:
filename = RECORD_FILE_FUBING;
record_len = sizeof(struct record_fubing);
break;
case CMA_MSG_TYPE_DATA_DDXWFTZ:
// break;
case CMA_MSG_TYPE_DATA_DDXWFBX:
// break;
case CMA_MSG_TYPE_DATA_DXHCH:
// break;
case CMA_MSG_TYPE_DATA_DXWD:
// break;
case CMA_MSG_TYPE_DATA_DXFP:
case CMA_MSG_TYPE_CTL_DXFP_PAR:
// break;
case CMA_MSG_TYPE_DATA_DXWDTZH:
// break;
case CMA_MSG_TYPE_DATA_DXWDGJ:
// break;
case CMA_MSG_TYPE_DATA_XCHWS:
// break;
default:
logcat("Invalid Sensor tyep.\n");
return -1;
}
total = File_GetNumberOfRecords(filename, record_len);
if (total == 0) {
return 0;
}
if (start == end) {
memset(&record, 0, record_len);
if (File_GetRecordByIndex(filename, &record, record_len, (total - 1)) == record_len) {
CMA_Send_SensorData(fd, type, (record + sizeof(time_t)), 0);
usleep(100 * 1000);
}
return 0;
}
if (start > end) {
t_max = start;
t_min = end;
}
else {
t_max = end;
t_min = start;
}
for (i = 0; i < total; i++) {
memset(&record, 0, record_len);
memset(&t, 0, sizeof(time_t));
if (File_GetRecordByIndex(filename, &record, record_len, i) == record_len) {
memcpy(&t, record, sizeof(time_t));
if ((t <= t_max) && (t >= t_min)) {
CMA_Send_SensorData(fd, type, (record + sizeof(time_t)), 0);
usleep(100 * 1000);
}
}
}
return 0;
}
int CMA_RequestData_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
byte req_type;
byte sbuf[MAX_DATA_BUFSIZE];
int time_start = 0, time_end = 0;
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
req_type = *(rbuf + sizeof(frame_head_t));
memcpy(&time_start, (rbuf + sizeof(frame_head_t) + 1), sizeof(int));
memcpy(&time_end, (rbuf + sizeof(frame_head_t) + 1 + sizeof(int)), sizeof(int));
logcat("Request Data from: %s", ctime((time_t *)&time_start));
logcat("to: %s", ctime((time_t *)&time_end));
/*
* Response Process
*/
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = 2;
*(sbuf) = 0xff;
*(sbuf + 1) = req_type;
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
if (req_type == 0xff) {
logcat("CMD: Send all Sensor Data.\n");
int i;
int num = sizeof(Request_data_type) / sizeof(int);
for (i = 0; i < num; i++) {
req_type = Request_data_type[i];
CMA_Send_RecordingData(fd, req_type, time_start, time_end);
}
}
else
CMA_Send_RecordingData(fd, req_type, time_start, time_end);
return 0;
}
int CMA_SamplePar_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
Ctl_sample_par_t *sample_par = (Ctl_sample_par_t *)(rbuf + sizeof(frame_head_t) + 1);
byte sbuf[MAX_DATA_BUFSIZE];
byte set_type = *(rbuf + sizeof(frame_head_t));
usint cycle = 0;
logcat("Enter func: %s\n", __func__);
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
if (set_type == 0x00) {
/* Get Sample Parameter from Sensor */
switch (sample_par->Request_Type) {
case CMA_MSG_TYPE_CTL_QX_PAR:
cycle = Device_getSampling_Cycle("qixiang:samp_period");
if (cycle > 0)
sample_par->Main_Time = cycle;
break;
case CMA_MSG_TYPE_CTL_TGQX_PAR:
cycle = Device_getSampling_Cycle("tgqingxie:samp_period");
if (cycle > 0)
sample_par->Main_Time = cycle;
break;
case CMA_MSG_TYPE_CTL_FUBING_PAR:
cycle = Device_getSampling_Cycle("fubing:samp_period");
if (cycle > 0)
sample_par->Main_Time = cycle;
break;
default:
sbuf[0] = 0x00;
return -1;
}
}
else if (set_type == 0x01) {
; /* Set Sample Parameter from Sensor */
switch (sample_par->Request_Type) {
case CMA_MSG_TYPE_CTL_QX_PAR:
cycle = sample_par->Main_Time;
if (cycle > 0)
Device_setSampling_Cycle("qixiang:samp_period", cycle);
sample_dev.interval = cycle * 60;
break;
case CMA_MSG_TYPE_CTL_TGQX_PAR:
cycle = sample_par->Main_Time;
if (cycle > 0)
Device_setSampling_Cycle("tgqingxie:samp_period", cycle);
sample_dev_1.interval = cycle * 60;
break;
case CMA_MSG_TYPE_CTL_FUBING_PAR:
cycle = sample_par->Main_Time;
if (cycle > 0)
Device_setSampling_Cycle("fubing:samp_period", cycle);
sample_dev_2.interval = cycle * 60;
break;
default:
sbuf[0] = 0x00;
return -1;
}
}
else
sbuf[0] = 0x00;
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = sizeof(Ctl_sample_par_t) + 1;
memcpy((sbuf + 1), sample_par, sizeof(Ctl_sample_par_t));
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
return 0;
}
int CMA_ModelPar_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
byte sbuf[MAX_DATA_BUFSIZE];
byte set_type = *(rbuf + sizeof(frame_head_t));
byte req_type = *(rbuf + sizeof(frame_head_t) + 1);
byte config_num = *(rbuf + sizeof(frame_head_t) + 2);
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
if (set_type == 0x00) {
; /* Get Model Parameter from Sensor, return config_num and Model parameter*/
}
else if (set_type == 0x01) {
; /* Set Model Parameter from Sensor */
}
else
sbuf[0] = 0x00;
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = 3 + config_num * 10;
sbuf[1] = req_type;
sbuf[2] = config_num;
// memcpy((sbuf + 3), (rbuf + sizeof(frame_head_t) + 3), config_num * 10);
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
return 0;
}
int CMA_Alarm_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
byte sbuf[MAX_DATA_BUFSIZE];
byte set_type = *(rbuf + sizeof(frame_head_t));
byte req_type = *(rbuf + sizeof(frame_head_t) + 1);
byte config_num = *(rbuf + sizeof(frame_head_t) + 2);
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
if (set_type == 0x00) {
/* Get Sensor Alarm Setting from Sensor, return config_num and Alarm value*/
Device_GetAlarm_Threshold(req_type, (sbuf + 3), &config_num);
}
else if (set_type == 0x01) {
; /* Set Alarm Value to Sensor */
Device_SetAlarm_Threshold(req_type, (rbuf + sizeof(frame_head_t) + 3), config_num);
}
else
sbuf[0] = 0x00;
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = 3 + config_num * 10;
sbuf[1] = req_type;
sbuf[2] = config_num;
if (set_type == 0x01)
memcpy((sbuf + 3), (rbuf + sizeof(frame_head_t) + 3), config_num * 10);
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
return 0;
}
int CMA_UpDevice_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
Ctl_up_device_t *up_device = (Ctl_up_device_t *)(rbuf + sizeof(frame_head_t) + 1);
byte sbuf[MAX_DATA_BUFSIZE];
byte set_type = *(rbuf + sizeof(frame_head_t));
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
if (set_type == 0x00) {
/* Get up device info, ip addr and port */
if (Device_getServerInfo(up_device) < 0)
sbuf[0] = 0x00;
}
else if (set_type == 0x01) {
Device_setServerInfo(up_device); /* Set up device info, ip addr and port */
}
else
sbuf[0] = 0x00;
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = sizeof(Ctl_up_device_t) + 1;
memcpy((sbuf + 1), up_device, sizeof(Ctl_up_device_t));
if (Commu_SendPacket(fd, p_head, sbuf) < 0)
return -1;
return 0;
}
int CMA_BasicInfo_SetReq_Response(int fd, byte *rbuf)
{
frame_head_t *p_head = (frame_head_t *)rbuf;
byte sbuf[MAX_DATA_BUFSIZE];
byte set_type = *(rbuf + sizeof(frame_head_t));
byte req_type = *(rbuf + sizeof(frame_head_t) + 1);
byte msg_type = *(rbuf + sizeof(frame_head_t) + 2);
memset(sbuf, 0, MAX_DATA_BUFSIZE);
sbuf[0] = 0xff;
if (set_type == 0x00) {
/* Get Sensor basic information */
}
else if (set_type == 0x01) {
; /* Set Basic information to Sensor */
}
else
sbuf[0] = 0x00;
p_head->frame_type = CMA_FRAME_TYPE_CONTROL_RES;
p_head->pack_len = 3;
sbuf[1] = req_type;
sbuf[2] = msg_type;