1 /* Copyright (c) 2020, 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_NativeAgpsHandler"
30 
31 #include <LocAdapterBase.h>
32 #include <SystemStatus.h>
33 #include <DataItemId.h>
34 #include <DataItemsFactoryProxy.h>
35 #include <DataItemConcreteTypesBase.h>
36 #include <loc_log.h>
37 #include <NativeAgpsHandler.h>
38 #include <GnssAdapter.h>
39 
40 using namespace loc_core;
41 
42 // IDataItemObserver overrides
getName(string & name)43 void NativeAgpsHandler::getName(string& name) {
44     name = "NativeAgpsHandler";
45 }
46 
notify(const list<IDataItemCore * > & dlist)47 void NativeAgpsHandler::notify(const list<IDataItemCore*>& dlist) {
48     for (auto each : dlist) {
49         switch (each->getId()) {
50             case NETWORKINFO_DATA_ITEM_ID: {
51                     NetworkInfoDataItemBase* networkInfo =
52                         static_cast<NetworkInfoDataItemBase*>(each);
53                     uint64_t mobileBit = (uint64_t )1 << loc_core::TYPE_MOBILE;
54                     uint64_t allTypes = networkInfo->mAllTypes;
55                     mConnected = ((networkInfo->mAllTypes & mobileBit) == mobileBit);
56                     /**
57                      * mApn Telephony preferred Access Point Name to use for
58                      * carrier data connection when connected to a cellular network.
59                      * Empty string, otherwise.
60                      */
61                     mApn = networkInfo->mApn;
62                     LOC_LOGd("updated mConnected:%d, mApn: %s", mConnected, mApn.c_str());
63                     break;
64             }
65             default:
66                     break;
67         }
68     }
69 }
70 
71 NativeAgpsHandler* NativeAgpsHandler::sLocalHandle = nullptr;
NativeAgpsHandler(IOsObserver * sysStatObs,GnssAdapter & adapter)72 NativeAgpsHandler::NativeAgpsHandler(IOsObserver* sysStatObs, GnssAdapter& adapter) :
73         mSystemStatusObsrvr(sysStatObs), mConnected(false), mAdapter(adapter) {
74     sLocalHandle = this;
75     list<DataItemId> subItemIdList = {NETWORKINFO_DATA_ITEM_ID};
76     mSystemStatusObsrvr->subscribe(subItemIdList, this);
77 }
78 
~NativeAgpsHandler()79 NativeAgpsHandler::~NativeAgpsHandler() {
80     if (nullptr != mSystemStatusObsrvr) {
81         LOC_LOGd("Unsubscribe for network info.");
82         list<DataItemId> subItemIdList = {NETWORKINFO_DATA_ITEM_ID};
83         mSystemStatusObsrvr->unsubscribe(subItemIdList, this);
84     }
85     sLocalHandle = nullptr;
86     mSystemStatusObsrvr = nullptr;
87 }
88 
89 
getAgpsCbInfo()90 AgpsCbInfo NativeAgpsHandler::getAgpsCbInfo() {
91     AgpsCbInfo nativeCbInfo = {};
92     nativeCbInfo.statusV4Cb = (void*)agnssStatusIpV4Cb;
93     nativeCbInfo.atlType = AGPS_ATL_TYPE_WWAN;
94     return nativeCbInfo;
95 }
96 
agnssStatusIpV4Cb(AGnssExtStatusIpV4 statusInfo)97 void NativeAgpsHandler::agnssStatusIpV4Cb(AGnssExtStatusIpV4 statusInfo) {
98     if (nullptr != sLocalHandle) {
99         sLocalHandle->processATLRequestRelease(statusInfo);
100     } else {
101         LOC_LOGe("sLocalHandle is null");
102     }
103 }
104 
processATLRequestRelease(AGnssExtStatusIpV4 statusInfo)105 void NativeAgpsHandler::processATLRequestRelease(AGnssExtStatusIpV4 statusInfo) {
106     if (LOC_AGPS_TYPE_WWAN_ANY == statusInfo.type) {
107         LOC_LOGd("status.type = %d status.apnTypeMask = 0x%X", statusInfo.type,
108                  statusInfo.apnTypeMask);
109         switch (statusInfo.status) {
110         case LOC_GPS_REQUEST_AGPS_DATA_CONN:
111             if (mConnected) {
112                 mAdapter.dataConnOpenCommand(LOC_AGPS_TYPE_WWAN_ANY, mApn.c_str(), mApn.size(),
113                     AGPS_APN_BEARER_IPV4);
114             } else {
115                 mAdapter.dataConnFailedCommand(LOC_AGPS_TYPE_WWAN_ANY);
116             }
117             break;
118         case LOC_GPS_RELEASE_AGPS_DATA_CONN:
119             mAdapter.dataConnClosedCommand(LOC_AGPS_TYPE_WWAN_ANY);
120             break;
121         default:
122             LOC_LOGe("Invalid Request: %d", statusInfo.status);
123         }
124     } else {
125         LOC_LOGe("mAgpsManger is null or invalid request type!");
126     }
127 }
128