client: add unified function to generate MGCP messages

currently the only way to generate MGCP messages is to use
mgcp_msg_crcx(), mgcp_msg_mdcx() and mgcp_msg_dlcx(). All
three function take a fixed set of parameters via their
parameter list. There is no way to add or leave away optional
parameters.

add function mgcp_msg_gen(), this function takes a unified
message struct. The struct features a presence bitmask which
allows to enable and disable parameters as needed. It is also
possible to add new parameters in the future without breaking
the API.

Depends: libosmocore I15e1af68616309555d0ed9ac5da027c9833d42e3

Change-Id: I29c5e2fb972896faeb771ba040f015592487fcbe
diff --git a/tests/mgcp_client/mgcp_client_test.c b/tests/mgcp_client/mgcp_client_test.c
index 69e1810..7977a6a 100644
--- a/tests/mgcp_client/mgcp_client_test.c
+++ b/tests/mgcp_client/mgcp_client_test.c
@@ -149,6 +149,80 @@
 		"a=ptime:20\r\n");
 }
 
+void test_mgcp_msg(void)
+{
+	struct msgb *msg;
+	char audio_ip_overflow[5000];
+
+	/* A message struct prefilled with some arbitary values */
+	struct mgcp_msg mgcp_msg = {
+		.audio_ip = "192.168.100.23",
+		.endpoint = "23@mgw",
+		.audio_port = 1234,
+		.call_id = 47,
+		.conn_id = 11,
+		.conn_mode = MGCP_CONN_RECV_SEND
+	};
+
+	if (mgcp)
+		talloc_free(mgcp);
+	mgcp = mgcp_client_init(ctx, &conf);
+
+	printf("\n");
+
+	printf("Generated CRCX message:\n");
+	mgcp_msg.verb = MGCP_VERB_CRCX;
+	mgcp_msg.presence =
+	    (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
+	     MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE);
+	msg = mgcp_msg_gen(mgcp, &mgcp_msg);
+	printf("%s\n", (char *)msg->data);
+
+	printf("Generated MDCX message:\n");
+	mgcp_msg.verb = MGCP_VERB_MDCX;
+	mgcp_msg.presence =
+	    (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
+	     MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
+	     MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
+	msg = mgcp_msg_gen(mgcp, &mgcp_msg);
+	printf("%s\n", (char *)msg->data);
+
+	printf("Generated DLCX message:\n");
+	mgcp_msg.verb = MGCP_VERB_DLCX;
+	mgcp_msg.presence =
+	    (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
+	     MGCP_MSG_PRESENCE_CONN_ID);
+	msg = mgcp_msg_gen(mgcp, &mgcp_msg);
+	printf("%s\n", (char *)msg->data);
+
+	printf("Generated AUEP message:\n");
+	mgcp_msg.verb = MGCP_VERB_AUEP;
+	mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
+	msg = mgcp_msg_gen(mgcp, &mgcp_msg);
+	printf("%s\n", msg->data);
+
+	printf("Generated RSIP message:\n");
+	mgcp_msg.verb = MGCP_VERB_RSIP;
+	mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
+	msg = mgcp_msg_gen(mgcp, &mgcp_msg);
+	printf("%s\n", (char *)msg->data);
+
+	printf("Overfolow test:\n");
+	mgcp_msg.verb = MGCP_VERB_MDCX;
+	mgcp_msg.presence =
+	    (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
+	     MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
+	     MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
+	memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow));
+	audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0';
+	mgcp_msg.audio_ip = audio_ip_overflow;
+	msg = mgcp_msg_gen(mgcp, &mgcp_msg);
+	OSMO_ASSERT(msg == NULL);
+
+	printf("\n");
+	msgb_free(msg);
+}
+
 static const struct log_info_cat log_categories[] = {
 };
 
@@ -171,6 +245,7 @@
 	mgcp_client_conf_init(&conf);
 
 	test_crcx();
+	test_mgcp_msg();
 
 	printf("Done\n");
 	fprintf(stderr, "Done\n");
diff --git a/tests/mgcp_client/mgcp_client_test.err b/tests/mgcp_client/mgcp_client_test.err
index a965a70..24151ee 100644
--- a/tests/mgcp_client/mgcp_client_test.err
+++ b/tests/mgcp_client/mgcp_client_test.err
@@ -1 +1,2 @@
+DLMGCP message buffer to small, can not generate MGCP message
 Done
diff --git a/tests/mgcp_client/mgcp_client_test.ok b/tests/mgcp_client/mgcp_client_test.ok
index d35f2d6..e3b6113 100644
--- a/tests/mgcp_client/mgcp_client_test.ok
+++ b/tests/mgcp_client/mgcp_client_test.ok
@@ -28,4 +28,34 @@
   head.trans_id = 1
   head.comment = OK
   audio_port = 16002
+
+Generated CRCX message:
+CRCX 1 23@mgw MGCP 1.0

+C: 2f

+I: 11

+L: p:20, a:AMR, nt:IN

+M: sendrecv

+
+Generated MDCX message:
+MDCX 2 23@mgw MGCP 1.0

+C: 2f

+I: 11

+M: sendrecv

+

+c=IN IP4 192.168.100.23

+m=audio 1234 RTP/AVP 255

+
+Generated DLCX message:
+DLCX 3 23@mgw MGCP 1.0

+C: 2f

+I: 11

+
+Generated AUEP message:
+AUEP 4 23@mgw MGCP 1.0

+
+Generated RSIP message:
+RSIP 5 23@mgw MGCP 1.0

+
+Overfolow test:
+
 Done