Android: Mimics stack behavior and allows "null" entityHandler to be passed into...
[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 = "/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         if(null == entityHandler){
495             entityHandler = new EntityHandler() {
496                 @Override
497                 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest) {
498                     return EntityHandlerResult.OK;
499                 }
500             };
501         }
502
503         return OcPlatform.registerResource1(resourceUri,
504                 resourceTypeName,
505                 resourceInterface,
506                 entityHandler,
507                 resProperty);
508     }
509
510     private static native OcResourceHandle registerResource1(
511             String resourceUri,
512             String resourceTypeName,
513             String resourceInterface,
514             EntityHandler entityHandler,
515             int resourceProperty) throws OcException;
516
517     /**
518      * Register Device Info
519      *
520      * @param ocDeviceInfo object containing all the device specific information
521      * @throws OcException
522      */
523     public static void registerDeviceInfo(
524             OcDeviceInfo ocDeviceInfo) throws OcException {
525         OcPlatform.initCheck();
526         OcPlatform.registerDeviceInfo0(
527                 ocDeviceInfo.getDeviceName()
528         );
529     }
530
531     private static native void registerDeviceInfo0(
532             String deviceName
533     ) throws OcException;
534
535     /**
536      * Register Platform Info
537      *
538      * @param ocPlatformInfo object containing all the platform specific information
539      * @throws OcException
540      */
541     public static void registerPlatformInfo(
542             OcPlatformInfo ocPlatformInfo) throws OcException {
543         OcPlatform.initCheck();
544         OcPlatform.registerPlatformInfo0(
545                 ocPlatformInfo.getPlatformID(),
546                 ocPlatformInfo.getManufacturerName(),
547                 ocPlatformInfo.getManufacturerUrl(),
548                 ocPlatformInfo.getModelNumber(),
549                 ocPlatformInfo.getDateOfManufacture(),
550                 ocPlatformInfo.getPlatformVersion(),
551                 ocPlatformInfo.getOperatingSystemVersion(),
552                 ocPlatformInfo.getHardwareVersion(),
553                 ocPlatformInfo.getFirmwareVersion(),
554                 ocPlatformInfo.getSupportUrl(),
555                 ocPlatformInfo.getSystemTime()
556         );
557     }
558
559     private static native void registerPlatformInfo0(
560             String platformId, String manufacturerName, String manufacturerUrl,
561             String modelNumber, String dateOfManufacture, String platformVersion,
562             String operatingSystemVersion, String hardwareVersion, String firmwareVersion,
563             String supportUrl, String systemTime
564     ) throws OcException;
565
566     /**
567      * This API unregisters a resource with the server NOTE: This API applies to server side only.
568      *
569      * @param ocResourceHandle This is the resource handle which we which to unregister from the
570      *                         server
571      * @throws OcException
572      */
573     public static void unregisterResource(
574             OcResourceHandle ocResourceHandle) throws OcException {
575         OcPlatform.initCheck();
576         OcPlatform.unregisterResource0(ocResourceHandle);
577     }
578
579     private static native void unregisterResource0(
580             OcResourceHandle ocResourceHandle) throws OcException;
581
582
583     /**
584      * Add a resource to a collection resource
585      *
586      * @param ocResourceCollectionHandle handle to the collection resource
587      * @param ocResourceHandle           handle to resource to be added to the collection resource
588      * @throws OcException
589      */
590     public static void bindResource(
591             OcResourceHandle ocResourceCollectionHandle,
592             OcResourceHandle ocResourceHandle) throws OcException {
593         OcPlatform.initCheck();
594         OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle);
595     }
596
597     private static native void bindResource0(
598             OcResourceHandle ocResourceCollectionHandle,
599             OcResourceHandle ocResourceHandle) throws OcException;
600
601     /**
602      * Add multiple resources to a collection resource.
603      *
604      * @param ocResourceCollectionHandle handle to the collection resource
605      * @param ocResourceHandleList       reference to list of resource handles to be added to the
606      *                                   collection resource
607      * @throws OcException
608      */
609     public static void bindResources(
610             OcResourceHandle ocResourceCollectionHandle,
611             List<OcResourceHandle> ocResourceHandleList) throws OcException {
612         OcPlatform.initCheck();
613         OcPlatform.bindResources0(
614                 ocResourceCollectionHandle,
615                 ocResourceHandleList.toArray(
616                         new OcResourceHandle[ocResourceHandleList.size()])
617         );
618     }
619
620     private static native void bindResources0(
621             OcResourceHandle ocResourceCollectionHandle,
622             OcResourceHandle[] ocResourceHandleArray) throws OcException;
623
624     /**
625      * Unbind a resource from a collection resource.
626      *
627      * @param ocResourceCollectionHandle handle to the collection resource
628      * @param ocResourceHandle           resource handle to be unbound from the collection resource
629      * @throws OcException
630      */
631     public static void unbindResource(
632             OcResourceHandle ocResourceCollectionHandle,
633             OcResourceHandle ocResourceHandle) throws OcException {
634         OcPlatform.initCheck();
635         OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle);
636     }
637
638     private static native void unbindResource0(
639             OcResourceHandle ocResourceCollectionHandle,
640             OcResourceHandle ocResourceHandle) throws OcException;
641
642     /**
643      * Unbind resources from a collection resource.
644      *
645      * @param ocResourceCollectionHandle Handle to the collection resource
646      * @param ocResourceHandleList       List of resource handles to be unbound from the collection
647      *                                   resource
648      * @throws OcException
649      */
650     public static void unbindResources(
651             OcResourceHandle ocResourceCollectionHandle,
652             List<OcResourceHandle> ocResourceHandleList) throws OcException {
653         OcPlatform.initCheck();
654         OcPlatform.unbindResources0(
655                 ocResourceCollectionHandle,
656                 ocResourceHandleList.toArray(
657                         new OcResourceHandle[ocResourceHandleList.size()])
658         );
659     }
660
661     private static native void unbindResources0(
662             OcResourceHandle ocResourceCollectionHandle,
663             OcResourceHandle[] ocResourceHandleArray) throws OcException;
664
665     /**
666      * Binds a type to a particular resource
667      *
668      * @param ocResourceHandle handle to the resource
669      * @param resourceTypeName new typename to bind to the resource
670      * @throws OcException
671      */
672     public static void bindTypeToResource(
673             OcResourceHandle ocResourceHandle,
674             String resourceTypeName) throws OcException {
675         OcPlatform.initCheck();
676         OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName);
677     }
678
679     private static native void bindTypeToResource0(
680             OcResourceHandle ocResourceHandle,
681             String resourceTypeName) throws OcException;
682
683     /**
684      * Binds an interface to a particular resource
685      *
686      * @param ocResourceHandle      handle to the resource
687      * @param resourceInterfaceName new interface to bind to the resource
688      * @throws OcException
689      */
690     public static void bindInterfaceToResource(
691             OcResourceHandle ocResourceHandle,
692             String resourceInterfaceName) throws OcException {
693         OcPlatform.initCheck();
694         OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName);
695     }
696
697     private static native void bindInterfaceToResource0(
698             OcResourceHandle ocResourceHandle,
699             String resourceInterfaceName) throws OcException;
700
701     /**
702      * Start Presence announcements.
703      *
704      * @param ttl time to live in seconds
705      * @throws OcException
706      */
707     public static void startPresence(int ttl) throws OcException {
708         OcPlatform.initCheck();
709         OcPlatform.startPresence0(ttl);
710     }
711
712     private static native void startPresence0(int ttl) throws OcException;
713
714     /**
715      * Stop Presence announcements.
716      *
717      * @throws OcException
718      */
719     public static void stopPresence() throws OcException {
720         OcPlatform.initCheck();
721         OcPlatform.stopPresence0();
722     }
723
724     private static native void stopPresence0() throws OcException;
725
726     /**
727      * Subscribes to a server's presence change events. By making this subscription, every time a
728      * server adds/removes/alters a resource, starts or is intentionally stopped
729      *
730      * @param host               The IP address/addressable name of the server to subscribe to
731      * @param connectivityType   a type of connectivity indicating the interface. Example: IPV4,
732      *                           IPV6, ALL
733      * @param onPresenceListener listener that will receive notifications/subscription events
734      * @return a handle object that can be used to identify this subscription request. It can be
735      * used to unsubscribe from these events in the future
736      * @throws OcException
737      */
738     public static OcPresenceHandle subscribePresence(
739             String host,
740             EnumSet<OcConnectivityType> connectivityTypeSet,
741             OnPresenceListener onPresenceListener) throws OcException {
742         OcPlatform.initCheck();
743         int connTypeInt = 0;
744
745         for (OcConnectivityType connType : OcConnectivityType.values()) {
746             if (connectivityTypeSet.contains(connType))
747                 connTypeInt |= connType.getValue();
748         }
749         return OcPlatform.subscribePresence0(
750                 host,
751                 connTypeInt,
752                 onPresenceListener
753         );
754     }
755
756     private static native OcPresenceHandle subscribePresence0(
757             String host,
758             int connectivityType,
759             OnPresenceListener onPresenceListener) throws OcException;
760
761     /**
762      * Subscribes to a server's presence change events. By making this subscription, every time a
763      * server adds/removes/alters a resource, starts or is intentionally stopped
764      *
765      * @param host               The IP address/addressable name of the server to subscribe to
766      * @param resourceType       a resource type specified as a filter for subscription events.
767      * @param connectivityType   a type of connectivity indicating the interface. Example: IPV4,
768      *                           IPV6, ALL
769      * @param onPresenceListener listener that will receive notifications/subscription events
770      * @return a handle object that can be used to identify this subscription request. It can be
771      * used to unsubscribe from these events in the future
772      * @throws OcException
773      */
774     public static OcPresenceHandle subscribePresence(
775             String host,
776             String resourceType,
777             EnumSet<OcConnectivityType> connectivityTypeSet,
778             OnPresenceListener onPresenceListener) throws OcException {
779         OcPlatform.initCheck();
780         int connTypeInt = 0;
781
782         for (OcConnectivityType connType : OcConnectivityType.values()) {
783             if (connectivityTypeSet.contains(connType))
784                 connTypeInt |= connType.getValue();
785         }
786         return OcPlatform.subscribePresence1(
787                 host,
788                 resourceType,
789                 connTypeInt,
790                 onPresenceListener);
791     }
792
793     private static native OcPresenceHandle subscribePresence1(
794             String host,
795             String resourceType,
796             int connectivityType,
797             OnPresenceListener onPresenceListener) throws OcException;
798
799     /**
800      * Unsubscribes from a previously subscribed server's presence events. Note that you may for
801      * a short time still receive events from the server since it may take time for the
802      * unsubscribe to take effect.
803      *
804      * @param ocPresenceHandle the handle object provided by the subscribePresence call that
805      *                         identifies this subscription
806      * @throws OcException
807      */
808     public static void unsubscribePresence(
809             OcPresenceHandle ocPresenceHandle) throws OcException {
810         OcPlatform.initCheck();
811         OcPlatform.unsubscribePresence0(ocPresenceHandle);
812     }
813
814     private static native void unsubscribePresence0(
815             OcPresenceHandle ocPresenceHandle) throws OcException;
816
817     /**
818      * Creates a resource proxy object so that get/put/observe functionality can be used without
819      * discovering the object in advance. Note that the consumer of this method needs to provide
820      * all of the details required to correctly contact and observe the object. If the consumer
821      * lacks any of this information, they should discover the resource object normally.
822      * Additionally, you can only create this object if OcPlatform was initialized to be a Client
823      * or Client/Server.
824      *
825      * @param host             a string containing a resolvable host address of the server holding
826      *                         the resource
827      * @param uri              the rest of the resource's URI that will permit messages to be
828      *                         properly routed.
829      *                         Example: /a/light
830      * @param connectivityType a type of connectivity indicating the interface. Example: IPV4,
831      *                         IPV6, ALL
832      * @param isObservable     a boolean containing whether the resource supports observation
833      * @param resourceTypeList a collection of resource types implemented by the resource
834      * @param interfaceList    a collection of interfaces that the resource supports/implements
835      * @return new resource object
836      * @throws OcException
837      */
838     public static OcResource constructResourceObject(
839             String host,
840             String uri,
841             EnumSet<OcConnectivityType> connectivityTypeSet,
842             boolean isObservable,
843             List<String> resourceTypeList,
844             List<String> interfaceList) throws OcException {
845         OcPlatform.initCheck();
846         int connTypeInt = 0;
847
848         for (OcConnectivityType connType : OcConnectivityType.values()) {
849             if (connectivityTypeSet.contains(connType))
850                 connTypeInt |= connType.getValue();
851         }
852         return OcPlatform.constructResourceObject0(
853                 host,
854                 uri,
855                 connTypeInt,
856                 isObservable,
857                 resourceTypeList.toArray(new String[resourceTypeList.size()]),
858                 interfaceList.toArray(new String[interfaceList.size()])
859         );
860     }
861
862     private static native OcResource constructResourceObject0(
863             String host,
864             String uri,
865             int connectivityType,
866             boolean isObservable,
867             String[] resourceTypes,
868             String[] interfaces) throws OcException;
869
870     /**
871      * Allows application entity handler to send response to an incoming request.
872      *
873      * @param ocResourceResponse resource response
874      * @throws OcException
875      */
876     public static void sendResponse(OcResourceResponse ocResourceResponse)
877             throws OcException {
878         OcPlatform.initCheck();
879         OcPlatform.sendResponse0(ocResourceResponse);
880     }
881
882     private static native void sendResponse0(OcResourceResponse ocResourceResponse)
883             throws OcException;
884
885     /**
886      * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
887      * Event listeners are notified asynchronously
888      */
889     public interface OnResourceFoundListener {
890         public void onResourceFound(OcResource resource);
891     }
892
893     /**
894      * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call.
895      * Event listeners are notified asynchronously
896      */
897     public interface OnDeviceFoundListener {
898         public void onDeviceFound(OcRepresentation ocRepresentation);
899     }
900
901     /**
902      * An OnPlatformFoundListener can be registered via the OcPlatform.getPlatformInfo call.
903      * Event listeners are notified asynchronously
904      */
905     public interface OnPlatformFoundListener {
906         public void onPlatformFound(OcRepresentation ocRepresentation);
907     }
908
909     /**
910      * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call.
911      * Event listeners are notified asynchronously
912      */
913     public interface OnPresenceListener {
914         public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress);
915     }
916
917     /**
918      * An EntityHandler can be registered via the OcPlatform.registerResource call.
919      * Event listeners are notified asynchronously
920      */
921     public interface EntityHandler {
922         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
923     }
924
925     private static void initCheck() {
926         if (!sIsPlatformInitialized) {
927             throw new IllegalStateException("OcPlatform must be configured by making a call to " +
928                     "OcPlatform.Configure before any other API calls are permitted");
929         }
930     }
931 }