Move struct cbc bring up code to its own file and functions

This way it's clear and we have in one place all the related code that
neds to be run at a given time.t

Change-Id: I00fe755340664d4ca4a5cfdcae8d4cd33b2f40a1
This commit is contained in:
Pau Espin 2022-07-22 14:55:18 +02:00
parent d462e452e1
commit 5661cef889
7 changed files with 100 additions and 34 deletions

View File

@ -1,4 +1,5 @@
log stderr
logging level main notice
logging level rest notice
logging level cbsp notice
logging level sbcap notice

View File

@ -79,6 +79,8 @@ struct cbc {
};
extern struct cbc *g_cbc;
struct cbc *cbc_alloc(void *ctx);
int cbc_start(struct cbc *cbc);
/* rest_api.c */
int rest_api_init(void *ctx, const char *bind_addr, uint16_t port);

View File

@ -1,6 +1,7 @@
#pragma once
enum {
DMAIN,
DCBSP,
DSBcAP,
DREST,

View File

@ -10,6 +10,7 @@ AM_LDFLAGS=$(COVERAGE_LDFLAGS)
bin_PROGRAMS = osmo-cbc
osmo_cbc_SOURCES = \
cbc_data.c \
cbc_main.c \
cbc_message.c \
cbc_peer.c \

83
src/cbc_data.c Normal file
View File

@ -0,0 +1,83 @@
/* Osmocom CBC (Cell Broacast Centre) */
/* (C) 2022 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
* All Rights Reserved
* Author: Pau Espin Pedrol <pespin@sysmocom.de>
*
* SPDX-License-Identifier: AGPL-3.0+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <errno.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/utils.h>
#include <osmocom/cbc/cbc_data.h>
#include <osmocom/cbc/cbsp_link.h>
#include <osmocom/cbc/sbcap_link.h>
#include <osmocom/cbc/rest_it_op.h>
#include <osmocom/cbc/debug.h>
struct cbc *cbc_alloc(void *ctx)
{
struct cbc *cbc;
cbc = talloc_zero(ctx, struct cbc);
INIT_LLIST_HEAD(&cbc->peers);
INIT_LLIST_HEAD(&cbc->messages);
INIT_LLIST_HEAD(&cbc->expired_messages);
cbc->config.cbsp.local_host = talloc_strdup(cbc, "127.0.0.1");
cbc->config.cbsp.local_port = CBSP_TCP_PORT;
/* cbc->config.sbcap local_host set up during VTY (and vty_go_parent) */
cbc->config.sbcap.local_port = SBcAP_SCTP_PORT;
cbc->config.ecbe.local_host = talloc_strdup(cbc, "127.0.0.1");
cbc->config.ecbe.local_port = 12345;
cbc->it_q.rest2main = osmo_it_q_alloc(cbc, "rest2main", 10, rest2main_read_cb, NULL);
OSMO_ASSERT(cbc->it_q.rest2main);
osmo_fd_register(&cbc->it_q.rest2main->event_ofd);
return cbc;
}
int cbc_start(struct cbc *cbc)
{
void *tall_rest_ctx;
int rc;
tall_rest_ctx = talloc_named_const(cbc, 0, "REST");
if (!(cbc->cbsp.mgr = cbc_cbsp_mgr_create(cbc))) {
LOGP(DMAIN, LOGL_ERROR, "Error binding CBSP port\n");
return -EIO;
}
if (!(cbc->sbcap.mgr = cbc_sbcap_mgr_create(cbc))) {
LOGP(DMAIN, LOGL_ERROR, "Error binding SBc-AP port\n");
return -EIO;
}
rc = rest_api_init(tall_rest_ctx, cbc->config.ecbe.local_host, cbc->config.ecbe.local_port);
if (rc < 0) {
LOGP(DMAIN, LOGL_ERROR, "Error binding ECBE port\n");
return -EIO;
}
return 0;
}

View File

