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 "query_app_info_callback_stub.h"
17 
18 #include "nfc_service_ipc_interface_code.h"
19 #include "loghelper.h"
20 
21 namespace OHOS {
22 namespace NFC {
QueryAppInfoCallbackStub()23 QueryAppInfoCallbackStub::QueryAppInfoCallbackStub()
24     : queryTagAppByTechCallback_(nullptr), queryHceAppCallback_(nullptr), isRemoteDied_(false)
25 {}
26 
~QueryAppInfoCallbackStub()27 QueryAppInfoCallbackStub::~QueryAppInfoCallbackStub()
28 {}
29 
GetInstance()30 QueryAppInfoCallbackStub& QueryAppInfoCallbackStub::GetInstance()
31 {
32     static QueryAppInfoCallbackStub instance;
33     return instance;
34 }
35 
OnQueryAppInfo(std::string type,std::vector<int> techList,std::vector<AAFwk::Want> & hceAppList,std::vector<AppExecFwk::ElementName> & elementNameList)36 bool QueryAppInfoCallbackStub::OnQueryAppInfo(std::string type, std::vector<int> techList,
37     std::vector<AAFwk::Want> &hceAppList, std::vector<AppExecFwk::ElementName> &elementNameList)
38 {
39     if (type.compare(KEY_TAG_APP) == 0) {
40         if (queryTagAppByTechCallback_) {
41             InfoLog("OnQueryAppInfo:call tag callback_");
42             elementNameList = queryTagAppByTechCallback_(techList);
43         }
44         return true;
45     } else if (type.compare(KEY_HCE_APP) == 0) {
46         if (queryHceAppCallback_) {
47             InfoLog("OnQueryAppInfo:call hce callback_");
48             hceAppList = queryHceAppCallback_();
49         }
50         return true;
51     }
52     return false;
53 }
54 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int QueryAppInfoCallbackStub::OnRemoteRequest(
56     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58     InfoLog("OnRemoteRequest: code = %{public}d", code);
59     if (isRemoteDied_) {
60         ErrorLog("remote service is died.");
61         return KITS::ERR_NFC_STATE_UNBIND;
62     }
63     if (data.ReadInterfaceToken() != GetDescriptor()) {
64         ErrorLog("OnRemoteRequest: token verification error.");
65         return KITS::ERR_NFC_PARAMETERS;
66     }
67     int exception = data.ReadInt32();
68     if (exception) {
69         ErrorLog("OnRemoteRequest:got exception: (%{public}d).", exception);
70         return exception;
71     }
72 
73     int ret = KITS::ERR_NFC_STATE_UNBIND;
74     switch (code) {
75         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_QUERY_APP_INFO_MSG_CALLBACK): {
76             ret = RemoteQueryAppInfo(data, reply);
77             break;
78         }
79 
80         default: {
81             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82             break;
83         }
84     }
85     return ret;
86 }
87 
RegisterQueryTagAppCallback(const QueryApplicationByVendor tagCallback)88 KITS::ErrorCode QueryAppInfoCallbackStub::RegisterQueryTagAppCallback(const QueryApplicationByVendor tagCallback)
89 {
90     if (queryTagAppByTechCallback_ != nullptr) {
91         InfoLog("RegisterQueryTagAppCallback::queryTagAppByTechCallback_ has registered!");
92         return KITS::ERR_NFC_PARAMETERS;
93     }
94     std::unique_lock<std::shared_mutex> guard(mutex_);
95     if (tagCallback == nullptr) {
96         InfoLog("RegisterQueryTagAppCallback::callback is nullptr!");
97         queryTagAppByTechCallback_ = tagCallback;
98         return KITS::ERR_NFC_PARAMETERS;
99     }
100     queryTagAppByTechCallback_ = tagCallback;
101     return KITS::ERR_NONE;
102 }
103 
RegisterQueryHceAppCallback(const QueryHceAppByVendor hceCallback)104 KITS::ErrorCode QueryAppInfoCallbackStub::RegisterQueryHceAppCallback(const QueryHceAppByVendor hceCallback)
105 {
106     if (queryHceAppCallback_ != nullptr) {
107         InfoLog("RegisterQueryHceAppCallback::queryHceAppCallback_ has registered!");
108         return KITS::ERR_NFC_PARAMETERS;
109     }
110     std::unique_lock<std::shared_mutex> guard(mutex_);
111     if (hceCallback == nullptr) {
112         InfoLog("RegisterQueryHceAppCallback::callback is nullptr!");
113         queryHceAppCallback_ = hceCallback;
114         return KITS::ERR_NFC_PARAMETERS;
115     }
116     queryHceAppCallback_ = hceCallback;
117     return KITS::ERR_NONE;
118 }
119 
RemoteQueryAppInfo(MessageParcel & data,MessageParcel & reply)120 int QueryAppInfoCallbackStub::RemoteQueryAppInfo(MessageParcel &data, MessageParcel &reply)
121 {
122     std::unique_lock<std::shared_mutex> guard(mutex_);
123     std::string type = data.ReadString();
124     std::vector<AppExecFwk::ElementName> elementNameList;
125     std::vector<AAFwk::Want> hceAppList;
126     std::vector<int> techList;
127     if (type.compare(KEY_TAG_APP) == 0) {
128         data.ReadInt32Vector(&techList);
129         OnQueryAppInfo(type, techList, hceAppList, elementNameList);
130         reply.WriteInt32(elementNameList.size());
131         for (AppExecFwk::ElementName elementName : elementNameList) {
132             elementName.Marshalling(reply);
133         }
134     } else if (type.compare(KEY_HCE_APP) == 0) {
135         OnQueryAppInfo(type, techList, hceAppList, elementNameList);
136         int appLen = static_cast<int>(hceAppList.size());
137         reply.WriteInt32(appLen);
138         for (int i = 0; i < appLen; i++) {
139             hceAppList[i].Marshalling(reply);
140         }
141     }
142     return KITS::ERR_NONE;
143 }
144 }  // namespace NFC
145 }  // namespace OHOS