Document Bluetooth files with overall file Doxygen comments. Add more initial RFCOMM...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 4 Jun 2010 06:25:12 +0000 (06:25 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 4 Jun 2010 06:25:12 +0000 (06:25 +0000)
Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c
Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
Demos/Host/Incomplete/BluetoothHost/Lib/SDPServices.c
Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c

index 03d0f2a..df8dfc5 100644 (file)
   this software.
 */
 
   this software.
 */
 
+/** \file
+ *
+ *  Bluetooth L2CAP layer management code. This module managed the creation,
+ *  configuration and teardown of L2CAP channels, and manages packet reception
+ *  and sending to and from other Bluetooth devices.
+ */
+
 /*
        TODO: Make SendPacket respect receiver's MTU
        TODO: Make ReceivePacket stitch together MTU fragments (?)
 /*
        TODO: Make SendPacket respect receiver's MTU
        TODO: Make ReceivePacket stitch together MTU fragments (?)
index 57b74e0..d718752 100644 (file)
   this software.
 */
 
   this software.
 */
 
+/** \file
+ *
+ *  Bluetooth HCI layer management code. This module manages the overall
+ *  Bluetooth stack connection state to and from other devices, processes
+ *  received events from the Bluetooth controller, and issues commands to
+ *  modify the controller's configuration, such as the broadcast name of the
+ *  device.
+ */
+
 /*
        TODO: Add local to remote device connections
  */
 /*
        TODO: Add local to remote device connections
  */
index d78a9ef..1a66798 100644 (file)
   this software.
 */
 
   this software.
 */
 
+/** \file
+ *
+ *  Main module for the Bluetooth stack. This module contains the overall Bluetooth
+ *  stack state variables and the main Bluetooth stack management functions.
+ */
+
 #include "BluetoothStack.h"
 
 /** Bluetooth device connection information structure. Once connected to a remote device, this structure tracks the
 #include "BluetoothStack.h"
 
 /** Bluetooth device connection information structure. Once connected to a remote device, this structure tracks the
index a441ae1..5101606 100644 (file)
   this software.\r
 */\r
 \r
   this software.\r
 */\r
 \r
+/** \file\r
+ *\r
+ *  RFCOMM layer module. This module manages the RFCOMM layer of the\r
+ *  stack, providing virtual serial port channels on top of the lower\r
+ *  L2CAP layer.\r
+ */\r
+\r
 #define  INCLUDE_FROM_RFCOMM_C\r
 #include "RFCOMM.h"\r
 \r
 #define  INCLUDE_FROM_RFCOMM_C\r
 #include "RFCOMM.h"\r
 \r
@@ -62,10 +69,10 @@ void RFCOMM_Initialize(void)
 \r
 void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)\r
 {\r
 \r
 void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)\r
 {\r
-       RFCOMM_Header_t* FrameHeader = (RFCOMM_Header_t*)Data;\r
+       const RFCOMM_Header_t* FrameHeader = (const RFCOMM_Header_t*)Data;\r
        \r
        /* Decode the RFCOMM frame type from the header */\r
        \r
        /* Decode the RFCOMM frame type from the header */\r
-       switch (FrameHeader->FrameType & ~FRAME_POLL_FINAL)\r
+       switch (FrameHeader->Control & ~FRAME_POLL_FINAL)\r
        {\r
                case RFCOMM_Frame_SABM:\r
                        RFCOMM_ProcessSABM(FrameHeader, Channel);\r
        {\r
                case RFCOMM_Frame_SABM:\r
                        RFCOMM_ProcessSABM(FrameHeader, Channel);\r
@@ -82,54 +89,94 @@ void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)
                case RFCOMM_Frame_UIH:\r
                        RFCOMM_ProcessUIH(FrameHeader, Channel);\r
                        break;\r
                case RFCOMM_Frame_UIH:\r
                        RFCOMM_ProcessUIH(FrameHeader, Channel);\r
                        break;\r
+               default:\r
+                       BT_RFCOMM_DEBUG(1, "<< Unknown Frame Type");\r
+                       break;\r
        }\r
 }\r
 \r
 static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
        }\r
 }\r
 \r
 static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
