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 27 namespace OHOS { 28 namespace NFC { 29 class NfcService; 30 class NfcEventHandler; 31 class HostCardEmulationManager; 32 using OHOS::AppExecFwk::ElementName; 33 class CeService : public IDefaultPaymentServiceChangeCallback, public std::enable_shared_from_this<CeService> { 34 public: 35 struct AidEntry { 36 std::string aid; 37 int route; 38 int aidInfo; 39 int power; 40 bool operator==(const AidEntry &other) const 41 { 42 return aid == other.aid && route == other.route && aidInfo == other.aidInfo && power == other.power; 43 } 44 }; 45 46 explicit CeService(std::weak_ptr<NfcService> nfcService, std::weak_ptr<NCI::INciCeInterface> nciCeProxy); 47 ~CeService(); 48 49 void HandleFieldActivated(); 50 void HandleFieldDeactivated(); 51 void OnCardEmulationData(const std::vector<uint8_t> &data); 52 void OnCardEmulationActivated(); 53 void OnCardEmulationDeactivated(); 54 static void PublishFieldOnOrOffCommonEvent(bool isFieldOn); 55 bool RegHceCmdCallback(const sptr<KITS::IHceCmdCallback> &callback, const std::string &type, 56 Security::AccessToken::AccessTokenID callerToken); 57 58 bool UnRegHceCmdCallback(const std::string &type, Security::AccessToken::AccessTokenID callerToken); 59 60 bool UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken); 61 bool IsDefaultService(ElementName &element, const std::string &type); 62 63 bool SendHostApduData(const std::string &hexCmdData, bool raw, std::string &hexRespData, 64 Security::AccessToken::AccessTokenID callerToken); 65 66 bool InitConfigAidRouting(); 67 void OnDefaultPaymentServiceChange() override; 68 OHOS::sptr<OHOS::IRemoteObject> AsObject() override; 69 void Initialize(); 70 void Deinitialize(); 71 void OnAppAddOrChangeOrRemove(std::shared_ptr<EventFwk::CommonEventData> data); 72 void ConfigRoutingAndCommit(); 73 74 private: 75 void BuildAidEntries(std::map<std::string, AidEntry> &aidEntries); 76 void ClearAidEntriesCache(); 77 uint64_t lastFieldOnTime_ = 0; 78 uint64_t lastFieldOffTime_ = 0; 79 80 std::weak_ptr<NfcService> nfcService_{}; 81 82 friend class NfcService; 83 std::weak_ptr<NCI::INciCeInterface> nciCeProxy_{}; 84 std::shared_ptr<HostCardEmulationManager> hostCardEmulationManager_{}; 85 ElementName defaultPaymentElement_; 86 sptr<DefaultPaymentServiceChangeCallback> dataRdbObserver_; 87 88 std::mutex configRoutingMutex_ {}; 89 std::map<std::string, AidEntry> aidToAidEntry_{}; 90 }; 91 } // namespace NFC 92 } // namespace OHOS 93 #endif