Debian Codegraph @

src/osmo-bts-oc2g/misc/oc2gbts_mgr_calib.c

raw
1 /* OCXO calibration control for OC-2G BTS management daemon */
2
3 /* Copyright (C) 2015 by Yves Godin <[email protected]>
4 *
5 * Based on sysmoBTS:
6 * sysmobts_mgr_calib.c
7 * (C) 2014,2015 by Holger Hans Peter Freyther
8 * (C) 2014 by Harald Welte for the IPA code from the oml router
9 *
10 * All Rights Reserved
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27 #include "misc/oc2gbts_mgr.h"
28 #include "misc/oc2gbts_misc.h"
29 #include "misc/oc2gbts_clock.h"
30 #include "misc/oc2gbts_swd.h"
31 #include "misc/oc2gbts_par.h"
32 #include "misc/oc2gbts_led.h"
33 #include "osmo-bts/msg_utils.h"
34
35 #include <osmocom/core/logging.h>
36 #include <osmocom/core/select.h>
37
38 #include <osmocom/ctrl/control_cmd.h>
39 #include <osmocom/ctrl/ports.h>
40
41 #include <osmocom/gsm/ipa.h>
42 #include <osmocom/gsm/protocol/ipaccess.h>
43
44 #include <osmocom/abis/abis.h>
45 #include <osmocom/abis/e1_input.h>
46 #include <osmocom/abis/ipa.h>
47
48 #include <time.h>
49 #include <sys/sysinfo.h>
50 #include <errno.h>
51
52 static void calib_adjust(struct oc2gbts_mgr_instance *mgr);
53 static void calib_state_reset(struct oc2gbts_mgr_instance *mgr, int reason);
54 static void calib_loop_run(void *_data);
55
56 static int ocxodac_saved_value = -1;
57
58 enum calib_state {
59 CALIB_INITIAL,
60 CALIB_IN_PROGRESS,
61 CALIB_GPS_WAIT_FOR_FIX,
62 };
63
64 enum calib_result {
65 CALIB_FAIL_START,
66 CALIB_FAIL_GPSFIX,
67 CALIB_FAIL_CLKERR,
68 CALIB_FAIL_OCXODAC,
69 CALIB_SUCCESS,
70 };
71
72 static inline int compat_gps_read(struct gps_data_t *data)
73 {
74 /* API break in gpsd 6bba8b329fc7687b15863d30471d5af402467802 */
75 #if GPSD_API_MAJOR_VERSION >= 7 && GPSD_API_MINOR_VERSION >= 0
76 return gps_read(data, NULL, 0);
77 #else
78 return gps_read(data);
79 #endif
80 }
81
82 static int oc2gbts_par_get_uptime(void *ctx, int *ret)
83 {
84 char *fpath;
85 FILE *fp;
86 int rc;
87
88 fpath = talloc_asprintf(ctx, "%s", UPTIME_TMP_PATH);
89 if (!fpath)
90 return -EINVAL;
91
92 fp = fopen(fpath, "r");
93 if (!fp)
94 fprintf(stderr, "Failed to open %s due to '%s' error\n", fpath, strerror(errno));
95
96 talloc_free(fpath);
97
98 if (fp == NULL) {
99 return -errno;
100 }
101
102 rc = fscanf(fp, "%d", ret);
103 if (rc != 1) {
104 fclose(fp);
105 return -EIO;
106 }
107 fclose(fp);
108
109 return 0;
110 }
111
112 static int oc2gbts_par_set_uptime(void *ctx, int val)
113 {
114 char *fpath;
115 FILE *fp;
116 int rc;
117
118 fpath = talloc_asprintf(ctx, "%s", UPTIME_TMP_PATH);
119 if (!fpath)
120 return -EINVAL;
121
122 fp = fopen(fpath, "w");
123 if (!fp)
124 fprintf(stderr, "Failed to open %s due to '%s' error\n", fpath, strerror(errno));
125
126 talloc_free(fpath);
127
128 if (fp == NULL) {
129 return -errno;
130 }
131
132 rc = fprintf(fp, "%d", val);
133 if (rc < 0) {
134 fclose(fp);
135 return -EIO;
136 }
137 fsync(fileno(fp));
138 fclose(fp);
139
140 return 0;
141 }
142
143 static void mgr_gps_close(struct oc2gbts_mgr_instance *mgr)
144 {
145 if (!mgr->gps.gps_open)
146 return;
147
148 osmo_timer_del(&mgr->gps.fix_timeout);
149
150 osmo_fd_unregister(&mgr->gps.gpsfd);
151 gps_close(&mgr->gps.gpsdata);
152 memset(&mgr->gps.gpsdata, 0, sizeof(mgr->gps.gpsdata));
153 mgr->gps.gps_open = 0;
154 }
155
156 static void mgr_gps_checkfix(struct oc2gbts_mgr_instance *mgr)
157 {
158 struct gps_data_t *data = &mgr->gps.gpsdata;
159
160 /* No 3D fix yet */
161 if (data->fix.mode < MODE_3D) {
162 LOGP(DCALIB, LOGL_DEBUG, "Fix mode not enough: %d\n",
163 data->fix.mode);
164 return;
165 }
166
167 /* Satellite used checking */
168 if (data->satellites_used < 1) {
169 LOGP(DCALIB, LOGL_DEBUG, "Not enough satellites used: %d\n",
170 data->satellites_used);
171 return;
172 }
173
174 #if GPSD_API_MAJOR_VERSION >= 9
175 mgr->gps.gps_fix_now = data->fix.time.tv_sec;
176 #else
177 mgr->gps.gps_fix_now = (time_t) data->fix.time;
178 #endif
179
180 LOGP(DCALIB, LOGL_INFO, "Got a GPS fix, satellites used: %d, timestamp: %ld\n",
181 data->satellites_used, mgr->gps.gps_fix_now);
182 osmo_timer_del(&mgr->gps.fix_timeout);
183 mgr_gps_close(mgr);
184 }
185
186 static int mgr_gps_read(struct osmo_fd *fd, unsigned int what)
187 {
188 int rc;
189 struct oc2gbts_mgr_instance *mgr = fd->data;
190
191 rc = compat_gps_read(&mgr->gps.gpsdata);
192 if (rc == -1) {
193 LOGP(DCALIB, LOGL_ERROR, "gpsd vanished during read.\n");
194 calib_state_reset(mgr, CALIB_FAIL_GPSFIX);
195 return -1;
196 }
197
198 if (rc > 0)
199 mgr_gps_checkfix(mgr);
200 return 0;
201 }
202
203 static void mgr_gps_fix_timeout(void *_data)
204 {
205 struct oc2gbts_mgr_instance *mgr = _data;
206
207 LOGP(DCALIB, LOGL_ERROR, "Failed to acquire GPS fix.\n");
208 mgr_gps_close(mgr);
209 }
210
211 static void mgr_gps_open(struct oc2gbts_mgr_instance *mgr)
212 {
213 int rc;
214
215 if (mgr->gps.gps_open)
216 return;
217
218 rc = gps_open("localhost", DEFAULT_GPSD_PORT, &mgr->gps.gpsdata);
219 if (rc != 0) {
220 LOGP(DCALIB, LOGL_ERROR, "Failed to connect to GPS %d\n", rc);
221 calib_state_reset(mgr, CALIB_FAIL_GPSFIX);
222 return;
223 }
224
225 mgr->gps.gps_open = 1;
226 gps_stream(&mgr->gps.gpsdata, WATCH_ENABLE, NULL);
227
228 osmo_fd_setup(&mgr->gps.gpsfd, mgr->gps.gpsdata.gps_fd, OSMO_FD_READ | OSMO_FD_EXCEPT,
229 mgr_gps_read, mgr, 0);
230 if (osmo_fd_register(&mgr->gps.gpsfd) < 0) {
231 LOGP(DCALIB, LOGL_ERROR, "Failed to register GPSD fd\n");
232 calib_state_reset(mgr, CALIB_FAIL_GPSFIX);
233 }
234
235 mgr->calib.state = CALIB_GPS_WAIT_FOR_FIX;
236 mgr->gps.fix_timeout.data = mgr;
237 mgr->gps.fix_timeout.cb = mgr_gps_fix_timeout;
238 osmo_timer_schedule(&mgr->gps.fix_timeout, 60, 0);
239 LOGP(DCALIB, LOGL_INFO, "Opened the GPSD connection waiting for fix: %d\n",
240 mgr->gps.gpsfd.fd);
241 }
242
243 /* OC2G CTRL interface related functions */
244 static void send_ctrl_cmd(struct oc2gbts_mgr_instance *mgr, struct msgb *msg)
245 {
246 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL);
247 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
248 ipa_client_conn_send(mgr->oc2gbts_ctrl.bts_conn, msg);
249 }
250
251 static void send_set_ctrl_cmd(struct oc2gbts_mgr_instance *mgr, const char *key, const int val, const char *text)
252 {
253 struct msgb *msg;
254 int ret;
255
256 msg = msgb_alloc_headroom(1024, 128, "CTRL SET");
257 ret = snprintf((char *) msg->data, 4096, "SET %u %s %d, %s",
258 mgr->oc2gbts_ctrl.last_seqno++, key, val, text);
259 msg->l2h = msgb_put(msg, ret);
260 return send_ctrl_cmd(mgr, msg);
261 }
262
263 static void calib_start(struct oc2gbts_mgr_instance *mgr)
264 {
265 int rc;
266
267 rc = oc2gbts_clock_err_open();
268 if (rc != 0) {
269 LOGP(DCALIB, LOGL_ERROR, "Failed to open clock error module %d\n", rc);
270 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
271 return;
272 }
273
274 rc = oc2gbts_clock_dac_open();
275 if (rc != 0) {
276 LOGP(DCALIB, LOGL_ERROR, "Failed to open OCXO dac module %d\n", rc);
277 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
278 return;
279 }
280
281 calib_adjust(mgr);
282 }
283 static int get_uptime(int *uptime)
284 {
285 struct sysinfo s_info;
286 int rc;
287 rc = sysinfo(&s_info);
288 if(!rc)
289 *uptime = s_info.uptime /(60 * 60);
290
291 return rc;
292 }
293
294 static void calib_adjust(struct oc2gbts_mgr_instance *mgr)
295 {
296 int rc;
297 int fault;
298 int error_ppt;
299 int accuracy_ppq;
300 int interval_sec;
301 int dac_value;
302 int new_dac_value;
303 int dac_correction;
304 int now = 0;
305
306 /* Get GPS time via GPSD */
307 mgr_gps_open(mgr);
308
309 rc = oc2gbts_clock_err_get(&fault, &error_ppt,
310 &accuracy_ppq, &interval_sec);
311 if (rc < 0) {
312 LOGP(DCALIB, LOGL_ERROR,
313 "Failed to get clock error measurement %d\n", rc);
314 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
315 return;
316 }
317
318 /* get current up time */
319 rc = get_uptime(&now);
320 if (rc < 0)
321 LOGP(DTEMP, LOGL_ERROR, "Unable to read up time hours: %d (%s)\n", rc, strerror(errno));
322
323 /* read last up time */
324 rc = oc2gbts_par_get_uptime(tall_mgr_ctx, &mgr->gps.last_update);
325 if (rc < 0)
326 LOGP(DCALIB, LOGL_NOTICE, "Last GPS 3D fix can not read (%d). Last GPS 3D fix sets to zero\n", rc);
327
328 if (fault) {
329 LOGP(DCALIB, LOGL_NOTICE, "GPS has no fix, warn_flags=0x%08x, last=%d, now=%d\n",
330 mgr->oc2gbts_ctrl.warn_flags, mgr->gps.last_update, now);
331 if (now >= mgr->gps.last_update + mgr->gps.gps_fix_limit.thresh_warn_max * 24) {
332 if (!(mgr->oc2gbts_ctrl.warn_flags & S_MGR_GPS_FIX_WARN_ALARM)) {
333 mgr->oc2gbts_ctrl.warn_flags |= S_MGR_GPS_FIX_WARN_ALARM;
334 oc2gbts_mgr_dispatch_alarm(mgr, NM_EVT_CAUSE_WARN_GPS_FIX_FAIL, "oc2g-oml-alert", "GPS 3D fix has been lost");
335
336 LOGP(DCALIB, LOGL_NOTICE, "GPS has no fix since the last verification, warn_flags=0x%08x, last=%d, now=%d\n",
337 mgr->oc2gbts_ctrl.warn_flags, mgr->gps.last_update, now);
338
339 /* schedule LED pattern for GPS fix lost */
340 mgr->alarms.gps_fix_lost = 1;
341 /* update LED pattern */
342 select_led_pattern(mgr);
343 }
344 } else {
345 /* read from last GPS 3D fix timestamp */
346 rc = oc2gbts_par_get_gps_fix(tall_mgr_ctx, &mgr->gps.last_gps_fix);
347 if (rc < 0)
348 LOGP(DCALIB, LOGL_NOTICE, "Last GPS 3D fix timestamp can not read (%d)\n", rc);
349
350 if (difftime(mgr->gps.gps_fix_now, mgr->gps.last_gps_fix) > mgr->gps.gps_fix_limit.thresh_warn_max * 24 * 60 * 60) {
351 if (!(mgr->oc2gbts_ctrl.warn_flags & S_MGR_GPS_FIX_WARN_ALARM)) {
352 mgr->oc2gbts_ctrl.warn_flags |= S_MGR_GPS_FIX_WARN_ALARM;
353 oc2gbts_mgr_dispatch_alarm(mgr, NM_EVT_CAUSE_WARN_GPS_FIX_FAIL, "oc2g-oml-alert", "GPS 3D fix has been lost");
354
355 LOGP(DCALIB, LOGL_NOTICE, "GPS has no fix since the last known GPS fix, warn_flags=0x%08x, gps_last=%ld, gps_now=%ld\n",
356 mgr->oc2gbts_ctrl.warn_flags, mgr->gps.last_gps_fix, mgr->gps.gps_fix_now);
357
358 /* schedule LED pattern for GPS fix lost */
359 mgr->alarms.gps_fix_lost = 1;
360 /* update LED pattern */
361 select_led_pattern(mgr);
362 }
363 }
364 }
365
366 rc = oc2gbts_clock_err_reset();
367 if (rc < 0) {
368 LOGP(DCALIB, LOGL_ERROR,
369 "Failed to reset clock error module %d\n", rc);
370 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
371 return;
372 }
373
374 calib_state_reset(mgr, CALIB_FAIL_GPSFIX);
375 return;
376 }
377
378 if (!interval_sec) {
379 LOGP(DCALIB, LOGL_INFO, "Skipping this iteration, no integration time\n");
380 calib_state_reset(mgr, CALIB_SUCCESS);
381 return;
382 }
383
384 /* We got GPS 3D fix */
385 LOGP(DCALIB, LOGL_DEBUG, "Got GPS 3D fix warn_flags=0x%08x, uptime_last=%d, uptime_now=%d, gps_last=%ld, gps_now=%ld\n",
386 mgr->oc2gbts_ctrl.warn_flags, mgr->gps.last_update, now, mgr->gps.last_gps_fix, mgr->gps.gps_fix_now);
387
388 if (mgr->oc2gbts_ctrl.warn_flags & S_MGR_GPS_FIX_WARN_ALARM) {
389 /* Store GPS fix as soon as we send ceased alarm */
390 LOGP(DCALIB, LOGL_NOTICE, "Store GPS fix as soon as we send ceased alarm last=%ld, now=%ld\n",
391 mgr->gps.last_gps_fix , mgr->gps.gps_fix_now);
392 rc = oc2gbts_par_set_gps_fix(tall_mgr_ctx, mgr->gps.gps_fix_now);
393 if (rc < 0)
394 LOGP(DCALIB, LOGL_ERROR, "Failed to store GPS 3D fix to storage %d\n", rc);
395
396 /* Store last up time */
397 rc = oc2gbts_par_set_uptime(tall_mgr_ctx, now);
398 if (rc < 0)
399 LOGP(DCALIB, LOGL_ERROR, "Failed to store uptime to storage %d\n", rc);
400
401 mgr->gps.last_update = now;
402
403 /* schedule LED pattern for GPS fix resume */
404 mgr->alarms.gps_fix_lost = 0;
405 /* update LED pattern */
406 select_led_pattern(mgr);
407 /* send ceased alarm if possible */
408 oc2gbts_mgr_dispatch_alarm(mgr, NM_EVT_CAUSE_WARN_GPS_FIX_FAIL, "oc2g-oml-ceased", "GPS 3D fix has been lost");
409 mgr->oc2gbts_ctrl.warn_flags &= ~S_MGR_GPS_FIX_WARN_ALARM;
410
411 }
412 /* Store GPS fix at every hour */
413 if (now > mgr->gps.last_update) {
414 /* Store GPS fix every 60 minutes */
415 LOGP(DCALIB, LOGL_INFO, "Store GPS fix every hour last=%ld, now=%ld\n",
416 mgr->gps.last_gps_fix , mgr->gps.gps_fix_now);
417 rc = oc2gbts_par_set_gps_fix(tall_mgr_ctx, mgr->gps.gps_fix_now);
418 if (rc < 0)
419 LOGP(DCALIB, LOGL_ERROR, "Failed to store GPS 3D fix to storage %d\n", rc);
420
421 /* Store last up time every 60 minutes */
422 rc = oc2gbts_par_set_uptime(tall_mgr_ctx, now);
423 if (rc < 0)
424 LOGP(DCALIB, LOGL_ERROR, "Failed to store uptime to storage %d\n", rc);
425
426 /* update last uptime */
427 mgr->gps.last_update = now;
428 }
429
430 rc = oc2gbts_clock_dac_get(&dac_value);
431 if (rc < 0) {
432 LOGP(DCALIB, LOGL_ERROR,
433 "Failed to get OCXO dac value %d\n", rc);
434 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
435 return;
436 }
437
438 /* Set OCXO initial dac value */
439 if (ocxodac_saved_value < 0)
440 ocxodac_saved_value = dac_value;
441
442 LOGP(DCALIB, LOGL_INFO,
443 "Calibration ERR(%f PPB) ACC(%f PPB) INT(%d) DAC(%d)\n",
444 error_ppt / 1000., accuracy_ppq / 1000000., interval_sec, dac_value);
445
446 /* 1 unit of correction equal about 0.5 - 1 PPB correction */
447 dac_correction = (int)(-error_ppt * 0.0015);
448 new_dac_value = dac_value + dac_correction;
449
450 if (new_dac_value > 4095)
451 new_dac_value = 4095;
452 else if (new_dac_value < 0)
453 new_dac_value = 0;
454
455 /* We have a fix, make sure the measured error is
456 meaningful (10 times the accuracy) */
457 if ((new_dac_value != dac_value) && ((100l * abs(error_ppt)) > accuracy_ppq)) {
458
459 LOGP(DCALIB, LOGL_INFO,
460 "Going to apply %d as new clock setting.\n",
461 new_dac_value);
462
463 rc = oc2gbts_clock_dac_set(new_dac_value);
464 if (rc < 0) {
465 LOGP(DCALIB, LOGL_ERROR,
466 "Failed to set OCXO dac value %d\n", rc);
467 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
468 return;
469 }
470 rc = oc2gbts_clock_err_reset();
471 if (rc < 0) {
472 LOGP(DCALIB, LOGL_ERROR,
473 "Failed to reset clock error module %d\n", rc);
474 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
475 return;
476 }
477 }
478 /* New conditions to store DAC value:
479 * - Resolution accuracy less or equal than 0.01PPB (or 10000 PPQ)
480 * - Error less or equal than 2PPB (or 2000PPT)
481 * - Solution different than the last one */
482 else if (accuracy_ppq <= 10000) {
483 if((dac_value != ocxodac_saved_value) && (abs(error_ppt) < 2000)) {
484 LOGP(DCALIB, LOGL_INFO, "Saving OCXO DAC value to memory... val = %d\n", dac_value);
485 rc = oc2gbts_clock_dac_save();
486 if (rc < 0) {
487 LOGP(DCALIB, LOGL_ERROR,
488 "Failed to save OCXO dac value %d\n", rc);
489 calib_state_reset(mgr, CALIB_FAIL_OCXODAC);
490 } else {
491 ocxodac_saved_value = dac_value;
492 }
493 }
494
495 rc = oc2gbts_clock_err_reset();
496 if (rc < 0) {
497 LOGP(DCALIB, LOGL_ERROR,
498 "Failed to reset clock error module %d\n", rc);
499 calib_state_reset(mgr, CALIB_FAIL_CLKERR);
500 }
501 }
502
503 calib_state_reset(mgr, CALIB_SUCCESS);
504 return;
505 }
506
507 static void calib_close(struct oc2gbts_mgr_instance *mgr)
508 {
509 oc2gbts_clock_err_close();
510 oc2gbts_clock_dac_close();
511 }
512
513 static void calib_state_reset(struct oc2gbts_mgr_instance *mgr, int outcome)
514 {
515 if (mgr->calib.calib_from_loop) {
516 /*
517 * In case of success calibrate in two hours again
518 * and in case of a failure in some minutes.
519 *
520 * TODO NTQ: Select timeout based on last error and accuracy
521 */
522 int timeout = 60;
523 //int timeout = 2 * 60 * 60;
524 //if (outcome != CALIB_SUCESS) }
525 // timeout = 5 * 60;
526 //}
527
528 mgr->calib.calib_timeout.data = mgr;
529 mgr->calib.calib_timeout.cb = calib_loop_run;
530 osmo_timer_schedule(&mgr->calib.calib_timeout, timeout, 0);
531 /* TODO: do we want to notify if we got a calibration error, like no gps fix? */
532 oc2gbts_swd_event(mgr, SWD_CHECK_CALIB);
533 }
534
535 mgr->calib.state = CALIB_INITIAL;
536 calib_close(mgr);
537 }
538
539 static int calib_run(struct oc2gbts_mgr_instance *mgr, int from_loop)
540 {
541 if (mgr->calib.state != CALIB_INITIAL) {
542 LOGP(DCALIB, LOGL_ERROR, "Calib is already in progress.\n");
543 return -1;
544 }
545
546 /* Validates if we have a bts connection */
547 if (mgr->oc2gbts_ctrl.is_up) {
548 LOGP(DCALIB, LOGL_DEBUG, "Bts connection is up.\n");
549 oc2gbts_swd_event(mgr, SWD_CHECK_BTS_CONNECTION);
550 }
551
552 mgr->calib.calib_from_loop = from_loop;
553
554 /* From now on everything will be handled from the failure */
555 mgr->calib.state = CALIB_IN_PROGRESS;
556 calib_start(mgr);
557 return 0;
558 }
559
560 static void calib_loop_run(void *_data)
561 {
562 int rc;
563 struct oc2gbts_mgr_instance *mgr = _data;
564
565 LOGP(DCALIB, LOGL_INFO, "Going to calibrate the system.\n");
566 rc = calib_run(mgr, 1);
567 if (rc != 0) {
568 calib_state_reset(mgr, CALIB_FAIL_START);
569 }
570 }
571
572 int oc2gbts_mgr_calib_run(struct oc2gbts_mgr_instance *mgr)
573 {
574 return calib_run(mgr, 0);
575 }
576
577 static void schedule_bts_connect(struct oc2gbts_mgr_instance *mgr)
578 {
579 DEBUGP(DLCTRL, "Scheduling BTS connect\n");
580 osmo_timer_schedule(&mgr->oc2gbts_ctrl.recon_timer, 1, 0);
581 }
582
583 /* link to BSC has gone up or down */
584 static void bts_updown_cb(struct ipa_client_conn *link, int up)
585 {
586 struct oc2gbts_mgr_instance *mgr = link->data;
587
588 LOGP(DLCTRL, LOGL_INFO, "BTS connection %s\n", up ? "up" : "down");
589
590 if (up) {
591 mgr->oc2gbts_ctrl.is_up = 1;
592 mgr->oc2gbts_ctrl.last_seqno = 0;
593 /* handle any pending alarm */
594 handle_alert_actions(mgr);
595 handle_warn_actions(mgr);
596 } else {
597 mgr->oc2gbts_ctrl.is_up = 0;
598 schedule_bts_connect(mgr);
599 }
600
601 }
602
603 /* BTS re-connect timer call-back */
604 static void bts_recon_timer_cb(void *data)
605 {
606 int rc;
607 struct oc2gbts_mgr_instance *mgr = data;
608
609 /* update LED pattern */
610 select_led_pattern(mgr);
611
612 /* The connection failures are to be expected during boot */
613 osmo_fd_write_enable(mgr->oc2gbts_ctrl.bts_conn->ofd);
614 rc = ipa_client_conn_open(mgr->oc2gbts_ctrl.bts_conn);
615 if (rc < 0) {
616 LOGP(DLCTRL, LOGL_NOTICE, "Failed to connect to BTS.\n");
617 schedule_bts_connect(mgr);
618 }
619 }
620
621 static void oc2gbts_handle_ctrl(struct oc2gbts_mgr_instance *mgr, struct msgb *msg)
622 {
623 struct ctrl_cmd *cmd = ctrl_cmd_parse(tall_mgr_ctx, msg);
624 int cause = atoi(cmd->reply);
625
626 if (!cmd) {
627 LOGP(DCALIB, LOGL_ERROR, "Failed to parse command/response\n");
628 return;
629 }
630
631 switch (cmd->type) {
632 case CTRL_TYPE_GET_REPLY:
633 LOGP(DCALIB, LOGL_INFO, "Got GET_REPLY from BTS cause=0x%x\n", cause);
634 break;
635 case CTRL_TYPE_SET_REPLY:
636 LOGP(DCALIB, LOGL_INFO, "Got SET_REPLY from BTS cause=0x%x\n", cause);
637 break;
638 default:
639 LOGP(DCALIB, LOGL_ERROR,
640 "Unhandled CTRL response: %d. Resetting state\n",
641 cmd->type);
642 break;
643 }
644
645 talloc_free(cmd);
646 return;
647 }
648
649 static int bts_read_cb(struct ipa_client_conn *link, struct msgb *msg)
650 {
651 int rc;
652 struct ipaccess_head *hh = (struct ipaccess_head *) msgb_l1(msg);
653 struct ipaccess_head_ext *hh_ext;
654
655 LOGP(DLCTRL, LOGL_DEBUG, "Received data from BTS: %s\n",
656 osmo_hexdump(msgb_data(msg), msgb_length(msg)));
657
658 /* regular message handling */
659 rc = msg_verify_ipa_structure(msg);
660 if (rc < 0) {
661 LOGP(DCALIB, LOGL_ERROR,
662 "Invalid IPA message from BTS (rc=%d)\n", rc);
663 goto err;
664 }
665
666 switch (hh->proto) {
667 case IPAC_PROTO_OSMO:
668 hh_ext = (struct ipaccess_head_ext *) hh->data;
669 switch (hh_ext->proto) {
670 case IPAC_PROTO_EXT_CTRL:
671 oc2gbts_handle_ctrl(link->data, msg);
672 break;
673 default:
674 LOGP(DCALIB, LOGL_NOTICE,
675 "Unhandled osmo ID %u from BTS\n", hh_ext->proto);
676 };
677 msgb_free(msg);
678 break;
679 default:
680 LOGP(DCALIB, LOGL_NOTICE,
681 "Unhandled stream ID %u from BTS\n", hh->proto);
682 msgb_free(msg);
683 break;
684 }
685 return 0;
686 err:
687 msgb_free(msg);
688 return -1;
689 }
690
691 int oc2gbts_mgr_calib_init(struct oc2gbts_mgr_instance *mgr)
692 {
693 int rc;
694
695 /* initialize last uptime */
696 mgr->gps.last_update = 0;
697 rc = oc2gbts_par_set_uptime(tall_mgr_ctx, mgr->gps.last_update);
698 if (rc < 0)
699 LOGP(DCALIB, LOGL_ERROR, "Failed to store uptime to storage %d\n", rc);
700
701 /* get last GPS 3D fix timestamp */
702 mgr->gps.last_gps_fix = 0;
703 rc = oc2gbts_par_get_gps_fix(tall_mgr_ctx, &mgr->gps.last_gps_fix);
704 if (rc < 0) {
705 LOGP(DCALIB, LOGL_ERROR, "Failed to get last GPS 3D fix timestamp from storage. Create it anyway %d\n", rc);
706 rc = oc2gbts_par_set_gps_fix(tall_mgr_ctx, mgr->gps.last_gps_fix);
707 if (rc < 0)
708 LOGP(DCALIB, LOGL_ERROR, "Failed to store initial GPS fix to storage %d\n", rc);
709 }
710
711 mgr->calib.state = CALIB_INITIAL;
712 mgr->calib.calib_timeout.data = mgr;
713 mgr->calib.calib_timeout.cb = calib_loop_run;
714 osmo_timer_schedule(&mgr->calib.calib_timeout, 0, 0);
715
716 return rc;
717 }
718
719 int oc2gbts_mgr_control_init(struct oc2gbts_mgr_instance *mgr)
720 {
721 mgr->oc2gbts_ctrl.bts_conn = ipa_client_conn_create(tall_mgr_ctx, NULL, 0,
722 "127.0.0.1", OSMO_CTRL_PORT_BTS,
723 bts_updown_cb, bts_read_cb,
724 NULL, mgr);
725 if (!mgr->oc2gbts_ctrl.bts_conn) {
726 LOGP(DLCTRL, LOGL_ERROR, "Failed to create IPA connection to BTS\n");
727 return -1;
728 }
729
730 mgr->oc2gbts_ctrl.recon_timer.cb = bts_recon_timer_cb;
731 mgr->oc2gbts_ctrl.recon_timer.data = mgr;
732 schedule_bts_connect(mgr);
733
734 return 0;
735 }
736
737 void oc2gbts_mgr_dispatch_alarm(struct oc2gbts_mgr_instance *mgr, const int cause, const char *key, const char *text)
738 {
739 /* Make sure the control link is ready before sending alarm */
740 if (mgr->oc2gbts_ctrl.bts_conn->state != IPA_CLIENT_LINK_STATE_CONNECTED) {
741 LOGP(DLCTRL, LOGL_NOTICE, "MGR losts connection to BTS.\n");
742 LOGP(DLCTRL, LOGL_NOTICE, "MGR drops an alert cause=0x%x, text=%s to BTS\n", cause, text);
743 return;
744 }
745
746 LOGP(DLCTRL, LOGL_DEBUG, "MGR sends an alert cause=0x%x, text=%s to BTS\n", cause, text);
747 send_set_ctrl_cmd(mgr, key, cause, text);
748 return;
749 }
750
751