[SystemInfo] Fix for systemInfo CameraFlash
[platform/core/api/webapi-plugins.git] / src / systeminfo / systeminfo_instance.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "systeminfo/systeminfo_instance.h"
18
19 #include <device/led.h>
20 #include <functional>
21 #include <memory>
22
23 #include "common/picojson.h"
24 #include "common/logger.h"
25 #include "common/platform_exception.h"
26 #include "common/task-queue.h"
27
28 #include "systeminfo-utils.h"
29
30 namespace extension {
31 namespace systeminfo {
32
33 using namespace common;
34 using namespace extension::systeminfo;
35
36 //Callback functions declarations
37 static void OnBatteryChangedCallback(SysteminfoInstance& instance);
38 static void OnCpuChangedCallback(SysteminfoInstance& instance);
39 static void OnStorageChangedCallback(SysteminfoInstance& instance);
40 static void OnDisplayChangedCallback(SysteminfoInstance& instance);
41 static void OnDeviceOrientationChangedCallback(SysteminfoInstance& instance);
42 static void OnLocaleChangedCallback(SysteminfoInstance& instance);
43 static void OnNetworkChangedCallback(SysteminfoInstance& instance);
44 static void OnWifiNetworkChangedCallback(SysteminfoInstance& instance);
45 static void OnEthernetNetworkChangedCallback(SysteminfoInstance& instance);
46 static void OnCellularNetworkChangedCallback(SysteminfoInstance& instance);
47 static void OnPeripheralChangedCallback(SysteminfoInstance& instance);
48 static void OnMemoryChangedCallback(SysteminfoInstance& instance);
49 static void OnBrigthnessChangedCallback(SysteminfoInstance& instance);
50
51 namespace {
52 const std::string kPropertyIdString = "propertyId";
53 const std::string kListenerIdString = "listenerId";
54 const std::string kListenerConstValue = "SysteminfoCommonListenerLabel";
55
56 const std::string kPropertyIdBattery = "BATTERY";
57 const std::string kPropertyIdCpu = "CPU";
58 const std::string kPropertyIdStorage = "STORAGE";
59 const std::string kPropertyIdDisplay = "DISPLAY";
60 const std::string kPropertyIdDeviceOrientation = "DEVICE_ORIENTATION";
61 const std::string kPropertyIdBuild = "BUILD";
62 const std::string kPropertyIdLocale = "LOCALE";
63 const std::string kPropertyIdNetwork = "NETWORK";
64 const std::string kPropertyIdWifiNetwork = "WIFI_NETWORK";
65 const std::string kPropertyIdEthernetNetwork = "ETHERNET_NETWORK";
66 const std::string kPropertyIdCellularNetwork = "CELLULAR_NETWORK";
67 const std::string kPropertyIdSim = "SIM";
68 const std::string kPropertyIdPeripheral = "PERIPHERAL";
69 const std::string kPropertyIdMemory= "MEMORY";
70 const std::string kPropertyIdCameraFlash= "CAMERA_FLASH";
71
72 #define CHECK_EXIST(args, name, out) \
73   if (!args.contains(name)) {\
74     ReportError(TypeMismatchException(name" is required argument"), out);\
75       return;\
76     }
77
78 #define DEFAULT_REPORT_BOOL_CAPABILITY(str_name, feature_name) \
79   ret = SystemInfoDeviceCapability::GetValueBool(feature_name, &bool_value); \
80   if (ret.IsError()) { \
81     ReportError(ret,&out); \
82     return; \
83   } \
84   result_obj.insert(std::make_pair(str_name, picojson::value(bool_value)));
85
86 #define REPORT_BOOL_CAPABILITY(str_name, method) \
87   ret = method(&bool_value); \
88   if (ret.IsError()) { \
89     ReportError(ret,&out); \
90     return; \
91   } \
92   result_obj.insert(std::make_pair(str_name, picojson::value(bool_value)));
93
94 #define DEFAULT_REPORT_INT_CAPABILITY(str_name, feature_name) \
95   ret = SystemInfoDeviceCapability::GetValueInt(feature_name, &int_value); \
96   if (ret.IsError()) { \
97     ReportError(ret,&out); \
98     return; \
99   } \
100   result_obj.insert(std::make_pair(str_name, picojson::value(std::to_string(int_value))));
101
102 #define DEFAULT_REPORT_STRING_CAPABILITY(str_name, feature_name) \
103   ret = SystemInfoDeviceCapability::GetValueString(feature_name, &str_value); \
104   if (ret.IsError()) { \
105     ReportError(ret,&out); \
106     return; \
107   } \
108   result_obj.insert(std::make_pair(str_name, picojson::value(str_value)));
109
110 #define REPORT_STRING_CAPABILITY(str_name, method) \
111   ret = method(&str_value); \
112   if (ret.IsError()) { \
113     ReportError(ret,&out); \
114     return; \
115   } \
116   result_obj.insert(std::make_pair(str_name, picojson::value(str_value)));
117 }
118
119 SysteminfoInstance::SysteminfoInstance() {
120   LoggerD("Enter");
121   using std::placeholders::_1;
122   using std::placeholders::_2;
123
124 #define REGISTER_SYNC(c,x) \
125         RegisterSyncHandler(c, std::bind(&SysteminfoInstance::x, this, _1, _2));
126   REGISTER_SYNC("SystemInfo_getCapabilities", GetCapabilities);
127   REGISTER_SYNC("SystemInfo_getCapability", GetCapability);
128   REGISTER_SYNC("SystemInfo_addPropertyValueChangeListener", AddPropertyValueChangeListener);
129   REGISTER_SYNC("SystemInfo_removePropertyValueChangeListener", RemovePropertyValueChangeListener);
130   REGISTER_SYNC("SystemInfo_getTotalMemory", GetTotalMemory);
131   REGISTER_SYNC("SystemInfo_getAvailableMemory", GetAvailableMemory);
132   REGISTER_SYNC("SystemInfo_getCount", GetCount);
133   REGISTER_SYNC("SystemInfo_setBrightness", SetBrightness);
134   REGISTER_SYNC("SystemInfo_getBrightness", GetBrightness);
135   REGISTER_SYNC("SystemInfo_getMaxBrightness", GetMaxBrightness);
136
137 #undef REGISTER_SYNC
138 #define REGISTER_ASYNC(c,x) \
139         RegisterSyncHandler(c, std::bind(&SysteminfoInstance::x, this, _1, _2));
140   REGISTER_ASYNC("SystemInfo_getPropertyValue", GetPropertyValue);
141   REGISTER_ASYNC("SystemInfo_getPropertyValueArray", GetPropertyValueArray);
142 #undef REGISTER_ASYNC
143 }
144
145 SysteminfoInstance::~SysteminfoInstance() {
146   LoggerD("Entered");
147   //TODO Below solution is temporary
148   //Implementation should be changed that each SysteminfoInstance object
149   //should have own SystemInfoListeners manager
150   SysteminfoUtils::UnregisterBatteryListener();
151   SysteminfoUtils::UnregisterCpuListener();
152   SysteminfoUtils::UnregisterStorageListener();
153   SysteminfoUtils::UnregisterDisplayListener();
154   SysteminfoUtils::UnregisterDeviceOrientationListener();
155   SysteminfoUtils::UnregisterLocaleListener();
156   SysteminfoUtils::UnregisterNetworkListener();
157   SysteminfoUtils::UnregisterWifiNetworkListener();
158   SysteminfoUtils::UnregisterCellularNetworkListener();
159   SysteminfoUtils::UnregisterPeripheralListener();
160   SysteminfoUtils::UnregisterMemoryListener();
161 }
162
163 void SysteminfoInstance::GetCapabilities(const picojson::value& args, picojson::object& out) {
164   LoggerD("Enter");
165   picojson::value result = picojson::value(picojson::object());
166   picojson::object& result_obj = result.get<picojson::object>();
167
168   bool bool_value = false;
169   int int_value = 0;
170   std::string str_value = "";
171   PlatformResult ret(ErrorCode::NO_ERROR);
172   DEFAULT_REPORT_BOOL_CAPABILITY("bluetooth", "tizen.org/feature/network.bluetooth")
173   DEFAULT_REPORT_BOOL_CAPABILITY("nfc", "tizen.org/feature/network.nfc")
174   DEFAULT_REPORT_BOOL_CAPABILITY("nfcReservedPush", "tizen.org/feature/network.nfc.reserved_push")
175   DEFAULT_REPORT_INT_CAPABILITY("multiTouchCount", "tizen.org/feature/multi_point_touch.point_count")
176   DEFAULT_REPORT_BOOL_CAPABILITY("inputKeyboard", "tizen.org/feature/input.keyboard")
177   REPORT_BOOL_CAPABILITY("inputKeyboardLayout", SystemInfoDeviceCapability::IsInputKeyboardLayout)
178   DEFAULT_REPORT_BOOL_CAPABILITY("wifi", "tizen.org/feature/network.wifi")
179   DEFAULT_REPORT_BOOL_CAPABILITY("wifiDirect", "tizen.org/feature/network.wifi.direct")
180   DEFAULT_REPORT_STRING_CAPABILITY("platformName", "tizen.org/system/platform.name")
181   DEFAULT_REPORT_STRING_CAPABILITY("platformVersion", "tizen.org/feature/platform.version")
182   DEFAULT_REPORT_STRING_CAPABILITY("webApiVersion", "tizen.org/feature/platform.web.api.version")
183   DEFAULT_REPORT_BOOL_CAPABILITY("fmRadio", "tizen.org/feature/fmradio")
184   DEFAULT_REPORT_BOOL_CAPABILITY("opengles", "tizen.org/feature/opengles")
185   DEFAULT_REPORT_BOOL_CAPABILITY("openglesVersion1_1", "tizen.org/feature/opengles.version.1_1")
186   DEFAULT_REPORT_BOOL_CAPABILITY("openglesVersion2_0", "tizen.org/feature/opengles.version.2_0")
187   REPORT_STRING_CAPABILITY("openglestextureFormat",
188                            SystemInfoDeviceCapability::GetOpenglesTextureFormat)
189   DEFAULT_REPORT_BOOL_CAPABILITY("speechRecognition", "tizen.org/feature/speech.recognition")
190   DEFAULT_REPORT_BOOL_CAPABILITY("speechSynthesis", "tizen.org/feature/speech.synthesis")
191   DEFAULT_REPORT_BOOL_CAPABILITY("accelerometer", "tizen.org/feature/sensor.accelerometer")
192   DEFAULT_REPORT_BOOL_CAPABILITY("accelerometerWakeup", "tizen.org/feature/sensor.accelerometer.wakeup")
193   DEFAULT_REPORT_BOOL_CAPABILITY("barometer", "tizen.org/feature/sensor.barometer")
194   DEFAULT_REPORT_BOOL_CAPABILITY("barometerWakeup", "tizen.org/feature/sensor.barometer.wakeup")
195   DEFAULT_REPORT_BOOL_CAPABILITY("gyroscope", "tizen.org/feature/sensor.gyroscope")
196   DEFAULT_REPORT_BOOL_CAPABILITY("gyroscopeWakeup", "tizen.org/feature/sensor.gyroscope.wakeup")
197   DEFAULT_REPORT_BOOL_CAPABILITY("camera", "tizen.org/feature/camera")
198   DEFAULT_REPORT_BOOL_CAPABILITY("cameraFront", "tizen.org/feature/camera.front")
199   DEFAULT_REPORT_BOOL_CAPABILITY("cameraFrontFlash", "tizen.org/feature/camera.front.flash")
200   DEFAULT_REPORT_BOOL_CAPABILITY("cameraBack", "tizen.org/feature/camera.back")
201   DEFAULT_REPORT_BOOL_CAPABILITY("cameraBackFlash", "tizen.org/feature/camera.back.flash")
202   DEFAULT_REPORT_BOOL_CAPABILITY("location", "tizen.org/feature/location")
203   DEFAULT_REPORT_BOOL_CAPABILITY("locationGps", "tizen.org/feature/location.gps")
204   DEFAULT_REPORT_BOOL_CAPABILITY("locationWps", "tizen.org/feature/location.wps")
205   DEFAULT_REPORT_BOOL_CAPABILITY("microphone", "tizen.org/feature/microphone")
206   DEFAULT_REPORT_BOOL_CAPABILITY("usbHost", "tizen.org/feature/usb.host")
207   DEFAULT_REPORT_BOOL_CAPABILITY("usbAccessory", "tizen.org/feature/usb.accessory")
208   DEFAULT_REPORT_BOOL_CAPABILITY("screenOutputRca", "tizen.org/feature/screen.output.rca")
209   DEFAULT_REPORT_BOOL_CAPABILITY("screenOutputHdmi", "tizen.org/feature/screen.output.hdmi")
210   DEFAULT_REPORT_BOOL_CAPABILITY("graphicsAcceleration", "tizen.org/feature/graphics.acceleration")
211   DEFAULT_REPORT_BOOL_CAPABILITY("push", "tizen.org/feature/network.push")
212   DEFAULT_REPORT_BOOL_CAPABILITY("telephony", "tizen.org/feature/network.telephony")
213   DEFAULT_REPORT_BOOL_CAPABILITY("telephonyMms", "tizen.org/feature/network.telephony.mms")
214   DEFAULT_REPORT_BOOL_CAPABILITY("telephonySms", "tizen.org/feature/network.telephony.sms")
215   REPORT_STRING_CAPABILITY("platformCoreCpuArch",
216                              SystemInfoDeviceCapability::GetPlatfomCoreCpuArch)
217   REPORT_STRING_CAPABILITY("platformCoreFpuArch",
218                              SystemInfoDeviceCapability::GetPlatfomCoreFpuArch)
219   DEFAULT_REPORT_BOOL_CAPABILITY("sipVoip", "tizen.org/feature/sip.voip")
220   DEFAULT_REPORT_BOOL_CAPABILITY("magnetometer", "tizen.org/feature/sensor.magnetometer")
221   DEFAULT_REPORT_BOOL_CAPABILITY("magnetometerWakeup", "tizen.org/feature/sensor.magnetometer.wakeup")
222   DEFAULT_REPORT_BOOL_CAPABILITY("photometer", "tizen.org/feature/sensor.photometer")
223   DEFAULT_REPORT_BOOL_CAPABILITY("photometerWakeup", "tizen.org/feature/sensor.photometer.wakeup")
224   DEFAULT_REPORT_BOOL_CAPABILITY("proximity", "tizen.org/feature/sensor.proximity")
225   DEFAULT_REPORT_BOOL_CAPABILITY("proximityWakeup", "tizen.org/feature/sensor.proximity.wakeup")
226   DEFAULT_REPORT_BOOL_CAPABILITY("tiltmeter", "tizen.org/feature/sensor.tiltmeter")
227   DEFAULT_REPORT_BOOL_CAPABILITY("tiltmeterWakeup", "tizen.org/feature/sensor.tiltmeter.wakeup")
228   DEFAULT_REPORT_BOOL_CAPABILITY("dataEncryption", "tizen.org/feature/database.encryption")
229   DEFAULT_REPORT_BOOL_CAPABILITY("autoRotation", "tizen.org/feature/screen.auto_rotation")
230   DEFAULT_REPORT_BOOL_CAPABILITY("visionImageRecognition", "tizen.org/feature/vision.image_recognition")
231   DEFAULT_REPORT_BOOL_CAPABILITY("visionQrcodeGeneration", "tizen.org/feature/vision.qrcode_generation")
232   DEFAULT_REPORT_BOOL_CAPABILITY("visionQrcodeRecognition", "tizen.org/feature/vision.qrcode_recognition")
233   DEFAULT_REPORT_BOOL_CAPABILITY("visionFaceRecognition", "tizen.org/feature/vision.face_recognition")
234   DEFAULT_REPORT_BOOL_CAPABILITY("secureElement", "tizen.org/feature/network.secure_element")
235   REPORT_STRING_CAPABILITY("profile", SystemInfoDeviceCapability::GetProfile)
236   DEFAULT_REPORT_STRING_CAPABILITY("nativeApiVersion", "tizen.org/feature/platform.native.api.version")
237   DEFAULT_REPORT_STRING_CAPABILITY("duid", "tizen.org/system/tizenid")
238   DEFAULT_REPORT_BOOL_CAPABILITY("screenSizeNormal", "tizen.org/feature/screen.size.normal")
239   DEFAULT_REPORT_BOOL_CAPABILITY("screenSize480_800", "tizen.org/feature/screen.size.normal.480.800")
240   DEFAULT_REPORT_BOOL_CAPABILITY("screenSize720_1280", "tizen.org/feature/screen.size.normal.720.1280")
241   DEFAULT_REPORT_BOOL_CAPABILITY("shellAppWidget", "tizen.org/feature/shell.appwidget")
242   DEFAULT_REPORT_BOOL_CAPABILITY("nativeOspCompatible", "tizen.org/feature/platform.native.osp_compatible")
243
244   ReportSuccess(result, out);
245   LoggerD("Success");
246 }
247
248 void SysteminfoInstance::GetCapability(const picojson::value& args, picojson::object& out) {
249   LoggerD("Enter");
250   CHECK_EXIST(args, "key", out)
251   const std::string& key = args.get("key").get<std::string>();
252   LoggerD("Getting capability with key: %s ", key.c_str());
253
254   picojson::value result = picojson::value(picojson::object());
255   PlatformResult ret = SystemInfoDeviceCapability::GetCapability(key, result);
256   if (ret.IsSuccess()) {
257     ReportSuccess(result, out);
258     LoggerD("Success");
259   } else {
260     ReportError(ret, &out);
261   }
262 }
263
264 void SysteminfoInstance::GetPropertyValue(const picojson::value& args, picojson::object& out) {
265   LoggerD("Enter");
266   CHECK_EXIST(args, "callbackId", out)
267   CHECK_EXIST(args, "property", out)
268   const double callback_id = args.get("callbackId").get<double>();
269   const std::string& prop_id = args.get("property").get<std::string>();
270   LoggerD("Getting property with id: %s ", prop_id.c_str());
271
272   auto get = [this, prop_id, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
273     LoggerD("Getting");
274     picojson::value result = picojson::value(picojson::object());
275     PlatformResult ret = SysteminfoUtils::GetPropertyValue(prop_id, false, result);
276     if (ret.IsError()) {
277       ReportError(ret,&(response->get<picojson::object>()));
278       return;
279     }
280     ReportSuccess(result, response->get<picojson::object>());
281   };
282
283   auto get_response = [this, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
284     LoggerD("Getting response");
285     picojson::object& obj = response->get<picojson::object>();
286     obj.insert(std::make_pair("callbackId", picojson::value{static_cast<double>(callback_id)}));
287     LoggerD("message: %s", response->serialize().c_str());
288     PostMessage(response->serialize().c_str());
289   };
290
291   TaskQueue::GetInstance().Queue<picojson::value>
292   (get, get_response, std::shared_ptr<picojson::value>(new picojson::value(picojson::object())));
293 }
294
295 void SysteminfoInstance::GetPropertyValueArray(const picojson::value& args, picojson::object& out) {
296   LoggerD("Enter");
297   CHECK_EXIST(args, "callbackId", out)
298   CHECK_EXIST(args, "property", out)
299   const double callback_id = args.get("callbackId").get<double>();
300   const std::string& prop_id = args.get("property").get<std::string>();
301   LoggerD("Getting property arrray with id: %s ", prop_id.c_str());
302
303   auto get = [this, prop_id, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
304     LoggerD("Getting");
305     picojson::value result = picojson::value(picojson::object());
306     PlatformResult ret = SysteminfoUtils::GetPropertyValue(prop_id, true, result);
307     if (ret.IsError()) {
308       LoggerE("Failed: GetPropertyValue()");
309       ReportError(ret,&(response->get<picojson::object>()));
310       return;
311     }
312     ReportSuccess(result, response->get<picojson::object>());
313   };
314
315   auto get_response = [this, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
316     LoggerD("Getting response");
317     picojson::object& obj = response->get<picojson::object>();
318     obj.insert(std::make_pair("callbackId", picojson::value(callback_id)));
319     PostMessage(response->serialize().c_str());
320   };
321
322   TaskQueue::GetInstance().Queue<picojson::value>
323   (get, get_response, std::shared_ptr<picojson::value>(new picojson::value(picojson::object())));
324 }
325
326 void SysteminfoInstance::AddPropertyValueChangeListener(const picojson::value& args, picojson::object& out) {
327   LoggerD("Enter");
328   // Check type of property for which listener should be registered
329   CHECK_EXIST(args, "property", out)
330   const std::string& property_name = args.get("property").get<std::string>();
331
332   LoggerD("Adding listener for property with id: %s ", property_name.c_str());
333   PlatformResult ret(ErrorCode::NO_ERROR);
334   if (property_name == kPropertyIdBattery) {
335     ret = SysteminfoUtils::RegisterBatteryListener(OnBatteryChangedCallback, *this);
336   } else if (property_name == kPropertyIdCpu) {
337     ret = SysteminfoUtils::RegisterCpuListener(OnCpuChangedCallback, *this);
338   } else if (property_name == kPropertyIdStorage) {
339     ret = SysteminfoUtils::RegisterStorageListener(OnStorageChangedCallback, *this);
340   } else if (property_name == kPropertyIdDisplay) {
341     ret = SysteminfoUtils::RegisterDisplayListener(OnDisplayChangedCallback, *this);
342   } else if (property_name == kPropertyIdDeviceOrientation) {
343     ret = SysteminfoUtils::RegisterDeviceOrientationListener(OnDeviceOrientationChangedCallback, *this);
344   } else if (property_name == kPropertyIdBuild) {
345     LoggerW("BUILD property's value is a fixed value");
346     //should be accepted, but no registration is needed
347     //ret = PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "BUILD property's value is a fixed value");
348   } else if (property_name == kPropertyIdLocale) {
349     ret = SysteminfoUtils::RegisterLocaleListener(OnLocaleChangedCallback, *this);
350   } else if (property_name == kPropertyIdNetwork) {
351     ret = SysteminfoUtils::RegisterNetworkListener(OnNetworkChangedCallback, *this);
352   } else if (property_name == kPropertyIdWifiNetwork) {
353     ret = SysteminfoUtils::RegisterWifiNetworkListener(OnWifiNetworkChangedCallback, *this);
354   } else if (property_name == kPropertyIdEthernetNetwork) {
355     ret = SysteminfoUtils::RegisterEthernetNetworkListener(OnEthernetNetworkChangedCallback, *this);
356   } else if (property_name == kPropertyIdCellularNetwork) {
357     ret = SysteminfoUtils::RegisterCellularNetworkListener(OnCellularNetworkChangedCallback, *this);
358   } else if (property_name == kPropertyIdSim) {
359     //SIM listeners are not supported by core API, so we just pass over
360     LoggerW("SIM listener is not supported by Core API - ignoring");
361   } else if (property_name == kPropertyIdPeripheral) {
362     ret = SysteminfoUtils::RegisterPeripheralListener(OnPeripheralChangedCallback, *this);
363   } else if (property_name == kPropertyIdMemory) {
364     ret = SysteminfoUtils::RegisterMemoryListener(OnMemoryChangedCallback, *this);
365   } else if (property_name == kPropertyIdCameraFlash) {
366     ret = SysteminfoUtils::RegisterCameraFlashListener(OnBrigthnessChangedCallback, *this);
367   } else {
368     LoggerE("Not supported property");
369     ret = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Not supported property");
370   }
371   if (ret.IsSuccess()) {
372     ReportSuccess(out);
373     LoggerD("Success");
374     return;
375   }
376   LoggerD("Error");
377   ReportError(ret, &out);
378 }
379
380 void SysteminfoInstance::GetTotalMemory(const picojson::value& args, picojson::object& out) {
381   LoggerD("Enter");
382   picojson::value result = picojson::value(picojson::object());
383   picojson::object& result_obj = result.get<picojson::object>();
384
385   long long return_value = 0;
386   PlatformResult ret = SysteminfoUtils::GetTotalMemory(return_value);
387   if (ret.IsError()) {
388     LoggerD("Error");
389     ReportError(ret, &out);
390     return;
391   }
392   result_obj.insert(std::make_pair("totalMemory",
393                                    picojson::value(static_cast<double>(return_value))));
394
395   ReportSuccess(result, out);
396   LoggerD("Success");
397 }
398
399 void SysteminfoInstance::GetAvailableMemory(const picojson::value& args, picojson::object& out) {
400   LoggerD("Enter");
401   picojson::value result = picojson::value(picojson::object());
402   picojson::object& result_obj = result.get<picojson::object>();
403
404   long long return_value = 0;
405   PlatformResult ret = SysteminfoUtils::GetAvailableMemory(return_value);
406   if (ret.IsError()) {
407     LoggerD("Error");
408     ReportError(ret, &out);
409     return;
410   }
411   result_obj.insert(std::make_pair("availableMemory",
412                                    picojson::value(static_cast<double>(return_value))));
413
414   ReportSuccess(result, out);
415   LoggerD("Success");
416 }
417
418 void SysteminfoInstance::GetCount(const picojson::value& args, picojson::object& out) {
419
420   LoggerD("Enter");
421   CHECK_EXIST(args, "property", out)
422   const std::string& property = args.get("property").get<std::string>();
423   LoggerD("Getting count of property with id: %s ", property.c_str());
424
425   picojson::value result = picojson::value(picojson::object());
426   picojson::object& result_obj = result.get<picojson::object>();
427   unsigned long count = 0;
428   PlatformResult ret = SysteminfoUtils::GetCount(property, count);
429   if (ret.IsError()) {
430     LoggerE("Failed: GetCount()");
431     ReportError(ret, &out);
432     return;
433   }
434   result_obj.insert(std::make_pair("count", picojson::value(static_cast<double>(count))));
435
436   ReportSuccess(result, out);
437   LoggerD("Success");
438 }
439
440 void SysteminfoInstance::RemovePropertyValueChangeListener(const picojson::value& args, picojson::object& out) {
441   LoggerD("Enter");
442
443   // Check type of property for which listener should be removed
444   CHECK_EXIST(args, "property", out)
445   const std::string& property_name = args.get("property").get<std::string>();
446   LoggerD("Removing listener for property with id: %s ", property_name.c_str());
447   PlatformResult ret(ErrorCode::NO_ERROR);
448   if (property_name == kPropertyIdBattery) {
449     ret = SysteminfoUtils::UnregisterBatteryListener();
450   } else if (property_name == kPropertyIdCpu) {
451     ret = SysteminfoUtils::UnregisterCpuListener();
452   } else if (property_name == kPropertyIdStorage) {
453     ret = SysteminfoUtils::UnregisterStorageListener();
454   } else if (property_name == kPropertyIdDisplay) {
455     ret = SysteminfoUtils::UnregisterDisplayListener();
456   } else if (property_name == kPropertyIdDeviceOrientation) {
457     ret = SysteminfoUtils::UnregisterDeviceOrientationListener();
458   } else if (property_name == kPropertyIdBuild) {
459     LoggerW("BUILD property's value is a fixed value");
460     //should be accepted, but no unregistration is needed
461     //ret = PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "BUILD property's value is a fixed value");
462   } else if (property_name == kPropertyIdLocale) {
463     ret = SysteminfoUtils::UnregisterLocaleListener();
464   } else if (property_name == kPropertyIdNetwork) {
465     ret = SysteminfoUtils::UnregisterNetworkListener();
466   } else if (property_name == kPropertyIdWifiNetwork) {
467     ret = SysteminfoUtils::UnregisterWifiNetworkListener();
468   } else if (property_name == kPropertyIdEthernetNetwork) {
469     ret = SysteminfoUtils::UnregisterEthernetNetworkListener();
470   } else if (property_name == kPropertyIdCellularNetwork) {
471     ret = SysteminfoUtils::UnregisterCellularNetworkListener();
472   } else if (property_name == kPropertyIdSim) {
473     //SIM listeners are not supported by core API, so we just pass over
474     LoggerW("SIM listener is not supported by Core API - ignoring");
475   } else if (property_name == kPropertyIdPeripheral) {
476     ret = SysteminfoUtils::UnregisterPeripheralListener();
477   } else if (property_name == kPropertyIdMemory) {
478     ret = SysteminfoUtils::UnregisterMemoryListener();
479   } else if (property_name == kPropertyIdCameraFlash) {
480     ret = SysteminfoUtils::UnregisterCameraFlashListener();
481   } else {
482     LoggerE("Not supported property");
483     ret = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Not supported property");
484   }
485   if (ret.IsSuccess()) {
486     ReportSuccess(out);
487     LoggerD("Success");
488     return;
489   }
490   LoggerD("Error");
491   ReportError(ret, &out);
492 }
493
494 static void ReportSuccess(const picojson::value& result, picojson::object& out) {
495   out.insert(std::make_pair("status", picojson::value("success")));
496   out.insert(std::make_pair("result",  picojson::value(result)));
497 }
498
499 void SysteminfoInstance::SetBrightness(const picojson::value& args, picojson::object& out) {
500   LoggerD("entered");
501
502   CHECK_EXIST(args, "brightness", out)
503
504   const double brightness = args.get("brightness").get<double>();
505   int result = device_flash_set_brightness(brightness);
506   if (result != DEVICE_ERROR_NONE) {
507     LoggerE("Error occured");
508     if (DEVICE_ERROR_INVALID_PARAMETER == result) {
509       ReportError(PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Error occured"), &out);
510     } else {
511       ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out);
512     }
513     return;
514   }
515
516   ReportSuccess(out);
517 }
518
519 void SysteminfoInstance::GetBrightness(const picojson::value& args, picojson::object& out) {
520   LoggerD("entered");
521
522   int brightness = 0;
523   int result = device_flash_get_brightness(&brightness);
524   if (result != DEVICE_ERROR_NONE) {
525     LoggerE("Error occured");
526     ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out);
527     return;
528   }
529
530   ReportSuccess(picojson::value(std::to_string(brightness)), out);
531 }
532
533 void SysteminfoInstance::GetMaxBrightness(const picojson::value& args, picojson::object& out) {
534   LoggerD("entered");
535
536   int brightness = 0;
537   int result = device_flash_get_max_brightness(&brightness);
538   if (result != DEVICE_ERROR_NONE) {
539     LoggerE("Error occured");
540     ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "Not supported property"), &out);
541     return;
542   }
543   ReportSuccess(picojson::value(std::to_string(brightness)), out);
544 }
545
546 //Callback functions
547 void OnBatteryChangedCallback(SysteminfoInstance& instance)
548 {
549   LoggerD("Enter");
550   const std::shared_ptr<picojson::value>& response =
551       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
552   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdBattery);
553   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
554
555   picojson::value result = picojson::value(picojson::object());
556   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdBattery, true, result);
557   if (ret.IsSuccess()) {
558     ReportSuccess(result,response->get<picojson::object>());
559     instance.PostMessage(response->serialize().c_str());
560   }
561 }
562
563 void OnCpuChangedCallback(SysteminfoInstance& instance)
564 {
565   LoggerD("Enter");
566   const std::shared_ptr<picojson::value>& response =
567       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
568   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdCpu);
569   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
570
571   picojson::value result = picojson::value(picojson::object());
572   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdCpu, true, result);
573   if (ret.IsSuccess()) {
574     ReportSuccess(result,response->get<picojson::object>());
575     instance.PostMessage(response->serialize().c_str());
576   }
577 }
578
579 void OnStorageChangedCallback(SysteminfoInstance& instance)
580 {
581   LoggerD("Enter");
582   const std::shared_ptr<picojson::value>& response =
583       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
584   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdStorage);
585   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
586
587   picojson::value result = picojson::value(picojson::object());
588   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdStorage, true, result);
589   if (ret.IsSuccess()) {
590     ReportSuccess(result,response->get<picojson::object>());
591     instance.PostMessage(response->serialize().c_str());
592   }
593 }
594
595 void OnDisplayChangedCallback(SysteminfoInstance& instance)
596 {
597   LoggerD("Enter");
598   const std::shared_ptr<picojson::value>& response =
599       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
600   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdDisplay);
601   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
602
603   picojson::value result = picojson::value(picojson::object());
604   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdDisplay, true, result);
605   if (ret.IsSuccess()) {
606     ReportSuccess(result,response->get<picojson::object>());
607     instance.PostMessage(response->serialize().c_str());
608   }
609 }
610
611 void OnDeviceOrientationChangedCallback(SysteminfoInstance& instance)
612 {
613   LoggerD("Enter");
614   const std::shared_ptr<picojson::value>& response =
615       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
616   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdDeviceOrientation);
617   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
618
619   picojson::value result = picojson::value(picojson::object());
620   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdDeviceOrientation, true, result);
621   if (ret.IsSuccess()) {
622     ReportSuccess(result,response->get<picojson::object>());
623     instance.PostMessage(response->serialize().c_str());
624   }
625 }
626
627 void OnLocaleChangedCallback(SysteminfoInstance& instance)
628 {
629   LoggerD("Enter");
630   const std::shared_ptr<picojson::value>& response =
631       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
632   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdLocale);
633   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
634
635   picojson::value result = picojson::value(picojson::object());
636   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdLocale, true, result);
637   if (ret.IsSuccess()) {
638     ReportSuccess(result,response->get<picojson::object>());
639     instance.PostMessage(response->serialize().c_str());
640   }
641 }
642
643 void OnNetworkChangedCallback(SysteminfoInstance& instance)
644 {
645   LoggerD("Enter");
646   const std::shared_ptr<picojson::value>& response =
647       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
648   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdNetwork);
649   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
650
651   picojson::value result = picojson::value(picojson::object());
652   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdNetwork, true, result);
653   if (ret.IsSuccess()) {
654     ReportSuccess(result,response->get<picojson::object>());
655     instance.PostMessage(response->serialize().c_str());
656   }
657 }
658
659 void OnWifiNetworkChangedCallback(SysteminfoInstance& instance)
660 {
661   LoggerD("Enter");
662   const std::shared_ptr<picojson::value>& response =
663       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
664   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdWifiNetwork);
665   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
666
667   picojson::value result = picojson::value(picojson::object());
668   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdWifiNetwork, true, result);
669   if (ret.IsSuccess()) {
670     ReportSuccess(result,response->get<picojson::object>());
671     instance.PostMessage(response->serialize().c_str());
672   }
673 }
674
675 void OnEthernetNetworkChangedCallback(SysteminfoInstance& instance)
676 {
677   LoggerD("Entered");
678   const std::shared_ptr<picojson::value>& response =
679       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
680   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdEthernetNetwork);
681   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
682
683   picojson::value result = picojson::value(picojson::object());
684   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdEthernetNetwork, true, result);
685   if (ret.IsSuccess()) {
686     ReportSuccess(result,response->get<picojson::object>());
687     instance.PostMessage(response->serialize().c_str());
688   }
689 }
690
691 void OnCellularNetworkChangedCallback(SysteminfoInstance& instance)
692 {
693   LoggerD("Enter");
694   const std::shared_ptr<picojson::value>& response =
695       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
696   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdCellularNetwork);
697   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
698
699   picojson::value result = picojson::value(picojson::object());
700   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdCellularNetwork, true, result);
701   if (ret.IsSuccess()) {
702     ReportSuccess(result,response->get<picojson::object>());
703     instance.PostMessage(response->serialize().c_str());
704   }
705 }
706
707 void OnPeripheralChangedCallback(SysteminfoInstance& instance)
708 {
709   LoggerD("Enter");
710   const std::shared_ptr<picojson::value>& response =
711       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
712   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdPeripheral);
713   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
714
715   picojson::value result = picojson::value(picojson::object());
716   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdPeripheral, true, result);
717   if (ret.IsSuccess()) {
718     ReportSuccess(result,response->get<picojson::object>());
719     instance.PostMessage(response->serialize().c_str());
720   }
721 }
722
723 void OnMemoryChangedCallback(SysteminfoInstance& instance)
724 {
725   LoggerD("Enter");
726   const std::shared_ptr<picojson::value>& response =
727       std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
728   response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdMemory);
729   response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
730
731   picojson::value result = picojson::value(picojson::object());
732   PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdMemory, true, result);
733   if (ret.IsSuccess()) {
734     ReportSuccess(result,response->get<picojson::object>());
735     instance.PostMessage(response->serialize().c_str());
736   }
737 }
738
739 void OnBrigthnessChangedCallback(SysteminfoInstance &instance)
740 {
741     LoggerD("Enter");
742     const std::shared_ptr<picojson::value>& response =
743         std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
744     response->get<picojson::object>()[kPropertyIdString] = picojson::value(kPropertyIdCameraFlash);
745     response->get<picojson::object>()[kListenerIdString] = picojson::value(kListenerConstValue);
746
747     picojson::value result = picojson::value(picojson::object());
748     PlatformResult ret = SysteminfoUtils::GetPropertyValue(kPropertyIdCameraFlash, true, result);
749     if (ret.IsSuccess()) {
750       ReportSuccess(result,response->get<picojson::object>());
751       instance.PostMessage(response->serialize().c_str());
752     }
753 }
754
755 } // namespace systeminfo
756 } // namespace extension