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