Pau Espin Pedrol | 15fa64b | 2019-07-01 20:41:55 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 sysmocom - s.f.m.c. GmbH |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: AGPL-3.0+ |
| 6 | * |
| 7 | * Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation, either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * See the COPYING file in the main directory for details. |
| 22 | */ |
| 23 | |
| 24 | #include "proto_trxd.h" |
| 25 | |
| 26 | #include <osmocom/core/bits.h> |
| 27 | |
| 28 | static void trxd_fill_common(struct trxd_hdr_common *common, const struct trx_ul_burst_ind *bi, uint8_t version) |
| 29 | { |
| 30 | common->version = version && 0x07; |
| 31 | common->reserved = 0; |
| 32 | common->tn = bi->tn; |
| 33 | osmo_store32be(bi->fn, &common->fn); |
| 34 | } |
| 35 | |
| 36 | static void trxd_fill_v0_specific(struct trxd_hdr_v0_specific *v0, const struct trx_ul_burst_ind *bi) |
| 37 | { |
| 38 | int toa_int; |
| 39 | |
| 40 | /* in 1/256 symbols, round to closest integer */ |
| 41 | toa_int = (int) (bi->toa * 256.0 + 0.5); |
| 42 | v0->rssi = bi->rssi; |
| 43 | osmo_store16be(toa_int, &v0->toa); |
| 44 | } |
| 45 | |
| 46 | static void trxd_fill_burst_normalized255(uint8_t* soft_bits, const struct trx_ul_burst_ind *bi) |
| 47 | { |
| 48 | unsigned i; |
| 49 | for (i = 0; i < bi->nbits; i++) |
| 50 | soft_bits[i] = (char) round(bi->rx_burst[i] * 255.0); |
| 51 | } |
| 52 | |
| 53 | bool trxd_send_burst_ind_v0(size_t chan, int fd, const struct trx_ul_burst_ind *bi) { |
| 54 | int rc; |
| 55 | |
| 56 | /* v0 doesn't support idle frames, they are simply dropped, not sent */ |
| 57 | if(bi->idle) |
| 58 | return true; |
| 59 | |
| 60 | /* +2: Historically (OpenBTS times), two extra non-used bytes are sent appeneded to each burst */ |
| 61 | char buf[sizeof(struct trxd_hdr_v0) + bi->nbits + 2]; |
| 62 | struct trxd_hdr_v0* pkt = (struct trxd_hdr_v0*)buf; |
| 63 | |
| 64 | trxd_fill_common(&pkt->common, bi, 0); |
| 65 | trxd_fill_v0_specific(&pkt->v0, bi); |
| 66 | trxd_fill_burst_normalized255(&pkt->soft_bits[0], bi); |
| 67 | |
| 68 | /* +1: Historical reason. There's an uninitizalied byte in there: pkt->soft_bits[bi->nbits] */ |
| 69 | pkt->soft_bits[bi->nbits + 1] = '\0'; |
| 70 | |
| 71 | rc = write(fd, buf, sizeof(struct trxd_hdr_v0) + bi->nbits + 2); |
| 72 | if (rc <= 0) { |
| 73 | CLOGCHAN(chan, DMAIN, LOGL_NOTICE, "mDataSockets write(%d) failed: %d\n", fd, rc); |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |