Improve variable naming style
[platform/core/system/deviced.git] / src / auto-test / led.c
1 /*
2  * test
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations uynder the License.
17  */
18
19
20 #include "test.h"
21 #include <stdio.h>
22 #include <device/led-internal.h>
23
24 #define METHOD_LED_PLAYCUSTOM                           "playcustom"
25 #define METHOD_LED_STOPCUSTOM                           "stopcustom"
26 //#define METHOD_LED_GETBRIGHTNESS_FORCAMERA    "GetBrightnessForCamera"
27 #define METHOD_LED_GETBRIGHTNESS                        "GetBrightness"
28 #define METHOD_LED_GETMAXBRIGHTNESS                     "GetMaxBrightness"
29 #define METHOD_LED_SETBRIGHTNESS                        "SetBrightness"
30 #define METHOD_MULTI_LED_GETNUMBER                      "GetNumOfLeds"
31 #define METHOD_MULTI_LED_CONTROL                        "MultiLedControl"
32
33 #define RED 0xff0000
34 #define GREEN 0x00ff00
35 #define BLUE 0x0000ff
36 #define DEFAULT_FLAG    (1 << 0)
37
38 static unsigned int color[] = {BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE,BLUE};
39
40 static bool get_led_method(const char *method)
41 {
42         GVariant *msg;
43         int val, ret_dbus;
44         bool ret = FALSE;
45
46         ret_dbus = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
47                                         DEVICED_PATH_LED,
48                                         DEVICED_INTERFACE_LED,
49                                         method, NULL, &msg);
50         if (ret_dbus < 0) {
51                 _E("fail (%s): no reply", method);
52                 return ret;
53         }
54
55         if (!g_variant_get_safe(msg, "(i)", &val))
56                 _E("fail (%s): no message", method);
57         else {
58                 _I("success (%s): %d", method, val);
59                 ret = TRUE;
60         }
61
62         g_variant_unref(msg);
63         return ret;
64 }
65
66 static bool set_led_method(const char *method, GVariant *param)
67 {
68         GVariant *msg;
69         int val, ret_dbus;
70         bool ret = FALSE;
71
72         ret_dbus = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
73                                                 DEVICED_PATH_LED,
74                                                 DEVICED_INTERFACE_LED,
75                                                 method, param, &msg);
76         if (ret_dbus < 0) {
77                 _E("fail (%s): no reply", method);
78                 return ret;
79         }
80
81         if (!g_variant_get_safe(msg, "(i)", &val))
82                 _E("fail (%s): no message", method);
83         else {
84                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
85                         _I("Not supported feature! (%s): %d", method, val);
86                         ret = TRUE;
87                 } else if (val < 0) {
88                         _E("fail (%s): returned fail (%d)", method, val);
89                 } else {
90                         _I("success (%s): %d", method, val);
91                         ret = TRUE;
92                 }
93         }
94
95         g_variant_unref(msg);
96         return ret;
97 }
98
99 static bool get_led_maxbrightness(void)
100 {
101         return get_led_method(METHOD_LED_GETMAXBRIGHTNESS);
102 }
103
104 static bool get_led_brightness(void)
105 {
106         return get_led_method(METHOD_LED_GETBRIGHTNESS);
107 }
108
109 static bool set_led_brightness(int brightness, int enabled)
110 {
111         return set_led_method(METHOD_LED_SETBRIGHTNESS, g_variant_new("(ii)", brightness, enabled));
112 }
113
114 static bool set_led_playcustom(int on, int off, unsigned int color, unsigned int flags)
115 {
116         return set_led_method(METHOD_LED_PLAYCUSTOM, g_variant_new("(iiuu)", on, off, color, flags));
117 }
118
119 static bool set_led_stopcustom()
120 {
121         return set_led_method(METHOD_LED_STOPCUSTOM, NULL);
122 }
123
124 static bool get_multi_led_number(void)
125 {
126         int ret;
127         int number_of_led;
128
129         ret = device_multi_led_get_number(&number_of_led);
130
131         return capi_result(METHOD_MULTI_LED_GETNUMBER, ret);
132 }
133
134 static bool set_multi_led_control(unsigned int color[])
135 {
136         int ret;
137
138         ret = device_multi_led_control(color);
139
140         return capi_result(METHOD_MULTI_LED_CONTROL, ret);
141 }
142
143 void led_test_all(int *success, int *fail)
144 {
145         int s = 0;
146         int f = 0;
147
148         (get_led_maxbrightness())               ? s++ : f++;
149         (set_led_brightness(90, 1))             ? s++ : f++;
150         (set_led_brightness(0, 1))              ? s++ : f++;
151         (get_led_brightness())                  ? s++ : f++;
152         (set_led_playcustom(1, 0, RED, DEFAULT_FLAG))   ? s++ : f++;
153         (set_led_stopcustom())                  ? s++ : f++;
154         (get_multi_led_number())                ? s++ : f++;
155         (set_multi_led_control(color))          ? s++ : f++;
156
157         if (NULL != success)    *success = s;
158         if (NULL != fail)       *fail = f;
159 }
160
161 static void led_init(void *data)
162 {
163         int success = 0;
164         int fail = 0;
165
166         _I("start test");
167
168         led_test_all(&success, &fail);
169
170         _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
171 }
172
173 static void led_exit(void *data)
174 {
175         _I("end test");
176 }
177
178 static int led_unit(int argc, char **argv)
179 {
180         if (argc < 4) {
181                 int success = 0;
182                 int fail = 0;
183                 _I("start test");
184                 led_test_all(&success, &fail);
185                 _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
186         } else if (0 == strcasecmp(argv[3], METHOD_LED_GETMAXBRIGHTNESS)) {
187                 get_led_maxbrightness();
188         } else if (0 == strcasecmp(argv[3], METHOD_LED_GETBRIGHTNESS)) {
189                 get_led_brightness();
190         } else if (0 == strcasecmp(argv[3], METHOD_LED_SETBRIGHTNESS)) {
191                 set_led_brightness(atoi(argv[4]), atoi(argv[5]));
192         } else if (0 == strcasecmp(argv[3], METHOD_LED_PLAYCUSTOM)) {
193                 set_led_playcustom(atoi(argv[4]), atoi(argv[5]), strtoul(argv[6], NULL, 16), DEFAULT_FLAG);
194         } else if (0 == strcasecmp(argv[3], METHOD_MULTI_LED_GETNUMBER)) {
195                 get_multi_led_number();
196         } else if (0 == strcasecmp(argv[3], METHOD_MULTI_LED_CONTROL)) {
197                 set_multi_led_control(color);
198         } else if (0 == strcasecmp(argv[3], METHOD_LED_STOPCUSTOM)) {
199                 set_led_stopcustom();
200         } else {
201                 _E("Unknown test case!!!");
202         }
203
204         return 0;
205 }
206
207 static const struct test_ops led_test_ops = {
208         .priority = TEST_PRIORITY_NORMAL,
209         .name     = "led",
210         .init     = led_init,
211         .exit     = led_exit,
212         .unit     = led_unit,
213 };
214
215 TEST_OPS_REGISTER(&led_test_ops)