7cbb4ff2ea92e297d0b8deecbc90a8947ed8fbfa
[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.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.*;
40
41 import android.os.Message;
42 import android.util.Log;
43
44 /*
45  * For Creating the Resources [configuration, Maintenance & FactoryRest]  &
46  * for Handling of the Client's Request
47  */
48 public class ConfigurationServer {
49     private final String                      LOG_TAG            = "[CON-SERVER]"
50                                                                          + this.getClass()
51                                                                                  .getSimpleName();
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;
58
59     private final ThingsConfigurationListener thingConfigurationListener;
60     private final ThingsMaintenanceListener   thingsMaintenanceListener;
61     private final RequestHandler              requestHandler;
62
63     // constructor
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();
71
72         thingsconfig.setConfigurationListener(thingConfigurationListener);
73         thingsmnt.setThingsMaintenanceListener(thingsMaintenanceListener);
74
75     }
76
77     // Creating resources : configuration, maintenance, factoryReset
78     public void CreateConfigurationResource() {
79         Log.i(LOG_TAG, "CreateConfigurationResource: enter");
80
81         try {
82             conResource = new ConfigurationResource();
83             conResource.createResource(requestHandler);
84
85             mntResource = new MaintenanceResource();
86             mntResource.createResource(requestHandler);
87
88             factorySetResource = new FactorySetResource();
89             factorySetResource.createResource(requestHandler);
90         } catch (OcException e) {
91             Log.e(LOG_TAG, "OcException occured: " + e.toString());
92         }
93
94         Log.i(LOG_TAG, "CreateConfigurationResource: exit");
95
96         String message = "Resources Created Successfully(Server is Ready)";
97
98         Message msg = Message.obtain();
99         msg.what = 0;
100         MainActivity mainActivityObj = MainActivity.getMainActivityObject();
101         MainActivity.setmessage(message);
102         mainActivityObj.getmHandler().sendMessage(msg);
103     }
104
105     public void DoBootStrap() {
106         Log.i(LOG_TAG, "DoBootStrap: enter");
107
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());
112         }
113         Log.i(LOG_TAG, "DoBootStrap: exit");
114     }
115
116     private class ThingsConfigurationListener implements IConfigurationListener {
117
118         @Override
119         public void onBootStrapCallback(Vector<OcHeaderOption> headerOptions,
120                 OcRepresentation rep, int errorValue) {
121
122             String message;
123             Log.i(LOG_TAG, "onBootStrapCallback");
124
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");
135
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
147                     + "\n";
148
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);
160
161             // showing the formed message on the UI
162             Message msg = Message.obtain();
163             msg.what = 0;
164             MainActivity mainActivityObj = MainActivity.getMainActivityObject();
165             MainActivity.setmessage(message);
166             mainActivityObj.getmHandler().sendMessage(msg);
167             // TODO Auto-generated method stub
168         }
169
170         @Override
171         public void onUpdateConfigurationsCallback(
172                 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
173                 int errorValue) {
174             Log.i(LOG_TAG, "onUpdateConfigurationsCallback");
175         }
176
177         @Override
178         public void onGetConfigurationsCallback(
179                 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
180                 int errorValue) {
181             Log.i(LOG_TAG, "onGetConfigurationsCallback");
182         }
183     }
184
185     private class ThingsMaintenanceListener implements
186             IThingsMaintenanceListener {
187
188         // Callback Function for Reboot
189         @Override
190         public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
191                 OcRepresentation rep, int errorValue) {
192             Log.i(LOG_TAG, "onRebootCallback");
193         }
194
195         // Callback Function for FactoryReset
196         @Override
197         public void onFactoryResetCallback(
198                 Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
199                 int errorValue) {
200             Log.i(LOG_TAG, "onFactoryResetCallback");
201         }
202     }
203
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();
212     }
213
214     private class RequestHandler implements OcPlatform.EntityHandler {
215
216         @Override
217         public EntityHandlerResult handleEntity(OcResourceRequest request) {
218             Log.i(LOG_TAG, "handleEntity: enter");
219
220             EntityHandlerResult result = EntityHandlerResult.ERROR;
221             if (null == request) {
222                 Log.e(LOG_TAG, "handleEntity: Invalid OcResourceRequest!");
223                 return result;
224             }
225
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());
233
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();
239                     if (null == rep) {
240                         Log.e(LOG_TAG,
241                                 "handleEntity: Invalid resource representation!");
242                         return result;
243                     }
244
245                     if (request.getResourceUri().equalsIgnoreCase(
246                             conResource.getUri())) {
247                         conResource.setConfigurationRepresentation(rep);
248                     } else if (request.getResourceUri().equalsIgnoreCase(
249                             mntResource.getUri())) {
250
251                         String factorySetAtt = rep.getValueString("fr");
252                         if (factorySetAtt.equalsIgnoreCase("true")) {
253                             conResource.factoryReset();
254                         }
255                         mntResource.setDiagnosticsRepresentation(rep);
256                     }
257                     sendResponse(request);
258                 }
259             }
260
261             Log.i(LOG_TAG, "handleEntity: exit");
262             return result;
263         }
264     }
265
266     // For sending response to the client
267     private void sendResponse(OcResourceRequest request) {
268         Log.i(LOG_TAG, "sendResponse: enter");
269
270         OcResourceResponse response = new OcResourceResponse();
271         OcRepresentation rep = null;
272
273         response.setRequestHandle(request.getRequestHandle());
274         response.setResourceHandle(request.getResourceHandle());
275
276         if (request.getResourceUri().equalsIgnoreCase(conResource.getUri())) {
277             rep = conResource.getConfigurationRepresentation();
278         } else if (request.getResourceUri().equalsIgnoreCase(
279                 mntResource.getUri())) {
280             rep = mntResource.getDiagnosticsRepresentation();
281         }
282         response.setResourceRepresentation(rep, OcPlatform.DEFAULT_INTERFACE);
283         response.setErrorCode(200);
284
285         try {
286             OcPlatform.sendResponse(response);
287         } catch (OcException e) {
288             Log.e(LOG_TAG, "sendResponse: OcException occured: " + e.toString());
289         }
290         Log.i(LOG_TAG, "sendResponse: exit");
291     }
292 }
293
294 // Default values for Resources
295 class ConfigurationDefaultValues {
296
297     // configuration Resource default values
298
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();
304
305     public static String ConURIPrefix           = "/oic/con";
306     public static String ConResourceTypePrefix  = "oic.wk.con";
307
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";
315 }