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