-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.c
625 lines (568 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
/*
* Linux DMR Master server
Copyright (C) 2014 Wim Hofman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "master_server.h"
int servicePort = 50000;
int dmrPort = 50001;
int baseDmrPort = 50100;
int rdacPort = 50002;
int baseRdacPort = 50200;
int maxRepeaters = 20;
int echoId = 9990;
char version[5] = "0.5";
struct repeater repeaterList[100] = {0};
struct repeater emptyRepeater = {0};
struct masterInfo master;
static const struct masterInfo emptyMaster = {0};
struct ts tsInfo = {0};
struct sockaddr_in discardList[100] = {0};
int rdacSock=0;
int highestRepeater = 0;
int restart = 0;
sqlite3 *db;
sqlite3 *openDatabase();
int setRdacRepeater();
int findRdacRepeater();
state dmrState[3];
int (*sMasterTS1List)[2];
int (*sMasterTS2List)[2];
int (*repTS1List)[2];
int (*repTS2List)[2];
void *dmrListener();
void *rdacListener();
void *sMasterThread();
void *webServerListener();
void discard(struct sockaddr_in address){
int i;
for(i=0;i<maxRepeaters;i++){
if (discardList[i].sin_addr.s_addr == address.sin_addr.s_addr) return;
}
//If not found, find first empty position in the list
for(i=0;i<maxRepeaters;i++){
if (discardList[i].sin_addr.s_addr == 0) break;
}
//oops, max repeaters reached
if (i == maxRepeaters){
syslog(LOG_NOTICE,"Not possible to add repeater in discard list, maximum number reached");
return;
}
discardList[i] = address;
}
bool isDiscarded(struct sockaddr_in address){
int i;
for(i=0;i<maxRepeaters;i++){
if (discardList[i].sin_addr.s_addr == address.sin_addr.s_addr) return true;
}
return false;
}
int initRepeater(struct repeater repeaterInfo){
//Initialize a repeater in the DMR list
int i;
//If found based on IP, just return it's position in the list
for(i=0;i<maxRepeaters;i++){
if (repeaterList[i].address.sin_addr.s_addr == repeaterInfo.address.sin_addr.s_addr) return i;
}
//If not found, find first empty position in the list
for(i=0;i<maxRepeaters;i++){
if (repeaterList[i].address.sin_addr.s_addr == 0) break;
}
//oops, max repeaters reached
if (i == maxRepeaters){
syslog(LOG_NOTICE,"Not possible to add repeater, maximum number reached");
return 99;
}
//Set all the needed info for a repeater
repeaterInfo.address.sin_port=htons(dmrPort);
repeaterList[i].address = repeaterInfo.address;
repeaterList[i].id = repeaterInfo.id;
sprintf(repeaterList[i].callsign,"%s",repeaterInfo.callsign);
sprintf(repeaterList[i].txFreq,"%s",repeaterInfo.txFreq);
sprintf(repeaterList[i].shift,"%s",repeaterInfo.shift);
sprintf(repeaterList[i].hardware,"%s",repeaterInfo.hardware);
sprintf(repeaterList[i].firmware,"%s",repeaterInfo.firmware);
sprintf(repeaterList[i].mode,"%s",repeaterInfo.mode);
syslog(LOG_NOTICE,"Repeater added to list position %i",i);
//Highest filled position in the list
if (i +1 > highestRepeater) highestRepeater = i + 1;
return i;
}
int findRepeater(struct sockaddr_in address){
//Find a repeater in the DMR list
int i;
for(i=0;i<maxRepeaters;i++){
if (repeaterList[i].address.sin_addr.s_addr == address.sin_addr.s_addr) return i;
}
//If not found
return 99;
}
void serviceListener(port){
pthread_t thread;
int sockfd,n,i,rc;
struct sockaddr_in servaddr,cliaddr;
socklen_t len;
unsigned char buffer[500];
unsigned char response[500] ={0};
unsigned char command[] = {0x50,0x32,0x50,0x50};
unsigned char ping[] = {0x5a,0x5a,0x5a,0x5a};
unsigned char ping2[] = {0xe8,0x40,0xb4,0x10};
char str[INET_ADDRSTRLEN];
int redirectPort;
int repPos;
struct repeater repeaterInfo;
sqlite3_stmt *stmt;
unsigned char SQLQUERY[200] = {0};
fd_set fdService;
struct timeval timeout;
time_t timeNow;
syslog(LOG_NOTICE,"Listener for port %i started",port);
//Create the socket to listen to the service port
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
FD_ZERO(&fdService);
for (;;){
FD_SET(sockfd, &fdService);
timeout.tv_sec = 2;
timeout.tv_usec = 0;
rc = select(sockfd+1, &fdService, NULL, NULL, &timeout);
if (FD_ISSET(sockfd,&fdService)) {
len = sizeof(cliaddr);
memset(&buffer,0,500);
n = recvfrom(sockfd,buffer,500,0,(struct sockaddr *)&cliaddr,&len);
if (memcmp(buffer,command,sizeof(command)) == 0){ //See if what is received is a command or heartbeat
inet_ntop(AF_INET, &(cliaddr.sin_addr), str, INET_ADDRSTRLEN);
time(&timeNow);
switch (buffer[20]){
int rdacPos;
case 0x10:{ //PTPP request received
if(isDiscarded(cliaddr)) continue;
rdacPos = setRdacRepeater(cliaddr);
if (difftime(timeNow,rdacList[rdacPos].lastPTPPConnect) < 10) continue; //Ignore connect request
syslog(LOG_NOTICE,"PTPP request from repeater [%s]",str);
memcpy(response,buffer,n);
//Assign device ID
response[4]++;
response[13]=0x01;
response[n] = 0x01;
sendto(sockfd,response,n+1,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
syslog(LOG_NOTICE,"Assigned PTPP device 1 to repeater [%s]",str);
time(&rdacList[rdacPos].lastPTPPConnect);
break;}
case 0x11:{ //Request to startup DMR received
int rdacPos;
//See if the repeater is already known in the RDAC list
if(isDiscarded(cliaddr)) continue;
rdacPos = findRdacRepeater(cliaddr);
if (difftime(timeNow,rdacList[rdacPos].lastDMRConnect) < 10) continue; //Ignore connect request
time(&rdacList[rdacPos].lastDMRConnect);
syslog(LOG_NOTICE,"DMR request from repeater [%s]",str);
if (rdacPos == 99){ //If not ignore the DMR request
syslog(LOG_NOTICE,"DMR request from repeater not in RDAC list [%s], ignoring",str);
continue;
}
//See if RDAC info from the repeater has already been received
if (!rdacList[rdacPos].id > 0){ //If not ignore DMR request
syslog(LOG_NOTICE,"RDAC info not received from repeater yet [%s], ignoring",str);
continue;
}
//Now that we have repeater info, initialize the repeater
repPos = initRepeater(rdacList[rdacPos]);
if (repPos == 99) continue; //If 99 returned, more repeaters then allowed
memcpy(response,buffer,n);
//Assign device ID
response[4]++;
response[13]=0x01;
response[n] = 0x01;
cliaddr.sin_port=htons(port);
sendto(sockfd,response,n+1,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
syslog(LOG_NOTICE,"Assigned DMR device 1 to repeater [%s - %s]",str,repeaterList[repPos].callsign);
//Assign port number
response[4] = 0x0b;
unsigned char rep[] = {0x00,0xff,0x01,0x00};
memcpy(response + 12,rep,4);
unsigned char rport[] = {0xff,0x01,0x00,0x00};
redirectPort = baseDmrPort + repPos;
rport[2] = redirectPort;
rport[3] = redirectPort >> 8;
memcpy(response + n,rport,4);
if (repeaterList[repPos].dmrOnline){ //If repeater is not offline, but we still get a request, just point it back to old thread
syslog(LOG_NOTICE,"DMR request from repeater [%s - %s] already assigned a DMR port, not starting thread",str,repeaterList[repPos].callsign);
}
else{ //Start a new DMR thread for this repeater
pthread_create(&thread, NULL, dmrListener,(void *)repPos);
}
sendto(sockfd,response,n+4,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
syslog(LOG_NOTICE,"Re-directed repeater [%s - %s] to DMR port %i",str,repeaterList[repPos].callsign,redirectPort);
break;}
case 0x12:{ ////Request to startup RDAC received
int rdacPos;
//Initialize this repeater for RDAC
if(isDiscarded(cliaddr)) continue;
rdacPos = setRdacRepeater(cliaddr);
if (difftime(timeNow,rdacList[rdacPos].lastRDACConnect) < 10) continue; //Ignore connect request
syslog(LOG_NOTICE,"RDAC request from repeater [%s]",str);
if (rdacPos == 99) continue; //If 99 returned, more repeaters then allowed
memcpy(response,buffer,n);
//Assign device ID
response[4]++;
response[13]=0x01;
response[n] = 0x01;
cliaddr.sin_port=htons(port);
sendto(sockfd,response,n+1,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
syslog(LOG_NOTICE,"Assigned RDAC device 1 to repeater [%s]",str);
//Assign port number
response[4] = 0x0b;
unsigned char rep[] = {0xff,0xff,0x01,0x00};
memcpy(response + 12,rep,4);
unsigned char port[] = {0xff,0x01,0x00,0x00};
redirectPort = baseRdacPort + (rdacPos * 2);
port[2] = redirectPort;
port[3] = redirectPort >> 8;
memcpy(response + n,port,4);
if (rdacList[rdacPos].rdacOnline){ //If repeater is not offline, but we still get a request, just point it back to old thread
syslog(LOG_NOTICE,"RDAC request from repeater [%s - %s] already assigned a RDAC port, not starting thread",str,rdacList[rdacPos].callsign);
}
else{ //Start a new RDAC thread for this repeater
struct sockInfo *param = malloc(sizeof(struct sockInfo));
param->address = cliaddr;
param->port = redirectPort;
pthread_create(&thread, NULL, rdacListener,param);
}
sendto(sockfd,response,n+4,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
syslog(LOG_NOTICE,"Re-directed repeater [%s] to RDAC port %i",str,redirectPort);
time(&rdacList[rdacPos].lastRDACConnect);
break;}
}
}
if ((memcmp(buffer,ping,sizeof(ping)) == 0 || memcmp(buffer,ping2,sizeof(ping2)) == 0) && repeaterList[findRepeater(cliaddr)].dmrOnline){//Is this a heartbeat from a repeater on the service port ? And do we know the repeater ?
memcpy(response,buffer,n);
response[12]++;
sendto(sockfd,response,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
if (restart){
syslog(LOG_NOTICE,"Exiting serviceListener (restart)");
return;
}
}
}
else{
if (restart){
syslog(LOG_NOTICE,"Exiting serviceListener (restart)");
return;
}
}
}
}
int getMasterInfo(){
//Get the needed info from the SQLITE database
unsigned char SQLQUERY[200] = {0};
sqlite3_stmt *stmt;
sprintf(SQLQUERY,"SELECT ownName,ownCountryCode,ownRegion,sMasterIp,sMasterPort FROM sMaster");
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
sprintf(master.ownName,"%s",sqlite3_column_text(stmt,0));
sprintf(master.ownCountryCode,"%s",sqlite3_column_text(stmt,1));
sprintf(master.ownRegion,"%s",sqlite3_column_text(stmt,2));
sprintf(master.sMasterIp,"%s",sqlite3_column_text(stmt,3));
sprintf(master.sMasterPort,"%s",sqlite3_column_text(stmt,4));
master.ownCCInt = atoi(sqlite3_column_text(stmt,1));
master.ownRegionInt = atoi(sqlite3_column_text(stmt,2));
}
else{
syslog(LOG_NOTICE,"failed to read sMasterInfo, no row");
return 0;
}
}
else{
syslog(LOG_NOTICE,"failed to read sMasterInfo, query bad");
return 0;
}
sqlite3_finalize(stmt);
syslog(LOG_NOTICE,"sMaster info: ownName %s, ownCountryCode %s, ownRegion %s, sMasterIp %s, sMasterPort %s",
master.ownName,master.ownCountryCode,master.ownRegion,master.sMasterIp,master.sMasterPort);
sprintf(SQLQUERY,"SELECT servicePort, rdacPort, dmrPort, baseDmrPort, maxRepeaters, echoId FROM master");
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
servicePort = sqlite3_column_int(stmt,0);
rdacPort = sqlite3_column_int(stmt,1);
dmrPort = sqlite3_column_int(stmt,2);
baseDmrPort = sqlite3_column_int(stmt,3);
maxRepeaters = sqlite3_column_int(stmt,4) + 1;
echoId = sqlite3_column_int(stmt,5);
}
else{
syslog(LOG_NOTICE,"failed to read masterInfo, no row");
sqlite3_finalize(stmt);
return 0;
}
}
else{
syslog(LOG_NOTICE,"failed to read masterInfo, query bad");
sqlite3_finalize(stmt);
return 0;
}
syslog(LOG_NOTICE,"ServicePort %i rdacPort %i dmrPort %i baseDmrPort %i maxRepeaters %i echoId %i",
servicePort,rdacPort,dmrPort,baseDmrPort,maxRepeaters-1,echoId);
if (maxRepeaters > 98){
syslog(LOG_NOTICE,"maxRepeaters exceeded 98, quiting application");
sqlite3_finalize(stmt);
return 0;
}
sqlite3_finalize(stmt);
return 1;
}
int loadTalkGroups(){
//Loading the allowed talkgroups from the SQLITE database
unsigned char SQLQUERY[200] = {0};
char *lineread;
int i;
int size = 10;
sqlite3_stmt *stmt;
sMasterTS1List = malloc ((sizeof *sMasterTS1List) * size);
sMasterTS2List = malloc ((sizeof *sMasterTS2List) * size);
repTS1List = malloc ((sizeof *repTS1List) * size);
repTS2List = malloc ((sizeof *repTS2List) * size);
unsigned char sMasterTS1[100];
unsigned char sMasterTS2[100];
unsigned char repTS1[100];
unsigned char repTS2[100];
sprintf(SQLQUERY,"SELECT repTS1,repTS2,sMasterTS1,sMasterTS2 FROM master");
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
sprintf(tsInfo.repTS1,"%s",sqlite3_column_text(stmt,0));
sprintf(tsInfo.repTS2,"%s",sqlite3_column_text(stmt,1));
sprintf(tsInfo.sMasterTS1,"%s",sqlite3_column_text(stmt,2));
sprintf(tsInfo.sMasterTS2,"%s",sqlite3_column_text(stmt,3));
sqlite3_finalize(stmt);
memcpy(sMasterTS1,tsInfo.sMasterTS1,sizeof(sMasterTS1));
memcpy(sMasterTS2,tsInfo.sMasterTS2,sizeof(sMasterTS2));
memcpy(repTS1,tsInfo.repTS1,sizeof(repTS1));
memcpy(repTS2,tsInfo.repTS2,sizeof(repTS2));
//Assign the talkgroups that we allow on TS1 to and from sMaster
if (lineread = strtok(sMasterTS1,",")){
if (strstr(lineread,"**")){ //If ** in talkgroup, this is a range of 100
sMasterTS1List[0][0] = atoi(lineread) * 100;
sMasterTS1List[0][1] = (atoi(lineread) * 100) + 99;
sprintf(master.announcedCC1,"%4s",master.ownCountryCode);
}
else{
sMasterTS1List[0][0] = atoi(lineread);
sMasterTS1List[0][1] = atoi(lineread);
sprintf(master.announcedCC1,"%4s",lineread);
master.sMasterTS1GroupCount++;
}
while (lineread = strtok(NULL,",")){
if (master.sMasterTS1GroupCount > size) sMasterTS1List = realloc(sMasterTS1List, (sizeof *sMasterTS1List) * master.sMasterTS1GroupCount);
if (strstr(lineread,"**")){ //If ** in talkgroup, this is a range of 100
sMasterTS1List[master.sMasterTS1GroupCount][0] = atoi(lineread) * 100;
sMasterTS1List[master.sMasterTS1GroupCount][1] = (atoi(lineread) * 100) + 99;
sprintf(master.announcedCC1,"%s%4s",master.announcedCC1,master.ownCountryCode);
}
else{
sMasterTS1List[master.sMasterTS1GroupCount][0] = atoi(lineread);
sMasterTS1List[master.sMasterTS1GroupCount][1] = atoi(lineread);
sprintf(master.announcedCC1,"%s%4s",master.announcedCC1,lineread);
}
master.sMasterTS1GroupCount++;
}
}
if (master.sMasterTS1GroupCount < 10){ //If not 10 talkgroups for TS1, fill with 0 to report to sMaster
for (i=1;i<(10-master.sMasterTS1GroupCount);i++){
sprintf(master.announcedCC1,"%s 0",master.announcedCC1);
}
}
//Assign the talkgroups that we allow on TS2 to and from sMaster
if (lineread = strtok(sMasterTS2,",")){
if (strstr(lineread,"**")){//If ** in talkgroup, this is a range of 100
sMasterTS2List[0][0] = atoi(lineread) * 100;
sMasterTS2List[0][1] = (atoi(lineread) * 100) + 99;
sprintf(master.announcedCC2,"%4s",master.ownCountryCode);
}
else{
sMasterTS2List[0][0] = atoi(lineread);
sMasterTS2List[0][1] = atoi(lineread);
sprintf(master.announcedCC2,"%4s",lineread);
}
master.sMasterTS2GroupCount++;
while (lineread = strtok(NULL,",")){
if (master.sMasterTS2GroupCount > size) sMasterTS2List = realloc(sMasterTS2List, (sizeof *sMasterTS2List) * master.sMasterTS2GroupCount);
if (strstr(lineread,"**")){//If ** in talkgroup, this is a range of 100
sMasterTS2List[master.sMasterTS2GroupCount][0] = atoi(lineread) * 100;
sMasterTS2List[master.sMasterTS2GroupCount][1] = (atoi(lineread) * 100) + 99;
sprintf(master.announcedCC2,"%s%4s",master.announcedCC2,master.ownCountryCode);
}
else{
sMasterTS2List[master.sMasterTS2GroupCount][0] = atoi(lineread);
sMasterTS2List[master.sMasterTS2GroupCount][1] = atoi(lineread);
sprintf(master.announcedCC2,"%s%4s",master.announcedCC2,lineread);
}
master.sMasterTS2GroupCount++;
}
}
if (master.sMasterTS2GroupCount < 10){//If not 10 talkgroups for TS2, fill with 0 to report to sMaster
for (i=1;i<(10-master.sMasterTS2GroupCount);i++){
sprintf(master.announcedCC2,"%s 0",master.announcedCC2);
}
}
//Assign the talkgroups allowed between repeaters on TS1
if (lineread = strtok(repTS1,",")){
if (strstr(lineread,"**")){
repTS1List[0][0] = atoi(lineread) * 100;
repTS1List[0][1] = (atoi(lineread) * 100) + 99;
}
else{
repTS1List[0][0] = atoi(lineread);
repTS1List[0][1] = atoi(lineread);
}
master.repTS1GroupCount++;
while (lineread = strtok(NULL,",")){
if (master.repTS1GroupCount > size) repTS1List = realloc(repTS1List, (sizeof *repTS1List) * master.repTS1GroupCount);
if (strstr(lineread,"**")){//If ** in talkgroup, this is a range of 100
repTS1List[master.repTS1GroupCount][0] = atoi(lineread) * 100;
repTS1List[master.repTS1GroupCount][1] = (atoi(lineread) * 100) + 99;
}
else{
repTS1List[master.repTS1GroupCount][0] = atoi(lineread);
repTS1List[master.repTS1GroupCount][1] = atoi(lineread);
}
master.repTS1GroupCount++;
}
}
if (lineread = strtok(repTS2,",")){
if (strstr(lineread,"**")){//If ** in talkgroup, this is a range of 100
repTS2List[0][0] = atoi(lineread) * 100;
repTS2List[0][1] = (atoi(lineread) * 100) + 99;
}
else{
repTS2List[0][0] = atoi(lineread);
repTS2List[0][1] = atoi(lineread);
}
master.repTS2GroupCount++;
while (lineread = strtok(NULL,",")){
if (master.repTS2GroupCount > size) repTS2List = realloc(repTS2List, (sizeof *repTS2List) * master.repTS2GroupCount);
if (strstr(lineread,"**")){
repTS2List[master.repTS2GroupCount][0] = atoi(lineread) * 100;
repTS2List[master.repTS2GroupCount][1] = (atoi(lineread) * 100) + 99;
}
else{
repTS2List[master.repTS2GroupCount][0] = atoi(lineread);
repTS2List[master.repTS2GroupCount][1] = atoi(lineread);
}
master.repTS2GroupCount++;
}
}
//Below code just to show the loaded talkgroups in syslog
syslog(LOG_NOTICE,"sMaster talk groups TS1");
if (master.sMasterTS1GroupCount>0){
for (i=0;i<master.sMasterTS1GroupCount;i++){
syslog(LOG_NOTICE,"(%i) %i - %i",i,sMasterTS1List[i][0],sMasterTS1List[i][1]);
}
}
else syslog(LOG_NOTICE,"NONE");
syslog(LOG_NOTICE,"Announced country codes TS1|%s|",master.announcedCC1);
syslog(LOG_NOTICE,"sMaster talk groups TS2");
if (master.sMasterTS2GroupCount>0){
for (i=0;i<master.sMasterTS2GroupCount;i++){
syslog(LOG_NOTICE,"(%i) %i - %i",i,sMasterTS2List[i][0],sMasterTS2List[i][1]);
}
}
else syslog(LOG_NOTICE,"NONE");
syslog(LOG_NOTICE,"Announced country codes TS2|%s|",master.announcedCC2);
syslog(LOG_NOTICE,"repTS1 talk groups");
if (master.repTS1GroupCount>0){
for (i=0;i<master.repTS1GroupCount;i++){
syslog(LOG_NOTICE,"(%i) %i - %i",i,repTS1List[i][0],repTS1List[i][1]);
}
}
else syslog(LOG_NOTICE,"NONE");
syslog(LOG_NOTICE,"repTS2 talk groups");
if (master.repTS2GroupCount>0){
for (i=0;i<master.repTS2GroupCount;i++){
syslog(LOG_NOTICE,"(%i) %i - %i",i,repTS2List[i][0],repTS2List[i][1]);
}
}
else syslog(LOG_NOTICE,"NONE");
return 1;
}
}
sqlite3_finalize(stmt);
syslog(LOG_NOTICE,"Failed to load talkgroups. Is master table populated in database ?");
return 0;
}
int main(int argc, char**argv)
{
// Our process ID and Session ID
pid_t pid,sid;
printf("Becoming a daemon...\n");
// Fork off the parent process
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
// If we got a good PID, then we can exit the parent process.
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// Change the file mode mask
umask(0);
setlogmask (LOG_UPTO (LOG_NOTICE));
openlog("Master-server", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
// Create a new SID for the child process
sid = setsid();
if (sid < 0) {
// Log the failure
exit(EXIT_FAILURE);
}
// Close out the standard file descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
pthread_t thread;
int port;
int dbInit;
int i;
db = openDatabase();
//Init and create sqlite database if needed
dbInit = initDatabase(db);
if (dbInit == 0){
syslog(LOG_NOTICE,"Failed to init database");
return 0;
}
//Start webserver thread
pthread_create(&thread, NULL, webServerListener,NULL);
for(;;){
restart = 0;
dmrState[1] = IDLE;
dmrState[2] = IDLE;
//Get info to get us going
if(!getMasterInfo()) return 0;
//Load the allowed talkgroups
if(!loadTalkGroups()) return 0;
//Start sMaster Thread
pthread_create(&thread, NULL, sMasterThread,NULL);
//Start listening on the service port
serviceListener(servicePort);
//If we got here, we are restarting, waiting for all threads to end
sleep(6);
master = emptyMaster;
highestRepeater = 0;
for (i=0;i<99;i++){
repeaterList[i] = emptyRepeater;
rdacList[i] = emptyRepeater;
}
}
}