/* * Copyright (C) 2024 LingYun IoT System Studio * Author: Guo Wenxue * * A character skeleton driver test code in user space. */ #include #include #include #include #include #include #include int main (int argc, char **argv) { char *devname = "/dev/chrdev0"; char buf[1024]; int rv = 0; int fd; fd = open(devname, O_RDWR); 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)); rv = 2; goto cleanup; } printf("Write %d bytes data okay\n", rv); 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)); rv = 3; goto cleanup; } printf("Read %d bytes data: %s\n", rv, buf); cleanup: close(fd); return rv; }