commit | author | age
|
f5c330
|
1 |
/********************************************************************************* |
G |
2 |
* Copyright: (C) 2023 Avnet |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: file.c |
|
6 |
* Description: This file |
|
7 |
* |
|
8 |
* Version: 1.0.0(2023年11月02日) |
|
9 |
* Author: Guo Wenxue <guowenxue@avnet.com> |
|
10 |
* ChangeLog: 1, Release initial version on "2023年11月02日 14时47分52秒" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#include <stdio.h> |
|
15 |
#include <unistd.h> |
|
16 |
#include <string.h> |
|
17 |
#include <errno.h> |
|
18 |
#include <fcntl.h> |
|
19 |
#include <sys/types.h> |
|
20 |
#include <sys/stat.h> |
|
21 |
#include <sys/ioctl.h> |
|
22 |
|
|
23 |
/* ioctl definitions, use 'z' as magic number */ |
|
24 |
#define CHRDEV_IOC_MAGIC 'z' |
|
25 |
#define CHRDEV_IOCRESET _IO(CHRDEV_IOC_MAGIC, 0) |
|
26 |
#define CHRDEV_IOCSET _IOW(CHRDEV_IOC_MAGIC, 1, int) |
|
27 |
#define CHRDEV_IOCGET _IOR(CHRDEV_IOC_MAGIC, 2, int) |
|
28 |
|
|
29 |
int main (int argc, char **argv) |
|
30 |
{ |
|
31 |
char *devname = "/dev/chrdev"; |
|
32 |
char buf[1024]; |
|
33 |
int rv, fd; |
|
34 |
int value = 0x12345678; |
|
35 |
|
|
36 |
fd = open(devname, O_RDWR|O_TRUNC); |
|
37 |
if( fd < 0 ) |
|
38 |
{ |
|
39 |
printf("Open device %s failed: %s\n", devname, strerror(errno)); |
|
40 |
return 1; |
|
41 |
} |
|
42 |
|
|
43 |
rv = write(fd, "Hello", 5); |
|
44 |
if( rv< 0) |
|
45 |
{ |
|
46 |
printf("Write data into device failed, rv=%d: %s\n", rv, strerror(errno)); |
|
47 |
return 2; |
|
48 |
} |
|
49 |
printf("Write %d bytes data okay\n", rv); |
|
50 |
|
|
51 |
memset(buf, 0, sizeof(buf)); |
|
52 |
rv = read(fd, buf, sizeof(buf)); |
|
53 |
if( rv< 0) |
|
54 |
{ |
|
55 |
printf("Read data from device failed, rv=%d: %s\n", rv, strerror(errno)); |
|
56 |
return 3; |
|
57 |
} |
|
58 |
printf("Read %d bytes data: %s\n", rv, buf); |
|
59 |
|
|
60 |
ioctl(fd, CHRDEV_IOCSET, &value); |
|
61 |
value = 0; |
|
62 |
ioctl(fd, CHRDEV_IOCGET, &value); |
|
63 |
printf("ioctl() read value=0x%0x\n", value); |
|
64 |
|
|
65 |
close(fd); |
|
66 |
return 0; |
|
67 |
} |
|
68 |
|