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
| /*
| * Copyright (C) 2024 LingYun IoT System Studio
| * Author: Guo Wenxue <guowenxue@gmail.com>
| *
| * A character skeleton driver test code in user space.
| */
|
| #include <stdio.h>
| #include <unistd.h>
| #include <string.h>
| #include <errno.h>
| #include <sys/types.h>
| #include <sys/stat.h>
| #include <fcntl.h>
|
| int main (int argc, char **argv)
| {
| char *devname = "/dev/chrdev0";
| char buf[1024];
| int rv = 0;
| int fd;
|
| fd = open(devname, O_RDWR);
| if( fd < 0 )
| {
| printf("Open device %s failed: %s\n", devname, strerror(errno));
| return 1;
| }
|
| rv = write(fd, "Hello", 5);
| if( rv< 0)
| {
| printf("Write data into device failed, rv=%d: %s\n", rv, strerror(errno));
| rv = 2;
| goto cleanup;
| }
| printf("Write %d bytes data okay\n", rv);
|
| memset(buf, 0, sizeof(buf));
| rv = read(fd, buf, sizeof(buf));
| if( rv< 0)
| {
| printf("Read data from device failed, rv=%d: %s\n", rv, strerror(errno));
| rv = 3;
| goto cleanup;
| }
| printf("Read %d bytes data: %s\n", rv, buf);
|
| cleanup:
| close(fd);
| return rv;
| }
|
|