• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "nfc_cardEmulation_controller.h"
17 
18 #include "cj_lambda.h"
19 #include "nfc_cardEmulation_ffi.h"
20 
21 namespace OHOS {
22 namespace NFC {
23 namespace KITS {
24 std::mutex CjNfcCardEmulationController::controllerMutex_;
25 std::shared_ptr<CjNfcCardEmulationController> CjNfcCardEmulationController::controller_ { nullptr };
26 const int8_t HCE_CMD = 0; // defined by cangjie enum NfcEventType
27 
GetInstance()28 std::shared_ptr<CjNfcCardEmulationController> CjNfcCardEmulationController::GetInstance()
29 {
30     if (controller_ == nullptr) {
31         std::lock_guard<std::mutex> lock(controllerMutex_);
32         if (controller_ == nullptr) {
33             auto controller = std::make_shared<CjNfcCardEmulationController>();
34             controller_ = controller;
35         }
36     }
37     return controller_;
38 }
39 
Subscribe(int8_t type,int64_t id)40 int32_t CjNfcCardEmulationController::Subscribe(int8_t type, int64_t id)
41 {
42     RegisterListener(type, id);
43     return SUCCESS_CODE;
44 }
45 
UnSubscribe(int8_t type)46 int32_t CjNfcCardEmulationController::UnSubscribe(int8_t type)
47 {
48     UnRegisterListener(type);
49     return SUCCESS_CODE;
50 }
51 
RegisterListener(int8_t type,int64_t id)52 void CjNfcCardEmulationController::RegisterListener(int8_t type, int64_t id)
53 {
54     std::lock_guard<std::recursive_mutex> lock(mutex_);
55     switch (type) {
56         case HCE_CMD:
57             InitHceCmd(id);
58             break;
59         default:
60             return;
61     }
62 }
63 
UnRegisterListener(int8_t type)64 void CjNfcCardEmulationController::UnRegisterListener(int8_t type)
65 {
66     std::lock_guard<std::recursive_mutex> lock(mutex_);
67     switch (type) {
68         case HCE_CMD:
69             hceCmd_ = nullptr;
70             break;
71         default:
72             return;
73     }
74 }
75 
InitHceCmd(int64_t id)76 void CjNfcCardEmulationController::InitHceCmd(int64_t id)
77 {
78     auto callback = reinterpret_cast<void (*)(const uint8_t*, int64_t)>(id);
79     hceCmd_ = [lambda = CJLambda::Create(callback)](const uint8_t* head, int64_t size) -> void { lambda(head, size); };
80 }
81 
HceCmd(const std::vector<uint8_t> & data)82 void CjNfcCardEmulationController::HceCmd(const std::vector<uint8_t>& data)
83 {
84     std::lock_guard<std::recursive_mutex> lock(mutex_);
85     if (hceCmd_ == nullptr) {
86         return;
87     }
88     hceCmd_(data.data(), data.size());
89     return;
90 }
91 
92 } // namespace KITS
93 } // namespace NFC
94 } // namespace OHOS