blob: b5a947f42e0762b3939e99fab15a68b6f37d158f [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001
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 Welte27205342017-06-03 09:51:45 +02009# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +020010# 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 Welte27205342017-06-03 09:51:45 +020016# GNU General Public License for more details.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020017#
Harald Welte27205342017-06-03 09:51:45 +020018# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020019# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21import socket
22import struct
23
24from . import log
25
26class CtrlInterfaceExn(Exception):
27 pass
28
29class OsmoCtrl(log.Origin):
30
31 def __init__(self, host, port):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020032 super().__init__(log.C_BUS, 'Ctrl', host=host, port=port)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020033 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):
70 return self.sck.recv(length)
71
72 def do_set(self, var, value, id=0):
73 setmsg = "SET %s %s %s" %(id, var, value)
74 self._send(setmsg)
75
76 def do_get(self, var, id=0):
77 getmsg = "GET %s %s" %(id, var)
78 self._send(getmsg)
79
80 def __enter__(self):
81 self.connect()
82 return self
83
84 def __exit__(self, *exc_info):
85 self.disconnect()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020086
87# vim: expandtab tabstop=4 shiftwidth=4