Neels Hofmeyr | 2188a77 | 2016-05-20 21:59:55 +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 <openbsc/mgcp.h> |
| 26 | #include <openbsc/mgcpgw_client.h> |
| 27 | |
| 28 | void *ctx; |
| 29 | |
| 30 | #define buf_len 4096 |
| 31 | |
| 32 | #if 0 |
| 33 | static 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 | |
| 41 | static 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 | |
| 62 | static 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 | |
| 73 | static struct mgcpgw_client_conf conf; |
| 74 | struct mgcpgw_client *mgcp = NULL; |
| 75 | |
| 76 | static 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); |
| 90 | mgcpgw_client_rx(mgcp, from_str(compose)); |
| 91 | } |
| 92 | |
| 93 | void 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 | |
| 110 | mgcp_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 | |
| 116 | OSMO_ASSERT(mgcpgw_client_pending_add(mgcp, trans_id, test_response_cb, mgcp)); |
| 117 | |
| 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 | |
| 127 | void 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); |
| 136 | mgcp = mgcpgw_client_init(ctx, &conf); |
| 137 | |
| 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 | |
| 152 | int main(int argc, char **argv) |
| 153 | { |
| 154 | ctx = talloc_named_const(NULL, 1, "mgcpgw_client_test"); |
| 155 | msgb_talloc_ctx_init(ctx, 0); |
| 156 | osmo_init_logging(&log_info); |
| 157 | |
| 158 | mgcpgw_client_conf_init(&conf); |
| 159 | |
| 160 | test_crcx(); |
| 161 | |
| 162 | printf("Done\n"); |
| 163 | fprintf(stderr, "Done\n"); |
| 164 | return EXIT_SUCCESS; |
| 165 | } |