simulator.c 30.7 KB
Newer Older
1
/*
laurent's avatar
laurent committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
* 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
22 23
*/

laurent's avatar
laurent committed
24

laurent's avatar
laurent committed
25 26 27 28 29 30
/*
 * 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
 */

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#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>
46
#include <common/utils/load_module_shlib.h>
frtabu's avatar
frtabu committed
47
#include <common/utils/telnetsrv/telnetsrv.h>
48
#include <common/config/config_userapi.h>
49 50 51
#include "common_lib.h"
#include <openair1/PHY/defs_eNB.h>
#include "openair1/PHY/defs_UE.h"
52
#define CHANNELMOD_DYNAMICLOAD
53
#include <openair1/SIMULATION/TOOLS/sim.h>
laurent's avatar
laurent committed
54
#include <targets/ARCH/rfsimulator/rfsimulator.h>
55

56
#define PORT 4043 //default TCP port for this simulator
57
#define CirSize 6144000 // 100ms is enough
58 59 60
#define sampleToByte(a,b) ((a)*(b)*sizeof(sample_t))
#define byteToSample(a,b) ((a)/(sizeof(sample_t)*(b)))

61 62 63
#define MAX_SIMULATION_CONNECTED_NODES 5
#define GENERATE_CHANNEL 10 //each frame in DL

64

65
//
66

67 68
#define RFSIMU_SECTION    "rfsimulator"
#define RFSIMU_OPTIONS_PARAMNAME "options"
69 70 71


#define RFSIM_CONFIG_HELP_OPTIONS     " list of comma separated options to enable rf simulator functionalities. Available options: \n"\
72 73
  "        chanmod:   enable channel modelisation\n"\
  "        saviq:     enable saving written iqs to a file\n"
74 75 76 77
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/*                                            configuration parameters for the rfsimulator device                                                                              */
/*   optname                     helpstr                     paramflags           XXXptr                               defXXXval                          type         numelt  */
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
Laurent Thomas's avatar
Laurent Thomas committed
78 79
#define simOpt PARAMFLAG_NOFREE|PARAMFLAG_CMDLINE_NOPREFIXENABLED
#define RFSIMULATOR_PARAMS_DESC {					\
80 81 82 83 84 85 86 87
    {"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 }\
88
  };
89

frtabu's avatar
frtabu committed
90 91


92
static int rfsimu_setchanmod_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg);
frtabu's avatar
frtabu committed
93
static telnetshell_cmddef_t rfsimu_cmdarray[] = {
94
  {"setmodel","<model name> <model type>",(cmdfunc_t)rfsimu_setchanmod_cmd,TELNETSRV_CMDFLAG_PUSHINTPOOLQ},
frtabu's avatar
frtabu committed
95 96 97 98 99
  {"","",NULL},
};

static telnetshell_vardef_t rfsimu_vardef[] = {
  {"",0,NULL}
100
};
101
pthread_mutex_t Sockmutex;
102

103
typedef struct complex16 sample_t; // 2*16 bits complex number
104

105 106
typedef struct buffer_s {
  int conn_sock;
laurent's avatar
laurent committed
107
  openair0_timestamp lastReceivedTS;
108
  bool headerMode;
109
  bool trashingPacket;
110
  samplesBlockHeader_t th;
111 112 113 114
  char *transferPtr;
  uint64_t remainToTransfer;
  char *circularBufEnd;
  sample_t *circularBuf;
115
  channel_desc_t *channel_model;
116 117 118 119
} buffer_t;

typedef struct {
  int listen_sock, epollfd;
laurent's avatar
laurent committed
120
  openair0_timestamp nextTimestamp;
121
  openair0_timestamp lastWroteTS;
122 123
  uint64_t typeStamp;
  char *ip;
124
  uint16_t port;
125
  int saveIQfile;
126
  buffer_t buf[FD_SETSIZE];
127 128 129 130
  int rx_num_channels;
  int tx_num_channels;
  double sample_rate;
  double tx_bw;
131
  int channelmod;
132 133
  double chan_pathloss;
  double chan_forgetfact;
134
  int    chan_offset;
Laurent THOMAS's avatar
Laurent THOMAS committed
135
  float  noise_power_dB;
frtabu's avatar
frtabu committed
136 137
  void *telnetcmd_qid;
  poll_telnetcmdq_func_t poll_telnetcmdq;
138 139
} rfsimulator_state_t;

