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