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