140

Laurent Thomas's avatar
Laurent Thomas committed
141
static void allocCirBuf(rfsimulator_state_t *bridge, int sock) {
142 143 144 145
  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;
146
  ptr->lastReceivedTS=0;
147
  ptr->headerMode=true;
148
  ptr->trashingPacket=false;
149
  ptr->transferPtr=(char *)&ptr->th;
150
  ptr->remainToTransfer=sizeof(samplesBlockHeader_t);
151
  int sendbuff=1000*1000*100;
152 153 154 155 156
  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, "");
157

158
  if ( bridge->channelmod > 0) {
159 160 161 162 163 164 165 166 167 168 169 170
    // 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
laurent's avatar
laurent committed
171
    static bool init_done=false;
laurent's avatar
laurent committed
172

laurent's avatar
laurent committed
173
    if (!init_done) {
174 175 176
      uint64_t rand;
      FILE *h=fopen("/dev/random","r");

177 178
      if ( 1 != fread(&rand,sizeof(rand),1,h) )
        LOG_W(HW, "Simulator can't read /dev/random\n");
179 180

      fclose(h);
laurent's avatar
laurent committed
181
      randominit(rand);
laurent's avatar
laurent committed
182
      tableNor(rand);
laurent's avatar
laurent committed
183 184
      init_done=true;
    }
185 186 187
    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);
188
    set_channeldesc_owner(ptr->channel_model, RFSIMU_MODULEID);
189 190
    random_channel(ptr->channel_model,false);
  }
191 192
}

Laurent Thomas's avatar
Laurent Thomas committed
193
static void removeCirBuf(rfsimulator_state_t *bridge, int sock) {
194 195 196
  AssertFatal( epoll_ctl(bridge->epollfd, EPOLL_CTL_DEL,  sock, NULL) != -1, "");
  close(sock);
  free(bridge->buf[sock].circularBuf);
197 198 199
  // Fixme: no free_channel_desc_scm(bridge->buf[sock].channel_model) implemented
  // a lot of mem leaks
  free(bridge->buf[sock].channel_model);
200 201 202 203
  memset(&bridge->buf[sock], 0, sizeof(buffer_t));
  bridge->buf[sock].conn_sock=-1;
}

