3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  RFCOMM multiplexer control layer module. This module handles multiplexer 
  34  *  channel commands to the control DLCI in the RFCOMM layer, to open, configure, 
  35  *  test and close logical RFCOMM channels. 
  38 #define  INCLUDE_FROM_RFCOMM_CONTROL_C 
  39 #include "RFCOMMControl.h" 
  41 void RFCOMM_ProcessControlCommand(const uint8_t* Command
, 
  42                                   Bluetooth_Channel_t
* const ACLChannel
) 
  44         const RFCOMM_Command_t
* CommandHeader  
= (const RFCOMM_Command_t
*)Command
; 
  45         const uint8_t*          CommandData    
= (const uint8_t*)Command 
+ sizeof(RFCOMM_Command_t
); 
  46         uint8_t                 CommandDataLen 
= RFCOMM_GetVariableFieldValue(&CommandData
); 
  48         switch (CommandHeader
->Command
) 
  50                 case RFCOMM_Control_Test
: 
  51                         RFCOMM_ProcessTestCommand(CommandHeader
, CommandDataLen
, CommandData
, ACLChannel
); 
  53                 case RFCOMM_Control_FlowControlEnable
: 
  54                         RFCOMM_ProcessFCECommand(CommandHeader
, CommandData
, ACLChannel
); 
  56                 case RFCOMM_Control_FlowControlDisable
: 
  57                         RFCOMM_ProcessFCDCommand(CommandHeader
, CommandData
, ACLChannel
); 
  59                 case RFCOMM_Control_ModemStatus
: 
  60                         RFCOMM_ProcessMSCCommand(CommandHeader
, CommandDataLen
, CommandData
, ACLChannel
); 
  62                 case RFCOMM_Control_RemotePortNegotiation
: 
  63                         RFCOMM_ProcessRPNCommand(CommandHeader
, CommandData
, ACLChannel
); 
  65                 case RFCOMM_Control_RemoteLineStatus
: 
  66                         RFCOMM_ProcessRLSCommand(CommandHeader
, CommandData
, ACLChannel
); 
  68                 case RFCOMM_Control_DLCParameterNegotiation
: 
  69                         RFCOMM_ProcessDPNCommand(CommandHeader
, CommandData
, ACLChannel
); 
  72                         BT_RFCOMM_DEBUG(1, "<< Unknown Command"); 
  77 static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t
* const CommandHeader
, 
  78                                       const uint8_t CommandDataLen
, 
  79                                       const uint8_t* CommandData
, 
  80                                       Bluetooth_Channel_t
* const ACLChannel
) 
  82         const uint8_t* Params 
= (const uint8_t*)CommandData
; 
  84         BT_RFCOMM_DEBUG(1, "<< TEST Command"); 
  88                 RFCOMM_Command_t CommandHeader
; 
  90                 uint8_t          TestData
[CommandDataLen
]; 
  93         /* Fill out the Test response data */ 
  94         TestResponse
.CommandHeader 
= (RFCOMM_Command_t
){.Command 
= RFCOMM_Control_Test
, .EA 
= true, .CR 
= false}; 
  95         TestResponse
.Length        
= (CommandDataLen 
<< 1) | 0x01; 
  96         memcpy(TestResponse
.TestData
, Params
, CommandDataLen
); 
  98         BT_RFCOMM_DEBUG(1, ">> TEST Response"); 
 100         /* Send the PDN response to acknowledge the command */ 
 101         RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, sizeof(TestResponse
), &TestResponse
, ACLChannel
); 
 104 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t
* const CommandHeader
, 
 105                                      const uint8_t* CommandData
, 
 106                                                  Bluetooth_Channel_t
* const ACLChannel
) 
 108         BT_RFCOMM_DEBUG(1, "<< FCE Command"); 
 111 static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t
* const CommandHeader
, 
 112                                      const uint8_t* CommandData
, 
 113                                                  Bluetooth_Channel_t
* const ACLChannel
) 
 115         BT_RFCOMM_DEBUG(1, "<< FCD Command"); 
 118 static void RFCOMM_ProcessMSCCommand(const RFCOMM_Command_t
* const CommandHeader
, 
 119                                      const uint8_t CommandDataLen
, 
 120                                      const uint8_t* CommandData
, 
 121                                      Bluetooth_Channel_t
* const ACLChannel
) 
 123         const RFCOMM_MSC_Parameters_t
* Params 
= (const RFCOMM_MSC_Parameters_t
*)CommandData
; 
 125         BT_RFCOMM_DEBUG(1, "<< MSC %s", (CommandHeader
->CR
) ? 
"Command" : "Response"); 
 126         BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params
->Channel
.DLCI
); 
 128         /* Ignore status flags sent to the control channel */ 
 129         if (Params
->Channel
.DLCI 
== RFCOMM_CONTROL_DLCI
) 
 132         /* Retrieve existing channel configuration data, if already opened */ 
 133         RFCOMM_Channel_t
