1 /* Copyright (c) 2011-2017, 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 __LOC_CONTEXT_BASE__
30 #define __LOC_CONTEXT_BASE__
31 
32 #include <stdbool.h>
33 #include <ctype.h>
34 #include <MsgTask.h>
35 #include <LocApiBase.h>
36 #include <LBSProxyBase.h>
37 #include <loc_cfg.h>
38 
39 /* GPS.conf support */
40 /* NOTE: the implementaiton of the parser casts number
41    fields to 32 bit. To ensure all 'n' fields working,
42    they must all be 32 bit fields. */
43 typedef struct loc_gps_cfg_s
44 {
45     uint32_t       INTERMEDIATE_POS;
46     uint32_t       ACCURACY_THRES;
47     uint32_t       SUPL_VER;
48     uint32_t       SUPL_MODE;
49     uint32_t       SUPL_ES;
50     uint32_t       CAPABILITIES;
51     uint32_t       LPP_PROFILE;
52     uint32_t       XTRA_VERSION_CHECK;
53     char           XTRA_SERVER_1[LOC_MAX_PARAM_STRING];
54     char           XTRA_SERVER_2[LOC_MAX_PARAM_STRING];
55     char           XTRA_SERVER_3[LOC_MAX_PARAM_STRING];
56     uint32_t       USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL;
57     uint32_t       NMEA_PROVIDER;
58     GnssConfigGpsLock   GPS_LOCK;
59     uint32_t       A_GLONASS_POS_PROTOCOL_SELECT;
60     uint32_t       AGPS_CERT_WRITABLE_MASK;
61     uint32_t       AGPS_CONFIG_INJECT;
62     uint32_t       LPPE_CP_TECHNOLOGY;
63     uint32_t       LPPE_UP_TECHNOLOGY;
64     uint32_t       EXTERNAL_DR_ENABLED;
65     char           SUPL_HOST[LOC_MAX_PARAM_STRING];
66     uint32_t       SUPL_PORT;
67     uint32_t       MODEM_TYPE;
68     char           MO_SUPL_HOST[LOC_MAX_PARAM_STRING];
69     uint32_t       MO_SUPL_PORT;
70     uint32_t       CONSTRAINED_TIME_UNCERTAINTY_ENABLED;
71     double         CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD;
72     uint32_t       CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET;
73     uint32_t       POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED;
74     char           PROXY_APP_PACKAGE_NAME[LOC_MAX_PARAM_STRING];
75     uint32_t       CP_MTLR_ES;
76 } loc_gps_cfg_s_type;
77 
78 /* NOTE: the implementaiton of the parser casts number
79    fields to 32 bit. To ensure all 'n' fields working,
80    they must all be 32 bit fields. */
81 /* Meanwhile, *_valid fields are 8 bit fields, and 'f'
82    fields are double. Rigid as they are, it is the
83    the status quo, until the parsing mechanism is
84    change, that is. */
85 typedef struct
86 {
87     uint8_t        GYRO_BIAS_RANDOM_WALK_VALID;
88     double         GYRO_BIAS_RANDOM_WALK;
89     uint32_t       SENSOR_ACCEL_BATCHES_PER_SEC;
90     uint32_t       SENSOR_ACCEL_SAMPLES_PER_BATCH;
91     uint32_t       SENSOR_GYRO_BATCHES_PER_SEC;
92     uint32_t       SENSOR_GYRO_SAMPLES_PER_BATCH;
93     uint32_t       SENSOR_ACCEL_BATCHES_PER_SEC_HIGH;
94     uint32_t       SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH;
95     uint32_t       SENSOR_GYRO_BATCHES_PER_SEC_HIGH;
96     uint32_t       SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH;
97     uint32_t       SENSOR_CONTROL_MODE;
98     uint32_t       SENSOR_ALGORITHM_CONFIG_MASK;
99     uint8_t        ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
100     double         ACCEL_RANDOM_WALK_SPECTRAL_DENSITY;
101     uint8_t        ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
102     double         ANGLE_RANDOM_WALK_SPECTRAL_DENSITY;
103     uint8_t        RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
104     double         RATE_RANDOM_WALK_SPECTRAL_DENSITY;
105     uint8_t        VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
106     double         VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY;
107 } loc_sap_cfg_s_type;
108 
109 namespace loc_core {
110 
111 class LocAdapterBase;
112 
113 class ContextBase {
114     static LBSProxyBase* getLBSProxy(const char* libName);
115     LocApiBase* createLocApi(LOC_API_ADAPTER_EVENT_MASK_T excludedMask);
116     static const loc_param_s_type mGps_conf_table[];
117     static const loc_param_s_type mSap_conf_table[];
118 protected:
119     const LBSProxyBase* mLBSProxy;
120     const MsgTask* mMsgTask;
121     LocApiBase* mLocApi;
122     LocApiProxyBase *mLocApiProxy;
123 
124 public:
125     ContextBase(const MsgTask* msgTask,
126                 LOC_API_ADAPTER_EVENT_MASK_T exMask,
127                 const char* libName);
~ContextBase()128     inline virtual ~ContextBase() {
129         if (nullptr != mLocApi) {
130             mLocApi->destroy();
131             mLocApi = nullptr;
132         }
133         if (nullptr != mLBSProxy) {
134             delete mLBSProxy;
135             mLBSProxy = nullptr;
136         }
137     }
138 
getMsgTask()139     inline const MsgTask* getMsgTask() { return mMsgTask; }
getLocApi()140     inline LocApiBase* getLocApi() { return mLocApi; }
getLocApiProxy()141     inline LocApiProxyBase* getLocApiProxy() { return mLocApiProxy; }
hasAgpsExtendedCapabilities()142     inline bool hasAgpsExtendedCapabilities() { return mLBSProxy->hasAgpsExtendedCapabilities(); }
hasNativeXtraClient()143     inline bool hasNativeXtraClient() { return mLBSProxy->hasNativeXtraClient(); }
modemPowerVote(bool power)144     inline void modemPowerVote(bool power) const { return mLBSProxy->modemPowerVote(power); }
getIzatDevId()145     inline IzatDevId_t getIzatDevId() const {
146         return mLBSProxy->getIzatDevId();
147     }
sendMsg(const LocMsg * msg)148     inline void sendMsg(const LocMsg *msg) { getMsgTask()->sendMsg(msg); }
149 
150     static loc_gps_cfg_s_type mGps_conf;
151     static loc_sap_cfg_s_type mSap_conf;
152     static bool sIsEngineCapabilitiesKnown;
153     static uint64_t sSupportedMsgMask;
154     static uint8_t sFeaturesSupported[MAX_FEATURE_LENGTH];
155     static bool sGnssMeasurementSupported;
156 
157     void readConfig();
158     static uint32_t getCarrierCapabilities();
159     void setEngineCapabilities(uint64_t supportedMsgMask,
160             uint8_t *featureList, bool gnssMeasurementSupported);
161 
isEngineCapabilitiesKnown()162     static inline bool isEngineCapabilitiesKnown() {
163         return sIsEngineCapabilitiesKnown;
164     }
165 
isMessageSupported(LocCheckingMessagesID msgID)166     static inline bool isMessageSupported(LocCheckingMessagesID msgID) {
167 
168         // confirm if msgID is not larger than the number of bits in
169         // mSupportedMsg
170         if ((uint64_t)msgID > (sizeof(sSupportedMsgMask) << 3)) {
171             return false;
172         } else {
173             uint32_t messageChecker = 1 << msgID;
174             return (messageChecker & sSupportedMsgMask) == messageChecker;
175         }
176     }
177 
178     /*
179         Check if a feature is supported
180     */
181     static bool isFeatureSupported(uint8_t featureVal);
182 
183     /*
184         Check if gnss measurement is supported
185     */
186     static bool gnssConstellationConfig();
187 
188 };
189 
190 struct LocApiResponse: LocMsg {
191     private:
192         ContextBase& mContext;
193         std::function<void (LocationError err)> mProcImpl;
procLocApiResponse194         inline virtual void proc() const {
195             mProcImpl(mLocationError);
196         }
197     protected:
198         LocationError mLocationError;
199     public:
LocApiResponseLocApiResponse200         inline LocApiResponse(ContextBase& context,
201                               std::function<void (LocationError err)> procImpl ) :
202                               mContext(context), mProcImpl(procImpl) {}
203 
returnToSenderLocApiResponse204         void returnToSender(const LocationError err) {
205             mLocationError = err;
206             mContext.sendMsg(this);
207         }
208 };
209 
210 struct LocApiCollectiveResponse: LocMsg {
211     private:
212         ContextBase& mContext;
213         std::function<void (std::vector<LocationError> errs)> mProcImpl;
procLocApiCollectiveResponse214         inline virtual void proc() const {
215             mProcImpl(mLocationErrors);
216         }
217     protected:
218         std::vector<LocationError> mLocationErrors;
219     public:
LocApiCollectiveResponseLocApiCollectiveResponse220         inline LocApiCollectiveResponse(ContextBase& context,
221                               std::function<void (std::vector<LocationError> errs)> procImpl ) :
222                               mContext(context), mProcImpl(procImpl) {}
~LocApiCollectiveResponseLocApiCollectiveResponse223         inline virtual ~LocApiCollectiveResponse() {
224         }
225 
returnToSenderLocApiCollectiveResponse226         void returnToSender(std::vector<LocationError>& errs) {
227             mLocationErrors = errs;
228             mContext.sendMsg(this);
229         }
230 };
231 
232 
233 template <typename DATA>
234 struct LocApiResponseData: LocMsg {
235     private:
236         ContextBase& mContext;
237         std::function<void (LocationError err, DATA data)> mProcImpl;
procLocApiResponseData238         inline virtual void proc() const {
239             mProcImpl(mLocationError, mData);
240         }
241     protected:
242         LocationError mLocationError;
243         DATA mData;
244     public:
LocApiResponseDataLocApiResponseData245         inline LocApiResponseData(ContextBase& context,
246                               std::function<void (LocationError err, DATA data)> procImpl ) :
247                               mContext(context), mProcImpl(procImpl) {}
248 
returnToSenderLocApiResponseData249         void returnToSender(const LocationError err, const DATA data) {
250             mLocationError = err;
251             mData = data;
252             mContext.sendMsg(this);
253         }
254 };
255 
256 
257 } // namespace loc_core
258 
259 #endif //__LOC_CONTEXT_BASE__
260