Fixed Benito and USBtoSerial projects not turning off the USART before reconfiguring...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / BluetoothEvents.c
index 17cf7ed..543e1b8 100644 (file)
 \r
 #include "BluetoothEvents.h"\r
 \r
-/** Bluetooth RFCOMM channel structure - used to send and receive RFCOMM data between the local and remote\r
- *  device once a RFCOMM channel has been opened.\r
+/** Pointer to the opened Bluetooth ACL channel structure for RFCOMM, used to send and receive data between the\r
+ *  local and remote device once a RFCOMM channel has been opened.\r
  */\r
-Bluetooth_Channel_t* RFCOMMChannel = NULL;\r
+Bluetooth_Channel_t* SerialChannel_ACL    = NULL;\r
 \r
+/** Pointer to the opened RFCOMM logical channel between local and remote device, once a RFCOMM ACL channel has been\r
+ *  negotiated and a logical RFCOMM channel requested.\r
+ */\r
+RFCOMM_Channel_t*    SerialChannel_RFCOMM = NULL;\r
 \r
 /** Bluetooth stack callback event for when the Bluetooth stack has fully initialized using the attached\r
  *  Bluetooth dongle.\r
@@ -111,46 +115,79 @@ void Bluetooth_DisconnectionComplete(void)
  */\r
 bool Bluetooth_ChannelConnectionRequest(const uint16_t PSM)\r
 {\r
-       /* Always accept channel connection requests regardless of PSM */\r
-       return true;\r
+       /* Only accept connections for channels that will be used for RFCOMM or SDP data */\r
+       return ((PSM == CHANNEL_PSM_RFCOMM) || (PSM == CHANNEL_PSM_SDP));\r
 }\r
 \r
 /** Bluetooth stack callback event for when a Bluetooth ACL channel has been fully created and configured,\r
  *  either at the request of the local device, or the remote device.\r
  *\r
- *  \param[in] Channel  Bluetooth ACL data channel information structure for the channel that can now be used\r
+ *  \param[in] ACLChannel  Bluetooth ACL data channel information structure for the channel that can now be used\r
  */\r
-void Bluetooth_ChannelOpened(Bluetooth_Channel_t* const Channel)\r
+void Bluetooth_ChannelOpened(Bluetooth_Channel_t* const ACLChannel)\r
 {\r
        /* Save the RFCOMM channel for later use when we want to send RFCOMM data */\r
-       if (Channel->PSM == CHANNEL_PSM_RFCOMM)\r
-         RFCOMMChannel = Channel;\r
+       if (ACLChannel->PSM == CHANNEL_PSM_RFCOMM)\r
+         SerialChannel_ACL = ACLChannel;\r
 }\r
 \r
 /** Bluetooth stack callback event for a non-signal ACL packet reception. This callback fires once a connection\r
- *  to a remote Bluetooth device has been made, and the remote device has sent a non-signalling ACL packet.\r
+ *  to a remote Bluetooth device has been made, and the remote device has sent a non-signaling ACL packet.\r
  *\r
- *  \param[in] Data     Pointer to a buffer where the received data is stored\r
- *  \param[in] DataLen  Length of the packet data, in bytes\r
- *  \param[in] Channel  Bluetooth ACL data channel information structure for the packet's destination channel\r
+ *  \param[in] Data        Pointer to a buffer where the received data is stored\r
+ *  \param[in] DataLen     Length of the packet data, in bytes\r
+ *  \param[in] ACLChannel  Bluetooth ACL data channel information structure for the packet's destination channel\r
  */\r
-void Bluetooth_PacketReceived(void* Data, uint16_t DataLen, Bluetooth_Channel_t* const Channel)\r
+void Bluetooth_PacketReceived(void* Data, uint16_t DataLen, Bluetooth_Channel_t* const ACLChannel)\r
 {\r
        /* Run the correct packet handler based on the received packet's PSM, which indicates the service being carried */\r
-       switch (Channel->PSM)\r
+       switch (ACLChannel->PSM)\r
        {\r
                case CHANNEL_PSM_SDP:\r
                        /* Service Discovery Protocol packet */\r
-                       SDP_ProcessPacket(Data, Channel);\r
+                       SDP_ProcessPacket(Data, ACLChannel);\r
                        break;\r
                case CHANNEL_PSM_RFCOMM:\r
                        /* RFCOMM (Serial Port) Protocol packet */\r
-                       RFCOMM_ProcessPacket(Data, Channel);\r
-                       break;\r
-               default:\r
-                       /* Unknown Protocol packet */\r
-                       printf_P(PSTR("Unknown Packet Received (Channel 0x%04X, PSM: 0x%02X, Len: 0x%04X):\r\n"),\r
-                                     Channel->LocalNumber, Channel->PSM, DataLen);                     \r
+                       RFCOMM_ProcessPacket(Data, ACLChannel);\r
                        break;\r
        }\r
 }\r
+\r
+/** RFCOMM layer callback for event for when a RFCOMM logical channel has been fully opened and configured between\r
+ *  the local and remote device. Once open, this RFCOMM channel can be read from and written to freely until is it\r
+ *  closed by either end.\r
+ *\r
+ *  \param[in] RFCOMMChannel  RFCOMM channel that was opened\r
+ */ \r
+void RFCOMM_ChannelOpened(RFCOMM_Channel_t* const RFCOMMChannel)\r
+{\r
+       /* Save the serial port RFCOMM logical channel for later use */\r
+       SerialChannel_RFCOMM = RFCOMMChannel;\r
+}\r
+\r
+/** RFCOMM layer callback event for when a packet is received on an open RFCOMM channel.\r
+ *\r
+ *  \param[in] ACLChannel  RFCOMM ACL channel that the data was directed to\r
+ *  \param[in] DataLen     Length of the received data, in bytes\r
+ *  \param[in] Data        Pointer to a buffer where the received data is stored\r
+ */\r
+void RFCOMM_DataReceived(RFCOMM_Channel_t* const ACLChannel, uint16_t DataLen, const uint8_t* Data)\r
+{\r
+       /* Write the received bytes to the serial port */\r
+       for (uint8_t i = 0; i < DataLen; i++)\r
+         putchar(Data[i]);\r
+       \r
+       /* Echo the data back to the sending device */\r
+       RFCOMM_SendData(DataLen, Data, SerialChannel_RFCOMM, SerialChannel_ACL);\r
+}\r
+\r
+/** RFCOMM layer callback event for when the remote device has updated the channel terminal control signals\r
+ *  for a particular RFCOMM channel.\r
+ *\r
+ *  \param[in] RFCOMMChannel  RFCOMM logical channel whose signals were altered\r
+ */ \r
+void RFCOMM_ChannelSignalsReceived(RFCOMM_Channel_t* const RFCOMMChannel)\r
+{\r
+       // Currently do nothing in response to the remote device sending new terminal control signals\r
+}\r