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