1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "chre_api/chre/gnss.h"
18 
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/util/macros.h"
21 #include "chre/util/system/napp_permissions.h"
22 #include "chre/util/time.h"
23 
24 using chre::EventLoopManager;
25 using chre::EventLoopManagerSingleton;
26 using chre::Milliseconds;
27 using chre::NanoappPermissions;
28 
chreGnssGetCapabilities()29 DLL_EXPORT uint32_t chreGnssGetCapabilities() {
30 #ifdef CHRE_GNSS_SUPPORT_ENABLED
31   return chre::EventLoopManagerSingleton::get()
32       ->getGnssManager()
33       .getCapabilities();
34 #else
35   return CHRE_GNSS_CAPABILITIES_NONE;
36 #endif  // CHRE_GNSS_SUPPORT_ENABLED
37 }
38 
chreGnssLocationSessionStartAsync(uint32_t minIntervalMs,uint32_t minTimeToNextFixMs,const void * cookie)39 DLL_EXPORT bool chreGnssLocationSessionStartAsync(uint32_t minIntervalMs,
40                                                   uint32_t minTimeToNextFixMs,
41                                                   const void *cookie) {
42 #ifdef CHRE_GNSS_SUPPORT_ENABLED
43   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
44   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_GNSS) &&
45          chre::EventLoopManagerSingleton::get()
46              ->getGnssManager()
47              .getLocationSession()
48              .addRequest(nanoapp, Milliseconds(minIntervalMs),
49                          Milliseconds(minTimeToNextFixMs), cookie);
50 #else
51   return false;
52 #endif  // CHRE_GNSS_SUPPORT_ENABLED
53 }
54 
chreGnssLocationSessionStopAsync(const void * cookie)55 DLL_EXPORT bool chreGnssLocationSessionStopAsync(const void *cookie) {
56 #ifdef CHRE_GNSS_SUPPORT_ENABLED
57   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
58   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_GNSS) &&
59          chre::EventLoopManagerSingleton::get()
60              ->getGnssManager()
61              .getLocationSession()
62              .removeRequest(nanoapp, cookie);
63 #else
64   return false;
65 #endif  // CHRE_GNSS_SUPPORT_ENABLED
66 }
67 
chreGnssMeasurementSessionStartAsync(uint32_t minIntervalMs,const void * cookie)68 DLL_EXPORT bool chreGnssMeasurementSessionStartAsync(uint32_t minIntervalMs,
69                                                      const void *cookie) {
70 #ifdef CHRE_GNSS_SUPPORT_ENABLED
71   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
72   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_GNSS) &&
73          chre::EventLoopManagerSingleton::get()
74              ->getGnssManager()
75              .getMeasurementSession()
76              .addRequest(nanoapp, Milliseconds(minIntervalMs),
77                          Milliseconds(0) /* minTimeToNext */, cookie);
78 #else
79   return false;
80 #endif  // CHRE_GNSS_SUPPORT_ENABLED
81 }
82 
chreGnssMeasurementSessionStopAsync(const void * cookie)83 DLL_EXPORT bool chreGnssMeasurementSessionStopAsync(const void *cookie) {
84 #ifdef CHRE_GNSS_SUPPORT_ENABLED
85   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
86   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_GNSS) &&
87          chre::EventLoopManagerSingleton::get()
88              ->getGnssManager()
89              .getMeasurementSession()
90              .removeRequest(nanoapp, cookie);
91 #else
92   return false;
93 #endif  // CHRE_GNSS_SUPPORT_ENABLED
94 }
95 
chreGnssConfigurePassiveLocationListener(bool enable)96 DLL_EXPORT bool chreGnssConfigurePassiveLocationListener(bool enable) {
97 #ifdef CHRE_GNSS_SUPPORT_ENABLED
98   chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
99   return nanoapp->permitPermissionUse(NanoappPermissions::CHRE_PERMS_GNSS) &&
100          chre::EventLoopManagerSingleton::get()
101              ->getGnssManager()
102              .configurePassiveLocationListener(nanoapp, enable);
103 #else
104   return false;
105 #endif  // CHRE_GNSS_SUPPORT_ENABLED
106 }
107