ContextBase.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Copyright (c) 2011-2017, 2020-2021 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. /*
  30. Changes from Qualcomm Innovation Center are provided under the following license:
  31. Copyright (c) 2022, 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  32. Redistribution and use in source and binary forms, with or without
  33. modification, are permitted (subject to the limitations in the
  34. disclaimer below) provided that the following conditions are met:
  35. * Redistributions of source code must retain the above copyright
  36. notice, this list of conditions and the following disclaimer.
  37. * Redistributions in binary form must reproduce the above
  38. copyright notice, this list of conditions and the following
  39. disclaimer in the documentation and/or other materials provided
  40. with the distribution.
  41. * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
  42. contributors may be used to endorse or promote products derived
  43. from this software without specific prior written permission.
  44. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  45. GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
  46. HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  47. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  48. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  49. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  50. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  52. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  53. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  54. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  55. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  56. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. */
  58. #ifndef __LOC_CONTEXT_BASE__
  59. #define __LOC_CONTEXT_BASE__
  60. #include <stdbool.h>
  61. #include <ctype.h>
  62. #include <loc_pla.h>
  63. #include <MsgTask.h>
  64. #include <LocApiBase.h>
  65. #include <LBSProxyBase.h>
  66. #include <loc_cfg.h>
  67. #ifdef NO_UNORDERED_SET_OR_MAP
  68. #include <map>
  69. #define unordered_map map
  70. #else
  71. #include <unordered_map>
  72. #endif
  73. /* GPS.conf support */
  74. /* NOTE: the implementaiton of the parser casts number
  75. fields to 32 bit. To ensure all 'n' fields working,
  76. they must all be 32 bit fields. */
  77. typedef struct loc_gps_cfg_s
  78. {
  79. uint32_t INTERMEDIATE_POS;
  80. uint32_t ACCURACY_THRES;
  81. uint32_t SUPL_VER;
  82. uint32_t SUPL_MODE;
  83. uint32_t SUPL_ES;
  84. uint32_t CAPABILITIES;
  85. uint32_t LPP_PROFILE;
  86. char XTRA_SERVER_1[LOC_MAX_PARAM_STRING];
  87. char XTRA_SERVER_2[LOC_MAX_PARAM_STRING];
  88. char XTRA_SERVER_3[LOC_MAX_PARAM_STRING];
  89. uint32_t USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL;
  90. uint32_t NMEA_PROVIDER;
  91. char NMEA_REPORT_RATE[LOC_MAX_PARAM_NAME];
  92. GnssConfigGpsLock GPS_LOCK;
  93. uint32_t A_GLONASS_POS_PROTOCOL_SELECT;
  94. uint32_t AGPS_CERT_WRITABLE_MASK;
  95. uint32_t AGPS_CONFIG_INJECT;
  96. uint32_t LPPE_CP_TECHNOLOGY;
  97. uint32_t LPPE_UP_TECHNOLOGY;
  98. uint32_t EXTERNAL_DR_ENABLED;
  99. char SUPL_HOST[LOC_MAX_PARAM_STRING];
  100. uint32_t SUPL_PORT;
  101. uint32_t MODEM_TYPE;
  102. char MO_SUPL_HOST[LOC_MAX_PARAM_STRING];
  103. uint32_t MO_SUPL_PORT;
  104. uint32_t CONSTRAINED_TIME_UNCERTAINTY_ENABLED;
  105. double CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD;
  106. uint32_t CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET;
  107. uint32_t POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED;
  108. uint32_t CP_MTLR_ES;
  109. uint32_t GNSS_DEPLOYMENT;
  110. uint32_t CUSTOM_NMEA_GGA_FIX_QUALITY_ENABLED;
  111. uint32_t NI_SUPL_DENY_ON_NFW_LOCKED;
  112. uint32_t ENABLE_NMEA_PRINT;
  113. uint32_t NMEA_TAG_BLOCK_GROUPING_ENABLED;
  114. } loc_gps_cfg_s_type;
  115. /* NOTE: the implementation of the parser casts number
  116. fields to 32 bit. To ensure all 'n' fields working,
  117. they must all be 32 bit fields. */
  118. /* Meanwhile, *_valid fields are 8 bit fields, and 'f'
  119. fields are double. Rigid as they are, it is the
  120. the status quo, until the parsing mechanism is
  121. changed, that is. */
  122. typedef struct
  123. {
  124. uint8_t GYRO_BIAS_RANDOM_WALK_VALID;
  125. double GYRO_BIAS_RANDOM_WALK;
  126. uint32_t SENSOR_ACCEL_BATCHES_PER_SEC;
  127. uint32_t SENSOR_ACCEL_SAMPLES_PER_BATCH;
  128. uint32_t SENSOR_GYRO_BATCHES_PER_SEC;
  129. uint32_t SENSOR_GYRO_SAMPLES_PER_BATCH;
  130. uint32_t SENSOR_ACCEL_BATCHES_PER_SEC_HIGH;
  131. uint32_t SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH;
  132. uint32_t SENSOR_GYRO_BATCHES_PER_SEC_HIGH;
  133. uint32_t SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH;
  134. uint32_t SENSOR_CONTROL_MODE;
  135. uint32_t SENSOR_ALGORITHM_CONFIG_MASK;
  136. uint8_t ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
  137. double ACCEL_RANDOM_WALK_SPECTRAL_DENSITY;
  138. uint8_t ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
  139. double ANGLE_RANDOM_WALK_SPECTRAL_DENSITY;
  140. uint8_t RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
  141. double RATE_RANDOM_WALK_SPECTRAL_DENSITY;
  142. uint8_t VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID;
  143. double VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY;
  144. } loc_sap_cfg_s_type;
  145. using namespace loc_util;
  146. namespace loc_core {
  147. class LocAdapterBase;
  148. class ContextBase {
  149. static LBSProxyBase* getLBSProxy(const char* libName);
  150. LocApiBase* createLocApi(LOC_API_ADAPTER_EVENT_MASK_T excludedMask);
  151. static const loc_param_s_type mGps_conf_table[];
  152. static const loc_param_s_type mSap_conf_table[];
  153. protected:
  154. const LBSProxyBase* mLBSProxy;
  155. const MsgTask* mMsgTask;
  156. LocApiBase* mLocApi;
  157. LocApiProxyBase *mLocApiProxy;
  158. public:
  159. ContextBase(const MsgTask* msgTask,
  160. LOC_API_ADAPTER_EVENT_MASK_T exMask,
  161. const char* libName);
  162. inline virtual ~ContextBase() {
  163. if (nullptr != mLocApi) {
  164. mLocApi->destroy();
  165. mLocApi = nullptr;
  166. }
  167. if (nullptr != mLBSProxy) {
  168. delete mLBSProxy;
  169. mLBSProxy = nullptr;
  170. }
  171. }
  172. inline const MsgTask* getMsgTask() { return mMsgTask; }
  173. inline LocApiBase* getLocApi() { return mLocApi; }
  174. inline LocApiProxyBase* getLocApiProxy() { return mLocApiProxy; }
  175. inline bool hasAgpsExtendedCapabilities() { return mLBSProxy->hasAgpsExtendedCapabilities(); }
  176. inline bool hasNativeXtraClient() { return mLBSProxy->hasNativeXtraClient(); }
  177. inline void modemPowerVote(bool power) const { return mLBSProxy->modemPowerVote(power); }
  178. inline const LBSProxyBase* getLBSProxyBase() { return mLBSProxy; }
  179. inline IzatDevId_t getIzatDevId() const {
  180. return mLBSProxy->getIzatDevId();
  181. }
  182. inline void sendMsg(const LocMsg *msg) { getMsgTask()->sendMsg(msg); }
  183. static loc_gps_cfg_s_type mGps_conf;
  184. static loc_sap_cfg_s_type mSap_conf;
  185. static bool sIsEngineCapabilitiesKnown;
  186. static uint64_t sSupportedMsgMask;
  187. static uint8_t sFeaturesSupported[MAX_FEATURE_LENGTH];
  188. static bool sGnssMeasurementSupported;
  189. static GnssNMEARptRate sNmeaReportRate;
  190. static LocationCapabilitiesMask sQwesFeatureMask;
  191. static LocationHwCapabilitiesMask sHwCapabilitiesMask;
  192. void readConfig();
  193. static uint32_t getCarrierCapabilities();
  194. void setEngineCapabilities(uint64_t supportedMsgMask,
  195. uint8_t *featureList, bool gnssMeasurementSupported);
  196. static inline bool isEngineCapabilitiesKnown() {
  197. return sIsEngineCapabilitiesKnown;
  198. }
  199. static inline bool isMessageSupported(LocCheckingMessagesID msgID) {
  200. // confirm if msgID is not larger than the number of bits in
  201. // mSupportedMsg
  202. if ((uint64_t)msgID > (sizeof(sSupportedMsgMask) << 3)) {
  203. return false;
  204. } else {
  205. uint32_t messageChecker = 1 << msgID;
  206. return (messageChecker & sSupportedMsgMask) == messageChecker;
  207. }
  208. }
  209. /*
  210. Check if a feature is supported
  211. */
  212. static bool isFeatureSupported(uint8_t featureVal);
  213. /*
  214. Check if gnss measurement is supported
  215. */
  216. static bool gnssConstellationConfig();
  217. /*
  218. set QWES feature status info
  219. */
  220. static inline void setQwesFeatureStatus(
  221. const std::unordered_map<LocationQwesFeatureType, bool> &featureMap) {
  222. std::unordered_map<LocationQwesFeatureType, bool>::const_iterator itr;
  223. static LocationQwesFeatureType locQwesFeatType[LOCATION_QWES_FEATURE_TYPE_MAX];
  224. for (itr = featureMap.begin(); itr != featureMap.end(); ++itr) {
  225. LOC_LOGi("Feature : %d isValid: %d", itr->first, itr->second);
  226. locQwesFeatType[itr->first] = itr->second;
  227. switch (itr->first) {
  228. case LOCATION_QWES_FEATURE_TYPE_CARRIER_PHASE:
  229. if (itr->second) {
  230. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_CARRIER_PHASE_BIT;
  231. } else {
  232. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_CARRIER_PHASE_BIT;
  233. }
  234. break;
  235. case LOCATION_QWES_FEATURE_TYPE_SV_POLYNOMIAL:
  236. if (itr->second) {
  237. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_SV_POLYNOMIAL_BIT;
  238. } else {
  239. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_SV_POLYNOMIAL_BIT;
  240. }
  241. break;
  242. case LOCATION_QWES_FEATURE_TYPE_GNSS_SINGLE_FREQUENCY:
  243. if (itr->second) {
  244. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_GNSS_SINGLE_FREQUENCY;
  245. } else {
  246. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_GNSS_SINGLE_FREQUENCY;
  247. }
  248. break;
  249. case LOCATION_QWES_FEATURE_TYPE_SV_EPH:
  250. if (itr->second) {
  251. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_SV_EPHEMERIS_BIT;
  252. } else {
  253. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_SV_EPHEMERIS_BIT;
  254. }
  255. break;
  256. case LOCATION_QWES_FEATURE_TYPE_GNSS_MULTI_FREQUENCY:
  257. if (itr->second) {
  258. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_GNSS_MULTI_FREQUENCY;
  259. } else {
  260. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_GNSS_MULTI_FREQUENCY;
  261. }
  262. break;
  263. case LOCATION_QWES_FEATURE_TYPE_PPE:
  264. if (itr->second) {
  265. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_PPE;
  266. } else {
  267. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_PPE;
  268. }
  269. break;
  270. case LOCATION_QWES_FEATURE_TYPE_QDR2:
  271. if (itr->second) {
  272. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_QDR2;
  273. } else {
  274. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_QDR2;
  275. }
  276. break;
  277. case LOCATION_QWES_FEATURE_TYPE_QDR3:
  278. if (itr->second) {
  279. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_QDR3;
  280. } else {
  281. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_QDR3;
  282. }
  283. break;
  284. case LOCATION_QWES_FEATURE_TYPE_VPE:
  285. if (itr->second) {
  286. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_VPE;
  287. } else {
  288. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_VPE;
  289. }
  290. break;
  291. case LOCATION_QWES_FEATURE_TYPE_DGNSS:
  292. if (itr->second) {
  293. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_DGNSS;
  294. } else {
  295. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_DGNSS;
  296. }
  297. break;
  298. case LOCATION_QWES_FEATURE_TYPE_RSSI_POSITIONING:
  299. if (itr->second) {
  300. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_WIFI_RSSI_POSITIONING;
  301. } else {
  302. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_WIFI_RSSI_POSITIONING;
  303. }
  304. break;
  305. case LOCATION_QWES_FEATURE_TYPE_RTT_POSITIONING:
  306. if (itr->second) {
  307. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_WIFI_RTT_POSITIONING;
  308. } else {
  309. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_WIFI_RTT_POSITIONING;
  310. }
  311. break;
  312. }
  313. }
  314. // Set CV2X basic when time freq and tunc is set
  315. // CV2X_BASIC = LOCATION_QWES_FEATURE_TYPE_TIME_FREQUENCY &
  316. // LOCATION_QWES_FEATURE_TYPE_TIME_UNCERTAINTY
  317. // Set CV2X premium when time freq and tunc is set
  318. // CV2X_PREMIUM = CV2X_BASIC & LOCATION_QWES_FEATURE_TYPE_QDR3 &
  319. // LOCATION_QWES_FEATURE_TYPE_CLOCK_ESTIMATE
  320. bool cv2xBasicEnabled = (1 == locQwesFeatType[LOCATION_QWES_FEATURE_TYPE_TIME_FREQUENCY]) &&
  321. (1 == locQwesFeatType[LOCATION_QWES_FEATURE_TYPE_TIME_UNCERTAINTY]);
  322. bool cv2xPremiumEnabled = cv2xBasicEnabled &&
  323. (1 == locQwesFeatType[LOCATION_QWES_FEATURE_TYPE_QDR3]) &&
  324. (1 == locQwesFeatType[LOCATION_QWES_FEATURE_TYPE_CLOCK_ESTIMATE]);
  325. LOC_LOGd("CV2X_BASIC:%d, CV2X_PREMIUM:%d", cv2xBasicEnabled, cv2xPremiumEnabled);
  326. if (cv2xBasicEnabled) {
  327. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_CV2X_LOCATION_BASIC;
  328. } else {
  329. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_CV2X_LOCATION_BASIC;
  330. }
  331. if (cv2xPremiumEnabled) {
  332. sQwesFeatureMask |= LOCATION_CAPABILITIES_QWES_CV2X_LOCATION_PREMIUM;
  333. } else {
  334. sQwesFeatureMask &= ~LOCATION_CAPABILITIES_QWES_CV2X_LOCATION_PREMIUM;
  335. }
  336. }
  337. /*
  338. get QWES feature status info
  339. */
  340. static inline LocationCapabilitiesMask getQwesFeatureStatus() {
  341. return (ContextBase::sQwesFeatureMask);
  342. }
  343. /*
  344. set HW feature status info
  345. */
  346. static inline void setHwCapabilities(const LocationHwCapabilitiesMask& mask) {
  347. sHwCapabilitiesMask |= mask;
  348. }
  349. /*
  350. get HW feature status info
  351. */
  352. static inline LocationHwCapabilitiesMask getHwCapabilitiesMask() {
  353. return (ContextBase::sHwCapabilitiesMask);
  354. }
  355. };
  356. struct LocApiResponse: LocMsg {
  357. private:
  358. ContextBase& mContext;
  359. std::function<void (LocationError err)> mProcImpl;
  360. inline virtual void proc() const {
  361. mProcImpl(mLocationError);
  362. }
  363. protected:
  364. LocationError mLocationError;
  365. public:
  366. inline LocApiResponse(ContextBase& context,
  367. std::function<void (LocationError err)> procImpl ) :
  368. mContext(context), mProcImpl(procImpl) {}
  369. void returnToSender(const LocationError err) {
  370. mLocationError = err;
  371. mContext.sendMsg(this);
  372. }
  373. };
  374. struct LocApiCollectiveResponse: LocMsg {
  375. private:
  376. ContextBase& mContext;
  377. std::function<void (std::vector<LocationError> errs)> mProcImpl;
  378. inline virtual void proc() const {
  379. mProcImpl(mLocationErrors);
  380. }
  381. protected:
  382. std::vector<LocationError> mLocationErrors;
  383. public:
  384. inline LocApiCollectiveResponse(ContextBase& context,
  385. std::function<void (std::vector<LocationError> errs)> procImpl ) :
  386. mContext(context), mProcImpl(procImpl) {}
  387. inline virtual ~LocApiCollectiveResponse() {
  388. }
  389. void returnToSender(std::vector<LocationError>& errs) {
  390. mLocationErrors = errs;
  391. mContext.sendMsg(this);
  392. }
  393. };
  394. template <typename DATA>
  395. struct LocApiResponseData: LocMsg {
  396. private:
  397. ContextBase& mContext;
  398. std::function<void (LocationError err, DATA data)> mProcImpl;
  399. inline virtual void proc() const {
  400. mProcImpl(mLocationError, mData);
  401. }
  402. protected:
  403. LocationError mLocationError;
  404. DATA mData;
  405. public:
  406. inline LocApiResponseData(ContextBase& context,
  407. std::function<void (LocationError err, DATA data)> procImpl ) :
  408. mContext(context), mProcImpl(procImpl) {}
  409. void returnToSender(const LocationError err, const DATA data) {
  410. mLocationError = err;
  411. mData = data;
  412. mContext.sendMsg(this);
  413. }
  414. };
  415. } // namespace loc_core
  416. #endif //__LOC_CONTEXT_BASE__