1 /*
2  * Copyright (C) 2022 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 "common_hisysevent.h"
17 
18 #include <string>
19 
20 #include <initializer_list>
21 
22 #include "hisysevent.h"
23 
24 #include "location_log.h"
25 #include "hook_utils.h"
26 
27 namespace OHOS {
28 namespace Location {
29 template<typename... Types>
WriteEvent(const std::string & eventType,Types...args)30 static void WriteEvent(const std::string& eventType, Types... args)
31 {
32     int ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::LOCATION, eventType,
33         HiviewDFX::HiSysEvent::EventType::STATISTIC, args...);
34     if (ret != 0) {
35         LBSLOGE(COMMON_UTILS, "Write event fail: %{public}s, ret = %{public}d", eventType.c_str(), ret);
36     }
37 }
38 
WriteGnssStateEvent(const std::string & state,const pid_t pid,const pid_t uid)39 void WriteGnssStateEvent(const std::string& state, const pid_t pid, const pid_t uid)
40 {
41     WriteEvent("GNSS_STATE", "STATE", state, "PID", pid, "UID", uid);
42 }
43 
WriteAppLocatingStateEvent(const std::string & state,const pid_t pid,const pid_t uid)44 void WriteAppLocatingStateEvent(const std::string& state, const pid_t pid, const pid_t uid)
45 {
46     WriteEvent("APP_LOCATING_STATE", "STATE", state, "PID", pid, "UID", uid);
47 }
48 
WriteLocationSwitchStateEvent(const std::string & state)49 void WriteLocationSwitchStateEvent(const std::string& state)
50 {
51     WriteEvent("SWITCH_STATE", "STATE", state);
52 }
53 
WriteLocationInnerEvent(const int event,std::initializer_list<std::string> params)54 void WriteLocationInnerEvent(const int event, std::initializer_list<std::string> params)
55 {
56     std::vector<std::string> names;
57     std::vector<std::string> values;
58     bool flag = true;
59     for (auto x: params) {
60         if (flag) {
61             names.push_back(x);
62         } else {
63             values.push_back(x);
64         }
65         flag = !flag;
66     }
67     HookUtils::ExecuteHookWhenReportInnerInfo(event, names, values);
68 }
69 
WriteLocationInnerEvent(const int event,std::vector<std::string> names,std::vector<std::string> & values)70 void WriteLocationInnerEvent(const int event, std::vector<std::string> names, std::vector<std::string>& values)
71 {
72     HookUtils::ExecuteHookWhenReportInnerInfo(event, names, values);
73 }
74 }  // namespace Location
75 }  // namespace OHOS
76