• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_idl_proxy.h"
23 #include "data_collect_manager.h"
24 #include "security_guard_define.h"
25 #include "security_guard_log.h"
26 
27 using namespace OHOS;
28 using namespace OHOS::Security::SecurityGuard;
29 
30 static std::mutex g_mutex;
RequestSecurityEventInfoAsyncImpl(const DeviceIdentify * devId,const char * eventJson,RequestSecurityEventInfoCallBack callback)31 static int32_t RequestSecurityEventInfoAsyncImpl(const DeviceIdentify *devId, const char *eventJson,
32     RequestSecurityEventInfoCallBack callback)
33 {
34     if (devId == nullptr || eventJson == nullptr || devId->length >= DEVICE_ID_MAX_LEN) {
35         return BAD_PARAM;
36     }
37     std::unique_lock<std::mutex> lock(g_mutex);
38     uint8_t tmp[DEVICE_ID_MAX_LEN] = {};
39     (void) memset_s(tmp, DEVICE_ID_MAX_LEN, 0, DEVICE_ID_MAX_LEN);
40     errno_t rc = memcpy_s(tmp, DEVICE_ID_MAX_LEN, devId->identity, devId->length);
41     if (rc != EOK) {
42         SGLOGE("identity memcpy error, code=%{public}d", rc);
43         return NULL_OBJECT;
44     }
45     std::string identity(reinterpret_cast<const char *>(tmp));
46     std::string eventList(eventJson);
47     auto func = [callback] (std::string &devId, std::string &riskData, uint32_t status,
48         const std::string &errMsg)-> int32_t {
49         if (devId.length() >= DEVICE_ID_MAX_LEN) {
50             return BAD_PARAM;
51         }
52 
53         struct DeviceIdentify identity;
54         (void) memset_s(&identity, sizeof(DeviceIdentify), 0, sizeof(DeviceIdentify));
55         errno_t rc = memcpy_s(identity.identity, DEVICE_ID_MAX_LEN, devId.c_str(), devId.length());
56         if (rc != EOK) {
57             return NULL_OBJECT;
58         }
59         identity.length = devId.length();
60         callback(&identity, riskData.c_str(), status);
61         return SUCCESS;
62     };
63     return DataCollectManager::GetInstance().RequestSecurityEventInfo(identity, eventList, func);
64 }
65 
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
RequestSecurityEventInfoAsync(const DeviceIdentify * devId,const char * eventJson,RequestSecurityEventInfoCallBack callback)71 int32_t RequestSecurityEventInfoAsync(const DeviceIdentify *devId, const char *eventJson,
72     RequestSecurityEventInfoCallBack callback)
73 {
74     return RequestSecurityEventInfoAsyncImpl(devId, eventJson, callback);
75 }
76 
77 #ifdef __cplusplus
78 }
79 #endif