XtraSystemStatusObserver.cpp 14 KB

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