1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (C) 2023-2023 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#ifndef CE_SERVICE_H
16bc2ed2b3Sopenharmony_ci#define CE_SERVICE_H
17bc2ed2b3Sopenharmony_ci#include "nfc_service.h"
18bc2ed2b3Sopenharmony_ci#include "host_card_emulation_manager.h"
19bc2ed2b3Sopenharmony_ci#include "inci_ce_interface.h"
20bc2ed2b3Sopenharmony_ci#include "ihce_cmd_callback.h"
21bc2ed2b3Sopenharmony_ci#include "app_data_parser.h"
22bc2ed2b3Sopenharmony_ci#include "common_event_manager.h"
23bc2ed2b3Sopenharmony_ci#include "element_name.h"
24bc2ed2b3Sopenharmony_ci#include "idefault_payment_service_change_callback.h"
25bc2ed2b3Sopenharmony_ci#include "default_payment_service_change_callback.h"
26bc2ed2b3Sopenharmony_ci#include "app_state_observer.h"
27bc2ed2b3Sopenharmony_ci#include "infc_app_state_observer.h"
28bc2ed2b3Sopenharmony_ci
29bc2ed2b3Sopenharmony_cinamespace OHOS {
30bc2ed2b3Sopenharmony_cinamespace NFC {
31bc2ed2b3Sopenharmony_ciclass NfcService;
32bc2ed2b3Sopenharmony_ciclass NfcEventHandler;
33bc2ed2b3Sopenharmony_ciclass HostCardEmulationManager;
34bc2ed2b3Sopenharmony_ciusing OHOS::AppExecFwk::ElementName;
35bc2ed2b3Sopenharmony_ciclass CeService : public IDefaultPaymentServiceChangeCallback,
36bc2ed2b3Sopenharmony_ci                  public std::enable_shared_from_this<CeService>,
37bc2ed2b3Sopenharmony_ci                  public INfcAppStateObserver {
38bc2ed2b3Sopenharmony_cipublic:
39bc2ed2b3Sopenharmony_ci    struct AidEntry {
40bc2ed2b3Sopenharmony_ci        std::string aid;
41bc2ed2b3Sopenharmony_ci        int route;
42bc2ed2b3Sopenharmony_ci        int aidInfo;
43bc2ed2b3Sopenharmony_ci        int power;
44bc2ed2b3Sopenharmony_ci        bool operator==(const AidEntry &other) const
45bc2ed2b3Sopenharmony_ci        {
46bc2ed2b3Sopenharmony_ci            return aid == other.aid && route == other.route && aidInfo == other.aidInfo && power == other.power;
47bc2ed2b3Sopenharmony_ci        }
48bc2ed2b3Sopenharmony_ci    };
49bc2ed2b3Sopenharmony_ci
50bc2ed2b3Sopenharmony_ci    explicit CeService(std::weak_ptr<NfcService> nfcService, std::weak_ptr<NCI::INciCeInterface> nciCeProxy);
51bc2ed2b3Sopenharmony_ci    ~CeService();
52bc2ed2b3Sopenharmony_ci
53bc2ed2b3Sopenharmony_ci    void HandleFieldActivated();
54bc2ed2b3Sopenharmony_ci    void HandleFieldDeactivated();
55bc2ed2b3Sopenharmony_ci    void OnCardEmulationData(const std::vector<uint8_t> &data);
56bc2ed2b3Sopenharmony_ci    void OnCardEmulationActivated();
57bc2ed2b3Sopenharmony_ci    void OnCardEmulationDeactivated();
58bc2ed2b3Sopenharmony_ci    static void PublishFieldOnOrOffCommonEvent(bool isFieldOn);
59bc2ed2b3Sopenharmony_ci    bool RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> &callback, const std::string &type,
60bc2ed2b3Sopenharmony_ci                           Security::AccessToken::AccessTokenID callerToken);
61bc2ed2b3Sopenharmony_ci
62bc2ed2b3Sopenharmony_ci    bool UnRegHceCmdCallback(const std::string &type, Security::AccessToken::AccessTokenID callerToken);
63bc2ed2b3Sopenharmony_ci
64bc2ed2b3Sopenharmony_ci    bool UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken);
65bc2ed2b3Sopenharmony_ci    bool IsDefaultService(ElementName &element, const std::string &type);
66bc2ed2b3Sopenharmony_ci
67bc2ed2b3Sopenharmony_ci    bool SendHostApduData(const std::string &hexCmdData, bool raw, std::string &hexRespData,
68bc2ed2b3Sopenharmony_ci                          Security::AccessToken::AccessTokenID callerToken);
69bc2ed2b3Sopenharmony_ci
70bc2ed2b3Sopenharmony_ci    bool InitConfigAidRouting(bool forceUpdate);
71bc2ed2b3Sopenharmony_ci    void OnDefaultPaymentServiceChange() override;
72bc2ed2b3Sopenharmony_ci    OHOS::sptr<OHOS::IRemoteObject> AsObject() override;
73bc2ed2b3Sopenharmony_ci    void Initialize();
74bc2ed2b3Sopenharmony_ci    void Deinitialize();
75bc2ed2b3Sopenharmony_ci    bool StartHce(const ElementName &element, const std::vector<std::string> &aids);
76bc2ed2b3Sopenharmony_ci    bool StopHce(const ElementName &element, Security::AccessToken::AccessTokenID callerToken);
77bc2ed2b3Sopenharmony_ci    bool HandleWhenRemoteDie(Security::AccessToken::AccessTokenID callerToken);
78bc2ed2b3Sopenharmony_ci    void OnAppAddOrChangeOrRemove(std::shared_ptr<EventFwk::CommonEventData> data);
79bc2ed2b3Sopenharmony_ci
80bc2ed2b3Sopenharmony_ci    void ConfigRoutingAndCommit();
81bc2ed2b3Sopenharmony_ci    void SearchElementByAid(const std::string &aid, ElementName &aidElement);
82bc2ed2b3Sopenharmony_ci    KITS::DefaultPaymentType GetDefaultPaymentType();
83bc2ed2b3Sopenharmony_ci
84bc2ed2b3Sopenharmony_ci    void HandleAppStateChanged(const std::string &bundleName, const std::string &abilityName,
85bc2ed2b3Sopenharmony_ci                               int abilityState) override;
86bc2ed2b3Sopenharmony_ci
87bc2ed2b3Sopenharmony_ci    void HandleDataShareReady();
88bc2ed2b3Sopenharmony_ci
89bc2ed2b3Sopenharmony_ciprivate:
90bc2ed2b3Sopenharmony_ci    void BuildAidEntries(std::map<std::string, AidEntry> &aidEntries);
91bc2ed2b3Sopenharmony_ci    void ClearAidEntriesCache();
92bc2ed2b3Sopenharmony_ci    bool IsDynamicAid(const std::string &aid);
93bc2ed2b3Sopenharmony_ci    bool IsPaymentAid(const std::string &aid, const AppDataParser::HceAppAidInfo &hceApp);
94bc2ed2b3Sopenharmony_ci    void SetHceInfo(const ElementName &element, const std::vector<std::string> &aids);
95bc2ed2b3Sopenharmony_ci    void ClearHceInfo();
96bc2ed2b3Sopenharmony_ci    bool AppEventCheckValid(std::shared_ptr<EventFwk::CommonEventData> data);
97bc2ed2b3Sopenharmony_ci    void UpdateDefaultPaymentBundleInstalledStatus(bool installed);
98bc2ed2b3Sopenharmony_ci
99bc2ed2b3Sopenharmony_ci    void HandleOtherAidConflicted(const std::vector<AppDataParser::HceAppAidInfo> &hceApps);
100bc2ed2b3Sopenharmony_ci    bool UpdateDefaultPaymentType();
101bc2ed2b3Sopenharmony_ci    void UpdateDefaultPaymentElement(const ElementName &element);
102bc2ed2b3Sopenharmony_ci    void NotifyDefaultPaymentType(int paymentType);
103bc2ed2b3Sopenharmony_ci    bool InitDefaultPaymentApp();
104bc2ed2b3Sopenharmony_ci
105bc2ed2b3Sopenharmony_ci    uint64_t lastFieldOnTime_ = 0;
106bc2ed2b3Sopenharmony_ci    uint64_t lastFieldOffTime_ = 0;
107bc2ed2b3Sopenharmony_ci    bool initDefaultPaymentAppDone_ = false;
108bc2ed2b3Sopenharmony_ci
109bc2ed2b3Sopenharmony_ci    std::weak_ptr<NfcService> nfcService_{};
110bc2ed2b3Sopenharmony_ci
111bc2ed2b3Sopenharmony_ci    friend class NfcService;
112bc2ed2b3Sopenharmony_ci    std::weak_ptr<NCI::INciCeInterface> nciCeProxy_{};
113bc2ed2b3Sopenharmony_ci    std::shared_ptr<HostCardEmulationManager> hostCardEmulationManager_{};
114bc2ed2b3Sopenharmony_ci    ElementName defaultPaymentElement_;
115bc2ed2b3Sopenharmony_ci    bool defaultPaymentBundleInstalled_ {};
116bc2ed2b3Sopenharmony_ci    KITS::DefaultPaymentType defaultPaymentType_ = KITS::DefaultPaymentType::TYPE_EMPTY;
117bc2ed2b3Sopenharmony_ci    sptr<DefaultPaymentServiceChangeCallback> dataRdbObserver_;
118bc2ed2b3Sopenharmony_ci
119bc2ed2b3Sopenharmony_ci    ElementName foregroundElement_ {};
120bc2ed2b3Sopenharmony_ci    std::vector<std::string> dynamicAids_ {};
121bc2ed2b3Sopenharmony_ci
122bc2ed2b3Sopenharmony_ci    std::mutex configRoutingMutex_ {};
123bc2ed2b3Sopenharmony_ci    std::map<std::string, AidEntry> aidToAidEntry_{};
124bc2ed2b3Sopenharmony_ci    std::shared_ptr<AppStateObserver> appStateObserver_;
125bc2ed2b3Sopenharmony_ci};
126bc2ed2b3Sopenharmony_ci} // namespace NFC
127bc2ed2b3Sopenharmony_ci} // namespace OHOS
128bc2ed2b3Sopenharmony_ci#endif