/*
|
* 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 <sys/ioctl.h>
|
#include <fcntl.h>
|
|
#define CHR_MAGIC 'c'
|
#define CMD_READ _IOR(CHR_MAGIC, 0, int)
|
#define CMD_WRITE _IOW(CHR_MAGIC, 1, int)
|
|
int main (int argc, char **argv)
|
{
|
char *devname = "/dev/chrdev0";
|
int value;
|
int fd;
|
|
fd = open(devname, O_RDWR);
|
if( fd < 0 )
|
{
|
printf("Open device %s failed: %s\n", devname, strerror(errno));
|
return 1;
|
}
|
|
if( ioctl(fd, CMD_READ, &value) < 0 )
|
{
|
printf("ioctl() failed: %s\n", strerror(errno));
|
goto cleanup;
|
}
|
printf("Default value in driver: 0x%0x\n", value);
|
|
value = 0x12345678;
|
if( ioctl(fd, CMD_WRITE, &value) < 0 )
|
{
|
printf("ioctl() failed: %s\n", strerror(errno));
|
goto cleanup;
|
}
|
printf("Wriee value into driver: 0x%0x\n", value);
|
|
value = 0;
|
if( ioctl(fd, CMD_READ, &value) < 0 )
|
{
|
printf("ioctl() failed: %s\n", strerror(errno));
|
goto cleanup;
|
}
|
printf("Read value from driver : 0x%0x\n", value);
|
|
cleanup:
|
close(fd);
|
return 0;
|
}
|