/* Return: amount of PDU data */ uint16_t MSTP_Get_Send( volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ uint16_t pdu_len = 0; uint8_t destination = 0; /* destination address */ (void) timeout; if (!Transmit_Packet.ready) { return 0; } /* load destination MAC address */ if (Transmit_Packet.address.mac_len == 1) { destination = Transmit_Packet.address.mac[0]; } else { return 0; } if ((MAX_HEADER + Transmit_Packet.pdu_len) > MAX_MPDU) { return 0; } /* convert the PDU into the MSTP Frame */ pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, mstp_port->This_Station, &Transmit_Packet.pdu[0], Transmit_Packet.pdu_len); Transmit_Packet.ready = false; return pdu_len; }
/* returns number of bytes sent on success, zero on failure */ int dlmstp_send_pdu( BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) { /* number of bytes of data */ int bytes_sent = 0; unsigned npdu_len = 0; uint8_t frame_type = 0; uint8_t destination = 0; /* destination address */ BACNET_ADDRESS src; unsigned i = 0; /* loop counter */ if (MSTP_Port.TxReady == false) { if (npdu_data->data_expecting_reply) MSTP_Port.TxFrameType = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; else MSTP_Port.TxFrameType = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; /* load destination MAC address */ if (dest && dest->mac_len == 1) { destination = dest->mac[0]; } else { return -2; } dlmstp_get_my_address(&src); if ((MAX_HEADER + pdu_len) > MAX_MPDU) { return -4; } bytes_sent = MSTP_Create_Frame((uint8_t *) & MSTP_Port.TxBuffer[0], sizeof(MSTP_Port.TxBuffer), MSTP_Port.TxFrameType, destination, MSTP_Port.This_Station, pdu, pdu_len); MSTP_Port.TxLength = bytes_sent; MSTP_Port.TxReady = true; MSTP_Packets++; } return bytes_sent; }
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */ uint16_t MSTP_Get_Reply( volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ uint16_t pdu_len = 0; /* return value */ uint8_t destination = 0; /* destination address */ bool matched = false; (void) timeout; if (!Transmit_Packet.ready) { return 0; } /* load destination MAC address */ if (Transmit_Packet.address.mac_len == 1) { destination = Transmit_Packet.address.mac[0]; } else { return 0; } if ((MAX_HEADER + Transmit_Packet.pdu_len) > MAX_MPDU) { return 0; } /* is this the reply to the DER? */ matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0], mstp_port->DataLength, mstp_port->SourceAddress, &Transmit_Packet.pdu[0], Transmit_Packet.pdu_len, &Transmit_Packet.address); if (!matched) return 0; /* convert the PDU into the MSTP Frame */ pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, mstp_port->This_Station, &Transmit_Packet.pdu[0], Transmit_Packet.pdu_len); Transmit_Packet.ready = false; return pdu_len; }