forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] android app supports Thread Commissioning (project-chip#1988)
* [android] android app supports Thread Commissioning * Restyled by google-java-format * Restyled by prettier-markdown * addresses comments * addresses comments * fix device info encoding * Restyled by google-java-format * remove ot-commissioner from gradle build * update readme * update README to include checking out ot-commissioner * add comments Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
1 parent
bcb7b5c
commit d61348d
Showing
30 changed files
with
1,483 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...IPTool/app/src/main/java/com/google/chip/chiptool/commissioner/BorderAgentDiscoverer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.google.chip.chiptool.commissioner; | ||
|
||
import android.Manifest; | ||
import android.Manifest.permission; | ||
import android.content.Context; | ||
import android.net.nsd.NsdManager; | ||
import android.net.nsd.NsdServiceInfo; | ||
import android.net.wifi.WifiManager; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.util.Log; | ||
import androidx.annotation.RequiresPermission; | ||
import java.util.Map; | ||
|
||
public class BorderAgentDiscoverer implements NsdManager.DiscoveryListener { | ||
|
||
private static final String TAG = BorderAgentDiscoverer.class.getSimpleName(); | ||
|
||
private static final String SERVICE_TYPE = "_meshcop._udp"; | ||
private static final String KEY_DISCRIMINATOR = "discriminator"; | ||
private static final String KEY_NETWORK_NAME = "nn"; | ||
private static final String KEY_EXTENDED_PAN_ID = "xp"; | ||
|
||
private static final byte[] PSKC = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; | ||
|
||
private WifiManager.MulticastLock wifiMulticastLock; | ||
private NsdManager nsdManager; | ||
private NetworkAdapter networkAdapter; | ||
|
||
@RequiresPermission(permission.INTERNET) | ||
public BorderAgentDiscoverer(Context context, NetworkAdapter networkAdapter) { | ||
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); | ||
wifiMulticastLock = wifi.createMulticastLock("multicastLock"); | ||
|
||
nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE); | ||
|
||
this.networkAdapter = networkAdapter; | ||
} | ||
|
||
public void start() { | ||
wifiMulticastLock.setReferenceCounted(true); | ||
wifiMulticastLock.acquire(); | ||
|
||
nsdManager.discoverServices( | ||
BorderAgentDiscoverer.SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, this); | ||
} | ||
|
||
public void stop() { | ||
nsdManager.stopServiceDiscovery(this); | ||
|
||
if (wifiMulticastLock != null) { | ||
wifiMulticastLock.release(); | ||
wifiMulticastLock = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void onDiscoveryStarted(String serviceType) { | ||
Log.d(TAG, "start discovering Border Agent"); | ||
} | ||
|
||
@Override | ||
public void onDiscoveryStopped(String serviceType) { | ||
Log.d(TAG, "stop discovering Border Agent"); | ||
} | ||
|
||
@Override | ||
public void onServiceFound(NsdServiceInfo nsdServiceInfo) { | ||
Log.d(TAG, "a Border Agent service found"); | ||
|
||
nsdManager.resolveService( | ||
nsdServiceInfo, | ||
new NsdManager.ResolveListener() { | ||
@Override | ||
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { | ||
Log.e( | ||
TAG, | ||
String.format( | ||
"failed to resolve service %s, error: %d", serviceInfo.toString(), errorCode)); | ||
} | ||
|
||
@Override | ||
public void onServiceResolved(NsdServiceInfo serviceInfo) { | ||
Log.d(TAG, "successfully resolved service " + serviceInfo.toString()); | ||
|
||
Map<String, byte[]> attrs = serviceInfo.getAttributes(); | ||
|
||
String discriminator = "CC11BB22"; | ||
|
||
try { | ||
if (attrs.containsKey(KEY_DISCRIMINATOR)) { | ||
discriminator = new String(attrs.get(KEY_DISCRIMINATOR)); | ||
} | ||
final BorderAgentInfo borderAgent = | ||
new BorderAgentInfo( | ||
discriminator, | ||
new String(attrs.get(KEY_NETWORK_NAME)), | ||
attrs.get(KEY_EXTENDED_PAN_ID), | ||
serviceInfo.getHost(), | ||
serviceInfo.getPort(), | ||
PSKC); | ||
|
||
Handler handler = new Handler(Looper.getMainLooper()); | ||
handler.post( | ||
new Runnable() { | ||
@RequiresPermission(Manifest.permission.CAMERA) | ||
@Override | ||
public void run() { | ||
networkAdapter.addNetwork(new NetworkInfo(borderAgent)); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
Log.e(TAG, "invalid Border Agent service: " + e.toString()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onServiceLost(NsdServiceInfo nsdServiceInfo) { | ||
Log.d(TAG, "a Border Agent service is gone"); | ||
} | ||
|
||
@Override | ||
public void onStartDiscoveryFailed(String serviceType, int errorCode) { | ||
Log.d(TAG, "start discovering Border Agent failed: " + errorCode); | ||
} | ||
|
||
@Override | ||
public void onStopDiscoveryFailed(String serviceType, int errorCode) { | ||
Log.d(TAG, "stop discovering Border Agent failed: " + errorCode); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...oid/CHIPTool/app/src/main/java/com/google/chip/chiptool/commissioner/BorderAgentInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.google.chip.chiptool.commissioner; | ||
|
||
import android.os.Parcel; | ||
import android.os.Parcelable; | ||
import androidx.annotation.NonNull; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
public class BorderAgentInfo implements Parcelable { | ||
public String discriminator; | ||
public String networkName; | ||
public byte[] extendedPanId; | ||
public InetAddress host; | ||
public int port; | ||
public byte[] pskc; | ||
|
||
public BorderAgentInfo( | ||
@NonNull String discriminator, | ||
@NonNull String networkName, | ||
@NonNull byte[] extendedPanId, | ||
@NonNull InetAddress host, | ||
@NonNull int port, | ||
@NonNull byte[] pskc) { | ||
this.discriminator = discriminator; | ||
this.networkName = networkName; | ||
this.extendedPanId = extendedPanId; | ||
this.host = host; | ||
this.port = port; | ||
this.pskc = pskc; | ||
} | ||
|
||
protected BorderAgentInfo(Parcel in) { | ||
discriminator = in.readString(); | ||
networkName = in.readString(); | ||
extendedPanId = in.createByteArray(); | ||
try { | ||
host = InetAddress.getByAddress(in.createByteArray()); | ||
} catch (UnknownHostException e) { | ||
} | ||
port = in.readInt(); | ||
pskc = in.createByteArray(); | ||
} | ||
|
||
@Override | ||
public void writeToParcel(Parcel dest, int flags) { | ||
dest.writeString(discriminator); | ||
dest.writeString(networkName); | ||
dest.writeByteArray(extendedPanId); | ||
dest.writeByteArray(host.getAddress()); | ||
dest.writeInt(port); | ||
dest.writeByteArray(pskc); | ||
} | ||
|
||
@Override | ||
public int describeContents() { | ||
return 0; | ||
} | ||
|
||
public static final Creator<BorderAgentInfo> CREATOR = | ||
new Creator<BorderAgentInfo>() { | ||
@Override | ||
public BorderAgentInfo createFromParcel(Parcel in) { | ||
return new BorderAgentInfo(in); | ||
} | ||
|
||
@Override | ||
public BorderAgentInfo[] newArray(int size) { | ||
return new BorderAgentInfo[size]; | ||
} | ||
}; | ||
} |
Oops, something went wrong.