Exemple #1
0
    def discover_device(self, dev):
        if dev._discovery_lock:
            return
        dev._discovery_lock = True
        dev.discovery_tries = dev.discovery_tries + 1
        try:
            dev.log.debug("trying to do TLV discover")
            dev.device_info = tlvlib.discovery(dev.address)
            dev.product_type = dev.device_info[0][1]
            dev.label = dev.device_info[0][0]
            print "\tFound: ", dev.device_info[0][
                0], " Product Type: 0x%016x" % dev.product_type
            seconds, nanoseconds = tlvlib.convert_ieee64_time(
                dev.device_info[0][2])
            dev.boot_time = time.time() - seconds

            i = 1
            for data in dev.device_info[1]:
                if data[0] == tlvlib.INSTANCE_BUTTON_GENERIC:
                    dev.button_instance = i
                elif data[0] == tlvlib.INSTANCE_MOTION_GENERIC:
                    dev.motion_instance = i
                elif data[0] == tlvlib.INSTANCE_LEDS_GENERIC:
                    dev.leds_instance = i
                elif data[0] == tlvlib.INSTANCE_TEMP_GENERIC:
                    dev.temperature_instance = i
                elif data[0] == tlvlib.INSTANCE_TEMPHUM_GENERIC:
                    dev.temperature_instance = i
                elif data[0] == tlvlib.INSTANCE_NETWORK_STATISTICS:
                    print "\tFound:  Network Statistics"
                    dev.nstats_instance = i
                elif data[0] == tlvlib.INSTANCE_SLEEP:
                    print "\tFound:  Sleep instance"
                    dev.sleep_instance = i
                    dev.set_sleepy_device()
                i += 1

            if dev.next_update == 0:
                if self.grab_device(dev):
                    dev.next_update = time.time(
                    ) + self.watchdog_time - self.guard_time
            if dev.button_instance:
                print "\tFound:  Button instance - arming device!"
                dev.arm_device(dev.button_instance)

            if dev.motion_instance:
                print "\tFound:  Motion instance - arming device!"
                dev.arm_device(dev.motion_instance)

            de = DeviceEvent(dev, EVENT_DISCOVERY)
            self.send_event(de)
        except Exception as e:
            dev.log.error("discovery failed: %s", str(e))
        dev._discovery_lock = False
Exemple #2
0
    def discover_device(self, dev):
        if dev._discovery_lock:
            return
        dev._discovery_lock = True
        dev.discovery_tries = dev.discovery_tries + 1
        try:
            dev.log.debug("trying to do TLV discover")
            dev.device_info = tlvlib.discovery(dev.address)
            dev.product_type = dev.device_info[0][1]
            dev.label = dev.device_info[0][0]
            print "\tFound: ", dev.device_info[0][0], " Product Type: 0x%016x"%dev.product_type
            seconds,nanoseconds = tlvlib.convert_ieee64_time(dev.device_info[0][2])
            dev.boot_time = time.time() - seconds

            i = 1
            for data in dev.device_info[1]:
                if data[0] == tlvlib.INSTANCE_BUTTON_GENERIC:
                    dev.button_instance = i
                elif data[0] == tlvlib.INSTANCE_MOTION_GENERIC:
                    dev.motion_instance = i
                elif data[0] == tlvlib.INSTANCE_LEDS_GENERIC:
                    dev.leds_instance = i
                elif data[0] == tlvlib.INSTANCE_LAMP:
                    dev.lamp_instance = i
                elif data[0] == tlvlib.INSTANCE_TEMP_GENERIC:
                    dev.temperature_instance = i
                elif data[0] == tlvlib.INSTANCE_TEMPHUM_GENERIC:
                    dev.temperature_instance = i
                elif data[0] == tlvlib.INSTANCE_NETWORK_STATISTICS:
                    print "\tFound:  Network Statistics"
                    dev.nstats_instance = i
                elif data[0] == tlvlib.INSTANCE_SLEEP:
                    print "\tFound:  Sleep instance"
                    dev.sleep_instance = i
                    dev.set_sleepy_device()
                i += 1

            if dev.next_update == 0:
               if self.grab_device(dev):
                    dev.next_update = time.time() + self.watchdog_time - self.guard_time
            if dev.button_instance:
                print "\tFound:  Button instance - arming device!"
                dev.arm_device(dev.button_instance)

            if dev.motion_instance:
                print "\tFound:  Motion instance - arming device!"
                dev.arm_device(dev.motion_instance)

            de = DeviceEvent(dev, EVENT_DISCOVERY)
            self.send_event(de)
        except Exception as e:
            dev.log.error("discovery failed: %s", str(e))
        dev._discovery_lock = False
