blob: e6f2ca57adb33a52f313337527464fa097a9d956 [file] [log] [blame]
Neels Hofmeyr306dc092018-09-30 05:01:20 +02001#include <stdint.h>
2#include <string.h>
3
4#include <osmocom/core/msgb.h>
5#include <osmocom/core/application.h>
6#include <osmocom/core/logging.h>
7
8#include <osmocom/mgcp/iuup_cn_node.h>
9#include <osmocom/mgcp/iuup_protocol.h>
10
11void *ctx = NULL;
12
13static const char *dump(struct msgb *msg)
14{
15 return osmo_hexdump_nospc(msg->data, msg->len);
16}
17
18struct msgb *msgb_from_hex(const char *label, const char *hex)
19{
20 struct msgb *msg = msgb_alloc_headroom(4096 + OSMO_IUUP_HEADROOM,
21 OSMO_IUUP_HEADROOM, label);
22 unsigned char *rc;
23 msg->l2h = msg->data;
24 rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg)));
25 OSMO_ASSERT(rc == msg->l2h);
26 return msg;
27}
28
29const char *expect_rx_payload = NULL;
30int rx_payload(struct msgb *msg, void *node_priv)
31{
32 printf("rx_payload() invoked by iuup_cn!\n");
33 printf(" [IuUP] -RTP->\n");
34 printf("%s\n", dump(msg));
35 printf("node_priv=%p\n", node_priv);
36 if (!expect_rx_payload) {
37 printf("ERROR: did not expect rx_payload()\n");
38 exit(-1);
39 } else if (strcmp(expect_rx_payload, dump(msg))) {
40 printf("ERROR: mismatches expected msg %s\n", expect_rx_payload);
41 exit(-1);
42 } else
43 printf("ok: matches expected msg\n");
44 expect_rx_payload = NULL;
45 return 0;
46}
47
48const char *expect_tx_msg = NULL;
49int tx_msg(struct msgb *msg, void *node_priv)
50{
51 printf("tx_msg() invoked by iuup_cn!\n");
52 printf(" <-PDU- [IuUP]\n");
53 printf("%s\n", dump(msg));
54 printf("node_priv=%p\n", node_priv);
55 if (!expect_tx_msg) {
56 printf("ERROR: did not expect tx_msg()\n");
57 exit(-1);
58 } else if (strcmp(expect_tx_msg, dump(msg))) {
59 printf("ERROR: mismatches expected msg %s\n", expect_tx_msg);
60 exit(-1);
61 } else
62 printf("ok: matches expected msg\n");
63 expect_tx_msg = NULL;
64 return 0;
65}
66
67static int rx_pdu(struct osmo_iuup_cn *cn, struct msgb *msg)
68{
69 int rc;
70 printf(" -PDU-> [IuUP]\n");
71 printf("%s\n", dump(msg));
72 rc = osmo_iuup_cn_rx_pdu(cn, msg);
73 printf("rc=%d\n", rc);
74 return rc;
75}
76
77static int tx_payload(struct osmo_iuup_cn *cn, struct msgb *msg)
78{
79 int rc;
80 printf(" [IuUP] <-RTP-\n");
81 printf("%s\n", dump(msg));
82 rc = osmo_iuup_cn_tx_payload(cn, msg);
83 printf("rc=%d\n", rc);
84 return rc;
85}
86
87void test_cn_session()
88{
89 void *node_priv = (void*)0x2342;
90
91 struct osmo_iuup_cn_cfg cfg = {
92 .node_priv = node_priv,
93 .rx_payload = rx_payload,
94 .tx_msg = tx_msg,
95 };
96
97 struct osmo_iuup_cn *cn = osmo_iuup_cn_init(ctx, &cfg, __func__);
98 OSMO_ASSERT(cn);
99
100 printf("\nSend IuUP Initialization. Expecting direct tx_msg() of the Initialization Ack\n");
101 expect_tx_msg = "8060dc5219495e3f00010111" /* RTP header */
102 "e4002400"; /* IuUP Init Ack */
103 rx_pdu(cn,
104 msgb_from_hex("IuUP-Init",
105 "8060dc5219495e3f00010111" /* <- RTP header */
106 "e000df99" /* <- IuUP header */
107 "160051673c01270000820000001710000100" /* IuUP params */));
108
109#define RTP_HEADER "8060944c6256042c00010102"
110#define IUUP_HEADER "0100e2b3"
111#define RTP_PAYLOAD "6cfb23bc46d18180c3e5ffe040045600005a7d35b625b80005fff03214ced0"
112 printf("\nReceive payload encapsulated in IuUP. Expecting rx_payload() of just RTP packet\n");
113 printf("i.e. should strip away " IUUP_HEADER "\n");
114 expect_rx_payload = RTP_HEADER "f03c" RTP_PAYLOAD;
115 rx_pdu(cn,
116 msgb_from_hex("IuUP-Data",
117 RTP_HEADER IUUP_HEADER RTP_PAYLOAD));
118
119 printf("\nTransmit RTP. Expecting tx_msg() with inserted IuUP header\n");
120 expect_tx_msg = RTP_HEADER "000002b3" RTP_PAYLOAD;
121 tx_payload(cn,
122 msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD));
123
124 printf("\nMore RTP, each time the Frame Nr advances, causing a new header CRC.\n");
125 expect_tx_msg = RTP_HEADER "0100e2b3" RTP_PAYLOAD;
126 tx_payload(cn,
127 msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD));
128 expect_tx_msg = RTP_HEADER "02007eb3" RTP_PAYLOAD;
129 tx_payload(cn,
130 msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD));
131 expect_tx_msg = RTP_HEADER "03009eb3" RTP_PAYLOAD;
132 tx_payload(cn,
133 msgb_from_hex("RTP data", RTP_HEADER "f03c" RTP_PAYLOAD));
134
135 printf("All done.\n");
136}
137
138static const struct log_info_cat log_categories[] = {
139};
140
141const struct log_info log_info = {
142 .cat = log_categories,
143 .num_cat = ARRAY_SIZE(log_categories),
144};
145
146int main(void)
147{
148 ctx = talloc_named_const(NULL, 0, __FILE__);
149 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
150 osmo_init_logging2(ctx, &log_info);
151
152 test_cn_session();
153
154 talloc_free(msgb_ctx);
155 return 0;
156}