Exemple #1
0
 def transmit_product_code(self):
     """
     Transmit SWAP status packet containing the product code
     """
     pcode = SwapValue(self.FIRMSERVER_PRODUCT_CODE)
     status = SwapStatusPacket(self.server.devaddress, SwapRegId.ID_PRODUCT_CODE, pcode)
     status.send(self.server)
Exemple #2
0
    def swapPacketReceived(self, packet):
        """
        New SWAP packet received
        
        @param packet: SWAP packet received
        """
        if packet.destAddress in [
                SwapAddress.BROADCAST_ADDR, self.server.devaddress
        ]:
            # Packet broadcasted or explicitely addressed to our server
            if packet.function == SwapFunction.QUERY:
                # SWAP query received
                if packet.destAddress == self.server.devaddress:
                    # Local register being queried
                    if packet.regId == SwapRegId.ID_PRODUCT_CODE:
                        # Server's product code
                        self.transmit_product_code()
                    elif packet.regId == SwapRegId.ID_FIRMWARE:
                        # Query comming from our node in programming mode?
                        if self.prog_address == packet.srcAddress:
                            # Firmware image being queried
                            if packet.value.getLength() == 2:
                                # The query contains 2 bytes of data
                                # Take line number
                                nb_line = (packet.value.toList()[0] <<
                                           8) | packet.value.toList()[1]
                                line = self.hexfile.get_line(nb_line)

                                if line is not None:
                                    prefix = packet.value.toList()
                                    payload = prefix + line.data
                                    val = SwapValue(payload)
                                    page_packet = SwapStatusPacket(
                                        self.server.devaddress,
                                        SwapRegId.ID_FIRMWARE, val)
                                    page_packet.send(self.server)

                                if nb_line == 0:
                                    self.hexfile_line = 0
                                    print "Erasing flash memory"
                                    self.progress + 1
                                elif nb_line == 1:
                                    print "Uploading code to node"
                                elif 1 < nb_line < (
                                        self.hexfile.nbof_data_lines - 1):
                                    if (nb_line != self.hexfile_line):
                                        self.hexfile_line = nb_line
                                        self.progress + 1
                                        self.progress.show_progress()
                                elif nb_line == (self.hexfile.nbof_data_lines):
                                    self.progress + 2
                                    self.progress.show_progress()
                                    print "\n\rUpload completed"
                                    self.prog_address = None
                                    self.hexfile_line = 0
