blob: 850ded64a9463ca62a7b678410649dc8a7f2f10e [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
Harald Weltef29e0d02018-08-24 21:42:22 +020019static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
Harald Welte2ff0ab92018-08-17 22:10:49 +020020{
21 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
22 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
Harald Weltef29e0d02018-08-24 21:42:22 +020023 ipa_client_conn_send(ipa, msg_tx);
Harald Welte2ff0ab92018-08-17 22:10:49 +020024 /* msg_tx is now queued and will be freed. */
25}
26
Harald Weltef29e0d02018-08-24 21:42:22 +020027void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
Harald Welte2ff0ab92018-08-17 22:10:49 +020028{
29 struct msgb *msg = rspro_enc_msg(rspro);
30 OSMO_ASSERT(msg);
Harald Weltef29e0d02018-08-24 21:42:22 +020031 push_and_send(ipa, msg);
Harald Welte2ff0ab92018-08-17 22:10:49 +020032}
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:
Harald Welte417b9612018-09-24 14:53:41 +020044 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020045 break;
46 default:
47 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
48 return -1;
49 }
50
51 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020052}
53
Harald Welte24173fb2018-08-24 20:37:28 +020054int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
Harald Welte2ff0ab92018-08-17 22:10:49 +020055{
56 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
57 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
58 struct bankd_client *bc = conn->data;
Harald Welte24173fb2018-08-24 20:37:28 +020059 int rc;
Harald Welte2ff0ab92018-08-17 22:10:49 +020060
61 if (msgb_length(msg) < sizeof(*hh))
62 goto invalid;
63 msg->l2h = &hh->data[0];
64 if (hh->proto != IPAC_PROTO_OSMO)
65 goto invalid;
66 if (!he || msgb_l2len(msg) < sizeof(*he))
67 goto invalid;
68 msg->l2h = &he->data[0];
69
70 if (he->proto != IPAC_PROTO_EXT_RSPRO)
71 goto invalid;
72
Harald Welte2ff0ab92018-08-17 22:10:49 +020073 printf("Received RSPRO %s\n", msgb_hexdump(msg));
74
Harald Welte24173fb2018-08-24 20:37:28 +020075 rc = bankd_handle_msg(bc, 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}