class modem_listener: def __init__(self, name, port, rate, verbose): self.name = name self.port = port self.baudrate = rate self.verbose = verbose self.modem = None self.counter = 1 self.pub = rospy.Publisher('dash7', dash7, queue_size=1) self.pubrate = rospy.Rate(10) self.location = [] def set_location(self, x, y, z): self.location = [x, y, z] def location_broadcast(self, msg): br = tf.TransformBroadcaster() br.sendTransform((self.location[0], self.location[1], self.location[2]), tf.transformations.quaternion_from_euler(0.0, 0.0, 0.0, ), msg.header.stamp, self.name, "base_link") def received_command_callback(self, cmd): print cmd if cmd.interface_status != None: if cmd.interface_status.operand.interface_id == 215: # 215 is the interface_id that corresponds to DASH7 rospy.loginfo("Received Dash7 message") message = dash7() message = self.parse(message, cmd) self.pub.publish(message) self.location_broadcast(message) def parse(self, message, cmd): rospy.loginfo("Parse Dash7 message") message.header.stamp = rospy.Time.now() message.header.frame_id = str(cmd.interface_status.operand.interface_status.addressee.id) message.rx_address = str(self.modem.uid) message.tx_address = str(cmd.interface_status.operand.interface_status.addressee.id) message.rx_level = cmd.interface_status.operand.interface_status.rx_level message.link_budget = cmd.interface_status.operand.interface_status.link_budget message.channel_band = cmd.interface_status.operand.interface_status.channel_header.channel_band.name message.channel_class = cmd.interface_status.operand.interface_status.channel_header.channel_class.name message.channel_index = cmd.interface_status.operand.interface_status.channel_index message.counter = self.counter self.counter += 1 return message def listen(self): rospy.loginfo("Port %s with Baudrate %s", self.port, self.baudrate) while True: try: rospy.loginfo('make modem connection') self.modem = Modem(self.port, self.baudrate, receive_callback=self.received_command_callback, show_logging=self.verbose) self.modem.start_reading() while not rospy.is_shutdown(): pass except Exception, e: raise e
def on_connect(): global modem if modem == None: modem = Modem(config.device, config.rate, command_received_callback) modem.start_reading() print("modem: " + str(modem.uid)) emit('module_info', { 'uid': hex(modem.uid), 'application_name': modem.firmware_version.application_name, 'git_sha1': modem.firmware_version.git_sha1, 'd7ap_version': modem.firmware_version.d7ap_version }, broadcast=True)
def on_connect(): global modem if modem == None: modem = Modem(config.device, config.rate, command_received_callback) modem.start_reading() print("modem: " + str(modem.uid)) emit('module_info', { 'uid': modem.uid, 'application_name': modem.firmware_version.application_name, 'git_sha1': modem.firmware_version.git_sha1, 'd7ap_version': modem.firmware_version.d7ap_version }, broadcast=True)
class ThroughtPutTest: def __init__(self): self.argparser = argparse.ArgumentParser( fromfile_prefix_chars="@", description="Test throughput over 2 serial D7 modems") self.argparser.add_argument("-n", "--msg-count", help="number of messages to transmit", type=int, default=10) self.argparser.add_argument( "-p", "--payload-size", help="number of bytes of (appl level) payload to transmit", type=int, default=50) self.argparser.add_argument( "-sw", "--serial-transmitter", help="serial device /dev file transmitter node", default=None) self.argparser.add_argument( "-sr", "--serial-receiver", help="serial device /dev file receiver node", default=None) self.argparser.add_argument("-r", "--rate", help="baudrate for serial device", type=int, default=115200) self.argparser.add_argument( "-uid", "--unicast-uid", help="UID to use for unicast transmission, " "when not using receiver " "(in hexstring, for example 0xb57000009151d)", default=None) self.argparser.add_argument( "-to", "--receiver-timeout", help="timeout for the receiver (in seconds)", type=int, default=10) self.argparser.add_argument("-v", "--verbose", help="verbose", default=False, action="store_true") self.config = self.argparser.parse_args() if self.config.serial_transmitter == None and self.config.serial_receiver == None: self.argparser.error( "At least a transmitter or receiver is required.") if self.config.serial_receiver == None and self.config.unicast_uid == None: self.argparser.error( "When running without receiver a --unicast-uid parameter is required." ) if self.config.serial_transmitter == None: self.transmitter_modem = None print("Running without transmitter") else: self.transmitter_modem = Modem(self.config.serial_transmitter, self.config.rate, None, show_logging=self.config.verbose) access_profile = AccessProfile( channel_header=ChannelHeader( channel_band=ChannelBand.BAND_868, channel_coding=ChannelCoding.PN9, channel_class=ChannelClass.NORMAL_RATE), sub_profiles=[ SubProfile(subband_bitmap=0x01, scan_automation_period=CT(exp=0, mant=0)), SubProfile(), SubProfile(), SubProfile() ], sub_bands=[ SubBand( channel_index_start=0, channel_index_end=0, eirp=10, cca=86 # TODO ) ]) print("Write Access Profile") write_ap_cmd = Command.create_with_write_file_action_system_file( file=AccessProfileFile(access_profile=access_profile, access_specifier=0)) self.transmitter_modem.send_command(write_ap_cmd) if self.config.serial_receiver == None: self.receiver_modem = None print("Running without receiver") else: self.receiver_modem = Modem(self.config.serial_receiver, self.config.rate, self.receiver_cmd_callback, show_logging=self.config.verbose) self.receiver_modem.send_command( Command.create_with_write_file_action_system_file( DllConfigFile(active_access_class=0x01))) print("Receiver scanning on Access Class = 0x01") def start(self): self.received_commands = defaultdict(list) payload = range(self.config.payload_size) if self.receiver_modem != None: addressee_id = int(self.receiver_modem.uid, 16) else: addressee_id = int(self.config.unicast_uid, 16) if self.transmitter_modem != None: print( "\n==> broadcast, with QoS, transmitter active access class = 0x01 ====" ) self.transmitter_modem.send_command( Command.create_with_write_file_action_system_file( DllConfigFile(active_access_class=0x01))) interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_ANY), addressee=Addressee( access_class=0x01, id_type=IdType.NBID, id=CT(exp=0, mant=1) # we expect one responder )) self.start_transmitting( interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) print( "\n==> broadcast, no QoS, transmitter active access class = 0x01 ====" ) self.transmitter_modem.send_command( Command.create_with_write_file_action_system_file( DllConfigFile(active_access_class=0x01))) interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_NO), addressee=Addressee(access_class=0x01, id_type=IdType.NOID)) self.start_transmitting( interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) print( "\n==> unicast, with QoS, transmitter active access class = 0x01" ) interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_ANY), addressee=Addressee(access_class=0x01, id_type=IdType.UID, id=addressee_id)) self.start_transmitting( interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) print( "\n==> unicast, no QoS, transmitter active access class = 0x01" ) interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_NO), addressee=Addressee(access_class=0x01, id_type=IdType.UID, id=addressee_id)) self.start_transmitting( interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) else: # receive only self.receiver_modem.start_reading() self.wait_for_receiver(payload) def start_transmitting(self, interface_configuration, payload): print( "Running throughput test with payload size {} and interface_configuration {}\n\nrunning ...\n" .format(len(payload), interface_configuration)) if self.receiver_modem != None: self.received_commands = defaultdict(list) self.receiver_modem.start_reading() command = Command.create_with_return_file_data_action( file_id=0x40, data=payload, interface_type=InterfaceType.D7ASP, interface_configuration=interface_configuration) start = time.time() for i in range(self.config.msg_count): sys.stdout.write("{}/{}\r".format(i + 1, self.config.msg_count)) sys.stdout.flush() self.transmitter_modem.d7asp_fifo_flush(command) end = time.time() print("transmitter: sending {} messages completed in: {} s".format( self.config.msg_count, end - start)) print( "transmitter: throughput = {} bps with a payload size of {} bytes". format((self.config.msg_count * self.config.payload_size * 8) / (end - start), self.config.payload_size)) def wait_for_receiver(self, payload): if self.receiver_modem == None: print( "Running without receiver so we are not waiting for messages to be received ..." ) else: start = time.time() total_recv = 0 while total_recv < self.config.msg_count and time.time( ) - start < self.config.receiver_timeout: total_recv = sum( len(v) for v in self.received_commands.values()) time.sleep(2) print( "waiting for receiver to finish ... (current nr of recv msgs: {})" .format(total_recv)) print("finished receiving or timeout") self.receiver_modem.cancel_read() payload_has_errors = False for sender_cmd in self.received_commands.values(): for cmd in sender_cmd: if type(cmd.actions[0].op ) != ReturnFileData and cmd.actions[ 0].operand.data != payload: payload_has_errors = True print( "receiver: received unexpected command: {}".format( cmd)) if payload_has_errors == False and total_recv == self.config.msg_count: print( "receiver: OK: received {} messages with correct payload:". format(total_recv)) for sender, cmds in self.received_commands.items(): print("\t{}: {}".format(sender, len(cmds))) else: print( "receiver: NOK: received messages {}:".format(total_recv)) for sender, cmds in self.received_commands.items(): print("\t{}: {}".format(sender, len(cmds))) def receiver_cmd_callback(self, cmd): print("recv cmd: ".format(cmd)) if cmd.interface_status != None: uid = cmd.interface_status.operand.interface_status.addressee.id self.received_commands[uid].append(cmd) else: print("Unexpected cmd received, reboot?\n\t{}".format(cmd))
count = 0 argparser = argparse.ArgumentParser() argparser.add_argument("-d", "--device", help="serial device /dev file modem", default="/dev/ttyUSB0") argparser.add_argument("-r", "--rate", help="baudrate for serial device", type=int, default=115200) argparser.add_argument("-v", "--verbose", help="verbose", default=False, action="store_true") config = argparser.parse_args() modem = Modem(config.device, config.rate, receive_callback=received_command_callback, show_logging=config.verbose) modem.start_reading() # Program loop while True: pass
def received_command_callback(cmd): print cmd argparser = argparse.ArgumentParser() argparser.add_argument("-d", "--device", help="serial device /dev file modem", default="/dev/ttyUSB0") argparser.add_argument("-r", "--rate", help="baudrate for serial device", type=int, default=115200) argparser.add_argument("-v", "--verbose", help="verbose", default=False, action="store_true") config = argparser.parse_args() modem = Modem(config.device, config.rate, receive_callback=received_command_callback, show_logging=config.verbose) modem.d7asp_fifo_flush( alp_command=Command.create_with_read_file_action_system_file( file=UidFile(), interface_type=InterfaceType.D7ASP, interface_configuration=Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_ALL), addressee=Addressee( access_class=0, id_type=IdType.NOID ) ) ) ) modem.start_reading() while True: pass
class ThroughtPutTest: def __init__(self): self.argparser = argparse.ArgumentParser( fromfile_prefix_chars="@", description="Test throughput over 2 serial D7 modems" ) self.argparser.add_argument("-n", "--msg-count", help="number of messages to transmit", type=int, default=10) self.argparser.add_argument("-p", "--payload-size", help="number of bytes of (appl level) payload to transmit", type=int, default=50) self.argparser.add_argument("-sw", "--serial-transmitter", help="serial device /dev file transmitter node", default=None) self.argparser.add_argument("-sr", "--serial-receiver", help="serial device /dev file receiver node", default=None) self.argparser.add_argument("-r", "--rate", help="baudrate for serial device", type=int, default=115200) self.argparser.add_argument("-uid", "--unicast-uid", help="UID to use for unicast transmission, " "when not using receiver " "(in hexstring, for example 0xb57000009151d)", default=None) self.argparser.add_argument("-to", "--receiver-timeout", help="timeout for the receiver (in seconds)", type=int, default=10) self.argparser.add_argument("-v", "--verbose", help="verbose", default=False, action="store_true") self.config = self.argparser.parse_args() if self.config.serial_transmitter == None and self.config.serial_receiver == None: self.argparser.error("At least a transmitter or receiver is required.") if self.config.serial_receiver == None and self.config.unicast_uid == None: self.argparser.error("When running without receiver a --unicast-uid parameter is required.") if self.config.serial_transmitter == None: self.transmitter_modem = None print("Running without transmitter") else: self.transmitter_modem = Modem(self.config.serial_transmitter, self.config.rate, None, show_logging=self.config.verbose) access_profile = AccessProfile( scan_type_is_foreground=True, csma_ca_mode=CsmaCaMode.UNC, subnet=03, scan_automation_period=CT(0), subbands=[Subband( channel_header=ChannelHeader(channel_band=ChannelBand.BAND_433, channel_coding=ChannelCoding.PN9, channel_class=ChannelClass.NORMAL_RATE), channel_index_start=16, channel_index_end=16, eirp=10, ccao=0 # TODO ) ] ) print("Write Access Profile") write_ap_cmd = Command.create_with_write_file_action_system_file(file=AccessProfileFile(access_profile=access_profile, access_specifier=0)) self.transmitter_modem.send_command(write_ap_cmd) if self.config.serial_receiver == None: self.receiver_modem = None print("Running without receiver") else: self.receiver_modem = Modem(self.config.serial_receiver, self.config.rate, self.receiver_cmd_callback, show_logging=self.config.verbose) self.receiver_modem.send_command(Command.create_with_write_file_action_system_file(DllConfigFile(active_access_class=2))) print("Receiver scanning on Access Class = 2") def start(self): self.received_commands = defaultdict(list) payload = range(self.config.payload_size) if self.receiver_modem != None: addressee_id = self.receiver_modem.uid else: addressee_id = int(self.config.unicast_uid, 16) if self.transmitter_modem != None: print("\n==> broadcast, with QoS, transmitter active access class = 0 ====") self.transmitter_modem.send_command(Command.create_with_write_file_action_system_file(DllConfigFile(active_access_class=0))) interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_ANY), addressee=Addressee( access_class=2, id_type=IdType.NOID ) ) self.start_transmitting(interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) print("\n==> broadcast, no QoS, transmitter active access class = 0 ====") self.transmitter_modem.send_command(Command.create_with_write_file_action_system_file(DllConfigFile(active_access_class=0))) interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_NO), addressee=Addressee( access_class=2, id_type=IdType.NOID ) ) self.start_transmitting(interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) print("\n==> unicast, with QoS, transmitter active access class = 0") interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_ANY), addressee=Addressee( access_class=2, id_type=IdType.UID, id=addressee_id ) ) self.start_transmitting(interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) print("\n==> unicast, no QoS, transmitter active access class = 0") interface_configuration = Configuration( qos=QoS(resp_mod=ResponseMode.RESP_MODE_NO), addressee=Addressee( access_class=2, id_type=IdType.UID, id=addressee_id ) ) self.start_transmitting(interface_configuration=interface_configuration, payload=payload) self.wait_for_receiver(payload) else: # receive only self.receiver_modem.start_reading() self.wait_for_receiver(payload) def start_transmitting(self, interface_configuration, payload): print("Running throughput test with payload size {} and interface_configuration {}\n\nrunning ...\n".format(len(payload), interface_configuration)) if self.receiver_modem != None: self.received_commands = defaultdict(list) self.receiver_modem.start_reading() command = Command.create_with_return_file_data_action( file_id=0x40, data=payload, interface_type=InterfaceType.D7ASP, interface_configuration=interface_configuration ) start = time.time() for i in range(self.config.msg_count): sys.stdout.write("{}/{}\r".format(i + 1, self.config.msg_count)) sys.stdout.flush() self.transmitter_modem.d7asp_fifo_flush(command) end = time.time() print("transmitter: sending {} messages completed in: {} s".format(self.config.msg_count, end - start)) print("transmitter: throughput = {} bps with a payload size of {} bytes".format( (self.config.msg_count * self.config.payload_size * 8) / (end - start), self.config.payload_size) ) def wait_for_receiver(self, payload): if self.receiver_modem == None: print("Running without receiver so we are not waiting for messages to be received ...") else: start = time.time() total_recv = 0 while total_recv < self.config.msg_count and time.time() - start < self.config.receiver_timeout: total_recv = sum(len(v) for v in self.received_commands.values()) time.sleep(2) print("waiting for receiver to finish ... (current nr of recv msgs: {})".format(total_recv)) print("finished receiving or timeout") self.receiver_modem.cancel_read() payload_has_errors = False for sender_cmd in self.received_commands.values(): for cmd in sender_cmd: if type(cmd.actions[0].op) != ReturnFileData and cmd.actions[0].operand.data != payload: payload_has_errors = True print ("receiver: received unexpected command: {}".format(cmd)) if payload_has_errors == False and total_recv == self.config.msg_count: print("receiver: OK: received {} messages with correct payload:".format(total_recv)) for sender, cmds in self.received_commands.items(): print("\t{}: {}".format(sender, len(cmds))) else: print("receiver: NOK: received messages {}:".format(total_recv)) for sender, cmds in self.received_commands.items(): print("\t{}: {}".format(sender, len(cmds))) def receiver_cmd_callback(self, cmd): if cmd.interface_status != None: uid = cmd.interface_status.operand.interface_status.addressee.id self.received_commands[uid].append(cmd) else: print("Unexpected cmd received, reboot?\n\t{}".format(cmd))