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 "constant_definition.h"
17 #include "napi_util.h"
18 #include "location_log.h"
19 #include "location_napi_adapter.h"
20 #include "location_napi_event.h"
21 #include "location_napi_system.h"
22
23 namespace OHOS {
24 namespace Location {
LocationRequestPriorityTypeConstructor(napi_env env)25 napi_value LocationRequestPriorityTypeConstructor(napi_env env)
26 {
27 napi_value locationRequestPriority = nullptr;
28 NAPI_CALL(env, napi_create_object(env, &locationRequestPriority));
29 SetEnumPropertyByInteger(env, locationRequestPriority, PRIORITY_UNSET, "UNSET");
30 SetEnumPropertyByInteger(env, locationRequestPriority, PRIORITY_ACCURACY, "ACCURACY");
31 SetEnumPropertyByInteger(env, locationRequestPriority, PRIORITY_LOW_POWER, "LOW_POWER");
32 SetEnumPropertyByInteger(env, locationRequestPriority, PRIORITY_FAST_FIRST_FIX, "FIRST_FIX");
33 return locationRequestPriority;
34 }
35
LocationRequestScenarioTypeConstructor(napi_env env)36 napi_value LocationRequestScenarioTypeConstructor(napi_env env)
37 {
38 napi_value locationRequestScenario = nullptr;
39 NAPI_CALL(env, napi_create_object(env, &locationRequestScenario));
40 SetEnumPropertyByInteger(env, locationRequestScenario, SCENE_UNSET, "UNSET");
41 SetEnumPropertyByInteger(env, locationRequestScenario, SCENE_NAVIGATION, "NAVIGATION");
42 SetEnumPropertyByInteger(env, locationRequestScenario, SCENE_TRAJECTORY_TRACKING, "TRAJECTORY_TRACKING");
43 SetEnumPropertyByInteger(env, locationRequestScenario, SCENE_CAR_HAILING, "CAR_HAILING");
44 SetEnumPropertyByInteger(env, locationRequestScenario, SCENE_DAILY_LIFE_SERVICE, "DAILY_LIFE_SERVICE");
45 SetEnumPropertyByInteger(env, locationRequestScenario, SCENE_NO_POWER, "NO_POWER");
46 return locationRequestScenario;
47 }
48
LocationPrivacyTypeConstructor(napi_env env)49 napi_value LocationPrivacyTypeConstructor(napi_env env)
50 {
51 napi_value locationPrivacyType = nullptr;
52 NAPI_CALL(env, napi_create_object(env, &locationPrivacyType));
53 SetEnumPropertyByInteger(env, locationPrivacyType, PRIVACY_TYPE_OTHERS, "OTHERS");
54 SetEnumPropertyByInteger(env, locationPrivacyType, PRIVACY_TYPE_STARTUP, "STARTUP");
55 SetEnumPropertyByInteger(env, locationPrivacyType, PRIVACY_TYPE_CORE_LOCATION, "CORE_LOCATION");
56 return locationPrivacyType;
57 }
58
CountryCodeTypeConstructor(napi_env env)59 napi_value CountryCodeTypeConstructor(napi_env env)
60 {
61 napi_value countryCodeType = nullptr;
62 NAPI_CALL(env, napi_create_object(env, &countryCodeType));
63 SetEnumPropertyByInteger(env, countryCodeType, COUNTRY_CODE_FROM_LOCALE, "COUNTRY_CODE_FROM_LOCALE");
64 SetEnumPropertyByInteger(env, countryCodeType, COUNTRY_CODE_FROM_SIM, "COUNTRY_CODE_FROM_SIM");
65 SetEnumPropertyByInteger(env, countryCodeType, COUNTRY_CODE_FROM_LOCATION, "COUNTRY_CODE_FROM_LOCATION");
66 SetEnumPropertyByInteger(env, countryCodeType, COUNTRY_CODE_FROM_NETWORK, "COUNTRY_CODE_FROM_NETWORK");
67 return countryCodeType;
68 }
69
GeoLocationErrorCodeTypeConstructor(napi_env env)70 napi_value GeoLocationErrorCodeTypeConstructor(napi_env env)
71 {
72 napi_value geoLocationErrorCode = nullptr;
73 NAPI_CALL(env, napi_create_object(env, &geoLocationErrorCode));
74 SetEnumPropertyByInteger(env, geoLocationErrorCode, INPUT_PARAMS_ERROR, "INPUT_PARAMS_ERROR");
75 SetEnumPropertyByInteger(env, geoLocationErrorCode, REVERSE_GEOCODE_ERROR, "REVERSE_GEOCODE_ERROR");
76 SetEnumPropertyByInteger(env, geoLocationErrorCode, GEOCODE_ERROR, "GEOCODE_ERROR");
77 SetEnumPropertyByInteger(env, geoLocationErrorCode, LOCATOR_ERROR, "LOCATOR_ERROR");
78 SetEnumPropertyByInteger(env, geoLocationErrorCode, LOCATION_SWITCH_ERROR, "LOCATION_SWITCH_ERROR");
79 SetEnumPropertyByInteger(env, geoLocationErrorCode, LAST_KNOWN_LOCATION_ERROR, "LAST_KNOWN_LOCATION_ERROR");
80 SetEnumPropertyByInteger(env, geoLocationErrorCode, LOCATION_REQUEST_TIMEOUT_ERROR, "LOCATION_REQUEST_TIMEOUT_ERROR");
81 return geoLocationErrorCode;
82 }
83
84 #ifndef ENABLE_NAPI_MANAGER
85 /*
86 * Module initialization function
87 */
Init(napi_env env,napi_value exports)88 static napi_value Init(napi_env env, napi_value exports)
89 {
90 LBSLOGI(LOCATION_NAPI, "Init,location_napi_entry");
91
92 napi_property_descriptor desc[] = {
93 DECLARE_NAPI_FUNCTION("getLastLocation", GetLastLocation),
94 DECLARE_NAPI_FUNCTION("isLocationEnabled", IsLocationEnabled),
95 DECLARE_NAPI_FUNCTION("requestEnableLocation", RequestEnableLocation),
96 DECLARE_NAPI_FUNCTION("enableLocation", EnableLocation),
97 DECLARE_NAPI_FUNCTION("disableLocation", DisableLocation),
98 DECLARE_NAPI_FUNCTION("getAddressesFromLocation", GetAddressesFromLocation),
99 DECLARE_NAPI_FUNCTION("getAddressesFromLocationName", GetAddressesFromLocationName),
100 DECLARE_NAPI_FUNCTION("isGeoServiceAvailable", IsGeoServiceAvailable),
101 DECLARE_NAPI_FUNCTION("getCachedGnssLocationsSize", GetCachedGnssLocationsSize),
102 DECLARE_NAPI_FUNCTION("flushCachedGnssLocations", FlushCachedGnssLocations),
103 DECLARE_NAPI_FUNCTION("sendCommand", SendCommand),
104 DECLARE_NAPI_FUNCTION("on", On),
105 DECLARE_NAPI_FUNCTION("off", Off),
106 DECLARE_NAPI_FUNCTION("getCurrentLocation", GetCurrentLocation),
107
108 DECLARE_NAPI_FUNCTION("getLocation", GetLocation),
109 DECLARE_NAPI_FUNCTION("getLocationType", GetLocationType),
110 DECLARE_NAPI_FUNCTION("subscribe", Subscribe),
111 DECLARE_NAPI_FUNCTION("unsubscribe", Unsubscribe),
112 DECLARE_NAPI_FUNCTION("getSupportedCoordTypes", GetSupportedCoordTypes),
113
114 DECLARE_NAPI_PROPERTY("LocationRequestPriority", LocationRequestPriorityTypeConstructor(env)),
115 DECLARE_NAPI_PROPERTY("LocationRequestScenario", LocationRequestScenarioTypeConstructor(env)),
116 DECLARE_NAPI_PROPERTY("LocationPrivacyType", LocationPrivacyTypeConstructor(env)),
117 DECLARE_NAPI_PROPERTY("GeoLocationErrorCode", GeoLocationErrorCodeTypeConstructor(env)),
118 };
119
120 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
121 return exports;
122 }
123
124 static napi_module g_locationModule = {
125 .nm_version = 1,
126 .nm_flags = 0,
127 .nm_filename = nullptr,
128 .nm_register_func = Init,
129 .nm_modname = "geolocation",
130 .nm_priv = ((void *)0),
131 .reserved = { 0 }
132 };
133
134 #else
135
136 /*
137 * Module initialization function
138 */
InitManager(napi_env env,napi_value exports)139 static napi_value InitManager(napi_env env, napi_value exports)
140 {
141 LBSLOGI(LOCATION_NAPI, "Init, location_napi_manager_entry");
142
143 napi_property_descriptor desc[] = {
144 DECLARE_NAPI_FUNCTION("on", On),
145 DECLARE_NAPI_FUNCTION("off", Off),
146 DECLARE_NAPI_FUNCTION("getCurrentLocation", GetCurrentLocation),
147 DECLARE_NAPI_FUNCTION("getLastLocation", GetLastLocation),
148 DECLARE_NAPI_FUNCTION("isLocationEnabled", IsLocationEnabled),
149 DECLARE_NAPI_FUNCTION("enableLocation", EnableLocation),
150 DECLARE_NAPI_FUNCTION("disableLocation", DisableLocation),
151 DECLARE_NAPI_FUNCTION("requestEnableLocation", RequestEnableLocation),
152 DECLARE_NAPI_FUNCTION("isGeocoderAvailable", IsGeoServiceAvailable),
153 DECLARE_NAPI_FUNCTION("getAddressesFromLocation", GetAddressesFromLocation),
154 DECLARE_NAPI_FUNCTION("getAddressesFromLocationName", GetAddressesFromLocationName),
155 DECLARE_NAPI_FUNCTION("isLocationPrivacyConfirmed", IsLocationPrivacyConfirmed),
156 DECLARE_NAPI_FUNCTION("setLocationPrivacyConfirmStatus", SetLocationPrivacyConfirmStatus),
157 DECLARE_NAPI_FUNCTION("getCachedGnssLocationsSize", GetCachedGnssLocationsSize),
158 DECLARE_NAPI_FUNCTION("flushCachedGnssLocations", FlushCachedGnssLocations),
159 DECLARE_NAPI_FUNCTION("sendCommand", SendCommand),
160 DECLARE_NAPI_FUNCTION("getCountryCode", GetIsoCountryCode),
161 DECLARE_NAPI_FUNCTION("enableLocationMock", EnableLocationMock),
162 DECLARE_NAPI_FUNCTION("disableLocationMock", DisableLocationMock),
163 DECLARE_NAPI_FUNCTION("setMockedLocations", SetMockedLocations),
164 DECLARE_NAPI_FUNCTION("enableReverseGeocodingMock", EnableReverseGeocodingMock),
165 DECLARE_NAPI_FUNCTION("disableReverseGeocodingMock", DisableReverseGeocodingMock),
166 DECLARE_NAPI_FUNCTION("setReverseGeocodingMockInfo", SetReverseGeocodingMockInfo),
167
168 DECLARE_NAPI_PROPERTY("LocationRequestPriority", LocationRequestPriorityTypeConstructor(env)),
169 DECLARE_NAPI_PROPERTY("LocationRequestScenario", LocationRequestScenarioTypeConstructor(env)),
170 DECLARE_NAPI_PROPERTY("LocationPrivacyType", LocationPrivacyTypeConstructor(env)),
171 DECLARE_NAPI_PROPERTY("CountryCodeType", CountryCodeTypeConstructor(env)),
172 };
173
174 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
175 return exports;
176 }
177
178 static napi_module g_locationManagerModule = {
179 .nm_version = 1,
180 .nm_flags = 0,
181 .nm_filename = nullptr,
182 .nm_register_func = InitManager,
183 .nm_modname = "geoLocationManager",
184 .nm_priv = ((void *)0),
185 .reserved = { 0 }
186 };
187 #endif
188
189 #ifndef ENABLE_NAPI_MANAGER
RegisterGeolocationModule(void)190 extern "C" __attribute__((constructor)) void RegisterGeolocationModule(void)
191 {
192 napi_module_register(&g_locationModule);
193 }
194 #else
RegisterGeolocationManagerModule(void)195 extern "C" __attribute__((constructor)) void RegisterGeolocationManagerModule(void)
196 {
197 napi_module_register(&g_locationManagerModule);
198 }
199 #endif
200 } // namespace Location
201 } // namespace OHOS
202