2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "systeminfo/systeminfo_instance.h"
19 #include <device/led.h>
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"
29 #include "systeminfo-utils.h"
30 #include "systeminfo_device_capability.h"
33 namespace systeminfo {
35 using common::PlatformResult;
36 using common::ErrorCode;
37 using common::TypeMismatchException;
40 const std::string kPropertyIdString = "propertyId";
41 const std::string kListenerIdString = "listenerId";
43 const std::string kPrivilegeLED = "http://tizen.org/privilege/led";
45 #define CHECK_EXIST(args, name, out) \
46 if (!args.contains(name)) { \
47 LogAndReportError(TypeMismatchException(name " is required argument"), out); \
52 SysteminfoInstance::SysteminfoInstance() : manager_(this) {
54 using std::placeholders::_1;
55 using std::placeholders::_2;
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
73 SysteminfoInstance::~SysteminfoInstance() {
77 void SysteminfoInstance::SystemInfoGetCapabilities(const picojson::value& args,
78 picojson::object& out) {
81 "getCapabilities() is deprecated and will be removed from next release. "
82 "Use getCapability() instead.",
85 manager_.GetCapabilities(args, &out);
88 void SysteminfoInstance::SystemInfoGetCapability(const picojson::value& args,
89 picojson::object& out) {
91 manager_.GetCapability(args, &out);
94 void SysteminfoInstance::SystemInfoGetPropertyValue(const picojson::value& args,
95 picojson::object& out) {
97 manager_.GetPropertyValue(args, &out);
100 void SysteminfoInstance::SystemInfoGetPropertyValueArray(const picojson::value& args,
101 picojson::object& out) {
103 manager_.GetPropertyValueArray(args, &out);
106 void SysteminfoInstance::SystemInfoAddPropertyValueChangeListener(const picojson::value& args,
107 picojson::object& out) {
109 manager_.AddPropertyValueChangeListener(args, &out);
112 void SysteminfoInstance::SystemInfoRemovePropertyValueChangeListener(const picojson::value& args,
113 picojson::object& out) {
115 manager_.RemovePropertyValueChangeListener(args, &out);
118 void SysteminfoInstance::SystemInfoGetTotalMemory(const picojson::value& args,
119 picojson::object& out) {
121 manager_.GetTotalMemory(args, &out);
124 void SysteminfoInstance::SystemInfoGetAvailableMemory(const picojson::value& args,
125 picojson::object& out) {
127 manager_.GetAvailableMemory(args, &out);
130 void SysteminfoInstance::SystemInfoGetCount(const picojson::value& args, picojson::object& out) {
132 manager_.GetCount(args, &out);
135 void SysteminfoInstance::SystemInfoSetBrightness(const picojson::value& args,
136 picojson::object& out) {
138 CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
140 CHECK_EXIST(args, "brightness", out)
142 const double brightness = args.get("brightness").get<double>();
143 int result = device_flash_set_brightness(brightness);
144 if (result != DEVICE_ERROR_NONE) {
145 if (DEVICE_ERROR_INVALID_PARAMETER == result) {
147 PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Error occured"), &out,
148 ("device_flash_set_brightness error: %d (%s)", result, get_error_message(result)));
151 PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out,
152 ("device_flash_set_brightness error: %d (%s)", result, get_error_message(result)));
160 void SysteminfoInstance::SystemInfoGetBrightness(const picojson::value& args,
161 picojson::object& out) {
163 CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
166 int result = device_flash_get_brightness(&brightness);
167 if (result != DEVICE_ERROR_NONE) {
169 PlatformResult(ErrorCode::UNKNOWN_ERR, "Error occured"), &out,
170 ("device_flash_get_brightness error: %d (%s)", result, get_error_message(result)));
174 ReportSuccess(picojson::value(std::to_string(brightness)), out);
177 void SysteminfoInstance::SystemInfoGetMaxBrightness(const picojson::value& args,
178 picojson::object& out) {
180 CHECK_PRIVILEGE_ACCESS(kPrivilegeLED, &out);
183 int result = device_flash_get_max_brightness(&brightness);
184 if (result != DEVICE_ERROR_NONE) {
186 PlatformResult(ErrorCode::UNKNOWN_ERR, "Not supported property"), &out,
187 ("device_flash_get_max_brightness error: %d (%s)", result, get_error_message(result)));
190 ReportSuccess(picojson::value(std::to_string(brightness)), out);
193 } // namespace systeminfo
194 } // namespace extension