Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / base / OcSecureResource.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 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.base;
24
25 import java.util.List;
26 import java.util.EnumSet;
27 import java.util.Map;
28
29 public class OcSecureResource {
30
31     private OcSecureResource(long nativeHandle) {
32         this.mNativeHandle = nativeHandle;
33     }
34
35     /**
36      *  Method to Start Ownership Transfer of an un-owned device.
37      *
38      *  @param DoOwnershipTransferListener  Callback function, which will be called after
39      *                                      completion of ownership Transfer.
40      *  @throws OcException
41      */
42     public native void doOwnershipTransfer(DoOwnershipTransferListener doOwnershipTransferListener)
43         throws OcException;
44
45     /**
46      *  Method removes device credential from all devices in subnet
47      *
48      *  @param timeout
49      *  @param RemoveDeviceListener         Callback function, which will be called after
50      *                                      completion of removing device.
51      *  @throws OcException
52      */
53     public native void removeDevice(int timeout,  RemoveDeviceListener removeDeviceListener)
54         throws OcException;
55
56     /**
57      *  Method removes the credential & relationship between the two devices.
58      *
59      *  @param jobject                      Second device
60      *  @param UnlinkDevicesListener        Callback function, which will be called after
61      *                                      completion of removing device.
62      *  @throws OcException
63      */
64     public native void unlinkDevices(Object device2, UnlinkDevicesListener unlinkDevicesListener)
65         throws OcException;
66
67     /**
68      *  Method removes the credential & relationship between the two devices.
69      *
70      *  @param EnumSet<CredType>            OR'ed Cred Types
71      *  @param KeySize                          keySize
72      *  @param Object                       Second device
73      *  @param ProvisionCredentialsListener Callback function, which will be called after
74      *                                      completion of removing device.
75      *  @throws OcException
76      */
77     public void provisionCredentials(EnumSet<CredType> credTypeSet, KeySize keysize, Object device2,
78             ProvisionCredentialsListener provisionCredentialsListener) throws OcException {
79         int credTypeInt = 0;
80
81         for (CredType credType : CredType.values()) {
82             if (credTypeSet.contains(credType))
83                 credTypeInt |= credType.getValue();
84         }
85         this.provisionCredentials1(credTypeInt, keysize.getValue(),
86                 device2, provisionCredentialsListener);
87     }
88     private native void provisionCredentials1(int type, int keySize, Object device2,
89             ProvisionCredentialsListener provisionCredentialsListener)
90         throws OcException;
91
92     /**
93      *  Method send ACL information to resource.
94      *
95      *  @param jobject                      Acl
96      *  @param ProvisionAclListener         Callback function, which will be called after
97      *                                      completion of removing device.
98      *  @throws OcException
99      */
100     public native void provisionACL(Object acl, ProvisionAclListener provisionACLListener)
101         throws OcException;
102
103
104     /**
105      *  Method provisions credentials between two devices and ACLs for the devices who
106      *  act as a server.
107      *
108      *  @param EnumSet<CredType>            OR'ed Cred Types
109      *  @param KeySize                      keySize
110      *  @param Object                       First acl
111      *  @param Object                       Second device
112      *  @param Object                       Second acl
113      *  @param ProvisionPairwiseDevicesListener Callback function, which will be called after
114      *                                      completion of removing device.
115      *  @throws OcException
116      */
117     public void provisionPairwiseDevices(EnumSet<CredType> credTypeSet, KeySize keysize, Object acl1,
118             Object device2, Object acl2,
119             ProvisionPairwiseDevicesListener provisionPairwiseDevicesListener) throws OcException {
120         int credTypeInt = 0;
121
122         for (CredType credType : CredType.values()) {
123             if (credTypeSet.contains(credType))
124                 credTypeInt |= credType.getValue();
125         }
126         this.provisionPairwiseDevices1(credTypeInt, keysize.getValue(), acl1, device2,
127                 acl2, provisionPairwiseDevicesListener);
128     }
129     private native void provisionPairwiseDevices1(int type, int keySize, Object acl1,
130             Object device2, Object acl2,
131             ProvisionPairwiseDevicesListener provisionPairwiseDevicesListener) throws OcException;
132
133     /**
134      *  Method to configure resource for direct pairing
135      *
136      *  @param pin                      pin number
137      *  @param pdacls                   Array of Device Pairing Access Control List
138      *  @param type                     List of supported OcPrmType
139      *  @param edp                      enable (1) / disable (0)
140      *  @param ProvisionDirectPairing   Callback function, which will be called after completion
141      *                                  of Direct Pairing.
142      *  @throws OcException
143      */
144
145     public void doProvisionDirectPairing(String pin, OicSecPdAcl[] pdacls, List<OcPrmType> type,
146             boolean edp , ProvisionDirectPairingListener provisionDirectPairingListener)
147         throws OcException {
148
149             int[] typeArray = new int[type.size()];
150             int i = 0;
151             for (OcPrmType ocPrmType:type) {
152                 typeArray[i++] = ocPrmType.getValue();
153             }
154
155             this.provisionDirectPairing(pin, pdacls, typeArray, (edp?1:0),
156                     provisionDirectPairingListener);
157         }
158
159     private native void provisionDirectPairing(String pin, OicSecPdAcl[] pdacls, int[] type,
160             int edp , ProvisionDirectPairingListener provisionDirectPairingListener)
161         throws OcException;
162
163     /**
164      * provisionDirectPairingListener can be registered with doOwnershipTransfer
165      * call.
166      * Listener notified asynchronously.
167      */
168     public interface ProvisionDirectPairingListener {
169         public void provisionDirectPairingListener(List<ProvisionResult> provisionResultList,
170                 int hasError);
171     }
172
173     /**
174      * doOwnershipTransferListener can be registered with doOwnershipTransfer
175      * call.
176      * Listener notified asynchronously.
177      */
178     public interface DoOwnershipTransferListener {
179         public void doOwnershipTransferListener(List<ProvisionResult> provisionResultList,
180                 int hasError);
181     }
182
183     /**
184      * removeDeviceListener can be registered with removeDeviceListener
185      * call.
186      * Listener notified asynchronously.
187      */
188     public interface RemoveDeviceListener {
189         public void removeDeviceListener(List<ProvisionResult> provisionResultList,
190                 int hasError);
191     }
192
193     /**
194      * unlinkDevicesListener can be registered with unlinkDevicesListener
195      * call.
196      * Listener notified asynchronously.
197      */
198     public interface UnlinkDevicesListener {
199         public void unlinkDevicesListener(List<ProvisionResult> provisionResultList,
200                 int hasError);
201     }
202
203     /**
204      * provisionCredentialsListener can be registered with provisionCredentialsListener
205      * call.
206      * Listener notified asynchronously.
207      */
208     public interface ProvisionCredentialsListener {
209         public void provisionCredentialsListener(List<ProvisionResult> provisionResultList,
210                 int hasError);
211     }
212
213     /**
214      * provisionAclListener can be registered with provisionAclListener
215      * call.
216      * Listener notified asynchronously.
217      */
218     public interface ProvisionAclListener {
219         public void provisionAclListener(List<ProvisionResult> provisionResultList,
220                 int hasError);
221     }
222
223     /**
224      * provisionPairwiseDevicesListener can be registered with provisionPairwiseDevicesListener
225      * call.
226      * Listener notified asynchronously.
227      */
228     public interface ProvisionPairwiseDevicesListener {
229         public void provisionPairwiseDevicesListener(List<ProvisionResult> provisionResultList,
230                 int hasError);
231     }
232
233     /** Method to get List of device ID of devices linked with invoking device.
234      *
235      *  @return Sring List  List of device id's of linked devices.
236      */
237      public native List<String> getLinkedDevices();
238
239      /**
240       * Method to get IP address of sercure discovered device.
241       * @return Stringified IP address.
242       */
243     public native String getIpAddr();
244
245     /**
246      * Method to get device id of a device.
247      * @return  Device ID of the selected device.
248      */
249     public native String getDeviceID();
250
251     /**
252      * Method to get device status (ON/OFF) of a device.
253      * @return      ON/OFF
254      */
255
256     public DeviceStatus getDeviceStatus() throws OcException {
257         return DeviceStatus.convertDeviceStatus(this.deviceStatus());
258     }
259     public native int deviceStatus() throws OcException;
260
261     /**
262      * Method to get device  ownership (OWNED/UNOWNED) status.
263      * @return      OWNED/UNOWNED
264      */
265
266     public OwnedStatus getOwnedStatus() throws OcException {
267         return  OwnedStatus.convertOwnedStatus(this.ownedStatus());
268     }
269     public native int ownedStatus() throws OcException;
270
271     @Override
272     protected void finalize() throws Throwable {
273         super.finalize();
274         dispose();
275     }
276
277     private native void dispose();
278
279     private long mNativeHandle;
280 }