Laurent Thomas's avatar
Laurent Thomas committed
204
static void socketError(rfsimulator_state_t *bridge, int sock) {
205 206 207 208
  if (bridge->buf[sock].conn_sock!=-1) {
    LOG_W(HW,"Lost socket \n");
    removeCirBuf(bridge, sock);

Laurent Thomas's avatar
Laurent Thomas committed
209
    if (bridge->typeStamp==UE_MAGICDL)
210 211 212 213 214 215 216 217 218
      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\
219
                 or use rfsimulator.serveraddr configuration option\n\
220 221 222 223 224 225 226
\x1b[m"

enum  blocking_t {
  notBlocking,
  blocking
};

Laurent Thomas's avatar
Laurent Thomas committed
227
static void setblocking(int sock, enum blocking_t active) {
228 229 230 231 232 233 234 235 236 237 238
  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, "");
}

239
static bool flushInput(rfsimulator_state_t *t, int timeout, int nsamps);
240

Laurent Thomas's avatar
Laurent Thomas committed
241
static void fullwrite(int fd, void *_buf, ssize_t count, rfsimulator_state_t *t) {
242 243 244 245
  if (t->saveIQfile != -1) {
    if (write(t->saveIQfile, _buf, count) != count )
      LOG_E(HW,"write in save iq file failed (%s)\n",strerror(errno));
  }
246

247 248
  AssertFatal(fd>=0 && _buf && count >0 && t,
              "Bug: %d/%p/%zd/%p", fd, _buf, count, t);
249
  char *buf = _buf;
250
  ssize_t l;
251 252 253 254 255 256 257 258 259 260
  setblocking(fd, notBlocking);

  while (count) {
    l = write(fd, buf, count);

    if (l <= 0) {
      if (errno==EINTR)
        continue;

      if(errno==EAGAIN) {
261 262
        // The opposite side is saturated
        // we read incoming sockets meawhile waiting
263 264
        //flushInput(t, 5);
        usleep(500);
265 266 267 268 269 270 271 272 273
        continue;
      } else
        return;
    }

    count -= l;
    buf += l;
  }
}
274

Laurent Thomas's avatar
Laurent Thomas committed
275
static void rfsimulator_readconfig(rfsimulator_state_t *rfsimulator) {
276
  char *saveF=NULL;
277
  char *modelname=NULL;
278
  paramdef_t rfsimu_params[] = RFSIMULATOR_PARAMS_DESC;
279 280 281
  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");
282 283
  rfsimulator->saveIQfile = -1;

284
  for(int i=0; i<rfsimu_params[p].numelt ; i++) {
285 286 287 288 289 290 291 292 293 294
    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) {
295
      init_channelmod();
296 297
      load_channellist(rfsimulator->tx_num_channels, rfsimulator->rx_num_channels, rfsimulator->sample_rate, rfsimulator->tx_bw);
      rfsimulator->channelmod=true;
298 299 300 301
    } else {
      fprintf(stderr,"Unknown rfsimulator option: %s\n",rfsimu_params[p].strlistptr[i]);
      exit(-1);
    }
302
  }
303

304 305
  /* for compatibility keep environment variable usage */
  if ( getenv("RFSIMULATOR") != NULL ) {
306
    rfsimulator->ip=getenv("RFSIMULATOR");
307
  }
308

309
  if ( strncasecmp(rfsimulator->ip,"enb",3) == 0 ||
310
       strncasecmp(rfsimulator->ip,"server",3) == 0 )
Laurent Thomas's avatar
Laurent Thomas committed
311
    rfsimulator->typeStamp = ENB_MAGICDL;
312
  else
Laurent Thomas's avatar
Laurent Thomas committed
313
    rfsimulator->typeStamp = UE_MAGICDL;
314
}
315

316
static int rfsimu_setchanmod_cmd(char *buff, int debug, telnet_printfunc_t prnt, void *arg) {
317
  char *modelname=NULL;
318 319 320 321
  char *modeltype=NULL;
  if (debug)
  	  prnt("rfsimu_setchanmod_cmd buffer \"%s\"\n",buff);
  int s = sscanf(buff,"%m[^ ] %ms\n",&modelname, &modeltype);
322

323 324
  if (s == 2) {
    int channelmod=modelid_fromstrtype(modeltype);
325

326
    if (channelmod<0)
327
      prnt("ERROR: model type %s unknown\n",modeltype);
328
    else {
329
      rfsimulator_state_t *t = (rfsimulator_state_t *)arg;
330
      int found=0;
331
      for (int i=0; i<FD_SETSIZE; i++) {
332
        buffer_t *b=&t->buf[i];
333 334 335 336 337
        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)) {
338

339
          channel_desc_t *newmodel=new_channel_desc_scm(t->tx_num_channels,t->rx_num_channels,
340 341 342 343 344 345
                                   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
346 347 348
							       t->chan_pathloss,
							       t->noise_power_dB
							       ); // path_loss in dB
349
          set_channeldesc_owner(newmodel, RFSIMU_MODULEID);
350
          set_channeldesc_name(newmodel,modelname);
351 352 353 354
          random_channel(newmodel,false);
          channel_desc_t *oldmodel=b->channel_model;
          b->channel_model=newmodel;
          free_channel_desc_scm(oldmodel);
355 356 357
          prnt("New model type %s applied to channel %s connected to sock %d\n",modeltype,modelname,i);
          found=1;
          break;
358
        }
359 360 361
      } /* for */
      if (found==0)
      	prnt("Channel %s not found or not currently used\n",modelname); 
362 363
    }
  } else {
364
    prnt("ERROR: 2 parameters required: model name and model type (%i found)\n",s);
frtabu's avatar
frtabu committed
365
  }
