Add information request/response signal handler to the Bluetooth Host demo.
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / BluetoothACLPackets.c
index ed39d5c..17503ea 100644 (file)
@@ -56,35 +56,33 @@ void Bluetooth_ProcessACLPackets(void)
        BT_ACL_DEBUG("-- Payload Length: 0x%04X", DataHeader.PayloadLength);\r
 #endif\r
 \r
-       if (DataHeader.DestinationChannel == BLUETOOTH_CHANNEL_SIGNALING)\r
+       if (DataHeader.DestinationChannel == BT_CHANNEL_SIGNALING)\r
        {\r
                Bluetooth_SignalCommand_Header_t SignalCommandHeader;\r
                Pipe_Read_Stream_LE(&SignalCommandHeader, sizeof(SignalCommandHeader));\r
 \r
                switch (SignalCommandHeader.Code)\r
                {\r
-                       case BLUETOOTH_SIGNAL_CONNECTION_REQUEST:\r
+                       case BT_SIGNAL_CONNECTION_REQUEST:\r
                                Bluetooth_SignalPacket_ConnectionRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);\r
                                break;\r
-                       case BLUETOOTH_SIGNAL_CONFIGURATION_REQUEST:\r
+                       case BT_SIGNAL_CONFIGURATION_REQUEST:\r
                                Bluetooth_SignalPacket_ConfigurationRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);\r
                                break;\r
-                       case BLUETOOTH_SIGNAL_DISCONNECTION_REQUEST:\r
+                       case BT_SIGNAL_DISCONNECTION_REQUEST:\r
                                Bluetooth_SignalPacket_DisconnectionRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);\r
                                break;                  \r
-                       case BLUETOOTH_SIGNAL_ECHO_REQUEST:\r
+                       case BT_SIGNAL_ECHO_REQUEST:\r
                                Bluetooth_SignalPacket_EchoRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);\r
                                break;\r
-                       case BLUETOOTH_SIGNAL_INFORMATION_REQUEST:\r
-                               BT_ACL_DEBUG("<< Information Request", NULL);\r
-\r
-                               Pipe_Discard_Stream(ACLPacketHeader.DataLength);\r
-                               Pipe_ClearIN();         \r
-                               Pipe_Freeze();\r
+                       case BT_SIGNAL_INFORMATION_REQUEST:\r
+                               Bluetooth_SignalPacket_InformationRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);\r
                                break;\r
                        default:\r
+                               #if (ACL_DEBUG_LEVEL > 0)\r
                                BT_ACL_DEBUG("<< Unknown Signaling Command 0x%02X", SignalCommandHeader.Code);\r
-                                       \r
+                               #endif\r
+       \r
                                Pipe_Discard_Stream(ACLPacketHeader.DataLength);\r
                                Pipe_ClearIN();         \r
                                Pipe_Freeze();\r
@@ -116,7 +114,9 @@ static inline void Bluetooth_SignalPacket_ConnectionRequest(Bluetooth_ACL_Header
        \r
        Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest));\r
 \r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG("<< L2CAP Connection Request", NULL);\r
+#endif\r
 #if (ACL_DEBUG_LEVEL > 1)\r
        BT_ACL_DEBUG("-- PSM: 0x%04X", ConnectionRequest.PSM);\r
        BT_ACL_DEBUG("-- Source Channel: 0x%04X", ConnectionRequest.SourceChannel);\r
