Remove resource directory dependency in lib OC
[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         if (ocObservationIdList == null) {
176             throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
177         }
178
179         byte[] idArr = new byte[ocObservationIdList.size()];
180         Iterator<Byte> it = ocObservationIdList.iterator();
181         int i = 0;
182         while (it.hasNext()) {
183             idArr[i++] = (byte) it.next();
184         }
185
186         OcPlatform.notifyListOfObservers2(
187                 ocResourceHandle,
188                 idArr,
189                 ocResourceResponse);
190     }
191
192     private static native void notifyListOfObservers2(
193             OcResourceHandle ocResourceHandle,
194             byte[] ocObservationIdArray,
195             OcResourceResponse ocResourceResponse) throws OcException;
196
197     /**
198      * API for notifying only specific clients that resource's attributes have changed.
199      * <p>
200      * Note: This API is for server side only.
201      * </p>
202      *
203      * @param ocResourceHandle    resource handle of the resource
204      * @param ocObservationIdList These set of ids are ones which which will be notified upon
205      *                            resource change.
206      * @param ocResourceResponse  OcResourceResponse object used by app to fill the response for
207      *                            this resource change
208      * @param qualityOfService    the quality of communication
209      * @throws OcException if failure
210      */
211     public static void notifyListOfObservers(
212             OcResourceHandle ocResourceHandle,
213             List<Byte> ocObservationIdList,
214             OcResourceResponse ocResourceResponse,
215             QualityOfService qualityOfService) throws OcException {
216         OcPlatform.initCheck();
217
218         if (ocObservationIdList == null) {
219             throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
220         }
221
222         byte[] idArr = new byte[ocObservationIdList.size()];
223         Iterator<Byte> it = ocObservationIdList.iterator();
224         int i = 0;
225         while (it.hasNext()) {
226             idArr[i++] = (byte) it.next();
227         }
228
229         OcPlatform.notifyListOfObservers3(
230                 ocResourceHandle,
231                 idArr,
232                 ocResourceResponse,
233                 qualityOfService.getValue()
234         );
235     }
236
237     private static native void notifyListOfObservers3(
238             OcResourceHandle ocResourceHandle,
239             byte[] ocObservationIdArray,
240             OcResourceResponse ocResourceResponse,
241             int qualityOfService) throws OcException;
242
243     /**
244      * API for Service and Resource Discovery
245      * <p>
246      * Note: This API is for client side only.
247      * </p>
248      *
249      * @param host                    Host Address of a service to direct resource discovery query.
250      *                                If empty, performs multicast resource discovery query
251      * @param resourceUri             name of the resource. If null or empty, performs search for all
252      *                                resource names
253      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
254      * @param onResourceFoundListener Handles events, success states and failure states.
255      * @throws OcException if failure
256      */
257     public static void findResource(
258             String host,
259             String resourceUri,
260             EnumSet<OcConnectivityType> connectivityTypeSet,
261             OnResourceFoundListener onResourceFoundListener) throws OcException {
262         OcPlatform.initCheck();
263
264         int connTypeInt = 0;
265
266         for (OcConnectivityType connType : OcConnectivityType.values()) {
267             if (connectivityTypeSet.contains(connType))
268                 connTypeInt |= connType.getValue();
269         }
270
271         OcPlatform.findResource0(
272                 host,
273                 resourceUri,
274                 connTypeInt,
275                 onResourceFoundListener
276         );
277     }
278
279     private static native void findResource0(
280             String host,
281             String resourceUri,
282             int connectivityType,
283             OnResourceFoundListener onResourceFoundListener) throws OcException;
284
285     /**
286      * API for Service and Resource Discovery.
287      * <p>
288      * Note: This API is for client side only.
289      * </p>
290      *
291      * @param host                    Host IP Address of a service to direct resource discovery query.
292      *                                If empty, performs multicast resource discovery query
293      * @param resourceUri             name of the resource. If null or empty, performs search for all
294      *                                resource names
295      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
296      * @param onResourceFoundListener Handles events, success states and failure states.
297      * @param qualityOfService        the quality of communication
298      * @throws OcException if failure
299      */
300     public static void findResource(
301             String host,
302             String resourceUri,
303             EnumSet<OcConnectivityType> connectivityTypeSet,
304             OnResourceFoundListener onResourceFoundListener,
305             QualityOfService qualityOfService) throws OcException {
306         OcPlatform.initCheck();
307
308         int connTypeInt = 0;
309
310         for (OcConnectivityType connType : OcConnectivityType.values()) {
311             if (connectivityTypeSet.contains(connType))
312                 connTypeInt |= connType.getValue();
313         }
314
315         OcPlatform.findResource1(host,
316                 resourceUri,
317                 connTypeInt,
318                 onResourceFoundListener,
319                 qualityOfService.getValue()
320         );
321     }
322
323     private static native void findResource1(
324             String host,
325             String resourceUri,
326             int connectivityType,
327             OnResourceFoundListener onResourceFoundListener,
328             int qualityOfService) throws OcException;
329
330     /**
331      * API for Device Discovery
332      *
333      * @param host                  Host IP Address. If null or empty, Multicast is performed.
334      * @param deviceUri             Uri containing address to the virtual device
335      * @param connectivityTypeSet   Set of types of connectivity. Example: IP
336      * @param onDeviceFoundListener Handles events, success states and failure states.
337      * @throws OcException if failure
338      */
339     public static void getDeviceInfo(
340             String host,
341             String deviceUri,
342             EnumSet<OcConnectivityType> connectivityTypeSet,
343             OnDeviceFoundListener onDeviceFoundListener) throws OcException {
344         OcPlatform.initCheck();
345         int connTypeInt = 0;
346
347         for (OcConnectivityType connType : OcConnectivityType.values()) {
348             if (connectivityTypeSet.contains(connType))
349                 connTypeInt |= connType.getValue();
350         }
351         OcPlatform.getDeviceInfo0(
352                 host,
353                 deviceUri,
354                 connTypeInt,
355                 onDeviceFoundListener
356         );
357     }
358
359     private static native void getDeviceInfo0(
360             String host,
361             String deviceUri,
362             int connectivityType,
363             OnDeviceFoundListener onDeviceFoundListener) throws OcException;
364
365     /**
366      * API for Device Discovery
367      *
368      * @param host                  Host IP Address. If null or empty, Multicast is performed.
369      * @param deviceUri             Uri containing address to the virtual device
370      * @param connectivityTypeSet   Set of types of connectivity. Example: IP
371      * @param onDeviceFoundListener Handles events, success states and failure states.
372      * @param qualityOfService      the quality of communication
373      * @throws OcException if failure
374      */
375     public static void getDeviceInfo(
376             String host,
377             String deviceUri,
378             EnumSet<OcConnectivityType> connectivityTypeSet,
379             OnDeviceFoundListener onDeviceFoundListener,
380             QualityOfService qualityOfService) throws OcException {
381         OcPlatform.initCheck();
382         int connTypeInt = 0;
383
384         for (OcConnectivityType connType : OcConnectivityType.values()) {
385             if (connectivityTypeSet.contains(connType))
386                 connTypeInt |= connType.getValue();
387         }
388         OcPlatform.getDeviceInfo1(
389                 host,
390                 deviceUri,
391                 connTypeInt,
392                 onDeviceFoundListener,
393                 qualityOfService.getValue()
394         );
395     }
396
397     private static native void getDeviceInfo1(
398             String host,
399             String deviceUri,
400             int connectivityType,
401             OnDeviceFoundListener onDeviceFoundListener,
402             int qualityOfService) throws OcException;
403
404     /**
405      * API for Platform Discovery
406      *
407      * @param host                    Host IP Address. If null or empty, Multicast is performed.
408      * @param platformUri             Uri containing address to the platform
409      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
410      * @param onPlatformFoundListener Handles events, success states and failure states.
411      * @throws OcException if failure
412      */
413
414     public static void getPlatformInfo(
415             String host,
416             String platformUri,
417             EnumSet<OcConnectivityType> connectivityTypeSet,
418             OnPlatformFoundListener onPlatformFoundListener) throws OcException {
419         OcPlatform.initCheck();
420         int connTypeInt = 0;
421
422         for (OcConnectivityType connType : OcConnectivityType.values()) {
423             if (connectivityTypeSet.contains(connType))
424                 connTypeInt |= connType.getValue();
425         }
426         OcPlatform.getPlatformInfo0(
427                 host,
428                 platformUri,
429                 connTypeInt,
430                 onPlatformFoundListener
431         );
432     }
433
434     private static native void getPlatformInfo0(
435             String host,
436             String platformUri,
437             int connectivityType,
438             OnPlatformFoundListener onPlatformInfoFoundListener) throws OcException;
439
440     /**
441      * API for Platform Discovery
442      *
443      * @param host                    Host IP Address. If null or empty, Multicast is performed.
444      * @param platformUri             Uri containing address to the platform
445      * @param connectivityTypeSet     Set of types of connectivity. Example: IP
446      * @param onPlatformFoundListener Handles events, success states and failure states.
447      * @param qualityOfService        the quality of communication
448      * @throws OcException if failure
449      */
450
451     public static void getPlatformInfo(
452             String host,
453             String platformUri,
454             EnumSet<OcConnectivityType> connectivityTypeSet,
455             OnPlatformFoundListener onPlatformFoundListener,
456             QualityOfService qualityOfService) throws OcException {
457         OcPlatform.initCheck();
458         int connTypeInt = 0;
459
460         for (OcConnectivityType connType : OcConnectivityType.values()) {
461             if (connectivityTypeSet.contains(connType))
462                 connTypeInt |= connType.getValue();
463         }
464         OcPlatform.getPlatformInfo1(
465                 host,
466                 platformUri,
467                 connTypeInt,
468                 onPlatformFoundListener,
469                 qualityOfService.getValue()
470         );
471     }
472
473     private static native void getPlatformInfo1(
474             String host,
475             String platformUri,
476             int connectivityType,
477             OnPlatformFoundListener onPlatformFoundListener,
478             int qualityOfService) throws OcException;
479
480     /**
481      * This API registers a resource with the server
482      * <p/>
483      * Note: This API applies to server & client side.
484      * </P>
485      *
486      * @param ocResource The instance of OcResource with all data filled
487      * @return resource handle
488      * @throws OcException if failure
489      */
490     public static OcResourceHandle registerResource(
491             OcResource ocResource) throws OcException {
492         OcPlatform.initCheck();
493         return OcPlatform.registerResource0(ocResource);
494     }
495
496     private static native OcResourceHandle registerResource0(
497             OcResource ocResource) throws OcException;
498
499     /**
500      * This API registers a resource with the server NOTE: This API applies to server side only.
501      * <p/>
502      * Note: This API applies to server side only.
503      * </P>
504      *
505      * @param resourceUri         The URI of the resource. Example: "a/light"
506      * @param resourceTypeName    The resource type. Example: "light"
507      * @param resourceInterface   The resource interface (whether it is collection etc).
508      * @param entityHandler       entity handler.
509      * @param resourcePropertySet indicates the property of the resource
510      * @return resource handle
511      * @throws OcException if failure
512      */
513     public static OcResourceHandle registerResource(
514             String resourceUri,
515             String resourceTypeName,
516             String resourceInterface,
517             EntityHandler entityHandler,
518             EnumSet<ResourceProperty> resourcePropertySet) throws OcException {
519         OcPlatform.initCheck();
520
521         int resProperty = 0;
522
523         for (ResourceProperty prop : ResourceProperty.values()) {
524             if (resourcePropertySet.contains(prop))
525                 resProperty |= prop.getValue();
526         }
527
528         return OcPlatform.registerResource1(resourceUri,
529                 resourceTypeName,
530                 resourceInterface,
531                 entityHandler,
532                 resProperty);
533     }
534
535     private static native OcResourceHandle registerResource1(
536             String resourceUri,
537             String resourceTypeName,
538             String resourceInterface,
539             EntityHandler entityHandler,
540             int resourceProperty) throws OcException;
541
542     /**
543      * Register Device Info
544      *
545      * @param ocDeviceInfo object containing all the device specific information
546      * @throws OcException if failure
547      */
548     public static void registerDeviceInfo(
549             OcDeviceInfo ocDeviceInfo) throws OcException {
550         OcPlatform.initCheck();
551         OcPlatform.registerDeviceInfo0(
552                 ocDeviceInfo.getDeviceName(),
553                 ocDeviceInfo.getDeviceTypes().toArray(
554                         new String[ocDeviceInfo.getDeviceTypes().size()]
555                 )
556         );
557     }
558
559     private static native void registerDeviceInfo0(
560             String deviceName,
561             String[] deviceTypes
562     ) throws OcException;
563
564     /**
565      * Register Platform Info
566      *
567      * @param ocPlatformInfo object containing all the platform specific information
568      * @throws OcException if failure
569      */
570     public static void registerPlatformInfo(
571             OcPlatformInfo ocPlatformInfo) throws OcException {
572         OcPlatform.initCheck();
573         OcPlatform.registerPlatformInfo0(
574                 ocPlatformInfo.getPlatformId(),
575                 ocPlatformInfo.getManufacturerName(),
576                 ocPlatformInfo.getManufacturerUrl(),
577                 ocPlatformInfo.getModelNumber(),
578                 ocPlatformInfo.getDateOfManufacture(),
579                 ocPlatformInfo.getPlatformVersion(),
580                 ocPlatformInfo.getOperatingSystemVersion(),
581                 ocPlatformInfo.getHardwareVersion(),
582                 ocPlatformInfo.getFirmwareVersion(),
583                 ocPlatformInfo.getSupportUrl(),
584                 ocPlatformInfo.getSystemTime()
585         );
586     }
587
588     private static native void registerPlatformInfo0(
589             String platformId, String manufacturerName, String manufacturerUrl,
590             String modelNumber, String dateOfManufacture, String platformVersion,
591             String operatingSystemVersion, String hardwareVersion, String firmwareVersion,
592             String supportUrl, String systemTime
593     ) throws OcException;
594
595     /**
596      * This API unregisters a resource with the server NOTE: This API applies to server side only.
597      *
598      * @param ocResourceHandle This is the resource handle which we which to unregister from the
599      *                         server
600      * @throws OcException if failure
601      */
602     public static void unregisterResource(
603             OcResourceHandle ocResourceHandle) throws OcException {
604         OcPlatform.initCheck();
605         OcPlatform.unregisterResource0(ocResourceHandle);
606     }
607
608     private static native void unregisterResource0(
609             OcResourceHandle ocResourceHandle) throws OcException;
610
611
612     /**
613      * Add a resource to a collection resource
614      *
615      * @param ocResourceCollectionHandle handle to the collection resource
616      * @param ocResourceHandle           handle to resource to be added to the collection resource
617      * @throws OcException if failure
618      */
619     public static void bindResource(
620             OcResourceHandle ocResourceCollectionHandle,
621             OcResourceHandle ocResourceHandle) throws OcException {
622         OcPlatform.initCheck();
623         OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle);
624     }
625
626     private static native void bindResource0(
627             OcResourceHandle ocResourceCollectionHandle,
628             OcResourceHandle ocResourceHandle) throws OcException;
629
630     /**
631      * Add multiple resources to a collection resource.
632      *
633      * @param ocResourceCollectionHandle handle to the collection resource
634      * @param ocResourceHandleList       reference to list of resource handles to be added to the
635      *                                   collection resource
636      * @throws OcException if failure
637      */
638     public static void bindResources(
639             OcResourceHandle ocResourceCollectionHandle,
640             List<OcResourceHandle> ocResourceHandleList) throws OcException {
641         OcPlatform.initCheck();
642
643         if (ocResourceHandleList == null) {
644             throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
645         }
646
647         OcPlatform.bindResources0(
648                 ocResourceCollectionHandle,
649                 ocResourceHandleList.toArray(
650                         new OcResourceHandle[ocResourceHandleList.size()])
651         );
652     }
653
654     private static native void bindResources0(
655             OcResourceHandle ocResourceCollectionHandle,
656             OcResourceHandle[] ocResourceHandleArray) throws OcException;
657
658     /**
659      * Unbind a resource from a collection resource.
660      *
661      * @param ocResourceCollectionHandle handle to the collection resource
662      * @param ocResourceHandle           resource handle to be unbound from the collection resource
663      * @throws OcException if failure
664      */
665     public static void unbindResource(
666             OcResourceHandle ocResourceCollectionHandle,
667             OcResourceHandle ocResourceHandle) throws OcException {
668         OcPlatform.initCheck();
669         OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle);
670     }
671
672     private static native void unbindResource0(
673             OcResourceHandle ocResourceCollectionHandle,
674             OcResourceHandle ocResourceHandle) throws OcException;
675
676     /**
677      * Unbind resources from a collection resource.
678      *
679      * @param ocResourceCollectionHandle Handle to the collection resource
680      * @param ocResourceHandleList       List of resource handles to be unbound from the collection
681      *                                   resource
682      * @throws OcException if failure
683      */
684     public static void unbindResources(
685             OcResourceHandle ocResourceCollectionHandle,
686             List<OcResourceHandle> ocResourceHandleList) throws OcException {
687         OcPlatform.initCheck();
688
689         if (ocResourceHandleList == null) {
690             throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
691         }
692
693         OcPlatform.unbindResources0(
694                 ocResourceCollectionHandle,
695                 ocResourceHandleList.toArray(
696                         new OcResourceHandle[ocResourceHandleList.size()])
697         );
698     }
699
700     private static native void unbindResources0(
701             OcResourceHandle ocResourceCollectionHandle,
702             OcResourceHandle[] ocResourceHandleArray) throws OcException;
703
704     /**
705      * Binds a type to a particular resource
706      *
707      * @param ocResourceHandle handle to the resource
708      * @param resourceTypeName new typename to bind to the resource
709      * @throws OcException if failure
710      */
711     public static void bindTypeToResource(
712             OcResourceHandle ocResourceHandle,
713             String resourceTypeName) throws OcException {
714         OcPlatform.initCheck();
715         OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName);
716     }
717
718     private static native void bindTypeToResource0(
719             OcResourceHandle ocResourceHandle,
720             String resourceTypeName) throws OcException;
721
722     /**
723      * Binds an interface to a particular resource
724      *
725      * @param ocResourceHandle      handle to the resource
726      * @param resourceInterfaceName new interface to bind to the resource
727      * @throws OcException if failure
728      */
729     public static void bindInterfaceToResource(
730             OcResourceHandle ocResourceHandle,
731             String resourceInterfaceName) throws OcException {
732         OcPlatform.initCheck();
733         OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName);
734     }
735
736     private static native void bindInterfaceToResource0(
737             OcResourceHandle ocResourceHandle,
738             String resourceInterfaceName) throws OcException;
739
740     /**
741      * Start Presence announcements.
742      *
743      * @param ttl time to live in seconds
744      * @throws OcException if failure
745      */
746     public static void startPresence(int ttl) throws OcException {
747         OcPlatform.initCheck();
748         OcPlatform.startPresence0(ttl);
749     }
750
751     private static native void startPresence0(int ttl) throws OcException;
752
753     /**
754      * Stop Presence announcements.
755      *
756      * @throws OcException if failure
757      */
758     public static void stopPresence() throws OcException {
759         OcPlatform.initCheck();
760         OcPlatform.stopPresence0();
761     }
762
763     private static native void stopPresence0() throws OcException;
764
765     /**
766      * Subscribes to a server's presence change events. By making this subscription, every time a
767      * server adds/removes/alters a resource, starts or is intentionally stopped
768      *
769      * @param host                The IP address/addressable name of the server to subscribe to
770      * @param connectivityTypeSet Set of types of connectivity. Example: IP
771      * @param onPresenceListener  listener that will receive notifications/subscription events
772      * @return a handle object that can be used to identify this subscription request. It can be
773      * used to unsubscribe from these events in the future
774      * @throws OcException if failure
775      */
776     public static OcPresenceHandle subscribePresence(
777             String host,
778             EnumSet<OcConnectivityType> connectivityTypeSet,
779             OnPresenceListener onPresenceListener) throws OcException {
780         OcPlatform.initCheck();
781         int connTypeInt = 0;
782
783         for (OcConnectivityType connType : OcConnectivityType.values()) {
784             if (connectivityTypeSet.contains(connType))
785                 connTypeInt |= connType.getValue();
786         }
787         return OcPlatform.subscribePresence0(
788                 host,
789                 connTypeInt,
790                 onPresenceListener
791         );
792     }
793
794     private static native OcPresenceHandle subscribePresence0(
795             String host,
796             int connectivityType,
797             OnPresenceListener onPresenceListener) throws OcException;
798
799     /**
800      * Subscribes to a server's presence change events. By making this subscription, every time a
801      * server adds/removes/alters a resource, starts or is intentionally stopped
802      *
803      * @param host                The IP address/addressable name of the server to subscribe to
804      * @param resourceType        a resource type specified as a filter for subscription events.
805      * @param connectivityTypeSet Set of types of connectivity. Example: IP
806      * @param onPresenceListener  listener that will receive notifications/subscription events
807      * @return a handle object that can be used to identify this subscription request. It can be
808      * used to unsubscribe from these events in the future
809      * @throws OcException if failure
810      */
811     public static OcPresenceHandle subscribePresence(
812             String host,
813             String resourceType,
814             EnumSet<OcConnectivityType> connectivityTypeSet,
815             OnPresenceListener onPresenceListener) throws OcException {
816         OcPlatform.initCheck();
817         int connTypeInt = 0;
818
819         for (OcConnectivityType connType : OcConnectivityType.values()) {
820             if (connectivityTypeSet.contains(connType))
821                 connTypeInt |= connType.getValue();
822         }
823         return OcPlatform.subscribePresence1(
824                 host,
825                 resourceType,
826                 connTypeInt,
827                 onPresenceListener);
828     }
829
830     private static native OcPresenceHandle subscribePresence1(
831             String host,
832             String resourceType,
833             int connectivityType,
834             OnPresenceListener onPresenceListener) throws OcException;
835
836     /**
837      * Unsubscribes from a previously subscribed server's presence events. Note that you may for
838      * a short time still receive events from the server since it may take time for the
839      * unsubscribe to take effect.
840      *
841      * @param ocPresenceHandle the handle object provided by the subscribePresence call that
842      *                         identifies this subscription
843      * @throws OcException if failure
844      */
845     public static void unsubscribePresence(
846             OcPresenceHandle ocPresenceHandle) throws OcException {
847         OcPlatform.initCheck();
848         OcPlatform.unsubscribePresence0(ocPresenceHandle);
849     }
850
851     private static native void unsubscribePresence0(
852             OcPresenceHandle ocPresenceHandle) throws OcException;
853
854     /**
855      * Subscribes to a server's device presence change events.
856      *
857      * @param host                The IP address/addressable name of the server to subscribe to.
858      * @param di                  Vector which can have the devices id.
859      * @param connectivityTypeSet Set of connectivity types, e.g. IP.
860      * @param onObserveListener   The handler method will be invoked with a map
861      *                            of attribute name and values.
862      * @return a handle object that can be used to identify this subscription request.
863      *         It can be used to unsubscribe from these events in the future.
864      * @throws OcException if failure.
865      */
866     public static OcPresenceHandle subscribeDevicePresence(
867             String host,
868             List<String> di,
869             EnumSet<OcConnectivityType> connectivityTypeSet,
870             OcResource.OnObserveListener onObserveListener) throws OcException {
871         OcPlatform.initCheck();
872         int connTypeInt = 0;
873
874         for (OcConnectivityType connType : OcConnectivityType.values()) {
875             if (connectivityTypeSet.contains(connType))
876                 connTypeInt |= connType.getValue();
877         }
878         return OcPlatform.subscribeDevicePresence0(
879                 host,
880                 di.toArray(new String[di.size()]),
881                 connTypeInt,
882                 onObserveListener);
883     }
884
885     private static native OcPresenceHandle subscribeDevicePresence0(
886             String host,
887             String[] di,
888             int connectivityType,
889             OcResource.OnObserveListener onObserveListener) throws OcException;
890
891     /**
892      * Creates a resource proxy object so that get/put/observe functionality can be used without
893      * discovering the object in advance. Note that the consumer of this method needs to provide
894      * all of the details required to correctly contact and observe the object. If the consumer
895      * lacks any of this information, they should discover the resource object normally.
896      * Additionally, you can only create this object if OcPlatform was initialized to be a Client
897      * or Client/Server.
898      *
899      * @param host                a string containing a resolvable host address of the server holding
900      *                            the resource
901      * @param uri                 the rest of the resource's URI that will permit messages to be
902      *                            properly routed.
903      *                            Example: /a/light
904      * @param connectivityTypeSet Set of types of connectivity. Example: IP
905      * @param isObservable        a boolean containing whether the resource supports observation
906      * @param resourceTypeList    a collection of resource types implemented by the resource
907      * @param interfaceList       a collection of interfaces that the resource supports/implements
908      * @return new resource object
909      * @throws OcException if failure
910      */
911     public static OcResource constructResourceObject(
912             String host,
913             String uri,
914             EnumSet<OcConnectivityType> connectivityTypeSet,
915             boolean isObservable,
916             List<String> resourceTypeList,
917             List<String> interfaceList) throws OcException {
918         OcPlatform.initCheck();
919         int connTypeInt = 0;
920
921         for (OcConnectivityType connType : OcConnectivityType.values()) {
922             if (connectivityTypeSet.contains(connType))
923                 connTypeInt |= connType.getValue();
924         }
925         return OcPlatform.constructResourceObject0(
926                 host,
927                 uri,
928                 connTypeInt,
929                 isObservable,
930                 resourceTypeList.toArray(new String[resourceTypeList.size()]),
931                 interfaceList.toArray(new String[interfaceList.size()])
932         );
933     }
934
935     private static native OcResource constructResourceObject0(
936             String host,
937             String uri,
938             int connectivityType,
939             boolean isObservable,
940             String[] resourceTypes,
941             String[] interfaces) throws OcException;
942
943     /**
944      * Allows application entity handler to send response to an incoming request.
945      *
946      * @param ocResourceResponse resource response
947      * @throws OcException if failure
948      */
949     public static void sendResponse(OcResourceResponse ocResourceResponse)
950             throws OcException {
951         OcPlatform.initCheck();
952         OcPlatform.sendResponse0(ocResourceResponse);
953     }
954
955     private static native void sendResponse0(OcResourceResponse ocResourceResponse)
956             throws OcException;
957
958     /**
959      *  Method to find all devices which are eligible for direct pairing and return the list.
960      *
961      *  @param timeout timeout for discovering direct pair devices.
962      *  @param FindDirectPairingListener Callback function, which will receive the list of direct
963      *                                  pairable devices.
964      *  @throws OcException
965      */
966    public static native void findDirectPairingDevices(int timeout,
967             FindDirectPairingListener onFindDirectPairingListener) throws OcException;
968
969     /**
970      *  Method to get list of all paired devices for a given device.
971      *
972      *  @param GetDirectPairedListener Callback function, which will receive the list of direct
973      *                                 paired devices.
974      *  @throws OcException
975      */
976     public native void getDirectPairedDevices(GetDirectPairedListener onGetDirectPairedListener)
977         throws OcException;
978
979     /**
980      *  Method to perform direct pairing between two devices.
981      *
982      *  @param peer  Target peer
983      *  @param prmType Pairing Method to be used for Pairing
984      *  @param pin pin
985      *  @param DirectPairingListener Callback function, which will be called after
986      *                                      completion of direct pairing.
987      *  @throws OcException
988      */
989     public static void doDirectPairing(
990             OcDirectPairDevice peer,
991             OcPrmType prmType,
992             String pin,
993             DirectPairingListener onDirectPairingListener) throws OcException {
994
995         OcPlatform.doDirectPairing0(
996                 peer,
997                 prmType.getValue(),
998                 pin,
999                 onDirectPairingListener
1000                 );
1001     }
1002
1003     private static native void doDirectPairing0(OcDirectPairDevice peer,
1004             int pmSel, String pinNumber, DirectPairingListener onDirectPairingListener)
1005     throws OcException;
1006
1007     /**
1008      * An FindDirectPairingListener can be registered via the OcPlatform.findDirectPairingDevices call.
1009      * Event listeners are notified asynchronously
1010      */
1011     public interface FindDirectPairingListener {
1012         public void onFindDirectPairingListener(List<OcDirectPairDevice> ocPairedDeviceList);
1013     }
1014
1015     /**
1016      * Listerner to Get List of already Direct Paired devices.
1017      * An GetDirectPairedListener can be registered via the OcPlatform.getDirectPairedDevices call.
1018      * Event listeners are notified asynchronously
1019      */
1020     public interface GetDirectPairedListener {
1021         public void onGetDirectPairedListener(List<OcDirectPairDevice> ocPairedDeviceList);
1022     }
1023
1024     /**
1025      * Listner to get result of doDirectPairing.
1026      * An DirectPairingListener can be registered via the OcPlatform.doDirectPairing call.
1027      * Event listeners are notified asynchronously
1028      */
1029     public interface DirectPairingListener {
1030         public void onDirectPairingListener(String devId, int result);
1031     }
1032
1033     /**
1034      * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
1035      * Event listeners are notified asynchronously
1036      */
1037     public interface OnResourceFoundListener {
1038         public void onResourceFound(OcResource resource);
1039         public void onFindResourceFailed(Throwable ex, String uri);
1040     }
1041
1042     /**
1043      * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call.
1044      * Event listeners are notified asynchronously
1045      */
1046     public interface OnDeviceFoundListener {
1047         public void onDeviceFound(OcRepresentation ocRepresentation);
1048     }
1049
1050     /**
1051      * An OnPlatformFoundListener can be registered via the OcPlatform.getPlatformInfo call.
1052      * Event listeners are notified asynchronously
1053      */
1054     public interface OnPlatformFoundListener {
1055         public void onPlatformFound(OcRepresentation ocRepresentation);
1056     }
1057
1058     /**
1059      * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call.
1060      * Event listeners are notified asynchronously
1061      */
1062     public interface OnPresenceListener {
1063         public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress);
1064     }
1065
1066     /**
1067      * An EntityHandler can be registered via the OcPlatform.registerResource call.
1068      * Event listeners are notified asynchronously
1069      *
1070      * @note entityhandler callback :
1071      * When you set specific return value like EntityHandlerResult.OK, SLOW
1072      * and etc in entity handler callback,
1073      * ocstack will be not send response automatically to client
1074      * except for error return value like EntityHandlerResult.ERROR
1075      * If you want to send response to client with specific result,
1076      * sendResponse API should be called with the result value.
1077      */
1078     public interface EntityHandler {
1079         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
1080     }
1081
1082     private static void initCheck() {
1083         if (!sIsPlatformInitialized) {
1084             throw new IllegalStateException("OcPlatform must be configured by making a call to " +
1085                     "OcPlatform.Configure before any other API calls are permitted");
1086         }
1087     }
1088
1089     /**
1090      * Gets platform quality of service
1091      *
1092      * @return quality of service
1093      */
1094     public static QualityOfService getPlatformQualityOfService() {
1095         OcPlatform.initCheck();
1096         return sPlatformQualityOfService;
1097     }
1098
1099     /**
1100      * Create an account manager object that can be used for doing request to account server.
1101      * You can only create this object if OCPlatform was initialized to be a Client or
1102      * Client/Server. Otherwise, this will return an empty shared ptr.
1103      *
1104      * @note For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
1105      *       methods of this object to work since device id is not generated on Client mode.
1106      *
1107      * @param host                Host IP Address of a account server.
1108      * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
1109      * @return new AccountManager object
1110      * @throws OcException if failure
1111      */
1112     public static OcAccountManager constructAccountManagerObject(
1113             String host,
1114             EnumSet<OcConnectivityType> connectivityTypeSet) throws OcException {
1115         OcPlatform.initCheck();
1116         int connTypeInt = 0;
1117
1118         for (OcConnectivityType connType : OcConnectivityType.values()) {
1119             if (connectivityTypeSet.contains(connType))
1120             {
1121                 connTypeInt |= connType.getValue();
1122             }
1123         }
1124         return OcPlatform.constructAccountManagerObject0(
1125                 host,
1126                 connTypeInt
1127                 );
1128     }
1129
1130     private static native OcAccountManager constructAccountManagerObject0(
1131             String host,
1132             int connectivityType) throws OcException;
1133     /**
1134      * Method to get device Id in byte array.
1135      * @return My DeviceId.
1136      */
1137     public static native byte[] getDeviceId();
1138
1139     /**
1140      * Method to set DeviceId.
1141      */
1142     public static native void setDeviceId(byte[] deviceId) throws OcException;
1143 }