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
32 TODO: Make SendPacket respect receiver's MTU
33 TODO: Make ReceivePacket stitch together MTU fragments (?)
34 TODO: Add channel opened/closed callbacks
35 TODO: Figure out why delay is needed in connection for services
38 #define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C
39 #include "BluetoothACLPackets.h"
41 /** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth
42 * stack task to manage the ACL processing state.
44 void Bluetooth_ACLTask(void)
46 /* Process incomming ACL packets, if any */
47 Bluetooth_ProcessIncommingACLPackets();
49 /* Check for any half-open channels, send configuration details to the remote device if found */
50 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
52 Bluetooth_Channel_t
* ChannelData
= &Bluetooth_Connection
.Channels
[i
];
54 bool MustSendConfigReq
= true;
56 /* Check if we are in a channel state which requires a configuration request to be sent */
57 switch (ChannelData
->State
)
59 case Channel_Config_WaitConfig
:
60 ChannelData
->State
= Channel_Config_WaitReqResp
;
62 case Channel_Config_WaitSendConfig
:
63 ChannelData
->State
= Channel_Config_WaitResp
;
66 MustSendConfigReq
= false;
70 /* Only send a configuration request if it the channel was in a state which required it */
71 if (MustSendConfigReq
)
75 BT_Signal_Header_t SignalCommandHeader
;
76 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
80 BT_Config_Option_Header_t Header
;
85 /* Fill out the Signal Command header in the response packet */
86 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_REQUEST
;
87 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
88 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.ConfigurationRequest
) +
89 sizeof(PacketData
.Option_LocalMTU
);
91 /* Fill out the Configuration Request in the response packet, including local MTU information */
92 PacketData
.ConfigurationRequest
.DestinationChannel
= ChannelData
->RemoteNumber
;
93 PacketData
.ConfigurationRequest
.Flags
= 0;
94 PacketData
.Option_LocalMTU
.Header
.Type
= BT_CONFIG_OPTION_MTU
;
95 PacketData
.Option_LocalMTU
.Header
.Length
= sizeof(PacketData
.Option_LocalMTU
.Value
);
96 PacketData
.Option_LocalMTU
.Value
= ChannelData
->LocalMTU
;
98 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
100 BT_ACL_DEBUG(1, ">> L2CAP Configuration Request");
101 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData
.ConfigurationRequest
.DestinationChannel
);
106 /** Incomming ACL packet processing task. This task is called by the main ACL processing task to read in and process
107 * any incomming ACL packets to the device, handling signal requests as they are received or passing along channel
108 * data to the user application.
110 static void Bluetooth_ProcessIncommingACLPackets(void)
112 BT_ACL_Header_t ACLPacketHeader
;
113 BT_DataPacket_Header_t DataHeader
;
115 Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE
);
118 if (!(Pipe_IsReadWriteAllowed()))
124 /* Read in the received ACL packet headers when it has been discovered that a packet has been received */
125 Pipe_Read_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
));
126 Pipe_Read_Stream_LE(&DataHeader
, sizeof(DataHeader
));
129 BT_ACL_DEBUG(2, "Packet Received");
130 BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader
.ConnectionHandle
& 0x0FFF));
131 BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader
.DataLength
);
132 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
);
133 BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader
.PayloadLength
);
135 /* Check the packet's destination channel - signalling channel should be processed by the stack internally */
136 if (DataHeader
.DestinationChannel
== BT_CHANNEL_SIGNALING
)
138 /* Read in the Signal Command header of the incomming packet */
139 BT_Signal_Header_t SignalCommandHeader
;
140 Pipe_Read_Stream_LE(&SignalCommandHeader
, sizeof(SignalCommandHeader
));
142 /* Dispatch to the appropriate handler function based on the Signal message code */
143 switch (SignalCommandHeader
.Code
)
145 case BT_SIGNAL_CONNECTION_REQUEST
:
146 Bluetooth_Signal_ConnectionReq(&SignalCommandHeader
);
148 case BT_SIGNAL_CONNECTION_RESPONSE
:
149 Bluetooth_Signal_ConnectionResp(&SignalCommandHeader
);
151 case BT_SIGNAL_CONFIGURATION_REQUEST
:
152 Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader
);
154 case BT_SIGNAL_CONFIGURATION_RESPONSE
:
155 Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader
);
157 case BT_SIGNAL_DISCONNECTION_REQUEST
:
158 Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader
);
160 case BT_SIGNAL_DISCONNECTION_RESPONSE
:
161 Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader
);
163 case BT_SIGNAL_ECHO_REQUEST
:
164 Bluetooth_Signal_EchoReq(&SignalCommandHeader
);
166 case BT_SIGNAL_INFORMATION_REQUEST
:
167 Bluetooth_Signal_InformationReq(&SignalCommandHeader
);
169 case BT_SIGNAL_COMMAND_REJECT
:
170 BT_ACL_DEBUG(1, "<< Command Reject");
172 uint16_t RejectReason
;
173 Pipe_Read_Stream_LE(&RejectReason
, sizeof(RejectReason
));
174 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
- sizeof(RejectReason
));
178 BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason
);
181 BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader
.Code
);
183 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
);
191 /* Non-signalling packet received, read in the packet contents and pass to the user application */
192 uint8_t PacketData
[DataHeader
.PayloadLength
];
193 Pipe_Read_Stream_LE(PacketData
, DataHeader
.PayloadLength
);
197 Bluetooth_PacketReceived(PacketData
, DataHeader
.PayloadLength
,
198 Bluetooth_GetChannelData(DataHeader
.DestinationChannel
, CHANNEL_SEARCH_LOCALNUMBER
));
202 /** Sends a packet to the remote device on the specified channel.
204 * \param[in] Data Pointer to a buffer where the data is to be sourced from
205 * \param[in] DataLen Length of the data to send
206 * \param[in] Channel Channel information structure containing the destination channel's information, NULL to send
207 * to the remote device's signalling channel
209 * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum
211 uint8_t Bluetooth_SendPacket(void* Data
, const uint16_t DataLen
, Bluetooth_Channel_t
* const Channel
)
213 BT_ACL_Header_t ACLPacketHeader
;
214 BT_DataPacket_Header_t DataHeader
;
216 /* A remote device must be connected before a packet transmission is attempted */
217 if (!(Bluetooth_Connection
.IsConnected
))
218 return BT_SENDPACKET_NotConnected
;
220 /* If the destination channel is not the signalling channel and it is not currently fully open, abort */
221 if ((Channel
!= NULL
) && (Channel
->State
!= Channel_Open
))
222 return BT_SENDPACKET_ChannelNotOpen
;
224 /* Fill out the packet's header from the remote device connection information structure */
225 ACLPacketHeader
.ConnectionHandle
= (Bluetooth_Connection
.ConnectionHandle
| BT_ACL_FIRST_AUTOFLUSH
);
226 ACLPacketHeader
.DataLength
= sizeof(DataHeader
) + DataLen
;
227 DataHeader
.PayloadLength
= DataLen
;
228 DataHeader
.DestinationChannel
= (Channel
== NULL
) ? BT_CHANNEL_SIGNALING
: Channel
->RemoteNumber
;
230 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE
);
233 /* Write the packet contents to the pipe so that it can be sent to the remote device */
234 Pipe_Write_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
));
235 Pipe_Write_Stream_LE(&DataHeader
, sizeof(DataHeader
));
236 Pipe_Write_Stream_LE(Data
, DataLen
);
242 BT_ACL_DEBUG(2, "Packet Sent");
243 BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader
.ConnectionHandle
& 0x0FFF));
244 BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader
.DataLength
);
245 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
);
246 BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader
.PayloadLength
);
248 return BT_SENDPACKET_NoError
;
251 /** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged.
253 * \note The channel is not immediately opened when this function returns - it must undergo a two way
254 * connection and configuration process first as the main Bluetooth stack processing task is
255 * repeatedly called. The returned channel is unusable by the user application until its State
256 * element has progressed to the Open state.
258 * \param[in] PSM PSM of the service that the channel is to be opened for
260 * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels
262 Bluetooth_Channel_t
* Bluetooth_OpenChannel(const uint16_t PSM
)
264 Bluetooth_Channel_t
* ChannelData
= NULL
;
266 /* Search through the channel information list for a free channel item */
267 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
269 if (Bluetooth_Connection
.Channels
[i
].State
== Channel_Closed
)
271 ChannelData
= &Bluetooth_Connection
.Channels
[i
];
273 /* Set the new channel structure's local channel number to a unique value within the connection orientated
274 channel address space */
275 ChannelData
->LocalNumber
= (BT_CHANNELNUMBER_BASEOFFSET
+ i
);
280 /* If no free channel item was found in the list, all channels are occupied - abort */
281 if (ChannelData
== NULL
)
284 /* Reset and fill out the allocated channel's information structure with defaults */
285 ChannelData
->RemoteNumber
= 0;
286 ChannelData
->PSM
= PSM
;
287 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
288 ChannelData
->State
= Channel_WaitConnectRsp
;
292 BT_Signal_Header_t SignalCommandHeader
;
293 BT_Signal_ConnectionReq_t ConnectionRequest
;
296 /* Fill out the Signal Command header in the response packet */
297 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_REQUEST
;
298 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
299 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.ConnectionRequest
);
301 /* Fill out the Connection Request in the response packet */
302 PacketData
.ConnectionRequest
.PSM
= PSM
;
303 PacketData
.ConnectionRequest
.SourceChannel
= ChannelData
->LocalNumber
;
305 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
307 BT_ACL_DEBUG(1, ">> L2CAP Connection Request");
308 BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData
.ConnectionRequest
.PSM
);
309 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData
.ConnectionRequest
.SourceChannel
);
314 /** Closes a bluetooth channel that is open to the currently connected remote device, so that no further data
317 * \note The channel is not immediately closed when this function returns - it must undergo an asynchronous
318 * disconnection process first as the main Bluetooth stack processing task is repeatedly called. The
319 * returned channel is unusable by the user application upon return however the channel is not completely
320 * closed until its State element has progressed to the Closed state.
322 * \param[in,out] Channel Channel information structure of the channel to close
324 void Bluetooth_CloseChannel(Bluetooth_Channel_t
* const Channel
)
326 /* Don't try to close a non-existing or already closed channel */
327 if ((Channel
== NULL
) || (Channel
->State
== Channel_Closed
))
330 /* Set the channel's state to the start of the teardown process */
331 Channel
->State
= Channel_WaitDisconnect
;
335 BT_Signal_Header_t SignalCommandHeader
;
336 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
339 /* Fill out the Signal Command header in the response packet */
340 PacketData
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_REQUEST
;
341 PacketData
.SignalCommandHeader
.Identifier
= ++Bluetooth_Connection
.SignallingIdentifier
;
342 PacketData
.SignalCommandHeader
.Length
= sizeof(PacketData
.DisconnectionRequest
);
344 /* Fill out the Disconnection Request in the response packet */
345 PacketData
.DisconnectionRequest
.DestinationChannel
= Channel
->RemoteNumber
;
346 PacketData
.DisconnectionRequest
.SourceChannel
= Channel
->LocalNumber
;
348 Bluetooth_SendPacket(&PacketData
, sizeof(PacketData
), NULL
);
350 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Request");
351 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData
.DisconnectionRequest
.DestinationChannel
);
352 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData
.DisconnectionRequest
.SourceChannel
);
355 /** Internal Bluetooth stack Signal Command processing routine for a Connection Request command.
357 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
359 static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
361 BT_Signal_ConnectionReq_t ConnectionRequest
;
363 Pipe_Read_Stream_LE(&ConnectionRequest
, sizeof(ConnectionRequest
));
368 BT_ACL_DEBUG(1, "<< L2CAP Connection Request");
369 BT_ACL_DEBUG(2, "-- PSM: 0x%04X", ConnectionRequest
.PSM
);
370 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionRequest
.SourceChannel
);
372 _delay_ms(15); // TODO - Determine why this is needed
374 /* Try to retrieve the existing channel's information structure if it exists */
375 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionRequest
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
377 /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */
378 if (ChannelData
== NULL
)
380 /* Look through the channel information list for a free entry */
381 for (uint8_t i
= 0; i
< BLUETOOTH_MAX_OPEN_CHANNELS
; i
++)
383 if (Bluetooth_Connection
.Channels
[i
].State
== Channel_Closed
)
385 ChannelData
= &Bluetooth_Connection
.Channels
[i
];
387 /* Set the new channel structure's local channel number to a unique value within the connection orientated
388 channel address space */
389 ChannelData
->LocalNumber
= (BT_CHANNELNUMBER_BASEOFFSET
+ i
);
395 uint8_t ChannelStatus
= BT_CONNECTION_REFUSED_RESOURCES
;
397 /* Reset the channel item contents only if a channel entry was found for it */
398 if (ChannelData
!= NULL
)
400 /* Check if the user application will allow the connection based on its PSM */
401 if (Bluetooth_ChannelConnectionRequest(ConnectionRequest
.PSM
))
403 ChannelData
->RemoteNumber
= ConnectionRequest
.SourceChannel
;
404 ChannelData
->PSM
= ConnectionRequest
.PSM
;
405 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
406 ChannelData
->State
= Channel_Config_WaitConfig
;
408 ChannelStatus
= BT_CONNECTION_SUCCESSFUL
;
412 ChannelStatus
= BT_CONNECTION_REFUSED_PSM
;
418 BT_Signal_Header_t SignalCommandHeader
;
419 BT_Signal_ConnectionResp_t ConnectionResponse
;
422 /* Fill out the Signal Command header in the response packet */
423 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_RESPONSE
;
424 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
425 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConnectionResponse
);
427 /* Fill out the Connection Response in the response packet */
428 ResponsePacket
.ConnectionResponse
.DestinationChannel
= ChannelData
->LocalNumber
;
429 ResponsePacket
.ConnectionResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
430 ResponsePacket
.ConnectionResponse
.Result
= ChannelStatus
;
431 ResponsePacket
.ConnectionResponse
.Status
= 0x00;
433 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
435 BT_ACL_DEBUG(1, ">> L2CAP Connection Response");
436 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConnectionResponse
.Result
);
437 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.DestinationChannel
);
438 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.SourceChannel
);
441 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
443 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
445 static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
447 BT_Signal_ConnectionResp_t ConnectionResponse
;
449 Pipe_Read_Stream_LE(&ConnectionResponse
, sizeof(ConnectionResponse
));
454 BT_ACL_DEBUG(1, "<< L2CAP Connection Response");
455 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConnectionResponse
.Result
);
456 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionResponse
.SourceChannel
);
457 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConnectionResponse
.DestinationChannel
);
459 /* Search for the referenced channel in the channel information list */
460 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionResponse
.SourceChannel
, CHANNEL_SEARCH_LOCALNUMBER
);
462 /* Only progress if the referenced channel data was found */
463 if (ChannelData
!= NULL
)
465 /* Set the channel structure's remote channel number to the channel allocated on the remote device */
466 ChannelData
->RemoteNumber
= ConnectionResponse
.SourceChannel
;
467 ChannelData
->State
= (ConnectionResponse
.Result
== BT_CONNECTION_SUCCESSFUL
) ?
468 Channel_Config_WaitConfig
: Channel_Closed
;
472 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
474 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
476 static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
478 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
480 /* Allocate a buffer large enough to hold the variable number of configuration options in the request */
481 uint8_t OptionsLen
= (SignalCommandHeader
->Length
- sizeof(ConfigurationRequest
));
482 uint8_t Options
[OptionsLen
];
484 Pipe_Read_Stream_LE(&ConfigurationRequest
, sizeof(ConfigurationRequest
));
485 Pipe_Read_Stream_LE(&Options
, sizeof(Options
));
490 /* Search for the referenced channel in the channel information list */
491 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationRequest
.DestinationChannel
, CHANNEL_SEARCH_LOCALNUMBER
);
493 BT_ACL_DEBUG(1, "<< L2CAP Configuration Request");
494 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest
.DestinationChannel
);
495 BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", OptionsLen
);
497 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
498 if (ChannelData
!= NULL
)
500 /* Iterate through each option in the configuration request to look for ones which can be processed */
501 uint8_t OptionPos
= 0;
502 while (OptionPos
< OptionsLen
)
504 BT_Config_Option_Header_t
* OptionHeader
= (BT_Config_Option_Header_t
*)&Options
[OptionPos
];
505 void* OptionData
= &Options
[OptionPos
+ sizeof(BT_Config_Option_Header_t
)];
507 BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader
->Type
);
508 BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(BT_Config_Option_Header_t
) + OptionHeader
->Length
));
510 /* Store the remote MTU option's value if present */
511 if (OptionHeader
->Type
== BT_CONFIG_OPTION_MTU
)
512 ChannelData
->RemoteMTU
= *((uint16_t*)OptionData
);
514 /* Progress to the next option in the packet */
515 OptionPos
+= (sizeof(BT_Config_Option_Header_t
) + OptionHeader
->Length
);
521 BT_Signal_Header_t SignalCommandHeader
;
522 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
525 /* Fill out the Signal Command header in the response packet */
526 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_RESPONSE
;
527 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
528 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConfigurationResponse
);
530 /* Fill out the Configuration Response in the response packet */
531 ResponsePacket
.ConfigurationResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
532 ResponsePacket
.ConfigurationResponse
.Flags
= 0x00;
533 ResponsePacket
.ConfigurationResponse
.Result
= (ChannelData
!= NULL
) ? BT_CONFIGURATION_SUCCESSFUL
: BT_CONFIGURATION_REJECTED
;
535 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
537 if (ChannelData
!= NULL
)
539 switch (ChannelData
->State
)
541 case Channel_Config_WaitConfig
:
542 ChannelData
->State
= Channel_Config_WaitSendConfig
;
544 case Channel_Config_WaitReqResp
:
545 ChannelData
->State
= Channel_Config_WaitResp
;
547 case Channel_Config_WaitReq
:
548 ChannelData
->State
= Channel_Open
;
553 BT_ACL_DEBUG(1, ">> L2CAP Configuration Response");
554 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConfigurationResponse
.SourceChannel
);
555 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConfigurationResponse
.Result
);
558 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
560 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
562 static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
564 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
566 Pipe_Read_Stream_LE(&ConfigurationResponse
, sizeof(ConfigurationResponse
));
571 BT_ACL_DEBUG(1, "<< L2CAP Configuration Response");
572 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse
.SourceChannel
);
573 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse
.Result
);
575 /* Search for the referenced channel in the channel information list */
576 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationResponse
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
578 /* Only update the channel's state if it was found in the channel list */
579 if (ChannelData
!= NULL
)
581 /* Check if the channel configuration completed successfuly */
582 if (ConfigurationResponse
.Result
== BT_CONFIGURATION_SUCCESSFUL
)
584 switch (ChannelData
->State
)
586 case Channel_Config_WaitReqResp
:
587 ChannelData
->State
= Channel_Config_WaitReq
;
589 case Channel_Config_WaitResp
:
590 ChannelData
->State
= Channel_Open
;
596 /* Configuration failed - close the channel */
597 ChannelData
->State
= Channel_Closed
;
602 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
604 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
606 static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
608 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
610 Pipe_Read_Stream_LE(&DisconnectionRequest
, sizeof(DisconnectionRequest
));
612 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Request");
613 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionRequest
.DestinationChannel
);
614 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionRequest
.SourceChannel
);
619 /* Search for the referenced channel in the channel information list */
620 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionRequest
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
624 BT_Signal_Header_t SignalCommandHeader
;
625 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
628 /* Fill out the Signal Command header in the response packet */
629 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_RESPONSE
;
630 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
631 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.DisconnectionResponse
);
633 /* Fill out the Disconnection Response in the response packet */
634 ResponsePacket
.DisconnectionResponse
.DestinationChannel
= ChannelData
->RemoteNumber
;
635 ResponsePacket
.DisconnectionResponse
.SourceChannel
= ChannelData
->LocalNumber
;
637 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
639 /* If the channel was found in the channel list, close it */
640 if (ChannelData
!= NULL
)
641 ChannelData
->State
= Channel_Closed
;
643 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response");
644 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.SourceChannel
);
645 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.DestinationChannel
);
648 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
650 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
652 static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t
* const SignalCommandHeader
)
654 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
656 Pipe_Read_Stream_LE(&DisconnectionResponse
, sizeof(DisconnectionResponse
));
658 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response");
659 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse
.DestinationChannel
);
660 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse
.SourceChannel
);
665 /* Search for the referenced channel in the channel information list */
666 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionResponse
.SourceChannel
, CHANNEL_SEARCH_REMOTENUMBER
);
668 /* If the channel was found in the channel list, close it */
669 if (ChannelData
!= NULL
)
670 ChannelData
->State
= Channel_Closed
;
673 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
675 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
677 static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
679 BT_ACL_DEBUG(1, "<< L2CAP Echo Request");
686 BT_Signal_Header_t SignalCommandHeader
;
689 /* Fill out the Signal Command header in the response packet */
690 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_ECHO_RESPONSE
;
691 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
692 ResponsePacket
.SignalCommandHeader
.Length
= 0;
694 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
696 BT_ACL_DEBUG(1, ">> L2CAP Echo Response");
699 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
701 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
703 static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t
* const SignalCommandHeader
)
705 BT_Signal_InformationReq_t InformationRequest
;
707 Pipe_Read_Stream_LE(&InformationRequest
, sizeof(InformationRequest
));
709 BT_ACL_DEBUG(1, "<< L2CAP Information Request");
710 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest
.InfoType
);
717 BT_Signal_Header_t SignalCommandHeader
;
718 BT_Signal_InformationResp_t InformationResponse
;
725 /* Retrieve the requested information and store it in the outgoing packet, if found */
726 switch (InformationRequest
.InfoType
)
729 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
732 *((uint16_t*)&ResponsePacket
.Data
) = MAXIMUM_CHANNEL_MTU
;
734 case BT_INFOREQ_EXTENDEDFEATURES
:
735 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
738 *((uint32_t*)&ResponsePacket
.Data
) = 0;
741 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_NOTSUPPORTED
;
746 /* Fill out the Signal Command header in the response packet */
747 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_INFORMATION_RESPONSE
;
748 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
749 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.InformationResponse
) + DataLen
;
751 /* Fill out the Information Response in the response packet */
752 ResponsePacket
.InformationResponse
.InfoType
= InformationRequest
.InfoType
;
754 Bluetooth_SendPacket(&ResponsePacket
, (sizeof(ResponsePacket
) - sizeof(ResponsePacket
.Data
) + DataLen
), NULL
);
756 BT_ACL_DEBUG(1, ">> L2CAP Information Response");
757 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.InformationResponse
.Result
);