XtraSystemStatusObserver.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Copyright (c) 2017-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 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. #define LOG_TAG "LocSvc_XtraSystemStatusObs"
  59. #include <sys/stat.h>
  60. #include <sys/un.h>
  61. #include <errno.h>
  62. #include <ctype.h>
  63. #include <cutils/properties.h>
  64. #include <math.h>
  65. #include <arpa/inet.h>
  66. #include <netinet/in.h>
  67. #include <netdb.h>
  68. #include <string>
  69. #include <loc_log.h>
  70. #include <loc_nmea.h>
  71. #include <SystemStatus.h>
  72. #include <vector>
  73. #include <sstream>
  74. #include <XtraSystemStatusObserver.h>
  75. #include <LocAdapterBase.h>
  76. #include <DataItemId.h>
  77. #include <DataItemsFactoryProxy.h>
  78. #include <DataItemConcreteTypes.h>
  79. using namespace loc_util;
  80. using namespace loc_core;
  81. #ifdef LOG_TAG
  82. #undef LOG_TAG
  83. #endif
  84. #define LOG_TAG "LocSvc_XSSO"
  85. class XtraIpcListener : public ILocIpcListener {
  86. IOsObserver* mSystemStatusObsrvr;
  87. const MsgTask* mMsgTask;
  88. XtraSystemStatusObserver& mXSSO;
  89. public:
  90. inline XtraIpcListener(IOsObserver* observer, const MsgTask* msgTask,
  91. XtraSystemStatusObserver& xsso) :
  92. mSystemStatusObsrvr(observer), mMsgTask(msgTask), mXSSO(xsso) {}
  93. virtual void onReceive(const char* data, uint32_t length __unused,
  94. const LocIpcRecver* recver __unused) override {
  95. #define STRNCMP(str, constStr) strncmp(str, constStr, sizeof(constStr)-1)
  96. if (!STRNCMP(data, "ping")) {
  97. LOC_LOGd("ping received");
  98. #ifdef USE_GLIB
  99. } else if ((!STRNCMP(data, "connectBackhaul")) || (!STRNCMP(data, "disconnectBackhaul"))) {
  100. char clientName[30] = {0};
  101. uint16_t prefSub;
  102. char prefApnName[30] = {0};
  103. uint16_t prefIpType;
  104. int ret = sscanf(data, "%*s %29s %u %29s %u",
  105. clientName, &prefSub, prefApnName, &prefIpType);
  106. BackhaulContext ctx = { clientName, prefSub,
  107. (0 == STRNCMP(prefApnName, "EMPTY")) ? "" : prefApnName, prefIpType };
  108. if (!STRNCMP(data, "connectBackhaul")) {
  109. mSystemStatusObsrvr->connectBackhaul(ctx);
  110. } else {
  111. mSystemStatusObsrvr->disconnectBackhaul(ctx);
  112. }
  113. #endif
  114. } else if (!STRNCMP(data, "requestStatus")) {
  115. int32_t xtraStatusUpdated = 0;
  116. char socketName[40] = {0};
  117. sscanf(data, "%*s %d %39s", &xtraStatusUpdated, socketName);
  118. struct HandleStatusRequestMsg : public LocMsg {
  119. XtraSystemStatusObserver& mXSSO;
  120. int32_t mStatusUpdated;
  121. string mSocketName;
  122. inline HandleStatusRequestMsg(XtraSystemStatusObserver& xsso,
  123. int32_t statusUpdated, string socketName) :
  124. mXSSO(xsso), mStatusUpdated(statusUpdated),
  125. mSocketName(socketName) {}
  126. inline void proc() const override {
  127. mXSSO.onStatusRequested(mStatusUpdated);
  128. /* SSR for DGnss Ntrip Source*/
  129. if (0 == mSocketName.compare(LOC_IPC_DGNSS)) {
  130. mXSSO.restartDgnssSource();
  131. }
  132. }
  133. };
  134. mMsgTask->sendMsg(new HandleStatusRequestMsg(mXSSO, xtraStatusUpdated, socketName));
  135. } else {
  136. LOC_LOGw("unknown event: %s", data);
  137. }
  138. }
  139. };
  140. XtraSystemStatusObserver::XtraSystemStatusObserver(IOsObserver* sysStatObs,
  141. const MsgTask* msgTask) :
  142. mSystemStatusObsrvr(sysStatObs), mMsgTask(msgTask),
  143. mGpsLock(-1), mConnections(~0), mRoaming(false), mXtraThrottle(true),
  144. mReqStatusReceived(false),
  145. mIsConnectivityStatusKnown(false),
  146. mXtraSender(LocIpc::getLocIpcLocalSender(LOC_IPC_XTRA)),
  147. mDgnssSender(LocIpc::getLocIpcLocalSender(LOC_IPC_DGNSS)),
  148. mDelayLocTimer(*mXtraSender, *mDgnssSender) {
  149. subscribe(true);
  150. auto recver = LocIpc::getLocIpcLocalRecver(
  151. make_shared<XtraIpcListener>(sysStatObs, msgTask, *this),
  152. LOC_IPC_HAL);
  153. mIpc.startNonBlockingListening(recver);
  154. mDelayLocTimer.start(100 /*.1 sec*/, false);
  155. }
  156. bool XtraSystemStatusObserver::updateLockStatus(GnssConfigGpsLock lock) {
  157. // mask NI(NFW bit) since from XTRA's standpoint GPS is enabled if
  158. // MO(AFW bit) is enabled and disabled when MO is disabled
  159. mGpsLock = lock & ~GNSS_CONFIG_GPS_LOCK_NFW_ALL;
  160. if (!mReqStatusReceived) {
  161. return true;
  162. }
  163. stringstream ss;
  164. ss << "gpslock";
  165. ss << " " << mGpsLock;
  166. string s = ss.str();
  167. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()) );
  168. }
  169. bool XtraSystemStatusObserver::updateConnections(uint64_t allConnections,
  170. NetworkInfoType* networkHandleInfo, bool roaming) {
  171. mIsConnectivityStatusKnown = true;
  172. mConnections = allConnections;
  173. mRoaming = roaming;
  174. LOC_LOGd("updateConnections mConnections:%" PRIx64 " mRoaming:%u",
  175. mConnections, mRoaming);
  176. for (uint8_t i = 0; i < MAX_NETWORK_HANDLES; ++i) {
  177. mNetworkHandle[i] = networkHandleInfo[i];
  178. LOC_LOGd("updateConnections [%d] networkHandle:%" PRIx64 " networkType:%u",
  179. i, mNetworkHandle[i].networkHandle, mNetworkHandle[i].networkType);
  180. }
  181. if (!mReqStatusReceived) {
  182. return true;
  183. }
  184. stringstream ss;
  185. ss << "connection" << endl << mConnections << endl
  186. << mNetworkHandle[0].toString() << endl
  187. << mNetworkHandle[1].toString() << endl
  188. << mNetworkHandle[2].toString() << endl
  189. << mNetworkHandle[3].toString() << endl
  190. << mNetworkHandle[4].toString() << endl
  191. << mNetworkHandle[5].toString() << endl
  192. << mNetworkHandle[6].toString() << endl
  193. << mNetworkHandle[7].toString() << endl
  194. << mNetworkHandle[8].toString() << endl
  195. << mNetworkHandle[MAX_NETWORK_HANDLES-1].toString() << endl
  196. << (mRoaming ? 1 : 0);
  197. string s = ss.str();
  198. LocIpc::send(*mDgnssSender, (const uint8_t*)s.data(), s.size());
  199. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()));
  200. }
  201. bool XtraSystemStatusObserver::updateTac(const string& tac) {
  202. mTac = tac;
  203. if (!mReqStatusReceived) {
  204. return true;
  205. }
  206. stringstream ss;
  207. ss << "tac";
  208. ss << " " << tac.c_str();
  209. string s = ss.str();
  210. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()) );
  211. }
  212. bool XtraSystemStatusObserver::updateMccMnc(const string& mccmnc) {
  213. mMccmnc = mccmnc;
  214. if (!mReqStatusReceived) {
  215. return true;
  216. }
  217. stringstream ss;
  218. ss << "mncmcc";
  219. ss << " " << mccmnc.c_str();
  220. string s = ss.str();
  221. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()) );
  222. }
  223. bool XtraSystemStatusObserver::updateXtraThrottle(const bool enabled) {
  224. mXtraThrottle = enabled;
  225. if (!mReqStatusReceived) {
  226. return true;
  227. }
  228. stringstream ss;
  229. ss << "xtrathrottle";
  230. ss << " " << (enabled ? 1 : 0);
  231. string s = ss.str();
  232. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()) );
  233. }
  234. bool XtraSystemStatusObserver::notifySessionStart() {
  235. if (!mReqStatusReceived) {
  236. return true;
  237. }
  238. string s = "sessionstart";
  239. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()) );
  240. }
  241. inline bool XtraSystemStatusObserver::onStatusRequested(int32_t statusUpdated) {
  242. mReqStatusReceived = true;
  243. if (statusUpdated) {
  244. return true;
  245. }
  246. stringstream ss;
  247. ss << "respondStatus" << endl;
  248. (mGpsLock == -1 ? ss : ss << mGpsLock) << endl;
  249. (mConnections == (uint64_t)~0 ? ss : ss << mConnections) << endl
  250. << mNetworkHandle[0].toString() << endl
  251. << mNetworkHandle[1].toString() << endl
  252. << mNetworkHandle[2].toString() << endl
  253. << mNetworkHandle[3].toString() << endl
  254. << mNetworkHandle[4].toString() << endl
  255. << mNetworkHandle[5].toString() << endl
  256. << mNetworkHandle[6].toString() << endl
  257. << mNetworkHandle[7].toString() << endl
  258. << mNetworkHandle[8].toString() << endl
  259. << mNetworkHandle[MAX_NETWORK_HANDLES-1].toString() << endl
  260. << mTac << endl << mMccmnc << endl << mIsConnectivityStatusKnown;
  261. string s = ss.str();
  262. LocIpc::send(*mDgnssSender, (const uint8_t*)s.data(), s.size());
  263. return ( LocIpc::send(*mXtraSender, (const uint8_t*)s.data(), s.size()) );
  264. }
  265. void XtraSystemStatusObserver::startDgnssSource(const StartDgnssNtripParams& params) {
  266. stringstream ss;
  267. const GnssNtripConnectionParams* ntripParams = &(params.ntripParams);
  268. ss << "startDgnssSource" << endl;
  269. ss << ntripParams->useSSL << endl;
  270. ss << ntripParams->hostNameOrIp.data() << endl;
  271. ss << ntripParams->port << endl;
  272. ss << ntripParams->mountPoint.data() << endl;
  273. ss << ntripParams->username.data() << endl;
  274. ss << ntripParams->password.data() << endl;
  275. if (ntripParams->requiresNmeaLocation && !params.nmea.empty()) {
  276. ss << params.nmea.data() << endl;
  277. }
  278. string s = ss.str();
  279. LOC_LOGd("%s", s.data());
  280. LocIpc::send(*mDgnssSender, (const uint8_t*)s.data(), s.size());
  281. // make a local copy of the string for SSR
  282. mNtripParamsString.assign(std::move(s));
  283. }
  284. void XtraSystemStatusObserver::restartDgnssSource() {
  285. if (!mNtripParamsString.empty()) {
  286. LocIpc::send(*mDgnssSender,
  287. (const uint8_t*)mNtripParamsString.data(), mNtripParamsString.size());
  288. LOC_LOGv("Xtra SSR %s", mNtripParamsString.data());
  289. }
  290. }
  291. void XtraSystemStatusObserver::stopDgnssSource() {
  292. LOC_LOGv();
  293. mNtripParamsString.clear();
  294. const char s[] = "stopDgnssSource";
  295. LocIpc::send(*mDgnssSender, (const uint8_t*)s, strlen(s));
  296. }
  297. void XtraSystemStatusObserver::updateNmeaToDgnssServer(const string& nmea)
  298. {
  299. stringstream ss;
  300. ss << "updateDgnssServerNmea" << endl;
  301. ss << nmea.data() << endl;
  302. string s = ss.str();
  303. LOC_LOGd("%s", s.data());
  304. LocIpc::send(*mDgnssSender, (const uint8_t*)s.data(), s.size());
  305. }
  306. void XtraSystemStatusObserver::subscribe(bool yes)
  307. {
  308. // Subscription data unordered_set
  309. unordered_set<DataItemId> subItemIdSet;
  310. subItemIdSet.insert(NETWORKINFO_DATA_ITEM_ID);
  311. subItemIdSet.insert(MCCMNC_DATA_ITEM_ID);
  312. if (yes) {
  313. mSystemStatusObsrvr->subscribe(subItemIdSet, this);
  314. unordered_set<DataItemId> reqItemIdSet;
  315. reqItemIdSet.insert(TAC_DATA_ITEM_ID);
  316. mSystemStatusObsrvr->requestData(reqItemIdSet, this);
  317. } else {
  318. mSystemStatusObsrvr->unsubscribe(subItemIdSet, this);
  319. }
  320. }
  321. // IDataItemObserver overrides
  322. void XtraSystemStatusObserver::getName(string& name)
  323. {
  324. name = "XtraSystemStatusObserver";
  325. }
  326. void XtraSystemStatusObserver::notify(const unordered_set<IDataItemCore*>& dlist)
  327. {
  328. struct HandleOsObserverUpdateMsg : public LocMsg {
  329. XtraSystemStatusObserver* mXtraSysStatObj;
  330. list <IDataItemCore*> mDataItemList;
  331. inline HandleOsObserverUpdateMsg(XtraSystemStatusObserver* xtraSysStatObs,
  332. const unordered_set<IDataItemCore*>& dataItemList) :
  333. mXtraSysStatObj(xtraSysStatObs) {
  334. for (auto eachItem : dataItemList) {
  335. IDataItemCore* dataitem = DataItemsFactoryProxy::createNewDataItem(eachItem);
  336. if (NULL == dataitem) {
  337. break;
  338. }
  339. mDataItemList.push_back(dataitem);
  340. }
  341. }
  342. inline ~HandleOsObserverUpdateMsg() {
  343. for (auto itor = mDataItemList.begin(); itor != mDataItemList.end(); ++itor) {
  344. if (*itor != nullptr) {
  345. delete *itor;
  346. *itor = nullptr;
  347. }
  348. }
  349. }
  350. inline void proc() const {
  351. for (auto each : mDataItemList) {
  352. switch (each->getId())
  353. {
  354. case NETWORKINFO_DATA_ITEM_ID:
  355. {
  356. NetworkInfoDataItem* networkInfo = static_cast<NetworkInfoDataItem*>(each);
  357. NetworkInfoType* networkHandleInfo =
  358. static_cast<NetworkInfoType*>(networkInfo->getNetworkHandle());
  359. mXtraSysStatObj->updateConnections(networkInfo->getAllTypes(),
  360. networkHandleInfo, (*networkInfo).mRoaming);
  361. }
  362. break;
  363. case TAC_DATA_ITEM_ID:
  364. {
  365. TacDataItem* tac = static_cast<TacDataItem*>(each);
  366. mXtraSysStatObj->updateTac(tac->mValue);
  367. }
  368. break;
  369. case MCCMNC_DATA_ITEM_ID:
  370. {
  371. MccmncDataItem* mccmnc = static_cast<MccmncDataItem*>(each);
  372. mXtraSysStatObj->updateMccMnc(mccmnc->mValue);
  373. }
  374. break;
  375. default:
  376. break;
  377. }
  378. }
  379. }
  380. };
  381. mMsgTask->sendMsg(new (nothrow) HandleOsObserverUpdateMsg(this, dlist));
  382. }