2 *******************************************************************
4 * Copyright 2015 Intel Corporation.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base;
25 import org.iotivity.ca.CaInterface;
26 import org.iotivity.base.BuildConfig;
28 import java.util.EnumSet;
29 import java.util.Iterator;
30 import java.util.List;
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.
37 public final class OcPlatform {
40 System.loadLibrary("oc_logger");
41 System.loadLibrary("octbstack");
42 System.loadLibrary("connectivity_abstraction");
43 System.loadLibrary("oc");
44 if (0 != BuildConfig.SECURED)
46 System.loadLibrary("ocprovision");
48 System.loadLibrary("ocstack-jni");
54 public static final String DEFAULT_INTERFACE = "oic.if.baseline";
57 * Used in discovering (GET) links to other resources of a collection
59 public static final String LINK_INTERFACE = "oic.if.ll";
62 * Used in GET, PUT, POST, DELETE methods on links to other resources of a collection
64 public static final String BATCH_INTERFACE = "oic.if.b";
67 * Used in GET, PUT, POST methods on links to other remote resources of a group
69 public static final String GROUP_INTERFACE = "oic.mi.grp";
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";
77 private static volatile boolean sIsPlatformInitialized = false;
78 private static QualityOfService sPlatformQualityOfService = QualityOfService.NA;
80 private static volatile boolean sIsStopPlatform = false;
82 private OcPlatform() {
86 * API for setting the configuration of the OcPlatform.
88 * Note: Any calls made to this AFTER the first call to OcPlatform.Configure will have no affect
91 * @param platformConfig platform configuration
93 public synchronized static void Configure(PlatformConfig platformConfig) {
97 sIsStopPlatform = false;
100 if (!sIsPlatformInitialized) {
101 CaInterface.initialize(platformConfig.getActivity(), platformConfig.getContext());
103 sPlatformQualityOfService = platformConfig.getQualityOfService();
105 OcPlatform.configure(
106 platformConfig.getServiceType().getValue(),
107 platformConfig.getModeType().getValue(),
108 platformConfig.getIpAddress(),
109 platformConfig.getPort(),
110 platformConfig.getQualityOfService().getValue(),
111 platformConfig.getSvrDbPath(),
112 platformConfig.getDBDefaultPath(),
113 platformConfig.getDBRescuePath(),
114 platformConfig.getKeySize(),
115 platformConfig.getKey(),
116 platformConfig.getAvailableTransportType()
119 sIsPlatformInitialized = true;
123 private static native void configure(int serviceType,
127 int qualityOfService,
129 String dbPathDefault,
136 * API for stop all process of the OcPlatform.
137 * All of threads and memory will be terminated by this API.
138 * Iotivity Core can be started again through Configure(PlatformConfig platformConfig) API.
139 * Both Configure and Shutdown API is filtering for duplicated calling even while processing.
141 * Note: This API is for both server and client side.
144 public synchronized static void Shutdown() {
145 if (!sIsStopPlatform)
148 sIsStopPlatform = true;
149 sIsPlatformInitialized = false;
153 private static native void stop();
154 private static native void start();
157 * API for notifying base that resource's attributes have changed.
159 * Note: This API is for server side only.
162 * @param ocResourceHandle resource handle of the resource
163 * @throws OcException if failure
165 public static void notifyAllObservers(
166 OcResourceHandle ocResourceHandle) throws OcException {
167 OcPlatform.initCheck();
168 OcPlatform.notifyAllObservers0(ocResourceHandle);
171 private static native void notifyAllObservers0(
172 OcResourceHandle ocResourceHandle) throws OcException;
175 * API for notifying base that resource's attributes have changed.
177 * Note: This API is for server side only.
180 * @param ocResourceHandle resource handle of the resource
181 * @param qualityOfService the quality of communication
182 * @throws OcException if failure
184 public static void notifyAllObservers(
185 OcResourceHandle ocResourceHandle,
186 QualityOfService qualityOfService) throws OcException {
187 OcPlatform.initCheck();
188 OcPlatform.notifyAllObservers1(ocResourceHandle, qualityOfService.getValue());
191 private static native void notifyAllObservers1(
192 OcResourceHandle ocResourceHandle,
193 int qualityOfService) throws OcException;
196 * API for notifying only specific clients that resource's attributes have changed.
198 * Note: This API is for server side only.
201 * @param ocResourceHandle resource handle of the resource
202 * @param ocObservationIdList These set of ids are ones which which will be notified upon
204 * @param ocResourceResponse OcResourceResponse object used by app to fill the response for
205 * this resource change
206 * @throws OcException if failure
208 public static void notifyListOfObservers(
209 OcResourceHandle ocResourceHandle,
210 List<Byte> ocObservationIdList,
211 OcResourceResponse ocResourceResponse) throws OcException {
212 OcPlatform.initCheck();
214 if (ocObservationIdList == null) {
215 throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
218 byte[] idArr = new byte[ocObservationIdList.size()];
219 Iterator<Byte> it = ocObservationIdList.iterator();
221 while (it.hasNext()) {
222 idArr[i++] = (byte) it.next();
225 OcPlatform.notifyListOfObservers2(
231 private static native void notifyListOfObservers2(
232 OcResourceHandle ocResourceHandle,
233 byte[] ocObservationIdArray,
234 OcResourceResponse ocResourceResponse) throws OcException;
237 * API for notifying only specific clients that resource's attributes have changed.
239 * Note: This API is for server side only.
242 * @param ocResourceHandle resource handle of the resource
243 * @param ocObservationIdList These set of ids are ones which which will be notified upon
245 * @param ocResourceResponse OcResourceResponse object used by app to fill the response for
246 * this resource change
247 * @param qualityOfService the quality of communication
248 * @throws OcException if failure
250 public static void notifyListOfObservers(
251 OcResourceHandle ocResourceHandle,
252 List<Byte> ocObservationIdList,
253 OcResourceResponse ocResourceResponse,
254 QualityOfService qualityOfService) throws OcException {
255 OcPlatform.initCheck();
257 if (ocObservationIdList == null) {
258 throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
261 byte[] idArr = new byte[ocObservationIdList.size()];
262 Iterator<Byte> it = ocObservationIdList.iterator();
264 while (it.hasNext()) {
265 idArr[i++] = (byte) it.next();
268 OcPlatform.notifyListOfObservers3(
272 qualityOfService.getValue()
276 private static native void notifyListOfObservers3(
277 OcResourceHandle ocResourceHandle,
278 byte[] ocObservationIdArray,
279 OcResourceResponse ocResourceResponse,
280 int qualityOfService) throws OcException;
283 * API for Service and Resource Discovery
285 * Note: This API is for client side only.
288 * @param host Host Address of a service to direct resource discovery query.
289 * If empty, performs multicast resource discovery query
290 * @param resourceUri name of the resource. If null or empty, performs search for all
292 * @param connectivityTypeSet Set of types of connectivity. Example: IP
293 * @param onResourceFoundListener Handles events, success states and failure states.
294 * @throws OcException if failure
296 public static void findResource(
299 EnumSet<OcConnectivityType> connectivityTypeSet,
300 OnResourceFoundListener onResourceFoundListener) throws OcException {
301 OcPlatform.initCheck();
305 for (OcConnectivityType connType : OcConnectivityType.values()) {
306 if (connectivityTypeSet.contains(connType))
307 connTypeInt |= connType.getValue();
310 OcPlatform.findResource0(
314 onResourceFoundListener
318 private static native void findResource0(
321 int connectivityType,
322 OnResourceFoundListener onResourceFoundListener) throws OcException;
325 * API for Service and Resource Discovery.
327 * Note: This API is for client side only.
330 * @param host Host IP Address of a service to direct resource discovery query.
331 * If empty, performs multicast resource discovery query
332 * @param resourceUri name of the resource. If null or empty, performs search for all
334 * @param connectivityTypeSet Set of types of connectivity. Example: IP
335 * @param onResourceFoundListener Handles events, success states and failure states.
336 * @param qualityOfService the quality of communication
337 * @throws OcException if failure
339 public static void findResource(
342 EnumSet<OcConnectivityType> connectivityTypeSet,
343 OnResourceFoundListener onResourceFoundListener,
344 QualityOfService qualityOfService) throws OcException {
345 OcPlatform.initCheck();
349 for (OcConnectivityType connType : OcConnectivityType.values()) {
350 if (connectivityTypeSet.contains(connType))
351 connTypeInt |= connType.getValue();
354 OcPlatform.findResource1(host,
357 onResourceFoundListener,
358 qualityOfService.getValue()
362 private static native void findResource1(
365 int connectivityType,
366 OnResourceFoundListener onResourceFoundListener,
367 int qualityOfService) throws OcException;
370 * API for Device Discovery
372 * @param host Host IP Address. If null or empty, Multicast is performed.
373 * @param deviceUri Uri containing address to the virtual device
374 * @param connectivityTypeSet Set of types of connectivity. Example: IP
375 * @param onDeviceFoundListener Handles events, success states and failure states.
376 * @throws OcException if failure
378 public static void getDeviceInfo(
381 EnumSet<OcConnectivityType> connectivityTypeSet,
382 OnDeviceFoundListener onDeviceFoundListener) throws OcException {
383 OcPlatform.initCheck();
386 for (OcConnectivityType connType : OcConnectivityType.values()) {
387 if (connectivityTypeSet.contains(connType))
388 connTypeInt |= connType.getValue();
390 OcPlatform.getDeviceInfo0(
394 onDeviceFoundListener
398 private static native void getDeviceInfo0(
401 int connectivityType,
402 OnDeviceFoundListener onDeviceFoundListener) throws OcException;
405 * API for Device Discovery
407 * @param host Host IP Address. If null or empty, Multicast is performed.
408 * @param deviceUri Uri containing address to the virtual device
409 * @param connectivityTypeSet Set of types of connectivity. Example: IP
410 * @param onDeviceFoundListener Handles events, success states and failure states.
411 * @param qualityOfService the quality of communication
412 * @throws OcException if failure
414 public static void getDeviceInfo(
417 EnumSet<OcConnectivityType> connectivityTypeSet,
418 OnDeviceFoundListener onDeviceFoundListener,
419 QualityOfService qualityOfService) throws OcException {
420 OcPlatform.initCheck();
423 for (OcConnectivityType connType : OcConnectivityType.values()) {
424 if (connectivityTypeSet.contains(connType))
425 connTypeInt |= connType.getValue();
427 OcPlatform.getDeviceInfo1(
431 onDeviceFoundListener,
432 qualityOfService.getValue()
436 private static native void getDeviceInfo1(
439 int connectivityType,
440 OnDeviceFoundListener onDeviceFoundListener,
441 int qualityOfService) throws OcException;
444 * API for Platform Discovery
446 * @param host Host IP Address. If null or empty, Multicast is performed.
447 * @param platformUri Uri containing address to the platform
448 * @param connectivityTypeSet Set of types of connectivity. Example: IP
449 * @param onPlatformFoundListener Handles events, success states and failure states.
450 * @throws OcException if failure
453 public static void getPlatformInfo(
456 EnumSet<OcConnectivityType> connectivityTypeSet,
457 OnPlatformFoundListener onPlatformFoundListener) throws OcException {
458 OcPlatform.initCheck();
461 for (OcConnectivityType connType : OcConnectivityType.values()) {
462 if (connectivityTypeSet.contains(connType))
463 connTypeInt |= connType.getValue();
465 OcPlatform.getPlatformInfo0(
469 onPlatformFoundListener
473 private static native void getPlatformInfo0(
476 int connectivityType,
477 OnPlatformFoundListener onPlatformInfoFoundListener) throws OcException;
480 * API for Platform Discovery
482 * @param host Host IP Address. If null or empty, Multicast is performed.
483 * @param platformUri Uri containing address to the platform
484 * @param connectivityTypeSet Set of types of connectivity. Example: IP
485 * @param onPlatformFoundListener Handles events, success states and failure states.
486 * @param qualityOfService the quality of communication
487 * @throws OcException if failure
490 public static void getPlatformInfo(
493 EnumSet<OcConnectivityType> connectivityTypeSet,
494 OnPlatformFoundListener onPlatformFoundListener,
495 QualityOfService qualityOfService) throws OcException {
496 OcPlatform.initCheck();
499 for (OcConnectivityType connType : OcConnectivityType.values()) {
500 if (connectivityTypeSet.contains(connType))
501 connTypeInt |= connType.getValue();
503 OcPlatform.getPlatformInfo1(
507 onPlatformFoundListener,
508 qualityOfService.getValue()
512 private static native void getPlatformInfo1(
515 int connectivityType,
516 OnPlatformFoundListener onPlatformFoundListener,
517 int qualityOfService) throws OcException;
520 * This API registers a resource with the server
522 * Note: This API applies to server & client side.
525 * @param ocResource The instance of OcResource with all data filled
526 * @return resource handle
527 * @throws OcException if failure
529 public static OcResourceHandle registerResource(
530 OcResource ocResource) throws OcException {
531 OcPlatform.initCheck();
532 return OcPlatform.registerResource0(ocResource);
535 private static native OcResourceHandle registerResource0(
536 OcResource ocResource) throws OcException;
539 * This API registers a resource with the server NOTE: This API applies to server side only.
541 * Note: This API applies to server side only.
544 * @param resourceUri The URI of the resource. Example: "a/light"
545 * @param resourceTypeName The resource type. Example: "light"
546 * @param resourceInterface The resource interface (whether it is collection etc).
547 * @param entityHandler entity handler.
548 * @param resourcePropertySet indicates the property of the resource
549 * @return resource handle
550 * @throws OcException if failure
552 public static OcResourceHandle registerResource(
554 String resourceTypeName,
555 String resourceInterface,
556 EntityHandler entityHandler,
557 EnumSet<ResourceProperty> resourcePropertySet) throws OcException {
558 OcPlatform.initCheck();
562 for (ResourceProperty prop : ResourceProperty.values()) {
563 if (resourcePropertySet.contains(prop))
564 resProperty |= prop.getValue();
567 return OcPlatform.registerResource1(resourceUri,
574 private static native OcResourceHandle registerResource1(
576 String resourceTypeName,
577 String resourceInterface,
578 EntityHandler entityHandler,
579 int resourceProperty) throws OcException;
582 * Register Device Info
584 * @param ocDeviceInfo object containing all the device specific information
585 * @throws OcException if failure
587 public static void registerDeviceInfo(
588 OcDeviceInfo ocDeviceInfo) throws OcException {
589 OcPlatform.initCheck();
590 OcPlatform.registerDeviceInfo0(
591 ocDeviceInfo.getDeviceName(),
592 ocDeviceInfo.getDeviceTypes().toArray(
593 new String[ocDeviceInfo.getDeviceTypes().size()]
598 private static native void registerDeviceInfo0(
601 ) throws OcException;
606 * @param ocDeviceInfo object containing all the device specific information
607 * @throws OcException if failure
609 public static void setPropertyValue(
610 int path, String propName, String propValue) throws OcException {
611 OcPlatform.initCheck();
612 OcPlatform.setPropertyValue1(path, propName, propValue);
615 public static void setPropertyValue(
616 int path, String propName, List<String> propValue) throws OcException {
617 OcPlatform.initCheck();
618 OcPlatform.setPropertyValue0(path, propName, propValue.toArray(new String[propValue.size()]));
621 public static void getPropertyValue(
622 int path, String propName, String propValue) throws OcException {
623 OcPlatform.initCheck();
624 OcPlatform.getPropertyValue0(path, propName, propValue);
627 private static native void setPropertyValue1(
631 ) throws OcException;
634 private static native void setPropertyValue0(
638 ) throws OcException;
640 private static native void getPropertyValue0(
644 ) throws OcException;
647 * Register Platform Info
649 * @param ocPlatformInfo object containing all the platform specific information
650 * @throws OcException if failure
652 public static void registerPlatformInfo(
653 OcPlatformInfo ocPlatformInfo) throws OcException {
654 OcPlatform.initCheck();
655 OcPlatform.registerPlatformInfo0(
656 ocPlatformInfo.getPlatformId(),
657 ocPlatformInfo.getManufacturerName(),
658 ocPlatformInfo.getManufacturerUrl(),
659 ocPlatformInfo.getModelNumber(),
660 ocPlatformInfo.getDateOfManufacture(),
661 ocPlatformInfo.getPlatformVersion(),
662 ocPlatformInfo.getOperatingSystemVersion(),
663 ocPlatformInfo.getHardwareVersion(),
664 ocPlatformInfo.getFirmwareVersion(),
665 ocPlatformInfo.getSupportUrl(),
666 ocPlatformInfo.getSystemTime()
670 private static native void registerPlatformInfo0(
671 String platformId, String manufacturerName, String manufacturerUrl,
672 String modelNumber, String dateOfManufacture, String platformVersion,
673 String operatingSystemVersion, String hardwareVersion, String firmwareVersion,
674 String supportUrl, String systemTime
675 ) throws OcException;
678 * This API unregisters a resource with the server NOTE: This API applies to server side only.
680 * @param ocResourceHandle This is the resource handle which we which to unregister from the
682 * @throws OcException if failure
684 public static void unregisterResource(
685 OcResourceHandle ocResourceHandle) throws OcException {
686 OcPlatform.initCheck();
687 OcPlatform.unregisterResource0(ocResourceHandle);
690 private static native void unregisterResource0(
691 OcResourceHandle ocResourceHandle) throws OcException;
695 * Add a resource to a collection resource
697 * @param ocResourceCollectionHandle handle to the collection resource
698 * @param ocResourceHandle handle to resource to be added to the collection resource
699 * @throws OcException if failure
701 public static void bindResource(
702 OcResourceHandle ocResourceCollectionHandle,
703 OcResourceHandle ocResourceHandle) throws OcException {
704 OcPlatform.initCheck();
705 OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle);
708 private static native void bindResource0(
709 OcResourceHandle ocResourceCollectionHandle,
710 OcResourceHandle ocResourceHandle) throws OcException;
713 * Add multiple resources to a collection resource.
715 * @param ocResourceCollectionHandle handle to the collection resource
716 * @param ocResourceHandleList reference to list of resource handles to be added to the
717 * collection resource
718 * @throws OcException if failure
720 public static void bindResources(
721 OcResourceHandle ocResourceCollectionHandle,
722 List<OcResourceHandle> ocResourceHandleList) throws OcException {
723 OcPlatform.initCheck();
725 if (ocResourceHandleList == null) {
726 throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
729 OcPlatform.bindResources0(
730 ocResourceCollectionHandle,
731 ocResourceHandleList.toArray(
732 new OcResourceHandle[ocResourceHandleList.size()])
736 private static native void bindResources0(
737 OcResourceHandle ocResourceCollectionHandle,
738 OcResourceHandle[] ocResourceHandleArray) throws OcException;
741 * Unbind a resource from a collection resource.
743 * @param ocResourceCollectionHandle handle to the collection resource
744 * @param ocResourceHandle resource handle to be unbound from the collection resource
745 * @throws OcException if failure
747 public static void unbindResource(
748 OcResourceHandle ocResourceCollectionHandle,
749 OcResourceHandle ocResourceHandle) throws OcException {
750 OcPlatform.initCheck();
751 OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle);
754 private static native void unbindResource0(
755 OcResourceHandle ocResourceCollectionHandle,
756 OcResourceHandle ocResourceHandle) throws OcException;
759 * Unbind resources from a collection resource.
761 * @param ocResourceCollectionHandle Handle to the collection resource
762 * @param ocResourceHandleList List of resource handles to be unbound from the collection
764 * @throws OcException if failure
766 public static void unbindResources(
767 OcResourceHandle ocResourceCollectionHandle,
768 List<OcResourceHandle> ocResourceHandleList) throws OcException {
769 OcPlatform.initCheck();
771 if (ocResourceHandleList == null) {
772 throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
775 OcPlatform.unbindResources0(
776 ocResourceCollectionHandle,
777 ocResourceHandleList.toArray(
778 new OcResourceHandle[ocResourceHandleList.size()])
782 private static native void unbindResources0(
783 OcResourceHandle ocResourceCollectionHandle,
784 OcResourceHandle[] ocResourceHandleArray) throws OcException;
787 * Binds a type to a particular resource
789 * @param ocResourceHandle handle to the resource
790 * @param resourceTypeName new typename to bind to the resource
791 * @throws OcException if failure
793 public static void bindTypeToResource(
794 OcResourceHandle ocResourceHandle,
795 String resourceTypeName) throws OcException {
796 OcPlatform.initCheck();
797 OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName);
800 private static native void bindTypeToResource0(
801 OcResourceHandle ocResourceHandle,
802 String resourceTypeName) throws OcException;
805 * Binds an interface to a particular resource
807 * @param ocResourceHandle handle to the resource
808 * @param resourceInterfaceName new interface to bind to the resource
809 * @throws OcException if failure
811 public static void bindInterfaceToResource(
812 OcResourceHandle ocResourceHandle,
813 String resourceInterfaceName) throws OcException {
814 OcPlatform.initCheck();
815 OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName);
818 private static native void bindInterfaceToResource0(
819 OcResourceHandle ocResourceHandle,
820 String resourceInterfaceName) throws OcException;
823 * Start Presence announcements.
825 * @param ttl time to live in seconds
826 * @throws OcException if failure
828 public static void startPresence(int ttl) throws OcException {
829 OcPlatform.initCheck();
830 OcPlatform.startPresence0(ttl);
833 private static native void startPresence0(int ttl) throws OcException;
836 * Stop Presence announcements.
838 * @throws OcException if failure
840 public static void stopPresence() throws OcException {
841 OcPlatform.initCheck();
842 OcPlatform.stopPresence0();
845 private static native void stopPresence0() throws OcException;
848 * Subscribes to a server's presence change events. By making this subscription, every time a
849 * server adds/removes/alters a resource, starts or is intentionally stopped
851 * @param host The IP address/addressable name of the server to subscribe to
852 * @param connectivityTypeSet Set of types of connectivity. Example: IP
853 * @param onPresenceListener listener that will receive notifications/subscription events
854 * @return a handle object that can be used to identify this subscription request. It can be
855 * used to unsubscribe from these events in the future
856 * @throws OcException if failure
858 public static OcPresenceHandle subscribePresence(
860 EnumSet<OcConnectivityType> connectivityTypeSet,
861 OnPresenceListener onPresenceListener) throws OcException {
862 OcPlatform.initCheck();
865 for (OcConnectivityType connType : OcConnectivityType.values()) {
866 if (connectivityTypeSet.contains(connType))
867 connTypeInt |= connType.getValue();
869 return OcPlatform.subscribePresence0(
876 private static native OcPresenceHandle subscribePresence0(
878 int connectivityType,
879 OnPresenceListener onPresenceListener) throws OcException;
882 * Subscribes to a server's presence change events. By making this subscription, every time a
883 * server adds/removes/alters a resource, starts or is intentionally stopped
885 * @param host The IP address/addressable name of the server to subscribe to
886 * @param resourceType a resource type specified as a filter for subscription events.
887 * @param connectivityTypeSet Set of types of connectivity. Example: IP
888 * @param onPresenceListener listener that will receive notifications/subscription events
889 * @return a handle object that can be used to identify this subscription request. It can be
890 * used to unsubscribe from these events in the future
891 * @throws OcException if failure
893 public static OcPresenceHandle subscribePresence(
896 EnumSet<OcConnectivityType> connectivityTypeSet,
897 OnPresenceListener onPresenceListener) throws OcException {
898 OcPlatform.initCheck();
901 for (OcConnectivityType connType : OcConnectivityType.values()) {
902 if (connectivityTypeSet.contains(connType))
903 connTypeInt |= connType.getValue();
905 return OcPlatform.subscribePresence1(
912 private static native OcPresenceHandle subscribePresence1(
915 int connectivityType,
916 OnPresenceListener onPresenceListener) throws OcException;
919 * Unsubscribes from a previously subscribed server's presence events. Note that you may for
920 * a short time still receive events from the server since it may take time for the
921 * unsubscribe to take effect.
923 * @param ocPresenceHandle the handle object provided by the subscribePresence call that
924 * identifies this subscription
925 * @throws OcException if failure
927 public static void unsubscribePresence(
928 OcPresenceHandle ocPresenceHandle) throws OcException {
929 OcPlatform.initCheck();
930 OcPlatform.unsubscribePresence0(ocPresenceHandle);
933 private static native void unsubscribePresence0(
934 OcPresenceHandle ocPresenceHandle) throws OcException;
937 * Subscribes to a server's device presence change events.
939 * @param host The IP address/addressable name of the server to subscribe to.
940 * @param di Vector which can have the devices id.
941 * @param connectivityTypeSet Set of connectivity types, e.g. IP.
942 * @param onObserveListener The handler method will be invoked with a map
943 * of attribute name and values.
944 * @return a handle object that can be used to identify this subscription request.
945 * It can be used to unsubscribe from these events in the future.
946 * @throws OcException if failure.
948 public static OcPresenceHandle subscribeDevicePresence(
951 EnumSet<OcConnectivityType> connectivityTypeSet,
952 OcResource.OnObserveListener onObserveListener) throws OcException {
953 OcPlatform.initCheck();
956 for (OcConnectivityType connType : OcConnectivityType.values()) {
957 if (connectivityTypeSet.contains(connType))
958 connTypeInt |= connType.getValue();
960 return OcPlatform.subscribeDevicePresence0(
962 di.toArray(new String[di.size()]),
967 private static native OcPresenceHandle subscribeDevicePresence0(
970 int connectivityType,
971 OcResource.OnObserveListener onObserveListener) throws OcException;
974 * Creates a resource proxy object so that get/put/observe functionality can be used without
975 * discovering the object in advance. Note that the consumer of this method needs to provide
976 * all of the details required to correctly contact and observe the object. If the consumer
977 * lacks any of this information, they should discover the resource object normally.
978 * Additionally, you can only create this object if OcPlatform was initialized to be a Client
981 * @param host a string containing a resolvable host address of the server holding
983 * @param uri the rest of the resource's URI that will permit messages to be
986 * @param connectivityTypeSet Set of types of connectivity. Example: IP
987 * @param isObservable a boolean containing whether the resource supports observation
988 * @param resourceTypeList a collection of resource types implemented by the resource
989 * @param interfaceList a collection of interfaces that the resource supports/implements
990 * @return new resource object
991 * @throws OcException if failure
993 public static OcResource constructResourceObject(
996 EnumSet<OcConnectivityType> connectivityTypeSet,
997 boolean isObservable,
998 List<String> resourceTypeList,
999 List<String> interfaceList) throws OcException {
1000 OcPlatform.initCheck();
1001 int connTypeInt = 0;
1003 for (OcConnectivityType connType : OcConnectivityType.values()) {
1004 if (connectivityTypeSet.contains(connType))
1005 connTypeInt |= connType.getValue();
1007 return OcPlatform.constructResourceObject0(
1012 resourceTypeList.toArray(new String[resourceTypeList.size()]),
1013 interfaceList.toArray(new String[interfaceList.size()])
1017 private static native OcResource constructResourceObject0(
1020 int connectivityType,
1021 boolean isObservable,
1022 String[] resourceTypes,
1023 String[] interfaces) throws OcException;
1026 * Allows application entity handler to send response to an incoming request.
1028 * @param ocResourceResponse resource response
1029 * @throws OcException if failure
1031 public static void sendResponse(OcResourceResponse ocResourceResponse)
1032 throws OcException {
1033 OcPlatform.initCheck();
1034 OcPlatform.sendResponse0(ocResourceResponse);
1037 private static native void sendResponse0(OcResourceResponse ocResourceResponse)
1041 * Method to find all devices which are eligible for direct pairing and return the list.
1043 * @param timeout timeout for discovering direct pair devices.
1044 * @param FindDirectPairingListener Callback function, which will receive the list of direct
1046 * @throws OcException
1048 public static native void findDirectPairingDevices(int timeout,
1049 FindDirectPairingListener onFindDirectPairingListener) throws OcException;
1052 * Method to get list of all paired devices for a given device.
1054 * @param GetDirectPairedListener Callback function, which will receive the list of direct
1056 * @throws OcException
1058 public native void getDirectPairedDevices(GetDirectPairedListener onGetDirectPairedListener)
1062 * Method to perform direct pairing between two devices.
1064 * @param peer Target peer
1065 * @param prmType Pairing Method to be used for Pairing
1067 * @param DirectPairingListener Callback function, which will be called after
1068 * completion of direct pairing.
1069 * @throws OcException
1071 public static void doDirectPairing(
1072 OcDirectPairDevice peer,
1075 DirectPairingListener onDirectPairingListener) throws OcException {
1077 OcPlatform.doDirectPairing0(
1081 onDirectPairingListener
1085 private static native void doDirectPairing0(OcDirectPairDevice peer,
1086 int pmSel, String pinNumber, DirectPairingListener onDirectPairingListener)
1090 * An FindDirectPairingListener can be registered via the OcPlatform.findDirectPairingDevices call.
1091 * Event listeners are notified asynchronously
1093 public interface FindDirectPairingListener {
1094 public void onFindDirectPairingListener(List<OcDirectPairDevice> ocPairedDeviceList);
1098 * Listerner to Get List of already Direct Paired devices.
1099 * An GetDirectPairedListener can be registered via the OcPlatform.getDirectPairedDevices call.
1100 * Event listeners are notified asynchronously
1102 public interface GetDirectPairedListener {
1103 public void onGetDirectPairedListener(List<OcDirectPairDevice> ocPairedDeviceList);
1107 * Listner to get result of doDirectPairing.
1108 * An DirectPairingListener can be registered via the OcPlatform.doDirectPairing call.
1109 * Event listeners are notified asynchronously
1111 public interface DirectPairingListener {
1112 public void onDirectPairingListener(String devId, int result);
1116 * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
1117 * Event listeners are notified asynchronously
1119 public interface OnResourceFoundListener {
1120 public void onResourceFound(OcResource resource);
1121 public void onFindResourceFailed(Throwable ex, String uri);
1125 * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call.
1126 * Event listeners are notified asynchronously
1128 public interface OnDeviceFoundListener {
1129 public void onDeviceFound(OcRepresentation ocRepresentation);
1133 * An OnPlatformFoundListener can be registered via the OcPlatform.getPlatformInfo call.
1134 * Event listeners are notified asynchronously
1136 public interface OnPlatformFoundListener {
1137 public void onPlatformFound(OcRepresentation ocRepresentation);
1141 * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call.
1142 * Event listeners are notified asynchronously
1144 public interface OnPresenceListener {
1145 public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress);
1149 * An EntityHandler can be registered via the OcPlatform.registerResource call.
1150 * Event listeners are notified asynchronously
1152 * @note entityhandler callback :
1153 * When you set specific return value like EntityHandlerResult.OK, SLOW
1154 * and etc in entity handler callback,
1155 * ocstack will be not send response automatically to client
1156 * except for error return value like EntityHandlerResult.ERROR
1157 * If you want to send response to client with specific result,
1158 * sendResponse API should be called with the result value.
1160 public interface EntityHandler {
1161 public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
1165 * Listner to get result of findKeepAliveResource and sendKeepAliveRequest.
1166 * An PingListener can be registered via the OcPlatform.KeepAliveListener and
1167 * OcPlatform.sendKeepAliveRequest call.
1168 * Event listeners are notified asynchronously
1170 public interface KeepAliveListener {
1171 public void onKeepAliveListener(OcRepresentation ocRepresentation, int result);
1174 private static void initCheck() {
1175 if (!sIsPlatformInitialized) {
1176 throw new IllegalStateException("OcPlatform must be configured by making a call to " +
1177 "OcPlatform.Configure before any other API calls are permitted");
1182 * Gets platform quality of service
1184 * @return quality of service
1186 public static QualityOfService getPlatformQualityOfService() {
1187 OcPlatform.initCheck();
1188 return sPlatformQualityOfService;
1192 * Create an account manager object that can be used for doing request to account server.
1193 * You can only create this object if OCPlatform was initialized to be a Client or
1194 * Client/Server. Otherwise, this will return an empty shared ptr.
1196 * @note For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
1197 * methods of this object to work since device id is not generated on Client mode.
1199 * @param host Host IP Address of a account server.
1200 * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
1201 * @return new AccountManager object
1202 * @throws OcException if failure
1204 public static OcAccountManager constructAccountManagerObject(
1206 EnumSet<OcConnectivityType> connectivityTypeSet) throws OcException {
1207 OcPlatform.initCheck();
1208 int connTypeInt = 0;
1210 for (OcConnectivityType connType : OcConnectivityType.values()) {
1211 if (connectivityTypeSet.contains(connType))
1213 connTypeInt |= connType.getValue();
1216 return OcPlatform.constructAccountManagerObject0(
1222 private static native OcAccountManager constructAccountManagerObject0(
1224 int connectivityType) throws OcException;
1226 * Method to get device Id in byte array.
1227 * @return My DeviceId.
1229 public static native byte[] getDeviceId();
1232 * Method to set DeviceId.
1234 public static native void setDeviceId(byte[] deviceId) throws OcException;
1237 * gets OCRepresentation of KeepAlive resource from given host.
1239 * @param host Host IP Address of pingResource
1240 * @param onKeepAliveFoundListener Function to callback with result code and OCRepresentation.
1241 * @throws OcException if failure
1243 public static void findKeepAliveResource(String host,
1244 KeepAliveListener onKeepAliveFoundListener)
1245 throws OcException {
1246 OcPlatform.initCheck();
1247 OcPlatform.findKeepAliveResourceImpl(host, onKeepAliveFoundListener);
1250 private static native void findKeepAliveResourceImpl(String host,
1251 KeepAliveListener onKeepAliveFoundListener)
1255 * send KeepAlive request to given host.
1257 * @param interval interval time of expected pong response
1258 * @param keepAliveResponseListener handles callback
1259 * @throws OcException if failure
1262 public static void sendKeepAliveRequest(String host, OcRepresentation ocRepresentation,
1263 KeepAliveListener keepAliveResponseListener)
1264 throws OcException {
1265 OcPlatform.initCheck();
1266 OcPlatform.sendKeepAliveRequestImpl(host, ocRepresentation, keepAliveResponseListener);
1269 private static native void sendKeepAliveRequestImpl(String host,
1270 OcRepresentation ocRepresentation,
1271 KeepAliveListener keepAliveResponseListener)