3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  31 #define  INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C 
  32 #include "BluetoothACLPackets.h" 
  34 void Bluetooth_ProcessACLPackets(void) 
  36         Bluetooth_ACL_Header_t ACLPacketHeader
; 
  37         Bluetooth_DataPacket_Header_t DataHeader
; 
  39         Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE
); 
  40         Pipe_SetToken(PIPE_TOKEN_IN
); 
  43         if (!(Pipe_IsReadWriteAllowed())) 
  49         Pipe_Read_Stream_LE(&ACLPacketHeader
, sizeof(ACLPacketHeader
)); 
  50         Pipe_Read_Stream_LE(&DataHeader
, sizeof(DataHeader
)); 
  52         BT_DEBUG("(ACL) Packet Received", NULL
); 
  53         BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader
.ConnectionHandle
); 
  54         BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader
.DataLength
); 
  55         BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader
.DestinationChannel
); 
  56         BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader
.PayloadLength
); 
  58         if (DataHeader
.DestinationChannel 
== BLUETOOTH_CHANNEL_SIGNALING
) 
  60                 Bluetooth_SignalCommand_Header_t SignalCommandHeader
; 
  61                 Pipe_Read_Stream_LE(&SignalCommandHeader
, sizeof(SignalCommandHeader
)); 
  63                 switch (SignalCommandHeader
.Code
) 
  65                         case BLUETOOTH_SIGNAL_CONNECTION_REQUEST
: 
  66                                 Bluetooth_ProcessSignalPacket_ConnectionRequest(&ACLPacketHeader
, &DataHeader
, &SignalCommandHeader
); 
  68                         case BLUETOOTH_SIGNAL_CONFIGURATION_REQUEST
: 
  69                                 Bluetooth_ProcessSignalPacket_ConfigurationRequest(&ACLPacketHeader
, &DataHeader
, &SignalCommandHeader
); 
  71                         case BLUETOOTH_SIGNAL_INFORMATION_REQUEST
: 
  72                                 BT_DEBUG("(ACL) -- Information Request, Discarded", NULL
); 
  74                                 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
); 
  79                                 BT_DEBUG("(ACL) >> Unknown Signaling Command 0x%02X", SignalCommandHeader
.Code
); 
  81                                 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
); 
  89                 uint8_t DataPayload
[DataHeader
.PayloadLength
]; 
  90                 Pipe_Read_Stream_LE(&DataPayload
, sizeof(DataPayload
)); 
  91                 DataHeader
.PayloadLength 
= 0; 
  93                 BT_DEBUG("(ACL) -- Data Payload: ", NULL
); 
  94                 for (uint16_t B 
= 0; B 
< sizeof(DataPayload
); B
++) 
  95                   printf("0x%02X ", DataPayload
[B
]); 
  98                 Pipe_Discard_Stream(ACLPacketHeader
.DataLength
); 
 104 static inline void Bluetooth_ProcessSignalPacket_ConnectionRequest(Bluetooth_ACL_Header_t
* ACLPacketHeader
, 
 105                                                                    Bluetooth_DataPacket_Header_t
* DataHeader
, 
 106                                                                    Bluetooth_SignalCommand_Header_t
* SignalCommandHeader
) 
 108         Bluetooth_SignalCommand_ConnectionRequest_t ConnectionRequest
; 
 110         Pipe_Read_Stream_LE(&ConnectionRequest
, sizeof(ConnectionRequest
)); 
 112         BT_DEBUG("(ACL) >> L2CAP Connection Request", NULL
); 
 113         BT_DEBUG("(ACL) -- PSM: 0x%04X", ConnectionRequest
.PSM
); 
 114         BT_DEBUG("(ACL) -- Source Channel: 0x%04X", ConnectionRequest
.SourceChannel
); 
 118         Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE
); 
 119         Pipe_SetToken(PIPE_TOKEN_OUT
); 
 122         Bluetooth_SignalCommand_ConnectionResponse_t ConnectionResponse
; 
 124         ACLPacketHeader
->DataLength           
= sizeof(*DataHeader
) + sizeof(*SignalCommandHeader
) + sizeof(ConnectionResponse
); 
 125         DataHeader
->PayloadLength             
= sizeof(*SignalCommandHeader
) + sizeof(ConnectionResponse
); 
 126         DataHeader
->DestinationChannel        
= BLUETOOTH_CHANNEL_SIGNALING
; 
 127         SignalCommandHeader
->Code             
= BLUETOOTH_SIGNAL_CONNECTION_RESPONSE
; 
 128         SignalCommandHeader
->Length           
= sizeof(ConnectionResponse
); 
 130         Bluetooth_Channel_t
* ChannelData      
= Bluetooth_InitChannelData(ConnectionRequest
.SourceChannel
, ConnectionRequest
.PSM
); 
 132         ConnectionResponse
