[Update] change Api desgin and modify function & data structure naming issue.
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / mediator / EasySetup.java
1 /**
2  * ***************************************************************
3  *
4  * Copyright 2017 Samsung Electronics All Rights Reserved.
5  *
6  *
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * ****************************************************************
21  */
22
23 package org.iotivity.service.easysetup.mediator;
24
25 import android.content.Context;
26 import android.util.Log;
27
28 import org.iotivity.base.OcConnectivityType;
29 import org.iotivity.base.OcResource;
30 import org.iotivity.base.OcPlatform;
31
32 import java.util.ArrayList;
33 import java.util.EnumSet;
34 import java.util.List;
35
36 /**
37  * <To be modified>
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
42  * enrolling device.
43  */
44 public class EasySetup {
45
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;
49
50     private static Context mContext;
51
52     private ArrayList<RemoteEnrollee> mRemoteEnrolleeList;
53
54     protected RemoteEnrollee mRemoteEnrollee;
55
56     //function to call the native nativeCreateRemoteEnrollee
57     private native RemoteEnrollee nativeCreateRemoteEnrollee(String Host, String Uri, String devID,
58                                                              Boolean isObservable, int conType);
59     static {
60         // Load Easy Setup JNI interface
61         System.loadLibrary("ESMediatorRich");
62         System.loadLibrary("easysetup-jni");
63     }
64
65     private EasySetup() {
66         mRemoteEnrolleeList = new ArrayList<RemoteEnrollee>();
67         mContext = null;
68     }
69
70     /**
71      * Gives a singleton instance of Easy setup and initialize the easy setup
72      */
73     public synchronized static EasySetup getInstance(Context context) {
74         if (sInstance == null) {
75             sInstance = new EasySetup();
76             mContext = context;
77         }
78         return sInstance;
79     }
80
81     /**
82      * API to create a new RemoteEnrollee instance
83      */
84     public synchronized RemoteEnrollee createRemoteEnrollee(OcResource enrolleeResource)
85     {
86         // native call
87         String Host = enrolleeResource.getHost();
88         String HostInIPv6;
89         int pos1 = Host.indexOf("%");   // it indicates the address is IPv6.
90         if(pos1 >= 0) {
91             int pos2 = Host.indexOf("]");
92             HostInIPv6 = Host.substring(0, pos1) + Host.substring(pos2, Host.length());
93             Host = HostInIPv6;
94             Log.d(TAG,"Host address of the resource(truncated): " + HostInIPv6);
95         }
96
97         String Uri = enrolleeResource.getUri();
98         String devID = enrolleeResource.getServerId();
99         Boolean isObservable = enrolleeResource.isObservable();
100         EnumSet<OcConnectivityType> ConType = enrolleeResource.getConnectivityTypeSet();
101         int conType = 0;
102         for (OcConnectivityType type : ConType)
103         {
104             conType |= type.getValue();
105         }
106         List<String> resourceTypes = enrolleeResource.getResourceTypes();
107         List<String> resourceInterfaces = enrolleeResource.getResourceInterfaces();
108
109         if(!resourceTypes.contains(PROV_RESOURCE_TYPE)
110                 || !resourceInterfaces.contains(OcPlatform.BATCH_INTERFACE))
111         {
112             Log.e(TAG, "Validation check for OcResource is failed.");
113             return null;
114         }
115         mRemoteEnrollee = nativeCreateRemoteEnrollee(Host, Uri, devID, isObservable, conType);
116         mRemoteEnrolleeList.add(mRemoteEnrollee);
117         return mRemoteEnrollee;
118     }
119
120     /**
121      * Reset the Easy setup
122      */
123     public void finish() {
124         //Call the stop Provisioning
125         //for (RemoteEnrollee remoteEnrollee : mRemoteEnrolleeList)
126         //    remoteEnrollee.stopProvisioningProcess();
127         }
128 }