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) { g_read_clock_handler = handler; }
34 
Unregister()35 void LinkClocker::Unregister() { g_read_clock_handler = &g_empty_handler; }
36 
OnHciEvent(const HciPacket & packet)37 void LinkClocker::OnHciEvent(const HciPacket& packet) {
38   const int HCI_CMD_READ_CLOCK = 0x1407;
39   const int HCI_EVT_COMMAND_COMPLETE = 0x0e;
40 
41   // HCI Event [Core 4.E.5.4.4]
42   // |  [0]  Event Code
43   // |  [1]  Parameter Total Length
44   // | [2+]  Parameters
45 
46   if (packet.size() < 2) {
47     return;
48   }
49 
50   const uint8_t* payload = packet.data() + 2;
51   size_t payload_length = std::min(size_t(packet[1]), packet.size() - 2);
52   int event_code = packet[0];
53 
54   if (event_code != HCI_EVT_COMMAND_COMPLETE) {
55     return;
56   }
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) {
64     return;
65   }
66 
67   uint16_t op_code = payload[1] | (payload[2] << 8);
68   const uint8_t* parameters = payload + 3;
69   size_t parameters_length = payload_length - 3;
70 
71   if (op_code != HCI_CMD_READ_CLOCK) {
72     return;
73   }
74 
75   // HCI Read Clock return parameters [Core 4.E.7.5.6]
76   // |    [0]  Status, 0 when OK
77   // | [1..2]  Connection_handle
78   // | [3..6]  Clock (28-bits meaningful)
79   // | [7..8]  Accuracy
80 
81   if (parameters_length < 9) {
82     return;
83   }
84 
85   uint8_t status = parameters[0];
86 
87   if (status != 0) {
88     return;
89   }
90 
91   uint32_t bt_clock = ((uint32_t)parameters[3] << 0) | ((uint32_t)parameters[4] << 8) |
92                       ((uint32_t)parameters[5] << 16) | ((uint32_t)parameters[6] << 24);
93 
94   // The connection handle is ignored as we are reading the local clock
95   // (Which_Clock parameter is 0).
96   // The reason the read clock measurement is extracted here is that
97   // getting the local timestamp from the bound gd HCI event callback
98   // adds jitter.
99 
100   unsigned timestamp_us = bluetooth::common::time_get_audio_server_tick_us();
101 
102   (*g_read_clock_handler).OnEvent(timestamp_us, bt_clock << 4);
103 }
104 
__anone67b84260202() 105 const ModuleFactory LinkClocker::Factory = ModuleFactory([]() { return new LinkClocker(); });
106 
107 }  // namespace bluetooth::hal
108