blob: 0189f590972954efc99f61ed0293ef769ae5577e [file] [log] [blame]
Harald Welte2ff0ab92018-08-17 22:10:49 +02001
2#include <errno.h>
3#include <string.h>
4
5#include <talloc.h>
6
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/fsm.h>
9#include <osmocom/core/utils.h>
10#include <osmocom/core/logging.h>
11#include <osmocom/core/application.h>
12
13#include <osmocom/abis/ipa.h>
14#include <osmocom/gsm/protocol/ipaccess.h>
15
16#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020017#include "client.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020018
19static void bankd_send(struct bankd_client *bc, struct msgb *msg_tx)
20{
21 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
22 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
23 ipa_client_conn_send(bc->bankd_conn, msg_tx);
24 /* msg_tx is now queued and will be freed. */
25}
26
Harald Welte24173fb2018-08-24 20:37:28 +020027void bankd_send_rspro(struct bankd_client *bc, RsproPDU_t *rspro)
Harald Welte2ff0ab92018-08-17 22:10:49 +020028{
29 struct msgb *msg = rspro_enc_msg(rspro);
30 OSMO_ASSERT(msg);
31 bankd_send(bc, msg);
32}
33
Harald Welte24173fb2018-08-24 20:37:28 +020034static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020035{
Harald Welte24173fb2018-08-24 20:37:28 +020036 RsproPDU_t *pdu = rspro_dec_msg(msg);
37 if (!pdu) {
38 fprintf(stderr, "Error decoding PDU\n");
39 return -1;
Harald Welte2ff0ab92018-08-17 22:10:49 +020040 }
Harald Welte24173fb2018-08-24 20:37:28 +020041
42 switch (pdu->msg.present) {
43 case RsproPDUchoice_PR_connectClientRes:
44 break;
45 default:
46 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
47 return -1;
48 }
49
50 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020051}
52
Harald Welte24173fb2018-08-24 20:37:28 +020053int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020054{
55 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
56 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
57 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020058 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020059
60 if (msgb_length(msg) < sizeof(*hh))
61 goto invalid;
62 msg->l2h = &hh->data[0];
63 if (hh->proto != IPAC_PROTO_OSMO)
64 goto invalid;
65 if (!he || msgb_l2len(msg) < sizeof(*he))
66 goto invalid;
67 msg->l2h = &he->data[0];
68
69 if (he->proto != IPAC_PROTO_EXT_RSPRO)
70 goto invalid;
71
Harald Welte2ff0ab92018-08-17 22:10:49 +020072 printf("Received RSPRO %s\n", msgb_hexdump(msg));
73
Harald Welte24173fb2018-08-24 20:37:28 +020074 rc = bankd_handle_msg(bc, msg);
Harald Welte2ff0ab92018-08-17 22:10:49 +020075 msgb_free(msg);
Harald Welte24173fb2018-08-24 20:37:28 +020076
77 return rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020078
79invalid:
80 msgb_free(msg);
81 return -1;
82}
83
84static const struct log_info_cat default_categories[] = {
Harald Welte24173fb2018-08-24 20:37:28 +020085 [DMAIN] = {
86 .name = "DMAIN",
87 .loglevel = LOGL_DEBUG,
88 .enabled = 1,
89 },
Harald Welte2ff0ab92018-08-17 22:10:49 +020090};
91
92static const struct log_info log_info = {
93 .cat = default_categories,
94 .num_cat = ARRAY_SIZE(default_categories),
95};
96
Harald Welte24173fb2018-08-24 20:37:28 +020097static struct bankd_client *g_client;
Harald Welte2ff0ab92018-08-17 22:10:49 +020098static void *g_tall_ctx;
99void __thread *talloc_asn1_ctx;
100extern int asn_debug;
101
102int main(int argc, char **argv)
103{
Harald Welte2ff0ab92018-08-17 22:10:49 +0200104 g_tall_ctx = talloc_named_const(NULL, 0, "global");
105
Harald Welte24173fb2018-08-24 20:37:28 +0200106 osmo_fsm_register(&remsim_client_bankd_fsm);
107 osmo_fsm_register(&remsim_client_server_fsm);
108
109 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
110 g_client->bankd_host = "localhost";
111 g_client->bankd_port = 9999;
112 g_client->own_comp_id.type = ComponentType_remsimClient;
113 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name");
114 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
115 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
Harald Welte2ff0ab92018-08-17 22:10:49 +0200116
117 //asn_debug = 1;
118 osmo_init_logging2(g_tall_ctx, &log_info);
119
Harald Welte24173fb2018-08-24 20:37:28 +0200120 if (bankd_conn_fsm_alloc(g_client) < 0) {
Harald Welte2ff0ab92018-08-17 22:10:49 +0200121 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
122 exit(1);
123 }
Harald Welte2ff0ab92018-08-17 22:10:49 +0200124
125 while (1) {
126 osmo_select_main(0);
127 }
128}