laforge has submitted this change. (
https://gerrit.osmocom.org/c/osmo-cbc/+/28734 )
Change subject: Move struct cbc bring up code to its own file and functions
......................................................................
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
---
M doc/examples/osmo-cbc/osmo-cbc.cfg
M include/osmocom/cbc/cbc_data.h
M include/osmocom/cbc/debug.h
M src/Makefile.am
A src/cbc_data.c
M src/cbc_main.c
M src/rest_api.c
7 files changed, 100 insertions(+), 34 deletions(-)
Approvals:
laforge: Looks good to me, approved
Jenkins Builder: Verified
diff --git a/doc/examples/osmo-cbc/osmo-cbc.cfg b/doc/examples/osmo-cbc/osmo-cbc.cfg
index cc4272f..994da8c 100644
--- a/doc/examples/osmo-cbc/osmo-cbc.cfg
+++ b/doc/examples/osmo-cbc/osmo-cbc.cfg
@@ -1,4 +1,5 @@
log stderr
+ logging level main notice
logging level rest notice
logging level cbsp notice
logging level sbcap notice
diff --git a/include/osmocom/cbc/cbc_data.h b/include/osmocom/cbc/cbc_data.h
index d96e5ca..d3a8319 100644
--- a/include/osmocom/cbc/cbc_data.h
+++ b/include/osmocom/cbc/cbc_data.h
@@ -79,6 +79,8 @@
};
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);
diff --git a/include/osmocom/cbc/debug.h b/include/osmocom/cbc/debug.h
index 1ec0a8e..025419e 100644
--- a/include/osmocom/cbc/debug.h
+++ b/include/osmocom/cbc/debug.h
@@ -1,6 +1,7 @@
#pragma once
enum {
+ DMAIN,
DCBSP,
DSBcAP,
DREST,
diff --git a/src/Makefile.am b/src/Makefile.am
index 32cccd7..e72b20b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@
bin_PROGRAMS = osmo-cbc
osmo_cbc_SOURCES = \
+ cbc_data.c \
cbc_main.c \
cbc_message.c \
cbc_peer.c \
diff --git a/src/cbc_data.c b/src/cbc_data.c
new file mode 100644
index 0000000..5d5a64f
--- /dev/null
+++ b/src/cbc_data.c
@@ -0,0 +1,83 @@
+/* Osmocom CBC (Cell Broacast Centre) */
+
+/* (C) 2022 by sysmocom - s.f.m.c. GmbH <info(a)sysmocom.de>
+ * All Rights Reserved
+ * Author: Pau Espin Pedrol <pespin(a)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;
+}
diff --git a/src/cbc_main.c b/src/cbc_main.c
index cd80e31..4ad59b6 100644
--- a/src/cbc_main.c
+++ b/src/cbc_main.c
@@ -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 @@
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 @@
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 @@
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);
diff --git a/src/rest_api.c b/src/rest_api.c
index 4294485..3af50e6 100644
--- a/src/rest_api.c
+++ b/src/rest_api.c
@@ -686,6 +686,8 @@
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;
--
To view, visit
https://gerrit.osmocom.org/c/osmo-cbc/+/28734
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings
Gerrit-Project: osmo-cbc
Gerrit-Branch: master
Gerrit-Change-Id: I00fe755340664d4ca4a5cfdcae8d4cd33b2f40a1
Gerrit-Change-Number: 28734
Gerrit-PatchSet: 3
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: merged