1 /* Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation, nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 #include "GeofenceAdapter.h"
30 #include "location_interface.h"
31 
32 static GeofenceAdapter* gGeofenceAdapter = NULL;
33 
34 static void initialize();
35 static void deinitialize();
36 
37 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks);
38 static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb);
39 static void requestCapabilities(LocationAPI* client);
40 
41 static uint32_t* addGeofences(LocationAPI* client, size_t count, GeofenceOption*, GeofenceInfo*);
42 static void removeGeofences(LocationAPI* client, size_t count, uint32_t* ids);
43 static void modifyGeofences(LocationAPI* client, size_t count, uint32_t* ids,
44                                GeofenceOption* options);
45 static void pauseGeofences(LocationAPI* client, size_t count, uint32_t* ids);
46 static void resumeGeofences(LocationAPI* client, size_t count, uint32_t* ids);
47 
48 static const GeofenceInterface gGeofenceInterface = {
49     sizeof(GeofenceInterface),
50     initialize,
51     deinitialize,
52     addClient,
53     removeClient,
54     requestCapabilities,
55     addGeofences,
56     removeGeofences,
57     modifyGeofences,
58     pauseGeofences,
59     resumeGeofences
60 };
61 
62 #ifndef DEBUG_X86
getGeofenceInterface()63 extern "C" const GeofenceInterface* getGeofenceInterface()
64 #else
65 const GeofenceInterface* getGeofenceInterface()
66 #endif // DEBUG_X86
67 {
68    return &gGeofenceInterface;
69 }
70 
initialize()71 static void initialize()
72 {
73     if (NULL == gGeofenceAdapter) {
74         gGeofenceAdapter = new GeofenceAdapter();
75     }
76 }
77 
deinitialize()78 static void deinitialize()
79 {
80     if (NULL != gGeofenceAdapter) {
81         delete gGeofenceAdapter;
82         gGeofenceAdapter = NULL;
83     }
84 }
85 
addClient(LocationAPI * client,const LocationCallbacks & callbacks)86 static void addClient(LocationAPI* client, const LocationCallbacks& callbacks)
87 {
88     if (NULL != gGeofenceAdapter) {
89         gGeofenceAdapter->addClientCommand(client, callbacks);
90     }
91 }
92 
removeClient(LocationAPI * client,removeClientCompleteCallback rmClientCb)93 static void removeClient(LocationAPI* client, removeClientCompleteCallback rmClientCb)
94 {
95     if (NULL != gGeofenceAdapter) {
96         gGeofenceAdapter->removeClientCommand(client, rmClientCb);
97     }
98 }
99 
requestCapabilities(LocationAPI * client)100 static void requestCapabilities(LocationAPI* client)
101 {
102     if (NULL != gGeofenceAdapter) {
103         gGeofenceAdapter->requestCapabilitiesCommand(client);
104     }
105 }
106 
addGeofences(LocationAPI * client,size_t count,GeofenceOption * options,GeofenceInfo * info)107 static uint32_t* addGeofences(LocationAPI* client, size_t count,
108                               GeofenceOption* options, GeofenceInfo* info)
109 {
110     if (NULL != gGeofenceAdapter) {
111         return gGeofenceAdapter->addGeofencesCommand(client, count, options, info);
112     } else {
113         return NULL;
114     }
115 }
116 
removeGeofences(LocationAPI * client,size_t count,uint32_t * ids)117 static void removeGeofences(LocationAPI* client, size_t count, uint32_t* ids)
118 {
119     if (NULL != gGeofenceAdapter) {
120         return gGeofenceAdapter->removeGeofencesCommand(client, count, ids);
121     }
122 }
123 
modifyGeofences(LocationAPI * client,size_t count,uint32_t * ids,GeofenceOption * options)124 static void modifyGeofences(LocationAPI* client, size_t count, uint32_t* ids,
125                             GeofenceOption* options)
126 {
127     if (NULL != gGeofenceAdapter) {
128         return gGeofenceAdapter->modifyGeofencesCommand(client, count, ids, options);
129     }
130 }
131 
pauseGeofences(LocationAPI * client,size_t count,uint32_t * ids)132 static void pauseGeofences(LocationAPI* client, size_t count, uint32_t* ids)
133 {
134     if (NULL != gGeofenceAdapter) {
135         return gGeofenceAdapter->pauseGeofencesCommand(client, count, ids);
136     }
137 }
138 
resumeGeofences(LocationAPI * client,size_t count,uint32_t * ids)139 static void resumeGeofences(LocationAPI* client, size_t count, uint32_t* ids)
140 {
141     if (NULL != gGeofenceAdapter) {
142         return gGeofenceAdapter->resumeGeofencesCommand(client, count, ids);
143     }
144 }
145 
146