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 ******************************************************************/
23 * This file contains class which provides functions
24 * for things maintenance.
27 package org.iotivity.service.tm;
29 import java.util.Vector;
31 import org.iotivity.base.OcException;
32 import org.iotivity.base.OcHeaderOption;
33 import org.iotivity.base.OcRepresentation;
34 import org.iotivity.base.OcResource;
36 import android.util.Log;
39 * This class provides a set of APIs relating to Things Maintenance.
41 public class ThingsMaintenance {
43 private IThingsMaintenanceListener thingsMaintenanceListener;
44 private static ThingsMaintenance s_thingsMaintenanceInstance;
45 private final String LOG_TAG = this.getClass()
49 private static native int nativeReboot(OcResource resource);
51 private static native int nativeFactoryReset(OcResource resource);
53 private static native String nativeGetListOfSupportedMaintenanceUnits();
56 * Function for Getting instance of ThingsMaintenance object.
58 * @return ThingsMaintenance instance.
60 public static ThingsMaintenance getInstance() {
61 if (null == s_thingsMaintenanceInstance) {
62 s_thingsMaintenanceInstance = new ThingsMaintenance();
64 return s_thingsMaintenanceInstance;
68 * Provides interface for receiving asynchronous response for Things
69 * Maintenance feature APIs.
71 public interface IThingsMaintenanceListener {
73 * Asynchronous response callback for reboot API.
75 * @param headerOptions
76 * It comprises of optionID and optionData as members.
78 * Configuration parameters are carried as a pair of
79 * attribute key and value in a form of OCRepresentation
84 public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
85 OcRepresentation rep, int errorValue);
88 * Asynchronous response callback for factoryReset API.
90 * @param headerOptions
91 * It comprises of optionID and optionData as members.
93 * Configuration parameters are carried as a pair of
94 * attribute key and value in a form of OCRepresentation
99 public void onFactoryResetCallback(
100 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
105 * Set listener for receiving asynchronous response for Things Maintenance
109 * IThingsMaintenanceListener to receive asynchronous response
110 * for diagnostic feature APIs.
112 public void setThingsMaintenanceListener(IThingsMaintenanceListener listener) {
113 thingsMaintenanceListener = listener;
117 * API to is used to send a request to a server(thing or device) to be
118 * rebooted. On receiving the request, the server attempts to reboot itself
119 * in a deterministic time. The target thing could be a group of multiple
120 * things or a single thing.
122 * Listener should be set using setDiagnosticsListener API.
124 * Listener IThingsMaintenanceListener::onRebootCallback will be notified
125 * when the response arrives.
128 * resource pointer representing the target group
130 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
133 * @throws OcException {@link OcException}
135 public OCStackResult reboot(OcResource resource) throws OcException {
137 OCStackResult result;
138 if (null == thingsMaintenanceListener) {
139 result = OCStackResult.OC_STACK_LISTENER_NOT_SET;
141 int ordinal = nativeReboot(resource);
142 result = OCStackResult.conversion(ordinal);
148 * API to restore all configuration parameters to default one on
149 * thing(device). All configuration parameters refers Configuration
150 * resource, which they could have been modified for various reasons (e.g.,
151 * for a request to update a value). If developers on the client want to
152 * restore the parameters, just use the factoryReset function.The target
153 * thing could be a group of multiple things or a single thing.
156 * Listener should be set using setDiagnosticsListener API.
158 * Listener IThingsMaintenanceListener::onFactoryResetCallback will be
159 * notified when the response arrives.
162 * resource pointer representing the target group
164 * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
167 * @throws OcException {@link OcException}
169 public OCStackResult factoryReset(OcResource resource) throws OcException {
171 OCStackResult result;
172 if (null == thingsMaintenanceListener) {
173 result = OCStackResult.OC_STACK_LISTENER_NOT_SET;
175 int ordinal = nativeFactoryReset(resource);
176 result = OCStackResult.conversion(ordinal);
182 * This callback method is called when a response for the reboot request
185 * @param headerOptions
186 * It comprises of optionID and optionData as members.
188 * Configuration parameters are carried as a pair of attribute
189 * key and value in a form of OCRepresentation instance.
193 public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
194 OcRepresentation rep, int errorValue) {
195 if (null != thingsMaintenanceListener) {
196 Log.i("ThingsManagerCallback : onRebootCallback",
197 "Received Callback from JNI");
198 thingsMaintenanceListener.onRebootCallback(headerOptions, rep,
204 * This callback method is called when a response for the factoryReset
205 * request just arrives.
207 * @param headerOptions
208 * It comprises of optionID and optionData as members.
210 * Configuration parameters are carried as a pair of attribute
211 * key and value in a form of OCRepresentation instance.
215 public void onFactoryResetCallback(Vector<OcHeaderOption> headerOptions,
216 OcRepresentation rep, int errorValue) {
217 if (null != thingsMaintenanceListener) {
218 Log.i("ThingsManagerCallback : onFactoryResetCallback",
219 "Received Callback from JNI");
220 thingsMaintenanceListener.onFactoryResetCallback(headerOptions,
226 * API for getting the list of supported Maintenance units. It shows which
227 * Maintenance Names are supported and their brief descriptions. This
228 * information is provided in JSON format.
230 * @return Returns the configuration list in JSON format.
232 public String getListOfSupportedMaintenanceUnits() {
233 return nativeGetListOfSupportedMaintenanceUnits();