+ uint8_t FCS = 0xFF;
+
+ /* Calculate new Frame CRC value via the given data bytes and the CRC table */
+ for (uint8_t i = 0; i < Length; i++)
+ FCS = pgm_read_byte(&CRC8_Table[FCS ^ ((const uint8_t*)FrameStart)[i]]);
+
+ return ~FCS;
+}
+
+static void RFCOMM_ProcessDM(const RFCOMM_Address_t* const FrameAddress,
+ Bluetooth_Channel_t* const ACLChannel)
+{
+ BT_RFCOMM_DEBUG(1, "<< DM Received");
+ BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
+}
+
+static void RFCOMM_ProcessDISC(const RFCOMM_Address_t* const FrameAddress,
+ Bluetooth_Channel_t* const ACLChannel)
+{
+ BT_RFCOMM_DEBUG(1, "<< DISC Received");
+ BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
+
+ RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(FrameAddress->DLCI);
+
+ /* If the requested channel is currently open, destroy it */
+ if (RFCOMMChannel != NULL)
+ RFCOMMChannel->State = RFCOMM_Channel_Closed;
+
+ BT_RFCOMM_DEBUG(1, ">> UA Sent");
+ RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, ACLChannel);
+}
+
+static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress,
+ Bluetooth_Channel_t* const ACLChannel)
+{
+ BT_RFCOMM_DEBUG(1, "<< SABM Received");
+ BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
+
+ if (FrameAddress->DLCI == RFCOMM_CONTROL_DLCI)
+ {
+ BT_RFCOMM_DEBUG(1, ">> UA Sent");
+
+ /* Free channel found, or request was to the control channel - accept SABM by sending a UA frame */
+ RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, ACLChannel);
+
+ return;
+ }
+
+ /* Find the existing channel's entry in the channel table */
+ RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(FrameAddress->DLCI);
+
+ /* Existing entry not found, create a new entry for the channel */
+ if (RFCOMMChannel == NULL)
+ RFCOMMChannel = RFCOMM_GetFreeChannelEntry(FrameAddress->DLCI);
+
+ /* If space was found in the channel table for the new channel, ACK the request */
+ if (RFCOMMChannel != NULL)
+ {
+ BT_RFCOMM_DEBUG(1, ">> UA Sent");
+
+ /* Free channel found, or request was to the control channel - accept SABM by sending a UA frame */
+ RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, ACLChannel);
+ }
+ else