Imported Upstream version 0.9.2
[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
27 import java.util.EnumSet;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /**
32  * Contains the main entrance/functionality of the product. To set a custom configuration, the
33  * implementer must make a call to OcPlatform.Configure before the first usage of a function in this
34  * class.
35  */
36 public final class OcPlatform {
37
38     static {
39         System.loadLibrary("oc_logger");
40         System.loadLibrary("octbstack");
41         System.loadLibrary("connectivity_abstraction");
42         System.loadLibrary("oc");
43         System.loadLibrary("ocstack-jni");
44     }
45
46     /**
47      * Default interface
48      */
49     public static final String DEFAULT_INTERFACE = "oic.if.baseline";
50
51     /**
52      * Used in discovering (GET) links to other resources of a collection
53      */
54     public static final String LINK_INTERFACE = "oic.if.ll";
55
56     /**
57      * Used in GET, PUT, POST, DELETE methods on links to other resources of a collection
58      */
59     public static final String BATCH_INTERFACE = "oic.if.b";
60
61     /**
62      * Used in GET, PUT, POST methods on links to other remote resources of a group
63      */
64     public static final String GROUP_INTERFACE = "oic.mi.grp";
65
66     public static final String WELL_KNOWN_QUERY = "224.0.1.187:5683/oic/res";
67     public static final String MULTICAST_PREFIX = "224.0.1.187:5683";
68     public static final String MULTICAST_IP = "224.0.1.187";
69     public static final int MULTICAST_PORT = 5683;
70     public static final int DEFAULT_PRESENCE_TTL = 60;
71     public static final String DEVICE_URI = "/oic/d";
72     public static final String PRESENCE_URI = "/oic/ad";
73
74     private static volatile boolean sIsPlatformInitialized = false;
75
76     private OcPlatform() {
77     }
78
79     /**
80      * API for setting the configuration of the OcPlatform.
81      * Note: Any calls made to this AFTER the first call to OcPlatform.Configure will have no affect
82      *
83      * @param platformConfig platform configuration
84      */
85     public synchronized static void Configure(PlatformConfig platformConfig) {
86         if (!sIsPlatformInitialized) {
87             CaInterface.initialize(platformConfig.getContext());
88
89             OcPlatform.configure(
90                     platformConfig.getServiceType().getValue(),
91                     platformConfig.getModeType().getValue(),
92                     platformConfig.getIpAddress(),
93                     platformConfig.getPort(),
94                     platformConfig.getQualityOfService().getValue(),
95                     platformConfig.getSvrDbPath()
96             );
97
98             sIsPlatformInitialized = true;
99         }
100     }
101
102     private static native void configure(int serviceType,
103                                          int modeType,
104                                          String ipAddress,
105                                          int port,
106                                          int qualityOfService,
107                                          String dbPath);
108
109     /**
110      * API for notifying base that resource's attributes have changed.
111      *
112      * @param ocResourceHandle resource handle of the resource
113      * @throws OcException
114      */
115     public static void notifyAllObservers(
116             OcResourceHandle ocResourceHandle) throws OcException {
117         OcPlatform.initCheck();
118         OcPlatform.notifyAllObservers0(ocResourceHandle);
119     }
120
121     private static native void notifyAllObservers0(
122             OcResourceHandle ocResourceHandle) throws OcException;
123
124     /**
125      * API for notifying base that resource's attributes have changed.
126      *
127      * @param ocResourceHandle resource handle of the resource
128      * @param qualityOfService the quality of communication
129      * @throws OcException
130      */
131     public static void notifyAllObservers(
132             OcResourceHandle ocResourceHandle,
133             QualityOfService qualityOfService) throws OcException {
134         OcPlatform.initCheck();
135         OcPlatform.notifyAllObservers1(ocResourceHandle, qualityOfService.getValue());
136     }
137
138     private static native void notifyAllObservers1(
139             OcResourceHandle ocResourceHandle,
140             int qualityOfService) throws OcException;
141
142     /**
143      * API for notifying only specific clients that resource's attributes have changed.
144      *
145      * @param ocResourceHandle    resource handle of the resource
146      * @param ocObservationIdList These set of ids are ones which which will be notified upon
147      *                            resource change.
148      * @param ocResourceResponse  OcResourceResponse object used by app to fill the response for
149      *                            this resource change
150      * @throws OcException
151      */
152     public static void notifyListOfObservers(
153             OcResourceHandle ocResourceHandle,
154             List<Byte> ocObservationIdList,
155             OcResourceResponse ocResourceResponse) throws OcException {
156         OcPlatform.initCheck();
157
158         byte[] idArr = new byte[ocObservationIdList.size()];
159         Iterator<Byte> it = ocObservationIdList.iterator();
160         int i = 0;
161         while (it.hasNext()) {
162             idArr[i++] = (byte) it.next();
163         }
164
165         OcPlatform.notifyListOfObservers2(
166                 ocResourceHandle,
167                 idArr,
168                 ocResourceResponse);
169     }
170
171     private static native void notifyListOfObservers2(
172             OcResourceHandle ocResourceHandle,
173             byte[] ocObservationIdArray,
174             OcResourceResponse ocResourceResponse) throws OcException;
175
176     /**
177      * API for notifying only specific clients that resource's attributes have changed.
178      *
179      * @param ocResourceHandle    resource handle of the resource
180      * @param ocObservationIdList These set of ids are ones which which will be notified upon
181      *                            resource change.
182      * @param ocResourceResponse  OcResourceResponse object used by app to fill the response for
183      *                            this resource change
184      * @param qualityOfService    the quality of communication
185      * @throws OcException
186      */
187     public static void notifyListOfObservers(
188             OcResourceHandle ocResourceHandle,
189             List<Byte> ocObservationIdList,
190             OcResourceResponse ocResourceResponse,
191             QualityOfService qualityOfService) throws OcException {
192         OcPlatform.initCheck();
193
194         byte[] idArr = new byte[ocObservationIdList.size()];
195         Iterator<Byte> it = ocObservationIdList.iterator();
196         int i = 0;
197         while (it.hasNext()) {
198             idArr[i++] = (byte) it.next();
199         }
200
201         OcPlatform.notifyListOfObservers3(
202                 ocResourceHandle,
203                 idArr,
204                 ocResourceResponse,
205                 qualityOfService.getValue()
206         );
207     }
208
209     private static native void notifyListOfObservers3(
210             OcResourceHandle ocResourceHandle,
211             byte[] ocObservationIdArray,
212             OcResourceResponse ocResourceResponse,
213             int qualityOfService) throws OcException;
214
215     /**
216      * API for Service and Resource Discovery. NOTE: This API applies to client side only
217      *
218      * @param host                    Host IP Address of a service to direct resource discovery query.
219      *                                If empty, performs multicast resource discovery query
220      * @param resourceUri             name of the resource. If null or empty, performs search for all
221      *                                resource names
222      * @param connectivityType        a type of connectivity indicating the interface. Example: IPV4,
223      *                                IPV6, ALL
224      * @param onResourceFoundListener Handles events, success states and failure states.
225      * @throws OcException
226      */
227     public static void findResource(
228             String host,
229             String resourceUri,
230             EnumSet<OcConnectivityType> connectivityTypeSet,
231             OnResourceFoundListener onResourceFoundListener) throws OcException {
232         OcPlatform.initCheck();
233
234         int connTypeInt = 0;
235
236         for (OcConnectivityType connType : OcConnectivityType.values()) {
237             if (connectivityTypeSet.contains(connType))
238                 connTypeInt |= connType.getValue();
239         }
240
241         OcPlatform.findResource0(
242                 host,
243                 resourceUri,
244                 connTypeInt,
245                 onResourceFoundListener
246         );
247     }
248
249     private static native void findResource0(
250             String host,
251             String resourceUri,
252             int connectivityType,
253             OnResourceFoundListener onResourceFoundListener) throws OcException;
254
255     /**
256      * API for Service and Resource Discovery. NOTE: This API applies to client side only
257      *
258      * @param host                    Host IP Address of a service to direct resource discovery query.
259      *                                If empty, performs multicast resource discovery query
260      * @param resourceUri             name of the resource. If null or empty, performs search for all
261      *                                resource names
262      * @param connectivityType        a type of connectivity indicating the interface. Example: IPV4,
263      *                                IPV6, ALL
264      * @param onResourceFoundListener Handles events, success states and failure states.
265      * @param qualityOfService        the quality of communication
266      * @throws OcException
267      */
268     public static void findResource(
269             String host,
270             String resourceUri,
271             EnumSet<OcConnectivityType> connectivityTypeSet,
272             OnResourceFoundListener onResourceFoundListener,
273             QualityOfService qualityOfService) throws OcException {
274         OcPlatform.initCheck();
275
276         int connTypeInt = 0;
277
278         for (OcConnectivityType connType : OcConnectivityType.values()) {
279             if (connectivityTypeSet.contains(connType))
280                 connTypeInt |= connType.getValue();
281         }
282
283         OcPlatform.findResource1(host,
284                 resourceUri,
285                 connTypeInt,
286                 onResourceFoundListener,
287                 qualityOfService.getValue()
288         );
289     }
290
291     private static native void findResource1(
292             String host,
293             String resourceUri,
294             int connectivityType,
295             OnResourceFoundListener onResourceFoundListener,
296             int qualityOfService) throws OcException;
297
298     /**
299      * API for Device Discovery
300      *
301      * @param host                  Host IP Address. If null or empty, Multicast is performed.
302      * @param deviceUri             Uri containing address to the virtual device
303      * @param connectivityType      a type of connectivity indicating the interface. Example: IPV4,
304      *                              IPV6, ALL
305      * @param onDeviceFoundListener Handles events, success states and failure states.
306      * @throws OcException
307      */
308     public static void getDeviceInfo(
309             String host,
310             String deviceUri,
311             EnumSet<OcConnectivityType> connectivityTypeSet,
312             OnDeviceFoundListener onDeviceFoundListener) throws OcException {
313         OcPlatform.initCheck();
314         int connTypeInt = 0;
315
316         for (OcConnectivityType connType : OcConnectivityType.values()) {
317             if (connectivityTypeSet.contains(connType))
318                 connTypeInt |= connType.getValue();
319         }
320         OcPlatform.getDeviceInfo0(
321                 host,
322                 deviceUri,
323                 connTypeInt,
324                 onDeviceFoundListener
325         );
326     }
327
328     private static native void getDeviceInfo0(
329             String host,
330             String deviceUri,
331             int connectivityType,
332             OnDeviceFoundListener onDeviceFoundListener) throws OcException;
333
334     /**
335      * API for Device Discovery
336      *
337      * @param host                  Host IP Address. If null or empty, Multicast is performed.
338      * @param deviceUri             Uri containing address to the virtual device
339      * @param connectivityType      a type of connectivity indicating the interface. Example: IPV4,
340      *                              IPV6, ALL
341      * @param onDeviceFoundListener Handles events, success states and failure states.
342      * @param qualityOfService      the quality of communication
343      * @throws OcException
344      */
345     public static void getDeviceInfo(
346             String host,
347             String deviceUri,
348             EnumSet<OcConnectivityType> connectivityTypeSet,
349             OnDeviceFoundListener onDeviceFoundListener,
350             QualityOfService qualityOfService) throws OcException {
351         OcPlatform.initCheck();
352         int connTypeInt = 0;
353
354         for (OcConnectivityType connType : OcConnectivityType.values()) {
355             if (connectivityTypeSet.contains(connType))
356                 connTypeInt |= connType.getValue();
357         }
358         OcPlatform.getDeviceInfo1(
359                 host,
360                 deviceUri,
361                 connTypeInt,
362                 onDeviceFoundListener,
363                 qualityOfService.getValue()
364         );
365     }
366
367     private static native void getDeviceInfo1(
368             String host,
369             String deviceUri,
370             int connectivityType,
371             OnDeviceFoundListener onDeviceFoundListener,
372             int qualityOfService) throws OcException;
373
374     /**
375      * API for Platform Discovery
376      *
377      * @param host                    Host IP Address. If null or empty, Multicast is performed.
378      * @param platformUri             Uri containing address to the platform
379      * @param connectivityType        a type of connectivity indicating the interface. Example: IPV4,
380      *                                IPV6, ALL
381      * @param onPlatformFoundListener Handles events, success states and failure states.
382      * @throws OcException
383      */
384
385     public static void getPlatformInfo(
386             String host,
387             String platformUri,
388             EnumSet<OcConnectivityType> connectivityTypeSet,
389             OnPlatformFoundListener onPlatformFoundListener) throws OcException {
390         OcPlatform.initCheck();
391         int connTypeInt = 0;
392
393         for (OcConnectivityType connType : OcConnectivityType.values()) {
394             if (connectivityTypeSet.contains(connType))
395                 connTypeInt |= connType.getValue();
396         }
397         OcPlatform.getPlatformInfo0(
398                 host,
399                 platformUri,
400                 connTypeInt,
401                 onPlatformFoundListener
402         );
403     }
404
405     private static native void getPlatformInfo0(
406             String host,
407             String platformUri,
408             int connectivityType,
409             OnPlatformFoundListener onPlatformInfoFoundListener) throws OcException;
410
411     /**
412      * API for Platform Discovery
413      *
414      * @param host                    Host IP Address. If null or empty, Multicast is performed.
415      * @param platformUri             Uri containing address to the platform
416      * @param connectivityType        a type of connectivity indicating the interface. Example: IPV4,
417      *                                IPV6, ALL
418      * @param onPlatformFoundListener Handles events, success states and failure states.
419      * @param qualityOfService        the quality of communication
420      * @throws OcException
421      */
422
423     public static void getPlatformInfo(
424             String host,
425             String platformUri,
426             EnumSet<OcConnectivityType> connectivityTypeSet,
427             OnPlatformFoundListener onPlatformFoundListener,
428             QualityOfService qualityOfService) throws OcException {
429         OcPlatform.initCheck();
430         int connTypeInt = 0;
431
432         for (OcConnectivityType connType : OcConnectivityType.values()) {
433             if (connectivityTypeSet.contains(connType))
434                 connTypeInt |= connType.getValue();
435         }
436         OcPlatform.getPlatformInfo1(
437                 host,
438                 platformUri,
439                 connTypeInt,
440                 onPlatformFoundListener,
441                 qualityOfService.getValue()
442         );
443     }
444
445     private static native void getPlatformInfo1(
446             String host,
447             String platformUri,
448             int connectivityType,
449             OnPlatformFoundListener onPlatformFoundListener,
450             int qualityOfService) throws OcException;
451
452     /**
453      * This API registers a resource with the server NOTE: This API applies to server side only.
454      *
455      * @param ocResource The instance of OcResource with all data filled
456      * @return resource handle
457      * @throws OcException
458      */
459     public static OcResourceHandle registerResource(
460             OcResource ocResource) throws OcException {
461         OcPlatform.initCheck();
462         return OcPlatform.registerResource0(ocResource);
463     }
464
465     private static native OcResourceHandle registerResource0(
466             OcResource ocResource) throws OcException;
467
468     /**
469      * This API registers a resource with the server NOTE: This API applies to server side only.
470      *
471      * @param resourceUri         The URI of the resource. Example: "a/light"
472      * @param resourceTypeName    The resource type. Example: "light"
473      * @param resourceInterface   The resource interface (whether it is collection etc).
474      * @param entityHandler       entity handler.
475      * @param resourcePropertySet indicates the property of the resource
476      * @return resource handle
477      * @throws OcException
478      */
479     public static OcResourceHandle registerResource(
480             String resourceUri,
481             String resourceTypeName,
482             String resourceInterface,
483             EntityHandler entityHandler,
484             EnumSet<ResourceProperty> resourcePropertySet) throws OcException {
485         OcPlatform.initCheck();
486
487         int resProperty = 0;
488
489         for (ResourceProperty prop : ResourceProperty.values()) {
490             if (resourcePropertySet.contains(prop))
491                 resProperty |= prop.getValue();
492         }
493
494         return OcPlatform.registerResource1(resourceUri,
495                 resourceTypeName,
496                 resourceInterface,
497                 entityHandler,
498                 resProperty);
499     }
500
501     private static native OcResourceHandle registerResource1(
502             String resourceUri,
503             String resourceTypeName,
504             String resourceInterface,
505             EntityHandler entityHandler,
506             int resourceProperty) throws OcException;
507
508     /**
509      * Register Device Info
510      *
511      * @param ocDeviceInfo object containing all the device specific information
512      * @throws OcException
513      */
514     public static void registerDeviceInfo(
515             OcDeviceInfo ocDeviceInfo) throws OcException {
516         OcPlatform.initCheck();
517         OcPlatform.registerDeviceInfo0(
518                 ocDeviceInfo.getDeviceName()
519         );
520     }
521
522     private static native void registerDeviceInfo0(
523             String deviceName
524     ) throws OcException;
525
526     /**
527      * Register Platform Info
528      *
529      * @param ocPlatformInfo object containing all the platform specific information
530      * @throws OcException
531      */
532     public static void registerPlatformInfo(
533             OcPlatformInfo ocPlatformInfo) throws OcException {
534         OcPlatform.initCheck();
535         OcPlatform.registerPlatformInfo0(
536                 ocPlatformInfo.getPlatformID(),
537                 ocPlatformInfo.getManufacturerName(),
538                 ocPlatformInfo.getManufacturerUrl(),
539                 ocPlatformInfo.getModelNumber(),
540                 ocPlatformInfo.getDateOfManufacture(),
541                 ocPlatformInfo.getPlatformVersion(),
542                 ocPlatformInfo.getOperatingSystemVersion(),
543                 ocPlatformInfo.getHardwareVersion(),
544                 ocPlatformInfo.getFirmwareVersion(),
545                 ocPlatformInfo.getSupportUrl(),
546                 ocPlatformInfo.getSystemTime()
547         );
548     }
549
550     private static native void registerPlatformInfo0(
551             String platformId, String manufacturerName, String manufacturerUrl,
552             String modelNumber, String dateOfManufacture, String platformVersion,
553             String operatingSystemVersion, String hardwareVersion, String firmwareVersion,
554             String supportUrl, String systemTime
555     ) throws OcException;
556
557     /**
558      * This API unregisters a resource with the server NOTE: This API applies to server side only.
559      *
560      * @param ocResourceHandle This is the resource handle which we which to unregister from the
561      *                         server
562      * @throws OcException
563      */
564     public static void unregisterResource(
565             OcResourceHandle ocResourceHandle) throws OcException {
566         OcPlatform.initCheck();
567         OcPlatform.unregisterResource0(ocResourceHandle);
568     }
569
570     private static native void unregisterResource0(
571             OcResourceHandle ocResourceHandle) throws OcException;
572
573
574     /**
575      * Add a resource to a collection resource
576      *
577      * @param ocResourceCollectionHandle handle to the collection resource
578      * @param ocResourceHandle           handle to resource to be added to the collection resource
579      * @throws OcException
580      */
581     public static void bindResource(
582             OcResourceHandle ocResourceCollectionHandle,
583             OcResourceHandle ocResourceHandle) throws OcException {
584         OcPlatform.initCheck();
585         OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle);
586     }
587
588     private static native void bindResource0(
589             OcResourceHandle ocResourceCollectionHandle,
590             OcResourceHandle ocResourceHandle) throws OcException;
591
592     /**
593      * Add multiple resources to a collection resource.
594      *
595      * @param ocResourceCollectionHandle handle to the collection resource
596      * @param ocResourceHandleList       reference to list of resource handles to be added to the
597      *                                   collection resource
598      * @throws OcException
599      */
600     public static void bindResources(
601             OcResourceHandle ocResourceCollectionHandle,
602             List<OcResourceHandle> ocResourceHandleList) throws OcException {
603         OcPlatform.initCheck();
604         OcPlatform.bindResources0(
605                 ocResourceCollectionHandle,
606                 ocResourceHandleList.toArray(
607                         new OcResourceHandle[ocResourceHandleList.size()])
608         );
609     }
610
611     private static native void bindResources0(
612             OcResourceHandle ocResourceCollectionHandle,
613             OcResourceHandle[] ocResourceHandleArray) throws OcException;
614
615     /**
616      * Unbind a resource from a collection resource.
617      *
618      * @param ocResourceCollectionHandle handle to the collection resource
619      * @param ocResourceHandle           resource handle to be unbound from the collection resource
620      * @throws OcException
621      */
622     public static void unbindResource(
623             OcResourceHandle ocResourceCollectionHandle,
624             OcResourceHandle ocResourceHandle) throws OcException {
625         OcPlatform.initCheck();
626         OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle);
627     }
628
629     private static native void unbindResource0(
630             OcResourceHandle ocResourceCollectionHandle,
631             OcResourceHandle ocResourceHandle) throws OcException;
632
633     /**
634      * Unbind resources from a collection resource.
635      *
636      * @param ocResourceCollectionHandle Handle to the collection resource
637      * @param ocResourceHandleList       List of resource handles to be unbound from the collection
638      *                                   resource
639      * @throws OcException
640      */
641     public static void unbindResources(
642             OcResourceHandle ocResourceCollectionHandle,
643             List<OcResourceHandle> ocResourceHandleList) throws OcException {
644         OcPlatform.initCheck();
645         OcPlatform.unbindResources0(
646                 ocResourceCollectionHandle,
647                 ocResourceHandleList.toArray(
648                         new OcResourceHandle[ocResourceHandleList.size()])
649         );
650     }
651
652     private static native void unbindResources0(
653             OcResourceHandle ocResourceCollectionHandle,
654             OcResourceHandle[] ocResourceHandleArray) throws OcException;
655
656     /**
657      * Binds a type to a particular resource
658      *
659      * @param ocResourceHandle handle to the resource
660      * @param resourceTypeName new typename to bind to the resource
661      * @throws OcException
662      */
663     public static void bindTypeToResource(
664             OcResourceHandle ocResourceHandle,
665             String resourceTypeName) throws OcException {
666         OcPlatform.initCheck();
667         OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName);
668     }
669
670     private static native void bindTypeToResource0(
671             OcResourceHandle ocResourceHandle,
672             String resourceTypeName) throws OcException;
673
674     /**
675      * Binds an interface to a particular resource
676      *
677      * @param ocResourceHandle      handle to the resource
678      * @param resourceInterfaceName new interface to bind to the resource
679      * @throws OcException
680      */
681     public static void bindInterfaceToResource(
682             OcResourceHandle ocResourceHandle,
683             String resourceInterfaceName) throws OcException {
684         OcPlatform.initCheck();
685         OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName);
686     }
687
688     private static native void bindInterfaceToResource0(
689             OcResourceHandle ocResourceHandle,
690             String resourceInterfaceName) throws OcException;
691
692     /**
693      * Start Presence announcements.
694      *
695      * @param ttl time to live in seconds
696      * @throws OcException
697      */
698     public static void startPresence(int ttl) throws OcException {
699         OcPlatform.initCheck();
700         OcPlatform.startPresence0(ttl);
701     }
702
703     private static native void startPresence0(int ttl) throws OcException;
704
705     /**
706      * Stop Presence announcements.
707      *
708      * @throws OcException
709      */
710     public static void stopPresence() throws OcException {
711         OcPlatform.initCheck();
712         OcPlatform.stopPresence0();
713     }
714
715     private static native void stopPresence0() throws OcException;
716
717     /**
718      * Subscribes to a server's presence change events. By making this subscription, every time a
719      * server adds/removes/alters a resource, starts or is intentionally stopped
720      *
721      * @param host               The IP address/addressable name of the server to subscribe to
722      * @param connectivityType   a type of connectivity indicating the interface. Example: IPV4,
723      *                           IPV6, ALL
724      * @param onPresenceListener listener that will receive notifications/subscription events
725      * @return a handle object that can be used to identify this subscription request. It can be
726      * used to unsubscribe from these events in the future
727      * @throws OcException
728      */
729     public static OcPresenceHandle subscribePresence(
730             String host,
731             EnumSet<OcConnectivityType> connectivityTypeSet,
732             OnPresenceListener onPresenceListener) throws OcException {
733         OcPlatform.initCheck();
734         int connTypeInt = 0;
735
736         for (OcConnectivityType connType : OcConnectivityType.values()) {
737             if (connectivityTypeSet.contains(connType))
738                 connTypeInt |= connType.getValue();
739         }
740         return OcPlatform.subscribePresence0(
741                 host,
742                 connTypeInt,
743                 onPresenceListener
744         );
745     }
746
747     private static native OcPresenceHandle subscribePresence0(
748             String host,
749             int connectivityType,
750             OnPresenceListener onPresenceListener) throws OcException;
751
752     /**
753      * Subscribes to a server's presence change events. By making this subscription, every time a
754      * server adds/removes/alters a resource, starts or is intentionally stopped
755      *
756      * @param host               The IP address/addressable name of the server to subscribe to
757      * @param resourceType       a resource type specified as a filter for subscription events.
758      * @param connectivityType   a type of connectivity indicating the interface. Example: IPV4,
759      *                           IPV6, ALL
760      * @param onPresenceListener listener that will receive notifications/subscription events
761      * @return a handle object that can be used to identify this subscription request. It can be
762      * used to unsubscribe from these events in the future
763      * @throws OcException
764      */
765     public static OcPresenceHandle subscribePresence(
766             String host,
767             String resourceType,
768             EnumSet<OcConnectivityType> connectivityTypeSet,
769             OnPresenceListener onPresenceListener) throws OcException {
770         OcPlatform.initCheck();
771         int connTypeInt = 0;
772
773         for (OcConnectivityType connType : OcConnectivityType.values()) {
774             if (connectivityTypeSet.contains(connType))
775                 connTypeInt |= connType.getValue();
776         }
777         return OcPlatform.subscribePresence1(
778                 host,
779                 resourceType,
780                 connTypeInt,
781                 onPresenceListener);
782     }
783
784     private static native OcPresenceHandle subscribePresence1(
785             String host,
786             String resourceType,
787             int connectivityType,
788             OnPresenceListener onPresenceListener) throws OcException;
789
790     /**
791      * Unsubscribes from a previously subscribed server's presence events. Note that you may for
792      * a short time still receive events from the server since it may take time for the
793      * unsubscribe to take effect.
794      *
795      * @param ocPresenceHandle the handle object provided by the subscribePresence call that
796      *                         identifies this subscription
797      * @throws OcException
798      */
799     public static void unsubscribePresence(
800             OcPresenceHandle ocPresenceHandle) throws OcException {
801         OcPlatform.initCheck();
802         OcPlatform.unsubscribePresence0(ocPresenceHandle);
803     }
804
805     private static native void unsubscribePresence0(
806             OcPresenceHandle ocPresenceHandle) throws OcException;
807
808     /**
809      * Creates a resource proxy object so that get/put/observe functionality can be used without
810      * discovering the object in advance. Note that the consumer of this method needs to provide
811      * all of the details required to correctly contact and observe the object. If the consumer
812      * lacks any of this information, they should discover the resource object normally.
813      * Additionally, you can only create this object if OcPlatform was initialized to be a Client
814      * or Client/Server.
815      *
816      * @param host             a string containing a resolvable host address of the server holding
817      *                         the resource
818      * @param uri              the rest of the resource's URI that will permit messages to be
819      *                         properly routed.
820      *                         Example: /a/light
821      * @param connectivityType a type of connectivity indicating the interface. Example: IPV4,
822      *                         IPV6, ALL
823      * @param isObservable     a boolean containing whether the resource supports observation
824      * @param resourceTypeList a collection of resource types implemented by the resource
825      * @param interfaceList    a collection of interfaces that the resource supports/implements
826      * @return new resource object
827      * @throws OcException
828      */
829     public static OcResource constructResourceObject(
830             String host,
831             String uri,
832             EnumSet<OcConnectivityType> connectivityTypeSet,
833             boolean isObservable,
834             List<String> resourceTypeList,
835             List<String> interfaceList) throws OcException {
836         OcPlatform.initCheck();
837         int connTypeInt = 0;
838
839         for (OcConnectivityType connType : OcConnectivityType.values()) {
840             if (connectivityTypeSet.contains(connType))
841                 connTypeInt |= connType.getValue();
842         }
843         return OcPlatform.constructResourceObject0(
844                 host,
845                 uri,
846                 connTypeInt,
847                 isObservable,
848                 resourceTypeList.toArray(new String[resourceTypeList.size()]),
849                 interfaceList.toArray(new String[interfaceList.size()])
850         );
851     }
852
853     private static native OcResource constructResourceObject0(
854             String host,
855             String uri,
856             int connectivityType,
857             boolean isObservable,
858             String[] resourceTypes,
859             String[] interfaces) throws OcException;
860
861     /**
862      * Allows application entity handler to send response to an incoming request.
863      *
864      * @param ocResourceResponse resource response
865      * @throws OcException
866      */
867     public static void sendResponse(OcResourceResponse ocResourceResponse)
868             throws OcException {
869         OcPlatform.initCheck();
870         OcPlatform.sendResponse0(ocResourceResponse);
871     }
872
873     private static native void sendResponse0(OcResourceResponse ocResourceResponse)
874             throws OcException;
875
876     /**
877      * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
878      * Event listeners are notified asynchronously
879      */
880     public interface OnResourceFoundListener {
881         public void onResourceFound(OcResource resource);
882     }
883
884     /**
885      * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call.
886      * Event listeners are notified asynchronously
887      */
888     public interface OnDeviceFoundListener {
889         public void onDeviceFound(OcRepresentation ocRepresentation);
890     }
891
892     /**
893      * An OnPlatformFoundListener can be registered via the OcPlatform.getPlatformInfo call.
894      * Event listeners are notified asynchronously
895      */
896     public interface OnPlatformFoundListener {
897         public void onPlatformFound(OcRepresentation ocRepresentation);
898     }
899
900     /**
901      * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call.
902      * Event listeners are notified asynchronously
903      */
904     public interface OnPresenceListener {
905         public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress);
906     }
907
908     /**
909      * An EntityHandler can be registered via the OcPlatform.registerResource call.
910      * Event listeners are notified asynchronously
911      */
912     public interface EntityHandler {
913         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
914     }
915
916     private static void initCheck() {
917         if (!sIsPlatformInitialized) {
918             throw new IllegalStateException("OcPlatform must be configured by making a call to " +
919                     "OcPlatform.Configure before any other API calls are permitted");
920         }
921     }
922 }