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 ******************************************************************/
21 #include "conserverapp.h"
26 #include "ThingsConfiguration.h"
27 #include "ThingsMaintenance.h"
28 #include "configurationresource.h"
29 #include "maintenanceresource.h"
30 #include "factorysetresource.h"
35 namespace PH = std::placeholders;
37 /* Default system configuration value's variables
38 The variable's names should be same as the names of "extern" variables defined in
39 "configurationresource.h" */
40 std::string defaultDeviceName;
41 std::string defaultLocation;
42 std::string defaultLocationName;
43 std::string defaultRegion;
44 std::string defaultCurrency;
46 static ThingsConfiguration *g_thingsConf;
48 const int SUCCESS_RESPONSE = 0;
50 bool resources_created = false;
52 // Forward declaring the entityHandler (Configuration)
53 bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request);
54 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest);
55 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request);
57 ConfigurationResource *myConfigurationResource;
58 MaintenanceResource *myMaintenanceResource;
59 FactorySetResource *myFactorySetResource;
61 typedef std::function< void(OCRepresentation &) > putFunc;
62 typedef std::function< OCRepresentation(void) > getFunc;
64 typedef struct appdata
68 Evas_Object *naviframe;
69 Evas_Object *scroller;
70 Evas_Object *layout, *base_layout;
71 Evas_Object *bootButton;
72 Evas_Object *createConfButton;
75 Evas_Object *log_entry;
77 std::string logMessage;
79 getFunc getGetFunction(std::string uri)
83 if (uri == myConfigurationResource->getUri())
85 res = std::bind(&ConfigurationResource::getConfigurationRepresentation,
86 myConfigurationResource);
88 else if (uri == myMaintenanceResource->getUri())
90 res = std::bind(&MaintenanceResource::getMaintenanceRepresentation,
91 myMaintenanceResource);
97 putFunc getPutFunction(std::string uri)
101 if (uri == myConfigurationResource->getUri())
103 res = std::bind(&ConfigurationResource::setConfigurationRepresentation,
104 myConfigurationResource, std::placeholders::_1);
106 else if (uri == myMaintenanceResource->getUri())
108 res = std::bind(&MaintenanceResource::setMaintenanceRepresentation,
109 myMaintenanceResource, std::placeholders::_1);
115 // This function prepares a response for the incoming request
116 bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request)
118 dlog_print(DLOG_INFO, LOG_TAG, "#### In Server CPP prepareResponseForResource");
122 // Get the request type and request flag
123 std::string requestType = request->getRequestType();
124 int requestFlag = request->getRequestHandlerFlag();
126 if (requestFlag == RequestHandlerFlag::RequestFlag)
128 dlog_print(DLOG_INFO, LOG_TAG, "#### requestFlag : Request");
130 // If the request type is GET
131 if (requestType == "GET")
133 dlog_print(DLOG_INFO, LOG_TAG, "#### requestType : GET");
135 // GET operations are directly handled while sending the response
138 else if (requestType == "PUT")
140 dlog_print(DLOG_INFO, LOG_TAG, "#### requestType : PUT");
142 OCRepresentation rep = request->getResourceRepresentation();
144 // Get appropriate function to be called for the PUT request
145 putFunction = getPutFunction(request->getResourceUri());
147 // Do related operations related to PUT request
151 else if (requestType == "POST")
153 // POST request operations
155 else if (requestType == "DELETE")
157 // DELETE request operations
160 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
162 dlog_print(DLOG_INFO, LOG_TAG, "#### requestFlag : Observer");
167 dlog_print(DLOG_INFO, LOG_TAG, "#### Request invalid");
173 // This function sends a response for the incoming request
174 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest)
176 auto pResponse = std::make_shared< OC::OCResourceResponse >();
178 // Check for query params (if any)
179 QueryParamsMap queryParamsMap = pRequest->getQueryParameters();
181 pResponse->setRequestHandle(pRequest->getRequestHandle());
182 pResponse->setResourceHandle(pRequest->getResourceHandle());
185 getFunction = getGetFunction(pRequest->getResourceUri());
187 OCRepresentation rep;
190 auto findRes = queryParamsMap.find("if");
192 if (findRes != queryParamsMap.end())
194 pResponse->setResourceRepresentation(rep, findRes->second);
198 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
201 pResponse->setErrorCode(200);
202 pResponse->setResponseResult(OC_EH_OK);
204 return OCPlatform::sendResponse(pResponse);
207 // This function handles the requests and sends the response
208 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request)
210 dlog_print(DLOG_INFO, LOG_TAG, "#### In Server CPP (entityHandlerForResource) entity"
212 OCEntityHandlerResult ehResult = OC_EH_ERROR;
214 if (prepareResponseForResource(request))
216 if (OC_STACK_OK == sendResponseForResource(request))
219 dlog_print(DLOG_INFO, LOG_TAG, "#### sendResponse success.");
223 dlog_print(DLOG_INFO, LOG_TAG, "#### sendResponse failed.");
228 dlog_print(DLOG_INFO, LOG_TAG, "#### PrepareResponse failed.");
233 // Updates the log in the UI
234 void *updateLog(void *data)
236 std::string *log = (std::string *)data;
240 dlog_print(DLOG_INFO, LOG_TAG, "#### No log !!!!");
245 elm_entry_entry_append(log_entry, log->c_str());
246 elm_entry_cursor_end_set(log_entry);
247 dlog_print(DLOG_INFO, LOG_TAG, "%s", log->c_str());
249 dlog_print(DLOG_INFO, LOG_TAG, "#### updateLog exit!!!!");
254 win_delete_request_cb(void *data , Evas_Object *obj , void *event_info)
259 // Function to delete all the configuration resources which are created
260 void deleteResources()
262 if (NULL != myConfigurationResource)
263 myConfigurationResource->deleteResource();
264 if (NULL != myMaintenanceResource)
265 myMaintenanceResource->deleteResource();
266 if (NULL != myFactorySetResource)
267 myFactorySetResource->deleteResource();
273 win_back_cb(void *data, Evas_Object *obj, void *event_info)
281 /* Callback Function to be called by the platform
282 when response arrives from the BootStrap Server */
283 void onBootStrapCallback(const HeaderOptions &headerOptions, const OCRepresentation &rep,
286 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap entry");
288 if (SUCCESS_RESPONSE != eCode)
290 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap -- onGET Response error: %d", eCode);
294 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap -- GET request was successful");
295 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap -- Resource URI: %s", rep.getUri().c_str());
297 logMessage = "----------------------------<br>";
298 logMessage += "GET request was successful<br>";
299 logMessage += "URI : " + rep.getUri() + "<br>";
301 defaultRegion = rep.getValue< std::string >(DEFAULT_REGION);
302 defaultCurrency = rep.getValue< std::string >(DEFAULT_CURRENCY);
303 defaultLocation = rep.getValue< std::string >(DEFAULT_LOCATION);
304 defaultLocationName = rep.getValue< std::string >(DEFAULT_LOCATIONNAME);
305 defaultDeviceName = rep.getValue< std::string >(DEFAULT_DEVICENAME);
307 logMessage += "Device Name : " + defaultDeviceName + "<br>";
308 logMessage += "Location : " + defaultLocation + "<br>";
309 logMessage += "Location Name : " + defaultLocationName + "<br>";
310 logMessage += "currency : " + defaultCurrency + "<br>";
311 logMessage += "Region : " + defaultRegion + "<br>";
313 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
315 //Call updateLog in the thread safe mode
316 ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
320 // Function to be called when the doBootStrap UI button is clicked
322 doBootStrap_cb(void *data , Evas_Object *obj , void *event_info)
325 if (NULL == g_thingsConf)
327 dlog_print(DLOG_ERROR, LOG_TAG, "#### doBootstrap returned g_thingsConf NULL check");
331 OCStackResult result = g_thingsConf->doBootstrap(&onBootStrapCallback);
333 if (OC_STACK_OK == result)
335 dlog_print(DLOG_INFO, LOG_TAG, "#### doBootstrap returned OC_STACK_OK");
339 dlog_print(DLOG_INFO, LOG_TAG, "#### doBootstrap failed");
343 // Function to be called when Create Configuration Resources UI button is clicked
345 createConfResource_cb(void *data , Evas_Object *obj , void *event_info)
347 logMessage = "----------------------------<br>";
348 if (!resources_created)
350 resources_created = true;
351 myConfigurationResource = new ConfigurationResource();
352 myConfigurationResource->createResource(&entityHandlerForResource);
354 myMaintenanceResource = new MaintenanceResource();
355 myMaintenanceResource->createResource(&entityHandlerForResource);
357 myFactorySetResource = new FactorySetResource();
358 myFactorySetResource->createResource(&entityHandlerForResource);
360 myMaintenanceResource->factoryReset = std::function < void()
361 > (std::bind(&ConfigurationResource::factoryReset,
362 myConfigurationResource));
364 logMessage += "Resources Created Successfully!!! Server is Ready!!!<br>";
368 logMessage += "Resources were created already!!! <br>";
371 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
372 // Show the log in the UI
373 ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
377 create_base_gui(appdata_s *ad)
380 ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
381 elm_win_autodel_set(ad->win, EINA_TRUE);
383 if (elm_win_wm_rotation_supported_get(ad->win))
385 int rots[4] = { 0, 90, 180, 270 };
386 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
389 evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
390 eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb, ad);
393 ad->conform = elm_conformant_add(ad->win);
394 evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
395 elm_win_resize_object_add(ad->win, ad->conform);
396 evas_object_show(ad->conform);
399 ad->base_layout = elm_layout_add(ad->conform);
400 evas_object_size_hint_weight_set(ad->base_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
401 elm_layout_theme_set(ad->base_layout, "layout", "application", "default");
402 evas_object_show(ad->base_layout);
404 elm_object_content_set(ad->conform, ad->base_layout);
407 ad->naviframe = elm_naviframe_add(ad->base_layout);
408 elm_object_part_content_set(ad->base_layout, "elm.swallow.content", ad->naviframe);
411 ad->scroller = elm_scroller_add(ad->naviframe);
412 elm_scroller_bounce_set(ad->scroller, EINA_FALSE, EINA_TRUE);
413 elm_scroller_policy_set(ad->scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
416 ad->layout = elm_layout_add(ad->naviframe);
417 evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, 0.0);
418 elm_layout_file_set(ad->layout, ELM_DEMO_EDJ, "mainpage_layout");
420 elm_object_content_set(ad->scroller, ad->layout);
422 ad->bootButton = elm_button_add(ad->layout);
423 elm_object_text_set(ad->bootButton, "doBootStrap");
424 evas_object_size_hint_weight_set(ad->bootButton, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
425 evas_object_size_hint_align_set(ad->bootButton, EVAS_HINT_FILL, EVAS_HINT_FILL);
426 evas_object_smart_callback_add(ad->bootButton, "clicked", doBootStrap_cb, ad);
427 elm_object_part_content_set(ad->layout, "bootstrap_button", ad->bootButton);
429 ad->createConfButton = elm_button_add(ad->layout);
430 elm_object_text_set(ad->createConfButton, "Create Configuration Resources");
431 evas_object_size_hint_weight_set(ad->createConfButton, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
432 evas_object_size_hint_align_set(ad->createConfButton, EVAS_HINT_FILL, EVAS_HINT_FILL);
433 evas_object_smart_callback_add(ad->createConfButton, "clicked", createConfResource_cb, ad);
434 elm_object_part_content_set(ad->layout, "create_conf_button", ad->createConfButton);
436 log_entry = elm_entry_add(ad->layout);
437 elm_entry_scrollable_set(log_entry, EINA_TRUE);
438 elm_entry_editable_set(log_entry, EINA_FALSE);
439 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
440 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
441 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
442 elm_object_part_content_set(ad->layout, "log", log_entry);
444 elm_naviframe_item_push(ad->naviframe, "Configuration Server", NULL, NULL, ad->scroller, NULL);
446 // Show window after base gui is set up
447 evas_object_show(ad->win);
450 // Function which configures the OCPlatform
456 PlatformConfig config
457 { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
459 OCPlatform::Configure(config);
461 dlog_print(DLOG_INFO, LOG_TAG, "#### Platform configuration done!!!!");
463 catch (OCException &e)
465 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
471 app_create(void *data)
473 /* Hook to take necessary actions before main event loop starts
474 Initialize UI resources and application's data
475 If this function returns true, the main loop of application starts
476 If this function returns false, the application is terminated */
477 appdata_s *ad = (appdata_s *)data;
479 elm_app_base_scale_set(1.8);
481 // Create and show the UI
484 // Configure the OCPlatform
485 configure_platform();
487 g_thingsConf = new ThingsConfiguration();
493 app_control(app_control_h app_control, void *data)
495 // Handle the launch request.
499 app_pause(void *data)
501 // Take necessary actions when application becomes invisible.
505 app_resume(void *data)
507 // Take necessary actions when application becomes visible.
511 app_terminate(void *data)
513 // Release all resources.
517 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
519 // APP_EVENT_LANGUAGE_CHANGED
521 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
522 elm_language_set(locale);
528 ui_app_orient_changed(app_event_info_h event_info, void *user_data)
530 // APP_EVENT_DEVICE_ORIENTATION_CHANGED
535 ui_app_region_changed(app_event_info_h event_info, void *user_data)
537 // APP_EVENT_REGION_FORMAT_CHANGED
541 ui_app_low_battery(app_event_info_h event_info, void *user_data)
543 // APP_EVENT_LOW_BATTERY
547 ui_app_low_memory(app_event_info_h event_info, void *user_data)
549 // APP_EVENT_LOW_MEMORY
553 main(int argc, char *argv[])
558 ui_app_lifecycle_callback_s event_callback = {0,};
559 app_event_handler_h handlers[5] = {NULL, };
561 event_callback.create = app_create;
562 event_callback.terminate = app_terminate;
563 event_callback.pause = app_pause;
564 event_callback.resume = app_resume;
565 event_callback.app_control = app_control;
567 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY,
568 ui_app_low_battery, &ad);
569 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY,
570 ui_app_low_memory, &ad);
571 ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED],
572 APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
573 ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED,
574 ui_app_lang_changed, &ad);
575 ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
576 APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
577 ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
579 ret = ui_app_main(argc, argv, &event_callback, &ad);
580 if (APP_ERROR_NONE != ret)
582 dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);