Extra protocol value in the RFCOMM protocol is the channel number, not the PSM -...
[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 /*
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
36 */
37
38 #define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C
39 #include "BluetoothACLPackets.h"
40
41 /** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth
42 * stack task to manage the ACL processing state.
43 */
44 void Bluetooth_ACLTask(void)
45 {
46 /* Process incomming ACL packets, if any */
47 Bluetooth_ProcessIncommingACLPackets();
48
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++)
51 {
52 Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];
53
54 bool MustSendConfigReq = true;
55
56 /* Check if we are in a channel state which requires a configuration request to be sent */
57 switch (ChannelData->State)
58 {
59 case Channel_Config_WaitConfig:
60 ChannelData->State = Channel_Config_WaitReqResp;
61 break;
62 case Channel_Config_WaitSendConfig:
63 ChannelData->State = Channel_Config_WaitResp;
64 break;
65 default:
66 MustSendConfigReq = false;
67 break;
68 }
69
70 /* Only send a configuration request if it the channel was in a state which required it */
71 if (MustSendConfigReq)
72 {
73 struct
74 {
75 BT_Signal_Header_t SignalCommandHeader;
76 BT_Signal_ConfigurationReq_t ConfigurationRequest;
77
78 struct
79 {
80 BT_Config_Option_Header_t Header;
81 uint16_t Value;
82 } Option_LocalMTU;
83 } PacketData;
84
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);
90
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;
97
98 Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
99
100 BT_ACL_DEBUG(1, ">> L2CAP Configuration Request");
101 BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.ConfigurationRequest.DestinationChannel);
102 }
103 }
104 }
105
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.
109 */
110 static void Bluetooth_ProcessIncommingACLPackets(void)
111 {
112 BT_ACL_Header_t ACLPacketHeader;
113 BT_DataPacket_Header_t DataHeader;
114
115 Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE);
116 Pipe_Unfreeze();
117
118 if (!(Pipe_IsReadWriteAllowed()))
119 {
120 Pipe_Freeze();
121 return;
122 }
123
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));
127
128 BT_ACL_DEBUG(2, "");
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);
134
135 /* Check the packet's destination channel - signalling channel should be processed by the stack internally */
136 if (DataHeader.DestinationChannel == BT_CHANNEL_SIGNALING)
137 {
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));
141
142 /* Dispatch to the appropriate handler function based on the Signal message code */
143 switch (SignalCommandHeader.Code)
144 {
145 case BT_SIGNAL_CONNECTION_REQUEST:
146 Bluetooth_Signal_ConnectionReq(&SignalCommandHeader);
147 break;
148 case BT_SIGNAL_CONNECTION_RESPONSE:
149 Bluetooth_Signal_ConnectionResp(&SignalCommandHeader);
150 break;
151 case BT_SIGNAL_CONFIGURATION_REQUEST:
152 Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader);
153 break;
154 case BT_SIGNAL_CONFIGURATION_RESPONSE:
155 Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader);
156 break;
157 case BT_SIGNAL_DISCONNECTION_REQUEST:
158 Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader);
159 break;
160 case BT_SIGNAL_DISCONNECTION_RESPONSE:
161 Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader);
162 break;
163 case BT_SIGNAL_ECHO_REQUEST:
164 Bluetooth_Signal_EchoReq(&SignalCommandHeader);
165 break;
166 case BT_SIGNAL_INFORMATION_REQUEST:
167 Bluetooth_Signal_InformationReq(&SignalCommandHeader);
168 break;
169 case BT_SIGNAL_COMMAND_REJECT:
170 BT_ACL_DEBUG(1, "<< Command Reject");
171
172 uint16_t RejectReason;
173 Pipe_Read_Stream_LE(&RejectReason, sizeof(RejectReason));
174 Pipe_Discard_Stream(ACLPacketHeader.DataLength - sizeof(RejectReason));
175 Pipe_ClearIN();
176 Pipe_Freeze();
177
178 BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason);
179 break;
180 default:
181 BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader.Code);
182
183 Pipe_Discard_Stream(ACLPacketHeader.DataLength);
184 Pipe_ClearIN();
185 Pipe_Freeze();
186 break;
187 }
188 }
189 else
190 {
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);
194 Pipe_ClearIN();
195 Pipe_Freeze();
196
197 Bluetooth_PacketReceived(PacketData, DataHeader.PayloadLength,
198 Bluetooth_GetChannelData(DataHeader.DestinationChannel, CHANNEL_SEARCH_LOCALNUMBER));
199 }
200 }
201
202 /** Sends a packet to the remote device on the specified channel.
203 *
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
208 *
209 * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum
210 */
211 uint8_t Bluetooth_SendPacket(void* Data, const uint16_t DataLen, Bluetooth_Channel_t* const Channel)
212 {
213 BT_ACL_Header_t ACLPacketHeader;
214 BT_DataPacket_Header_t DataHeader;
215
216 /* A remote device must be connected before a packet transmission is attempted */
217 if (!(Bluetooth_Connection.IsConnected))
218 return BT_SENDPACKET_NotConnected;
219
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;
223
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;
229
230 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);
231 Pipe_Unfreeze();
232
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);
237 Pipe_ClearOUT();
238
239 Pipe_Freeze();
240
241 BT_ACL_DEBUG(2, "");
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);
247
248 return BT_SENDPACKET_NoError;
249 }
250
251 /** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged.
252 *
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.
257 *
258 * \param[in] PSM PSM of the service that the channel is to be opened for
259 *
260 * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels
261 */
262 Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM)
263 {
264 Bluetooth_Channel_t* ChannelData = NULL;
265
266 /* Search through the channel information list for a free channel item */
267 for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
268 {
269 if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
270 {
271 ChannelData = &Bluetooth_Connection.Channels[i];
272
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);
276 break;
277 }
278 }
279
280 /* If no free channel item was found in the list, all channels are occupied - abort */
281 if (ChannelData == NULL)
282 return NULL;
283
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;
289
290 struct
291 {
292 BT_Signal_Header_t SignalCommandHeader;
293 BT_Signal_ConnectionReq_t ConnectionRequest;
294 } PacketData;
295
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);
300
301 /* Fill out the Connection Request in the response packet */
302 PacketData.ConnectionRequest.PSM = PSM;
303 PacketData.ConnectionRequest.SourceChannel = ChannelData->LocalNumber;
304
305 Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
306
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);
310
311 return ChannelData;
312 }
313
314 /** Closes a bluetooth channel that is open to the currently connected remote device, so that no further data
315 * can be exchanged.
316 *
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.
321 *
322 * \param[in,out] Channel Channel information structure of the channel to close
323 */
324 void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel)
325 {
326 /* Don't try to close a non-existing or already closed channel */
327 if ((Channel == NULL) || (Channel->State == Channel_Closed))
328 return;
329
330 /* Set the channel's state to the start of the teardown process */
331 Channel->State = Channel_WaitDisconnect;
332
333 struct
334 {
335 BT_Signal_Header_t SignalCommandHeader;
336 BT_Signal_DisconnectionReq_t DisconnectionRequest;
337 } PacketData;
338
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);
343
344 /* Fill out the Disconnection Request in the response packet */
345 PacketData.DisconnectionRequest.DestinationChannel = Channel->RemoteNumber;
346 PacketData.DisconnectionRequest.SourceChannel = Channel->LocalNumber;
347
348 Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
349
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);
353 }
354
355 /** Internal Bluetooth stack Signal Command processing routine for a Connection Request command.
356 *
357 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
358 */
359 static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* const SignalCommandHeader)
360 {
361 BT_Signal_ConnectionReq_t ConnectionRequest;
362
363 Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest));
364
365 Pipe_ClearIN();
366 Pipe_Freeze();
367
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);
371
372 _delay_ms(15); // TODO - Determine why this is needed
373
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);
376
377 /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */
378 if (ChannelData == NULL)
379 {
380 /* Look through the channel information list for a free entry */
381 for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
382 {
383 if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
384 {
385 ChannelData = &Bluetooth_Connection.Channels[i];
386
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);
390 break;
391 }
392 }
393 }
394
395 uint8_t ChannelStatus = BT_CONNECTION_REFUSED_RESOURCES;
396
397 /* Reset the channel item contents only if a channel entry was found for it */
398 if (ChannelData != NULL)
399 {
400 /* Check if the user application will allow the connection based on its PSM */
401 if (Bluetooth_ChannelConnectionRequest(ConnectionRequest.PSM))
402 {
403 ChannelData->RemoteNumber = ConnectionRequest.SourceChannel;
404 ChannelData->PSM = ConnectionRequest.PSM;
405 ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
406 ChannelData->State = Channel_Config_WaitConfig;
407
408 ChannelStatus = BT_CONNECTION_SUCCESSFUL;
409 }
410 else
411 {
412 ChannelStatus = BT_CONNECTION_REFUSED_PSM;
413 }
414 }
415
416 struct
417 {
418 BT_Signal_Header_t SignalCommandHeader;
419 BT_Signal_ConnectionResp_t ConnectionResponse;
420 } ResponsePacket;
421
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);
426
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;
432
433 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
434
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);
439 }
440
441 /** Internal Bluetooth stack Signal Command processing routine for a Connection Response command.
442 *
443 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
444 */
445 static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* const SignalCommandHeader)
446 {
447 BT_Signal_ConnectionResp_t ConnectionResponse;
448
449 Pipe_Read_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse));
450
451 Pipe_ClearIN();
452 Pipe_Freeze();
453
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);
458
459 /* Search for the referenced channel in the channel information list */
460 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionResponse.SourceChannel, CHANNEL_SEARCH_LOCALNUMBER);
461
462 /* Only progress if the referenced channel data was found */
463 if (ChannelData != NULL)
464 {
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;
469 }
470 }
471
472 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command.
473 *
474 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
475 */
476 static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* const SignalCommandHeader)
477 {
478 BT_Signal_ConfigurationReq_t ConfigurationRequest;
479
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];
483
484 Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest));
485 Pipe_Read_Stream_LE(&Options, sizeof(Options));
486
487 Pipe_ClearIN();
488 Pipe_Freeze();
489
490 /* Search for the referenced channel in the channel information list */
491 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, CHANNEL_SEARCH_LOCALNUMBER);
492
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);
496
497 /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */
498 if (ChannelData != NULL)
499 {
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)
503 {
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)];
506
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));
509
510 /* Store the remote MTU option's value if present */
511 if (OptionHeader->Type == BT_CONFIG_OPTION_MTU)
512 ChannelData->RemoteMTU = *((uint16_t*)OptionData);
513
514 /* Progress to the next option in the packet */
515 OptionPos += (sizeof(BT_Config_Option_Header_t) + OptionHeader->Length);
516 }
517 }
518
519 struct
520 {
521 BT_Signal_Header_t SignalCommandHeader;
522 BT_Signal_ConfigurationResp_t ConfigurationResponse;
523 } ResponsePacket;
524
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);
529
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;
534
535 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
536
537 if (ChannelData != NULL)
538 {
539 switch (ChannelData->State)
540 {
541 case Channel_Config_WaitConfig:
542 ChannelData->State = Channel_Config_WaitSendConfig;
543 break;
544 case Channel_Config_WaitReqResp:
545 ChannelData->State = Channel_Config_WaitResp;
546 break;
547 case Channel_Config_WaitReq:
548 ChannelData->State = Channel_Open;
549 break;
550 }
551 }
552
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);
556 }
557
558 /** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command.
559 *
560 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
561 */
562 static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t* const SignalCommandHeader)
563 {
564 BT_Signal_ConfigurationResp_t ConfigurationResponse;
565
566 Pipe_Read_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse));
567
568 Pipe_ClearIN();
569 Pipe_Freeze();
570
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);
574
575 /* Search for the referenced channel in the channel information list */
576 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationResponse.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER);
577
578 /* Only update the channel's state if it was found in the channel list */
579 if (ChannelData != NULL)
580 {
581 /* Check if the channel configuration completed successfuly */
582 if (ConfigurationResponse.Result == BT_CONFIGURATION_SUCCESSFUL)
583 {
584 switch (ChannelData->State)
585 {
586 case Channel_Config_WaitReqResp:
587 ChannelData->State = Channel_Config_WaitReq;
588 break;
589 case Channel_Config_WaitResp:
590 ChannelData->State = Channel_Open;
591 break;
592 }
593 }
594 else
595 {
596 /* Configuration failed - close the channel */
597 ChannelData->State = Channel_Closed;
598 }
599 }
600 }
601
602 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command.
603 *
604 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
605 */
606 static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* const SignalCommandHeader)
607 {
608 BT_Signal_DisconnectionReq_t DisconnectionRequest;
609
610 Pipe_Read_Stream_LE(&DisconnectionRequest, sizeof(DisconnectionRequest));
611
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);
615
616 Pipe_ClearIN();
617 Pipe_Freeze();
618
619 /* Search for the referenced channel in the channel information list */
620 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER);
621
622 struct
623 {
624 BT_Signal_Header_t SignalCommandHeader;
625 BT_Signal_DisconnectionResp_t DisconnectionResponse;
626 } ResponsePacket;
627
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);
632
633 /* Fill out the Disconnection Response in the response packet */
634 ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->RemoteNumber;
635 ResponsePacket.DisconnectionResponse.SourceChannel = ChannelData->LocalNumber;
636
637 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
638
639 /* If the channel was found in the channel list, close it */
640 if (ChannelData != NULL)
641 ChannelData->State = Channel_Closed;
642
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);
646 }
647
648 /** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command.
649 *
650 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
651 */
652 static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t* const SignalCommandHeader)
653 {
654 BT_Signal_DisconnectionResp_t DisconnectionResponse;
655
656 Pipe_Read_Stream_LE(&DisconnectionResponse, sizeof(DisconnectionResponse));
657
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);
661
662 Pipe_ClearIN();
663 Pipe_Freeze();
664
665 /* Search for the referenced channel in the channel information list */
666 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionResponse.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER);
667
668 /* If the channel was found in the channel list, close it */
669 if (ChannelData != NULL)
670 ChannelData->State = Channel_Closed;
671 }
672
673 /** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
674 *
675 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
676 */
677 static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t* const SignalCommandHeader)
678 {
679 BT_ACL_DEBUG(1, "<< L2CAP Echo Request");
680
681 Pipe_ClearIN();
682 Pipe_Freeze();
683
684 struct
685 {
686 BT_Signal_Header_t SignalCommandHeader;
687 } ResponsePacket;
688
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;
693
694 Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
695
696 BT_ACL_DEBUG(1, ">> L2CAP Echo Response");
697 }
698
699 /** Internal Bluetooth stack Signal Command processing routine for an Information Request command.
700 *
701 * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header
702 */
703 static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t* const SignalCommandHeader)
704 {
705 BT_Signal_InformationReq_t InformationRequest;
706
707 Pipe_Read_Stream_LE(&InformationRequest, sizeof(InformationRequest));
708
709 BT_ACL_DEBUG(1, "<< L2CAP Information Request");
710 BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest.InfoType);
711
712 Pipe_ClearIN();
713 Pipe_Freeze();
714
715 struct
716 {
717 BT_Signal_Header_t SignalCommandHeader;
718 BT_Signal_InformationResp_t InformationResponse;
719
720 uint8_t Data[4];
721 } ResponsePacket;
722
723 uint8_t DataLen = 0;
724
725 /* Retrieve the requested information and store it in the outgoing packet, if found */
726 switch (InformationRequest.InfoType)
727 {
728 case BT_INFOREQ_MTU:
729 ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;
730 DataLen = 2;
731
732 *((uint16_t*)&ResponsePacket.Data) = MAXIMUM_CHANNEL_MTU;
733 break;
734 case BT_INFOREQ_EXTENDEDFEATURES:
735 ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL;
736 DataLen = 4;
737
738 *((uint32_t*)&ResponsePacket.Data) = 0;
739 break;
740 default:
741 ResponsePacket.InformationResponse.Result = BT_INFORMATION_NOTSUPPORTED;
742 DataLen = 0;
743 break;
744 }
745
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;
750
751 /* Fill out the Information Response in the response packet */
752 ResponsePacket.InformationResponse.InfoType = InformationRequest.InfoType;
753
754 Bluetooth_SendPacket(&ResponsePacket, (sizeof(ResponsePacket) - sizeof(ResponsePacket.Data) + DataLen), NULL);
755
756 BT_ACL_DEBUG(1, ">> L2CAP Information Response");
757 BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.InformationResponse.Result);
758 }