wfd_scan - kensuke/How-to-Miracast-on-AOSP GitHub Wiki
.
Wireless Display Scanning (p2p) & Device Filtering
Sequence Diagram
Method Flow
1. Settings App
125 public void onResume() {
126 super.onResume();
127
128 Context context = getActivity();
129 IntentFilter filter = new IntentFilter();
130 filter.addAction(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED);
131 context.registerReceiver(mReceiver, filter);
132
133 getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
134 Settings.Global.WIFI_DISPLAY_ON), false, mSettingsObserver);
135
136 mDisplayManager.scanWifiDisplays();
137
138 update();
139 }
2. DisplayManager.java
200 public void scanWifiDisplays() {
201 mGlobal.scanWifiDisplays();
202 }
3. DisplayManagerGlobal.java
256 public void scanWifiDisplays() {
257 try {
258 mDm.scanWifiDisplays();
259 } catch (RemoteException ex) {
260 Log.e(TAG, "Failed to scan for Wifi displays.", ex);
261 }
262 }
4. DisplayManagerService.java
407 public void scanWifiDisplays() {
408 final long token = Binder.clearCallingIdentity();
409 try {
410 synchronized (mSyncRoot) {
411 if (mWifiDisplayAdapter != null) {
412 mWifiDisplayAdapter.requestScanLocked();
413 }
414 }
415 } finally {
416 Binder.restoreCallingIdentity(token);
417 }
418 }
5. WifiDisplayAdapter.java
151 public void requestScanLocked() {
152 if (DEBUG) {
153 Slog.d(TAG, "requestScanLocked");
154 }
155
156 getHandler().post(new Runnable() {
157 @Override
158 public void run() {
159 if (mDisplayController != null) {
160 mDisplayController.requestScan();
161 }
162 }
163 });
164 }
6. WifiDisplayController.java
217 public void requestScan() {
218 discoverPeers();
219 }
294 private void discoverPeers() {
295 if (!mDiscoverPeersInProgress) {
296 mDiscoverPeersInProgress = true;
297 mDiscoverPeersRetriesLeft = DISCOVER_PEERS_MAX_RETRIES;
298 handleScanStarted();
299 tryDiscoverPeers();
300 }
301 }
303 private void tryDiscoverPeers() {
304 mWifiP2pManager.discoverPeers(mWifiP2pChannel, new ActionListener() {
305 @Override
306 public void onSuccess() {
307 if (DEBUG) {
308 Slog.d(TAG, "Discover peers succeeded. Requesting peers now.");
309 }
310
311 mDiscoverPeersInProgress = false;
312 requestPeers();
313 }
314
315 @Override
316 public void onFailure(int reason) {
// ...
350 private void requestPeers() {
351 mWifiP2pManager.requestPeers(mWifiP2pChannel, new PeerListListener() {
352 @Override
353 public void onPeersAvailable(WifiP2pDeviceList peers) {
354 if (DEBUG) {
355 Slog.d(TAG, "Received list of peers.");
356 }
357
358 mAvailableWifiDisplayPeers.clear();
359 for (WifiP2pDevice device : peers.getDeviceList()) {
360 if (DEBUG) {
361 Slog.d(TAG, " " + describeWifiP2pDevice(device));
362 }
363
364 if (isWifiDisplay(device)) {
365 mAvailableWifiDisplayPeers.add(device);
366 }
367 }
368
369 handleScanFinished();
370 }
371 });
372 }
819 private static boolean isWifiDisplay(WifiP2pDevice device) {
820 return device.wfdInfo != null
821 && device.wfdInfo.isWfdEnabled()
822 && isPrimarySinkDeviceType(device.wfdInfo.getDeviceType());
823 }
825 private static boolean isPrimarySinkDeviceType(int deviceType) {
826 return deviceType == WifiP2pWfdInfo.PRIMARY_SINK
827 || deviceType == WifiP2pWfdInfo.SOURCE_OR_PRIMARY_SINK;
828 }
7. WifiP2pManager.java
959 public void discoverPeers(Channel c, ActionListener listener) {
960 checkChannel(c);
961 c.mAsyncChannel.sendMessage(DISCOVER_PEERS, 0, c.putListener(listener));
962 }
8. WifiP2pService.java
847 case WifiP2pManager.DISCOVER_PEERS:
848 // do not send service discovery request while normal find operation.
849 clearSupplicantServiceRequest();
850 if (mWifiNative.p2pFind(DISCOVER_TIMEOUT_S)) {
851 replyToMessage(message, WifiP2pManager.DISCOVER_PEERS_SUCCEEDED);
852 sendP2pDiscoveryChangedBroadcast(true);
853 } else {
854 replyToMessage(message, WifiP2pManager.DISCOVER_PEERS_FAILED,
855 WifiP2pManager.ERROR);
856 }
857 break;
9. WifiNative.java
522 public boolean p2pFind() {
523 return doBooleanCommand("P2P_FIND");
524 }
99 private boolean doBooleanCommand(String command) {
100 if (DBG) Log.d(mTAG, "doBoolean: " + command);
101 return doBooleanCommand(mInterface, command);
102 }
76 private native boolean doBooleanCommand(String iface, String command);
10. wpa_supplicant_8
1652 static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[])
1653 {
1654 return wpa_cli_cmd(ctrl, "P2P_FIND", 0, argc, argv);
1655 }
11. // ..., wlan driver
// ...
12. // other p2p device
// ...