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