Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* |
| 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> |
| 25 | #include <osmocom/legacy_mgcp/mgcp.h> |
| 26 | #include <osmocom/legacy_mgcp/mgcpgw_client.h> |
| 27 | #include <osmocom/legacy_mgcp/mgcpgw_client_internal.h> |
| 28 | |
| 29 | void *ctx; |
| 30 | |
| 31 | #define buf_len 4096 |
| 32 | |
| 33 | #if 0 |
| 34 | static struct msgb *from_hex(const char *hex) |
| 35 | { |
| 36 | struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex"); |
| 37 | unsigned int l = osmo_hexparse(hex, msg->data, buf_len); |
| 38 | msg->l2h = msgb_put(msg, l); |
| 39 | return msg; |
| 40 | } |
| 41 | |
| 42 | static struct msgb *mgcp_from_str(const char *head, const char *params) |
| 43 | { |
| 44 | struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str"); |
| 45 | unsigned int l; |
| 46 | char *data; |
| 47 | l = strlen(head); |
| 48 | msg->l2h = msgb_put(msg, l); |
| 49 | data = (char*)msgb_l2(msg); |
| 50 | strncpy(data, head, l); |
| 51 | |
| 52 | data = (char*)msgb_put(msg, 1); |
| 53 | *data = '\n'; |
| 54 | |
| 55 | l = strlen(params); |
| 56 | data = (char*)msgb_put(msg, l); |
| 57 | strncpy(data, params, l); |
| 58 | |
| 59 | return msg; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | static struct msgb *from_str(const char *str) |
| 64 | { |
| 65 | struct msgb *msg = msgb_alloc(buf_len, "from_str"); |
| 66 | unsigned int l = strlen(str); |
| 67 | char *data; |
| 68 | msg->l2h = msgb_put(msg, l); |
| 69 | data = (char*)msgb_l2(msg); |
| 70 | strncpy(data, str, l); |
| 71 | return msg; |
| 72 | } |
| 73 | |
| 74 | static struct mgcpgw_client_conf conf; |
| 75 | struct mgcpgw_client *mgcp = NULL; |
| 76 | |
| 77 | static void reply_to(mgcp_trans_id_t trans_id, int code, const char *comment, |
| 78 | int conn_id, const char *params) |
| 79 | { |
| 80 | static char compose[4096 - 128]; |
| 81 | int len; |
| 82 | |
| 83 | len = snprintf(compose, sizeof(compose), |
| 84 | "%d %u %s\r\nI: %d\n\n%s", |
| 85 | code, trans_id, comment, conn_id, params); |
| 86 | OSMO_ASSERT(len < sizeof(compose)); |
| 87 | OSMO_ASSERT(len > 0); |
| 88 | |
| 89 | printf("composed response:\n-----\n%s\n-----\n", |
| 90 | compose); |
| 91 | mgcpgw_client_rx(mgcp, from_str(compose)); |
| 92 | } |
| 93 | |
| 94 | void test_response_cb(struct mgcp_response *response, void *priv) |
| 95 | { |
| 96 | OSMO_ASSERT(priv == mgcp); |
| 97 | mgcp_response_parse_params(response); |
| 98 | |
| 99 | printf("response cb received:\n" |
| 100 | " head.response_code = %d\n" |
| 101 | " head.trans_id = %u\n" |
| 102 | " head.comment = %s\n" |
| 103 | " audio_port = %u\n", |
| 104 | response->head.response_code, |
| 105 | response->head.trans_id, |
| 106 | response->head.comment, |
| 107 | response->audio_port |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg) |
| 112 | { |
| 113 | mgcp_trans_id_t trans_id; |
| 114 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 115 | char *end; |
| 116 | |
| 117 | OSMO_ASSERT(mgcpgw_client_pending_add(mgcp, trans_id, test_response_cb, mgcp)); |
| 118 | |
| 119 | end = (char*)msgb_put(msg, 1); |
| 120 | *end = '\0'; |
| 121 | printf("composed:\n-----\n%s\n-----\n", |
| 122 | (char*)msgb_l2(msg)); |
| 123 | |
| 124 | talloc_free(msg); |
| 125 | return trans_id; |
| 126 | } |
| 127 | |
| 128 | void test_crcx(void) |
| 129 | { |
| 130 | struct msgb *msg; |
| 131 | mgcp_trans_id_t trans_id; |
| 132 | |
| 133 | printf("\n===== %s =====\n", __func__); |
| 134 | |
| 135 | if (mgcp) |
| 136 | talloc_free(mgcp); |
| 137 | mgcp = mgcpgw_client_init(ctx, &conf); |
| 138 | |
| 139 | msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK); |
| 140 | trans_id = dummy_mgcp_send(msg); |
| 141 | |
| 142 | reply_to(trans_id, 200, "OK", 1, |
| 143 | "v=0\r\n" |
| 144 | "o=- 1 23 IN IP4 10.9.1.120\r\n" |
| 145 | "s=-\r\n" |
| 146 | "c=IN IP4 10.9.1.120\r\n" |
| 147 | "t=0 0\r\n" |
| 148 | "m=audio 16002 RTP/AVP 98\r\n" |
| 149 | "a=rtpmap:98 AMR/8000\r\n" |
| 150 | "a=ptime:20\r\n"); |
| 151 | } |
| 152 | |
| 153 | static const struct log_info_cat log_categories[] = { |
| 154 | }; |
| 155 | |
| 156 | const struct log_info log_info = { |
| 157 | .cat = log_categories, |
| 158 | .num_cat = ARRAY_SIZE(log_categories), |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | int main(int argc, char **argv) |
| 163 | { |
| 164 | ctx = talloc_named_const(NULL, 1, "mgcpgw_client_test"); |
| 165 | msgb_talloc_ctx_init(ctx, 0); |
| 166 | osmo_init_logging(&log_info); |
| 167 | |
| 168 | mgcpgw_client_conf_init(&conf); |
| 169 | |
| 170 | test_crcx(); |
| 171 | |
| 172 | printf("Done\n"); |
| 173 | fprintf(stderr, "Done\n"); |
| 174 | return EXIT_SUCCESS; |
| 175 | } |