1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi.hotspot2; 18 19 import android.util.Log; 20 import android.util.Pair; 21 22 import com.android.server.wifi.WifiNative; 23 import com.android.server.wifi.hotspot2.anqp.ANQPElement; 24 import com.android.server.wifi.hotspot2.anqp.Constants; 25 26 import java.util.HashSet; 27 import java.util.List; 28 import java.util.Map; 29 import java.util.Set; 30 31 /** 32 * This class handles passpoint specific interactions with the AP, such as ANQP 33 * elements requests, passpoint icon requests, and wireless network management 34 * event notifications. 35 */ 36 public class PasspointEventHandler { 37 private final WifiNative mSupplicantHook; 38 private final Callbacks mCallbacks; 39 40 /** 41 * Interface to be implemented by the client to receive callbacks for passpoint 42 * related events. 43 */ 44 public interface Callbacks { 45 /** 46 * Invoked on received of ANQP response. |anqpElements| will be null on failure. 47 * @param bssid BSSID of the AP 48 * @param anqpElements ANQP elements to be queried 49 */ 50 void onANQPResponse(long bssid, 51 Map<Constants.ANQPElementType, ANQPElement> anqpElements); 52 53 /** 54 * Invoked on received of icon response. |filename| and |data| will be null 55 * on failure. 56 * @param bssid BSSID of the AP 57 * @param filename Name of the icon file 58 * @data icon data bytes 59 */ 60 void onIconResponse(long bssid, String filename, byte[] data); 61 62 /** 63 * Invoked on received of Hotspot 2.0 Wireless Network Management frame. 64 * @param data Wireless Network Management frame data 65 */ 66 void onWnmFrameReceived(WnmData data); 67 } 68 69 public PasspointEventHandler(WifiNative supplicantHook, Callbacks callbacks) { 70 mSupplicantHook = supplicantHook; 71 mCallbacks = callbacks; 72 } 73 74 /** 75 * Request the specified ANQP elements |elements| from the specified AP |bssid|. 76 * @param bssid BSSID of the AP 77 * @param elements ANQP elements to be queried 78 * @return true if request is sent successfully, false otherwise. 79 */ 80 public boolean requestANQP(long bssid, List<Constants.ANQPElementType> elements) { 81 Pair<Set<Integer>, Set<Integer>> querySets = buildAnqpIdSet(elements); 82 if (bssid == 0 || querySets == null) return false; 83 if (!mSupplicantHook.requestAnqp( 84 Utils.macToString(bssid), querySets.first, querySets.second)) { 85 Log.d(Utils.hs2LogTag(getClass()), "ANQP failed on " + Utils.macToString(bssid)); 86 return false; 87 } 88 Log.d(Utils.hs2LogTag(getClass()), "ANQP initiated on " + Utils.macToString(bssid)); 89 return true; 90 } 91 92 /** 93 * Request a passpoint icon file |filename| from the specified AP |bssid|. 94 * @param bssid BSSID of the AP 95 * @param fileName name of the icon file 96 * @return true if request is sent successfully, false otherwise 97 */ 98 public boolean requestIcon(long bssid, String fileName) { 99 if (bssid == 0 || fileName == null) return false; 100 return mSupplicantHook.requestIcon(Utils.macToString(bssid), fileName); 101 } 102 103 /** 104 * Invoked when ANQP query is completed. 105 * TODO(zqiu): currently ANQP completion notification is through WifiMonitor, 106 * this shouldn't be needed once we switch over to wificond for ANQP requests. 107 * @param anqpEvent ANQP result data retrieved. ANQP elements could be empty in the event to 108 * indicate any failures. 109 */ 110 public void notifyANQPDone(AnqpEvent anqpEvent) { 111 if (anqpEvent == null) return; 112 mCallbacks.onANQPResponse(anqpEvent.getBssid(), anqpEvent.getElements()); 113 } 114 115 /** 116 * Invoked when icon query is completed. 117 * TODO(zqiu): currently icon completion notification is through WifiMonitor, 118 * this shouldn't be needed once we switch over to wificond for icon requests. 119 * @param iconEvent icon event data 120 */ 121 public void notifyIconDone(IconEvent iconEvent) { 122 if (iconEvent == null) return; 123 mCallbacks.onIconResponse( 124 iconEvent.getBSSID(), iconEvent.getFileName(), iconEvent.getData()); 125 } 126 127 /** 128 * Invoked when a Wireless Network Management (WNM) frame is received. 129 * TODO(zqiu): currently WNM frame notification is through WifiMonitor, 130 * this shouldn't be needed once we switch over to wificond for WNM frame monitoring. 131 * @param data WNM frame data 132 */ 133 public void notifyWnmFrameReceived(WnmData data) { 134 mCallbacks.onWnmFrameReceived(data); 135 } 136 137 /** 138 * Create the set of ANQP ID's to query. 139 * 140 * @param querySet elements to query 141 * @return Pair of <set of ANQP ID's, set of HS20 subtypes> 142 */ 143 private static Pair<Set<Integer>, Set<Integer>> buildAnqpIdSet( 144 List<Constants.ANQPElementType> querySet) { 145 Set<Integer> anqpIds = new HashSet<>(); 146 Set<Integer> hs20Subtypes = new HashSet<>(); 147 for (Constants.ANQPElementType elementType : querySet) { 148 Integer id = Constants.getANQPElementID(elementType); 149 if (id != null) { 150 anqpIds.add(id); 151 } else { 152 id = Constants.getHS20ElementID(elementType); 153 hs20Subtypes.add(id); 154 } 155 } 156 return Pair.create(anqpIds, hs20Subtypes); 157 } 158 159 } 160