view smsc-daemon/main.c @ 9:dfe99a061249

smsc-daemon: fflush the log file after writing MO SM to it
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 25 Aug 2023 15:48:08 -0800
parents 22ddb71f6883
children d9db8661d9f3
line wrap: on
line source

/*
 * This C module is part of proto-smsc-daemon concoction, a prototype/test
 * implementation of a GSUP-based GSM SMSC.  It is based on the osmo-euse-demo
 * program from OsmoHLR package.
 *
 * proto-smsc-daemon author: Mychaela N. Falconia <falcon@freecalypso.org>,
 * no copyright.
 *
 * osmo-euse-demo author: Harald Welte <laforge@gnumonks.org>, (C) 2018, AGPL3+
 */

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>

#include <osmocom/core/msgb.h>
#include <osmocom/core/select.h>
#include <osmocom/core/application.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>

#include <osmocom/gsm/gsup.h>
#include <osmocom/gsm/ipa.h>

#include <osmocom/gsupclient/gsup_client.h>

#include "logging.h"

extern int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg);

#define	MAX_IPA_NAME	31

FILE *smsc_log_file;
char smsc_ipa_name[MAX_IPA_NAME+1];
struct ipaccess_unit gsup_ipa_dev;
struct osmo_gsup_client *g_gc;

static struct log_info_cat default_categories[] = {
	[DMAIN] = {
		.name = "DMAIN",
		.description = "Main Program",
		.enabled = 1, .loglevel = LOGL_DEBUG,
	},
};

static const struct log_info smsc_log_info = {
	.cat = default_categories,
	.num_cat = ARRAY_SIZE(default_categories),
};

static void set_ipa_name(const char *arg)
{
	if (strlen(arg) > MAX_IPA_NAME) {
		fprintf(stderr, "error: IPA name argument is too long\n");
		exit(1);
	}
	strcpy(smsc_ipa_name, arg);
}

static void open_log_file(const char *filename)
{
	smsc_log_file = fopen(filename, "a");
	if (!smsc_log_file) {
		perror(filename);
		exit(1);
	}
}

static void build_ipa_dev(void)
{
	gsup_ipa_dev.unit_name = "SMSC";
	gsup_ipa_dev.serno = smsc_ipa_name;
}

int main(int argc, char **argv)
{
	char *server_host = "127.0.0.1";
	uint16_t server_port = OSMO_GSUP_PORT;
	void *ctx = talloc_named_const(NULL, 0, "proto-smsc");

	osmo_init_logging2(ctx, &smsc_log_info);

	if (argc != 3) {
		fprintf(stderr, "usage: %s ipa-name logfile\n", argv[0]);
		exit(1);
	}
	set_ipa_name(argv[1]);
	open_log_file(argv[2]);

	build_ipa_dev();
	g_gc = osmo_gsup_client_create2(ctx, &gsup_ipa_dev, server_host,
					server_port, gsupc_read_cb, NULL);
	if (!g_gc) {
		fprintf(stderr, "error: osmo_gsup_client_create2() failed\n");
		exit(1);
	}

	while (1) {
		osmo_select_main(0);
	}

	exit(0);
}