Exemplo n.º 1
0
    def _recv_frame(self, raw_frame):
        if not raw_frame.extended:
            return

        frame = transport.Frame(raw_frame.id, raw_frame.data,
                                raw_frame.ts_monotonic, raw_frame.ts_real)

        transfer_frames = self._transfer_manager.receive_frame(frame)
        if not transfer_frames:
            return

        transfer = transport.Transfer()
        transfer.from_frames(transfer_frames)

        self._transfer_hook_dispatcher.call_hooks(
            self._transfer_hook_dispatcher.TRANSFER_DIRECTION_INCOMING,
            transfer)

        if (transfer.service_not_message and not transfer.request_not_response) and \
                transfer.dest_node_id == self._node_id:
            # This is a reply to a request we sent. Look up the original request and call the appropriate callback
            requests = self._outstanding_requests.keys()
            for key in requests:
                if transfer.is_response_to(self._outstanding_requests[key]):
                    # Call the request's callback and remove it from the active list
                    event = TransferEvent(transfer, self, 'response')
                    self._outstanding_request_callbacks[key](event)
                    del self._outstanding_requests[key]
                    del self._outstanding_request_callbacks[key]
                    break
        elif not transfer.service_not_message or transfer.dest_node_id == self._node_id:
            # This is a request or a broadcast; look up the appropriate handler by data type ID
            self._handler_dispatcher.call_handlers(transfer)
Exemplo n.º 2
0
    def _recv_frame(self, dev, message):
        frame_id, frame_data, ext_id = message
        if not ext_id:
            return

        frame = transport.Frame(frame_id, frame_data)
        # logging.debug("Node._recv_frame(): got {0!s}".format(frame))

        transfer_frames = self.transfer_manager.receive_frame(frame)
        if not transfer_frames:
            return

        transfer = transport.Transfer()
        transfer.from_frames(transfer_frames)

        logging.debug("Node._recv_frame(): received {0!r}".format(transfer))

        # If it's a node info request, keep track of the status of each node
        if transfer.payload.type == uavcan.protocol.NodeStatus:
            self.node_info[transfer.source_node_id] = {
                "uptime": transfer.payload.uptime_sec,
                "health": transfer.payload.health,
                "mode": transfer.payload.mode,
                "sub_mode": transfer.payload.sub_mode,
                "vendor_specific_status_code":
                transfer.payload.vendor_specific_status_code,
                "timestamp": time.time()
            }

        if (transfer.service_not_message and not
                transfer.request_not_response) and \
                transfer.dest_node_id == self.node_id:
            # This is a reply to a request we sent. Look up the original
            # request and call the appropriate callback
            requests = self.outstanding_requests.keys()
            for key in requests:
                if transfer.is_response_to(self.outstanding_requests[key]):
                    # Call the request's callback and remove it from the
                    # active list
                    self.outstanding_request_callbacks[key](transfer.payload,
                                                            transfer)
                    del self.outstanding_requests[key]
                    del self.outstanding_request_callbacks[key]
                    del self.outstanding_request_timestamps[key]
                    del self.outstanding_request_retries[key]
                    break
        elif not transfer.service_not_message or \
                transfer.dest_node_id == self.node_id:
            # This is a request, a unicast or a broadcast; look up the
            # appropriate handler by data type ID
            for handler in self.handlers:
                if handler[0] == transfer.payload.type:
                    kwargs = handler[2] if len(handler) == 3 else {}
                    h = handler[1](transfer.payload, transfer, self, **kwargs)
                    h._execute()
Exemplo n.º 3
0
    def handle_raw_frame(self, raw_frame, handle_transfer=True):
        frame = transport.Frame(raw_frame.id, raw_frame.data, raw_frame.ts_monotonic, raw_frame.ts_real)

        transfer_frames = self._transfer_manager.receive_frame(frame)
        if not transfer_frames:
            return

        transfer = transport.Transfer()
        transfer.from_frames(transfer_frames)

        if handle_transfer:
            self.handle_transfer(transfer)
            return None
        else:
            return transfer
Exemplo n.º 4
0
    def _recv_frame(self, dev, message):
        frame_id, frame_data, ext_id = message
        if not ext_id:
            return

        frame = transport.Frame(frame_id, frame_data)
        # logging.debug("Node._recv_frame(): got {0!s}".format(frame))

        transfer_frames = self.transfer_manager.receive_frame(frame)
        if not transfer_frames:
            return

        dtid = transfer_frames[0].data_type_id
        if transfer_frames[0].transfer_priority == \
                transport.TransferPriority.SERVICE:
            kind = dsdl.parser.CompoundType.KIND_SERVICE
        else:
            kind = dsdl.parser.CompoundType.KIND_MESSAGE
        datatype = uavcan.DATATYPES.get((dtid, kind))
        if not datatype:
            logging.debug(("Node._recv_frame(): unrecognised data type " +
                           "ID {0:d} for kind {1:d}").format(dtid, kind))
            return

        transfer = transport.Transfer()
        transfer.from_frames(transfer_frames, datatype_crc=datatype.base_crc)

        if transfer.is_message():
            payload = datatype()  # Broadcast or unicast
        elif transfer.is_request():
            payload = datatype(mode="request")
        else:  # transfer.is_response()
            payload = datatype(mode="response")

        payload.unpack(transport.bits_from_bytes(transfer.payload))

        logging.info("Node._recv_frame(): received {0!r}".format(payload))

        # If it's a node info request, keep track of the status of each node
        if payload.type == uavcan.protocol.NodeStatus:
            self.node_info[transfer.source_node_id] = {
                "uptime": payload.uptime_sec,
                "status": payload.status_code,
                "timestamp": time.time()
            }

        if transfer.is_response() and transfer.dest_node_id == self.node_id:
            # This is a reply to a request we sent. Look up the original
            # request and call the appropriate callback
            requests = self.outstanding_requests.keys()
            for key in requests:
                if transfer.is_response_to(self.outstanding_requests[key]):
                    # Call the request's callback and remove it from the
                    # active list
                    if key in self.outstanding_request_callbacks:
                        self.outstanding_request_callbacks[key]((payload, transfer))
                        del self.outstanding_request_callbacks[key]
                    del self.outstanding_requests[key]
                    del self.outstanding_request_timestamps[key]
                    break
        elif transfer.is_broadcast() or transfer.dest_node_id == self.node_id:
            # This is a request, a unicast or a broadcast; look up the
            # appropriate handler by data type ID
            for handler in self.handlers:
                if handler[0] == datatype:
                    kwargs = handler[2] if len(handler) == 3 else {}
                    h = handler[1](payload, transfer, self, **kwargs)
                    h._execute()