366

367
  free(modelname);
368
  free(modeltype);
frtabu's avatar
frtabu committed
369 370 371
  return CMDSTATUS_FOUND;
}

Laurent Thomas's avatar
Laurent Thomas committed
372
static int startServer(openair0_device *device) {
373
  rfsimulator_state_t *t = (rfsimulator_state_t *) device->priv;
Laurent Thomas's avatar
Laurent Thomas committed
374
  t->typeStamp=ENB_MAGICDL;
375 376 377 378 379 380 381
  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:
382
    htons(t->port),
383 384 385 386 387
sin_addr:
    { s_addr: INADDR_ANY }
  };
  bind(t->listen_sock, (struct sockaddr *)&addr, sizeof(addr));
  AssertFatal(listen(t->listen_sock, 5) == 0, "");
388
  struct epoll_event ev= {0};
389 390 391 392 393 394
  ev.events = EPOLLIN;
  ev.data.fd = t->listen_sock;
  AssertFatal(epoll_ctl(t->epollfd, EPOLL_CTL_ADD,  t->listen_sock, &ev) != -1, "");
  return 0;
}

Laurent Thomas's avatar
Laurent Thomas committed
395
static int startClient(openair0_device *device) {
396
  rfsimulator_state_t *t = device->priv;
Laurent Thomas's avatar
Laurent Thomas committed
397
  t->typeStamp=UE_MAGICDL;
398 399 400 401 402 403
  int sock;
  AssertFatal((sock = socket(AF_INET, SOCK_STREAM, 0)) >= 0, "");
  struct sockaddr_in addr = {
sin_family:
    AF_INET,
sin_port:
404
    htons(t->port),
405 406 407 408 409 410 411
sin_addr:
    { s_addr: INADDR_ANY }
  };
  addr.sin_addr.s_addr = inet_addr(t->ip);
  bool connected=false;

  while(!connected) {
412
    LOG_I(HW,"rfsimulator: trying to connect to %s:%d\n", t->ip, t->port);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

    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;
}

428 429 430 431
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);

432
  LOG_D(HW,"sending %d samples at time: %ld\n", nsamps, timestamp);
433 434

  for (int i=0; i<FD_SETSIZE; i++) {
laurent's avatar
laurent committed
435
    buffer_t *b=&t->buf[i];
436

laurent's avatar
laurent committed
437
    if (b->conn_sock >= 0 ) {
438
      samplesBlockHeader_t header= {t->typeStamp, nsamps, nbAnt, timestamp};
laurent's avatar
laurent committed
439
      fullwrite(b->conn_sock,&header, sizeof(header), t);
440 441 442 443 444 445 446 447 448
      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];
      }

laurent's avatar
laurent committed
449 450 451
      if (b->conn_sock >= 0 ) {
        fullwrite(b->conn_sock, (void *)tmpSamples, sampleToByte(nsamps,nbAnt), t);
      }
452 453 454
    }
  }

455 456 457
  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);

laurent's avatar
laurent committed
458
  if (t->lastWroteTS > timestamp+nsamps)
459
    LOG_E(HW,"Not supported to send Tx out of order (same in USRP) %lu, %lu\n",
460
          t->lastWroteTS, timestamp);
laurent's avatar
laurent committed
461

462 463 464 465 466
  t->lastWroteTS=timestamp+nsamps;

  if (!alreadyLocked)
    pthread_mutex_unlock(&Sockmutex);

467 468 469 470 471
  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;
}

Laurent Thomas's avatar
Laurent Thomas committed
472
static int rfsimulator_write(openair0_device *device, openair0_timestamp timestamp, void **samplesVoid, int nsamps, int nbAnt, int flags) {
473
  return rfsimulator_write_internal(device->priv, timestamp, samplesVoid, nsamps, nbAnt, flags, false);
474 475 476
}

