1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ohos_bt_gatt_utils.h"
17 #include "bluetooth_log.h"
18 #include <map>
19 #include <queue>
20 #include <mutex>
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 using namespace std;
27
28 namespace OHOS {
29 namespace Bluetooth {
30
31 #define MAX_ADV_ADDR_MAP_SIZE 128
32 #define ADV_ADDR_TIMEOUT (60 * 60 * 1000) // 1 hour
33 #define MS_PER_SECOND 1000
34 #define NS_PER_MS 1000000
35
36 struct CaseInsensitiveCompare {
operator ()OHOS::Bluetooth::CaseInsensitiveCompare37 bool operator()(const string& s1, const string& s2) const
38 {
39 return lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
40 [](char c1, char c2) { return tolower(c1) < tolower(c2); });
41 }
42 };
43 static mutex g_mapQueMutex;
44 static map<string, uint64_t, CaseInsensitiveCompare> g_advAddrMap; // map<addr, time>
45 static queue<pair<string, uint64_t>> g_advTimeQueue; // pair<addr, time>
46
GetBootMillis()47 static uint64_t GetBootMillis()
48 {
49 struct timespec ts = {};
50 clock_gettime(CLOCK_BOOTTIME, &ts);
51 return ts.tv_sec * MS_PER_SECOND + ts.tv_nsec / NS_PER_MS;
52 }
53
54 /*
55 * The method is only available for {@link BleStartAdvWithAddr}.
56 * Because there cannot be duplicate adv addresses within one hour,
57 * this method will delete adv addresses after one hour.
58 */
RemoveTimeoutAdvAddr()59 void RemoveTimeoutAdvAddr()
60 {
61 lock_guard<mutex> lock(g_mapQueMutex);
62 uint64_t currentMillis = GetBootMillis();
63 while (!g_advTimeQueue.empty() && currentMillis >= g_advTimeQueue.front().second + ADV_ADDR_TIMEOUT) {
64 g_advAddrMap.erase(g_advTimeQueue.front().first);
65 g_advTimeQueue.pop();
66 }
67 }
68
69 /*
70 * This method is only available for {@link BleStartAdvWithAddr}.
71 * Duplicate addresses within 15 minutes are allowed to be broadcast,
72 * and duplicate addresses after 15 minutes are not allowed to be broadcast.
73 * There is no limit on non-duplicate addresses.
74 */
CanStartAdv(const string & addrStr)75 bool CanStartAdv(const string& addrStr)
76 {
77 lock_guard<mutex> lock(g_mapQueMutex);
78 uint64_t currentMillis = GetBootMillis();
79 auto addrTime = g_advAddrMap.find(addrStr);
80 if (addrTime != g_advAddrMap.end()) {
81 if (currentMillis >= addrTime->second + ADV_ADDR_TIME_THRESHOLD) {
82 HILOGW("has the same adv addr in [15mins, 60mins]");
83 return false;
84 } else {
85 return true;
86 }
87 }
88 if (g_advTimeQueue.size() >= MAX_ADV_ADDR_MAP_SIZE) {
89 g_advAddrMap.erase(g_advTimeQueue.front().first);
90 g_advTimeQueue.pop();
91 }
92 g_advTimeQueue.push(pair<string, uint64_t>(addrStr, currentMillis));
93 g_advAddrMap.insert(make_pair(addrStr, currentMillis));
94 return true;
95 }
96
97 } // namespace Bluetooth
98 } // namespace OHOS
99 #ifdef __cplusplus
100 }
101 #endif