forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivertest_relay.c
251 lines (209 loc) · 7.69 KB
/
drivertest_relay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************
* apps/testing/drivers/drivertest/drivertest_relay.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <nuttx/power/relay.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
/****************************************************************************
* Private Types
****************************************************************************/
struct relay_args_s
{
FAR char *devname; /* Relay device name */
int autotest; /* autotest or not */
bool set; /* set the relay driver */
bool setvalue; /* the setvalue, 0 : open, 1: close */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: show_usage
****************************************************************************/
static void show_usage(FAR const char *progname, int exitcode)
{
printf("Usage: %s -d <device name> -s <0/1> -g\n", progname);
printf("Where:\n");
printf(" -d relay device name [default name: /dev/relay0].\n");
printf(" -a autotest num, if autotest enable -s and -g are ignored.\n");
printf(" -s set relay status to [0 : open] or [1 : close].\n\n");
printf("Example 0:\n");
printf(" Description: auto test the relay driver 10 times\n");
printf(" cmocka_driver_relay -d /dev/relay0 -a 10\n\n");
printf("Example 1:\n");
printf(" Description: set the relay to close and then get the status\n");
printf(" cmocka_driver_relay -d /dev/relay0 -s 1\n\n");
printf("Example 2:\n");
printf(" Description: get the relay status\n");
printf(" cmocka_driver_relay -d /dev/relay0\n\n");
exit(exitcode);
}
/****************************************************************************
* Name: parse_commandline
****************************************************************************/
static void parse_commandline(int argc, FAR char **argv,
FAR struct relay_args_s *relay_args)
{
int option;
while ((option = getopt(argc, argv, "d:a:s:")) != ERROR)
{
switch (option)
{
case 'd':
relay_args->devname = optarg;
break;
case 'a':
relay_args->autotest = atoi(optarg);
break;
case 's':
relay_args->set = true;
relay_args->setvalue = atoi(optarg) != 0;
break;
case '?':
printf("Unknown option: %c\n", optopt);
show_usage(argv[0], EXIT_FAILURE);
break;
}
}
printf("Relay test args:\n");
printf(" devname = %s\n", relay_args->devname);
printf(" autotest = %d\n", relay_args->autotest);
printf(" set = %d\n", relay_args->set);
printf(" setvalue = %d\n", relay_args->setvalue);
}
/****************************************************************************
* Name: setup
****************************************************************************/
static int setup(FAR void **state)
{
return 0;
}
/****************************************************************************
* Name: relaytest01
****************************************************************************/
static void relaytest01(FAR void **state)
{
FAR struct relay_args_s *relay_args;
int fd;
bool getstatus;
bool setstatus;
int ret;
int i;
relay_args = (FAR struct relay_args_s *)*state;
fd = open(relay_args->devname, O_RDWR);
assert_true(fd > 0);
/* Auto test, repeat set different status to the relay and check the
* relay status.
*/
ret = ioctl(fd, RELAYIOC_GET, &getstatus);
assert_return_code(ret, OK);
for (i = 0; i < relay_args->autotest; i++)
{
setstatus = !getstatus;
ret = ioctl(fd, RELAYIOC_SET, &setstatus);
assert_return_code(ret, OK);
ret = ioctl(fd, RELAYIOC_GET, &getstatus);
assert_return_code(ret, OK);
assert_true(setstatus == getstatus);
usleep(100000);
}
/* Auto test, repeate set same status to the relay and check the relay
* status
*/
for (i = 0; i < relay_args->autotest; i++)
{
setstatus = getstatus;
ret = ioctl(fd, RELAYIOC_SET, &setstatus);
assert_return_code(ret, OK);
ret = ioctl(fd, RELAYIOC_GET, &getstatus);
assert_return_code(ret, OK);
assert_true(setstatus == getstatus);
usleep(100000);
}
/* If autotest enable, ignore the set/get operation */
if (relay_args->autotest != 0)
{
goto out;
}
/* Set/get the relay status */
if (relay_args->set)
{
ret = ioctl(fd, RELAYIOC_GET, &getstatus);
assert_return_code(ret, OK);
printf("Relay status before set: %s\n", getstatus ? "open" : "close");
ret = ioctl(fd, RELAYIOC_SET, &relay_args->setvalue);
assert_return_code(ret, OK);
printf("Set relay to: %s\n", relay_args->setvalue ? "open" : "close");
ret = ioctl(fd, RELAYIOC_GET, &getstatus);
assert_return_code(ret, OK);
printf("Relay status after set: %s\n", getstatus ? "open" : "close");
}
else
{
ret = ioctl(fd, RELAYIOC_GET, &getstatus);
assert_return_code(ret, OK);
printf("Relay status: %s\n", getstatus ? "open" : "close");
}
out:
close(fd);
}
/****************************************************************************
* Name: teardown
****************************************************************************/
static int teardown(FAR void **state)
{
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: drivertest_relay_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct relay_args_s relay_args =
{
.devname = "/dev/relay0", /* Relay device name */
.autotest = 0, /* autotest or not */
.set = false, /* set the relay driver */
.setvalue = false, /* the setvalue, 0 : open, 1: close */
};
parse_commandline(argc, argv, &relay_args);
const struct CMUnitTest tests[] =
{
cmocka_unit_test_prestate_setup_teardown(relaytest01, setup,
teardown, &relay_args),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}