/*********************************************************************************
|
* Copyright: (C) 2023 Avnet
|
* All rights reserved.
|
*
|
* Filename: file.c
|
* Description: This file
|
*
|
* Version: 1.0.0(2023年11月02日)
|
* Author: Guo Wenxue <guowenxue@avnet.com>
|
* ChangeLog: 1, Release initial version on "2023年11月02日 14时47分52秒"
|
*
|
********************************************************************************/
|
|
#include <stdio.h>
|
#include <unistd.h>
|
#include <string.h>
|
#include <errno.h>
|
#include <fcntl.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <sys/ioctl.h>
|
|
/* ioctl definitions, use 'z' as magic number */
|
#define CHRDEV_IOC_MAGIC 'z'
|
#define CHRDEV_IOCRESET _IO(CHRDEV_IOC_MAGIC, 0)
|
#define CHRDEV_IOCSET _IOW(CHRDEV_IOC_MAGIC, 1, int)
|
#define CHRDEV_IOCGET _IOR(CHRDEV_IOC_MAGIC, 2, int)
|
|
int main (int argc, char **argv)
|
{
|
char *devname = "/dev/chrdev";
|
char buf[1024];
|
int rv, fd;
|
int value = 0x12345678;
|
|
fd = open(devname, O_RDWR|O_TRUNC);
|
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));
|
return 2;
|
}
|
printf("Write %d bytes data okay\n", rv);
|
|
#if 0
|
lseek(fd, 3, SEEK_SET);
|
#endif
|
|
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));
|
return 3;
|
}
|
printf("Read %d bytes data: %s\n", rv, buf);
|
|
#if 0
|
ioctl(fd, CHRDEV_IOCSET, &value);
|
value = 0;
|
ioctl(fd, CHRDEV_IOCGET, &value);
|
printf("ioctl() read value=0x%0x\n", value);
|
#endif
|
|
close(fd);
|
return 0;
|
}
|