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
31 #define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C
32 #include "BluetoothACLPackets.h"
34 /** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth
35 * stack task to manage the ACL processing state.
37 void Bluetooth_ACLTask(void)
39 /* Process incomming ACL packets, if any */
40 Bluetooth_ProcessIncommingACLPackets();
42 /* Check for any half-open channels, send configuration details to the remote device if found */
43 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
45 Bluetooth_Channel_t
* ChannelData
= &Bluetooth_Connection
.Channels
[i
];
47 bool MustSendConfigReq
= true;
49 /* Check if we are in a channel state which requires a configuration request to be sent */
50 switch (ChannelData
->State
)
52 case Channel_Config_WaitConfig
:
53 ChannelData
->State
= Channel_Config_WaitReqResp
;
55 case Channel_Config_WaitSendConfig
:
56 ChannelData
->State
= Channel_Config_WaitResp
;
59 MustSendConfigReq
= false;
63 /* Only send a configuration request if it the channel was in a state which required it */
64 if (MustSendConfigReq
)
68 BT_Signal_Header_t SignalCommandHeader
;
69 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
73 BT_Config_Option_Header_t Header
;
78 /* Fill out the Signal Command header in the response packet */
79 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_REQUEST
;
80 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
81 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.ConfigurationRequest
) +
82 sizeof(PacketData
.Option_LocalMTU
);
84 /* Fill out the Configuration Request in the response packet, including local MTU information */
85 PacketData
.ConfigurationRequest
.DestinationChannel
= ChannelData
->RemoteNumber
;
86 PacketData
.ConfigurationRequest
.Flags
= 0;
87 PacketData
.Option_LocalMTU
.Header
.Type
= BT_CONFIG_OPTION_MTU
;
88 PacketData
.Option_LocalMTU
.Header
.Length
= sizeof(PacketData
.Option_LocalMTU
.Value
);
89 PacketData
.Option_LocalMTU
.Value
= ChannelData
->LocalMTU
;
91 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
93 BT_ACL_DEBUG(1, ">> L2CAP Configuration Request");
94 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData
.ConfigurationRequest
.DestinationChannel
);
99 /** Incomming ACL packet processing task. This task is called by the main ACL processing task to read in and process
100 * any incomming ACL packets to the device, handling signal requests as they are received or passing along channel
101 * data to the user application.
103 static void Bluetooth_ProcessIncommingACLPackets(void)
105 BT_ACL_Header_t ACLPacketHeader
;
106 BT_DataPacket_Header_t DataHeader
;
108 Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE
);
111 if (!(Pipe_IsReadWriteAllowed()))
117 /* Read in the received ACL packet headers when it has been discovered that a packet has been received */
118 Pipe_Read_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
));
119 Pipe_Read_Stream_LE(&DataHeader
, sizeof(DataHeader
));
122 BT_ACL_DEBUG(2, "Packet Received");
123 BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader
.ConnectionHandle
& 0x0FFF));
124 BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader
.DataLength
);
125 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
);
126 BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader
.PayloadLength
);
128 /* Check the packet's destination channel - signalling channel should be processed by the stack internally */
129 if (DataHeader
.DestinationChannel
== BT_CHANNEL_SIGNALING
)
131 /* Read in the Signal Command header of the incomming packet */
132 BT_Signal_Header_t SignalCommandHeader
;
133 Pipe_Read_Stream_LE(&SignalCommandHeader
, sizeof(SignalCommandHeader
));
135 /* Dispatch to the appropriate handler function based on the Signal message code */
136 switch (SignalCommandHeader
.Code
)
138 case BT_SIGNAL_CONNECTION_REQUEST
:
139 Bluetooth_Signal_ConnectionReq(&SignalCommandHeader
);
141 case BT_SIGNAL_CONNECTION_RESPONSE
:
142 Bluetooth_Signal_ConnectionResp(&SignalCommandHeader
);
144 case BT_SIGNAL_CONFIGURATION_REQUEST
:
145 Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader
);
147 case BT_SIGNAL_CONFIGURATION_RESPONSE
:
148 Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader
);
150 case BT_SIGNAL_DISCONNECTION_REQUEST
:
151 Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader
);
153 case BT_SIGNAL_DISCONNECTION_RESPONSE
:
154 Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader
);
156 case BT_SIGNAL_ECHO_REQUEST
:
157 Bluetooth_Signal_EchoReq(&SignalCommandHeader
);
159 case BT_SIGNAL_INFORMATION_REQUEST
:
160 Bluetooth_Signal_InformationReq(&SignalCommandHeader
);
162 case BT_SIGNAL_COMMAND_REJECT
:
163 BT_ACL_DEBUG(1, "<< Command Reject");
165 uint16_t RejectReason
;
166 Pipe_Read_Stream_LE(&RejectReason
, sizeof(RejectReason
));
167 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
- sizeof(RejectReason
));
171 BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason
);
174 BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader
.Code
);
176 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
);
184 /* Non-signalling packet received, read in the packet contents and pass to the user application */
185 uint8_t PacketData
[DataHeader
.PayloadLength
];
186 Pipe_Read_Stream_LE(PacketData
, DataHeader
.PayloadLength
);
190 Bluetooth_PacketReceived(PacketData
, DataHeader
.PayloadLength
, Bluetooth_GetChannelData(DataHeader
.DestinationChannel
, false));
194 /** Sends a packet to the remote device on the specified channel.
196 * \param[in] Data Pointer to a buffer where the data is to be sourced from
197 * \param[in] DataLen Length of the data to send
198 * \param[in] Channel Channel information structure containing the destination channel's information, NULL to send
199 * to the remote device's signalling channel
201 * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum
203 uint8_t Bluetooth_SendPacket(void* Data
, const uint16_t DataLen
, Bluetooth_Channel_t
* const Channel
)
205 BT_ACL_Header_t ACLPacketHeader
;
206 BT_DataPacket_Header_t DataHeader
;
208 /* A remote device must be connected before a packet transmission is attempted */
209 if (!(Bluetooth_Connection
.IsConnected
))
210 return BT_SENDPACKET_NotConnected
;
212 /* If the destination channel is not the signalling channel and it is not currently fully open, abort */
213 if ((Channel
!= NULL
) && (Channel
->State
!= Channel_Open
))
214 return BT_SENDPACKET_ChannelNotOpen
;
216 /* Fill out the packet's header from the remote device connection information structure */
217 ACLPacketHeader
.ConnectionHandle
= (Bluetooth_Connection
.ConnectionHandle
| BT_ACL_FIRST_AUTOFLUSH
);
218 ACLPacketHeader
.DataLength
= sizeof(DataHeader
) + DataLen
;
219 DataHeader
.PayloadLength
= DataLen
;
220 DataHeader
.DestinationChannel
= (Channel
== NULL
) ? BT_CHANNEL_SIGNALING
: Channel
->RemoteNumber
;
222 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE
);
225 /* Write the packet contents to the pipe so that it can be sent to the remote device */
226 Pipe_Write_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
));
227 Pipe_Write_Stream_LE(&DataHeader
, sizeof(DataHeader
));
228 Pipe_Write_Stream_LE(Data
, DataLen
);
234 BT_ACL_DEBUG(2, "Packet Sent");
235 BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader
.ConnectionHandle
& 0x0FFF));
236 BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader
.DataLength
);
237 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
);
238 BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader
.PayloadLength
);
240 return BT_SENDPACKET_NoError
;
243 /** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged.
245 * \note The channel is not immediately opened when this function returns - it must undergo a two way
246 * connection and configuration process first as the main Bluetooth stack processing task is
247 * repeatedly called. The returned channel is unusable by the user application until its State
248 * element has progressed to the Open state.
250 * \param[in] PSM PSM of the service that the channel is to be opened for
252 * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels
254 Bluetooth_Channel_t
* Bluetooth_OpenChannel(const uint16_t PSM
)
256 Bluetooth_Channel_t
* ChannelData
= NULL
;
258 /* Search through the channel information list for a free channel item */
259 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
261 if (Bluetooth_Connection
.Channels
[i
].State
== Channel_Closed
)
263 ChannelData
= &Bluetooth_Connection
.Channels
[i
];
265 /* Set the new channel structure's local channel number to a unique value within the connection orientated
266 channel address space */
267 ChannelData
->LocalNumber
= (BT_CHANNELNUMBER_BASEOFFSET
+ i
);
272 /* If no free channel item was found in the list, all channels are occupied - abort */
273 if (ChannelData
== NULL
)
276 /* Reset and fill out the allocated channel's information structure with defaults */
277 ChannelData
->RemoteNumber
= 0;
278 ChannelData
->PSM
= PSM
;
279 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
280 ChannelData
->State
= Channel_WaitConnectRsp
;
284 BT_Signal_Header_t SignalCommandHeader
;
285 BT_Signal_ConnectionReq_t ConnectionRequest
;
288 /* Fill out the Signal Command header in the response packet */
289 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_REQUEST
;
290 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
291 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.ConnectionRequest
);
293 /* Fill out the Connection Request in the response packet */
294 PacketData
.ConnectionRequest
.PSM
= PSM
;
295 PacketData
.ConnectionRequest
.SourceChannel
= ChannelData
->LocalNumber
;
297 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
299 BT_ACL_DEBUG(1, ">> L2CAP Connection Request");
300 BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData
.ConnectionRequest
.PSM
);
301 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData
.ConnectionRequest
.SourceChannel
);
306 /** Closes a bluetooth channel that is open to the currently connected remote device, so that no further data
309 * \note The channel is not immediately closed when this function returns - it must undergo an asynchronous
310 * disconnection process first as the main Bluetooth stack processing task is repeatedly called. The
311 * returned channel is unusable by the user application upon return however the channel is not completely
312 * closed until its State element has progressed to the Closed state.
314 * \param[in,out] Channel Channel information structure of the channel to close
316 void Bluetooth_CloseChannel(Bluetooth_Channel_t
* const Channel
)
318 /* Don't try to close a non-existing or already closed channel */
319 if ((Channel
== NULL
) || (Channel
->State
== Channel_Closed
))
322 /* Set the channel's state to the start of the teardown process */
323 Channel
->State
= Channel_WaitDisconnect
;
327 BT_Signal_Header_t SignalCommandHeader
;
328 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
331 /* Fill out the Signal Command header in the response packet */
332 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_REQUEST
;
333 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
334 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.DisconnectionRequest
);
336 /* Fill out the Disconnection Request in the response packet */
337 PacketData
.DisconnectionRequest
.DestinationChannel
= Channel
->RemoteNumber
;
338 PacketData
.DisconnectionRequest
.SourceChannel
= Channel
->LocalNumber
;
340 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
342 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Request");
343 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData
.DisconnectionRequest
.DestinationChannel
);
344 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData
.DisconnectionRequest
.SourceChannel
);
347 /** Internal Bluetooth stack Signal Command processing routine for a Connection Request command.
349 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
351 static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
353 BT_Signal_ConnectionReq_t ConnectionRequest
;
355 Pipe_Read_Stream_LE(&ConnectionRequest
, sizeof(ConnectionRequest
));
360 BT_ACL_DEBUG(1, "<< L2CAP Connection Request");
361 BT_ACL_DEBUG(2, "-- PSM: 0x%04X", ConnectionRequest
.PSM
);
362 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionRequest
.SourceChannel
);
364 /* Try to retrieve the existing channel's information structure if it exists */
365 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionRequest
.SourceChannel
, true);
367 /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */
368 if (ChannelData
== NULL
)
370 /* Look through the channel information list for a free entry */
371 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
373 if (Bluetooth_Connection
.Channels
[i
].State
== Channel_Closed
)
375 ChannelData
= &Bluetooth_Connection
.Channels
[i
];
377 /* Set the new channel structure's local channel number to a unique value within the connection orientated
378 channel address space */
379 ChannelData
->LocalNumber
= (BT_CHANNELNUMBER_BASEOFFSET
+ i
);
385 uint8_t ChannelStatus
= BT_CONNECTION_REFUSED_RESOURCES
;
387 /* Reset the channel item contents only if a channel entry was found for it */
388 if (ChannelData
!= NULL
)
390 /* Check if the user application will allow the connection based on its PSM */
391 if (Bluetooth_ChannelConnectionRequest(ConnectionRequest
.PSM
))
393 ChannelData
->RemoteNumber
= ConnectionRequest
.SourceChannel
;
394 ChannelData
->PSM
= ConnectionRequest
.PSM
;
395 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
396 ChannelData
->State
= Channel_Config_WaitConfig
;
398 ChannelStatus
= BT_CONNECTION_SUCCESSFUL
;
402 ChannelStatus
= BT_CONNECTION_REFUSED_PSM
;
408 BT_Signal_Header_t SignalCommandHeader
;
409 BT_Signal_ConnectionResp_t ConnectionResponse
;
412 /* Fill out the Signal Command header in the response packet */
413 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_RESPONSE
;
414 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
415 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConnectionResponse
);
417 /* Fill out the Connection Response in the response packet */
418 ResponsePacket
.ConnectionResponse
.DestinationChannel
= ChannelData
->LocalNumber
;
419 ResponsePacket
.ConnectionResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
420 ResponsePacket
.ConnectionResponse
.Result
= ChannelStatus
;
421 ResponsePacket
.ConnectionResponse
.Status
= 0x00;
423 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
425 BT_ACL_DEBUG(1, ">> L2CAP Connection Response");
426 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConnectionResponse
.Result
);
427 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.DestinationChannel
);
428 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.SourceChannel
);
431 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
433 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
435 static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
437 BT_Signal_ConnectionResp_t ConnectionResponse
;
439 Pipe_Read_Stream_LE(&ConnectionResponse
, sizeof(ConnectionResponse
));
444 BT_ACL_DEBUG(1, "<< L2CAP Connection Response");
445 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConnectionResponse
.Result
);
446 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionResponse
.SourceChannel
);
447 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConnectionResponse
.DestinationChannel
);
449 /* Search for the referenced channel in the channel information list */
450 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionResponse
.SourceChannel
, false);
452 /* Only progress if the referenced channel data was found */
453 if (ChannelData
!= NULL
)
455 /* Set the channel structure's remote channel number to the channel allocated on the remote device */
456 ChannelData
->RemoteNumber
= ConnectionResponse
.SourceChannel
;
457 ChannelData
->State
= (ConnectionResponse
.Result
== BT_CONNECTION_SUCCESSFUL
) ?
458 Channel_Config_WaitConfig
: Channel_Closed
;
462 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
464 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
466 static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
468 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
470 /* Allocate a buffer large enough to hold the variable number of configuration options in the request */
471 uint8_t OptionsLen
= (SignalCommandHeader
->Length
- sizeof(ConfigurationRequest
));
472 uint8_t Options
[OptionsLen
];
474 Pipe_Read_Stream_LE(&ConfigurationRequest
, sizeof(ConfigurationRequest
));
475 Pipe_Read_Stream_LE(&Options
, sizeof(Options
));
480 /* Search for the referenced channel in the channel information list */
481 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationRequest
.DestinationChannel
, false);
483 BT_ACL_DEBUG(1, "<< L2CAP Configuration Request");
484 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest
.DestinationChannel
);
485 BT_ACL_DEBUG(2, "-- Remote MTU: 0x%04X", ChannelData
->RemoteMTU
);
486 BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", OptionsLen
);
488 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
489 if (ChannelData
!= NULL
)
491 /* Iterate through each option in the configuration request to look for ones which can be processed */
492 uint8_t OptionPos
= 0;
493 while (OptionPos
< OptionsLen
)
495 BT_Config_Option_Header_t
* OptionHeader
= (BT_Config_Option_Header_t
*)&Options
[OptionPos
];
496 void* OptionData
= &Options
[OptionPos
+ sizeof(BT_Config_Option_Header_t
)];
498 BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader
->Type
);
499 BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(BT_Config_Option_Header_t
) + OptionHeader
->Length
));
501 /* Store the remote MTU option's value if present */
502 if (OptionHeader
->Type
== BT_CONFIG_OPTION_MTU
)
503 ChannelData
->RemoteMTU
= *((uint16_t*)OptionData
);
505 /* Progress to the next option in the packet */
506 OptionPos
+= (sizeof(BT_Config_Option_Header_t
) + OptionHeader
->Length
);
512 BT_Signal_Header_t SignalCommandHeader
;
513 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
516 /* Fill out the Signal Command header in the response packet */
517 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_RESPONSE
;
518 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
519 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConfigurationResponse
);
521 /* Fill out the Configuration Response in the response packet */
522 ResponsePacket
.ConfigurationResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
523 ResponsePacket
.ConfigurationResponse
.Flags
= 0x00;
524 ResponsePacket
.ConfigurationResponse
.Result
= (ChannelData
!= NULL
) ? BT_CONFIGURATION_SUCCESSFUL
: BT_CONFIGURATION_REJECTED
;
526 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
528 if (ChannelData
!= NULL
)
530 switch (ChannelData
->State
)
532 case Channel_Config_WaitConfig
:
533 ChannelData
->State
= Channel_Config_WaitSendConfig
;
535 case Channel_Config_WaitReqResp
:
536 ChannelData
->State
= Channel_Config_WaitResp
;
538 case Channel_Config_WaitReq
:
539 ChannelData
->State
= Channel_Open
;
544 BT_ACL_DEBUG(1, ">> L2CAP Configuration Response");
545 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConfigurationResponse
.SourceChannel
);
546 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConfigurationResponse
.Result
);
549 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
551 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
553 static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
555 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
557 Pipe_Read_Stream_LE(&ConfigurationResponse
, sizeof(ConfigurationResponse
));
562 BT_ACL_DEBUG(1, "<< L2CAP Configuration Response");
563 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse
.SourceChannel
);
564 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse
.Result
);
566 /* Search for the referenced channel in the channel information list */
567 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationResponse
.SourceChannel
, true);
569 /* Only update the channel's state if it was found in the channel list */
570 if (ChannelData
!= NULL
)
572 /* Check if the channel configuration completed successfuly */
573 if (ConfigurationResponse
.Result
== BT_CONFIGURATION_SUCCESSFUL
)
575 switch (ChannelData
->State
)
577 case Channel_Config_WaitReqResp
:
578 ChannelData
->State
= Channel_Config_WaitReq
;
580 case Channel_Config_WaitResp
:
581 ChannelData
->State
= Channel_Open
;
587 /* Configuration failed - close the channel */
588 ChannelData
->State
= Channel_Closed
;
593 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
595 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
597 static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
599 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
601 Pipe_Read_Stream_LE(&DisconnectionRequest
, sizeof(DisconnectionRequest
));
603 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Request");
604 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionRequest
.DestinationChannel
);
605 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionRequest
.SourceChannel
);
610 /* Search for the referenced channel in the channel information list */
611 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionRequest
.SourceChannel
, true);
615 BT_Signal_Header_t SignalCommandHeader
;
616 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
619 /* Fill out the Signal Command header in the response packet */
620 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_RESPONSE
;
621 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
622 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.DisconnectionResponse
);
624 /* Fill out the Disconnection Response in the response packet */
625 ResponsePacket
.DisconnectionResponse
.DestinationChannel
= ChannelData
->RemoteNumber
;
626 ResponsePacket
.DisconnectionResponse
.SourceChannel
= ChannelData
->LocalNumber
;
628 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
630 /* If the channel was found in the channel list, close it */
631 if (ChannelData
!= NULL
)
632 ChannelData
->State
= Channel_Closed
;
634 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response");
635 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.SourceChannel
);
636 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.DestinationChannel
);
639 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
641 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
643 static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
645 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
647 Pipe_Read_Stream_LE(&DisconnectionResponse
, sizeof(DisconnectionResponse
));
649 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response");
650 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse
.DestinationChannel
);
651 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse
.SourceChannel
);
656 /* Search for the referenced channel in the channel information list */
657 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionResponse
.SourceChannel
, true);
659 /* If the channel was found in the channel list, close it */
660 if (ChannelData
!= NULL
)
661 ChannelData
->State
= Channel_Closed
;
664 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
666 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
668 static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
670 BT_ACL_DEBUG(1, "<< L2CAP Echo Request");
677 BT_Signal_Header_t SignalCommandHeader
;
680 /* Fill out the Signal Command header in the response packet */
681 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_ECHO_RESPONSE
;
682 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
683 ResponsePacket
.SignalCommandHeader
.Length
= 0;
685 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
687 BT_ACL_DEBUG(1, ">> L2CAP Echo Response");
690 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
692 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
694 static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
696 BT_Signal_InformationReq_t InformationRequest
;
698 Pipe_Read_Stream_LE(&InformationRequest
, sizeof(InformationRequest
));
700 BT_ACL_DEBUG(1, "<< L2CAP Information Request");
701 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest
.InfoType
);
708 BT_Signal_Header_t SignalCommandHeader
;
709 BT_Signal_InformationResp_t InformationResponse
;
716 /* Retrieve the requested information and store it in the outgoing packet, if found */
717 switch (InformationRequest
.InfoType
)
720 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
723 *((uint16_t*)&ResponsePacket
.Data
) = MAXIMUM_CHANNEL_MTU
;
725 case BT_INFOREQ_EXTENDEDFEATURES
:
726 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
729 *((uint32_t*)&ResponsePacket
.Data
) = 0;
732 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_NOTSUPPORTED
;
737 /* Fill out the Signal Command header in the response packet */
738 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_INFORMATION_RESPONSE
;
739 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
740 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.InformationResponse
) + DataLen
;
742 /* Fill out the Information Response in the response packet */
743 ResponsePacket
.InformationResponse
.InfoType
= InformationRequest
.InfoType
;
745 Bluetooth_SendPacket(&ResponsePacket
, (sizeof(ResponsePacket
) - sizeof(ResponsePacket
.Data
) + DataLen
), NULL
);
747 BT_ACL_DEBUG(1, ">> L2CAP Information Response");
748 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.InformationResponse
.Result
);