-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiqplayer_lib.c
345 lines (312 loc) · 12.4 KB
/
iqplayer_lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* 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
*
* 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:
*/
/** iqplayer_lib.cpp
*
* \author:FrancoisTaburet: [email protected]
*/
#define _LARGEFILE_SOURCE
#define _FILE_OFFSET_BITS 64
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <sys/resource.h>
#include <errno.h>
#include <math.h>
#include <sys/time.h>
#include "common_lib.h"
#include "assertions.h"
#include "common/utils/LOG/log.h"
static void busy_wait(uint32_t usecs) {
uint32_t elapsed_usecs;
struct timespec requestStart, requestEnd;
// Active wait
clock_gettime(CLOCK_REALTIME, &requestStart);
while (1) {
clock_gettime(CLOCK_REALTIME, &requestEnd);
elapsed_usecs = ( requestEnd.tv_sec - requestStart.tv_sec ) / 1e-6
+ ( requestEnd.tv_nsec - requestStart.tv_nsec ) / 1e3;
if (elapsed_usecs >= usecs)
break;
}
//LOG_I(HW, "Expected: %d Elapsed: %d us\n", usecs, elapsed_usecs);
}
static void parse_iqfile_header(openair0_device *device, iqfile_header_t *iq_fh) {
char tmp[4]=OAIIQFILE_ID;
AssertFatal((memcmp(iq_fh->oaiid,tmp,sizeof(iq_fh->oaiid)) == 0),
"iqfile doesn't seem to be compatible with oai (invalid id %.4s in header)\n",
iq_fh->oaiid);
device->type = iq_fh->devtype;
device->openair0_cfg[0].tx_sample_advance=iq_fh->tx_sample_advance;
device->openair0_cfg[0].tx_bw = device->openair0_cfg[0].rx_bw = iq_fh->bw;
device->recplay_state->nbSamplesBlocks=iq_fh->nbSamplesBlocks;
LOG_UI(HW,"Replay iqs from %s device, bandwidth %e\n",get_devname(iq_fh->devtype),iq_fh->bw);
}
/*! \brief Called to start the iqplayer device. Return 0 if OK, < 0 if error
@param device pointer to the device structure specific to the RF hardware target
*/
static int iqplayer_loadfile(openair0_device *device, openair0_config_t *openair0_cfg) {
recplay_state_t *s = device->recplay_state;
recplay_conf_t *c = openair0_cfg->recplay_conf;
struct stat sb;
s->fd = open(c->u_sf_filename, O_RDONLY );
if (s->fd >= 0 ) {
fstat(s->fd, &sb);
} else {
LOG_E( HW,"Cannot open %s exiting.\n", c->u_sf_filename );
exit(-1);
}
if (c->use_mmap) {
// use mmap
s->mapsize=sb.st_size;
LOG_I(HW,"Loading subframes using mmap() from %s size=%lu bytes ...\n",c->u_sf_filename, (uint64_t)sb.st_size );
void *mptr = mmap(NULL, sb.st_size, PROT_WRITE, MAP_PRIVATE, s->fd, 0) ;
if (mptr != MAP_FAILED) {
s->ms_sample = (iqrec_t *) ( mptr + sizeof(iqfile_header_t));
parse_iqfile_header(device, (iqfile_header_t *)mptr);
s->ms_sample = (iqrec_t *)((char *)mptr + sizeof(iqfile_header_t));
LOG_I(HW,"Loaded %u subframes.\n",s->nbSamplesBlocks );
} else {
LOG_E(HW,"Cannot mmap file, exiting, errnor %s\n",strerror(errno));
close(s->fd);
exit(-1);
}
} else {
iqfile_header_t fh;
size_t hs = read(s->fd,&fh,sizeof(fh));
if (hs == sizeof(fh)) {
parse_iqfile_header(device, &fh);
fstat(s->fd, &sb);
s->mapsize=sb.st_size;
LOG_I(HW, "Loading %u subframes from %s,size=%lu bytes ...\n",s->nbSamplesBlocks, c->u_sf_filename,(uint64_t)sb.st_size);
// allocate buffer for 1 subframe at a time
s->ms_sample = (iqrec_t *) malloc(sizeof(iqrec_t)+BELL_LABS_IQ_BYTES_PER_SF);
if (s->ms_sample == NULL) {
LOG_E(HW,"Memory allocation failed for individual subframe replay mode.\n" );
close(s->fd);
exit(-1);
}
memset(s->ms_sample, 0, sizeof(iqrec_t));
// point at beginning of iqs in file
if (lseek(s->fd,sizeof(iqfile_header_t), SEEK_SET) == 0) {
LOG_I(HW,"Initial seek at beginning of the file\n" );
} else {
LOG_I(HW,"Problem initial seek at beginning of the file, %s\n",strerror(errno));
}
} else {
LOG_E(HW,"Cannot read header in %s exiting.\n",c->u_sf_filename );
close(s->fd);
exit(-1);
}
}
s->currentPtr=(uint8_t *)s->ms_sample;
return 0;
}
/*! \brief print the device statistics
* \param device the hardware to use
* \returns 0 on success
*/
static int trx_iqplayer_get_stats(openair0_device *device)
{
LOG_I(HW, "trx_iqplayer_get_stats() called, not implemented\n");
return 0;
}
/*! \brief Reset device statistics
* \param device the hardware to use
* \returns 0 in success
*/
static int trx_iqplayer_reset_stats(openair0_device *device)
{
LOG_I(HW, "trx_iqplayer_reset_stats() called, not implemented\n");
return 0;
}
/*! \brief Stop operation of the transceiver
*/
static int trx_iqplayer_stop(openair0_device *device)
{
LOG_I(HW, "trx_iqplayer_stop() called, not implemented\n");
return 0;
}
/*! \brief Set RX frequencies
* \param device the hardware to use
* \param openair0_cfg RF frontend parameters set by application
* \returns 0 in success
*/
static int trx_iqplayer_set_freq(openair0_device *device, openair0_config_t *openair0_cfg)
{
LOG_I(HW, "trx_iqplayer_set_freq() called, not implemented\n");
return 0;
}
/*! \brief Set gains
* \param device the hardware to use
* \param openair0_cfg RF frontend parameters set by application
* \returns 0 in success
*/
static int trx_iqplayer_set_gains(openair0_device *device, openair0_config_t *openair0_cfg)
{
LOG_I(HW, "trx_iqplayer_set_gains() called, not implemented\n");
return 0;
}
/*! \brief start the oai iq player
* \param device, the hardware used
*/
static int trx_iqplayer_start(openair0_device *device) {
return 0;
}
/*! \brief Terminate operation of the oai iq player
* \param device, the hardware used
*/
static void trx_iqplayer_end(openair0_device *device) {
if (device == NULL)
return;
if (device->recplay_state == NULL)
return;
if (device->openair0_cfg->recplay_conf->use_mmap) {
if (device->recplay_state->ms_sample != MAP_FAILED) {
munmap(device->recplay_state->ms_sample, device->recplay_state->mapsize);
}
} else {
if (device->recplay_state->ms_sample != NULL) {
free(device->recplay_state->ms_sample);
}
}
device->recplay_state->ms_sample = NULL;
if (device->recplay_state->fd >= 0) {
close(device->recplay_state->fd);
device->recplay_state->fd = -1;
}
}
/*! \brief Write iqs function when in replay mode, just introduce a delay, as configured at init time,
@param device pointer to the device structure specific to the RF hardware target
@param timestamp The timestamp at which the first sample MUST be sent
@param buff Buffer which holds the samples
@param nsamps number of samples to be sent
@param antenna_id index of the antenna if the device has multiple antennas
@param flags flags must be set to true if timestamp parameter needs to be applied
*/
static int trx_iqplayer_write(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = device->openair0_cfg->recplay_conf->u_sf_write_delay * 1000;
nanosleep(&req, NULL);
return nsamps;
}
/*! \brief Receive samples from iq file.
* Read \ref nsamps samples from each channel to buffers. buff[0] is the array for
* the first channel. *ptimestamp is the time at which the first sample
* was received.
* \param device the hardware to use
* \param[out] ptimestamp the time at which the first sample was received.
* \param[out] buff An array of pointers to buffers for received samples. The buffers must be large enough to hold the number of samples \ref nsamps.
* \param nsamps Number of samples. One sample is 2 byte I + 2 byte Q => 4 byte.
* \param antenna_id Index of antenna for which to receive samples
* \returns the number of sample read
*/
static int trx_iqplayer_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int cc) {
recplay_state_t *s = device->recplay_state;
static struct timeval tprev;
static int read_count = 0;
if (s->curSamplesBlock==0 && s->wrap_count==0) {
s->currentTs=s->ms_sample->ts;
tprev.tv_sec = s->ms_sample->tv_sec;
tprev.tv_usec = s->ms_sample->tv_usec;
LOG_I(HW, "First timestamp=%lu s->nbSamplesBlocks=%u\n", s->currentTs, s->nbSamplesBlocks);
}
if (s->curSamplesBlock == s->nbSamplesBlocks) {
if (s->wrap_count == device->openair0_cfg->recplay_conf->u_sf_loops) {
LOG_W(HW, "iqplayer device terminating subframes replay after %u iteration\n",
device->openair0_cfg->recplay_conf->u_sf_loops);
exit_function(__FILE__, __FUNCTION__, __LINE__,"replay ended, triggering process termination\n", OAI_EXIT_NORMAL);
}
if (s->wrap_count > 0) {
LOG_I(HW, "wrapping on iq file (%ld)\n", s->wrap_count);
}
s->curSamplesBlock = 0;
s->wrap_count++;
device->recplay_state->currentPtr=(uint8_t *)device->recplay_state->ms_sample;
if (!(device->openair0_cfg->recplay_conf->use_mmap) ) {
close(device->recplay_state->fd);
iqplayer_loadfile(device, device->openair0_cfg);
}
}
if (!(device->openair0_cfg->recplay_conf->use_mmap)) {
// read sample from file
if (read(s->fd, s->ms_sample, sizeof(iqrec_t)) != sizeof(iqrec_t)) {
LOG_E(HW,"pb reading iq record header at index %lu %s\n",sizeof(iqrec_t)*s->curSamplesBlock, strerror(errno) );
exit(-1);
} else {
if (read(s->fd, s->ms_sample+1, BELL_LABS_IQ_BYTES_PER_SF) != BELL_LABS_IQ_BYTES_PER_SF) {
LOG_E(HW,"pb reading iq record data at index %lu %s\n",sizeof(iqrec_t)*s->curSamplesBlock, strerror(errno) );
exit(-1);
}
}
}
iqrec_t *curHeader=(iqrec_t *)s->currentPtr;
if (curHeader->header != BELL_LABS_IQ_HEADER) {
LOG_I(HW,"Problem iq header nsamps %d\n", nsamps);
AssertFatal(curHeader->header==BELL_LABS_IQ_HEADER,"" );
}
// the current timestamp is the stored timestamp until we wrap on input
AssertFatal(nsamps*4==curHeader->nbBytes,"nsamps=%d curHeader->nbBytes=%d", (int)nsamps, (int)curHeader->nbBytes);
*ptimestamp = s->currentTs;
memcpy(buff[0], curHeader+1, nsamps*4);
s->curSamplesBlock++;
// Prepare for next read
s->currentTs+=nsamps;
if (device->openair0_cfg->recplay_conf->use_mmap)
s->currentPtr+=sizeof(iqrec_t)+BELL_LABS_IQ_BYTES_PER_SF;
// subframe read delay is not taken into account
// Real time interval is handle to mimic RF board latcency
struct timeval tcur, tdiff;
tcur.tv_sec = curHeader->tv_sec;
tcur.tv_usec = curHeader->tv_usec;
timersub(&tcur, &tprev, &tdiff);
if (tdiff.tv_usec > 0) {
busy_wait(tdiff.tv_usec);
}
tprev = tcur;
read_count++;
LOG_D(HW, "returning %d samples at ts %lu read_count %d\n", nsamps, *ptimestamp, read_count);
return nsamps;
}
int device_init(openair0_device *device, openair0_config_t *openair0_cfg) {
device->openair0_cfg = openair0_cfg;
device->trx_start_func = trx_iqplayer_start;
device->trx_get_stats_func = trx_iqplayer_get_stats;
device->trx_reset_stats_func = trx_iqplayer_reset_stats;
device->trx_end_func = trx_iqplayer_end;
device->trx_stop_func = trx_iqplayer_stop;
device->trx_set_freq_func = trx_iqplayer_set_freq;
device->trx_set_gains_func = trx_iqplayer_set_gains;
// Replay subframes from from file
// openair0_cfg[0].rx_gain_calib_table = calib_table_b210_38;
// bw_gain_adjust=1;
device->trx_write_func = trx_iqplayer_write;
device->trx_read_func = trx_iqplayer_read;
iqplayer_loadfile(device, openair0_cfg);
LOG_UI(HW,"iqplayer device initialized, replay %s for %i iteration(s)\n",openair0_cfg->recplay_conf->u_sf_filename,openair0_cfg->recplay_conf->u_sf_loops);
return 0;
}
/*@}*/