-       uint8_t* CurrBufferPos = ((uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
-       uint16_t DataLen       = RFCOMM_GetFrameDataLength(&CurrBufferPos);\r
-\r
        BT_RFCOMM_DEBUG(1, "<< SABM Received");\r
        BT_RFCOMM_DEBUG(1, "<< SABM Received");\r
-       BT_RFCOMM_DEBUG(2, "-- Data Length 0x%04X", DataLen);\r
+       BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);\r
+       \r
+       // TODO: Reset channel send/receive state here\r
+       \r
+       struct\r
+       {\r
+               RFCOMM_Header_t FrameHeader;\r
+               uint8_t         FrameLength;\r
+               uint8_t         FCS;\r
+       } ResponsePacket;\r
+       \r
+       /* Copy over the same frame header as the sent packet to copy the logical RFCOMM channel address */\r
+       ResponsePacket.FrameHeader.Address              = FrameHeader->Address;\r
+       \r
+       /* Set the frame type to an Unnumbered Acknowledgement to acknowledge the SABM request */\r
+       ResponsePacket.FrameHeader.Control = RFCOMM_Frame_UA;\r
+       \r
+       /* Set the length to 0 (LSB indicates end of 8-bit length field) */\r
+       ResponsePacket.FrameLength = 0x01;\r
+       \r
+       /* Calculate the frame checksum from all fields except the FCS field itself */\r
+       ResponsePacket.FCS = RFCOMM_GetFCSValue(&ResponsePacket, sizeof(ResponsePacket) - sizeof(ResponsePacket.FCS));\r
+       \r
+       BT_RFCOMM_DEBUG(1, ">> UA Sent");\r
 \r
 \r
-       for (uint16_t i = 0; i < DataLen; i++)\r
-         printf("0x%02X ", CurrBufferPos[i]);\r
-       printf("\r\n");\r
+       /* Send the completed response packet to the sender */\r
+       Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), Channel);\r
 }\r
 \r
 static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
 }\r
 \r
 static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
+       const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\r
        BT_RFCOMM_DEBUG(1, "<< UA Received");\r
        BT_RFCOMM_DEBUG(1, "<< UA Received");\r
+       BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);\r
 }\r
 \r
 static void RFCOMM_ProcessDM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
 }\r
 \r
 static void RFCOMM_ProcessDM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
+       const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\r
        BT_RFCOMM_DEBUG(1, "<< DM Received");\r
        BT_RFCOMM_DEBUG(1, "<< DM Received");\r
+       BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);\r
 }\r
 \r
 static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
 }\r
 \r
 static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
+       const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\r
        BT_RFCOMM_DEBUG(1, "<< DISC Received");\r
        BT_RFCOMM_DEBUG(1, "<< DISC Received");\r
+       BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);\r
 }\r
 \r
 static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
 }\r
 \r
 static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)\r
 {\r
+       const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\r
        BT_RFCOMM_DEBUG(1, "<< UIH Received");\r
        BT_RFCOMM_DEBUG(1, "<< UIH Received");\r
+       BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);\r
 }\r
 \r
 }\r
 \r
-static uint16_t RFCOMM_GetFrameDataLength(void** BufferPos)\r
+static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length)\r
 {\r
 {\r
-       uint8_t FirstOctet = *((uint8_t*)*BufferPos);\r
-       (*BufferPos)++;\r
+       const uint8_t* CurrPos = FrameStart;\r
+       uint8_t        FCS     = 0xFF;\r
+       \r
+       while (Length--)\r
+         FCS = pgm_read_byte(CRC8_Table[FCS ^ *(CurrPos++)]);\r
 \r
 \r
+       return ~FCS;\r
+}\r
+\r
+static uint16_t RFCOMM_GetFrameDataLength(const uint8_t** BufferPos)\r
+{\r
+       uint8_t FirstOctet  = *((*BufferPos)++);\r
        uint8_t SecondOctet = 0;\r
        \r
        if (!(FirstOctet & 0x01))\r
        uint8_t SecondOctet = 0;\r
        \r
        if (!(FirstOctet & 0x01))\r
-       {\r
-               SecondOctet = *((uint8_t*)*BufferPos);\r
-               (*BufferPos)++;\r
-       }\r
+         SecondOctet = *((*BufferPos)++);\r
        \r
        return (((uint16_t)SecondOctet << 7) | (FirstOctet >> 1));\r
 }\r
        \r
        return (((uint16_t)SecondOctet << 7) | (FirstOctet >> 1));\r
 }\r
