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