diff --git a/examples/osmux-test-input.c b/examples/osmux-test-input.c index f611dce..b4e5b24 100644 --- a/examples/osmux-test-input.c +++ b/examples/osmux-test-input.c @@ -71,7 +71,11 @@ static struct osmo_rtp_handle *rtp; static void osmux_deliver(struct msgb *batch_msg, void *data) { - printf("sending batch (len=%d)\n", batch_msg->len); + char buf[1024]; + + osmux_snprintf(buf, sizeof(buf), batch_msg); + LOGP(DOSMUX_TEST, LOGL_DEBUG, "sending batch (len=%d): %s\n", + batch_msg->len, buf); osmo_dgram_send(conn, batch_msg); } @@ -131,8 +135,6 @@ int read_cb(struct osmo_dgram *conn) struct rtp_hdr *rtph; int ret, ccid; - LOGP(DOSMUX_TEST, LOGL_DEBUG, "received message from datagram\n"); - msg = msgb_alloc(RTP_MSGB_SIZE, "OSMUX/test"); if (msg == NULL) { LOGP(DOSMUX_TEST, LOGL_ERROR, "cannot allocate message\n"); @@ -154,6 +156,11 @@ int read_cb(struct osmo_dgram *conn) if (rtph->payload_type == RTP_PT_AMR) amr_write(msg); + char buf[1024]; + + osmo_rtp_snprintf(buf, sizeof(buf), msg); + LOGP(DOSMUX_TEST, LOGL_DEBUG, "received RTP (len=%d): %s\n", msg->len, buf); + ccid = get_ccid(rtph->ssrc); if (ccid < 0) register_ccid(rtph->ssrc); diff --git a/examples/osmux-test-output.c b/examples/osmux-test-output.c index 5fe2bba..ea07606 100644 --- a/examples/osmux-test-output.c +++ b/examples/osmux-test-output.c @@ -78,7 +78,7 @@ static void tx_cb(struct msgb *msg, void *data) char buf[4096]; osmo_rtp_snprintf(buf, sizeof(buf), msg); - printf("sending: %s\n", buf); + LOGP(DOSMUX_TEST, LOGL_DEBUG, "sending: %s\n", buf); osmo_dgram_send(conn, msg); amr_write(msg); @@ -102,10 +102,12 @@ int read_cb(struct osmo_dgram *conn) return -1; } - LOGP(DOSMUX_TEST, LOGL_DEBUG, "received OSMUX message (len=%d)\n", msg->len); + char buf[1024]; + osmux_snprintf(buf, sizeof(buf), msg); + LOGP(DOSMUX_TEST, LOGL_DEBUG, "received OSMUX message (len=%d) %s\n", + msg->len, buf); while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) { - printf("tx_sched\n"); osmux_xfrm_output(osmuxh, &h_output, &list); osmux_tx_sched(&list, tx_cb, NULL); } diff --git a/include/osmocom/netif/osmux.h b/include/osmocom/netif/osmux.h index d7ebf55..d50c7d5 100644 --- a/include/osmocom/netif/osmux.h +++ b/include/osmocom/netif/osmux.h @@ -56,11 +56,15 @@ struct osmux_out_handle { uint32_t rtp_timestamp; }; +struct osmux_hdr *osmux_get_hdr(struct msgb *msg); + static inline uint8_t *osmux_get_payload(struct osmux_hdr *osmuxh) { return (uint8_t *)osmuxh + sizeof(struct osmux_hdr); } +int osmux_snprintf(char *buf, size_t size, struct msgb *msg); + void osmux_xfrm_input_init(struct osmux_in_handle *h); int osmux_xfrm_input(struct osmux_in_handle *h, struct msgb *msg, int ccid); diff --git a/src/osmux.c b/src/osmux.c index d2dce87..5dd0210 100644 --- a/src/osmux.c +++ b/src/osmux.c @@ -34,6 +34,23 @@ static void *osmux_ctx; +struct osmux_hdr *osmux_get_hdr(struct msgb *msg) +{ + struct osmux_hdr *osmuxh = (struct osmux_hdr *)msg->data; + + if (msg->len < sizeof(struct osmux_hdr)) { + DEBUGPC(DLMUX, "received OSMUX frame too short (len = %d)\n", + msg->len); + return NULL; + } + return osmuxh; +} + +static uint32_t osmux_get_payload_len(struct osmux_hdr *osmuxh) +{ + return osmo_amr_bytes(osmuxh->amr_ft) * (osmuxh->ctr+1); +} + struct osmux_hdr *osmux_xfrm_output_pull(struct msgb *msg) { struct osmux_hdr *osmuxh = NULL; @@ -505,3 +522,78 @@ void osmux_xfrm_output_init(struct osmux_out_handle *h) h->rtp_seq = (uint16_t)random(); h->rtp_timestamp = (uint32_t)random(); } + +#define SNPRINTF_BUFFER_SIZE(ret, size, len, offset) \ + size += ret; \ + if (ret > len) \ + ret = len; \ + offset += ret; \ + len -= ret; + +static int osmux_snprintf_header(char *buf, size_t size, struct osmux_hdr *osmuxh) +{ + int ret; + int len = size, offset = 0; + + ret = snprintf(buf, len, "OSMUX seq=%03u ccid=%03u " + "ft=%01u ctr=%01u " + "amr_f=%01u amr_q=%01u " + "amr_ft=%02u amr_cmr=%02u ", + osmuxh->seq, osmuxh->circuit_id, + osmuxh->ft, osmuxh->ctr, + osmuxh->amr_f, osmuxh->amr_q, + osmuxh->amr_ft, osmuxh->amr_cmr); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + return offset; +} + +static int osmux_snprintf_payload(char *buf, size_t size, + const uint8_t *payload, int payload_len) +{ + int ret, i; + int len = size, offset = 0; + + for (i=0; ilen, len = size; + struct osmux_hdr *osmuxh = (struct osmux_hdr *)msg->data; + int this_len; + + while (msg_len > 0) { + this_len = sizeof(struct osmux_hdr) + + osmux_get_payload_len(osmuxh); + + ret = osmux_snprintf_header(buf+offset, size, osmuxh); + if (ret < 0) + break; + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + ret = osmux_snprintf_payload(buf+offset, size, + osmux_get_payload(osmuxh), + osmux_get_payload_len(osmuxh)); + if (ret < 0) + break; + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + msg_len -= this_len; + + osmuxh = (struct osmux_hdr *)((uint8_t *)msg->data + this_len); + } + + return offset; +}