• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (nfcService_.lock() == nullptr) {
37         return KITS::ERR_NFC_PARAMETERS;
38     }
39     return nfcService_.lock()->GetNfcState();
40 }
41 
TurnOn()42 int NfcControllerImpl::TurnOn()
43 {
44     if (nfcService_.lock() == nullptr) {
45         return KITS::ERR_NFC_PARAMETERS;
46     }
47     nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_ON);
48     return KITS::ERR_NONE;
49 }
50 
TurnOff()51 int NfcControllerImpl::TurnOff()
52 {
53     if (nfcService_.lock() == nullptr) {
54         return KITS::ERR_NFC_PARAMETERS;
55     }
56     nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_OFF);
57     return KITS::ERR_NONE;
58 }
59 
IsNfcOpen(bool & isOpen)60 int NfcControllerImpl::IsNfcOpen(bool &isOpen)
61 {
62     if (nfcService_.lock() == nullptr) {
63         return KITS::ERR_NFC_PARAMETERS;
64     }
65     isOpen = nfcService_.lock()->IsNfcEnabled();
66     return KITS::ERR_NONE;
67 }
68 
RegisterCallBack(const sptr<INfcControllerCallback> & callback,const std::string & type,Security::AccessToken::AccessTokenID callerToken)69 KITS::ErrorCode NfcControllerImpl::RegisterCallBack(const sptr<INfcControllerCallback> &callback,
70     const std::string& type, Security::AccessToken::AccessTokenID callerToken)
71 {
72     if (nfcService_.lock() == nullptr) {
73         return KITS::ERR_NFC_PARAMETERS;
74     }
75     if (!nfcService_.lock()->SetRegisterCallBack(callback, type, callerToken)) {
76         return KITS::ERR_NONE;
77     }
78     return KITS::ERR_NFC_PARAMETERS;
79 }
80 
UnRegisterCallBack(const std::string & type,Security::AccessToken::AccessTokenID callerToken)81 KITS::ErrorCode NfcControllerImpl::UnRegisterCallBack(const std::string& type,
82     Security::AccessToken::AccessTokenID callerToken)
83 {
84     if (nfcService_.lock() == nullptr) {
85         return KITS::ERR_NFC_PARAMETERS;
86     }
87     if (!nfcService_.lock()->RemoveRegisterCallBack(type, callerToken)) {
88         return KITS::ERR_NONE;
89     }
90     return KITS::ERR_NFC_PARAMETERS;
91 }
92 
UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken)93 KITS::ErrorCode NfcControllerImpl::UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken)
94 {
95     if (nfcService_.lock() == nullptr) {
96         return KITS::ERR_NFC_PARAMETERS;
97     }
98     if (!nfcService_.lock()->RemoveAllRegisterCallBack(callerToken)) {
99         return KITS::ERR_NONE;
100     }
101     return KITS::ERR_NFC_PARAMETERS;
102 }
103 
GetTagServiceIface()104 OHOS::sptr<IRemoteObject> NfcControllerImpl::GetTagServiceIface()
105 {
106     if (nfcService_.lock() == nullptr) {
107         return nullptr;
108     }
109     return nfcService_.lock()->GetTagServiceIface();
110 }
111 
Dump(int32_t fd,const std::vector<std::u16string> & args)112 int32_t NfcControllerImpl::Dump(int32_t fd, const std::vector<std::u16string>& args)
113 {
114     if (nfcService_.lock() == nullptr) {
115         return KITS::ERR_NFC_PARAMETERS;
116     }
117     std::string info = GetDumpInfo();
118     int ret = dprintf(fd, "%s\n", info.c_str());
119     if (ret < 0) {
120         ErrorLog("NfcControllerImpl Dump ret = %{public}d", ret);
121         return KITS::ERR_NFC_PARAMETERS;
122     }
123     return KITS::ERR_NONE;
124 }
125 
GetDumpInfo()126 std::string NfcControllerImpl::GetDumpInfo()
127 {
128     std::string info;
129     return info.append(DUMP_LINE)
130         .append(" NFC DUMP ")
131         .append(DUMP_LINE)
132         .append(DUMP_END)
133         .append("NFC_STATE          : ")
134         .append(std::to_string(nfcService_.lock()->GetNfcState()))
135         .append(DUMP_END)
136         .append("SCREEN_STATE       : ")
137         .append(std::to_string(nfcService_.lock()->GetScreenState()))
138         .append(DUMP_END)
139         .append("NCI_VERSION        : ")
140         .append(std::to_string(nfcService_.lock()->GetNciVersion()))
141         .append(DUMP_END);
142 }
143 }  // namespace NFC
144 }  // namespace OHOS
145