Exemple #3
0
    def dispatch_command(self, command):
        """
        Apply user command
        
        @param command : user command
        """
        if len(command) == 1:
            if command[0] == "help":
                self.print_help()
            elif command[0] == "version":
                print self.VERSION_NUMBER
            elif command[0] == "quit":
                self.stop()
                sys.exit(0)
        elif len(command) < 2:
            print "Incorrect command. Run \"help\" to get more information"
        # Print SWAP traffic
        elif command[0] == "traffic":
            if command[1].lower() == "on":
                self.verbose = True
                self.server.verbose = True
                self.server.modem.verbose = True
                self.server.modem._serport._verbose = True
            elif command[1].lower() == "off":
                self.verbose = False
                self.server.verbose = False
                self.server.modem.verbose = False
                self.server.modem._serport._verbose = False
            else:
                print command[1] + " is not a correct value"
        # Set HEX file
        elif command[0] == "hexfile":
            # HEX file object
            self.hexfile = HexFile(command[1])
        # Print list of nodes
        elif command[0] == "list":
            if command[1] == "nodes":
                if len(self.network.motes) == 0:
                    print "No nodes detected"
                else:
                    for mote in self.network.motes:
                        print "Addr: " + hex(
                            mote.address) + " - " + mote.definition.product
        # Clear list list of nodes
        elif command[0] == "clear":
            if command[1] == "nodes":
                self.server.network.clear()
                self.server.network.save()
        # Change device device property
        elif command[0] == "node":
            if len(command) < 3:
                print "Insufficient arguments"
                print "Correct format is: node <address> <options...>"
            else:
                addr = self.str_to_int(command[1])
                if addr is None:
                    print "Incorrect address format"
                else:
                    # Get mote object
                    mote = self.server.network.get_mote(address=addr)

                    if mote is None:
                        print "Node not found in data base"
                        print "Clear your list of nodes and restart them again"
                    else:
                        # Program node
                        if command[2] == "program":
                            if (self.hexfile == None):
                                print "Please set a hexfile before running \"program\""
                            else:
                                # Create progress bar
                                self.progress = AnimatedProgressBar(
                                    end=self.hexfile.nbof_data_lines, width=50)
                                # Save address of node being programmed
                                self.prog_address = addr
                                # Transmit product code
                                self.transmit_product_code()

                                # Put node in upgrade mode
                                val = SwapValue(SwapState.UPGRADE, 1)
                                if mote.cmdRegisterWack(
                                        SwapRegId.ID_SYSTEM_STATE, val):
                                    print "Node now in programming mode"
                                elif self.hexfile_line == 0:
                                    print "Unable to put node in progamming mode"
                                    self.prog_address = None
                        # Restart node (if not sleeping)
                        elif command[2] == "restart":
                            if mote.restart():
                                print "Node restarting"
                            else:
                                print "Got no response from node. It's probably sleeping"
                        # Show details of device
                        elif command[2] == "details":
                            print "SWAP address : " + hex(mote.address)
                            print "Developer : " + mote.definition.manufacturer
                            print "Product name : " + mote.definition.product
                            if mote.config_registers is not None:
                                print "Config registers :"
                                for reg in mote.config_registers:
                                    print "Register ID : " + hex(reg.id)
                                    print "Register name : " + reg.name
                                    print "Register value : 0x" + reg.value.toAsciiHex(
                                    )
                                    for param in reg.parameters:
                                        print "  Parameter name : " + param.name
                                        print "  Parameter value : 0x" + param.value.toAsciiHex(
                                        )
                            if mote.regular_registers is not None:
                                print "Regular registers :"
                                for reg in mote.regular_registers:
                                    print "Register ID : " + hex(reg.id)
                                    print "Register name : " + reg.name
                                    print "Register value : 0x" + reg.value.toAsciiHex(
                                    )
                                    for endp in reg.parameters:
                                        print "  Endpoint name : " + endp.name
                                        print "  Endpoint value : 0x" + endp.value.toAsciiHex(
                                        )
                        # Change device address
                        elif command[2] == "address":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_DEVICE_ADDR), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_addr = self.str_to_int(command[3])
                                if new_addr is not None:
                                    self.print_confirmation(
                                        mote.setAddress(new_addr), command[2],
                                        command[3])
                        # Change Tx interval
                        elif command[2] == "txinterval":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_TX_INTERVAL), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_interval = self.str_to_int(command[3])
                                if new_interval is not None:
                                    self.print_confirmation(
                                        mote.setTxInterval(new_interval),
                                        command[2], command[3])
                        # Change network id
                        elif command[2] == "netid":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_NETWORK_ID), "network ID")
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_netid = self.str_to_int(command[3])
                                if new_netid is not None:
                                    self.print_confirmation(
                                        mote.setNetworkId(new_netid),
                                        command[2], command[3])
                        # Change frequency channel
                        elif command[2] == "channel":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_FREQ_CHANNEL), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_channel = self.str_to_int(command[3])
                                if new_channel is not None:
                                    self.print_confirmation(
                                        mote.setFreqChannel(new_channel),
                                        command[2], command[3])
                        # Read/write register
                        elif command[2] == "reg":
                            if len(command) > 3:
                                reg_id = self.str_to_int(command[3])
                                if reg_id is not None:
                                    if len(command) == 4:
                                        self.print_response(
                                            mote.qryRegisterWack(reg_id),
                                            "register value")
                                    elif len(command) == 5:
                                        reg = mote.getRegister(reg_id)
                                        val = SwapValue(
                                            command[4], reg.value.getLength())
                                        self.print_confirmation(
                                            mote.cmdRegisterWack(reg_id, val),
                                            "register value", command[4])
                                    else:
                                        print "Too many arguments"
                                        print "Correct format is: node <address> reg <reg_id> [<reg_val>]"
                            else:
                                print "Insufficient arguments"
                                print "Correct format is: node <address> reg <reg_id> [<reg_val>]"
                        else:
                            print "Command not supported"
        else:
            print "Command not supported"
