Fix byte ordering of UUIDs in the SDP server - host can now successfully pair to...
authorDean Camera <dean@fourwalledcubicle.com>
Wed, 2 Jun 2010 14:49:06 +0000 (14:49 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Wed, 2 Jun 2010 14:49:06 +0000 (14:49 +0000)
Demos/Host/Incomplete/BluetoothHost/Lib/SDPServices.c
Demos/Host/Incomplete/BluetoothHost/Lib/SDPServices.h
Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c
Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h
LUFA/Common/Common.h

index 511e0b0..908be0c 100644 (file)
@@ -34,7 +34,7 @@ const struct
 {\r
        uint8_t  Header;\r
        uint32_t Data;\r
-} PROGMEM RFCOMM_Attribute_ServiceHandle =\r
+} PROGMEM SerialPort_Attribute_ServiceHandle =\r
        {\r
                (SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_32Bit),\r
                SWAPENDIAN_32(0x00010001),\r
@@ -45,7 +45,7 @@ const struct
        uint8_t    Header;\r
        uint16_t   Size;\r
        ItemUUID_t UUIDList[];\r
-} PROGMEM RFCOMM_Attribute_ServiceClassIDs =\r
+} PROGMEM SerialPort_Attribute_ServiceClassIDs =\r
        {\r
                (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable16Bit),\r
                SWAPENDIAN_16(sizeof(ItemUUID_t) * 1),\r
@@ -56,11 +56,11 @@ const struct
 \r
 const struct\r
 {\r
-       uint8_t        Header;\r
-       uint16_t       Size;\r
+       uint8_t  Header;\r
+       uint16_t Size;\r
 \r
        ItemProtocol_t ProtocolList[];\r
-} PROGMEM RFCOMM_Attribute_ProtocolDescriptor =\r
+} PROGMEM SerialPort_Attribute_ProtocolDescriptor =\r
        {\r
                (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable16Bit),\r
                SWAPENDIAN_16(sizeof(ItemProtocol_t) * 2),\r
@@ -87,7 +87,7 @@ const struct
        uint8_t    Header;\r
        uint16_t   Size;\r
        ItemUUID_t UUIDList[];\r
-} PROGMEM RFCOMM_Attribute_BrowseGroupList =\r
+} PROGMEM SerialPort_Attribute_BrowseGroupList =\r
        {\r
                (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable16Bit),\r
                SWAPENDIAN_16(sizeof(ItemUUID_t) * 1),\r
@@ -98,14 +98,14 @@ const struct
        \r
 const struct\r
 {\r
-       uint8_t      Header;\r
-       uint8_t      Size;\r
-       ItemLangID_t OffsetList[];\r
-} PROGMEM RFCOMM_Attribute_LanguageBaseIDOffset =\r
+       uint8_t            Header;\r
+       uint8_t            Size;\r
+       ItemLangEncoding_t LanguageEncodings[];\r
+} PROGMEM SerialPort_Attribute_LanguageBaseIDOffset =\r
        {\r
                .Header = (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable8Bit),\r
-               .Size   = (sizeof(ItemLangID_t) * 1),\r
-               .OffsetList =\r
+               .Size   = (sizeof(ItemLangEncoding_t) * 1),\r
+               .LanguageEncodings =\r
                        {\r
                                {\r
                                        {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_16Bit), SWAPENDIAN_16(0x454E)},\r
@@ -120,11 +120,11 @@ const struct
        uint8_t Header;\r
        uint8_t Size;\r
        char    Text[];\r
-} PROGMEM RFCOMM_Attribute_ServiceName =\r
+} PROGMEM SerialPort_Attribute_ServiceName =\r
        {\r
                (SDP_DATATYPE_String | SDP_DATASIZE_Variable8Bit),\r
-               sizeof("Serial Port") - 1,\r
-               "Serial Port",\r
+               sizeof("Wireless Serial Port") - 1,\r
+               "Wireless Serial Port",\r
        };\r
 \r
 const struct\r
@@ -132,22 +132,22 @@ const struct
        uint8_t Header;\r
        uint8_t Size;\r
        char    Text[];\r
-} PROGMEM RFCOMM_Attribute_ServiceDescription =\r
+} PROGMEM SerialPort_Attribute_ServiceDescription =\r
        {\r
                (SDP_DATATYPE_String | SDP_DATASIZE_Variable8Bit),\r
                sizeof("Wireless Serial Port Service") - 1,\r
                "Wireless Serial Port Service",\r
        };\r
 \r
