Timur Davydov has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-trx/+/42357?usp=email )
Change subject: proto_trxd: add helpers to prepare burst indication packets ......................................................................
proto_trxd: add helpers to prepare burst indication packets
- introduce trxd_prepare_burst_ind_v0() and trxd_prepare_burst_ind_v1() - reuse prepared packet size in send helpers - handle idle frames during packet preparation - use ssize_t consistently for prepared/write sizes
Change-Id: Ie94a1df4879cbf6056cd05c9621c46db1ed972fd --- M Transceiver52M/proto_trxd.c M Transceiver52M/proto_trxd.h 2 files changed, 59 insertions(+), 19 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/57/42357/1
diff --git a/Transceiver52M/proto_trxd.c b/Transceiver52M/proto_trxd.c index e5a889d..7cfd0a3 100644 --- a/Transceiver52M/proto_trxd.c +++ b/Transceiver52M/proto_trxd.c @@ -65,15 +65,18 @@ soft_bits[i] = (uint8_t) round(bi->rx_burst[i] * 255.0); }
-bool trxd_send_burst_ind_v0(size_t chan, int fd, const struct trx_ul_burst_ind *bi) { - int rc; - - /* v0 doesn't support idle frames, they are simply dropped, not sent */ - if (bi->idle) - return true; - +ssize_t trxd_prepare_burst_ind_v0(const struct trx_ul_burst_ind *bi, char* buf, size_t maxLen) +{ /* +2: Historically (OpenBTS times), two extra non-used bytes are sent appended to each burst */ - char buf[sizeof(struct trxd_hdr_v0) + bi->nbits + 2]; + const size_t buf_len = sizeof(struct trxd_hdr_v0) + bi->nbits + 2; + + if (maxLen < buf_len) + return -1; + + /* v0 doesn't support idle frames, they are not filled */ + if (bi->idle) + return 0; + struct trxd_hdr_v0* pkt = (struct trxd_hdr_v0*)buf;
trxd_fill_common(&pkt->common, bi, 0); @@ -83,24 +86,41 @@ /* +1: Historical reason. There's an uninitizalied byte in there: pkt->soft_bits[bi->nbits] */ pkt->soft_bits[bi->nbits + 1] = '\0';
- rc = write(fd, buf, sizeof(struct trxd_hdr_v0) + bi->nbits + 2); + return buf_len; +} + +bool trxd_send_burst_ind_v0(size_t chan, int fd, const struct trx_ul_burst_ind *bi) { + /* +2: Historically (OpenBTS times), two extra non-used bytes are sent appended to each burst */ + char buf[sizeof(struct trxd_hdr_v0) + bi->nbits + 2]; + + const ssize_t pktsz = trxd_prepare_burst_ind_v0(bi, buf, sizeof(buf)); + if (pktsz < 0) { + CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "trxd_prepare_burst_ind_v0 failed: %zd\n", pktsz); + return false; + } + + /* packet size 0 indicates idle frames, they are simply dropped, not sent */ + if (pktsz == 0) + return true; + + const ssize_t rc = write(fd, buf, pktsz); if (rc <= 0) { - CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "mDataSockets write(%d) failed: %d\n", fd, rc); + CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "mDataSockets write(%d) failed: %zd\n", fd, rc); return false; } return true; }
-bool trxd_send_burst_ind_v1(size_t chan, int fd, const struct trx_ul_burst_ind *bi) { - int rc; - size_t buf_len; - - buf_len = sizeof(struct trxd_hdr_v1); +ssize_t trxd_prepare_burst_ind_v1(const struct trx_ul_burst_ind *bi, char* buffer, size_t maxLen) +{ + size_t buf_len = sizeof(struct trxd_hdr_v1); if (!bi->idle) buf_len += bi->nbits; - char buf[buf_len];
- struct trxd_hdr_v1* pkt = (struct trxd_hdr_v1*)buf; + if (maxLen < buf_len) + return -1; + + struct trxd_hdr_v1* pkt = (struct trxd_hdr_v1*)buffer; trxd_fill_common(&pkt->common, bi, 1); trxd_fill_v0_specific(&pkt->v0, bi); trxd_fill_v1_specific(&pkt->v1, bi); @@ -108,9 +128,26 @@ if (!bi->idle) trxd_fill_burst_normalized255(&pkt->soft_bits[0], bi);
- rc = write(fd, buf, buf_len); + return buf_len; +} + +bool trxd_send_burst_ind_v1(size_t chan, int fd, const struct trx_ul_burst_ind *bi) { + size_t buf_len; + + buf_len = sizeof(struct trxd_hdr_v1); + if (!bi->idle) + buf_len += bi->nbits; + char buf[buf_len]; + + const ssize_t pktsize = trxd_prepare_burst_ind_v1(bi, buf, buf_len); + if (pktsize <= 0) { + CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "trxd_prepare_burst_ind_v1 failed: %zd\n", pktsize); + return false; + } + + const ssize_t rc = write(fd, buf, pktsize); if (rc <= 0) { - CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "mDataSockets write(%d) failed: %d\n", fd, rc); + CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "mDataSockets write(%d) failed: %zd\n", fd, rc); return false; } return true; diff --git a/Transceiver52M/proto_trxd.h b/Transceiver52M/proto_trxd.h index c250a74..e7e2e68 100644 --- a/Transceiver52M/proto_trxd.h +++ b/Transceiver52M/proto_trxd.h @@ -36,7 +36,10 @@ float ci; // Carrier-to-Interference ratio, in dB };
+ssize_t trxd_prepare_burst_ind_v0(const struct trx_ul_burst_ind *bi, char* buf, size_t maxLen); bool trxd_send_burst_ind_v0(size_t chan, int fd, const struct trx_ul_burst_ind *bi); + +ssize_t trxd_prepare_burst_ind_v1(const struct trx_ul_burst_ind *bi, char* buffer, size_t maxLen); bool trxd_send_burst_ind_v1(size_t chan, int fd, const struct trx_ul_burst_ind *bi);
/* The latest supported TRXD header format version */