1 /* Copyright (c) 2013-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 #ifndef GEOFENCE_ADAPTER_H
30 #define GEOFENCE_ADAPTER_H
31 
32 #include <LocAdapterBase.h>
33 #include <LocContext.h>
34 #include <LocationAPI.h>
35 #include <map>
36 
37 using namespace loc_core;
38 
39 #define COPY_IF_NOT_NULL(dest, src, len) do { \
40     if (NULL!=dest && NULL!=src) { \
41         for (size_t i=0; i<len; ++i) { \
42             dest[i] = src[i]; \
43         } \
44     } \
45 } while (0)
46 
47 typedef struct GeofenceKey {
48     LocationAPI* client;
49     uint32_t id;
GeofenceKeyGeofenceKey50     inline GeofenceKey() :
51         client(NULL), id(0) {}
GeofenceKeyGeofenceKey52     inline GeofenceKey(LocationAPI* _client, uint32_t _id) :
53         client(_client), id(_id) {}
54 } GeofenceKey;
55 inline bool operator <(GeofenceKey const& left, GeofenceKey const& right) {
56     return left.id < right.id || (left.id == right.id && left.client < right.client);
57 }
58 inline bool operator ==(GeofenceKey const& left, GeofenceKey const& right) {
59     return left.id == right.id && left.client == right.client;
60 }
61 inline bool operator !=(GeofenceKey const& left, GeofenceKey const& right) {
62     return left.id != right.id || left.client != right.client;
63 }
64 typedef struct {
65     GeofenceKey key;
66     GeofenceBreachTypeMask breachMask;
67     uint32_t responsiveness;
68     uint32_t dwellTime;
69     double latitude;
70     double longitude;
71     double radius;
72     bool paused;
73 } GeofenceObject;
74 typedef std::map<uint32_t, GeofenceObject> GeofencesMap; //map of hwId to GeofenceObject
75 typedef std::map<GeofenceKey, uint32_t> GeofenceIdMap; //map of GeofenceKey to hwId
76 
77 class GeofenceAdapter : public LocAdapterBase {
78 
79     /* ==== GEOFENCES ====================================================================== */
80     GeofencesMap mGeofences; //map hwId to GeofenceObject
81     GeofenceIdMap mGeofenceIds; //map of GeofenceKey to hwId
82 
83 protected:
84 
85     /* ==== CLIENT ========================================================================= */
86     virtual void updateClientsEventMask();
87     virtual void stopClientSessions(LocationAPI* client);
88 
89 public:
90 
91     GeofenceAdapter();
~GeofenceAdapter()92     virtual ~GeofenceAdapter() {}
93 
94     /* ==== SSR ============================================================================ */
95     /* ======== EVENTS ====(Called from QMI Thread)========================================= */
96     virtual void handleEngineUpEvent();
97     /* ======== UTILITIES ================================================================== */
98     void restartGeofences();
99 
100     /* ==== GEOFENCES ====================================================================== */
101     /* ======== COMMANDS ====(Called from Client Thread)==================================== */
102     uint32_t* addGeofencesCommand(LocationAPI* client, size_t count,
103                                   GeofenceOption* options, GeofenceInfo* info);
104     void removeGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids);
105     void pauseGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids);
106     void resumeGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids);
107     void modifyGeofencesCommand(LocationAPI* client, size_t count, uint32_t* ids,
108                                 GeofenceOption* options);
109     /* ======== RESPONSES ================================================================== */
110     void reportResponse(LocationAPI* client, size_t count, LocationError* errs, uint32_t* ids);
111     /* ======== UTILITIES ================================================================== */
112     void saveGeofenceItem(LocationAPI* client,
113                           uint32_t clientId,
114                           uint32_t hwId,
115                           const GeofenceOption& options,
116                           const GeofenceInfo& info);
117     void removeGeofenceItem(uint32_t hwId);
118     void pauseGeofenceItem(uint32_t hwId);
119     void resumeGeofenceItem(uint32_t hwId);
120     void modifyGeofenceItem(uint32_t hwId, const GeofenceOption& options);
121     LocationError getHwIdFromClient(LocationAPI* client, uint32_t clientId, uint32_t& hwId);
122     LocationError getGeofenceKeyFromHwId(uint32_t hwId, GeofenceKey& key);
123     void dump();
124 
125     /* ==== REPORTS ======================================================================== */
126     /* ======== EVENTS ====(Called from QMI Thread)========================================= */
127     void geofenceBreachEvent(size_t count, uint32_t* hwIds, Location& location,
128                              GeofenceBreachType breachType, uint64_t timestamp);
129     void geofenceStatusEvent(GeofenceStatusAvailable available);
130     /* ======== UTILITIES ================================================================== */
131     void geofenceBreach(size_t count, uint32_t* hwIds, const Location& location,
132                         GeofenceBreachType breachType, uint64_t timestamp);
133     void geofenceStatus(GeofenceStatusAvailable available);
134 };
135 
136 #endif /* GEOFENCE_ADAPTER_H */
137