예제 #1
0
    def _fetch_periodic(self, device):
        t = []
        t.append(tlvlib.create_get_tlv64(0, tlvlib.VARIABLE_UNIT_BOOT_TIMER))
        if False and device.nstats_instance is not None:
            t.append(nstatslib.create_nstats_tlv(device.nstats_instance))

        if device.button_instance is not None:
            t.append(tlvlib.create_set_vector_tlv(0, tlvlib.VARIABLE_EVENT_ARRAY,
                                               0, 0, 1,
                                               struct.pack("!L", 1)))
            t.append(tlvlib.create_set_vector_tlv(device.button_instance,
                                               tlvlib.VARIABLE_EVENT_ARRAY,
                                               0, 0, 2,
                                               struct.pack("!LL", 1, 2)))

        if device.motion_instance is not None:
            if device.button_instance is None:
                t.append(tlvlib.create_set_vector_tlv(0, tlvlib.VARIABLE_EVENT_ARRAY,
                                                      0, 0, 1,
                                                      struct.pack("!L", 1)))
            t.append(tlvlib.create_set_vector_tlv(device.motion_instance,
                                               tlvlib.VARIABLE_EVENT_ARRAY,
                                               0, 0, 2,
                                               struct.pack("!LL", 1, 2)))

        try:
            device.log.debug("requesting periodic data: %s",tlvlib.get_tlv_short_info(t))
            return device.send_tlv(t)
        except socket.timeout:
            device.log.error("failed to fetch from device (time out)")
            return False
예제 #2
0
    def arm_device(self, instances):
        tlvs = [tlvlib.create_set_vector_tlv(0, tlvlib.VARIABLE_EVENT_ARRAY,
                                             0, 0, 1,
                                             struct.pack("!L", 1))]
        if type(instances) == list:
            for i in instances:
                t = tlvlib.create_set_vector_tlv(i, tlvlib.VARIABLE_EVENT_ARRAY,
                                                 0, 0, 2,
                                                 struct.pack("!LL", 1, 2))
                tlvs.append(t)
        else:
            t = tlvlib.create_set_vector_tlv(instances,
                                          tlvlib.VARIABLE_EVENT_ARRAY, 0, 0, 2,
                                          struct.pack("!LL", 1, 2))
            tlvs.append(t)

        if not self.send_tlv(tlvs):
            log.warning("Failed to arm device!")
예제 #3
0
def send_upgrade(segment, size, instance, host, port):
    print "Upgrading ", segment[0] + 1, instance, len(
        segment[1]), " at ", segment[0] * size, "\b" * 35,
    sys.stdout.flush()
    t1 = tlvlib.create_set_tlv32(instance, tlvlib.VARIABLE_WRITE_CONTROL,
                                 tlvlib.FLASH_WRITE_CONTROL_WRITE_ENABLE)
    t2 = tlvlib.create_set_vector_tlv(instance, tlvlib.VARIABLE_FLASH,
                                      tlvlib.SIZE32, segment[0] * size / 4,
                                      len(segment[1]) / 4, segment[1])
    try:
        enc, tlvs = tlvlib.send_tlv([t1, t2], host, port, 1.5)
        if enc.error != 0 or tlvs[0].error != 0 or tlvs[1].error != 0:
            print
            print "ERROR: failed to write image segment"
            return False
    except socket.timeout:
        return False
    return True
예제 #4
0
        t2 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_NUMBER_OF_LEDS)
        enc, tlvs = tlvlib.send_tlv([t1,t2], host)
        print "\tleds are set to 0x%02x"%tlvs[0].int_value,"(" + str(tlvs[1].int_value) + " user leds)"

    elif data[0] == tlvlib.INSTANCE_BUTTON_GENERIC:
        t1 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPIO_INPUT)
        t2 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPIO_TRIGGER_COUNTER)
        enc, tlvs = tlvlib.send_tlv([t1,t2], host)
        if tlvs[0].int_value == 0:
            print "\tbutton is not pressed, has been pressed",tlvs[1].int_value,"times"
        else:
            print "\tbutton is pressed, has been pressed",tlvs[1].int_value,"times"
        if verbose:
            # Arm instance 0 and rearm the button instance
            t0 = tlvlib.create_set_vector_tlv(0, tlvlib.VARIABLE_EVENT_ARRAY,
                                              tlvlib.SIZE32, 0, 1,
                                              struct.pack("!L", 1))
            t1 = tlvlib.create_set_vector_tlv(i, tlvlib.VARIABLE_EVENT_ARRAY,
                                              tlvlib.SIZE32, 0, 2,
                                              struct.pack("!LL", 1, 2))
            enc, tlvs = tlvlib.send_tlv([t0,t1], host)

    elif data[0] == tlvlib.INSTANCE_GPIO:
        t = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPOUT_OUTPUT)
        enc, tlvs = tlvlib.send_tlv(t, host)
        tlv = tlvs[0]
        print "\tgpout set to", tlv.int_value
        t1 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPOUT_CONFIG)
        t2 = tlvlib.create_get_tlv64(i, tlvlib.VARIABLE_GPOUT_CAPABILITIES)
        enc, tlvs = tlvlib.send_tlv([t1,t2], host, show_error=False)
        if tlvs[0].error == 0:
