示例#1
0
    def provision(self,
                  uuid=None,
                  key_index=0,
                  name="",
                  context_id=0,
                  attention_duration_s=0):
        """Starts provisioning of the given unprovisioned device.

        Parameters:
        -----------
            uuid : uint8_t[16]
                16-byte long hexadcimal string or bytearray
            key_index : uint16_t
                NetKey index
            name : string
                Name to give the device (stored in the database)
            conext-id:
                Provisioning context ID to use. Normally no reason to change.
            attention_duration_s : uint8_t
                Time in seconds during which the device will identify itself using any means it can.
        """

        if not uuid:
            uuid = self.unprov_list.pop(0)
        elif isinstance(uuid, str):
            uuid = bytearray.fromhex(uuid)
        elif not isinstance(uuid, bytearray):
            raise TypeError("UUID must be string or bytearray")

        if len(uuid) != 16:
            raise ValueError("UUID must be 16 bytes long")

        netkey = None
        for key in self.prov_db.net_keys:
            if key_index == key.index:
                netkey = key
                break

        if not netkey:
            raise ValueError("No network key found for key index %d" %
                             (key_index))

        self.iaci.send(
            cmd.Provision(context_id, uuid, netkey.key, netkey.index,
                          self.prov_db.iv_index, self.__next_free_address,
                          self.prov_db.iv_update, netkey.phase > 0,
                          attention_duration_s))

        self.__session_data["UUID"] = uuid
        self.__session_data["name"] = name
        self.__session_data["net_keys"] = [netkey.index]
        self.__session_data["unicast_address"] = mt.UnicastAddress(
            self.__next_free_address)
        self.__session_data["config_complete"] = False
        self.__session_data["security"] = netkey.min_security
示例#2
0
    def __model_app_status_handler(self, opcode, message):
        status, element_address, appkey_index = struct.unpack(
            "<BHH", message.data[:5])
        status = AccessStatus(status)
        model_id = mt.ModelId.unpack(message.data[5:])
        element_address = mt.UnicastAddress(element_address)
        appkey_index = mt.KeyIndex(appkey_index)

        self.logger.info("Model app bind status: %s", status)
        if status == AccessStatus.SUCCESS:
            model = self.model_get(element_address, model_id)

            # Was last command a bind or unbind?
            if self.previous_command == "bind" and appkey_index not in model.bind:
                model.bind.append(appkey_index)
            elif appkey_index in model.bind:
                model.bind.remove(appkey_index)

            self.db_save()
            self.logger.info("Appkey %s %d to model %r at %04x",
                             self.previous_command, appkey_index, model_id, element_address)