Imported Upstream version 1.2.0
[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 to provision the Trust certificate chain to secured device.
94      *
95      *  @param EnumSet<CredType>            OR'ed Cred Types
96      *  @param int                          credId
97      *  @param ProvisionTrustCertChainListener Callback function, which will be called after
98      *                                      proviosion trust certificate chain.
99      *  @throws OcException
100      */
101     public void provisionTrustCertChain(EnumSet<CredType> credTypeSet, int credId,
102             ProvisionTrustCertChainListener provisionTrustCertChainListener) throws OcException {
103         int credTypeInt = 0;
104
105         for (CredType credType : CredType.values()) {
106             if (credTypeSet.contains(credType))
107                 credTypeInt |= credType.getValue();
108         }
109         this.provisionTrustCertChain1(credTypeInt, credId,
110                 provisionTrustCertChainListener);
111     }
112     private native void provisionTrustCertChain1(int credType, int credId,
113             ProvisionTrustCertChainListener provisionTrustCertChainListener)
114         throws OcException;
115
116     /**
117      *  Method send ACL information to resource.
118      *
119      *  @param jobject                      Acl
120      *  @param ProvisionAclListener         Callback function, which will be called after
121      *                                      completion of removing device.
122      *  @throws OcException
123      */
124     public native void provisionACL(Object acl, ProvisionAclListener provisionACLListener)
125         throws OcException;
126
127
128     /**
129      *  Method provisions credentials between two devices and ACLs for the devices who
130      *  act as a server.
131      *
132      *  @param EnumSet<CredType>            OR'ed Cred Types
133      *  @param KeySize                      keySize
134      *  @param Object                       First acl
135      *  @param Object                       Second device
136      *  @param Object                       Second acl
137      *  @param ProvisionPairwiseDevicesListener Callback function, which will be called after
138      *                                      completion of removing device.
139      *  @throws OcException
140      */
141     public void provisionPairwiseDevices(EnumSet<CredType> credTypeSet, KeySize keysize, Object acl1,
142             Object device2, Object acl2,
143             ProvisionPairwiseDevicesListener provisionPairwiseDevicesListener) throws OcException {
144         int credTypeInt = 0;
145
146         for (CredType credType : CredType.values()) {
147             if (credTypeSet.contains(credType))
148                 credTypeInt |= credType.getValue();
149         }
150         this.provisionPairwiseDevices1(credTypeInt, keysize.getValue(), acl1, device2,
151                 acl2, provisionPairwiseDevicesListener);
152     }
153     private native void provisionPairwiseDevices1(int type, int keySize, Object acl1,
154             Object device2, Object acl2,
155             ProvisionPairwiseDevicesListener provisionPairwiseDevicesListener) throws OcException;
156
157     /**
158      *  Method to configure resource for direct pairing
159      *
160      *  @param pin                      pin number
161      *  @param pdacls                   Array of Device Pairing Access Control List
162      *  @param type                     List of supported OcPrmType
163      *  @param edp                      enable (1) / disable (0)
164      *  @param ProvisionDirectPairing   Callback function, which will be called after completion
165      *                                  of Direct Pairing.
166      *  @throws OcException
167      */
168
169     public void doProvisionDirectPairing(String pin, OicSecPdAcl[] pdacls, List<OcPrmType> type,
170             boolean edp , ProvisionDirectPairingListener provisionDirectPairingListener)
171         throws OcException {
172
173             int[] typeArray = new int[type.size()];
174             int i = 0;
175             for (OcPrmType ocPrmType:type) {
176                 typeArray[i++] = ocPrmType.getValue();
177             }
178
179             this.provisionDirectPairing(pin, pdacls, typeArray, (edp?1:0),
180                     provisionDirectPairingListener);
181         }
182
183     private native void provisionDirectPairing(String pin, OicSecPdAcl[] pdacls, int[] type,
184             int edp , ProvisionDirectPairingListener provisionDirectPairingListener)
185         throws OcException;
186
187     /**
188      * provisionDirectPairingListener can be registered with doOwnershipTransfer
189      * call.
190      * Listener notified asynchronously.
191      */
192     public interface ProvisionDirectPairingListener {
193         public void provisionDirectPairingListener(List<ProvisionResult> provisionResultList,
194                 int hasError);
195     }
196
197     /**
198      * doOwnershipTransferListener can be registered with doOwnershipTransfer
199      * call.
200      * Listener notified asynchronously.
201      */
202     public interface DoOwnershipTransferListener {
203         public void doOwnershipTransferListener(List<ProvisionResult> provisionResultList,
204                 int hasError);
205     }
206
207     /**
208      * removeDeviceListener can be registered with removeDeviceListener
209      * call.
210      * Listener notified asynchronously.
211      */
212     public interface RemoveDeviceListener {
213         public void removeDeviceListener(List<ProvisionResult> provisionResultList,
214                 int hasError);
215     }
216
217     /**
218      * unlinkDevicesListener can be registered with unlinkDevicesListener
219      * call.
220      * Listener notified asynchronously.
221      */
222     public interface UnlinkDevicesListener {
223         public void unlinkDevicesListener(List<ProvisionResult> provisionResultList,
224                 int hasError);
225     }
226
227     /**
228      * provisionCredentialsListener can be registered with provisionCredentialsListener
229      * call.
230      * Listener notified asynchronously.
231      */
232     public interface ProvisionCredentialsListener {
233         public void provisionCredentialsListener(List<ProvisionResult> provisionResultList,
234                 int hasError);
235     }
236
237         /**
238      * provisionTrustCertChainListener can be registered with ProvisionTrustCertChainListener
239      * call.
240      * Listener notified asynchronously.
241      */
242     public interface ProvisionTrustCertChainListener {
243         public void provisionTrustCertChainListener(List<ProvisionResult> provisionResultList,
244                 int hasError);
245     }
246         
247     /**
248      * provisionAclListener can be registered with provisionAclListener
249      * call.
250      * Listener notified asynchronously.
251      */
252     public interface ProvisionAclListener {
253         public void provisionAclListener(List<ProvisionResult> provisionResultList,
254                 int hasError);
255     }
256
257     /**
258      * provisionPairwiseDevicesListener can be registered with provisionPairwiseDevicesListener
259      * call.
260      * Listener notified asynchronously.
261      */
262     public interface ProvisionPairwiseDevicesListener {
263         public void provisionPairwiseDevicesListener(List<ProvisionResult> provisionResultList,
264                 int hasError);
265     }
266
267     /** Method to get List of device ID of devices linked with invoking device.
268      *
269      *  @return Sring List  List of device id's of linked devices.
270      */
271      public native List<String> getLinkedDevices();
272
273      /**
274       * Method to get IP address of sercure discovered device.
275       * @return Stringified IP address.
276       */
277     public native String getIpAddr();
278
279     /**
280      * Method to get device id of a device.
281      * @return  Device ID of the selected device.
282      */
283     public native String getDeviceID();
284
285     /**
286      * Method to get device status (ON/OFF) of a device.
287      * @return      ON/OFF
288      */
289
290     public DeviceStatus getDeviceStatus() throws OcException {
291         return DeviceStatus.convertDeviceStatus(this.deviceStatus());
292     }
293     public native int deviceStatus() throws OcException;
294
295     /**
296      * Method to get device  ownership (OWNED/UNOWNED) status.
297      * @return      OWNED/UNOWNED
298      */
299
300     public OwnedStatus getOwnedStatus() throws OcException {
301         return  OwnedStatus.convertOwnedStatus(this.ownedStatus());
302     }
303     public native int ownedStatus() throws OcException;
304
305     @Override
306     protected void finalize() throws Throwable {
307         super.finalize();
308         dispose();
309     }
310
311     private native void dispose();
312
313     private long mNativeHandle;
314 }