예제 #5
0
파일: leds.py 프로젝트: sics-iot/sparrow
    if led is not None:
        t = tlvlib.create_set_tlv32(instance, op, led)
        ts.append(t)
    t = tlvlib.create_get_tlv32(instance, tlvlib.VARIABLE_LED_CONTROL)
    ts.append(t)
    enc,tlvs = tlvlib.send_tlv(ts, host)
    t = tlvs[len(tlvs) - 1]
    if t.error == 0:
        print "Current leds:",bin(t.int_value)
    exit()

# Control leds via states (supports multicolor leds)
VARIABLE_LED_STATE = 0x105
if state is not None:
    t = tlvlib.create_set_vector_tlv(instance, VARIABLE_LED_STATE,
                                     tlvlib.SIZE32, led - 1, 1,
                                     struct.pack("!L", state))
    ts.append(t)

if led is not None:
    t = tlvlib.create_get_vector_tlv(instance, VARIABLE_LED_STATE,
                                     tlvlib.SIZE32, led - 1, 1)
else:
    t = tlvlib.create_get_vector_tlv(instance, VARIABLE_LED_STATE,
                                     tlvlib.SIZE32, 0, 32)
ts.append(t)

enc,tlvs = tlvlib.send_tlv(ts, host)
t = tlvs[len(tlvs) - 1]
if t.error == 0:
    for r in range(0, t.element_count):
예제 #6
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
예제 #7
0
    if led is not None:
        t = tlvlib.create_set_tlv32(instance, op, led)
        ts.append(t)
    t = tlvlib.create_get_tlv32(instance, tlvlib.VARIABLE_LED_CONTROL)
    ts.append(t)
    enc, tlvs = tlvlib.send_tlv(ts, host)
    t = tlvs[len(tlvs) - 1]
    if t.error == 0:
        print "Current leds:", bin(t.int_value)
    exit()

# Control leds via states (supports multicolor leds)
VARIABLE_LED_STATE = 0x105
if state is not None:
    t = tlvlib.create_set_vector_tlv(instance, VARIABLE_LED_STATE,
                                     tlvlib.SIZE32, led - 1, 1,
                                     struct.pack("!L", state))
    ts.append(t)

if led is not None:
    t = tlvlib.create_get_vector_tlv(instance, VARIABLE_LED_STATE,
                                     tlvlib.SIZE32, led - 1, 1)
else:
    t = tlvlib.create_get_vector_tlv(instance, VARIABLE_LED_STATE,
                                     tlvlib.SIZE32, 0, 32)
ts.append(t)

enc, tlvs = tlvlib.send_tlv(ts, host)
t = tlvs[len(tlvs) - 1]
if t.error == 0:
    for r in range(0, t.element_count):
예제 #8
0
            tlvs[1].int_value) + " user leds)"

    elif data[0] == tlvlib.INSTANCE_BUTTON_GENERIC:
        t1 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPIO_INPUT)
        t2 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPIO_TRIGGER_COUNTER)
        enc, tlvs = tlvlib.send_tlv([t1, t2], host)
        if tlvs[0].int_value == 0:
            print "\tbutton is not pressed, has been pressed", tlvs[
                1].int_value, "times"
        else:
            print "\tbutton is pressed, has been pressed", tlvs[
                1].int_value, "times"
        if verbose:
            # Arm instance 0 and rearm the button instance
            t0 = tlvlib.create_set_vector_tlv(0, tlvlib.VARIABLE_EVENT_ARRAY,
                                              tlvlib.SIZE32, 0, 1,
                                              struct.pack("!L", 1))
            t1 = tlvlib.create_set_vector_tlv(i, tlvlib.VARIABLE_EVENT_ARRAY,
                                              tlvlib.SIZE32, 0, 2,
                                              struct.pack("!LL", 1, 2))
            enc, tlvs = tlvlib.send_tlv([t0, t1], host)

    elif data[0] == tlvlib.INSTANCE_GPIO:
        t = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPOUT_OUTPUT)
        enc, tlvs = tlvlib.send_tlv(t, host)
        tlv = tlvs[0]
        print "\tgpout set to", tlv.int_value
        t1 = tlvlib.create_get_tlv32(i, tlvlib.VARIABLE_GPOUT_CONFIG)
        t2 = tlvlib.create_get_tlv64(i, tlvlib.VARIABLE_GPOUT_CAPABILITIES)
        enc, tlvs = tlvlib.send_tlv([t1, t2], host, show_error=False)
        if tlvs[0].error == 0: