15bbf6e98Sopenharmony_ci/*
25bbf6e98Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
35bbf6e98Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45bbf6e98Sopenharmony_ci * you may not use this file except in compliance with the License.
55bbf6e98Sopenharmony_ci * You may obtain a copy of the License at
65bbf6e98Sopenharmony_ci *
75bbf6e98Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85bbf6e98Sopenharmony_ci *
95bbf6e98Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105bbf6e98Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115bbf6e98Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bbf6e98Sopenharmony_ci * See the License for the specific language governing permissions and
135bbf6e98Sopenharmony_ci * limitations under the License.
145bbf6e98Sopenharmony_ci */
155bbf6e98Sopenharmony_ci
165bbf6e98Sopenharmony_ci#include "devattest_task.h"
175bbf6e98Sopenharmony_ci
185bbf6e98Sopenharmony_ci#include <pthread.h>
195bbf6e98Sopenharmony_ci#include "iservice_registry.h"
205bbf6e98Sopenharmony_ci#include "singleton.h"
215bbf6e98Sopenharmony_ci#include "devattest_log.h"
225bbf6e98Sopenharmony_ci#include "devattest_errno.h"
235bbf6e98Sopenharmony_ci#include "attest_entry.h"
245bbf6e98Sopenharmony_ci
255bbf6e98Sopenharmony_cinamespace OHOS {
265bbf6e98Sopenharmony_cinamespace DevAttest {
275bbf6e98Sopenharmony_ciusing namespace OHOS;
285bbf6e98Sopenharmony_ci
295bbf6e98Sopenharmony_ciconstexpr std::int32_t SA_ID_DEVICE_ATTEST_SERVICE = 5501;
305bbf6e98Sopenharmony_ciconst char* ATTEST_RUN_TASK_ID = "attest_run";
315bbf6e98Sopenharmony_ciDevAttestTask::DevAttestTask()
325bbf6e98Sopenharmony_ci{
335bbf6e98Sopenharmony_ci}
345bbf6e98Sopenharmony_ci
355bbf6e98Sopenharmony_ciDevAttestTask::~DevAttestTask()
365bbf6e98Sopenharmony_ci{
375bbf6e98Sopenharmony_ci}
385bbf6e98Sopenharmony_ci
395bbf6e98Sopenharmony_cibool DevAttestTask::CreateThread()
405bbf6e98Sopenharmony_ci{
415bbf6e98Sopenharmony_ci    pthread_t tid;
425bbf6e98Sopenharmony_ci    pthread_attr_t attr;
435bbf6e98Sopenharmony_ci    pthread_attr_init(&attr);
445bbf6e98Sopenharmony_ci    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
455bbf6e98Sopenharmony_ci    int priority = 0;
465bbf6e98Sopenharmony_ci    struct sched_param sched = {static_cast<int>(priority)};
475bbf6e98Sopenharmony_ci    pthread_attr_setschedparam(&attr, &sched);
485bbf6e98Sopenharmony_ci    int ret = pthread_create(&tid, &attr, DevAttestTask::Run, NULL);
495bbf6e98Sopenharmony_ci    if (ret != DEVATTEST_SUCCESS) {
505bbf6e98Sopenharmony_ci        HILOGE("thread create failed, ret: %{public}d", ret);
515bbf6e98Sopenharmony_ci        return false;
525bbf6e98Sopenharmony_ci    }
535bbf6e98Sopenharmony_ci    return true;
545bbf6e98Sopenharmony_ci}
555bbf6e98Sopenharmony_ci
565bbf6e98Sopenharmony_civoid* DevAttestTask::Run(void* arg)
575bbf6e98Sopenharmony_ci{
585bbf6e98Sopenharmony_ci    (void)pthread_setname_np(pthread_self(), ATTEST_RUN_TASK_ID); // set pthread name, at most 15 bytes.
595bbf6e98Sopenharmony_ci    (void)AttestTask();
605bbf6e98Sopenharmony_ci    UnloadTask();
615bbf6e98Sopenharmony_ci    HILOGI("Thread exited...");
625bbf6e98Sopenharmony_ci    return nullptr;
635bbf6e98Sopenharmony_ci}
645bbf6e98Sopenharmony_ci
655bbf6e98Sopenharmony_civoid DevAttestTask::UnloadTask(void)
665bbf6e98Sopenharmony_ci{
675bbf6e98Sopenharmony_ci    sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
685bbf6e98Sopenharmony_ci    if (samgrProxy == nullptr) {
695bbf6e98Sopenharmony_ci        HILOGE("get samgr failed");
705bbf6e98Sopenharmony_ci        return;
715bbf6e98Sopenharmony_ci    }
725bbf6e98Sopenharmony_ci    int32_t ret = samgrProxy->UnloadSystemAbility(SA_ID_DEVICE_ATTEST_SERVICE);
735bbf6e98Sopenharmony_ci    if (ret != DEVATTEST_SUCCESS) {
745bbf6e98Sopenharmony_ci        HILOGE("remove system ability failed");
755bbf6e98Sopenharmony_ci        return;
765bbf6e98Sopenharmony_ci    }
775bbf6e98Sopenharmony_ci}
785bbf6e98Sopenharmony_ci} // end of DevAttest
795bbf6e98Sopenharmony_ci} // end of OHOS