Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | |
| 2 | # osmo_gsm_tester: specifics for running a sysmoBTS |
| 3 | # |
| 4 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 5 | # |
| 6 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 7 | # |
| 8 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 9 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 10 | # published by the Free Software Foundation, either version 3 of the |
| 11 | # License, or (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 16 | # GNU General Public License for more details. |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 17 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 18 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | import socket |
| 22 | import struct |
| 23 | |
| 24 | from . import log |
| 25 | |
| 26 | class CtrlInterfaceExn(Exception): |
| 27 | pass |
| 28 | |
| 29 | class OsmoCtrl(log.Origin): |
| 30 | |
| 31 | def __init__(self, host, port): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 32 | super().__init__(log.C_BUS, 'Ctrl', host=host, port=port) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 33 | self.host = host |
| 34 | self.port = port |
| 35 | self.sck = None |
| 36 | |
| 37 | def prefix_ipa_ctrl_header(self, data): |
| 38 | if isinstance(data, str): |
| 39 | data = data.encode('utf-8') |
| 40 | s = struct.pack(">HBB", len(data)+1, 0xee, 0) |
| 41 | return s + data |
| 42 | |
| 43 | def remove_ipa_ctrl_header(self, data): |
| 44 | if (len(data) < 4): |
| 45 | raise CtrlInterfaceExn("Answer too short!") |
| 46 | (plen, ipa_proto, osmo_proto) = struct.unpack(">HBB", data[:4]) |
| 47 | if (plen + 3 > len(data)): |
| 48 | self.err('Warning: Wrong payload length', expected=plen, got=len(data)-3) |
| 49 | if (ipa_proto != 0xee or osmo_proto != 0): |
| 50 | raise CtrlInterfaceExn("Wrong protocol in answer!") |
| 51 | return data[4:plen+3], data[plen+3:] |
| 52 | |
| 53 | def connect(self): |
| 54 | self.dbg('Connecting') |
| 55 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 56 | self.sck.connect((self.host, self.port)) |
| 57 | self.sck.setblocking(1) |
| 58 | |
| 59 | def disconnect(self): |
| 60 | self.dbg('Disconnecting') |
| 61 | if self.sck is not None: |
| 62 | self.sck.close() |
| 63 | |
| 64 | def _send(self, data): |
| 65 | self.dbg('Sending', data=data) |
| 66 | data = self.prefix_ipa_ctrl_header(data) |
| 67 | self.sck.send(data) |
| 68 | |
| 69 | def receive(self, length = 1024): |
Pau Espin Pedrol | 66c96c3 | 2017-08-23 10:48:05 +0200 | [diff] [blame] | 70 | data = self.sck.recv(length) |
| 71 | self.dbg('Receiving', data=data) |
| 72 | return data |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 73 | |
| 74 | def do_set(self, var, value, id=0): |
| 75 | setmsg = "SET %s %s %s" %(id, var, value) |
| 76 | self._send(setmsg) |
| 77 | |
| 78 | def do_get(self, var, id=0): |
| 79 | getmsg = "GET %s %s" %(id, var) |
| 80 | self._send(getmsg) |
| 81 | |
| 82 | def __enter__(self): |
| 83 | self.connect() |
| 84 | return self |
| 85 | |
| 86 | def __exit__(self, *exc_info): |
| 87 | self.disconnect() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 88 | |
| 89 | # vim: expandtab tabstop=4 shiftwidth=4 |