async def broadcast(self, profile, cluster, src_ep, dst_ep, grpid, radius, sequence, data, broadcast_address=BroadcastAddress.RX_ON_WHEN_IDLE): if not self.is_controller_running: raise ControllerError("ApplicationController is not running") aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_NONE ) aps_frame.groupId = t.uint16_t(grpid) aps_frame.sequence = t.uint8_t(sequence) with self._pending.new(sequence) as req: async with self._in_flight_msg: res = await self._ezsp.sendBroadcast(broadcast_address, aps_frame, radius, sequence, data) if res[0] != t.EmberStatus.SUCCESS: hdr, hdr_args = self._dst_pp(broadcast_address, aps_frame) msg = hdr + "Broadcast failure: %s" msg_args = (hdr_args + (res[0], )) raise DeliveryError(msg % msg_args) # Wait for messageSentHandler message res = await asyncio.wait_for(req.send, timeout=APS_ACK_TIMEOUT) return res
async def request(self, nwk, profile, cluster, src_ep, dst_ep, sequence, data, expect_reply=True, timeout=APS_REPLY_TIMEOUT): if not self.is_controller_running: raise ControllerError("ApplicationController is not running") aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY ) aps_frame.groupId = t.uint16_t(0) aps_frame.sequence = t.uint8_t(sequence) with self._pending.new(sequence, expect_reply) as req: async with self._in_flight_msg: res = await self._ezsp.sendUnicast(self.direct, nwk, aps_frame, sequence, data) if res[0] != t.EmberStatus.SUCCESS: hdr, hdr_args = self._dst_pp(nwk, aps_frame) msg = hdr + "message send failure: %s" msg_args = (hdr_args + (res[0], )) raise DeliveryError(msg % msg_args) res = await asyncio.wait_for(req.send, timeout=APS_ACK_TIMEOUT) if expect_reply: res = await asyncio.wait_for(req.reply, timeout) return res
async def request( self, device, profile, cluster, src_ep, dst_ep, sequence, data, expect_reply=True, use_ieee=False, ): """Submit and send data out as an unicast transmission. :param device: destination device :param profile: Zigbee Profile ID to use for outgoing message :param cluster: cluster id where the message is being sent :param src_ep: source endpoint id :param dst_ep: destination endpoint id :param sequence: transaction sequence number of the message :param data: Zigbee message payload :param expect_reply: True if this is essentially a request :param use_ieee: use EUI64 for destination addressing :returns: return a tuple of a status and an error_message. Original requestor has more context to provide a more meaningful error message """ if not self.is_controller_running: raise ControllerError("ApplicationController is not running") aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY) aps_frame.groupId = t.uint16_t(0) aps_frame.sequence = t.uint8_t(sequence) message_tag = self.get_sequence() if use_ieee: LOGGER.warning(("EUI64 addressing is not currently supported, " "reverting to NWK")) if expect_reply and device.node_desc.is_end_device in (True, None): LOGGER.debug("Extending timeout for %s/0x%04x", device.ieee, device.nwk) await self._ezsp.setExtendedTimeout(device.ieee, True) with self._pending.new(message_tag) as req: async with self._in_flight_msg: res = await self._ezsp.sendUnicast(self.direct, device.nwk, aps_frame, message_tag, data) if res[0] != t.EmberStatus.SUCCESS: return res[0], "EZSP sendUnicast failure: %s" % (res[0], ) res = await asyncio.wait_for(req.result, APS_ACK_TIMEOUT) return res
async def mrequest( self, group_id, profile, cluster, src_ep, sequence, data, *, hops=EZSP_DEFAULT_RADIUS, non_member_radius=EZSP_MULTICAST_NON_MEMBER_RADIUS ): """Submit and send data out as a multicast transmission. :param group_id: destination multicast address :param profile: Zigbee Profile ID to use for outgoing message :param cluster: cluster id where the message is being sent :param src_ep: source endpoint id :param sequence: transaction sequence number of the message :param data: Zigbee message payload :param hops: the message will be delivered to all nodes within this number of hops of the sender. A value of zero is converted to MAX_HOPS :param non_member_radius: the number of hops that the message will be forwarded by devices that are not members of the group. A value of 7 or greater is treated as infinite :returns: return a tuple of a status and an error_message. Original requestor has more context to provide a more meaningful error message """ if not self.is_controller_running: raise ControllerError("ApplicationController is not running") aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(src_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY ) aps_frame.groupId = t.uint16_t(group_id) aps_frame.sequence = t.uint8_t(sequence) message_tag = self.get_sequence() with self._pending.new(message_tag) as req: async with self._in_flight_msg: res = await self._ezsp.sendMulticast( aps_frame, hops, non_member_radius, message_tag, data ) if res[0] != t.EmberStatus.SUCCESS: return res[0], "EZSP sendMulticast failure: %s" % (res[0],) res = await asyncio.wait_for(req.result, APS_ACK_TIMEOUT) return res
def get_aps(self, profile, cluster, endpoint): f = t.EmberApsFrame() f.profileId = t.uint16_t(profile) f.clusterId = t.uint16_t(cluster) f.sourceEndpoint = t.uint8_t(endpoint) f.destinationEndpoint = t.uint8_t(endpoint) f.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY) f.groupId = t.uint16_t(0) f.sequence = t.uint8_t(self._application.get_sequence()) return f
async def broadcast( self, profile, cluster, src_ep, dst_ep, grpid, radius, sequence, data, broadcast_address=BroadcastAddress.RX_ON_WHEN_IDLE, ): """Submit and send data out as an unicast transmission. :param profile: Zigbee Profile ID to use for outgoing message :param cluster: cluster id where the message is being sent :param src_ep: source endpoint id :param dst_ep: destination endpoint id :param: grpid: group id to address the broadcast to :param radius: max radius of the broadcast :param sequence: transaction sequence number of the message :param data: zigbee message payload :param timeout: how long to wait for transmission ACK :param broadcast_address: broadcast address. :returns: return a tuple of a status and an error_message. Original requestor has more context to provide a more meaningful error message """ if not self.is_controller_running: raise ControllerError("ApplicationController is not running") aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption(t.EmberApsOption.APS_OPTION_NONE) aps_frame.groupId = t.uint16_t(grpid) aps_frame.sequence = t.uint8_t(sequence) message_tag = self.get_sequence() with self._pending.new(message_tag) as req: async with self._in_flight_msg: res = await self._ezsp.sendBroadcast(broadcast_address, aps_frame, radius, message_tag, data) if res[0] != t.EmberStatus.SUCCESS: return res[0], "broadcast send failure" # Wait for messageSentHandler message res = await asyncio.wait_for(req.result, timeout=APS_ACK_TIMEOUT) return res
def __init__(self, config: Dict): super().__init__(config) self._ctrl_event = asyncio.Event() self._ezsp = None self._multicast = None self._pending = zigpy.util.Requests() self._watchdog_task = None self._reset_task = None self._in_flight_msg = None self._tx_options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY) self.use_source_routing = self.config[CONF_PARAM_SRC_RTG]
def __init__(self, ezsp, database_file=None, config={}): super().__init__(database_file=database_file, config=CONFIG_SCHEMA(config)) self._ctrl_event = asyncio.Event() self._ezsp = ezsp self._multicast = bellows.multicast.Multicast(ezsp) self._pending = zigpy.util.Requests() self._watchdog_task = None self._reset_task = None self._in_flight_msg = None self._tx_options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY ) self.use_source_routing = self.config[CONF_PARAM_SRC_RTG]
async def request(self, nwk, profile, cluster, src_ep, dst_ep, sequence, data, expect_reply=True, timeout=10): assert sequence not in self._pending send_fut = asyncio.Future() reply_fut = None if expect_reply: reply_fut = asyncio.Future() self._pending[sequence] = (send_fut, reply_fut) aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY) aps_frame.groupId = t.uint16_t(0) aps_frame.sequence = t.uint8_t(sequence) v = await self._ezsp.sendUnicast(self.direct, nwk, aps_frame, sequence, data) if v[0] != t.EmberStatus.SUCCESS: self._pending.pop(sequence) send_fut.cancel() if expect_reply: reply_fut.cancel() raise DeliveryError("Message send failure %s" % (v[0], )) # Wait for messageSentHandler message v = await send_fut if expect_reply: # Wait for reply try: v = await asyncio.wait_for(reply_fut, timeout) except: # noqa: E722 # If we timeout (or fail for any reason), clear the future self._pending.pop(sequence) raise return v
async def request(self, nwk, profile, cluster, src_ep, dst_ep, sequence, data, expect_reply=True, timeout=10): assert sequence not in self._pending send_fut = asyncio.Future() reply_fut = None if expect_reply: reply_fut = asyncio.Future() self._pending[sequence] = (send_fut, reply_fut) aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY) aps_frame.groupId = t.uint16_t(0) aps_frame.sequence = t.uint8_t(sequence) v = await self._ezsp.sendUnicast(self.direct, nwk, aps_frame, sequence, data) if v[0] != t.EmberStatus.SUCCESS: self._pending.pop(sequence) send_fut.cancel() if expect_reply: reply_fut.cancel() raise DeliveryError("Message send failure _send_unicast_fail %s" % (v[0], )) try: v = await send_fut except DeliveryError as e: LOGGER.debug("DeliveryError: %s", e) raise except Exception as e: LOGGER.debug("other Exception: %s", e) if expect_reply: v = await asyncio.wait_for(reply_fut, timeout) return v
async def send_zdo_broadcast(self, command, grpid, radius, args): """ create aps_frame for zdo broadcast""" aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(0x0000) # 0 for zdo aps_frame.clusterId = t.uint16_t(command) aps_frame.sourceEndpoint = t.uint8_t(0) # endpoint 0x00 for zdo aps_frame.destinationEndpoint = t.uint8_t(0) # endpoint 0x00 for zdo aps_frame.options = t.EmberApsOption(t.EmberApsOption.APS_OPTION_NONE) aps_frame.groupId = t.uint16_t(grpid) aps_frame.sequence = t.uint8_t(self.get_sequence()) radius = t.uint8_t(radius) data = aps_frame.sequence.to_bytes(1, 'little') schema = zigpy.zdo.types.CLUSTERS[command][2] data += t.serialize(args, schema) LOGGER.debug("zdo-broadcast: %s - %s", aps_frame, data) await self._ezsp.sendBroadcast(0xfffd, aps_frame, radius, len(data), data)
async def request(self, nwk, profile, cluster, src_ep, dst_ep, sequence, data, expect_reply=True, timeout=15): # LOGGER.debug("pending message queue length: %s", len(self._pending)) assert sequence not in self._pending send_fut = asyncio.Future() reply_fut = None if expect_reply: reply_fut = asyncio.Future() self._pending[sequence] = (send_fut, reply_fut) aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = t.EmberApsOption( t.EmberApsOption.APS_OPTION_RETRY | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY ) aps_frame.groupId = t.uint16_t(0) aps_frame.sequence = t.uint8_t(sequence) LOGGER.debug( "sendUnicast to NWKID:0x%04x DST_EP:%s PROFIL_ID:%s CLUSTER:0x%04x TSN:%s, Timeout:%s", nwk, dst_ep, profile, cluster, sequence, timeout, ) try: v = await asyncio.wait_for(self._ezsp.sendUnicast(self.direct, nwk, aps_frame, sequence, data), timeout) except asyncio.TimeoutError: LOGGER.debug( "sendunicast uart timeout NWKID:0x%04x DST_EP:%s PROFIL_ID:%s CLUSTER:0x%04x TSN:%s, Timeout:%s", nwk, dst_ep, profile, cluster, sequence, timeout, ) self._pending.pop(sequence) send_fut.cancel() if expect_reply: reply_fut.cancel() raise DeliveryError("Message send failure uart timeout") if v[0] != t.EmberStatus.SUCCESS: self._pending.pop(sequence) send_fut.cancel() if expect_reply: reply_fut.cancel() LOGGER.debug("sendunicast send failure NWKID:0x%04x DST_EP:%s PROFIL_ID:%s CLUSTER:0x%04x TSN:%s, Timeout:%s", nwk, dst_ep, profile, cluster, sequence, timeout, ) raise DeliveryError("Message send failure _send_unicast_fail") try: v = await asyncio.wait_for(send_fut, timeout) except DeliveryError as e: LOGGER.debug("0x%04x:%s:0x%04x sendunicast send_ACK failure - Error:%s", nwk, dst_ep, cluster, e) raise except asyncio.TimeoutError: LOGGER.debug("sendunicast message send_ACK timeout NWKID:0x%04x DST_EP:%s PROFIL_ID:%s CLUSTER:0x%04x TSN:%s, Timeout:%s", nwk, dst_ep, profile, cluster, sequence, timeout, ) self._pending.pop(sequence) if expect_reply: reply_fut.cancel() raise DeliveryError("ACK_TIMEOUT") # if v != t.EmberStatus.SUCCESS: # self._pending.pop(sequence) # if expect_reply: # reply_fut.cancel() # LOGGER.debug("sendunicast send_ACK failure 0x%04x:%s:0x%04x = %s", nwk, dst_ep, cluster, v) # raise DeliveryError("sendunicast send_ACK failure") if expect_reply: try: v = await asyncio.wait_for(reply_fut, timeout) except asyncio.TimeoutError: LOGGER.debug( "[0x%04x:%s:0x%04x] sendunicast reply(TSN:%s) timeout failure", nwk, dst_ep, cluster, sequence) self._pending.pop(sequence) raise DeliveryError("sendunicast reply timeout error") return v
async def request( self, device, profile, cluster, src_ep, dst_ep, sequence, data, expect_reply=True, use_ieee=False, ): """Submit and send data out as an unicast transmission. :param device: destination device :param profile: Zigbee Profile ID to use for outgoing message :param cluster: cluster id where the message is being sent :param src_ep: source endpoint id :param dst_ep: destination endpoint id :param sequence: transaction sequence number of the message :param data: Zigbee message payload :param expect_reply: True if this is essentially a request :param use_ieee: use EUI64 for destination addressing :returns: return a tuple of a status and an error_message. Original requestor has more context to provide a more meaningful error message """ if not self.is_controller_running: raise ControllerError("ApplicationController is not running") aps_frame = t.EmberApsFrame() aps_frame.profileId = t.uint16_t(profile) aps_frame.clusterId = t.uint16_t(cluster) aps_frame.sourceEndpoint = t.uint8_t(src_ep) aps_frame.destinationEndpoint = t.uint8_t(dst_ep) aps_frame.options = self._tx_options aps_frame.groupId = t.uint16_t(0) aps_frame.sequence = t.uint8_t(sequence) message_tag = self.get_sequence() if use_ieee: LOGGER.warning(("EUI64 addressing is not currently supported, " "reverting to NWK")) with self._pending.new(message_tag) as req: async with self._in_flight_msg: if expect_reply and device.node_desc.is_end_device in (True, None): LOGGER.debug("Extending timeout for %s/0x%04x", device.ieee, device.nwk) await self._ezsp.setExtendedTimeout(device.ieee, True) if self.use_source_routing and device.relays is not None: res = await self._ezsp.setSourceRoute( device.nwk, device.relays) if res[0] != t.EmberStatus.SUCCESS: LOGGER.warning("Couldn't set source route for %s: %s", device.nwk, res) else: aps_frame.options = t.EmberApsOption( aps_frame.options ^ t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY) LOGGER.debug( "Set source route for %s to %s: %s", device.nwk, device.relays, res, ) delays = [0.5, 1.0, 1.5] while True: status, _ = await self._ezsp.sendUnicast( self.direct, device.nwk, aps_frame, message_tag, data) if not (status == t.EmberStatus.MAX_MESSAGE_LIMIT_REACHED and delays): # retry only on MAX_MESSAGE_LIMIT_REACHED if tries are left break delay = delays.pop(0) LOGGER.debug("retrying request %s tag in %ss", message_tag, delay) await asyncio.sleep(delay) if status != t.EmberStatus.SUCCESS: return status, f"EZSP sendUnicast failure: {str(status)}" res = await asyncio.wait_for(req.result, APS_ACK_TIMEOUT) return res