mirror of
https://gitea.osmocom.org/cellular-infrastructure/osmo-mgw.git
synced 2025-11-01 20:43:47 +00:00
Add mgcp_common.h to libosmo-mgcp-client, to not need to share a header file
with libosmo-legacy-mgcp (nor the upcoming libosmo-mgcp). Both libraries use
the enum mgcp_connection_mode (and a for-loop macro and a string mangling
function), so far declared in mgcp.h, and both mgcp-dev and mgcp-client-dev
debian packages would require this header file to be installed. So far the
mgcp-client-dev lacks this header file, which causes the osmo-msc debian
package to fail building. Ways to solve:
- If both -dev debian packages installed the same header file in the same
place, they would conflict if ever installed at the same time.
- mgcp-client-dev can depend on mgcp-dev, but it seems bad to keep such a large
dependency for just one enum and two helpers.
- Instead, this patch solves this by copying the few definitions to
libosmo-mgcp-client.
Once libosmo-mgcp-client has its own copy of those definitions, it is
fully self-contained and depending builds (osmo-msc deb) will succeed.
Copy the few actually common definitions to new header
<osmocom/mgcp_client/mgcp_common.h>. The nature of this .h is that it may be
shared with future libosmo-mgcp without causing linking problems.
Remove libosmo-legacy-mgcp/mgcp_common.c file from the build of
libosmo-mgcp-client, no longer needed.
Add to new mgcp_common.h:
- enum mgcp_connection_mode;
- for_each_non_empty_line() macro.
- mgcp_msg_terminate_nul() as static function. Its complexity is just above
your average static inline, but being inline is a way to use it in both mgcp
and mgcp_client without linking problems.
Replace for_each_line() use in mgcp_client with for_each_non_empty_line()
(for_each_non_empty_line() replaces for_each_line() and uses strtok_r() instead
of a local reinvention).
mgcp_connection_mode_strs are actually only used in libosmo-mgcp-client, so
rename to mgcp_client_ prefix and move to mgcp_client.c.
BTW, the future plan for upcoming libosmo-mgcp is to use the identical header
file, and keep only one copy in the git source tree. The second copy may be
generated during 'make', to avoid code dup while having two distinct headers.
Related: I8e3359bedf973077c0a038aa04f5371a00c48fa0 (fix osmo-msc after this),
I7a5d3b9a2eb90be7e34b95efa529429f2e6c3ed8 (mgcp_common.h)
Change-Id: Ifb8f3fc2b399662a9dbba174e942352a1a21df3f
175 lines
4.1 KiB
C
175 lines
4.1 KiB
C
/*
|
|
* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
|
|
* All Rights Reserved
|
|
*
|
|
* Author: Neels Hofmeyr
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <osmocom/core/msgb.h>
|
|
#include <osmocom/core/application.h>
|
|
#include <osmocom/mgcp_client/mgcp_client.h>
|
|
#include <osmocom/mgcp_client/mgcp_client_internal.h>
|
|
|
|
void *ctx;
|
|
|
|
#define buf_len 4096
|
|
|
|
#if 0
|
|
static struct msgb *from_hex(const char *hex)
|
|
{
|
|
struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex");
|
|
unsigned int l = osmo_hexparse(hex, msg->data, buf_len);
|
|
msg->l2h = msgb_put(msg, l);
|
|
return msg;
|
|
}
|
|
|
|
static struct msgb *mgcp_from_str(const char *head, const char *params)
|
|
{
|
|
struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str");
|
|
unsigned int l;
|
|
char *data;
|
|
l = strlen(head);
|
|
msg->l2h = msgb_put(msg, l);
|
|
data = (char*)msgb_l2(msg);
|
|
strncpy(data, head, l);
|
|
|
|
data = (char*)msgb_put(msg, 1);
|
|
*data = '\n';
|
|
|
|
l = strlen(params);
|
|
data = (char*)msgb_put(msg, l);
|
|
strncpy(data, params, l);
|
|
|
|
return msg;
|
|
}
|
|
#endif
|
|
|
|
static struct msgb *from_str(const char *str)
|
|
{
|
|
struct msgb *msg = msgb_alloc(buf_len, "from_str");
|
|
unsigned int l = strlen(str);
|
|
char *data;
|
|
msg->l2h = msgb_put(msg, l);
|
|
data = (char*)msgb_l2(msg);
|
|
strncpy(data, str, l);
|
|
return msg;
|
|
}
|
|
|
|
static struct mgcp_client_conf conf;
|
|
struct mgcp_client *mgcp = NULL;
|
|
|
|
static void reply_to(mgcp_trans_id_t trans_id, int code, const char *comment,
|
|
int conn_id, const char *params)
|
|
{
|
|
static char compose[4096 - 128];
|
|
int len;
|
|
|
|
len = snprintf(compose, sizeof(compose),
|
|
"%d %u %s\r\nI: %d\n\n%s",
|
|
code, trans_id, comment, conn_id, params);
|
|
OSMO_ASSERT(len < sizeof(compose));
|
|
OSMO_ASSERT(len > 0);
|
|
|
|
printf("composed response:\n-----\n%s\n-----\n",
|
|
compose);
|
|
mgcp_client_rx(mgcp, from_str(compose));
|
|
}
|
|
|
|
void test_response_cb(struct mgcp_response *response, void *priv)
|
|
{
|
|
OSMO_ASSERT(priv == mgcp);
|
|
mgcp_response_parse_params(response);
|
|
|
|
printf("response cb received:\n"
|
|
" head.response_code = %d\n"
|
|
" head.trans_id = %u\n"
|
|
" head.comment = %s\n"
|
|
" audio_port = %u\n",
|
|
response->head.response_code,
|
|
response->head.trans_id,
|
|
response->head.comment,
|
|
response->audio_port
|
|
);
|
|
}
|
|
|
|
mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg)
|
|
{
|
|
mgcp_trans_id_t trans_id;
|
|
trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
|
|
char *end;
|
|
|
|
OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp));
|
|
|
|
end = (char*)msgb_put(msg, 1);
|
|
*end = '\0';
|
|
printf("composed:\n-----\n%s\n-----\n",
|
|
(char*)msgb_l2(msg));
|
|
|
|
talloc_free(msg);
|
|
return trans_id;
|
|
}
|
|
|
|
void test_crcx(void)
|
|
{
|
|
struct msgb *msg;
|
|
mgcp_trans_id_t trans_id;
|
|
|
|
printf("\n===== %s =====\n", __func__);
|
|
|
|
if (mgcp)
|
|
talloc_free(mgcp);
|
|
mgcp = mgcp_client_init(ctx, &conf);
|
|
|
|
msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK);
|
|
trans_id = dummy_mgcp_send(msg);
|
|
|
|
reply_to(trans_id, 200, "OK", 1,
|
|
"v=0\r\n"
|
|
"o=- 1 23 IN IP4 10.9.1.120\r\n"
|
|
"s=-\r\n"
|
|
"c=IN IP4 10.9.1.120\r\n"
|
|
"t=0 0\r\n"
|
|
"m=audio 16002 RTP/AVP 98\r\n"
|
|
"a=rtpmap:98 AMR/8000\r\n"
|
|
"a=ptime:20\r\n");
|
|
}
|
|
|
|
static const struct log_info_cat log_categories[] = {
|
|
};
|
|
|
|
const struct log_info log_info = {
|
|
.cat = log_categories,
|
|
.num_cat = ARRAY_SIZE(log_categories),
|
|
};
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
ctx = talloc_named_const(NULL, 1, "mgcp_client_test");
|
|
msgb_talloc_ctx_init(ctx, 0);
|
|
osmo_init_logging(&log_info);
|
|
|
|
mgcp_client_conf_init(&conf);
|
|
|
|
test_crcx();
|
|
|
|
printf("Done\n");
|
|
fprintf(stderr, "Done\n");
|
|
return EXIT_SUCCESS;
|
|
}
|