xref: /aosp_15_r20/external/pigweed/pw_bluetooth_proxy/rfcomm_fcs.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_bluetooth_proxy/internal/rfcomm_fcs.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include <cstdint>
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker namespace pw::bluetooth::proxy {
20*61c4878aSAndroid Build Coastguard Worker 
21*61c4878aSAndroid Build Coastguard Worker namespace {
22*61c4878aSAndroid Build Coastguard Worker 
23*61c4878aSAndroid Build Coastguard Worker // Reversed CRC Table for RFCOMM FCS, 8-bit, poly=0x07
24*61c4878aSAndroid Build Coastguard Worker // RFCOMM Spec v1.2 section B.3.5
25*61c4878aSAndroid Build Coastguard Worker constexpr auto kRfcommCrcTable = std::array<uint8_t, 256>{
26*61c4878aSAndroid Build Coastguard Worker     0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75, 0x0e, 0x9f, 0xed,
27*61c4878aSAndroid Build Coastguard Worker     0x7c, 0x09, 0x98, 0xea, 0x7b, 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a,
28*61c4878aSAndroid Build Coastguard Worker     0xf8, 0x69, 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67, 0x38,
29*61c4878aSAndroid Build Coastguard Worker     0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d, 0x36, 0xa7, 0xd5, 0x44,
30*61c4878aSAndroid Build Coastguard Worker     0x31, 0xa0, 0xd2, 0x43, 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0,
31*61c4878aSAndroid Build Coastguard Worker     0x51, 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
32*61c4878aSAndroid Build Coastguard Worker 
33*61c4878aSAndroid Build Coastguard Worker     0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05, 0x7e, 0xef, 0x9d,
34*61c4878aSAndroid Build Coastguard Worker     0x0c, 0x79, 0xe8, 0x9a, 0x0b, 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa,
35*61c4878aSAndroid Build Coastguard Worker     0x88, 0x19, 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17, 0x48,
36*61c4878aSAndroid Build Coastguard Worker     0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d, 0x46, 0xd7, 0xa5, 0x34,
37*61c4878aSAndroid Build Coastguard Worker     0x41, 0xd0, 0xa2, 0x33, 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0,
38*61c4878aSAndroid Build Coastguard Worker     0x21, 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
39*61c4878aSAndroid Build Coastguard Worker 
40*61c4878aSAndroid Build Coastguard Worker     0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95, 0xee, 0x7f, 0x0d,
41*61c4878aSAndroid Build Coastguard Worker     0x9c, 0xe9, 0x78, 0x0a, 0x9b, 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a,
42*61c4878aSAndroid Build Coastguard Worker     0x18, 0x89, 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87, 0xd8,
43*61c4878aSAndroid Build Coastguard Worker     0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad, 0xd6, 0x47, 0x35, 0xa4,
44*61c4878aSAndroid Build Coastguard Worker     0xd1, 0x40, 0x32, 0xa3, 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20,
45*61c4878aSAndroid Build Coastguard Worker     0xb1, 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
46*61c4878aSAndroid Build Coastguard Worker 
47*61c4878aSAndroid Build Coastguard Worker     0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5, 0x9e, 0x0f, 0x7d,
48*61c4878aSAndroid Build Coastguard Worker     0xec, 0x99, 0x08, 0x7a, 0xeb, 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a,
49*61c4878aSAndroid Build Coastguard Worker     0x68, 0xf9, 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7, 0xa8,
50*61c4878aSAndroid Build Coastguard Worker     0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd, 0xa6, 0x37, 0x45, 0xd4,
51*61c4878aSAndroid Build Coastguard Worker     0xa1, 0x30, 0x42, 0xd3, 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50,
52*61c4878aSAndroid Build Coastguard Worker     0xc1, 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf};
53*61c4878aSAndroid Build Coastguard Worker 
54*61c4878aSAndroid Build Coastguard Worker }  // namespace
55*61c4878aSAndroid Build Coastguard Worker 
56*61c4878aSAndroid Build Coastguard Worker // SABM, DISC, UA, DM frame types:
57*61c4878aSAndroid Build Coastguard Worker //   FCS should be calculated over address, control and length fields.
58*61c4878aSAndroid Build Coastguard Worker // UIH frame type:
59*61c4878aSAndroid Build Coastguard Worker //   FCS should be calculated over address and control fields.
RfcommFcs(const emboss::RfcommFrameView & rfcomm)60*61c4878aSAndroid Build Coastguard Worker uint8_t RfcommFcs(const emboss::RfcommFrameView& rfcomm) {
61*61c4878aSAndroid Build Coastguard Worker   uint8_t crc = 0xff;
62*61c4878aSAndroid Build Coastguard Worker   size_t len = 2;
63*61c4878aSAndroid Build Coastguard Worker   if (!rfcomm.uih().Read()) {
64*61c4878aSAndroid Build Coastguard Worker     ++len;
65*61c4878aSAndroid Build Coastguard Worker     if (rfcomm.has_length_extended().ValueOrDefault()) {
66*61c4878aSAndroid Build Coastguard Worker       ++len;
67*61c4878aSAndroid Build Coastguard Worker     }
68*61c4878aSAndroid Build Coastguard Worker   }
69*61c4878aSAndroid Build Coastguard Worker 
70*61c4878aSAndroid Build Coastguard Worker   auto data = pw::span(rfcomm.BackingStorage().data(), len);
71*61c4878aSAndroid Build Coastguard Worker   for (const uint8_t d : data) {
72*61c4878aSAndroid Build Coastguard Worker     crc = kRfcommCrcTable[crc ^ d];
73*61c4878aSAndroid Build Coastguard Worker   }
74*61c4878aSAndroid Build Coastguard Worker   return 0xff - crc;
75*61c4878aSAndroid Build Coastguard Worker }
76*61c4878aSAndroid Build Coastguard Worker 
77*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::bluetooth::proxy
78