-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeDIS.c
800 lines (603 loc) · 22.5 KB
/
BeDIS.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
/*
2020 © Copyright (c) BiDaE Technology Inc.
Provided under BiDaE SHAREWARE LICENSE-1.0 in the LICENSE.
Project Name:
BeDIS
File Name:
BeDIS.c
File Description:
This file contains code of functions used in both gateway and Lbeacon.
Version:
2.0, 20190617
Abstract:
BeDIS uses LBeacons to deliver 3D coordinates and textual descriptions of
their locations to users' devices. Basically, a LBeacon is an inexpensive,
Bluetooth Smart Ready device. The 3D coordinates and location description
of every LBeacon are retrieved from BeDIS (Building/environment Data and
Information System) and stored locally during deployment and maintenance
times. Once initialized, each LBeacon broadcasts its coordinates and
location description to Bluetooth enabled user devices within its coverage
area.
Authors:
Gary Xiao , [email protected]
Joey Zhou , [email protected]
Holly Wang , [email protected]
Jake Lee , [email protected]
Chun Yu Lai , [email protected]
Jia Ying Shi , [email protected]
*/
#include "BeDIS.h"
unsigned int twoc(int in, int t)
{
return (in < 0) ? (in + (2 << (t - 1))) : in;
}
char decimal_to_hex(int number)
{
char c;
if(number <= 9)
c = number +'0';
else
c = (number - 10) + 'A';
return c;
}
int hex_to_decimal(char hex_number){
int a = 0;
if(hex_number >= 'A' )
a = hex_number - 'A' + 10;
else
a = hex_number - '0';
return a;
}
void init_buffer(BufferListHead *buffer_list_head, void (*function_p)(void *),
int priority_nice)
{
init_entry( &(buffer_list_head -> list_head));
init_entry( &(buffer_list_head -> priority_list_entry));
pthread_mutex_init( &buffer_list_head -> list_lock, 0);
buffer_list_head -> function = function_p;
buffer_list_head -> arg = (void *) buffer_list_head;
buffer_list_head -> priority_nice = priority_nice;
}
void init_Address_Map(AddressMapArray *address_map)
{
int n;
pthread_mutex_init( &address_map -> list_lock, 0);
memset(address_map -> address_map_list, 0,
sizeof(address_map -> address_map_list));
for(n = 0; n < MAX_NUMBER_NODES; n ++)
address_map -> in_use[n] = false;
}
int is_in_Address_Map(AddressMapArray *address_map,
AddressMapType type,
char *identifier)
{
int n;
if (type == ADDRESS_MAP_TYPE_GATEWAY)
{
for(n = 0; n < MAX_NUMBER_NODES; n++)
{
if (address_map -> in_use[n] == true &&
strncmp(address_map -> address_map_list[n].identifier,
identifier,
LENGTH_OF_IDENTIFIER) == 0)
{
zlog_debug(category_debug,
"unique_id matached n=%d [%s] [%s]\n",
n,
address_map->address_map_list[n].identifier,
identifier);
return n;
}
}
}
else if(type == ADDRESS_MAP_TYPE_LBEACON)
{
for(n = 0; n < MAX_NUMBER_NODES; n++){
if (address_map -> in_use[n] == true &&
strncmp(address_map -> address_map_list[n].identifier,
identifier,
LENGTH_OF_IDENTIFIER) == 0)
{
zlog_debug(category_debug,
"uuid matached n=%d [%s] [%s]\n",
n,
address_map->address_map_list[n].identifier,
identifier);
return n;
}
}
}
return -1;
}
ErrorCode update_entry_in_Address_Map(AddressMapArray *address_map,
int index,
AddressMapType type,
char *address,
char *identifier,
char *API_version)
{
int current_time = get_system_time();
address_map -> in_use[index] = true;
address_map -> last_reported_timestamp[index] = current_time;
memset(address_map->address_map_list[index].API_version, 0,
LENGTH_OF_API_VERSION);
strncpy(address_map->address_map_list[index].API_version,
API_version, strlen(API_version));
if(type == ADDRESS_MAP_TYPE_GATEWAY){
memset(address_map->address_map_list[index].identifier, 0,
LENGTH_OF_IDENTIFIER);
strncpy(address_map->address_map_list[index].identifier,
identifier, strlen(identifier));
memset(address_map->address_map_list[index].net_address, 0,
NETWORK_ADDR_LENGTH);
strncpy(address_map->address_map_list[index].net_address,
address, strlen(address));
}else if(type == ADDRESS_MAP_TYPE_LBEACON){
memset(address_map->address_map_list[index].identifier, 0,
LENGTH_OF_IDENTIFIER);
strncpy(address_map->address_map_list[index].identifier,
identifier, strlen(identifier));
memset(address_map->address_map_list[index].net_address, 0,
NETWORK_ADDR_LENGTH);
strncpy(address_map->address_map_list[index].net_address,
address, strlen(address));
}
return WORK_SUCCESSFULLY;
}
ErrorCode update_report_timestamp_in_Address_Map(AddressMapArray *address_map,
AddressMapType type,
char *identifer)
{
int index = -1;
int current_time = get_system_time();
index = is_in_Address_Map(address_map, type, identifer);
if(index != -1){
address_map -> last_reported_timestamp[index] = current_time;
}
return WORK_SUCCESSFULLY;
}
ErrorCode release_not_used_entry_from_Address_Map(AddressMapArray *address_map,
int tolerance_duration)
{
int i;
int current_time = get_system_time();
for(i = 0;i < MAX_NUMBER_NODES;i ++)
{
if (address_map -> in_use[i] == true &&
(current_time - address_map ->last_reported_timestamp[i] >
tolerance_duration)){
address_map -> in_use[i] = false;
printf("release index [%d], net_address [%s], identifier [%s]\n",
i,
address_map->address_map_list[i].net_address,
address_map->address_map_list[i].identifier);
}
}
return WORK_SUCCESSFULLY;
}
ErrorCode dump_ip_of_active_entry_from_Address_Map(char *filename,
AddressMapArray *address_map,
int tolerance_duration){
int i;
int current_time = get_system_time();
int retry_times = 0;
FILE *active_file = NULL;
retry_times = FILE_OPEN_RETRY;
while(retry_times--){
active_file =
fopen(filename, "w");
if(NULL != active_file){
break;
}
}
if(NULL == active_file)
return E_OPEN_FILE;
for(i = 0;i < MAX_NUMBER_NODES;i ++)
{
if (address_map -> in_use[i] == true &&
(current_time - address_map ->last_reported_timestamp[i] <
tolerance_duration)){
fprintf(active_file, "%s\n",
address_map->address_map_list[i].net_address);
}
}
fclose(active_file);
return WORK_SUCCESSFULLY;
}
void *sort_priority_list(CommonConfig *common_config, BufferListHead *list_head){
List_Entry *list_pointer,
*next_list_pointer;
List_Entry critical_priority_head, high_priority_head,
normal_priority_head, low_priority_head;
BufferListHead *current_head, *next_head;
init_entry( &critical_priority_head);
init_entry( &high_priority_head);
init_entry( &normal_priority_head);
init_entry( &low_priority_head);
pthread_mutex_lock( &list_head -> list_lock);
list_for_each_safe(list_pointer, next_list_pointer,
&list_head -> priority_list_entry){
remove_list_node(list_pointer);
current_head = ListEntry(list_pointer, BufferListHead,
priority_list_entry);
if(current_head -> priority_nice == common_config->time_critical_priority)
insert_list_tail( list_pointer, &critical_priority_head);
else if(current_head -> priority_nice == common_config->high_priority)
insert_list_tail( list_pointer, &high_priority_head);
else if(current_head -> priority_nice == common_config->normal_priority)
insert_list_tail( list_pointer, &normal_priority_head);
else if(current_head -> priority_nice == common_config->low_priority)
insert_list_tail( list_pointer, &low_priority_head);
}
if(is_entry_list_empty(&critical_priority_head) == false){
list_pointer = critical_priority_head.next;
remove_list_node(list_pointer -> prev);
concat_list( &list_head -> priority_list_entry, list_pointer);
}
if(is_entry_list_empty(&high_priority_head) == false){
list_pointer = high_priority_head.next;
remove_list_node(list_pointer -> prev);
concat_list( &list_head -> priority_list_entry, list_pointer);
}
if(is_entry_list_empty(&normal_priority_head) == false){
list_pointer = normal_priority_head.next;
remove_list_node(list_pointer -> prev);
concat_list( &list_head -> priority_list_entry, list_pointer);
}
if(is_entry_list_empty(&low_priority_head) == false){
list_pointer = low_priority_head.next;
remove_list_node(list_pointer -> prev);
concat_list( &list_head -> priority_list_entry, list_pointer);
}
pthread_mutex_unlock( &list_head -> list_lock);
}
void *CommUnit_routine()
{
/* The last reset time */
int init_time;
int uptime;
Threadpool thpool;
int return_error_value;
/* A flag to indicate whether any buffer nodes were processed in
this iteration of the while loop */
bool did_work;
/* The pointer to the current priority buffer list entry */
List_Entry *current_entry, *list_entry;
/* The pointer to the current node in the list */
BufferNode *current_node;
/* The pointer to the current buffer list head */
BufferListHead *current_head;
/* wait for NSI get ready */
while(NSI_initialization_complete == false)
{
sleep_t(BUSY_WAITING_TIME_IN_MS);
if(initialization_failed == true)
{
return (void *)NULL;
}
}
#ifdef debugging
zlog_info(category_debug,"[CommUnit] thread pool Initializing");
#endif
/* Initialize the threadpool with specified number of worker threads
according to the data stored in the configuration file. */
thpool = thpool_init(common_config.number_worker_threads);
#ifdef debugging
zlog_info(category_debug, "[CommUnit] thread pool Initialized");
#endif
uptime = get_clock_time();
/* Set the initial time. */
init_time = uptime;
/* All the buffers lists have been initialized and the thread pool
initialized. Set the flag to true. */
CommUnit_initialization_complete = true;
/* When there is no dead thead, continue to work. */
while(ready_to_work == true)
{
uptime = get_clock_time();
/* In the normal situation, the scanning starts from the high priority
to lower priority. When the timer expired for MAX_STARVATION_TIME,
reverse the scanning order */
while(ready_to_work == true &&
(uptime - init_time < MAX_STARVATION_TIME) ||
(uptime < init_time))
{
/* Scan the priority_list to get the buffer list with the highest
priority among all lists that are not empty. */
did_work = false;
pthread_mutex_lock( &priority_list_head.list_lock);
list_for_each(current_entry,
&priority_list_head.priority_list_entry)
{
current_head = ListEntry(current_entry, BufferListHead,
priority_list_entry);
pthread_mutex_lock( ¤t_head -> list_lock);
if (is_entry_list_empty( ¤t_head->list_head) == true)
{
pthread_mutex_unlock( ¤t_head -> list_lock);
/* Go to check the next buffer list in the priority list */
continue;
}
else
{
list_entry = current_head -> list_head.next;
remove_list_node(list_entry);
pthread_mutex_unlock( ¤t_head -> list_lock);
current_node = ListEntry(list_entry, BufferNode,
buffer_entry);
if(uptime - current_node->uptime_at_receive >
common_config.min_age_out_of_date_packet_in_sec){
mp_free(&node_mempool, current_node);
continue;
}
/* Have a worker thread execute the function specified by the
function pointer to do the work */
return_error_value = thpool_add_work(thpool,
current_head -> function,
current_node,
current_head ->
priority_nice);
did_work = true;
break;
}
}
uptime = get_clock_time();
pthread_mutex_unlock( &priority_list_head.list_lock);
if(did_work == false){
sleep_t(BUSY_WAITING_TIME_IN_PRIORITY_LIST_IN_MS);
}
}
/* Scan the priority list in reverse order to prevent starving the
lowest priority buffer list. */
pthread_mutex_lock( &priority_list_head.list_lock);
/* In starvation scenario, we still need to process time-critial buffer
lists first. We first process Geo_fence_alert_buffer_list and
then Geo_fence_receive_buffer_list, and finally reversely
traverse the priority list.
*/
list_for_each(current_entry, &priority_list_head.priority_list_entry)
{
current_head = ListEntry(current_entry, BufferListHead,
priority_list_entry);
if(current_head -> priority_nice
!= common_config.time_critical_priority){
break;
}
pthread_mutex_lock( ¤t_head -> list_lock);
if (is_entry_list_empty( ¤t_head->list_head) == true)
{
pthread_mutex_unlock( ¤t_head -> list_lock);
continue;
}
else
{
list_entry = current_head -> list_head.next;
remove_list_node(list_entry);
pthread_mutex_unlock( ¤t_head -> list_lock);
current_node = ListEntry(list_entry, BufferNode,
buffer_entry);
return_error_value = thpool_add_work(thpool,
current_head -> function,
current_node,
current_head ->
priority_nice);
did_work = true;
break;
}
}
// reversly traverse the priority list
list_for_each_reverse(current_entry,
&priority_list_head.priority_list_entry)
{
current_head = ListEntry(current_entry, BufferListHead,
priority_list_entry);
pthread_mutex_lock( ¤t_head -> list_lock);
if (is_entry_list_empty( ¤t_head->list_head) == true)
{
pthread_mutex_unlock( ¤t_head -> list_lock);
continue;
}
else
{
list_entry = current_head -> list_head.next;
remove_list_node(list_entry);
pthread_mutex_unlock( ¤t_head -> list_lock);
current_node = ListEntry(list_entry, BufferNode,
buffer_entry);
/* Call the function pointed to by the function pointer to do
the work */
return_error_value = thpool_add_work(thpool,
current_head -> function,
current_node,
current_head ->
priority_nice);
did_work = true;
}
}
/* Update the init_time */
init_time = get_clock_time();
pthread_mutex_unlock( &priority_list_head.list_lock);
/* If during this iteration of while loop no work were done,
sleep before starting the next iteration */
if(did_work == false)
{
sleep_t(BUSY_WAITING_TIME_IN_PRIORITY_LIST_IN_MS);
}
} /* End while(ready_to_work == true) */
/* Destroy the thread pool */
thpool_destroy(thpool);
return (void *)NULL;
}
void trim_string_tail(char *message)
{
int idx = 0;
/* discard the whitespace, newline, carry-return characters at the end */
if(strlen(message) > 0){
idx = strlen(message) - 1;
while(10 == message[idx] ||
13 == message[idx] ||
32 == message[idx]){
message[idx] = '\0';
idx--;
}
}
}
void fetch_next_string(FILE *file, char *message, size_t message_size)
{
char config_setting[CONFIG_BUFFER_SIZE];
char *config_message = NULL;
fgets(config_setting, sizeof(config_setting), file);
config_message = strstr((char *)config_setting, DELIMITER);
config_message = config_message + strlen(DELIMITER);
trim_string_tail(config_message);
memset(message, 0, message_size);
strcpy(message, config_message);
}
bool is_numeric(char * str_value)
{
size_t len = 0;
int i = 0;
len = strlen(str_value);
for(i = 0 ; i < len ; i++){
if(str_value[i] < '0' || str_value[i] > '9'){
if(i == 0 && str_value[i] == '-'){
continue;
}
else{
return false;
}
}
}
return true;
}
void ctrlc_handler(int stop) { ready_to_work = false; }
int strncmp_caseinsensitive(char const *str_a, char const *str_b, size_t len)
{
int index = 0;
int diff = 0;
for(index = 0; index < len; index++)
{
diff = tolower((unsigned char)str_a[index]) -
tolower((unsigned char)str_b[index]);
if(0 != diff)
{
return -1;
}
}
return 0;
}
ErrorCode strtolowercase(char const * source_str, char * buf, size_t buf_len){
int i = 0;
int str_len = strlen(source_str);
if(buf_len < str_len)
return E_BUFFER_SIZE;
for(i = 0; i < str_len; i++){
buf[i] = tolower((unsigned char)source_str[i]);
}
return WORK_SUCCESSFULLY;
}
ErrorCode remove_uuid_hyphen(char const * source_str, char * buf, size_t buf_len){
int i = 0;
int dest_index = 0;
for(i = 0 ; i < strlen(source_str) ; i ++){
if(source_str[i] >= '0' && source_str[i] <= '9'){
if(buf_len > dest_index){
buf[dest_index] = source_str[i];
dest_index ++;
}else
return E_INPUT_PARAMETER;
}
}
buf[dest_index] = '\0';
return WORK_SUCCESSFULLY;
}
ErrorCode add_uuid_hyphen(char const * source_str, char * buf, size_t buf_len){
int i = 0;
int dest_index = 0;
for(i = 0 ; i < strlen(source_str) ; i ++){
buf[dest_index] = source_str[i];
dest_index++;
if(i == 7 || i == 11 || i == 15 || i == 19){
buf[dest_index] = '-';
dest_index++;
}
}
buf[dest_index] = '\0';
return WORK_SUCCESSFULLY;
}
ErrorCode startThread(pthread_t *thread, void *( *start_routine)(void *),
void *arg)
{
pthread_attr_t attr;
if ( pthread_attr_init( &attr) != 0 ||
pthread_create(thread, &attr, start_routine, arg) != 0 ||
pthread_detach( *thread))
{
zlog_info(category_debug, "Start Thread Error.");
return E_START_THREAD;
}
return WORK_SUCCESSFULLY;
}
int get_system_time()
{
/* Return value as a long long type */
int system_time;
time_t now = time(NULL);
/* second ver. */
system_time = (int)now;
return system_time;
}
int get_clock_time()
{
#ifdef _WIN32
return GetTickCount() / 1000;
#elif __unix__
struct timespec current_time;
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
return current_time.tv_sec;
#endif
}
char *strtok_save(char *str, char *delim, char **saveptr)
{
char *tmp;
if(str == NULL)
{
tmp = *saveptr;
}
else
{
tmp = str;
}
if(strncmp(tmp, delim, strlen(delim)) == 0)
{
*saveptr += strlen(delim) * sizeof(char);
return NULL;
}
#ifdef _WIN32
return strtok_s(str, delim, saveptr);
#elif __unix__
return strtok_r(str, delim, saveptr);
#endif
}
int display_time(void)
{
// variables to store date and time components
//int hours, minutes, seconds, day, month, year;
// time_t is arithmetic time type
time_t now;
// Obtain current time
// time() returns the current time of the system as a time_t value
time(&now);
// Convert to local time format and print to stdout
zlog_debug(category_debug, "%s", ctime(&now));
return 0;
}
void sleep_t(int wait_time)
{
#ifdef _WIN32
Sleep(wait_time);
#elif __unix__
wait_time*=1000;
usleep(wait_time);
#endif
}