AGnss.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  3. * Not a Contribution
  4. */
  5. /*
  6. * Copyright (C) 2016 The Android Open Source Project
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #define LOG_TAG "LocSvc_AGnssInterface"
  21. #include <log_util.h>
  22. #include "Gnss.h"
  23. #include "AGnss.h"
  24. namespace android {
  25. namespace hardware {
  26. namespace gnss {
  27. namespace V1_0 {
  28. namespace implementation {
  29. static AGnss* spAGnss = nullptr;
  30. AGnss::AGnss(Gnss* gnss) : mGnss(gnss) {
  31. spAGnss = this;
  32. }
  33. AGnss::~AGnss() {
  34. spAGnss = nullptr;
  35. }
  36. void AGnss::agnssStatusIpV4Cb(AGnssExtStatusIpV4 status){
  37. if (nullptr != spAGnss) {
  38. spAGnss->statusIpV4Cb(status);
  39. }
  40. }
  41. void AGnss::statusIpV4Cb(AGnssExtStatusIpV4 status) {
  42. IAGnssCallback::AGnssStatusIpV4 st = {};
  43. switch (status.type) {
  44. case LOC_AGPS_TYPE_SUPL:
  45. st.type = IAGnssCallback::AGnssType::TYPE_SUPL;
  46. break;
  47. case LOC_AGPS_TYPE_C2K:
  48. st.type = IAGnssCallback::AGnssType::TYPE_C2K;
  49. break;
  50. default:
  51. LOC_LOGE("invalid type: %d", status.type);
  52. return;
  53. }
  54. switch (status.status) {
  55. case LOC_GPS_REQUEST_AGPS_DATA_CONN:
  56. st.status = IAGnssCallback::AGnssStatusValue::REQUEST_AGNSS_DATA_CONN;
  57. break;
  58. case LOC_GPS_RELEASE_AGPS_DATA_CONN:
  59. st.status = IAGnssCallback::AGnssStatusValue::RELEASE_AGNSS_DATA_CONN;
  60. break;
  61. case LOC_GPS_AGPS_DATA_CONNECTED:
  62. st.status = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONNECTED;
  63. break;
  64. case LOC_GPS_AGPS_DATA_CONN_DONE:
  65. st.status = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONN_DONE;
  66. break;
  67. case LOC_GPS_AGPS_DATA_CONN_FAILED:
  68. st.status = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONN_FAILED;
  69. break;
  70. default:
  71. LOC_LOGE("invalid status: %d", status.status);
  72. return;
  73. }
  74. st.ipV4Addr = status.ipV4Addr;
  75. mMutex.lock();
  76. auto aGnssCbIface = mAGnssCbIface;
  77. mMutex.unlock();
  78. if (aGnssCbIface != nullptr) {
  79. auto r = aGnssCbIface->agnssStatusIpV4Cb(st);
  80. if (!r.isOk()) {
  81. LOC_LOGw("Error invoking AGNSS status cb %s", r.description().c_str());
  82. }
  83. } else {
  84. LOC_LOGw("setCallback has not been called yet");
  85. }
  86. }
  87. Return<void> AGnss::setCallback(const sp<IAGnssCallback>& callback) {
  88. if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
  89. LOC_LOGE("Null GNSS interface");
  90. return Void();
  91. }
  92. // Save the interface
  93. mMutex.lock();
  94. mAGnssCbIface = callback;
  95. mMutex.unlock();
  96. AgpsCbInfo cbInfo = {};
  97. cbInfo.statusV4Cb = (void*)agnssStatusIpV4Cb;
  98. cbInfo.atlType = AGPS_ATL_TYPE_SUPL | AGPS_ATL_TYPE_SUPL_ES;
  99. mGnss->getGnssInterface()->agpsInit(cbInfo);
  100. return Void();
  101. }
  102. Return<bool> AGnss::dataConnClosed() {
  103. if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
  104. LOC_LOGE("Null GNSS interface");
  105. return false;
  106. }
  107. mGnss->getGnssInterface()->agpsDataConnClosed(LOC_AGPS_TYPE_SUPL);
  108. return true;
  109. }
  110. Return<bool> AGnss::dataConnFailed() {
  111. if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
  112. LOC_LOGE("Null GNSS interface");
  113. return false;
  114. }
  115. mGnss->getGnssInterface()->agpsDataConnFailed(LOC_AGPS_TYPE_SUPL);
  116. return true;
  117. }
  118. Return<bool> AGnss::dataConnOpen(const hidl_string& apn,
  119. IAGnss::ApnIpType apnIpType) {
  120. if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
  121. LOC_LOGE("Null GNSS interface");
  122. return false;
  123. }
  124. /* Validate */
  125. if(apn.empty()){
  126. LOC_LOGE("Invalid APN");
  127. return false;
  128. }
  129. LOC_LOGD("dataConnOpen APN name = [%s]", apn.c_str());
  130. AGpsBearerType bearerType;
  131. switch (apnIpType) {
  132. case IAGnss::ApnIpType::IPV4:
  133. bearerType = AGPS_APN_BEARER_IPV4;
  134. break;
  135. case IAGnss::ApnIpType::IPV6:
  136. bearerType = AGPS_APN_BEARER_IPV6;
  137. break;
  138. case IAGnss::ApnIpType::IPV4V6:
  139. bearerType = AGPS_APN_BEARER_IPV4V6;
  140. break;
  141. default:
  142. bearerType = AGPS_APN_BEARER_IPV4;
  143. break;
  144. }
  145. mGnss->getGnssInterface()->agpsDataConnOpen(
  146. LOC_AGPS_TYPE_SUPL, apn.c_str(), apn.size(), (int)bearerType);
  147. return true;
  148. }
  149. Return<bool> AGnss::setServer(IAGnssCallback::AGnssType type,
  150. const hidl_string& hostname,
  151. int32_t port) {
  152. if (mGnss == nullptr) {
  153. LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
  154. return false;
  155. }
  156. GnssConfig config;
  157. memset(&config, 0, sizeof(GnssConfig));
  158. config.size = sizeof(GnssConfig);
  159. config.flags = GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
  160. config.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
  161. if (type == IAGnssCallback::AGnssType::TYPE_SUPL) {
  162. config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_SUPL;
  163. } else if (type == IAGnssCallback::AGnssType::TYPE_C2K) {
  164. config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_C2K;
  165. } else {
  166. LOC_LOGE("%s]: invalid AGnssType: %d", __FUNCTION__, static_cast<int>(type));
  167. return false;
  168. }
  169. config.assistanceServer.hostName = strdup(hostname.c_str());
  170. config.assistanceServer.port = port;
  171. return mGnss->updateConfiguration(config);
  172. }
  173. } // namespace implementation
  174. } // namespace V1_0
  175. } // namespace gnss
  176. } // namespace hardware
  177. } // namespace android