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