1 /*
2  * Copyright (c) 2017, 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 
21 #define LOG_TAG "LocSvc_AGnssInterface"
22 
23 #include <log_util.h>
24 #include "Gnss.h"
25 #include "AGnss.h"
26 #include <gps_extended_c.h>
27 
28 namespace android {
29 namespace hardware {
30 namespace gnss {
31 namespace V1_0 {
32 namespace implementation {
33 
34 sp<IAGnssCallback> AGnss::sAGnssCbIface = nullptr;
35 
AGnss(Gnss * gnss)36 AGnss::AGnss(Gnss* gnss) : mGnss(gnss) {
37 }
38 
agnssStatusIpV4Cb(IAGnssCallback::AGnssStatusIpV4 status)39 void AGnss::agnssStatusIpV4Cb(IAGnssCallback::AGnssStatusIpV4 status){
40 
41     sAGnssCbIface->agnssStatusIpV4Cb(status);
42 }
43 
setCallback(const sp<IAGnssCallback> & callback)44 Return<void> AGnss::setCallback(const sp<IAGnssCallback>& callback) {
45 
46     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
47         LOC_LOGE("Null GNSS interface");
48         return Void();
49     }
50 
51     // Save the interface
52     sAGnssCbIface = callback;
53 
54     AgpsCbInfo cbInfo = {};
55     cbInfo.statusV4Cb = (void*)agnssStatusIpV4Cb;
56     cbInfo.cbPriority = AGPS_CB_PRIORITY_LOW;
57 
58     mGnss->getGnssInterface()->agpsInit(cbInfo);
59     return Void();
60 }
61 
dataConnClosed()62 Return<bool> AGnss::dataConnClosed() {
63 
64     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
65         LOC_LOGE("Null GNSS interface");
66         return false;
67     }
68 
69     mGnss->getGnssInterface()->agpsDataConnClosed(LOC_AGPS_TYPE_SUPL);
70     return true;
71 }
72 
dataConnFailed()73 Return<bool> AGnss::dataConnFailed() {
74 
75     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
76         LOC_LOGE("Null GNSS interface");
77         return false;
78     }
79 
80     mGnss->getGnssInterface()->agpsDataConnFailed(LOC_AGPS_TYPE_SUPL);
81     return true;
82 }
83 
dataConnOpen(const hidl_string & apn,IAGnss::ApnIpType apnIpType)84 Return<bool> AGnss::dataConnOpen(const hidl_string& apn,
85         IAGnss::ApnIpType apnIpType) {
86 
87     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
88         LOC_LOGE("Null GNSS interface");
89         return false;
90     }
91 
92     /* Validate */
93     if(apn.empty()){
94         LOC_LOGE("Invalid APN");
95         return false;
96     }
97 
98     LOC_LOGD("dataConnOpen APN name = [%s]", apn.c_str());
99 
100     mGnss->getGnssInterface()->agpsDataConnOpen(
101             LOC_AGPS_TYPE_SUPL, apn.c_str(), apn.size(), (int)apnIpType);
102     return true;
103 }
104 
setServer(IAGnssCallback::AGnssType type,const hidl_string & hostname,int32_t port)105 Return<bool> AGnss::setServer(IAGnssCallback::AGnssType type,
106                               const hidl_string& hostname,
107                               int32_t port) {
108     if (mGnss == nullptr) {
109         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
110         return false;
111     }
112 
113     GnssConfig config;
114     memset(&config, 0, sizeof(GnssConfig));
115     config.size = sizeof(GnssConfig);
116     config.flags = GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
117     config.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
118     if (type == IAGnssCallback::AGnssType::TYPE_SUPL) {
119         config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_SUPL;
120     } else if (type == IAGnssCallback::AGnssType::TYPE_C2K) {
121         config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_C2K;
122     } else {
123         LOC_LOGE("%s]: invalid AGnssType: %d", __FUNCTION__, static_cast<int>(type));
124         return false;
125     }
126     config.assistanceServer.hostName = strdup(hostname.c_str());
127     config.assistanceServer.port = port;
128     return mGnss->updateConfiguration(config);
129 }
130 
131 }  // namespace implementation
132 }  // namespace V1_0
133 }  // namespace gnss
134 }  // namespace hardware
135 }  // namespace android
136