Home | History | Annotate | Download | only in rfcomm
      1 /******************************************************************************
      2  *
      3  *  Copyright 2018 The Android Open Source Project
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 #pragma once
     19 
     20 #include <vector>
     21 
     22 #include <gmock/gmock.h>
     23 
     24 #include "bt_types.h"
     25 
     26 namespace bluetooth {
     27 namespace rfcomm {
     28 
     29 class RfcommCallback {
     30  public:
     31   virtual void PortManagementCallback(uint32_t code, uint16_t port_handle,
     32                                       uint16_t callback_index) = 0;
     33   virtual void PortEventCallback(uint32_t code, uint16_t port_handle,
     34                                  uint16_t callback_index) = 0;
     35   virtual ~RfcommCallback() = default;
     36 };
     37 
     38 class MockRfcommCallback : public RfcommCallback {
     39  public:
     40   MOCK_METHOD3(PortManagementCallback, void(uint32_t code, uint16_t port_handle,
     41                                             uint16_t callback_index));
     42   MOCK_METHOD3(PortEventCallback, void(uint32_t code, uint16_t port_handle,
     43                                        uint16_t callback_index));
     44 };
     45 
     46 /**
     47  * Create DLCI using direction of service channel number
     48  *
     49  * @param on_originator_side is this a channel on initiator side
     50  * @param scn service channel number
     51  * @return DLCI
     52  */
     53 uint8_t GetDlci(bool on_originator_side, uint8_t scn);
     54 
     55 /**
     56  * Create address field in RFCOMM packet
     57  *
     58  * @param ea end of field byte, true if field only has 1 byte, false otherwise
     59  * @param cr command and response bit, true if packet is from initiator,
     60  *           false otherwise, not actual "command" and "response"
     61  * @param dlci DLCI of this pcaket, RFCOMM_MX_DLCI for multiplexer control
     62  * @return address field
     63  */
     64 uint8_t GetAddressField(bool ea, bool cr, uint8_t dlci);
     65 
     66 /**
     67  * Create control field in RFCOMM packet
     68  *
     69  * @param pf Poll/Finish bit, normally 1 for multiplexer control channel
     70  * @param frame_type frame type
     71  * @return control field
     72  */
     73 uint8_t GetControlField(bool pf, uint8_t frame_type);
     74 
     75 /**
     76  * Create Multiplexer control channel parameter negotiation frame
     77  *
     78  * @param dlci DLCI to be configured
     79  * @param i_bits i bits
     80  * @param cl_bits cl bits
     81  * @param priority priority
     82  * @param timer_value timer value
     83  * @param rfcomm_mtu rfcomm mtu
     84  * @param max_num_retransmission maximum number of retransmission
     85  * @param err_recovery_window_k error recovery window k
     86  * @return vector of bytes of this frame
     87  */
     88 std::vector<uint8_t> CreateMccPnFrame(uint8_t dlci, uint8_t i_bits,
     89                                       uint8_t cl_bits, uint8_t priority,
     90                                       uint8_t timer_value, uint16_t rfcomm_mtu,
     91                                       uint8_t max_num_retransmission,
     92                                       uint8_t err_recovery_window_k);
     93 /**
     94  * Create Multiplexer Control Modem Status Configuration Frame
     95  *
     96  * @param dlci DLCI to be configured
     97  * @param fc flow control
     98  * @param rtc ready to communicate
     99  * @param rtr ready to receive
    100  * @param ic incoming call indicator
    101  * @param dv is data valid
    102  * @return vector of bytes
    103  */
    104 std::vector<uint8_t> CreateMccMscFrame(uint8_t dlci, bool fc, bool rtc,
    105                                        bool rtr, bool ic, bool dv);
    106 
    107 /**
    108  * Create Multiplexer Control Frame
    109  *
    110  * @param command_type type of command
    111  * @param cr command or response flag, true when this is a command, false when
    112  *           this is a response, regardless of connection direction
    113  * @param data frame data
    114  * @return vector of bytes
    115  */
    116 std::vector<uint8_t> CreateMultiplexerControlFrame(
    117     uint8_t command_type, bool cr, const std::vector<uint8_t>& data);
    118 
    119 /**
    120  * Create a general RFCOMM packet
    121  *
    122  * @param address address byte
    123  * @param control control byte
    124  * @param credits number of credits, <= 0 will cause this to be ignored
    125  * @param data frame data
    126  * @return vector of bytes
    127  */
    128 std::vector<uint8_t> CreateRfcommPacket(uint8_t address, uint8_t control,
    129                                         int credits,
    130                                         const std::vector<uint8_t>& data);
    131 /*
    132  * Various shortcut for getting frequently used packets
    133  */
    134 
    135 /**
    136  * Create SABM packet that is used to connect to a service channel number in a
    137  * multiplexer
    138  *
    139  * @param dlci DLCI to be connected
    140  * @param l2cap_lcid L2CAP channel ID
    141  * @param acl_handle ACL handle
    142  * @return vector of bytes of unwrapped ACL packet
    143  */
    144 std::vector<uint8_t> CreateQuickSabmPacket(uint8_t dlci, uint16_t l2cap_lcid,
    145                                            uint16_t acl_handle);
    146 
    147 /**
    148  * Create UA packet that is used to acknowledge service channel connection
    149  *
    150  * @param dlci DLCI to be connected
    151  * @param l2cap_lcid L2CAP channel ID
    152  * @param acl_handle ACL handle
    153  * @return vector of bytes of unwrapped ACL packet
    154  */
    155 std::vector<uint8_t> CreateQuickUaPacket(uint8_t dlci, uint16_t l2cap_lcid,
    156                                          uint16_t acl_handle);
    157 
    158 /**
    159  * Create parameter negotiation packet used to setup parameters for a DLCI
    160  *
    161  * @param rfc_cr RFCOMM command/response bit, true of initiator
    162  * @param target_dlci DLCI to be configured
    163  * @param mx_cr multiplexer command or reponse, regardless of initiator
    164  * @param rfc_mtu RFCOMM mtu to be used for DLCI
    165  * @param cl CL bit
    166  * @param priority prirority
    167  * @param k error recovery window k
    168  * @param l2cap_lcid L2CAP channel ID
    169  * @param acl_handle ACL handle
    170  * @return vector of bytes of unwrapped ACL packet
    171  */
    172 std::vector<uint8_t> CreateQuickPnPacket(bool rfc_cr, uint8_t target_dlci,
    173                                          bool mx_cr, uint16_t rfc_mtu,
    174                                          uint8_t cl, uint8_t priority,
    175                                          uint8_t k, uint16_t l2cap_lcid,
    176                                          uint16_t acl_handle);
    177 
    178 /**
    179  * Create modem signal control packet
    180  *
    181  * @param rfc_cr RFCOMM command/response bit, true of initiator
    182  * @param dlci DLCI to be configured
    183  * @param l2cap_lcid L2CAP channel ID
    184  * @param acl_handle ACL handle
    185  * @param mx_cr multiplexer command or reponse, regardless of initiator
    186  * @param fc flow control
    187  * @param rtc ready to communicate
    188  * @param rtr ready to receive
    189  * @param ic incoming call indicator
    190  * @param dv data valid
    191  * @return vector of bytes of unwrapped ACL packet
    192  */
    193 std::vector<uint8_t> CreateQuickMscPacket(bool rfc_cr, uint8_t dlci,
    194                                           uint16_t l2cap_lcid,
    195                                           uint16_t acl_handle, bool mx_cr,
    196                                           bool fc, bool rtc, bool rtr, bool ic,
    197                                           bool dv);
    198 
    199 /**
    200  * Create a quick RFCOMM data packet
    201  *
    202  * @param dlci DLCI of this packet
    203  * @param cr command or control, true for initiator
    204  * @param l2cap_lcid L2CAP channel ID
    205  * @param acl_handle ACL handle
    206  * @param credits number of credits
    207  * @param data data bytes
    208  * @return vector of bytes of unwrapped ACL packet
    209  */
    210 std::vector<uint8_t> CreateQuickDataPacket(uint8_t dlci, bool cr,
    211                                            uint16_t l2cap_lcid,
    212                                            uint16_t acl_handle, int credits,
    213                                            const std::vector<uint8_t>& data);
    214 
    215 /**
    216  * Create a quick RFCOMM data packet
    217  *
    218  * @param dlci DLCI of this packet
    219  * @param cr command or control, true for initiator
    220  * @param l2cap_lcid L2CAP channel ID
    221  * @param acl_handle ACL handle
    222  * @param credits number of credits
    223  * @param str message in string format
    224  * @return vector of bytes of unwrapped ACL packet
    225  */
    226 std::vector<uint8_t> CreateQuickDataPacket(uint8_t dlci, bool cr,
    227                                            uint16_t l2cap_lcid,
    228                                            uint16_t acl_handle, int credits,
    229                                            const std::string& str);
    230 
    231 }  // namespace rfcomm
    232 }  // namespace bluetooth