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", NULL
);
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
));
121 BT_ACL_DEBUG(2, "", NULL
);
122 BT_ACL_DEBUG(2, "Packet Received", NULL
);
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", NULL
);
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 Data Pointer to a buffer where the data is to be sourced from
197 * \param DataLen Length of the data to send
198 * \param 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
, uint16_t DataLen
, Bluetooth_Channel_t
* 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
);
233 BT_ACL_DEBUG(2, "", NULL
);
234 BT_ACL_DEBUG(2, "Packet Sent", NULL
);
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 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(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", NULL
);
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 Channel Channel information structure of the channel to close
316 void Bluetooth_CloseChannel(Bluetooth_Channel_t
* 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", NULL
);
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 SignalCommandHeader Pointer to the start of the received packet's Signal Command header
351 static inline void Bluetooth_Signal_ConnectionReq(BT_Signal_Header_t
* SignalCommandHeader
)
353 BT_Signal_ConnectionReq_t ConnectionRequest
;
355 Pipe_Read_Stream_LE(&ConnectionRequest
, sizeof(ConnectionRequest
));
360 BT_ACL_DEBUG(1, "<< L2CAP Connection Request", NULL
);
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 /* Reset the channel item contents only if a channel entry was found for it */
386 if (ChannelData
!= NULL
)
388 ChannelData
->RemoteNumber
= ConnectionRequest
.SourceChannel
;
389 ChannelData
->PSM
= ConnectionRequest
.PSM
;
390 ChannelData
->LocalMTU
= MAXIMUM_CHANNEL_MTU
;
391 ChannelData
->State
= Channel_Config_WaitConfig
;
396 BT_Signal_Header_t SignalCommandHeader
;
397 BT_Signal_ConnectionResp_t ConnectionResponse
;
400 /* Fill out the Signal Command header in the response packet */
401 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONNECTION_RESPONSE
;
402 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
403 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConnectionResponse
);
405 /* Fill out the Connection Response in the response packet */
406 ResponsePacket
.ConnectionResponse
.DestinationChannel
= ChannelData
->LocalNumber
;
407 ResponsePacket
.ConnectionResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
408 ResponsePacket
.ConnectionResponse
.Result
= (ChannelData
== NULL
) ? BT_CONNECTION_REFUSED_RESOURCES
:
409 BT_CONNECTION_SUCCESSFUL
;
410 ResponsePacket
.ConnectionResponse
.Status
= 0x00;
412 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
414 BT_ACL_DEBUG(1, ">> L2CAP Connection Response", NULL
);
415 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConnectionResponse
.Result
);
416 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.DestinationChannel
);
417 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConnectionResponse
.SourceChannel
);
420 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
422 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
424 static inline void Bluetooth_Signal_ConnectionResp(BT_Signal_Header_t
* SignalCommandHeader
)
426 BT_Signal_ConnectionResp_t ConnectionResponse
;
428 Pipe_Read_Stream_LE(&ConnectionResponse
, sizeof(ConnectionResponse
));
433 BT_ACL_DEBUG(1, "<< L2CAP Connection Response", NULL
);
434 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConnectionResponse
.Result
);
435 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionResponse
.SourceChannel
);
436 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConnectionResponse
.DestinationChannel
);
438 /* Search for the referenced channel in the channel information list */
439 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConnectionResponse
.SourceChannel
, false);
441 /* Only progress if the referenced channel data was found */
442 if (ChannelData
!= NULL
)
444 /* Set the channel structure's remote channel number to the channel allocated on the remote device */
445 ChannelData
->RemoteNumber
= ConnectionResponse
.SourceChannel
;
446 ChannelData
->State
= (ConnectionResponse
.Result
== BT_CONNECTION_SUCCESSFUL
) ?
447 Channel_Config_WaitConfig
: Channel_Closed
;
451 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
453 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
455 static inline void Bluetooth_Signal_ConfigurationReq(BT_Signal_Header_t
* SignalCommandHeader
)
457 BT_Signal_ConfigurationReq_t ConfigurationRequest
;
459 /* Allocate a buffer large enough to hold the variable number of configuration options in the request */
460 uint8_t OptionsLen
= (SignalCommandHeader
->Length
- sizeof(ConfigurationRequest
));
461 uint8_t Options
[OptionsLen
];
463 Pipe_Read_Stream_LE(&ConfigurationRequest
, sizeof(ConfigurationRequest
));
464 Pipe_Read_Stream_LE(&Options
, sizeof(Options
));
469 /* Search for the referenced channel in the channel information list */
470 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationRequest
.DestinationChannel
, false);
472 BT_ACL_DEBUG(1, "<< L2CAP Configuration Request", NULL
);
473 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest
.DestinationChannel
);
474 BT_ACL_DEBUG(2, "-- Remote MTU: 0x%04X", ChannelData
->RemoteMTU
);
475 BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", OptionsLen
);
477 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
478 if (ChannelData
!= NULL
)
480 /* Iterate through each option in the configuration request to look for ones which can be processed */
481 uint8_t OptionPos
= 0;
482 while (OptionPos
< OptionsLen
)
484 BT_Config_Option_Header_t
* OptionHeader
= (BT_Config_Option_Header_t
*)&Options
[OptionPos
];
485 void* OptionData
= &Options
[OptionPos
+ sizeof(*OptionHeader
)];
487 BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader
->Type
);
488 BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(*OptionHeader
) + OptionHeader
->Length
));
490 /* Store the remote MTU option's value if present */
491 if (OptionHeader
->Type
== BT_CONFIG_OPTION_MTU
)
492 ChannelData
->RemoteMTU
= *((uint16_t*)OptionData
);
494 /* Progress to the next option in the packet */
495 OptionPos
+= (sizeof(*OptionHeader
) + OptionHeader
->Length
);
501 BT_Signal_Header_t SignalCommandHeader
;
502 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
505 /* Fill out the Signal Command header in the response packet */
506 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_CONFIGURATION_RESPONSE
;
507 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
508 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.ConfigurationResponse
);
510 /* Fill out the Configuration Response in the response packet */
511 ResponsePacket
.ConfigurationResponse
.SourceChannel
= ChannelData
->RemoteNumber
;
512 ResponsePacket
.ConfigurationResponse
.Flags
= 0x00;
513 ResponsePacket
.ConfigurationResponse
.Result
= (ChannelData
!= NULL
) ? BT_CONFIGURATION_SUCCESSFUL
: BT_CONFIGURATION_REJECTED
;
515 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
517 if (ChannelData
!= NULL
)
519 switch (ChannelData
->State
)
521 case Channel_Config_WaitConfig
:
522 ChannelData
->State
= Channel_Config_WaitSendConfig
;
524 case Channel_Config_WaitReqResp
:
525 ChannelData
->State
= Channel_Config_WaitResp
;
527 case Channel_Config_WaitReq
:
528 ChannelData
->State
= Channel_Open
;
533 BT_ACL_DEBUG(1, ">> L2CAP Configuration Response", NULL
);
534 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.ConfigurationResponse
.SourceChannel
);
535 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.ConfigurationResponse
.Result
);
538 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
540 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
542 static inline void Bluetooth_Signal_ConfigurationResp(BT_Signal_Header_t
* SignalCommandHeader
)
544 BT_Signal_ConfigurationResp_t ConfigurationResponse
;
546 Pipe_Read_Stream_LE(&ConfigurationResponse
, sizeof(ConfigurationResponse
));
551 BT_ACL_DEBUG(1, "<< L2CAP Configuration Response", NULL
);
552 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse
.SourceChannel
);
553 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse
.Result
);
555 /* Search for the referenced channel in the channel information list */
556 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(ConfigurationResponse
.SourceChannel
, true);
558 /* Only update the channel's state if it was found in the channel list */
559 if (ChannelData
!= NULL
)
561 /* Check if the channel configuration completed successfuly */
562 if (ConfigurationResponse
.Result
== BT_CONFIGURATION_SUCCESSFUL
)
564 switch (ChannelData
->State
)
566 case Channel_Config_WaitReqResp
:
567 ChannelData
->State
= Channel_Config_WaitReq
;
569 case Channel_Config_WaitResp
:
570 ChannelData
->State
= Channel_Open
;
576 /* Configuration failed - close the channel */
577 ChannelData
->State
= Channel_Closed
;
582 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
584 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
586 static inline void Bluetooth_Signal_DisconnectionReq(BT_Signal_Header_t
* SignalCommandHeader
)
588 BT_Signal_DisconnectionReq_t DisconnectionRequest
;
590 Pipe_Read_Stream_LE(&DisconnectionRequest
, sizeof(DisconnectionRequest
));
592 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Request", NULL
);
593 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionRequest
.DestinationChannel
);
594 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionRequest
.SourceChannel
);
599 /* Search for the referenced channel in the channel information list */
600 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionRequest
.SourceChannel
, true);
604 BT_Signal_Header_t SignalCommandHeader
;
605 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
608 /* Fill out the Signal Command header in the response packet */
609 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_DISCONNECTION_RESPONSE
;
610 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
611 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.DisconnectionResponse
);
613 /* Fill out the Disconnection Response in the response packet */
614 ResponsePacket
.DisconnectionResponse
.DestinationChannel
= ChannelData
->RemoteNumber
;
615 ResponsePacket
.DisconnectionResponse
.SourceChannel
= ChannelData
->LocalNumber
;
617 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
619 /* If the channel was found in the channel list, close it */
620 if (ChannelData
!= NULL
)
621 ChannelData
->State
= Channel_Closed
;
623 BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response", NULL
);
624 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.SourceChannel
);
625 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket
.DisconnectionResponse
.DestinationChannel
);
628 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
630 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
632 static inline void Bluetooth_Signal_DisconnectionResp(BT_Signal_Header_t
* SignalCommandHeader
)
634 BT_Signal_DisconnectionResp_t DisconnectionResponse
;
636 Pipe_Read_Stream_LE(&DisconnectionResponse
, sizeof(DisconnectionResponse
));
638 BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response", NULL
);
639 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse
.DestinationChannel
);
640 BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse
.SourceChannel
);
645 /* Search for the referenced channel in the channel information list */
646 Bluetooth_Channel_t
* ChannelData
= Bluetooth_GetChannelData(DisconnectionResponse
.SourceChannel
, true);
648 /* If the channel was found in the channel list, close it */
649 if (ChannelData
!= NULL
)
650 ChannelData
->State
= Channel_Closed
;
653 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
655 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
657 static inline void Bluetooth_Signal_EchoReq(BT_Signal_Header_t
* SignalCommandHeader
)
659 BT_ACL_DEBUG(1, "<< L2CAP Echo Request", NULL
);
666 BT_Signal_Header_t SignalCommandHeader
;
669 /* Fill out the Signal Command header in the response packet */
670 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_ECHO_RESPONSE
;
671 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
672 ResponsePacket
.SignalCommandHeader
.Length
= 0;
674 Bluetooth_SendPacket(&ResponsePacket
, sizeof(ResponsePacket
), NULL
);
676 BT_ACL_DEBUG(1, ">> L2CAP Echo Response", NULL
);
679 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
681 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
683 static inline void Bluetooth_Signal_InformationReq(BT_Signal_Header_t
* SignalCommandHeader
)
685 BT_Signal_InformationReq_t InformationRequest
;
687 Pipe_Read_Stream_LE(&InformationRequest
, sizeof(InformationRequest
));
689 BT_ACL_DEBUG(1, "<< L2CAP Information Request", NULL
);
690 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest
.InfoType
);
697 BT_Signal_Header_t SignalCommandHeader
;
698 BT_Signal_InformationResp_t InformationResponse
;
705 /* Retrieve the requested information and store it in the outgoing packet, if found */
706 switch (InformationRequest
.InfoType
)
709 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
712 *((uint16_t*)&ResponsePacket
.Data
) = MAXIMUM_CHANNEL_MTU
;
714 case BT_INFOREQ_EXTENDEDFEATURES
:
715 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_SUCCESSFUL
;
718 *((uint32_t*)&ResponsePacket
.Data
) = 0;
721 ResponsePacket
.InformationResponse
.Result
= BT_INFORMATION_NOTSUPPORTED
;
726 /* Fill out the Signal Command header in the response packet */
727 ResponsePacket
.SignalCommandHeader
.Code
= BT_SIGNAL_INFORMATION_RESPONSE
;
728 ResponsePacket
.SignalCommandHeader
.Identifier
= SignalCommandHeader
->Identifier
;
729 ResponsePacket
.SignalCommandHeader
.Length
= sizeof(ResponsePacket
.InformationResponse
) + DataLen
;
731 /* Fill out the Information Response in the response packet */
732 ResponsePacket
.InformationResponse
.InfoType
= InformationRequest
.InfoType
;
734 Bluetooth_SendPacket(&ResponsePacket
, (sizeof(ResponsePacket
) - sizeof(ResponsePacket
.Data
) + DataLen
), NULL
);
736 BT_ACL_DEBUG(1, ">> L2CAP Information Response", NULL
);
737 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket
.InformationResponse
.Result
);