forked from kb1isz/linDmrMaster
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqlite.c
248 lines (221 loc) · 9.91 KB
/
sqlite.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
/*
* 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"
sqlite3 *openDatabase(){
int rc;
sqlite3 *db;
rc = sqlite3_open("Master_Server.sql", &db);
if (rc){
syslog(LOG_NOTICE,"Cant't open database: %s",sqlite3_errmsg(db));
sqlite3_close(db);
return NULL;
}
return db;
}
void closeDatabase(sqlite3 *db){
if (sqlite3_close(db) != SQLITE_OK) syslog(LOG_NOTICE,"Failed to close SQLITE database");
}
bool isFieldExisting(sqlite3 *db, char *table, char *field){
sqlite3_stmt *stmt;
char SQLQUERY[200];
syslog(LOG_NOTICE,"Database, checking column '%s' in table '%s'",field,table);
sprintf(SQLQUERY,"PRAGMA table_info(%s)",table);
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
while (sqlite3_step(stmt) == SQLITE_ROW){
if (strcmp(sqlite3_column_text(stmt,1),field) == 0){
sqlite3_finalize(stmt);
return true;
}
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
syslog(LOG_NOTICE,"field %s doesn't exist",field);
return false;
}
bool isTableExisting(sqlite3 *db, char *table){
sqlite3_stmt *stmt;
char SQLQUERY[200];
syslog(LOG_NOTICE,"Database, checking table '%s'",table);
sprintf(SQLQUERY,"SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '%s'",table);
if (sqlite3_prepare_v2(db,SQLQUERY,-1,&stmt,0) == 0){
if (sqlite3_step(stmt) == SQLITE_ROW){
if(sqlite3_column_int(stmt,0) == 0){
syslog(LOG_NOTICE,"table %s doesn't exist",table);
sqlite3_finalize(stmt);
return false;
}
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
int initDatabase(sqlite3 *db){
char SQLQUERY[1000];
if (!isTableExisting(db,"master")){
sprintf(SQLQUERY,"CREATE TABLE master (repTS1 VARCHAR(100) default '',repTS2 VARCHAR(100) default '',sMasterTS1 VARCHAR(100) default '',sMasterTS2 VARCHAR(100) default '', timeBase INTEGER default 60, servicePort int default 50000, rdacPort int default 50002,dmrPort int default 50001, baseDmrPort int default 50100, baseRdacPort int default 50200, maxRepeaters int default 20, echoId int default 9990, rrsGpsId int default 500, aprsUrl VARCHAR(100) default '', aprsPort VARCHAR(7) default '8080')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
sprintf(SQLQUERY,"INSERT INTO master (repTS1) VALUES ('')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"sMaster")){
sprintf(SQLQUERY,"CREATE TABLE sMaster (ownName VARCHAR(100) default '',ownCountryCode VARCHAR(5) default '',ownRegion VARCHAR(2) default '',sMasterIp VARCHAR(100) default '', sMasterPort VARCHAR(5) default '62010')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
sprintf(SQLQUERY,"INSERT INTO sMaster (ownName) VALUES ('')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table sMaster created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"repeaters")){
sprintf(SQLQUERY,"CREATE TABLE repeaters (repeaterId INTEGER default 0 ,callsign VARCHAR(10) default '',txFreq VARCHAR(10) default '',shift VARCHAR(7) default '', hardware VARCHAR(11) default '', firmware VARCHAR(12) default '', mode VARCHAR(4) default '', currentAddress INTEGER default 0, timeStamp varchar(20) default '1970-1-1 00:00:00', ipAddress VARCHAR(50) default '',language VARCHAR(50) default 'english', geoLocation VARCHAR(20) default '', aprsPass VARCHAR(5) default '0000', aprsBeacon VARCHAR(100) default 'DMR repeater', aprsPHG VARCHHAR(7) default '')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table repeater created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"callsigns")){
sprintf(SQLQUERY,"CREATE TABLE callsigns (radioId INTEGER default 0 PRIMARY KEY, callsign VARCHAR(32) default '', name VARCHAR(32) default '', aprsSuffix VARCHAR(3) default '-9', aprsBeacon VARCHAR(100) default 'DMR terminal', aprsSymbol INTEGER default 62, hasSendAprs INTEGER default 0, messageStore INTEGER default 1, email VARCHAR(100) default '', login VARCHAR(50) default '', password VARCHAR(50) default '', lastAprsTime INTEGER default 0, madeChange INTEGER default 0, city VARCHAR(32) default '', state VARCHAR(32) default '', country VARCHAR(32) default '', radio VARCHAR(32) default '', homeRepeaterId VARCHAR(32) default '', remarks VARCHAR(32) default '')");
if (sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table callsigns created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"rrs")){
sprintf(SQLQUERY,"CREATE TABLE rrs (radioId INTEGER default 0 PRIMARY KEY, callsign VARCHAR(32) default '',name VARCHAR(32) default '', registerTime VARCHAR(20) default '1970-01-01 00:00:00', onRepeater VARCHAR(32) default '')");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table rrs created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"traffic")){
sprintf(SQLQUERY,"CREATE TABLE traffic (senderId int default 0 PRIMARY KEY,senderCallsign varchar(32) default '',targetId int default 0,targetCallsign varchar(32) default '',channel int default 0,serviceType varchar(15) default 'Voice',callType varchar(15) default 'Group',timeStamp int default 0, onRepeater varchar(32) default '', senderName varchar(32) default '')");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table traffic created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"voiceTraffic")){
sprintf(SQLQUERY,"CREATE TABLE voiceTraffic (senderId int default 0 PRIMARY KEY,senderCallsign varchar(32) default '',targetId int default 0,targetCallsign varchar(32) default '',channel int default 0,serviceType varchar(15) default 'Voice',callType varchar(15) default 'Group',timeStamp int default 0, onRepeater varchar(32) default '', senderName varchar(32) default '')");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table voiceTraffic created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isTableExisting(db,"localReflectors")){
sprintf(SQLQUERY,"CREATE TABLE localReflectors(id int primary key,name varchar(50))");
if(sqlite3_exec(db,SQLQUERY,NULL,NULL,NULL) == 0){
syslog(LOG_NOTICE,"Table localReflectors created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
//check for new fields added later in development
if (!isFieldExisting(db,"repeaters","currentReflector")){
sprintf(SQLQUERY,"alter table repeaters add currentReflector integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field currentreflector in repeaters created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"repeaters","autoReflector")){
sprintf(SQLQUERY,"alter table repeaters add autoReflector integer default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field autoReflector in repeaters created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"master","echoSlot")){
sprintf(SQLQUERY,"alter table master add echoSlot integer default 1");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field echoSlot in master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
if (!isFieldExisting(db,"rrs","unixTime")){
sprintf(SQLQUERY,"alter table rrs unixTime long default 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"field echoSlot in master created");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
return 0;
}
}
//Clean database
sprintf(SQLQUERY,"update repeaters set currentReflector = 0");
if (sqlite3_exec(db,SQLQUERY,0,0,0) == 0){
syslog(LOG_NOTICE,"Reflector status cleaned");
}
else{
syslog(LOG_NOTICE,"Database error: %s",sqlite3_errmsg(db));
}
return 1;
}