+ \r
+ BT_ACL_DEBUG(2, "");\r
+ BT_ACL_DEBUG(2, "Packet Sent");\r
+ BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader.ConnectionHandle & 0x0FFF));\r
+ BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader.DataLength);\r
+ BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader.DestinationChannel);\r
+ BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader.PayloadLength);\r
+\r
+ return BT_SENDPACKET_NoError;\r
+}\r
+\r
+/** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged.\r
+ *\r
+ * \note The channel is not immediately opened when this function returns - it must undergo a two way\r
+ * connection and configuration process first as the main Bluetooth stack processing task is\r
+ * repeatedly called. The returned channel is unusable by the user application until its State\r
+ * element has progressed to the Open state.\r
+ *\r
+ * \param[in] PSM PSM of the service that the channel is to be opened for\r
+ *\r
+ * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels\r
+ */\r
+Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM)\r
+{\r
+ Bluetooth_Channel_t* ChannelData = NULL;\r
+\r
+ /* Search through the channel information list for a free channel item */\r
+ for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)\r
+ {\r
+ if (Bluetooth_Connection.Channels[i].State == Channel_Closed)\r
+ {\r
+ ChannelData = &Bluetooth_Connection.Channels[i];\r
+ \r
+ /* Set the new channel structure's local channel number to a unique value within the connection orientated\r
+ channel address space */\r
+ ChannelData->LocalNumber = (BT_CHANNELNUMBER_BASEOFFSET + i);\r
+ break;\r
+ }\r
+ }\r
+\r
+ /* If no free channel item was found in the list, all channels are occupied - abort */\r
+ if (ChannelData == NULL)\r
+ return NULL;\r
+\r
+ /* Reset and fill out the allocated channel's information structure with defaults */\r
+ ChannelData->RemoteNumber = 0;\r
+ ChannelData->PSM = PSM;\r
+ ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;\r
+ ChannelData->State = Channel_WaitConnectRsp;\r
+ \r
+ struct\r
+ {\r
+ BT_Signal_Header_t SignalCommandHeader;\r
+ BT_Signal_ConnectionReq_t ConnectionRequest;\r
+ } PacketData;\r
+\r
+ /* Fill out the Signal Command header in the response packet */\r
+ PacketData.SignalCommandHeader.Code = BT_SIGNAL_CONNECTION_REQUEST;\r
+ PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier;\r
+ PacketData.SignalCommandHeader.Length = sizeof(PacketData.ConnectionRequest);\r
+ \r
+ /* Fill out the Connection Request in the response packet */\r
+ PacketData.ConnectionRequest.PSM = PSM;\r
+ PacketData.ConnectionRequest.SourceChannel = ChannelData->LocalNumber;\r
+ \r
+ Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);\r
+\r
+ BT_ACL_DEBUG(1, ">> L2CAP Connection Request");\r
+ BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData.ConnectionRequest.PSM);\r
+ BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.ConnectionRequest.SourceChannel);\r
+\r
+ return ChannelData;\r