-const ServiceAttributeTable_t PROGMEM RFCOMM_Attribute_Table[] =\r
+const ServiceAttributeTable_t PROGMEM SerialPort_Attribute_Table[] =\r
        {\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICERECORDHANDLE,    .Data = &RFCOMM_Attribute_ServiceHandle       },\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICECLASSIDS,        .Data = &RFCOMM_Attribute_ServiceClassIDs     },\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_PROTOCOLDESCRIPTORLIST, .Data = &RFCOMM_Attribute_ProtocolDescriptor  },\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_BROWSEGROUPLIST,        .Data = &RFCOMM_Attribute_BrowseGroupList     },\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_LANGUAGEBASEATTROFFSET, .Data = &RFCOMM_Attribute_LanguageBaseIDOffset},\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICENAME,            .Data = &RFCOMM_Attribute_ServiceName         },\r
-               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICEDESCRIPTION,     .Data = &RFCOMM_Attribute_ServiceDescription  },\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICERECORDHANDLE,    .Data = &SerialPort_Attribute_ServiceHandle       },\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICECLASSIDS,        .Data = &SerialPort_Attribute_ServiceClassIDs     },\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_PROTOCOLDESCRIPTORLIST, .Data = &SerialPort_Attribute_ProtocolDescriptor  },\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_BROWSEGROUPLIST,        .Data = &SerialPort_Attribute_BrowseGroupList     },\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_LANGUAGEBASEATTROFFSET, .Data = &SerialPort_Attribute_LanguageBaseIDOffset},\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICENAME,            .Data = &SerialPort_Attribute_ServiceName         },\r
+               {.AttributeID = SDP_ATTRIBUTE_ID_SERVICEDESCRIPTION,     .Data = &SerialPort_Attribute_ServiceDescription  },\r
 \r
                SERVICE_ATTRIBUTE_TABLE_TERMINATOR\r
        };\r
index 654ed94..738cc06 100644 (file)
                #define UUID_SIZE_BYTES                         16\r
                \r
                /** First 80 bits common to all standardized Bluetooth services. */\r
-               #define BASE_80BIT_UUID                         SWAPENDIAN_32(0xFB349B5F), SWAPENDIAN_16(0x8000), SWAPENDIAN_16(0x0080), SWAPENDIAN_16(0x0010)\r
+               #define BASE_80BIT_UUID                         0x0000, 0x0010, 0x0080, {0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}\r
                \r
-               #define SDP_UUID                                {BASE_80BIT_UUID, {0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}\r
-               #define RFCOMM_UUID                             {BASE_80BIT_UUID, {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}}\r
-               #define L2CAP_UUID                              {BASE_80BIT_UUID, {0x00, 0x00, 0x00, 0x00, 0x01, 0x00}}\r
-               #define SDP_CLASS_UUID                          {BASE_80BIT_UUID, {0x00, 0x00, 0x00, 0x00, 0x10, 0x00}}\r
-               #define SP_CLASS_UUID                           {BASE_80BIT_UUID, {0x00, 0x00, 0x00, 0x00, 0x11, 0x01}}\r
-               #define PUBLICBROWSEGROUP_CLASS_UUID            {BASE_80BIT_UUID, {0x00, 0x00, 0x00, 0x00, 0x10, 0x02}}\r
+               #define RFCOMM_UUID                             {SWAPENDIAN_32(0x00000003), BASE_80BIT_UUID}\r
+               #define L2CAP_UUID                              {SWAPENDIAN_32(0x00000100), BASE_80BIT_UUID}\r
+               #define SP_CLASS_UUID                           {SWAPENDIAN_32(0x00001101), BASE_80BIT_UUID}\r
+               #define PUBLICBROWSEGROUP_CLASS_UUID            {SWAPENDIAN_32(0x00001002), BASE_80BIT_UUID}\r
                \r
                #define SDP_ATTRIBUTE_ID_SERVICERECORDHANDLE    0x0000\r
                #define SDP_ATTRIBUTE_ID_SERVICECLASSIDS        0x0001\r
                #define SDP_ATTRIBUTE_ID_PROTOCOLDESCRIPTORLIST 0x0004\r
                #define SDP_ATTRIBUTE_ID_BROWSEGROUPLIST        0x0005\r
                #define SDP_ATTRIBUTE_ID_LANGUAGEBASEATTROFFSET 0x0006\r
-               #define SDP_ATTRIBUTE_ID_VERSION                0x0200\r
                #define SDP_ATTRIBUTE_ID_SERVICENAME            0x0100\r
                #define SDP_ATTRIBUTE_ID_SERVICEDESCRIPTION     0x0101\r
                \r
