From f62a64e440a1cb8a654e3f49b8f4202022e0348a Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Tue, 26 Mar 2013 09:19:53 +0100 Subject: [PATCH] TRX: Add AMR Payload handling --- src/osmo-bts-trx/amr.c | 81 ++++++++++++++++++++++++++++++++++++++++++ src/osmo-bts-trx/amr.h | 8 +++++ 2 files changed, 89 insertions(+) create mode 100644 src/osmo-bts-trx/amr.c create mode 100644 src/osmo-bts-trx/amr.h diff --git a/src/osmo-bts-trx/amr.c b/src/osmo-bts-trx/amr.c new file mode 100644 index 000000000..70d94ca9a --- /dev/null +++ b/src/osmo-bts-trx/amr.c @@ -0,0 +1,81 @@ +/* AMR support for OsmoBTS-TRX */ + +/* (C) 2013 by Andreas Eversberg + * + * All Rights Reserved + * + * 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 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 . + * + */ + +#include +#include +#include +#include +#include + +static int amr_len_by_ft[16] = { + 12, 13, 15, 17, 19, 20, 26, 31, + 0, 0, 0, 0, 0, 0, 0, 0 +}; + +int amr_decompose_payload(uint8_t *payload, int payload_len, uint8_t *_cmr, + uint8_t *_ft, uint8_t *_bfi) +{ + uint8_t cmr, f, ft, q; + + if (payload_len < 2) + return -EINVAL; + + cmr = payload[0] >> 4; + if (_cmr) + *_cmr = cmr; + + f = payload[1] >> 7; + + ft = (payload[1] >> 3) & 0xf; + if (_ft) + *_ft = ft; + + q = (payload[1] >> 2) & 0x1; + if (_bfi) + *_bfi = !q; + + if (f) { + fprintf(stderr, "%s: multiple payloads not supported\n", + __func__); + return -ENOTSUP; + } + + if (payload_len - 2 < amr_len_by_ft[ft]) + return -EINVAL; + + return 2 + amr_len_by_ft[ft]; +} + +int amr_compose_payload(uint8_t *payload, uint8_t cmr, uint8_t ft, uint8_t bfi) +{ + if (cmr >= 16) + return -EINVAL; + + if (ft >= 16) + return -EINVAL; + + payload[0] = cmr << 4; + + payload[1] = (ft << 3) | ((!bfi) << 2); /* F = 0 */ + + return 2 + amr_len_by_ft[ft]; +} + diff --git a/src/osmo-bts-trx/amr.h b/src/osmo-bts-trx/amr.h new file mode 100644 index 000000000..17eb5f049 --- /dev/null +++ b/src/osmo-bts-trx/amr.h @@ -0,0 +1,8 @@ +#ifndef _TRX_AMR_H +#define _TRX_AMR_H + +int amr_decompose_payload(uint8_t *payload, int payload_len, uint8_t *_cmr, + uint8_t *_ft, uint8_t *_bfi); +int amr_compose_payload(uint8_t *payload, uint8_t cmr, uint8_t ft, uint8_t bfi); + +#endif /* _TRX_AMR_H */