Added GSM network support.
[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/openbsc.h"
14
15
16 char *gsm_conf_error = "";
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         {
53                 SPRINT(gsm_conf_error, "Cannot open %s\n",filename);
54                 return(0);
55         }
56
57         line=0;
58         while((fgets(buffer,sizeof(buffer),fp)))
59         {
60                 line++;
61                 buffer[sizeof(buffer)-1]=0;
62                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
63                 p=buffer;
64
65                 while(*p <= 32) /* skip spaces */
66                 {
67                         if (*p == 0)
68                                 break;
69                         p++;
70                 }
71                 if (*p==0 || *p=='#') /* ignore comments and empty line */
72                         continue;
73
74                 option[0]=0;
75                 i=0; /* read option */
76                 while(*p > 32)
77                 {
78                         if (i+1 >= sizeof(option))
79                         {
80                                 SPRINT(gsm_conf_error, "Error in %s (line %d): option too long.\n",filename,line);
81                                 goto error;
82                         }
83                         option[i+1] = '\0';
84                         option[i++] = *p++;
85                 }
86
87                 while(*p <= 32) /* skip spaces */
88                 {
89                         if (*p == 0)
90                                 break;
91                         p++;
92                 }
93
94                 params[0][0] = 0;
95                 pnum = 0;
96                 while(*p!=0 && *p!='#' && pnum < 10) /* param */
97                 {
98                         i=0; /* read param */
99                         while(*p > 32)
100                         {
101                                 if (i+1 >= sizeof(params[pnum]))
102                                 {
103                                         SPRINT(gsm_conf_error, "Error in %s (line %d): param too long.\n",filename,line);
104                                         goto error;
105                                 }
106                                 params[pnum][i+1] = '\0';
107                                 params[pnum][i++] = *p++;
108                         }
109                         while(*p <= 32) /* skip spaces */
110                         {
111                                 if (*p == 0)
112                                         break;
113                                 p++;
114                         }
115                         pnum++;
116                         params[pnum][0] = 0;
117                 }
118
119                 /* at this point we have option and param */
120
121                 /* check option */
122                 if (!strcmp(option,"debug"))
123                 {
124                         if (params[0][0]==0)
125                         {
126                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
127                                 goto error;
128                         }
129                         SCPY(gsm_conf->debug, params[0]);
130
131                 } else
132                 if (!strcmp(option,"interface-bsc"))
133                 {
134                         if (params[0][0]==0)
135                         {
136                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
137                                 goto error;
138                         }
139                         SCPY(gsm_conf->interface_bsc, params[0]);
140
141                 } else
142                 if (!strcmp(option,"interface-lcr"))
143                 {
144                         if (params[0][0]==0)
145                         {
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->interface_lcr, params[0]);
150
151                 } else
152                 if (!strcmp(option,"short-name"))
153                 {
154                         if (params[0][0]==0)
155                         {
156                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
157                                 goto error;
158                         }
159                         SCPY(gsm_conf->short_name, params[0]);
160
161                 } else
162                 if (!strcmp(option,"long-name"))
163                 {
164                         if (params[0][0]==0)
165                         {
166                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
167                                 goto error;
168                         }
169                         SCPY(gsm_conf->long_name, params[0]);
170
171                 } else
172                 if (!strcmp(option,"mcc"))
173                 {
174                         if (params[0][0]==0)
175                         {
176                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
177                                 goto error;
178                         }
179                         gsm_conf->mcc = atoi(params[0]);
180
181                 } else
182                 if (!strcmp(option,"mnc"))
183                 {
184                         if (params[0][0]==0)
185                         {
186                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
187                                 goto error;
188                         }
189                         gsm_conf->mnc = atoi(params[0]);
190
191                 } else
192                 if (!strcmp(option,"lac"))
193                 {
194                         if (params[0][0]==0)
195                         {
196                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
197                                 goto error;
198                         }
199                         gsm_conf->lac = atoi(params[0]);
200
201                 } else
202                 if (!strcmp(option,"hlr"))
203                 {
204                         if (params[0][0]==0)
205                         {
206                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
207                                 goto error;
208                         }
209                         SCPY(gsm_conf->hlr, params[0]);
210
211                 } else
212                 if (!strcmp(option,"allow-all"))
213                 {
214                         gsm_conf->allow_all = 1;
215                 } else
216                 if (!strcmp(option,"keep-l2"))
217                 {
218                         gsm_conf->keep_l2 = 1;
219
220                 } else
221                 if (!strcmp(option,"no-mergency-shutdown"))
222                 {
223                         gsm_conf->noemergshut = 1;
224                 } else
225                 if (!strcmp(option,"bts"))
226                 {
227                         if (gsm_conf->numbts == 8)
228                         {
229                                 SPRINT(gsm_conf_error, "Error in %s (line %d): too many BTS defined.\n",filename,line);
230                                 goto error;
231                         }
232                         if (params[0][0]==0)
233                         {
234                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter <bts-type> for option %s missing.\n",filename,line,option);
235                                 goto error;
236                         }
237                         if (params[1][0]==0)
238                         {
239                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter <card number> for option %s missing.\n",filename,line,option);
240                                 goto error;
241                         }
242                         if (params[2][0]==0)
243                         {
244                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter <frequency> for option %s missing.\n",filename,line,option);
245                                 goto error;
246                         }
247                         if (!strcmp(params[0], "bs11"))
248                         {
249                                 gsm_conf->bts[gsm_conf->numbts].type = GSM_BTS_TYPE_BS11;
250                         } else {
251                                 SPRINT(gsm_conf_error, "Error in %s (line %d): unknown BTS type '%s'.\n",filename,line,params[0]);
252                                 goto error;
253                         }
254                         gsm_conf->bts[gsm_conf->numbts].card = atoi(params[1]);
255                         gsm_conf->bts[gsm_conf->numbts].numtrx = 0;
256                         while (params[gsm_conf->bts[gsm_conf->numbts].numtrx+2][0])
257                         {
258                                 if (gsm_conf->bts[gsm_conf->numbts].numtrx == 8)
259                                 {
260                                         SPRINT(gsm_conf_error, "Error in %s (line %d): too many frequencies defined.\n",filename,line);
261                                         goto error;
262                                 }
263                                 gsm_conf->bts[gsm_conf->numbts].frequency[gsm_conf->bts[gsm_conf->numbts].numtrx++] = atoi(params[gsm_conf->bts[gsm_conf->numbts].numtrx+2]);
264                         }
265                         gsm_conf->numbts++;
266                 } else
267                 {
268                         SPRINT(gsm_conf_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
269                         goto error;
270                 }
271         }
272
273         if (fp) fclose(fp);
274         return(1);
275 error:
276         if (fp) fclose(fp);
277         return(0);
278 }
279
280