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.IConfigurationListener;
35 import org.iotivity.service.tm.IDiagnosticsListener;
36 import org.iotivity.service.tm.OCStackResult;
37 import org.iotivity.service.tm.ThingsManager;
39 import android.os.Message;
40 import android.util.Log;
43 * For Creating the Resources [configurtion, Diagnostic & FactoryRest] &
44 * for Handling of the Client's Request
46 public class ConfigurationServer implements IDiagnosticsListener,
47 IConfigurationListener, OcPlatform.EntityHandler {
48 private final String LOG_TAG = "[CON-SERVER]"
51 private ThingsManager thingsmanager = null;
52 private ConfigurationResource conResource = null;
53 private DiagnosticsResource diagResource = null;
54 private FactorySetResource factorySetResource = null;
57 public ConfigurationServer() {
58 thingsmanager = new ThingsManager();
59 thingsmanager.setDiagnosticsListener(this);
60 thingsmanager.setConfigurationListener(this);
63 public void DoBootStrap() {
64 Log.i(LOG_TAG, "DoBootStrap: enter");
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());
71 Log.i(LOG_TAG, "DoBootStrap: exit");
74 // Creating resources : configuration, diagnostics, factoryReset
75 public void CreateConfigurationResource() {
76 Log.i(LOG_TAG, "CreateConfigurationResource: enter");
79 conResource = new ConfigurationResource();
80 conResource.createResource(this);
82 diagResource = new DiagnosticsResource();
83 diagResource.createResource(this);
85 factorySetResource = new FactorySetResource();
86 factorySetResource.createResource(this);
87 } catch (OcException e) {
88 Log.e(LOG_TAG, "OcException occured: " + e.toString());
91 Log.i(LOG_TAG, "CreateConfigurationResource: exit");
93 String message = "Resources Created Successfully(Server is Ready)";
95 Message msg = Message.obtain();
97 MainActivity mainActivityObj = MainActivity.getMainActivityObject();
98 MainActivity.setmessage(message);
99 mainActivityObj.getmHandler().sendMessage(msg);
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();
113 // Callback Function for doBootStrap
115 public void onBootStrapCallback(Vector<OcHeaderOption> headerOptions,
116 OcRepresentation rep, int errorValue) {
118 Log.i(LOG_TAG, "onBootStrapCallback");
120 // setting the default values received from bootstrap Server
122 ConfigurationDefaultValues.defaultRegion = rep.getValueString("r");
123 ConfigurationDefaultValues.defaultSystemTime = rep.getValueString("st");
124 ConfigurationDefaultValues.defaultCurrency = rep.getValueString("c");
125 ConfigurationDefaultValues.defaultLocation = rep.getValueString("loc");
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";
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);
147 // showing the formed message on the UI
148 Message msg = Message.obtain();
150 MainActivity mainActivityObj = MainActivity.getMainActivityObject();
151 MainActivity.setmessage(message);
152 mainActivityObj.getmHandler().sendMessage(msg);
155 // Callback Function for Reboot
157 public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
158 OcRepresentation rep, int errorValue) {
159 Log.i(LOG_TAG, "onRebootCallback");
162 // Callback Function for FactoryReset
164 public void onFactoryResetCallback(Vector<OcHeaderOption> headerOptions,
165 OcRepresentation rep, int errorValue) {
166 Log.i(LOG_TAG, "onFactoryResetCallback");
169 // For Handling the Client's Request
171 public EntityHandlerResult handleEntity(OcResourceRequest request) {
172 Log.i(LOG_TAG, "handleEntity: enter");
174 EntityHandlerResult result = EntityHandlerResult.ERROR;
175 if (null == request) {
176 Log.e(LOG_TAG, "handleEntity: Invalid OcResourceRequest!");
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());
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();
195 "handleEntity: Invalid resource representation!");
199 if (request.getResourceUri().equalsIgnoreCase(
200 conResource.getUri())) {
201 conResource.setConfigurationRepresentation(rep);
202 } else if (request.getResourceUri().equalsIgnoreCase(
203 diagResource.getUri())) {
205 String factorySetAtt = rep.getValueString("fr");
206 if (factorySetAtt.equalsIgnoreCase("true")) {
207 conResource.factoryReset();
209 diagResource.setDiagnosticsRepresentation(rep);
211 sendResponse(request);
215 Log.i(LOG_TAG, "handleEntity: exit");
219 // For sending response to the client
220 private void sendResponse(OcResourceRequest request) {
221 Log.i(LOG_TAG, "sendResponse: enter");
223 OcResourceResponse response = new OcResourceResponse();
224 OcRepresentation rep = null;
226 response.setRequestHandle(request.getRequestHandle());
227 response.setResourceHandle(request.getResourceHandle());
229 if (request.getResourceUri().equalsIgnoreCase(conResource.getUri())) {
230 rep = conResource.getConfigurationRepresentation();
231 } else if (request.getResourceUri().equalsIgnoreCase(
232 diagResource.getUri())) {
233 rep = diagResource.getDiagnosticsRepresentation();
235 response.setResourceRepresentation(rep, OcPlatform.DEFAULT_INTERFACE);
236 response.setErrorCode(200);
239 OcPlatform.sendResponse(response);
240 } catch (OcException e) {
241 Log.e(LOG_TAG, "sendResponse: OcException occured: " + e.toString());
243 Log.i(LOG_TAG, "sendResponse: exit");
247 public void onUpdateConfigurationsCallback(
248 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
250 // TODO Auto-generated method stub
255 public void onGetConfigurationsCallback(
256 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
258 // TODO Auto-generated method stub
263 // Default values for Resources
264 class ConfigurationDefaultValues {
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";
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";