Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / con-server / src / com / example / con_server / ConfigurationServer.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 package com.example.con_server;
21
22 import java.util.EnumSet;
23 import java.util.Vector;
24
25 import org.iotivity.base.EntityHandlerResult;
26 import org.iotivity.base.OcException;
27 import org.iotivity.base.OcHeaderOption;
28 import org.iotivity.base.OcPlatform;
29 import org.iotivity.base.OcRepresentation;
30 import org.iotivity.base.OcResourceRequest;
31 import org.iotivity.base.OcResourceResponse;
32 import org.iotivity.base.RequestHandlerFlag;
33 import org.iotivity.base.RequestType;
34 import org.iotivity.service.tm.IConfigurationListener;
35 import org.iotivity.service.tm.IDiagnosticsListener;
36 import org.iotivity.service.tm.OCStackResult;
37 import org.iotivity.service.tm.ThingsManager;
38
39 import android.os.Message;
40 import android.util.Log;
41
42 /*
43  * For Creating the Resources [configurtion, Diagnostic & FactoryRest]  &
44  * for Handling of the Client's Request
45  */
46 public class ConfigurationServer implements IDiagnosticsListener,
47         IConfigurationListener, OcPlatform.EntityHandler {
48     private final String          LOG_TAG            = "[CON-SERVER]"
49                                                              + this.getClass()
50                                                                      .getSimpleName();
51     private ThingsManager         thingsmanager      = null;
52     private ConfigurationResource conResource        = null;
53     private DiagnosticsResource   diagResource       = null;
54     private FactorySetResource    factorySetResource = null;
55
56     // constructor
57     public ConfigurationServer() {
58         thingsmanager = new ThingsManager();
59         thingsmanager.setDiagnosticsListener(this);
60         thingsmanager.setConfigurationListener(this);
61     }
62
63     public void DoBootStrap() {
64         Log.i(LOG_TAG, "DoBootStrap: enter");
65
66         OCStackResult result = thingsmanager.doBootstrap();
67         if (OCStackResult.OC_STACK_ERROR == result) {
68             Log.e(LOG_TAG, "doBootStrap returned error: "
69                     + OCStackResult.OC_STACK_ERROR.name());
70         }
71         Log.i(LOG_TAG, "DoBootStrap: exit");
72     }
73
74     // Creating resources : configuration, diagnostics, factoryReset
75     public void CreateConfigurationResource() {
76         Log.i(LOG_TAG, "CreateConfigurationResource: enter");
77
78         try {
79             conResource = new ConfigurationResource();
80             conResource.createResource(this);
81
82             diagResource = new DiagnosticsResource();
83             diagResource.createResource(this);
84
85             factorySetResource = new FactorySetResource();
86             factorySetResource.createResource(this);
87         } catch (OcException e) {
88             Log.e(LOG_TAG, "OcException occured: " + e.toString());
89         }
90
91         Log.i(LOG_TAG, "CreateConfigurationResource: exit");
92
93         String message = "Resources Created Successfully(Server is Ready)";
94
95         Message msg = Message.obtain();
96         msg.what = 0;
97         MainActivity mainActivityObj = MainActivity.getMainActivityObject();
98         MainActivity.setmessage(message);
99         mainActivityObj.getmHandler().sendMessage(msg);
100
101     }
102
103     // For deleting all the resources
104     public void deleteResources() {
105         if (null != conResource)
106             conResource.deleteResource();
107         if (null != diagResource)
108             diagResource.deleteResource();
109         if (null != factorySetResource)
110             factorySetResource.deleteResource();
111     }
112
113     // Callback Function for doBootStrap
114     @Override
115     public void onBootStrapCallback(Vector<OcHeaderOption> headerOptions,
116             OcRepresentation rep, int errorValue) {
117         String message;
118         Log.i(LOG_TAG, "onBootStrapCallback");
119
120         // setting the default values received from bootstrap Server
121
122         ConfigurationDefaultValues.defaultRegion = rep.getValueString("r");
123         ConfigurationDefaultValues.defaultSystemTime = rep.getValueString("st");
124         ConfigurationDefaultValues.defaultCurrency = rep.getValueString("c");
125         ConfigurationDefaultValues.defaultLocation = rep.getValueString("loc");
126
127         // forming the message to display on UI
128         message = "URI : " + rep.getUri() + "\n";
129         message = message + "Region : "
130                 + ConfigurationDefaultValues.defaultRegion + "\n";
131         message = message + "System Time : "
132                 + ConfigurationDefaultValues.defaultSystemTime + "\n";
133         message = message + "Currency : "
134                 + ConfigurationDefaultValues.defaultCurrency + "\n";
135         message = message + "Location : "
136                 + ConfigurationDefaultValues.defaultLocation + "\n";
137
138         Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
139         Log.i(LOG_TAG, "Region: " + ConfigurationDefaultValues.defaultRegion);
140         Log.i(LOG_TAG, "System Time: "
141                 + ConfigurationDefaultValues.defaultSystemTime);
142         Log.i(LOG_TAG, "Currency: "
143                 + ConfigurationDefaultValues.defaultCurrency);
144         Log.i(LOG_TAG, "Location: "
145                 + ConfigurationDefaultValues.defaultLocation);
146
147         // showing the formed message on the UI
148         Message msg = Message.obtain();
149         msg.what = 0;
150         MainActivity mainActivityObj = MainActivity.getMainActivityObject();
151         MainActivity.setmessage(message);
152         mainActivityObj.getmHandler().sendMessage(msg);
153     }
154
155     // Callback Function for Reboot
156     @Override
157     public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
158             OcRepresentation rep, int errorValue) {
159         Log.i(LOG_TAG, "onRebootCallback");
160     }
161
162     // Callback Function for FactoryReset
163     @Override
164     public void onFactoryResetCallback(Vector<OcHeaderOption> headerOptions,
165             OcRepresentation rep, int errorValue) {
166         Log.i(LOG_TAG, "onFactoryResetCallback");
167     }
168
169     // For Handling the Client's Request
170     @Override
171     public EntityHandlerResult handleEntity(OcResourceRequest request) {
172         Log.i(LOG_TAG, "handleEntity: enter");
173
174         EntityHandlerResult result = EntityHandlerResult.ERROR;
175         if (null == request) {
176             Log.e(LOG_TAG, "handleEntity: Invalid OcResourceRequest!");
177             return result;
178         }
179
180         RequestType requestType = request.getRequestType();
181         EnumSet<RequestHandlerFlag> requestHandlerFlag = request
182                 .getRequestHandlerFlagSet();
183         Log.i(LOG_TAG, "prepareResponseForResource: request type: "
184                 + requestType.name());
185         Log.i(LOG_TAG, "prepareResponseForResource: request for resource: "
186                 + request.getResourceUri());
187
188         if (requestHandlerFlag.contains(RequestHandlerFlag.REQUEST)) {
189             if (RequestType.GET == requestType) {
190                 sendResponse(request);
191             } else if (RequestType.PUT == requestType) {
192                 OcRepresentation rep = request.getResourceRepresentation();
193                 if (null == rep) {
194                     Log.e(LOG_TAG,
195                             "handleEntity: Invalid resource representation!");
196                     return result;
197                 }
198
199                 if (request.getResourceUri().equalsIgnoreCase(
200                         conResource.getUri())) {
201                     conResource.setConfigurationRepresentation(rep);
202                 } else if (request.getResourceUri().equalsIgnoreCase(
203                         diagResource.getUri())) {
204
205                     String factorySetAtt = rep.getValueString("fr");
206                     if (factorySetAtt.equalsIgnoreCase("true")) {
207                         conResource.factoryReset();
208                     }
209                     diagResource.setDiagnosticsRepresentation(rep);
210                 }
211                 sendResponse(request);
212             }
213         }
214
215         Log.i(LOG_TAG, "handleEntity: exit");
216         return result;
217     }
218
219     // For sending response to the client
220     private void sendResponse(OcResourceRequest request) {
221         Log.i(LOG_TAG, "sendResponse: enter");
222
223         OcResourceResponse response = new OcResourceResponse();
224         OcRepresentation rep = null;
225
226         response.setRequestHandle(request.getRequestHandle());
227         response.setResourceHandle(request.getResourceHandle());
228
229         if (request.getResourceUri().equalsIgnoreCase(conResource.getUri())) {
230             rep = conResource.getConfigurationRepresentation();
231         } else if (request.getResourceUri().equalsIgnoreCase(
232                 diagResource.getUri())) {
233             rep = diagResource.getDiagnosticsRepresentation();
234         }
235         response.setResourceRepresentation(rep, OcPlatform.DEFAULT_INTERFACE);
236         response.setErrorCode(200);
237
238         try {
239             OcPlatform.sendResponse(response);
240         } catch (OcException e) {
241             Log.e(LOG_TAG, "sendResponse: OcException occured: " + e.toString());
242         }
243         Log.i(LOG_TAG, "sendResponse: exit");
244     }
245
246     @Override
247     public void onUpdateConfigurationsCallback(
248             Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
249             int errorValue) {
250         // TODO Auto-generated method stub
251
252     }
253
254     @Override
255     public void onGetConfigurationsCallback(
256             Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
257             int errorValue) {
258         // TODO Auto-generated method stub
259
260     }
261 }
262
263 // Default values for Resources
264 class ConfigurationDefaultValues {
265
266     // configuration Resource default values
267     public static String defaultLocation        = new String();
268     public static String defaultRegion          = new String();
269     public static String defaultSystemTime      = new String();
270     public static String defaultCurrency        = new String();
271     public static String ConURIPrefix           = "/oic/con";
272     public static String ConResourceTypePrefix  = "oic.con";
273
274     // Diagnostics Resource default values
275     public static String diagURIPrefix          = "/oic/diag";
276     public static String diagResourceTypePrefix = "oic.diag";
277     public static String diagnosticsValue       = "false";
278     public static String defaultFactoryReset    = "false";
279     public static String defaultReboot          = "false";
280     public static String defaultStartCollection = "false";
281 }