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 "sg_obtaindata_client.h"
17 
18 #include "iservice_registry.h"
19 #include "securec.h"
20 
21 #include "data_collect_manager_callback_service.h"
22 #include "data_collect_manager_proxy.h"
23 #include "security_guard_define.h"
24 #include "security_guard_log.h"
25 
26 using namespace OHOS;
27 using namespace OHOS::Security::SecurityGuard;
28 
29 static std::mutex g_mutex;
30 
RequestSecurityEventInfo(std::string & devId,std::string & eventList,RequestRiskDataCallback callback)31 static int32_t RequestSecurityEventInfo(std::string &devId, std::string &eventList,
32     RequestRiskDataCallback callback)
33 {
34     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35     if (registry == nullptr) {
36         SGLOGE("GetSystemAbilityManager error");
37         return NULL_OBJECT;
38     }
39 
40     auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
41     auto proxy = iface_cast<DataCollectManagerProxy>(object);
42     if (proxy == nullptr) {
43         SGLOGE("proxy is null");
44         return NULL_OBJECT;
45     }
46 
47     auto obj = new (std::nothrow) DataCollectManagerCallbackService(callback);
48     if (obj == nullptr) {
49         SGLOGE("stub is null");
50         return NULL_OBJECT;
51     }
52     int32_t ret = proxy->RequestRiskData(devId, eventList, obj);
53     if (ret != 0) {
54         SGLOGE("RequestSecurityEventInfo error, ret=%{public}d", ret);
55         return ret;
56     }
57     return SUCCESS;
58 }
59 
RequestSecurityEventInfoAsyncImpl(const DeviceIdentify * devId,const char * eventJson,RequestSecurityEventInfoCallBack callback)60 static int32_t RequestSecurityEventInfoAsyncImpl(const DeviceIdentify *devId, const char *eventJson,
61     RequestSecurityEventInfoCallBack callback)
62 {
63     if (devId == nullptr || eventJson == nullptr || devId->length >= DEVICE_ID_MAX_LEN) {
64         return BAD_PARAM;
65     }
66     std::unique_lock<std::mutex> lock(g_mutex);
67     uint8_t tmp[DEVICE_ID_MAX_LEN] = {};
68     (void) memset_s(tmp, DEVICE_ID_MAX_LEN, 0, DEVICE_ID_MAX_LEN);
69     errno_t rc = memcpy_s(tmp, DEVICE_ID_MAX_LEN, devId->identity, devId->length);
70     if (rc != EOK) {
71         SGLOGE("identity memcpy error, code=%{public}d", rc);
72         return NULL_OBJECT;
73     }
74     std::string identity(reinterpret_cast<const char *>(tmp));
75     std::string eventList(eventJson);
76     auto func = [callback] (std::string &devId, std::string &riskData, uint32_t status,
77         const std::string &errMsg)-> int32_t {
78         SGLOGI("riskData=%{public}s, status=%{public}u, errMsg=%{public}s", riskData.c_str(), status, errMsg.c_str());
79         if (devId.length() >= DEVICE_ID_MAX_LEN) {
80             return BAD_PARAM;
81         }
82 
83         struct DeviceIdentify identity;
84         (void) memset_s(&identity, sizeof(DeviceIdentify), 0, sizeof(DeviceIdentify));
85         errno_t rc = memcpy_s(identity.identity, DEVICE_ID_MAX_LEN, devId.c_str(), devId.length());
86         if (rc != EOK) {
87             return NULL_OBJECT;
88         }
89         identity.length = devId.length();
90         callback(&identity, riskData.c_str(), status);
91         return SUCCESS;
92     };
93     return RequestSecurityEventInfo(identity, eventList, func);
94 }
95 
96 
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100 
RequestSecurityEventInfoAsync(const DeviceIdentify * devId,const char * eventJson,RequestSecurityEventInfoCallBack callback)101 int32_t RequestSecurityEventInfoAsync(const DeviceIdentify *devId, const char *eventJson,
102     RequestSecurityEventInfoCallBack callback)
103 {
104     return RequestSecurityEventInfoAsyncImpl(devId, eventJson, callback);
105 }
106 
107 #ifdef __cplusplus
108 }
109 #endif