def zdo_f(): app = mock.MagicMock() app._ieee = t.EmberEUI64(map(t.uint8_t, [8, 9, 10, 11, 12, 13, 14, 15])) app.get_sequence = mock.MagicMock(return_value=123) ieee = t.EmberEUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) dev = bellows.zigbee.device.Device(app, ieee, 65535) return zdo.ZDO(dev)
def zha_security(config: Dict[str, Any], controller: bool = False) -> None: isc = t.EmberInitialSecurityState() isc.bitmask = t.uint16_t( t.EmberInitialSecurityBitmask.HAVE_PRECONFIGURED_KEY | t.EmberInitialSecurityBitmask.REQUIRE_ENCRYPTED_KEY) isc.preconfiguredKey = t.EmberKeyData( config[zigpy.config.CONF_NWK_TC_LINK_KEY]) nwk_key = config[zigpy.config.CONF_NWK_KEY] if nwk_key is None: nwk_key = os.urandom(16) isc.networkKey = t.EmberKeyData(nwk_key) isc.networkKeySequenceNumber = t.uint8_t( config[zigpy.config.CONF_NWK_KEY_SEQ]) tc_addr = config[zigpy.config.CONF_NWK_TC_ADDRESS] if tc_addr is None: tc_addr = [0x00] * 8 isc.preconfiguredTrustCenterEui64 = t.EmberEUI64(tc_addr) if controller: isc.bitmask |= ( t.EmberInitialSecurityBitmask.TRUST_CENTER_GLOBAL_LINK_KEY | t.EmberInitialSecurityBitmask.HAVE_NETWORK_KEY) isc.bitmask = t.uint16_t(isc.bitmask) return isc
def zcl(ctx, database, node, cluster, endpoint): """Peform ZCL operations against a device""" node = t.EmberEUI64([int(p, base=16) for p in node.split(':')]) ctx.obj['database_file'] = database ctx.obj['node'] = node ctx.obj['endpoint'] = endpoint ctx.obj['cluster'] = cluster
async def form_network(self): nwk = self.config[zigpy.config.CONF_NWK] pan_id = nwk[zigpy.config.CONF_NWK_PAN_ID] if pan_id is None: pan_id = int.from_bytes(os.urandom(2), byteorder="little") extended_pan_id = nwk[zigpy.config.CONF_NWK_EXTENDED_PAN_ID] if extended_pan_id is None: extended_pan_id = t.EmberEUI64([t.uint8_t(0)] * 8) hashed_tclk = self._ezsp.ezsp_version > 4 initial_security_state = bellows.zigbee.util.zha_security( nwk, controller=True, hashed_tclk=hashed_tclk) v = await self._ezsp.setInitialSecurityState(initial_security_state) assert v[0] == t.EmberStatus.SUCCESS # TODO: Better check parameters = t.EmberNetworkParameters() parameters.panId = t.EmberPanId(pan_id) parameters.extendedPanId = extended_pan_id parameters.radioTxPower = t.uint8_t(8) parameters.radioChannel = t.uint8_t(nwk[zigpy.config.CONF_NWK_CHANNEL]) parameters.joinMethod = t.EmberJoinMethod.USE_MAC_ASSOCIATION parameters.nwkManagerId = t.EmberNodeId(0) parameters.nwkUpdateId = t.uint8_t( nwk[zigpy.config.CONF_NWK_UPDATE_ID]) parameters.channels = nwk[zigpy.config.CONF_NWK_CHANNELS] await self._ezsp.formNetwork(parameters) await self._ezsp.setValue( self._ezsp.types.EzspValueId.VALUE_STACK_TOKEN_WRITING, 1)
def zha_security(controller=False): empty_key_data = t.EmberKeyData() empty_key_data.contents = t.fixed_list(16, t.uint8_t)([t.uint8_t(0)] * 16) zha_key = t.EmberKeyData() zha_key.contents = t.fixed_list( 16, t.uint8_t)([t.uint8_t(c) for c in b"ZigBeeAlliance09"]) isc = t.EmberInitialSecurityState() isc.bitmask = t.uint16_t( t.EmberInitialSecurityBitmask.HAVE_PRECONFIGURED_KEY | t.EmberInitialSecurityBitmask.REQUIRE_ENCRYPTED_KEY) isc.preconfiguredKey = zha_key isc.networkKey = empty_key_data isc.networkKeySequenceNumber = t.uint8_t(0) isc.preconfiguredTrustCenterEui64 = t.EmberEUI64([t.uint8_t(0)] * 8) if controller: isc.bitmask |= ( t.EmberInitialSecurityBitmask.TRUST_CENTER_GLOBAL_LINK_KEY | t.EmberInitialSecurityBitmask.HAVE_NETWORK_KEY) isc.bitmask = t.uint16_t(isc.bitmask) random_key = t.fixed_list( 16, t.uint8_t)([t.uint8_t(x) for x in os.urandom(16)]) isc.networkKey = random_key return isc
async def permit_with_key(self, node, code, time_s=60): if type(node) is not t.EmberEUI64: node = t.EmberEUI64([t.uint8_t(p) for p in node]) key = zigpy.util.convert_install_code(code) if key is None: raise Exception("Invalid install code") link_key = t.EmberKeyData() link_key.contents = key v = await self._ezsp.addTransientLinkKey(node, link_key) if v[0] != t.EmberStatus.SUCCESS: raise Exception("Failed to set link key") v = await self._ezsp.setPolicy( t.EzspPolicyId.TC_KEY_REQUEST_POLICY, t.EzspDecisionId.GENERATE_NEW_TC_LINK_KEY, ) if v[0] != t.EmberStatus.SUCCESS: raise Exception( "Failed to change policy to allow generation of new trust center keys" ) return await self.permit(time_s)
def test_multi_address_3(): ma = types.MultiAddress() ma.addrmode = 3 ma.ieee = t.EmberEUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) ma.endpoint = 1 ser = ma.serialize() ma2, data = types.MultiAddress.deserialize(ser) assert data == b'' assert ma2.addrmode == ma.addrmode assert ma2.ieee == ma.ieee assert ma2.endpoint == ma.endpoint
def test_get_aps(): app_mock = mock.MagicMock() ieee = t.EmberEUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) dev = device.Device(app_mock, ieee, 65535) ep = endpoint.Endpoint(dev, 55) ep.status = endpoint.Status.INITIALIZED ep.profile_id = 99 aps = ep.get_aps(255) assert aps.profileId == 99 assert aps.clusterId == 255 assert aps.sourceEndpoint == 55 assert aps.destinationEndpoint == 55
def permit_with_key(self, node, code, time_s=60): if type(node) is not t.EmberEUI64: node = t.EmberEUI64([t.uint8_t(p) for p in node]) key = bellows.zigbee.util.convert_install_code(code) if key is None: raise Exception("Invalid install code") v = yield from self._ezsp.addTransientLinkKey(node, key) if v[0] != 0: raise Exception("Failed to set link key") return self._ezsp.permitJoining(time_s, True)
def permit_with_key(ctx, database, duration_s, node, code): """Allow devices to join this ZigBee network using an install code""" ctx.obj['database_file'] = database node = t.EmberEUI64([t.uint8_t(p, base=16) for p in node.split(':')]) code = binascii.unhexlify(code) def inner(ctx): app = ctx.obj['app'] try: yield from app.permit_with_key(node, code, duration_s) click.echo("Joins are permitted for the next %ss..." % (duration_s, )) yield from asyncio.sleep(duration_s + 1) click.echo("Done") except Exception as e: click.echo(e) return util.app(inner)(ctx)
async def permit_with_key(self, node, code, time_s=60): if type(node) is not t.EmberEUI64: node = t.EmberEUI64([t.uint8_t(p) for p in node]) key = zigpy.util.convert_install_code(code) if key is None: raise Exception("Invalid install code") v = await self._ezsp.addTransientLinkKey(node, key) if v[0] != t.EmberStatus.SUCCESS: raise Exception("Failed to set link key") v = await self._ezsp.setPolicy( t.EzspPolicyId.TC_KEY_REQUEST_POLICY, t.EzspDecisionId.GENERATE_NEW_TC_LINK_KEY, ) if v[0] != t.EmberStatus.SUCCESS: raise Exception("Failed to change policy to allow generation of new trust center keys") """ send mgmt-permit-join to all router """ await self.send_zdo_broadcast(0x0036, 0x0000, 0x00, [time_s, 0]) return self._ezsp.permitJoining(time_s, True)
async def permit_with_key(self, node, code, time_s=60): if type(node) is not t.EmberEUI64: node = t.EmberEUI64([t.uint8_t(p) for p in node]) key = zigpy.util.convert_install_code(code) if key is None: raise Exception("Invalid install code") link_key = t.EmberKeyData(key) v = await self._ezsp.addTransientLinkKey(node, link_key) if v[0] != t.EmberStatus.SUCCESS: raise Exception("Failed to set link key") if self._ezsp.ezsp_version >= 8: mask_type = self._ezsp.types.EzspDecisionBitmask.ALLOW_JOINS bitmask = mask_type.ALLOW_JOINS | mask_type.JOINS_USE_INSTALL_CODE_KEY await self._ezsp.setPolicy( self._ezsp.types.EzspPolicyId.TRUST_CENTER_POLICY, bitmask) return await super().permit(time_s)
def permit_with_key(self, node, code, time_s=60): if type(node) is not t.EmberEUI64: node = t.EmberEUI64([t.uint8_t(p) for p in node]) key = bellows.zigbee.util.convert_install_code(code) if key is None: raise Exception("Invalid install code") v = yield from self._ezsp.addTransientLinkKey(node, key) if v[0] != 0: raise Exception("Failed to set link key") v = yield from self._ezsp.setPolicy( t.EzspPolicyId.TC_KEY_REQUEST_POLICY, t.EzspDecisionId.GENERATE_NEW_TC_LINK_KEY, ) if v[0] != 0: raise Exception( "Failed to change policy to allow generation of new trust center keys" ) return self._ezsp.permitJoining(time_s, True)
def zdo(ctx, node, database): """Perform ZDO operations against a device""" node = t.EmberEUI64([int(p, base=16) for p in node.split(':')]) ctx.obj['node'] = node ctx.obj['database_file'] = database
def ieee(init=0): return t.EmberEUI64(map(t.uint8_t, range(init, init + 8)))
async def _restore(ezsp, backup_data, force, update_eui64_token=False, upg_tc_link_key=False): """Restore backup.""" (status, ) = await ezsp.networkInit() LOGGER.debug("Network init status: %s", status) assert status in (t.EmberStatus.SUCCESS, t.EmberStatus.NOT_JOINED) if status == t.EmberStatus.SUCCESS: if not force: click.echo("Network is up, not forcing restore") return try: (status, ) = await ezsp.leaveNetwork() if status != t.EmberStatus.NETWORK_DOWN: LOGGER.error("Couldn't leave network") return except asyncio.TimeoutError: LOGGER.error("Didn't not receive stack changed status callback") return if update_eui64_token: ncp_eui64 = t.EmberEUI64(backup_data[ATTR_NODE_EUI64]).serialize() (status, ) = await ezsp.setMfgToken(t.EzspMfgTokenId.MFG_CUSTOM_EUI_64, ncp_eui64) sec_bitmask = (t.EmberInitialSecurityBitmask.HAVE_PRECONFIGURED_KEY | t.EmberInitialSecurityBitmask.REQUIRE_ENCRYPTED_KEY | t.EmberInitialSecurityBitmask.TRUST_CENTER_GLOBAL_LINK_KEY | t.EmberInitialSecurityBitmask.HAVE_NETWORK_KEY | t.EmberInitialSecurityBitmask.NO_FRAME_COUNTER_RESET) if not is_well_known_key(backup_data[ATTR_KEY_GLOBAL][ATTR_KEY]): sec_bitmask |= t.EmberInitialSecurityBitmask.TRUST_CENTER_USES_HASHED_LINK_KEY init_sec_state = t.EmberInitialSecurityState( bitmask=sec_bitmask, preconfiguredKey=backup_data[ATTR_KEY_GLOBAL][ATTR_KEY], networkKey=backup_data[ATTR_KEY_NWK][ATTR_KEY], networkKeySequenceNumber=backup_data[ATTR_KEY_NWK][ATTR_KEY_SEQ], preconfiguredTrustCenterEui64=[0x00] * 8, ) if (upg_tc_link_key and t.EmberInitialSecurityBitmask.TRUST_CENTER_USES_HASHED_LINK_KEY not in sec_bitmask): init_sec_state.bitmask |= ( t.EmberInitialSecurityBitmask.TRUST_CENTER_USES_HASHED_LINK_KEY) init_sec_state.preconfiguredKey = t.EmberKeyData(os.urandom(16)) (status, ) = await ezsp.setInitialSecurityState(init_sec_state) LOGGER.debug("Set initial security state: %s", status) assert status == t.EmberStatus.SUCCESS if backup_data[ATTR_KEY_TABLE]: await _restore_keys(ezsp, backup_data[ATTR_KEY_TABLE]) network_key = backup_data[ATTR_KEY_NWK] (status, ) = await ezsp.setValue( ezsp.types.EzspValueId.VALUE_NWK_FRAME_COUNTER, t.uint32_t(network_key[ATTR_KEY_FRAME_COUNTER_OUT]).serialize(), ) LOGGER.debug("Set network frame counter: %s", status) assert status == t.EmberStatus.SUCCESS tc_key = backup_data[ATTR_KEY_GLOBAL] (status, ) = await ezsp.setValue( ezsp.types.EzspValueId.VALUE_APS_FRAME_COUNTER, t.uint32_t(tc_key[ATTR_KEY_FRAME_COUNTER_OUT]).serialize(), ) LOGGER.debug("Set network frame counter: %s", status) assert status == t.EmberStatus.SUCCESS await _form_network(ezsp, backup_data) await asyncio.sleep(2)
def convert_ieee(s): l = [t.uint8_t(p, base=16) for p in s.split(b':')] return t.EmberEUI64(l)
def convert(self, value, param, ctx): if ":" not in value or len(value) != 23: self.fail("Node format should be a 8 byte hex string seprated by ':'") return t.EmberEUI64([t.uint8_t(p, base=16) for p in value.split(':')])
def dev(): app_mock = mock.MagicMock() ieee = t.EmberEUI64(map(t.uint8_t, [0, 1, 2, 3, 4, 5, 6, 7])) return device.Device(app_mock, ieee, 65535)