static bool flushInput(rfsimulator_state_t *t, int timeout, int nsamps_for_initial) {
477 478
  // Process all incoming events on sockets
  // store the data in lists
479
  struct epoll_event events[FD_SETSIZE]= {{0}};
480
  int nfds = epoll_wait(t->epollfd, events, FD_SETSIZE, timeout);
481 482

  if ( nfds==-1 ) {
483
    if ( errno==EINTR || errno==EAGAIN ) {
484
      return false;
485
    } else
486 487 488 489 490 491 492 493 494 495 496
      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);
Laurent Thomas's avatar
Laurent Thomas committed
497
      LOG_I(HW,"A client connected, sending the current time\n");
498 499 500 501 502 503
      struct complex16 v= {0};
      void *samplesVoid[t->tx_num_channels];

      for ( int i=0; i < t->tx_num_channels; i++)
        samplesVoid[i]=(void *)&v;

504
      rfsimulator_write_internal(t, t->lastWroteTS > 1 ? t->lastWroteTS-1 : 0,
505
                                 samplesVoid, 1,
506
                                 t->tx_num_channels, 1, false);
507 508 509 510 511 512 513 514 515 516 517 518 519
    } 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;
      }

520
      ssize_t blockSz;
521 522 523 524

      if ( b->headerMode)
        blockSz=b->remainToTransfer;
      else
525
        blockSz= b->transferPtr + b->remainToTransfer <= b->circularBufEnd ?
526
                 b->remainToTransfer :
527
                 b->circularBufEnd - b->transferPtr ;
528

529
      ssize_t sz=recv(fd, b->transferPtr, blockSz, MSG_DONTWAIT);
530 531 532 533

      if ( sz < 0 ) {
        if ( errno != EAGAIN ) {
          LOG_E(HW,"socket failed %s\n", strerror(errno));
534
          //abort();
535 536 537 538
        }
      } else if ( sz == 0 )
        continue;

539
      LOG_D(HW, "Socket rcv %zd bytes\n", sz);
540 541 542
      AssertFatal((b->remainToTransfer-=sz) >= 0, "");
      b->transferPtr+=sz;

543
      if (b->transferPtr==b->circularBufEnd )
544 545 546 547
        b->transferPtr=(char *)b->circularBuf;

      // check the header and start block transfer
      if ( b->headerMode==true && b->remainToTransfer==0) {
Laurent Thomas's avatar
Laurent Thomas committed
548 549
        AssertFatal( (t->typeStamp == UE_MAGICDL  && b->th.magic==ENB_MAGICDL) ||
                     (t->typeStamp == ENB_MAGICDL && b->th.magic==UE_MAGICDL), "Socket Error in protocol");
550 551
        b->headerMode=false;

552
        if ( t->nextTimestamp == 0 ) { // First block in UE, resync with the eNB current TS
553 554 555 556 557 558 559 560 561
          t->nextTimestamp=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->nextTimestamp);
          b->trashingPacket=true;
        } else if ( b->lastReceivedTS < b->th.timestamp) {
562
          int nbAnt= b->th.nbAnt;
laurent's avatar
laurent committed
563

564
          if ( b->th.timestamp-b->lastReceivedTS < CirSize ) {
laurent's avatar
laurent committed
565 566 567 568 569
            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;
              }
570
            }
571
          } else {
572 573
            memset(b->circularBuf, 0, sampleToByte(CirSize,1));
          }
laurent's avatar
laurent committed
574

575
          if (b->lastReceivedTS != 0 && b->th.timestamp-b->lastReceivedTS < 1000)
576
            LOG_W(HW,"UEsock: %d gap of: %ld in reception\n", fd, b->th.timestamp-b->lastReceivedTS );
577

578
          b->lastReceivedTS=b->th.timestamp;
579
        } else if ( b->lastReceivedTS > b->th.timestamp && b->th.size == 1 ) {
580 581 582 583 584
          LOG_W(HW,"Received Rx/Tx synchro out of order\n");
          b->trashingPacket=true;
        } else if ( b->lastReceivedTS == b->th.timestamp ) {
          // normal case
        } else {
laurent's avatar
laurent committed
585
          LOG_E(HW, "received data in past: current is %lu, new reception: %lu!\n", b->lastReceivedTS, b->th.timestamp);
586
          b->trashingPacket=true;
laurent's avatar
laurent committed
587
        }
588

589 590
        pthread_mutex_lock(&Sockmutex);

591 592
        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);
593 594

        pthread_mutex_unlock(&Sockmutex);
595
        b->transferPtr=(char *)&b->circularBuf[(b->lastReceivedTS*b->th.nbAnt)%CirSize];
596 597 598 599
        b->remainToTransfer=sampleToByte(b->th.size, b->th.nbAnt);
      }

      if ( b->headerMode==false ) {
600 601
        if ( ! b->trashingPacket ) {
          b->lastReceivedTS=b->th.timestamp+b->th.size-byteToSample(b->remainToTransfer,b->th.nbAnt);
602
          LOG_D(HW,"UEsock: %d Set b->lastReceivedTS %ld\n", fd, b->lastReceivedTS);
603
        }
604

laurent's avatar
laurent committed
605
        if ( b->remainToTransfer==0) {
606
          LOG_D(HW,"UEsock: %d Completed block reception: %ld\n", fd, b->lastReceivedTS);
laurent's avatar
laurent committed
607 608
          b->headerMode=true;
          b->transferPtr=(char *)&b->th;
609
          b->remainToTransfer=sizeof(samplesBlockHeader_t);
laurent's avatar
laurent committed
610
          b->th.magic=-1;
611
          b->trashingPacket=false;
612 613 614 615 616 617 618 619
        }
      }
    }
  }

  return nfds>0;
}

Laurent Thomas's avatar
Laurent Thomas committed
620
static int rfsimulator_read(openair0_device *device, openair0_timestamp *ptimestamp, void **samplesVoid, int nsamps, int nbAnt) {
621 622
  if (nbAnt > 4) {
    LOG_W(HW, "rfsimulator: only 4 antenna tested\n");
623 624 625 626 627 628 629 630 631 632 633 634 635 636
  }

  rfsimulator_state_t *t = device->priv;
  LOG_D(HW, "Enter rfsimulator_read, expect %d samples, will release at TS: %ld\n", nsamps, t->nextTimestamp+nsamps);
  // 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)
637 638
    if ( t->nextTimestamp == 0)
      LOG_W(HW,"No connected device, generating void samples...\n");
639

640
    if (!flushInput(t, 10,  nsamps)) {
641 642 643 644
      for (int x=0; x < nbAnt; x++)
        memset(samplesVoid[x],0,sampleToByte(nsamps,1));

      t->nextTimestamp+=nsamps;
645

646
      if ( ((t->nextTimestamp/nsamps)%100) == 0)
647
        LOG_D(HW,"No UE, Generated void samples for Rx: %ld\n", t->nextTimestamp);
648

649 650 651 652
      *ptimestamp = t->nextTimestamp-nsamps;
      return nsamps;
    }
  } else {
653 654 655 656 657 658
    pthread_mutex_lock(&Sockmutex);

    if ( t->nextTimestamp > 0 && t->lastWroteTS < t->nextTimestamp) {
      pthread_mutex_unlock(&Sockmutex);
      usleep(10000);
      pthread_mutex_lock(&Sockmutex);
laurent's avatar
laurent committed
659

660 661 662 663 664
      if ( t->lastWroteTS < t->nextTimestamp ) {
        // Assuming Tx is not done fully in another thread
        // We can never write is the past from the received time
        // So, the node perform receive but will never write these symbols
        // let's tell this to the opposite node
665 666
        // We send timestamp for nb samples required
        // assuming this should have been done earlier if a Tx would exist
667 668
        pthread_mutex_unlock(&Sockmutex);
        struct complex16 v= {0};
669
        void *dummyS[t->tx_num_channels];
670 671

        for ( int i=0; i < t->tx_num_channels; i++)
672
          dummyS[i]=(void *)&v;
laurent's avatar
laurent committed
673

674 675
        LOG_I(HW, "No samples Tx occured, so we send 1 sample to notify it: Tx:%lu, Rx:%lu\n",
              t->lastWroteTS, t->nextTimestamp);
676
        rfsimulator_write_internal(t, t->nextTimestamp,
677
                                   dummyS, 1,
678 679
                                   t->tx_num_channels, 1, true);
      } else {
680
        pthread_mutex_unlock(&Sockmutex);
681 682 683 684
        LOG_W(HW, "trx_write came from another thread\n");
      }
    } else
      pthread_mutex_unlock(&Sockmutex);
685

686 687 688 689 690
    bool have_to_wait;

    do {
      have_to_wait=false;

691
      for ( int sock=0; sock<FD_SETSIZE; sock++) {
692 693
        buffer_t *b=&t->buf[sock];

laurent's avatar
laurent committed
694 695
        if ( b->circularBuf )
          if ( t->nextTimestamp+nsamps > b->lastReceivedTS ) {
laurent's avatar
laurent committed
696 697
            have_to_wait=true;
            break;
698
          }
laurent's avatar
laurent committed
699
      }
700 701 702 703 704 705

      if (have_to_wait)
        /*printf("Waiting on socket, current last ts: %ld, expected at least : %ld\n",
          ptr->lastReceivedTS,
          t->nextTimestamp+nsamps);
        */
706
        flushInput(t, 3, nsamps);
707 708 709 710 711 712 713
    } while (have_to_wait);
  }

  // Clear the output buffer
  for (int a=0; a<nbAnt; a++)
    memset(samplesVoid[a],0,sampleToByte(nsamps,1));

