Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / android / CHIPTool / app / src / main / java / com / google / chip / chiptool / commissioner / thread / internal / NetworkCredentialFetcher.java
1 /*
2  *   Copyright (c) 2020 Project CHIP Authors
3  *   All rights reserved.
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 package com.google.chip.chiptool.commissioner.thread.internal;
20
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;
37
38 class NetworkCredentialFetcher {
39
40   private static final String TAG = NetworkCredentialFetcher.class.getSimpleName();
41
42   NativeCommissionerHandler nativeCommissionerHandler = new NativeCommissionerHandler();
43   Commissioner nativeCommissioner;
44
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);
51   }
52
53   public void cancel() {
54     if (nativeCommissioner != null) {
55       Log.d(TAG, "cancel requesting credential");
56       nativeCommissioner.cancelRequests();
57     }
58   }
59
60   private byte[] fetchNetworkCredential(
61       @NonNull InetAddress address, int port, @NonNull byte[] pskc)
62       throws ThreadCommissionerException {
63     nativeCommissioner = Commissioner.create(nativeCommissionerHandler);
64
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());
72
73     try {
74       // Initialize the native commissioner
75       throwIfFail(nativeCommissioner.init(config));
76
77       // Petition to be the active commissioner in the Thread Network.
78       String[] existingCommissionerId = new String[1];
79       throwIfFail(
80           nativeCommissioner.petition(existingCommissionerId, address.getHostAddress(), port));
81
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;
91       throw e;
92     }
93   }
94
95   private void throwIfFail(Error error) throws ThreadCommissionerException {
96     if (error.getCode() != ErrorCode.kNone) {
97       throw new ThreadCommissionerException(error.getCode().swigValue(), error.getMessage());
98     }
99   }
100 }
101
102 class NativeCommissionerLogger extends Logger {
103   private static final String TAG = "NativeCommissioner";
104
105   @Override
106   public void log(LogLevel level, String region, String msg) {
107     Log.d(TAG, String.format("[ %s ]: %s", region, msg));
108   }
109 }
110
111 class NativeCommissionerHandler extends CommissionerHandler {
112   private static final String TAG = NativeCommissionerHandler.class.getSimpleName();
113
114   @Override
115   public String onJoinerRequest(ByteArray joinerId) {
116     Log.d(TAG, "A joiner is requesting commissioning");
117     return "";
118   }
119
120   @Override
121   public void onJoinerConnected(ByteArray joinerId, Error error) {
122     Log.d(TAG, "A joiner is connected");
123   }
124
125   @Override
126   public boolean onJoinerFinalize(
127       ByteArray joinerId,
128       String vendorName,
129       String vendorModel,
130       String vendorSwVersion,
131       ByteArray vendorStackVersion,
132       String provisioningUrl,
133       ByteArray vendorData) {
134     Log.d(TAG, "A joiner is finalizing");
135     return true;
136   }
137
138   @Override
139   public void onKeepAliveResponse(Error error) {
140     Log.d(TAG, "received keep-alive response: " + error.toString());
141   }
142
143   @Override
144   public void onPanIdConflict(String peerAddr, ChannelMask channelMask, int panId) {
145     Log.d(TAG, "received PAN ID CONFLICT report");
146   }
147
148   @Override
149   public void onEnergyReport(String aPeerAddr, ChannelMask aChannelMask, ByteArray aEnergyList) {
150     Log.d(TAG, "received ENERGY SCAN report");
151   }
152
153   @Override
154   public void onDatasetChanged() {
155     Log.d(TAG, "Thread Network Dataset chanaged");
156   }
157 }