Windows Cert Testing (#943)

* PR for cert test interface for Windows driver model.
This commit is contained in:
Nick Banks
2016-11-29 12:10:34 -08:00
committed by Jonathan Hui
parent c2598bf4c9
commit 24b98dbb85
28 changed files with 4722 additions and 604 deletions
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/python
#
# Copyright (c) 2016, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import unittest
import ctypes
class GUID(ctypes.Structure):
_fields_ = [("Data1", ctypes.c_uint),
("Data2", ctypes.c_ushort),
("Data3", ctypes.c_ushort),
("Data4", ctypes.c_ubyte * 8)]
class otDeviceList(ctypes.Structure):
_fields_ = [("aDevicesLength", ctypes.c_ushort),
("aDevices", GUID * 64)]
class Cert_otLwf(unittest.TestCase):
def setUp(self):
# Load the DLL
self.Api = ctypes.WinDLL("otApi.dll")
if self.Api == None:
raise OSError("Failed to load otApi.dll!")
# Define the functions
self.Api.otApiInit.restype = ctypes.c_void_p
self.Api.otApiFinalize.argtypes = [ctypes.c_void_p]
self.Api.otApiFinalize.restype = None
self.Api.otFreeMemory.argtypes = [ctypes.c_void_p]
self.Api.otFreeMemory.restype = None
self.Api.otEnumerateDevices.argtypes = [ctypes.c_void_p]
self.Api.otEnumerateDevices.restype = ctypes.POINTER(otDeviceList)
def tearDown(self):
if self.ApiInstance:
self.Api.otApiFinalize(self.ApiInstance)
def test(self):
# Instantiate the API
self.ApiInstance = self.Api.otApiInit()
# Assert that it didn't return NULL
self.assertNotEqual(self.ApiInstance, None)
# Query the device list
devices = self.Api.otEnumerateDevices(self.ApiInstance)
# Assert that it didn't return NULL
self.assertNotEqual(devices, None)
# Print the number of devices
print("devices found: %d" % devices.contents.aDevicesLength)
if __name__ == '__main__':
unittest.main()
+79 -430
View File
@@ -30,568 +30,217 @@
import os
import sys
import time
import pexpect
if sys.platform != 'win32':
import node_cli
else:
import node_api
import unittest
class Node:
def __init__(self, nodeid):
self.nodeid = nodeid
self.verbose = int(float(os.getenv('VERBOSE', 0)))
self.node_type = os.getenv('NODE_TYPE', 'sim')
if self.node_type == 'soc':
self.__init_soc(nodeid)
elif self.node_type == 'ncp-sim':
self.__init_ncp_sim(nodeid)
if sys.platform != 'win32':
self.interface = node_cli.otCli(nodeid)
else:
self.__init_sim(nodeid)
self.interface = node_api.otApi(nodeid)
if self.verbose:
self.pexpect.logfile_read = sys.stdout
self.clear_whitelist()
self.disable_whitelist()
self.set_timeout(100)
def __init_sim(self, nodeid):
""" Initialize a simulation node. """
if "OT_CLI_PATH" in os.environ.keys():
cmd = os.environ['OT_CLI_PATH']
elif "top_builddir" in os.environ.keys():
srcdir = os.environ['top_builddir']
cmd = '%s/examples/apps/cli/ot-cli-ftd' % srcdir
else:
cmd = './ot-cli-ftd'
cmd += ' %d' % nodeid
print ("%s" % cmd)
self.pexpect = pexpect.spawn(cmd, timeout=4)
# Add delay to ensure that the process is ready to receive commands.
time.sleep(0.2)
def __init_ncp_sim(self, nodeid):
""" Initialize an NCP simulation node. """
if "top_builddir" in os.environ.keys():
builddir = os.environ['top_builddir']
if "top_srcdir" in os.environ.keys():
srcdir = os.environ['top_srcdir']
else:
srcdir = os.path.dirname(os.path.realpath(__file__))
srcdir += "/../../.."
cmd = 'python %s/tools/spinel-cli/spinel-cli.py -p %s/examples/apps/ncp/ot-ncp -n' % (srcdir, builddir)
else:
cmd = './ot-ncp'
cmd += ' %d' % nodeid
print ("%s" % cmd)
self.pexpect = pexpect.spawn(cmd, timeout=4)
time.sleep(0.2)
self.pexpect.expect('spinel-cli >')
self.debug(int(os.getenv('DEBUG', '0')))
def __init_soc(self, nodeid):
""" Initialize a System-on-a-chip node connected via UART. """
import fdpexpect
serialPort = '/dev/ttyUSB%d' % ((nodeid-1)*2)
self.pexpect = fdpexpect.fdspawn(os.open(serialPort, os.O_RDWR|os.O_NONBLOCK|os.O_NOCTTY))
self.interface.clear_whitelist()
self.interface.disable_whitelist()
self.interface.set_timeout(100)
def __del__(self):
if self.pexpect.isalive():
self.send_command('exit')
self.pexpect.expect(pexpect.EOF)
self.pexpect.terminate()
self.pexpect.close(force=True)
del self.interface
def send_command(self, cmd):
print ("%d: %s" % (self.nodeid, cmd))
self.pexpect.sendline(cmd)
def get_commands(self):
self.send_command('?')
self.pexpect.expect('Commands:')
commands = []
while True:
i = self.pexpect.expect(['Done', '(\S+)'])
if i != 0:
commands.append(self.pexpect.match.groups()[0])
else:
break
return commands
def set_mode(self, mode):
cmd = 'mode ' + mode
self.send_command(cmd)
self.pexpect.expect('Done')
def set_mode(self, mode):
self.interface.set_mode(mode)
def debug(self, level):
self.send_command('debug '+str(level))
self.interface.debug(level)
def interface_up(self):
self.send_command('ifconfig up')
self.pexpect.expect('Done')
self.interface.interface_up()
def interface_down(self):
self.send_command('ifconfig down')
self.pexpect.expect('Done')
self.interface.interface_down()
def thread_start(self):
self.send_command('thread start')
self.pexpect.expect('Done')
self.interface.thread_start()
def thread_stop(self):
self.send_command('thread stop')
self.pexpect.expect('Done')
self.interface.thread_stop()
def commissioner_start(self):
cmd = 'commissioner start'
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.commissioner_start()
def commissioner_add_joiner(self, addr, psk):
cmd = 'commissioner joiner add ' + addr + ' ' + psk
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.commissioner_add_joiner(addr, psk)
def joiner_start(self, pskd='', provisioning_url=''):
cmd = 'joiner start ' + pskd + ' ' + provisioning_url
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.joiner_start(pskd, provisioning_url)
def start(self):
self.interface_up()
self.thread_start()
self.interface.interface_up()
self.interface.thread_start()
def stop(self):
self.thread_stop()
self.interface_down()
self.interface.thread_stop()
self.interface.interface_down()
def clear_whitelist(self):
self.send_command('whitelist clear')
self.pexpect.expect('Done')
self.interface.clear_whitelist()
def enable_whitelist(self):
self.send_command('whitelist enable')
self.pexpect.expect('Done')
self.interface.enable_whitelist()
def disable_whitelist(self):
self.send_command('whitelist disable')
self.pexpect.expect('Done')
self.interface.disable_whitelist()
def add_whitelist(self, addr, rssi=None):
cmd = 'whitelist add ' + addr
if rssi != None:
cmd += ' ' + str(rssi)
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.add_whitelist(addr, rssi)
def remove_whitelist(self, addr):
cmd = 'whitelist remove ' + addr
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.remove_whitelist(addr)
def get_addr16(self):
self.send_command('rloc16')
i = self.pexpect.expect('([0-9a-fA-F]{4})')
if i == 0:
addr16 = int(self.pexpect.match.groups()[0], 16)
self.pexpect.expect('Done')
return addr16
return self.interface.get_addr16()
def get_addr64(self):
self.send_command('extaddr')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
if i == 0:
addr64 = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
return addr64
return self.interface.get_addr64()
def get_hashmacaddr(self):
self.send_command('hashmacaddr')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
if i == 0:
addr = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
return addr
return self.interface.get_hashmacaddr()
def get_channel(self):
self.send_command('channel')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
channel = int(self.pexpect.match.groups()[0])
self.pexpect.expect('Done')
return channel
return self.interface.get_channel()
def set_channel(self, channel):
cmd = 'channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_channel(channel)
def get_masterkey(self):
self.send_command('masterkey')
i = self.pexpect.expect('([0-9a-fA-F]{32})')
if i == 0:
masterkey = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
return masterkey
return self.interface.get_masterkey()
def set_masterkey(self, masterkey):
cmd = 'masterkey ' + masterkey
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_masterkey(masterkey)
def get_key_sequence_counter(self):
self.send_command('keysequence counter')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
key_sequence_counter = int(self.pexpect.match.groups()[0])
self.pexpect.expect('Done')
return key_sequence_counter
return self.interface.get_key_sequence_counter()
def set_key_sequence_counter(self, key_sequence_counter):
cmd = 'keysequence counter %d' % key_sequence_counter
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_key_sequence_counter(key_sequence_counter)
def set_key_switch_guardtime(self, key_switch_guardtime):
cmd = 'keysequence guardtime %d' % key_switch_guardtime
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_key_switch_guardtime(key_switch_guardtime)
def set_network_id_timeout(self, network_id_timeout):
cmd = 'networkidtimeout %d' % network_id_timeout
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_network_id_timeout(network_id_timeout)
def get_network_name(self):
self.send_command('networkname')
while True:
i = self.pexpect.expect(['Done', '(\S+)'])
if i != 0:
network_name = self.pexpect.match.groups()[0].decode('utf-8')
else:
break
return network_name
return self.interface.get_network_name()
def set_network_name(self, network_name):
cmd = 'networkname ' + network_name
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_network_name(network_name)
def get_panid(self):
self.send_command('panid')
i = self.pexpect.expect('([0-9a-fA-F]{4})')
if i == 0:
panid = int(self.pexpect.match.groups()[0], 16)
self.pexpect.expect('Done')
return panid
return self.interface.get_panid()
def set_panid(self, panid):
cmd = 'panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_panid(panid)
def get_partition_id(self):
self.send_command('leaderpartitionid')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
weight = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return weight
return self.interface.get_partition_id()
def set_partition_id(self, partition_id):
cmd = 'leaderpartitionid %d' % partition_id
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_partition_id(partition_id)
def set_router_upgrade_threshold(self, threshold):
cmd = 'routerupgradethreshold %d' % threshold
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_router_upgrade_threshold(threshold)
def set_router_downgrade_threshold(self, threshold):
cmd = 'routerdowngradethreshold %d' % threshold
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_router_downgrade_threshold(threshold)
def release_router_id(self, router_id):
cmd = 'releaserouterid %d' % router_id
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.release_router_id(router_id)
def get_state(self):
states = ['detached', 'child', 'router', 'leader']
self.send_command('state')
match = self.pexpect.expect(states)
self.pexpect.expect('Done')
return states[match]
return self.interface.get_state()
def set_state(self, state):
cmd = 'state ' + state
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_state(state)
def get_timeout(self):
self.send_command('childtimeout')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
timeout = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return timeout
return self.interface.get_timeout()
def set_timeout(self, timeout):
cmd = 'childtimeout %d' % timeout
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_timeout(timeout)
def set_max_children(self, number):
cmd = 'childmax %d' % number
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_max_children(number)
def get_weight(self):
self.send_command('leaderweight')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
weight = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return weight
return self.interface.get_weight()
def set_weight(self, weight):
cmd = 'leaderweight %d' % weight
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_weight(weight)
def add_ipaddr(self, ipaddr):
cmd = 'ipaddr add ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.add_ipaddr(ipaddr)
def get_addrs(self):
addrs = []
self.send_command('ipaddr')
while True:
i = self.pexpect.expect(['(\S+:\S+)\r\n', 'Done'])
if i == 0:
addrs.append(self.pexpect.match.groups()[0].decode("utf-8"))
elif i == 1:
break
return addrs
return self.interface.get_addrs()
def get_context_reuse_delay(self):
self.send_command('contextreusedelay')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
timeout = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return timeout
return self.interface.get_context_reuse_delay()
def set_context_reuse_delay(self, delay):
cmd = 'contextreusedelay %d' % delay
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_context_reuse_delay(delay)
def add_prefix(self, prefix, flags, prf = 'med'):
cmd = 'prefix add ' + prefix + ' ' + flags + ' ' + prf
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.add_prefix(prefix, flags, prf)
def remove_prefix(self, prefix):
cmd = ' prefix remove ' + prefix
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.remove_prefix(prefix)
def add_route(self, prefix, prf = 'med'):
cmd = 'route add ' + prefix + ' ' + prf
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.add_route(prefix, prf)
def remove_route(self, prefix):
cmd = 'route remove ' + prefix
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.remove_route(prefix)
def register_netdata(self):
self.send_command('netdataregister')
self.pexpect.expect('Done')
self.interface.register_netdata()
def energy_scan(self, mask, count, period, scan_duration, ipaddr):
cmd = 'commissioner energy ' + str(mask) + ' ' + str(count) + ' ' + str(period) + ' ' + str(scan_duration) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Energy:', timeout=8)
self.interface.energy_scan(mask, count, period, scan_duration, ipaddr)
def panid_query(self, panid, mask, ipaddr):
cmd = 'commissioner panid ' + str(panid) + ' ' + str(mask) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Conflict:', timeout=8)
self.interface.panid_query(panid, mask, ipaddr)
def scan(self):
self.send_command('scan')
results = []
while True:
i = self.pexpect.expect(['\|\s(\S+)\s+\|\s(\S+)\s+\|\s([0-9a-fA-F]{4})\s\|\s([0-9a-fA-F]{16})\s\|\s(\d+)\r\n',
'Done'])
if i == 0:
results.append(self.pexpect.match.groups())
else:
break
return results
return self.interface.scan()
def ping(self, ipaddr, num_responses=1, size=None):
cmd = 'ping ' + ipaddr
if size != None:
cmd += ' ' + str(size)
self.send_command(cmd)
result = True
try:
responders = {}
while len(responders) < num_responses:
i = self.pexpect.expect(['from (\S+):'])
if i == 0:
responders[self.pexpect.match.groups()[0]] = 1
self.pexpect.expect('\n')
except pexpect.TIMEOUT:
result = False
return result
return self.interface.ping(ipaddr, num_responses, size)
def set_router_selection_jitter(self, jitter):
cmd = 'routerselectionjitter %d' % jitter
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.set_router_selection_jitter(jitter)
def set_active_dataset(self, timestamp, panid=None, channel=None, channel_mask=None, master_key=None):
self.send_command('dataset clear')
self.pexpect.expect('Done')
cmd = 'dataset activetimestamp %d' % timestamp
self.send_command(cmd)
self.pexpect.expect('Done')
if panid != None:
cmd = 'dataset panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
if channel != None:
cmd = 'dataset channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
if channel_mask != None:
cmd = 'dataset channelmask %d' % channel_mask
self.send_command(cmd)
self.pexpect.expect('Done')
if master_key != None:
cmd = 'dataset masterkey ' + master_key
self.send_command(cmd)
self.pexpect.expect('Done')
self.send_command('dataset commit active')
self.pexpect.expect('Done')
self.interface.set_active_dataset(timestamp, panid, channel, channel_mask, master_key)
def set_pending_dataset(self, pendingtimestamp, activetimestamp, panid=None, channel=None):
self.send_command('dataset clear')
self.pexpect.expect('Done')
cmd = 'dataset pendingtimestamp %d' % pendingtimestamp
self.send_command(cmd)
self.pexpect.expect('Done')
cmd = 'dataset activetimestamp %d' % activetimestamp
self.send_command(cmd)
self.pexpect.expect('Done')
if panid != None:
cmd = 'dataset panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
if channel != None:
cmd = 'dataset channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
self.send_command('dataset commit pending')
self.pexpect.expect('Done')
self.interface.set_pending_dataset(pendingtimestamp, activetimestamp, panid, channel)
def announce_begin(self, mask, count, period, ipaddr):
cmd = 'commissioner announce ' + str(mask) + ' ' + str(count) + ' ' + str(period) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.announce_begin(mask, count, period, ipaddr)
def send_mgmt_active_set(self, active_timestamp=None, channel=None, channel_mask=None, extended_panid=None,
panid=None, master_key=None, mesh_local=None, network_name=None, binary=None):
cmd = 'dataset mgmtsetcommand active '
if active_timestamp != None:
cmd += 'activetimestamp %d ' % active_timestamp
if channel != None:
cmd += 'channel %d ' % channel
if channel_mask != None:
cmd += 'channelmask %d ' % channel_mask
if extended_panid != None:
cmd += 'extpanid ' + extended_panid + ' '
if panid != None:
cmd += 'panid %d ' % panid
if master_key != None:
cmd += 'masterkey ' + master_key + ' '
if mesh_local != None:
cmd += 'localprefix ' + mesh_local + ' '
if network_name != None:
cmd += 'networkname ' + network_name + ' '
if binary != None:
cmd += 'binary ' + binary + ' '
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.send_mgmt_active_set(active_timestamp, channel, channel_mask, extended_panid, panid,
master_key, mesh_local, network_name, binary)
def send_mgmt_pending_set(self, pending_timestamp=None, active_timestamp=None, delay_timer=None, channel=None,
panid=None, master_key=None, mesh_local=None, network_name=None):
cmd = 'dataset mgmtsetcommand pending '
if pending_timestamp != None:
cmd += 'pendingtimestamp %d ' % pending_timestamp
if active_timestamp != None:
cmd += 'activetimestamp %d ' % active_timestamp
if delay_timer != None:
cmd += 'delaytimer %d ' % delay_timer
if channel != None:
cmd += 'channel %d ' % channel
if panid != None:
cmd += 'panid %d ' % panid
if master_key != None:
cmd += 'masterkey ' + master_key + ' '
if mesh_local != None:
cmd += 'localprefix ' + mesh_local + ' '
if network_name != None:
cmd += 'networkname ' + network_name + ' '
self.send_command(cmd)
self.pexpect.expect('Done')
self.interface.send_mgmt_pending_set(pending_timestamp, active_timestamp, delay_timer, channel, panid,
master_key, mesh_local, network_name)
if __name__ == '__main__':
unittest.main()
+588
View File
@@ -0,0 +1,588 @@
#!/usr/bin/python
#
# Copyright (c) 2016, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import os
import sys
import time
import ctypes
class otApi:
def __init__(self, nodeid):
self.verbose = int(float(os.getenv('VERBOSE', 0)))
self.__init_dll(nodeid)
def __del__(self):
self.Api.otNodeFinalize(self.otNode)
def set_mode(self, mode):
if self.Api.otNodeSetMode(self.otNode, mode.encode('utf-8')) != 0:
raise OSError("otNodeSetMode failed!")
def interface_up(self):
if self.Api.otNodeInterfaceUp(self.otNode) != 0:
raise OSError("otNodeInterfaceUp failed!")
def interface_down(self):
if self.Api.otNodeInterfaceDown(self.otNode) != 0:
raise OSError("otNodeInterfaceDown failed!")
def thread_start(self):
if self.Api.otNodeThreadStart(self.otNode) != 0:
raise OSError("otNodeThreadStart failed!")
def thread_stop(self):
if self.Api.otNodeThreadStop(self.otNode) != 0:
raise OSError("otNodeThreadStop failed!")
def commissioner_start(self):
if self.Api.otNodeCommissionerStart(self.otNode) != 0:
raise OSError("otNodeCommissionerStart failed!")
def commissioner_add_joiner(self, addr, psk):
if self.Api.otNodeCommissionerJoinerAdd(self.otNode, addr.encode('utf-8'), psk.encode('utf-8')) != 0:
raise OSError("otNodeCommissionerJoinerAdd failed!")
def joiner_start(self, pskd='', provisioning_url=''):
if self.Api.otNodeJoinerStart(self.otNode, pskd.encode('utf-8'), provisioning_url.encode('utf-8')) != 0:
raise OSError("otNodeJoinerStart failed!")
def clear_whitelist(self):
if self.Api.otNodeClearWhitelist(self.otNode) != 0:
raise OSError("otNodeClearWhitelist failed!")
def enable_whitelist(self):
if self.Api.otNodeEnableWhitelist(self.otNode) != 0:
raise OSError("otNodeEnableWhitelist failed!")
def disable_whitelist(self):
if self.Api.otNodeDisableWhitelist(self.otNode) != 0:
raise OSError("otNodeDisableWhitelist failed!")
def add_whitelist(self, addr, rssi=None):
if rssi == None:
rssi = 0
if self.Api.otNodeAddWhitelist(self.otNode, addr.encode('utf-8'), ctypes.c_byte(rssi)) != 0:
raise OSError("otNodeAddWhitelist failed!")
def remove_whitelist(self, addr):
if self.Api.otNodeRemoveWhitelist(self.otNode, addr.encode('utf-8')) != 0:
raise OSError("otNodeRemoveWhitelist failed!")
def get_addr16(self):
return self.Api.otNodeGetAddr16(self.otNode)
def get_addr64(self):
return self.Api.otNodeGetAddr64(self.otNode).decode('utf-8')
def get_hashmacaddr(self):
return self.Api.otNodeGetHashMacAddress(self.otNode).decode('utf-8')
def get_channel(self):
return self.Api.otNodeGetChannel(self.otNode)
def set_channel(self, channel):
if self.Api.otNodeSetChannel(self.otNode, ctypes.c_ubyte(channel)) != 0:
raise OSError("otNodeSetChannel failed!")
def get_masterkey(self):
return self.Api.otNodeGetMasterkey(self.otNode).decode("utf-8")
def set_masterkey(self, masterkey):
if self.Api.otNodeSetMasterkey(self.otNode, masterkey.encode('utf-8')) != 0:
raise OSError("otNodeSetMasterkey failed!")
def get_key_sequence_counter(self):
return self.Api.otNodeGetKeySequenceCounter(self.otNode)
def set_key_sequence_counter(self, key_sequence_counter):
if self.Api.otNodeSetKeySequenceCounter(self.otNode, ctypes.c_uint(key_sequence_counter)) != 0:
raise OSError("otNodeSetKeySequenceCounter failed!")
def set_key_switch_guardtime(self, key_switch_guardtime):
if self.Api.otNodeSetKeySwitchGuardTime(self.otNode, ctypes.c_uint(key_switch_guardtime)) != 0:
raise OSError("otNodeSetKeySwitchGuardTime failed!")
def set_network_id_timeout(self, network_id_timeout):
if self.Api.otNodeSetNetworkIdTimeout(self.otNode, ctypes.c_ubyte(network_id_timeout)) != 0:
raise OSError("otNodeSetNetworkIdTimeout failed!")
def get_network_name(self):
return self.Api.otNodeGetNetworkName(self.otNode).decode("utf-8")
def set_network_name(self, network_name):
if self.Api.otNodeSetNetworkName(self.otNode, network_name.encode('utf-8')) != 0:
raise OSError("otNodeSetNetworkName failed!")
def get_panid(self):
return int(self.Api.otNodeGetPanId(self.otNode))
def set_panid(self, panid):
if self.Api.otNodeSetPanId(self.otNode, ctypes.c_ushort(panid)) != 0:
raise OSError("otNodeSetPanId failed!")
def get_partition_id(self):
return int(self.Api.otNodeGetPartitionId(self.otNode))
def set_partition_id(self, partition_id):
if self.Api.otNodeSetPartitionId(self.otNode, ctypes.c_uint(partition_id)) != 0:
raise OSError("otNodeSetPartitionId failed!")
def set_router_upgrade_threshold(self, threshold):
if self.Api.otNodeSetRouterUpgradeThreshold(self.otNode, ctypes.c_ubyte(threshold)) != 0:
raise OSError("otNodeSetRouterUpgradeThreshold failed!")
def set_router_downgrade_threshold(self, threshold):
if self.Api.otNodeSetRouterDowngradeThreshold(self.otNode, ctypes.c_ubyte(threshold)) != 0:
raise OSError("otNodeSetRouterDowngradeThreshold failed!")
def release_router_id(self, router_id):
if self.Api.otNodeReleaseRouterId(self.otNode, ctypes.c_ubyte(router_id)) != 0:
raise OSError("otNodeReleaseRouterId failed!")
def get_state(self):
return self.Api.otNodeGetState(self.otNode).decode('utf-8')
def set_state(self, state):
if self.Api.otNodeSetState(self.otNode, state.encode('utf-8')) != 0:
raise OSError("otNodeSetState failed!")
def get_timeout(self):
return int(self.Api.otNodeGetTimeout(self.otNode))
def set_timeout(self, timeout):
if self.Api.otNodeSetTimeout(self.otNode, ctypes.c_uint(timeout)) != 0:
raise OSError("otNodeSetTimeout failed!")
def set_max_children(self, number):
if self.Api.otNodeSetMaxChildren(self.otNode, ctypes.c_ubyte(number)) != 0:
raise OSError("otNodeSetMaxChildren failed!")
def get_weight(self):
return int(self.Api.otNodeGetWeight(self.otNode))
def set_weight(self, weight):
if self.Api.otNodeSetWeight(self.otNode, ctypes.c_ubyte(weight)) != 0:
raise OSError("otNodeSetWeight failed!")
def add_ipaddr(self, ipaddr):
if self.Api.otNodeAddIpAddr(self.otNode, ipaddr.encode('utf-8')) != 0:
raise OSError("otNodeAddIpAddr failed!")
def get_addrs(self):
return self.Api.otNodeGetAddrs(self.otNode).decode("utf-8").split("\n")
def get_context_reuse_delay(self):
return int(self.Api.otNodeGetContextReuseDelay(self.otNode))
def set_context_reuse_delay(self, delay):
if self.Api.otNodeSetContextReuseDelay(self.otNode, ctypes.c_uint(delay)) != 0:
raise OSError("otNodeSetContextReuseDelay failed!")
def add_prefix(self, prefix, flags, prf = 'med'):
if self.Api.otNodeAddPrefix(self.otNode, prefix.encode('utf-8'), flags.encode('utf-8'), prf.encode('utf-8')) != 0:
raise OSError("otNodeAddPrefix failed!")
def remove_prefix(self, prefix):
if self.Api.otNodeRemovePrefix(self.otNode, prefix.encode('utf-8')) != 0:
raise OSError("otNodeRemovePrefix failed!")
def add_route(self, prefix, prf = 'med'):
if self.Api.otNodeAddRoute(self.otNode, prefix.encode('utf-8'), prf.encode('utf-8')) != 0:
raise OSError("otNodeAddRoute failed!")
def remove_route(self, prefix):
if self.Api.otNodeRemoveRoute(self.otNode, prefix.encode('utf-8')) != 0:
raise OSError("otNodeRemovePrefix failed!")
def register_netdata(self):
if self.Api.otNodeRegisterNetdata(self.otNode) != 0:
raise OSError("otNodeRegisterNetdata failed!")
def energy_scan(self, mask, count, period, scan_duration, ipaddr):
if self.Api.otNodeEnergyScan(self.otNode, ctypes.c_uint(mask), ctypes.c_ubyte(count), ctypes.c_ushort(period), ctypes.c_ushort(scan_duration), ipaddr.encode('utf-8')) != 0:
raise OSError("otNodeEnergyScan failed!")
def panid_query(self, panid, mask, ipaddr):
if self.Api.otNodePanIdQuery(self.otNode, ctypes.c_ushort(panid), ctypes.c_uint(mask), ipaddr.encode('utf-8')) != 0:
raise OSError("otNodePanIdQuery failed!")
def scan(self):
return self.Api.otNodeScan(self.otNode).decode("utf-8").split("\n")
def ping(self, ipaddr, num_responses=1, size=None):
if size == None:
size = 100
numberOfResponders = self.Api.otNodePing(self.otNode, ipaddr.encode('utf-8'), ctypes.c_ushort(size), ctypes.c_uint(num_responses))
return numberOfResponders >= num_responses
def set_router_selection_jitter(self, jitter):
if self.Api.otNodeSetRouterSelectionJitter(self.otNode, ctypes.c_ubyte(jitter)) != 0:
raise OSError("otNodeSetRouterSelectionJitter failed!")
def set_active_dataset(self, timestamp, panid=None, channel=None, channel_mask=None, master_key=None):
if panid == None:
panid = 0
if channel == None:
channel = 0
if channel_mask == None:
channel_mask = 0
if master_key == None:
master_key = ""
if self.Api.otNodeSetActiveDataset(
self.otNode,
ctypes.c_ulonglong(timestamp),
ctypes.c_ushort(panid),
ctypes.c_ushort(channel),
ctypes.c_uint(channel_mask),
master_key.encode('utf-8')
) != 0:
raise OSError("otNodeSetActiveDataset failed!")
def set_pending_dataset(self, pendingtimestamp, activetimestamp, panid=None, channel=None):
if pendingtimestamp == None:
pendingtimestamp = 0
if activetimestamp == None:
activetimestamp = 0
if panid == None:
panid = 0
if channel == None:
channel = 0
if self.Api.otNodeSetPendingDataset(
self.otNode,
ctypes.c_ulonglong(activetimestamp),
ctypes.c_ulonglong(pendingtimestamp),
ctypes.c_ushort(panid),
ctypes.c_ushort(channel)
) != 0:
raise OSError("otNodeSetPendingDataset failed!")
def announce_begin(self, mask, count, period, ipaddr):
if self.Api.otNodeCommissionerAnnounceBegin(self.otNode, ctypes.c_uint(mask), ctypes.c_ubyte(count), ctypes.c_ushort(period), ipaddr.encode('utf-8')) != 0:
raise OSError("otNodeCommissionerAnnounceBegin failed!")
def send_mgmt_active_set(self, active_timestamp=None, channel=None, channel_mask=None, extended_panid=None,
panid=None, master_key=None, mesh_local=None, network_name=None, binary=None):
if active_timestamp == None:
active_timestamp = 0
if panid == None:
panid = 0
if channel == None:
channel = 0
if channel_mask == None:
channel_mask = 0
if extended_panid == None:
extended_panid = ""
if master_key == None:
master_key = ""
if mesh_local == None:
mesh_local = ""
if network_name == None:
network_name = ""
if binary == None:
binary = ""
if self.Api.otNodeSendActiveSet(
self.otNode,
ctypes.c_ulonglong(active_timestamp),
ctypes.c_ushort(panid),
ctypes.c_ushort(channel),
ctypes.c_uint(channel_mask),
extended_panid.encode('utf-8'),
master_key.encode('utf-8'),
mesh_local.encode('utf-8'),
network_name.encode('utf-8'),
binary.encode('utf-8')
) != 0:
raise OSError("otNodeSendActiveSet failed!")
def send_mgmt_pending_set(self, pending_timestamp=None, active_timestamp=None, delay_timer=None, channel=None,
panid=None, master_key=None, mesh_local=None, network_name=None):
if pending_timestamp == None:
pending_timestamp = 0
if active_timestamp == None:
active_timestamp = 0
if delay_timer == None:
delay_timer = 0
if panid == None:
panid = 0
if channel == None:
channel = 0
if master_key == None:
master_key = ""
if mesh_local == None:
mesh_local = ""
if network_name == None:
network_name = ""
if self.Api.otNodeSendPendingSet(
self.otNode,
ctypes.c_ulonglong(active_timestamp),
ctypes.c_ulonglong(pending_timestamp),
ctypes.c_uint(delay_timer),
ctypes.c_ushort(panid),
ctypes.c_ushort(channel),
master_key.encode('utf-8'),
mesh_local.encode('utf-8'),
network_name.encode('utf-8')
) != 0:
raise OSError("otNodeSendPendingSet failed!")
def log(self, message):
self.Api.otNodeLog(message)
def __init_dll(self, nodeid):
""" Initialize the API from a Windows DLL. """
# Load the DLL
self.Api = ctypes.WinDLL("otnodeapi.dll")
if self.Api == None:
raise OSError("Failed to load otnodeapi.dll!")
# Define the functions
self.Api.otNodeLog.argtypes = [ctypes.c_char_p]
self.Api.otNodeInit.argtypes = [ctypes.c_uint]
self.Api.otNodeInit.restype = ctypes.c_void_p
self.Api.otNodeFinalize.argtypes = [ctypes.c_void_p]
self.Api.otNodeSetMode.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeInterfaceUp.argtypes = [ctypes.c_void_p]
self.Api.otNodeInterfaceDown.argtypes = [ctypes.c_void_p]
self.Api.otNodeThreadStart.argtypes = [ctypes.c_void_p]
self.Api.otNodeThreadStop.argtypes = [ctypes.c_void_p]
self.Api.otNodeCommissionerStart.argtypes = [ctypes.c_void_p]
self.Api.otNodeCommissionerJoinerAdd.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
self.Api.otNodeCommissionerStop.argtypes = [ctypes.c_void_p]
self.Api.otNodeJoinerStart.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
self.Api.otNodeJoinerStop.argtypes = [ctypes.c_void_p]
self.Api.otNodeClearWhitelist.argtypes = [ctypes.c_void_p]
self.Api.otNodeEnableWhitelist.argtypes = [ctypes.c_void_p]
self.Api.otNodeDisableWhitelist.argtypes = [ctypes.c_void_p]
self.Api.otNodeAddWhitelist.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_byte]
self.Api.otNodeRemoveWhitelist.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeGetAddr16.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetAddr16.restype = ctypes.c_ushort
self.Api.otNodeGetAddr64.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetAddr64.restype = ctypes.c_char_p
self.Api.otNodeGetHashMacAddress.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetHashMacAddress.restype = ctypes.c_char_p
self.Api.otNodeSetChannel.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeGetChannel.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetChannel.restype = ctypes.c_ubyte
self.Api.otNodeSetMasterkey.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeGetMasterkey.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetMasterkey.restype = ctypes.c_char_p
self.Api.otNodeGetKeySequenceCounter.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetKeySequenceCounter.restype = ctypes.c_uint
self.Api.otNodeSetKeySequenceCounter.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
self.Api.otNodeSetKeySwitchGuardTime.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
self.Api.otNodeSetNetworkIdTimeout.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeGetNetworkName.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetNetworkName.restype = ctypes.c_char_p
self.Api.otNodeSetNetworkName.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeGetPanId.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetPanId.restype = ctypes.c_ushort
self.Api.otNodeSetPanId.argtypes = [ctypes.c_void_p,
ctypes.c_ushort]
self.Api.otNodeGetPartitionId.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetPartitionId.restype = ctypes.c_uint
self.Api.otNodeSetPartitionId.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
self.Api.otNodeSetRouterUpgradeThreshold.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeSetRouterDowngradeThreshold.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeReleaseRouterId.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeGetState.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetState.restype = ctypes.c_char_p
self.Api.otNodeSetState.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeGetTimeout.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetTimeout.restype = ctypes.c_uint
self.Api.otNodeSetTimeout.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
self.Api.otNodeGetWeight.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetWeight.restype = ctypes.c_ubyte
self.Api.otNodeSetWeight.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeAddIpAddr.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeGetAddrs.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetAddrs.restype = ctypes.c_char_p
self.Api.otNodeGetContextReuseDelay.argtypes = [ctypes.c_void_p]
self.Api.otNodeGetContextReuseDelay.restype = ctypes.c_uint
self.Api.otNodeSetContextReuseDelay.argtypes = [ctypes.c_void_p,
ctypes.c_uint]
self.Api.otNodeAddPrefix.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p]
self.Api.otNodeRemovePrefix.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeAddRoute.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
self.Api.otNodeRemoveRoute.argtypes = [ctypes.c_void_p,
ctypes.c_char_p]
self.Api.otNodeRegisterNetdata.argtypes = [ctypes.c_void_p]
self.Api.otNodeEnergyScan.argtypes = [ctypes.c_void_p,
ctypes.c_uint,
ctypes.c_ubyte,
ctypes.c_ushort,
ctypes.c_ushort,
ctypes.c_char_p]
self.Api.otNodePanIdQuery.argtypes = [ctypes.c_void_p,
ctypes.c_ushort,
ctypes.c_uint,
ctypes.c_char_p]
self.Api.otNodeScan.argtypes = [ctypes.c_void_p]
self.Api.otNodeScan.restype = ctypes.c_char_p
self.Api.otNodePing.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_ushort,
ctypes.c_uint]
self.Api.otNodePing.restype = ctypes.c_uint
self.Api.otNodeSetRouterSelectionJitter.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
self.Api.otNodeCommissionerAnnounceBegin.argtypes = [ctypes.c_void_p,
ctypes.c_uint,
ctypes.c_ubyte,
ctypes.c_ushort,
ctypes.c_char_p]
self.Api.otNodeSetActiveDataset.argtypes = [ctypes.c_void_p,
ctypes.c_ulonglong,
ctypes.c_ushort,
ctypes.c_ushort,
ctypes.c_uint,
ctypes.c_char_p]
self.Api.otNodeSetPendingDataset.argtypes = [ctypes.c_void_p,
ctypes.c_ulonglong,
ctypes.c_ulonglong,
ctypes.c_ushort,
ctypes.c_ushort]
self.Api.otNodeSendPendingSet.argtypes = [ctypes.c_void_p,
ctypes.c_ulonglong,
ctypes.c_ulonglong,
ctypes.c_uint,
ctypes.c_ushort,
ctypes.c_ushort,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p]
self.Api.otNodeSendActiveSet.argtypes = [ctypes.c_void_p,
ctypes.c_ulonglong,
ctypes.c_ushort,
ctypes.c_ushort,
ctypes.c_uint,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p]
self.Api.otNodeSetMaxChildren.argtypes = [ctypes.c_void_p,
ctypes.c_ubyte]
# Initialize a new node
self.otNode = self.Api.otNodeInit(ctypes.c_uint(nodeid))
if self.otNode == None:
raise OSError("otNodeInit failed!")
+581
View File
@@ -0,0 +1,581 @@
#!/usr/bin/python
#
# Copyright (c) 2016, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import os
import sys
import time
import pexpect
class otCli:
def __init__(self, nodeid):
self.nodeid = nodeid
self.verbose = int(float(os.getenv('VERBOSE', 0)))
self.node_type = os.getenv('NODE_TYPE', 'sim')
if self.node_type == 'soc':
self.__init_soc(nodeid)
elif self.node_type == 'ncp-sim':
self.__init_ncp_sim(nodeid)
else:
self.__init_sim(nodeid)
if self.verbose:
self.pexpect.logfile_read = sys.stdout
def __init_sim(self, nodeid):
""" Initialize a simulation node. """
if "OT_CLI_PATH" in os.environ.keys():
cmd = os.environ['OT_CLI_PATH']
elif "top_builddir" in os.environ.keys():
srcdir = os.environ['top_builddir']
cmd = '%s/examples/apps/cli/ot-cli-ftd' % srcdir
else:
cmd = './ot-cli-ftd'
cmd += ' %d' % nodeid
print ("%s" % cmd)
self.pexpect = pexpect.spawn(cmd, timeout=4)
# Add delay to ensure that the process is ready to receive commands.
time.sleep(0.2)
def __init_ncp_sim(self, nodeid):
""" Initialize an NCP simulation node. """
if "top_builddir" in os.environ.keys():
builddir = os.environ['top_builddir']
if "top_srcdir" in os.environ.keys():
srcdir = os.environ['top_srcdir']
else:
srcdir = os.path.dirname(os.path.realpath(__file__))
srcdir += "/../../.."
cmd = 'python %s/tools/spinel-cli/spinel-cli.py -p %s/examples/apps/ncp/ot-ncp -n' % (srcdir, builddir)
else:
cmd = './ot-ncp'
cmd += ' %d' % nodeid
print ("%s" % cmd)
self.pexpect = pexpect.spawn(cmd, timeout=4)
time.sleep(0.2)
self.pexpect.expect('spinel-cli >')
self.debug(int(os.getenv('DEBUG', '0')))
def __init_soc(self, nodeid):
""" Initialize a System-on-a-chip node connected via UART. """
import fdpexpect
serialPort = '/dev/ttyUSB%d' % ((nodeid-1)*2)
self.pexpect = fdpexpect.fdspawn(os.open(serialPort, os.O_RDWR|os.O_NONBLOCK|os.O_NOCTTY))
def __del__(self):
if self.pexpect.isalive():
self.send_command('exit')
self.pexpect.expect(pexpect.EOF)
self.pexpect.terminate()
self.pexpect.close(force=True)
def send_command(self, cmd):
print ("%d: %s" % (self.nodeid, cmd))
self.pexpect.sendline(cmd)
def get_commands(self):
self.send_command('?')
self.pexpect.expect('Commands:')
commands = []
while True:
i = self.pexpect.expect(['Done', '(\S+)'])
if i != 0:
commands.append(self.pexpect.match.groups()[0])
else:
break
return commands
def set_mode(self, mode):
cmd = 'mode ' + mode
self.send_command(cmd)
self.pexpect.expect('Done')
def debug(self, level):
self.send_command('debug '+str(level))
def interface_up(self):
self.send_command('ifconfig up')
self.pexpect.expect('Done')
def interface_down(self):
self.send_command('ifconfig down')
self.pexpect.expect('Done')
def thread_start(self):
self.send_command('thread start')
self.pexpect.expect('Done')
def thread_stop(self):
self.send_command('thread stop')
self.pexpect.expect('Done')
def commissioner_start(self):
cmd = 'commissioner start'
self.send_command(cmd)
self.pexpect.expect('Done')
def commissioner_add_joiner(self, addr, psk):
cmd = 'commissioner joiner add ' + addr + ' ' + psk
self.send_command(cmd)
self.pexpect.expect('Done')
def joiner_start(self, pskd='', provisioning_url=''):
cmd = 'joiner start ' + pskd + ' ' + provisioning_url
self.send_command(cmd)
self.pexpect.expect('Done')
def clear_whitelist(self):
self.send_command('whitelist clear')
self.pexpect.expect('Done')
def enable_whitelist(self):
self.send_command('whitelist enable')
self.pexpect.expect('Done')
def disable_whitelist(self):
self.send_command('whitelist disable')
self.pexpect.expect('Done')
def add_whitelist(self, addr, rssi=None):
cmd = 'whitelist add ' + addr
if rssi != None:
cmd += ' ' + str(rssi)
self.send_command(cmd)
self.pexpect.expect('Done')
def remove_whitelist(self, addr):
cmd = 'whitelist remove ' + addr
self.send_command(cmd)
self.pexpect.expect('Done')
def get_addr16(self):
self.send_command('rloc16')
i = self.pexpect.expect('([0-9a-fA-F]{4})')
if i == 0:
addr16 = int(self.pexpect.match.groups()[0], 16)
self.pexpect.expect('Done')
return addr16
def get_addr64(self):
self.send_command('extaddr')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
if i == 0:
addr64 = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
return addr64
def get_hashmacaddr(self):
self.send_command('hashmacaddr')
i = self.pexpect.expect('([0-9a-fA-F]{16})')
if i == 0:
addr = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
return addr
def get_channel(self):
self.send_command('channel')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
channel = int(self.pexpect.match.groups()[0])
self.pexpect.expect('Done')
return channel
def set_channel(self, channel):
cmd = 'channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
def get_masterkey(self):
self.send_command('masterkey')
i = self.pexpect.expect('([0-9a-fA-F]{32})')
if i == 0:
masterkey = self.pexpect.match.groups()[0].decode("utf-8")
self.pexpect.expect('Done')
return masterkey
def set_masterkey(self, masterkey):
cmd = 'masterkey ' + masterkey
self.send_command(cmd)
self.pexpect.expect('Done')
def get_key_sequence_counter(self):
self.send_command('keysequence counter')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
key_sequence_counter = int(self.pexpect.match.groups()[0])
self.pexpect.expect('Done')
return key_sequence_counter
def set_key_sequence_counter(self, key_sequence_counter):
cmd = 'keysequence counter %d' % key_sequence_counter
self.send_command(cmd)
self.pexpect.expect('Done')
def set_key_switch_guardtime(self, key_switch_guardtime):
cmd = 'keysequence guardtime %d' % key_switch_guardtime
self.send_command(cmd)
self.pexpect.expect('Done')
def set_network_id_timeout(self, network_id_timeout):
cmd = 'networkidtimeout %d' % network_id_timeout
self.send_command(cmd)
self.pexpect.expect('Done')
def get_network_name(self):
self.send_command('networkname')
while True:
i = self.pexpect.expect(['Done', '(\S+)'])
if i != 0:
network_name = self.pexpect.match.groups()[0].decode('utf-8')
else:
break
return network_name
def set_network_name(self, network_name):
cmd = 'networkname ' + network_name
self.send_command(cmd)
self.pexpect.expect('Done')
def get_panid(self):
self.send_command('panid')
i = self.pexpect.expect('([0-9a-fA-F]{4})')
if i == 0:
panid = int(self.pexpect.match.groups()[0], 16)
self.pexpect.expect('Done')
return panid
def set_panid(self, panid):
cmd = 'panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
def get_partition_id(self):
self.send_command('leaderpartitionid')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
weight = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return weight
def set_partition_id(self, partition_id):
cmd = 'leaderpartitionid %d' % partition_id
self.send_command(cmd)
self.pexpect.expect('Done')
def set_router_upgrade_threshold(self, threshold):
cmd = 'routerupgradethreshold %d' % threshold
self.send_command(cmd)
self.pexpect.expect('Done')
def set_router_downgrade_threshold(self, threshold):
cmd = 'routerdowngradethreshold %d' % threshold
self.send_command(cmd)
self.pexpect.expect('Done')
def release_router_id(self, router_id):
cmd = 'releaserouterid %d' % router_id
self.send_command(cmd)
self.pexpect.expect('Done')
def get_state(self):
states = ['detached', 'child', 'router', 'leader']
self.send_command('state')
match = self.pexpect.expect(states)
self.pexpect.expect('Done')
return states[match]
def set_state(self, state):
cmd = 'state ' + state
self.send_command(cmd)
self.pexpect.expect('Done')
def get_timeout(self):
self.send_command('childtimeout')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
timeout = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return timeout
def set_timeout(self, timeout):
cmd = 'childtimeout %d' % timeout
self.send_command(cmd)
self.pexpect.expect('Done')
def set_max_children(self, number):
cmd = 'childmax %d' % number
self.send_command(cmd)
self.pexpect.expect('Done')
def get_weight(self):
self.send_command('leaderweight')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
weight = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return weight
def set_weight(self, weight):
cmd = 'leaderweight %d' % weight
self.send_command(cmd)
self.pexpect.expect('Done')
def add_ipaddr(self, ipaddr):
cmd = 'ipaddr add ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Done')
def get_addrs(self):
addrs = []
self.send_command('ipaddr')
while True:
i = self.pexpect.expect(['(\S+:\S+)\r\n', 'Done'])
if i == 0:
addrs.append(self.pexpect.match.groups()[0].decode("utf-8"))
elif i == 1:
break
return addrs
def get_context_reuse_delay(self):
self.send_command('contextreusedelay')
i = self.pexpect.expect('(\d+)\r\n')
if i == 0:
timeout = self.pexpect.match.groups()[0]
self.pexpect.expect('Done')
return timeout
def set_context_reuse_delay(self, delay):
cmd = 'contextreusedelay %d' % delay
self.send_command(cmd)
self.pexpect.expect('Done')
def add_prefix(self, prefix, flags, prf = 'med'):
cmd = 'prefix add ' + prefix + ' ' + flags + ' ' + prf
self.send_command(cmd)
self.pexpect.expect('Done')
def remove_prefix(self, prefix):
cmd = ' prefix remove ' + prefix
self.send_command(cmd)
self.pexpect.expect('Done')
def add_route(self, prefix, prf = 'med'):
cmd = 'route add ' + prefix + ' ' + prf
self.send_command(cmd)
self.pexpect.expect('Done')
def remove_route(self, prefix):
cmd = 'route remove ' + prefix
self.send_command(cmd)
self.pexpect.expect('Done')
def register_netdata(self):
self.send_command('netdataregister')
self.pexpect.expect('Done')
def energy_scan(self, mask, count, period, scan_duration, ipaddr):
cmd = 'commissioner energy ' + str(mask) + ' ' + str(count) + ' ' + str(period) + ' ' + str(scan_duration) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Energy:', timeout=8)
def panid_query(self, panid, mask, ipaddr):
cmd = 'commissioner panid ' + str(panid) + ' ' + str(mask) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Conflict:', timeout=8)
def scan(self):
self.send_command('scan')
results = []
while True:
i = self.pexpect.expect(['\|\s(\S+)\s+\|\s(\S+)\s+\|\s([0-9a-fA-F]{4})\s\|\s([0-9a-fA-F]{16})\s\|\s(\d+)\r\n',
'Done'])
if i == 0:
results.append(self.pexpect.match.groups())
else:
break
return results
def ping(self, ipaddr, num_responses=1, size=None):
cmd = 'ping ' + ipaddr
if size != None:
cmd += ' ' + str(size)
self.send_command(cmd)
result = True
try:
responders = {}
while len(responders) < num_responses:
i = self.pexpect.expect(['from (\S+):'])
if i == 0:
responders[self.pexpect.match.groups()[0]] = 1
self.pexpect.expect('\n')
except pexpect.TIMEOUT:
result = False
return result
def set_router_selection_jitter(self, jitter):
cmd = 'routerselectionjitter %d' % jitter
self.send_command(cmd)
self.pexpect.expect('Done')
def set_active_dataset(self, timestamp, panid=None, channel=None, channel_mask=None, master_key=None):
self.send_command('dataset clear')
self.pexpect.expect('Done')
cmd = 'dataset activetimestamp %d' % timestamp
self.send_command(cmd)
self.pexpect.expect('Done')
if panid != None:
cmd = 'dataset panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
if channel != None:
cmd = 'dataset channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
if channel_mask != None:
cmd = 'dataset channelmask %d' % channel_mask
self.send_command(cmd)
self.pexpect.expect('Done')
if master_key != None:
cmd = 'dataset masterkey ' + master_key
self.send_command(cmd)
self.pexpect.expect('Done')
self.send_command('dataset commit active')
self.pexpect.expect('Done')
def set_pending_dataset(self, pendingtimestamp, activetimestamp, panid=None, channel=None):
self.send_command('dataset clear')
self.pexpect.expect('Done')
cmd = 'dataset pendingtimestamp %d' % pendingtimestamp
self.send_command(cmd)
self.pexpect.expect('Done')
cmd = 'dataset activetimestamp %d' % activetimestamp
self.send_command(cmd)
self.pexpect.expect('Done')
if panid != None:
cmd = 'dataset panid %d' % panid
self.send_command(cmd)
self.pexpect.expect('Done')
if channel != None:
cmd = 'dataset channel %d' % channel
self.send_command(cmd)
self.pexpect.expect('Done')
self.send_command('dataset commit pending')
self.pexpect.expect('Done')
def announce_begin(self, mask, count, period, ipaddr):
cmd = 'commissioner announce ' + str(mask) + ' ' + str(count) + ' ' + str(period) + ' ' + ipaddr
self.send_command(cmd)
self.pexpect.expect('Done')
def send_mgmt_active_set(self, active_timestamp=None, channel=None, channel_mask=None, extended_panid=None,
panid=None, master_key=None, mesh_local=None, network_name=None, binary=None):
cmd = 'dataset mgmtsetcommand active '
if active_timestamp != None:
cmd += 'activetimestamp %d ' % active_timestamp
if channel != None:
cmd += 'channel %d ' % channel
if channel_mask != None:
cmd += 'channelmask %d ' % channel_mask
if extended_panid != None:
cmd += 'extpanid ' + extended_panid + ' '
if panid != None:
cmd += 'panid %d ' % panid
if master_key != None:
cmd += 'masterkey ' + master_key + ' '
if mesh_local != None:
cmd += 'localprefix ' + mesh_local + ' '
if network_name != None:
cmd += 'networkname ' + network_name + ' '
if binary != None:
cmd += 'binary ' + binary + ' '
self.send_command(cmd)
self.pexpect.expect('Done')
def send_mgmt_pending_set(self, pending_timestamp=None, active_timestamp=None, delay_timer=None, channel=None,
panid=None, master_key=None, mesh_local=None, network_name=None):
cmd = 'dataset mgmtsetcommand pending '
if pending_timestamp != None:
cmd += 'pendingtimestamp %d ' % pending_timestamp
if active_timestamp != None:
cmd += 'activetimestamp %d ' % active_timestamp
if delay_timer != None:
cmd += 'delaytimer %d ' % delay_timer
if channel != None:
cmd += 'channel %d ' % channel
if panid != None:
cmd += 'panid %d ' % panid
if master_key != None:
cmd += 'masterkey ' + master_key + ' '
if mesh_local != None:
cmd += 'localprefix ' + mesh_local + ' '
if network_name != None:
cmd += 'networkname ' + network_name + ' '
self.send_command(cmd)
self.pexpect.expect('Done')