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 "dslm_service.h"
17
18#include <dlfcn.h>
19#include <thread>
20
21#include "iremote_object.h"
22#include "singleton.h"
23#include "utils_log.h"
24
25#include "device_security_defines.h"
26#include "dslm_hidumper.h"
27#include "dslm_ipc_process.h"
28#include "dslm_rpc_process.h"
29
30namespace OHOS {
31namespace Security {
32namespace DeviceSecurityLevel {
33REGISTER_SYSTEM_ABILITY_BY_ID(DslmService, DEVICE_SECURITY_LEVEL_MANAGER_SA_ID, true);
34
35DslmService::DslmService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate), IRemoteStub(true)
36{
37    SECURITY_LOG_INFO("object initialization");
38    ProcessLoadPlugin();
39}
40
41void DslmService::OnStart()
42{
43    SECURITY_LOG_INFO("start");
44    std::thread thread([this]() {
45        if (InitService() == SUCCESS) {
46            SECURITY_LOG_INFO("init service success");
47        }
48        if (!Publish(this)) {
49            SECURITY_LOG_ERROR("publish service failed");
50        }
51    });
52    thread.detach();
53}
54
55void DslmService::OnStop()
56{
57    UnInitService();
58    SECURITY_LOG_INFO("stop service");
59}
60
61int32_t DslmService::Dump(int fd, const std::vector<std::u16string> &args)
62{
63    DslmDumper(fd);
64    return 0;
65}
66
67int32_t DslmService::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
68{
69    do {
70        if (IDeviceSecurityLevel::GetDescriptor() != data.ReadInterfaceToken()) {
71            SECURITY_LOG_ERROR("local descriptor is not equal remote");
72            break;
73        }
74        switch (code) {
75            case CMD_GET_DEVICE_SECURITY_LEVEL:
76                return ProcessGetDeviceSecurityLevel(data, reply);
77            default:
78                return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
79        }
80    } while (false);
81
82    return ERR_REQUEST_CODE_ERR;
83}
84
85int32_t DslmService::ProcessGetDeviceSecurityLevel(MessageParcel &data, MessageParcel &reply)
86{
87    return Singleton<DslmIpcProcess>::GetInstance().DslmProcessGetDeviceSecurityLevel(data, reply);
88}
89
90void DslmService::ProcessLoadPlugin(void)
91{
92#ifdef PLUGIN_SO_PATH
93    auto *handle = dlopen(PLUGIN_SO_PATH, RTLD_NOW);
94    if (!handle) {
95        SECURITY_LOG_ERROR("load %{public}s failed for %{public}s", PLUGIN_SO_PATH, dlerror());
96    }
97#endif
98}
99} // namespace DeviceSecurityLevel
100} // namespace Security
101} // namespace OHOS
102