• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bundle_mgr_interface.h"
29 #include "bundle_mgr_proxy.h"
30 
31 namespace OHOS {
32 namespace NFC {
33 using OHOS::AppExecFwk::ElementName;
34 class NfcService;
35 class NfcAbilityConnectionCallback;
36 class CeService;
37 class HostCardEmulationManager : public std::enable_shared_from_this<HostCardEmulationManager> {
38 public:
39     enum HceState {
40         INITIAL_STATE = 0,
41         WAIT_FOR_SELECT,
42         WAIT_FOR_SERVICE,
43         WAIT_FOR_DEACTIVATE,
44         DATA_TRANSFER,
45     };
46     explicit HostCardEmulationManager(std::weak_ptr<NfcService> nfcService,
47                                       std::weak_ptr<NCI::INciCeInterface> nciCeProxy,
48                                       std::weak_ptr<CeService> ceService);
49     ~HostCardEmulationManager();
50     void OnHostCardEmulationDataNfcA(const std::vector<uint8_t>& data);
51     void OnCardEmulationActivated();
52     void OnCardEmulationDeactivated();
53     class HceCmdRegistryData {
54     public:
55         bool isEnabled_ = false;
56         AppExecFwk::ElementName element_;
57         Security::AccessToken::AccessTokenID callerToken_ = 0;
58         sptr<KITS::IHceCmdCallback> callback_ = nullptr;
59     };
60 
61     bool RegHceCmdCallback(const sptr<KITS::IHceCmdCallback>& callback, const std::string& type,
62                            Security::AccessToken::AccessTokenID callerToken);
63     bool UnRegHceCmdCallback(const std::string& type, Security::AccessToken::AccessTokenID callerToken);
64     bool UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken);
65 
66     bool SendHostApduData(std::string hexCmdData, bool raw, std::string& hexRespData,
67                           Security::AccessToken::AccessTokenID callerToken);
68 
69     void HandleQueueData();
70     bool IsFaModeApplication(ElementName& elementName);
71     void HandleQueueDataForFa(const std::string &bundleName);
72     sptr<AppExecFwk::IBundleMgr> NfcGetBundleMgrProxy();
73     void HandleDataForStageApplication(const std::string& aid, ElementName& aidElement,
74         const std::vector<uint8_t>& data);
75     void HandleDataForFaApplication(const std::string& aid, ElementName& aidElement, const std::vector<uint8_t>& data);
76     bool IsFaServiceConnected(ElementName& aidElement);
77 
78 private:
79     void HandleDataOnW4Select(const std::string& aid, ElementName& aidElement, const std::vector<uint8_t>& data);
80     void HandleDataOnDataTransfer(const std::string& aid, ElementName& aidElement,
81                                   const std::vector<uint8_t>& data);
82     void HandleDataOnW4SelectForFa(const std::string& aid, ElementName& aidElement, const std::vector<uint8_t>& data);
83     void HandleDataOnDataTransferForFa(const std::string& aid, ElementName& aidElement,
84         const std::vector<uint8_t>& data);
85     void SendDataToFaService(const std::vector<uint8_t>& data, const std::string &bundleName);
86 
87     bool ExistService(ElementName& aidElement);
88     std::string ParseSelectAid(const std::vector<uint8_t>& data);
89     void SendDataToService(const std::vector<uint8_t>& data);
90     bool DispatchAbilitySingleApp(ElementName& element);
91     bool DispatchAbilitySingleAppForFaModel(ElementName& element);
92     bool EraseHceCmdCallback(Security::AccessToken::AccessTokenID callerToken);
93     bool IsCorrespondentService(Security::AccessToken::AccessTokenID callerToken);
94 
95     std::weak_ptr<NfcService> nfcService_{};
96     std::weak_ptr<NCI::INciCeInterface> nciCeProxy_{};
97     friend class NfcService;
98 
99     std::weak_ptr<CeService> ceService_{};
100     friend class CeService;
101 
102     std::map<std::string, HostCardEmulationManager::HceCmdRegistryData> bundleNameToHceCmdRegData_{};
103     HceState hceState_;
104     AppExecFwk::ElementName aidElement_;
105     std::vector<uint8_t> queueHceData_{};
106 
107     sptr<NfcAbilityConnectionCallback> abilityConnection_{};
108     friend class NfcAbilityConnectionCallback;
109 
110     std::mutex regInfoMutex_ {};
111     std::mutex hceStateMutex_ {};
112 };
113 } // namespace NFC
114 } // namespace OHOS
115 #endif // HOST_CARDEMULATIONMANAGER_H
116