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 "configurationresource.h"
27 #include "diagnosticsresource.h"
28 #include "factorysetresource.h"
34 namespace PH = std::placeholders;
36 /* Default system configuration value's variables
37 The variable's names should be same as the names of "extern" variables defined in
38 "configurationresource.h" */
39 std::string defaultLocation;
40 std::string defaultRegion;
41 std::string defaultSystemTime;
42 std::string defaultCurrency;
44 static ThingsManager *g_thingsmanager;
46 const int SUCCESS_RESPONSE = 0;
48 bool resources_created = false;
50 // Forward declaring the entityHandler (Configuration)
51 bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request);
52 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest);
53 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request);
55 ConfigurationResource *myConfigurationResource;
56 DiagnosticsResource *myDiagnosticsResource;
57 FactorySetResource *myFactorySetResource;
59 typedef std::function< void(OCRepresentation &) > putFunc;
60 typedef std::function< OCRepresentation(void) > getFunc;
62 typedef struct appdata
66 Evas_Object *naviframe;
67 Evas_Object *scroller;
68 Evas_Object *layout, *base_layout;
69 Evas_Object *bootButton;
70 Evas_Object *createConfButton;
73 Evas_Object *log_entry;
75 std::string logMessage;
77 getFunc getGetFunction(std::string uri)
81 if (uri == myConfigurationResource->getUri())
83 res = std::bind(&ConfigurationResource::getConfigurationRepresentation,
84 myConfigurationResource);
86 else if (uri == myDiagnosticsResource->getUri())
88 res = std::bind(&DiagnosticsResource::getDiagnosticsRepresentation,
89 myDiagnosticsResource);
95 putFunc getPutFunction(std::string uri)
99 if (uri == myConfigurationResource->getUri())
101 res = std::bind(&ConfigurationResource::setConfigurationRepresentation,
102 myConfigurationResource, std::placeholders::_1);
104 else if (uri == myDiagnosticsResource->getUri())
106 res = std::bind(&DiagnosticsResource::setDiagnosticsRepresentation,
107 myDiagnosticsResource, std::placeholders::_1);
113 // This function prepares a response for the incoming request
114 bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request)
116 dlog_print(DLOG_INFO, LOG_TAG, "#### In Server CPP prepareResponseForResource");
120 // Get the request type and request flag
121 std::string requestType = request->getRequestType();
122 int requestFlag = request->getRequestHandlerFlag();
124 if (requestFlag == RequestHandlerFlag::RequestFlag)
126 dlog_print(DLOG_INFO, LOG_TAG, "#### requestFlag : Request");
128 // If the request type is GET
129 if (requestType == "GET")
131 dlog_print(DLOG_INFO, LOG_TAG, "#### requestType : GET");
133 // GET operations are directly handled while sending the response
136 else if (requestType == "PUT")
138 dlog_print(DLOG_INFO, LOG_TAG, "#### requestType : PUT");
140 OCRepresentation rep = request->getResourceRepresentation();
142 // Get appropriate function to be called for the PUT request
143 putFunction = getPutFunction(request->getResourceUri());
145 // Do related operations related to PUT request
149 else if (requestType == "POST")
151 // POST request operations
153 else if (requestType == "DELETE")
155 // DELETE request operations
158 else if (requestFlag == RequestHandlerFlag::ObserverFlag)
160 dlog_print(DLOG_INFO, LOG_TAG, "#### requestFlag : Observer");
165 dlog_print(DLOG_INFO, LOG_TAG, "#### Request invalid");
171 // This function sends a response for the incoming request
172 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest)
174 auto pResponse = std::make_shared< OC::OCResourceResponse >();
176 // Check for query params (if any)
177 QueryParamsMap queryParamsMap = pRequest->getQueryParameters();
179 pResponse->setRequestHandle(pRequest->getRequestHandle());
180 pResponse->setResourceHandle(pRequest->getResourceHandle());
183 getFunction = getGetFunction(pRequest->getResourceUri());
185 OCRepresentation rep;
188 auto findRes = queryParamsMap.find("if");
190 if (findRes != queryParamsMap.end())
192 pResponse->setResourceRepresentation(rep, findRes->second);
196 pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
199 pResponse->setErrorCode(200);
200 pResponse->setResponseResult(OC_EH_OK);
202 return OCPlatform::sendResponse(pResponse);
205 // This function handles the requests and sends the response
206 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request)
208 dlog_print(DLOG_INFO, LOG_TAG, "#### In Server CPP (entityHandlerForResource) entity"
210 OCEntityHandlerResult ehResult = OC_EH_ERROR;
212 if (prepareResponseForResource(request))
214 if (OC_STACK_OK == sendResponseForResource(request))
217 dlog_print(DLOG_INFO, LOG_TAG, "#### sendResponse success.");
221 dlog_print(DLOG_INFO, LOG_TAG, "#### sendResponse failed.");
226 dlog_print(DLOG_INFO, LOG_TAG, "#### PrepareResponse failed.");
231 // Updates the log in the UI
232 void *updateLog(void *data)
234 string *log = (string *)data;
236 elm_entry_entry_append(log_entry, log->c_str());
237 elm_entry_cursor_end_set(log_entry);
239 dlog_print(DLOG_INFO, LOG_TAG, "%s", log->c_str());
240 dlog_print(DLOG_INFO, LOG_TAG, "#### updateLog exit!!!!");
246 win_delete_request_cb(void *data , Evas_Object *obj , void *event_info)
251 // Function to delete all the configuration resources which are created
252 void deleteResources()
254 if (NULL != myConfigurationResource)
255 myConfigurationResource->deleteResource();
256 if (NULL != myDiagnosticsResource)
257 myDiagnosticsResource->deleteResource();
258 if (NULL != myFactorySetResource)
259 myFactorySetResource->deleteResource();
263 win_back_cb(void *data, Evas_Object *obj, void *event_info)
271 /* Callback Function to be called by the platform
272 when response arrives from the BootStrap Server */
273 void onBootStrapCallback(const HeaderOptions &headerOptions, const OCRepresentation &rep,
276 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap entry");
278 if (SUCCESS_RESPONSE != eCode)
280 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap -- onGET Response error: %d", eCode);
284 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap -- GET request was successful");
285 dlog_print(DLOG_INFO, LOG_TAG, "#### onBootStrap -- Resource URI: %s", rep.getUri().c_str());
287 logMessage = "----------------------------<br>";
288 logMessage += "GET request was successful<br>";
289 logMessage += "URI : " + rep.getUri() + "<br>";
291 defaultRegion = rep.getValue< std::string >("r");
292 defaultSystemTime = rep.getValue< std::string >("st");
293 defaultCurrency = rep.getValue< std::string >("c");
294 defaultLocation = rep.getValue< std::string >("loc");
296 logMessage += "Location : " + defaultLocation + "<br>";
297 logMessage += "SystemTime : " + defaultSystemTime + "<br>";
298 logMessage += "currency : " + defaultCurrency + "<br>";
299 logMessage += "Region : " + defaultRegion + "<br>";
301 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
303 //Call updateLog in the thread safe mode
304 ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
308 // Function to be called when the doBootStrap UI button is clicked
310 doBootStrap_cb(void *data , Evas_Object *obj , void *event_info)
312 OCStackResult result = g_thingsmanager->doBootstrap(&onBootStrapCallback);
313 if (OC_STACK_OK == result)
315 dlog_print(DLOG_INFO, LOG_TAG, "#### doBootstrap returned OC_STACK_OK");
319 dlog_print(DLOG_INFO, LOG_TAG, "#### doBootstrap failed");
323 // Function to be called when Create Configuration Resources UI button is clicked
325 createConfResource_cb(void *data , Evas_Object *obj , void *event_info)
327 logMessage = "----------------------------<br>";
328 if (!resources_created)
330 resources_created = true;
331 myConfigurationResource = new ConfigurationResource();
332 myConfigurationResource->createResource(&entityHandlerForResource);
334 myDiagnosticsResource = new DiagnosticsResource();
335 myDiagnosticsResource->createResource(&entityHandlerForResource);
337 myFactorySetResource = new FactorySetResource();
338 myFactorySetResource->createResource(&entityHandlerForResource);
340 myDiagnosticsResource->factoryReset = std::function < void()
341 > (std::bind(&ConfigurationResource::factoryReset,
342 myConfigurationResource));
344 logMessage += "Resources Created Successfully!!! Server is Ready!!!<br>";
348 logMessage += "Resources were created already!!! <br>";
351 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
352 // Show the log in the UI
353 ecore_main_loop_thread_safe_call_sync(updateLog, &logMessage);
357 create_base_gui(appdata_s *ad)
360 ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
361 elm_win_autodel_set(ad->win, EINA_TRUE);
363 if (elm_win_wm_rotation_supported_get(ad->win))
365 int rots[4] = { 0, 90, 180, 270 };
366 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
369 evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
370 eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb, ad);
373 ad->conform = elm_conformant_add(ad->win);
374 evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
375 elm_win_resize_object_add(ad->win, ad->conform);
376 evas_object_show(ad->conform);
379 ad->base_layout = elm_layout_add(ad->conform);
380 evas_object_size_hint_weight_set(ad->base_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
381 elm_layout_theme_set(ad->base_layout, "layout", "application", "default");
382 evas_object_show(ad->base_layout);
384 elm_object_content_set(ad->conform, ad->base_layout);
387 ad->naviframe = elm_naviframe_add(ad->base_layout);
388 elm_object_part_content_set(ad->base_layout, "elm.swallow.content", ad->naviframe);
391 ad->scroller = elm_scroller_add(ad->naviframe);
392 elm_scroller_bounce_set(ad->scroller, EINA_FALSE, EINA_TRUE);
393 elm_scroller_policy_set(ad->scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
396 ad->layout = elm_layout_add(ad->naviframe);
397 evas_object_size_hint_weight_set(ad->layout, EVAS_HINT_EXPAND, 0.0);
398 elm_layout_file_set(ad->layout, ELM_DEMO_EDJ, "mainpage_layout");
400 elm_object_content_set(ad->scroller, ad->layout);
402 ad->bootButton = elm_button_add(ad->layout);
403 elm_object_text_set(ad->bootButton, "doBootStrap");
404 evas_object_size_hint_weight_set(ad->bootButton, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
405 evas_object_size_hint_align_set(ad->bootButton, EVAS_HINT_FILL, EVAS_HINT_FILL);
406 evas_object_smart_callback_add(ad->bootButton, "clicked", doBootStrap_cb, ad);
407 elm_object_part_content_set(ad->layout, "bootstrap_button", ad->bootButton);
409 ad->createConfButton = elm_button_add(ad->layout);
410 elm_object_text_set(ad->createConfButton, "Create Configuration Resources");
411 evas_object_size_hint_weight_set(ad->createConfButton, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
412 evas_object_size_hint_align_set(ad->createConfButton, EVAS_HINT_FILL, EVAS_HINT_FILL);
413 evas_object_smart_callback_add(ad->createConfButton, "clicked", createConfResource_cb, ad);
414 elm_object_part_content_set(ad->layout, "create_conf_button", ad->createConfButton);
416 log_entry = elm_entry_add(ad->layout);
417 elm_entry_scrollable_set(log_entry, EINA_TRUE);
418 elm_entry_editable_set(log_entry, EINA_FALSE);
419 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
420 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
421 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
422 elm_object_part_content_set(ad->layout, "log", log_entry);
424 elm_naviframe_item_push(ad->naviframe, "Configuration Server", NULL, NULL, ad->scroller, NULL);
426 // Show window after base gui is set up
427 evas_object_show(ad->win);
430 // Function which configures the OCPlatform
436 PlatformConfig config
437 { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
439 OCPlatform::Configure(config);
441 dlog_print(DLOG_INFO, LOG_TAG, "#### Platform configuration done!!!!");
443 catch (OCException &e)
445 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
451 app_create(void *data)
453 /* Hook to take necessary actions before main event loop starts
454 Initialize UI resources and application's data
455 If this function returns true, the main loop of application starts
456 If this function returns false, the application is terminated */
457 appdata_s *ad = (appdata_s *)data;
459 elm_app_base_scale_set(1.8);
461 // Create and show the UI
464 // Configure the OCPlatform
465 configure_platform();
467 g_thingsmanager = new ThingsManager();
473 app_control(app_control_h app_control, void *data)
475 // Handle the launch request.
479 app_pause(void *data)
481 // Take necessary actions when application becomes invisible.
485 app_resume(void *data)
487 // Take necessary actions when application becomes visible.
491 app_terminate(void *data)
493 // Release all resources.
497 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
499 // APP_EVENT_LANGUAGE_CHANGED
501 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
502 elm_language_set(locale);
508 ui_app_orient_changed(app_event_info_h event_info, void *user_data)
510 // APP_EVENT_DEVICE_ORIENTATION_CHANGED
515 ui_app_region_changed(app_event_info_h event_info, void *user_data)
517 // APP_EVENT_REGION_FORMAT_CHANGED
521 ui_app_low_battery(app_event_info_h event_info, void *user_data)
523 // APP_EVENT_LOW_BATTERY
527 ui_app_low_memory(app_event_info_h event_info, void *user_data)
529 // APP_EVENT_LOW_MEMORY
533 main(int argc, char *argv[])
538 ui_app_lifecycle_callback_s event_callback = {0,};
539 app_event_handler_h handlers[5] = {NULL, };
541 event_callback.create = app_create;
542 event_callback.terminate = app_terminate;
543 event_callback.pause = app_pause;
544 event_callback.resume = app_resume;
545 event_callback.app_control = app_control;
547 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY,
548 ui_app_low_battery, &ad);
549 ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY,
550 ui_app_low_memory, &ad);
551 ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED],
552 APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
553 ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED,
554 ui_app_lang_changed, &ad);
555 ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
556 APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
557 ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
559 ret = ui_app_main(argc, argv, &event_callback, &ad);
560 if (APP_ERROR_NONE != ret)
562 dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);