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 #include "napi_util.h"
17 #include "location_log.h"
18 #include "location_napi_adapter.h"
19 #include "location_napi_event.h"
20 #include "location_napi_system.h"
21
22 namespace OHOS {
23 namespace Location {
24 #ifndef ENABLE_NAPI_MANAGER
25 /*
26 * Module initialization function
27 */
Init(napi_env env,napi_value exports)28 static napi_value Init(napi_env env, napi_value exports)
29 {
30 LBSLOGI(LOCATION_NAPI, "Init,location_napi_entry");
31
32 napi_property_descriptor desc[] = {
33 DECLARE_NAPI_FUNCTION("getLastLocation", GetLastLocation),
34 DECLARE_NAPI_FUNCTION("isLocationEnabled", IsLocationEnabled),
35 DECLARE_NAPI_FUNCTION("requestEnableLocation", RequestEnableLocation),
36 DECLARE_NAPI_FUNCTION("enableLocation", EnableLocation),
37 DECLARE_NAPI_FUNCTION("disableLocation", DisableLocation),
38 DECLARE_NAPI_FUNCTION("getAddressesFromLocation", GetAddressesFromLocation),
39 DECLARE_NAPI_FUNCTION("getAddressesFromLocationName", GetAddressesFromLocationName),
40 DECLARE_NAPI_FUNCTION("isGeoServiceAvailable", IsGeoServiceAvailable),
41 DECLARE_NAPI_FUNCTION("getCachedGnssLocationsSize", GetCachedGnssLocationsSize),
42 DECLARE_NAPI_FUNCTION("flushCachedGnssLocations", FlushCachedGnssLocations),
43 DECLARE_NAPI_FUNCTION("sendCommand", SendCommand),
44 DECLARE_NAPI_FUNCTION("on", On),
45 DECLARE_NAPI_FUNCTION("off", Off),
46 DECLARE_NAPI_FUNCTION("getCurrentLocation", GetCurrentLocation),
47
48 DECLARE_NAPI_FUNCTION("getLocation", GetLocation),
49 DECLARE_NAPI_FUNCTION("getLocationType", GetLocationType),
50 DECLARE_NAPI_FUNCTION("subscribe", Subscribe),
51 DECLARE_NAPI_FUNCTION("unsubscribe", Unsubscribe),
52 DECLARE_NAPI_FUNCTION("getSupportedCoordTypes", GetSupportedCoordTypes),
53 };
54
55 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
56 return exports;
57 }
58
59 static napi_module g_locationModule = {
60 .nm_version = 1,
61 .nm_flags = 0,
62 .nm_filename = nullptr,
63 .nm_register_func = Init,
64 .nm_modname = "geolocation",
65 .nm_priv = ((void *)0),
66 .reserved = { 0 }
67 };
68
69 #else
70
71 /*
72 * Module initialization function
73 */
74 static napi_value InitManager(napi_env env, napi_value exports)
75 {
76 LBSLOGI(LOCATION_NAPI, "Init, location_napi_manager_entry");
77
78 napi_property_descriptor desc[] = {
79 DECLARE_NAPI_FUNCTION("on", On),
80 DECLARE_NAPI_FUNCTION("off", Off),
81 DECLARE_NAPI_FUNCTION("getCurrentLocation", GetCurrentLocation),
82 DECLARE_NAPI_FUNCTION("getLastLocation", GetLastLocation),
83 DECLARE_NAPI_FUNCTION("isLocationEnabled", IsLocationEnabled),
84 DECLARE_NAPI_FUNCTION("enableLocation", EnableLocation),
85 DECLARE_NAPI_FUNCTION("disableLocation", DisableLocation),
86 DECLARE_NAPI_FUNCTION("requestEnableLocation", RequestEnableLocation),
87 DECLARE_NAPI_FUNCTION("isGeocoderAvailable", IsGeoServiceAvailable),
88 DECLARE_NAPI_FUNCTION("getAddressesFromLocation", GetAddressesFromLocation),
89 DECLARE_NAPI_FUNCTION("getAddressesFromLocationName", GetAddressesFromLocationName),
90 DECLARE_NAPI_FUNCTION("isLocationPrivacyConfirmed", IsLocationPrivacyConfirmed),
91 DECLARE_NAPI_FUNCTION("setLocationPrivacyConfirmStatus", SetLocationPrivacyConfirmStatus),
92 DECLARE_NAPI_FUNCTION("getCachedGnssLocationsSize", GetCachedGnssLocationsSize),
93 DECLARE_NAPI_FUNCTION("flushCachedGnssLocations", FlushCachedGnssLocations),
94 DECLARE_NAPI_FUNCTION("sendCommand", SendCommand),
95 DECLARE_NAPI_FUNCTION("getCountryCode", GetIsoCountryCode),
96 DECLARE_NAPI_FUNCTION("enableLocationMock", EnableLocationMock),
97 DECLARE_NAPI_FUNCTION("disableLocationMock", DisableLocationMock),
98 DECLARE_NAPI_FUNCTION("setMockedLocations", SetMockedLocations),
99 DECLARE_NAPI_FUNCTION("enableReverseGeocodingMock", EnableReverseGeocodingMock),
100 DECLARE_NAPI_FUNCTION("disableReverseGeocodingMock", DisableReverseGeocodingMock),
101 DECLARE_NAPI_FUNCTION("setReverseGeocodingMockInfo", SetReverseGeocodingMockInfo),
102 };
103
104 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
105 return exports;
106 }
107
108 static napi_module g_locationManagerModule = {
109 .nm_version = 1,
110 .nm_flags = 0,
111 .nm_filename = nullptr,
112 .nm_register_func = InitManager,
113 .nm_modname = "geoLocationManager",
114 .nm_priv = ((void *)0),
115 .reserved = { 0 }
116 };
117 #endif
118
119 #ifndef ENABLE_NAPI_MANAGER
RegisterGeolocationModule(void)120 extern "C" __attribute__((constructor)) void RegisterGeolocationModule(void)
121 {
122 napi_module_register(&g_locationModule);
123 }
124 #else
RegisterGeolocationManagerModule(void)125 extern "C" __attribute__((constructor)) void RegisterGeolocationManagerModule(void)
126 {
127 napi_module_register(&g_locationManagerModule);
128 }
129 #endif
130 } // namespace Location
131 } // namespace OHOS
132