[Code format] Fixed formating with auto-format tool
[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/logger.h"
24 #include "common/picojson.h"
25 #include "common/platform_exception.h"
26 #include "common/task-queue.h"
27 #include "common/tools.h"
28
29 #include "systeminfo-utils.h"
30 #include "systeminfo_device_capability.h"
31
32 namespace extension {
33 namespace systeminfo {
34
35 using common::PlatformResult;
36 using common::ErrorCode;
37 using common::TypeMismatchException;
38
39 namespace {
40 const std::string kPropertyIdString = "propertyId";
41 const std::string kListenerIdString = "listenerId";
42
43 const std::string kPrivilegeLED = "http://tizen.org/privilege/led";
44
45 #define CHECK_EXIST(args, name, out)                                             \
46   if (!args.contains(name)) {                                                    \
47     LogAndReportError(TypeMismatchException(name " is required argument"), out); \
48     return;                                                                      \
49   }
50 }
51
52 SysteminfoInstance::SysteminfoInstance() : manager_(this) {
53   ScopeLogger();
54   using std::placeholders::_1;
55   using std::placeholders::_2;
56
57 #define REGISTER_METHOD(M) RegisterSyncHandler(#M, std::bind(&SysteminfoInstance::M, this, _1, _2))
58   REGISTER_METHOD(SystemInfoGetCapabilities);
59   REGISTER_METHOD(SystemInfoGetCapability);
60   REGISTER_METHOD(SystemInfoAddPropertyValueChangeListener);
61   REGISTER_METHOD(SystemInfoRemovePropertyValueChangeListener);
62   REGISTER_METHOD(SystemInfoGetTotalMemory);
63   REGISTER_METHOD(SystemInfoGetAvailableMemory);
64   REGISTER_METHOD(SystemInfoGetCount);
65   REGISTER_METHOD(SystemInfoSetBrightness);
66   REGISTER_METHOD(SystemInfoGetBrightness);
67   REGISTER_METHOD(SystemInfoGetMaxBrightness);
68   REGISTER_METHOD(SystemInfoGetPropertyValue);
69   REGISTER_METHOD(SystemInfoGetPropertyValueArray);
70 #undef REGISTER_METHOD
71 }
72
73 SysteminfoInstance::~SysteminfoInstance() {
74   ScopeLogger();
75 }
76
77 void SysteminfoInstance::SystemInfoGetCapabilities(const picojson::value& args,
78                                                    picojson::object& out) {
79   ScopeLogger();
80   LoggerW(
81       "DEPRECATION WARNING: getCapabilities() is deprecated and will be removed from next release. "
82       "Use getCapability() instead.");
83
84   manager_.GetCapabilities(args, &out);
85 }
86
87 void SysteminfoInstance::SystemInfoGetCapability(const picojson::value& args,
88                                                  picojson::object& out) {
89   ScopeLogger();
90   manager_.GetCapability(args, &out);
91 }
92
93 void SysteminfoInstance::SystemInfoGetPropertyValue(const picojson::value& args,
94                                                     picojson::object& out) {
95   ScopeLogger();
96   manager_.GetPropertyValue(args, &out);
97 }
98
99 void SysteminfoInstance::SystemInfoGetPropertyValueArray(const picojson::value& args,
100                                                          picojson::object& out) {
101   ScopeLogger();
102   manager_.GetPropertyValueArray(args, &out);
103 }
104
105 void SysteminfoInstance::SystemInfoAddPropertyValueChangeListener(const picojson::value& args,
106                                                                   picojson::object& out) {
107   ScopeLogger();
108   manager_.AddPropertyValueChangeListener(args, &out);
109 }
110
111 void SysteminfoInstance::SystemInfoRemovePropertyValueChangeListener(const picojson::value& args,
112                                                                      picojson::object& out) {
113   ScopeLogger();
114   manager_.RemovePropertyValueChangeListener(args, &out);
115 }
116
117 void SysteminfoInstance::SystemInfoGetTotalMemory(const picojson::value& args,
118                                                   picojson::object& out) {
119   ScopeLogger();
120   manager_.GetTotalMemory(args, &out);
121 }
122
123 void SysteminfoInstance::SystemInfoGetAvailableMemory(const picojson::value& args,
124                                                       picojson::object& out) {
125   ScopeLogger();
126   manager_.GetAvailableMemory(args, &out);
127 }
128
129 void SysteminfoInstance::SystemInfoGetCount(const picojson::value& args, picojson::object& out) {
130   ScopeLogger();
131   manager_.GetCount(args, &out);
132 }
133
134 void SysteminfoInstance::SystemInfoSetBrightness(const picojson::value& args,
135                                                  picojson::object& out) {
136   ScopeLogger();
137   CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
138
139   CHECK_EXIST(args, "brightness", out)
140
141   const double brightness = args.get("brightness").get<double>();
142   int result = device_flash_set_brightness(brightness);
143   if (result != DEVICE_ERROR_NONE) {
144     if (DEVICE_ERROR_INVALID_PARAMETER == result) {
145       LogAndReportError(
146           PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Error occured"), &out,
147           ("device_flash_set_brightness error: %d (%s)", result, get_error_message(result)));
148     } else {
149       LogAndReportError(
150           PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out,
151           ("device_flash_set_brightness error: %d (%s)", result, get_error_message(result)));
152     }
153     return;
154   }
155
156   ReportSuccess(out);
157 }
158
159 void SysteminfoInstance::SystemInfoGetBrightness(const picojson::value& args,
160                                                  picojson::object& out) {
161   ScopeLogger();
162   CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
163
164   int brightness = 0;
165   int result = device_flash_get_brightness(&brightness);
166   if (result != DEVICE_ERROR_NONE) {
167     LogAndReportError(
168         PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out,
169         ("device_flash_get_brightness error: %d (%s)", result, get_error_message(result)));
170     return;
171   }
172
173   ReportSuccess(picojson::value(std::to_string(brightness)), out);
174 }
175
176 void SysteminfoInstance::SystemInfoGetMaxBrightness(const picojson::value& args,
177                                                     picojson::object& out) {
178   ScopeLogger();
179   CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
180
181   int brightness = 0;
182   int result = device_flash_get_max_brightness(&brightness);
183   if (result != DEVICE_ERROR_NONE) {
184     LogAndReportError(
185         PlatformResult(ErrorCode::UNKNOWN_ERR, "Not supported property"), &out,
186         ("device_flash_get_max_brightness error: %d (%s)", result, get_error_message(result)));
187     return;
188   }
189   ReportSuccess(picojson::value(std::to_string(brightness)), out);
190 }
191
192 }  // namespace systeminfo
193 }  // namespace extension