/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef NAPI_BLUETOOTH_EVENT_H_ #define NAPI_BLUETOOTH_EVENT_H_ #include #include #include namespace OHOS { namespace Bluetooth { class AsyncEventData : public BluetoothCallbackInfo { public: std::function packResult; explicit AsyncEventData(const std::shared_ptr &cb, std::function p) { this->env_ = cb->env_; this->callback_ = cb->callback_; this->state_ = cb->state_; this->deviceId_ = cb->deviceId_; this->info_ = cb->info_; packResult = p; } AsyncEventData() = delete; virtual ~AsyncEventData() { } }; void UpdateCallbackInfo(std::map> callbackInfos, const std::string &type); class NapiEvent { public: static napi_value CreateResult(const std::shared_ptr &cb, int value); static napi_value CreateResult(const std::shared_ptr &cb, BluetoothOppTransferInformation &information); static napi_value OnEvent(napi_env env, napi_callback_info cbinfo, std::map> &callbackInfos); static napi_value OffEvent(napi_env env, napi_callback_info cbinfo, std::map> &callbackInfos); static void EventNotify(AsyncEventData *asyncEvent); template static void CheckAndNotify(const std::shared_ptr &cb, const T& obj) { if (cb == nullptr) { HILOGI("checkAndNotify event cb is nullptr!"); return; } auto func = std::bind( [](const std::shared_ptr &cb, T& obj) -> napi_value { return NapiEvent::CreateResult(cb, obj); }, cb, obj); AsyncEventData *asyncEvent = new (std::nothrow)AsyncEventData(cb, func); if (asyncEvent == nullptr) { HILOGI("asyncEvent is nullptr!"); return; } EventNotify(asyncEvent); } }; } // namespace Bluetooth } // namespace OHOS #endif /* NAPI_BLUETOOTH_EVENT_H_ */