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