* RFCOMMChannel 
= RFCOMM_GetChannelData(Params
->Channel
.DLCI
); 
 135         /* If the channel does not exist, abort */ 
 136         if (RFCOMMChannel 
== NULL
) 
 139         /* Check if the MSC packet is a command or a response */ 
 140         if (CommandHeader
->CR
) 
 142                 /* Save the new channel signals to the channel state structure */ 
 143                 RFCOMMChannel
->Remote
.Signals  
= Params
->Signals
; 
 144                 RFCOMMChannel
->ConfigFlags    
|= RFCOMM_CONFIG_REMOTESIGNALS
; 
 146                 /* If the command contains the optional break signals field, store the value */ 
 147                 if (CommandDataLen 
== sizeof(RFCOMM_MSC_Parameters_t
)) 
 148                   RFCOMMChannel
->Remote
.BreakSignal 
= Params
->BreakSignal
; 
 150                 /* Notify the user application that the signals have been received */ 
 151                 RFCOMM_ChannelSignalsReceived(RFCOMMChannel
); 
 155                         RFCOMM_Command_t        CommandHeader
; 
 157                         RFCOMM_MSC_Parameters_t Params
; 
 160                 /* Fill out the MS response data */ 
 161                 MSResponse
.CommandHeader  
= (RFCOMM_Command_t
){.Command 
= RFCOMM_Control_ModemStatus
, .EA 
= true, .CR 
= false}; 
 162                 MSResponse
.Length         
= (CommandDataLen 
<< 1) | 0x01; 
 163                 memcpy(&MSResponse
.Params
, Params
, sizeof(RFCOMM_MSC_Parameters_t
)); 
 165                 BT_RFCOMM_DEBUG(1, ">> MSC Response"); 
 167                 /* Send the MSC response to acknowledge the command */ 
 168                 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, 
 169                                                  (sizeof(MSResponse
) - sizeof(MSResponse
.Params
) + CommandDataLen
), &MSResponse
, ACLChannel
); 
 173                 /* Indicate that the remote device has acknowledged the sent signals */ 
 174                 RFCOMMChannel
->ConfigFlags 
|= RFCOMM_CONFIG_LOCALSIGNALS
; 
 178 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t
* const CommandHeader
, 
 179                                      const uint8_t* CommandData
, 
 180                                                  Bluetooth_Channel_t
* const ACLChannel
) 
 182         BT_RFCOMM_DEBUG(1, "<< RPN Command"); 
 185 static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t
* const CommandHeader
, 
 186                                      const uint8_t* CommandData
, 
 187                                                  Bluetooth_Channel_t
* const ACLChannel
) 
 189         BT_RFCOMM_DEBUG(1, "<< RLS Command"); 
 192 static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t
* const CommandHeader
, 
 193                                      const uint8_t* CommandData
, 
 194                                                  Bluetooth_Channel_t
* const ACLChannel
) 
 196         const RFCOMM_DPN_Parameters_t
* Params 
= (const RFCOMM_DPN_Parameters_t
*)CommandData
; 
 198         BT_RFCOMM_DEBUG(1, "<< DPN Command"); 
 199         BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params
->DLCI
); 
 201         /* Ignore parameter negotiations to the control channel */ 
 202         if (Params
->DLCI 
== RFCOMM_CONTROL_DLCI
) 
 205         /* Retrieve existing channel configuration data, if already opened */ 
 206         RFCOMM_Channel_t
* RFCOMMChannel 
= RFCOMM_GetChannelData(Params
->DLCI
); 
 208         /* Check if the channel has no corresponding entry - remote did not open it first */ 
 209         if (RFCOMMChannel 
== NULL
) 
 211                 /* Create a new entry in the channel table for the new channel */ 
 212                 RFCOMMChannel 
= RFCOMM_GetFreeChannelEntry(Params
->DLCI
); 
 214                 /* No free entry was found, discard the request */ 
 215                 if (RFCOMMChannel 
== NULL
) 
 217                         BT_RFCOMM_DEBUG(2, "-- No Free Channel"); 
 222         /* Save the new channel configuration */ 
 223         RFCOMMChannel
->State       
= RFCOMM_Channel_Configure
; 
 224         RFCOMMChannel
->Priority    
= Params
->Priority
; 
 225         RFCOMMChannel
->MTU         
= Params
->MaximumFrameSize
; 
 229                 RFCOMM_Command_t        CommandHeader
; 
 231                 RFCOMM_DPN_Parameters_t Params
; 
 234         /* Fill out the DPN response data */ 
 235         DPNResponse
.CommandHeader 
= (RFCOMM_Command_t
){.Command 
= RFCOMM_Control_DLCParameterNegotiation
, .EA 
= true, .CR 
= false}; 
 236         DPNResponse
.Length                  
= (sizeof(DPNResponse
.Params
) << 1) | 0x01; 
 237         memcpy(&DPNResponse
.Params
, Params
, sizeof(RFCOMM_DPN_Parameters_t
)); 
 238         DPNResponse
.Params
.ConvergenceLayer 
= 0x00; // TODO: Enable credit based transaction support 
 240         BT_RFCOMM_DEBUG(1, ">> DPN Response"); 
 242         /* Send the DPN response to acknowledge the command */ 
 243         RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, sizeof(DPNResponse
), &DPNResponse
, ACLChannel
);