diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c index c90fccddba..32bdb8c093 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c @@ -31,13 +31,48 @@ #define INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C #include "ServiceDiscoveryProtocol.h" +const struct +{ + uint8_t Header; + uint32_t Data; +} PROGMEM ServiceDiscoveryServer_Attribute_ServiceHandle = {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_32Bit), 0x00000000}; + +const struct +{ + uint8_t Header; + uint16_t Size; + ClassUUID_t UUIDList[]; +} PROGMEM ServiceDiscoveryServer_Attribute_ServiceClassIDs = + { + (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable16Bit), (sizeof(ClassUUID_t) * 1), + { + {.Header = (SDP_DATATYPE_UUID | SDP_DATASIZE_128Bit), .UUID = {BASE_96BIT_UUID, 0x01, 0x00, 0x00, 0x00}} + } + }; + +const struct +{ + uint8_t Header; + uint16_t Data; +} PROGMEM ServiceDiscoveryServer_Attribute_Version = {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_16Bit), 0x0100}; + +/** Service Discovery Protocol attribute table, listing all supported attributes of the service. */ +const ServiceAttributeTable_t ServiceDiscoveryServer_Attribute_Table[] PROGMEM = + { + {.AttributeID = SDP_ATTRIBUTE_ID_SERVICERECORDHANDLE, .Data = &ServiceDiscoveryServer_Attribute_ServiceHandle }, + {.AttributeID = SDP_ATTRIBUTE_ID_SERVICECLASSIDS, .Data = &ServiceDiscoveryServer_Attribute_ServiceClassIDs }, + {.AttributeID = SDP_ATTRIBUTE_ID_VERSION, .Data = &ServiceDiscoveryServer_Attribute_Version }, + + SERVICE_ATTRIBUTE_TABLE_TERMINATOR + }; + /** Service Discovery Protocol attribute, indicationg the service's name. */ const struct { uint8_t Header; uint8_t Length; uint8_t Data[]; -} PROGMEM SDP_Attribute_Name = {(SDP_DATATYPE_String | 5), sizeof("SDP"), "SDP"}; +} PROGMEM SDP_Attribute_Name = {(SDP_DATATYPE_String | SDP_DATASIZE_Variable8Bit), sizeof("SDP"), "SDP"}; /** Service Discovery Protocol attribute, indicationg the service's description. */ const struct @@ -45,37 +80,37 @@ const struct uint8_t Header; uint8_t Length; uint8_t Data[]; -} PROGMEM SDP_Attribute_Description = {(SDP_DATATYPE_String | 5), sizeof("BT Service Discovery"), "BT Service Discovery"}; +} PROGMEM SDP_Attribute_Description = {(SDP_DATATYPE_String | SDP_DATASIZE_Variable8Bit), sizeof("BT Service Discovery"), "BT Service Discovery"}; /** Service Discovery Protocol attribute, indicationg the service's availability. */ const struct { uint8_t Header; uint8_t Data; -} PROGMEM SDP_Attribute_Availability = {(SDP_DATATYPE_UnsignedInt | 0), 0xFF}; +} PROGMEM SDP_Attribute_Availability = {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_8Bit), 0xFF}; const struct { uint8_t Header; uint16_t Data; -} PROGMEM SDP_Attribute_LanguageOffset = {(SDP_DATATYPE_UnsignedInt | 1), SDP_ATTRIBUTE_LANGOFFSET}; +} PROGMEM SDP_Attribute_LanguageOffset = {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_16Bit), SDP_ATTRIBUTE_LANGOFFSET}; const struct { uint8_t Header; - uint16_t Data; -} PROGMEM SDP_Attribute_ServiceHandle = {(SDP_DATATYPE_UnsignedInt | 1), 0x0001}; + uint32_t Data; +} PROGMEM SDP_Attribute_ServiceHandle = {(SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_32Bit), 0x00000001}; const struct { uint8_t Header; - uint8_t Size; + uint16_t Size; ClassUUID_t UUIDList[]; } PROGMEM SDP_Attribute_ServiceClassIDs = { - (SDP_DATATYPE_Sequence | 5), (sizeof(ClassUUID_t) * 1), + (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable16Bit), (sizeof(ClassUUID_t) * 1), { - {.Header = (SDP_DATATYPE_UUID | 5), .Size = UUID_SIZE_BYTES, .UUID = {BASE_96BIT_UUID, 0x01, 0x00, 0x00, 0x00}} + {.Header = (SDP_DATATYPE_UUID | SDP_DATASIZE_128Bit), .UUID = {BASE_96BIT_UUID, 0x00, 0x00, 0x00, 0x01}} } }; @@ -96,8 +131,12 @@ const ServiceAttributeTable_t SDP_Attribute_Table[] PROGMEM = */ const ServiceTable_t SDP_Services_Table[] PROGMEM = { - { // 128-bit UUID for the SDP service + { // 128-bit UUID for the Service Discovery Server Service .UUID = {BASE_96BIT_UUID, 0x01, 0x00, 0x00, 0x00}, + .AttributeTable = ServiceDiscoveryServer_Attribute_Table, + }, + { // 128-bit UUID for the SDP service + .UUID = {BASE_96BIT_UUID, 0x00, 0x00, 0x00, 0x01}, .AttributeTable = SDP_Attribute_Table, }, #if 0 @@ -196,7 +235,7 @@ static void SDP_ProcessServiceSearchAttribute(const SDP_PDUHeader_t* const SDPHe } ResponsePacket; /* Create a pointer to the buffer to indicate the current location for response data to be added */ - uint8_t* CurrResponsePos = ResponsePacket.ResponseData; + void* CurrResponsePos = ResponsePacket.ResponseData; /* Clamp the maximum attribute size to the size of the allocated buffer */ if (MaxAttributeSize > sizeof(ResponsePacket.ResponseData)) @@ -215,6 +254,8 @@ static void SDP_ProcessServiceSearchAttribute(const SDP_PDUHeader_t* const SDPHe if (AttributeTable == NULL) continue; + BT_SDP_DEBUG(2, " -- Found UUID %d in table", CurrUUIDItem); + /* Add an inner Data Element Sequence header for the current UUID's found Attributes */ uint16_t* CurrentUUIDResponseSize = SDP_AddDataElementHeader16(&CurrResponsePos, SDP_DATATYPE_Sequence); @@ -232,25 +273,11 @@ static void SDP_ProcessServiceSearchAttribute(const SDP_PDUHeader_t* const SDPHe /* If the Attribute does not exist in the current UUID's Attribute table, continue to the next Attribute ID */ if (AttributeValue == NULL) continue; - - /* Retrieve the size of the attribute value from its container header */ - uint32_t AttributeValueLength = SDP_GetLocalAttributeContainerSize(AttributeValue); + BT_SDP_DEBUG(2, " -- Add Attribute 0x%04X", CurrAttributeID); - /* Add a Data Element header to the response for the Attribute ID */ - *((uint8_t*)CurrResponsePos) = (1 | SDP_DATATYPE_UnsignedInt); - CurrResponsePos += sizeof(uint8_t); - - /* Add the Attribute ID to the created Data Element */ - *((uint16_t*)CurrResponsePos) = CurrAttributeID; - CurrResponsePos += sizeof(uint16_t); - - /* Copy over the Attribute value Data Element container to the response */ - memcpy_P(CurrResponsePos, AttributeValue, AttributeValueLength); - CurrResponsePos += AttributeValueLength; - /* Increment the current UUID's returned Attribute container size by the number of added bytes */ - *CurrentUUIDResponseSize += sizeof(uint8_t) + sizeof(uint16_t) + AttributeValueLength; + *CurrentUUIDResponseSize += SDP_AddAttributeToResponse(CurrAttributeID, AttributeValue, &CurrResponsePos); } /* Increment the outer container size by the number of added bytes */ @@ -274,6 +301,34 @@ static void SDP_ProcessServiceSearchAttribute(const SDP_PDUHeader_t* const SDPHe Channel); } +/** Adds the given attribute ID and value to the reponse buffer, and advances the response buffer pointer past the added data. + * + * \param[in] AttributeID Attribute ID to add to the response buffer + * \param[in] AttributeValue Pointer to the start of the Attribute's value, located in PROGMEM + * \param[in, out] ResponseBuffer Pointer to a buffer where the Attribute and Attribute Value is to be added + * + * \return Number of bytes added to the response buffer + */ +static uint16_t SDP_AddAttributeToResponse(const uint16_t AttributeID, const void* AttributeValue, void** ResponseBuffer) +{ + /* Retrieve the size of the attribute value from its container header */ + uint32_t AttributeValueLength = SDP_GetLocalAttributeContainerSize(AttributeValue); + + /* Add a Data Element header to the response for the Attribute ID */ + *((uint8_t*)*ResponseBuffer) = (SDP_DATATYPE_UnsignedInt | SDP_DATASIZE_16Bit); + *ResponseBuffer += sizeof(uint8_t); + + /* Add the Attribute ID to the created Data Element */ + *((uint16_t*)*ResponseBuffer) = AttributeID; + *ResponseBuffer += sizeof(uint16_t); + + /* Copy over the Attribute value Data Element container to the response */ + memcpy_P(*ResponseBuffer, AttributeValue, AttributeValueLength); + *ResponseBuffer += AttributeValueLength; + + return (sizeof(uint8_t) + sizeof(uint16_t) + AttributeValueLength); +} + /** Retrieves a pointer to the value of the given Attribute ID from the given Attribute table. * * \param[in] AttributeTable Pointer to the Attribute table to search in @@ -412,11 +467,11 @@ static uint32_t SDP_GetLocalAttributeContainerSize(const void* const AttributeDa /* Convert the Data Element size index into a size in bytes */ switch (SizeIndex) { - case 5: - return (1 + sizeof(uint8_t)) + pgm_read_byte(AttributeData + 1); - case 6: + case SDP_DATASIZE_Variable8Bit: + return (1 + sizeof(uint8_t)) + pgm_read_byte(AttributeData + 1); + case SDP_DATASIZE_Variable16Bit: return (1 + sizeof(uint16_t)) + pgm_read_word(AttributeData + 1); - case 7: + case SDP_DATASIZE_Variable32Bit: return (1 + sizeof(uint32_t)) + pgm_read_dword(AttributeData + 1); default: return (1 + (1 << SizeIndex)); @@ -444,17 +499,17 @@ static uint32_t SDP_GetDataElementSize(const void** const DataElementHeader, uin /* Convert the Data Element size index into a size in bytes */ switch (SizeIndex) { - case 5: + case SDP_DATASIZE_Variable8Bit: ElementValue = *((uint8_t*)*DataElementHeader); *DataElementHeader += sizeof(uint8_t); *ElementHeaderSize = (1 + sizeof(uint8_t)); break; - case 6: + case SDP_DATASIZE_Variable16Bit: ElementValue = *((uint16_t*)*DataElementHeader); *DataElementHeader += sizeof(uint16_t); *ElementHeaderSize = (1 + sizeof(uint16_t)); break; - case 7: + case SDP_DATASIZE_Variable32Bit: ElementValue = *((uint32_t*)*DataElementHeader); *DataElementHeader += sizeof(uint32_t); *ElementHeaderSize = (1 + sizeof(uint32_t)); diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h index c65a34170d..bf79c531e0 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h @@ -58,6 +58,7 @@ #define SDP_ATTRIBUTE_ID_SERVICECLASSIDS 0x0001 #define SDP_ATTRIBUTE_ID_LANGIDOFFSET 0x0006 #define SDP_ATTRIBUTE_ID_AVAILABILITY 0x0008 + #define SDP_ATTRIBUTE_ID_VERSION 0x0200 #define SDP_ATTRIBUTE_ID_NAME (0x0000 + SDP_ATTRIBUTE_LANGOFFSET) #define SDP_ATTRIBUTE_ID_DESCRIPTION (0x0001 + SDP_ATTRIBUTE_LANGOFFSET) @@ -74,44 +75,71 @@ #define SERVICE_ATTRIBUTE_TABLE_TERMINATOR {.Data = NULL} /* Enums: */ + /** Data sizes for SDP Data Element headers, to indicate the size of the data contained in the element. When creating + * a Data Element, a value from this enum should be ORed with a value from the \ref ServiceDiscovery_DataTypes_t enum. + */ + enum ServiceDiscovery_DataSizes_t + { + SDP_DATASIZE_8Bit = 0, /**< Contained data is 8 bits in length. */ + SDP_DATASIZE_16Bit = 1, /**< Contained data is 16 bits in length. */ + SDP_DATASIZE_32Bit = 2, /**< Contained data is 32 bits in length. */ + SDP_DATASIZE_64Bit = 3, /**< Contained data is 64 bits in length. */ + SDP_DATASIZE_128Bit = 4, /**< Contained data is 128 bits in length. */ + SDP_DATASIZE_Variable8Bit = 5, /**< Contained data is encoded in an 8 bit size integer following the header. */ + SDP_DATASIZE_Variable16Bit = 6, /**< Contained data is encoded in an 16 bit size integer following the header. */ + SDP_DATASIZE_Variable32Bit = 7, /**< Contained data is encoded in an 32 bit size integer following the header. */ + }; + + /** Data types for SDP Data Element headers, to indicate the type of data contained in the element. When creating + * a Data Element, a value from this enum should be ORed with a value from the \ref ServiceDiscovery_DataSizes_t enum. + */ enum ServiceDiscovery_DataTypes_t { - SDP_DATATYPE_Nill = (0 << 3), - SDP_DATATYPE_UnsignedInt = (1 << 3), - SDP_DATATYPE_SignedInt = (2 << 3), - SDP_DATATYPE_UUID = (3 << 3), - SDP_DATATYPE_String = (4 << 3), - SDP_DATATYPE_Boolean = (5 << 3), - SDP_DATATYPE_Sequence = (6 << 3), - SDP_DATATYPE_Alternative = (7 << 3), - SDP_DATATYPE_URL = (8 << 3), + SDP_DATATYPE_Nill = (0 << 3), /**< Indicates the container data is a Nill (null) type. */ + SDP_DATATYPE_UnsignedInt = (1 << 3), /**< Indicates the container data is an unsigned integer. */ + SDP_DATATYPE_SignedInt = (2 << 3), /**< Indicates the container data is a signed integer. */ + SDP_DATATYPE_UUID = (3 << 3), /**< Indicates the container data is a UUID. */ + SDP_DATATYPE_String = (4 << 3), /**< Indicates the container data is an ASCII string. */ + SDP_DATATYPE_Boolean = (5 << 3), /**< Indicates the container data is a logical boolean. */ + SDP_DATATYPE_Sequence = (6 << 3), /**< Indicates the container data is a sequence of containers. */ + SDP_DATATYPE_Alternative = (7 << 3), /**< Indicates the container data is a sequence of alternative containers. */ + SDP_DATATYPE_URL = (8 << 3), /**< Indicates the container data is a URL. */ }; /* Type Defines: */ + /** Header for all SPD transaction packets. This header is sent at the start of all SDP packets sent to or from a SDP + * server. + */ typedef struct { - uint8_t PDU; - uint16_t TransactionID; - uint16_t ParameterLength; + uint8_t PDU; /**< SDP packet type, a SDP_PDU_* mask value */ + uint16_t TransactionID; /**< Unique transaction ID number to associate requests and responses */ + uint16_t ParameterLength; /**< Length of the data following the SDP header */ } SDP_PDUHeader_t; + /** Structure for the association of attribute ID values to an attribute value in FLASH. A table of these + * structures can then be built up for each supported UUID service within the device. + */ typedef struct { - uint16_t AttributeID; - const void* Data; + uint16_t AttributeID; /**< Attribute ID of the table element which the UUID service supports */ + const void* Data; /**< Pointer to the attribute data, located in PROGMEM memory space */ } ServiceAttributeTable_t; + /** Structure for the association of service UUID values to attribute tables stored in FLASH. A table of these + * structures can then be built up for each supported UUID service within the device. + */ typedef struct { - uint8_t UUID[16]; - const void* AttributeTable; + uint8_t UUID[UUID_SIZE_BYTES]; /**< UUID of a service supported by the device */ + const void* AttributeTable; /**< Pointer to the UUID's attribute table, located in PROGMEM memory space */ } ServiceTable_t; + /** Structure for a list of Data Elements containing UUIDs, for service attributes requiring UUID lists. */ typedef struct { - uint8_t Header; - uint8_t Size; - uint16_t UUID[UUID_SIZE_BYTES]; + uint8_t Header; /**< Data Element header, should be (SDP_DATATYPE_Sequence | SDP_DATASIZE_Variable16Bit) */ + uint16_t UUID[UUID_SIZE_BYTES]; /**< UUID to store in the list Data Element */ } ClassUUID_t; /* Inline Functions: */ @@ -128,9 +156,9 @@ * * \return Pointer to the 16-bit size value of the contaner header, which has been pre-zeroed */ - static inline uint16_t* SDP_AddDataElementHeader16(uint8_t** BufferPos, const uint8_t Type) + static inline uint16_t* SDP_AddDataElementHeader16(void** BufferPos, const uint8_t Type) { - **BufferPos = (6 | Type); + *((uint8_t*)*BufferPos) = (SDP_DATASIZE_Variable16Bit | Type); uint16_t* SizePos = (uint16_t*)*(BufferPos + 1); *SizePos = 0; @@ -147,10 +175,11 @@ static void SDP_ProcessServiceAttribute(const SDP_PDUHeader_t* const SDPHeader, Bluetooth_Channel_t* const Channel); static void SDP_ProcessServiceSearchAttribute(const SDP_PDUHeader_t* const SDPHeader, Bluetooth_Channel_t* const Channel); - static void* SDP_GetAttributeValue(const ServiceAttributeTable_t* AttributeTable, const uint16_t AttributeID); + static uint16_t SDP_AddAttributeToResponse(const uint16_t AttributeID, const void* AttributeValue, void** ResponseBuffer); + static void* SDP_GetAttributeValue(const ServiceAttributeTable_t* AttributeTable, const uint16_t AttributeID); static ServiceAttributeTable_t* SDP_GetAttributeTable(const uint8_t* const UUID); - static uint8_t SDP_GetAttributeList(uint16_t AttributeList[][2], const void** const CurrentParameter); - static uint8_t SDP_GetUUIDList(uint8_t UUIDList[][UUID_SIZE_BYTES], const void** const CurrentParameter); + static uint8_t SDP_GetAttributeList(uint16_t AttributeList[][2], const void** const CurrentParameter); + static uint8_t SDP_GetUUIDList(uint8_t UUIDList[][UUID_SIZE_BYTES], const void** const CurrentParameter); static uint32_t SDP_GetLocalAttributeContainerSize(const void* const AttributeData); static uint32_t SDP_GetDataElementSize(const void** const AttributeHeader, uint8_t* const ElementHeaderSize); #endif