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,
81 LOCATION_REQUEST_TIMEOUT_ERROR, "LOCATION_REQUEST_TIMEOUT_ERROR");
82 return geoLocationErrorCode;
83 }
84
LocatingRequiredDataTypeConstructor(napi_env env)85 napi_value LocatingRequiredDataTypeConstructor(napi_env env)
86 {
87 napi_value locatingPriority = nullptr;
88 NAPI_CALL(env, napi_create_object(env, &locatingPriority));
89 SetEnumPropertyByInteger(env, locatingPriority, LocatingRequiredDataType::WIFI, "WIFI");
90 SetEnumPropertyByInteger(env, locatingPriority, LocatingRequiredDataType::BLUE_TOOTH, "BLUE_TOOTH");
91 return locatingPriority;
92 }
93
94 #ifndef ENABLE_NAPI_MANAGER
95 /*
96 * Module initialization function
97 */
Init(napi_env env,napi_value exports)98 static napi_value Init(napi_env env, napi_value exports)
99 {
100 LBSLOGI(LOCATION_NAPI, "Init,location_napi_entry");
101
102 napi_property_descriptor desc[] = {
103 DECLARE_NAPI_FUNCTION("getLastLocation", GetLastLocation),
104 DECLARE_NAPI_FUNCTION("isLocationEnabled", IsLocationEnabled),
105 DECLARE_NAPI_FUNCTION("requestEnableLocation", RequestEnableLocation),
106 DECLARE_NAPI_FUNCTION("enableLocation", EnableLocation),
107 DECLARE_NAPI_FUNCTION("disableLocation", DisableLocation),
108 DECLARE_NAPI_FUNCTION("getAddressesFromLocation", GetAddressesFromLocation),
109 DECLARE_NAPI_FUNCTION("getAddressesFromLocationName", GetAddressesFromLocationName),
110 DECLARE_NAPI_FUNCTION("isGeoServiceAvailable", IsGeoServiceAvailable),
111 DECLARE_NAPI_FUNCTION("getCachedGnssLocationsSize", GetCachedGnssLocationsSize),
112 DECLARE_NAPI_FUNCTION("flushCachedGnssLocations", FlushCachedGnssLocations),
113 DECLARE_NAPI_FUNCTION("sendCommand", SendCommand),
114 DECLARE_NAPI_FUNCTION("on", On),
115 DECLARE_NAPI_FUNCTION("off", Off),
116 DECLARE_NAPI_FUNCTION("getCurrentLocation", GetCurrentLocation),
117
118 DECLARE_NAPI_FUNCTION("getLocation", GetLocation),
119 DECLARE_NAPI_FUNCTION("getLocationType", GetLocationType),
120 DECLARE_NAPI_FUNCTION("subscribe", Subscribe),
121 DECLARE_NAPI_FUNCTION("unsubscribe", Unsubscribe),
122 DECLARE_NAPI_FUNCTION("getSupportedCoordTypes", GetSupportedCoordTypes),
123
124 DECLARE_NAPI_PROPERTY("LocationRequestPriority", LocationRequestPriorityTypeConstructor(env)),
125 DECLARE_NAPI_PROPERTY("LocationRequestScenario", LocationRequestScenarioTypeConstructor(env)),
126 DECLARE_NAPI_PROPERTY("LocationPrivacyType", LocationPrivacyTypeConstructor(env)),
127 DECLARE_NAPI_PROPERTY("GeoLocationErrorCode", GeoLocationErrorCodeTypeConstructor(env)),
128 };
129
130 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
131 return exports;
132 }
133
134 static napi_module g_locationModule = {
135 .nm_version = 1,
136 .nm_flags = 0,
137 .nm_filename = nullptr,
138 .nm_register_func = Init,
139 .nm_modname = "geolocation",
140 .nm_priv = ((void *)0),
141 .reserved = { 0 }
142 };
143
144 #else
145
146 /*
147 * Module initialization function
148 */
InitManager(napi_env env,napi_value exports)149 static napi_value InitManager(napi_env env, napi_value exports)
150 {
151 LBSLOGD(LOCATION_NAPI, "Init, location_napi_manager_entry");
152
153 napi_property_descriptor desc[] = {
154 DECLARE_NAPI_FUNCTION("on", On),
155 DECLARE_NAPI_FUNCTION("off", Off),
156 DECLARE_NAPI_FUNCTION("getCurrentLocation", GetCurrentLocation),
157 DECLARE_NAPI_FUNCTION("getLastLocation", GetLastLocation),
158 DECLARE_NAPI_FUNCTION("isLocationEnabled", IsLocationEnabled),
159 DECLARE_NAPI_FUNCTION("enableLocation", EnableLocation),
160 DECLARE_NAPI_FUNCTION("disableLocation", DisableLocation),
161 DECLARE_NAPI_FUNCTION("requestEnableLocation", RequestEnableLocation),
162 DECLARE_NAPI_FUNCTION("isGeocoderAvailable", IsGeoServiceAvailable),
163 DECLARE_NAPI_FUNCTION("getAddressesFromLocation", GetAddressesFromLocation),
164 DECLARE_NAPI_FUNCTION("getAddressesFromLocationName", GetAddressesFromLocationName),
165 DECLARE_NAPI_FUNCTION("isLocationPrivacyConfirmed", IsLocationPrivacyConfirmed),
166 DECLARE_NAPI_FUNCTION("setLocationPrivacyConfirmStatus", SetLocationPrivacyConfirmStatus),
167 DECLARE_NAPI_FUNCTION("getCachedGnssLocationsSize", GetCachedGnssLocationsSize),
168 DECLARE_NAPI_FUNCTION("flushCachedGnssLocations", FlushCachedGnssLocations),
169 DECLARE_NAPI_FUNCTION("sendCommand", SendCommand),
170 DECLARE_NAPI_FUNCTION("getCountryCode", GetIsoCountryCode),
171 DECLARE_NAPI_FUNCTION("enableLocationMock", EnableLocationMock),
172 DECLARE_NAPI_FUNCTION("disableLocationMock", DisableLocationMock),
173 DECLARE_NAPI_FUNCTION("setMockedLocations", SetMockedLocations),
174 DECLARE_NAPI_FUNCTION("enableReverseGeocodingMock", EnableReverseGeocodingMock),
175 DECLARE_NAPI_FUNCTION("disableReverseGeocodingMock", DisableReverseGeocodingMock),
176 DECLARE_NAPI_FUNCTION("setReverseGeocodingMockInfo", SetReverseGeocodingMockInfo),
177 DECLARE_NAPI_FUNCTION("getLocatingRequiredData", GetLocatingRequiredData),
178
179 DECLARE_NAPI_PROPERTY("LocationRequestPriority", LocationRequestPriorityTypeConstructor(env)),
180 DECLARE_NAPI_PROPERTY("LocationRequestScenario", LocationRequestScenarioTypeConstructor(env)),
181 DECLARE_NAPI_PROPERTY("LocationPrivacyType", LocationPrivacyTypeConstructor(env)),
182 DECLARE_NAPI_PROPERTY("CountryCodeType", CountryCodeTypeConstructor(env)),
183 DECLARE_NAPI_PROPERTY("LocatingRequiredDataType ", LocatingRequiredDataTypeConstructor(env)),
184 };
185
186 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
187 return exports;
188 }
189
190 static napi_module g_locationManagerModule = {
191 .nm_version = 1,
192 .nm_flags = 0,
193 .nm_filename = nullptr,
194 .nm_register_func = InitManager,
195 .nm_modname = "geoLocationManager",
196 .nm_priv = ((void *)0),
197 .reserved = { 0 }
198 };
199 #endif
200
201 #ifndef ENABLE_NAPI_MANAGER
RegisterGeolocationModule(void)202 extern "C" __attribute__((constructor)) void RegisterGeolocationModule(void)
203 {
204 napi_module_register(&g_locationModule);
205 }
206 #else
RegisterGeolocationManagerModule(void)207 extern "C" __attribute__((constructor)) void RegisterGeolocationManagerModule(void)
208 {
209 napi_module_register(&g_locationManagerModule);
210 }
211 #endif
212 } // namespace Location
213 } // namespace OHOS
214