2 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 package com.google.chip.chiptool.commissioner.thread.internal;
21 import android.util.Log;
22 import androidx.annotation.NonNull;
23 import com.google.chip.chiptool.commissioner.thread.BorderAgentInfo;
24 import com.google.chip.chiptool.commissioner.thread.CommissionerUtils;
25 import com.google.chip.chiptool.commissioner.thread.ThreadCommissionerException;
26 import com.google.chip.chiptool.commissioner.thread.ThreadNetworkCredential;
27 import io.openthread.commissioner.ByteArray;
28 import io.openthread.commissioner.ChannelMask;
29 import io.openthread.commissioner.Commissioner;
30 import io.openthread.commissioner.CommissionerHandler;
31 import io.openthread.commissioner.Config;
32 import io.openthread.commissioner.Error;
33 import io.openthread.commissioner.ErrorCode;
34 import io.openthread.commissioner.LogLevel;
35 import io.openthread.commissioner.Logger;
36 import java.net.InetAddress;
38 class NetworkCredentialFetcher {
40 private static final String TAG = NetworkCredentialFetcher.class.getSimpleName();
42 NativeCommissionerHandler nativeCommissionerHandler = new NativeCommissionerHandler();
43 Commissioner nativeCommissioner;
45 public ThreadNetworkCredential fetchNetworkCredential(
46 @NonNull BorderAgentInfo borderAgentInfo, @NonNull byte[] pskc)
47 throws ThreadCommissionerException {
48 byte[] rawActiveDataset =
49 fetchNetworkCredential(borderAgentInfo.host, borderAgentInfo.port, pskc);
50 return new ThreadNetworkCredential(rawActiveDataset);
53 public void cancel() {
54 if (nativeCommissioner != null) {
55 Log.d(TAG, "cancel requesting credential");
56 nativeCommissioner.cancelRequests();
60 private byte[] fetchNetworkCredential(
61 @NonNull InetAddress address, int port, @NonNull byte[] pskc)
62 throws ThreadCommissionerException {
63 nativeCommissioner = Commissioner.create(nativeCommissionerHandler);
65 Config config = new Config();
66 config.setId("TestComm");
67 config.setDomainName("TestDomain");
68 config.setEnableCcm(false);
69 config.setEnableDtlsDebugLogging(true);
70 config.setPSKc(new ByteArray(pskc));
71 config.setLogger(new NativeCommissionerLogger());
74 // Initialize the native commissioner
75 throwIfFail(nativeCommissioner.init(config));
77 // Petition to be the active commissioner in the Thread Network.
78 String[] existingCommissionerId = new String[1];
80 nativeCommissioner.petition(existingCommissionerId, address.getHostAddress(), port));
82 // Fetch Active Operational Dataset.
83 ByteArray rawActiveDataset = new ByteArray();
84 throwIfFail(nativeCommissioner.getRawActiveDataset(rawActiveDataset, 0xFFFF));
85 nativeCommissioner.resign();
86 nativeCommissioner = null;
87 return CommissionerUtils.getByteArray(rawActiveDataset);
88 } catch (ThreadCommissionerException e) {
89 nativeCommissioner.resign();
90 nativeCommissioner = null;
95 private void throwIfFail(Error error) throws ThreadCommissionerException {
96 if (error.getCode() != ErrorCode.kNone) {
97 throw new ThreadCommissionerException(error.getCode().swigValue(), error.getMessage());
102 class NativeCommissionerLogger extends Logger {
103 private static final String TAG = "NativeCommissioner";
106 public void log(LogLevel level, String region, String msg) {
107 Log.d(TAG, String.format("[ %s ]: %s", region, msg));
111 class NativeCommissionerHandler extends CommissionerHandler {
112 private static final String TAG = NativeCommissionerHandler.class.getSimpleName();
115 public String onJoinerRequest(ByteArray joinerId) {
116 Log.d(TAG, "A joiner is requesting commissioning");
121 public void onJoinerConnected(ByteArray joinerId, Error error) {
122 Log.d(TAG, "A joiner is connected");
126 public boolean onJoinerFinalize(
130 String vendorSwVersion,
131 ByteArray vendorStackVersion,
132 String provisioningUrl,
133 ByteArray vendorData) {
134 Log.d(TAG, "A joiner is finalizing");
139 public void onKeepAliveResponse(Error error) {
140 Log.d(TAG, "received keep-alive response: " + error.toString());
144 public void onPanIdConflict(String peerAddr, ChannelMask channelMask, int panId) {
145 Log.d(TAG, "received PAN ID CONFLICT report");
149 public void onEnergyReport(String aPeerAddr, ChannelMask aChannelMask, ByteArray aEnergyList) {
150 Log.d(TAG, "received ENERGY SCAN report");
154 public void onDatasetChanged() {
155 Log.d(TAG, "Thread Network Dataset chanaged");