Exemple #3
0
arg = 1
host = "localhost"
instance = -1

if len(sys.argv) > 1 and sys.argv[1] == "-v":
    verbose = True
    arg = 2
if len(sys.argv) > arg:
    host = sys.argv[arg]
    print "HOST: ", host
    arg += 1

if len(sys.argv) > arg:
    instance = int(sys.argv[arg])

d = tlvlib.discovery(host)
print "Product label:", d[0][0], " type: %016x"%d[0][1], " instances:", len(d[1])
print "Booted at:",tlvlib.get_start_ieee64_time_as_string(d[0][2]),"-",tlvlib.convert_ieee64_time_to_string(d[0][2])

if verbose and instance <= 0:
    t1 = tlvlib.create_get_tlv128(0, tlvlib.VARIABLE_SW_REVISION)
    t2 = tlvlib.create_get_tlv32(0, tlvlib.VARIABLE_BOOTLOADER_VERSION)
    t3 = tlvlib.create_get_tlv64(0, tlvlib.VARIABLE_CHASSIS_CAPABILITIES)
    t4 = tlvlib.create_get_tlv32(0, tlvlib.VARIABLE_RESET_CAUSE)
    enc, tlvs = tlvlib.send_tlv([t1,t2,t3,t4], host, show_error=False)
    revision = ""
    if tlvs[3].error == 0:
        print "Last reset cause:", tlvlib.get_reset_cause_as_string(tlvs[3].int_value)
    if tlvs[0].error == 0:
        revision += "SW revision: " + tlvlib.convert_string(tlvs[0].value)
    if tlvs[1].error == 0:
Exemple #4
0
    def setup(self):
        # discover and - if it is a NBR then find serial radio and configure the
        # beacons to something good!
        d = tlvlib.discovery(self.router_host)
        print "Product label:", d[0][0]

        if d[0][1] != tlvlib.INSTANCE_BORDER_ROUTER:
            print "Error - could not find the radio - not starting the device server"
            return False

        i = 1
        for data in d[1]:
            print "Instance:",i , " type: %016x"%data[0], " ", data[1]
            # check for radio and if found - configure beacons
            if data[0] == tlvlib.INSTANCE_RADIO:
                self.radio_instance = i
            elif data[0] == tlvlib.INSTANCE_ROUTER:
                self.router_instance = i
                t = tlvlib.create_get_tlv(i, tlvlib.VARIABLE_NETWORK_ADDRESS, 2)
                enc, t = tlvlib.send_tlv(t, self.router_host)

                self.router_address = socket.inet_ntop(socket.AF_INET6, t[0].value)
                print "\tRouter address:", self.router_address

                self.router_prefix = socket.inet_ntop(socket.AF_INET6, t[0].value[0:8] + binascii.unhexlify("0000000000000000"))
                #while self.router_prefix.endswith("::"):
                #    self.router_prefix = self.router_prefix[0:len(self.router_prefix) - 1]
                print "\tNetwork prefix:",self.router_prefix
                if self.device_server_host:
                    self.udp_address = self.device_server_host
                else:
                    self.udp_address = self._lookup_device_host(self.router_prefix, socket.inet_ntop(socket.AF_INET6, t[0].value[0:8] + binascii.unhexlify("0000000000000001")))
                print "\tNetwork address:",self.udp_address
            elif data[0] == tlvlib.INSTANCE_BORDER_ROUTER_MANAGEMENT:
                self.brm_instance = i
            elif data[0] == tlvlib.INSTANCE_NETWORK_STATISTICS:
                self.nstats_instance = i
            i = i + 1

        if not self.radio_instance:
            print "Error - could not find the radio instance - not starting the device server"
            return False

        if not self.router_instance:
            print "Error - could not find the router instance - not starting the device server"
            return False

        # Setup socket to make sure it is possible to bind the address
        try:
            self._sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
            self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self._sock.bind((self.udp_address, self.udp_port))
            #self._sock.bind(('', self.udp_port))
            self._sock.settimeout(1.0)


            self._sock4 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
            self._sock4.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self._sock4.bind(('localhost', self.udp_port))
            self._sock4.settimeout(1.0)

        except Exception as e:
            print e
            print "Error - could not bind to the address", self.udp_address
            return False

        # set radio channel and panid
        self.set_channel_panid()

        # set-up beacon to ...
        IPv6Str = binascii.hexlify(socket.inet_pton(socket.AF_INET6, self.udp_address))
        BEACON = "fe02010a020090da01%08x"%self.location + "18020090da03" + IPv6Str + "%4x"%self.udp_port + "000000"
        beacon_payload = binascii.unhexlify(BEACON)
        print "Setting beacon with length",len(beacon_payload),"in instance", self.radio_instance
        print "\t",BEACON
        t = tlvlib.create_set_vector_tlv(self.radio_instance,
                                      tlvlib.VARIABLE_RADIO_BEACON_RESPONSE,
                                      0, 0, len(beacon_payload) / 4,
                                      beacon_payload)
        enc, t = tlvlib.send_tlv(t, self.router_host)
