blob: 45a415956168a421c8b11b64456282a1c7c2cdc5 [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;
83 noiseVector 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:
Erica7143bf2020-04-08 01:39:17 +0200151struct ctrl_msg {
152 char data[101];
153 ctrl_msg() {};
154};
155
156struct ctrl_sock_state {
157 osmo_fd conn_bfd;
158 std::deque<ctrl_msg> txmsgqueue;
159 ctrl_sock_state() {
160 conn_bfd.fd = -1;
161 }
162 ~ctrl_sock_state() {
163 if(conn_bfd.fd >= 0) {
164 close(conn_bfd.fd);
165 conn_bfd.fd = -1;
166 osmo_fd_unregister(&conn_bfd);
167 }
168 }
169};
170
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200171 const struct trx_cfg *cfg; ///< VTY populated config
Pau Espin Pedrolb9d25152019-07-01 19:03:49 +0200172 std::vector<int> mDataSockets; ///< socket for writing to/reading from GSM core
Erica7143bf2020-04-08 01:39:17 +0200173 std::vector<ctrl_sock_state> mCtrlSockets; ///< socket for writing/reading control commands from GSM core
Pau Espin Pedrolb9d25152019-07-01 19:03:49 +0200174 int mClockSocket; ///< socket for writing clock updates to GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000175
Thomas Tsou204a9f12013-10-29 18:34:16 -0400176 std::vector<VectorQueue> mTxPriorityQueues; ///< priority queue of transmit bursts received from GSM core
177 std::vector<VectorFIFO *> mReceiveFIFO; ///< radioInterface FIFO of receive bursts
dburgessb3a0ca42011-10-12 07:44:40 +0000178
Thomas Tsou204a9f12013-10-29 18:34:16 -0400179 std::vector<Thread *> mRxServiceLoopThreads; ///< thread to pull bursts into receive FIFO
180 Thread *mRxLowerLoopThread; ///< thread to pull bursts into receive FIFO
181 Thread *mTxLowerLoopThread; ///< thread to push bursts into transmit FIFO
Thomas Tsou204a9f12013-10-29 18:34:16 -0400182 std::vector<Thread *> mTxPriorityQueueServiceLoopThreads; ///< thread to process transmit bursts from GSM core
dburgessb3a0ca42011-10-12 07:44:40 +0000183
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800184 GSM::Time mTransmitLatency; ///< latency between basestation clock and transmit deadline clock
185 GSM::Time mLatencyUpdateTime; ///< last time latency was updated
Pau Espin Pedrol7c405a02017-07-04 16:24:06 +0200186 GSM::Time mTransmitDeadlineClock; ///< deadline for pushing bursts into transmit FIFO
dburgessb3a0ca42011-10-12 07:44:40 +0000187 GSM::Time mLastClockUpdateTime; ///< last time clock update was sent up to core
188
189 RadioInterface *mRadioInterface; ///< associated radioInterface object
190 double txFullScale; ///< full scale input to radio
191 double rxFullScale; ///< full scale output to radio
192
dburgessb3a0ca42011-10-12 07:44:40 +0000193 /** modulate and add a burst to the transmit queue */
Thomas Tsoua2fe91a2013-11-13 22:48:11 -0500194 void addRadioVector(size_t chan, BitVector &bits,
Thomas Tsou204a9f12013-10-29 18:34:16 -0400195 int RSSI, GSM::Time &wTime);
dburgessb3a0ca42011-10-12 07:44:40 +0000196
Thomas Tsou15d743e2014-01-25 02:34:03 -0500197 /** Update filler table */
198 void updateFillerTable(size_t chan, radioVector *burst);
199
dburgessb3a0ca42011-10-12 07:44:40 +0000200 /** Push modulated burst into transmit FIFO corresponding to a particular timestamp */
201 void pushRadioVector(GSM::Time &nowTime);
202
Thomas Tsou204a9f12013-10-29 18:34:16 -0400203 /** Pull and demodulate a burst from the receive FIFO */
Pau Espin Pedrol923b4bc2019-09-06 15:01:26 +0200204 int pullRadioVector(size_t chan, struct trx_ul_burst_ind *ind);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400205
dburgessb3a0ca42011-10-12 07:44:40 +0000206 /** Set modulus for specific timeslot */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400207 void setModulus(size_t timeslot, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000208
209 /** return the expected burst type for the specified timestamp */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400210 CorrType expectedCorrType(GSM::Time currTime, size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000211
212 /** send messages over the clock socket */
Pau Espin Pedrol76ff96e2019-08-26 12:05:48 +0200213 bool writeClockInterface(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000214
Erica7143bf2020-04-08 01:39:17 +0200215 static int ctrl_sock_cb(struct osmo_fd *bfd, unsigned int flags);
216 int ctrl_sock_write(int chan);
217 void ctrl_sock_send(ctrl_msg& m, int chan);
218 /** drive handling of control messages from GSM core */
219 int ctrl_sock_handle_rx(int chan);
220
Thomas Tsou204a9f12013-10-29 18:34:16 -0400221 size_t mChans;
Alexander Chemeris37229202015-06-20 01:37:54 +0300222 bool mOn; ///< flag to indicate that transceiver is powered on
Pau Espin Pedrol934da482017-07-04 16:25:20 +0200223 bool mForceClockInterface; ///< flag to indicate whether IND CLOCK shall be sent unconditionally after transceiver is started
Alexander Chemeris5a068062015-06-20 01:38:47 +0300224 bool mHandover[8][8]; ///< expect handover to the timeslot/subslot
dburgessb3a0ca42011-10-12 07:44:40 +0000225 double mTxFreq; ///< the transmit frequency
226 double mRxFreq; ///< the receive frequency
dburgessb3a0ca42011-10-12 07:44:40 +0000227 unsigned mTSC; ///< the midamble sequence code
Alexander Chemeris78d1fc92016-03-19 21:16:22 +0300228 unsigned mMaxExpectedDelayAB; ///< maximum expected time-of-arrival offset in GSM symbols for Access Bursts (RACH)
229 unsigned mMaxExpectedDelayNB; ///< maximum expected time-of-arrival offset in GSM symbols for Normal Bursts
Alexander Chemerise692ce92015-06-12 00:15:31 -0400230 unsigned mWriteBurstToDiskMask; ///< debug: bitmask to indicate which timeslots to dump to disk
dburgessb3a0ca42011-10-12 07:44:40 +0000231
Pau Espin Pedrolc3325b92019-07-22 17:47:02 +0200232 std::vector<unsigned> mVersionTRXD; ///< Format version to use for TRXD protocol communication, per channel
Thomas Tsou204a9f12013-10-29 18:34:16 -0400233 std::vector<TransceiverState> mStates;
dburgessb3a0ca42011-10-12 07:44:40 +0000234
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800235 /** Start and stop I/O threads through the control socket API */
236 bool start();
237 void stop();
238
Martin Hauke066fd042019-10-13 19:08:00 +0200239 /** Protect destructor accessible stop call */
Tom Tsoueb54bdd2014-11-25 16:06:32 -0800240 Mutex mLock;
241
dburgessb3a0ca42011-10-12 07:44:40 +0000242protected:
Thomas Tsou204a9f12013-10-29 18:34:16 -0400243 /** drive lower receive I/O and burst generation */
Pau Espin Pedrol76ff96e2019-08-26 12:05:48 +0200244 bool driveReceiveRadio();
dburgessb3a0ca42011-10-12 07:44:40 +0000245
Thomas Tsou204a9f12013-10-29 18:34:16 -0400246 /** drive demodulation of GSM bursts */
Pau Espin Pedrol76ff96e2019-08-26 12:05:48 +0200247 bool driveReceiveFIFO(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000248
249 /** drive transmission of GSM bursts */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400250 void driveTxFIFO();
dburgessb3a0ca42011-10-12 07:44:40 +0000251
dburgessb3a0ca42011-10-12 07:44:40 +0000252 /**
253 drive modulation and sorting of GSM bursts from GSM core
254 @return true if a burst was transferred successfully
255 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400256 bool driveTxPriorityQueue(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000257
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200258 friend void *RxUpperLoopAdapter(TrxChanThParams *params);
259 friend void *TxUpperLoopAdapter(TrxChanThParams *params);
260 friend void *RxLowerLoopAdapter(Transceiver *transceiver);
261 friend void *TxLowerLoopAdapter(Transceiver *transceiver);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400262
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200263 double rssiOffset(size_t chan);
dburgessb3a0ca42011-10-12 07:44:40 +0000264 void reset();
kurtis.heimerl6b495a52011-11-26 03:17:21 +0000265
Pau Espin Pedrol607a4142019-07-01 13:56:17 +0200266 void logRxBurst(size_t chan, const struct trx_ul_burst_ind *bi);
dburgessb3a0ca42011-10-12 07:44:40 +0000267};
268
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200269void *RxUpperLoopAdapter(TrxChanThParams *params);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400270
Thomas Tsou92c16df2013-09-28 18:04:19 -0400271/** Main drive threads */
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200272void *RxLowerLoopAdapter(Transceiver *transceiver);
273void *TxLowerLoopAdapter(Transceiver *transceiver);
dburgessb3a0ca42011-10-12 07:44:40 +0000274
dburgessb3a0ca42011-10-12 07:44:40 +0000275/** transmit queueing thread loop */
Pau Espin Pedrol720b9122019-07-22 18:10:52 +0200276void *TxUpperLoopAdapter(TrxChanThParams *params);