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 <sys/ioctl.h> |
|
15 |
#include <fcntl.h> |
|
16 |
|
|
17 |
#define CHR_MAGIC 'c' |
|
18 |
#define CMD_READ _IOR(CHR_MAGIC, 0, int) |
|
19 |
#define CMD_WRITE _IOW(CHR_MAGIC, 1, int) |
|
20 |
|
|
21 |
int main (int argc, char **argv) |
|
22 |
{ |
|
23 |
char *devname = "/dev/chrdev0"; |
|
24 |
int value; |
|
25 |
int fd; |
|
26 |
|
|
27 |
fd = open(devname, O_RDWR); |
|
28 |
if( fd < 0 ) |
|
29 |
{ |
|
30 |
printf("Open device %s failed: %s\n", devname, strerror(errno)); |
|
31 |
return 1; |
|
32 |
} |
|
33 |
|
|
34 |
if( ioctl(fd, CMD_READ, &value) < 0 ) |
|
35 |
{ |
|
36 |
printf("ioctl() failed: %s\n", strerror(errno)); |
|
37 |
goto cleanup; |
|
38 |
} |
|
39 |
printf("Default value in driver: 0x%0x\n", value); |
|
40 |
|
|
41 |
value = 0x12345678; |
|
42 |
if( ioctl(fd, CMD_WRITE, &value) < 0 ) |
|
43 |
{ |
|
44 |
printf("ioctl() failed: %s\n", strerror(errno)); |
|
45 |
goto cleanup; |
|
46 |
} |
|
47 |
printf("Wriee value into driver: 0x%0x\n", value); |
|
48 |
|
|
49 |
value = 0; |
|
50 |
if( ioctl(fd, CMD_READ, &value) < 0 ) |
|
51 |
{ |
|
52 |
printf("ioctl() failed: %s\n", strerror(errno)); |
|
53 |
goto cleanup; |
|
54 |
} |
|
55 |
printf("Read value from driver : 0x%0x\n", value); |
|
56 |
|
|
57 |
cleanup: |
|
58 |
close(fd); |
|
59 |
return 0; |
|
60 |
} |