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 NFC_POLLING_MANAGER_H
16 #define NFC_POLLING_MANAGER_H
17 #include "access_token.h"
18 #include "common_event_manager.h"
19 #include "iforeground_callback.h"
20 #include "ireader_mode_callback.h"
21 #include "nfc_polling_params.h"
22 #include "taginfo_parcelable.h"
23 #include "inci_nfcc_interface.h"
24 #include "inci_tag_interface.h"
25 
26 namespace OHOS {
27 namespace NFC {
28 // ms wait for setting the routing table.
29 static const int WAIT_MS_SET_ROUTE = 10 * 1000;
30 class NfcService;
31 class NfcPollingManager {
32 public:
33     NfcPollingManager(std::weak_ptr<NfcService> nfcService,
34                       std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy,
35                       std::weak_ptr<NCI::INciTagInterface> nciTagProxy);
36     ~NfcPollingManager();
37     class ForegroundRegistryData {
38     public:
39         bool isEnabled_ = false;
40         uint16_t techMask_ = 0xFFFF;
41         AppExecFwk::ElementName element_;
42         Security::AccessToken::AccessTokenID callerToken_ = 0;
43         sptr<KITS::IForegroundCallback> callback_ = nullptr;
44     };
45 
46     class ReaderModeRegistryData {
47     public:
48         bool isEnabled_ = false;
49         uint16_t techMask_ = 0xFFFF;
50         AppExecFwk::ElementName element_;
51         Security::AccessToken::AccessTokenID callerToken_ = 0;
52         sptr<KITS::IReaderModeCallback> callback_ = nullptr;
53     };
54 
55     void ResetCurrPollingParams();
56     std::shared_ptr<NfcPollingParams> GetCurrentParameters();
57     std::shared_ptr<NfcPollingParams> GetPollingParameters(int screenState);
58 
59     // polling
60     void StartPollingLoop(bool force);
61     // screen changed
62     void HandleScreenChanged(int screenState);
63     // package updated
64     bool HandlePackageUpdated(std::shared_ptr<EventFwk::CommonEventData> data);
65 
66     bool EnableForegroundDispatch(AppExecFwk::ElementName &element, const std::vector<uint32_t> &discTech,
67                                   const sptr<KITS::IForegroundCallback> &callback);
68     bool DisableForegroundDispatch(const AppExecFwk::ElementName &element);
69     bool DisableForegroundByDeathRcpt();
70     bool IsForegroundEnabled();
71     void SendTagToForeground(KITS::TagInfoParcelable* tagInfo);
72     std::shared_ptr<NfcPollingManager::ForegroundRegistryData> GetForegroundData();
73 
74     bool EnableReaderMode(AppExecFwk::ElementName &element, std::vector<uint32_t> &discTech,
75                           const sptr<KITS::IReaderModeCallback> &callback);
76     bool DisableReaderMode(AppExecFwk::ElementName &element);
77     bool DisableReaderModeByDeathRcpt();
78     bool IsReaderModeEnabled();
79     void SendTagToReaderApp(KITS::TagInfoParcelable* tagInfo);
80     std::shared_ptr<NfcPollingManager::ReaderModeRegistryData> GetReaderModeData();
81 
82 private:
83     int screenState_ = 0;
84     std::shared_ptr<NfcPollingManager::ForegroundRegistryData> foregroundData_ {};
85     std::shared_ptr<NfcPollingManager::ReaderModeRegistryData> readerModeData_ {};
86     std::shared_ptr<NfcPollingParams> currPollingParams_ {};
87     std::weak_ptr<NfcService> nfcService_ {};
88     std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy_ {};
89     std::weak_ptr<NCI::INciTagInterface> nciTagProxy_ {};
90 
91     // lock
92     std::mutex mutex_ {};
93 };
94 } // namespace NFC
95 } // namespace OHOS
96 #endif // NFC_POLLING_MANAGER_H