summaryrefslogtreecommitdiff
path: root/system/stack/test/rfcomm/stack_rfcomm_test_utils.cc
blob: fa0f35079bc3d7a068ad1f42381266bc41cc6dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/******************************************************************************
 *
 *  Copyright 2018 The Android Open Source Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include <bitset>
#include <string>
#include <vector>

#include "stack/rfcomm/rfc_int.h"
#include "stack_rfcomm_test_utils.h"
#include "stack_test_packet_utils.h"

namespace bluetooth {
namespace rfcomm {

uint8_t GetDlci(bool on_originator_side, uint8_t scn) {
  return static_cast<uint8_t>((scn << 1) + (on_originator_side ? 1 : 0));
}

uint8_t GetAddressField(bool ea, bool cr, uint8_t dlci) {
  std::bitset<8> address;
  address.set(0, ea);
  // For UIH frame, cr for initiating device is 1, otherwise 0
  // Otherwise:
  //  Command: Initiator -> Responder: 1
  //  Command: Responder -> Initiator 0
  //  Response: Initiator -> Responder 0
  //  Response: Responder -> Initiator 1
  // Initiator is defined by the one who send SABM=1 command
  address.set(1, cr);
  address |= dlci << 2;
  return static_cast<uint8_t>(address.to_ulong());
}

uint8_t GetControlField(bool pf, uint8_t frame_type) {
  std::bitset<8> control;
  control |= frame_type;
  control.set(4, pf);
  return static_cast<uint8_t>(control.to_ulong());
}

uint8_t GetFrameTypeFromControlField(uint8_t control_field) {
  return static_cast<uint8_t>(control_field & ~(0b10000));
}

std::vector<uint8_t> CreateMccPnFrame(uint8_t dlci, uint8_t i_bits,
                                      uint8_t cl_bits, uint8_t priority,
                                      uint8_t timer_value, uint16_t rfcomm_mtu,
                                      uint8_t max_num_retransmission,
                                      uint8_t err_recovery_window_k) {
  // Data in little endian order
  std::vector<uint8_t> result;
  result.push_back(static_cast<uint8_t>(dlci & 0b00111111));
  result.push_back(static_cast<uint8_t>((cl_bits << 4) | (i_bits & 0x0F)));
  result.push_back(static_cast<uint8_t>(priority & 0b00111111));
  result.push_back(timer_value);
  result.push_back(static_cast<uint8_t>(rfcomm_mtu));
  result.push_back(static_cast<uint8_t>(rfcomm_mtu >> 8));
  result.push_back(max_num_retransmission);
  result.push_back(static_cast<uint8_t>(err_recovery_window_k & 0b111));
  return result;
}

std::vector<uint8_t> CreateMccMscFrame(uint8_t dlci, bool fc, bool rtc,
                                       bool rtr, bool ic, bool dv) {
  // Data in little endian order
  std::vector<uint8_t> result;
  result.push_back(static_cast<uint8_t>((dlci << 2) | 0b11));
  std::bitset<8> v24_signals;
  // EA = 1, single byte
  v24_signals.set(0, true);
  v24_signals.set(1, fc);
  v24_signals.set(2, rtc);
  v24_signals.set(3, rtr);
  v24_signals.set(6, ic);
  v24_signals.set(7, dv);
  result.push_back(static_cast<uint8_t>(v24_signals.to_ulong()));
  return result;
}

std::vector<uint8_t> CreateMultiplexerControlFrame(
    uint8_t command_type, bool cr, const std::vector<uint8_t>& data) {
  // Data in little endian order
  std::vector<uint8_t> result;
  std::bitset<8> header;
  header.set(0, true);  // EA is always 1
  header.set(1, cr);
  header |= command_type << 2;
  result.push_back(static_cast<uint8_t>(header.to_ulong()));
  // 7 bit length + EA(1)
  result.push_back(static_cast<uint8_t>((data.size() << 1) + 1));
  result.insert(result.end(), data.begin(), data.end());
  return result;
}

std::vector<uint8_t> CreateRfcommPacket(uint8_t address, uint8_t control,
                                        int credits,
                                        const std::vector<uint8_t>& data) {
  // Data in little endian order
  std::vector<uint8_t> result;
  result.push_back(address);
  result.push_back(control);
  size_t length = data.size();
  if ((length & 0b1000000) != 0) {
    // 15 bits of length in little endian order + EA(0)
    // Lower 7 bits + EA(0)
    result.push_back(static_cast<uint8_t>(length) << 1);
    // Upper 8 bits
    result.push_back(static_cast<uint8_t>(length >> 8));
  } else {
    // 7 bits of length + EA(1)
    result.push_back(static_cast<uint8_t>((length << 1) + 1));
  }
  if (credits > 0) {
    result.push_back(static_cast<uint8_t>(credits));
  }
  result.insert(result.end(), data.begin(), data.end());
  if (GetFrameTypeFromControlField(control) == RFCOMM_UIH) {
    result.push_back(rfc_calc_fcs(2, result.data()));
  } else {
    result.push_back(
        rfc_calc_fcs(static_cast<uint16_t>(result.size()), result.data()));
  }
  return result;
}

std::vector<uint8_t> CreateQuickUaPacket(uint8_t dlci, uint16_t l2cap_lcid,
                                         uint16_t acl_handle) {
  uint8_t address_field = GetAddressField(true, true, dlci);
  uint8_t control_field = GetControlField(true, RFCOMM_UA);
  std::vector<uint8_t> rfcomm_packet =
      CreateRfcommPacket(address_field, control_field, -1, {});
  std::vector<uint8_t> l2cap_packet =
      CreateL2capDataPacket(l2cap_lcid, rfcomm_packet);
  return CreateAclPacket(acl_handle, 0b10, 0b00, l2cap_packet);
}

std::vector<uint8_t> CreateQuickSabmPacket(uint8_t dlci, uint16_t l2cap_lcid,
                                           uint16_t acl_handle) {
  uint8_t address_field = GetAddressField(true, true, dlci);
  uint8_t control_field = GetControlField(true, RFCOMM_SABME);
  std::vector<uint8_t> rfcomm_packet =
      CreateRfcommPacket(address_field, control_field, -1, {});
  std::vector<uint8_t> l2cap_packet =
      CreateL2capDataPacket(l2cap_lcid, rfcomm_packet);
  return CreateAclPacket(acl_handle, 0b10, 0b00, l2cap_packet);
}

std::vector<uint8_t> CreateQuickPnPacket(bool rfc_cr, uint8_t target_dlci,
                                         bool mx_cr, uint16_t rfc_mtu,
                                         uint8_t cl, uint8_t priority,
                                         uint8_t k, uint16_t l2cap_lcid,
                                         uint16_t acl_handle) {
  uint8_t address_field = GetAddressField(true, rfc_cr, RFCOMM_MX_DLCI);
  uint8_t control_field = GetControlField(false, RFCOMM_UIH);
  std::vector<uint8_t> mcc_pn_data = CreateMccPnFrame(
      target_dlci, 0x0, cl, priority, RFCOMM_T1_DSEC, rfc_mtu, RFCOMM_N2, k);
  std::vector<uint8_t> mcc_payload =
      CreateMultiplexerControlFrame(0x20, mx_cr, mcc_pn_data);
  std::vector<uint8_t> rfcomm_packet =
      CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
  std::vector<uint8_t> l2cap_packet =
      CreateL2capDataPacket(l2cap_lcid, rfcomm_packet);
  return CreateAclPacket(acl_handle, 0b10, 0b00, l2cap_packet);
}

std::vector<uint8_t> CreateQuickMscPacket(bool rfc_cr, uint8_t dlci,
                                          uint16_t l2cap_lcid,
                                          uint16_t acl_handle, bool mx_cr,
                                          bool fc, bool rtc, bool rtr, bool ic,
                                          bool dv) {
  uint8_t address_field = GetAddressField(true, rfc_cr, RFCOMM_MX_DLCI);
  uint8_t control_field = GetControlField(false, RFCOMM_UIH);
  std::vector<uint8_t> mcc_msc_data =
      CreateMccMscFrame(dlci, fc, rtc, rtr, ic, dv);
  std::vector<uint8_t> mcc_payload =
      CreateMultiplexerControlFrame(0x38, mx_cr, mcc_msc_data);
  std::vector<uint8_t> rfcomm_packet =
      CreateRfcommPacket(address_field, control_field, -1, mcc_payload);
  std::vector<uint8_t> l2cap_packet =
      CreateL2capDataPacket(l2cap_lcid, rfcomm_packet);
  return CreateAclPacket(acl_handle, 0b10, 0b00, l2cap_packet);
}

std::vector<uint8_t> CreateQuickDataPacket(uint8_t dlci, bool cr,
                                           uint16_t l2cap_lcid,
                                           uint16_t acl_handle, int credits,
                                           const std::vector<uint8_t>& data) {
  uint8_t address_field = GetAddressField(true, cr, dlci);
  uint8_t control_field =
      GetControlField(credits > 0 ? true : false, RFCOMM_UIH);
  std::vector<uint8_t> rfcomm_packet =
      CreateRfcommPacket(address_field, control_field, credits, data);
  std::vector<uint8_t> l2cap_packet =
      CreateL2capDataPacket(l2cap_lcid, rfcomm_packet);
  return CreateAclPacket(acl_handle, 0b10, 0b00, l2cap_packet);
}

std::vector<uint8_t> CreateQuickDataPacket(uint8_t dlci, bool cr,
                                           uint16_t l2cap_lcid,
                                           uint16_t acl_handle, int credits,
                                           const std::string& str) {
  std::vector<uint8_t> data(str.begin(), str.end());
  return CreateQuickDataPacket(dlci, cr, l2cap_lcid, acl_handle, credits, data);
}

}  // namespace rfcomm
}  // namespace bluetooth