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 #include "device_security_level_inner.h"
16
17 #include "ohos_types.h"
18 #include "utils_log.h"
19 #include "utils_mutex.h"
20
GetMutex(void)21 static inline Mutex *GetMutex(void)
22 {
23 static Mutex mutex = INITED_MUTEX;
24 return &mutex;
25 }
26
GetInnerApi(void)27 static DslmFeatureApi *GetInnerApi(void)
28 {
29 DslmFeatureApi *api = NULL;
30 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(DSLM_SAMGR_SERVICE, DSLM_SAMGR_FEATURE);
31 if (iUnknown == NULL) {
32 SECURITY_LOG_ERROR("iUnknown is NULL");
33 return NULL;
34 }
35
36 int32_t result = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&api);
37 if (result != 0 || api == NULL) {
38 SECURITY_LOG_ERROR("QueryInterface failed");
39 return NULL;
40 }
41 return api;
42 }
43
ReleaseInnerApi(DslmFeatureApi * innerApi)44 static void ReleaseInnerApi(DslmFeatureApi *innerApi)
45 {
46 if (innerApi == NULL) {
47 return;
48 }
49
50 int32_t result = innerApi->Release((IUnknown *)innerApi);
51 SECURITY_LOG_INFO("[Release api S:%s, F:%s]: ret:%d", DSLM_SAMGR_SERVICE, DSLM_SAMGR_FEATURE, result);
52 }
53
RequestDeviceSecurityInfoAsyncImpl(const DeviceIdentify * identify,const RequestOption * option,DeviceSecurityInfoCallback callback)54 int32_t RequestDeviceSecurityInfoAsyncImpl(const DeviceIdentify *identify, const RequestOption *option,
55 DeviceSecurityInfoCallback callback)
56 {
57 static uint32_t generated = 0;
58 if (identify == NULL || callback == NULL) {
59 SECURITY_LOG_ERROR("GetDeviceSecurityInfo input error");
60 return ERR_INVALID_PARA;
61 }
62
63 static RequestOption defaultOption = {0, DEFAULT_KEEP_LEN, 0};
64 if (option == NULL) {
65 option = &defaultOption;
66 }
67 if (option->timeout > MAX_KEEP_LEN) {
68 SECURITY_LOG_ERROR("GetDeviceSecurityInfo input error, timeout too long");
69 return ERR_INVALID_PARA;
70 }
71
72 LockMutex(GetMutex());
73 uint32_t cookie = ++generated;
74 UnlockMutex(GetMutex());
75 DslmFeatureApi *api = GetInnerApi();
76 if (api == NULL) {
77 SECURITY_LOG_ERROR("[GetFeatureApi S:%s F:%s]: failed", DSLM_SAMGR_SERVICE, DSLM_SAMGR_FEATURE);
78 return ERR_IPC_ERR;
79 }
80 if (api->DslmGetDeviceSecurityLevel == NULL) {
81 SECURITY_LOG_ERROR("empty api");
82 return ERR_IPC_ERR;
83 }
84
85 DslmAsyncCallParams params = { identify, option, cookie};
86 BOOL result = api->DslmGetDeviceSecurityLevel((IUnknown *)api, ¶ms, callback);
87 if (result != SUCCESS) {
88 SECURITY_LOG_ERROR("GetDeviceSecurityInfo RequestDeviceSecurityLevel error");
89 return result;
90 }
91 SECURITY_LOG_INFO("GetDeviceSecurityInfo RequestDeviceSecurityLevel success");
92 ReleaseInnerApi(api);
93 return SUCCESS;
94 }