replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / base / OcPlatform.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2015 Intel Corporation.
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 org.iotivity.ca.CaInterface;
26 import org.iotivity.base.BuildConfig;
27
28 import java.util.EnumSet;
29 import java.util.Iterator;
30 import java.util.List;
31
32 /**
33  * This class contains the main entrance/functionality of the product. To set a custom
34  * configuration, the implementer must make a call to OcPlatform.Configure before the first usage
35  * of a method in this class.
36  */
37 public final class OcPlatform {
38
39     static {
40         System.loadLibrary("oc_logger");
41         System.loadLibrary("octbstack");
42         System.loadLibrary("connectivity_abstraction");
43         System.loadLibrary("oc");
44         if (0 != BuildConfig.SECURED)
45         {
46             System.loadLibrary("ocprovision");
47         }
48         System.loadLibrary("ocstack-jni");
49     }
50
51     /**
52      * Default interface
53      */
54     public static final String DEFAULT_INTERFACE = "oic.if.baseline";
55
56     /**
57      * Used in discovering (GET) links to other resources of a collection
58      */
59     public static final String LINK_INTERFACE = "oic.if.ll";
60
61     /**
62      * Used in GET, PUT, POST, DELETE methods on links to other resources of a collection
63      */
64     public static final String BATCH_INTERFACE = "oic.if.b";
65
66     /**
67      * Used in GET, PUT, POST methods on links to other remote resources of a group
68      */
69     public static final String GROUP_INTERFACE = "oic.mi.grp";
70
71     public static final String WELL_KNOWN_QUERY = "/oic/res";
72     public static final String WELL_KNOWN_DEVICE_QUERY = "/oic/d";
73     public static final String WELL_KNOWN_PLATFORM_QUERY = "/oic/p";
74     public static final int DEFAULT_PRESENCE_TTL = 60;
75     public static final String PRESENCE_URI = "/oic/ad";
76
77     private static volatile boolean sIsPlatformInitialized = false;
78     private static QualityOfService sPlatformQualityOfService = QualityOfService.NA;
79
80     private static volatile boolean sIsStopPlatform = false;
81
82     private OcPlatform() {
83     }
84
85     /**
86      * API for setting the configuration of the OcPlatform.
87      * <p>
88      * Note: Any calls made to this AFTER the first call to OcPlatform.Configure will have no affect
89      * </p>
90      *
91      * @param platformConfig platform configuration
92      */
93     public synchronized static void Configure(PlatformConfig platformConfig) {
94         if (sIsStopPlatform)
95         {
96             OcPlatform.start();
97             sIsStopPlatform = false;
98         }
99
100         if (!sIsPlatformInitialized) {
101             CaInterface.initialize(platformConfig.getActivity(), platformConfig.getContext());
102
103             sPlatformQualityOfService = platformConfig.getQualityOfService();
104
105             OcPlatform.configure(
106                     platformConfig.getServiceType().getValue(),
107                     platformConfig.getModeType().getValue(),
108                     platformConfig.getIpAddress(),
109                     platformConfig.getPort(),
110                     platformConfig.getQualityOfService().getValue(),
111                     platformConfig.getSvrDbPath(),
112                     platformConfig.getDBDefaultPath(),
113                     platformConfig.getDBRescuePath(),
114                     platformConfig.getKeySize(),
115                     platformConfig.getKey(),
116                     platformConfig.getAvailableTransportType()
117             );
118
119             sIsPlatformInitialized = true;
120         }
121     }
122
123     private static native void configure(int serviceType,
124                                          int modeType,
125                                          String ipAddress,
126                                          int port,
127                                          int qualityOfService,
128                                          String dbPath,
129                                          String dbPathDefault,
130                                          String dbRescuePath,
131                                          int keySize,
132                                          byte[] key,
133                                          int transport);
134
135     /**
136      * API for stop all process of the OcPlatform.
137      * All of threads and memory will be terminated by this API.
138      * Iotivity Core can be started again through Configure(PlatformConfig platformConfig) API.
139      * Both Configure and Shutdown API is filtering for duplicated calling even while processing.
140      * <p>
141      * Note: This API is for both server and client side.
142      * </p>
143      */
144     public synchronized static void Shutdown() {
145         if (!sIsStopPlatform)
146         {
147             OcPlatform.stop();
148             sIsStopPlatform = true;
149             sIsPlatformInitialized = false;
150         }
151     }
152
153     private static native void stop();
154     private static native void start();
155
156     /**
157      * API for notifying base that resource's attributes have changed.
158      * <p>
159      * Note: This API is for server side only.
160      * </p>
161      *
162      * @param ocResourceHandle resource handle of the resource
163      * @throws OcException if failure
164      */
165     public static void notifyAllObservers(
166             OcResourceHandle ocResourceHandle) throws OcException {
167         OcPlatform.initCheck();
168         OcPlatform.notifyAllObservers0(ocResourceHandle);
169     }
170
171     private static native void notifyAllObservers0(
172             OcResourceHandle ocResourceHandle) throws OcException;
173
174     /**
175      * API for notifying base that resource's attributes have changed.
176      * <p>
177      * Note: This API is for server side only.
178      * </p>
179      *
180      * @param ocResourceHandle resource handle of the resource
181      * @param qualityOfService the quality of communication
182      * @throws OcException if failure
183      */
184     public static void notifyAllObservers(
185             OcResourceHandle ocResourceHandle,
186             QualityOfService qualityOfService) throws OcException {
187         OcPlatform.initCheck();
188         OcPlatform.notifyAllObservers1(ocResourceHandle, qualityOfService.getValue());
189     }
190
191     private static native void notifyAllObservers1(
192             OcResourceHandle ocResourceHandle,
193             int qualityOfService) throws OcException;
194
195     /**
196      * API for notifying only specific clients that resource's attributes have changed.
197      * <p>
198      * Note: This API is for server side only.
199      * </p>
200      *
201      * @param ocResourceHandle    resource handle of the resource
202      * @param ocObservationIdList These set of ids are ones which which will be notified upon
203      *                            resource change.
204      * @param ocResourceResponse  OcResourceResponse object used by app to fill the response for
205      *                            this resource change
206      * @throws OcException if failure
207      */
208     public static void notifyListOfObservers(
209             OcResourceHandle ocResourceHandle,
210             List<Byte> ocObservationIdList,
211             OcResourceResponse ocResourceResponse) throws OcException {
212         OcPlatform.initCheck();
213
214         if (ocObservationIdList == null) {
215             throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
216         }
217
218         byte[] idArr = new byte[ocObservationIdList.size()];
219         Iterator<Byte> it = ocObservationIdList.iterator();
220         int i = 0;
221         while (it.hasNext()) {
222             idArr[i++] = (byte) it.next();
223         }
224
225         OcPlatform.notifyListOfObservers2(
226                 ocResourceHandle,
227                 idArr,
228                 ocResourceResponse);
229     }
230
231     private static native void notifyListOfObservers2(
232             OcResourceHandle ocResourceHandle,
233             byte[] ocObservationIdArray,
234             OcResourceResponse ocResourceResponse) throws OcException;
235
236     /**
237      * API for notifying only specific clients that resource's attributes have changed.
238      * <p>
239      * Note: This API is for server side only.
240      * </p>
241      *
242      * @param ocResourceHandle    resource handle of the resource
243      * @param ocObservationIdList These set of ids are ones which which will be notified upon
244      *                            resource change.
245      * @param ocResourceResponse  OcResourceResponse object used by app to fill the response for
246      *                            this resource change
247      * @param qualityOfService    the quality of communication
248      * @throws OcException if failure
249      */
250     public static void notifyListOfObservers(
251             OcResourceHandle ocResourceHandle,
252             List<Byte> ocObservationIdList,
253             OcResourceResponse ocResourceResponse,
254             QualityOfService qualityOfService) throws OcException {
255         OcPlatform.initCheck();
256
257         if (ocObservationIdList == null) {
258             throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
259         }
260
261         byte[] idArr = new byte[ocObservationIdList.size()];
262         Iterator<Byte> it = ocObservationIdList.iterator();
263         int i = 0;
264         while (it.hasNext()) {
265             idArr[i++] = (byte) it.next();
266         }
267
268         OcPlatform.notifyListOfObservers3(
269                 ocResourceHandle,
270                 idArr,
271                 ocResourceResponse,
272                 qualityOfService.getValue()
273         );
274     }
275
276     private static native void notifyListOfObservers3(
277             OcResourceHandle ocResourceHandle,
278             byte[] ocObservationIdArray,
279             OcResourceResponse ocResourceResponse,
280             int qualityOfService) throws OcException;
281
282     /**
283      * API for Service and Resource Discovery
284      * <p>
285      * Note: This API is for client side only.
286      * </p>
287      *
288      * @param host                    Host Address of a service to direct resource discovery query.
289      *                                If empty, performs multicast resource discovery query
290      * @param resourceUri             name of the resource. If null or empty, performs search for all
291      *                                resource names
292      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
293      * @param onResourceFoundListener Handles events, success states and failure states.
294      * @throws OcException if failure
295      */
296     public static void findResource(
297             String host,
298             String resourceUri,
299             EnumSet<OcConnectivityType> connectivityTypeSet,
300             OnResourceFoundListener onResourceFoundListener) throws OcException {
301         OcPlatform.initCheck();
302
303         int connTypeInt = 0;
304
305         for (OcConnectivityType connType : OcConnectivityType.values()) {
306             if (connectivityTypeSet.contains(connType))
307                 connTypeInt |= connType.getValue();
308         }
309
310         OcPlatform.findResource0(
311                 host,
312                 resourceUri,
313                 connTypeInt,
314                 onResourceFoundListener
315         );
316     }
317
318     private static native void findResource0(
319             String host,
320             String resourceUri,
321             int connectivityType,
322             OnResourceFoundListener onResourceFoundListener) throws OcException;
323
324     /**
325      * API for Service and Resource Discovery.
326      * <p>
327      * Note: This API is for client side only.
328      * </p>
329      *
330      * @param host                    Host IP Address of a service to direct resource discovery query.
331      *                                If empty, performs multicast resource discovery query
332      * @param resourceUri             name of the resource. If null or empty, performs search for all
333      *                                resource names
334      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
335      * @param onResourceFoundListener Handles events, success states and failure states.
336      * @param qualityOfService        the quality of communication
337      * @throws OcException if failure
338      */
339     public static void findResource(
340             String host,
341             String resourceUri,
342             EnumSet<OcConnectivityType> connectivityTypeSet,
343             OnResourceFoundListener onResourceFoundListener,
344             QualityOfService qualityOfService) throws OcException {
345         OcPlatform.initCheck();
346
347         int connTypeInt = 0;
348
349         for (OcConnectivityType connType : OcConnectivityType.values()) {
350             if (connectivityTypeSet.contains(connType))
351                 connTypeInt |= connType.getValue();
352         }
353
354         OcPlatform.findResource1(host,
355                 resourceUri,
356                 connTypeInt,
357                 onResourceFoundListener,
358                 qualityOfService.getValue()
359         );
360     }
361
362     private static native void findResource1(
363             String host,
364             String resourceUri,
365             int connectivityType,
366             OnResourceFoundListener onResourceFoundListener,
367             int qualityOfService) throws OcException;
368
369     /**
370      * API for Device Discovery
371      *
372      * @param host                  Host IP Address. If null or empty, Multicast is performed.
373      * @param deviceUri             Uri containing address to the virtual device
374      * @param connectivityTypeSet   Set of types of connectivity. Example: IP
375      * @param onDeviceFoundListener Handles events, success states and failure states.
376      * @throws OcException if failure
377      */
378     public static void getDeviceInfo(
379             String host,
380             String deviceUri,
381             EnumSet<OcConnectivityType> connectivityTypeSet,
382             OnDeviceFoundListener onDeviceFoundListener) throws OcException {
383         OcPlatform.initCheck();
384         int connTypeInt = 0;
385
386         for (OcConnectivityType connType : OcConnectivityType.values()) {
387             if (connectivityTypeSet.contains(connType))
388                 connTypeInt |= connType.getValue();
389         }
390         OcPlatform.getDeviceInfo0(
391                 host,
392                 deviceUri,
393                 connTypeInt,
394                 onDeviceFoundListener
395         );
396     }
397
398     private static native void getDeviceInfo0(
399             String host,
400             String deviceUri,
401             int connectivityType,
402             OnDeviceFoundListener onDeviceFoundListener) throws OcException;
403
404     /**
405      * API for Device Discovery
406      *
407      * @param host                  Host IP Address. If null or empty, Multicast is performed.
408      * @param deviceUri             Uri containing address to the virtual device
409      * @param connectivityTypeSet   Set of types of connectivity. Example: IP
410      * @param onDeviceFoundListener Handles events, success states and failure states.
411      * @param qualityOfService      the quality of communication
412      * @throws OcException if failure
413      */
414     public static void getDeviceInfo(
415             String host,
416             String deviceUri,
417             EnumSet<OcConnectivityType> connectivityTypeSet,
418             OnDeviceFoundListener onDeviceFoundListener,
419             QualityOfService qualityOfService) throws OcException {
420         OcPlatform.initCheck();
421         int connTypeInt = 0;
422
423         for (OcConnectivityType connType : OcConnectivityType.values()) {
424             if (connectivityTypeSet.contains(connType))
425                 connTypeInt |= connType.getValue();
426         }
427         OcPlatform.getDeviceInfo1(
428                 host,
429                 deviceUri,
430                 connTypeInt,
431                 onDeviceFoundListener,
432                 qualityOfService.getValue()
433         );
434     }
435
436     private static native void getDeviceInfo1(
437             String host,
438             String deviceUri,
439             int connectivityType,
440             OnDeviceFoundListener onDeviceFoundListener,
441             int qualityOfService) throws OcException;
442
443     /**
444      * API for Platform Discovery
445      *
446      * @param host                    Host IP Address. If null or empty, Multicast is performed.
447      * @param platformUri             Uri containing address to the platform
448      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
449      * @param onPlatformFoundListener Handles events, success states and failure states.
450      * @throws OcException if failure
451      */
452
453     public static void getPlatformInfo(
454             String host,
455             String platformUri,
456             EnumSet<OcConnectivityType> connectivityTypeSet,
457             OnPlatformFoundListener onPlatformFoundListener) throws OcException {
458         OcPlatform.initCheck();
459         int connTypeInt = 0;
460
461         for (OcConnectivityType connType : OcConnectivityType.values()) {
462             if (connectivityTypeSet.contains(connType))
463                 connTypeInt |= connType.getValue();
464         }
465         OcPlatform.getPlatformInfo0(
466                 host,
467                 platformUri,
468                 connTypeInt,
469                 onPlatformFoundListener
470         );
471     }
472
473     private static native void getPlatformInfo0(
474             String host,
475             String platformUri,
476             int connectivityType,
477             OnPlatformFoundListener onPlatformInfoFoundListener) throws OcException;
478
479     /**
480      * API for Platform Discovery
481      *
482      * @param host                    Host IP Address. If null or empty, Multicast is performed.
483      * @param platformUri             Uri containing address to the platform
484      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
485      * @param onPlatformFoundListener Handles events, success states and failure states.
486      * @param qualityOfService        the quality of communication
487      * @throws OcException if failure
488      */
489
490     public static void getPlatformInfo(
491             String host,
492             String platformUri,
493             EnumSet<OcConnectivityType> connectivityTypeSet,
494             OnPlatformFoundListener onPlatformFoundListener,
495             QualityOfService qualityOfService) throws OcException {
496         OcPlatform.initCheck();
497         int connTypeInt = 0;
498
499         for (OcConnectivityType connType : OcConnectivityType.values()) {
500             if (connectivityTypeSet.contains(connType))
501                 connTypeInt |= connType.getValue();
502         }
503         OcPlatform.getPlatformInfo1(
504                 host,
505                 platformUri,
506                 connTypeInt,
507                 onPlatformFoundListener,
508                 qualityOfService.getValue()
509         );
510     }
511
512     private static native void getPlatformInfo1(
513             String host,
514             String platformUri,
515             int connectivityType,
516             OnPlatformFoundListener onPlatformFoundListener,
517             int qualityOfService) throws OcException;
518
519     /**
520      * This API registers a resource with the server
521      * <p/>
522      * Note: This API applies to server & client side.
523      * </P>
524      *
525      * @param ocResource The instance of OcResource with all data filled
526      * @return resource handle
527      * @throws OcException if failure
528      */
529     public static OcResourceHandle registerResource(
530             OcResource ocResource) throws OcException {
531         OcPlatform.initCheck();
532         return OcPlatform.registerResource0(ocResource);
533     }
534
535     private static native OcResourceHandle registerResource0(
536             OcResource ocResource) throws OcException;
537
538     /**
539      * This API registers a resource with the server NOTE: This API applies to server side only.
540      * <p/>
541      * Note: This API applies to server side only.
542      * </P>
543      *
544      * @param resourceUri         The URI of the resource. Example: "a/light"
545      * @param resourceTypeName    The resource type. Example: "light"
546      * @param resourceInterface   The resource interface (whether it is collection etc).
547      * @param entityHandler       entity handler.
548      * @param resourcePropertySet indicates the property of the resource
549      * @return resource handle
550      * @throws OcException if failure
551      */
552     public static OcResourceHandle registerResource(
553             String resourceUri,
554             String resourceTypeName,
555             String resourceInterface,
556             EntityHandler entityHandler,
557             EnumSet<ResourceProperty> resourcePropertySet) throws OcException {
558         OcPlatform.initCheck();
559
560         int resProperty = 0;
561
562         for (ResourceProperty prop : ResourceProperty.values()) {
563             if (resourcePropertySet.contains(prop))
564                 resProperty |= prop.getValue();
565         }
566
567         return OcPlatform.registerResource1(resourceUri,
568                 resourceTypeName,
569                 resourceInterface,
570                 entityHandler,
571                 resProperty);
572     }
573
574     private static native OcResourceHandle registerResource1(
575             String resourceUri,
576             String resourceTypeName,
577             String resourceInterface,
578             EntityHandler entityHandler,
579             int resourceProperty) throws OcException;
580
581     /**
582      * Register Device Info
583      *
584      * @param ocDeviceInfo object containing all the device specific information
585      * @throws OcException if failure
586      */
587     public static void registerDeviceInfo(
588             OcDeviceInfo ocDeviceInfo) throws OcException {
589         OcPlatform.initCheck();
590         OcPlatform.registerDeviceInfo0(
591                 ocDeviceInfo.getDeviceName(),
592                 ocDeviceInfo.getDeviceTypes().toArray(
593                         new String[ocDeviceInfo.getDeviceTypes().size()]
594                 )
595         );
596     }
597
598     private static native void registerDeviceInfo0(
599             String deviceName,
600             String[] deviceTypes
601     ) throws OcException;
602
603     /**
604      * Set param Info
605      *
606      * @param ocDeviceInfo object containing all the device specific information
607      * @throws OcException if failure
608      */
609     public static void setPropertyValue(
610             int path, String propName, String propValue) throws OcException {
611         OcPlatform.initCheck();
612         OcPlatform.setPropertyValue1(path, propName, propValue);
613     }
614
615     public static void setPropertyValue(
616             int path, String propName, List<String> propValue) throws OcException {
617         OcPlatform.initCheck();
618         OcPlatform.setPropertyValue0(path, propName, propValue.toArray(new String[propValue.size()]));
619     }
620
621     public static void getPropertyValue(
622             int path, String propName, String propValue) throws OcException {
623         OcPlatform.initCheck();
624         OcPlatform.getPropertyValue0(path, propName, propValue);
625     }
626
627     private static native void setPropertyValue1(
628             int path,
629             String propName,
630             String propValue
631     ) throws OcException;
632
633
634     private static native void setPropertyValue0(
635             int path,
636             String propName,
637             String[] propValue
638     ) throws OcException;
639
640     private static native void getPropertyValue0(
641             int path,
642             String propName,
643             String propValue
644     ) throws OcException;
645
646     /**
647      * Register Platform Info
648      *
649      * @param ocPlatformInfo object containing all the platform specific information
650      * @throws OcException if failure
651      */
652     public static void registerPlatformInfo(
653             OcPlatformInfo ocPlatformInfo) throws OcException {
654         OcPlatform.initCheck();
655         OcPlatform.registerPlatformInfo0(
656                 ocPlatformInfo.getPlatformId(),
657                 ocPlatformInfo.getManufacturerName(),
658                 ocPlatformInfo.getManufacturerUrl(),
659                 ocPlatformInfo.getModelNumber(),
660                 ocPlatformInfo.getDateOfManufacture(),
661                 ocPlatformInfo.getPlatformVersion(),
662                 ocPlatformInfo.getOperatingSystemVersion(),
663                 ocPlatformInfo.getHardwareVersion(),
664                 ocPlatformInfo.getFirmwareVersion(),
665                 ocPlatformInfo.getSupportUrl(),
666                 ocPlatformInfo.getSystemTime()
667         );
668     }
669
670     private static native void registerPlatformInfo0(
671             String platformId, String manufacturerName, String manufacturerUrl,
672             String modelNumber, String dateOfManufacture, String platformVersion,
673             String operatingSystemVersion, String hardwareVersion, String firmwareVersion,
674             String supportUrl, String systemTime
675     ) throws OcException;
676
677     /**
678      * This API unregisters a resource with the server NOTE: This API applies to server side only.
679      *
680      * @param ocResourceHandle This is the resource handle which we which to unregister from the
681      *                         server
682      * @throws OcException if failure
683      */
684     public static void unregisterResource(
685             OcResourceHandle ocResourceHandle) throws OcException {
686         OcPlatform.initCheck();
687         OcPlatform.unregisterResource0(ocResourceHandle);
688     }
689
690     private static native void unregisterResource0(
691             OcResourceHandle ocResourceHandle) throws OcException;
692
693
694     /**
695      * Add a resource to a collection resource
696      *
697      * @param ocResourceCollectionHandle handle to the collection resource
698      * @param ocResourceHandle           handle to resource to be added to the collection resource
699      * @throws OcException if failure
700      */
701     public static void bindResource(
702             OcResourceHandle ocResourceCollectionHandle,
703             OcResourceHandle ocResourceHandle) throws OcException {
704         OcPlatform.initCheck();
705         OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle);
706     }
707
708     private static native void bindResource0(
709             OcResourceHandle ocResourceCollectionHandle,
710             OcResourceHandle ocResourceHandle) throws OcException;
711
712     /**
713      * Add multiple resources to a collection resource.
714      *
715      * @param ocResourceCollectionHandle handle to the collection resource
716      * @param ocResourceHandleList       reference to list of resource handles to be added to the
717      *                                   collection resource
718      * @throws OcException if failure
719      */
720     public static void bindResources(
721             OcResourceHandle ocResourceCollectionHandle,
722             List<OcResourceHandle> ocResourceHandleList) throws OcException {
723         OcPlatform.initCheck();
724
725         if (ocResourceHandleList == null) {
726             throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
727         }
728
729         OcPlatform.bindResources0(
730                 ocResourceCollectionHandle,
731                 ocResourceHandleList.toArray(
732                         new OcResourceHandle[ocResourceHandleList.size()])
733         );
734     }
735
736     private static native void bindResources0(
737             OcResourceHandle ocResourceCollectionHandle,
738             OcResourceHandle[] ocResourceHandleArray) throws OcException;
739
740     /**
741      * Unbind a resource from a collection resource.
742      *
743      * @param ocResourceCollectionHandle handle to the collection resource
744      * @param ocResourceHandle           resource handle to be unbound from the collection resource
745      * @throws OcException if failure
746      */
747     public static void unbindResource(
748             OcResourceHandle ocResourceCollectionHandle,
749             OcResourceHandle ocResourceHandle) throws OcException {
750         OcPlatform.initCheck();
751         OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle);
752     }
753
754     private static native void unbindResource0(
755             OcResourceHandle ocResourceCollectionHandle,
756             OcResourceHandle ocResourceHandle) throws OcException;
757
758     /**
759      * Unbind resources from a collection resource.
760      *
761      * @param ocResourceCollectionHandle Handle to the collection resource
762      * @param ocResourceHandleList       List of resource handles to be unbound from the collection
763      *                                   resource
764      * @throws OcException if failure
765      */
766     public static void unbindResources(
767             OcResourceHandle ocResourceCollectionHandle,
768             List<OcResourceHandle> ocResourceHandleList) throws OcException {
769         OcPlatform.initCheck();
770
771         if (ocResourceHandleList == null) {
772             throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
773         }
774
775         OcPlatform.unbindResources0(
776                 ocResourceCollectionHandle,
777                 ocResourceHandleList.toArray(
778                         new OcResourceHandle[ocResourceHandleList.size()])
779         );
780     }
781
782     private static native void unbindResources0(
783             OcResourceHandle ocResourceCollectionHandle,
784             OcResourceHandle[] ocResourceHandleArray) throws OcException;
785
786     /**
787      * Binds a type to a particular resource
788      *
789      * @param ocResourceHandle handle to the resource
790      * @param resourceTypeName new typename to bind to the resource
791      * @throws OcException if failure
792      */
793     public static void bindTypeToResource(
794             OcResourceHandle ocResourceHandle,
795             String resourceTypeName) throws OcException {
796         OcPlatform.initCheck();
797         OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName);
798     }
799
800     private static native void bindTypeToResource0(
801             OcResourceHandle ocResourceHandle,
802             String resourceTypeName) throws OcException;
803
804     /**
805      * Binds an interface to a particular resource
806      *
807      * @param ocResourceHandle      handle to the resource
808      * @param resourceInterfaceName new interface to bind to the resource
809      * @throws OcException if failure
810      */
811     public static void bindInterfaceToResource(
812             OcResourceHandle ocResourceHandle,
813             String resourceInterfaceName) throws OcException {
814         OcPlatform.initCheck();
815         OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName);
816     }
817
818     private static native void bindInterfaceToResource0(
819             OcResourceHandle ocResourceHandle,
820             String resourceInterfaceName) throws OcException;
821
822     /**
823      * Start Presence announcements.
824      *
825      * @param ttl time to live in seconds
826      * @throws OcException if failure
827      */
828     public static void startPresence(int ttl) throws OcException {
829         OcPlatform.initCheck();
830         OcPlatform.startPresence0(ttl);
831     }
832
833     private static native void startPresence0(int ttl) throws OcException;
834
835     /**
836      * Stop Presence announcements.
837      *
838      * @throws OcException if failure
839      */
840     public static void stopPresence() throws OcException {
841         OcPlatform.initCheck();
842         OcPlatform.stopPresence0();
843     }
844
845     private static native void stopPresence0() throws OcException;
846
847     /**
848      * Subscribes to a server's presence change events. By making this subscription, every time a
849      * server adds/removes/alters a resource, starts or is intentionally stopped
850      *
851      * @param host                The IP address/addressable name of the server to subscribe to
852      * @param connectivityTypeSet Set of types of connectivity. Example: IP
853      * @param onPresenceListener  listener that will receive notifications/subscription events
854      * @return a handle object that can be used to identify this subscription request. It can be
855      * used to unsubscribe from these events in the future
856      * @throws OcException if failure
857      */
858     public static OcPresenceHandle subscribePresence(
859             String host,
860             EnumSet<OcConnectivityType> connectivityTypeSet,
861             OnPresenceListener onPresenceListener) throws OcException {
862         OcPlatform.initCheck();
863         int connTypeInt = 0;
864
865         for (OcConnectivityType connType : OcConnectivityType.values()) {
866             if (connectivityTypeSet.contains(connType))
867                 connTypeInt |= connType.getValue();
868         }
869         return OcPlatform.subscribePresence0(
870                 host,
871                 connTypeInt,
872                 onPresenceListener
873         );
874     }
875
876     private static native OcPresenceHandle subscribePresence0(
877             String host,
878             int connectivityType,
879             OnPresenceListener onPresenceListener) throws OcException;
880
881     /**
882      * Subscribes to a server's presence change events. By making this subscription, every time a
883      * server adds/removes/alters a resource, starts or is intentionally stopped
884      *
885      * @param host                The IP address/addressable name of the server to subscribe to
886      * @param resourceType        a resource type specified as a filter for subscription events.
887      * @param connectivityTypeSet Set of types of connectivity. Example: IP
888      * @param onPresenceListener  listener that will receive notifications/subscription events
889      * @return a handle object that can be used to identify this subscription request. It can be
890      * used to unsubscribe from these events in the future
891      * @throws OcException if failure
892      */
893     public static OcPresenceHandle subscribePresence(
894             String host,
895             String resourceType,
896             EnumSet<OcConnectivityType> connectivityTypeSet,
897             OnPresenceListener onPresenceListener) throws OcException {
898         OcPlatform.initCheck();
899         int connTypeInt = 0;
900
901         for (OcConnectivityType connType : OcConnectivityType.values()) {
902             if (connectivityTypeSet.contains(connType))
903                 connTypeInt |= connType.getValue();
904         }
905         return OcPlatform.subscribePresence1(
906                 host,
907                 resourceType,
908                 connTypeInt,
909                 onPresenceListener);
910     }
911
912     private static native OcPresenceHandle subscribePresence1(
913             String host,
914             String resourceType,
915             int connectivityType,
916             OnPresenceListener onPresenceListener) throws OcException;
917
918     /**
919      * Unsubscribes from a previously subscribed server's presence events. Note that you may for
920      * a short time still receive events from the server since it may take time for the
921      * unsubscribe to take effect.
922      *
923      * @param ocPresenceHandle the handle object provided by the subscribePresence call that
924      *                         identifies this subscription
925      * @throws OcException if failure
926      */
927     public static void unsubscribePresence(
928             OcPresenceHandle ocPresenceHandle) throws OcException {
929         OcPlatform.initCheck();
930         OcPlatform.unsubscribePresence0(ocPresenceHandle);
931     }
932
933     private static native void unsubscribePresence0(
934             OcPresenceHandle ocPresenceHandle) throws OcException;
935
936     /**
937      * Subscribes to a server's device presence change events.
938      *
939      * @param host                The IP address/addressable name of the server to subscribe to.
940      * @param di                  Vector which can have the devices id.
941      * @param connectivityTypeSet Set of connectivity types, e.g. IP.
942      * @param onObserveListener   The handler method will be invoked with a map
943      *                            of attribute name and values.
944      * @return a handle object that can be used to identify this subscription request.
945      *         It can be used to unsubscribe from these events in the future.
946      * @throws OcException if failure.
947      */
948     public static OcPresenceHandle subscribeDevicePresence(
949             String host,
950             List<String> di,
951             EnumSet<OcConnectivityType> connectivityTypeSet,
952             OcResource.OnObserveListener onObserveListener) throws OcException {
953         OcPlatform.initCheck();
954         int connTypeInt = 0;
955
956         for (OcConnectivityType connType : OcConnectivityType.values()) {
957             if (connectivityTypeSet.contains(connType))
958                 connTypeInt |= connType.getValue();
959         }
960         return OcPlatform.subscribeDevicePresence0(
961                 host,
962                 di.toArray(new String[di.size()]),
963                 connTypeInt,
964                 onObserveListener);
965     }
966
967     private static native OcPresenceHandle subscribeDevicePresence0(
968             String host,
969             String[] di,
970             int connectivityType,
971             OcResource.OnObserveListener onObserveListener) throws OcException;
972
973     /**
974      * Creates a resource proxy object so that get/put/observe functionality can be used without
975      * discovering the object in advance. Note that the consumer of this method needs to provide
976      * all of the details required to correctly contact and observe the object. If the consumer
977      * lacks any of this information, they should discover the resource object normally.
978      * Additionally, you can only create this object if OcPlatform was initialized to be a Client
979      * or Client/Server.
980      *
981      * @param host                a string containing a resolvable host address of the server holding
982      *                            the resource
983      * @param uri                 the rest of the resource's URI that will permit messages to be
984      *                            properly routed.
985      *                            Example: /a/light
986      * @param connectivityTypeSet Set of types of connectivity. Example: IP
987      * @param isObservable        a boolean containing whether the resource supports observation
988      * @param resourceTypeList    a collection of resource types implemented by the resource
989      * @param interfaceList       a collection of interfaces that the resource supports/implements
990      * @return new resource object
991      * @throws OcException if failure
992      */
993     public static OcResource constructResourceObject(
994             String host,
995             String uri,
996             EnumSet<OcConnectivityType> connectivityTypeSet,
997             boolean isObservable,
998             List<String> resourceTypeList,
999             List<String> interfaceList) throws OcException {
1000         OcPlatform.initCheck();
1001         int connTypeInt = 0;
1002
1003         for (OcConnectivityType connType : OcConnectivityType.values()) {
1004             if (connectivityTypeSet.contains(connType))
1005                 connTypeInt |= connType.getValue();
1006         }
1007         return OcPlatform.constructResourceObject0(
1008                 host,
1009                 uri,
1010                 connTypeInt,
1011                 isObservable,
1012                 resourceTypeList.toArray(new String[resourceTypeList.size()]),
1013                 interfaceList.toArray(new String[interfaceList.size()])
1014         );
1015     }
1016
1017     private static native OcResource constructResourceObject0(
1018             String host,
1019             String uri,
1020             int connectivityType,
1021             boolean isObservable,
1022             String[] resourceTypes,
1023             String[] interfaces) throws OcException;
1024
1025     /**
1026      * Allows application entity handler to send response to an incoming request.
1027      *
1028      * @param ocResourceResponse resource response
1029      * @throws OcException if failure
1030      */
1031     public static void sendResponse(OcResourceResponse ocResourceResponse)
1032             throws OcException {
1033         OcPlatform.initCheck();
1034         OcPlatform.sendResponse0(ocResourceResponse);
1035     }
1036
1037     private static native void sendResponse0(OcResourceResponse ocResourceResponse)
1038             throws OcException;
1039
1040     /**
1041      *  Method to find all devices which are eligible for direct pairing and return the list.
1042      *
1043      *  @param timeout timeout for discovering direct pair devices.
1044      *  @param FindDirectPairingListener Callback function, which will receive the list of direct
1045      *                                  pairable devices.
1046      *  @throws OcException
1047      */
1048    public static native void findDirectPairingDevices(int timeout,
1049             FindDirectPairingListener onFindDirectPairingListener) throws OcException;
1050
1051     /**
1052      *  Method to get list of all paired devices for a given device.
1053      *
1054      *  @param GetDirectPairedListener Callback function, which will receive the list of direct
1055      *                                 paired devices.
1056      *  @throws OcException
1057      */
1058     public native void getDirectPairedDevices(GetDirectPairedListener onGetDirectPairedListener)
1059         throws OcException;
1060
1061     /**
1062      *  Method to perform direct pairing between two devices.
1063      *
1064      *  @param peer  Target peer
1065      *  @param prmType Pairing Method to be used for Pairing
1066      *  @param pin pin
1067      *  @param DirectPairingListener Callback function, which will be called after
1068      *                                      completion of direct pairing.
1069      *  @throws OcException
1070      */
1071     public static void doDirectPairing(
1072             OcDirectPairDevice peer,
1073             OcPrmType prmType,
1074             String pin,
1075             DirectPairingListener onDirectPairingListener) throws OcException {
1076
1077         OcPlatform.doDirectPairing0(
1078                 peer,
1079                 prmType.getValue(),
1080                 pin,
1081                 onDirectPairingListener
1082                 );
1083     }
1084
1085     private static native void doDirectPairing0(OcDirectPairDevice peer,
1086             int pmSel, String pinNumber, DirectPairingListener onDirectPairingListener)
1087     throws OcException;
1088
1089     /**
1090      * An FindDirectPairingListener can be registered via the OcPlatform.findDirectPairingDevices call.
1091      * Event listeners are notified asynchronously
1092      */
1093     public interface FindDirectPairingListener {
1094         public void onFindDirectPairingListener(List<OcDirectPairDevice> ocPairedDeviceList);
1095     }
1096
1097     /**
1098      * Listerner to Get List of already Direct Paired devices.
1099      * An GetDirectPairedListener can be registered via the OcPlatform.getDirectPairedDevices call.
1100      * Event listeners are notified asynchronously
1101      */
1102     public interface GetDirectPairedListener {
1103         public void onGetDirectPairedListener(List<OcDirectPairDevice> ocPairedDeviceList);
1104     }
1105
1106     /**
1107      * Listner to get result of doDirectPairing.
1108      * An DirectPairingListener can be registered via the OcPlatform.doDirectPairing call.
1109      * Event listeners are notified asynchronously
1110      */
1111     public interface DirectPairingListener {
1112         public void onDirectPairingListener(String devId, int result);
1113     }
1114
1115     /**
1116      * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
1117      * Event listeners are notified asynchronously
1118      */
1119     public interface OnResourceFoundListener {
1120         public void onResourceFound(OcResource resource);
1121         public void onFindResourceFailed(Throwable ex, String uri);
1122     }
1123
1124     /**
1125      * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call.
1126      * Event listeners are notified asynchronously
1127      */
1128     public interface OnDeviceFoundListener {
1129         public void onDeviceFound(OcRepresentation ocRepresentation);
1130     }
1131
1132     /**
1133      * An OnPlatformFoundListener can be registered via the OcPlatform.getPlatformInfo call.
1134      * Event listeners are notified asynchronously
1135      */
1136     public interface OnPlatformFoundListener {
1137         public void onPlatformFound(OcRepresentation ocRepresentation);
1138     }
1139
1140     /**
1141      * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call.
1142      * Event listeners are notified asynchronously
1143      */
1144     public interface OnPresenceListener {
1145         public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress);
1146     }
1147
1148     /**
1149      * An EntityHandler can be registered via the OcPlatform.registerResource call.
1150      * Event listeners are notified asynchronously
1151      *
1152      * @note entityhandler callback :
1153      * When you set specific return value like EntityHandlerResult.OK, SLOW
1154      * and etc in entity handler callback,
1155      * ocstack will be not send response automatically to client
1156      * except for error return value like EntityHandlerResult.ERROR
1157      * If you want to send response to client with specific result,
1158      * sendResponse API should be called with the result value.
1159      */
1160     public interface EntityHandler {
1161         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
1162     }
1163
1164     /**
1165      * Listner to get result of findKeepAliveResource and sendKeepAliveRequest.
1166      * An PingListener can be registered via the OcPlatform.KeepAliveListener and
1167      * OcPlatform.sendKeepAliveRequest call.
1168      * Event listeners are notified asynchronously
1169      */
1170     public interface KeepAliveListener {
1171         public void onKeepAliveListener(OcRepresentation ocRepresentation, int result);
1172     }
1173
1174     private static void initCheck() {
1175         if (!sIsPlatformInitialized) {
1176             throw new IllegalStateException("OcPlatform must be configured by making a call to " +
1177                     "OcPlatform.Configure before any other API calls are permitted");
1178         }
1179     }
1180
1181     /**
1182      * Gets platform quality of service
1183      *
1184      * @return quality of service
1185      */
1186     public static QualityOfService getPlatformQualityOfService() {
1187         OcPlatform.initCheck();
1188         return sPlatformQualityOfService;
1189     }
1190
1191     /**
1192      * Create an account manager object that can be used for doing request to account server.
1193      * You can only create this object if OCPlatform was initialized to be a Client or
1194      * Client/Server. Otherwise, this will return an empty shared ptr.
1195      *
1196      * @note For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
1197      *       methods of this object to work since device id is not generated on Client mode.
1198      *
1199      * @param host                Host IP Address of a account server.
1200      * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
1201      * @return new AccountManager object
1202      * @throws OcException if failure
1203      */
1204     public static OcAccountManager constructAccountManagerObject(
1205             String host,
1206             EnumSet<OcConnectivityType> connectivityTypeSet) throws OcException {
1207         OcPlatform.initCheck();
1208         int connTypeInt = 0;
1209
1210         for (OcConnectivityType connType : OcConnectivityType.values()) {
1211             if (connectivityTypeSet.contains(connType))
1212             {
1213                 connTypeInt |= connType.getValue();
1214             }
1215         }
1216         return OcPlatform.constructAccountManagerObject0(
1217                 host,
1218                 connTypeInt
1219                 );
1220     }
1221
1222     private static native OcAccountManager constructAccountManagerObject0(
1223             String host,
1224             int connectivityType) throws OcException;
1225     /**
1226      * Method to get device Id in byte array.
1227      * @return My DeviceId.
1228      */
1229     public static native byte[] getDeviceId();
1230
1231     /**
1232      * Method to set DeviceId.
1233      */
1234     public static native void setDeviceId(byte[] deviceId) throws OcException;
1235
1236     /**
1237     * gets OCRepresentation of KeepAlive resource from given host.
1238     *
1239     * @param host                   Host IP Address of pingResource
1240     * @param onKeepAliveFoundListener Function to callback with result code and OCRepresentation.
1241     * @throws OcException if failure
1242     */
1243     public static void findKeepAliveResource(String host,
1244                                              KeepAliveListener onKeepAliveFoundListener)
1245                                              throws OcException {
1246         OcPlatform.initCheck();
1247         OcPlatform.findKeepAliveResourceImpl(host, onKeepAliveFoundListener);
1248     }
1249
1250     private static native void findKeepAliveResourceImpl(String host,
1251                                                          KeepAliveListener onKeepAliveFoundListener)
1252                                                          throws OcException;
1253
1254     /**
1255     * send KeepAlive request to given host.
1256     *
1257     * @param interval             interval time of expected pong response
1258     * @param keepAliveResponseListener handles callback
1259     * @throws OcException if failure
1260     *
1261     */
1262     public static void sendKeepAliveRequest(String host, OcRepresentation ocRepresentation,
1263                                             KeepAliveListener keepAliveResponseListener)
1264                                             throws OcException {
1265         OcPlatform.initCheck();
1266         OcPlatform.sendKeepAliveRequestImpl(host, ocRepresentation, keepAliveResponseListener);
1267     }
1268
1269     private static native void sendKeepAliveRequestImpl(String host,
1270                                                         OcRepresentation ocRepresentation,
1271                                                         KeepAliveListener keepAliveResponseListener)
1272                                                         throws OcException;
1273 }