1 /*
2  * Copyright (c) 2019, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials provided
12  *       with the distribution.
13  *     * Neither the name of The Linux Foundation nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <android/hardware/gnss/visibility_control/1.0/IGnssVisibilityControl.h>
31 #include <hidl/MQDescriptor.h>
32 #include <hidl/Status.h>
33 #include "GnssVisibilityControl.h"
34 #include <location_interface.h>
35 
36 namespace android {
37 namespace hardware {
38 namespace gnss {
39 namespace visibility_control {
40 namespace V1_0 {
41 namespace implementation {
42 
43 using ::android::hardware::hidl_array;
44 using ::android::hardware::hidl_memory;
45 using ::android::hardware::hidl_string;
46 using ::android::hardware::hidl_vec;
47 using ::android::hardware::Return;
48 using ::android::hardware::Void;
49 using ::android::sp;
50 
51 static GnssVisibilityControl* spGnssVisibilityControl = nullptr;
52 
53 static void convertGnssNfwNotification(GnssNfwNotification& in,
54     IGnssVisibilityControlCallback::NfwNotification& out);
55 
GnssVisibilityControl(Gnss * gnss)56 GnssVisibilityControl::GnssVisibilityControl(Gnss* gnss) : mGnss(gnss) {
57     spGnssVisibilityControl = this;
58 }
~GnssVisibilityControl()59 GnssVisibilityControl::~GnssVisibilityControl() {
60     spGnssVisibilityControl = nullptr;
61 }
62 
nfwStatusCb(GnssNfwNotification notification)63 void GnssVisibilityControl::nfwStatusCb(GnssNfwNotification notification) {
64     if (nullptr != spGnssVisibilityControl) {
65         spGnssVisibilityControl->statusCb(notification);
66     }
67 }
68 
isInEmergencySession()69 bool GnssVisibilityControl::isInEmergencySession() {
70     if (nullptr != spGnssVisibilityControl) {
71         return spGnssVisibilityControl->isE911Session();
72     }
73     return false;
74 }
75 
convertGnssNfwNotification(GnssNfwNotification & in,IGnssVisibilityControlCallback::NfwNotification & out)76 static void convertGnssNfwNotification(GnssNfwNotification& in,
77     IGnssVisibilityControlCallback::NfwNotification& out)
78 {
79     memset(&out, 0, sizeof(IGnssVisibilityControlCallback::NfwNotification));
80     out.proxyAppPackageName = in.proxyAppPackageName;
81     out.protocolStack = (IGnssVisibilityControlCallback::NfwProtocolStack)in.protocolStack;
82     out.otherProtocolStackName = in.otherProtocolStackName;
83     out.requestor = (IGnssVisibilityControlCallback::NfwRequestor)in.requestor;
84     out.requestorId = in.requestorId;
85     out.responseType = (IGnssVisibilityControlCallback::NfwResponseType)in.responseType;
86     out.inEmergencyMode = in.inEmergencyMode;
87     out.isCachedLocation = in.isCachedLocation;
88 }
89 
statusCb(GnssNfwNotification notification)90 void GnssVisibilityControl::statusCb(GnssNfwNotification notification) {
91 
92     if (mGnssVisibilityControlCbIface != nullptr) {
93         IGnssVisibilityControlCallback::NfwNotification nfwNotification;
94 
95         // Convert from one structure to another
96         convertGnssNfwNotification(notification, nfwNotification);
97 
98         auto r = mGnssVisibilityControlCbIface->nfwNotifyCb(nfwNotification);
99         if (!r.isOk()) {
100             LOC_LOGw("Error invoking NFW status cb %s", r.description().c_str());
101         }
102     } else {
103         LOC_LOGw("setCallback has not been called yet");
104     }
105 }
106 
isE911Session()107 bool GnssVisibilityControl::isE911Session() {
108 
109     if (mGnssVisibilityControlCbIface != nullptr) {
110         auto r = mGnssVisibilityControlCbIface->isInEmergencySession();
111         if (!r.isOk()) {
112             LOC_LOGw("Error invoking NFW status cb %s", r.description().c_str());
113             return false;
114         } else {
115             return (r);
116         }
117     } else {
118         LOC_LOGw("setCallback has not been called yet");
119         return false;
120     }
121 }
122 
123 // Methods from ::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControl follow.
enableNfwLocationAccess(const hidl_vec<::android::hardware::hidl_string> & proxyApps)124 Return<bool> GnssVisibilityControl::enableNfwLocationAccess(const hidl_vec<::android::hardware::hidl_string>& proxyApps) {
125 
126     if (nullptr == mGnss || nullptr == mGnss->getGnssInterface()) {
127         LOC_LOGe("Null GNSS interface");
128         return false;
129     }
130 
131     /* If the vector is empty we need to disable all NFW clients
132        If there is at least one app in the vector we need to enable
133        all NFW clients */
134     if (0 == proxyApps.size()) {
135         mGnss->getGnssInterface()->enableNfwLocationAccess(false);
136     } else {
137         mGnss->getGnssInterface()->enableNfwLocationAccess(true);
138     }
139 
140     return true;
141 }
142 /**
143  * Registers the callback for HAL implementation to use.
144  *
145  * @param callback Handle to IGnssVisibilityControlCallback interface.
146  */
setCallback(const::android::sp<::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControlCallback> & callback)147 Return<bool> GnssVisibilityControl::setCallback(const ::android::sp<::android::hardware::gnss::visibility_control::V1_0::IGnssVisibilityControlCallback>& callback) {
148 
149     if (nullptr == mGnss || nullptr == mGnss->getGnssInterface()) {
150         LOC_LOGe("Null GNSS interface");
151         return false;
152     }
153     mGnssVisibilityControlCbIface = callback;
154 
155     NfwCbInfo cbInfo = {};
156     cbInfo.visibilityControlCb = (void*)nfwStatusCb;
157     cbInfo.isInEmergencySession = (void*)isInEmergencySession;
158 
159     mGnss->getGnssInterface()->nfwInit(cbInfo);
160 
161     return true;
162 }
163 
164 }  // namespace implementation
165 }  // namespace V1_0
166 }  // namespace visibility_control
167 }  // namespace gnss
168 }  // namespace hardware
169 }  // namespace android
170