laurent's avatar
laurent committed
714
  // Add all input nodes signal in the output buffer
715 716 717
  for (int sock=0; sock<FD_SETSIZE; sock++) {
    buffer_t *ptr=&t->buf[sock];

laurent's avatar
laurent committed
718
    if ( ptr->circularBuf ) {
719 720 721 722 723 724
      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);
725

726
      if (t->poll_telnetcmdq)
727
        t->poll_telnetcmdq(t->telnetcmd_qid,t);
728

729
      for (int a=0; a<nbAnt; a++) {//loop over number of Rx antennas
730
        if ( ptr->channel_model != NULL ) // apply a channel model
731 732 733 734
          rxAddInput( ptr->circularBuf, (struct complex16 *) samplesVoid[a],
                      a,
                      ptr->channel_model,
                      nsamps,
735 736
                      t->nextTimestamp,
                      CirSize
737 738
                    );
        else { // no channel modeling
739 740 741 742 743
          double H_awgn_mimo[4][4] ={{1.0, 0.5, 0.25, 0.125},//rx 0
                                     {0.5, 1.0, 0.5, 0.25},  //rx 1
                                     {0.25, 0.5, 1.0, 0.5},  //rx 2
                                     {0.125, 0.25, 0.5, 1.0}};//rx 3

744
          sample_t *out=(sample_t *)samplesVoid[a];
745
          int nbAnt_tx = ptr->th.nbAnt;//number of Tx antennas
746

747 748
          //LOG_I(HW, "nbAnt_tx %d\n",nbAnt_tx);
          for (int i=0; i < nsamps; i++) {//loop over nsamps
749
            for (int a_tx=0; a_tx<nbAnt_tx; a_tx++) { //sum up signals from nbAnt_tx antennas
750 751
              out[i].r += (short)(ptr->circularBuf[((t->nextTimestamp+i)*nbAnt_tx+a_tx)%CirSize].r*H_awgn_mimo[a][a_tx]);
              out[i].i += (short)(ptr->circularBuf[((t->nextTimestamp+i)*nbAnt_tx+a_tx)%CirSize].i*H_awgn_mimo[a][a_tx]);
752
            } // end for a_tx
753
          } // end for i (number of samps)
754
        } // end of no channel modeling
755
      } // end for a (number of rx antennas)
756 757 758 759 760 761 762 763 764 765 766
    }
  }

  *ptimestamp = t->nextTimestamp; // return the time of the first sample
  t->nextTimestamp+=nsamps;
  LOG_D(HW,"Rx to upper layer: %d from %ld to %ld, energy in first antenna %d\n",
        nsamps,
        *ptimestamp, t->nextTimestamp,
        signal_energy(samplesVoid[0], nsamps));
  return nsamps;
}
Laurent Thomas's avatar
Laurent Thomas committed
767 768

static int rfsimulator_get_stats(openair0_device *device) {
769 770
  return 0;
}
Laurent Thomas's avatar
Laurent Thomas committed
771
static int rfsimulator_reset_stats(openair0_device *device) {
772 773
  return 0;
}
Laurent Thomas's avatar
Laurent Thomas committed
774 775
static void rfsimulator_end(openair0_device *device) {}
static int rfsimulator_stop(openair0_device *device) {
776 777
  return 0;
}
Laurent Thomas's avatar
Laurent Thomas committed
778
static int rfsimulator_set_freq(openair0_device *device, openair0_config_t *openair0_cfg,int exmimo_dump_config) {
779 780
  return 0;
}
Laurent Thomas's avatar
Laurent Thomas committed
781
static int rfsimulator_set_gains(openair0_device *device, openair0_config_t *openair0_cfg) {
782 783
  return 0;
}
Laurent Thomas's avatar
Laurent Thomas committed
784
static int rfsimulator_write_init(openair0_device *device) {
WANG Tsu-Han's avatar
WANG Tsu-Han committed
785 786
  return 0;
}
787 788
__attribute__((__visibility__("default")))
int device_init(openair0_device *device, openair0_config_t *openair0_cfg) {
789 790
  // to change the log level, use this on command line
  // --log_config.hw_log_level debug
791
  rfsimulator_state_t *rfsimulator = (rfsimulator_state_t *)calloc(sizeof(rfsimulator_state_t),1);
792 793 794 795 796
  // 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;  
797
  rfsimulator_readconfig(rfsimulator);
798
  pthread_mutex_init(&Sockmutex, NULL);
Laurent Thomas's avatar
Laurent Thomas committed
799 800 801 802
  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;
803 804 805 806 807 808 809 810 811
  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 */
Laurent Thomas's avatar
Laurent Thomas committed
812
  device->type = RFSIMULATOR;
francescomani's avatar
francescomani committed
813
  openair0_cfg[0].rx_gain[0] = 0;
814 815
  device->openair0_cfg=&openair0_cfg[0];
  device->priv = rfsimulator;
WANG Tsu-Han's avatar
WANG Tsu-Han committed
816
  device->trx_write_init = rfsimulator_write_init;
817 818 819 820 821

  for (int i=0; i<FD_SETSIZE; i++)
    rfsimulator->buf[i].conn_sock=-1;

  AssertFatal((rfsimulator->epollfd = epoll_create1(0)) != -1,"");
822

823
  randominit(0);
824
  set_taus_seed(0);
825
  /* look for telnet server, if it is loaded, add the channel modeling commands to it */
frtabu's avatar
frtabu committed
826
  add_telnetcmd_func_t addcmd = (add_telnetcmd_func_t)get_shlibmodule_fptr("telnetsrv", TELNET_ADDCMD_FNAME);
827

frtabu's avatar
frtabu committed
828
  if (addcmd != NULL) {
829
    rfsimulator->poll_telnetcmdq =  (poll_telnetcmdq_func_t)get_shlibmodule_fptr("telnetsrv", TELNET_POLLCMDQ_FNAME);
frtabu's avatar
frtabu committed
830
    addcmd("rfsimu",rfsimu_vardef,rfsimu_cmdarray);
831

frtabu's avatar
frtabu committed
832
    for(int i=0; rfsimu_cmdarray[i].cmdfunc != NULL; i++) {
833
      if (  rfsimu_cmdarray[i].qptr != NULL) {
frtabu's avatar
frtabu committed
834 835 836
        rfsimulator->telnetcmd_qid = rfsimu_cmdarray[i].qptr;
        break;
      }
837 838 839
    }
  }

840 841
  return 0;
}