2017-03-28 12:30:28 +00:00
|
|
|
|
|
|
|
# osmo_gsm_tester: specifics for running a sysmoBTS
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
|
|
|
|
#
|
|
|
|
# Author: Neels Hofmeyr <neels@hofmeyr.de>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2017-06-03 07:51:45 +00:00
|
|
|
# it under the terms of the GNU General Public License as
|
2017-03-28 12:30:28 +00:00
|
|
|
# published by the Free Software Foundation, either version 3 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
|
2017-06-03 07:51:45 +00:00
|
|
|
# GNU General Public License for more details.
|
2017-03-28 12:30:28 +00:00
|
|
|
#
|
2017-06-03 07:51:45 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2017-03-28 12:30:28 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import struct
|
|
|
|
|
2020-04-10 18:46:07 +00:00
|
|
|
from ..core import log
|
2017-03-28 12:30:28 +00:00
|
|
|
|
|
|
|
class CtrlInterfaceExn(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class OsmoCtrl(log.Origin):
|
|
|
|
|
|
|
|
def __init__(self, host, port):
|
fix and refactor logging: drop 'with', simplify
With the recent fix of the junit report related issues, another issue arose:
the 'with log.Origin' was changed to disallow __enter__ing an object twice to
fix problems, now still code would fail because it tries to do 'with' on the
same object twice. The only reason is to ensure that logging is associated with
a given object. Instead of complicating even more, implement differently.
Refactor logging to simplify use: drop the 'with Origin' style completely, and
instead use the python stack to determine which objects are created by which,
and which object to associate a log statement with.
The new way: we rely on the convention that each class instance has a local
'self' referencing the object instance. If we need to find an origin as a new
object's parent, or to associate a log message with, we traverse each stack
frame, fetching the first local 'self' object that is a log.Origin class
instance.
How to use:
Simply call log.log() anywhere, and it finds an Origin object to log for, from
the stack. Alternatively call self.log() for any Origin() object to skip the
lookup.
Create classes as child class of log.Origin and make sure to call
super().__init__(category, name). This constructor will magically find a parent
Origin on the stack.
When an exception happens, we first escalate the exception up through call
scopes to where ever it is handled by log.log_exn(). This then finds an Origin
object in the traceback's stack frames, no need to nest in 'with' scopes.
Hence the 'with log.Origin' now "happens implicitly", we can write pure natural
python code, no more hassles with scope ordering.
Furthermore, any frame can place additional logging information in a frame by
calling log.ctx(). This is automatically inserted in the ancestry associated
with a log statement / exception.
Change-Id: I5f9b53150f2bb6fa9d63ce27f0806f0ca6a45e90
2017-06-09 23:18:27 +00:00
|
|
|
super().__init__(log.C_BUS, 'Ctrl', host=host, port=port)
|
2017-03-28 12:30:28 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.sck = None
|
|
|
|
|
|
|
|
def prefix_ipa_ctrl_header(self, data):
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
s = struct.pack(">HBB", len(data)+1, 0xee, 0)
|
|
|
|
return s + data
|
|
|
|
|
|
|
|
def remove_ipa_ctrl_header(self, data):
|
|
|
|
if (len(data) < 4):
|
|
|
|
raise CtrlInterfaceExn("Answer too short!")
|
|
|
|
(plen, ipa_proto, osmo_proto) = struct.unpack(">HBB", data[:4])
|
|
|
|
if (plen + 3 > len(data)):
|
|
|
|
self.err('Warning: Wrong payload length', expected=plen, got=len(data)-3)
|
|
|
|
if (ipa_proto != 0xee or osmo_proto != 0):
|
|
|
|
raise CtrlInterfaceExn("Wrong protocol in answer!")
|
|
|
|
return data[4:plen+3], data[plen+3:]
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.dbg('Connecting')
|
|
|
|
self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.sck.connect((self.host, self.port))
|
|
|
|
self.sck.setblocking(1)
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
self.dbg('Disconnecting')
|
|
|
|
if self.sck is not None:
|
|
|
|
self.sck.close()
|
|
|
|
|
|
|
|
def _send(self, data):
|
|
|
|
self.dbg('Sending', data=data)
|
|
|
|
data = self.prefix_ipa_ctrl_header(data)
|
|
|
|
self.sck.send(data)
|
|
|
|
|
|
|
|
def receive(self, length = 1024):
|
2017-08-23 08:48:05 +00:00
|
|
|
data = self.sck.recv(length)
|
|
|
|
self.dbg('Receiving', data=data)
|
|
|
|
return data
|
2017-03-28 12:30:28 +00:00
|
|
|
|
|
|
|
def do_set(self, var, value, id=0):
|
|
|
|
setmsg = "SET %s %s %s" %(id, var, value)
|
|
|
|
self._send(setmsg)
|
|
|
|
|
|
|
|
def do_get(self, var, id=0):
|
|
|
|
getmsg = "GET %s %s" %(id, var)
|
|
|
|
self._send(getmsg)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.connect()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *exc_info):
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
# vim: expandtab tabstop=4 shiftwidth=4
|