1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hal/link_clocker.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include <algorithm>
22 
23 #include "common/time_util.h"
24 
25 namespace bluetooth::hal {
26 
27 static class : public ReadClockHandler {
OnEvent(uint32_t,uint32_t)28   void OnEvent(uint32_t, uint32_t) override {}
29 } g_empty_handler;
30 
31 static std::atomic<ReadClockHandler*> g_read_clock_handler = &g_empty_handler;
32 
Register(ReadClockHandler * handler)33 void LinkClocker::Register(ReadClockHandler* handler) {
34   g_read_clock_handler = handler;
35 }
36 
Unregister()37 void LinkClocker::Unregister() {
38   g_read_clock_handler = &g_empty_handler;
39 }
40 
OnHciEvent(const HciPacket & packet)41 void LinkClocker::OnHciEvent(const HciPacket& packet) {
42   const int HCI_CMD_READ_CLOCK = 0x1407;
43   const int HCI_EVT_COMMAND_COMPLETE = 0x0e;
44 
45   // HCI Event [Core 4.E.5.4.4]
46   // |  [0]  Event Code
47   // |  [1]  Parameter Total Length
48   // | [2+]  Parameters
49 
50   if (packet.size() < 2) return;
51 
52   const uint8_t* payload = packet.data() + 2;
53   size_t payload_length = std::min(size_t(packet[1]), packet.size() - 2);
54   int event_code = packet[0];
55 
56   if (event_code != HCI_EVT_COMMAND_COMPLETE) return;
57 
58   // HCI Command Complete Event [Core 4.E.7.7.14]
59   // |    [0]  Num_HCI_Command_Packets, Ignored
60   // | [1..2]  Command_Opcode, catch `HCI_LE_Set_CIG_Parameters`
61   // |   [3+]  Return Parameters
62 
63   if (payload_length < 3) return;
64 
65   uint16_t op_code = payload[1] | (payload[2] << 8);
66   const uint8_t* parameters = payload + 3;
67   size_t parameters_length = payload_length - 3;
68 
69   if (op_code != HCI_CMD_READ_CLOCK) return;
70 
71   // HCI Read Clock return parameters [Core 4.E.7.5.6]
72   // |    [0]  Status, 0 when OK
73   // | [1..2]  Connection_handle
74   // | [3..6]  Clock (28-bits meaningful)
75   // | [7..8]  Accuracy
76 
77   if (parameters_length < 9) return;
78 
79   uint8_t status = parameters[0];
80 
81   if (status != 0) return;
82 
83   uint32_t bt_clock = ((uint32_t)parameters[3] << 0) | ((uint32_t)parameters[4] << 8) |
84                       ((uint32_t)parameters[5] << 16) | ((uint32_t)parameters[6] << 24);
85 
86   // The connection handle is ignored as we are reading the local clock
87   // (Which_Clock parameter is 0).
88   // The reason the read clock measurement is extracted here is that
89   // getting the local timestamp from the bound gd HCI event callback
90   // adds jitter.
91 
92   unsigned timestamp_us = bluetooth::common::time_get_audio_server_tick_us();
93 
94   (*g_read_clock_handler).OnEvent(timestamp_us, bt_clock << 4);
95 }
96 
__anon704ccef00202() 97 const ModuleFactory LinkClocker::Factory = ModuleFactory([]() { return new LinkClocker(); });
98 
99 }  // namespace bluetooth::hal
100