3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Bluetooth L2CAP layer management code. This module managed the creation,
34 * configuration and teardown of L2CAP channels, and manages packet reception
35 * and sending to and from other Bluetooth devices.
39 TODO: Make SendPacket respect receiver's MTU
40 TODO: Make ReceivePacket stitch together MTU fragments (?)
41 TODO: Add channel opened/closed callbacks
44 #define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C
45 #include "BluetoothACLPackets.h"
47 /** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth
48 * stack task to manage the ACL processing state.
50 void Bluetooth_ACLTask(void)
52 /* Process incomming ACL packets, if any */
53 Bluetooth_ProcessIncommingACLPackets();
55 /* Check for any half-open channels, send configuration details to the remote device if found */
56 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
58 Bluetooth_Channel_t
* ChannelData
= &Bluetooth_Connection
.Channels
[i
];
60 bool MustSendConfigReq
= true;
62 /* Check if we are in a channel state which requires a configuration request to be sent */
63 switch (ChannelData
->State
)
65 case Channel_Config_WaitConfig
:
66 ChannelData
->State
= Channel_Config_WaitReqResp
;
68 case Channel_Config_WaitSendConfig
:
69 ChannelData
->State
= Channel_Config_WaitResp
;
72 MustSendConfigReq
= false;
76 /* Only send a configuration request if it the channel was in a state which required it */
77 if (MustSendConfigReq
)
81 BT_Signal_Header_t SignalCommandHeader
;
82 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
86 BT_Config_Option_Header_t Header
;
91 /* Fill out the Signal Command header in the response packet */
92 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_REQUEST
;
93 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
94 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.ConfigurationRequest
) +
95 sizeof(PacketData
.Option_LocalMTU
);
97 /* Fill out the Configuration Request in the response packet, including local MTU information */
98 PacketData
.ConfigurationRequest
.DestinationChannel
= ChannelData
->RemoteNumber
;
99 PacketData
.ConfigurationRequest
.Flags
= 0;
100 PacketData
.Option_LocalMTU
.Header
.Type
= BT_CONFIG_OPTION_MTU
;
101 PacketData
.Option_LocalMTU
.Header
.Length
= sizeof(PacketData
.Option_LocalMTU
.Value
);
102 PacketData
.Option_LocalMTU
.Value
= ChannelData
->LocalMTU
;
104 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
106 BT_ACL_DEBUG(1, ">> L2CAP Configuration Request");
107 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData
.ConfigurationRequest
.DestinationChannel
);
112 /** Incomming ACL packet processing task. This task is called by the main ACL processing task to read in and process
113 * any incomming ACL packets to the device, handling signal requests as they are received or passing along channel
114 * data to the user application.
116 static void Bluetooth_ProcessIncommingACLPackets(void)
118 BT_ACL_Header_t ACLPacketHeader
;
119 BT_DataPacket_Header_t DataHeader
;
121 Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE
);
124 if (!(Pipe_IsReadWriteAllowed()))
130 /* Read in the received ACL packet headers when it has been discovered that a packet has been received */
131 Pipe_Read_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
));
132 Pipe_Read_Stream_LE(&DataHeader
, sizeof(DataHeader
));
135 BT_ACL_DEBUG(2, "Packet Received");
136 BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader
.ConnectionHandle
& 0x0FFF));
137 BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader
.DataLength
);
138 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
);
139 BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader
.PayloadLength
);
141 /* Check the packet's destination channel - signalling channel should be processed by the stack internally */
142 if (DataHeader
.DestinationChannel
== BT_CHANNEL_SIGNALING
)
144 /* Read in the Signal Command header of the incomming packet */
145 BT_Signal_Header_t SignalCommandHeader
;
146 Pipe_Read_Stream_LE(&SignalCommandHeader
, sizeof(SignalCommandHeader
));
148 /* Dispatch to the appropriate handler function based on the Signal message code */
149 switch (SignalCommandHeader
.Code
)
151 case BT_SIGNAL_CONNECTION_REQUEST
:
152 Bluetooth_Signal_ConnectionReq(&SignalCommandHeader
);
154 case BT_SIGNAL_CONNECTION_RESPONSE
:
155 Bluetooth_Signal_ConnectionResp(&SignalCommandHeader
);
157 case BT_SIGNAL_CONFIGURATION_REQUEST
:
158 Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader
);
160 case BT_SIGNAL_CONFIGURATION_RESPONSE
:
161 Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader
);
163 case BT_SIGNAL_DISCONNECTION_REQUEST
:
164 Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader
);
166 case BT_SIGNAL_DISCONNECTION_RESPONSE
:
167 Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader
);
169 case BT_SIGNAL_ECHO_REQUEST
:
170 Bluetooth_Signal_EchoReq(&SignalCommandHeader
);
172 case BT_SIGNAL_INFORMATION_REQUEST
:
173 Bluetooth_Signal_InformationReq(&SignalCommandHeader
);
175 case BT_SIGNAL_COMMAND_REJECT
:
176 BT_ACL_DEBUG(1, "<< Command Reject");
178 uint16_t RejectReason
;
179 Pipe_Read_Stream_LE(&RejectReason
, sizeof(RejectReason
));
180 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
- sizeof(RejectReason
));
184 BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason
);
187 BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader
.Code
);
189 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
);
197 /* Non-signalling packet received, read in the packet contents and pass to the user application */
198 uint8_t PacketData
[DataHeader
.PayloadLength
];
199 Pipe_Read_Stream_LE(PacketData
, DataHeader
.PayloadLength
);
203 Bluetooth_PacketReceived(PacketData
, DataHeader
.PayloadLength
,
204 Bluetooth_GetChannelData(DataHeader
.DestinationChannel
, CHANNEL_SEARCH_LOCALNUMBER
));
208 /** Sends a packet to the remote device on the specified channel.
210 * \param[in] Data Pointer to a buffer where the data is to be sourced from
211 * \param[in] DataLen Length of the data to send
212 * \param[in] Channel Channel information structure containing the destination channel's information, NULL to send
213 * to the remote device's signalling channel
215 * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum
217 uint8_t Bluetooth_SendPacket(void* Data
, const uint16_t DataLen
, Bluetooth_Channel_t
* const Channel
)
219 BT_ACL_Header_t ACLPacketHeader
;
220 BT_DataPacket_Header_t DataHeader
;
222 /* A remote device must be connected before a packet transmission is attempted */
223 if (!(Bluetooth_Connection
.IsConnected
))
224 return BT_SENDPACKET_NotConnected
;
226 /* If the destination channel is not the signalling channel and it is not currently fully open, abort */
227 if ((Channel
!= NULL
) && (Channel
->State
!= Channel_Open
))
228 return BT_SENDPACKET_ChannelNotOpen
;
230 /* Fill out the packet's header from the remote device connection information structure */
231 ACLPacketHeader
.ConnectionHandle
= (Bluetooth_Connection
.ConnectionHandle
| BT_ACL_FIRST_AUTOFLUSH
);
232 ACLPacketHeader
.DataLength
= sizeof(DataHeader
) + DataLen
;
233 DataHeader
.PayloadLength
= DataLen
;
234 DataHeader
.DestinationChannel
= (Channel
== NULL
) ? BT_CHANNEL_SIGNALING
: Channel
->RemoteNumber
;
236 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE
);
238 Pipe_WaitUntilReady();
241 /* Write the packet contents to the pipe so that it can be sent to the remote device */
242 Pipe_Write_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
));
243 Pipe_Write_Stream_LE(&DataHeader
, sizeof(DataHeader
));
244 Pipe_Write_Stream_LE(Data
, DataLen
);
250 BT_ACL_DEBUG(2, "Packet Sent");
251 BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader
.ConnectionHandle
& 0x0FFF));
252 BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader
.DataLength
);
253 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
);
254 BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader
.PayloadLength
);
256 return BT_SENDPACKET_NoError
;
259 /** Opens a Bluetooth channel to the currently connected remote device, so that data can be exchanged.
261 * \note The channel is not immediately opened when this function returns - it must undergo a two way
262 * connection and configuration process first as the main Bluetooth stack processing task is
263 * repeatedly called. The returned channel is unusable by the user application until its State
264 * element has progressed to the Open state.
266 * \param[in] PSM PSM of the service that the channel is to be opened for
268 * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels
270 Bluetooth_Channel_t
* Bluetooth_OpenChannel(const uint16_t PSM
)
272 Bluetooth_Channel_t
* ChannelData
= NULL
;
274 /* Search through the channel information list for a free channel item */
275 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
277 if (Bluetooth_Connection
.Channels
[i
].State
== Channel_Closed
)
279 ChannelData
= &Bluetooth_Connection
.Channels
[i
];
281 /* Set the new channel structure's local channel number to a unique value within the connection orientated
282 channel address space */
283 ChannelData
->LocalNumber
= (BT_CHANNELNUMBER_BASEOFFSET
+ i
);
288 /* If no free channel item was found in the list, all channels are occupied - abort */
289 if (ChannelData
== NULL
)
292 /* Reset and fill out the allocated channel's information structure with defaults */
293 ChannelData
->RemoteNumber
= 0;
294 ChannelData
->PSM
= PSM
;
295 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
296 ChannelData
->State
= Channel_WaitConnectRsp
;
300 BT_Signal_Header_t SignalCommandHeader
;
301 BT_Signal_ConnectionReq_t ConnectionRequest
;
304 /* Fill out the Signal Command header in the response packet */
305 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_REQUEST
;
306 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
307 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.ConnectionRequest
);
309 /* Fill out the Connection Request in the response packet */
310 PacketData
.ConnectionRequest
.PSM
= PSM
;
311 PacketData
.ConnectionRequest
.SourceChannel
= ChannelData
->LocalNumber
;
313 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
315 BT_ACL_DEBUG(1, ">> L2CAP Connection Request");
316 BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData
.ConnectionRequest
.PSM
);
317 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData
.ConnectionRequest
.SourceChannel
);
322 /** Closes a Bluetooth channel that is open to the currently connected remote device, so that no further data
325 * \note The channel is not immediately closed when this function returns - it must undergo an asynchronous
326 * disconnection process first as the main Bluetooth stack processing task is repeatedly called. The
327 * returned channel is unusable by the user application upon return however the channel is not completely
328 * closed until its State element has progressed to the Closed state.
330 * \param[in,out] Channel Channel information structure of the channel to close
332 void Bluetooth_CloseChannel(Bluetooth_Channel_t
* const Channel
)
334 /* Don't try to close a non-existing or already closed channel */
335 if ((Channel
== NULL
) || (Channel
->State
== Channel_Closed
))
338 /* Set the channel's state to the start of the teardown process */
339 Channel
->State
= Channel_WaitDisconnect
;
343 BT_Signal_Header_t SignalCommandHeader
;
344 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
347 /* Fill out the Signal Command header in the response packet */
348 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_REQUEST
;
349 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
350 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.DisconnectionRequest
);
352 /* Fill out the Disconnection Request in the response packet */
353 PacketData
.DisconnectionRequest
.DestinationChannel
= Channel
->RemoteNumber
;
354 PacketData
.DisconnectionRequest
.SourceChannel
= Channel
->LocalNumber
;
356 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
358 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Request");
359 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData
.DisconnectionRequest
.DestinationChannel
);
360 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData
.DisconnectionRequest
.SourceChannel
);
363 /** Internal Bluetooth stack Signal Command processing routine for a Connection Request command.
365 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
367 static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
369 BT_Signal_ConnectionReq_t ConnectionRequest
;
371 Pipe_Read_Stream_LE(&ConnectionRequest
, sizeof(ConnectionRequest
));
376 BT_ACL_DEBUG(1, "<< L2CAP Connection Request");
377 BT_ACL_DEBUG(2, "-- PSM: 0x%04X", ConnectionRequest
.PSM
);
378 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionRequest
.SourceChannel
);
380 /* Try to retrieve the existing channel's information structure if it exists */
381 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionRequest
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
383 /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */
384 if (ChannelData
== NULL
)
386 /* Look through the channel information list for a free entry */
387 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
389 if (Bluetooth_Connection
.Channels
[i
].State
== Channel_Closed
)
391 ChannelData
= &Bluetooth_Connection
.Channels
[i
];
393 /* Set the new channel structure's local channel number to a unique value within the connection orientated
394 channel address space */
395 ChannelData
->LocalNumber
= (BT_CHANNELNUMBER_BASEOFFSET
+ i
);
401 uint8_t ChannelStatus
= BT_CONNECTION_REFUSED_RESOURCES
;
403 /* Reset the channel item contents only if a channel entry was found for it */
404 if (ChannelData
!= NULL
)
406 /* Check if the user application will allow the connection based on its PSM */
407 if (Bluetooth_ChannelConnectionRequest(ConnectionRequest
.PSM
))
409 ChannelData
->RemoteNumber
= ConnectionRequest
.SourceChannel
;
410 ChannelData
->PSM
= ConnectionRequest
.PSM
;
411 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
412 ChannelData
->State
= Channel_Config_WaitConfig
;
414 ChannelStatus
= BT_CONNECTION_SUCCESSFUL
;
418 ChannelStatus
= BT_CONNECTION_REFUSED_PSM
;
424 BT_Signal_Header_t SignalCommandHeader
;
425 BT_Signal_ConnectionResp_t ConnectionResponse
;
428 /* Fill out the Signal Command header in the response packet */
429 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_RESPONSE
;
430 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
431 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConnectionResponse
);
433 /* Fill out the Connection Response in the response packet */
434 ResponsePacket
.ConnectionResponse
.DestinationChannel
= ChannelData
->LocalNumber
;
435 ResponsePacket
.ConnectionResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
436 ResponsePacket
.ConnectionResponse
.Result
= ChannelStatus
;
437 ResponsePacket
.ConnectionResponse
.Status
= 0x00;
439 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
441 BT_ACL_DEBUG(1, ">> L2CAP Connection Response");
442 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConnectionResponse
.Result
);
443 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.DestinationChannel
);
444 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.SourceChannel
);
447 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
449 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
451 static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
453 BT_Signal_ConnectionResp_t ConnectionResponse
;
455 Pipe_Read_Stream_LE(&ConnectionResponse
, sizeof(ConnectionResponse
));
460 BT_ACL_DEBUG(1, "<< L2CAP Connection Response");
461 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConnectionResponse
.Result
);
462 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionResponse
.SourceChannel
);
463 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConnectionResponse
.DestinationChannel
);
465 /* Search for the referenced channel in the channel information list */
466 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionResponse
.SourceChannel
, CHANNEL_SEARCH_LOCALNUMBER
);
468 /* Only progress if the referenced channel data was found */
469 if (ChannelData
!= NULL
)
471 /* Set the channel structure's remote channel number to the channel allocated on the remote device */
472 ChannelData
->RemoteNumber
= ConnectionResponse
.SourceChannel
;
473 ChannelData
->State
= (ConnectionResponse
.Result
== BT_CONNECTION_SUCCESSFUL
) ?
474 Channel_Config_WaitConfig
: Channel_Closed
;
478 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
480 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
482 static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
484 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
486 /* Allocate a buffer large enough to hold the variable number of configuration options in the request */
487 uint8_t OptionsLen
= (SignalCommandHeader
->Length
- sizeof(ConfigurationRequest
));
488 uint8_t Options
[OptionsLen
];
490 Pipe_Read_Stream_LE(&ConfigurationRequest
, sizeof(ConfigurationRequest
));
491 Pipe_Read_Stream_LE(&Options
, sizeof(Options
));
496 /* Search for the referenced channel in the channel information list */
497 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationRequest
.DestinationChannel
, CHANNEL_SEARCH_LOCALNUMBER
);
499 BT_ACL_DEBUG(1, "<< L2CAP Configuration Request");
500 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest
.DestinationChannel
);
501 BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", OptionsLen
);
503 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
504 if (ChannelData
!= NULL
)
506 /* Iterate through each option in the configuration request to look for ones which can be processed */
507 uint8_t OptionPos
= 0;
508 while (OptionPos
< OptionsLen
)
510 BT_Config_Option_Header_t
* OptionHeader
= (BT_Config_Option_Header_t
*)&Options
[OptionPos
];
511 void* OptionData
= &Options
[OptionPos
+ sizeof(BT_Config_Option_Header_t
)];
513 BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader
->Type
);
514 BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(BT_Config_Option_Header_t
) + OptionHeader
->Length
));
516 /* Store the remote MTU option's value if present */
517 if (OptionHeader
->Type
== BT_CONFIG_OPTION_MTU
)
518 ChannelData
->RemoteMTU
= *((uint16_t*)OptionData
);
520 /* Progress to the next option in the packet */
521 OptionPos
+= (sizeof(BT_Config_Option_Header_t
) + OptionHeader
->Length
);
527 BT_Signal_Header_t SignalCommandHeader
;
528 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
531 /* Fill out the Signal Command header in the response packet */
532 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_RESPONSE
;
533 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
534 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConfigurationResponse
);
536 /* Fill out the Configuration Response in the response packet */
537 ResponsePacket
.ConfigurationResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
538 ResponsePacket
.ConfigurationResponse
.Flags
= 0x00;
539 ResponsePacket
.ConfigurationResponse
.Result
= (ChannelData
!= NULL
) ? BT_CONFIGURATION_SUCCESSFUL
: BT_CONFIGURATION_REJECTED
;
541 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
543 if (ChannelData
!= NULL
)
545 switch (ChannelData
->State
)
547 case Channel_Config_WaitConfig
:
548 ChannelData
->State
= Channel_Config_WaitSendConfig
;
550 case Channel_Config_WaitReqResp
:
551 ChannelData
->State
= Channel_Config_WaitResp
;
553 case Channel_Config_WaitReq
:
554 ChannelData
->State
= Channel_Open
;
559 BT_ACL_DEBUG(1, ">> L2CAP Configuration Response");
560 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConfigurationResponse
.SourceChannel
);
561 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConfigurationResponse
.Result
);
564 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
566 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
568 static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
570 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
572 Pipe_Read_Stream_LE(&ConfigurationResponse
, sizeof(ConfigurationResponse
));
577 BT_ACL_DEBUG(1, "<< L2CAP Configuration Response");
578 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse
.SourceChannel
);
579 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse
.Result
);
581 /* Search for the referenced channel in the channel information list */
582 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationResponse
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
584 /* Only update the channel's state if it was found in the channel list */
585 if (ChannelData
!= NULL
)
587 /* Check if the channel configuration completed successfuly */
588 if (ConfigurationResponse
.Result
== BT_CONFIGURATION_SUCCESSFUL
)
590 switch (ChannelData
->State
)
592 case Channel_Config_WaitReqResp
:
593 ChannelData
->State
= Channel_Config_WaitReq
;
595 case Channel_Config_WaitResp
:
596 ChannelData
->State
= Channel_Open
;
602 /* Configuration failed - close the channel */
603 ChannelData
->State
= Channel_Closed
;
608 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
610 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
612 static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
614 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
616 Pipe_Read_Stream_LE(&DisconnectionRequest
, sizeof(DisconnectionRequest
));
618 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Request");
619 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionRequest
.DestinationChannel
);
620 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionRequest
.SourceChannel
);
625 /* Search for the referenced channel in the channel information list */
626 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionRequest
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
630 BT_Signal_Header_t SignalCommandHeader
;
631 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
634 /* Fill out the Signal Command header in the response packet */
635 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_RESPONSE
;
636 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
637 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.DisconnectionResponse
);
639 /* Fill out the Disconnection Response in the response packet */
640 ResponsePacket
.DisconnectionResponse
.DestinationChannel
= ChannelData
->RemoteNumber
;
641 ResponsePacket
.DisconnectionResponse
.SourceChannel
= ChannelData
->LocalNumber
;
643 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
645 /* If the channel was found in the channel list, close it */
646 if (ChannelData
!= NULL
)
647 ChannelData
->State
= Channel_Closed
;
649 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response");
650 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.SourceChannel
);
651 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.DestinationChannel
);
654 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
656 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
658 static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
660 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
662 Pipe_Read_Stream_LE(&DisconnectionResponse
, sizeof(DisconnectionResponse
));
664 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response");
665 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse
.DestinationChannel
);
666 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse
.SourceChannel
);
671 /* Search for the referenced channel in the channel information list */
672 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionResponse
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
674 /* If the channel was found in the channel list, close it */
675 if (ChannelData
!= NULL
)
676 ChannelData
->State
= Channel_Closed
;
679 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
681 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
683 static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
685 BT_ACL_DEBUG(1, "<< L2CAP Echo Request");
692 BT_Signal_Header_t SignalCommandHeader
;
695 /* Fill out the Signal Command header in the response packet */
696 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_ECHO_RESPONSE
;
697 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
698 ResponsePacket
.SignalCommandHeader
.Length
= 0;
700 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
702 BT_ACL_DEBUG(1, ">> L2CAP Echo Response");
705 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
707 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
709 static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
711 BT_Signal_InformationReq_t InformationRequest
;
713 Pipe_Read_Stream_LE(&InformationRequest
, sizeof(InformationRequest
));
715 BT_ACL_DEBUG(1, "<< L2CAP Information Request");
716 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest
.InfoType
);
723 BT_Signal_Header_t SignalCommandHeader
;
724 BT_Signal_InformationResp_t InformationResponse
;
731 /* Retrieve the requested information and store it in the outgoing packet, if found */
732 switch (InformationRequest
.InfoType
)
735 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
738 *((uint16_t*)&ResponsePacket
.Data
) = MAXIMUM_CHANNEL_MTU
;
740 case BT_INFOREQ_EXTENDEDFEATURES
:
741 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
744 *((uint32_t*)&ResponsePacket
.Data
) = 0;
747 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_NOTSUPPORTED
;
752 /* Fill out the Signal Command header in the response packet */
753 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_INFORMATION_RESPONSE
;
754 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
755 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.InformationResponse
) + DataLen
;
757 /* Fill out the Information Response in the response packet */
758 ResponsePacket
.InformationResponse
.InfoType
= InformationRequest
.InfoType
;
760 Bluetooth_SendPacket(&ResponsePacket
, (sizeof(ResponsePacket
) - sizeof(ResponsePacket
.Data
) + DataLen
), NULL
);
762 BT_ACL_DEBUG(1, ">> L2CAP Information Response");
763 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.InformationResponse
.Result
);