3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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
, Bluetooth_Channel_t
* const Channel
) 
  43         const RFCOMM_Command_t
* CommandHeader  
= (const RFCOMM_Command_t
*)Command
; 
  44         const uint8_t*          CommandData    
= (const uint8_t*)Command 
+ sizeof(RFCOMM_Command_t
); 
  45         uint8_t                 CommandDataLen 
= RFCOMM_GetVariableFieldValue(&CommandData
); 
  47         switch (CommandHeader
->Command
) 
  49                 case RFCOMM_Control_Test
: 
  50                         RFCOMM_ProcessTestCommand(CommandHeader
, CommandData
, Channel
); 
  52                 case RFCOMM_Control_FlowControlEnable
: 
  53                         RFCOMM_ProcessFCECommand(CommandHeader
, CommandData
, Channel
); 
  55                 case RFCOMM_Control_FlowControlDisable
: 
  56                         RFCOMM_ProcessFCDCommand(CommandHeader
, CommandData
, Channel
); 
  58                 case RFCOMM_Control_ModemStatus
: 
  59                         RFCOMM_ProcessMSCommand(CommandHeader
, CommandDataLen
, CommandData
, Channel
); 
  61                 case RFCOMM_Control_RemotePortNegotiation
: 
  62                         RFCOMM_ProcessRPNCommand(CommandHeader
, CommandData
, Channel
); 
  64                 case RFCOMM_Control_RemoteLineStatus
: 
  65                         RFCOMM_ProcessRLSCommand(CommandHeader
, CommandData
, Channel
); 
  67                 case RFCOMM_Control_DLCParameterNegotiation
: 
  68                         RFCOMM_ProcessDPNCommand(CommandHeader
, CommandData
, Channel
); 
  71                         BT_RFCOMM_DEBUG(1, "<< Unknown Command");                
  76 static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
, 
  77                                                   Bluetooth_Channel_t
* const Channel
) 
  79         BT_RFCOMM_DEBUG(1, "<< TEST Command"); 
  82 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
, 
  83                                                  Bluetooth_Channel_t
* const Channel
) 
  85         BT_RFCOMM_DEBUG(1, "<< FCE Command"); 
  88 static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
, 
  89                                                  Bluetooth_Channel_t
* const Channel
) 
  91         BT_RFCOMM_DEBUG(1, "<< FCD Command"); 
  94 static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t CommandDataLen
, 
  95                                     const uint8_t* CommandData
, Bluetooth_Channel_t
* const Channel
) 
  97         const RFCOMM_MS_Parameters_t
* Params 
= (const RFCOMM_MS_Parameters_t
*)CommandData
; 
  99         BT_RFCOMM_DEBUG(1, "<< MS Command"); 
 100         BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params
->Channel
.DLCI
); 
 102         /* Ignore status flags sent to the control channel */ 
 103         if (Params
->Channel
.DLCI 
== RFCOMM_CONTROL_DLCI
) 
 106         /* Retrieve existing channel configuration data, if already opened */ 
 107         RFCOMM_Channel_t
* RFCOMMChannel 
= RFCOMM_GetChannelData(Params
->Channel
.DLCI
);   
 109         /* If the channel does not exist, abort */ 
 110         if (RFCOMMChannel 
== NULL
) 
 113         /* Save the new channel signals to the channel state structure */ 
 114         RFCOMMChannel
->Signals 
= Params
->Signals
; 
 116         /* If the command contains the optional break signals field, store the value */ 
 117         if (CommandDataLen 
== sizeof(RFCOMM_MS_Parameters_t
)) 
 118           RFCOMMChannel
->BreakSignals 
= Params
->BreakSignals
; 
 122                 RFCOMM_Command_t       CommandHeader
; 
 124                 RFCOMM_MS_Parameters_t Params
; 
 127         /* Fill out the MS response data */ 
 128         MSResponse
