-
Notifications
You must be signed in to change notification settings - Fork 2
/
ieee1888_ilonss_gw.c
1445 lines (1202 loc) · 47.4 KB
/
ieee1888_ilonss_gw.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
/*
* IEEE1888 - iLon/IP GW
*
* author: Hideya Ochiai
* create: 2012-10-03
* update: 2012-12-06 from Dallas to Tokyo
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "ilonss.h"
#include "ieee1888.h"
#include "ieee1888_datapool.h"
#define IEEE1888_ILONSS_POINTID_LEN 256
#define IEEE1888_ILONSS_VALUE_LEN 1024
#define IEEE1888_ILONSS_TIME_LEN 32
#define IEEE1888_ILONSS_POINT_COUNT 1024
#define IEEE1888_ILONSS_HOSTNAME_LEN 64
#define IEEE1888_ILONSS_BULK_SESSION_TIMEOUT 15
// access methods and access permission
#define IEEE1888_ILONSS_ACCESS_NONE 0
#define IEEE1888_ILONSS_ACCESS_READ 1
#define IEEE1888_ILONSS_ACCESS_WRITE 2
#define IEEE1888_ILONSS_ACCESS_READWRITE 3
#define IEEE1888_ILONSS_OK 0
#define IEEE1888_ILONSS_ERROR 1
// log level
#define IEEE1888_ILONSS_LOGLEVEL_DEBUG 1
#define IEEE1888_ILONSS_LOGLEVEL_INFO 2
#define IEEE1888_ILONSS_LOGLEVEL_WARN 3
#define IEEE1888_ILONSS_LOGLEVEL_ERROR 4
struct ilonssGW_baseConfig {
char point_id[IEEE1888_ILONSS_POINTID_LEN];
char host[IEEE1888_ILONSS_HOSTNAME_LEN];
unsigned short port;
char object_id[1024];
char data_type[1024];
int priority;
uint8_t permission;
int8_t exp;
time_t status_time;
char status_value[IEEE1888_ILONSS_VALUE_LEN];
};
struct ilonssGW_baseConfig m_config[IEEE1888_ILONSS_POINT_COUNT];
int n_m_config=0;
pthread_t __ilonssGW_writeClient_thread;
void* ilonssGW_writeClient_thread(void* args);
pthread_t __ilonssGW_fetchClient_thread;
void* ilonssGW_fetchClient_thread(void* args);
char m_writeServer_ids[IEEE1888_ILONSS_POINT_COUNT][IEEE1888_ILONSS_POINTID_LEN];
char m_fetchServer_ids[IEEE1888_ILONSS_POINT_COUNT][IEEE1888_ILONSS_POINTID_LEN];
char m_writeClient_ids[IEEE1888_ILONSS_POINT_COUNT][IEEE1888_ILONSS_POINTID_LEN];
char m_fetchClient_ids[IEEE1888_ILONSS_POINT_COUNT][IEEE1888_ILONSS_POINTID_LEN];
int n_m_writeServer_ids=0;
int n_m_fetchServer_ids=0;
int n_m_writeClient_ids=0;
int n_m_fetchClient_ids=0;
#define IEEE1888_SERVER_URL_LEN 256
char m_writeClient_ieee1888_server_url[IEEE1888_SERVER_URL_LEN];
int m_writeClient_trigger_frequency;
int m_writeClient_trigger_offset;
char m_fetchClient_ieee1888_server_url[IEEE1888_SERVER_URL_LEN];
int m_fetchClient_trigger_frequency;
int m_fetchClient_trigger_offset;
pthread_t __ilonssGW_printStatus_thread;
void* ilonssGW_printStatus_thread(void* args);
char m_printStatus_filepath[256];
int m_datapool_timespan;
void ilonssGW_log(const char* logMessage, int logLevel);
ieee1888_error* ilonssGW_pointsTest(char ids[][IEEE1888_ILONSS_POINTID_LEN], uint8_t access[], int n_point){
int i,j;
for(i=0;i<n_point;i++){
for(j=0;j<n_m_config;j++){
if(strcmp(ids[i],m_config[j].point_id)==0){
if((access[i]&m_config[j].permission)!=access[i]){
char buf[1024];
if(access[i]==IEEE1888_ILONSS_ACCESS_READ){
sprintf(buf,"Read access to %s is prohibitted.",ids[i]);
}else if(access[i]==IEEE1888_ILONSS_ACCESS_WRITE){
sprintf(buf,"Write access to %s is prohibitted.",ids[i]);
}else{
sprintf(buf,"Unknown access to %s is prohibitted.",ids[i]);
}
return ieee1888_mk_error_forbidden(buf);
}
break;
}
}
if(j==n_m_config){
char buf[1024];
sprintf(buf,"%s",ids[i]);
return ieee1888_mk_error_point_not_found(buf);
}
}
return NULL;
}
int ilonssGW_findConfig(char id[], struct ilonssGW_baseConfig** pconfig){
int j;
for(j=0;j<n_m_config;j++){
if(strcmp(id,m_config[j].point_id)==0){
*pconfig=&m_config[j];
return IEEE1888_ILONSS_OK;
}
}
return IEEE1888_ILONSS_ERROR;
}
#define NOF_CONCURRENCY 80
int ilonssGW_bacnetRead(char ids[][IEEE1888_ILONSS_POINTID_LEN], time_t times[], char values[][IEEE1888_ILONSS_VALUE_LEN], int n_point){
int i=0, j=0, k, prev_port;
char prev_host[IEEE1888_ILONSS_HOSTNAME_LEN];
struct ilonssGW_baseConfig* config;
struct ilonssGW_baseConfig* config_list[NOF_CONCURRENCY];
char names[NOF_CONCURRENCY][1024];
char types[NOF_CONCURRENCY][1024];
time_t start=time(NULL);
char black_host[8][IEEE1888_ILONSS_HOSTNAME_LEN];
int n_black_host=0;
do {
int nof_process = NOF_CONCURRENCY;
if (i+NOF_CONCURRENCY > n_point) {
nof_process = n_point - i;
}
ilonssGW_findConfig(ids[i], &config);
strcpy(prev_host, config->host);
prev_port = config->port;
for (j=0; j<nof_process; j++) {
if(ilonssGW_findConfig(ids[j+i], &config)!=IEEE1888_ILONSS_OK){
continue;
}
time_t now=time(NULL);
if(now<start || now>start+IEEE1888_ILONSS_BULK_SESSION_TIMEOUT){
ilonssGW_log("iLon bulk session(read) timedout\n",IEEE1888_ILONSS_LOGLEVEL_ERROR);
return IEEE1888_ILONSS_ERROR;
}
k=-1;
for(k=0;k<n_black_host;k++){
if(strcmp(black_host[k],config->host)==0){
break;
}
}
if(k<n_black_host){
continue;
}
if (strcmp(prev_host, config->host) != 0 || prev_port != config->port) {
break;
}
strcpy(names[j], config->object_id);
strcpy(types[j], config->data_type);
config_list[j] = config;
strcpy(prev_host, config->host);
prev_port = config->port;
}
// raw read: invoke readProperties
struct ilon_data bdata[NOF_CONCURRENCY];
if (ILONSS_OK == readProperties(config_list[j-1]->host,config_list[j-1]->port,
names, types,
bdata, j) ) {
for (k=0; k<j; k++) {
time_t record_time=time(NULL);
// copy value to the area of caller
//printf("writing %s, to %d/%d\n", bdata[k].value, k+i, n_point);
strncpy(values[k+i],bdata[k].value,IEEE1888_ILONSS_VALUE_LEN);
times[k+i]=record_time;
// load into the status buffer
config_list[k]->status_time=record_time;
strncpy(config_list[k]->status_value,bdata[k].value,IEEE1888_ILONSS_VALUE_LEN);
}
} else {
char logbuf[1000];
for (k=0; k<j; k++) {
sprintf(logbuf,"Failed to get data of %s from iLonSS\n",config_list[k]->point_id);
ilonssGW_log(logbuf,IEEE1888_ILONSS_LOGLEVEL_WARN);
values[k+i][0]='\0';
times[k+i]=0;
config_list[k]->status_time=0;
config_list[k]->status_value[0]='\0';
}
if(n_black_host<8){
strcpy(black_host[n_black_host],config->host);
n_black_host++;
}
}
} while ( (i+=j) <n_point);
return IEEE1888_ILONSS_OK;
}
int ilonssGW_bacnetWrite(char ids[][IEEE1888_ILONSS_POINTID_LEN], char values[][IEEE1888_ILONSS_VALUE_LEN], int n_point){
int i;
struct ilonssGW_baseConfig *config;
// pre-processing -- prepare binary objects to write (if error found, abort the mission.)
struct ilon_data bdata[n_point];
for(i=0;i<n_point;i++){
if(ilonssGW_findConfig(ids[i],&config)==IEEE1888_ILONSS_OK){
// parse the value and generate binary format according to the config.type and config.exp
char value_str[IEEE1888_ILONSS_VALUE_LEN];
memset(value_str,0,sizeof(value_str));
strcpy(value_str,values[i]);
// load into the status buffer
config->status_time=time(NULL);
strncpy(config->status_value,value_str,IEEE1888_ILONSS_VALUE_LEN);
}
}
time_t start=time(NULL);
char black_host[8][IEEE1888_ILONSS_HOSTNAME_LEN];
int n_black_host=0;
// write processing
for(i=0;i<n_point;i++){
if(ilonssGW_findConfig(ids[i],&config)==IEEE1888_ILONSS_OK){
time_t now=time(NULL);
if(now<start || now>start+IEEE1888_ILONSS_BULK_SESSION_TIMEOUT){
ilonssGW_log("iLon bulk session(write) timedout\n",IEEE1888_ILONSS_LOGLEVEL_ERROR);
return IEEE1888_ILONSS_ERROR;
}
int k=-1;
for(k=0;k<n_black_host;k++){
if(strcmp(black_host[k],config->host)==0){
break;
}
}
if(k<n_black_host){
continue;
}
strcpy(bdata[i].value, config->status_value);
strcpy(bdata[i].type, config->data_type);
bdata[i].priority = config->priority;
if(ILONSS_OK == writeProperty(config->host,config->port,
config->object_id,
&bdata[i]) ){
// SUCCESS
}else{
char logbuf[1000];
sprintf(logbuf,"Failed to set the data of %s into iLon\n",config->point_id);
ilonssGW_log(logbuf,IEEE1888_ILONSS_LOGLEVEL_ERROR);
if(n_black_host<8){
strcpy(black_host[n_black_host],config->host);
n_black_host++;
}
// return IEEE1888_ILONSS_ERROR;
}
}else{
return IEEE1888_ILONSS_ERROR;
}
}
if(n_black_host>0){
return IEEE1888_ILONSS_ERROR;
}
return IEEE1888_ILONSS_OK;
}
ieee1888_error* ilonssGW_ieee1888read(ieee1888_point point[], int n_point, time_t timeAs){
ilonssGW_log("ilonssGW_ieee1888read(begin)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
uint8_t access[n_point];
memset(access,IEEE1888_ILONSS_ACCESS_READ,sizeof(access));
int i;
char ids[n_point][IEEE1888_ILONSS_POINTID_LEN];
for(i=0;i<n_point;i++){
if(strlen(point[i].id)>=IEEE1888_ILONSS_POINTID_LEN){
ilonssGW_log("TOO LONG POINT ID\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888read(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return ieee1888_mk_error_server_error("TOO LONG POINT ID");
}
strncpy(ids[i],point[i].id,IEEE1888_ILONSS_POINTID_LEN);
}
ieee1888_error* myerr=ilonssGW_pointsTest(ids,access,n_point);
if(myerr!=NULL){
return myerr;
}
char values[n_point][IEEE1888_ILONSS_VALUE_LEN];
time_t times[n_point];
if(ilonssGW_bacnetRead(ids,times,values,n_point)==IEEE1888_ILONSS_OK){
for(i=0;i<n_point;i++){
if(times[i]!=0){
ieee1888_value* v=ieee1888_mk_value();
v->time=ieee1888_mk_time(timeAs);
v->content=ieee1888_mk_string(values[i]);
point[i].value=v;
point[i].n_value=1;
}
}
ilonssGW_log("ilonssGW_ieee1888read(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return NULL;
}else{
ilonssGW_log("ilonssGW ilonssRead failed\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888read(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return ieee1888_mk_error_server_error("ilonssGW ilonssRead failed");
}
}
ieee1888_error* ilonssGW_ieee1888write(ieee1888_point point[], int n_point){
ilonssGW_log("ilonssGW_ieee1888write(begin)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
uint8_t access[n_point];
memset(access,IEEE1888_ILONSS_ACCESS_WRITE,sizeof(access));
int i;
char ids[n_point][IEEE1888_ILONSS_POINTID_LEN];
char values[n_point][IEEE1888_ILONSS_VALUE_LEN];
for(i=0;i<n_point;i++){
if(strlen(point[i].id)>=IEEE1888_ILONSS_POINTID_LEN){
ilonssGW_log("TOO LONG POINT ID\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888write(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return ieee1888_mk_error_server_error("TOO LONG POINT ID");
}
strncpy(ids[i],point[i].id,IEEE1888_ILONSS_POINTID_LEN);
if(point[i].n_value>0 && point[i].value!=NULL){
ieee1888_value* v=&(point[i].value[point[i].n_value-1]);
if(strlen(v->content)>=IEEE1888_ILONSS_VALUE_LEN){
ilonssGW_log("TOO LONG VALUE\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888write(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return ieee1888_mk_error_server_error("TOO LONG VALUE");
}
strncpy(values[i],v->content,IEEE1888_ILONSS_VALUE_LEN);
}else{
// nothing to do for index i
values[i][0]='\0';
}
}
// point id schema test
ieee1888_error* myerr=ilonssGW_pointsTest(ids,access,n_point);
if(myerr!=NULL){
return myerr;
}
// shrink the ids
int k_point=0;
for(i=0;i<n_point;i++){
if(values[i][0]=='\0'){
continue;
}
if(k_point<i){
strcpy(ids[k_point],ids[i]);
strcpy(values[k_point],values[i]);
}
k_point++;
}
if(ilonssGW_bacnetWrite(ids,values,k_point)==IEEE1888_ILONSS_OK){
ilonssGW_log("ilonssGW_ieee1888write(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return NULL;
}else{
ilonssGW_log("ilonssGW ilonssWrite failed\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888write(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return ieee1888_mk_error_server_error("ilonssGW ilonssWrite failed");
}
}
/*
* IEEE1888 service handlers
*
* -- ilonssGW_ieee1888_server_query
* returns the data by reading from bacnet (by calling ilonssGW_ieee1888read method)
*
* -- ilonssGW_ieee1888_server_data
* pushes data by writing to bacnet (by calling ilonssGW_ieee1888write method)
*
* -- ilonssGW_ieee1888_server_data_parse_request
* parses the data request (prepare for committment)
*
* -- ilonssGW_ieee1888_server_data_commit_request
* executes the data request (prepared for committment)
* by calling ilonssGW_ieee1888write method
*/
ieee1888_transport* ilonssGW_ieee1888_server_query(ieee1888_transport* request, char** args){
ilonssGW_log("ilonssGW_ieee1888_server_query(begin)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
ieee1888_transport* response=(ieee1888_transport*)ieee1888_clone_objects((ieee1888_object*)request,1);
if(response->body!=NULL){
ieee1888_destroy_objects((ieee1888_object*)response->body);
free(response->body);
response->body=NULL;
}
ieee1888_header* header=response->header;
if(header==NULL){
response->header=ieee1888_mk_header();
response->header->error=ieee1888_mk_error_invalid_request("No header in the request.");
ilonssGW_log("INVALID_REQUEST (No header in the request)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_query(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
if(header->OK!=NULL){
response->header->error=ieee1888_mk_error_invalid_request("Invalid OK in the header.");
ilonssGW_log("INVALID_REQUEST (Invalid OK in the header)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_query(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
if(header->error!=NULL){
response->header->error=ieee1888_mk_error_invalid_request("Invalid error in the header.");
ilonssGW_log("INVALID_REQUEST (Invalid error in the header)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_query(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
ieee1888_query* query=header->query;
if(header->query==NULL){
response->header->error=ieee1888_mk_error_invalid_request("No query in the header.");
ilonssGW_log("INVALID_REQUEST (No query in the header)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_query(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
ieee1888_error* error=NULL;
if(strcmp(query->type,"storage")==0){
if(query->callbackData!=NULL){
// Do nothing (just ignore it)
}
if(query->callbackControl!=NULL){
// Do nothing (just ignore it)
}
// Parse the keys in detail
ieee1888_key* keys=query->key;
int n_keys=query->n_key;
ieee1888_point* points=NULL;
int n_points=0;
if(n_keys>0){
points=ieee1888_mk_point_array(n_keys);
n_points=n_keys;
}
int i;
for(i=0;i<n_keys;i++){
ieee1888_key* key=&keys[i];
if(key->id==NULL){
// error -- invalid id
error=ieee1888_mk_error_invalid_request("ID is missing in the query key");
ilonssGW_log("INVALID_REQUEST (ID is missing in the query key)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->attrName==NULL){
// error -- invalid attrName
error=ieee1888_mk_error_invalid_request("attrName is missing in the query key");
ilonssGW_log("INVALID_REQUEST (attrName is missing in the query key)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(strcmp(key->attrName,"time")!=0){
// error -- unsupported attrName
error=ieee1888_mk_error_query_not_supported("attrName other than \"time\" are not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (attrName other than \"time\" are not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->eq!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("eq in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (eq in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->neq!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("neq in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (neq in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->lt!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("lt in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (lt in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->gt!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("gt in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (gt in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->lteq!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("lteq in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (lteq in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->gteq!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("gteq in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (gteq in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->trap!=NULL){
// error -- not supported
error=ieee1888_mk_error_query_not_supported("trap in the query key is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (trap in the query key is not supported.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else if(key->select!=NULL && strcmp(key->select,"minimum")!=0 && strcmp(key->select,"maximum")!=0){
// error -- invalid select
error=ieee1888_mk_error_invalid_request("Invalid select in the query key.");
ilonssGW_log("INVALID_REQUEST (Invalid select in the query key.)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
break;
}else{
// fetchServer ID validity check
int j;
for(j=0;j<n_m_fetchServer_ids;j++){
if(strcmp(key->id,m_fetchServer_ids[j])==0){
points[i].id=ieee1888_mk_uri(key->id);
break;
}
}
if(j==n_m_fetchServer_ids){
char sbuf[IEEE1888_ILONSS_POINTID_LEN+25];
sprintf(sbuf,"Not allowed for fetch %s",key->id);
error=ieee1888_mk_error_forbidden(sbuf);
break;
}
}
}
if(error==NULL && n_points>0){
error=ilonssGW_ieee1888read(points,n_points,time(NULL));
}
if(error==NULL){
// if no error (return OK)
response->header->OK=ieee1888_mk_OK();
response->body=ieee1888_mk_body();
response->body->point=points;
response->body->n_point=n_points;
}else{
// if error exists (return the error without body)
response->header->error=error;
ieee1888_body* body=ieee1888_mk_body();
body->point=points;
body->n_point=n_points;
ieee1888_destroy_objects((ieee1888_object*)body);
free(body);
}
}else if(strcmp(query->type,"stream")==0){
// not supported
error=ieee1888_mk_error_query_not_supported("type=\"stream\" in the query is not supported.");
ilonssGW_log("QUERY_NOT_SUPPORTED (type=\"stream\" in the query is not supported.\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
response->header->error=error;
}else{
// error (invalid request)
error=ieee1888_mk_error_invalid_request("Invalid query type.");
ilonssGW_log("INVALID_REQUEST (Invalid query type.\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
response->header->error=error;
}
ilonssGW_log("ilonssGW_ieee1888_server_query(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
ieee1888_error* ilonssGW_ieee1888_server_data_parse_request(ieee1888_pointSet* pointSet, int n_pointSet, ieee1888_point* point, int n_point, ieee1888_point* w_points, int* n_w_points){
// fprintf(stdout,"ilonssGW_ieee1888_server_data_parse_request(begin)\n");
int i,j;
for(i=0;i<n_pointSet;i++){
ieee1888_error* error=ilonssGW_ieee1888_server_data_parse_request(pointSet[i].pointSet, pointSet[i].n_pointSet, pointSet[i].point, pointSet[i].n_point, w_points, n_w_points);
if(error!=NULL){
return error;
}
}
for(i=0;i<n_point;i++){
for(j=0;j<n_m_writeServer_ids;j++){
if(strcmp(point[i].id,m_writeServer_ids[j])==0){
// clone the point and the values
int n=*n_w_points;
w_points[n].id=ieee1888_mk_uri(point[i].id);
w_points[n].value=(ieee1888_value*)ieee1888_clone_objects((ieee1888_object*)point[i].value,point[i].n_value);
w_points[n].n_value=point[i].n_value;
++*n_w_points;
break;
}
}
if(j==n_m_writeServer_ids){
// Error
char ssbuf[128];
char sbuf[200];
strncpy(ssbuf,point[i].id,127);
sprintf(sbuf,"No WRITE permission for the point(%s)",ssbuf);
//fprintf(stdout,"ERROR: FORBIDDEN (%s)\n",sbuf);
//fprintf(stdout,"ilonssGW_ieee1888_server_data_parse_request(end)\n");
return ieee1888_mk_error_forbidden(sbuf);
}
}
// fprintf(stdout,"ilonssGW_ieee1888_server_data_parse_request(end)\n");
return NULL;
}
ieee1888_transport* ilonssGW_ieee1888_server_data(ieee1888_transport* request,char** args){
ilonssGW_log("ilonssGW_ieee1888_server_data(begin)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
ieee1888_transport* response=ieee1888_mk_transport();
// check the validity of body
ieee1888_body* body=request->body;
if(body==NULL){
response->header=ieee1888_mk_header();
response->header->error=ieee1888_mk_error_invalid_request("No body in the request.");
ilonssGW_log("INVALID_REQUEST (No body in the request)\n",IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_data(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
// Data buffer between parsing and committing the request
ieee1888_point* w_points=ieee1888_mk_point_array(IEEE1888_ILONSS_POINT_COUNT);
int n_w_points=0;
// parse the "data" request (with preparing committing them), and returns error (only if failed).
ieee1888_error* error=ilonssGW_ieee1888_server_data_parse_request(body->pointSet,body->n_pointSet,body->point,body->n_point,w_points,&n_w_points);
if(error!=NULL){
response->header=ieee1888_mk_header();
response->header->error=error;
int d=0;
for(d=0;d<IEEE1888_ILONSS_POINT_COUNT;d++){
ieee1888_destroy_objects((ieee1888_object*)(w_points+d));
}
free(w_points);
char logbuf[2000];
sprintf(logbuf,"error replied: type=%s message=%s \n",error->type,error->content);
ilonssGW_log(logbuf,IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_data(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
// writeServer ID validity check
int i,j;
for(i=0;i<n_w_points;i++){
for(j=0;j<n_m_writeServer_ids;j++){
if(strcmp(w_points[i].id,m_writeServer_ids[j])==0){
break;
}
}
if(j==n_m_writeServer_ids){
char sbuf[IEEE1888_ILONSS_POINTID_LEN*2];
sprintf(sbuf,"Not allowed for WRITE %s",w_points[i].id);
error=ieee1888_mk_error_forbidden(sbuf);
break;
}
if(error!=NULL){
response->header=ieee1888_mk_header();
response->header->error=error;
int d=0;
for(d=0;d<IEEE1888_ILONSS_POINT_COUNT;d++){
ieee1888_destroy_objects((ieee1888_object*)(w_points+d));
}
free(w_points);
char logbuf[2000];
sprintf(logbuf,"error replied: type=%s message=%s \n",error->type,error->content);
ilonssGW_log(logbuf,IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_data(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
}
// commit the "data" request, and returns error (only if failed).
if(error==NULL){
error=ilonssGW_ieee1888write(w_points,n_w_points);
}
if(error!=NULL){
response->header=ieee1888_mk_header();
response->header->error=error;
int d=0;
for(d=0;d<IEEE1888_ILONSS_POINT_COUNT;d++){
ieee1888_destroy_objects((ieee1888_object*)(w_points+d));
}
free(w_points);
char logbuf[2000];
sprintf(logbuf,"error replied: type=%s message=%s \n",error->type,error->content);
ilonssGW_log(logbuf,IEEE1888_ILONSS_LOGLEVEL_WARN);
ilonssGW_log("ilonssGW_ieee1888_server_data(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
// return OK, because succeeded.
response->header=ieee1888_mk_header();
response->header->OK=ieee1888_mk_OK();
int d=0;
for(d=0;d<IEEE1888_ILONSS_POINT_COUNT;d++){
ieee1888_destroy_objects((ieee1888_object*)(w_points+d));
}
free(w_points);
ilonssGW_log("ilonssGW_ieee1888_server_data(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
return response;
}
/*
* Automatic Threads (for periodic execution of WRITE and FETCH)
*
* -- ilonssGW_writeClient_thread
* periodically reads the bacnet, and WRITE into m_writeClient_ieee1888_server_url.
* (*) works only for pre-configured addresses by ilonssGW_writeClient_ids
*
* -- ilonssGW_fetchClient_thread
* periodically FETCH from m_fetchClient_ieee1888_server_url, and writes the bacnet.
* (*) works only for pre-configured addresses by ilonssGW_fetchClient_ids
*
*/
void* ilonssGW_writeClient_thread(void* args){
// if no points to work on --> return
if(n_m_writeClient_ids==0){
return NULL;
}
int i;
sleep(10); // wait at the startup
time_t last_upload=0;
// for debug
// int counter=0;
while(1){
// time check interval: 1 seconds
sleep(1);
time_t now=time(NULL);
if((last_upload-m_writeClient_trigger_offset)/m_writeClient_trigger_frequency
==(now-m_writeClient_trigger_offset)/m_writeClient_trigger_frequency ){
continue;
}
last_upload=now;
// for debug
// printf("count: %d\n",counter++);
ieee1888_point* point=ieee1888_mk_point_array(n_m_writeClient_ids);
int n_point=n_m_writeClient_ids;
for(i=0;i<n_point;i++){
point[i].id=ieee1888_mk_uri(m_writeClient_ids[i]);
}
// read from ilonss
time_t time_to_present=(now/m_writeClient_trigger_frequency)*m_writeClient_trigger_frequency;
if(ilonssGW_ieee1888read(point,n_point,time_to_present)==IEEE1888_ILONSS_OK){
// send the data
ilonssGW_log("ilonssGW_writeClient_thread_WRITE(begin)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
ieee1888_transport* rq_transport=ieee1888_mk_transport();
ieee1888_body* rq_body=ieee1888_mk_body();
rq_transport->body=rq_body;
rq_body->point=point;
rq_body->n_point=n_point;
ieee1888_transport* rs_transport=ieee1888_client_data(rq_transport,m_writeClient_ieee1888_server_url,NULL,NULL);
//ieee1888_dump_objects((ieee1888_object*)rq_transport);
if(rs_transport!=NULL && rs_transport->header!=NULL && rs_transport->header->OK!=NULL){
ilonssGW_log("writeClient success\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
}else{
// Failed --> DUMP INTO DATAPOOL for retry
ieee1888_datapool_dump(rq_transport);
ilonssGW_log("writeClient failure\n",IEEE1888_ILONSS_LOGLEVEL_ERROR);
// fprintf(stdout," this ==> server \n");
if(rq_transport!=NULL){
// ieee1888_dump_objects((ieee1888_object*)rq_transport);
}
// fprintf(stdout," this <== server \n");
if(rs_transport!=NULL){
// ieee1888_dump_objects((ieee1888_object*)rs_transport);
}
}
if(rq_transport!=NULL){
ieee1888_destroy_objects((ieee1888_object*)rq_transport);
free(rq_transport);
}
if(rs_transport!=NULL){
ieee1888_destroy_objects((ieee1888_object*)rs_transport);
free(rs_transport);
}
ilonssGW_log("ilonssGW_writeClient_thread_WRITE(end)\n",IEEE1888_ILONSS_LOGLEVEL_INFO);
}else{
ilonssGW_log("ilonssGW_writeClient_thread: no WRITE client operation because of the failure of ilonssGW_ieee1888read\n",IEEE1888_ILONSS_LOGLEVEL_ERROR);
}
}
}
void* ilonssGW_fetchClient_thread(void* args){
// if no points to work on --> return
if(n_m_fetchClient_ids==0){
return NULL;
}
int i;
sleep(10); // wait at the startup
time_t last_download=0;
// int counter=0; // for debug
while(1){
// time check interval: 1 seconds
sleep(1);
time_t now=time(NULL);
if((last_download-m_fetchClient_trigger_offset)/m_fetchClient_trigger_frequency
==(now-m_fetchClient_trigger_offset)/m_fetchClient_trigger_frequency ){
continue;
}
// for debug
// printf("count:%d\n",counter++);
last_download=now;
ieee1888_key* keys=ieee1888_mk_key_array(n_m_fetchClient_ids);
int n_keys=n_m_fetchClient_ids;
for(i=0;i<n_keys;i++){
keys[i].id=ieee1888_mk_uri(m_fetchClient_ids[i]);
keys[i].attrName=ieee1888_mk_string("time");
keys[i].select=ieee1888_mk_string("maximum");
}
// fetch the data (cursor function is not implemented)
ilonssGW_log("ilonssGW_fetchClient_thread_FETCH(begin)\n", IEEE1888_ILONSS_LOGLEVEL_INFO);
ieee1888_transport* rq_transport=ieee1888_mk_transport();
ieee1888_header* rq_header=ieee1888_mk_header();
ieee1888_query* rq_query=ieee1888_mk_query();
rq_transport->header=rq_header;
rq_header->query=rq_query;
rq_query->type=ieee1888_mk_string("storage");
rq_query->id=ieee1888_mk_new_uuid();
rq_query->key=keys;
rq_query->n_key=n_keys;
ieee1888_transport* rs_transport=ieee1888_client_query(rq_transport,m_fetchClient_ieee1888_server_url,NULL,NULL);
if(rs_transport!=NULL && rs_transport->header!=NULL && rs_transport->header->OK!=NULL && rs_transport->body!=NULL){
ilonssGW_log("fetchClient success.\n", IEEE1888_ILONSS_LOGLEVEL_INFO);
ieee1888_body* rs_body=rs_transport->body;
ieee1888_point* rs_point=rs_body->point;
int n_rs_point=rs_body->n_point;
ieee1888_error* local_error=ilonssGW_ieee1888write(rs_point,n_rs_point);
if(local_error!=NULL){
ieee1888_destroy_objects((ieee1888_object*)local_error);
free(local_error);
}
}else{
ilonssGW_log("fetchClient failure.\n", IEEE1888_ILONSS_LOGLEVEL_ERROR);
// fprintf(stdout," this ==> server \n");
if(rq_transport!=NULL){
// ieee1888_dump_objects((ieee1888_object*)rq_transport);
}
// fprintf(stdout," this <== server \n");
if(rs_transport!=NULL){
// ieee1888_dump_objects((ieee1888_object*)rs_transport);
}
}
if(rq_transport!=NULL){
ieee1888_destroy_objects((ieee1888_object*)rq_transport);
free(rq_transport);
}
if(rs_transport!=NULL){
ieee1888_destroy_objects((ieee1888_object*)rs_transport);
free(rs_transport);
}
ilonssGW_log("ilonssGW_fetchClient_thread_FETCH(end)\n", IEEE1888_ILONSS_LOGLEVEL_INFO);
}
}
int ilonssGW_readConfig(const char* configPath){
int i,k;
ilonssGW_log("ilonssGW_readConfig(begin)\n", IEEE1888_ILONSS_LOGLEVEL_INFO);
// initialize the memory space
memset(m_config,0,sizeof(m_config));
n_m_config=0;
memset(m_writeServer_ids,0,sizeof(m_writeServer_ids));
memset(m_fetchServer_ids,0,sizeof(m_fetchServer_ids));
memset(m_writeClient_ids,0,sizeof(m_writeClient_ids));
memset(m_fetchClient_ids,0,sizeof(m_fetchClient_ids));
n_m_writeServer_ids=0;
n_m_fetchServer_ids=0;
n_m_writeClient_ids=0;
n_m_fetchClient_ids=0;
memset(m_writeClient_ieee1888_server_url,0,sizeof(m_writeClient_ieee1888_server_url));
m_writeClient_trigger_frequency=0;
m_writeClient_trigger_offset=0;
memset(m_fetchClient_ieee1888_server_url,0,sizeof(m_fetchClient_ieee1888_server_url));
m_fetchClient_trigger_frequency=0;
m_fetchClient_trigger_offset=0;
m_datapool_timespan=60;
// open file
FILE* fp=fopen(configPath,"r");
if(fp==NULL){
ilonssGW_log("Failed to read the configFile\n",IEEE1888_ILONSS_LOGLEVEL_ERROR);
return IEEE1888_ILONSS_ERROR;
}
// `ead a line
char line[2048];
while(fgets(line,2048,fp)!=NULL){