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