• 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 "napi_util.h"
17 #include "securec.h"
18 #include "string_ex.h"
19 #include "common_utils.h"
20 #include "location_log.h"
21 #include "beacon_fence_napi.h"
22 
23 namespace OHOS {
24 namespace Location {
25 static constexpr int MAX_BUF_LEN = 100;
26 
ParseBeaconFenceRequest(const napi_env & env,const napi_value & value,std::shared_ptr<BeaconFenceRequest> & request)27 bool ParseBeaconFenceRequest(
28     const napi_env& env, const napi_value& value, std::shared_ptr<BeaconFenceRequest>& request)
29 {
30     napi_valuetype valueType;
31     NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false);
32     if (valueType != napi_object) {
33         LBSLOGE(NAPI_UTILS, "Wrong argument type, value should be object");
34         return false;
35     }
36     return GenBeaconFenceRequest(env, value, request);
37 }
38 
GenBeaconFenceRequest(const napi_env & env,const napi_value & value,std::shared_ptr<BeaconFenceRequest> & beaconFenceRequest)39 bool GenBeaconFenceRequest(
40     const napi_env& env, const napi_value& value, std::shared_ptr<BeaconFenceRequest>& beaconFenceRequest)
41 {
42     if (beaconFenceRequest == nullptr) {
43         LBSLOGE(NAPI_UTILS, "beaconFenceRequest == nullptr");
44         return false;
45     }
46     napi_value beaconValue = GetNapiValueByKey(env, "beacon", value);
47     if (beaconValue == nullptr) {
48         LBSLOGE(LOCATOR_STANDARD, "parse beacon failed");
49         return false;
50     }
51     std::shared_ptr<BeaconFence> beaconFence = std::make_shared<BeaconFence>();
52     bool isValidParameter = JsObjToBeaconFence(env, beaconValue, beaconFence);
53     if (!isValidParameter) {
54         return false;
55     }
56     beaconFenceRequest->SetBeaconFence(beaconFence);
57     std::string fenceExtensionAbilityName = "";
58     int fenceExtensionAbilityNameRes =
59         JsObjectToString(env, value, "fenceExtensionAbilityName", MAX_BUF_LEN, fenceExtensionAbilityName);
60     if (fenceExtensionAbilityNameRes == SUCCESS) {
61         beaconFenceRequest->SetFenceExtensionAbilityName(fenceExtensionAbilityName);
62     }
63     napi_value callbackNapiValue = nullptr;
64     NAPI_CALL_BASE(env,
65         napi_get_named_property(env, value, "transitionCallback", &callbackNapiValue), false);
66     napi_valuetype napiValueType = napi_undefined;
67     NAPI_CALL_BASE(env, napi_typeof(env, callbackNapiValue, &napiValueType), false);
68     if (napiValueType == napi_undefined && fenceExtensionAbilityNameRes != SUCCESS) {
69         LBSLOGE(NAPI_UTILS, "%{public}s, parse transitionCallback and fenceExtensionAbilityName failed", __func__);
70         return false;
71     }
72     return true;
73 }
74 
JsObjToBeaconFenceTransitionCallback(const napi_env & env,const napi_value & object,sptr<LocationGnssGeofenceCallbackNapi> callbackHost)75 void JsObjToBeaconFenceTransitionCallback(const napi_env& env, const napi_value& object,
76     sptr<LocationGnssGeofenceCallbackNapi> callbackHost)
77 {
78     napi_ref handlerRef = nullptr;
79     napi_value callbackNapiValue = nullptr;
80     NAPI_CALL_RETURN_VOID(env,
81         napi_get_named_property(env, object, "transitionCallback", &callbackNapiValue));
82     NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, callbackNapiValue, 1, &handlerRef));
83     callbackHost->SetEnv(env);
84     callbackHost->SetHandleCb(handlerRef);
85 }
86 
JsObjToBeaconFence(const napi_env & env,const napi_value & object,const std::shared_ptr<BeaconFence> & beaconFence)87 bool JsObjToBeaconFence(const napi_env& env, const napi_value& object,
88     const std::shared_ptr<BeaconFence>& beaconFence)
89 {
90     if (JsObjectToString(env, object, "identifier", MAX_BUF_LEN, beaconFence->identifier_) != SUCCESS) {
91         LBSLOGE(LOCATOR_STANDARD, "parse identifier failed");
92         return false;
93     }
94     int value = 0;
95     if (JsObjectToInt(env, object, "beaconFenceInfoType", value) == SUCCESS) {
96         beaconFence->SetBeaconFenceInfoType(static_cast<BeaconFenceInfoType>(value));
97     } else {
98         LBSLOGE(LOCATOR_STANDARD, "parse beaconFenceInfoType failed");
99         return false;
100     }
101     if (!GenBeaconManufactureDataRequest(env, object, beaconFence)) {
102         LBSLOGE(LOCATOR_STANDARD, "parse ManufactureData failed");
103         return false;
104     }
105     return true;
106 }
107 
GenBeaconManufactureDataRequest(const napi_env & env,const napi_value & object,std::shared_ptr<BeaconFence> beaconFence)108 bool GenBeaconManufactureDataRequest(
109     const napi_env& env, const napi_value& object, std::shared_ptr<BeaconFence> beaconFence)
110 {
111     napi_value manufactureDataValue = GetNapiValueByKey(env, "manufactureData", object);
112     if (manufactureDataValue == nullptr) {
113         LBSLOGE(LOCATOR_STANDARD, "parse manufactureDataValue failed");
114         return false;
115     }
116     BeaconManufactureData beaconManufactureData = {0};
117     if (JsObjectToInt(env, manufactureDataValue, "manufactureId", beaconManufactureData.manufactureId) != SUCCESS) {
118         LBSLOGE(LOCATOR_STANDARD, "parse manufactureId failed");
119         return false;
120     }
121     std::vector<uint8_t> manufactureDataVector {};
122     if (!ParseArrayBufferParams(env, manufactureDataValue, "manufactureData", manufactureDataVector)) {
123         LBSLOGE(LOCATOR_STANDARD, "parse manufactureData failed");
124         return false;
125     }
126     beaconManufactureData.manufactureData = std::move(manufactureDataVector);
127     std::vector<uint8_t> manufactureDataMaskVector {};
128     if (!ParseArrayBufferParams(env, manufactureDataValue, "manufactureDataMask", manufactureDataMaskVector)) {
129         LBSLOGE(LOCATOR_STANDARD, "parse manufactureDataMask failed");
130         return false;
131     }
132     beaconManufactureData.manufactureDataMask = std::move(manufactureDataMaskVector);
133     beaconFence->SetBeaconManufactureData(beaconManufactureData);
134     return true;
135 }
136 
ParseArrayBufferParams(napi_env env,napi_value object,const char * name,std::vector<uint8_t> & outParam)137 bool ParseArrayBufferParams(napi_env env, napi_value object, const char *name, std::vector<uint8_t> &outParam)
138 {
139     bool hasProperty = false;
140     napi_has_named_property(env, object, name, &hasProperty);
141     if (hasProperty) {
142         napi_value property;
143         napi_get_named_property(env, object, name, &property);
144         bool isArrayBuffer = false;
145         napi_is_arraybuffer(env, property, &isArrayBuffer);
146         if (!isArrayBuffer) {
147             LBSLOGE(NAPI_UTILS, "Wrong argument type,arraybuffer");
148             return false;
149         }
150         uint8_t *data = nullptr;
151         size_t size = 0;
152         bool isSuccess = ParseArrayBuffer(env, &data, size, property);
153         if (!isSuccess) {
154             LBSLOGE(NAPI_UTILS, "ParseArrayBuffer faild.");
155             return false;
156         }
157         outParam = std::vector<uint8_t>(data, data + size);
158     }
159     return hasProperty;
160 }
161 
ParseArrayBuffer(napi_env env,uint8_t ** data,size_t & size,napi_value args)162 bool ParseArrayBuffer(napi_env env, uint8_t** data, size_t &size, napi_value args)
163 {
164     napi_status status;
165     napi_valuetype valuetype;
166     napi_typeof(env, args, &valuetype);
167     if (valuetype != napi_object) {
168         LBSLOGE(NAPI_UTILS, "Wrong argument type(%{public}d). object expected.", valuetype);
169         return false;
170     }
171     status = napi_get_arraybuffer_info(env, args, reinterpret_cast<void**>(data), &size);
172     if (status != napi_ok) {
173         LBSLOGE(NAPI_UTILS, "can not get arraybuffer, error is %{public}d", status);
174         return false;
175     }
176     LBSLOGD(NAPI_UTILS, "arraybuffer size is %{public}zu", size);
177     return true;
178 }
179 }  // namespace Location
180 }  // namespace OHOS
181