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.content.Context;
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import com.google.chip.chiptool.commissioner.thread.BorderAgentInfo;
25 import com.google.chip.chiptool.commissioner.thread.ThreadCommissionerException;
26 import com.google.chip.chiptool.commissioner.thread.ThreadCommissionerService;
27 import com.google.chip.chiptool.commissioner.thread.ThreadNetworkCredential;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.CompletionException;
31 public class ThreadCommissionerServiceImpl implements ThreadCommissionerService {
33 private static final String TAG = ThreadCommissionerException.class.getSimpleName();
35 private BorderAgentDatabase borderAgentDatabase;
37 public ThreadCommissionerServiceImpl(@NonNull Context context) {
38 this.borderAgentDatabase = BorderAgentDatabase.getDatabase(context);
42 public CompletableFuture<ThreadNetworkCredential> FetchThreadNetworkCredential(
43 @NonNull BorderAgentInfo borderAgentInfo, @Nullable byte[] pskc) {
44 return getThreadNetworkCredential(borderAgentInfo)
47 if (credential != null) {
51 NetworkCredentialFetcher credentialFetcher = new NetworkCredentialFetcher();
53 credential = credentialFetcher.fetchNetworkCredential(borderAgentInfo, pskc);
54 addThreadNetworkCredential(borderAgentInfo, pskc, credential).get();
56 } catch (Exception ex) {
57 throw new CompletionException(ex);
63 public CompletableFuture<ThreadNetworkCredential> getThreadNetworkCredential(
64 @NonNull BorderAgentInfo borderAgentInfo) {
65 return getBorderAgentRecord(borderAgentInfo)
67 borderAgentRecord -> {
68 if (borderAgentRecord == null) {
71 return new ThreadNetworkCredential(borderAgentRecord.getActiveOperationalDataset());
75 CompletableFuture<BorderAgentRecord> getBorderAgentRecord(
76 @NonNull BorderAgentInfo borderAgentInfo) {
77 return borderAgentDatabase.getBorderAgent(borderAgentInfo.discriminator);
81 public CompletableFuture<Void> deleteThreadNetworkCredential(
82 @NonNull BorderAgentInfo borderAgentInfo) {
83 return borderAgentDatabase.deleteBorderAgent(borderAgentInfo.discriminator);
86 // This method adds given Thread Network Credential into database on the phone.
87 CompletableFuture<Void> addThreadNetworkCredential(
88 @NonNull BorderAgentInfo borderAgentInfo,
90 @NonNull ThreadNetworkCredential networkCredential) {
91 BorderAgentRecord borderAgentRecord =
92 new BorderAgentRecord(
93 borderAgentInfo.discriminator,
94 borderAgentInfo.networkName,
95 borderAgentInfo.extendedPanId,
97 networkCredential.getEncoded());
98 return borderAgentDatabase.insertBorderAgent(borderAgentRecord);