def __init__(self, nodes_list):
     self.state = {}
     self.nodes = NodeAggregator(nodes_list)
     self.current_sender_node = None
示例#2
0
 def __init__(self, nodes_list):
     self.state = {}
     self.nodes = NodeAggregator(nodes_list)
     self.current_sender_node = None
class RadioCharac(object):
    """ Radio Characterization """
    def __init__(self, nodes_list):
        self.state = {}
        self.nodes = NodeAggregator(nodes_list)
        self.current_sender_node = None

    def start(self):
        """ Start nodes connection """
        self.nodes.start()

    def stop(self):
        """ Stop nodes connection """
        self.nodes.stop()

    def _answers_handler(self, node_id, msg):
        """ Handle answers """
        try:
            line = msg.split(' ')

            if line[0] == 'config_radio':
                if line[1] == 'ACK':
                    self.state['config']['success'].append(node_id)
                else:
                    self.state['config']['failure'].append(node_id)
            elif line[0] == 'send_packets':
                if line[1] == 'ACK':
                    # send_packets ACK 0
                    self.state['send']['success'].append(node_id)
                else:
                    # send_packets NACK 42
                    self.state['send']['failure'].append('%s %s' %
                                                         (node_id, line[2]))

            elif line[0] == 'radio_rx':
                # "radio_rx m3-128 -17dBm 5 -61 255 sender power num rssi lqi"
                sender = line[1]

                # add list for this node
                results = self.state['radio'][sender].get(
                    node_id, {
                        'success': [],
                        'errors': []
                    })
                self.state['radio'][sender][node_id] = results

                # add rx informations
                #results.append("%s" % ' '.join(line[2:6]))
                results['success'].append(tuple(line[2:6]))
            elif line[0] == 'radio_rx_error':
                sender = self.current_sender_node.node_id

                # add list for this node
                results = self.state['radio'][sender].get(
                    node_id, {
                        'success': [],
                        'errors': []
                    })
                self.state['radio'][sender][node_id] = results

                results['errors'].append("%s" % line[1])

                # print >> sys.stderr, "Radio_rx_error %s %s sender %s" % (
                #         node_id, line[1], self.current_sender_node.node_id)

            else:
                print >> sys.stderr, "UNKOWN_MSG: %s %r" % (node_id, msg)

        except IndexError:
            print >> sys.stderr, "UNKOWN_MSG: %s %r" % (node_id, msg)

    def run_characterization(self, channel, power, num_pkts, delay):
        """ Run the radio characterizations on nodes"""

        self.start()

        self.state['options'] = {
            'power': power,
            'channel': channel,
            'num_pkts': num_pkts,
            'delay': delay
        }

        self.state['config'] = {'success': [], 'failure': []}
        self.state['send'] = {'success': [], 'failure': []}

        #nodes = self.nodes.values()
        #self.nodes_cli('--update', firmware=FIRMWARE_PATH)
        #time.sleep(2)  # wait started

        # init all nodes handlers and radio config
        self.state['radio'] = {}

        for node in self.nodes.values():
            self.state['radio'][node.node_id] = {}
            node.line_handler.append(self._answers_handler)

        cmd = "config_radio -c {channel}\n"
        self.nodes.broadcast(cmd.format(channel=channel))

        time.sleep(10)  # wait

        cmd = "send_packets -i {node_id} -p {power} -n {num_pkts} -d {delay}\n"
        for node in self.nodes.values():
            self.current_sender_node = node
            print >> sys.stderr, "sending node %s" % node.node_id
            node.send(
                cmd.format(node_id=node.node_id,
                           power=power,
                           num_pkts=num_pkts,
                           delay=delay))
            time.sleep(2)
            self.current_sender_node = None

        self.stop()
        return self.state
示例#4
0
class RadioCharac(object):
    """ Radio Characterization """
    def __init__(self, nodes_list):
        self.state = {}
        self.nodes = NodeAggregator(nodes_list)
        self.current_sender_node = None

    def start(self):
        """ Start nodes connection """
        self.nodes.start()

    def stop(self):
        """ Stop nodes connection """
        self.nodes.stop()

    def _answers_handler(self, node_id, msg):
        """ Handle answers """
        try:
            line = msg.split(' ')

            if line[0] == 'config_radio':
                if line[1] == 'ACK':
                    self.state['config']['success'].append(node_id)
                else:
                    self.state['config']['failure'].append(node_id)
            elif line[0] == 'send_packets':
                if line[1] == 'ACK':
                    # send_packets ACK 0
                    self.state['send']['success'].append(node_id)
                else:
                    # send_packets NACK 42
                    self.state['send']['failure'].append('%s %s' %
                                                         (node_id, line[2]))

            elif line[0] == 'radio_rx':
                # "radio_rx m3-128 -17dBm 5 -61 255 sender power num rssi lqi"
                sender = line[1]

                # add list for this node
                results = self.state['radio'][sender].get(
                    node_id, {'success': [], 'errors': []})
                self.state['radio'][sender][node_id] = results

                # add rx informations
                #results.append("%s" % ' '.join(line[2:6]))
                results['success'].append(tuple(line[2:6]))
            elif line[0] == 'radio_rx_error':
                sender = self.current_sender_node.node_id

                # add list for this node
                results = self.state['radio'][sender].get(
                    node_id, {'success': [], 'errors': []})
                self.state['radio'][sender][node_id] = results

                results['errors'].append("%s" % line[1])

                # print >> sys.stderr, "Radio_rx_error %s %s sender %s" % (
                #         node_id, line[1], self.current_sender_node.node_id)

            else:
                print >> sys.stderr, "UNKOWN_MSG: %s %r" % (node_id, msg)

        except IndexError:
            print >> sys.stderr, "UNKOWN_MSG: %s %r" % (node_id, msg)


    def run_characterization(self, channel, power, num_pkts, delay):
        """ Run the radio characterizations on nodes"""

        self.start()

        self.state['options'] = {'power': power, 'channel': channel,
                                 'num_pkts': num_pkts, 'delay': delay}

        self.state['config'] = {'success': [], 'failure': []}
        self.state['send'] = {'success': [], 'failure': []}

        #nodes = self.nodes.values()
        #self.nodes_cli('--update', firmware=FIRMWARE_PATH)
        #time.sleep(2)  # wait started

        # init all nodes handlers and radio config
        self.state['radio'] = {}

        for node in self.nodes.values():
            self.state['radio'][node.node_id] = {}
            node.line_handler.append(self._answers_handler)

        cmd = "config_radio -c {channel}\n"
        self.nodes.broadcast(cmd.format(channel=channel))

        time.sleep(10)  # wait

        cmd = "send_packets -i {node_id} -p {power} -n {num_pkts} -d {delay}\n"
        for node in self.nodes.values():
            self.current_sender_node = node
            print >> sys.stderr, "sending node %s" % node.node_id
            node.send(cmd.format(node_id=node.node_id, power=power,
                                 num_pkts=num_pkts, delay=delay))
            time.sleep(2)
            self.current_sender_node = None


        self.stop()
        return self.state