1 /*
2 * Copyright (C) 2022 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 #include "nfc_controller_impl.h"
16
17 #include "nfc_sdk_common.h"
18 #include "loghelper.h"
19
20 namespace OHOS {
21 namespace NFC {
22 const std::string DUMP_LINE = "---------------------------";
23 const std::string DUMP_END = "\n";
24
NfcControllerImpl(std::weak_ptr<NfcService> nfcService)25 NfcControllerImpl::NfcControllerImpl(std::weak_ptr<NfcService> nfcService)
26 : NfcControllerStub(), nfcService_(nfcService)
27 {
28 }
29
~NfcControllerImpl()30 NfcControllerImpl::~NfcControllerImpl()
31 {
32 }
33
GetState()34 int NfcControllerImpl::GetState()
35 {
36 return nfcService_.lock()->GetNfcState();
37 }
38
TurnOn()39 int NfcControllerImpl::TurnOn()
40 {
41 nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_ON);
42 return KITS::ERR_NONE;
43 }
44
TurnOff()45 int NfcControllerImpl::TurnOff()
46 {
47 nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_OFF);
48 return KITS::ERR_NONE;
49 }
50
IsNfcOpen(bool & isOpen)51 int NfcControllerImpl::IsNfcOpen(bool &isOpen)
52 {
53 isOpen = nfcService_.lock()->IsNfcEnabled();
54 return KITS::ERR_NONE;
55 }
56
RegisterCallBack(const sptr<INfcControllerCallback> & callback,const std::string & type,Security::AccessToken::AccessTokenID callerToken)57 KITS::ErrorCode NfcControllerImpl::RegisterCallBack(const sptr<INfcControllerCallback> &callback,
58 const std::string& type, Security::AccessToken::AccessTokenID callerToken)
59 {
60 if (!nfcService_.lock()->SetRegisterCallBack(callback, type, callerToken)) {
61 return KITS::ERR_NONE;
62 }
63 return KITS::ERR_NFC_PARAMETERS;
64 }
65
UnRegisterCallBack(const std::string & type,Security::AccessToken::AccessTokenID callerToken)66 KITS::ErrorCode NfcControllerImpl::UnRegisterCallBack(const std::string& type,
67 Security::AccessToken::AccessTokenID callerToken)
68 {
69 if (!nfcService_.lock()->RemoveRegisterCallBack(type, callerToken)) {
70 return KITS::ERR_NONE;
71 }
72 return KITS::ERR_NFC_PARAMETERS;
73 }
74
UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken)75 KITS::ErrorCode NfcControllerImpl::UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken)
76 {
77 if (!nfcService_.lock()->RemoveAllRegisterCallBack(callerToken)) {
78 return KITS::ERR_NONE;
79 }
80 return KITS::ERR_NFC_PARAMETERS;
81 }
82
GetTagServiceIface()83 OHOS::sptr<IRemoteObject> NfcControllerImpl::GetTagServiceIface()
84 {
85 return nfcService_.lock()->GetTagServiceIface();
86 }
87
Dump(int32_t fd,const std::vector<std::u16string> & args)88 int32_t NfcControllerImpl::Dump(int32_t fd, const std::vector<std::u16string>& args)
89 {
90 std::string info = GetDumpInfo();
91 int ret = dprintf(fd, "%s\n", info.c_str());
92 if (ret < 0) {
93 ErrorLog("NfcControllerImpl Dump ret = %{public}d", ret);
94 return KITS::ERR_NFC_PARAMETERS;
95 }
96 return KITS::ERR_NONE;
97 }
98
GetDumpInfo()99 std::string NfcControllerImpl::GetDumpInfo()
100 {
101 std::string info;
102 return info.append(DUMP_LINE)
103 .append(" NFC DUMP ")
104 .append(DUMP_LINE)
105 .append(DUMP_END)
106 .append("NFC_STATE : ")
107 .append(std::to_string(nfcService_.lock()->GetNfcState()))
108 .append(DUMP_END)
109 .append("SCREEN_STATE : ")
110 .append(std::to_string(nfcService_.lock()->GetScreenState()))
111 .append(DUMP_END)
112 .append("NCI_VERSION : ")
113 .append(std::to_string(nfcService_.lock()->GetNciVersion()))
114 .append(DUMP_END);
115 }
116 } // namespace NFC
117 } // namespace OHOS
118