diff options
author | Piotr Krysik <ptrkrysik@gmail.com> | 2017-09-12 11:45:20 +0200 |
---|---|---|
committer | Piotr Krysik <ptrkrysik@gmail.com> | 2017-09-12 11:45:20 +0200 |
commit | e6e787959f51f280a78a2dc7bd7d1c16b11f8fa9 (patch) | |
tree | 55fb27eda2931f318450f72fd89dd7e0d2a16a9f | |
parent | 45b87000728ac61c086e70f78fb1f638f026fd4b (diff) | |
parent | 76e4b334535223a07c0a8d93186a046b7d9818cb (diff) |
Merge branch 'fixeria/trx' into development
# Conflicts:
# swig/grgsm_swig.i
-rw-r--r-- | apps/trx/ctrl_if.py | 82 | ||||
-rw-r--r-- | apps/trx/ctrl_if_bb.py | 142 | ||||
-rw-r--r-- | apps/trx/fake_pm.py | 53 | ||||
-rwxr-xr-x | apps/trx/grgsm_trx.py | 155 | ||||
-rw-r--r-- | apps/trx/radio_if.py | 175 | ||||
-rw-r--r-- | apps/trx/udp_link.py | 56 | ||||
-rw-r--r-- | grc/CMakeLists.txt | 1 | ||||
-rw-r--r-- | grc/trx_interface/CMakeLists.txt | 22 | ||||
-rw-r--r-- | grc/trx_interface/gsm_trx.xml | 75 | ||||
-rw-r--r-- | include/grgsm/CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/grgsm/trx_interface/CMakeLists.txt | 25 | ||||
-rw-r--r-- | include/grgsm/trx_interface/trx.h | 63 | ||||
-rw-r--r-- | lib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/trx_interface/trx_impl.cc | 273 | ||||
-rw-r--r-- | lib/trx_interface/trx_impl.h | 61 | ||||
-rw-r--r-- | lib/trx_interface/udp_socket.cc | 119 | ||||
-rw-r--r-- | lib/trx_interface/udp_socket.h | 69 | ||||
-rw-r--r-- | swig/grgsm_swig.i | 4 |
18 files changed, 1377 insertions, 1 deletions
diff --git a/apps/trx/ctrl_if.py b/apps/trx/ctrl_if.py new file mode 100644 index 0000000..0129d7f --- /dev/null +++ b/apps/trx/ctrl_if.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# CTRL interface implementation +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from udp_link import UDPLink + +class CTRLInterface(UDPLink): + def handle_rx(self, data): + if self.verify_req(data): + request = self.prepare_req(data) + rc = self.parse_cmd(request) + + if type(rc) is tuple: + self.send_response(request, rc[0], rc[1]) + else: + self.send_response(request, rc) + else: + print("[!] Wrong data on CTRL interface") + + def verify_req(self, data): + # Verify command signature + return data.startswith("CMD") + + def prepare_req(self, data): + # Strip signature, paddings and \0 + request = data[4:].strip().strip("\0") + # Split into a command and arguments + request = request.split(" ") + # Now we have something like ["TXTUNE", "941600"] + return request + + def verify_cmd(self, request, cmd, argc): + # Check if requested command matches + if request[0] != cmd: + return False + + # And has enough arguments + if len(request) - 1 != argc: + return False + + # Check if all arguments are numeric + for v in request[1:]: + if not v.isdigit(): + return False + + return True + + def send_response(self, request, response_code, params = None): + # Include status code, for example ["TXTUNE", "0", "941600"] + request.insert(1, str(response_code)) + + # Optionally append command specific parameters + if params is not None: + request += params + + # Add the response signature, and join back to string + response = "RSP " + " ".join(request) + "\0" + # Now we have something like "RSP TXTUNE 0 941600" + self.send(response) + + def parse_cmd(self, request): + raise NotImplementedError diff --git a/apps/trx/ctrl_if_bb.py b/apps/trx/ctrl_if_bb.py new file mode 100644 index 0000000..b0d54f9 --- /dev/null +++ b/apps/trx/ctrl_if_bb.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# CTRL interface for OsmocomBB +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from ctrl_if import CTRLInterface + +class CTRLInterfaceBB(CTRLInterface): + def __init__(self, remote_addr, remote_port, bind_port, tb, pm): + print("[i] Init CTRL interface") + CTRLInterface.__init__(self, remote_addr, remote_port, bind_port) + + # Set link to the follow graph (top block) + self.tb = tb + # Power measurement + self.pm = pm + + def shutdown(self): + print("[i] Shutdown CTRL interface") + CTRLInterface.shutdown(self) + + def parse_cmd(self, request): + # Power control + if self.verify_cmd(request, "POWERON", 0): + print("[i] Recv POWERON CMD") + + # Ensure transceiver isn't working + if self.tb.trx_started: + print("[!] Transceiver already started") + return -1 + + # Ensure transceiver is ready to start + if not self.tb.check_available(): + print("[!] Transceiver isn't ready to start") + return -1 + + print("[i] Starting transceiver...") + self.tb.trx_started = True + self.tb.start() + + return 0 + + elif self.verify_cmd(request, "POWEROFF", 0): + print("[i] Recv POWEROFF cmd") + + # TODO: flush all buffers between blocks + if self.tb.trx_started: + print("[i] Stopping transceiver...") + self.tb.trx_started = False + self.tb.stop() + self.tb.wait() + + return 0 + + elif self.verify_cmd(request, "SETRXGAIN", 1): + print("[i] Recv SETRXGAIN cmd") + + # TODO: check gain value + gain = int(request[1]) + self.tb.set_gain(gain) + + return 0 + + # Tuning Control + elif self.verify_cmd(request, "RXTUNE", 1): + print("[i] Recv RXTUNE cmd") + + # TODO: check freq range + freq = int(request[1]) * 1000 + self.tb.set_fc(freq) + + return 0 + + elif self.verify_cmd(request, "TXTUNE", 1): + print("[i] Recv TXTUNE cmd") + + # TODO: is not implemented yet + return 0 + + # Timeslot management + elif self.verify_cmd(request, "SETSLOT", 2): + print("[i] Recv SETSLOT cmd") + + # Obtain TS index + tn = int(request[1]) + if tn not in range(0, 8): + print("[!] TS index should be in range: 0..7") + return -1 + + # Ignore timeslot type for now + # Value 0 means 'drop all' + config = -1 if int(request[2]) == 0 else tn + + print("[i] Configure timeslot filter to: %s" + % ("drop all" if config == -1 else "TS %d" % tn)) + + # HACK: configure built-in timeslot filter + self.tb.gsm_trx_if.ts_filter_set_tn(config) + + return 0 + + # Power measurement + elif self.verify_cmd(request, "MEASURE", 1): + print("[i] Recv MEASURE cmd") + + # TODO: check freq range + meas_freq = int(request[1]) * 1000 + + # HACK: send fake low power values + # until actual power measurement is implemented + meas_dbm = str(self.pm.measure(meas_freq)) + + return (0, [meas_dbm]) + + # Misc + elif self.verify_cmd(request, "ECHO", 0): + print("[i] Recv ECHO cmd") + return 0 + + # Wrong / unknown command + else: + print("[!] Wrong request on CTRL interface") + return -1 diff --git a/apps/trx/fake_pm.py b/apps/trx/fake_pm.py new file mode 100644 index 0000000..1d76916 --- /dev/null +++ b/apps/trx/fake_pm.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# Virtual Um-interface (fake transceiver) +# Power measurement emulation for BB +# +# (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from random import randint + +class FakePM: + # Freq. list for good power level + bts_list = [] + + def __init__(self, noise_min, noise_max, bts_min, bts_max): + # Save power level ranges + self.noise_min = noise_min + self.noise_max = noise_max + self.bts_min = bts_min + self.bts_max = bts_max + + def measure(self, bts): + if bts in self.bts_list: + return randint(self.bts_min, self.bts_max) + else: + return randint(self.noise_min, self.noise_max) + + def update_bts_list(self, new_list): + self.bts_list = new_list + + def add_bts_list(self, add_list): + self.bts_list += add_list + + def del_bts_list(self, del_list): + for item in del_list: + if item in self.bts_list: + self.bts_list.remove(item) diff --git a/apps/trx/grgsm_trx.py b/apps/trx/grgsm_trx.py new file mode 100755 index 0000000..fbc9350 --- /dev/null +++ b/apps/trx/grgsm_trx.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import signal +import getopt +import sys + +from ctrl_if_bb import CTRLInterfaceBB +from radio_if import RadioInterface +from fake_pm import FakePM + +COPYRIGHT = \ + "Copyright (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com>\n" \ + "License GPLv2+: GNU GPL version 2 or later " \ + "<http://gnu.org/licenses/gpl.html>\n" \ + "This is free software: you are free to change and redistribute it.\n" \ + "There is NO WARRANTY, to the extent permitted by law.\n" + +class Application: + # Application variables + remote_addr = "127.0.0.1" + base_port = 5700 + + # PHY specific + phy_sample_rate = 2000000 + phy_subdev_spec = False + phy_gain = 30 + phy_args = "" + phy_ppm = 0 + + def __init__(self): + self.print_copyright() + self.parse_argv() + + # Set up signal handlers + signal.signal(signal.SIGINT, self.sig_handler) + + def run(self): + # Init Radio interface + self.radio = RadioInterface(self.phy_args, self.phy_subdev_spec, + self.phy_sample_rate, self.phy_gain, self.phy_ppm, + self.remote_addr, self.base_port) + + # Power measurement emulation + # Noise: -120 .. -105 + # BTS: -75 .. -50 + self.pm = FakePM(-120, -105, -75, -50) + + # Init TRX CTRL interface + self.server = CTRLInterfaceBB(self.remote_addr, + self.base_port + 101, self.base_port + 1, + self.radio, self.pm) + + print("[i] Init complete") + + # Enter main loop + while True: + self.server.loop() + + def shutdown(self): + print("[i] Shutting down...") + self.server.shutdown() + self.radio.shutdown() + + def print_copyright(self): + print(COPYRIGHT) + + def print_help(self): + s = " Usage: " + sys.argv[0] + " [options]\n\n" \ + " Some help...\n" \ + " -h --help this text\n\n" + + # TRX specific + s += " TRX interface specific\n" \ + " -s --remote-addr Set remote address (default 127.0.0.1)\n" \ + " -p --base-port Set base port number (default 5700)\n\n" + + # PHY specific + s += " Radio interface specific\n" \ + " -a --device-args Set device arguments\n" \ + " -s --sample-rate Set PHY sample rate (default 2000000)\n" \ + " -S --subdev-spec Set PHY sub-device specification\n" \ + " -g --gain Set PHY gain (default 30)\n" \ + " --ppm Set PHY frequency correction (default 0)\n" + + print(s) + + def parse_argv(self): + try: + opts, args = getopt.getopt(sys.argv[1:], + "a:p:i:s:S:g:h", + ["help", "remote-addr=", "base-port=", "device-args=", + "gain=", "subdev-spec=", "sample-rate=", "ppm="]) + except getopt.GetoptError as err: + # Print(help and exit) + self.print_help() + print("[!] " + str(err)) + sys.exit(2) + + for o, v in opts: + if o in ("-h", "--help"): + self.print_help() + sys.exit(2) + + # TRX specific + elif o in ("-i", "--remote-addr"): + self.remote_addr = v + elif o in ("-p", "--base-port"): + if int(v) >= 0 and int(v) <= 65535: + self.base_port = int(v) + else: + print("[!] The port number should be in range [0-65536]") + sys.exit(2) + + # PHY specific + elif o in ("-a", "--device-args"): + self.phy_args = v + elif o in ("-g", "--gain"): + self.phy_gain = int(v) + elif o in ("-S", "--subdev-spec"): + self.phy_subdev_spec = v + elif o in ("-s", "--sample-rate"): + self.phy_sample_rate = int(v) + elif o in ("--ppm"): + self.phy_ppm = int(v) + + def sig_handler(self, signum, frame): + print("Signal %d received" % signum) + if signum is signal.SIGINT: + self.shutdown() + sys.exit(0) + +if __name__ == '__main__': + app = Application() + app.run() diff --git a/apps/trx/radio_if.py b/apps/trx/radio_if.py new file mode 100644 index 0000000..c162f69 --- /dev/null +++ b/apps/trx/radio_if.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# Follow graph implementation +# +# (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import pmt +import time +import grgsm +import osmosdr + +from math import pi + +from gnuradio import blocks +from gnuradio import gr + +class RadioInterface(gr.top_block): + # PHY specific variables + samp_rate = 2000000 + shiftoff = 400e3 + subdev_spec = "" # TODO: use it + device_args = "" + fc = 941.6e6 # TODO: set ARFCN to 0? + gain = 30 + ppm = 0 + + # Application state flags + trx_started = False + fc_set = False + + def __init__(self, phy_args, phy_subdev_spec, + phy_sample_rate, phy_gain, phy_ppm, + trx_remote_addr, trx_base_port): + print("[i] Init Radio interface") + + # TRX block specific variables + self.trx_remote_addr = trx_remote_addr + self.trx_base_port = trx_base_port + + # PHY specific variables + self.subdev_spec = phy_subdev_spec + self.samp_rate = phy_sample_rate + self.device_args = phy_args + self.gain = phy_gain + self.ppm = phy_ppm + + gr.top_block.__init__(self, "GR-GSM TRX") + shift_fc = self.fc - self.shiftoff + + ################################################## + # PHY Definition + ################################################## + self.phy = osmosdr.source( + args = "numchan=%d %s" % (1, self.device_args)) + + self.phy.set_bandwidth(250e3 + abs(self.shiftoff), 0) + self.phy.set_center_freq(shift_fc, 0) + self.phy.set_sample_rate(self.samp_rate) + self.phy.set_freq_corr(self.ppm, 0) + self.phy.set_iq_balance_mode(2, 0) + self.phy.set_dc_offset_mode(2, 0) + self.phy.set_gain_mode(False, 0) + self.phy.set_gain(self.gain, 0) + self.phy.set_if_gain(20, 0) + self.phy.set_bb_gain(20, 0) + self.phy.set_antenna("", 0) + + ################################################## + # GR-GSM Magic + ################################################## + self.blocks_rotator = blocks.rotator_cc( + -2 * pi * self.shiftoff / self.samp_rate) + + self.gsm_input = grgsm.gsm_input( + ppm = self.ppm, osr = 4, fc = self.fc, + samp_rate_in = self.samp_rate) + + self.gsm_receiver = grgsm.receiver(4, ([0]), ([])) + + self.gsm_clck_ctrl = grgsm.clock_offset_control( + shift_fc, self.samp_rate, osr = 4) + + self.gsm_trx_if = grgsm.trx(self.trx_remote_addr, + str(self.trx_base_port)) + + ################################################## + # Connections + ################################################## + self.connect((self.phy, 0), (self.blocks_rotator, 0)) + self.connect((self.blocks_rotator, 0), (self.gsm_input, 0)) + self.connect((self.gsm_input, 0), (self.gsm_receiver, 0)) + + self.msg_connect((self.gsm_receiver, 'measurements'), + (self.gsm_clck_ctrl, 'measurements')) + + self.msg_connect((self.gsm_clck_ctrl, 'ctrl'), + (self.gsm_input, 'ctrl_in')) + + self.msg_connect((self.gsm_receiver, 'C0'), + (self.gsm_trx_if, 'bursts')) + + def check_available(self): + return self.fc_set + + def shutdown(self): + print("[i] Shutdown Radio interface") + self.stop() + self.wait() + + def get_args(self): + return self.args + + def set_args(self, args): + self.args = args + + def get_fc(self): + return self.fc + + def set_fc(self, fc): + self.phy.set_center_freq(fc - self.shiftoff, 0) + self.gsm_input.set_fc(fc) + self.fc_set = True + self.fc = fc + + def get_gain(self): + return self.gain + + def set_gain(self, gain): + self.phy.set_gain(gain, 0) + self.gain = gain + + def get_ppm(self): + return self.ppm + + def set_ppm(self, ppm): + self.rtlsdr_source_0.set_freq_corr(ppm, 0) + self.ppm = ppm + + def get_samp_rate(self): + return self.samp_rate + + def set_samp_rate(self, samp_rate): + self.blocks_rotator.set_phase_inc( + -2 * pi * self.shiftoff / samp_rate) + self.gsm_input.set_samp_rate_in(samp_rate) + self.phy.set_sample_rate(samp_rate) + self.samp_rate = samp_rate + + def get_shiftoff(self): + return self.shiftoff + + def set_shiftoff(self, shiftoff): + self.blocks_rotator.set_phase_inc( + -2 * pi * shiftoff / self.samp_rate) + self.phy.set_bandwidth(250e3 + abs(shiftoff), 0) + self.phy.set_center_freq(self.fc - shiftoff, 0) + self.shiftoff = shiftoff diff --git a/apps/trx/udp_link.py b/apps/trx/udp_link.py new file mode 100644 index 0000000..0d90ec9 --- /dev/null +++ b/apps/trx/udp_link.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# GR-GSM based transceiver +# UDP link implementation +# +# (C) 2017 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import socket +import select + +class UDPLink: + def __init__(self, remote_addr, remote_port, bind_port): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.bind(('0.0.0.0', bind_port)) + self.sock.setblocking(0) + + # Save remote info + self.remote_addr = remote_addr + self.remote_port = remote_port + + def loop(self): + r_event, w_event, x_event = select.select([self.sock], [], []) + + # Check for incoming data + if self.sock in r_event: + data, addr = self.sock.recvfrom(128) + self.handle_rx(data.decode()) + + def shutdown(self): + self.sock.close(); + + def send(self, data): + if type(data) not in [bytearray, bytes]: + data = data.encode() + + self.sock.sendto(data, (self.remote_addr, self.remote_port)) + + def handle_rx(self, data): + raise NotImplementedError diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index 2b94539..045d6a5 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -23,6 +23,7 @@ add_subdirectory(demapping) add_subdirectory(receiver) add_subdirectory(flow_control) add_subdirectory(misc_utils) +add_subdirectory(trx_interface) install(FILES gsm_block_tree.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/trx_interface/CMakeLists.txt b/grc/trx_interface/CMakeLists.txt new file mode 100644 index 0000000..752b7a7 --- /dev/null +++ b/grc/trx_interface/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright 2011,2012 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +install(FILES + gsm_trx.xml DESTINATION share/gnuradio/grc/blocks +) diff --git a/grc/trx_interface/gsm_trx.xml b/grc/trx_interface/gsm_trx.xml new file mode 100644 index 0000000..cbe268c --- /dev/null +++ b/grc/trx_interface/gsm_trx.xml @@ -0,0 +1,75 @@ +<?xml version="1.0"?> +<block> + <name>TRX Interface</name> + <key>grgsm_trx_interface</key> + <import>import grgsm</import> + <make>grgsm.trx($remote_addr, $base_port)</make> + + <param> + <name>base_port</name> + <key>base_port</key> + <value>5700</value> + <type>string</type> + </param> + + <param> + <name>remote_addr</name> + <key>remote_addr</key> + <value>127.0.0.1</value> + <type>string</type> + </param> + + <sink> + <name>bursts</name> + <type>message</type> + <optional>1</optional> + </sink> + + <source> + <name>bursts</name> + <type>message</type> + <optional>1</optional> + </source> + + <doc> + OsmoTRX like UDP interface for external applications. + + There are three UDP sockets: CLCK, CTRL and DATA. + Give a base port B (5700 by default), the SCH clock + interface is at port P=B. The TRX-side control interface + is on port P=B+100+1 and the data interface is on an odd + numbered port P=B+100+2. + + Indications on the SCH Clock Interface (CLCK) + The SCH clock interface is output only (from the radio). + CLOCK gives the current value of the BTS clock to + be used by external applications. The indications are + sent whenever a transmission packet arrives that is too + late or too early. + + Commands on Control Interface (CTRL) + The control interface uses a command-response protocol. + Commands are NULL-terminated ASCII strings. Each command + has a corresponding response. This interface isn't handled + by this particular block, and should be implemented outside. + + Messages on the Data Interface + Messages on the data interface carry one radio burst per + UDP message. + + Received Data Burst: + 1 byte timeslot index + 4 bytes GSM frame number, big endian + 1 byte RSSI in -dBm + 2 bytes correlator timing offset in 1/256 symbol steps, + 2's-comp, big endian + 148 bytes soft symbol estimates, 0 -> definite "0", + 255 -> definite "1" + + Transmit Data Burst: + 1 byte timeslot index + 4 bytes GSM frame number, big endian + 1 byte transmit level wrt ARFCN max, -dB (attenuation) + 148 bytes output symbol values, 0 & 1 + </doc> +</block> diff --git a/include/grgsm/CMakeLists.txt b/include/grgsm/CMakeLists.txt index d9b032d..1ce9134 100644 --- a/include/grgsm/CMakeLists.txt +++ b/include/grgsm/CMakeLists.txt @@ -35,3 +35,4 @@ add_subdirectory(receiver) add_subdirectory(misc_utils) add_subdirectory(qa_utils) add_subdirectory(flow_control) +add_subdirectory(trx_interface) diff --git a/include/grgsm/trx_interface/CMakeLists.txt b/include/grgsm/trx_interface/CMakeLists.txt new file mode 100644 index 0000000..8d35ef7 --- /dev/null +++ b/include/grgsm/trx_interface/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright 2011,2012 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +######################################################################## +# Install public header files +######################################################################## +install(FILES + trx.h DESTINATION include/grgsm/trx_interface +) diff --git a/include/grgsm/trx_interface/trx.h b/include/grgsm/trx_interface/trx.h new file mode 100644 index 0000000..997ef62 --- /dev/null +++ b/include/grgsm/trx_interface/trx.h @@ -0,0 +1,63 @@ +/* -*- c++ -*- */ +/* @file + * @author Vadim Yanitskiy <axilirator@gmail.com> + * @section LICENSE + * + * Gr-gsm is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * Gr-gsm 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gr-gsm; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef INCLUDED_GRGSM_TRX_H +#define INCLUDED_GRGSM_TRX_H + +#include <grgsm/api.h> +#include <gnuradio/sync_block.h> + +namespace gr { + namespace grgsm { + + /*! + * \brief <+description of block+> + * \ingroup grgsm + * + */ + class GRGSM_API trx : virtual public gr::block + { + public: + typedef boost::shared_ptr<trx> sptr; + + /*! + * \brief Return a shared_ptr to a new instance of grgsm::trx. + * + * To avoid accidental use of raw pointers, grgsm::trx's + * constructor is in a private implementation + * class. grgsm::trx::make is the public interface for + * creating new instances. + */ + static sptr make( + const std::string &remote_addr, + const std::string &base_port); + + /* Expose internal timeslot filter API */ + virtual void ts_filter_set_tn(int tn) = 0; + virtual int ts_filter_get_tn(void) = 0; + }; + + } // namespace grgsm +} // namespace gr + +#endif /* INCLUDED_GRGSM_TRX_H */ + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 9880e58..7804e07 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -96,6 +96,8 @@ list(APPEND grgsm_sources qa_utils/message_source_impl.cc qa_utils/message_sink_impl.cc decryption/decryption_impl.cc + trx_interface/udp_socket.cc + trx_interface/trx_impl.cc ) diff --git a/lib/trx_interface/trx_impl.cc b/lib/trx_interface/trx_impl.cc new file mode 100644 index 0000000..2068da3 --- /dev/null +++ b/lib/trx_interface/trx_impl.cc @@ -0,0 +1,273 @@ +/* -*- c++ -*- */ +/* @file + * @author Vadim Yanitskiy <axilirator@gmail.com> + * @section LICENSE + * + * Gr-gsm is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * Gr-gsm 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gr-gsm; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gnuradio/io_signature.h> +#include <boost/lexical_cast.hpp> + +#include "udp_socket.h" +#include "trx_impl.h" + +#define BURST_SIZE 148 +#define DATA_IF_MTU 160 + +/** + * 41-bit RACH synchronization sequence + * GSM 05.02 Chapter 5.2.7 Access burst (AB) + */ +static uint8_t rach_synch_seq[] = { + 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, + 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, +}; + +namespace gr { + namespace grgsm { + + trx::sptr + trx::make( + const std::string &remote_addr, + const std::string &base_port) + { + int base_port_int = boost::lexical_cast<int> (base_port); + + return gnuradio::get_initial_sptr + (new trx_impl(remote_addr, base_port_int)); + } + + /* + * The private constructor + */ + trx_impl::trx_impl(const std::string &remote_addr, int base_port) + : gr::block("trx", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)) + { + message_port_register_in(pmt::mp("bursts")); + message_port_register_out(pmt::mp("bursts")); + + // Bind a port handler + set_msg_handler(pmt::mp("bursts"), + boost::bind(&trx_impl::handle_dl_burst, this, _1)); + + // Prepare port numbers + std::string clck_src_port = boost::lexical_cast<std::string> (base_port + 0); + std::string clck_dst_port = boost::lexical_cast<std::string> (base_port + 100); + std::string data_src_port = boost::lexical_cast<std::string> (base_port + 2); + std::string data_dst_port = boost::lexical_cast<std::string> (base_port + 102); + + // Init DATA & CLCK interfaces + d_data_sock = new udp_socket(remote_addr, + data_src_port, data_dst_port, DATA_IF_MTU); + d_clck_sock = new udp_socket(remote_addr, + clck_src_port, clck_dst_port, DATA_IF_MTU); + + // Bind DATA interface handler + d_data_sock->udp_rx_handler = boost::bind( + &trx_impl::handle_ul_burst, this, _1, _2); + + // Init timeslot filter + d_ts_filter_tn = -1; + } + + /* + * Our virtual destructor. + */ + trx_impl::~trx_impl() + { + // Release all UDP sockets and free memory + delete d_data_sock; + delete d_clck_sock; + } + + /* + * Timeslot filter API (getter and setter) + */ + void + trx_impl::ts_filter_set_tn(int tn) + { + d_ts_filter_tn = (tn >= 0 && tn <= 7) ? tn : -1; + } + + int + trx_impl::ts_filter_get_tn(void) + { + return d_ts_filter_tn; + } + + /* + * Check if burst is a RACH burst + */ + bool trx_impl::detect_rach(uint8_t *burst) + { + // Compare synchronization sequence + for (int i = 0; i < 41; i++) + if (burst[i + 8] != rach_synch_seq[i]) + return false; + + // Make sure TB and GP are filled by 0x00 + for (int i = 0; i < 63; i++) + if (burst[i + 85] != 0x00) + return false; + + return true; + } + + /* + * Create an UDP payload with clock indication + */ + void + trx_impl::clck_ind_send(uint32_t frame_nr) + { + char buf[20]; + size_t n; + + n = snprintf(buf, 20, "IND CLOCK %u", frame_nr); + d_clck_sock->udp_send((uint8_t *) buf, n + 1); + } + + /* + * Create an UDP payload with burst bits + * and some channel data. + */ + void + trx_impl::burst_pack(pmt::pmt_t msg, uint8_t *buf) + { + pmt::pmt_t header_plus_burst = pmt::cdr(msg); + + // Extract GSMTAP header from message + gsmtap_hdr *header = (gsmtap_hdr *) + pmt::blob_data(header_plus_burst); + + // Pack timeslot index + buf[0] = header->timeslot; + + // Extract frame number + uint32_t frame_nr = be32toh(header->frame_number); + + // HACK: send clock indications every 51-th frame + if (frame_nr % 51 == 0) + clck_ind_send(frame_nr); + + // Pack frame number + buf[1] = (frame_nr >> 24) & 0xff; + buf[2] = (frame_nr >> 16) & 0xff; + buf[3] = (frame_nr >> 8) & 0xff; + buf[4] = (frame_nr >> 0) & 0xff; + + // Pack RSSI (-dBm) + buf[5] = -(uint8_t) header->signal_dbm; + + // Pack correlator timing offset (TOA) + // FIXME: where to find this value? + buf[6] = 0; + buf[7] = 0; + + // Extract bits {0..1} from message + // Despite GR-GSM uses int8_t, they are not real sbits {-127..127} + uint8_t *burst = (uint8_t *) + (pmt::blob_data(header_plus_burst)) + sizeof(gsmtap_hdr); + + // Convert to transceiver interface specific bits {255..0} + for (int i = 0; i < 148; i++) + buf[8 + i] = burst[i] ? 255 : 0; + + // Fill two unused bytes + buf[156] = 0x00; + buf[157] = 0x00; + } + + void + trx_impl::handle_dl_burst(pmt::pmt_t msg) + { + // 8 bytes of header + 148 bytes of burst + // + two unused, but required bytes + // otherwise bursts would be rejected + uint8_t buf[158]; + + // Compose a new UDP payload with burst + burst_pack(msg, buf); + + // Timeslot filter + if (d_ts_filter_tn != -1 && buf[0] != d_ts_filter_tn) + return; + + // Send a burst + d_data_sock->udp_send(buf, 158); + } + + void + trx_impl::handle_ul_burst(uint8_t *payload, size_t len) + { + // Check length according to the protocol + if (len != 154) + return; + + /* Make sure TS index is correct */ + if (payload[0] >= 8) + return; + + /* Unpack and check frame number */ + uint32_t fn = (payload[1] << 24) + | (payload[2] << 16) + | (payload[3] << 8) + | payload[4]; + + if (fn >= 2715648) + return; + + // Prepare a buffer for GSMTAP header and burst + uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE]; + + // Set up pointer to GSMTAP header structure + struct gsmtap_hdr *header = (struct gsmtap_hdr *) buf; + memset(header, 0x00, sizeof(struct gsmtap_hdr)); + + // Fill in basic info + header->version = GSMTAP_VERSION; + header->hdr_len = sizeof(gsmtap_hdr) / 4; + header->type = GSMTAP_TYPE_UM_BURST; + + // Set timeslot index and frame number + header->timeslot = payload[0]; + header->frame_number = htobe32(fn); + + // Check if one is a RACH burst + header->sub_type = detect_rach(payload + 6) ? + GSMTAP_BURST_ACCESS : GSMTAP_BURST_NORMAL; + + // Copy burst bits (0 & 1) for source message + memcpy(buf + sizeof(gsmtap_hdr), payload + 6, BURST_SIZE); + + // Create a pmt blob + pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE); + pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob); + + /* Send a message to the output */ + message_port_pub(pmt::mp("bursts"), msg); + } + + } /* namespace grgsm */ +} /* namespace gr */ diff --git a/lib/trx_interface/trx_impl.h b/lib/trx_interface/trx_impl.h new file mode 100644 index 0000000..19e4848 --- /dev/null +++ b/lib/trx_interface/trx_impl.h @@ -0,0 +1,61 @@ +/* -*- c++ -*- */ +/* @file + * @author Vadim Yanitskiy <axilirator@gmail.com> + * @section LICENSE + * + * Gr-gsm is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * Gr-gsm 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gr-gsm; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef INCLUDED_GRGSM_TRX_IMPL_H +#define INCLUDED_GRGSM_TRX_IMPL_H + +#include <stddef.h> + +#include <grgsm/gsmtap.h> +#include <grgsm/trx_interface/trx.h> + +namespace gr { + namespace grgsm { + + class trx_impl : public trx + { + private: + udp_socket *d_data_sock; + udp_socket *d_clck_sock; + int d_ts_filter_tn; + + bool detect_rach(uint8_t *burst); + void clck_ind_send(uint32_t frame_nr); + void burst_pack(pmt::pmt_t msg, uint8_t *buf); + + public: + trx_impl(const std::string &remote_addr, int base_port); + ~trx_impl(); + + /* Timeslot filter API */ + void ts_filter_set_tn(int tn); + int ts_filter_get_tn(void); + + void handle_dl_burst(pmt::pmt_t msg); + void handle_ul_burst(uint8_t *payload, size_t len); + }; + + } // namespace grgsm +} // namespace gr + +#endif /* INCLUDED_GRGSM_TRX_IMPL_H */ + diff --git a/lib/trx_interface/udp_socket.cc b/lib/trx_interface/udp_socket.cc new file mode 100644 index 0000000..be4bb66 --- /dev/null +++ b/lib/trx_interface/udp_socket.cc @@ -0,0 +1,119 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gnuradio/thread/thread.h> +#include <gnuradio/io_signature.h> +#include <gnuradio/blocks/pdu.h> +#include <pmt/pmt.h> + +#include <boost/lexical_cast.hpp> +#include "udp_socket.h" + +using boost::asio::ip::udp; + +namespace gr { + namespace grgsm { + + udp_socket::udp_socket( + const std::string &remote_addr, + const std::string &src_port, + const std::string &dst_port, + size_t mtu) + { + // Resize receive buffer according to MTU value + d_rxbuf.resize(mtu); + + // Resolve remote host address + udp::resolver resolver(d_io_service); + + udp::resolver::query rx_query( + udp::v4(), remote_addr, src_port, + boost::asio::ip::resolver_query_base::passive); + udp::resolver::query tx_query( + udp::v4(), remote_addr, dst_port, + boost::asio::ip::resolver_query_base::passive); + + d_udp_endpoint_rx = *resolver.resolve(rx_query); + d_udp_endpoint_tx = *resolver.resolve(tx_query); + + // Create a socket + d_udp_socket.reset(new udp::socket(d_io_service, d_udp_endpoint_rx)); + + // Setup read handler + d_udp_socket->async_receive_from( + boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, + boost::bind(&udp_socket::handle_udp_read, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + + // Start server + d_thread = gr::thread::thread( + boost::bind(&udp_socket::run_io_service, this)); + } + + udp_socket::~udp_socket() + { + // Stop server + d_io_service.stop(); + d_thread.interrupt(); + d_thread.join(); + } + + void + udp_socket::run_io_service(void) + { + d_io_service.run(); + } + + void + udp_socket::udp_send(uint8_t *data, size_t len) + { + d_udp_socket->send_to( + boost::asio::buffer(data, len), + d_udp_endpoint_tx); + } + + void + udp_socket::handle_udp_read( + const boost::system::error_code& error, + size_t bytes_transferred) + { + if (error) + return; + + // Call incoming data handler + if (udp_rx_handler != NULL) + udp_rx_handler((uint8_t *) &d_rxbuf[0], bytes_transferred); + + d_udp_socket->async_receive_from( + boost::asio::buffer(d_rxbuf), d_udp_endpoint_rx, + boost::bind(&udp_socket::handle_udp_read, this, + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred)); + } + + } /* namespace blocks */ +}/* namespace gr */ diff --git a/lib/trx_interface/udp_socket.h b/lib/trx_interface/udp_socket.h new file mode 100644 index 0000000..5bcc42b --- /dev/null +++ b/lib/trx_interface/udp_socket.h @@ -0,0 +1,69 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_GRGSM_TRX_UDP_SOCKET_H +#define INCLUDED_GRGSM_TRX_UDP_SOCKET_H + +#include <gnuradio/thread/thread.h> + +#include <boost/function.hpp> +#include <boost/array.hpp> +#include <boost/asio.hpp> +#include <boost/bind.hpp> +#include <pmt/pmt.h> + +namespace gr { + namespace grgsm { + + class udp_socket + { + private: + boost::asio::io_service d_io_service; + std::vector<char> d_rxbuf; + gr::thread::thread d_thread; + bool d_started; + bool d_finished; + + boost::asio::ip::udp::endpoint d_udp_endpoint_rx; + boost::asio::ip::udp::endpoint d_udp_endpoint_tx; + boost::shared_ptr<boost::asio::ip::udp::socket> d_udp_socket; + + void handle_udp_read(const boost::system::error_code& error, + size_t bytes_transferred); + void run_io_service(void); + + public: + udp_socket( + const std::string &remote_addr, + const std::string &src_port, + const std::string &dst_port, + size_t mtu); + ~udp_socket(); + + void udp_send(uint8_t *data, size_t len); + boost::function<void (uint8_t *, size_t)> udp_rx_handler; + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GRGSM_TRX_UDP_SOCKET_H */ diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i index 66615e6..a67e264 100644 --- a/swig/grgsm_swig.i +++ b/swig/grgsm_swig.i @@ -64,6 +64,7 @@ #include "grgsm/misc_utils/message_file_source.h" #include "grgsm/misc_utils/msg_to_tag.h" #include "grgsm/misc_utils/controlled_fractional_resampler_cc.h" +#include "grgsm/trx_interface/trx.h" %} %include "constants.i" @@ -143,4 +144,5 @@ GR_SWIG_BLOCK_MAGIC2(gsm, burst_source); GR_SWIG_BLOCK_MAGIC2(gsm, message_source); %include "grgsm/qa_utils/message_sink.h" GR_SWIG_BLOCK_MAGIC2(gsm, message_sink); - +%include "grgsm/trx_interface/trx.h" +GR_SWIG_BLOCK_MAGIC2(grgsm, trx); |