blob: babe4206364c38e7d049dd3d7f0d733e5ea4fbe5 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004* SPDX-License-Identifier: GPL-3.0+
5*
dburgessb3a0ca42011-10-12 07:44:40 +00006* This software is distributed under the terms of the GNU Public License.
7* See the COPYING file in the main directory for details.
8*
9* This use of this software may be subject to additional restrictions.
10* See the LEGAL file in the main directory for details.
11
12 This program is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25*/
26
dburgessb3a0ca42011-10-12 07:44:40 +000027#include "radioInterface.h"
28#include "Interthread.h"
29#include "GSMCommon.h"
dburgessb3a0ca42011-10-12 07:44:40 +000030
31#include <sys/types.h>
32#include <sys/socket.h>
33
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010034extern "C" {
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020035#include <osmocom/core/signal.h>
Erica7143bf2020-04-08 01:39:17 +020036#include <osmocom/core/select.h>
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010037#include "config_defs.h"
38}
39
Thomas Tsouf0782732013-10-29 15:55:47 -040040class Transceiver;
41
Erica7143bf2020-04-08 01:39:17 +020042extern Transceiver *transceiver;
43
Thomas Tsouf0782732013-10-29 15:55:47 -040044/** Channel descriptor for transceiver object and channel number pair */
Pau Espin Pedrol720b9122019-07-22 18:10:52 +020045struct TrxChanThParams {
46 Transceiver *trx;
47 size_t num;
Thomas Tsouf0782732013-10-29 15:55:47 -040048};
49
50/** Internal transceiver state variables */
51struct TransceiverState {
52 TransceiverState();
53 ~TransceiverState();
54
55 /* Initialize a multiframe slot in the filler table */
Pau Espin Pedrolefac20b2018-02-21 14:59:19 +010056 bool init(FillerType filler, size_t sps, float scale, size_t rtsc, unsigned rach_delay);
Thomas Tsouf0782732013-10-29 15:55:47 -040057
58 int chanType[8];
59
60 /* Last timestamp of each timeslot's channel estimate */
61 GSM::Time chanEstimateTime[8];
62
63 /* The filler table */
64 signalVector *fillerTable[102][8];
65 int fillerModulus[8];
Pau Espin Pedrolc0d6fd22020-07-09 16:51:47 +020066 FillerType mFiller;
Thomas Tsou15d743e2014-01-25 02:34:03 -050067 bool mRetrans;
Thomas Tsouf0782732013-10-29 15:55:47 -040068
69 /* Most recent channel estimate of all timeslots */
70 signalVector *chanResponse[8];
71
72 /* Most recent DFE feedback filter of all timeslots */
73 signalVector *DFEForward[8];
74 signalVector *DFEFeedback[8];
75
76 /* Most recent SNR, timing, and channel amplitude estimates */
77 float SNRestimate[8];
78 float chanRespOffset[8];
79 complex chanRespAmplitude[8];
Thomas Tsoua0179e32013-11-14 15:52:04 -050080
81 /* Received noise energy levels */
82 float mNoiseLev;
Eric8984d7f2022-07-19 21:15:24 +020083 avgVector mNoises;
Tom Tsoua4d1a412014-11-25 15:46:56 -080084
85 /* Shadowed downlink attenuation */
86 int mPower;
Pau Espin Pedrol92ba59d2020-06-29 14:34:59 +020087
Pau Espin Pedrol7d8676a2020-08-26 14:30:46 +020088 /* RF emission and reception disabled, as per NM Administrative State Locked */
89 bool mMuted;
90
Pau Espin Pedrol92ba59d2020-06-29 14:34:59 +020091 /* counters */
92 struct trx_counters ctrs;
Pau Espin Pedrolc0d6fd22020-07-09 16:51:47 +020093
94 /* Used to keep track of lost and out of order frames */
95 bool first_dl_fn_rcv[8];
96 GSM::Time last_dl_time_rcv[8];
Thomas Tsouf0782732013-10-29 15:55:47 -040097};
98
dburgessb3a0ca42011-10-12 07:44:40 +000099/** The Transceiver class, responsible for physical layer of basestation */
100class Transceiver {
Tom Tsou64ad7122015-05-19 18:26:31 -0700101public:
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200102 /** Transceiver constructor
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200103 @param cfg VTY populated config
Tom Tsou64ad7122015-05-19 18:26:31 -0700104 @param wTransmitLatency initial setting of transmit latency
105 @param radioInterface associated radioInterface object
106 */
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200107 Transceiver(const struct trx_cfg *cfg,
Alexander Chemerise8905a02015-06-03 23:47:56 -0400108 GSM::Time wTransmitLatency,
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200109 RadioInterface *wRadioInterface);
Tom Tsou64ad7122015-05-19 18:26:31 -0700110
111 /** Destructor */
112 ~Transceiver();
113
114 /** Start the control loop */
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200115 bool init(void);
Tom Tsou64ad7122015-05-19 18:26:31 -0700116
117 /** attach the radioInterface receive FIFO */
118 bool receiveFIFO(VectorFIFO *wFIFO, size_t chan)
119 {
120 if (chan >= mReceiveFIFO.size())
121 return false;
122
123 mReceiveFIFO[chan] = wFIFO;
124 return true;
125 }
126
127 /** accessor for number of channels */
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200128 size_t numChans() const { return cfg->num_chans; };
Tom Tsou64ad7122015-05-19 18:26:31 -0700129
130 /** Codes for channel combinations */
131 typedef enum {
132 FILL, ///< Channel is transmitted, but unused
133 I, ///< TCH/FS
134 II, ///< TCH/HS, idle every other slot
135 III, ///< TCH/HS
136 IV, ///< FCCH+SCH+CCCH+BCCH, uplink RACH
137 V, ///< FCCH+SCH+CCCH+BCCH+SDCCH/4+SACCH/4, uplink RACH+SDCCH/4
138 VI, ///< CCCH+BCCH, uplink RACH
139 VII, ///< SDCCH/8 + SACCH/8
140 VIII, ///< TCH/F + FACCH/F + SACCH/M
141 IX, ///< TCH/F + SACCH/M
142 X, ///< TCH/FD + SACCH/MD
143 XI, ///< PBCCH+PCCCH+PDTCH+PACCH+PTCCH
144 XII, ///< PCCCH+PDTCH+PACCH+PTCCH
145 XIII, ///< PDTCH+PACCH+PTCCH
146 NONE, ///< Channel is inactive, default
147 LOOPBACK ///< similar go VII, used in loopback testing
148 } ChannelCombination;
149
dburgessb3a0ca42011-10-12 07:44:40 +0000150private:
Eric5561f112022-07-19 21:18:21 +0200151 size_t mChans;
Erica7143bf2020-04-08 01:39:17 +0200152struct ctrl_msg {
153 char data[101];
154 ctrl_msg() {};
155};
156
157struct ctrl_sock_state {
158 osmo_fd conn_bfd;
159 std::deque<ctrl_msg> txmsgqueue;
160 ctrl_sock_state() {
161 conn_bfd.fd = -1;
162 }
163 ~ctrl_sock_state() {
164 if(conn_bfd.fd >= 0) {
Pau Espin Pedrol8a436242023-03-14 11:34:16 +0100165 osmo_fd_unregister(&conn_bfd);
Erica7143bf2020-04-08 01:39:17 +0200166 close(conn_bfd.fd);
167 conn_bfd.fd = -1;
Erica7143bf2020-04-08 01:39:17 +0200168 }
169 }
170};
171
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200172 const struct trx_cfg *cfg; ///< VTY populated config
Pau Espin Pedrolb9d25152019-07-01 19:03:49 +0200173 std::vector<int> mDataSockets; ///< socket for writing to/reading from GSM core
Erica7143bf2020-04-08 01:39:17 +0200174 std::vector<ctrl_sock_state> mCtrlSockets; ///< socket for writing/reading control commands from GSM core
Pau Espin Pedrolb9d25152019-07-01 19:03:49 +0200175 int mClockSocket; ///< socket for writing clock updates to GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000176
Thomas Tsou204a9f12013-10-29 18:34:16 -0400177 std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core
178 std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +0000179
Thomas Tsou204a9f12013-10-29 18:34:16 -0400180 std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO
181 Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO
182 Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO
Thomas Tsou204a9f12013-10-29 18:34:16 -0400183 std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000184
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800185 GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock
186 GSM::Time mLatencyUpdateTime; ///< last time latency was updated
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200187 GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO
dburgessb3a0ca42011-10-12 07:44:40 +0000188 GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core
189
190 RadioInterface *mRadioInterface; ///< associated radioInterface object
191 double txFullScale; ///< full scale input to radio
192 double rxFullScale; ///< full scale output to radio
193
dburgessb3a0ca42011-10-12 07:44:40 +0000194 /** modulate and add a burst to the transmit queue */
Thomas Tsoua2fe91a2013-11-13 22:48:11 -0500195 void addRadioVector(size_t chan, BitVector &bits,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400196 int RSSI, GSM::Time &wTime);
dburgessb3a0ca42011-10-12 07:44:40 +0000197
Thomas Tsou15d743e2014-01-25 02:34:03 -0500198 /** Update filler table */
199 void updateFillerTable(size_t chan, radioVector *burst);
200
dburgessb3a0ca42011-10-12 07:44:40 +0000201 /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */
202 void pushRadioVector(GSM::Time &nowTime);
203
Thomas Tsou204a9f12013-10-29 18:34:16 -0400204 /** Pull and demodulate a burst from the receive FIFO */
Pau Espin Pedrol923b4bc2019-09-06 15:01:26 +0200205 int pullRadioVector(size_t chan, struct trx_ul_burst_ind *ind);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400206
dburgessb3a0ca42011-10-12 07:44:40 +0000207 /** Set modulus for specific timeslot */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400208 void setModulus(size_t timeslot, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000209
210 /** return the expected burst type for the specified timestamp */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400211 CorrType expectedCorrType(GSM::Time currTime, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000212
213 /** send messages over the clock socket */
Pau Espin Pedrol76ff96e2019-08-26 12:05:48 +0200214 bool writeClockInterface(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000215
Erica7143bf2020-04-08 01:39:17 +0200216 static int ctrl_sock_cb(struct osmo_fd *bfd, unsigned int flags);
217 int ctrl_sock_write(int chan);
218 void ctrl_sock_send(ctrl_msg& m, int chan);
219 /** drive handling of control messages from GSM core */
220 int ctrl_sock_handle_rx(int chan);
221
Eric5561f112022-07-19 21:18:21 +0200222
Alexander Chemeris37229202015-06-20 01:37:54 +0300223 bool mOn; ///< flag to indicate that transceiver is powered on
Pau Espin Pedrol934da482017-07-04 16:25:20 +0200224 bool mForceClockInterface; ///< flag to indicate whether IND CLOCK shall be sent unconditionally after transceiver is started
Alexander Chemeris5a068062015-06-20 01:38:47 +0300225 bool mHandover[8][8]; ///< expect handover to the timeslot/subslot
dburgessb3a0ca42011-10-12 07:44:40 +0000226 double mTxFreq; ///< the transmit frequency
227 double mRxFreq; ///< the receive frequency
dburgessb3a0ca42011-10-12 07:44:40 +0000228 unsigned mTSC; ///< the midamble sequence code
Alexander Chemeris78d1fc92016-03-19 21:16:22 +0300229 unsigned mMaxExpectedDelayAB; ///< maximum expected time-of-arrival offset in GSM symbols for Access Bursts (RACH)
230 unsigned mMaxExpectedDelayNB; ///< maximum expected time-of-arrival offset in GSM symbols for Normal Bursts
Alexander Chemerise692ce92015-06-12 00:15:31 -0400231 unsigned mWriteBurstToDiskMask; ///< debug: bitmask to indicate which timeslots to dump to disk
dburgessb3a0ca42011-10-12 07:44:40 +0000232
Pau Espin Pedrolc3325b92019-07-22 17:47:02 +0200233 std::vector<unsigned> mVersionTRXD; ///< Format version to use for TRXD protocol communication, per channel
Thomas Tsou204a9f12013-10-29 18:34:16 -0400234 std::vector<TransceiverState> mStates;
dburgessb3a0ca42011-10-12 07:44:40 +0000235
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800236 /** Start and stop I/O threads through the control socket API */
237 bool start();
238 void stop();
239
Martin Hauke066fd042019-10-13 19:08:00 +0200240 /** Protect destructor accessible stop call */
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800241 Mutex mLock;
242
dburgessb3a0ca42011-10-12 07:44:40 +0000243protected:
Thomas Tsou204a9f12013-10-29 18:34:16 -0400244 /** drive lower receive I/O and burst generation */
Pau Espin Pedrol76ff96e2019-08-26 12:05:48 +0200245 bool driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000246
Thomas Tsou204a9f12013-10-29 18:34:16 -0400247 /** drive demodulation of GSM bursts */
Pau Espin Pedrol76ff96e2019-08-26 12:05:48 +0200248 bool driveReceiveFIFO(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000249
250 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400251 void driveTxFIFO();
dburgessb3a0ca42011-10-12 07:44:40 +0000252
dburgessb3a0ca42011-10-12 07:44:40 +0000253 /**
254 drive modulation and sorting of GSM bursts from GSM core
255 @return true if a burst was transferred successfully
256 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400257 bool driveTxPriorityQueue(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000258
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200259 friend void *RxUpperLoopAdapter(TrxChanThParams *params);
260 friend void *TxUpperLoopAdapter(TrxChanThParams *params);
261 friend void *RxLowerLoopAdapter(Transceiver *transceiver);
262 friend void *TxLowerLoopAdapter(Transceiver *transceiver);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400263
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200264 double rssiOffset(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000265 void reset();
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000266
Pau Espin Pedrol607a4142019-07-01 13:56:17 +0200267 void logRxBurst(size_t chan, const struct trx_ul_burst_ind *bi);
dburgessb3a0ca42011-10-12 07:44:40 +0000268};
269
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200270void *RxUpperLoopAdapter(TrxChanThParams *params);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400271
Thomas Tsou92c16df2013-09-28 18:04:19 -0400272/** Main drive threads */
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200273void *RxLowerLoopAdapter(Transceiver *transceiver);
274void *TxLowerLoopAdapter(Transceiver *transceiver);
dburgessb3a0ca42011-10-12 07:44:40 +0000275
dburgessb3a0ca42011-10-12 07:44:40 +0000276/** transmit queueing thread loop */
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200277void *TxUpperLoopAdapter(TrxChanThParams *params);