Skip to content

Commit

Permalink
[android] android app supports Thread Commissioning (project-chip#1988)
Browse files Browse the repository at this point in the history
* [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
wgtdkp and restyled-commits authored Aug 13, 2020
1 parent bcb7b5c commit d61348d
Show file tree
Hide file tree
Showing 30 changed files with 1,483 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@
branch = master
ignore = dirty
commit = 18e137db73e2ae6d307e62ac8430f7326efdb0c3
[submodule "third_party/ot-commissioner/repo"]
path = third_party/ot-commissioner/repo
url = https://github.com/openthread/ot-commissioner
16 changes: 12 additions & 4 deletions src/android/CHIPTool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ jniLibs/arm64-v8a

10. You will also need the "libc++\_shared.so" file in the jniLibs folder. This
file comes packaged with Android NDK and can be found under
$ANDROID_NDK_HOME/sources/cxx-stl/llvm-libc++/libs/$TARGET
`$ANDROID_NDK_HOME/sources/cxx-stl/llvm-libc++/libs/$TARGET`.

(Eventually hoping to not have to include this .so, but that needs some more
tweaking of the Android automake build rules. Include it in the interim to be
able to build the Android app).
(Eventually hoping to not have to include this .so, but that needs some more
tweaking of the Android automake build rules. Include it in the interim to
be able to build the Android app).

11. Build OT Commissioner

```shell
git submodule update --init --recursive third_party/ot-commissioner/repo
./third_party/ot-commissioner/build-android-libs.sh
## JAR and .so libraries will be copy to target directories.
```
5 changes: 5 additions & 0 deletions src/android/CHIPTool/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ dependencies {
implementation 'androidx.preference:preference:1.1.1'
implementation "com.google.android.gms:play-services-vision:20.1.0"
implementation "androidx.annotation:annotation:1.1.0"
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "androidx.core:core-ktx:1.3.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.work:work-runtime:2.3.3"
implementation 'com.google.code.gson:gson:2.8.5'
}
repositories {
mavenCentral()
Expand Down
6 changes: 6 additions & 0 deletions src/android/CHIPTool/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.google.chip.chiptool">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -22,6 +23,11 @@
android:name=".OnOffActivity"
android:exported="false"
android:screenOrientation="portrait"/>

<activity
android:name="com.google.chip.chiptool.commissioner.CommissionerActivity"
android:label="@string/commissioner_name">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/
package com.google.chip.chiptool

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.chip.chiptool.commissioner.CommissionerActivity
import com.google.chip.chiptool.echoclient.EchoClientFragment
import com.google.chip.chiptool.clusterclient.OnOffClientFragment
import com.google.chip.chiptool.setuppayloadscanner.BarcodeFragment
Expand Down Expand Up @@ -52,6 +54,11 @@ class CHIPToolActivity :
showFragment(BarcodeFragment.newInstance())
}

override fun handleCommissioningClicked() {
var intent = Intent(this, CommissionerActivity::class.java)
startActivityForResult(intent, REQUEST_CODE_COMMISSIONING)
}

override fun handleEchoClientClicked() {
showFragment(EchoClientFragment.newInstance())
}
Expand All @@ -60,11 +67,23 @@ class CHIPToolActivity :
showFragment(OnOffClientFragment.newInstance())
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

// Simply ignore the commissioning result.

// TODO: tracking commissioned devices.
}

private fun showFragment(fragment: Fragment) {
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment, fragment.javaClass.simpleName)
.addToBackStack(null)
.commit()
}

companion object {
var REQUEST_CODE_COMMISSIONING = 0xB003
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SelectActionFragment : Fragment() {
): View {
return inflater.inflate(R.layout.select_action_fragment, container, false).apply {
scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
commissioningBtn.setOnClickListener { getCallback()?.handleCommissioningClicked() }
echoClientBtn.setOnClickListener { getCallback()?.handleEchoClientClicked() }
onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
}
Expand All @@ -46,6 +47,8 @@ class SelectActionFragment : Fragment() {
interface Callback {
/** Notifies listener of Scan QR code button click. */
fun handleScanQrCodeClicked()
/** Notifies listener of Commissioning button click. */
fun handleCommissioningClicked()
/** Notifies listener of Echo client button click. */
fun handleEchoClientClicked()
/** Notifies listener of send command button click. */
Expand Down
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);
}
}
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];
}
};
}
Loading

0 comments on commit d61348d

Please sign in to comment.