aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python/usrp_mpm
diff options
context:
space:
mode:
authorAndrej Rode <andrej.rode@ettus.com>2017-03-27 17:07:44 -0700
committerMartin Braun <martin.braun@ettus.com>2017-12-22 15:03:44 -0800
commit8ac9014d5c30836665378d2d088b5c602162f8ac (patch)
tree21708ce88f72da4818e73afe054b89e7f1589fdd /mpm/python/usrp_mpm
parent6d332e5ca4a7311406ac285f827c31c9e5b94280 (diff)
downloaduhd-8ac9014d5c30836665378d2d088b5c602162f8ac.tar.gz
uhd-8ac9014d5c30836665378d2d088b5c602162f8ac.tar.bz2
uhd-8ac9014d5c30836665378d2d088b5c602162f8ac.zip
mpm: CMake cleanup, Python code enhancements
- Send user defined data in ping - Improve rpc_shell, add mpm_debug.py, fix tracebacks in multiprocessing
Diffstat (limited to 'mpm/python/usrp_mpm')
-rw-r--r--mpm/python/usrp_mpm/CMakeLists.txt13
-rw-r--r--mpm/python/usrp_mpm/__init__.py2
-rw-r--r--mpm/python/usrp_mpm/dboard_manager/CMakeLists.txt30
-rw-r--r--mpm/python/usrp_mpm/dboard_manager/__init__.py1
-rw-r--r--mpm/python/usrp_mpm/dboard_manager/base.py1
-rw-r--r--mpm/python/usrp_mpm/dboard_manager/magnesium.py2
-rw-r--r--mpm/python/usrp_mpm/dboard_manager/test.py57
-rw-r--r--mpm/python/usrp_mpm/discovery.py6
-rw-r--r--mpm/python/usrp_mpm/periph_manager/CMakeLists.txt28
-rw-r--r--mpm/python/usrp_mpm/periph_manager/__init__.py.in (renamed from mpm/python/usrp_mpm/periph_manager/__init__.py)4
-rw-r--r--mpm/python/usrp_mpm/periph_manager/n310.py2
-rw-r--r--mpm/python/usrp_mpm/periph_manager/test.py63
-rw-r--r--mpm/python/usrp_mpm/rpc_process.py48
-rw-r--r--mpm/python/usrp_mpm/rpc_server.py91
14 files changed, 259 insertions, 89 deletions
diff --git a/mpm/python/usrp_mpm/CMakeLists.txt b/mpm/python/usrp_mpm/CMakeLists.txt
index f8f94bb5d..12bcdac11 100644
--- a/mpm/python/usrp_mpm/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/CMakeLists.txt
@@ -18,15 +18,16 @@
########################################################################
# This file included, use CMake directory variables
########################################################################
-
-SET(USRP_MPM_FILES
+SET(USRP_MPM_FILES ${USRP_MPM_FILES})
+SET(USRP_MPM_TOP_FILES
${CMAKE_CURRENT_SOURCE_DIR}/__init__.py
${CMAKE_CURRENT_SOURCE_DIR}/types.py
${CMAKE_CURRENT_SOURCE_DIR}/discovery.py
- ${CMAKE_CURRENT_SOURCE_DIR}/rpc_process.py
${CMAKE_CURRENT_SOURCE_DIR}/rpc_server.py
${CMAKE_CURRENT_SOURCE_DIR}/periphs.py
- ${CMAKE_CURRENT_SOURCE_DIR}/periph_manager
- ${CMAKE_CURRENT_SOURCE_DIR}/dboard_manager
- PARENT_SCOPE
)
+LIST(APPEND USRP_MPM_FILES ${USRP_MPM_TOP_FILES})
+ADD_SUBDIRECTORY(periph_manager)
+ADD_SUBDIRECTORY(dboard_manager)
+
+SET(USRP_MPM_FILES ${USRP_MPM_FILES} PARENT_SCOPE)
diff --git a/mpm/python/usrp_mpm/__init__.py b/mpm/python/usrp_mpm/__init__.py
index c349d3d77..c7043c70c 100644
--- a/mpm/python/usrp_mpm/__init__.py
+++ b/mpm/python/usrp_mpm/__init__.py
@@ -19,7 +19,7 @@ MPM Module
"""
from discovery import spawn_discovery_process
-from rpc_process import spawn_rpc_process
+from rpc_server import spawn_rpc_process
import types
import periph_manager
import dboard_manager
diff --git a/mpm/python/usrp_mpm/dboard_manager/CMakeLists.txt b/mpm/python/usrp_mpm/dboard_manager/CMakeLists.txt
new file mode 100644
index 000000000..b642d506e
--- /dev/null
+++ b/mpm/python/usrp_mpm/dboard_manager/CMakeLists.txt
@@ -0,0 +1,30 @@
+#
+# Copyright 2017 Ettus Research (National Instruments)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as 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
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+########################################################################
+# This file included, use CMake directory variables
+########################################################################
+SET(USRP_MPM_FILES ${USRP_MPM_FILES})
+SET(USRP_MPM_DBMGR_FILES
+ ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/base.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/magnesium.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/eiscat.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/unknown.py
+ )
+LIST(APPEND USRP_MPM_FILES ${USRP_MPM_DBMGR_FILES})
+SET(USRP_MPM_FILES ${USRP_MPM_FILES} PARENT_SCOPE)
diff --git a/mpm/python/usrp_mpm/dboard_manager/__init__.py b/mpm/python/usrp_mpm/dboard_manager/__init__.py
index 774f348d4..02746e78f 100644
--- a/mpm/python/usrp_mpm/dboard_manager/__init__.py
+++ b/mpm/python/usrp_mpm/dboard_manager/__init__.py
@@ -20,6 +20,7 @@ dboards module __init__.py
from .. import libpyusrp_periphs as lib
from magnesium import magnesium
from eiscat import eiscat
+from test import test
from unknown import unknown
hw_pids = {
diff --git a/mpm/python/usrp_mpm/dboard_manager/base.py b/mpm/python/usrp_mpm/dboard_manager/base.py
index bf0a115cc..257a2424d 100644
--- a/mpm/python/usrp_mpm/dboard_manager/base.py
+++ b/mpm/python/usrp_mpm/dboard_manager/base.py
@@ -30,6 +30,7 @@ class dboard_manager(object):
Sanitizes arguments before calling C++ functions.
Ties various constants to specific daughterboard class
"""
+ _eeprom = {}
def __init__(self, eeprom={}):
self._eeprom = eeprom
diff --git a/mpm/python/usrp_mpm/dboard_manager/magnesium.py b/mpm/python/usrp_mpm/dboard_manager/magnesium.py
index 2201064ad..d48768208 100644
--- a/mpm/python/usrp_mpm/dboard_manager/magnesium.py
+++ b/mpm/python/usrp_mpm/dboard_manager/magnesium.py
@@ -20,6 +20,7 @@ magnesium dboard implementation module
from base import dboard_manager
from base import lib
from base import log
+import struct
class magnesium(dboard_manager):
hw_pid = 2
@@ -47,4 +48,3 @@ class magnesium(dboard_manager):
# magnesium eeprom contains
# nothing
return struct.unpack_from("x", data)
-
diff --git a/mpm/python/usrp_mpm/dboard_manager/test.py b/mpm/python/usrp_mpm/dboard_manager/test.py
new file mode 100644
index 000000000..b2e422bb4
--- /dev/null
+++ b/mpm/python/usrp_mpm/dboard_manager/test.py
@@ -0,0 +1,57 @@
+#
+# Copyright 2017 Ettus Research (National Instruments)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as 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
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+"""
+magnesium dboard implementation module
+"""
+from base import dboard_manager
+from base import log
+
+
+class fake_spi(object):
+ def __init__(self, addr):
+ self.addr = addr
+
+
+class test_device(object):
+ def __init__(self, dev1, dev2, dev3):
+ self.dev1 = fake_spi(dev1)
+ self.dev2 = fake_spi(dev2)
+ self.dev3 = fake_spi(dev3)
+
+ def test_method1(self, argument):
+ return argument
+
+
+class test(dboard_manager):
+ hw_pid = 234
+ special_eeprom_addrs = {"special0": "something"}
+ spi_chipselect = {"0": "dev1", "1": "dev2", "2": "dev3"}
+ spidevs = {}
+
+ def __init__(self, *args, **kwargs):
+ # eeprom_data is a tuple (head_dict, raw_data)
+ super(test, self).__init__(*args, **kwargs)
+ # I'm the test device, I can fake out my EEPROM
+ self.dev1 = "0"
+ self.dev2 = "1"
+ self.dev3 = "2"
+
+ def init_device(self):
+ self._device = test_device(self.dev1, self.dev2, self.dev3)
+
+
+
diff --git a/mpm/python/usrp_mpm/discovery.py b/mpm/python/usrp_mpm/discovery.py
index 727fe2b94..174866498 100644
--- a/mpm/python/usrp_mpm/discovery.py
+++ b/mpm/python/usrp_mpm/discovery.py
@@ -68,9 +68,9 @@ def _discovery_process(device_info, state):
try:
while True:
data, sender = sock.recvfrom(4096)
- if data == "MPM-DISC":
+ if data.strip("\0") == "MPM-DISC":
send_data = create_response_string()
send_sock.sendto(send_data, sender)
- except graceful_exit:
+ except:
sock.close()
- print("I'm done")
+ send_sock.close()
diff --git a/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt b/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt
new file mode 100644
index 000000000..879ac20c1
--- /dev/null
+++ b/mpm/python/usrp_mpm/periph_manager/CMakeLists.txt
@@ -0,0 +1,28 @@
+#
+# Copyright 2017 Ettus Research (National Instruments)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as 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
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+########################################################################
+# This file included, use CMake directory variables
+########################################################################
+SET(USRP_MPM_FILES ${USRP_MPM_FILES})
+SET(USRP_MPM_PERIPHMGR_FILES
+ ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py.in
+ ${CMAKE_CURRENT_SOURCE_DIR}/base.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/n310.py
+ )
+LIST(APPEND USRP_MPM_FILES ${USRP_MPM_TOP_FILES})
+SET(USRP_MPM_FILES ${USRP_MPM_FILES} PARENT_SCOPE)
diff --git a/mpm/python/usrp_mpm/periph_manager/__init__.py b/mpm/python/usrp_mpm/periph_manager/__init__.py.in
index 96eecf62e..0956b849e 100644
--- a/mpm/python/usrp_mpm/periph_manager/__init__.py
+++ b/mpm/python/usrp_mpm/periph_manager/__init__.py.in
@@ -24,6 +24,6 @@ from .. import dboard_manager
from .. import types
try:
- from n310 import n310 as periph_manager
+ from ${MPM_DEVICE} import ${MPM_DEVICE} as periph_manager
except ImportError:
- raise("Could not import n310")
+ raise Exception("Could not import ${MPM_DEVICE}")
diff --git a/mpm/python/usrp_mpm/periph_manager/n310.py b/mpm/python/usrp_mpm/periph_manager/n310.py
index e270387f6..d1c31540b 100644
--- a/mpm/python/usrp_mpm/periph_manager/n310.py
+++ b/mpm/python/usrp_mpm/periph_manager/n310.py
@@ -27,7 +27,7 @@ class n310(periph_manager):
dboard_eeprom_addrs = {"A": "something", "B": "else"}
dboard_spimaster_addrs = {"A": "something", "B": "else"}
- def __init__(self, eeprom_device, *args, **kwargs):
+ def __init__(self, *args, **kwargs):
# First initialize parent class - will populate self._eeprom_head and self._eeprom_rawdata
super(n310, self).__init__(*args, **kwargs)
data = self.read_eeprom_v1(self._eeprom_rawdata)
diff --git a/mpm/python/usrp_mpm/periph_manager/test.py b/mpm/python/usrp_mpm/periph_manager/test.py
new file mode 100644
index 000000000..c9cbc1f3f
--- /dev/null
+++ b/mpm/python/usrp_mpm/periph_manager/test.py
@@ -0,0 +1,63 @@
+#
+# Copyright 2017 Ettus Research (National Instruments)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as 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
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+"""
+test periph_manager implementation module
+"""
+from base import periph_manager
+from . import dboard_manager
+import random
+import string
+import struct
+
+
+class test(periph_manager):
+ hw_pids = "42"
+ mboard_eeprom_addr = None
+ dboard_eeprom_addrs = {"A": "something", "B": "else"}
+ dboard_spimaster_addrs = {"A": "something", "B": "else"}
+
+ def __init__(self, *args, **kwargs):
+ # First initialize parent class - will populate self._eeprom_head and self._eeprom_rawdata
+ # super(n310, self).__init__(*args, **kwargs)
+ # if header.get("dataversion", 0) == 1:
+ self._eeprom = self.read_eeprom_fake()
+ self._serial = "AABBCCDDEEFF"
+
+ # I'm the test periph_manager, I know I have test dboards attached
+ self.dboards = {
+ "A": dboard_manager.test(self.read_db_eeprom_random()),
+ "B": dboard_manager.test(self.read_db_eeprom_random())
+ }
+
+ def read_eeprom_fake(self):
+ fake_eeprom = {
+ "magic": 42,
+ "crc": 4242,
+ "data_version": 42,
+ "hw_pid": 42,
+ "hw_rev": 5
+ }
+
+ return fake_eeprom
+
+ def read_db_eeprom_random(self):
+ fake_eeprom = {
+ "serial": ''.join(
+ random.choice("ABCDEF" + string.digits)
+ for _ in range(16))
+ }
+ return fake_eeprom
diff --git a/mpm/python/usrp_mpm/rpc_process.py b/mpm/python/usrp_mpm/rpc_process.py
deleted file mode 100644
index b8783f325..000000000
--- a/mpm/python/usrp_mpm/rpc_process.py
+++ /dev/null
@@ -1,48 +0,0 @@
-#
-# Copyright 2017 Ettus Research (National Instruments)
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as 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
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-#
-"""
-Code to run the discovery port
-"""
-
-from __future__ import print_function
-from multiprocessing import Process
-
-from gevent.server import StreamServer
-from mprpc import RPCServer
-from types import graceful_exit, MPM_RPC_PORT
-import rpc_server
-
-
-
-def spawn_rpc_process(server, state, udp_port=MPM_RPC_PORT):
- """
- Returns a process that contains the RPC server
- """
-
- p_args = [server, udp_port, state]
- p = Process(target=_rpc_server_process, args=p_args)
- p.start()
- return p
-
-
-def _rpc_server_process(server, shared_state, port):
- try:
- rpc_class = getattr(rpc_server, server+"Server")
- server = StreamServer(('0.0.0.0', port), handle=rpc_class(shared_state))
- server.serve_forever()
- except graceful_exit:
- server.close()
diff --git a/mpm/python/usrp_mpm/rpc_server.py b/mpm/python/usrp_mpm/rpc_server.py
index a5dda0826..ddb588aa6 100644
--- a/mpm/python/usrp_mpm/rpc_server.py
+++ b/mpm/python/usrp_mpm/rpc_server.py
@@ -17,25 +17,57 @@
"""
Implemented RPC Servers
"""
-
from __future__ import print_function
+from gevent.server import StreamServer
+from types import graceful_exit, MPM_RPC_PORT
from mprpc import RPCServer
-from usrp_mpm import periphs
+from six import iteritems
+import time
+
+from multiprocessing import Process
+class MPMServer(RPCServer):
+ _db_methods = {}
+ def __init__(self, state, mgr):
+ self._state = state
+ # Instead do self.mboard = periphs.init_periph_manager(args...)
+ self.periph_manager = mgr
+ for db_slot, db in iteritems(mgr.dboards):
+ methods = (m for m in dir(db) if not m.startswith('_') and callable(getattr(db, m)))
+ for method in methods:
+ command_name = 'db_'+ db_slot + '_' + method
+ self._add_command(getattr(db,method), command_name)
+ db_methods = self._db_methods.get(db_slot, [])
+ db_methods.append(command_name)
+ self._db_methods.update({db_slot: db_methods})
-class EchoServer(RPCServer):
- def echo(self, arg):
- print(arg)
- return arg
+ # When we do init we can just add dboard/periph_manager methods with setattr(self, method)
+ # Maybe using partial
+ # To remove methods again we also have to remove them from self._methods dict (they're cached)
+ super(MPMServer, self).__init__()
+ def _add_command(self, function, command):
+ setattr(self, command, function)
-class ClaimServer(RPCServer):
- def __init__(self, state):
- self._state = state
- super(ClaimServer, self).__init__()
+
+ def list_methods(self):
+ """
+ Returns all public methods of this RPC server
+ """
+ methods = filter(lambda entry: not entry.startswith('_'), dir(self)) # Return public methods
+ methods_with_docs = map(lambda m: (m, getattr(self, m).__doc__), methods)
+ return methods_with_docs
+
+ def ping(self, data=None):
+ """
+ Take in data as argument and send it back
+ """
+ return data
def claim(self, token):
- 'claim `token` - claims the MPM device with given token'
+ """
+ claim `token` - claims the MPM device with given token
+ """
if self._state.claim_status.value:
if self._state.claim_token.value == token:
return True
@@ -45,28 +77,33 @@ class ClaimServer(RPCServer):
return True
def unclaim(self, token):
- 'unclaim `token` - unclaims the MPM device if it is claimed with this token'
+ """
+ unclaim `token` - unclaims the MPM device if it is claimed with this token
+ """
if self._state.claim_status.value and self._state.claim_token.value == token:
self._state.claim_status.value = False
self._state.claim_token.value = ""
return True
return False
- def list_methods(self):
- methods = filter(lambda entry: not entry.startswith('_'), dir(self)) # Return public methods
- methods_with_docs = map(lambda m: (m, getattr(self,m).__doc__), methods)
- return methods_with_docs
+def _rpc_server_process(shared_state, port, mgr):
+ """
+ Start the RPC server
+ """
+ server = StreamServer(('0.0.0.0', port), handle=MPMServer(shared_state, mgr))
+ try:
+ server.serve_forever()
+ except:
+ server.close()
-class MPMServer(RPCServer):
- def __init__(self, state):
- # Instead do self.mboard = periphs.init_periph_manager(args...)
- self.periph_manager = periphs.init_periph_manager()
- # When we do init we can just add dboard/periph_manager methods with setattr(self, method)
- # Maybe using partial
- # To remove methods again we also have to remove them from self._methods dict (they're cached)
- def get_clock_id(self, dboard):
- dboard = getattr(self.mboard, "get_dboard_"+dboard)
- clk = dboard.get_clock_gen()
- return clk.get_chip_id()
+def spawn_rpc_process(state, udp_port, mgr):
+ """
+ Returns a process that contains the RPC server
+ """
+
+ p_args = [udp_port, state, mgr]
+ p = Process(target=_rpc_server_process, args=p_args)
+ p.start()
+ return p