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     uint32_t       GNSS_DEPLOYMENT;
77 } loc_gps_cfg_s_type;
78 
79 /* NOTE: the implementaiton of the parser casts number
80    fields to 32 bit. To ensure all 'n' fields working,
81    they must all be 32 bit fields. */
82 /* Meanwhile, *_valid fields are 8 bit fields, and 'f'
83    fields are double. Rigid as they are, it is the
84    the status quo, until the parsing mechanism is
85    change, that is. */
86 typedef struct
87 {
88     uint8_t        GYRO_BIAS_RANDOM_WALK_VALID;
89     double         GYRO_BIAS_RANDOM_WALK;
90     uint32_t       SENSOR_ACCEL_BATCHES_PER_SEC;
91     uint32_t       SENSOR_ACCEL_SAMPLES_PER_BATCH;
92     uint32_t       SENSOR_GYRO_BATCHES_PER_SEC;
93     uint32_t       SENSOR_GYRO_SAMPLES_PER_BATCH;
94     uint32_t       SENSOR_ACCEL_BATCHES_PER_SEC_HIGH;
95     uint32_t       SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH;
96     uint32_t       SENSOR_GYRO_BATCHES_PER_SEC_HIGH;
97     uint32_t       SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH;
98     uint32_t       SENSOR_CONTROL_MODE;
99     uint32_t       SENSOR_ALGORITHM_CONFIG_MASK;
100     uint8_t        ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
101     double         ACCEL_RANDOM_WALK_SPECTRAL_DENSITY;
102     uint8_t        ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
103     double         ANGLE_RANDOM_WALK_SPECTRAL_DENSITY;
104     uint8_t        RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
105     double         RATE_RANDOM_WALK_SPECTRAL_DENSITY;
106     uint8_t        VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
107     double         VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY;
108 } loc_sap_cfg_s_type;
109 
110 namespace loc_core {
111 
112 class LocAdapterBase;
113 
114 class ContextBase {
115     static LBSProxyBase* getLBSProxy(const char* libName);
116     LocApiBase* createLocApi(LOC_API_ADAPTER_EVENT_MASK_T excludedMask);
117     static const loc_param_s_type mGps_conf_table[];
118     static const loc_param_s_type mSap_conf_table[];
119 protected:
120     const LBSProxyBase* mLBSProxy;
121     const MsgTask* mMsgTask;
122     LocApiBase* mLocApi;
123     LocApiProxyBase *mLocApiProxy;
124 
125 public:
126     ContextBase(const MsgTask* msgTask,
127                 LOC_API_ADAPTER_EVENT_MASK_T exMask,
128                 const char* libName);
~ContextBase()129     inline virtual ~ContextBase() {
130         if (nullptr != mLocApi) {
131             mLocApi->destroy();
132             mLocApi = nullptr;
133         }
134         if (nullptr != mLBSProxy) {
135             delete mLBSProxy;
136             mLBSProxy = nullptr;
137         }
138     }
139 
getMsgTask()140     inline const MsgTask* getMsgTask() { return mMsgTask; }
getLocApi()141     inline LocApiBase* getLocApi() { return mLocApi; }
getLocApiProxy()142     inline LocApiProxyBase* getLocApiProxy() { return mLocApiProxy; }
hasAgpsExtendedCapabilities()143     inline bool hasAgpsExtendedCapabilities() { return mLBSProxy->hasAgpsExtendedCapabilities(); }
hasNativeXtraClient()144     inline bool hasNativeXtraClient() { return mLBSProxy->hasNativeXtraClient(); }
modemPowerVote(bool power)145     inline void modemPowerVote(bool power) const { return mLBSProxy->modemPowerVote(power); }
getIzatDevId()146     inline IzatDevId_t getIzatDevId() const {
147         return mLBSProxy->getIzatDevId();
148     }
sendMsg(const LocMsg * msg)149     inline void sendMsg(const LocMsg *msg) { getMsgTask()->sendMsg(msg); }
150 
151     static loc_gps_cfg_s_type mGps_conf;
152     static loc_sap_cfg_s_type mSap_conf;
153     static bool sIsEngineCapabilitiesKnown;
154     static uint64_t sSupportedMsgMask;
155     static uint8_t sFeaturesSupported[MAX_FEATURE_LENGTH];
156     static bool sGnssMeasurementSupported;
157 
158     void readConfig();
159     static uint32_t getCarrierCapabilities();
160     void setEngineCapabilities(uint64_t supportedMsgMask,
161             uint8_t *featureList, bool gnssMeasurementSupported);
162 
isEngineCapabilitiesKnown()163     static inline bool isEngineCapabilitiesKnown() {
164         return sIsEngineCapabilitiesKnown;
165     }
166 
isMessageSupported(LocCheckingMessagesID msgID)167     static inline bool isMessageSupported(LocCheckingMessagesID msgID) {
168 
169         // confirm if msgID is not larger than the number of bits in
170         // mSupportedMsg
171         if ((uint64_t)msgID > (sizeof(sSupportedMsgMask) << 3)) {
172             return false;
173         } else {
174             uint32_t messageChecker = 1 << msgID;
175             return (messageChecker & sSupportedMsgMask) == messageChecker;
176         }
177     }
178 
179     /*
180         Check if a feature is supported
181     */
182     static bool isFeatureSupported(uint8_t featureVal);
183 
184     /*
185         Check if gnss measurement is supported
186     */
187     static bool gnssConstellationConfig();
188 
189 };
190 
191 struct LocApiResponse: LocMsg {
192     private:
193         ContextBase& mContext;
194         std::function<void (LocationError err)> mProcImpl;
procLocApiResponse195         inline virtual void proc() const {
196             mProcImpl(mLocationError);
197         }
198     protected:
199         LocationError mLocationError;
200     public:
LocApiResponseLocApiResponse201         inline LocApiResponse(ContextBase& context,
202                               std::function<void (LocationError err)> procImpl ) :
203                               mContext(context), mProcImpl(procImpl) {}
204 
returnToSenderLocApiResponse205         void returnToSender(const LocationError err) {
206             mLocationError = err;
207             mContext.sendMsg(this);
208         }
209 };
210 
211 struct LocApiCollectiveResponse: LocMsg {
212     private:
213         ContextBase& mContext;
214         std::function<void (std::vector<LocationError> errs)> mProcImpl;
procLocApiCollectiveResponse215         inline virtual void proc() const {
216             mProcImpl(mLocationErrors);
217         }
218     protected:
219         std::vector<LocationError> mLocationErrors;
220     public:
LocApiCollectiveResponseLocApiCollectiveResponse221         inline LocApiCollectiveResponse(ContextBase& context,
222                               std::function<void (std::vector<LocationError> errs)> procImpl ) :
223                               mContext(context), mProcImpl(procImpl) {}
~LocApiCollectiveResponseLocApiCollectiveResponse224         inline virtual ~LocApiCollectiveResponse() {
225         }
226 
returnToSenderLocApiCollectiveResponse227         void returnToSender(std::vector<LocationError>& errs) {
228             mLocationErrors = errs;
229             mContext.sendMsg(this);
230         }
231 };
232 
233 
234 template <typename DATA>
235 struct LocApiResponseData: LocMsg {
236     private:
237         ContextBase& mContext;
238         std::function<void (LocationError err, DATA data)> mProcImpl;
procLocApiResponseData239         inline virtual void proc() const {
240             mProcImpl(mLocationError, mData);
241         }
242     protected:
243         LocationError mLocationError;
244         DATA mData;
245     public:
LocApiResponseDataLocApiResponseData246         inline LocApiResponseData(ContextBase& context,
247                               std::function<void (LocationError err, DATA data)> procImpl ) :
248                               mContext(context), mProcImpl(procImpl) {}
249 
returnToSenderLocApiResponseData250         void returnToSender(const LocationError err, const DATA data) {
251             mLocationError = err;
252             mData = data;
253             mContext.sendMsg(this);
254         }
255 };
256 
257 
258 } // namespace loc_core
259 
260 #endif //__LOC_CONTEXT_BASE__
261