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
16 #include "loghelper.h"
17 #include "hce_session.h"
18 #include "external_deps_proxy.h"
19
20 namespace OHOS {
21 namespace NFC {
22 namespace HCE {
23 using OHOS::AppExecFwk::ElementName;
24 const std::string DUMP_LINE = "---------------------------";
25 const std::string DUMP_END = "\n";
26
HceSession(std::shared_ptr<INfcService> service)27 HceSession::HceSession(std::shared_ptr<INfcService> service) : nfcService_(service)
28 {
29 if (service == nullptr) {
30 ErrorLog("HceSession create fail, service is nullptr");
31 return;
32 }
33 ceService_ = service->GetCeService();
34 }
35
~HceSession()36 HceSession::~HceSession()
37 {
38 }
39
RegHceCmdCallbackByToken(const sptr<KITS::IHceCmdCallback> & callback,const std::string & type,Security::AccessToken::AccessTokenID callerToken)40 KITS::ErrorCode HceSession::RegHceCmdCallbackByToken(const sptr<KITS::IHceCmdCallback> &callback,
41 const std::string &type,
42 Security::AccessToken::AccessTokenID callerToken)
43 {
44 if (ceService_.expired()) {
45 ErrorLog("RegHceCmdCallback:ceService_ is nullptr");
46 return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
47 }
48 if (ceService_.lock()->RegHceCmdCallback(callback, type, callerToken)) {
49 return KITS::ERR_NONE;
50 }
51 return KITS::ERR_NFC_PARAMETERS;
52 }
53
UnRegHceCmdCallback(const std::string & type,Security::AccessToken::AccessTokenID callerToken)54 KITS::ErrorCode HceSession::UnRegHceCmdCallback(const std::string &type,
55 Security::AccessToken::AccessTokenID callerToken)
56 {
57 if (ceService_.expired()) {
58 ErrorLog("UnRegHceCmdCallback ceService_ is nullptr");
59 return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
60 }
61 if (ceService_.lock()->UnRegHceCmdCallback(type, callerToken)) {
62 return KITS::ERR_NONE;
63 }
64 return KITS::ERR_HCE_PARAMETERS;
65 }
66
UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken)67 KITS::ErrorCode HceSession::UnRegAllCallback(Security::AccessToken::AccessTokenID callerToken)
68 {
69 if (ceService_.expired()) {
70 ErrorLog("UnRegAllCallback ceService_ is nullptr");
71 return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
72 }
73 if (ceService_.lock()->UnRegAllCallback(callerToken)) {
74 return KITS::ERR_NONE;
75 }
76 return KITS::ERR_HCE_PARAMETERS;
77 }
78
SendRawFrameByToken(std::string hexCmdData,bool raw,std::string & hexRespData,Security::AccessToken::AccessTokenID callerToken)79 int HceSession::SendRawFrameByToken(std::string hexCmdData, bool raw, std::string &hexRespData,
80 Security::AccessToken::AccessTokenID callerToken)
81 {
82 if (ceService_.expired()) {
83 ErrorLog("SendRawFrame ceService_ is nullptr");
84 return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
85 }
86 bool success = ceService_.lock()->SendHostApduData(hexCmdData, raw, hexRespData, callerToken);
87 if (success) {
88 return NFC::KITS::ErrorCode::ERR_NONE;
89 } else {
90 return NFC::KITS::ErrorCode::ERR_HCE_STATE_IO_FAILED;
91 }
92 }
93
IsDefaultService(ElementName & element,const std::string & type,bool & isDefaultService)94 KITS::ErrorCode HceSession::IsDefaultService(ElementName &element, const std::string &type, bool &isDefaultService)
95 {
96 if (ceService_.expired()) {
97 ErrorLog("IsDefaultService ceService_ is nullptr");
98 return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
99 }
100 isDefaultService = ceService_.lock()->IsDefaultService(element, type);
101 return KITS::ERR_NONE;
102 }
103
Dump(int32_t fd,const std::vector<std::u16string> & args)104 int32_t HceSession::Dump(int32_t fd, const std::vector<std::u16string> &args)
105 {
106 std::string info = GetDumpInfo();
107 int ret = dprintf(fd, "%s\n", info.c_str());
108 if (ret < 0) {
109 ErrorLog("hceSession Dump ret = %{public}d", ret);
110 return NFC::KITS::ErrorCode::ERR_HCE_PARAMETERS;
111 }
112 return NFC::KITS::ErrorCode::ERR_NONE;
113 }
114
GetDumpInfo()115 std::string HceSession::GetDumpInfo()
116 {
117 std::string info;
118 return info.append(DUMP_LINE)
119 .append("Hce DUMP ")
120 .append(DUMP_LINE)
121 .append(DUMP_END)
122 .append("NFC_STATE : ")
123 .append(std::to_string(nfcService_.lock()->GetNfcState()))
124 .append(DUMP_END)
125 .append("SCREEN_STATE : ")
126 .append(std::to_string(nfcService_.lock()->GetScreenState()))
127 .append(DUMP_END)
128 .append("NCI_VERSION : ")
129 .append(std::to_string(nfcService_.lock()->GetNciVersion()))
130 .append(DUMP_END);
131 }
GetPaymentServices(std::vector<AbilityInfo> & abilityInfos)132 int HceSession::GetPaymentServices(std::vector<AbilityInfo> &abilityInfos)
133 {
134 ExternalDepsProxy::GetInstance().GetPaymentAbilityInfos(abilityInfos);
135 return NFC::KITS::ErrorCode::ERR_NONE;
136 }
137 } // namespace HCE
138 } // namespace NFC
139 } // namespace OHOS
140