mirror of
https://gitee.com/openharmony/msdp_device_status
synced 2025-03-01 12:05:55 +00:00
commit
9981c39648
@ -37,6 +37,7 @@ ohos_shared_library("stationary") {
|
||||
sources = [
|
||||
"${device_status_frameworks_path}/js/napi/src/devicestatus_event.cpp",
|
||||
"${device_status_frameworks_path}/js/napi/src/devicestatus_napi.cpp",
|
||||
"${device_status_frameworks_path}/js/napi/src/devicestatus_napi_error.cpp",
|
||||
]
|
||||
|
||||
configs = [
|
||||
|
@ -17,10 +17,11 @@
|
||||
#define DEVICE_STATUS_NAPI_H
|
||||
|
||||
#include <map>
|
||||
#include <uv.h>
|
||||
#include <tuple>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include <uv.h>
|
||||
|
||||
#include "devicestatus_data_utils.h"
|
||||
#include "devicestatus_callback_stub.h"
|
||||
@ -48,7 +49,7 @@ public:
|
||||
|
||||
static napi_value Init(napi_env env, napi_value exports);
|
||||
static napi_value SubscribeDeviceStatus(napi_env env, napi_callback_info info);
|
||||
static napi_value SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value *args,
|
||||
static napi_value SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value handler,
|
||||
int32_t type, int32_t event, int32_t latency);
|
||||
static napi_value UnsubscribeDeviceStatus(napi_env env, napi_callback_info info);
|
||||
static napi_value GetDeviceStatus(napi_env env, napi_callback_info info);
|
||||
@ -64,6 +65,11 @@ private:
|
||||
static bool CheckArguments(napi_env env, napi_callback_info info);
|
||||
static bool CheckUnsubArguments(napi_env env, napi_callback_info info);
|
||||
static bool CheckGetArguments(napi_env env, napi_callback_info info);
|
||||
static std::tuple<bool, napi_value, std::string, int32_t, int32_t> CheckSubscribeParam(napi_env env,
|
||||
napi_callback_info info);
|
||||
static std::tuple<bool, napi_value, int32_t, int32_t> CheckUnsubscribeParam(napi_env env,
|
||||
napi_callback_info info);
|
||||
static std::tuple<bool, napi_value, int32_t> CheckGetParam(napi_env env, napi_callback_info info);
|
||||
napi_ref callbackRef_ = { nullptr };
|
||||
static napi_ref devicestatusValueRef_;
|
||||
napi_env env_ = { nullptr };
|
||||
|
42
frameworks/js/napi/include/devicestatus_napi_error.h
Normal file
42
frameworks/js/napi/include/devicestatus_napi_error.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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 MSDP_DEVICE_STATUS_DEVICESTATUS_NAPI_ERROR_H
|
||||
#define MSDP_DEVICE_STATUS_DEVICESTATUS_NAPI_ERROR_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
|
||||
#include "devicestatus_hilog_wrapper.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Msdp {
|
||||
namespace DeviceStatus {
|
||||
const int32_t SERVICE_EXCEPTION = 201;
|
||||
const int32_t PARAM_ERROR = 202;
|
||||
const std::map <int32_t, std::string> ERROR_MESSAGES = {
|
||||
{SERVICE_EXCEPTION, "Service exception."},
|
||||
{PARAM_ERROR, "Param error."},
|
||||
};
|
||||
|
||||
napi_value CreateNapiError(const napi_env &env, const int32_t errCode, const std::string &errMessage);
|
||||
std::optional <std::string> GetErrMsg(int32_t errorCode);
|
||||
void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg);
|
||||
} // namespace DeviceStatus
|
||||
} // namespace Msdp
|
||||
} // namespace OHOS
|
||||
#endif //MSDP_DEVICE_STATUS_DEVICESTATUS_NAPI_ERROR_H
|
@ -22,6 +22,7 @@
|
||||
|
||||
#include "devicestatus_client.h"
|
||||
#include "devicestatus_common.h"
|
||||
#include "devicestatus_napi_error.h"
|
||||
|
||||
using namespace OHOS;
|
||||
using namespace OHOS::Msdp;
|
||||
@ -236,7 +237,129 @@ bool DeviceStatusNapi::CheckGetArguments(napi_env env, napi_callback_info info)
|
||||
return true;
|
||||
}
|
||||
|
||||
napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value *args,
|
||||
std::tuple<bool, napi_value, std::string, int32_t, int32_t> DeviceStatusNapi::CheckSubscribeParam(napi_env env,
|
||||
napi_callback_info info)
|
||||
{
|
||||
std::tuple<bool, napi_value, std::string, int32_t, int32_t> result { false, nullptr, "", -1, -1 };
|
||||
size_t argc = ARG_4;
|
||||
napi_value args[ARG_4] = {};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
if ((status != napi_ok) || (argc < ARG_4)) {
|
||||
ThrowErr(env, PARAM_ERROR, "Bad parameters");
|
||||
return result;
|
||||
}
|
||||
if (!CheckArguments(env, info)) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get on arguments");
|
||||
return result;
|
||||
}
|
||||
size_t modLen = 0;
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], nullptr, 0, &modLen);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get string item");
|
||||
return result;
|
||||
}
|
||||
char mode[NAPI_BUF_LENGTH] = {};
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], mode, modLen + 1, &modLen);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get mode");
|
||||
return result;
|
||||
}
|
||||
int32_t eventMode = 0;
|
||||
status = napi_get_value_int32(env, args[ARG_1], &eventMode);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get event value item");
|
||||
return result;
|
||||
}
|
||||
int32_t latencyMode = 0;
|
||||
status = napi_get_value_int32(env, args[ARG_2], &latencyMode);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get latency value item");
|
||||
return result;
|
||||
}
|
||||
return std::make_tuple(true, args[ARG_3], std::string(mode), eventMode, latencyMode);
|
||||
}
|
||||
|
||||
std::tuple<bool, napi_value, int32_t, int32_t> DeviceStatusNapi::CheckUnsubscribeParam(napi_env env,
|
||||
napi_callback_info info)
|
||||
{
|
||||
std::tuple<bool, napi_value, int32_t, int32_t> result { false, nullptr, -1, -1 };
|
||||
size_t argc = ARG_3;
|
||||
napi_value args[ARG_3] = {};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
if ((status != napi_ok) || (argc < ARG_3)) {
|
||||
ThrowErr(env, PARAM_ERROR, "Bad parameters");
|
||||
return result;
|
||||
}
|
||||
if (!CheckUnsubArguments(env, info)) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get off arguments");
|
||||
return result;
|
||||
}
|
||||
size_t modLen = 0;
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], nullptr, 0, &modLen);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get string item");
|
||||
return result;
|
||||
}
|
||||
char mode[NAPI_BUF_LENGTH] = {};
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], mode, modLen + 1, &modLen);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get mode");
|
||||
return result;
|
||||
}
|
||||
int32_t type = DeviceStatusNapi::ConvertTypeToInt(mode);
|
||||
if ((type < Type::TYPE_ABSOLUTE_STILL) || (type > Type::TYPE_LID_OPEN)) {
|
||||
ThrowErr(env, PARAM_ERROR, "type is illegal");
|
||||
return result;
|
||||
}
|
||||
int32_t event = 0;
|
||||
status = napi_get_value_int32(env, args[ARG_1], &event);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get int32 item");
|
||||
return result;
|
||||
}
|
||||
if ((event < ActivityEvent::ENTER) || (event > ActivityEvent::ENTER_EXIT)) {
|
||||
ThrowErr(env, PARAM_ERROR, "event is illegal");
|
||||
return result;
|
||||
}
|
||||
DEV_HILOGD(JS_NAPI, "type: %{public}d, event: %{public}d", type, event);
|
||||
return std::make_tuple(true, args[ARG_2], type, event);
|
||||
}
|
||||
|
||||
std::tuple<bool, napi_value, int32_t> DeviceStatusNapi::CheckGetParam(napi_env env, napi_callback_info info)
|
||||
{
|
||||
std::tuple<bool, napi_value, int32_t> result { false, nullptr, -1 };
|
||||
size_t argc = ARG_2;
|
||||
napi_value args[ARG_2] = {};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
if ((status != napi_ok) || (argc < ARG_2)) {
|
||||
ThrowErr(env, PARAM_ERROR, "Bad parameters");
|
||||
return result;
|
||||
}
|
||||
if (!CheckGetArguments(env, info)) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get once arguments");
|
||||
return result;
|
||||
}
|
||||
size_t modLen = 0;
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], nullptr, 0, &modLen);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get string item");
|
||||
return result;
|
||||
}
|
||||
char mode[NAPI_BUF_LENGTH] = {};
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], mode, modLen + 1, &modLen);
|
||||
if (status != napi_ok) {
|
||||
ThrowErr(env, PARAM_ERROR, "Failed to get mode");
|
||||
return result;
|
||||
}
|
||||
int32_t type = ConvertTypeToInt(mode);
|
||||
if ((type < Type::TYPE_ABSOLUTE_STILL) || (type > Type::TYPE_LID_OPEN)) {
|
||||
ThrowErr(env, PARAM_ERROR, "type is illegal");
|
||||
return result;
|
||||
}
|
||||
return std::make_tuple(true, args[ARG_1], type);
|
||||
}
|
||||
|
||||
napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value handler,
|
||||
int32_t type, int32_t event, int32_t latency)
|
||||
{
|
||||
if (g_obj == nullptr) {
|
||||
@ -255,7 +378,7 @@ napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_ca
|
||||
delete devicestatus;
|
||||
},
|
||||
nullptr, &(g_obj->callbackRef_));
|
||||
if (!g_obj->On(type, args[ARG_3], false)) {
|
||||
if (!g_obj->On(type, handler, false)) {
|
||||
DEV_HILOGE(JS_NAPI, "type:%{public}d already exists", type);
|
||||
return nullptr;
|
||||
}
|
||||
@ -270,8 +393,12 @@ napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_ca
|
||||
DEV_HILOGE(JS_NAPI, "callback is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
DeviceStatusClient::GetInstance().SubscribeCallback(Type(type),
|
||||
auto subscribeRet = DeviceStatusClient::GetInstance().SubscribeCallback(Type(type),
|
||||
ActivityEvent(event), ReportLatencyNs(latency), callback);
|
||||
if (subscribeRet != RET_OK) {
|
||||
ThrowErr(env, SERVICE_EXCEPTION, "on: Failed to SubscribeCallback");
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = callbackMap_.insert(std::pair<int32_t, sptr<IRemoteDevStaCallback>>(type, callback));
|
||||
if (!ret.second) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to insert");
|
||||
@ -284,91 +411,47 @@ napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_ca
|
||||
napi_value DeviceStatusNapi::SubscribeDeviceStatus(napi_env env, napi_callback_info info)
|
||||
{
|
||||
DEV_HILOGD(JS_NAPI, "Enter");
|
||||
size_t argc = ARG_4;
|
||||
napi_value args[ARG_4] = {};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
NAPI_ASSERT(env, (status == napi_ok) && (argc >= ARG_4), "Bad parameters");
|
||||
if (!CheckArguments(env, info)) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get arguements");
|
||||
return nullptr;
|
||||
}
|
||||
size_t modeLen = 0;
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], nullptr, 0, &modeLen);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get string item");
|
||||
return nullptr;
|
||||
}
|
||||
char mode[NAPI_BUF_LENGTH] = {};
|
||||
status = napi_get_value_string_utf8(env, args[ARG_0], mode, modeLen + 1, &modeLen);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get string item");
|
||||
return nullptr;
|
||||
}
|
||||
std::string typeMode = mode;
|
||||
int32_t eventMode = 0;
|
||||
status = napi_get_value_int32(env, args[ARG_1], &eventMode);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get event value item");
|
||||
return nullptr;
|
||||
}
|
||||
int32_t latencyMode = 0;
|
||||
status = napi_get_value_int32(env, args[ARG_2], &latencyMode);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get latency value item");
|
||||
const auto [ret, handler, typeMode, event, latency] = CheckSubscribeParam(env, info);
|
||||
if (!ret) {
|
||||
DEV_HILOGE(JS_NAPI, "on: SubscribeDeviceStatus is failed");
|
||||
return nullptr;
|
||||
}
|
||||
int32_t type = ConvertTypeToInt(typeMode);
|
||||
int32_t event = eventMode;
|
||||
int32_t latency = latencyMode;
|
||||
DEV_HILOGD(JS_NAPI, "type:%{public}d, event:%{public}d, latency:%{public}d", type, event, latency);
|
||||
|
||||
NAPI_ASSERT(env, (type >= Type::TYPE_ABSOLUTE_STILL) && (type <= Type::TYPE_LID_OPEN), "type is illegal");
|
||||
NAPI_ASSERT(env, (event >= ActivityEvent::ENTER) && (event <= ActivityEvent::ENTER_EXIT), "event is illegal");
|
||||
NAPI_ASSERT(env, (latency >= ReportLatencyNs::SHORT) && (latency <= ReportLatencyNs::LONG), "latency is illegal");
|
||||
return SubscribeDeviceStatusCallback(env, info, args, type, event, latency);
|
||||
if ((type < Type::TYPE_ABSOLUTE_STILL) || (type > Type::TYPE_LID_OPEN)) {
|
||||
ThrowErr(env, PARAM_ERROR, "type is illegal");
|
||||
return nullptr;
|
||||
}
|
||||
if ((event < ActivityEvent::ENTER) || (event > ActivityEvent::ENTER_EXIT)) {
|
||||
ThrowErr(env, PARAM_ERROR, "event is illegal");
|
||||
return nullptr;
|
||||
}
|
||||
if ((latency < ReportLatencyNs::SHORT) || (latency > ReportLatencyNs::LONG)) {
|
||||
ThrowErr(env, PARAM_ERROR, "latency is illegal");
|
||||
return nullptr;
|
||||
}
|
||||
return SubscribeDeviceStatusCallback(env, info, handler, type, event, latency);
|
||||
}
|
||||
|
||||
napi_value DeviceStatusNapi::UnsubscribeDeviceStatus(napi_env env, napi_callback_info info)
|
||||
{
|
||||
DEV_HILOGD(JS_NAPI, "Enter");
|
||||
size_t argc = ARG_3;
|
||||
napi_value args[ARG_3] = {};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
NAPI_ASSERT(env, (status == napi_ok) && (argc >= ARG_3), "Bad parameters");
|
||||
if (!CheckUnsubArguments(env, info)) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get unsub arguements");
|
||||
const auto [ret, handler, type, event] = CheckUnsubscribeParam(env, info);
|
||||
if (!ret) {
|
||||
DEV_HILOGE(JS_NAPI, "off: UnsubscribeDeviceStatus is failed");
|
||||
return nullptr;
|
||||
}
|
||||
size_t len;
|
||||
status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &len);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get string item");
|
||||
return nullptr;
|
||||
}
|
||||
std::vector<char> typeBuf(len + 1);
|
||||
status = napi_get_value_string_utf8(env, args[0], typeBuf.data(), len + 1, &len);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get string item");
|
||||
return nullptr;
|
||||
}
|
||||
int32_t type = ConvertTypeToInt(typeBuf.data());
|
||||
NAPI_ASSERT(env, (type >= Type::TYPE_ABSOLUTE_STILL) && (type <= Type::TYPE_LID_OPEN), "type is illegal");
|
||||
int32_t eventMode = 0;
|
||||
status = napi_get_value_int32(env, args[ARG_1], &eventMode);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get int32 item");
|
||||
return nullptr;
|
||||
}
|
||||
int32_t event = eventMode;
|
||||
NAPI_ASSERT(env, (event >= ActivityEvent::ENTER) && (event <= ActivityEvent::ENTER_EXIT), "event is illegal");
|
||||
DEV_HILOGD(JS_NAPI, "type:%{public}d, event:%{public}d", type, event);
|
||||
if (!g_obj->Off(type, args[ARG_2])) {
|
||||
if (!g_obj->Off(type, handler)) {
|
||||
DEV_HILOGE(JS_NAPI, "Not ready to Unsubscribe for type:%{public}d", type);
|
||||
return nullptr;
|
||||
}
|
||||
auto callbackIter = callbackMap_.find(type);
|
||||
if (callbackIter != callbackMap_.end()) {
|
||||
DeviceStatusClient::GetInstance().UnsubscribeCallback(Type(type), ActivityEvent(event), callbackIter->second);
|
||||
auto unsubscribeRet = DeviceStatusClient::GetInstance().UnsubscribeCallback(Type(type),
|
||||
ActivityEvent(event), callbackIter->second);
|
||||
if (unsubscribeRet != RET_OK) {
|
||||
ThrowErr(env, SERVICE_EXCEPTION, "off: Failed to UnsubscribeCallback");
|
||||
}
|
||||
callbackMap_.erase(type);
|
||||
} else {
|
||||
NAPI_ASSERT(env, false, "No existed callback");
|
||||
@ -381,28 +464,11 @@ napi_value DeviceStatusNapi::UnsubscribeDeviceStatus(napi_env env, napi_callback
|
||||
napi_value DeviceStatusNapi::GetDeviceStatus(napi_env env, napi_callback_info info)
|
||||
{
|
||||
DEV_HILOGD(JS_NAPI, "Enter");
|
||||
size_t argc = ARG_2;
|
||||
napi_value args[ARG_2] = {};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||
NAPI_ASSERT(env, (status == napi_ok) && (argc >= ARG_2), "Bad parameters");
|
||||
if (!CheckGetArguments(env, info)) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get once arguements");
|
||||
const auto [ret, handler, type] = CheckGetParam(env, info);
|
||||
if (!ret) {
|
||||
DEV_HILOGE(JS_NAPI, "once: GetDeviceStatus is failed");
|
||||
return nullptr;
|
||||
}
|
||||
size_t len;
|
||||
status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &len);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get string item");
|
||||
return nullptr;
|
||||
}
|
||||
std::vector<char> typeBuf(len + 1);
|
||||
status = napi_get_value_string_utf8(env, args[0], typeBuf.data(), len + 1, &len);
|
||||
if (status != napi_ok) {
|
||||
DEV_HILOGE(JS_NAPI, "Failed to get string item");
|
||||
return nullptr;
|
||||
}
|
||||
int32_t type = ConvertTypeToInt(typeBuf.data());
|
||||
NAPI_ASSERT(env, (type >= Type::TYPE_ABSOLUTE_STILL) && (type <= Type::TYPE_LID_OPEN), "type is illegal");
|
||||
if (g_obj == nullptr) {
|
||||
g_obj = new (std::nothrow) DeviceStatusNapi(env);
|
||||
if (g_obj == nullptr) {
|
||||
@ -418,13 +484,16 @@ napi_value DeviceStatusNapi::GetDeviceStatus(napi_env env, napi_callback_info in
|
||||
},
|
||||
nullptr, &(g_obj->callbackRef_));
|
||||
}
|
||||
if (!g_obj->On(type, args[ARG_1], true)) {
|
||||
if (!g_obj->On(type, handler, true)) {
|
||||
DEV_HILOGE(JS_NAPI, "type:%{public}d already exists", type);
|
||||
return nullptr;
|
||||
}
|
||||
Data devicestatusData = DeviceStatusClient::GetInstance().GetDeviceStatusData(Type(type));
|
||||
if (devicestatusData.type == Type::TYPE_INVALID) {
|
||||
ThrowErr(env, SERVICE_EXCEPTION, "once: Failed to GetDeviceStatusData");
|
||||
}
|
||||
g_obj->OnDeviceStatusChangedDone(devicestatusData.type, devicestatusData.value, true);
|
||||
g_obj->OffOnce(devicestatusData.type, args[ARG_1]);
|
||||
g_obj->OffOnce(devicestatusData.type, handler);
|
||||
DEV_HILOGD(JS_NAPI, "Exit");
|
||||
return nullptr;
|
||||
}
|
||||
|
54
frameworks/js/napi/src/devicestatus_napi_error.cpp
Normal file
54
frameworks/js/napi/src/devicestatus_napi_error.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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.
|
||||
*/
|
||||
|
||||
#include "devicestatus_napi_error.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace OHOS {
|
||||
namespace Msdp {
|
||||
namespace DeviceStatus {
|
||||
napi_value CreateNapiError(const napi_env &env, const int32_t errCode, const std::string &errMessage) {
|
||||
napi_value businessError = nullptr;
|
||||
napi_value code = nullptr;
|
||||
napi_value msg = nullptr;
|
||||
NAPI_CALL(env, napi_create_int32(env, errCode, &code));
|
||||
NAPI_CALL(env, napi_create_string_utf8(env, errMessage.c_str(), NAPI_AUTO_LENGTH, &msg));
|
||||
napi_create_error(env, nullptr, msg, &businessError);
|
||||
napi_set_named_property(env, businessError, "code", code);
|
||||
return businessError;
|
||||
}
|
||||
|
||||
std::optional <std::string> GetErrMsg(int32_t errorCode) {
|
||||
auto iter = ERROR_MESSAGES.find(errorCode);
|
||||
if (iter != ERROR_MESSAGES.end()) {
|
||||
return iter->second;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg) {
|
||||
DEV_HILOGE(JS_NAPI, "message: %{public}s, code: %{public}d", printMsg.c_str(), errCode);
|
||||
auto msg = GetErrMsg(errCode);
|
||||
if (!msg) {
|
||||
DEV_HILOGE(JS_NAPI, "errCode: %{public}d is invalid", errCode);
|
||||
return;
|
||||
}
|
||||
napi_value error = CreateNapiError(env, errCode, msg.value());
|
||||
napi_throw(env, error);
|
||||
}
|
||||
} // namespace DeviceStatus
|
||||
} // namespace Msdp
|
||||
} // namespace OHOS
|
@ -113,44 +113,58 @@ void DeviceStatusClient::DeviceStatusDeathRecipient::OnRemoteDied(const wptr<IRe
|
||||
DEV_HILOGD(INNERKIT, "Recv death notice");
|
||||
}
|
||||
|
||||
void DeviceStatusClient::SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency,
|
||||
int32_t DeviceStatusClient::SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency,
|
||||
sptr<IRemoteDevStaCallback> callback)
|
||||
{
|
||||
DEV_HILOGI(INNERKIT, "Enter event:%{public}d,latency:%{public}d", event, latency);
|
||||
typeMap_.insert(std::make_pair(type, 1));
|
||||
DEV_HILOGD(INNERKIT, "typeMap_ %{public}d, type: %{public}d", typeMap_[type], type);
|
||||
DEV_RET_IF_NULL((callback == nullptr) || (Connect() != RET_OK));
|
||||
if (callback == nullptr) {
|
||||
DEV_HILOGE(SERVICE, "callback is nullptr");
|
||||
return RET_ERR;
|
||||
}
|
||||
if (Connect() != RET_OK) {
|
||||
DEV_HILOGE(SERVICE, "Connect failed");
|
||||
return RET_ERR;
|
||||
}
|
||||
if (devicestatusProxy_ == nullptr) {
|
||||
DEV_HILOGE(SERVICE, "devicestatusProxy_ is nullptr");
|
||||
return;
|
||||
return RET_ERR;
|
||||
}
|
||||
if (type > Type::TYPE_INVALID && type <= Type::TYPE_LID_OPEN) {
|
||||
devicestatusProxy_->Subscribe(type, event, latency, callback);
|
||||
}
|
||||
return;
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void DeviceStatusClient::UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
|
||||
int32_t DeviceStatusClient::UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
|
||||
{
|
||||
DEV_HILOGI(INNERKIT, "UNevent: %{public}d", event);
|
||||
typeMap_.erase(type);
|
||||
DEV_HILOGD(INNERKIT, "typeMap_ %{public}d", typeMap_[type]);
|
||||
DEV_RET_IF_NULL((callback == nullptr) || (Connect() != RET_OK));
|
||||
if (callback == nullptr) {
|
||||
DEV_HILOGE(SERVICE, "callback is nullptr");
|
||||
return RET_ERR;
|
||||
}
|
||||
if (Connect() != RET_OK) {
|
||||
DEV_HILOGE(SERVICE, "Connect failed");
|
||||
return RET_ERR;
|
||||
}
|
||||
if (devicestatusProxy_ == nullptr) {
|
||||
DEV_HILOGE(SERVICE, "devicestatusProxy_ is nullptr");
|
||||
return;
|
||||
return RET_ERR;
|
||||
}
|
||||
if ((type < TYPE_INVALID) || (type > TYPE_MAX)) {
|
||||
DEV_HILOGE(INNERKIT, "type out of range");
|
||||
return;
|
||||
return RET_ERR;
|
||||
}
|
||||
if (event < ActivityEvent::EVENT_INVALID || event > ActivityEvent::ENTER_EXIT) {
|
||||
DEV_HILOGE(INNERKIT, "event out of range");
|
||||
return;
|
||||
return RET_ERR;
|
||||
}
|
||||
devicestatusProxy_->Unsubscribe(type, event, callback);
|
||||
DEV_HILOGD(INNERKIT, "Exit");
|
||||
return;
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
Data DeviceStatusClient::GetDeviceStatusData(Type type)
|
||||
@ -159,8 +173,10 @@ Data DeviceStatusClient::GetDeviceStatusData(Type type)
|
||||
Data devicestatusData;
|
||||
devicestatusData.type = Type::TYPE_INVALID;
|
||||
devicestatusData.value = OnChangedValue::VALUE_INVALID;
|
||||
|
||||
DEV_RET_IF_NULL_WITH_RET((Connect() != RET_OK), devicestatusData);
|
||||
if (Connect() != RET_OK) {
|
||||
DEV_HILOGE(SERVICE, "Connect failed");
|
||||
return devicestatusData;
|
||||
}
|
||||
if (devicestatusProxy_ == nullptr) {
|
||||
DEV_HILOGE(SERVICE, "devicestatusProxy_ is nullptr");
|
||||
return devicestatusData;
|
||||
|
@ -41,9 +41,9 @@ public:
|
||||
}
|
||||
DISALLOW_COPY_AND_MOVE(DeviceStatusClient);
|
||||
|
||||
void SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency,
|
||||
int32_t SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency,
|
||||
sptr<IRemoteDevStaCallback> callback);
|
||||
void UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback);
|
||||
int32_t UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback);
|
||||
Data GetDeviceStatusData(const Type type);
|
||||
void RegisterDeathListener(std::function<void()> deathListener);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user