+               /** Swaps the byte ordering of a 16-bit value at compile time. Do not use this macro for swapping byte orderings\r
+                *  of dynamic values computed at runtime -- use SwapEndian_16() instead.\r
+                *\r
+                *  \param[in]  x  16-bit value whose byte ordering is to be swapped\r
+                *\r
+                *  \return Input value with the byte ordering reversed\r
+                */\r
                #define SWAPENDIAN_16(x)                        ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))\r
-               #define SWAPENDIAN_32(x)                        ((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | \\r
-                                                                (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24))\r
+\r
+               /** Swaps the byte ordering of a 32-bit value at compile time. Do not use this macro for swapping byte orderings\r
+                *  of dynamic values computed at runtime -- use SwapEndian_32() instead.\r
+                *\r
+                *  \param[in]  x  32-bit value whose byte ordering is to be swapped\r
+                *\r
+                *  \return Input value with the byte ordering reversed\r
+                */\r
+               #define SWAPENDIAN_32(x)                        ((((x) & 0xFF000000UL) >> 24UL) | (((x) & 0x00FF0000UL) >> 8UL) | \\r
+                                                                (((x) & 0x0000FF00UL) << 8UL) | (((x) & 0x000000FFUL) << 24UL))\r
                \r
                /** Terminator for a service attribute table of type \ref ServiceAttributeTable_t. */\r
                #define SERVICE_ATTRIBUTE_TABLE_TERMINATOR      {.Data = NULL}\r
                \r
        /* Type Defines: */\r
+               /** Type define for a UUID value structure. This struct can be used to hold full 128-bit UUIDs. */\r
                typedef struct\r
                {\r
-                       uint32_t A;\r
-                       uint16_t B;\r
-                       uint16_t C;\r
-                       uint16_t D;\r
-                       uint8_t  E[6];\r
+                       uint32_t A; /**< Bits 0-31 of the UUID. */\r
+                       uint16_t B; /**< Bits 32-47 of the UUID. */\r
+                       uint16_t C; /**< Bits 48-63 of the UUID. */\r
+                       uint16_t D; /**< Bits 64-79 of the UUID. */\r
+                       uint8_t  E[6]; /**< Bits 80-128 of the UUID. */\r
                } UUID_t;\r
        \r
                /** Structure for the association of attribute ID values to an attribute value in FLASH. A table of these\r
                 *  structures can then be built up for each supported UUID service within the device.\r
+                *\r
+                *  \note This table must be terminated with a \ref SERVICE_ATTRIBUTE_TABLE_TERMINATOR element.\r
                 */\r
                typedef struct\r
                {\r
                        } Protocol;\r
                } ItemProtocol_t;\r
                \r
+               /** Structure for a list of Data Elements containing language encodings, including the language ID, Encoding ID and\r
+                *  Attribute base offset.\r
+                */\r
                typedef struct\r
                {\r
-                       Item16Bit_t LanguageID;\r
-                       Item16Bit_t EncodingID;\r
-                       Item16Bit_t OffsetID;\r
-               } ItemLangID_t;\r
+                       Item16Bit_t LanguageID; /**< Language ID for the current language */\r
+                       Item16Bit_t EncodingID; /**< Encoding used for the current language */\r
+                       Item16Bit_t OffsetID; /**< Attribute offset added to all strings using this language within the service */\r
+               } ItemLangEncoding_t;\r
                \r
        /* External Variables: */\r
-               extern const ServiceAttributeTable_t RFCOMM_Attribute_Table[];\r
+               extern const ServiceAttributeTable_t SerialPort_Attribute_Table[];\r
                \r
 #endif\r
index ffe2be0..df0db3b 100644 (file)
 /** Service attribute table list, containing a pointer to each service attribute table the device contains */
 const ServiceAttributeTable_t* SDP_Services_Table[] PROGMEM =
        {
-               RFCOMM_Attribute_Table,
+               SerialPort_Attribute_Table,
        };
 
 /** Base UUID value common to all standardized Bluetooth services */
