Add API description for javadoc and update nativeCreateRemoteEnrollee API
[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  * This provides an API to instanciate a new RemoteEnrollee object correspondent to Enrollee
38  * Device to be setup.
39  */
40 public class EasySetup {
41
42     private static final String TAG = EasySetup.class.getName();
43     public static final String PROV_RESOURCE_TYPE = "ocf.wk.prov";
44     private static EasySetup sInstance;
45
46     private static Context mContext;
47
48     private ArrayList<RemoteEnrollee> mRemoteEnrolleeList;
49
50     protected RemoteEnrollee mRemoteEnrollee;
51
52     // function to call the native nativeCreateRemoteEnrollee
53     private native RemoteEnrollee nativeCreateRemoteEnrollee(OcResource enrolleeResource);
54     static {
55         // Load Easy Setup JNI interface
56         try
57         {
58             System.loadLibrary("ocprovision");
59         } catch (UnsatisfiedLinkError e) {
60             Log.i(TAG, "ocprovision library does not exist. (Unsecure mode)");
61         }
62
63         System.loadLibrary("ocstack-jni");
64         System.loadLibrary("ESMediatorRich");
65         System.loadLibrary("easysetup-jni");
66     }
67
68     private EasySetup() {
69         mRemoteEnrolleeList = new ArrayList<RemoteEnrollee>();
70         mContext = null;
71     }
72
73     /**
74      * Gives a singleton instance of Easy setup and initialize the easy setup
75      */
76     public synchronized static EasySetup getInstance(Context context) {
77         if (sInstance == null) {
78             sInstance = new EasySetup();
79             mContext = context;
80         }
81         return sInstance;
82     }
83
84      /**
85      * This API is used for creating a remote Enrollee instance
86      *
87      * @param enrolleeResource an OCResource object corresponding to enrollee resource
88      *        discovered in a network. The OcResource object can be obtained by calling
89      *        OcPlatform.findResource() API. What resource you have to discover with
90      *        the OcPlatform.findResource() API is a "provisioning" resource with a certain
91      *        resource type, i.e. ocf.wk.prov
92      *
93      * @return Pointer to RemoteEnrollee instance
94      */
95     public synchronized RemoteEnrollee createRemoteEnrollee(OcResource enrolleeResource)
96     {
97         // native call
98         if(!enrolleeResource.getResourceTypes().contains(PROV_RESOURCE_TYPE)
99                 || !enrolleeResource.getResourceInterfaces().contains(OcPlatform.BATCH_INTERFACE))
100         {
101             Log.e(TAG, "Validation check for OcResource is failed.");
102             return null;
103         }
104         mRemoteEnrollee = nativeCreateRemoteEnrollee(enrolleeResource);
105         mRemoteEnrolleeList.add(mRemoteEnrollee);
106         return mRemoteEnrollee;
107     }
108 }