aboutsummaryrefslogtreecommitdiffstats
path: root/mpm
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-06-02 15:01:58 -0700
committerMartin Braun <martin.braun@ettus.com>2017-12-22 15:03:58 -0800
commit664f471baf4478b664566a13e81e847dac8a2041 (patch)
treee41b0e643c7cbec8f709aeabf47b73421ae38092 /mpm
parent11401e2564bd50bdd76c84d9baedbb797ecf3f61 (diff)
downloaduhd-664f471baf4478b664566a13e81e847dac8a2041.tar.gz
uhd-664f471baf4478b664566a13e81e847dac8a2041.tar.bz2
uhd-664f471baf4478b664566a13e81e847dac8a2041.zip
mpm: Addition Py3k fixes, all related to string/UTF-8/ascii differences
- Also fixed some error handling in various cases
Diffstat (limited to 'mpm')
-rw-r--r--mpm/python/usrp_mpm/discovery.py11
-rw-r--r--mpm/python/usrp_mpm/periph_manager/base.py9
-rw-r--r--mpm/python/usrp_mpm/rpc_server.py38
3 files changed, 38 insertions, 20 deletions
diff --git a/mpm/python/usrp_mpm/discovery.py b/mpm/python/usrp_mpm/discovery.py
index ab35f8d33..5f025283d 100644
--- a/mpm/python/usrp_mpm/discovery.py
+++ b/mpm/python/usrp_mpm/discovery.py
@@ -21,6 +21,7 @@ Code to run the discovery port
from __future__ import print_function
from multiprocessing import Process
import socket
+from builtins import bytes
from six import iteritems
from usrp_mpm.mpmtypes import MPM_DISCOVERY_PORT
from .mpmlog import get_main_logger
@@ -53,8 +54,7 @@ def _discovery_process(device_info, state):
return RESPONSE_SEP.join(
[RESPONSE_PREAMBLE] + \
["{k}={v}".format(k=k, v=v) for k, v in iteritems(device_info)] + \
- ["{k}={v}".format(k=RESPONSE_CLAIMED_KEY, v=state.claim_status.value)] + \
- ["{k}={v}".format(k="token", v=state.claim_token.value)]
+ ["{k}={v}".format(k=RESPONSE_CLAIMED_KEY, v=state.claim_status.value)]
)
log = get_main_logger().getChild('discovery')
@@ -67,12 +67,13 @@ def _discovery_process(device_info, state):
while True:
data, sender = sock.recvfrom(8000)
log.info("Got poked by: %s", sender[0])
- if data.strip("\0") == "MPM-DISC":
+ if data.strip(b"\0") == b"MPM-DISC":
log.info("Sending discovery response to %s port: %d",
sender[0], sender[1])
- send_data = create_response_string()
+ send_data = bytes(create_response_string(), 'ascii')
+ log.info(send_data)
send_sock.sendto(send_data, sender)
- elif data.strip("\0").startswith("MPM-ECHO"):
+ elif data.strip(b"\0").startswith(b"MPM-ECHO"):
log.info("Received echo request from {sender}".format(sender=sender[0]))
send_data = data
send_sock.sendto(send_data, sender)
diff --git a/mpm/python/usrp_mpm/periph_manager/base.py b/mpm/python/usrp_mpm/periph_manager/base.py
index ef9e4b092..89282265f 100644
--- a/mpm/python/usrp_mpm/periph_manager/base.py
+++ b/mpm/python/usrp_mpm/periph_manager/base.py
@@ -18,6 +18,7 @@
Mboard implementation base class
"""
+from __future__ import print_function
import os
from builtins import str
from builtins import range
@@ -168,7 +169,13 @@ class PeriphManagerBase(object):
for key in ('pid', 'serial', 'rev'):
# In C++, we can only handle dicts if all the values are of the
# same type. So we must convert them all to strings here:
- self.mboard_info[key] = str(self._eeprom_head.get(key, ''))
+ try:
+ self.mboard_info[key] = str(
+ self._eeprom_head.get(key, ''),
+ 'ascii'
+ )
+ except TypeError:
+ self.mboard_info[key] = str(self._eeprom_head.get(key, ''))
if 'pid' in self._eeprom_head and self._eeprom_head['pid'] not in self.pids:
self.log.error("Found invalid PID in EEPROM: 0x{:04X}. Valid PIDs are: {}".format(
self._eeprom_head['pid'],
diff --git a/mpm/python/usrp_mpm/rpc_server.py b/mpm/python/usrp_mpm/rpc_server.py
index 844c3e2bf..0084184f1 100644
--- a/mpm/python/usrp_mpm/rpc_server.py
+++ b/mpm/python/usrp_mpm/rpc_server.py
@@ -29,7 +29,7 @@ from gevent import spawn_later
from gevent import Greenlet
from gevent import monkey
monkey.patch_all()
-from builtins import str
+from builtins import str, bytes
from builtins import range
from mprpc import RPCServer
from .mpmlog import get_main_logger
@@ -101,8 +101,12 @@ class MPMServer(RPCServer):
self.log.trace("adding command %s pointing to %s", command, function)
def new_claimed_function(token, *args):
" Define a function that requires a claim token check "
- if token[:TOKEN_LEN] != self._state.claim_token.value:
- return False
+ if bytes(token[:TOKEN_LEN], 'ascii') != self._state.claim_token.value:
+ self.log.warning(
+ "Stopped attempt to access function `{}' with invalid " \
+ "token `{}'.".format(command, token)
+ )
+ raise RuntimeError("Invalid token!")
return function(*args)
new_claimed_function.__doc__ = function.__doc__
setattr(self, command, new_claimed_function)
@@ -133,22 +137,25 @@ class MPMServer(RPCServer):
self.log.debug("I was pinged from: %s:%s", self.client_host, self.client_port)
return data
- def claim(self, sender_id):
+ def claim(self, session_id):
"""
- claim `token` - tries to claim MPM device and provides a human readable sender_id
+ claim `token` - tries to claim MPM device and provides a human readable session_id
This is a safe method which can be called without a claim on the device
"""
self._state.lock.acquire()
if self._state.claim_status.value:
return ""
- self.log.debug("claiming from: %s", self.client_host)
- self.periph_manager.claimed = True
- self._state.claim_token.value = ''.join(
- choice(ascii_letters + digits) for _ in range(TOKEN_LEN)
+ self.log.debug(
+ "Claiming from: %s, Session ID: %s",
+ self.client_host,
+ session_id
)
+ self._state.claim_token.value = bytes(''.join(
+ choice(ascii_letters + digits) for _ in range(TOKEN_LEN)
+ ), 'ascii')
self._state.claim_status.value = True
self._state.lock.release()
- self.sender_id = sender_id
+ self.session_id = session_id
self._reset_timer()
self.log.debug("giving token: %s to host: %s", self._state.claim_token.value, self.client_host)
return self._state.claim_token.value
@@ -161,13 +168,16 @@ class MPMServer(RPCServer):
"""
self._state.lock.acquire()
if self._state.claim_status.value:
- if self._state.claim_token.value == token[:TOKEN_LEN]:
+ if self._state.claim_token.value == bytes(token[:TOKEN_LEN], 'ascii'):
self._state.lock.release()
self.log.debug("reclaimed from: %s", self.client_host)
self._reset_timer()
return True
self._state.lock.release()
- self.log.debug("reclaim failed from: %s", self.client_host)
+ self.log.debug(
+ "reclaim failed from: %s Invalid token: %s",
+ self.client_host, token[:TOKEN_LEN]
+ )
return False
self.log.debug("trying to reclaim unclaimed device from: %s", self.client_host)
return False
@@ -178,8 +188,8 @@ class MPMServer(RPCServer):
"""
self.log.debug("releasing claim")
self._state.claim_status.value = False
- self._state.claim_token.value = ""
- self.sender_id = None
+ self._state.claim_token.value = b''
+ self.session_id = None
self.periph_manager.claimed = False
self.periph_manager.deinit()
self._timer.kill()