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