1a8cf06c08ec6200d6e0592c33c5c608a0a7ad5a
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / BluetoothACLPackets.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C
32 #include "BluetoothACLPackets.h"
33
34 /** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth
35 * stack task to manage the ACL processing state.
36 */
37 void Bluetooth_ACLTask(void)
38 {
39 /* Process incomming ACL packets, if any */
40 Bluetooth_ProcessIncommingACLPackets();
41
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++)
44 {
45 Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];
46
47 bool MustSendConfigReq = true;
48
49 /* Check if we are in a channel state which requires a configuration request to be sent */
50 switch (ChannelData->State)
51 {
52 case Channel_Config_WaitConfig:
53 ChannelData->State = Channel_Config_WaitReqResp;
54 break;
55 case Channel_Config_WaitSendConfig:
56 ChannelData->State = Channel_Config_WaitResp;
57 break;
58 default:
59 MustSendConfigReq = false;
60 break;
61 }
62
63 /* Only send a configuration request if it the channel was in a state which required it */
64 if (MustSendConfigReq)
65 {
66 struct
67 {
68 BT_Signal_Header_t SignalCommandHeader;
69 BT_Signal_ConfigurationReq_t ConfigurationRequest;
70
71 struct
72 {
73 BT_Config_Option_Header_t Header;
74 uint16_t Value;
75 } Option_LocalMTU;
76 } PacketData;
77
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);
83
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;
90
91 Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
92
93 BT_ACL_DEBUG(1, ">> L2CAP Configuration Request");
94 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.ConfigurationRequest.DestinationChannel);
95 }
96 }
97 }
98
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.
102 */
103 static void Bluetooth_ProcessIncommingACLPackets(void)
104 {
105 BT_ACL_Header_t ACLPacketHeader;
106 BT_DataPacket_Header_t DataHeader;
107
108 Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE);
109 Pipe_Unfreeze();
110
111 if (!(Pipe_IsReadWriteAllowed()))
112 {
113 Pipe_Freeze();
114 return;
115 }
116
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));
120
121 BT_ACL_DEBUG(2, "");
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);
127
128 /* Check the packet's destination channel - signalling channel should be processed by the stack internally */
129 if (DataHeader.DestinationChannel == BT_CHANNEL_SIGNALING)
130 {
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));
134
135 /* Dispatch to the appropriate handler function based on the Signal message code */
136 switch (SignalCommandHeader.Code)
137 {
138 case BT_SIGNAL_CONNECTION_REQUEST:
139 Bluetooth_Signal_ConnectionReq(&SignalCommandHeader);
140 break;
141 case BT_SIGNAL_CONNECTION_RESPONSE:
142 Bluetooth_Signal_ConnectionResp(&SignalCommandHeader);
143 break;
144 case BT_SIGNAL_CONFIGURATION_REQUEST:
145 Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader);
146 break;
147 case BT_SIGNAL_CONFIGURATION_RESPONSE:
148 Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader);
149 break;
150 case BT_SIGNAL_DISCONNECTION_REQUEST:
151 Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader);
152 break;
153 case BT_SIGNAL_DISCONNECTION_RESPONSE:
154 Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader);
155 break;
156 case BT_SIGNAL_ECHO_REQUEST:
157 Bluetooth_Signal_EchoReq(&SignalCommandHeader);
158 break;
159 case BT_SIGNAL_INFORMATION_REQUEST:
160 Bluetooth_Signal_InformationReq(&SignalCommandHeader);
161 break;
162 case BT_SIGNAL_COMMAND_REJECT:
163 BT_ACL_DEBUG(1, "<< Command Reject");
164
165 uint16_t RejectReason;
166 Pipe_Read_Stream_LE(&RejectReason, sizeof(RejectReason));
167 Pipe_Discard_Stream(ACLPacketHeader.DataLength - sizeof(RejectReason));
168 Pipe_ClearIN();
169 Pipe_Freeze();
170
171 BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason);
172 break;
173 default:
174 BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader.Code);
175
176 Pipe_Discard_Stream(ACLPacketHeader.DataLength);
177 Pipe_ClearIN();
178 Pipe_Freeze();
179 break;
180 }
181 }
182 else
183 {
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);
187 Pipe_ClearIN();
188 Pipe_Freeze();
189
190 Bluetooth_PacketReceived(PacketData, DataHeader.PayloadLength, Bluetooth_GetChannelData(DataHeader.DestinationChannel, false));
191 }
192 }
193
194 /** Sends a packet to the remote device on the specified channel.
195 *
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
200 *
201 * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum
202 */
203 uint8_t Bluetooth_SendPacket(void* Data, const uint16_t DataLen, Bluetooth_Channel_t* const Channel)
204 {
205 BT_ACL_Header_t ACLPacketHeader;
206 BT_DataPacket_Header_t DataHeader;
207
208 /* A remote device must be connected before a packet transmission is attempted */
209 if (!(Bluetooth_Connection.IsConnected))
210 return BT_SENDPACKET_NotConnected;
211
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;
215
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;
221
222 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);
223 Pipe_Unfreeze();
224
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);
229 Pipe_ClearOUT();
230
231 Pipe_Freeze();
232
233 BT_ACL_DEBUG(2, "");
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);
239
240 return BT_SENDPACKET_NoError;
241 }
242
243 /** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged.
244 *
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.
249 *
250 * \param[in] PSM PSM of the service that the channel is to be opened for
251 *
252 * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels
253 */
254 Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM)
255 {
256 Bluetooth_Channel_t* ChannelData = NULL;
257
258 /* Search through the channel information list for a free channel item */
259 for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
260 {
261 if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
262 {
263 ChannelData = &Bluetooth_Connection.Channels[i];
264
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);
268 break;
269 }
270 }
271
272 /* If no free channel item was found in the list, all channels are occupied - abort */
273 if (ChannelData == NULL)
274 return NULL;
275
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;
281
282 struct
283 {
284 BT_Signal_Header_t SignalCommandHeader;
285 BT_Signal_ConnectionReq_t ConnectionRequest;
286 } PacketData;
287
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);
292
293 /* Fill out the Connection Request in the response packet */
294 PacketData.ConnectionRequest.PSM = PSM;
295 PacketData.ConnectionRequest.SourceChannel = ChannelData->LocalNumber;
296
297 Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
298
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);
302
303 return ChannelData;
304 }
305
306 /** Closes a bluetooth channel that is open to the currently connected remote device, so that no further data
307 * can be exchanged.
308 *
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.
313 *
314 * \param[in,out] Channel Channel information structure of the channel to close
315 */
316 void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel)
317 {
318 /* Don't try to close a non-existing or already closed channel */
319 if ((Channel == NULL) || (Channel->State == Channel_Closed))
320 return;
321
322 /* Set the channel's state to the start of the teardown process */
323 Channel->State = Channel_WaitDisconnect;
324
325 struct
326 {
327 BT_Signal_Header_t SignalCommandHeader;
328 BT_Signal_DisconnectionReq_t DisconnectionRequest;
329 } PacketData;
330
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);
335
336 /* Fill out the Disconnection Request in the response packet */
337 PacketData.DisconnectionRequest.DestinationChannel = Channel->RemoteNumber;
338 PacketData.DisconnectionRequest.SourceChannel = Channel->LocalNumber;
339
340 Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
341
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);
345 }
346
347 /** Internal Bluetooth stack Signal Command processing routine for a Connection Request command.
348 *
349 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
350 */
351 static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* const SignalCommandHeader)
352 {
353 BT_Signal_ConnectionReq_t ConnectionRequest;
354
355 Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest));
356
357 Pipe_ClearIN();
358 Pipe_Freeze();
359
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);
363
364 /* Try to retrieve the existing channel's information structure if it exists */
365 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionRequest.SourceChannel, true);
366
367 /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */
368 if (ChannelData == NULL)
369 {
370 /* Look through the channel information list for a free entry */
371 for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
372 {
373 if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
374 {
375 ChannelData = &Bluetooth_Connection.Channels[i];
376
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);
380 break;
381 }
382 }
383 }
384
385 uint8_t ChannelStatus = BT_CONNECTION_REFUSED_RESOURCES;
386
387 /* Reset the channel item contents only if a channel entry was found for it */
388 if (ChannelData != NULL)
389 {
390 /* Check if the user application will allow the connection based on its PSM */
391 if (Bluetooth_ChannelConnectionRequest(ConnectionRequest.PSM))
392 {
393 ChannelData->RemoteNumber = ConnectionRequest.SourceChannel;
394 ChannelData->PSM = ConnectionRequest.PSM;
395 ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
396 ChannelData->State = Channel_Config_WaitConfig;
397
398 ChannelStatus = BT_CONNECTION_SUCCESSFUL;
399 }
400 else
401 {
402 ChannelStatus = BT_CONNECTION_REFUSED_PSM;
403 }
404 }
405
406 struct
407 {
408 BT_Signal_Header_t SignalCommandHeader;
409 BT_Signal_ConnectionResp_t ConnectionResponse;
410 } ResponsePacket;
411
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);
416
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;
422
423 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
424
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);
429 }
430
431 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
432 *
433 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
434 */
435 static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* const SignalCommandHeader)
436 {
437 BT_Signal_ConnectionResp_t ConnectionResponse;
438
439 Pipe_Read_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse));
440
441 Pipe_ClearIN();
442 Pipe_Freeze();
443
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);
448
449 /* Search for the referenced channel in the channel information list */
450 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionResponse.SourceChannel, false);
451
452 /* Only progress if the referenced channel data was found */
453 if (ChannelData != NULL)
454 {
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;
459 }
460 }
461
462 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
463 *
464 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
465 */
466 static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* const SignalCommandHeader)
467 {
468 BT_Signal_ConfigurationReq_t ConfigurationRequest;
469
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];
473
474 Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest));
475 Pipe_Read_Stream_LE(&Options, sizeof(Options));
476
477 Pipe_ClearIN();
478 Pipe_Freeze();
479
480 /* Search for the referenced channel in the channel information list */
481 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, false);
482
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);
487
488 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
489 if (ChannelData != NULL)
490 {
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)
494 {
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)];
497
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));
500
501 /* Store the remote MTU option's value if present */
502 if (OptionHeader->Type == BT_CONFIG_OPTION_MTU)
503 ChannelData->RemoteMTU = *((uint16_t*)OptionData);
504
505 /* Progress to the next option in the packet */
506 OptionPos += (sizeof(BT_Config_Option_Header_t) + OptionHeader->Length);
507 }
508 }
509
510 struct
511 {
512 BT_Signal_Header_t SignalCommandHeader;
513 BT_Signal_ConfigurationResp_t ConfigurationResponse;
514 } ResponsePacket;
515
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);
520
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;
525
526 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
527
528 if (ChannelData != NULL)
529 {
530 switch (ChannelData->State)
531 {
532 case Channel_Config_WaitConfig:
533 ChannelData->State = Channel_Config_WaitSendConfig;
534 break;
535 case Channel_Config_WaitReqResp:
536 ChannelData->State = Channel_Config_WaitResp;
537 break;
538 case Channel_Config_WaitReq:
539 ChannelData->State = Channel_Open;
540 break;
541 }
542 }
543
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);
547 }
548
549 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
550 *
551 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
552 */
553 static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t* const SignalCommandHeader)
554 {
555 BT_Signal_ConfigurationResp_t ConfigurationResponse;
556
557 Pipe_Read_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse));
558
559 Pipe_ClearIN();
560 Pipe_Freeze();
561
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);
565
566 /* Search for the referenced channel in the channel information list */
567 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationResponse.SourceChannel, true);
568
569 /* Only update the channel's state if it was found in the channel list */
570 if (ChannelData != NULL)
571 {
572 /* Check if the channel configuration completed successfuly */
573 if (ConfigurationResponse.Result == BT_CONFIGURATION_SUCCESSFUL)
574 {
575 switch (ChannelData->State)
576 {
577 case Channel_Config_WaitReqResp:
578 ChannelData->State = Channel_Config_WaitReq;
579 break;
580 case Channel_Config_WaitResp:
581 ChannelData->State = Channel_Open;
582 break;
583 }
584 }
585 else
586 {
587 /* Configuration failed - close the channel */
588 ChannelData->State = Channel_Closed;
589 }
590 }
591 }
592
593 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
594 *
595 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
596 */
597 static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* const SignalCommandHeader)
598 {
599 BT_Signal_DisconnectionReq_t DisconnectionRequest;
600
601 Pipe_Read_Stream_LE(&DisconnectionRequest, sizeof(DisconnectionRequest));
602
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);
606
607 Pipe_ClearIN();
608 Pipe_Freeze();
609
610 /* Search for the referenced channel in the channel information list */
611 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, true);
612
613 struct
614 {
615 BT_Signal_Header_t SignalCommandHeader;
616 BT_Signal_DisconnectionResp_t DisconnectionResponse;
617 } ResponsePacket;
618
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);
623
624 /* Fill out the Disconnection Response in the response packet */
625 ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->RemoteNumber;
626 ResponsePacket.DisconnectionResponse.SourceChannel = ChannelData->LocalNumber;
627
628 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
629
630 /* If the channel was found in the channel list, close it */
631 if (ChannelData != NULL)
632 ChannelData->State = Channel_Closed;
633
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);
637 }
638
639 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
640 *
641 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
642 */
643 static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t* const SignalCommandHeader)
644 {
645 BT_Signal_DisconnectionResp_t DisconnectionResponse;
646
647 Pipe_Read_Stream_LE(&DisconnectionResponse, sizeof(DisconnectionResponse));
648
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);
652
653 Pipe_ClearIN();
654 Pipe_Freeze();
655
656 /* Search for the referenced channel in the channel information list */
657 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionResponse.SourceChannel, true);
658
659 /* If the channel was found in the channel list, close it */
660 if (ChannelData != NULL)
661 ChannelData->State = Channel_Closed;
662 }
663
664 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
665 *
666 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
667 */
668 static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t* const SignalCommandHeader)
669 {
670 BT_ACL_DEBUG(1, "<< L2CAP Echo Request");
671
672 Pipe_ClearIN();
673 Pipe_Freeze();
674
675 struct
676 {
677 BT_Signal_Header_t SignalCommandHeader;
678 } ResponsePacket;
679
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;
684
685 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
686
687 BT_ACL_DEBUG(1, ">> L2CAP Echo Response");
688 }
689
690 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
691 *
692 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
693 */
694 static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t* const SignalCommandHeader)
695 {
696 BT_Signal_InformationReq_t InformationRequest;
697
698 Pipe_Read_Stream_LE(&InformationRequest, sizeof(InformationRequest));
699
700 BT_ACL_DEBUG(1, "<< L2CAP Information Request");
701 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest.InfoType);
702
703 Pipe_ClearIN();
704 Pipe_Freeze();
705
706 struct
707 {
708 BT_Signal_Header_t SignalCommandHeader;
709 BT_Signal_InformationResp_t InformationResponse;
710
711 uint8_t Data[4];
712 } ResponsePacket;
713
714 uint8_t DataLen = 0;
715
716 /* Retrieve the requested information and store it in the outgoing packet, if found */
717 switch (InformationRequest.InfoType)
718 {
719 case BT_INFOREQ_MTU:
720 ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;
721 DataLen = 2;
722
723 *((uint16_t*)&ResponsePacket.Data) = MAXIMUM_CHANNEL_MTU;
724 break;
725 case BT_INFOREQ_EXTENDEDFEATURES:
726 ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;
727 DataLen = 4;
728
729 *((uint32_t*)&ResponsePacket.Data) = 0;
730 break;
731 default:
732 ResponsePacket.InformationResponse.Result = BT_INFORMATION_NOTSUPPORTED;
733 DataLen = 0;
734 break;
735 }
736
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;
741
742 /* Fill out the Information Response in the response packet */
743 ResponsePacket.InformationResponse.InfoType = InformationRequest.InfoType;
744
745 Bluetooth_SendPacket(&ResponsePacket, (sizeof(ResponsePacket) - sizeof(ResponsePacket.Data) + DataLen), NULL);
746
747 BT_ACL_DEBUG(1, ">> L2CAP Information Response");
748 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.InformationResponse.Result);
749 }