blob: 69e18104a7a669f9682a531c5b1d2c88b8ea03e2 [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/*
2 * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
3 * All Rights Reserved
4 *
5 * Author: Neels Hofmeyr
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <string.h>
22
23#include <osmocom/core/msgb.h>
24#include <osmocom/core/application.h>
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020025#include <osmocom/mgcp_client/mgcp_client.h>
26#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020027
28void *ctx;
29
30#define buf_len 4096
31
32#if 0
33static struct msgb *from_hex(const char *hex)
34{
35 struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex");
36 unsigned int l = osmo_hexparse(hex, msg->data, buf_len);
37 msg->l2h = msgb_put(msg, l);
38 return msg;
39}
40
41static struct msgb *mgcp_from_str(const char *head, const char *params)
42{
43 struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str");
44 unsigned int l;
45 char *data;
46 l = strlen(head);
47 msg->l2h = msgb_put(msg, l);
48 data = (char*)msgb_l2(msg);
49 strncpy(data, head, l);
50
51 data = (char*)msgb_put(msg, 1);
52 *data = '\n';
53
54 l = strlen(params);
55 data = (char*)msgb_put(msg, l);
56 strncpy(data, params, l);
57
58 return msg;
59}
60#endif
61
62static struct msgb *from_str(const char *str)
63{
64 struct msgb *msg = msgb_alloc(buf_len, "from_str");
65 unsigned int l = strlen(str);
66 char *data;
67 msg->l2h = msgb_put(msg, l);
68 data = (char*)msgb_l2(msg);
69 strncpy(data, str, l);
70 return msg;
71}
72
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020073static struct mgcp_client_conf conf;
74struct mgcp_client *mgcp = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +020075
76static void reply_to(mgcp_trans_id_t trans_id, int code, const char *comment,
77 int conn_id, const char *params)
78{
79 static char compose[4096 - 128];
80 int len;
81
82 len = snprintf(compose, sizeof(compose),
83 "%d %u %s\r\nI: %d\n\n%s",
84 code, trans_id, comment, conn_id, params);
85 OSMO_ASSERT(len < sizeof(compose));
86 OSMO_ASSERT(len > 0);
87
88 printf("composed response:\n-----\n%s\n-----\n",
89 compose);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020090 mgcp_client_rx(mgcp, from_str(compose));
Neels Hofmeyre9920f22017-07-10 15:07:22 +020091}
92
93void test_response_cb(struct mgcp_response *response, void *priv)
94{
95 OSMO_ASSERT(priv == mgcp);
96 mgcp_response_parse_params(response);
97
98 printf("response cb received:\n"
99 " head.response_code = %d\n"
100 " head.trans_id = %u\n"
101 " head.comment = %s\n"
102 " audio_port = %u\n",
103 response->head.response_code,
104 response->head.trans_id,
105 response->head.comment,
106 response->audio_port
107 );
108}
109
110mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg)
111{
112 mgcp_trans_id_t trans_id;
113 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
114 char *end;
115
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200116 OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200117
118 end = (char*)msgb_put(msg, 1);
119 *end = '\0';
120 printf("composed:\n-----\n%s\n-----\n",
121 (char*)msgb_l2(msg));
122
123 talloc_free(msg);
124 return trans_id;
125}
126
127void test_crcx(void)
128{
129 struct msgb *msg;
130 mgcp_trans_id_t trans_id;
131
132 printf("\n===== %s =====\n", __func__);
133
134 if (mgcp)
135 talloc_free(mgcp);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200136 mgcp = mgcp_client_init(ctx, &conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200137
138 msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK);
139 trans_id = dummy_mgcp_send(msg);
140
141 reply_to(trans_id, 200, "OK", 1,
142 "v=0\r\n"
143 "o=- 1 23 IN IP4 10.9.1.120\r\n"
144 "s=-\r\n"
145 "c=IN IP4 10.9.1.120\r\n"
146 "t=0 0\r\n"
147 "m=audio 16002 RTP/AVP 98\r\n"
148 "a=rtpmap:98 AMR/8000\r\n"
149 "a=ptime:20\r\n");
150}
151
152static const struct log_info_cat log_categories[] = {
153};
154
155const struct log_info log_info = {
156 .cat = log_categories,
157 .num_cat = ARRAY_SIZE(log_categories),
158};
159
160
161int main(int argc, char **argv)
162{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200163 ctx = talloc_named_const(NULL, 1, "mgcp_client_test");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200164 msgb_talloc_ctx_init(ctx, 0);
165 osmo_init_logging(&log_info);
Philipp Maier8348abb2017-10-25 12:14:13 +0200166 log_set_print_filename(osmo_stderr_target, 0);
167 log_set_print_timestamp(osmo_stderr_target, 0);
168 log_set_use_color(osmo_stderr_target, 0);
169 log_set_print_category(osmo_stderr_target, 1);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200170
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200171 mgcp_client_conf_init(&conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200172
173 test_crcx();
174
175 printf("Done\n");
176 fprintf(stderr, "Done\n");
177 return EXIT_SUCCESS;
178}