.Result             
= (ChannelData 
== NULL
) ? BLUETOOTH_CONNECTION_REFUSED_RESOURCES 
: 
 133                                                                                                                                     BLUETOOTH_CONNECTION_SUCCESSFUL
; 
 134         ConnectionResponse
.DestinationChannel 
= ChannelData
->LocalNumber
; 
 135         ConnectionResponse
.SourceChannel      
= ChannelData
->RemoteNumber
; 
 136         ConnectionResponse
.Status             
= 0x00; 
 138         Pipe_Write_Stream_LE(ACLPacketHeader
, sizeof(*ACLPacketHeader
)); 
 139         Pipe_Write_Stream_LE(DataHeader
, sizeof(*DataHeader
)); 
 140         Pipe_Write_Stream_LE(SignalCommandHeader
, sizeof(*SignalCommandHeader
)); 
 141         Pipe_Write_Stream_LE(&ConnectionResponse
, sizeof(ConnectionResponse
)); 
 146         BT_DEBUG("(ACL) Packet Sent", NULL
); 
 147         BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader
->ConnectionHandle
); 
 148         BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader
->DataLength
); 
 149         BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader
->DestinationChannel
); 
 150         BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader
->PayloadLength
);                  
 151         BT_DEBUG("(ACL) >> L2CAP Connection Response", NULL
); 
 152         BT_DEBUG("(ACL) -- Source Channel: 0x%04X", ConnectionResponse
.SourceChannel
); 
 153         BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", ConnectionResponse
.DestinationChannel
); 
 156 static inline void Bluetooth_ProcessSignalPacket_ConfigurationRequest(Bluetooth_ACL_Header_t
* ACLPacketHeader
, 
 157                                                                       Bluetooth_DataPacket_Header_t
* DataHeader
, 
 158                                                                       Bluetooth_SignalCommand_Header_t
* SignalCommandHeader
) 
 160         Bluetooth_SignalCommand_ConfigurationRequest_t ConfigurationRequest
; 
 162         Pipe_Read_Stream_LE(&ConfigurationRequest
, sizeof(ConfigurationRequest
)); 
 164         BT_DEBUG("(ACL) >> L2CAP Configuration Request", NULL
); 
 165         BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", ConfigurationRequest
.DestinationChannel
); 
 169         Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE
); 
 170         Pipe_SetToken(PIPE_TOKEN_OUT
); 
 173         Bluetooth_SignalCommand_ConfigurationResponse_t ConfigurationResponse
; 
 175         ACLPacketHeader
->DataLength           
= sizeof(*DataHeader
) + sizeof(*SignalCommandHeader
) + sizeof(ConfigurationResponse
); 
 176         DataHeader
->PayloadLength             
= sizeof(*SignalCommandHeader
) + sizeof(ConfigurationResponse
); 
 177         DataHeader
->DestinationChannel        
= BLUETOOTH_CHANNEL_SIGNALING
; 
 178         SignalCommandHeader
->Code             
= BLUETOOTH_SIGNAL_CONFIGURATION_RESPONSE
; 
 179         SignalCommandHeader
->Length           
= sizeof(ConfigurationResponse
); 
 181         Bluetooth_Channel_t
* ChannelData      
= Bluetooth_GetChannelData(ConfigurationRequest
.DestinationChannel
, CHANNEL_LOOKUP_BY_DESTINATION
); 
 183         if (ChannelData 
!= NULL
) 
 184           ChannelData
->State 
= Channel_Open
; 
 186          // TODO: Add channel config data to the tail of ConfigurationResponse 
 188         ConfigurationResponse
.SourceChannel   
= ChannelData
->RemoteNumber
; 
 189         ConfigurationResponse
.Flags           
= 0x00; 
 190         ConfigurationResponse
.Result          
= (ChannelData 
!= NULL
) ? BLUETOOTH_CONFIGURATION_SUCCESSFUL 
: BLUETOOTH_CONFIGURATION_REJECTED
; 
 192         Pipe_Write_Stream_LE(ACLPacketHeader
, sizeof(*ACLPacketHeader
)); 
 193         Pipe_Write_Stream_LE(DataHeader
, sizeof(*DataHeader
)); 
 194         Pipe_Write_Stream_LE(SignalCommandHeader
, sizeof(*SignalCommandHeader
)); 
 195         Pipe_Write_Stream_LE(&ConfigurationResponse
, sizeof(ConfigurationResponse
)); 
 200         BT_DEBUG("(ACL) Packet Sent", NULL
); 
 201         BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader
->ConnectionHandle
); 
 202         BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader
->DataLength
); 
 203         BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader
->DestinationChannel
); 
 204         BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader
->PayloadLength
);                  
 205         BT_DEBUG("(ACL) >> L2CAP Configuration Response", NULL
);