@ -45,9 +45,6 @@
#include <osmocom/vty/misc.h>
#include <osmocom/cbc/debug.h>
#include <osmocom/cbc/rest_it_op.h>
#include <osmocom/cbc/cbsp_link.h>
#include <osmocom/cbc/sbcap_link.h>
#include <osmocom/cbc/cbc_data.h>
#include <osmocom/cbc/cbc_vty.h>
@ -55,6 +52,13 @@ static void *tall_cbc_ctx;
struct cbc *g_cbc;
static const struct log_info_cat log_info_cat[] = {
[DMAIN] = {
.name = "DMAIN",
.description = "Main logging category",
.color = "\033[1;30m",
.enabled = 1,
.loglevel = LOGL_NOTICE,
},
[DCBSP] = {
.name = "DCBSP",
.description = "Cell Broadcast Service Protocol (CBC-BSC)",
@ -234,27 +238,16 @@ static void signal_handler(int signal)
int main(int argc, char **argv)
{
void *tall_rest_ctx;
int rc;
tall_cbc_ctx = talloc_named_const(NULL, 1, "osmo-cbc");
tall_rest_ctx = talloc_named_const(tall_cbc_ctx, 0, "REST");
msgb_talloc_ctx_init(tall_cbc_ctx, 0);
osmo_init_logging2(tall_cbc_ctx, &log_info);
log_enable_multithread();
osmo_stats_init(tall_cbc_ctx);
vty_init(&vty_info);
g_cbc = talloc_zero(tall_cbc_ctx, struct cbc);
INIT_LLIST_HEAD(&g_cbc->peers);
INIT_LLIST_HEAD(&g_cbc->messages);
INIT_LLIST_HEAD(&g_cbc->expired_messages);
g_cbc->config.cbsp.local_host = talloc_strdup(g_cbc, "127.0.0.1");
g_cbc->config.cbsp.local_port = CBSP_TCP_PORT;
/* g_cbc->config.sbcap local_host set up during VTY (and vty_go_parent) */
g_cbc->config.sbcap.local_port = SBcAP_SCTP_PORT;
g_cbc->config.ecbe.local_host = talloc_strdup(g_cbc, "127.0.0.1");
g_cbc->config.ecbe.local_port = 12345;
g_cbc = cbc_alloc(tall_cbc_ctx);
cbc_vty_init();
@ -276,26 +269,9 @@ int main(int argc, char **argv)
exit(1);
}
if (!(g_cbc->cbsp.mgr = cbc_cbsp_mgr_create(tall_cbc_ctx))) {
perror("Error binding CBSP port");
rc = cbc_start(g_cbc);
if (rc < 0)
exit(1);
}
if (!(g_cbc->sbcap.mgr = cbc_sbcap_mgr_create(tall_cbc_ctx))) {
perror("Error binding SBc-AP port\n");
exit(1);
}
rc = rest_api_init(tall_rest_ctx, g_cbc->config.ecbe.local_host, g_cbc->config.ecbe.local_port);
if (rc < 0) {
perror("Error binding ECBE port");
exit(1);
}
LOGP(DREST, LOGL_INFO, "Main thread tid: %lu\n", pthread_self());
g_cbc->it_q.rest2main = osmo_it_q_alloc(g_cbc, "rest2main", 10, rest2main_read_cb, NULL);
OSMO_ASSERT(g_cbc->it_q.rest2main);
osmo_fd_register(&g_cbc->it_q.rest2main->event_ofd);
signal(SIGUSR1, &signal_handler);
signal(SIGUSR2, &signal_handler);

View File

@ -686,6 +686,8 @@ int rest_api_init(void *ctx, const char *bind_addr, uint16_t port)
struct osmo_sockaddr_str sastr;
int i;
LOGP(DREST, LOGL_INFO, "Main thread tid: %lu\n", pthread_self());
#ifdef ULFIUS_MALLOC_NOT_BROKEN
/* See https://github.com/babelouest/ulfius/issues/63 */
g_tall_rest = ctx;