[Systeminfo] Uptime feature added
[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(SystemInfoGetDeviceUptime);
65   REGISTER_METHOD(SystemInfoGetCount);
66   REGISTER_METHOD(SystemInfoSetBrightness);
67   REGISTER_METHOD(SystemInfoGetBrightness);
68   REGISTER_METHOD(SystemInfoGetMaxBrightness);
69   REGISTER_METHOD(SystemInfoGetPropertyValue);
70   REGISTER_METHOD(SystemInfoGetPropertyValueArray);
71 #undef REGISTER_METHOD
72 }
73
74 SysteminfoInstance::~SysteminfoInstance() {
75   ScopeLogger();
76 }
77
78 void SysteminfoInstance::SystemInfoGetCapabilities(const picojson::value& args,
79                                                    picojson::object& out) {
80   ScopeLogger();
81   DEPRECATION_WARN(
82       "getCapabilities() is deprecated and will be removed from next release. "
83       "Use getCapability() instead.",
84       "2.3");
85
86   manager_.GetCapabilities(args, &out);
87 }
88
89 void SysteminfoInstance::SystemInfoGetCapability(const picojson::value& args,
90                                                  picojson::object& out) {
91   ScopeLogger();
92   manager_.GetCapability(args, &out);
93 }
94
95 void SysteminfoInstance::SystemInfoGetPropertyValue(const picojson::value& args,
96                                                     picojson::object& out) {
97   ScopeLogger();
98   manager_.GetPropertyValue(args, &out);
99 }
100
101 void SysteminfoInstance::SystemInfoGetPropertyValueArray(const picojson::value& args,
102                                                          picojson::object& out) {
103   ScopeLogger();
104   manager_.GetPropertyValueArray(args, &out);
105 }
106
107 void SysteminfoInstance::SystemInfoAddPropertyValueChangeListener(const picojson::value& args,
108                                                                   picojson::object& out) {
109   ScopeLogger();
110   manager_.AddPropertyValueChangeListener(args, &out);
111 }
112
113 void SysteminfoInstance::SystemInfoRemovePropertyValueChangeListener(const picojson::value& args,
114                                                                      picojson::object& out) {
115   ScopeLogger();
116   manager_.RemovePropertyValueChangeListener(args, &out);
117 }
118
119 void SysteminfoInstance::SystemInfoGetTotalMemory(const picojson::value& args,
120                                                   picojson::object& out) {
121   ScopeLogger();
122   manager_.GetTotalMemory(args, &out);
123 }
124
125 void SysteminfoInstance::SystemInfoGetAvailableMemory(const picojson::value& args,
126                                                       picojson::object& out) {
127   ScopeLogger();
128   manager_.GetAvailableMemory(args, &out);
129 }
130
131 void SysteminfoInstance::SystemInfoGetDeviceUptime(const picojson::value& args,
132                                                    picojson::object& out) {
133   ScopeLogger();
134   manager_.GetDeviceUptime(args, &out);
135 }
136
137 void SysteminfoInstance::SystemInfoGetCount(const picojson::value& args, picojson::object& out) {
138   ScopeLogger();
139   manager_.GetCount(args, &out);
140 }
141
142 void SysteminfoInstance::SystemInfoSetBrightness(const picojson::value& args,
143                                                  picojson::object& out) {
144   ScopeLogger();
145   CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
146
147   CHECK_EXIST(args, "brightness", out)
148
149   const double brightness = args.get("brightness").get<double>();
150   int result = device_flash_set_brightness(brightness);
151   if (result != DEVICE_ERROR_NONE) {
152     if (DEVICE_ERROR_INVALID_PARAMETER == result) {
153       LogAndReportError(
154           PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Error occured"), &out,
155           ("device_flash_set_brightness error: %d (%s)", result, get_error_message(result)));
156     } else {
157       LogAndReportError(
158           PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out,
159           ("device_flash_set_brightness error: %d (%s)", result, get_error_message(result)));
160     }
161     return;
162   }
163
164   ReportSuccess(out);
165 }
166
167 void SysteminfoInstance::SystemInfoGetBrightness(const picojson::value& args,
168                                                  picojson::object& out) {
169   ScopeLogger();
170   CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
171
172   int brightness = 0;
173   int result = device_flash_get_brightness(&brightness);
174   if (result != DEVICE_ERROR_NONE) {
175     LogAndReportError(
176         PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out,
177         ("device_flash_get_brightness error: %d (%s)", result, get_error_message(result)));
178     return;
179   }
180
181   ReportSuccess(picojson::value(std::to_string(brightness)), out);
182 }
183
184 void SysteminfoInstance::SystemInfoGetMaxBrightness(const picojson::value& args,
185                                                     picojson::object& out) {
186   ScopeLogger();
187   CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
188
189   int brightness = 0;
190   int result = device_flash_get_max_brightness(&brightness);
191   if (result != DEVICE_ERROR_NONE) {
192     LogAndReportError(
193         PlatformResult(ErrorCode::UNKNOWN_ERR, "Not supported property"), &out,
194         ("device_flash_get_max_brightness error: %d (%s)", result, get_error_message(result)));
195     return;
196   }
197   ReportSuccess(picojson::value(std::to_string(brightness)), out);
198 }
199
200 }  // namespace systeminfo
201 }  // namespace extension