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 16 #ifndef SCAN_CALLBACK_H 17 #define SCAN_CALLBACK_H 18 #define TDD_ENABLE 1 19 20 #include <mutex> 21 #include <uv.h> 22 #include <functional> 23 #include "napi/native_api.h" 24 #include "scan_callback_stub.h" 25 26 namespace OHOS::Scan { 27 struct CallbackParam { 28 napi_env env; 29 napi_ref ref; 30 std::mutex* mutexPtr; 31 uint32_t state; 32 33 bool isGetSucc; 34 int32_t sizeRead; 35 std::string message; 36 37 ScanDeviceInfoTCP deviceInfoTCP; 38 ScanDeviceInfo deviceInfo; 39 ScanDeviceInfoSync deviceInfoSync; 40 41 void InitialCallbackParam(napi_env &env_, napi_ref &ref_, std::mutex &mutex_); 42 void SetCallbackParam(uint32_t &state, const ScanDeviceInfo &deviceInfo); 43 void SetCallbackSyncParam(uint32_t &state, const ScanDeviceInfoSync &deviceInfoSync); 44 }; 45 46 struct Param { 47 napi_env env; 48 napi_ref callbackRef; 49 }; 50 51 class ScanCallback : public ScanCallbackStub { 52 public: 53 ScanCallback(napi_env env, napi_ref ref); 54 explicit ScanCallback(std::function<void(std::vector<ScanDeviceInfo> &infos)> callbackFunction); 55 virtual ~ScanCallback(); 56 bool OnCallback(uint32_t state, const ScanDeviceInfo &info) override; 57 bool OnCallbackSync(uint32_t state, const ScanDeviceInfoSync &info) override; 58 bool OnGetDevicesList(std::vector<ScanDeviceInfo> &info) override; 59 60 #ifndef TDD_ENABLE 61 private: 62 #endif 63 bool ExecuteNapiEventWork(CallbackParam* param, std::function<void(CallbackParam*)> workFunc); 64 void NapiCallFunction(CallbackParam* cbParam, size_t argcCount, napi_value* callbackValues); 65 napi_env env_; 66 napi_ref ref_; 67 std::function<void(std::vector<ScanDeviceInfo> &infos)> callbackFunction_; 68 std::mutex mutex_; 69 }; 70 } // namespace OHOS::Scan 71 72 #endif // SCAN_CALLBACK_H 73