commit | author | age
|
b8e5f6
|
1 |
/* |
G |
2 |
* Copyright (C) 2024 LingYun IoT System Studio |
|
3 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
4 |
* |
|
5 |
* A character skeleton driver test code in user space. |
|
6 |
*/ |
|
7 |
|
|
8 |
#include <stdio.h> |
|
9 |
#include <unistd.h> |
|
10 |
#include <string.h> |
|
11 |
#include <errno.h> |
|
12 |
#include <sys/types.h> |
|
13 |
#include <sys/stat.h> |
|
14 |
#include <fcntl.h> |
|
15 |
|
|
16 |
int main (int argc, char **argv) |
|
17 |
{ |
|
18 |
char *devname = "/dev/chrdev0"; |
|
19 |
char buf[1024]; |
|
20 |
int rv = 0; |
|
21 |
int fd; |
|
22 |
|
|
23 |
fd = open(devname, O_RDWR); |
|
24 |
if( fd < 0 ) |
|
25 |
{ |
|
26 |
printf("Open device %s failed: %s\n", devname, strerror(errno)); |
|
27 |
return 1; |
|
28 |
} |
|
29 |
|
|
30 |
rv = write(fd, "Hello", 5); |
|
31 |
if( rv< 0) |
|
32 |
{ |
|
33 |
printf("Write data into device failed, rv=%d: %s\n", rv, strerror(errno)); |
|
34 |
rv = 2; |
|
35 |
goto cleanup; |
|
36 |
} |
|
37 |
printf("Write %d bytes data okay\n", rv); |
|
38 |
|
|
39 |
memset(buf, 0, sizeof(buf)); |
|
40 |
rv = read(fd, buf, sizeof(buf)); |
|
41 |
if( rv< 0) |
|
42 |
{ |
|
43 |
printf("Read data from device failed, rv=%d: %s\n", rv, strerror(errno)); |
|
44 |
rv = 3; |
|
45 |
goto cleanup; |
|
46 |
} |
|
47 |
printf("Read %d bytes data: %s\n", rv, buf); |
|
48 |
|
|
49 |
cleanup: |
|
50 |
close(fd); |
|
51 |
return rv; |
|
52 |
} |