1 /* 2 * Copyright (C) 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 HOST_CARDEMULATIONMANAGER_H 16 #define HOST_CARDEMULATIONMANAGER_H 17 18 #include <vector> 19 #include <string> 20 #include "nfc_service.h" 21 #include "access_token.h" 22 #include "common_event_manager.h" 23 #include "ihce_cmd_callback.h" 24 #include "element_name.h" 25 #include "inci_ce_interface.h" 26 #include "nfc_ability_connection_callback.h" 27 #include "ce_service.h" 28 29 namespace OHOS { 30 namespace NFC { 31 using OHOS::AppExecFwk::ElementName; 32 class NfcService; 33 class NfcAbilityConnectionCallback; 34 class CeService; 35 class HostCardEmulationManager : public std::enable_shared_from_this<HostCardEmulationManager> { 36 public: 37 enum HceState { 38 INITIAL_STATE = 0, 39 WAIT_FOR_SELECT, 40 WAIT_FOR_SERVICE, 41 WAIT_FOR_DEACTIVATE, 42 DATA_TRANSFER, 43 }; 44 explicit HostCardEmulationManager(std::weak_ptr<NfcService> nfcService, 45 std::weak_ptr<NCI::INciCeInterface> nciCeProxy, 46 std::weak_ptr<CeService> ceService); 47 ~HostCardEmulationManager(); 48 void OnHostCardEmulationDataNfcA(const std::vector<uint8_t>& data); 49 void OnCardEmulationActivated(); 50 void OnCardEmulationDeactivated(); 51 class HceCmdRegistryData { 52 public: 53 bool isEnabled_ = false; 54 AppExecFwk::ElementName element_; 55 Security::AccessToken::AccessTokenID callerToken_ = 0; 56 sptr<KITS::IHceCmdCallback> callback_ = nullptr; 57 }; 58 59 bool RegHceCmdCallback(const sptr<KITS::IHceCmdCallback>& callback, const std::string& type, 60 Security::AccessToken::AccessTokenID callerToken); 61 bool UnRegHceCmdCallback(const std::string& type, Security::AccessToken::AccessTokenID callerToken); 62 bool UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken); 63 64 bool SendHostApduData(std::string hexCmdData, bool raw, std::string& hexRespData, 65 Security::AccessToken::AccessTokenID callerToken); 66 67 void HandleQueueData(); 68 69 private: 70 void HandleDataOnW4Select(const std::string& aid, ElementName& aidElement, const std::vector<uint8_t>& data); 71 void HandleDataOnDataTransfer(const std::string& aid, ElementName& aidElement, 72 const std::vector<uint8_t>& data); 73 bool ExistService(ElementName& aidElement); 74 std::string ParseSelectAid(const std::vector<uint8_t>& data); 75 void SendDataToService(const std::vector<uint8_t>& data); 76 bool DispatchAbilitySingleApp(ElementName& element); 77 bool EraseHceCmdCallback(Security::AccessToken::AccessTokenID callerToken); 78 bool IsCorrespondentService(Security::AccessToken::AccessTokenID callerToken); 79 80 std::weak_ptr<NfcService> nfcService_{}; 81 std::weak_ptr<NCI::INciCeInterface> nciCeProxy_{}; 82 friend class NfcService; 83 84 std::weak_ptr<CeService> ceService_{}; 85 friend class CeService; 86 87 std::map<std::string, HostCardEmulationManager::HceCmdRegistryData> bundleNameToHceCmdRegData_{}; 88 HceState hceState_; 89 std::vector<uint8_t> queueHceData_{}; 90 91 sptr<NfcAbilityConnectionCallback> abilityConnection_{}; 92 friend class NfcAbilityConnectionCallback; 93 94 std::mutex regInfoMutex_ {}; 95 std::mutex hceStateMutex_ {}; 96 }; 97 } // namespace NFC 98 } // namespace OHOS 99 #endif // HOST_CARDEMULATIONMANAGER_H 100