#        print "Result:"
#        tlvlib.print_tlv(t[0])
        return True
Exemple #5
0
    verbose = True

if args.k:
    keep_running = True

if not args.s:
    if not firmware:
        print "Specify image file"
        parser.print_help()
        exit()

    file = open(firmware, 'r')
    zip = zipfile.ZipFile(file)

try:
    d = tlvlib.discovery(host, port)
except socket.timeout:
    print "Failed to discover the device. Probably the device is offline or sleeping."
    exit()
producttype = "%016x" % d[0][1]
print "---- Upgrading ----"
print "Product label:", d[0][0], " type:", producttype, "instances:", len(d[1])
print "Booted at:", tlvlib.get_start_ieee64_time_as_string(
    d[0][2]), "-", tlvlib.convert_ieee64_time_to_string(d[0][2])

i = 1
upgrade = 0
for data in d[1]:
    if data[0] == tlvlib.INSTANCE_IMAGE:
        print "Instance " + str(
            data[2]) + ": type: %016x" % data[0], " ", data[1]
Exemple #6
0
arg = 1
host = "localhost"
instance = -1

if len(sys.argv) > 1 and sys.argv[1] == "-v":
    verbose = True
    arg = 2
if len(sys.argv) > arg:
    host = sys.argv[arg]
    print "HOST: ", host
    arg += 1

if len(sys.argv) > arg:
    instance = int(sys.argv[arg])

d = tlvlib.discovery(host)
print "Product label:", d[0][0], " type: %016x" % d[0][1], " instances:", len(
    d[1])
print "Booted at:", tlvlib.get_start_ieee64_time_as_string(
    d[0][2]), "-", tlvlib.convert_ieee64_time_to_string(d[0][2])

if verbose and instance <= 0:
    t1 = tlvlib.create_get_tlv128(0, tlvlib.VARIABLE_SW_REVISION)
    t2 = tlvlib.create_get_tlv32(0, tlvlib.VARIABLE_BOOTLOADER_VERSION)
    t3 = tlvlib.create_get_tlv64(0, tlvlib.VARIABLE_CHASSIS_CAPABILITIES)
    t4 = tlvlib.create_get_tlv32(0, tlvlib.VARIABLE_RESET_CAUSE)
    enc, tlvs = tlvlib.send_tlv([t1, t2, t3, t4], host, show_error=False)
    revision = ""
    if tlvs[3].error == 0:
        print "Last reset cause:", tlvlib.get_reset_cause_as_string(
            tlvs[3].int_value)