Exemple #4
0
    def getRegList(self, config=False):
        """
        Return list of registers

        @param config: Set to True if Configuration register are required. False for regular ones

        @return List of registers
        """
        if self.fileName is None:
            return None

        # List of config registers belonging to the current device
        lstRegs = []

        # Parse XML file
        tree = xml.parse(self.fileName)
        if tree is None:
            return None
        # Get the root node
        root = tree.getroot()
        # Get manufacturer

        # List of register elements belonging to the device
        regtype = "regular"
        if config == True:
            regtype = "config"
        lstElemReg = root.findall(regtype + "/reg")
        if lstElemReg is not None:
            for reg in lstElemReg:
                # Get register id
                strRegId = reg.get("id")
                if strRegId is not None:
                    regId = int(strRegId)
                    # Get register name
                    regName = reg.get("name", default="")
                    # Create register from id and mote
                    swRegister = SwapRegister(self.mote, regId, regName)

                    # List of endpoints belonging to the register
                    if config == True:
                        elementName = "param"
                    else:
                        elementName = "endpoint"
                    lstElemParam = reg.findall(elementName)
                    for param in lstElemParam:
                        # Read XML fields
                        paramType = param.get("type", default="num")
                        paramDir = param.get("dir", default="inp")
                        paramName = param.get("name", default="")
                        paramPos = "0"
                        elem = param.find("position")
                        if elem is not None:
                            paramPos = elem.text
                        paramSize = "1"
                        elem = param.find("size")
                        if elem is not None:
                            paramSize = elem.text
                        paramDef = "0"
                        elem = param.find("default")
                        if elem is not None:
                            paramDef = elem.text
                        if paramType in [SwapType.NUMBER, SwapType.BINARY]:
                            try:
                                defVal = int(paramDef)
                            except ValueError:
                                try:
                                    defVal = float(paramDef)
                                except ValueError:
                                    raise SwapException("Default value " +
                                                        str(paramDef) +
                                                        " is not a number")
                                    return
                        else:
                            defVal = paramDef
                        verif = None
                        elem = param.find("verif")
                        if elem is not None:
                            verif = elem.text
                        # Get list of units
                        units = param.findall("units/unit")
                        lstUnits = None
                        if units is not None and len(units) > 0:
                            lstUnits = []
                            for unit in units:
                                name = unit.get("name", default=None)
                                factor = unit.get("factor", default=1)
                                offset = unit.get("offset", default=0)
                                calc = unit.get("calc", default=None)
                                xmlUnit = XmlUnit(name, factor, offset, calc)
                                lstUnits.append(xmlUnit)

                        if config == True:
                            # Create SWAP config parameter
                            swParam = SwapCfgParam(register=swRegister,
                                                   pType=paramType,
                                                   name=paramName,
                                                   position=paramPos,
                                                   size=paramSize,
                                                   default=defVal,
                                                   verif=verif)
                        else:
                            # Create SWAP endpoint
                            swParam = SwapEndpoint(register=swRegister,
                                                   pType=paramType,
                                                   direction=paramDir,
                                                   name=paramName,
                                                   position=paramPos,
                                                   size=paramSize,
                                                   default=defVal,
                                                   verif=verif,
                                                   units=lstUnits)

                        # Add current parameter to the register
                        swRegister.add(swParam)

                    # Create empty value for the register
                    swRegister.value = SwapValue([0] * swRegister.getLength())
                    swRegister.update()
                    # Add endpoint to the list
                    lstRegs.append(swRegister)

        if len(lstRegs) == 0:
            return None
        else:
            return lstRegs