index 45f29ac..194551d 100644 (file)
@@ -52,7 +52,7 @@
                #define BT_RFCOMM_DEBUG(l, s, ...)              do { if (RFCOMM_DEBUG_LEVEL >= l) printf_P(PSTR("(RFCOMM) " s "\r\n"), ##__VA_ARGS__); } while (0)\r
                #define RFCOMM_DEBUG_LEVEL                      2\r
                \r
                #define BT_RFCOMM_DEBUG(l, s, ...)              do { if (RFCOMM_DEBUG_LEVEL >= l) printf_P(PSTR("(RFCOMM) " s "\r\n"), ##__VA_ARGS__); } while (0)\r
                #define RFCOMM_DEBUG_LEVEL                      2\r
                \r
-               #define FRAME_POLL_FINAL                        (1 << 5)\r
+               #define FRAME_POLL_FINAL                        (1 << 4)\r
        \r
        /* Enums: */\r
                /** Enum for the types of RFCOMM frames which can be exchanged on a Bluetooth channel. */\r
        \r
        /* Enums: */\r
                /** Enum for the types of RFCOMM frames which can be exchanged on a Bluetooth channel. */\r
@@ -73,9 +73,9 @@
                                unsigned char LogicalChannel   : 6;\r
                                unsigned char PollResponse     : 1;\r
                                unsigned char LastAddressOctet : 1;\r
                                unsigned char LogicalChannel   : 6;\r
                                unsigned char PollResponse     : 1;\r
                                unsigned char LastAddressOctet : 1;\r
-                       } Header;\r
+                       } Address;\r
                        \r
                        \r
-                       uint8_t FrameType;\r
+                       uint8_t Control;\r
                } RFCOMM_Header_t;\r
 \r
        /* Function Prototypes: */\r
                } RFCOMM_Header_t;\r
 \r
        /* Function Prototypes: */\r
@@ -89,7 +89,8 @@
                        static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);\r
                        static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);\r
 \r
                        static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);\r
                        static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);\r
 \r
-                       static uint16_t RFCOMM_GetFrameDataLength(void** BufferPos);\r
+                       static uint8_t  RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length);\r
+                       static uint16_t RFCOMM_GetFrameDataLength(const uint8_t** BufferPos);\r
                #endif\r
                \r
 #endif\r
                #endif\r
                \r
 #endif\r
index ca47f90..16317e7 100644 (file)
   this software.\r
 */\r
 \r
   this software.\r
 */\r
 \r
+/** \file\r
+ *\r
+ *  SDP Service Attribute definitions. This file contains the attributes\r
+ *  and attribute tables of all the services the device supports, which can\r
+ *  then be retrieved by a connected Bluetooth device via the SDP server.\r
+ */\r
+\r
 #include "SDPServices.h"\r
 \r
 #include "SDPServices.h"\r
 \r
+/** Serial Port Profile attribute, listing the unique service handle of the Serial Port service\r
+ *  within the device. This handle can then be requested by the SDP client in future transactions\r
+ *  in lieu of a search UUID list.\r
+ */\r
 const struct\r
 {\r
        uint8_t  Header;\r
 const struct\r
 {\r
        uint8_t  Header;\r
@@ -40,6 +51,10 @@ const struct
                SWAPENDIAN_32(0x00010001),\r
        };\r
 \r
                SWAPENDIAN_32(0x00010001),\r
        };\r
 \r
+/** Serial Port Profile attribute, listing the implemented Service Class UUIDs of the Serial Port service\r
+ *  within the device. This list indicates all the class UUIDs that apply to the Serial Port service, so that\r
+ *  a SDP client can search by a generalized class rather than a specific UUID to determine supported services.\r
+ */\r
 const struct\r
 {\r
        uint8_t    Header;\r
 const struct\r
 {\r
        uint8_t    Header;\r
@@ -54,6 +69,10 @@ const struct
                },\r
        };\r
 \r
                },\r
        };\r
 \r
