Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / ThingsMaintenance.java
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains class which provides functions
24  * for things maintenance.
25  */
26
27 package org.iotivity.service.tm;
28
29 import java.util.Vector;
30
31 import org.iotivity.base.OcException;
32 import org.iotivity.base.OcHeaderOption;
33 import org.iotivity.base.OcRepresentation;
34 import org.iotivity.base.OcResource;
35
36 import android.util.Log;
37
38 /**
39  * This class provides a set of APIs relating to Things Maintenance.
40  */
41 public class ThingsMaintenance {
42
43     private IThingsMaintenanceListener thingsMaintenanceListener;
44     private static ThingsMaintenance   s_thingsMaintenanceInstance;
45     private final String               LOG_TAG = this.getClass()
46                                                        .getSimpleName();
47
48     // Native methods
49     private static native int nativeReboot(OcResource resource);
50
51     private static native int nativeFactoryReset(OcResource resource);
52
53     private static native String nativeGetListOfSupportedMaintenanceUnits();
54
55     /**
56      * Function for Getting instance of ThingsMaintenance object.
57      *
58      * @return ThingsMaintenance instance.
59      */
60     public static ThingsMaintenance getInstance() {
61         if (null == s_thingsMaintenanceInstance) {
62             s_thingsMaintenanceInstance = new ThingsMaintenance();
63         }
64         return s_thingsMaintenanceInstance;
65     }
66
67     /**
68      * Provides interface for receiving asynchronous response for Things
69      * Maintenance feature APIs.
70      */
71     public interface IThingsMaintenanceListener {
72         /**
73          * Asynchronous response callback for reboot API.
74          *
75          * @param headerOptions
76          *            It comprises of optionID and optionData as members.
77          * @param rep
78          *            Configuration parameters are carried as a pair of
79          *            attribute key and value in a form of OCRepresentation
80          *            instance.
81          * @param errorValue
82          *            error code.
83          */
84         public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
85                 OcRepresentation rep, int errorValue);
86
87         /**
88          * Asynchronous response callback for factoryReset API.
89          *
90          * @param headerOptions
91          *            It comprises of optionID and optionData as members.
92          * @param rep
93          *            Configuration parameters are carried as a pair of
94          *            attribute key and value in a form of OCRepresentation
95          *            instance.
96          * @param errorValue
97          *            error code.
98          */
99         public void onFactoryResetCallback(
100                 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
101                 int errorValue);
102     }
103
104     /**
105      * Set listener for receiving asynchronous response for Things Maintenance
106      * APIs.
107      *
108      * @param listener
109      *            IThingsMaintenanceListener to receive asynchronous response
110      *            for diagnostic feature APIs.
111      */
112     public void setThingsMaintenanceListener(IThingsMaintenanceListener listener) {
113         thingsMaintenanceListener = listener;
114     }
115
116     /**
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.
121      * <p>
122      * Listener should be set using setDiagnosticsListener API.
123      * <p>
124      * Listener IThingsMaintenanceListener::onRebootCallback will be notified
125      * when the response arrives.
126      *
127      * @param resource
128      *            resource pointer representing the target group
129      *
130      * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
131      *         code.
132      *
133      * @throws OcException {@link OcException}
134      */
135     public OCStackResult reboot(OcResource resource) throws OcException {
136
137         OCStackResult result;
138         if (null == thingsMaintenanceListener) {
139             result = OCStackResult.OC_STACK_LISTENER_NOT_SET;
140         } else {
141             int ordinal = nativeReboot(resource);
142             result = OCStackResult.conversion(ordinal);
143         }
144         return result;
145     }
146
147     /**
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.
154      *
155      * <p>
156      * Listener should be set using setDiagnosticsListener API.
157      * <p>
158      * Listener IThingsMaintenanceListener::onFactoryResetCallback will be
159      * notified when the response arrives.
160      *
161      * @param resource
162      *            resource pointer representing the target group
163      *
164      * @return OCStackResult - OC_STACK_OK on success, otherwise a failure error
165      *         code.
166      *
167      * @throws OcException {@link OcException}
168      */
169     public OCStackResult factoryReset(OcResource resource) throws OcException {
170
171         OCStackResult result;
172         if (null == thingsMaintenanceListener) {
173             result = OCStackResult.OC_STACK_LISTENER_NOT_SET;
174         } else {
175             int ordinal = nativeFactoryReset(resource);
176             result = OCStackResult.conversion(ordinal);
177         }
178         return result;
179     }
180
181     /**
182      * This callback method is called when a response for the reboot request
183      * just arrives.
184      *
185      * @param headerOptions
186      *            It comprises of optionID and optionData as members.
187      * @param rep
188      *            Configuration parameters are carried as a pair of attribute
189      *            key and value in a form of OCRepresentation instance.
190      * @param errorValue
191      *            error code.
192      */
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,
199                     errorValue);
200         }
201     }
202
203     /**
204      * This callback method is called when a response for the factoryReset
205      * request just arrives.
206      *
207      * @param headerOptions
208      *            It comprises of optionID and optionData as members.
209      * @param rep
210      *            Configuration parameters are carried as a pair of attribute
211      *            key and value in a form of OCRepresentation instance.
212      * @param errorValue
213      *            error code.
214      */
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,
221                     rep, errorValue);
222         }
223     }
224
225     /**
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.
229      *
230      * @return Returns the configuration list in JSON format.
231      */
232     public String getListOfSupportedMaintenanceUnits() {
233         return nativeGetListOfSupportedMaintenanceUnits();
234     }
235 }