-const UUID_t BaseUUID PROGMEM = {BASE_80BIT_UUID, {0, 0, 0, 0, 0, 0}};
+const UUID_t BaseUUID PROGMEM = {0x00000000, BASE_80BIT_UUID};
 
 /** Main Service Discovery Protocol packet processing routine. This function processes incomming SDP packets from
  *  a connected Bluetooth device, and sends back appropriate responses to allow other devices to determine the
@@ -487,6 +487,16 @@ static void SDP_CheckUUIDMatch(uint8_t UUIDList[][UUID_SIZE_BYTES], const uint8_
                /* Look for matches in the UUID list against the current attribute UUID value */
                for (uint8_t i = 0; i < TotalUUIDs; i++)
                {
+                       uint8_t CurrentUUID[16];
+                       memcpy_P(CurrentUUID, (CurrAttribute + 1), 16);
+               
+                       BT_SDP_DEBUG(2, "-- TEST UUID: %02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
+                               CurrentUUID[0], CurrentUUID[1], CurrentUUID[2], CurrentUUID[3],
+                               CurrentUUID[4], CurrentUUID[5],
+                                               CurrentUUID[6], CurrentUUID[7],
+                               CurrentUUID[8], CurrentUUID[9],
+                                               CurrentUUID[10], CurrentUUID[11], CurrentUUID[12],  CurrentUUID[13],  CurrentUUID[14],  CurrentUUID[15]);
+
                        if (!(UUIDMatch[i]) && !(memcmp_P(UUIDList[i], (CurrAttribute + 1), UUID_SIZE_BYTES)))
                        {
                                /* Indicate match found for the current attribute UUID and early-abort */
@@ -580,19 +590,28 @@ static uint8_t SDP_GetUUIDList(uint8_t UUIDList[][UUID_SIZE_BYTES], const void**
                uint8_t* CurrentUUID = UUIDList[TotalUUIDs++];
                uint8_t  UUIDLength  = SDP_GetDataElementSize(CurrentParameter, &ElementHeaderSize);
                
-               /* Copy over the base UUID value to the free UUID slot in the list */
-               memcpy_P(CurrentUUID, &BaseUUID, sizeof(BaseUUID));
-
                /* Copy over UUID from the container to the free slot */
-               memcpy(&CurrentUUID[UUID_SIZE_BYTES - UUIDLength], *CurrentParameter, UUIDLength);
+               if (UUIDLength <= 4)
+               {
+                       /* Copy over the base UUID value to the free UUID slot in the list */
+                       memcpy_P(CurrentUUID, &BaseUUID, sizeof(BaseUUID));
+
+                       /* Copy over short UUID */
+                       memcpy(CurrentUUID + (4 - UUIDLength), *CurrentParameter, UUIDLength);
+               }
+               else
+               {
+                       /* Copy over full UUID */
+                       memcpy(CurrentUUID, *CurrentParameter, UUIDLength);             
+               }
                
                BT_SDP_DEBUG(2, "-- UUID (%d): %02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
                                UUIDLength,
                                CurrentUUID[0], CurrentUUID[1], CurrentUUID[2], CurrentUUID[3],
                                CurrentUUID[4], CurrentUUID[5],
-                                               CurrentUUID[6],  CurrentUUID[7],
-                               CurrentUUID[8],  CurrentUUID[9],
-                                               CurrentUUID[10],  CurrentUUID[11], CurrentUUID[12],  CurrentUUID[13],  CurrentUUID[14],  CurrentUUID[15]);
+                                               CurrentUUID[6], CurrentUUID[7],
+                               CurrentUUID[8], CurrentUUID[9],
+                                               CurrentUUID[10], CurrentUUID[11], CurrentUUID[12],  CurrentUUID[13],  CurrentUUID[14],  CurrentUUID[15]);
 
                ServicePatternLength -= (UUIDLength + ElementHeaderSize);
                *CurrentParameter    += UUIDLength;
index 50f4366..e46729c 100644 (file)
@@ -50,7 +50,7 @@
                
        /* Macros: */
                #define BT_SDP_DEBUG(l, s, ...)                 do { if (SDP_DEBUG_LEVEL >= l) printf_P(PSTR("(SDP) " s "\r\n"), ##__VA_ARGS__); } while (0)
-               #define SDP_DEBUG_LEVEL                         1
+               #define SDP_DEBUG_LEVEL                         0
                
                #define SDP_PDU_ERRORRESPONSE                   0x01
                #define SDP_PDU_SERVICESEARCHREQUEST            0x02
index 14473cd..76307d7 100644 (file)
                                                                                                                                "Assertion \"%s\" failed.\r\n"),     \
                                                                                                                                __FILE__, __func__, __LINE__, #x); } \
                                                        }MACROE
-                                                                                       
+
                /* Inline Functions: */
                        /** Function to reverse the individual bits in a byte - i.e. bit 7 is moved to bit 0, bit 6 to bit 1,
                         *  etc.