mirror of https://github.com/pvnis/srsRAN_4G.git
ZMQ: Split Tx and Rx, bug fixes and clean up
parent
cf550f6e56
commit
125f1e7282
@ -0,0 +1,198 @@
|
||||
/**
|
||||
*
|
||||
* \section COPYRIGHT
|
||||
*
|
||||
* Copyright 2013-2019 Software Radio Systems Limited
|
||||
*
|
||||
* \section LICENSE
|
||||
*
|
||||
* This file is part of the srsLTE library.
|
||||
*
|
||||
* srsLTE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* srsLTE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* A copy of the GNU Affero General Public License can be found in
|
||||
* the LICENSE file in the top-level directory of this distribution
|
||||
* and at http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rf_zmq_imp_trx.h"
|
||||
#include <srslte/phy/utils/vector.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <zconf.h>
|
||||
#include <zmq.h>
|
||||
|
||||
static void* rf_zmq_async_rx_thread(void* h)
|
||||
{
|
||||
rf_zmq_rx_t* q = (rf_zmq_rx_t*)h;
|
||||
|
||||
while (q->sock && q->running) {
|
||||
int nbytes = 0;
|
||||
int n = SRSLTE_ERROR;
|
||||
uint8_t dummy = 0xFF;
|
||||
|
||||
rf_zmq_info(q->id, "-- ASYNC RX wait...\n");
|
||||
|
||||
// Send request
|
||||
while (n < 0 && q->running) {
|
||||
rf_zmq_info(q->id, " - tx'ing rx request\n");
|
||||
n = zmq_send(q->sock, &dummy, sizeof(dummy), 0);
|
||||
if (n < 0) {
|
||||
if (rf_zmq_handle_error(q->id, "synchronous rx request send")) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Receive baseband
|
||||
for (n = (n < 0) ? 0 : -1; n < 0 && q->running;) {
|
||||
n = zmq_recv(q->sock, q->temp_buffer, ZMQ_MAX_BUFFER_SIZE, 0);
|
||||
if (n == -1) {
|
||||
if (rf_zmq_handle_error(q->id, "asynchronous rx baseband receive")) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} else if (n > ZMQ_MAX_BUFFER_SIZE) {
|
||||
fprintf(stderr,
|
||||
"[zmq] Error: receiver expected <= %ld bytes and received %d at channel %d.\n",
|
||||
ZMQ_MAX_BUFFER_SIZE,
|
||||
n,
|
||||
0);
|
||||
return NULL;
|
||||
} else {
|
||||
nbytes = n;
|
||||
}
|
||||
}
|
||||
|
||||
// Write received data in buffer
|
||||
if (nbytes > 0) {
|
||||
n = -1;
|
||||
|
||||
// Try to write in ring buffer
|
||||
while (n < 0 && q->running) {
|
||||
n = srslte_ringbuffer_write_timed(&q->ringbuffer, q->temp_buffer, nbytes, ZMQ_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
// Check write
|
||||
if (nbytes == n) {
|
||||
rf_zmq_info(q->id,
|
||||
" - received %d baseband samples (%d B). %d samples available.\n",
|
||||
NBYTES2NSAMPLES(n),
|
||||
n,
|
||||
NBYTES2NSAMPLES(srslte_ringbuffer_status(&q->ringbuffer)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int rf_zmq_rx_open(rf_zmq_rx_t* q, char* id, void* zmq_ctx, char* sock_args)
|
||||
{
|
||||
int ret = SRSLTE_ERROR;
|
||||
|
||||
if (q) {
|
||||
// Copy id
|
||||
strncpy(q->id, id, 16);
|
||||
|
||||
// Zero object
|
||||
bzero(q, sizeof(rf_zmq_tx_t));
|
||||
|
||||
// Create socket
|
||||
q->sock = zmq_socket(zmq_ctx, ZMQ_REQ);
|
||||
if (!q->sock) {
|
||||
fprintf(stderr, "[zmq] Error: creating transmitter socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
rf_zmq_info(q->id, "Connecting receiver: %s\n", sock_args);
|
||||
|
||||
ret = zmq_connect(q->sock, sock_args);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Error: connecting receiver socket: %s\n", zmq_strerror(zmq_errno()));
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
#if ZMQ_TIMEOUT_MS
|
||||
int timeout = ZMQ_TIMEOUT_MS;
|
||||
if (zmq_setsockopt(q->sock, ZMQ_RCVTIMEO, &timeout, sizeof(timeout)) == -1) {
|
||||
fprintf(stderr, "Error: setting receive timeout on rx socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
if (zmq_setsockopt(q->sock, ZMQ_SNDTIMEO, &timeout, sizeof(timeout)) == -1) {
|
||||
fprintf(stderr, "Error: setting receive timeout on rx socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
if (zmq_setsockopt(q->sock, ZMQ_LINGER, &timeout, sizeof(timeout)) == -1) {
|
||||
fprintf(stderr, "Error: setting linger timeout on rx socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (srslte_ringbuffer_init(&q->ringbuffer, ZMQ_MAX_BUFFER_SIZE)) {
|
||||
fprintf(stderr, "Error: initiating ringbuffer\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
q->temp_buffer = srslte_vec_malloc(ZMQ_MAX_BUFFER_SIZE);
|
||||
if (!q->temp_buffer) {
|
||||
fprintf(stderr, "Error: allocating rx buffer\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&q->mutex, NULL)) {
|
||||
fprintf(stderr, "Error: creating mutex\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
if (pthread_create(&q->thread, NULL, rf_zmq_async_rx_thread, q)) {
|
||||
fprintf(stderr, "Error: creating thread\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
q->running = true;
|
||||
ret = SRSLTE_SUCCESS;
|
||||
}
|
||||
|
||||
clean_exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rf_zmq_rx_baseband(rf_zmq_rx_t* q, cf_t* buffer, uint32_t nsamples)
|
||||
{
|
||||
return srslte_ringbuffer_read_timed(&q->ringbuffer, buffer, NSAMPLES2NBYTES(nsamples), ZMQ_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
void rf_zmq_rx_close(rf_zmq_rx_t* q)
|
||||
{
|
||||
rf_zmq_info(q->id, "Closing ...\n");
|
||||
q->running = false;
|
||||
|
||||
if (q->thread) {
|
||||
pthread_join(q->thread, NULL);
|
||||
pthread_detach(q->thread);
|
||||
}
|
||||
|
||||
srslte_ringbuffer_free(&q->ringbuffer);
|
||||
|
||||
if (q->temp_buffer) {
|
||||
free(q->temp_buffer);
|
||||
}
|
||||
|
||||
if (q->sock) {
|
||||
zmq_close(q->sock);
|
||||
q->sock = NULL;
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
/**
|
||||
*
|
||||
* \section COPYRIGHT
|
||||
*
|
||||
* Copyright 2013-2019 Software Radio Systems Limited
|
||||
*
|
||||
* \section LICENSE
|
||||
*
|
||||
* This file is part of the srsLTE library.
|
||||
*
|
||||
* srsLTE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* srsLTE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* A copy of the GNU Affero General Public License can be found in
|
||||
* the LICENSE file in the top-level directory of this distribution
|
||||
* and at http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRSLTE_RF_ZMQ_IMP_TRX_H
|
||||
#define SRSLTE_RF_ZMQ_IMP_TRX_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <srslte/phy/utils/ringbuffer.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Definitions */
|
||||
#define VERBOSE (0)
|
||||
#define NSAMPLES2NBYTES(X) (((uint32_t)(X)) * sizeof(cf_t))
|
||||
#define NBYTES2NSAMPLES(X) ((X) / sizeof(cf_t))
|
||||
#define ZMQ_MAX_BUFFER_SIZE (NSAMPLES2NBYTES(3072000)) // 10 subframes at 20 MHz
|
||||
#define ZMQ_TIMEOUT_MS (1000)
|
||||
#define ZMQ_BASERATE_DEFAULT_HZ (23040000)
|
||||
|
||||
typedef struct {
|
||||
char id[16];
|
||||
void* sock;
|
||||
uint64_t nsamples;
|
||||
bool running;
|
||||
pthread_mutex_t mutex;
|
||||
cf_t* zeros;
|
||||
} rf_zmq_tx_t;
|
||||
|
||||
typedef struct {
|
||||
char id[16];
|
||||
void* sock;
|
||||
uint64_t nsamples;
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
pthread_mutex_t mutex;
|
||||
srslte_ringbuffer_t ringbuffer;
|
||||
cf_t* temp_buffer;
|
||||
} rf_zmq_rx_t;
|
||||
|
||||
/*
|
||||
* Common functions
|
||||
*/
|
||||
SRSLTE_API void rf_zmq_info(char* id, const char* format, ...);
|
||||
|
||||
SRSLTE_API void rf_zmq_error(char* id, const char* format, ...);
|
||||
|
||||
SRSLTE_API int rf_zmq_handle_error(char* id, const char* text);
|
||||
|
||||
/*
|
||||
* Transmitter functions
|
||||
*/
|
||||
SRSLTE_API int rf_zmq_tx_open(rf_zmq_tx_t* q, const char* id, void* zmq_ctx, char* sock_args);
|
||||
|
||||
SRSLTE_API int rf_zmq_tx_align(rf_zmq_tx_t* q, uint64_t ts);
|
||||
|
||||
SRSLTE_API int rf_zmq_tx_baseband(rf_zmq_tx_t* q, cf_t* buffer, uint32_t nsamples);
|
||||
|
||||
SRSLTE_API void rf_zmq_tx_close(rf_zmq_tx_t* q);
|
||||
|
||||
/*
|
||||
* Receiver functions
|
||||
*/
|
||||
SRSLTE_API int rf_zmq_rx_open(rf_zmq_rx_t* q, char* id, void* zmq_ctx, char* sock_args);
|
||||
|
||||
SRSLTE_API int rf_zmq_rx_baseband(rf_zmq_rx_t* q, cf_t* buffer, uint32_t nsamples);
|
||||
|
||||
SRSLTE_API void rf_zmq_rx_close(rf_zmq_rx_t* q);
|
||||
|
||||
#endif // SRSLTE_RF_ZMQ_IMP_TRX_H
|
@ -0,0 +1,176 @@
|
||||
/**
|
||||
*
|
||||
* \section COPYRIGHT
|
||||
*
|
||||
* Copyright 2013-2019 Software Radio Systems Limited
|
||||
*
|
||||
* \section LICENSE
|
||||
*
|
||||
* This file is part of the srsLTE library.
|
||||
*
|
||||
* srsLTE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* srsLTE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* A copy of the GNU Affero General Public License can be found in
|
||||
* the LICENSE file in the top-level directory of this distribution
|
||||
* and at http://www.gnu.org/licenses/.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rf_zmq_imp_trx.h"
|
||||
#include <srslte/config.h>
|
||||
#include <srslte/phy/utils/vector.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <zmq.h>
|
||||
|
||||
int rf_zmq_tx_open(rf_zmq_tx_t* q, const char* id, void* zmq_ctx, char* sock_args)
|
||||
{
|
||||
int ret = SRSLTE_ERROR;
|
||||
|
||||
if (q) {
|
||||
// Copy id
|
||||
strncpy(q->id, id, 16);
|
||||
|
||||
// Zero object
|
||||
bzero(q, sizeof(rf_zmq_tx_t));
|
||||
|
||||
// Create socket
|
||||
q->sock = zmq_socket(zmq_ctx, ZMQ_REP);
|
||||
if (!q->sock) {
|
||||
fprintf(stderr, "[zmq] Error: creating transmitter socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
rf_zmq_info(q->id, "Binding transmitter: %s\n", sock_args);
|
||||
|
||||
ret = zmq_bind(q->sock, sock_args);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Error: connecting transmitter socket: %s\n", zmq_strerror(zmq_errno()));
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
#if ZMQ_TIMEOUT_MS
|
||||
int timeout = ZMQ_TIMEOUT_MS;
|
||||
if (zmq_setsockopt(q->sock, ZMQ_RCVTIMEO, &timeout, sizeof(timeout)) == -1) {
|
||||
fprintf(stderr, "Error: setting receive timeout on tx socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
if (zmq_setsockopt(q->sock, ZMQ_SNDTIMEO, &timeout, sizeof(timeout)) == -1) {
|
||||
fprintf(stderr, "Error: setting receive timeout on tx socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
timeout = 0;
|
||||
if (zmq_setsockopt(q->sock, ZMQ_LINGER, &timeout, sizeof(timeout)) == -1) {
|
||||
fprintf(stderr, "Error: setting linger timeout on tx socket\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (pthread_mutex_init(&q->mutex, NULL)) {
|
||||
fprintf(stderr, "Error: creating mutex\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
|
||||
q->zeros = srslte_vec_malloc(ZMQ_MAX_BUFFER_SIZE);
|
||||
if (!q->zeros) {
|
||||
fprintf(stderr, "Error: allocating zeros\n");
|
||||
goto clean_exit;
|
||||
}
|
||||
bzero(q->zeros, ZMQ_MAX_BUFFER_SIZE);
|
||||
|
||||
q->running = true;
|
||||
|
||||
ret = SRSLTE_SUCCESS;
|
||||
}
|
||||
|
||||
clean_exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rf_zmq_tx_align(rf_zmq_tx_t* q, uint64_t ts)
|
||||
{
|
||||
int64_t nsamples = (int64_t)ts - (int64_t)q->nsamples;
|
||||
|
||||
if (nsamples > 0) {
|
||||
rf_zmq_info(q->id, " - Detected Tx gap of %d samples.\n", nsamples);
|
||||
rf_zmq_tx_baseband(q, q->zeros, (uint32_t)nsamples);
|
||||
}
|
||||
|
||||
return (int)nsamples;
|
||||
}
|
||||
|
||||
int rf_zmq_tx_baseband(rf_zmq_tx_t* q, cf_t* buffer, uint32_t nsamples)
|
||||
{
|
||||
int n = SRSLTE_ERROR;
|
||||
pthread_mutex_lock(&q->mutex);
|
||||
|
||||
while (n < 0 && q->running) {
|
||||
// Receive Transmit request
|
||||
uint8_t dummy;
|
||||
n = zmq_recv(q->sock, &dummy, sizeof(dummy), 0);
|
||||
if (n < 0) {
|
||||
if (rf_zmq_handle_error(q->id, "tx request receive")) {
|
||||
n = SRSLTE_ERROR;
|
||||
goto clean_exit;
|
||||
}
|
||||
} else {
|
||||
// Tx request received successful
|
||||
rf_zmq_info(q->id, " - tx request received\n");
|
||||
rf_zmq_info(q->id, " - sending %d samples (%d B)\n", nsamples, NSAMPLES2NBYTES(nsamples));
|
||||
}
|
||||
|
||||
// Send base-band if request was received
|
||||
if (n > 0) {
|
||||
n = zmq_send(q->sock, buffer, NSAMPLES2NBYTES(nsamples), 0);
|
||||
if (n < 0) {
|
||||
if (rf_zmq_handle_error(q->id, "tx baseband send")) {
|
||||
n = SRSLTE_ERROR;
|
||||
goto clean_exit;
|
||||
}
|
||||
} else if (n != NSAMPLES2NBYTES(nsamples)) {
|
||||
rf_zmq_error(q->id,
|
||||
"[zmq] Error: transmitter expected %d bytes and sent %d. %s.\n",
|
||||
NSAMPLES2NBYTES(nsamples),
|
||||
n,
|
||||
strerror(zmq_errno()));
|
||||
n = SRSLTE_ERROR;
|
||||
goto clean_exit;
|
||||
}
|
||||
}
|
||||
|
||||
// If failed to receive request or send base-band, keep trying
|
||||
}
|
||||
|
||||
// Increment sample counter
|
||||
q->nsamples += nsamples;
|
||||
n = nsamples;
|
||||
|
||||
clean_exit:
|
||||
pthread_mutex_unlock(&q->mutex);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void rf_zmq_tx_close(rf_zmq_tx_t* q)
|
||||
{
|
||||
q->running = false;
|
||||
|
||||
if (q->zeros) {
|
||||
free(q->zeros);
|
||||
}
|
||||
|
||||
if (q->sock) {
|
||||
zmq_close(q->sock);
|
||||
q->sock = NULL;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue