2f46def0586c00d51eb4e9a3b56cf595ba46ef5a
[lcr.git] / gsm_conf.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading options.conf and filling structure                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14
15 char *gsm_conf_error = (char *)"";
16
17 /* read options
18  *
19  * read options from options.conf
20  */
21 int gsm_conf(struct gsm_conf *gsm_conf)
22 {
23         FILE *fp=NULL;
24         char filename[128];
25         char *p;
26         char option[32];
27         char params[11][256];
28         int pnum;
29         unsigned int line,i;
30         char buffer[256];
31
32         /* set defaults */
33         SCPY(gsm_conf->debug, "");
34         SCPY(gsm_conf->interface_bsc, "mISDN_l1loop.1");
35         SCPY(gsm_conf->interface_lcr, "mISDN_l1loop.2");
36         SCPY(gsm_conf->short_name, "LCR");
37         SCPY(gsm_conf->long_name, "Linux-Call-Router");
38         gsm_conf->mcc = 1;
39         gsm_conf->mnc = 1;
40         gsm_conf->lac = 1;
41         SCPY(gsm_conf->hlr, "hlr.sqlite3");
42         gsm_conf->allow_all = 0;
43         gsm_conf->keep_l2 = 0;
44         gsm_conf->numbts = 0;
45         //gsm_conf->bts[xx]
46         gsm_conf->noemergshut = 0;
47
48         SPRINT(filename, "%s/gsm.conf", CONFIG_DATA);
49
50         if (!(fp=fopen(filename,"r"))) {
51                 SPRINT(gsm_conf_error, "Cannot open %s\n",filename);
52                 return(0);
53         }
54
55         line=0;
56         while((fgets(buffer,sizeof(buffer),fp))) {
57                 line++;
58                 buffer[sizeof(buffer)-1]=0;
59                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
60                 p=buffer;
61
62                 while(*p <= 32) { /* skip spaces */
63                         if (*p == 0)
64                                 break;
65                         p++;
66                 }
67                 if (*p==0 || *p=='#') /* ignore comments and empty line */
68                         continue;
69
70                 option[0]=0;
71                 i=0; /* read option */
72                 while(*p > 32) {
73                         if (i+1 >= sizeof(option)) {
74                                 SPRINT(gsm_conf_error, "Error in %s (line %d): option too long.\n",filename,line);
75                                 goto error;
76                         }
77                         option[i+1] = '\0';
78                         option[i++] = *p++;
79                 }
80
81                 while(*p <= 32) { /* skip spaces */
82                         if (*p == 0)
83                                 break;
84                         p++;
85                 }
86
87                 params[0][0] = 0;
88                 pnum = 0;
89                 while(*p!=0 && *p!='#' && pnum < 10) { /* param */
90                         i=0; /* read param */
91                         while(*p > 32) {
92                                 if (i+1 >= sizeof(params[pnum])) {
93                                         SPRINT(gsm_conf_error, "Error in %s (line %d): param too long.\n",filename,line);
94                                         goto error;
95                                 }
96                                 params[pnum][i+1] = '\0';
97                                 params[pnum][i++] = *p++;
98                         }
99                         while(*p <= 32) { /* skip spaces */
100                                 if (*p == 0)
101                                         break;
102                                 p++;
103                         }
104                         pnum++;
105                         params[pnum][0] = 0;
106                 }
107
108                 /* at this point we have option and param */
109
110                 /* check option */
111                 if (!strcmp(option,"debug")) {
112                         if (params[0][0]==0) {
113                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
114                                 goto error;
115                         }
116                         SCPY(gsm_conf->debug, params[0]);
117
118                 } else
119                 if (!strcmp(option,"interface-bsc")) {
120                         if (params[0][0]==0) {
121                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
122                                 goto error;
123                         }
124                         SCPY(gsm_conf->interface_bsc, params[0]);
125
126                 } else
127                 if (!strcmp(option,"interface-lcr")) {
128                         if (params[0][0]==0) {
129                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
130                                 goto error;
131                         }
132                         SCPY(gsm_conf->interface_lcr, params[0]);
133
134                 } else
135                 if (!strcmp(option,"short-name")) {
136                         if (params[0][0]==0) {
137                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
138                                 goto error;
139                         }
140                         SCPY(gsm_conf->short_name, params[0]);
141
142                 } else
143                 if (!strcmp(option,"long-name")) {
144                         if (params[0][0]==0) {
145                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
146                                 goto error;
147                         }
148                         SCPY(gsm_conf->long_name, params[0]);
149
150                 } else
151                 if (!strcmp(option,"mcc")) {
152                         if (params[0][0]==0) {
153                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
154                                 goto error;
155                         }
156                         gsm_conf->mcc = atoi(params[0]);
157
158                 } else
159                 if (!strcmp(option,"mnc")) {
160                         if (params[0][0]==0) {
161                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
162                                 goto error;
163                         }
164                         gsm_conf->mnc = atoi(params[0]);
165
166                 } else
167                 if (!strcmp(option,"lac")) {
168                         if (params[0][0]==0) {
169                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
170                                 goto error;
171                         }
172                         gsm_conf->lac = atoi(params[0]);
173
174                 } else
175                 if (!strcmp(option,"hlr")) {
176                         if (params[0][0]==0) {
177                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
178                                 goto error;
179                         }
180                         SCPY(gsm_conf->hlr, params[0]);
181
182                 } else
183                 if (!strcmp(option,"allow-all")) {
184                         gsm_conf->allow_all = 1;
185                 } else
186                 if (!strcmp(option,"keep-l2")) {
187                         gsm_conf->keep_l2 = 1;
188
189                 } else
190                 if (!strcmp(option,"no-mergency-shutdown")) {
191                         gsm_conf->noemergshut = 1;
192                 } else
193                 if (!strcmp(option,"pcapfile")) {
194                         if (params[0][0]==0) {
195                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
196                                 goto error;
197                         }
198                         SCPY(gsm_conf->pcapfile, params[0]);
199                 } else
200                 if (!strcmp(option,"bts")) {
201                         if (gsm_conf->numbts == 8) {
202                                 SPRINT(gsm_conf_error, "Error in %s (line %d): too many BTS defined.\n",filename,line);
203                                 goto error;
204                         }
205                         if (params[0][0]==0) {
206                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter <bts-type> for option %s missing.\n",filename,line,option);
207                                 goto error;
208                         }
209                         if (params[1][0]==0) {
210                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter <card number> for option %s missing.\n",filename,line,option);
211                                 goto error;
212                         }
213                         if (params[2][0]==0) {
214                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter <frequency> for option %s missing.\n",filename,line,option);
215                                 goto error;
216                         }
217                         if (!strcmp(params[0], "bs11")) {
218                                 gsm_conf->bts[gsm_conf->numbts].type = GSM_BTS_TYPE_BS11;
219                         } else {
220                                 SPRINT(gsm_conf_error, "Error in %s (line %d): unknown BTS type '%s'.\n",filename,line,params[0]);
221                                 goto error;
222                         }
223                         gsm_conf->bts[gsm_conf->numbts].card = atoi(params[1]);
224                         gsm_conf->bts[gsm_conf->numbts].numtrx = 0;
225                         while (params[gsm_conf->bts[gsm_conf->numbts].numtrx+2][0]) {
226                                 if (gsm_conf->bts[gsm_conf->numbts].numtrx == 8) {
227                                         SPRINT(gsm_conf_error, "Error in %s (line %d): too many frequencies defined.\n",filename,line);
228                                         goto error;
229                                 }
230                                 gsm_conf->bts[gsm_conf->numbts].frequency[gsm_conf->bts[gsm_conf->numbts].numtrx++] = atoi(params[gsm_conf->bts[gsm_conf->numbts].numtrx+2]);
231                         }
232                         gsm_conf->numbts++;
233                 } else {
234                         SPRINT(gsm_conf_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
235                         goto error;
236                 }
237         }
238
239         if (fp) fclose(fp);
240         return(1);
241 error:
242         if (fp) fclose(fp);
243         return(0);
244 }
245
246