7b88d5e35b44aabc479d7de31a99d804076c7cd8
[platform/core/api/peripheral-io.git] / test / src / test_peripheral_pwm.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 <stdio.h>
18 #include <string.h>
19 #include <peripheral_io.h>
20 #include "test_peripheral_pwm.h"
21
22 #define PWM_CHIP 0
23 #define PWM_CHIP_INVALID -99
24 #define PWM_PIN 2
25 #define PWM_PIN_INVALID -99
26 #define PWM_PERIOD 1000
27 #define PWM_DUTY_CYCLE 100
28
29 static bool g_feature = true;
30 static int chip;
31 static int pin;
32
33 int test_peripheral_io_pwm_initialize(char *model, bool feature)
34 {
35         g_feature = feature;
36
37         if ((!strcmp(model, "rpi3")) || (!strcmp(model, "artik"))) {
38                 chip = PWM_CHIP;
39                 pin = PWM_PIN;
40         } else {
41                 return -1;
42         }
43         return 0;
44 }
45
46 int test_peripheral_io_pwm_peripheral_pwm_open_p(void)
47 {
48         int ret = PERIPHERAL_ERROR_NONE;
49
50         peripheral_pwm_h pwm_h = NULL;
51
52         if (g_feature == false) {
53                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
54                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
55
56         } else {
57                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
58                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
59
60                 ret = peripheral_pwm_close(pwm_h);
61                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
62         }
63
64         return PERIPHERAL_ERROR_NONE;
65 }
66
67 int test_peripheral_io_pwm_peripheral_pwm_open_n1(void)
68 {
69         int ret = PERIPHERAL_ERROR_NONE;
70
71         peripheral_pwm_h pwm_h = NULL;
72
73         if (g_feature == false) {
74                 ret = peripheral_pwm_open(PWM_CHIP_INVALID, pin, &pwm_h);
75                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
76
77         } else {
78                 ret = peripheral_pwm_open(PWM_CHIP_INVALID, pin, &pwm_h);
79                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
80         }
81
82         return PERIPHERAL_ERROR_NONE;
83 }
84
85 int test_peripheral_io_pwm_peripheral_pwm_open_n2(void)
86 {
87         int ret = PERIPHERAL_ERROR_NONE;
88
89         peripheral_pwm_h pwm_h = NULL;
90
91         if (g_feature == false) {
92                 ret = peripheral_pwm_open(chip, PWM_PIN_INVALID, &pwm_h);
93                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
94
95         } else {
96                 ret = peripheral_pwm_open(chip, PWM_PIN_INVALID, &pwm_h);
97                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
98         }
99
100         return PERIPHERAL_ERROR_NONE;
101 }
102
103 int test_peripheral_io_pwm_peripheral_pwm_open_n3(void)
104 {
105         int ret = PERIPHERAL_ERROR_NONE;
106
107         if (g_feature == false) {
108                 ret = peripheral_pwm_open(chip, pin, NULL);
109                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
110
111         } else {
112                 ret = peripheral_pwm_open(chip, pin, NULL);
113                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
114         }
115
116         return PERIPHERAL_ERROR_NONE;
117 }
118
119 int test_peripheral_io_pwm_peripheral_pwm_close_p(void)
120 {
121         int ret = PERIPHERAL_ERROR_NONE;
122
123         peripheral_pwm_h pwm_h = NULL;
124
125         if (g_feature == false) {
126                 ret = peripheral_pwm_close(pwm_h);
127                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
128
129         } else {
130                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
131                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
132
133                 ret = peripheral_pwm_close(pwm_h);
134                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
135         }
136
137         return PERIPHERAL_ERROR_NONE;
138 }
139
140 int test_peripheral_io_pwm_peripheral_pwm_close_n(void)
141 {
142         int ret = PERIPHERAL_ERROR_NONE;
143
144         if (g_feature == false) {
145                 ret = peripheral_pwm_close(NULL);
146                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
147
148         } else {
149                 ret = peripheral_pwm_close(NULL);
150                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
151         }
152
153         return PERIPHERAL_ERROR_NONE;
154 }
155
156 int test_peripheral_io_pwm_peripheral_pwm_set_period_p(void)
157 {
158         int ret = PERIPHERAL_ERROR_NONE;
159
160         peripheral_pwm_h pwm_h = NULL;
161
162         if (g_feature == false) {
163                 ret = peripheral_pwm_set_period(pwm_h, PWM_PERIOD);
164                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
165
166         } else {
167                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
168                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
169
170                 ret = peripheral_pwm_set_period(pwm_h, PWM_PERIOD);
171                 if (ret != PERIPHERAL_ERROR_NONE) {
172                         peripheral_pwm_close(pwm_h);
173                         return PERIPHERAL_ERROR_UNKNOWN;
174                 }
175
176                 ret = peripheral_pwm_close(pwm_h);
177                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
178         }
179
180         return PERIPHERAL_ERROR_NONE;
181 }
182
183 int test_peripheral_io_pwm_peripheral_pwm_set_period_n(void)
184 {
185         int ret = PERIPHERAL_ERROR_NONE;
186
187         if (g_feature == false) {
188                 ret = peripheral_pwm_set_period(NULL, PWM_PERIOD);
189                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
190
191         } else {
192                 ret = peripheral_pwm_set_period(NULL, PWM_PERIOD);
193                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
194         }
195
196         return PERIPHERAL_ERROR_NONE;
197 }
198
199 int test_peripheral_io_pwm_peripheral_pwm_set_duty_cycle_p(void)
200 {
201         int ret = PERIPHERAL_ERROR_NONE;
202
203         peripheral_pwm_h pwm_h = NULL;
204
205         if (g_feature == false) {
206                 ret = peripheral_pwm_set_duty_cycle(pwm_h, PWM_DUTY_CYCLE);
207                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
208
209         } else {
210                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
211                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
212
213                 ret = peripheral_pwm_set_duty_cycle(pwm_h, PWM_DUTY_CYCLE);
214                 if (ret != PERIPHERAL_ERROR_NONE) {
215                         peripheral_pwm_close(pwm_h);
216                         return PERIPHERAL_ERROR_UNKNOWN;
217                 }
218
219                 ret = peripheral_pwm_close(pwm_h);
220                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
221         }
222
223         return PERIPHERAL_ERROR_NONE;
224 }
225
226 int test_peripheral_io_pwm_peripheral_pwm_set_duty_cycle_n(void)
227 {
228         int ret = PERIPHERAL_ERROR_NONE;
229
230         if (g_feature == false) {
231                 ret = peripheral_pwm_set_duty_cycle(NULL, PWM_DUTY_CYCLE);
232                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
233
234         } else {
235                 ret = peripheral_pwm_set_duty_cycle(NULL, PWM_DUTY_CYCLE);
236                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
237         }
238
239         return PERIPHERAL_ERROR_NONE;
240 }
241
242 int test_peripheral_io_pwm_peripheral_pwm_set_polarity_p1(void)
243 {
244         int ret = PERIPHERAL_ERROR_NONE;
245
246         peripheral_pwm_h pwm_h = NULL;
247
248         if (g_feature == false) {
249                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH);
250                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
251
252         } else {
253                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
254                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
255
256                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH);
257                 if (ret != PERIPHERAL_ERROR_NONE) {
258                         peripheral_pwm_close(pwm_h);
259                         return PERIPHERAL_ERROR_UNKNOWN;
260                 }
261
262                 ret = peripheral_pwm_close(pwm_h);
263                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
264         }
265
266         return PERIPHERAL_ERROR_NONE;
267 }
268
269 int test_peripheral_io_pwm_peripheral_pwm_set_polarity_p2(void)
270 {
271         int ret = PERIPHERAL_ERROR_NONE;
272
273         peripheral_pwm_h pwm_h = NULL;
274
275         if (g_feature == false) {
276                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_LOW);
277                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
278
279         } else {
280                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
281                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
282
283                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_LOW);
284                 if (ret != PERIPHERAL_ERROR_NONE) {
285                         peripheral_pwm_close(pwm_h);
286                         return PERIPHERAL_ERROR_UNKNOWN;
287                 }
288
289                 ret = peripheral_pwm_close(pwm_h);
290                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
291         }
292
293         return PERIPHERAL_ERROR_NONE;
294 }
295
296 int test_peripheral_io_pwm_peripheral_pwm_set_polarity_n1(void)
297 {
298         int ret = PERIPHERAL_ERROR_NONE;
299
300         if (g_feature == false) {
301                 ret = peripheral_pwm_set_polarity(NULL, PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH);
302                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
303
304         } else {
305                 ret = peripheral_pwm_set_polarity(NULL, PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH);
306                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
307         }
308
309         return PERIPHERAL_ERROR_NONE;
310 }
311
312 int test_peripheral_io_pwm_peripheral_pwm_set_polarity_n2(void)
313 {
314         int ret = PERIPHERAL_ERROR_NONE;
315
316         peripheral_pwm_h pwm_h = NULL;
317
318         if (g_feature == false) {
319                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH - 1);
320                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
321
322         } else {
323                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
324                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
325
326                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH - 1);
327                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) {
328                         peripheral_pwm_close(pwm_h);
329                         return PERIPHERAL_ERROR_UNKNOWN;
330                 }
331
332                 ret = peripheral_pwm_close(pwm_h);
333                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
334         }
335
336         return PERIPHERAL_ERROR_NONE;
337 }
338
339 int test_peripheral_io_pwm_peripheral_pwm_set_polarity_n3(void)
340 {
341         int ret = PERIPHERAL_ERROR_NONE;
342
343         peripheral_pwm_h pwm_h = NULL;
344
345         if (g_feature == false) {
346                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_LOW + 1);
347                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
348
349         } else {
350                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
351                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
352
353                 ret = peripheral_pwm_set_polarity(pwm_h, PERIPHERAL_PWM_POLARITY_ACTIVE_LOW + 1);
354                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) {
355                         peripheral_pwm_close(pwm_h);
356                         return PERIPHERAL_ERROR_UNKNOWN;
357                 }
358
359                 ret = peripheral_pwm_close(pwm_h);
360                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
361         }
362
363         return PERIPHERAL_ERROR_NONE;
364 }
365
366 int test_peripheral_io_pwm_peripheral_pwm_set_enabled_p1(void)
367 {
368         int ret = PERIPHERAL_ERROR_NONE;
369
370         peripheral_pwm_h pwm_h = NULL;
371
372         bool enable = true;
373
374         if (g_feature == false) {
375                 ret = peripheral_pwm_set_enabled(pwm_h, enable);
376                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
377
378         } else {
379                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
380                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
381
382                 ret = peripheral_pwm_set_enabled(pwm_h, enable);
383                 if (ret != PERIPHERAL_ERROR_NONE) {
384                         peripheral_pwm_close(pwm_h);
385                         return PERIPHERAL_ERROR_UNKNOWN;
386                 }
387
388                 ret = peripheral_pwm_close(pwm_h);
389                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
390         }
391
392         return PERIPHERAL_ERROR_NONE;
393 }
394
395 int test_peripheral_io_pwm_peripheral_pwm_set_enabled_p2(void)
396 {
397         int ret = PERIPHERAL_ERROR_NONE;
398
399         peripheral_pwm_h pwm_h = NULL;
400
401         bool enable = false;
402
403         if (g_feature == false) {
404                 ret = peripheral_pwm_set_enabled(pwm_h, enable);
405                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
406
407         } else {
408                 ret = peripheral_pwm_open(chip, pin, &pwm_h);
409                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
410
411                 ret = peripheral_pwm_set_enabled(pwm_h, enable);
412                 if (ret != PERIPHERAL_ERROR_NONE) {
413                         peripheral_pwm_close(pwm_h);
414                         return PERIPHERAL_ERROR_UNKNOWN;
415                 }
416
417                 ret = peripheral_pwm_close(pwm_h);
418                 if (ret != PERIPHERAL_ERROR_NONE) return PERIPHERAL_ERROR_UNKNOWN;
419         }
420
421         return PERIPHERAL_ERROR_NONE;
422 }
423
424 int test_peripheral_io_pwm_peripheral_pwm_set_enabled_n(void)
425 {
426         int ret = PERIPHERAL_ERROR_NONE;
427
428         bool enable = true;
429
430         if (g_feature == false) {
431                 ret = peripheral_pwm_set_enabled(NULL, enable);
432                 if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED) return PERIPHERAL_ERROR_UNKNOWN;
433
434         } else {
435                 ret = peripheral_pwm_set_enabled(NULL, enable);
436                 if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) return PERIPHERAL_ERROR_UNKNOWN;
437         }
438
439         return PERIPHERAL_ERROR_NONE;
440 }