1/* Copyright (c) 2011-2014, 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#define LOG_NDDEBUG 0 30#define LOG_TAG "LocSvc_LocApiBase" 31 32#include <dlfcn.h> 33#include <LocApiBase.h> 34#include <LocAdapterBase.h> 35#include <log_util.h> 36#include <LocDualContext.h> 37 38namespace loc_core { 39 40#define TO_ALL_LOCADAPTERS(call) TO_ALL_ADAPTERS(mLocAdapters, (call)) 41#define TO_1ST_HANDLING_LOCADAPTERS(call) TO_1ST_HANDLING_ADAPTER(mLocAdapters, (call)) 42 43int hexcode(char *hexstring, int string_size, 44 const char *data, int data_size) 45{ 46 int i; 47 for (i = 0; i < data_size; i++) 48 { 49 char ch = data[i]; 50 if (i*2 + 3 <= string_size) 51 { 52 snprintf(&hexstring[i*2], 3, "%02X", ch); 53 } 54 else { 55 break; 56 } 57 } 58 return i; 59} 60 61int decodeAddress(char *addr_string, int string_size, 62 const char *data, int data_size) 63{ 64 const char addr_prefix = 0x91; 65 int i, idxOutput = 0; 66 67 if (!data || !addr_string) { return 0; } 68 69 if (data[0] != addr_prefix) 70 { 71 LOC_LOGW("decodeAddress: address prefix is not 0x%x but 0x%x", addr_prefix, data[0]); 72 addr_string[0] = '\0'; 73 return 0; // prefix not correct 74 } 75 76 for (i = 1; i < data_size; i++) 77 { 78 unsigned char ch = data[i], low = ch & 0x0F, hi = ch >> 4; 79 if (low <= 9 && idxOutput < string_size - 1) { addr_string[idxOutput++] = low + '0'; } 80 if (hi <= 9 && idxOutput < string_size - 1) { addr_string[idxOutput++] = hi + '0'; } 81 } 82 83 addr_string[idxOutput] = '\0'; // Terminates the string 84 85 return idxOutput; 86} 87 88struct LocSsrMsg : public LocMsg { 89 LocApiBase* mLocApi; 90 inline LocSsrMsg(LocApiBase* locApi) : 91 LocMsg(), mLocApi(locApi) 92 { 93 locallog(); 94 } 95 inline virtual void proc() const { 96 mLocApi->close(); 97 mLocApi->open(mLocApi->getEvtMask()); 98 } 99 inline void locallog() { 100 LOC_LOGV("LocSsrMsg"); 101 } 102 inline virtual void log() { 103 locallog(); 104 } 105}; 106 107struct LocOpenMsg : public LocMsg { 108 LocApiBase* mLocApi; 109 LOC_API_ADAPTER_EVENT_MASK_T mMask; 110 inline LocOpenMsg(LocApiBase* locApi, 111 LOC_API_ADAPTER_EVENT_MASK_T mask) : 112 LocMsg(), mLocApi(locApi), mMask(mask) 113 { 114 locallog(); 115 } 116 inline virtual void proc() const { 117 mLocApi->open(mMask); 118 } 119 inline void locallog() { 120 LOC_LOGV("%s:%d]: LocOpen Mask: %x\n", 121 __func__, __LINE__, mMask); 122 } 123 inline virtual void log() { 124 locallog(); 125 } 126}; 127 128LocApiBase::LocApiBase(const MsgTask* msgTask, 129 LOC_API_ADAPTER_EVENT_MASK_T excludedMask, 130 ContextBase* context) : 131 mExcludedMask(excludedMask), mMsgTask(msgTask), 132 mMask(0), mSupportedMsg(0), mContext(context) 133{ 134 memset(mLocAdapters, 0, sizeof(mLocAdapters)); 135} 136 137LOC_API_ADAPTER_EVENT_MASK_T LocApiBase::getEvtMask() 138{ 139 LOC_API_ADAPTER_EVENT_MASK_T mask = 0; 140 141 TO_ALL_LOCADAPTERS(mask |= mLocAdapters[i]->getEvtMask()); 142 143 return mask & ~mExcludedMask; 144} 145 146bool LocApiBase::isInSession() 147{ 148 bool inSession = false; 149 150 for (int i = 0; 151 !inSession && i < MAX_ADAPTERS && NULL != mLocAdapters[i]; 152 i++) { 153 inSession = mLocAdapters[i]->isInSession(); 154 } 155 156 return inSession; 157} 158 159void LocApiBase::addAdapter(LocAdapterBase* adapter) 160{ 161 for (int i = 0; i < MAX_ADAPTERS && mLocAdapters[i] != adapter; i++) { 162 if (mLocAdapters[i] == NULL) { 163 mLocAdapters[i] = adapter; 164 mMsgTask->sendMsg(new LocOpenMsg(this, 165 (adapter->getEvtMask()))); 166 break; 167 } 168 } 169} 170 171void LocApiBase::removeAdapter(LocAdapterBase* adapter) 172{ 173 for (int i = 0; 174 i < MAX_ADAPTERS && NULL != mLocAdapters[i]; 175 i++) { 176 if (mLocAdapters[i] == adapter) { 177 mLocAdapters[i] = NULL; 178 179 // shift the rest of the adapters up so that the pointers 180 // in the array do not have holes. This should be more 181 // performant, because the array maintenance is much much 182 // less frequent than event handlings, which need to linear 183 // search all the adapters 184 int j = i; 185 while (++i < MAX_ADAPTERS && mLocAdapters[i] != NULL); 186 187 // i would be MAX_ADAPTERS or point to a NULL 188 i--; 189 // i now should point to a none NULL adapter within valid 190 // range although i could be equal to j, but it won't hurt. 191 // No need to check it, as it gains nothing. 192 mLocAdapters[j] = mLocAdapters[i]; 193 // this makes sure that we exit the for loop 194 mLocAdapters[i] = NULL; 195 196 // if we have an empty list of adapters 197 if (0 == i) { 198 close(); 199 } else { 200 // else we need to remove the bit 201 mMsgTask->sendMsg(new LocOpenMsg(this, getEvtMask())); 202 } 203 } 204 } 205} 206 207void LocApiBase::updateEvtMask() 208{ 209 mMsgTask->sendMsg(new LocOpenMsg(this, getEvtMask())); 210} 211 212void LocApiBase::handleEngineUpEvent() 213{ 214 // This will take care of renegotiating the loc handle 215 mMsgTask->sendMsg(new LocSsrMsg(this)); 216 217 LocDualContext::injectFeatureConfig(mContext); 218 219 // loop through adapters, and deliver to all adapters. 220 TO_ALL_LOCADAPTERS(mLocAdapters[i]->handleEngineUpEvent()); 221} 222 223void LocApiBase::handleEngineDownEvent() 224{ 225 // loop through adapters, and deliver to all adapters. 226 TO_ALL_LOCADAPTERS(mLocAdapters[i]->handleEngineDownEvent()); 227} 228 229void LocApiBase::reportPosition(UlpLocation &location, 230 GpsLocationExtended &locationExtended, 231 void* locationExt, 232 enum loc_sess_status status, 233 LocPosTechMask loc_technology_mask) 234{ 235 // print the location info before delivering 236 LOC_LOGV("flags: %d\n source: %d\n latitude: %f\n longitude: %f\n " 237 "altitude: %f\n speed: %f\n bearing: %f\n accuracy: %f\n " 238 "timestamp: %lld\n rawDataSize: %d\n rawData: %p\n " 239 "Session status: %d\n Technology mask: %u", 240 location.gpsLocation.flags, location.position_source, 241 location.gpsLocation.latitude, location.gpsLocation.longitude, 242 location.gpsLocation.altitude, location.gpsLocation.speed, 243 location.gpsLocation.bearing, location.gpsLocation.accuracy, 244 location.gpsLocation.timestamp, location.rawDataSize, 245 location.rawData, status, loc_technology_mask); 246 // loop through adapters, and deliver to all adapters. 247 TO_ALL_LOCADAPTERS( 248 mLocAdapters[i]->reportPosition(location, 249 locationExtended, 250 locationExt, 251 status, 252 loc_technology_mask) 253 ); 254} 255 256void LocApiBase::reportSv(QtiGnssSvStatus &svStatus, 257 GpsLocationExtended &locationExtended, 258 void* svExt) 259{ 260 // print the SV info before delivering 261 LOC_LOGV("num sv: %d\n ephemeris mask: %dxn almanac mask: %x\n gps/glo/bds in use" 262 " mask: %x/%x/%x\n sv: prn snr elevation azimuth", 263 svStatus.num_svs, svStatus.ephemeris_mask, 264 svStatus.almanac_mask, svStatus.gps_used_in_fix_mask, 265 svStatus.glo_used_in_fix_mask, svStatus.bds_used_in_fix_mask); 266 for (int i = 0; i < svStatus.num_svs && i < GPS_MAX_SVS; i++) { 267 LOC_LOGV(" %d: %d %f %f %f", 268 i, 269 svStatus.sv_list[i].prn, 270 svStatus.sv_list[i].snr, 271 svStatus.sv_list[i].elevation, 272 svStatus.sv_list[i].azimuth); 273 } 274 // loop through adapters, and deliver to all adapters. 275 TO_ALL_LOCADAPTERS( 276 mLocAdapters[i]->reportSv(svStatus, 277 locationExtended, 278 svExt) 279 ); 280} 281 282void LocApiBase::reportStatus(GpsStatusValue status) 283{ 284 // loop through adapters, and deliver to all adapters. 285 TO_ALL_LOCADAPTERS(mLocAdapters[i]->reportStatus(status)); 286} 287 288void LocApiBase::reportNmea(const char* nmea, int length) 289{ 290 // loop through adapters, and deliver to all adapters. 291 TO_ALL_LOCADAPTERS(mLocAdapters[i]->reportNmea(nmea, length)); 292} 293 294void LocApiBase::reportXtraServer(const char* url1, const char* url2, 295 const char* url3, const int maxlength) 296{ 297 // loop through adapters, and deliver to the first handling adapter. 298 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportXtraServer(url1, url2, url3, maxlength)); 299 300} 301 302void LocApiBase::requestXtraData() 303{ 304 // loop through adapters, and deliver to the first handling adapter. 305 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestXtraData()); 306} 307 308void LocApiBase::requestTime() 309{ 310 // loop through adapters, and deliver to the first handling adapter. 311 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestTime()); 312} 313 314void LocApiBase::requestLocation() 315{ 316 // loop through adapters, and deliver to the first handling adapter. 317 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestLocation()); 318} 319 320void LocApiBase::requestATL(int connHandle, AGpsType agps_type) 321{ 322 // loop through adapters, and deliver to the first handling adapter. 323 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestATL(connHandle, agps_type)); 324} 325 326void LocApiBase::releaseATL(int connHandle) 327{ 328 // loop through adapters, and deliver to the first handling adapter. 329 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->releaseATL(connHandle)); 330} 331 332void LocApiBase::requestSuplES(int connHandle) 333{ 334 // loop through adapters, and deliver to the first handling adapter. 335 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestSuplES(connHandle)); 336} 337 338void LocApiBase::reportDataCallOpened() 339{ 340 // loop through adapters, and deliver to the first handling adapter. 341 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportDataCallOpened()); 342} 343 344void LocApiBase::reportDataCallClosed() 345{ 346 // loop through adapters, and deliver to the first handling adapter. 347 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->reportDataCallClosed()); 348} 349 350void LocApiBase::requestNiNotify(GpsNiNotification ¬ify, const void* data) 351{ 352 // loop through adapters, and deliver to the first handling adapter. 353 TO_1ST_HANDLING_LOCADAPTERS(mLocAdapters[i]->requestNiNotify(notify, data)); 354} 355 356void LocApiBase::saveSupportedMsgList(uint64_t supportedMsgList) 357{ 358 mSupportedMsg = supportedMsgList; 359} 360 361void* LocApiBase :: getSibling() 362 DEFAULT_IMPL(NULL) 363 364LocApiProxyBase* LocApiBase :: getLocApiProxy() 365 DEFAULT_IMPL(NULL) 366 367void LocApiBase::reportGpsMeasurementData(GpsData &gpsMeasurementData) 368{ 369 // loop through adapters, and deliver to all adapters. 370 TO_ALL_LOCADAPTERS(mLocAdapters[i]->reportGpsMeasurementData(gpsMeasurementData)); 371} 372 373enum loc_api_adapter_err LocApiBase:: 374 open(LOC_API_ADAPTER_EVENT_MASK_T mask) 375DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 376 377enum loc_api_adapter_err LocApiBase:: 378 close() 379DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 380 381enum loc_api_adapter_err LocApiBase:: 382 startFix(const LocPosMode& posMode) 383DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 384 385enum loc_api_adapter_err LocApiBase:: 386 stopFix() 387DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 388 389enum loc_api_adapter_err LocApiBase:: 390 deleteAidingData(GpsAidingData f) 391DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 392 393enum loc_api_adapter_err LocApiBase:: 394 enableData(int enable) 395DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 396 397enum loc_api_adapter_err LocApiBase:: 398 setAPN(char* apn, int len) 399DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 400 401enum loc_api_adapter_err LocApiBase:: 402 injectPosition(double latitude, double longitude, float accuracy) 403DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 404 405enum loc_api_adapter_err LocApiBase:: 406 setTime(GpsUtcTime time, int64_t timeReference, int uncertainty) 407DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 408 409enum loc_api_adapter_err LocApiBase:: 410 setXtraData(char* data, int length) 411DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 412 413enum loc_api_adapter_err LocApiBase:: 414 requestXtraServer() 415DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 416 417enum loc_api_adapter_err LocApiBase:: 418 atlOpenStatus(int handle, int is_succ, char* apn, 419 AGpsBearerType bear, AGpsType agpsType) 420DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 421 422enum loc_api_adapter_err LocApiBase:: 423 atlCloseStatus(int handle, int is_succ) 424DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 425 426enum loc_api_adapter_err LocApiBase:: 427 setPositionMode(const LocPosMode& posMode) 428DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 429 430enum loc_api_adapter_err LocApiBase:: 431 setServer(const char* url, int len) 432DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 433 434enum loc_api_adapter_err LocApiBase:: 435 setServer(unsigned int ip, int port, 436 LocServerType type) 437DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 438 439enum loc_api_adapter_err LocApiBase:: 440 informNiResponse(GpsUserResponseType userResponse, 441 const void* passThroughData) 442DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 443 444enum loc_api_adapter_err LocApiBase:: 445 setSUPLVersion(uint32_t version) 446DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 447 448enum loc_api_adapter_err LocApiBase:: 449 setLPPConfig(uint32_t profile) 450DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 451 452enum loc_api_adapter_err LocApiBase:: 453 setSensorControlConfig(int sensorUsage, 454 int sensorProvider) 455DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 456 457enum loc_api_adapter_err LocApiBase:: 458 setSensorProperties(bool gyroBiasVarianceRandomWalk_valid, 459 float gyroBiasVarianceRandomWalk, 460 bool accelBiasVarianceRandomWalk_valid, 461 float accelBiasVarianceRandomWalk, 462 bool angleBiasVarianceRandomWalk_valid, 463 float angleBiasVarianceRandomWalk, 464 bool rateBiasVarianceRandomWalk_valid, 465 float rateBiasVarianceRandomWalk, 466 bool velocityBiasVarianceRandomWalk_valid, 467 float velocityBiasVarianceRandomWalk) 468DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 469 470enum loc_api_adapter_err LocApiBase:: 471 setSensorPerfControlConfig(int controlMode, 472 int accelSamplesPerBatch, 473 int accelBatchesPerSec, 474 int gyroSamplesPerBatch, 475 int gyroBatchesPerSec, 476 int accelSamplesPerBatchHigh, 477 int accelBatchesPerSecHigh, 478 int gyroSamplesPerBatchHigh, 479 int gyroBatchesPerSecHigh, 480 int algorithmConfig) 481DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 482 483enum loc_api_adapter_err LocApiBase:: 484 setExtPowerConfig(int isBatteryCharging) 485DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 486 487enum loc_api_adapter_err LocApiBase:: 488 setAGLONASSProtocol(unsigned long aGlonassProtocol) 489DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 490 491enum loc_api_adapter_err LocApiBase:: 492 getWwanZppFix(GpsLocation& zppLoc) 493DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 494 495enum loc_api_adapter_err LocApiBase:: 496 getBestAvailableZppFix(GpsLocation& zppLoc) 497{ 498 memset(&zppLoc, 0, sizeof(zppLoc)); 499 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 500} 501 502enum loc_api_adapter_err LocApiBase:: 503 getBestAvailableZppFix(GpsLocation & zppLoc, LocPosTechMask & tech_mask) 504{ 505 memset(&zppLoc, 0, sizeof(zppLoc)); 506 memset(&tech_mask, 0, sizeof(tech_mask)); 507 DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 508} 509 510int LocApiBase:: 511 initDataServiceClient() 512DEFAULT_IMPL(-1) 513 514int LocApiBase:: 515 openAndStartDataCall() 516DEFAULT_IMPL(-1) 517 518void LocApiBase:: 519 stopDataCall() 520DEFAULT_IMPL() 521 522void LocApiBase:: 523 closeDataCall() 524DEFAULT_IMPL() 525 526int LocApiBase:: 527 setGpsLock(LOC_GPS_LOCK_MASK lock) 528DEFAULT_IMPL(-1) 529 530void LocApiBase:: 531 installAGpsCert(const DerEncodedCertificate* pData, 532 size_t length, 533 uint32_t slotBitMask) 534DEFAULT_IMPL() 535 536int LocApiBase:: 537 getGpsLock() 538DEFAULT_IMPL(-1) 539 540enum loc_api_adapter_err LocApiBase:: 541 setXtraVersionCheck(enum xtra_version_check check) 542DEFAULT_IMPL(LOC_API_ADAPTER_ERR_SUCCESS) 543 544int LocApiBase:: 545 updateRegistrationMask(LOC_API_ADAPTER_EVENT_MASK_T event, 546 loc_registration_mask_status isEnabled) 547DEFAULT_IMPL(-1) 548 549bool LocApiBase:: 550 gnssConstellationConfig() 551DEFAULT_IMPL(false) 552 553} // namespace loc_core 554