@@ -131,14 +131,13 @@ static inline void Bluetooth_SignalPacket_ConnectionRequest(Bluetooth_ACL_Header
 \r
        ACLPacketHeader->DataLength           = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(ConnectionResponse);\r
        DataHeader->PayloadLength             = sizeof(*SignalCommandHeader) + sizeof(ConnectionResponse);\r
-       DataHeader->DestinationChannel        = BLUETOOTH_CHANNEL_SIGNALING;\r
-       SignalCommandHeader->Code             = BLUETOOTH_SIGNAL_CONNECTION_RESPONSE;\r
+       DataHeader->DestinationChannel        = BT_CHANNEL_SIGNALING;\r
+       SignalCommandHeader->Code             = BT_SIGNAL_CONNECTION_RESPONSE;\r
        SignalCommandHeader->Length           = sizeof(ConnectionResponse);\r
        \r
        Bluetooth_Channel_t* ChannelData      = Bluetooth_InitChannelData(ConnectionRequest.SourceChannel, ConnectionRequest.PSM);\r
        \r
-       ConnectionResponse.Result             = (ChannelData == NULL) ? BLUETOOTH_CONNECTION_REFUSED_RESOURCES :\r
-                                                                                                                                   BLUETOOTH_CONNECTION_SUCCESSFUL;\r
+       ConnectionResponse.Result             = (ChannelData == NULL) ? BT_CONNECTION_REFUSED_RESOURCES : BT_CONNECTION_SUCCESSFUL;\r
        ConnectionResponse.DestinationChannel = ChannelData->LocalNumber;\r
        ConnectionResponse.SourceChannel      = ChannelData->RemoteNumber;\r
        ConnectionResponse.Status             = 0x00;\r
@@ -158,7 +157,9 @@ static inline void Bluetooth_SignalPacket_ConnectionRequest(Bluetooth_ACL_Header
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DataHeader->DestinationChannel);\r
        BT_ACL_DEBUG("-- Payload Length: 0x%04X", DataHeader->PayloadLength);                   \r
 #endif\r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG(">> L2CAP Connection Response", NULL);\r
+#endif\r
 #if (ACL_DEBUG_LEVEL > 1)\r
        BT_ACL_DEBUG("-- Source Channel: 0x%04X", ConnectionResponse.SourceChannel);\r
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", ConnectionResponse.DestinationChannel);\r
@@ -170,10 +171,13 @@ static inline void Bluetooth_SignalPacket_ConfigurationRequest(Bluetooth_ACL_Hea
                                                                Bluetooth_SignalCommand_Header_t* SignalCommandHeader)\r
 {\r
        Bluetooth_SignalCommand_ConfigurationRequest_t ConfigurationRequest;\r
-       \r
        Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest));\r
+       \r
+       // TODO: Process/Discard configuration options here\r
 \r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG("<< L2CAP Configuration Request", NULL);\r
+#endif\r
 #if (ACL_DEBUG_LEVEL > 1)\r
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", ConfigurationRequest.DestinationChannel);\r
 #endif\r
@@ -187,8 +191,8 @@ static inline void Bluetooth_SignalPacket_ConfigurationRequest(Bluetooth_ACL_Hea
 \r
        ACLPacketHeader->DataLength           = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(ConfigurationResponse);\r
        DataHeader->PayloadLength             = sizeof(*SignalCommandHeader) + sizeof(ConfigurationResponse);\r
-       DataHeader->DestinationChannel        = BLUETOOTH_CHANNEL_SIGNALING;\r
-       SignalCommandHeader->Code             = BLUETOOTH_SIGNAL_CONFIGURATION_RESPONSE;\r
+       DataHeader->DestinationChannel        = BT_CHANNEL_SIGNALING;\r
+       SignalCommandHeader->Code             = BT_SIGNAL_CONFIGURATION_RESPONSE;\r
        SignalCommandHeader->Length           = sizeof(ConfigurationResponse);\r
        \r
        Bluetooth_Channel_t* ChannelData      = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, false);\r
@@ -200,7 +204,7 @@ static inline void Bluetooth_SignalPacket_ConfigurationRequest(Bluetooth_ACL_Hea
 \r
        ConfigurationResponse.SourceChannel   = ChannelData->RemoteNumber;\r
        ConfigurationResponse.Flags           = 0x00;\r
-       ConfigurationResponse.Result          = (ChannelData != NULL) ? BLUETOOTH_CONFIGURATION_SUCCESSFUL : BLUETOOTH_CONFIGURATION_REJECTED;\r
+       ConfigurationResponse.Result          = (ChannelData != NULL) ? BT_CONFIGURATION_SUCCESSFUL : BT_CONFIGURATION_REJECTED;\r
 \r
        Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));\r
        Pipe_Write_Stream_LE(DataHeader, sizeof(*DataHeader));\r
@@ -217,7 +221,12 @@ static inline void Bluetooth_SignalPacket_ConfigurationRequest(Bluetooth_ACL_Hea
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DataHeader->DestinationChannel);\r
        BT_ACL_DEBUG("-- Payload Length: 0x%04X", DataHeader->PayloadLength);                   \r
 #endif\r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG(">> L2CAP Configuration Response", NULL);\r