.CommandHeader 
= (RFCOMM_Command_t
){.Command 
= RFCOMM_Control_ModemStatus
, .EA 
= true}; 
 129         MSResponse
.Length        
= (CommandDataLen 
<< 1) | 0x01; 
 130         MSResponse
.Params        
= *Params
; 
 132         BT_RFCOMM_DEBUG(1, ">> MS Response"); 
 134         /* Send the PDN response to acknowledge the command */ 
 135         RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, sizeof(MSResponse
), &MSResponse
, Channel
); 
 138 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
, 
 139                                                  Bluetooth_Channel_t
* const Channel
) 
 141         BT_RFCOMM_DEBUG(1, "<< RPN Command"); 
 144 static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
, 
 145                                                  Bluetooth_Channel_t
* const Channel
) 
 147         BT_RFCOMM_DEBUG(1, "<< RLS Command"); 
 150 static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
, 
 151                                                  Bluetooth_Channel_t
* const Channel
) 
 153         const RFCOMM_DPN_Parameters_t
* Params 
= (const RFCOMM_DPN_Parameters_t
*)CommandData
; 
 155         BT_RFCOMM_DEBUG(1, "<< DPN Command"); 
 156         BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params
->DLCI
); 
 158         /* Ignore parameter negotiations to the control channel */ 
 159         if (Params
->DLCI 
== RFCOMM_CONTROL_DLCI
) 
 162         /* Retrieve existing channel configuration data, if already opened */ 
 163         RFCOMM_Channel_t
* RFCOMMChannel 
= RFCOMM_GetChannelData(Params
->DLCI
); 
 165         /* Check if the channel has no corresponding entry - remote did not open it first */ 
 166         if (RFCOMMChannel 
== NULL
) 
 168                 /* Find a free entry in the RFCOMM channel multiplexer state array */ 
 169                 for (uint8_t i 
= 0; i 
< RFCOMM_MAX_OPEN_CHANNELS
; i
++) 
 171                         /* If the channel's DLCI is zero, the channel state entry is free */ 
 172                         if (!(RFCOMM_Channels
[i
].DLCI
)) 
 174                                 RFCOMMChannel               
= &RFCOMM_Channels
[i
]; 
 175                                 RFCOMMChannel
->DLCI         
= Params
->DLCI
; 
 176                                 RFCOMMChannel
->Signals      
= 0; 
 177                                 RFCOMMChannel
->BreakSignals 
= 0; 
 182                 /* No free entry was found, discard the request */ 
 183                 if (RFCOMMChannel 
== NULL
) 
 185                         BT_RFCOMM_DEBUG(2, "-- No Free Channel"); 
 190         /* Save the new channel configuration */ 
 191         RFCOMMChannel
->State       
= RFCOMM_Channel_Open
; 
 192         RFCOMMChannel
->Priority    
= Params
->Priority
; 
 193         RFCOMMChannel
->UseUIFrames 
= (Params
->FrameType 
!= 0); 
 194         RFCOMMChannel
->MTU         
= Params
->MaximumFrameSize
; 
 198                 RFCOMM_Command_t        CommandHeader
; 
 200                 RFCOMM_DPN_Parameters_t Params
; 
 203         /* Fill out the DPN response data */ 
 204         DPNResponse
.CommandHeader           
= (RFCOMM_Command_t
){.Command 
= RFCOMM_Control_DLCParameterNegotiation
, .EA 
= true}; 
 205         DPNResponse
.Length                  
= (sizeof(DPNResponse
.Params
) << 1) | 0x01; 
 206         DPNResponse
.Params                  
= *Params
; 
 207         DPNResponse
.Params
.ConvergenceLayer 
= 0x00; // TODO: Enable credit based transaction support 
 209         BT_RFCOMM_DEBUG(1, ">> DPN Response"); 
 211         /* Send the PDN response to acknowledge the command */ 
 212         RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, sizeof(DPNResponse
), &DPNResponse
, Channel
);