Add python functions to get/set ctrl variables

Add get_var and set_var functions which handle requested variable while
checking for proper response and id. Split header handling into separate
function.

Change-Id: I08705963c277bd93a011193dd7451a626d606c21
Related: OS#1646
This commit is contained in:
Max 2016-08-01 18:54:17 +02:00 committed by Harald Welte
parent 2a63d01c1e
commit dbb6392368
1 changed files with 27 additions and 4 deletions

View File

@ -10,15 +10,18 @@ verbose = False
def prefix_ipa_ctrl_header(data):
return struct.pack(">HBB", len(data)+1, 0xee, 0) + data
def ipa_ctrl_header(header):
(plen, ipa_proto, osmo_proto) = struct.unpack(">HBB", header)
return None if (ipa_proto != 0xee or osmo_proto != 0) else plen
def remove_ipa_ctrl_header(data):
if (len(data) < 4):
raise BaseException("Answer too short!")
(plen, ipa_proto, osmo_proto) = struct.unpack(">HBB", data[:4])
plen = ipa_ctrl_header(data[:4])
if (None == plen):
raise BaseException("Wrong protocol in answer!")
if (plen + 3 > len(data)):
print "Warning: Wrong payload length (expected %i, got %i)" % (plen, len(data) - 3)
if (ipa_proto != 0xee or osmo_proto != 0):
raise BaseException("Wrong protocol in answer!")
return data[4:plen+3], data[plen+3:]
def connect(host, port):
@ -44,6 +47,26 @@ def do_get(var, op_id, sck):
getmsg = "GET %s %s" %(op_id, var)
send(sck, getmsg)
def do_set_get(sck, var, value = None):
r = random.randint(1, sys.maxint)
if (value != None):
s = 'SET_REPLY'
do_set(var, value, r, sck)
else:
s = 'GET_REPLY'
do_get(var, r, sck)
(answer, data) = remove_ipa_ctrl_header(sck.recv(4096))
x = answer.split()
if (s == x[0] and str(r) == x[1] and var == x[2]):
return None if ('SET_REPLY' == s and value != x[3]) else x[3]
return None
def set_var(sck, var, val):
return do_set_get(sck, var, val)
def get_var(sck, var):
return do_set_get(sck, var)
if __name__ == '__main__':
random.seed()