1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License.
5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at
6bc2ed2b3Sopenharmony_ci *
7bc2ed2b3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc2ed2b3Sopenharmony_ci *
9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and
13bc2ed2b3Sopenharmony_ci * limitations under the License.
14bc2ed2b3Sopenharmony_ci */
15bc2ed2b3Sopenharmony_ci
16bc2ed2b3Sopenharmony_ci#include "nfc_notification_publisher.h"
17bc2ed2b3Sopenharmony_ci
18bc2ed2b3Sopenharmony_ci#include <dlfcn.h>
19bc2ed2b3Sopenharmony_ci
20bc2ed2b3Sopenharmony_ci#include "loghelper.h"
21bc2ed2b3Sopenharmony_ci
22bc2ed2b3Sopenharmony_cinamespace OHOS {
23bc2ed2b3Sopenharmony_cinamespace NFC {
24bc2ed2b3Sopenharmony_cinamespace TAG {
25bc2ed2b3Sopenharmony_ciNfcNotificationPublisher& NfcNotificationPublisher::GetInstance()
26bc2ed2b3Sopenharmony_ci{
27bc2ed2b3Sopenharmony_ci    static NfcNotificationPublisher instance;
28bc2ed2b3Sopenharmony_ci    return instance;
29bc2ed2b3Sopenharmony_ci}
30bc2ed2b3Sopenharmony_ci
31bc2ed2b3Sopenharmony_ciNfcNotificationPublisher::NfcNotificationPublisher()
32bc2ed2b3Sopenharmony_ci{
33bc2ed2b3Sopenharmony_ci    InfoLog("NfcNotificationPublisher constructor enter.");
34bc2ed2b3Sopenharmony_ci    if (!isNtfLibLoaded_) {
35bc2ed2b3Sopenharmony_ci        InitNfcNtfLib();
36bc2ed2b3Sopenharmony_ci    }
37bc2ed2b3Sopenharmony_ci}
38bc2ed2b3Sopenharmony_ci
39bc2ed2b3Sopenharmony_ciNfcNotificationPublisher::~NfcNotificationPublisher()
40bc2ed2b3Sopenharmony_ci{
41bc2ed2b3Sopenharmony_ci    InfoLog("NfcNotificationPublisher destructor enter.");
42bc2ed2b3Sopenharmony_ci    UnloadNfcNtfLib();
43bc2ed2b3Sopenharmony_ci}
44bc2ed2b3Sopenharmony_ci
45bc2ed2b3Sopenharmony_cistatic void NfcNotificationCallback(int notificationId)
46bc2ed2b3Sopenharmony_ci{
47bc2ed2b3Sopenharmony_ci    NfcNotificationPublisher::GetInstance().OnNotificationButtonClicked(notificationId);
48bc2ed2b3Sopenharmony_ci}
49bc2ed2b3Sopenharmony_ci
50bc2ed2b3Sopenharmony_civoid NfcNotificationPublisher::PublishNfcNotification(int notificationId, const std::string &name, int balance)
51bc2ed2b3Sopenharmony_ci{
52bc2ed2b3Sopenharmony_ci    if (nfcNtfInf_.publishNotification == nullptr) {
53bc2ed2b3Sopenharmony_ci        ErrorLog("func handle nullptr, fail to publish notification");
54bc2ed2b3Sopenharmony_ci        return;
55bc2ed2b3Sopenharmony_ci    }
56bc2ed2b3Sopenharmony_ci    if (notificationId == NFC_NO_HAP_SUPPORTED_NOTIFICATION_ID) {
57bc2ed2b3Sopenharmony_ci        usleep(NOTIFICATION_WAIT_TIME_US);
58bc2ed2b3Sopenharmony_ci    }
59bc2ed2b3Sopenharmony_ci    nfcNtfInf_.publishNotification(notificationId, name, balance);
60bc2ed2b3Sopenharmony_ci}
61bc2ed2b3Sopenharmony_ci
62bc2ed2b3Sopenharmony_civoid NfcNotificationPublisher::RegNotificationCallback(std::weak_ptr<NfcService> service)
63bc2ed2b3Sopenharmony_ci{
64bc2ed2b3Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
65bc2ed2b3Sopenharmony_ci    if (!isInitialized_ || nfcService_.expired()) {
66bc2ed2b3Sopenharmony_ci        nfcService_ = service;
67bc2ed2b3Sopenharmony_ci        isInitialized_ = true;
68bc2ed2b3Sopenharmony_ci    }
69bc2ed2b3Sopenharmony_ci    if (nfcNtfInf_.regNtfCallback == nullptr) {
70bc2ed2b3Sopenharmony_ci        ErrorLog("func handle nullptr, fail to publish notification");
71bc2ed2b3Sopenharmony_ci        return;
72bc2ed2b3Sopenharmony_ci    }
73bc2ed2b3Sopenharmony_ci    nfcNtfInf_.regNtfCallback(NfcNotificationCallback);
74bc2ed2b3Sopenharmony_ci}
75bc2ed2b3Sopenharmony_ci
76bc2ed2b3Sopenharmony_civoid NfcNotificationPublisher::UnloadNfcNtfLib()
77bc2ed2b3Sopenharmony_ci{
78bc2ed2b3Sopenharmony_ci    if (nfcNtfHandle_ != nullptr) {
79bc2ed2b3Sopenharmony_ci        dlclose(nfcNtfHandle_);
80bc2ed2b3Sopenharmony_ci        nfcNtfHandle_ = nullptr;
81bc2ed2b3Sopenharmony_ci    }
82bc2ed2b3Sopenharmony_ci
83bc2ed2b3Sopenharmony_ci    isNtfLibLoaded_ = false;
84bc2ed2b3Sopenharmony_ci}
85bc2ed2b3Sopenharmony_ci
86bc2ed2b3Sopenharmony_civoid NfcNotificationPublisher::InitNfcNtfLib()
87bc2ed2b3Sopenharmony_ci{
88bc2ed2b3Sopenharmony_ci    if (isNtfLibLoaded_) {
89bc2ed2b3Sopenharmony_ci        InfoLog("nfc notification lib already loaded.");
90bc2ed2b3Sopenharmony_ci        return;
91bc2ed2b3Sopenharmony_ci    }
92bc2ed2b3Sopenharmony_ci    nfcNtfHandle_ = dlopen(NFC_NTF_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
93bc2ed2b3Sopenharmony_ci    if (nfcNtfHandle_ == nullptr) {
94bc2ed2b3Sopenharmony_ci        ErrorLog("fail to dlopen nfc notification lib.");
95bc2ed2b3Sopenharmony_ci        return;
96bc2ed2b3Sopenharmony_ci    }
97bc2ed2b3Sopenharmony_ci    nfcNtfInf_.regNtfCallback = reinterpret_cast<void (*)(NfcNtfCallback *)>
98bc2ed2b3Sopenharmony_ci        (dlsym(nfcNtfHandle_, REG_NFC_CALLBACK_FUNC_NAME));
99bc2ed2b3Sopenharmony_ci    nfcNtfInf_.publishNotification = reinterpret_cast<void (*)(int, const std::string &, int)>
100bc2ed2b3Sopenharmony_ci        (dlsym(nfcNtfHandle_, PUBLISH_NTF_FUNC_NAME));
101bc2ed2b3Sopenharmony_ci    if (nfcNtfInf_.regNtfCallback == nullptr || nfcNtfInf_.publishNotification == nullptr) {
102bc2ed2b3Sopenharmony_ci        ErrorLog("fail to dlsym nfc notification lib.");
103bc2ed2b3Sopenharmony_ci        UnloadNfcNtfLib();
104bc2ed2b3Sopenharmony_ci        return;
105bc2ed2b3Sopenharmony_ci    }
106bc2ed2b3Sopenharmony_ci    isNtfLibLoaded_ = true;
107bc2ed2b3Sopenharmony_ci}
108bc2ed2b3Sopenharmony_ci
109bc2ed2b3Sopenharmony_civoid NfcNotificationPublisher::OnNotificationButtonClicked(int notificationId)
110bc2ed2b3Sopenharmony_ci{
111bc2ed2b3Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
112bc2ed2b3Sopenharmony_ci    if (nfcService_.expired()) {
113bc2ed2b3Sopenharmony_ci        ErrorLog("nfc service expired, fail to callback.");
114bc2ed2b3Sopenharmony_ci        return;
115bc2ed2b3Sopenharmony_ci    }
116bc2ed2b3Sopenharmony_ci    std::weak_ptr<TAG::TagDispatcher> tagDispatcher = nfcService_.lock()->GetTagDispatcher();
117bc2ed2b3Sopenharmony_ci    if (tagDispatcher.expired()) {
118bc2ed2b3Sopenharmony_ci        ErrorLog("tagDispatcher expired, fail to inform button clicking");
119bc2ed2b3Sopenharmony_ci        return;
120bc2ed2b3Sopenharmony_ci    }
121bc2ed2b3Sopenharmony_ci    tagDispatcher.lock()->OnNotificationButtonClicked(notificationId);
122bc2ed2b3Sopenharmony_ci}
123bc2ed2b3Sopenharmony_ci}  // namespace TAG
124bc2ed2b3Sopenharmony_ci}  // namespace NFC
125bc2ed2b3Sopenharmony_ci}  // namespace OHOS