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 (?)
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
*/
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
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
\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
- 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
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
- 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(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
- 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
+ const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\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
+ const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\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
+ const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\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
+ const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));\r
+\r
BT_RFCOMM_DEBUG(1, "<< UIH Received");\r
+ BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);\r
}\r
\r
-static uint16_t RFCOMM_GetFrameDataLength(void** BufferPos)\r
+static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length)\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
+ 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
- {\r
- SecondOctet = *((uint8_t*)*BufferPos);\r
- (*BufferPos)++;\r
- }\r
+ SecondOctet = *((*BufferPos)++);\r
\r
return (((uint16_t)SecondOctet << 7) | (FirstOctet >> 1));\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
unsigned char LogicalChannel : 6;\r
unsigned char PollResponse : 1;\r
unsigned char LastAddressOctet : 1;\r
- } Header;\r
+ } Address;\r
\r
- uint8_t FrameType;\r
+ uint8_t Control;\r
} RFCOMM_Header_t;\r
\r
/* Function Prototypes: */\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
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
+/** 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
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
},\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
(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
+/** 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
{(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
},\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
"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
"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
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"
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: