rename sdr to radio
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
# General
|
||||
This is an RF simulator that allows to test OAI without an RF board. It replaces an actual RF board driver.
|
||||
|
||||
As much as possible, it works like an RF board, but not in real-time: It can run faster than real-time if there is enough CPU, or slower (it is CPU-bound instead of real-time RF sampling-bound).
|
||||
|
||||
It can be run either in:
|
||||
|
||||
- "noS1" mode: the generated IP traffic is sent and received between gNB and UE IP tunnel interfaces ("oaitun") by applications like ping and iperf
|
||||
- "phy-test" mode: random UL and DL traffic is generated at every scheduling opportunity
|
||||
|
||||
|
||||
# build
|
||||
|
||||
## From [build_oai](../../../doc/BUILD.md) script
|
||||
The RF simulator is implemented as an OAI device and always built when you build the OAI eNB or the OAI UE.
|
||||
|
||||
Using the `-w SIMU` option it is possible to just re-build the RF simulator device.
|
||||
|
||||
Example:
|
||||
```bash
|
||||
./build_oai --UE --eNB
|
||||
Will compile UE
|
||||
Will compile eNB
|
||||
CMAKE_CMD=cmake ..
|
||||
No local radio head and no transport protocol selected
|
||||
No radio head has been selected (HW set to None)
|
||||
No transport protocol has been selected (TP set to None)
|
||||
RF HW set to None
|
||||
Flags for Deadline scheduler: False
|
||||
......................
|
||||
......................
|
||||
Compiling rfsimulator
|
||||
Log file for compilation has been written to: /usr/local/oai/rfsimu_config/openairinterface5g/cmake_targets/log/rfsimulator.txt
|
||||
rfsimulator compiled
|
||||
......................
|
||||
......................
|
||||
```
|
||||
|
||||
## Add the rfsimulator after initial build
|
||||
After any regular build you can compile the device from the build directory:
|
||||
```bash
|
||||
cd <path to oai sources>/openairinterface5g/cmake_targets/ran_build/build
|
||||
make rfsimulator
|
||||
```
|
||||
This is equivalent to using `-w SIMU` when running the `build_oai` script.
|
||||
|
||||
# Usage
|
||||
To use the RF simulator add the `--rfsim` option to the command line. By default the RF simulator device will try to connect to host 127.0.0.1, port 4043, which is usually the behavior for the UE.
|
||||
|
||||
The RF simulator is using the configuration module, and its parameters are defined in a specific section called "rfsimulator".
|
||||
|
||||
| parameter | usage | default |
|
||||
|:---------------------|:------------------------------------------------------------------------------------------------------------------|----:|
|
||||
| serveraddr | ip address to connect to, or "enb" to behave as a tcp server | 127.0.0.1 |
|
||||
| serverport | port number to connect to or to listen on (eNB, which behaves as a tcp server) | 4043 |
|
||||
| options | list of comma separated run-time options, two are supported: `chanmod` to enable channel modeling and `saviq` to write transmitted iqs to a file | all options disabled |
|
||||
| modelname | Name of the channel model to apply on received iqs when the `chanmod` option is enabled | AWGN |
|
||||
| IQfile | Path to the file to be used to store iqs, when the `saviq` option is enabled | /tmp/rfsimulator.iqs |
|
||||
|
||||
Setting the env variable RFSIMULATOR can be used instead of using the serveraddr parameter; it is to preserve compatibility with previous version.
|
||||
|
||||
## How to use the RF simulator options
|
||||
|
||||
To define and use a channel model, the configuration file needs to include a channel configuration file. To do this, add `@include "channelmod_rfsimu.conf"` to the end of the configuration file, and place the channel configuration file in the same directory. An example channel configuration file `channelmod_rfsimu.conf` is in `ci-scripts/conf_files`.
|
||||
|
||||
Add the following options to the command line to enable the channel model and the IQ samples saving for future replay:
|
||||
```bash
|
||||
--rfsimulator.options chanmod,saviq
|
||||
```
|
||||
or just:
|
||||
```bash
|
||||
--rfsimulator.options chanmod
|
||||
```
|
||||
to enable the channel model.
|
||||
|
||||
set the model with:
|
||||
```bash
|
||||
--rfsimulator.modelname AWGN
|
||||
```
|
||||
|
||||
Example run:
|
||||
|
||||
```bash
|
||||
sudo RFSIMULATOR=server ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-LTE-EPC/CONF/gnb.band78.tm1.106PRB.usrpn300.conf --parallel-config PARALLEL_SINGLE_THREAD --rfsim --phy-test --rfsimulator.options chanmod --rfsimulator.modelname AWGN
|
||||
```
|
||||
|
||||
where `@include "channelmod_rfsimu.conf"` has been added at the end of the file, and `ci-scripts/conf_files/channelmod_rfsimu.conf` copied to `targets/PROJECTS/GENERIC-LTE-EPC/CONF/`.
|
||||
|
||||
## 4G case
|
||||
|
||||
For the UE, it should be set to the IP address of the eNB. For example:
|
||||
```bash
|
||||
sudo RFSIMULATOR=192.168.2.200 ./lte-uesoftmodem -C 2685000000 -r 50
|
||||
```
|
||||
For the eNB, use a valid configuration file setup for the USRP board tests and start the softmodem as usual, **but**, adding the `--rfsim` option.
|
||||
```bash
|
||||
sudo RFSIMULATOR=enb ./lte-softmodem -O <config file> --rfsim
|
||||
```
|
||||
|
||||
Except this, the UE and the eNB can be used as if the RF is real. noS1 mode can also be used with the RF simulator.
|
||||
|
||||
If you reach 'RA not active' on UE, be careful to generate a valid SIM.
|
||||
```bash
|
||||
$OPENAIR_DIR/cmake_targets/ran_build/build/conf2uedata -c $OPENAIR_DIR/openair3/NAS/TOOLS/ue_eurecom_test_sfr.conf -o .
|
||||
```
|
||||
|
||||
## 5G case
|
||||
|
||||
If `build_oai` has not been run with `-w SIMU`, you need to build the `rfsimulator` manually. To do so:
|
||||
```bash
|
||||
cd ran_build/build
|
||||
make rfsimulator
|
||||
```
|
||||
|
||||
### Launch gNB in one window
|
||||
|
||||
```bash
|
||||
sudo RFSIMULATOR=server ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-LTE-EPC/CONF/gnb.band78.tm1.106PRB.usrpn300.conf --parallel-config PARALLEL_SINGLE_THREAD --rfsim --phy-test --nokrnmod 1
|
||||
```
|
||||
|
||||
### Launch UE in another window
|
||||
|
||||
```bash
|
||||
sudo RFSIMULATOR=<TARGET_GNB_INTERFACE_ADDRESS> ./nr-uesoftmodem --rfsim --phy-test --rrc_config_path .
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
1. This starts the gNB and UE in the `phy-test` UP-only mode where the gNB is started as if a UE had already connected, and a configurable scheduler is used instead of the default one. The options `-m`, `-l`, `-t`, `-M`, `-T`, `-D`, and `-U` can be used to configure this scheduler.
|
||||
2. <TARGET_GNB_INTERFACE_ADDRESS> can be 127.0.0.1 if both gNB and nrUE executables run on the same host, OR the IP interface address of the remote host running the gNB executable, if the gNB and nrUE run on separate hosts.
|
||||
3. The --rrc_config_path parameter SHALL specify where the 2 RAW files are located (`rbconfig.raw` and `reconfig.raw`).
|
||||
- If you are running on the same machine and launched the 2 executables (`nr-softmodem` and `nr-uesoftmodem`) from the same directory, nothing has to be done.
|
||||
- If you launched the 2 executables from 2 different folders, just point to the location where you launched the `nr-softmodem`:
|
||||
* `sudo RFSIMULATOR=<TARGET_GNB_INTERFACE_ADDRESS> ./nr-uesoftmodem --rfsim --phy-test --rrc_config_path /the/path/where/you/launched/nr-softmodem`
|
||||
- If you are not running on the same machine or launched the 2 executables from 2 different folders, you need to **COPY** the 2 raw files
|
||||
* `scp usera@machineA:/the/path/where/you/launched/nr-softmodem/r*config.raw userb@machineB:/the/path/where/you/will/launch/nr-uesoftmodem/`
|
||||
* Obviously this operation SHALL be done before launching the `nr-uesoftmodem` executable.
|
||||
4. To enable the noS1 mode, `--noS1` option should be added to the command line.
|
||||
5. To operate the gNB/UE with a 5GC, start them using the `--sa` option. More information can be found [here](../../../doc/TESTING_5GSA_setup.md#2-sa-setup-with-oai-nr-ue-softmodem).
|
||||
|
||||
|
||||
In the UE, you can add `-d` option to get the softscope.
|
||||
|
||||
### Testing IP traffic (ping and iperf)
|
||||
|
||||
```
|
||||
ping -I oaitun_enb1 10.0.1.2 (from gNB mchine)
|
||||
ping -I oaitun_ue1 10.0.1.1 (from nrUE mchine)
|
||||
```
|
||||
|
||||
```iperf (Downlink):
|
||||
Server nrUE machine: iperf -s -i 1 -u -B 10.0.1.2
|
||||
Client gNB machine: iperf -c 10.0.1.2 -u -b 0.1M --bind 10.0.1.1
|
||||
```
|
||||
|
||||
```iperf (Uplink):
|
||||
Server gNB machine: iperf -s -i 1 -u -B 10.0.1.1
|
||||
Client nrUE machine: iperf -c 10.0.1.1 -u -b 0.1M --bind 10.0.1.2
|
||||
Note: iperf tests can be performed only when running gNB and nrUE on separate hosts.
|
||||
```
|
||||
|
||||
### Store and replay
|
||||
|
||||
You can store emitted I/Q samples. If you set the option `saviq`, the simulator will write all the I/Q samples into this file. Then, you can replay with the executable `replay_node`.
|
||||
|
||||
First compile it like other binaries:
|
||||
```bash
|
||||
make replay_node
|
||||
```
|
||||
You can use this binary as I/Q data source to feed whatever UE or gNB with recorded I/Q samples.
|
||||
|
||||
The file format is successive blocks of a header followed by the I/Q array. If you have existing stored I/Q, you can adapt the tool `replay_node` to convert your format to the rfsimulator format.
|
||||
|
||||
The format intends to be compatible with the OAI store/replay feature on USRP.
|
||||
|
||||
### Channel simulation
|
||||
|
||||
When the `chanmod` option is enabled, the RF channel simulator is called.
|
||||
|
||||
In the current version all channel parameters are set depending on the model name via a call to:
|
||||
```bash
|
||||
new_channel_desc_scm(bridge->tx_num_channels,
|
||||
bridge->rx_num_channels,
|
||||
<model name>,
|
||||
bridge->sample_rate,
|
||||
bridge->tx_bw,
|
||||
0.0, // forgetting_factor
|
||||
0, // maybe used for TA
|
||||
0); // path_loss in dB
|
||||
```
|
||||
Only the input noise can be changed on command line with the `-s` parameter.
|
||||
|
||||
With path loss = 0 set `-s 5` to see a little noise. `-s` is a shortcut to `channelmod.s`. It is expected to enhance the channel modelization flexibility by the addition of more parameters in the channelmod section.
|
||||
|
||||
Example to add a very small noise:
|
||||
```bash
|
||||
-s 30
|
||||
```
|
||||
to add a lot of noise:
|
||||
```bash
|
||||
-s 5
|
||||
```
|
||||
|
||||
Example run commands:
|
||||
```bash
|
||||
sudo RFSIMULATOR=server ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-LTE-EPC/CONF/gnb.band78.tm1.106PRB.usrpn300.conf --parallel-config PARALLEL_SINGLE_THREAD --rfsim --phy-test --rfsimulator.options chanmod --rfsimulator.modelname AWGN
|
||||
|
||||
```
|
||||
# Real time control and monitoring
|
||||
|
||||
Add the `--telnetsrv` option to the command line. Then in a new shell, connect to the telnet server, example:
|
||||
```bash
|
||||
telnet 127.0.0.1 9090
|
||||
```
|
||||
once connected it is possible to monitor the current status:
|
||||
```bash
|
||||
channelmod show current
|
||||
```
|
||||
|
||||
see the available channel models:
|
||||
```bash
|
||||
channelmod show predef
|
||||
```
|
||||
|
||||
or modify the channel model, for example setting a new model:
|
||||
```bash
|
||||
rfsimu setmodel AWGN
|
||||
```
|
||||
setting the pathloss, etc...:
|
||||
```bash
|
||||
channelmod modify <channelid> <param> <value>
|
||||
channelmod modify 0 ploss 15
|
||||
```
|
||||
where:
|
||||
```bash
|
||||
<param name> can be one of "riceanf", "aoa", "randaoa", "ploss", "offset", "forgetf"
|
||||
```
|
||||
|
||||
# Caveats
|
||||
Still issues in power control: txgain, rxgain are not used.
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The OpenAirInterface Software Alliance licenses this file to You under
|
||||
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.openairinterface.org/?page_id=698
|
||||
*
|
||||
* Author and copyright: Laurent Thomas, open-cells.com
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*-------------------------------------------------------------------------------
|
||||
* For more information about the OpenAirInterface (OAI) Software Alliance:
|
||||
* contact@openairinterface.org
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
#include <common/utils/assertions.h>
|
||||
#include <common/utils/LOG/log.h>
|
||||
#include <common/config/config_userapi.h>
|
||||
#include <openair1/SIMULATION/TOOLS/sim.h>
|
||||
#include <common/utils/telnetsrv/telnetsrv.h>
|
||||
#include <common/utils/load_module_shlib.h>
|
||||
#include "rfsimulator.h"
|
||||
|
||||
/*
|
||||
Legacy study:
|
||||
The parameters are:
|
||||
gain&loss (decay, signal power, ...)
|
||||
either a fixed gain in dB, a target power in dBm or ACG (automatic control gain) to a target average
|
||||
=> don't redo the AGC, as it was used in UE case, that must have a AGC inside the UE
|
||||
will be better to handle the "set_gain()" called by UE to apply it's gain (enable test of UE power loop)
|
||||
lin_amp = pow(10.0,.05*txpwr_dBm)/sqrt(nb_tx_antennas);
|
||||
a lot of operations in legacy, grouped in one simulation signal decay: txgain*decay*rxgain
|
||||
|
||||
multi_path (auto convolution, ISI, ...)
|
||||
either we regenerate the channel (call again random_channel(desc,0)), or we keep it over subframes
|
||||
legacy: we regenerate each sub frame in UL, and each frame only in DL
|
||||
*/
|
||||
void rxAddInput( const c16_t *input_sig,
|
||||
c16_t *after_channel_sig,
|
||||
int rxAnt,
|
||||
channel_desc_t *channelDesc,
|
||||
int nbSamples,
|
||||
uint64_t TS,
|
||||
uint32_t CirSize
|
||||
) {
|
||||
// channelDesc->path_loss_dB should contain the total path gain
|
||||
// so, in actual RF: tx gain + path loss + rx gain (+antenna gain, ...)
|
||||
// UE and NB gain control to be added
|
||||
// Fixme: not sure when it is "volts" so dB is 20*log10(...) or "power", so dB is 10*log10(...)
|
||||
const double pathLossLinear = pow(10,channelDesc->path_loss_dB/20.0);
|
||||
// Energy in one sample to calibrate input noise
|
||||
// the normalized OAI value seems to be 256 as average amplitude (numerical amplification = 1)
|
||||
const double noise_per_sample = pow(10,channelDesc->noise_power_dB/10.0) * 256;
|
||||
const int dd = abs(channelDesc->channel_offset);
|
||||
const int nbTx=channelDesc->nb_tx;
|
||||
|
||||
for (int i=0; i<nbSamples; i++) {
|
||||
struct complex16 *out_ptr=after_channel_sig+i;
|
||||
struct complexd rx_tmp= {0};
|
||||
|
||||
for (int txAnt=0; txAnt < nbTx; txAnt++) {
|
||||
const struct complexd *channelModel= channelDesc->ch[rxAnt+(txAnt*channelDesc->nb_rx)];
|
||||
|
||||
//const struct complex *channelModelEnd=channelModel+channelDesc->channel_length;
|
||||
for (int l = 0; l<(int)channelDesc->channel_length; l++) {
|
||||
// let's assume TS+i >= l
|
||||
// fixme: the rfsimulator current structure is interleaved antennas
|
||||
// this has been designed to not have to wait a full block transmission
|
||||
// but it is not very usefull
|
||||
// it would be better to split out each antenna in a separate flow
|
||||
// that will allow to mix ru antennas freely
|
||||
// (X + cirSize) % cirSize to ensure that index is positive
|
||||
const int idx = ((TS + i - l - dd) * nbTx + txAnt + CirSize) % CirSize;
|
||||
const struct complex16 tx16 = input_sig[idx];
|
||||
rx_tmp.r += tx16.r * channelModel[l].r - tx16.i * channelModel[l].i;
|
||||
rx_tmp.i += tx16.i * channelModel[l].r + tx16.r * channelModel[l].i;
|
||||
} //l
|
||||
}
|
||||
|
||||
// Fixme: lround(), rount(), ... is detected by valgrind as error, not found why
|
||||
out_ptr->r += lround(rx_tmp.r*pathLossLinear + noise_per_sample*gaussZiggurat(0.0,1.0));
|
||||
out_ptr->i += lround(rx_tmp.i*pathLossLinear + noise_per_sample*gaussZiggurat(0.0,1.0));
|
||||
out_ptr++;
|
||||
}
|
||||
|
||||
if ( (TS*nbTx)%CirSize+nbSamples <= CirSize )
|
||||
// Cast to a wrong type for compatibility !
|
||||
LOG_D(HW,"Input power %f, output power: %f, channel path loss %f, noise coeff: %f \n",
|
||||
10*log10((double)signal_energy((int32_t *)&input_sig[(TS*nbTx)%CirSize], nbSamples)),
|
||||
10*log10((double)signal_energy((int32_t *)after_channel_sig, nbSamples)),
|
||||
channelDesc->path_loss_dB,
|
||||
10*log10(noise_per_sample));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The OpenAirInterface Software Alliance licenses this file to You under
|
||||
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.openairinterface.org/?page_id=698
|
||||
*
|
||||
* Author and copyright: Laurent Thomas, open-cells.com
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*-------------------------------------------------------------------------------
|
||||
* For more information about the OpenAirInterface (OAI) Software Alliance:
|
||||
* contact@openairinterface.org
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
#include <common/utils/assertions.h>
|
||||
#include <common/utils/LOG/log.h>
|
||||
#include <common/config/config_userapi.h>
|
||||
#include <openair1/SIMULATION/TOOLS/sim.h>
|
||||
#include "rfsimulator.h"
|
||||
|
||||
// Ziggurat
|
||||
static double wn[128],fn[128];
|
||||
static uint32_t iz,jz,jsr=123456789,kn[128];
|
||||
static int32_t hz;
|
||||
#define SHR3 (jz=jsr, jsr^=(jsr<<13),jsr^=(jsr>>17),jsr^=(jsr<<5),jz+jsr)
|
||||
#define UNI (0.5+(signed) SHR3 * 0.2328306e-9)
|
||||
|
||||
double nfix(void) {
|
||||
const double r = 3.442620;
|
||||
static double x, y;
|
||||
|
||||
for (;;) {
|
||||
x=hz * wn[iz];
|
||||
|
||||
if (iz==0) {
|
||||
do {
|
||||
x = - 0.2904764 * log (UNI);
|
||||
y = - log (UNI);
|
||||
} while (y+y < x*x);
|
||||
|
||||
return (hz>0)? r+x : -r-x;
|
||||
}
|
||||
|
||||
if (fn[iz]+UNI*(fn[iz-1]-fn[iz])<exp(-0.5*x*x)) {
|
||||
return x;
|
||||
}
|
||||
|
||||
hz = SHR3;
|
||||
iz = hz&127;
|
||||
|
||||
if (abs(hz) < kn[iz]) {
|
||||
return ((hz)*wn[iz]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!\Procedure to create tables for normal distribution kn,wn and fn. */
|
||||
|
||||
void tableNor(unsigned long seed) {
|
||||
jsr=seed;
|
||||
double dn = 3.442619855899;
|
||||
int i;
|
||||
const double m1 = 2147483648.0;
|
||||
double q;
|
||||
double tn = 3.442619855899;
|
||||
const double vn = 9.91256303526217E-03;
|
||||
q = vn/exp(-0.5*dn*dn);
|
||||
kn[0] = ((dn/q)*m1);
|
||||
kn[1] = 0;
|
||||
wn[0] = ( q / m1 );
|
||||
wn[127] = ( dn / m1 );
|
||||
fn[0] = 1.0;
|
||||
fn[127] = ( exp ( - 0.5 * dn * dn ) );
|
||||
|
||||
for ( i = 126; 1 <= i; i-- ) {
|
||||
dn = sqrt (-2.0 * log ( vn/dn + exp(-0.5*dn*dn)));
|
||||
kn[i+1] = ((dn / tn)*m1);
|
||||
tn = dn;
|
||||
fn[i] = (exp (-0.5*dn*dn));
|
||||
wn[i] = (dn / m1);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
double gaussZiggurat(double mean, double variance) {
|
||||
hz=SHR3;
|
||||
iz=hz&127;
|
||||
return abs(hz)<kn[iz]? hz*wn[iz] : nfix();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The OpenAirInterface Software Alliance licenses this file to You under
|
||||
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.openairinterface.org/?page_id=698
|
||||
*
|
||||
* Author and copyright: Laurent Thomas, open-cells.com
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*-------------------------------------------------------------------------------
|
||||
* For more information about the OpenAirInterface (OAI) Software Alliance:
|
||||
* contact@openairinterface.org
|
||||
*/
|
||||
|
||||
#ifndef __RFSIMULATOR_H
|
||||
#define __RFSIMULATOR_H
|
||||
double gaussZiggurat(double mean, double variance);
|
||||
void tableNor(unsigned long seed);
|
||||
void rxAddInput( const c16_t *input_sig,
|
||||
c16_t *after_channel_sig,
|
||||
int rxAnt,
|
||||
channel_desc_t *channelDesc,
|
||||
int nbSamples,
|
||||
uint64_t TS,
|
||||
uint32_t CirSize
|
||||
);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,984 @@
|
||||
/*
|
||||
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The OpenAirInterface Software Alliance licenses this file to You under
|
||||
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.openairinterface.org/?page_id=698
|
||||
*
|
||||
* Author and copyright: Laurent Thomas, open-cells.com
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*-------------------------------------------------------------------------------
|
||||
* For more information about the OpenAirInterface (OAI) Software Alliance:
|
||||
* contact@openairinterface.org
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Open issues and limitations
|
||||
* The read and write should be called in the same thread, that is not new USRP UHD design
|
||||
* When the opposite side switch from passive reading to active R+Write, the synchro is not fully deterministic
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <common/utils/assertions.h>
|
||||
#include <common/utils/LOG/log.h>
|
||||
#include <common/utils/load_module_shlib.h>
|
||||
#include <common/utils/telnetsrv/telnetsrv.h>
|
||||
#include <common/config/config_userapi.h>
|
||||
#include "common_lib.h"
|
||||
#include <openair1/PHY/defs_eNB.h>
|
||||
#include "openair1/PHY/defs_UE.h"
|
||||
#define CHANNELMOD_DYNAMICLOAD
|
||||
#include <openair1/SIMULATION/TOOLS/sim.h>
|
||||
#include "rfsimulator.h"
|
||||
|
||||
#define PORT 4043 //default TCP port for this simulator
|
||||
#define CirSize 6144000 // 100ms is enough
|
||||
#define sampleToByte(a,b) ((a)*(b)*sizeof(sample_t))
|
||||
#define byteToSample(a,b) ((a)/(sizeof(sample_t)*(b)))
|
||||
|
||||
#define MAX_SIMULATION_CONNECTED_NODES 5
|
||||
#define GENERATE_CHANNEL 10 //each frame in DL
|
||||
|
||||
|
||||
//
|
||||
|
||||
#define RFSIMU_SECTION "rfsimulator"
|
||||
#define RFSIMU_OPTIONS_PARAMNAME "options"
|
||||
|
||||
|
||||
#define RFSIM_CONFIG_HELP_OPTIONS " list of comma separated options to enable rf simulator functionalities. Available options: \n"\
|
||||
" chanmod: enable channel modelisation\n"\
|
||||
" saviq: enable saving written iqs to a file\n"
|
||||
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
|
||||
/* configuration parameters for the rfsimulator device */
|
||||
/* optname helpstr paramflags XXXptr defXXXval type numelt */
|
||||
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
|
||||
#define simOpt PARAMFLAG_NOFREE|PARAMFLAG_CMDLINE_NOPREFIXENABLED
|
||||
#define RFSIMULATOR_PARAMS_DESC { \
|
||||
{"serveraddr", "<ip address to connect to>\n", simOpt, strptr:&rfsimulator->ip, defstrval:"127.0.0.1", TYPE_STRING, 0 },\
|
||||
{"serverport", "<port to connect to>\n", simOpt, u16ptr:&(rfsimulator->port), defuintval:PORT, TYPE_UINT16, 0 },\
|
||||
{RFSIMU_OPTIONS_PARAMNAME, RFSIM_CONFIG_HELP_OPTIONS, 0, strlistptr:NULL, defstrlistval:NULL, TYPE_STRINGLIST,0 },\
|
||||
{"IQfile", "<file path to use when saving IQs>\n",simOpt, strptr:&saveF, defstrval:"/tmp/rfsimulator.iqs",TYPE_STRING, 0 },\
|
||||
{"modelname", "<channel model name>\n", simOpt, strptr:&modelname, defstrval:"AWGN", TYPE_STRING, 0 },\
|
||||
{"ploss", "<channel path loss in dB>\n", simOpt, dblptr:&(rfsimulator->chan_pathloss), defdblval:0, TYPE_DOUBLE, 0 },\
|
||||
{"forgetfact", "<channel forget factor ((0 to 1)>\n", simOpt, dblptr:&(rfsimulator->chan_forgetfact), defdblval:0, TYPE_DOUBLE, 0 },\
|
||||
{"offset", "<channel offset in samps>\n", simOpt, iptr:&(rfsimulator->chan_offset), defintval:0, TYPE_INT, 0 }\
|
||||
};
|
||||
|
||||
|
||||
|
||||
static int rfsimu_setchanmod_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg);
|
||||
static int rfsimu_setdistance_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg);
|
||||
static int rfsimu_getdistance_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg);
|
||||
static int rfsimu_vtime_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg);
|
||||
static telnetshell_cmddef_t rfsimu_cmdarray[] = {
|
||||
{"setmodel","<model name> <model type>",(cmdfunc_t)rfsimu_setchanmod_cmd,TELNETSRV_CMDFLAG_PUSHINTPOOLQ},
|
||||
{"setdistance","<model name> <distance>", (cmdfunc_t)rfsimu_setdistance_cmd, TELNETSRV_CMDFLAG_PUSHINTPOOLQ},
|
||||
{"getdistance","<model name>", (cmdfunc_t) rfsimu_getdistance_cmd, TELNETSRV_CMDFLAG_PUSHINTPOOLQ},
|
||||
{"vtime","", (cmdfunc_t) rfsimu_vtime_cmd, TELNETSRV_CMDFLAG_PUSHINTPOOLQ},
|
||||
{"","",NULL},
|
||||
};
|
||||
|
||||
static telnetshell_vardef_t rfsimu_vardef[] = {
|
||||
{"",0,NULL}
|
||||
};
|
||||
pthread_mutex_t Sockmutex;
|
||||
|
||||
typedef c16_t sample_t; // 2*16 bits complex number
|
||||
|
||||
typedef struct buffer_s {
|
||||
int conn_sock;
|
||||
openair0_timestamp lastReceivedTS;
|
||||
bool headerMode;
|
||||
bool trashingPacket;
|
||||
samplesBlockHeader_t th;
|
||||
char *transferPtr;
|
||||
uint64_t remainToTransfer;
|
||||
char *circularBufEnd;
|
||||
sample_t *circularBuf;
|
||||
channel_desc_t *channel_model;
|
||||
} buffer_t;
|
||||
|
||||
typedef struct {
|
||||
int listen_sock, epollfd;
|
||||
openair0_timestamp nextRxTstamp;
|
||||
openair0_timestamp lastWroteTS;
|
||||
uint64_t typeStamp;
|
||||
char *ip;
|
||||
uint16_t port;
|
||||
int saveIQfile;
|
||||
buffer_t buf[FD_SETSIZE];
|
||||
int rx_num_channels;
|
||||
int tx_num_channels;
|
||||
double sample_rate;
|
||||
double tx_bw;
|
||||
int channelmod;
|
||||
double chan_pathloss;
|
||||
double chan_forgetfact;
|
||||
int chan_offset;
|
||||
float noise_power_dB;
|
||||
void *telnetcmd_qid;
|
||||
poll_telnetcmdq_func_t poll_telnetcmdq;
|
||||
} rfsimulator_state_t;
|
||||
|
||||
|
||||
static void allocCirBuf(rfsimulator_state_t *bridge, int sock) {
|
||||
buffer_t *ptr=&bridge->buf[sock];
|
||||
AssertFatal ( (ptr->circularBuf=(sample_t *) malloc(sampleToByte(CirSize,1))) != NULL, "");
|
||||
ptr->circularBufEnd=((char *)ptr->circularBuf)+sampleToByte(CirSize,1);
|
||||
ptr->conn_sock=sock;
|
||||
ptr->lastReceivedTS=0;
|
||||
ptr->headerMode=true;
|
||||
ptr->trashingPacket=false;
|
||||
ptr->transferPtr=(char *)&ptr->th;
|
||||
ptr->remainToTransfer=sizeof(samplesBlockHeader_t);
|
||||
int sendbuff=1000*1000*100;
|
||||
AssertFatal ( setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)) == 0, "");
|
||||
struct epoll_event ev= {0};
|
||||
ev.events = EPOLLIN | EPOLLRDHUP;
|
||||
ev.data.fd = sock;
|
||||
AssertFatal(epoll_ctl(bridge->epollfd, EPOLL_CTL_ADD, sock, &ev) != -1, "");
|
||||
|
||||
if ( bridge->channelmod > 0) {
|
||||
// create channel simulation model for this mode reception
|
||||
// snr_dB is pure global, coming from configuration paramter "-s"
|
||||
// Fixme: referenceSignalPower should come from the right place
|
||||
// but the datamodel is inconsistant
|
||||
// legacy: RC.ru[ru_id]->frame_parms.pdsch_config_common.referenceSignalPower
|
||||
// (must not come from ru[]->frame_parms as it doesn't belong to ru !!!)
|
||||
// Legacy sets it as:
|
||||
// ptr->channel_model->path_loss_dB = -132.24 + snr_dB - RC.ru[0]->frame_parms->pdsch_config_common.referenceSignalPower;
|
||||
// we use directly the paramter passed on the command line ("-s")
|
||||
// the value channel_model->path_loss_dB seems only a storage place (new_channel_desc_scm() only copy the passed value)
|
||||
// Legacy changes directlty the variable channel_model->path_loss_dB place to place
|
||||
// while calling new_channel_desc_scm() with path losses = 0
|
||||
static bool init_done=false;
|
||||
|
||||
if (!init_done) {
|
||||
uint64_t rand;
|
||||
FILE *h=fopen("/dev/random","r");
|
||||
|
||||
if ( 1 != fread(&rand,sizeof(rand),1,h) )
|
||||
LOG_W(HW, "Simulator can't read /dev/random\n");
|
||||
|
||||
fclose(h);
|
||||
randominit(rand);
|
||||
tableNor(rand);
|
||||
init_done=true;
|
||||
}
|
||||
char *modelname = (bridge->typeStamp == ENB_MAGICDL) ? "rfsimu_channel_ue0":"rfsimu_channel_enB0";
|
||||
ptr->channel_model=find_channel_desc_fromname(modelname); // path_loss in dB
|
||||
AssertFatal((ptr->channel_model!= NULL),"Channel model %s not found, check config file\n",modelname);
|
||||
set_channeldesc_owner(ptr->channel_model, RFSIMU_MODULEID);
|
||||
random_channel(ptr->channel_model,false);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeCirBuf(rfsimulator_state_t *bridge, int sock) {
|
||||
AssertFatal( epoll_ctl(bridge->epollfd, EPOLL_CTL_DEL, sock, NULL) != -1, "");
|
||||
close(sock);
|
||||
free(bridge->buf[sock].circularBuf);
|
||||
// Fixme: no free_channel_desc_scm(bridge->buf[sock].channel_model) implemented
|
||||
// a lot of mem leaks
|
||||
//free(bridge->buf[sock].channel_model);
|
||||
memset(&bridge->buf[sock], 0, sizeof(buffer_t));
|
||||
bridge->buf[sock].conn_sock=-1;
|
||||
}
|
||||
|
||||
static void socketError(rfsimulator_state_t *bridge, int sock) {
|
||||
if (bridge->buf[sock].conn_sock!=-1) {
|
||||
LOG_W(HW,"Lost socket \n");
|
||||
removeCirBuf(bridge, sock);
|
||||
|
||||
if (bridge->typeStamp==UE_MAGICDL)
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#define helpTxt "\
|
||||
\x1b[31m\
|
||||
rfsimulator: error: you have to run one UE and one eNB\n\
|
||||
For this, export RFSIMULATOR=enb (eNB case) or \n\
|
||||
RFSIMULATOR=<an ip address> (UE case)\n\
|
||||
or use rfsimulator.serveraddr configuration option\n\
|
||||
\x1b[m"
|
||||
|
||||
enum blocking_t {
|
||||
notBlocking,
|
||||
blocking
|
||||
};
|
||||
|
||||
static void setblocking(int sock, enum blocking_t active) {
|
||||
int opts = fcntl(sock, F_GETFL);
|
||||
AssertFatal(opts >= 0, "fcntl(): errno %d, %s\n", errno, strerror(errno));
|
||||
|
||||
if (active==blocking)
|
||||
opts = opts & ~O_NONBLOCK;
|
||||
else
|
||||
opts = opts | O_NONBLOCK;
|
||||
|
||||
opts = fcntl(sock, F_SETFL, opts);
|
||||
AssertFatal(opts >= 0, "fcntl(): errno %d, %s\n", errno, strerror(errno));
|
||||
}
|
||||
|
||||
static bool flushInput(rfsimulator_state_t *t, int timeout, int nsamps);
|
||||
|
||||
static void fullwrite(int fd, void *_buf, ssize_t count, rfsimulator_state_t *t) {
|
||||
if (t->saveIQfile != -1) {
|
||||
if (write(t->saveIQfile, _buf, count) != count )
|
||||
LOG_E(HW,"write in save iq file failed (%s)\n",strerror(errno));
|
||||
}
|
||||
|
||||
AssertFatal(fd>=0 && _buf && count >0 && t,
|
||||
"Bug: %d/%p/%zd/%p", fd, _buf, count, t);
|
||||
char *buf = _buf;
|
||||
ssize_t l;
|
||||
|
||||
while (count) {
|
||||
l = write(fd, buf, count);
|
||||
|
||||
if (l <= 0) {
|
||||
if (errno==EINTR)
|
||||
continue;
|
||||
|
||||
if(errno==EAGAIN) {
|
||||
// The opposite side is saturated
|
||||
// we read incoming sockets meawhile waiting
|
||||
//flushInput(t, 5);
|
||||
usleep(500);
|
||||
continue;
|
||||
} else
|
||||
return;
|
||||
}
|
||||
|
||||
count -= l;
|
||||
buf += l;
|
||||
}
|
||||
}
|
||||
|
||||
static void rfsimulator_readconfig(rfsimulator_state_t *rfsimulator) {
|
||||
char *saveF=NULL;
|
||||
char *modelname=NULL;
|
||||
paramdef_t rfsimu_params[] = RFSIMULATOR_PARAMS_DESC;
|
||||
int p = config_paramidx_fromname(rfsimu_params,sizeof(rfsimu_params)/sizeof(paramdef_t), RFSIMU_OPTIONS_PARAMNAME) ;
|
||||
int ret = config_get( rfsimu_params,sizeof(rfsimu_params)/sizeof(paramdef_t),RFSIMU_SECTION);
|
||||
AssertFatal(ret >= 0, "configuration couldn't be performed");
|
||||
rfsimulator->saveIQfile = -1;
|
||||
|
||||
for(int i=0; i<rfsimu_params[p].numelt ; i++) {
|
||||
if (strcmp(rfsimu_params[p].strlistptr[i],"saviq") == 0) {
|
||||
rfsimulator->saveIQfile=open(saveF,O_APPEND| O_CREAT|O_TRUNC | O_WRONLY, 0666);
|
||||
|
||||
if ( rfsimulator->saveIQfile != -1 )
|
||||
LOG_I(HW,"rfsimulator: will save written IQ samples in %s\n", saveF);
|
||||
else
|
||||
LOG_E(HW, "can't open %s for IQ saving (%s)\n", saveF, strerror(errno));
|
||||
|
||||
break;
|
||||
} else if (strcmp(rfsimu_params[p].strlistptr[i],"chanmod") == 0) {
|
||||
init_channelmod();
|
||||
load_channellist(rfsimulator->tx_num_channels, rfsimulator->rx_num_channels, rfsimulator->sample_rate, rfsimulator->tx_bw);
|
||||
rfsimulator->channelmod=true;
|
||||
} else {
|
||||
fprintf(stderr,"Unknown rfsimulator option: %s\n",rfsimu_params[p].strlistptr[i]);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
/* for compatibility keep environment variable usage */
|
||||
if ( getenv("RFSIMULATOR") != NULL ) {
|
||||
rfsimulator->ip=getenv("RFSIMULATOR");
|
||||
}
|
||||
|
||||
if ( strncasecmp(rfsimulator->ip,"enb",3) == 0 ||
|
||||
strncasecmp(rfsimulator->ip,"server",3) == 0 )
|
||||
rfsimulator->typeStamp = ENB_MAGICDL;
|
||||
else
|
||||
rfsimulator->typeStamp = UE_MAGICDL;
|
||||
}
|
||||
|
||||
static int rfsimu_setchanmod_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg) {
|
||||
char *modelname=NULL;
|
||||
char *modeltype=NULL;
|
||||
if (debug)
|
||||
prnt("rfsimu_setchanmod_cmd buffer \"%s\"\n",buff);
|
||||
int s = sscanf(buff,"%m[^ ] %ms\n",&modelname, &modeltype);
|
||||
|
||||
if (s == 2) {
|
||||
int channelmod=modelid_fromstrtype(modeltype);
|
||||
|
||||
if (channelmod<0)
|
||||
prnt("ERROR: model type %s unknown\n",modeltype);
|
||||
else {
|
||||
rfsimulator_state_t *t = (rfsimulator_state_t *)arg;
|
||||
int found=0;
|
||||
for (int i=0; i<FD_SETSIZE; i++) {
|
||||
buffer_t *b=&t->buf[i];
|
||||
if ( b->channel_model==NULL)
|
||||
continue;
|
||||
if (b->channel_model->model_name==NULL)
|
||||
continue;
|
||||
if (b->conn_sock >= 0 && (strcmp(b->channel_model->model_name,modelname)==0)) {
|
||||
|
||||
channel_desc_t *newmodel=new_channel_desc_scm(t->tx_num_channels,t->rx_num_channels,
|
||||
channelmod,
|
||||
t->sample_rate,
|
||||
t->tx_bw,
|
||||
30e-9, // TDL delay-spread parameter
|
||||
t->chan_forgetfact, // forgetting_factor
|
||||
t->chan_offset, // maybe used for TA
|
||||
t->chan_pathloss,
|
||||
t->noise_power_dB
|
||||
); // path_loss in dB
|
||||
set_channeldesc_owner(newmodel, RFSIMU_MODULEID);
|
||||
set_channeldesc_name(newmodel,modelname);
|
||||
random_channel(newmodel,false);
|
||||
channel_desc_t *oldmodel=b->channel_model;
|
||||
b->channel_model=newmodel;
|
||||
free_channel_desc_scm(oldmodel);
|
||||
prnt("New model type %s applied to channel %s connected to sock %d\n",modeltype,modelname,i);
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
} /* for */
|
||||
if (found==0)
|
||||
prnt("Channel %s not found or not currently used\n",modelname);
|
||||
}
|
||||
} else {
|
||||
prnt("ERROR: 2 parameters required: model name and model type (%i found)\n",s);
|
||||
}
|
||||
|
||||
free(modelname);
|
||||
free(modeltype);
|
||||
return CMDSTATUS_FOUND;
|
||||
}
|
||||
|
||||
//static void print_cirBuf(struct complex16 *circularBuf,
|
||||
// uint64_t firstSample,
|
||||
// uint32_t cirSize,
|
||||
// int neg,
|
||||
// int pos,
|
||||
// int nbTx)
|
||||
//{
|
||||
// for (int i = -neg; i < pos ; ++i) {
|
||||
// for (int txAnt = 0; txAnt < nbTx; txAnt++) {
|
||||
// const int idx = ((firstSample + i) * nbTx + txAnt + cirSize) % cirSize;
|
||||
// if (i == 0)
|
||||
// printf("->");
|
||||
// printf("%08x%08x\n", circularBuf[idx].r, circularBuf[idx].i);
|
||||
// }
|
||||
// }
|
||||
// printf("\n");
|
||||
//}
|
||||
|
||||
static void rfsimu_offset_change_cirBuf(struct complex16 *circularBuf,
|
||||
uint64_t firstSample,
|
||||
uint32_t cirSize,
|
||||
int old_offset,
|
||||
int new_offset,
|
||||
int nbTx)
|
||||
{
|
||||
//int start = max(new_offset, old_offset) + 10;
|
||||
//int end = 10;
|
||||
//printf("new_offset %d old_offset %d start %d end %d\n", new_offset, old_offset, start, end);
|
||||
//printf("ringbuffer before:\n");
|
||||
//print_cirBuf(circularBuf, firstSample, cirSize, start, end, nbTx);
|
||||
|
||||
int doffset = new_offset - old_offset;
|
||||
if (doffset > 0) {
|
||||
/* Moving away, creating a gap. We need to insert "zero" samples between
|
||||
* the previous (end of the) slot and the new slot (at the ringbuffer
|
||||
* index) to prevent that the receiving side detects things that are not
|
||||
* in the channel (e.g., samples that have already been delivered). */
|
||||
for (int i = new_offset; i > 0; --i) {
|
||||
for (int txAnt = 0; txAnt < nbTx; txAnt++) {
|
||||
const int newidx = ((firstSample - i) * nbTx + txAnt + cirSize) % cirSize;
|
||||
if (i > doffset) {
|
||||
// shift samples not read yet
|
||||
const int oldidx = (newidx + doffset) % cirSize;
|
||||
circularBuf[newidx] = circularBuf[oldidx];
|
||||
} else {
|
||||
// create zero samples between slots
|
||||
const struct complex16 nullsample = {0, 0};
|
||||
circularBuf[newidx] = nullsample;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Moving closer, creating overlap between samples. For simplicity, we
|
||||
* simply drop `doffset` samples at the end of the previous slot
|
||||
* (this is, in a sense, arbitrary). In a real channel, there would be
|
||||
* some overlap between samples, e.g., for `doffset == 1` we could add
|
||||
* two samples. I think that we cannot do that for multiple samples,
|
||||
* though, and so we just drop some */
|
||||
// drop the last -doffset samples of the previous slot
|
||||
for (int i = old_offset; i > -doffset; --i) {
|
||||
for (int txAnt = 0; txAnt < nbTx; txAnt++) {
|
||||
const int oldidx = ((firstSample - i) * nbTx + txAnt + cirSize) % cirSize;
|
||||
const int newidx = (oldidx - doffset) % cirSize;
|
||||
circularBuf[newidx] = circularBuf[oldidx];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//printf("ringbuffer after:\n");
|
||||
//print_cirBuf(circularBuf, firstSample, cirSize, start, end, nbTx);
|
||||
}
|
||||
|
||||
static int rfsimu_setdistance_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg)
|
||||
{
|
||||
if (debug)
|
||||
prnt("%s() buffer \"%s\"\n", __func__, buff);
|
||||
|
||||
char *modelname;
|
||||
int distance;
|
||||
int s = sscanf(buff,"%m[^ ] %d\n", &modelname, &distance);
|
||||
if (s != 2) {
|
||||
prnt("require exact two parameters\n");
|
||||
return CMDSTATUS_VARNOTFOUND;
|
||||
}
|
||||
|
||||
rfsimulator_state_t *t = (rfsimulator_state_t *)arg;
|
||||
const double sample_rate = t->sample_rate;
|
||||
const double c = 299792458; /* 3e8 */
|
||||
|
||||
const int new_offset = (double) distance * sample_rate / c;
|
||||
const double new_distance = (double) new_offset * c / sample_rate;
|
||||
|
||||
prnt("\nnew_offset %d new (exact) distance %.3f m\n", new_offset, new_distance);
|
||||
|
||||
/* Set distance in rfsim and channel model, update channel and ringbuffer */
|
||||
for (int i=0; i<FD_SETSIZE; i++) {
|
||||
buffer_t *b=&t->buf[i];
|
||||
if (b->conn_sock <= 0
|
||||
|| b->channel_model == NULL
|
||||
|| b->channel_model->model_name == NULL
|
||||
|| strcmp(b->channel_model->model_name, modelname) != 0)
|
||||
continue;
|
||||
|
||||
channel_desc_t *cd = b->channel_model;
|
||||
const int old_offset = cd->channel_offset;
|
||||
cd->channel_offset = new_offset;
|
||||
|
||||
const int nbTx = cd->nb_tx;
|
||||
rfsimu_offset_change_cirBuf(b->circularBuf, t->nextRxTstamp, CirSize, old_offset, new_offset, nbTx);
|
||||
}
|
||||
|
||||
free(modelname);
|
||||
|
||||
return CMDSTATUS_FOUND;
|
||||
}
|
||||
|
||||
static int rfsimu_getdistance_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg)
|
||||
{
|
||||
if (debug)
|
||||
prnt("%s() buffer \"%s\"\n", __func__, buff);
|
||||
|
||||
char *modelname;
|
||||
int s = sscanf(buff,"%ms\n", &modelname);
|
||||
if (s != 1) {
|
||||
prnt("require exact two parameters\n");
|
||||
return CMDSTATUS_VARNOTFOUND;
|
||||
}
|
||||
|
||||
rfsimulator_state_t *t = (rfsimulator_state_t *)arg;
|
||||
const double sample_rate = t->sample_rate;
|
||||
const double c = 299792458; /* 3e8 */
|
||||
|
||||
for (int i=0; i<FD_SETSIZE; i++) {
|
||||
buffer_t *b=&t->buf[i];
|
||||
if (b->conn_sock <= 0
|
||||
|| b->channel_model == NULL
|
||||
|| b->channel_model->model_name == NULL
|
||||
|| strcmp(b->channel_model->model_name, modelname) != 0)
|
||||
continue;
|
||||
|
||||
channel_desc_t *cd = b->channel_model;
|
||||
const int offset = cd->channel_offset;
|
||||
const double distance = (double) offset * c / sample_rate;
|
||||
prnt("\noffset %d distance %.3f m\n", offset, distance);
|
||||
}
|
||||
|
||||
return CMDSTATUS_FOUND;
|
||||
}
|
||||
|
||||
static int rfsimu_vtime_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg)
|
||||
{
|
||||
rfsimulator_state_t *t = (rfsimulator_state_t *)arg;
|
||||
const openair0_timestamp ts = t->nextRxTstamp;
|
||||
const double sample_rate = t->sample_rate;
|
||||
prnt("vtime measurement: TS %llu sample_rate %.3f\n", ts, sample_rate);
|
||||
return CMDSTATUS_FOUND;
|
||||
}
|
||||
|
||||
static int startServer(openair0_device *device) {
|
||||
rfsimulator_state_t *t = (rfsimulator_state_t *) device->priv;
|
||||
t->typeStamp=ENB_MAGICDL;
|
||||
AssertFatal((t->listen_sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0, "");
|
||||
int enable = 1;
|
||||
AssertFatal(setsockopt(t->listen_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == 0, "");
|
||||
struct sockaddr_in addr = {
|
||||
sin_family:
|
||||
AF_INET,
|
||||
sin_port:
|
||||
htons(t->port),
|
||||
sin_addr:
|
||||
{ s_addr: INADDR_ANY }
|
||||
};
|
||||
bind(t->listen_sock, (struct sockaddr *)&addr, sizeof(addr));
|
||||
AssertFatal(listen(t->listen_sock, 5) == 0, "");
|
||||
struct epoll_event ev= {0};
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = t->listen_sock;
|
||||
AssertFatal(epoll_ctl(t->epollfd, EPOLL_CTL_ADD, t->listen_sock, &ev) != -1, "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int startClient(openair0_device *device) {
|
||||
rfsimulator_state_t *t = device->priv;
|
||||
t->typeStamp=UE_MAGICDL;
|
||||
int sock;
|
||||
AssertFatal((sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0, "");
|
||||
struct sockaddr_in addr = {
|
||||
sin_family:
|
||||
AF_INET,
|
||||
sin_port:
|
||||
htons(t->port),
|
||||
sin_addr:
|
||||
{ s_addr: INADDR_ANY }
|
||||
};
|
||||
addr.sin_addr.s_addr = inet_addr(t->ip);
|
||||
bool connected=false;
|
||||
|
||||
while(!connected) {
|
||||
LOG_I(HW,"rfsimulator: trying to connect to %s:%d\n", t->ip, t->port);
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
LOG_I(HW,"rfsimulator: connection established\n");
|
||||
connected=true;
|
||||
}
|
||||
|
||||
perror("rfsimulator");
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
setblocking(sock, notBlocking);
|
||||
allocCirBuf(t, sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rfsimulator_write_internal(rfsimulator_state_t *t, openair0_timestamp timestamp, void **samplesVoid, int nsamps, int nbAnt, int flags, bool alreadyLocked) {
|
||||
if (!alreadyLocked)
|
||||
pthread_mutex_lock(&Sockmutex);
|
||||
|
||||
LOG_D(HW,"sending %d samples at time: %ld, nbAnt %d\n", nsamps, timestamp, nbAnt);
|
||||
|
||||
for (int i=0; i<FD_SETSIZE; i++) {
|
||||
buffer_t *b=&t->buf[i];
|
||||
|
||||
if (b->conn_sock >= 0 ) {
|
||||
samplesBlockHeader_t header= {t->typeStamp, nsamps, nbAnt, timestamp};
|
||||
fullwrite(b->conn_sock,&header, sizeof(header), t);
|
||||
sample_t tmpSamples[nsamps][nbAnt];
|
||||
|
||||
for(int a=0; a<nbAnt; a++) {
|
||||
sample_t *in=(sample_t *)samplesVoid[a];
|
||||
|
||||
for(int s=0; s<nsamps; s++)
|
||||
tmpSamples[s][a]=in[s];
|
||||
}
|
||||
|
||||
if (b->conn_sock >= 0 ) {
|
||||
fullwrite(b->conn_sock, (void *)tmpSamples, sampleToByte(nsamps,nbAnt), t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( t->lastWroteTS != 0 && abs((double)t->lastWroteTS-timestamp) > (double)CirSize)
|
||||
LOG_E(HW,"Discontinuous TX gap too large Tx:%lu, %lu\n", t->lastWroteTS, timestamp);
|
||||
|
||||
if (t->lastWroteTS > timestamp+nsamps)
|
||||
LOG_E(HW,"Not supported to send Tx out of order (same in USRP) %lu, %lu\n",
|
||||
t->lastWroteTS, timestamp);
|
||||
|
||||
t->lastWroteTS=timestamp+nsamps;
|
||||
|
||||
if (!alreadyLocked)
|
||||
pthread_mutex_unlock(&Sockmutex);
|
||||
|
||||
LOG_D(HW,"sent %d samples at time: %ld->%ld, energy in first antenna: %d\n",
|
||||
nsamps, timestamp, timestamp+nsamps, signal_energy(samplesVoid[0], nsamps) );
|
||||
return nsamps;
|
||||
}
|
||||
|
||||
static int rfsimulator_write(openair0_device *device, openair0_timestamp timestamp, void **samplesVoid, int nsamps, int nbAnt, int flags) {
|
||||
return rfsimulator_write_internal(device->priv, timestamp, samplesVoid, nsamps, nbAnt, flags, false);
|
||||
}
|
||||
|
||||
static bool flushInput(rfsimulator_state_t *t, int timeout, int nsamps_for_initial) {
|
||||
// Process all incoming events on sockets
|
||||
// store the data in lists
|
||||
struct epoll_event events[FD_SETSIZE]= {{0}};
|
||||
int nfds = epoll_wait(t->epollfd, events, FD_SETSIZE, timeout);
|
||||
|
||||
if ( nfds==-1 ) {
|
||||
if ( errno==EINTR || errno==EAGAIN ) {
|
||||
return false;
|
||||
} else
|
||||
AssertFatal(false,"error in epoll_wait\n");
|
||||
}
|
||||
|
||||
for (int nbEv = 0; nbEv < nfds; ++nbEv) {
|
||||
int fd=events[nbEv].data.fd;
|
||||
|
||||
if (events[nbEv].events & EPOLLIN && fd == t->listen_sock) {
|
||||
int conn_sock;
|
||||
AssertFatal( (conn_sock = accept(t->listen_sock,NULL,NULL)) != -1, "");
|
||||
setblocking(conn_sock, notBlocking);
|
||||
allocCirBuf(t, conn_sock);
|
||||
LOG_I(HW,"A client connected, sending the current time\n");
|
||||
c16_t v= {0};
|
||||
void *samplesVoid[t->tx_num_channels];
|
||||
|
||||
for ( int i=0; i < t->tx_num_channels; i++)
|
||||
samplesVoid[i]=(void *)&v;
|
||||
|
||||
rfsimulator_write_internal(t, t->lastWroteTS > 1 ? t->lastWroteTS-1 : 0,
|
||||
samplesVoid, 1,
|
||||
t->tx_num_channels, 1, false);
|
||||
} else {
|
||||
if ( events[nbEv].events & (EPOLLHUP | EPOLLERR | EPOLLRDHUP) ) {
|
||||
socketError(t,fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
buffer_t *b=&t->buf[fd];
|
||||
|
||||
if ( b->circularBuf == NULL ) {
|
||||
LOG_E(HW, "received data on not connected socket %d\n", events[nbEv].data.fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
ssize_t blockSz;
|
||||
|
||||
if ( b->headerMode)
|
||||
blockSz=b->remainToTransfer;
|
||||
else
|
||||
blockSz= b->transferPtr + b->remainToTransfer <= b->circularBufEnd ?
|
||||
b->remainToTransfer :
|
||||
b->circularBufEnd - b->transferPtr ;
|
||||
|
||||
ssize_t sz=recv(fd, b->transferPtr, blockSz, MSG_DONTWAIT);
|
||||
|
||||
if ( sz < 0 ) {
|
||||
if ( errno != EAGAIN ) {
|
||||
LOG_E(HW,"socket failed %s\n", strerror(errno));
|
||||
//abort();
|
||||
}
|
||||
} else if ( sz == 0 )
|
||||
continue;
|
||||
|
||||
LOG_D(HW, "Socket rcv %zd bytes\n", sz);
|
||||
AssertFatal((b->remainToTransfer-=sz) >= 0, "");
|
||||
b->transferPtr+=sz;
|
||||
|
||||
if (b->transferPtr==b->circularBufEnd )
|
||||
b->transferPtr=(char *)b->circularBuf;
|
||||
|
||||
// check the header and start block transfer
|
||||
if ( b->headerMode==true && b->remainToTransfer==0) {
|
||||
AssertFatal( (t->typeStamp == UE_MAGICDL && b->th.magic==ENB_MAGICDL) ||
|
||||
(t->typeStamp == ENB_MAGICDL && b->th.magic==UE_MAGICDL), "Socket Error in protocol");
|
||||
b->headerMode=false;
|
||||
|
||||
if ( t->nextRxTstamp == 0 ) { // First block in UE, resync with the eNB current TS
|
||||
t->nextRxTstamp=b->th.timestamp> nsamps_for_initial ?
|
||||
b->th.timestamp - nsamps_for_initial :
|
||||
0;
|
||||
b->lastReceivedTS=b->th.timestamp> nsamps_for_initial ?
|
||||
b->th.timestamp :
|
||||
nsamps_for_initial;
|
||||
LOG_W(HW,"UE got first timestamp: starting at %lu\n", t->nextRxTstamp);
|
||||
b->trashingPacket=true;
|
||||
} else if ( b->lastReceivedTS < b->th.timestamp) {
|
||||
int nbAnt= b->th.nbAnt;
|
||||
|
||||
if ( b->th.timestamp-b->lastReceivedTS < CirSize ) {
|
||||
for (uint64_t index=b->lastReceivedTS; index < b->th.timestamp; index++ ) {
|
||||
for (int a=0; a < nbAnt; a++) {
|
||||
b->circularBuf[(index*nbAnt+a)%CirSize].r = 0;
|
||||
b->circularBuf[(index*nbAnt+a)%CirSize].i = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
memset(b->circularBuf, 0, sampleToByte(CirSize,1));
|
||||
}
|
||||
|
||||
if (b->lastReceivedTS != 0 && b->th.timestamp-b->lastReceivedTS < 1000)
|
||||
LOG_W(HW,"UEsock: %d gap of: %ld in reception\n", fd, b->th.timestamp-b->lastReceivedTS );
|
||||
|
||||
b->lastReceivedTS=b->th.timestamp;
|
||||
} else if ( b->lastReceivedTS > b->th.timestamp && b->th.size == 1 ) {
|
||||
LOG_W(HW,"Received Rx/Tx synchro out of order\n");
|
||||
b->trashingPacket=true;
|
||||
} else if ( b->lastReceivedTS == b->th.timestamp ) {
|
||||
// normal case
|
||||
} else {
|
||||
LOG_E(HW, "received data in past: current is %lu, new reception: %lu!\n", b->lastReceivedTS, b->th.timestamp);
|
||||
b->trashingPacket=true;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&Sockmutex);
|
||||
|
||||
if (t->lastWroteTS != 0 && ( abs((double)t->lastWroteTS-b->lastReceivedTS) > (double)CirSize))
|
||||
LOG_E(HW,"UEsock: %d Tx/Rx shift too large Tx:%lu, Rx:%lu\n", fd, t->lastWroteTS, b->lastReceivedTS);
|
||||
|
||||
pthread_mutex_unlock(&Sockmutex);
|
||||
b->transferPtr=(char *)&b->circularBuf[(b->lastReceivedTS*b->th.nbAnt)%CirSize];
|
||||
b->remainToTransfer=sampleToByte(b->th.size, b->th.nbAnt);
|
||||
}
|
||||
|
||||
if ( b->headerMode==false ) {
|
||||
if ( ! b->trashingPacket ) {
|
||||
b->lastReceivedTS=b->th.timestamp+b->th.size-byteToSample(b->remainToTransfer,b->th.nbAnt);
|
||||
LOG_D(HW,"UEsock: %d Set b->lastReceivedTS %ld\n", fd, b->lastReceivedTS);
|
||||
}
|
||||
|
||||
if ( b->remainToTransfer==0) {
|
||||
LOG_D(HW,"UEsock: %d Completed block reception: %ld\n", fd, b->lastReceivedTS);
|
||||
b->headerMode=true;
|
||||
b->transferPtr=(char *)&b->th;
|
||||
b->remainToTransfer=sizeof(samplesBlockHeader_t);
|
||||
b->th.magic=-1;
|
||||
b->trashingPacket=false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nfds>0;
|
||||
}
|
||||
|
||||
static int rfsimulator_read(openair0_device *device, openair0_timestamp *ptimestamp, void **samplesVoid, int nsamps, int nbAnt) {
|
||||
if (nbAnt > 4) {
|
||||
LOG_W(HW, "rfsimulator: only 4 antenna tested\n");
|
||||
}
|
||||
|
||||
rfsimulator_state_t *t = device->priv;
|
||||
LOG_D(HW, "Enter rfsimulator_read, expect %d samples, will release at TS: %ld, nbAnt %d\n", nsamps, t->nextRxTstamp+nsamps, nbAnt);
|
||||
// deliver data from received data
|
||||
// check if a UE is connected
|
||||
int first_sock;
|
||||
|
||||
for (first_sock=0; first_sock<FD_SETSIZE; first_sock++)
|
||||
if (t->buf[first_sock].circularBuf != NULL )
|
||||
break;
|
||||
|
||||
if ( first_sock == FD_SETSIZE ) {
|
||||
// no connected device (we are eNB, no UE is connected)
|
||||
if ( t->nextRxTstamp == 0)
|
||||
LOG_W(HW,"No connected device, generating void samples...\n");
|
||||
|
||||
if (!flushInput(t, 10, nsamps)) {
|
||||
for (int x=0; x < nbAnt; x++)
|
||||
memset(samplesVoid[x],0,sampleToByte(nsamps,1));
|
||||
|
||||
t->nextRxTstamp+=nsamps;
|
||||
|
||||
if ( ((t->nextRxTstamp/nsamps)%100) == 0)
|
||||
LOG_D(HW,"No UE, Generated void samples for Rx: %ld\n", t->nextRxTstamp);
|
||||
|
||||
*ptimestamp = t->nextRxTstamp-nsamps;
|
||||
return nsamps;
|
||||
}
|
||||
} else {
|
||||
|
||||
bool have_to_wait;
|
||||
|
||||
do {
|
||||
have_to_wait=false;
|
||||
|
||||
for ( int sock=0; sock<FD_SETSIZE; sock++) {
|
||||
buffer_t *b=&t->buf[sock];
|
||||
|
||||
if ( b->circularBuf )
|
||||
if ( t->nextRxTstamp+nsamps > b->lastReceivedTS ) {
|
||||
have_to_wait=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (have_to_wait)
|
||||
/*printf("Waiting on socket, current last ts: %ld, expected at least : %ld\n",
|
||||
ptr->lastReceivedTS,
|
||||
t->nextRxTstamp+nsamps);
|
||||
*/
|
||||
flushInput(t, 3, nsamps);
|
||||
} while (have_to_wait);
|
||||
}
|
||||
|
||||
// Clear the output buffer
|
||||
for (int a=0; a<nbAnt; a++)
|
||||
memset(samplesVoid[a],0,sampleToByte(nsamps,1));
|
||||
|
||||
// Add all input nodes signal in the output buffer
|
||||
for (int sock=0; sock<FD_SETSIZE; sock++) {
|
||||
buffer_t *ptr=&t->buf[sock];
|
||||
|
||||
if ( ptr->circularBuf ) {
|
||||
bool reGenerateChannel=false;
|
||||
|
||||
//fixme: when do we regenerate
|
||||
// it seems legacy behavior is: never in UL, each frame in DL
|
||||
if (reGenerateChannel)
|
||||
random_channel(ptr->channel_model,0);
|
||||
|
||||
if (t->poll_telnetcmdq)
|
||||
t->poll_telnetcmdq(t->telnetcmd_qid,t);
|
||||
|
||||
for (int a=0; a<nbAnt; a++) {//loop over number of Rx antennas
|
||||
if ( ptr->channel_model != NULL ) { // apply a channel model
|
||||
rxAddInput(ptr->circularBuf, (c16_t *) samplesVoid[a],
|
||||
a,
|
||||
ptr->channel_model,
|
||||
nsamps,
|
||||
t->nextRxTstamp,
|
||||
CirSize);
|
||||
}
|
||||
else { // no channel modeling
|
||||
|
||||
double H_awgn_mimo[4][4] ={{1.0, 0.2, 0.1, 0.05}, //rx 0
|
||||
{0.2, 1.0, 0.2, 0.1}, //rx 1
|
||||
{0.1, 0.2, 1.0, 0.2}, //rx 2
|
||||
{0.05, 0.1, 0.2, 1.0}};//rx 3
|
||||
|
||||
sample_t *out=(sample_t *)samplesVoid[a];
|
||||
int nbAnt_tx = ptr->th.nbAnt;//number of Tx antennas
|
||||
|
||||
//LOG_I(HW, "nbAnt_tx %d\n",nbAnt_tx);
|
||||
for (int i=0; i < nsamps; i++) {//loop over nsamps
|
||||
for (int a_tx=0; a_tx<nbAnt_tx; a_tx++) { //sum up signals from nbAnt_tx antennas
|
||||
out[i].r += (short)(ptr->circularBuf[((t->nextRxTstamp+i)*nbAnt_tx+a_tx)%CirSize].r*H_awgn_mimo[a][a_tx]);
|
||||
out[i].i += (short)(ptr->circularBuf[((t->nextRxTstamp+i)*nbAnt_tx+a_tx)%CirSize].i*H_awgn_mimo[a][a_tx]);
|
||||
} // end for a_tx
|
||||
} // end for i (number of samps)
|
||||
} // end of no channel modeling
|
||||
} // end for a (number of rx antennas)
|
||||
}
|
||||
}
|
||||
|
||||
*ptimestamp = t->nextRxTstamp; // return the time of the first sample
|
||||
t->nextRxTstamp+=nsamps;
|
||||
LOG_D(HW,"Rx to upper layer: %d from %ld to %ld, energy in first antenna %d\n",
|
||||
nsamps,
|
||||
*ptimestamp, t->nextRxTstamp,
|
||||
signal_energy(samplesVoid[0], nsamps));
|
||||
return nsamps;
|
||||
}
|
||||
|
||||
static int rfsimulator_get_stats(openair0_device *device) {
|
||||
return 0;
|
||||
}
|
||||
static int rfsimulator_reset_stats(openair0_device *device) {
|
||||
return 0;
|
||||
}
|
||||
static void rfsimulator_end(openair0_device *device) {
|
||||
rfsimulator_state_t* s = device->priv;
|
||||
for (int i = 0; i < FD_SETSIZE; i++) {
|
||||
buffer_t *b = &s->buf[i];
|
||||
if (b->conn_sock >= 0 )
|
||||
close(b->conn_sock);
|
||||
}
|
||||
close(s->epollfd);
|
||||
}
|
||||
static int rfsimulator_stop(openair0_device *device) {
|
||||
return 0;
|
||||
}
|
||||
static int rfsimulator_set_freq(openair0_device *device, openair0_config_t *openair0_cfg) {
|
||||
return 0;
|
||||
}
|
||||
static int rfsimulator_set_gains(openair0_device *device, openair0_config_t *openair0_cfg) {
|
||||
return 0;
|
||||
}
|
||||
static int rfsimulator_write_init(openair0_device *device) {
|
||||
return 0;
|
||||
}
|
||||
__attribute__((__visibility__("default")))
|
||||
int device_init(openair0_device *device, openair0_config_t *openair0_cfg) {
|
||||
// to change the log level, use this on command line
|
||||
// --log_config.hw_log_level debug
|
||||
rfsimulator_state_t *rfsimulator = (rfsimulator_state_t *)calloc(sizeof(rfsimulator_state_t),1);
|
||||
// initialize channel simulation
|
||||
rfsimulator->tx_num_channels=openair0_cfg->tx_num_channels;
|
||||
rfsimulator->rx_num_channels=openair0_cfg->rx_num_channels;
|
||||
rfsimulator->sample_rate=openair0_cfg->sample_rate;
|
||||
rfsimulator->tx_bw=openair0_cfg->tx_bw;
|
||||
rfsimulator_readconfig(rfsimulator);
|
||||
LOG_W(HW, "rfsim: sample_rate %f\n", rfsimulator->sample_rate);
|
||||
pthread_mutex_init(&Sockmutex, NULL);
|
||||
LOG_I(HW,"rfsimulator: running as %s\n", rfsimulator-> typeStamp == ENB_MAGICDL ? "server waiting opposite rfsimulators to connect" : "client: will connect to a rfsimulator server side");
|
||||
device->trx_start_func = rfsimulator->typeStamp == ENB_MAGICDL ?
|
||||
startServer :
|
||||
startClient;
|
||||
device->trx_get_stats_func = rfsimulator_get_stats;
|
||||
device->trx_reset_stats_func = rfsimulator_reset_stats;
|
||||
device->trx_end_func = rfsimulator_end;
|
||||
device->trx_stop_func = rfsimulator_stop;
|
||||
device->trx_set_freq_func = rfsimulator_set_freq;
|
||||
device->trx_set_gains_func = rfsimulator_set_gains;
|
||||
device->trx_write_func = rfsimulator_write;
|
||||
device->trx_read_func = rfsimulator_read;
|
||||
/* let's pretend to be a b2x0 */
|
||||
device->type = RFSIMULATOR;
|
||||
openair0_cfg[0].rx_gain[0] = 0;
|
||||
device->openair0_cfg=&openair0_cfg[0];
|
||||
device->priv = rfsimulator;
|
||||
device->trx_write_init = rfsimulator_write_init;
|
||||
|
||||
for (int i=0; i<FD_SETSIZE; i++)
|
||||
rfsimulator->buf[i].conn_sock=-1;
|
||||
|
||||
AssertFatal((rfsimulator->epollfd = epoll_create1(0)) != -1,"");
|
||||
|
||||
// we need to call randominit() for telnet server (use gaussdouble=>uniformrand)
|
||||
randominit(0);
|
||||
set_taus_seed(0);
|
||||
/* look for telnet server, if it is loaded, add the channel modeling commands to it */
|
||||
add_telnetcmd_func_t addcmd = (add_telnetcmd_func_t)get_shlibmodule_fptr("telnetsrv", TELNET_ADDCMD_FNAME);
|
||||
|
||||
if (addcmd != NULL) {
|
||||
rfsimulator->poll_telnetcmdq = (poll_telnetcmdq_func_t)get_shlibmodule_fptr("telnetsrv", TELNET_POLLCMDQ_FNAME);
|
||||
addcmd("rfsimu",rfsimu_vardef,rfsimu_cmdarray);
|
||||
|
||||
for(int i=0; rfsimu_cmdarray[i].cmdfunc != NULL; i++) {
|
||||
if ( rfsimu_cmdarray[i].qptr != NULL) {
|
||||
rfsimulator->telnetcmd_qid = rfsimu_cmdarray[i].qptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The OpenAirInterface Software Alliance licenses this file to You under
|
||||
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.openairinterface.org/?page_id=698
|
||||
*
|
||||
* Author and copyright: Laurent Thomas, open-cells.com
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*-------------------------------------------------------------------------------
|
||||
* For more information about the OpenAirInterface (OAI) Software Alliance:
|
||||
* contact@openairinterface.org
|
||||
*/
|
||||
|
||||
|
||||
#include <common/utils/simple_executable.h>
|
||||
|
||||
volatile int oai_exit = 0;
|
||||
|
||||
int fullread(int fd, void *_buf, int count) {
|
||||
char *buf = _buf;
|
||||
int ret = 0;
|
||||
int l;
|
||||
|
||||
while (count) {
|
||||
l = read(fd, buf, count);
|
||||
|
||||
if (l <= 0)
|
||||
return -1;
|
||||
|
||||
count -= l;
|
||||
buf += l;
|
||||
ret += l;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#define shift 4
|
||||
int32_t signal_energy(int32_t *input,uint32_t length) {
|
||||
int32_t i;
|
||||
int32_t temp,temp2;
|
||||
register __m64 mm0,mm1,mm2,mm3;
|
||||
__m64 *in = (__m64 *)input;
|
||||
mm0 = _mm_setzero_si64();//pxor(mm0,mm0);
|
||||
mm3 = _mm_setzero_si64();//pxor(mm3,mm3);
|
||||
|
||||
for (i=0; i<length>>1; i++) {
|
||||
mm1 = in[i];
|
||||
mm2 = mm1;
|
||||
mm1 = _m_pmaddwd(mm1,mm1);
|
||||
mm1 = _m_psradi(mm1,shift);// shift any 32 bits blocs of the word by the value shift
|
||||
mm0 = _m_paddd(mm0,mm1);// add the two 64 bits words 4 bytes by 4 bytes
|
||||
// mm2 = _m_psrawi(mm2,shift_DC);
|
||||
mm3 = _m_paddw(mm3,mm2);// add the two 64 bits words 2 bytes by 2 bytes
|
||||
}
|
||||
|
||||
mm1 = mm0;
|
||||
mm0 = _m_psrlqi(mm0,32);
|
||||
mm0 = _m_paddd(mm0,mm1);
|
||||
temp = _m_to_int(mm0);
|
||||
temp/=length;
|
||||
temp<<=shift; // this is the average of x^2
|
||||
// now remove the DC component
|
||||
mm2 = _m_psrlqi(mm3,32);
|
||||
mm2 = _m_paddw(mm2,mm3);
|
||||
mm2 = _m_pmaddwd(mm2,mm2);
|
||||
temp2 = _m_to_int(mm2);
|
||||
temp2/=(length*length);
|
||||
// temp2<<=(2*shift_DC);
|
||||
temp -= temp2;
|
||||
_mm_empty();
|
||||
_m_empty();
|
||||
return((temp>0)?temp:1);
|
||||
}
|
||||
|
||||
|
||||
void fullwrite(int fd, void *_buf, int count) {
|
||||
char *buf = _buf;
|
||||
int l;
|
||||
|
||||
while (count) {
|
||||
l = write(fd, buf, count);
|
||||
|
||||
if (l <= 0) {
|
||||
if (errno==EINTR)
|
||||
continue;
|
||||
|
||||
if(errno==EAGAIN) {
|
||||
continue;
|
||||
} else {
|
||||
AssertFatal(false,"Lost socket\n");
|
||||
}
|
||||
} else {
|
||||
count -= l;
|
||||
buf += l;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int server_start(short port) {
|
||||
int listen_sock;
|
||||
AssertFatal((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0, "");
|
||||
int enable = 1;
|
||||
AssertFatal(setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == 0, "");
|
||||
struct sockaddr_in addr = {
|
||||
sin_family:
|
||||
AF_INET,
|
||||
sin_port:
|
||||
htons(port),
|
||||
sin_addr:
|
||||
{ s_addr: INADDR_ANY }
|
||||
};
|
||||
bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr));
|
||||
AssertFatal(listen(listen_sock, 5) == 0, "");
|
||||
return accept(listen_sock,NULL,NULL);
|
||||
}
|
||||
|
||||
int client_start(char *IP, short port) {
|
||||
int sock;
|
||||
AssertFatal((sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0, "");
|
||||
struct sockaddr_in addr = {
|
||||
sin_family:
|
||||
AF_INET,
|
||||
sin_port:
|
||||
htons(port),
|
||||
sin_addr:
|
||||
{ s_addr: INADDR_ANY }
|
||||
};
|
||||
addr.sin_addr.s_addr = inet_addr(IP);
|
||||
bool connected=false;
|
||||
|
||||
while(!connected) {
|
||||
//LOG_I(HW,"rfsimulator: trying to connect to %s:%d\n", IP, port);
|
||||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
//LOG_I(HW,"rfsimulator: connection established\n");
|
||||
connected=true;
|
||||
}
|
||||
|
||||
perror("simulated node");
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
enum blocking_t {
|
||||
notBlocking,
|
||||
blocking
|
||||
};
|
||||
|
||||
void setblocking(int sock, enum blocking_t active) {
|
||||
int opts;
|
||||
AssertFatal( (opts = fcntl(sock, F_GETFL)) >= 0,"");
|
||||
|
||||
if (active==blocking)
|
||||
opts = opts & ~O_NONBLOCK;
|
||||
else
|
||||
opts = opts | O_NONBLOCK;
|
||||
|
||||
AssertFatal(fcntl(sock, F_SETFL, opts) >= 0, "");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if(argc < 4) {
|
||||
printf("Need parameters: source file, server or destination IP, TCP port (4043), 'UL|DL' if raw 2*16bits format: UL for UL IQ, DL for DL IQs \n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int fd;
|
||||
AssertFatal((fd=open(argv[1],O_RDONLY)) != -1, "file: %s", argv[1]);
|
||||
off_t fileSize=lseek(fd, 0, SEEK_END);
|
||||
int serviceSock;
|
||||
|
||||
if (strcmp(argv[2],"server")==0) {
|
||||
serviceSock=server_start(atoi(argv[3]));
|
||||
} else {
|
||||
serviceSock=client_start(argv[2],atoi(argv[3]));
|
||||
}
|
||||
|
||||
uint64_t typeStamp=ENB_MAGICDL;
|
||||
bool raw = false;
|
||||
|
||||
if ( argc == 5 ) {
|
||||
raw=true;
|
||||
|
||||
if (strcmp(argv[4],"UL") == 0 )
|
||||
typeStamp=UE_MAGICDL;
|
||||
}
|
||||
|
||||
samplesBlockHeader_t header;
|
||||
int bufSize=100000;
|
||||
void *buff=malloc(bufSize);
|
||||
uint64_t timestamp=0;
|
||||
const int blockSize=1920;
|
||||
// If fileSize is not multiple of blockSize*4 then discard remaining samples
|
||||
fileSize = (fileSize/(blockSize<<2))*(blockSize<<2);
|
||||
|
||||
while (1) {
|
||||
//Rewind the file to loop on the samples
|
||||
if ( lseek(fd, 0, SEEK_CUR) >= fileSize )
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
|
||||
// Read one block and send it
|
||||
setblocking(serviceSock, blocking);
|
||||
|
||||
if ( raw ) {
|
||||
header.magic=typeStamp;
|
||||
header.size=blockSize;
|
||||
header.nbAnt=1;
|
||||
header.timestamp=timestamp;
|
||||
timestamp+=blockSize;
|
||||
header.option_value=0;
|
||||
header.option_flag=0;
|
||||
} else {
|
||||
AssertFatal(read(fd,&header,sizeof(header)), "");
|
||||
}
|
||||
|
||||
fullwrite(serviceSock, &header, sizeof(header));
|
||||
int dataSize=sizeof(int32_t)*header.size*header.nbAnt;
|
||||
|
||||
if (dataSize>bufSize) {
|
||||
void *new_buff = realloc(buff, dataSize);
|
||||
|
||||
if (new_buff == NULL) {
|
||||
free(buff);
|
||||
AssertFatal(1, "Could not reallocate");
|
||||
} else {
|
||||
buff = new_buff;
|
||||
}
|
||||
}
|
||||
|
||||
AssertFatal(read(fd,buff,dataSize) == dataSize, "");
|
||||
|
||||
if (raw) // UHD shifts the 12 ADC values in MSB
|
||||
for (int i=0; i<header.size*header.nbAnt*2; i++)
|
||||
((int16_t *)buff)[i]/=16;
|
||||
|
||||
usleep(1000);
|
||||
printf("sending at ts: %lu, number of samples: %d, energy: %d\n",
|
||||
header.timestamp, header.size, signal_energy(buff, header.size));
|
||||
fullwrite(serviceSock, buff, dataSize);
|
||||
// Purge incoming samples
|
||||
setblocking(serviceSock, notBlocking);
|
||||
int ret;
|
||||
|
||||
do {
|
||||
char buff[64000];
|
||||
ret=read(serviceSock, buff, 64000);
|
||||
|
||||
if ( ret<0 && !( errno == EAGAIN || errno == EWOULDBLOCK ) ) {
|
||||
printf("error: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
} while ( ret > 0 ) ;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user