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 ******************************************************************/
20 package com.example.con_server;
22 import java.util.EnumSet;
23 import java.util.Vector;
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.OCStackResult;
35 import org.iotivity.service.tm.GroupManager;
36 import org.iotivity.service.tm.ThingsConfiguration;
37 import org.iotivity.service.tm.ThingsConfiguration.*;
38 import org.iotivity.service.tm.ThingsMaintenance;
39 import org.iotivity.service.tm.ThingsMaintenance.*;
41 import android.os.Message;
42 import android.util.Log;
45 * For Creating the Resources [configuration, Maintenance & FactoryRest] &
46 * for Handling of the Client's Request
48 public class ConfigurationServer {
49 private final String LOG_TAG = "[CON-SERVER]"
52 private GroupManager groupmanager = null;
53 private ThingsConfiguration thingsconfig = null;
54 private ThingsMaintenance thingsmnt = null;
55 private ConfigurationResource conResource = null;
56 private MaintenanceResource mntResource = null;
57 private FactorySetResource factorySetResource = null;
59 private final ThingsConfigurationListener thingConfigurationListener;
60 private final ThingsMaintenanceListener thingsMaintenanceListener;
61 private final RequestHandler requestHandler;
64 public ConfigurationServer() {
65 groupmanager = new GroupManager();
66 thingsconfig = ThingsConfiguration.getInstance();
67 thingsmnt = ThingsMaintenance.getInstance();
68 thingConfigurationListener = new ThingsConfigurationListener();
69 thingsMaintenanceListener = new ThingsMaintenanceListener();
70 requestHandler = new RequestHandler();
72 thingsconfig.setConfigurationListener(thingConfigurationListener);
73 thingsmnt.setThingsMaintenanceListener(thingsMaintenanceListener);
77 // Creating resources : configuration, maintenance, factoryReset
78 public void CreateConfigurationResource() {
79 Log.i(LOG_TAG, "CreateConfigurationResource: enter");
82 conResource = new ConfigurationResource();
83 conResource.createResource(requestHandler);
85 mntResource = new MaintenanceResource();
86 mntResource.createResource(requestHandler);
88 factorySetResource = new FactorySetResource();
89 factorySetResource.createResource(requestHandler);
90 } catch (OcException e) {
91 Log.e(LOG_TAG, "OcException occured: " + e.toString());
94 Log.i(LOG_TAG, "CreateConfigurationResource: exit");
96 String message = "Resources Created Successfully(Server is Ready)";
98 Message msg = Message.obtain();
100 MainActivity mainActivityObj = MainActivity.getMainActivityObject();
101 MainActivity.setmessage(message);
102 mainActivityObj.getmHandler().sendMessage(msg);
105 public void DoBootStrap() {
106 Log.i(LOG_TAG, "DoBootStrap: enter");
108 OCStackResult result = thingsconfig.doBootstrap();
109 if (OCStackResult.OC_STACK_ERROR == result) {
110 Log.e(LOG_TAG, "doBootStrap returned error: "
111 + OCStackResult.OC_STACK_ERROR.name());
113 Log.i(LOG_TAG, "DoBootStrap: exit");
116 private class ThingsConfigurationListener implements IConfigurationListener {
119 public void onBootStrapCallback(Vector<OcHeaderOption> headerOptions,
120 OcRepresentation rep, int errorValue) {
123 Log.i(LOG_TAG, "onBootStrapCallback");
125 // setting the default values received from bootstrap Server
126 ConfigurationDefaultValues.defaultDeviceName = rep
127 .getValueString("n");
128 ConfigurationDefaultValues.defaultLocation = rep
129 .getValueString("loc");
130 ConfigurationDefaultValues.defaultLocationName = rep
131 .getValueString("locn");
132 ConfigurationDefaultValues.defaultCurrency = rep
133 .getValueString("c");
134 ConfigurationDefaultValues.defaultRegion = rep.getValueString("r");
136 // forming the message to display on UI
137 message = "URI : " + rep.getUri() + "\n";
138 message += "Device Name : "
139 + ConfigurationDefaultValues.defaultDeviceName + "\n";
140 message += "Location : "
141 + ConfigurationDefaultValues.defaultLocation + "\n";
142 message += "Location Name : "
143 + ConfigurationDefaultValues.defaultLocationName + "\n";
144 message += "Currency : "
145 + ConfigurationDefaultValues.defaultCurrency + "\n";
146 message += "Region : " + ConfigurationDefaultValues.defaultRegion
149 Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
150 Log.i(LOG_TAG, "Region: "
151 + ConfigurationDefaultValues.defaultRegion);
152 Log.i(LOG_TAG, "Device Name: "
153 + ConfigurationDefaultValues.defaultDeviceName);
154 Log.i(LOG_TAG, "Location: "
155 + ConfigurationDefaultValues.defaultLocation);
156 Log.i(LOG_TAG, "Location Name: "
157 + ConfigurationDefaultValues.defaultLocationName);
158 Log.i(LOG_TAG, "Currency: "
159 + ConfigurationDefaultValues.defaultCurrency);
161 // showing the formed message on the UI
162 Message msg = Message.obtain();
164 MainActivity mainActivityObj = MainActivity.getMainActivityObject();
165 MainActivity.setmessage(message);
166 mainActivityObj.getmHandler().sendMessage(msg);
167 // TODO Auto-generated method stub
171 public void onUpdateConfigurationsCallback(
172 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
174 Log.i(LOG_TAG, "onUpdateConfigurationsCallback");
178 public void onGetConfigurationsCallback(
179 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
181 Log.i(LOG_TAG, "onGetConfigurationsCallback");
185 private class ThingsMaintenanceListener implements
186 IThingsMaintenanceListener {
188 // Callback Function for Reboot
190 public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
191 OcRepresentation rep, int errorValue) {
192 Log.i(LOG_TAG, "onRebootCallback");
195 // Callback Function for FactoryReset
197 public void onFactoryResetCallback(
198 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
200 Log.i(LOG_TAG, "onFactoryResetCallback");
204 // For deleting all the resources
205 public void deleteResources() {
206 if (null != conResource)
207 conResource.deleteResource();
208 if (null != mntResource)
209 mntResource.deleteResource();
210 if (null != factorySetResource)
211 factorySetResource.deleteResource();
214 private class RequestHandler implements OcPlatform.EntityHandler {
217 public EntityHandlerResult handleEntity(OcResourceRequest request) {
218 Log.i(LOG_TAG, "handleEntity: enter");
220 EntityHandlerResult result = EntityHandlerResult.ERROR;
221 if (null == request) {
222 Log.e(LOG_TAG, "handleEntity: Invalid OcResourceRequest!");
226 RequestType requestType = request.getRequestType();
227 EnumSet<RequestHandlerFlag> requestHandlerFlag = request
228 .getRequestHandlerFlagSet();
229 Log.i(LOG_TAG, "prepareResponseForResource: request type: "
230 + requestType.name());
231 Log.i(LOG_TAG, "prepareResponseForResource: request for resource: "
232 + request.getResourceUri());
234 if (requestHandlerFlag.contains(RequestHandlerFlag.REQUEST)) {
235 if (RequestType.GET == requestType) {
236 sendResponse(request);
237 } else if (RequestType.PUT == requestType) {
238 OcRepresentation rep = request.getResourceRepresentation();
241 "handleEntity: Invalid resource representation!");
245 if (request.getResourceUri().equalsIgnoreCase(
246 conResource.getUri())) {
247 conResource.setConfigurationRepresentation(rep);
248 } else if (request.getResourceUri().equalsIgnoreCase(
249 mntResource.getUri())) {
251 String factorySetAtt = rep.getValueString("fr");
252 if (factorySetAtt.equalsIgnoreCase("true")) {
253 conResource.factoryReset();
255 mntResource.setDiagnosticsRepresentation(rep);
257 sendResponse(request);
261 Log.i(LOG_TAG, "handleEntity: exit");
266 // For sending response to the client
267 private void sendResponse(OcResourceRequest request) {
268 Log.i(LOG_TAG, "sendResponse: enter");
270 OcResourceResponse response = new OcResourceResponse();
271 OcRepresentation rep = null;
273 response.setRequestHandle(request.getRequestHandle());
274 response.setResourceHandle(request.getResourceHandle());
276 if (request.getResourceUri().equalsIgnoreCase(conResource.getUri())) {
277 rep = conResource.getConfigurationRepresentation();
278 } else if (request.getResourceUri().equalsIgnoreCase(
279 mntResource.getUri())) {
280 rep = mntResource.getDiagnosticsRepresentation();
282 response.setResourceRepresentation(rep, OcPlatform.DEFAULT_INTERFACE);
283 response.setErrorCode(200);
286 OcPlatform.sendResponse(response);
287 } catch (OcException e) {
288 Log.e(LOG_TAG, "sendResponse: OcException occured: " + e.toString());
290 Log.i(LOG_TAG, "sendResponse: exit");
294 // Default values for Resources
295 class ConfigurationDefaultValues {
297 // configuration Resource default values
299 public static String defaultDeviceName = new String();
300 public static String defaultLocation = new String();
301 public static String defaultLocationName = new String();
302 public static String defaultCurrency = new String();
303 public static String defaultRegion = new String();
305 public static String ConURIPrefix = "/oic/con";
306 public static String ConResourceTypePrefix = "oic.wk.con";
308 // Diagnostics Resource default values
309 public static String diagURIPrefix = "/oic/mnt";
310 public static String diagResourceTypePrefix = "oic.wk.mnt";
311 public static String diagnosticsValue = "false";
312 public static String defaultFactoryReset = "false";
313 public static String defaultReboot = "false";
314 public static String defaultStartCollection = "false";