+#endif\r
+#if (ACL_DEBUG_LEVEL > 1)\r
+       BT_ACL_DEBUG("-- Result: 0x%02X", ConfigurationResponse.Result);\r
+#endif\r
 }\r
 \r
 static inline void Bluetooth_SignalPacket_DisconnectionRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,\r
@@ -228,7 +237,9 @@ static inline void Bluetooth_SignalPacket_DisconnectionRequest(Bluetooth_ACL_Hea
        \r
        Pipe_Read_Stream_LE(&DisconnectionRequest, sizeof(DisconnectionRequest));\r
 \r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG("<< L2CAP Disconnection Request", NULL);\r
+#endif\r
 #if (ACL_DEBUG_LEVEL > 1)\r
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DisconnectionRequest.DestinationChannel);\r
        BT_ACL_DEBUG("-- Source Channel: 0x%04X", DisconnectionRequest.SourceChannel);\r
@@ -243,8 +254,8 @@ static inline void Bluetooth_SignalPacket_DisconnectionRequest(Bluetooth_ACL_Hea
 \r
        ACLPacketHeader->DataLength           = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(DisconnectionResponse);\r
        DataHeader->PayloadLength             = sizeof(*SignalCommandHeader) + sizeof(DisconnectionResponse);\r
-       DataHeader->DestinationChannel        = BLUETOOTH_CHANNEL_SIGNALING;\r
-       SignalCommandHeader->Code             = BLUETOOTH_SIGNAL_DISCONNECTION_RESPONSE;\r
+       DataHeader->DestinationChannel        = BT_CHANNEL_SIGNALING;\r
+       SignalCommandHeader->Code             = BT_SIGNAL_DISCONNECTION_RESPONSE;\r
        SignalCommandHeader->Length           = sizeof(DisconnectionResponse);\r
        \r
        Bluetooth_Channel_t* ChannelData      = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, true);\r
@@ -270,7 +281,9 @@ static inline void Bluetooth_SignalPacket_DisconnectionRequest(Bluetooth_ACL_Hea
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DataHeader->DestinationChannel);\r
        BT_ACL_DEBUG("-- Payload Length: 0x%04X", DataHeader->PayloadLength);                   \r
 #endif\r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG(">> L2CAP Disconnection Response", NULL);\r
+#endif\r
 #if (ACL_DEBUG_LEVEL > 1)\r
        BT_ACL_DEBUG("-- Source Channel: 0x%04X", DisconnectionResponse.SourceChannel);\r
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DisconnectionResponse.DestinationChannel);\r
@@ -281,7 +294,9 @@ static inline void Bluetooth_SignalPacket_EchoRequest(Bluetooth_ACL_Header_t* AC
                                                       Bluetooth_DataPacket_Header_t* DataHeader,\r
                                                       Bluetooth_SignalCommand_Header_t* SignalCommandHeader)\r
 {\r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG("<< L2CAP Echo Request", NULL);\r
+#endif\r
        \r
        Pipe_ClearIN();\r
        Pipe_Freeze();\r
@@ -290,8 +305,8 @@ static inline void Bluetooth_SignalPacket_EchoRequest(Bluetooth_ACL_Header_t* AC
        \r
        ACLPacketHeader->DataLength           = sizeof(*DataHeader) + sizeof(*SignalCommandHeader);\r
        DataHeader->PayloadLength             = sizeof(*SignalCommandHeader);\r
-       DataHeader->DestinationChannel        = BLUETOOTH_CHANNEL_SIGNALING;\r
-       SignalCommandHeader->Code             = BLUETOOTH_SIGNAL_ECHO_RESPONSE;\r
+       DataHeader->DestinationChannel        = BT_CHANNEL_SIGNALING;\r
+       SignalCommandHeader->Code             = BT_SIGNAL_ECHO_RESPONSE;\r
        SignalCommandHeader->Length           = 0;\r
        \r
        Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));\r
@@ -308,5 +323,82 @@ static inline void Bluetooth_SignalPacket_EchoRequest(Bluetooth_ACL_Header_t* AC
        BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DataHeader->DestinationChannel);\r
        BT_ACL_DEBUG("-- Payload Length: 0x%04X", DataHeader->PayloadLength);                   \r
 #endif\r
