From 23914b9cf855de8e847d450c70494626da2216e6 Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Tue, 13 Mar 2018 01:09:56 +0700 Subject: Rename 'fake_trx' to 'trx_toolkit' This toolkit has branched out into several different tools for TRX interface hacking, and creating a virtual Um-interface (FakeTRX) is only one of its potential applications. Change-Id: I56bcbc76b9c273d6b469a2bb68ddc46f3980e835 --- src/target/trx_toolkit/clck_gen.py | 116 +++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 src/target/trx_toolkit/clck_gen.py (limited to 'src/target/trx_toolkit/clck_gen.py') diff --git a/src/target/trx_toolkit/clck_gen.py b/src/target/trx_toolkit/clck_gen.py new file mode 100755 index 00000000..4b86c31a --- /dev/null +++ b/src/target/trx_toolkit/clck_gen.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# TRX Toolkit +# Simple TDMA frame clock generator +# +# (C) 2017-2018 by Vadim Yanitskiy +# +# 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 copyright import print_copyright +CR_HOLDERS = [("2017-2018", "Vadim Yanitskiy ")] + +import signal +import time +import sys + +from threading import Timer +from udp_link import UDPLink +from gsm_shared import * + +class CLCKGen: + # GSM TDMA definitions + SEC_DELAY_US = 1000 * 1000 + GSM_FRAME_US = 4615.0 + + # Average loop back delay + LO_DELAY_US = 90.0 + + # State variables + timer = None + + def __init__(self, clck_links, clck_start = 0, ind_period = 102): + self.clck_links = clck_links + self.ind_period = ind_period + self.clck_start = clck_start + self.clck_src = clck_start + + # Calculate counter time + self.ctr_interval = self.GSM_FRAME_US - self.LO_DELAY_US + self.ctr_interval /= self.SEC_DELAY_US + self.ctr_interval *= self.ind_period + + def start(self): + # Send the first indication + self.send_clck_ind() + + def stop(self): + # Stop pending timer + if self.timer is not None: + self.timer.cancel() + self.timer = None + + # Reset the clock source + self.clck_src = self.clck_start + + def send_clck_ind(self): + # Keep clock cycle + if self.clck_src % GSM_HYPERFRAME >= 0: + self.clck_src %= GSM_HYPERFRAME + + # We don't need to send so often + if self.clck_src % self.ind_period == 0: + # Create UDP payload + payload = "IND CLOCK %u\0" % self.clck_src + + # Send indication to all UDP links + for link in self.clck_links: + link.send(payload) + + # Debug print + print("[T] %s" % payload) + + # Increase frame count + self.clck_src += self.ind_period + + # Schedule a new indication + self.timer = Timer(self.ctr_interval, self.send_clck_ind) + self.timer.start() + +# Just a wrapper for independent usage +class Application: + def __init__(self): + # Print copyright + print_copyright(CR_HOLDERS) + + # Set up signal handlers + signal.signal(signal.SIGINT, self.sig_handler) + + def run(self): + self.link = UDPLink("127.0.0.1", 5800, 5700) + self.clck = CLCKGen([self.link], ind_period = 51) + self.clck.start() + + def sig_handler(self, signum, frame): + print("Signal %d received" % signum) + if signum is signal.SIGINT: + self.clck.stop() + +if __name__ == '__main__': + app = Application() + app.run() -- cgit v1.2.3