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 #include "napi/native_api.h"
16 #include <LocationKit/oh_location.h>
17 #include <LocationKit/oh_location_type.h>
18
19 Location_RequestConfig* g_requestConfig = nullptr;
20
OhLocationIsEnabled(napi_env env, napi_callback_info info)21 static napi_value OhLocationIsEnabled(napi_env env, napi_callback_info info)
22 {
23 bool isEnabled = false;
24 auto resultCode = OH_Location_IsLocatingEnabled(&isEnabled);
25 napi_value result = nullptr;
26 napi_get_boolean(env, isEnabled, &result);
27 return result;
28 }
29
reportLocation(Location_Info* location, void* userData)30 void reportLocation(Location_Info* location, void* userData)
31 {
32 auto baseInfo = OH_LocationInfo_GetBasicInfo(location);
33 char additionalInfo[] = "";
34 auto addition = OH_LocationInfo_GetAdditionalInfo(location, additionalInfo, 0);
35 return;
36 }
37
OhLocationStartLocating(napi_env env, napi_callback_info info)38 static napi_value OhLocationStartLocating(napi_env env, napi_callback_info info)
39 {
40 if (g_requestConfig == nullptr) {
41 g_requestConfig = OH_Location_CreateRequestConfig();
42 }
43 OH_LocationRequestConfig_SetUseScene(g_requestConfig, LOCATION_USE_SCENE_NAVIGATION);
44 OH_LocationRequestConfig_SetPowerConsumptionScene(g_requestConfig, LOCATION_HIGH_POWER_CONSUMPTION);
45 OH_LocationRequestConfig_SetInterval(g_requestConfig, 1);
46 OH_LocationRequestConfig_SetCallback(g_requestConfig, reportLocation, (void *)(1));
47 OH_Location_StartLocating(g_requestConfig);
48 int32_t ret = 0;
49 napi_value result = nullptr;
50 napi_create_int32(env, ret, &result);
51 return result;
52 }
53
OhLocationStopLocating(napi_env env, napi_callback_info info)54 static napi_value OhLocationStopLocating(napi_env env, napi_callback_info info)
55 {
56 OH_Location_StopLocating(g_requestConfig);
57 if (g_requestConfig != nullptr) {
58 OH_Location_DestroyRequestConfig(g_requestConfig);
59 g_requestConfig = nullptr;
60 }
61 int32_t ret = 0;
62 napi_value result = nullptr;
63 napi_create_int32(env, ret, &result);
64 return result;
65 }
66
67 EXTERN_C_START
Init(napi_env env, napi_value exports)68 static napi_value Init(napi_env env, napi_value exports)
69 {
70 napi_property_descriptor desc[] = {
71 {"ohLocationIsEnabled", nullptr, OhLocationIsEnabled, nullptr, nullptr, nullptr, napi_default, nullptr},
72 {"ohLocationStartLocating", nullptr, OhLocationStartLocating, nullptr, nullptr, nullptr, napi_default, nullptr},
73 {"ohLocationStopLocating", nullptr, OhLocationStopLocating, nullptr, nullptr, nullptr, napi_default, nullptr},
74 };
75 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
76 return exports;
77 }
78 EXTERN_C_END
79
80 static napi_module geolocationcapi = {
81 .nm_version =1,
82 .nm_flags = 0,
83 .nm_filename = nullptr,
84 // 模块对外接口注册函数
85 .nm_register_func = Init,
86 // 自定义模块
87 .nm_modname = "geolocationcapi",
88 .nm_priv = ((void*)0),
89 .reserved = { 0 },
90 };
91
RegisterEntryModule(void)92 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
93 {
94 napi_module_register(&geolocationcapi);
95 }
96