+#if (ACL_DEBUG_LEVEL > 0)\r
        BT_ACL_DEBUG(">> L2CAP Echo Response", NULL);\r
+#endif\r
+}\r
+\r
+static inline void Bluetooth_SignalPacket_InformationRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,\r
+                                                             Bluetooth_DataPacket_Header_t* DataHeader,\r
+                                                             Bluetooth_SignalCommand_Header_t* SignalCommandHeader)\r
+{\r
+       Bluetooth_SignalCommand_InformationRequest_t InformationRequest;\r
+\r
+       Pipe_Read_Stream_LE(&InformationRequest, sizeof(InformationRequest));\r
+\r
+#if (ACL_DEBUG_LEVEL > 0)\r
+       BT_ACL_DEBUG("<< Information Request", NULL);\r
+#endif\r
+#if (ACL_DEBUG_LEVEL > 1)\r
+       BT_ACL_DEBUG("-- Info Type: 0x%04X", InformationRequest.InfoType);\r
+#endif\r
+       \r
+       Pipe_ClearIN();\r
+       Pipe_Freeze();\r
+       Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);\r
+       Pipe_Unfreeze();\r
+       \r
+       Bluetooth_SignalCommand_InformationResponse_t InformationResponse;\r
+       uint8_t ResponseData[4];\r
+       uint8_t ResponseLen;\r
+\r
+       switch (InformationRequest.InfoType)\r
+       {\r
+               case BT_INFOREQ_MTU:            \r
+                       InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;\r
+                       ResponseLen = 2;\r
+                       \r
+                       *((uint16_t*)&ResponseData) = 65533;\r
+                       break;\r
+               case BT_INFOREQ_EXTENDEDFEATURES:\r
+                       InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;\r
+                       ResponseLen = 4;\r
+                       \r
+                       *((uint32_t*)&ResponseData) = 0;\r
+                       break;\r
+               default:\r
+                       InformationResponse.Result = BT_INFORMATION_NOTSUPPORTED;\r
+                       ResponseLen = 0;\r
+                       break;\r
+       }\r
+       \r
+       ACLPacketHeader->DataLength           = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(InformationResponse) +\r
+                                               ResponseLen;\r
+       DataHeader->PayloadLength             = sizeof(*SignalCommandHeader) + sizeof(InformationResponse) + ResponseLen;\r
+       DataHeader->DestinationChannel        = BT_CHANNEL_SIGNALING;\r
+       SignalCommandHeader->Code             = BT_SIGNAL_INFORMATION_RESPONSE;\r
+       SignalCommandHeader->Length           = sizeof(InformationResponse) + ResponseLen;\r
+       \r
+       Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));\r
+       Pipe_Write_Stream_LE(DataHeader, sizeof(*DataHeader));\r
+       Pipe_Write_Stream_LE(SignalCommandHeader, sizeof(*SignalCommandHeader));\r
+       Pipe_Write_Stream_LE(&InformationResponse, sizeof(InformationResponse));\r
+       Pipe_Write_Stream_LE(ResponseData, ResponseLen);\r
+       \r
+       Pipe_ClearOUT();                \r
+       Pipe_Freeze();\r
+\r
+#if (ACL_DEBUG_LEVEL > 1)\r
+       BT_ACL_DEBUG("Packet Sent", NULL);\r
+       BT_ACL_DEBUG("-- Connection Handle: 0x%04X", (ACLPacketHeader->ConnectionHandle & 0x0FFF));\r
+       BT_ACL_DEBUG("-- Data Length: 0x%04X", ACLPacketHeader->DataLength);\r
+       BT_ACL_DEBUG("-- Destination Channel: 0x%04X", DataHeader->DestinationChannel);\r
+       BT_ACL_DEBUG("-- Payload Length: 0x%04X", DataHeader->PayloadLength);                   \r
+#endif\r
+#if (ACL_DEBUG_LEVEL > 0)\r
+       BT_ACL_DEBUG(">> L2CAP Information Response", NULL);    \r
+#endif\r
+#if (ACL_DEBUG_LEVEL > 1)\r
+       BT_ACL_DEBUG("-- Result: 0x%02X", InformationResponse.Result);\r
+#endif\r
 }\r