1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 * @file ThingsManager.java
23 * @brief This file provides a class for a set of APIs relating to group management,
24 * synchronization of group, configuration of things, and diagnostics
29 package org.iotivity.service.tm;
32 import java.util.Vector;
34 import org.iotivity.base.OcException;
35 import org.iotivity.base.OcResource;
36 import org.iotivity.base.OcResourceHandle;
38 import android.util.Log;
41 * This class provides a set of APIs relating to group management,
42 * synchronization of group, configuration of things, and diagnostics
46 public class ThingsManager {
48 private IFindCandidateResourceListener resourceListener;
49 private ISubscribePresenceListener presenceListener;
50 private IFindGroupListener groupListener;
51 private IConfigurationListener configurationListener;
52 private IDiagnosticsListener diagnosticsListener;
53 private IActionListener actionListener;
54 private final String LOG_TAG = this.getClass()
57 private final ThingsManagerCallback thingsManagerCallbackInterfaceObj;
58 private final ThingsManagerInterface thingsManagerInterfaceObj;
61 System.loadLibrary("gnustl_shared");
62 System.loadLibrary("oc_logger");
63 System.loadLibrary("connectivity_abstraction");
64 System.loadLibrary("ca-interface");
65 System.loadLibrary("octbstack");
66 System.loadLibrary("oc");
67 System.loadLibrary("TGMSDKLibrary");
68 System.loadLibrary("ocstack-jni");
69 System.loadLibrary("things-manager-jni");
73 * Constructor for ThingsManager. Constructs a new ThingsManager.
75 public ThingsManager() {
76 thingsManagerCallbackInterfaceObj = ThingsManagerCallback.getInstance();
77 thingsManagerInterfaceObj = ThingsManagerInterface.getInstance();
81 * Set listener for receiving notification of resource discovery.
84 * IFindCandidateResourceListener to receive the
85 * discovered resources.
87 public void setFindCandidateResourceListener(
88 IFindCandidateResourceListener listener) {
89 resourceListener = listener;
90 thingsManagerCallbackInterfaceObj
91 .registerFindCandidateResourceListener(listener);
92 thingsManagerInterfaceObj
93 .registerFindCandidateResourceListener(listener);
97 * Set listener for receiving child resource presence notifications.
100 * ISubscribePresenceListener to receive child resource
103 public void setSubscribePresenceListener(ISubscribePresenceListener listener) {
104 presenceListener = listener;
105 thingsManagerCallbackInterfaceObj
106 .registerSubscribePresenceListener(listener);
107 thingsManagerInterfaceObj.registerSubscribePresenceListener(listener);
111 * Set listener for receiving notification on whether the requested group is
115 * IFindGroupListener to receive group discovery status.
117 public void setGroupListener(IFindGroupListener listener) {
118 groupListener = listener;
119 thingsManagerCallbackInterfaceObj.registerGroupListener(listener);
120 thingsManagerInterfaceObj.registerGroupListener(listener);
124 * Set listener for receiving asynchronous response for configuration APIs.
127 * IConfigurationListener to receive asynchronous response
128 * for configuration APIs.
130 public void setConfigurationListener(IConfigurationListener listener) {
131 configurationListener = listener;
132 thingsManagerCallbackInterfaceObj
133 .registerConfigurationListener(listener);
134 thingsManagerInterfaceObj.registerConfigurationListener(listener);
138 * Set listener for receiving asynchronous response for diagnostic feature
142 * IDiagnosticsListener to receive asynchronous response
143 * for diagnostic feature APIs.
145 public void setDiagnosticsListener(IDiagnosticsListener listener) {
146 diagnosticsListener = listener;
147 thingsManagerCallbackInterfaceObj.registerDiagnosticsListener(listener);
151 * Set listener for receiving responses of Get, PUT and POST requests.
154 * IActionListener to receive Get, PUT and POST request
157 public void setActionListener(IActionListener listener) {
158 actionListener = listener;
159 thingsManagerCallbackInterfaceObj.registerActionListener(listener);
163 * API for discovering candidate resources with waiting delay. This
164 * operation returns all resources of given type on the network service.
165 * This operation is sent via multicast to all services. However, the filter
166 * limits the responders to just those that support the resource type in the
167 * query. Currently only exact matches are supported.
169 * Listener should be set using setFindCandidateResourceListener API
171 * Listener IFindCandidateResourceListener::onResourceCallback
172 * will be notified when resource is discovered in network.
174 * @param resourceTypes
175 * required resource types(called "candidate")
177 * Delay time to add in seconds before starting to find the
178 * resources in network.
180 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
184 public OCStackResult findCandidateResources(Vector<String> resourceTypes,
186 if (null == resourceListener) {
187 Log.e(LOG_TAG,"findCandidateResources: listener not set!");
188 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
191 OCStackResult result = thingsManagerInterfaceObj.findCandidateResources(
192 resourceTypes, waitTime);
193 if (OCStackResult.OC_STACK_OK != result) {
194 Log.e(LOG_TAG, "findCandidateResources: returned error: " + result.name());
201 * API for subscribing child's state. It subscribes the presence state of
202 * the resource. By making this subscription, every time a server
203 * adds/removes/alters a resource, starts or is intentionally stopped, a
204 * notification is sent to subscriber.
207 * Listener should be set using setSubscribePresenceListener API.
209 * Listener ISubscribePresenceListener::onPresenceCallback will
210 * be notified for resource presence status
213 * collection resource for subscribing presence of all child
216 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
220 public OCStackResult subscribeCollectionPresence(OcResource resource)
222 if (null == presenceListener) {
223 Log.e(LOG_TAG,"subscribeCollectionPresence: listener not set!");
224 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
227 OCStackResult result = thingsManagerInterfaceObj
228 .subscribeCollectionPresence(resource);
229 if (OCStackResult.OC_STACK_OK != result) {
230 Log.e(LOG_TAG, "subscribeCollectionPresence: returned error: " + result.name());
237 * API for finding a specific remote group when a resource tries to join a
240 * Listener should be set using setGroupListener API
242 * Listener IFindGroupListener::onGroupFindCallback will be
243 * called to notify whenever a requested group is found.
245 * @param collectionResourceTypes
246 * resource types of a group to find and join
248 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
253 public OCStackResult findGroup(Vector<String> collectionResourceTypes) {
254 if (null == groupListener) {
255 Log.e(LOG_TAG,"findGroup: listener not set!");
256 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
259 OCStackResult result = result = thingsManagerInterfaceObj.findGroup(collectionResourceTypes);
260 if (OCStackResult.OC_STACK_OK != result) {
261 Log.e(LOG_TAG, "findGroup: returned error: " + result.name());
268 * API for creating a new group.
270 * @param collectionResourceType
271 * resource type of a group to create
273 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
276 public OCStackResult createGroup(String collectionResourceType) {
277 OCStackResult result = thingsManagerInterfaceObj.createGroup(collectionResourceType);
278 if (OCStackResult.OC_STACK_OK != result) {
279 Log.e(LOG_TAG, "createGroup: returned error: " + result.name());
286 * API for joining a group. This API is used for joining the resource
287 * to local group which is created using @createGroup API.
289 * @param collectionResourceType
290 * resource type of a group to join.
291 * @param resourceHandle
292 * resource handle to join a group.
294 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
298 public OCStackResult joinGroup(String collectionResourceType,
299 OcResourceHandle resourceHandle) {
300 OCStackResult result = thingsManagerInterfaceObj.joinGroup(collectionResourceType,
302 if (OCStackResult.OC_STACK_OK != result) {
303 Log.e(LOG_TAG, "joinGroup: returned error: " + result.name());
310 * API for joining a group. This API is used for joining the resource to
314 * group resource to join.
315 * @param resourceHandle
316 * resource handle to join a group.
318 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
322 public OCStackResult joinGroup(OcResource resource,
323 OcResourceHandle resourceHandle) throws OcException {
324 OCStackResult result = thingsManagerInterfaceObj.joinGroup(resource, resourceHandle);
325 if (OCStackResult.OC_STACK_OK != result) {
326 Log.e(LOG_TAG, "joinGroup: returned error: " + result.name());
333 * API for leaving a joined local group.
335 * @param collectionResourceType
336 * resource type of a group to leave.
337 * @param resourceHandle
338 * resource handle to leave a group.
340 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
344 public OCStackResult leaveGroup(String collectionResourceType,
345 OcResourceHandle resourceHandle) {
346 OCStackResult result = thingsManagerInterfaceObj.leaveGroup(collectionResourceType,
348 if (OCStackResult.OC_STACK_OK != result) {
349 Log.e(LOG_TAG, "leaveGroup: returned error: " + result.name());
356 * API for leaving a joined remote group.
359 * resource of a group to leave.
360 * @param collectionResourceType
361 * resource type of a group to leave.
362 * @param resourceHandle
363 * resource handle to leave a group.
365 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
369 public OCStackResult leaveGroup(OcResource resource, String collectionResourceType,
370 OcResourceHandle resourceHandle) {
371 OCStackResult result = thingsManagerInterfaceObj.leaveGroup(resource, collectionResourceType,
373 if (OCStackResult.OC_STACK_OK != result) {
374 Log.e(LOG_TAG, "leaveGroup: returned error: " + result.name());
381 * API for deleting a created group.
383 * @param collectionResourceType
384 * resource type of a group to delete.
387 public void deleteGroup(String collectionResourceType) {
388 thingsManagerInterfaceObj.deleteGroup(collectionResourceType);
392 * API for getting a list of local groups.
394 * @return Returns the map with the group resource type and group
395 * resource handle on successs otherwise null.
398 public Map<String, OcResourceHandle> getGroupList() {
399 return thingsManagerInterfaceObj.getGroupList();
403 * API for registering and binding a resource to group.
406 * resource for register and bind to group. It has all data.
407 * @param collectionHandle
408 * collection resource handle. It will be the added child
411 * @return OcResourceHandle - Child resource handle.
414 public OcResourceHandle bindResourceToGroup(OcResource resource,
415 OcResourceHandle collectionHandle)
417 return thingsManagerInterfaceObj.bindResourceToGroup(resource,
422 * API for updating configuration value of multiple things of a target group
423 * or a single thing to a resource server(s).
426 * Listener should be set using setConfigurationListener API.
428 * Listener IConfigurationListener::onUpdateConfigurationsCallback will be
429 * notified when the response of update configuration arrives.
432 * resource representing the target group or the single thing.
433 * It is a pointer of resource instance of Configuration
434 * resource. The resource pointer can be acquired by performing
435 * findResource() function with a dedicated resource type,
436 * "oic.con". Note that, the resource pointer represents not only
437 * a single simple resource but also a collection resource
438 * composing multiple simple resources. In other words, using
439 * these APIs, developers can send a series of requests to
440 * multiple things by calling the corresponding function at once.
441 * @param configurations
442 * ConfigurationUnit: a nickname of attribute of target
443 * resource (e.g., installed location, currency, (IP)address)
444 * Value : a value to be updated. It represents an indicator of
445 * which resource developers want to access and which value
446 * developers want to update. Basically, developers could use a
447 * resource URI to access a specific resource but a resource URI
448 * might be hard for all developers to memorize lots of URIs,
449 * especially in the case of long URIs. To relieve the problem,
450 * Things Configuration introduces a easy-memorizing name, called
451 * ConfigurationName, instead of URI. And ConfigurationValue used
452 * in updateConfiguration() function indicates a value to be
453 * updated. Note that, only one configuration parameter is
454 * supported in this release. Multiple configuration parameters
455 * will be supported in future release.
457 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
461 public OCStackResult updateConfigurations(OcResource resource,
462 Map<String, String> configurations) throws OcException {
463 if (null == configurationListener) {
464 Log.e(LOG_TAG,"updateConfigurations: listener not set!");
465 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
468 OCStackResult result = thingsManagerInterfaceObj.updateConfigurations(resource,
470 if (OCStackResult.OC_STACK_OK != result) {
471 Log.e(LOG_TAG, "updateConfigurations: returned error: " + result.name());
478 * API for getting configuration value of multiple things of a target group
479 * or a single thing.To get a value, users need to know a Configuration Name
480 * indicating the target resource. In this release, the Configuration Name
481 * is "configuration".An update value is not needed. After that, users store
482 * them in form of a Vector and then use a getConfigurations() function.
485 * resource representing the target group or the single thing.
486 * @param configurations
487 * ConfigurationUnit: a nickname of attribute of target
490 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
494 public OCStackResult getConfigurations(OcResource resource,
495 Vector<String> configurations) throws OcException {
496 if (null == configurationListener) {
497 Log.e(LOG_TAG,"getConfigurations: listener not set!");
498 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
501 OCStackResult result = thingsManagerInterfaceObj.getConfigurations(resource,
503 if (OCStackResult.OC_STACK_OK != result) {
504 Log.e(LOG_TAG, "getConfigurations: returned error: " + result.name());
511 * API for getting the list of supported configuration units (configurable
512 * parameters). It shows which Configuration Names are supported and their
513 * brief descriptions. This information is provided in JSON format.
515 * @return Returns the configuration list in JSON format.
518 public String getListOfSupportedConfigurationUnits() {
519 return thingsManagerInterfaceObj.getListOfSupportedConfigurationUnits();
523 * API for bootstrapping system configuration parameters from a bootstrap
526 * Listener should be set using setConfigurationListener API.
528 * Listener IConfigurationListener::onBootStrapCallback will be
529 * notified when the response arrives.
531 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
535 public OCStackResult doBootstrap() {
536 if (null == configurationListener) {
537 Log.e(LOG_TAG,"doBootstrap: listener not set!");
538 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
541 OCStackResult result = thingsManagerInterfaceObj.doBootstrap();
542 if (OCStackResult.OC_STACK_OK != result) {
543 Log.e(LOG_TAG, "doBootstrap: returned error: " + result.name());
550 * API to is used to send a request to a server(thing or device) to be
551 * rebooted. On receiving the request, the server attempts to reboot itself
552 * in a deterministic time. The target thing could be a group of multiple
553 * things or a single thing.
555 * Listener should be set using setDiagnosticsListener API.
557 * Listener IDiagnosticsListener::onRebootCallback will be
558 * notified when the response arrives.
560 * resource pointer representing the target group
562 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
566 public OCStackResult reboot(OcResource resource) throws OcException {
567 if (null == diagnosticsListener) {
568 Log.e(LOG_TAG,"reboot: listener not set!");
569 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
572 OCStackResult result = thingsManagerInterfaceObj.reboot(resource);
573 if (OCStackResult.OC_STACK_OK != result) {
574 Log.e(LOG_TAG, "reboot: returned error: " + result.name());
581 * API to restore all configuration parameters to default one on
582 * thing(device). All configuration parameters refers Configuration
583 * resource, which they could have been modified for various reasons (e.g.,
584 * for a request to update a value). If developers on the client want to
585 * restore the parameters, just use the factoryReset function.The target
586 * thing could be a group of multiple things or a single thing.
589 * Listener should be set using setDiagnosticsListener API.
591 * Listener IDiagnosticsListener::onFactoryResetCallback will be
592 * notified when the response arrives.
595 * resource pointer representing the target group
597 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
601 public OCStackResult factoryReset(OcResource resource) throws OcException {
602 if (null == diagnosticsListener) {
603 Log.e(LOG_TAG,"factoryReset: listener not set!");
604 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
607 OCStackResult result = thingsManagerInterfaceObj.factoryReset(resource);
608 if (OCStackResult.OC_STACK_OK != result) {
609 Log.e(LOG_TAG, "factoryReset: returned error: " + result.name());
616 * API for adding a new ActionSet onto a specific resource.
619 * Listener should be set using setActionListener API.
621 * Listener IActionListener::onPutResponseCallback will be
622 * notified when the response of PUT operation arrives.
624 * resource pointer of the group resource
626 * pointer of ActionSet
628 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
632 public OCStackResult addActionSet(OcResource resource, ActionSet actionSet)
634 if (null == actionListener) {
635 Log.e(LOG_TAG,"addActionSet: listener not set!");
636 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
639 OCStackResult result = thingsManagerInterfaceObj
640 .addActionSet(resource, actionSet);
641 if (OCStackResult.OC_STACK_OK != result) {
642 Log.e(LOG_TAG, "addActionSet: returned error: " + result.name());
649 * API for executing a specific ActionSet belonging to a specific resource.
652 * Listener should be set using setActionListener API.
654 * Listener IActionListener::onPostResponseCallback will be
655 * notified when the response of POST operation arrives.
658 * resource pointer of the group resource
659 * @param actionsetName
660 * ActionSet name for removing the ActionSet
662 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
666 public OCStackResult executeActionSet(OcResource resource,
667 String actionsetName) throws OcException {
668 if (null == actionListener) {
669 Log.e(LOG_TAG,"executeActionSet: listener not set!");
670 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
673 OCStackResult result = thingsManagerInterfaceObj
674 .executeActionSet(resource, actionsetName);
675 if (OCStackResult.OC_STACK_OK != result) {
676 Log.e(LOG_TAG, "executeActionSet: returned error: " + result.name());
683 * API for executing a specific ActionSet belonging to a specific resource.
686 * Listener should be set using setActionListener API.
688 * Listener IActionListener::onPostResponseCallback will be
689 * notified when the response of POST operation arrives.
692 * resource pointer of the group resource
693 * @param actionsetName
694 * ActionSet name for removing the ActionSet
696 * Wait time for the ActionSet execution
698 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
702 public OCStackResult executeActionSet(OcResource resource,
703 String actionsetName, long delay) throws OcException {
704 if (null == actionListener) {
705 Log.e(LOG_TAG,"executeActionSet: listener not set!");
706 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
709 OCStackResult result = thingsManagerInterfaceObj
710 .executeActionSet(resource, actionsetName, delay);
711 if (OCStackResult.OC_STACK_OK != result) {
712 Log.e(LOG_TAG, "executeActionSet: returned error: " + result.name());
719 * API to cancel the existing ActionSet.
722 * Listener should be set using setActionListener API.
724 * Listener IActionListener::onPostResponseCallback will be
725 * notified when the response of POST operation arrives.
727 * resource pointer of the group resource.
728 * @param actionsetName
729 * ActionSet name that has to be cancelled.
731 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
736 public OCStackResult cancelActionSet(OcResource resource,
737 String actionsetName) throws OcException {
738 if (null == actionListener) {
739 Log.e(LOG_TAG,"cancelActionSet: listener not set!");
740 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
743 OCStackResult result = thingsManagerInterfaceObj.cancelActionSet(resource,
745 if (OCStackResult.OC_STACK_OK != result) {
746 Log.e(LOG_TAG, "cancelActionSet: returned error: " + result.name());
753 * API to to get an existing ActionSet belonging to a specific resource.
756 * Listener should be set using setActionListener API.
758 * Listener IActionListener::onPostResponseCallback will be
759 * notified when the response of POST operation arrives.
761 * resource pointer of the group resource
762 * @param actionsetName
763 * ActionSet name for removing the ActionSet
765 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
769 public OCStackResult getActionSet(OcResource resource, String actionsetName)
771 if (null == actionListener) {
772 Log.e(LOG_TAG,"getActionSet: listener not set!");
773 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
776 OCStackResult result = thingsManagerInterfaceObj.getActionSet(resource,
778 if (OCStackResult.OC_STACK_OK != result) {
779 Log.e(LOG_TAG, "getActionSet: returned error: " + result.name());
786 * API to delete an existing ActionSet belonging to a specific resource.
789 * Listener should be set using setActionListener API.
791 * Listener IActionListener::onPutResponseCallback will be
792 * notified when the response of PUT operation arrives.
795 * resource pointer of the group resource
796 * @param actionsetName
797 * ActionSet name for removing the ActionSet
799 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
803 public OCStackResult deleteActionSet(OcResource resource,
804 String actionsetName) throws OcException {
805 if (null == actionListener) {
806 Log.e(LOG_TAG,"deleteActionSet: listener not set!");
807 return OCStackResult.OC_STACK_LISTENER_NOT_SET;
810 OCStackResult result = thingsManagerInterfaceObj.deleteActionSet(resource,
812 if (OCStackResult.OC_STACK_OK != result) {
813 Log.e(LOG_TAG, "deleteActionSet: returned error: " + result.name());