2 * ***************************************************************
4 * Copyright 2017 Samsung Electronics All Rights Reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * ****************************************************************
23 package org.iotivity.service.easysetup.mediator;
25 import android.content.Context;
26 import android.util.Log;
28 import org.iotivity.base.OcConnectivityType;
29 import org.iotivity.base.OcResource;
30 import org.iotivity.base.OcPlatform;
32 import java.util.ArrayList;
33 import java.util.EnumSet;
34 import java.util.List;
38 * This is facade class, a single point of contact for Application.
39 * It contains set of APIs to do easy setup of the enrolling device.
40 * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
41 * PROVISION - This is a step where the netowork's detail & credentials are given to the
44 public class EasySetup {
46 private static final String TAG = EasySetup.class.getName();
47 public static final String PROV_RESOURCE_TYPE = "ocf.wk.prov";
48 private static EasySetup sInstance;
50 private static Context mContext;
52 private ArrayList<RemoteEnrollee> mRemoteEnrolleeList;
54 protected RemoteEnrollee mRemoteEnrollee;
56 //function to call the native nativeCreateRemoteEnrollee
57 private native RemoteEnrollee nativeCreateRemoteEnrollee(String Host, String Uri, String devID,
58 Boolean isObservable, int conType);
60 // Load Easy Setup JNI interface
61 System.loadLibrary("ESMediatorRich");
62 System.loadLibrary("easysetup-jni");
66 mRemoteEnrolleeList = new ArrayList<RemoteEnrollee>();
71 * Gives a singleton instance of Easy setup and initialize the easy setup
73 public synchronized static EasySetup getInstance(Context context) {
74 if (sInstance == null) {
75 sInstance = new EasySetup();
82 * API to create a new RemoteEnrollee instance
84 public synchronized RemoteEnrollee createRemoteEnrollee(OcResource enrolleeResource)
87 String Host = enrolleeResource.getHost();
89 int pos1 = Host.indexOf("%"); // it indicates the address is IPv6.
91 int pos2 = Host.indexOf("]");
92 HostInIPv6 = Host.substring(0, pos1) + Host.substring(pos2, Host.length());
94 Log.d(TAG,"Host address of the resource(truncated): " + HostInIPv6);
97 String Uri = enrolleeResource.getUri();
98 String devID = enrolleeResource.getServerId();
99 Boolean isObservable = enrolleeResource.isObservable();
100 EnumSet<OcConnectivityType> ConType = enrolleeResource.getConnectivityTypeSet();
102 for (OcConnectivityType type : ConType)
104 conType |= type.getValue();
106 List<String> resourceTypes = enrolleeResource.getResourceTypes();
107 List<String> resourceInterfaces = enrolleeResource.getResourceInterfaces();
109 if(!resourceTypes.contains(PROV_RESOURCE_TYPE)
110 || !resourceInterfaces.contains(OcPlatform.BATCH_INTERFACE))
112 Log.e(TAG, "Validation check for OcResource is failed.");
115 mRemoteEnrollee = nativeCreateRemoteEnrollee(Host, Uri, devID, isObservable, conType);
116 mRemoteEnrolleeList.add(mRemoteEnrollee);
117 return mRemoteEnrollee;
121 * Reset the Easy setup
123 public void finish() {
124 //Call the stop Provisioning
125 //for (RemoteEnrollee remoteEnrollee : mRemoteEnrolleeList)
126 // remoteEnrollee.stopProvisioningProcess();