Rename FunctionAttributes.h to Attributes.h, as some attributes are applicable to...
[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", NULL);
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, "", 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);
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", NULL);
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 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
200 *
201 * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum
202 */
203 uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t* 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, "", 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);
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 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(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", NULL);
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 Channel Channel information structure of the channel to close
315 */
316 void Bluetooth_CloseChannel(Bluetooth_Channel_t* 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", 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);
345 }
346
347 /** Internal Bluetooth stack Signal Command processing routine for a Connection Request command.
348 *
349 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
350 */
351 static inline void Bluetooth_Signal_ConnectionReq(BT_Signal_Header_t* 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", NULL);
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 /* Reset the channel item contents only if a channel entry was found for it */
386 if (ChannelData != NULL)
387 {
388 ChannelData->RemoteNumber = ConnectionRequest.SourceChannel;
389 ChannelData->PSM = ConnectionRequest.PSM;
390 ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
391 ChannelData->State = Channel_Config_WaitConfig;
392 }
393
394 struct
395 {
396 BT_Signal_Header_t SignalCommandHeader;
397 BT_Signal_ConnectionResp_t ConnectionResponse;
398 } ResponsePacket;
399
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);
404
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;
411
412 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
413
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);
418 }
419
420 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
421 *
422 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
423 */
424 static inline void Bluetooth_Signal_ConnectionResp(BT_Signal_Header_t* SignalCommandHeader)
425 {
426 BT_Signal_ConnectionResp_t ConnectionResponse;
427
428 Pipe_Read_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse));
429
430 Pipe_ClearIN();
431 Pipe_Freeze();
432
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);
437
438 /* Search for the referenced channel in the channel information list */
439 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionResponse.SourceChannel, false);
440
441 /* Only progress if the referenced channel data was found */
442 if (ChannelData != NULL)
443 {
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;
448 }
449 }
450
451 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
452 *
453 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
454 */
455 static inline void Bluetooth_Signal_ConfigurationReq(BT_Signal_Header_t* SignalCommandHeader)
456 {
457 BT_Signal_ConfigurationReq_t ConfigurationRequest;
458
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];
462
463 Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest));
464 Pipe_Read_Stream_LE(&Options, sizeof(Options));
465
466 Pipe_ClearIN();
467 Pipe_Freeze();
468
469 /* Search for the referenced channel in the channel information list */
470 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, false);
471
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);
476
477 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
478 if (ChannelData != NULL)
479 {
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)
483 {
484 BT_Config_Option_Header_t* OptionHeader = (BT_Config_Option_Header_t*)&Options[OptionPos];
485 void* OptionData = &Options[OptionPos + sizeof(*OptionHeader)];
486
487 BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader->Type);
488 BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(*OptionHeader) + OptionHeader->Length));
489
490 /* Store the remote MTU option's value if present */
491 if (OptionHeader->Type == BT_CONFIG_OPTION_MTU)
492 ChannelData->RemoteMTU = *((uint16_t*)OptionData);
493
494 /* Progress to the next option in the packet */
495 OptionPos += (sizeof(*OptionHeader) + OptionHeader->Length);
496 }
497 }
498
499 struct
500 {
501 BT_Signal_Header_t SignalCommandHeader;
502 BT_Signal_ConfigurationResp_t ConfigurationResponse;
503 } ResponsePacket;
504
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);
509
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;
514
515 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
516
517 if (ChannelData != NULL)
518 {
519 switch (ChannelData->State)
520 {
521 case Channel_Config_WaitConfig:
522 ChannelData->State = Channel_Config_WaitSendConfig;
523 break;
524 case Channel_Config_WaitReqResp:
525 ChannelData->State = Channel_Config_WaitResp;
526 break;
527 case Channel_Config_WaitReq:
528 ChannelData->State = Channel_Open;
529 break;
530 }
531 }
532
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);
536 }
537
538 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
539 *
540 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
541 */
542 static inline void Bluetooth_Signal_ConfigurationResp(BT_Signal_Header_t* SignalCommandHeader)
543 {
544 BT_Signal_ConfigurationResp_t ConfigurationResponse;
545
546 Pipe_Read_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse));
547
548 Pipe_ClearIN();
549 Pipe_Freeze();
550
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);
554
555 /* Search for the referenced channel in the channel information list */
556 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationResponse.SourceChannel, true);
557
558 /* Only update the channel's state if it was found in the channel list */
559 if (ChannelData != NULL)
560 {
561 /* Check if the channel configuration completed successfuly */
562 if (ConfigurationResponse.Result == BT_CONFIGURATION_SUCCESSFUL)
563 {
564 switch (ChannelData->State)
565 {
566 case Channel_Config_WaitReqResp:
567 ChannelData->State = Channel_Config_WaitReq;
568 break;
569 case Channel_Config_WaitResp:
570 ChannelData->State = Channel_Open;
571 break;
572 }
573 }
574 else
575 {
576 /* Configuration failed - close the channel */
577 ChannelData->State = Channel_Closed;
578 }
579 }
580 }
581
582 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
583 *
584 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
585 */
586 static inline void Bluetooth_Signal_DisconnectionReq(BT_Signal_Header_t* SignalCommandHeader)
587 {
588 BT_Signal_DisconnectionReq_t DisconnectionRequest;
589
590 Pipe_Read_Stream_LE(&DisconnectionRequest, sizeof(DisconnectionRequest));
591
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);
595
596 Pipe_ClearIN();
597 Pipe_Freeze();
598
599 /* Search for the referenced channel in the channel information list */
600 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, true);
601
602 struct
603 {
604 BT_Signal_Header_t SignalCommandHeader;
605 BT_Signal_DisconnectionResp_t DisconnectionResponse;
606 } ResponsePacket;
607
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);
612
613 /* Fill out the Disconnection Response in the response packet */
614 ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->RemoteNumber;
615 ResponsePacket.DisconnectionResponse.SourceChannel = ChannelData->LocalNumber;
616
617 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
618
619 /* If the channel was found in the channel list, close it */
620 if (ChannelData != NULL)
621 ChannelData->State = Channel_Closed;
622
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);
626 }
627
628 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
629 *
630 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
631 */
632 static inline void Bluetooth_Signal_DisconnectionResp(BT_Signal_Header_t* SignalCommandHeader)
633 {
634 BT_Signal_DisconnectionResp_t DisconnectionResponse;
635
636 Pipe_Read_Stream_LE(&DisconnectionResponse, sizeof(DisconnectionResponse));
637
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);
641
642 Pipe_ClearIN();
643 Pipe_Freeze();
644
645 /* Search for the referenced channel in the channel information list */
646 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionResponse.SourceChannel, true);
647
648 /* If the channel was found in the channel list, close it */
649 if (ChannelData != NULL)
650 ChannelData->State = Channel_Closed;
651 }
652
653 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
654 *
655 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
656 */
657 static inline void Bluetooth_Signal_EchoReq(BT_Signal_Header_t* SignalCommandHeader)
658 {
659 BT_ACL_DEBUG(1, "<< L2CAP Echo Request", NULL);
660
661 Pipe_ClearIN();
662 Pipe_Freeze();
663
664 struct
665 {
666 BT_Signal_Header_t SignalCommandHeader;
667 } ResponsePacket;
668
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;
673
674 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
675
676 BT_ACL_DEBUG(1, ">> L2CAP Echo Response", NULL);
677 }
678
679 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
680 *
681 * \param SignalCommandHeader Pointer to the start of the received packet's Signal Command header
682 */
683 static inline void Bluetooth_Signal_InformationReq(BT_Signal_Header_t* SignalCommandHeader)
684 {
685 BT_Signal_InformationReq_t InformationRequest;
686
687 Pipe_Read_Stream_LE(&InformationRequest, sizeof(InformationRequest));
688
689 BT_ACL_DEBUG(1, "<< L2CAP Information Request", NULL);
690 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest.InfoType);
691
692 Pipe_ClearIN();
693 Pipe_Freeze();
694
695 struct
696 {
697 BT_Signal_Header_t SignalCommandHeader;
698 BT_Signal_InformationResp_t InformationResponse;
699
700 uint8_t Data[4];
701 } ResponsePacket;
702
703 uint8_t DataLen = 0;
704
705 /* Retrieve the requested information and store it in the outgoing packet, if found */
706 switch (InformationRequest.InfoType)
707 {
708 case BT_INFOREQ_MTU:
709 ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;
710 DataLen = 2;
711
712 *((uint16_t*)&ResponsePacket.Data) = MAXIMUM_CHANNEL_MTU;
713 break;
714 case BT_INFOREQ_EXTENDEDFEATURES:
715 ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;
716 DataLen = 4;
717
718 *((uint32_t*)&ResponsePacket.Data) = 0;
719 break;
720 default:
721 ResponsePacket.InformationResponse.Result = BT_INFORMATION_NOTSUPPORTED;
722 DataLen = 0;
723 break;
724 }
725
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;
730
731 /* Fill out the Information Response in the response packet */
732 ResponsePacket.InformationResponse.InfoType = InformationRequest.InfoType;
733
734 Bluetooth_SendPacket(&ResponsePacket, (sizeof(ResponsePacket) - sizeof(ResponsePacket.Data) + DataLen), NULL);
735
736 BT_ACL_DEBUG(1, ">> L2CAP Information Response", NULL);
737 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.InformationResponse.Result);
738 }