+/** Serial Port Profile attribute, listing the Protocols (and their attributes) of the Serial Port service\r
+ *  within the device. This list indicates what protocols the service is layered on top of, as well as any\r
+ *  configuration information for each layer.\r
+ */\r
 const struct\r
 {\r
        uint8_t               Header;\r
 const struct\r
 {\r
        uint8_t               Header;\r
@@ -77,11 +96,15 @@ const struct
                        (sizeof(ItemUUID_t) + sizeof(Item8Bit_t)),\r
                        {\r
                                {(SDP_DATATYPE_UUID | SDP_DATASIZE_128Bit), RFCOMM_UUID},\r
                        (sizeof(ItemUUID_t) + sizeof(Item8Bit_t)),\r
                        {\r
                                {(SDP_DATATYPE_UUID | SDP_DATASIZE_128Bit), RFCOMM_UUID},\r
-                               {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_8Bit), 0x00},\r
+                               {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_8Bit), 0x03},\r
                        },\r
                },\r
        };\r
 \r
                        },\r
                },\r
        };\r
 \r
+/** Serial Port Profile attribute, listing the Browse Group List UUIDs which this service is a member of.\r
+ *  Browse Group UUIDs give a way to group together services within a device in a simple heirachy, so that\r
+ *  a SDP client can progressively narrow down an general browse to a specific service which it requires.\r
+ */\r
 const struct\r
 {\r
        uint8_t    Header;\r
 const struct\r
 {\r
        uint8_t    Header;\r
@@ -95,7 +118,10 @@ const struct
                        {(SDP_DATATYPE_UUID | SDP_DATASIZE_128Bit), PUBLICBROWSEGROUP_CLASS_UUID},\r
                },\r
        };\r
                        {(SDP_DATATYPE_UUID | SDP_DATASIZE_128Bit), PUBLICBROWSEGROUP_CLASS_UUID},\r
                },\r
        };\r
-       \r
+\r
+/** Serial Port Profile attribute, listing the languages (and their encodings) supported\r
+ *  by the Serial Port service in its text string attributes.\r
+ */\r
 const struct\r
 {\r
        uint8_t            Header;\r
 const struct\r
 {\r
        uint8_t            Header;\r
@@ -113,7 +139,8 @@ const struct
                        },\r
                },\r
        };\r
                        },\r
                },\r
        };\r
-       \r
+\r
+/** Serial Port Profile attribute, listing a human readable name of the service. */\r
 const struct\r
 {\r
        uint8_t Header;\r
 const struct\r
 {\r
        uint8_t Header;\r
@@ -126,6 +153,7 @@ const struct
                "Wireless Serial Port",\r
        };\r
 \r
                "Wireless Serial Port",\r
        };\r
 \r
+/** Serial Port Profile attribute, listing a human readable description of the service. */\r
 const struct\r
 {\r
        uint8_t Header;\r
 const struct\r
 {\r
        uint8_t Header;\r
@@ -138,6 +166,9 @@ const struct
                "Wireless Serial Port Service",\r
        };\r
 \r
                "Wireless Serial Port Service",\r
        };\r
 \r
+/** Service Attribute Table for the Serial Port service, linking each supported attribute ID to its data, so that\r
+ *  the SDP server can retrieve it for transmission back to a SDP client upon request.\r
+ */\r
 const ServiceAttributeTable_t PROGMEM SerialPort_Attribute_Table[] =\r
        {\r
                {.AttributeID = SDP_ATTRIBUTE_ID_SERVICERECORDHANDLE,    .Data = &SerialPort_Attribute_ServiceHandle       },\r
 const ServiceAttributeTable_t PROGMEM SerialPort_Attribute_Table[] =\r
        {\r
                {.AttributeID = SDP_ATTRIBUTE_ID_SERVICERECORDHANDLE,    .Data = &SerialPort_Attribute_ServiceHandle       },\r
index c07718e..6d8f270 100644 (file)
   this software.
 */
 
   this software.
 */
 
+/** \file
+ *
+ *  SDP layer module. This module implements a simple Service Discovery
+ *  Protocol server, which can broadcast the device's supported services
+ *  to other Bluetooth devices upon request, so that they can determine
+ *  what services are available.
+ */
+
 #define  INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C
 #include "ServiceDiscoveryProtocol.h"
 
 #define  INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C
 #include "ServiceDiscoveryProtocol.h"
 
@@ -62,6 +70,7 @@ void SDP_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)
        BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU);
        BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength);
 
        BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU);
        BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength);
 
+       /* Dispatch to the correct processing routine for the given SDP packet type */
        switch (SDPHeader->PDU)
        {
                case SDP_PDU_SERVICESEARCHREQUEST:
        switch (SDPHeader->PDU)
        {
                case SDP_PDU_SERVICESEARCHREQUEST: