From efe86853717db552f3a883b64fbaf01349d3f272 Mon Sep 17 00:00:00 2001 From: Maurizio Porrato Date: Sun, 8 Aug 2010 15:27:19 +0200 Subject: [PATCH] Initial commit --- LICENSE.txt | 165 +++++++++++++++++++++++++++++++++++++ docs/stations.conf.sample | 9 ++ frn/__init__.py | 0 frn/protocol/__init__.py | 0 frn/protocol/client.py | 167 ++++++++++++++++++++++++++++++++++++++ frn/utility.py | 46 +++++++++++ parrot.py | 47 +++++++++++ tools/gsmdecode.c | 36 ++++++++ 8 files changed, 470 insertions(+) create mode 100644 LICENSE.txt create mode 100644 docs/stations.conf.sample create mode 100644 frn/__init__.py create mode 100644 frn/protocol/__init__.py create mode 100644 frn/protocol/client.py create mode 100644 frn/utility.py create mode 100644 parrot.py create mode 100644 tools/gsmdecode.c diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/docs/stations.conf.sample b/docs/stations.conf.sample new file mode 100644 index 0000000..2ac1d6e --- /dev/null +++ b/docs/stations.conf.sample @@ -0,0 +1,9 @@ +[accountname] +operator=callsign, name +email=me@example.com +city=City - Locator +country=Your country +transmission=PC Only +description= +password=SECRET +network=Default network diff --git a/frn/__init__.py b/frn/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/frn/protocol/__init__.py b/frn/protocol/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/frn/protocol/client.py b/frn/protocol/client.py new file mode 100644 index 0000000..5438f2c --- /dev/null +++ b/frn/protocol/client.py @@ -0,0 +1,167 @@ +# -*- coding: utf-8 -*- + +from twisted.internet.protocol import ClientFactory +from twisted.protocols.basic import LineReceiver +from frn.utility import * + + +class FRNClient(LineReceiver): + + client_version = 2010002 + + def connectionMade(self): + self.login() + + def ready(self): + self.status = 'READY' + self.msgbuffer = [] + self.phase = 0 + self.setRawMode() + + def startMultiLineMessage(self, msgtype, rest=''): + self.status = msgtype + self.phase = None + self.setLineMode(rest) + + def collectMultiLineMessage(self, line): + if self.phase is None: + while line[0] not in '0123456789': # needed for client list + line = line[1:] + self.expected_lines = int(line.strip()) + self.msgbuffer = [] + self.phase = 0 + else: + self.msgbuffer.append(line) + self.phase += 1 + if self.phase >= self.expected_lines: + handler = getattr(self, 'on_'+self.status, self.on_unimplemented) + message = self.msgbuffer + self.ready() + handler(message) + + def startAudioMessage(self, rest=''): + self.status = 'AUDIO' + self.phase = None + self.msgbuffer = '' + if len(rest) > 0: + self.collectAudioMessage(rest) + + def collectAudioMessage(self, data): + needed = min(327, max(0, 327-len(self.msgbuffer))) + if len(data) > 0: + self.msgbuffer += data[:needed] + if len(self.msgbuffer) >= 327: + audio_data = self.msgbuffer + source = ord(audio_data[0])*256+ord(audio_data[1]) + self.ready() + self.on_AUDIO(source, audio_data[2:]) + if len(data) > needed: + self.dataReceived(data[needed:]) + + def lineReceived(self, line): + if self.status == 'AUTH': + if self.phase == 0: + self.latest_client_version = int(line.strip()) + self.phase = 1 + else: + self.serverdata = parseSimpleXML(line.strip()) + print self.serverdata + if int(self.serverdata['sv']) > 2009004: + self.sendLine(makeAuthKey(self.serverdata['kp'])) + self.ready() + else: + self.collectMultiLineMessage(line) + + def rawDataReceived(self, data): + if self.status == 'READY': + packet_type = ord(data[0]) + if packet_type == 0: # Keepalive + self.sendLine('P') + elif packet_type == 1: # TX ack + self.status == 'TX' + self.phase = 0 + if len(data) > 1: + self.dataReceived(data[1:]) + elif packet_type == 2: # Audio + self.startAudioMessage(data[1:]) + elif packet_type == 3: # Client list + self.startMultiLineMessage('CLIENTS', data[1:]) + elif packet_type == 4: # SMS + self.startMultiLineMessage('SMS', data[1:]) + elif packet_type == 5: # Channel list + self.startMultiLineMessage('CHANNELS', data[1:]) + else: + print "Unknown packet type %d" % packet_type + elif self.status == 'AUDIO': + self.collectAudioMessage(data) + elif self.status == 'TX': + if self.phase == 0: + self.phase = 1 + self.on_TX(ord(data[0])*256+ord(data[1])) + + def login(self): + d = self.factory.client_id + fields = [ + ('VX', self.client_version), + ('EA', d['email']), + ('PW', d['password']), + ('ON', d['operator']), + ('BC', d['transmission']), + ('DS', d['description']), + ('NN', d['country']), + ('CT', d['city']), + ('NT', d['network']) + ] + ap = "CT:"+formatSimpleXML(fields) + self.status = 'AUTH' + self.phase = 0 + self.sendLine(ap) + #self.request_rx() + + def set_status(self, status): + self.sendLine('ST:%s' % str(status)) + + def request_rx(self): + self.sendLine('RX0') + + def request_tx(self): + self.sendLine('TX0') + + def send_audio(self, frame): + self.sendLine('TX1') + self.transport.write(frame) + + def send_SMS(self, dest, text): + self.sendLine('TM:'+formatSimpleXML(dict(ID=dest, MS=text))) + + def on_unimplemented(self, msg): + print msg + + def on_AUDIO(self, from_id, frames): + pass + + def on_TX(self, my_id): + pass + + +class FRNClientFactory(ClientFactory): + + protocol = FRNClient + + def __init__(self, **kw): + self.client_id = kw + + def startedConnecting(self, connector): + print 'Started to connect.' + + def buildProtocol(self, addr): + print 'Connected.' + return ClientFactory.buildProtocol(self, addr) + + def clientConnectionLost(self, connector, reason): + print 'Lost connection. Reason:', reason + + def clientConnectionFailed(self, connector, reason): + print 'Connection failed. Reason:', reason + +# vim: set et ai sw=4 ts=4 sts=4: diff --git a/frn/utility.py b/frn/utility.py new file mode 100644 index 0000000..a2d233c --- /dev/null +++ b/frn/utility.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +from HTMLParser import HTMLParser + + +def makeAuthKey(kp): + if len(kp) != 6: + return 'ERROR' + aa, bb, cc = int(kp[:2]), int(kp[2:4]), int(kp[4:6]) + defgh = "%05d" % ((cc+7)*(cc+4)+(bb+1)*(aa+2), ) + return defgh[3]+defgh[0]+defgh[2]+defgh[4]+defgh[1] + + +class SimpleXMLParser(HTMLParser): + + def handle_starttag(self, tag, attrs): + if not hasattr(self, 'fields'): + self.fields = {} + self.next_field = tag + self.next_data = '' + + def handle_data(self, data): + self.next_data += data + + def handle_endtag(self, tag): + self.fields[self.next_field] = self.next_data + self.next_field = self.next_data = None + + def get_fields(self): + return self.fields + + +def parseSimpleXML(xml): + p = SimpleXMLParser() + p.feed(xml) + return p.get_fields() + + +def formatSimpleXML(elements): + if hasattr(elements, 'items'): + items = elements.items() + else: + items = elements + return ''.join(["<%s>%s" % (k.upper(),v,k.upper()) for k,v in items]) + +# vim: set et ai sw=4 ts=4 sts=4: diff --git a/parrot.py b/parrot.py new file mode 100644 index 0000000..98e09c3 --- /dev/null +++ b/parrot.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import with_statement +from frn.protocol.client import FRNClient, FRNClientFactory + + +class FRNParrot(FRNClient): + + def on_SMS(self, msg): + print "Messaggio %s da %s: %s" % (msg[2], msg[0], msg[1]) + if msg[2][0] == 'P': # Risponde solo ai messaggi privati + self.send_SMS(msg[0], msg[1]) + + def on_AUDIO(self, from_id, frames): + print "AUDIO from %d (%d bytes)" % (from_id, len(frames)) + with file('rx-%d.gsm' % from_id, 'ab') as f: + f.write(frames) + + +class FRNParrotFactory(FRNClientFactory): + protocol = FRNParrot + + +if __name__ == '__main__': + import sys + from ConfigParser import ConfigParser + from twisted.internet import reactor + + cfg = ConfigParser() + cfg.read('stations.conf') + + if len(sys.argv) > 1: + station = sys.argv[1] + else: + stations = cfg.sections() + stations.sort() + station = cfg.sections()[0] + print "Profile not specified: using '%s'" % station + + station_conf = dict(cfg.items(station)) + + reactor.connectTCP('master.freeradionetwork.it', 10024, + FRNParrotFactory(**station_conf)) + reactor.run() + +# vim: set et ai sw=4 ts=4 sts=4: diff --git a/tools/gsmdecode.c b/tools/gsmdecode.c new file mode 100644 index 0000000..60526c1 --- /dev/null +++ b/tools/gsmdecode.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +int main() { + gsm handle = gsm_create(); + int one = 1; + int zero = 0; + gsm_option(handle, GSM_OPT_WAV49, &one); + unsigned char src610[65]; + gsm_frame src; + gsm_signal dst[160]; + unsigned char outbuff[160]; + while (!feof(stdin)) { + int rv = fread(src610, 65, 1, stdin); + if (rv < 0) { + fprintf(stderr, "%s\n", strerror(errno)); + return -1; + } + else if (rv == 0) { + return 0; + } + int frameIndex = 0; + for (frameIndex = 0; frameIndex <= 1; frameIndex++) { + int iFrameIndex = !frameIndex; + gsm_decode(handle, src610 + 33 * frameIndex, dst); + rv = fwrite(dst, sizeof dst, 1, stdout); + if (rv < 0) { + fprintf(stderr, "%s\n", strerror(errno)); + return -1; + } + } + } + gsm_destroy(handle); +}