1 /*
2 * Copyright (C) Huawei Device Co., Ltd. 2025-2025. All rights reserved.
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 #ifndef LOG_TAG
17 #define LOG_TAG "bt_ha_event_utils"
18 #endif
19
20 #include "napi_ha_event_utils.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_errorcode.h"
23 #ifndef CROSS_PLATFORM
24 #include "app_event.h"
25 #include "app_event_processor_mgr.h"
26 #endif
27
28 namespace OHOS {
29 namespace Bluetooth {
30
31 constexpr int64_t INVALID_PROCESSOR_ID = -200;
32 const std::string SDK_NAME = "ConnectivityKit";
33 constexpr int32_t TIME_MS_PER_SECOND = 1000;
34 constexpr int32_t TIME_NS_PER_MS = 1000000;
35
36 int64_t NapiHaEventUtils::processorId_ = -1;
37 std::mutex NapiHaEventUtils::processorLock_;
38 SafeMap<napi_env, int32_t> NapiHaEventUtils::envErrCodeMap_ = {};
39
NapiHaEventUtils(napi_env env,const std::string & apiName)40 NapiHaEventUtils::NapiHaEventUtils(napi_env env, const std::string &apiName): env_(env), apiName_(apiName)
41 {
42 beginTime_ = GetNowTimeMs();
43 GenerateProcessorId();
44 }
45
~NapiHaEventUtils()46 NapiHaEventUtils::~NapiHaEventUtils()
47 {
48 WriteEndEvent();
49 }
50
WriteErrCode(napi_env env,const int32_t errCode)51 void NapiHaEventUtils::WriteErrCode(napi_env env, const int32_t errCode)
52 {
53 envErrCodeMap_.EnsureInsert(env, errCode);
54 }
55
WriteErrCode(const int32_t errCode)56 void NapiHaEventUtils::WriteErrCode(const int32_t errCode)
57 {
58 NapiHaEventUtils::WriteErrCode(env_, errCode);
59 }
60
GenerateProcessorId()61 void NapiHaEventUtils::GenerateProcessorId()
62 {
63 std::lock_guard<std::mutex> lock(processorLock_);
64 if (processorId_ == -1) {
65 processorId_ = AddProcessor();
66 }
67
68 if (processorId_ == INVALID_PROCESSOR_ID) { // 非应用不支持打点
69 HILOGE("invaild processorId !!!");
70 return;
71 }
72 }
73
GetNowTimeMs() const74 int64_t NapiHaEventUtils::GetNowTimeMs() const
75 {
76 #ifndef CROSS_PLATFORM
77 struct timespec ts = {};
78 clock_gettime(CLOCK_BOOTTIME, &ts);
79 return (int64_t)ts.tv_sec * TIME_MS_PER_SECOND + (int64_t)ts.tv_nsec / TIME_NS_PER_MS;
80 #else
81 return -1;
82 #endif
83 }
84
AddProcessor()85 int64_t NapiHaEventUtils::AddProcessor()
86 {
87 #ifndef CROSS_PLATFORM
88 HiviewDFX::HiAppEvent::ReportConfig config;
89 config.name = "ha_app_event"; // 系统预制so,实现上报功能,由HA提供
90 config.configName = "SDK_OCG"; // 固定内容,此配置内容由HA确认规格
91 return HiviewDFX::HiAppEvent::AppEventProcessorMgr::AddProcessor(config);
92 #else
93 return -1;
94 #endif
95 }
96
RandomTransId() const97 std::string NapiHaEventUtils::RandomTransId() const
98 {
99 return std::string("transId_") + std::to_string(std::rand());
100 }
101
WriteEndEvent() const102 void NapiHaEventUtils::WriteEndEvent() const
103 {
104 #ifndef CROSS_PLATFORM
105 HiviewDFX::HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiviewDFX::HiAppEvent::BEHAVIOR);
106 std::string transId = RandomTransId();
107 int32_t errCode = BT_NO_ERROR; // 默认API调用成功
108 errCode = envErrCodeMap_.ReadVal(env_);
109 envErrCodeMap_.Erase(env_);
110 event.AddParam("trans_id", transId);
111 event.AddParam("api_name", apiName_);
112 event.AddParam("sdk_name", SDK_NAME);
113 event.AddParam("begin_time", beginTime_);
114 event.AddParam("end_time", GetNowTimeMs());
115 event.AddParam("result", (errCode == BT_NO_ERROR ? 0 : 1));
116 event.AddParam("error_code", errCode);
117 int ret = Write(event);
118 HILOGD("WriteEndEvent transId:%{public}s, apiName:%{public}s, sdkName:%{public}s, errCode:%{public}d,"
119 "ret:%{public}d", transId.c_str(), apiName_.c_str(), SDK_NAME.c_str(), errCode, ret);
120 #endif
121 }
122
123 } // namespace Bluetooth
124 } // namespace OHOS