/*
 * Hidraw Userspace Harness Example
 *
 * Copyright (c) 2010 Alan Ott <alan@signal11.us>
 * Copyright (c) 2010 Signal 11 Software
 *
 * The code may be used by anyone for any purpose,
 * and can serve as a starting point for developing
 * applications using hidraw.
 */

#include <linux/types.h>

#define BUS_USB            0x03
#define BUS_HIL            0x04
#define BUS_BLUETOOTH      0x05
#define BUS_VIRTUAL        0x06

#define HID_MAX_DESCRIPTOR_SIZE     4096
struct hidraw_report_descriptor {
    __u32 size;
    __u8 value[HID_MAX_DESCRIPTOR_SIZE];
};
struct hidraw_devinfo {
    __u32 bustype;
    __s16 vendor;
    __s16 product;
};

/* Unix */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/* C */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

const char *bus_str(int bus);

int main(int argc, char **argv)
{
    int fd, out;
    int i, res, desc_size = 0;
    char buf[256];
    struct hidraw_report_descriptor rpt_desc;
    struct hidraw_devinfo info;

    /* Open the Device with non-blocking reads. In real life,
       don't use a hard coded path; use libudev instead. */
    fd = open(argv[1], O_RDONLY);

    if (fd < 0) {
        perror("Unable to open input file");
        return 1;
    }

    out = open("output", O_WRONLY|O_CREAT);

    if (out < 0) {
        perror("Unable to open output file");
        return 1;
    }

    memset(&rpt_desc, 0x0, sizeof(rpt_desc));
    memset(&info, 0x0, sizeof(info));
    memset(buf, 0x0, sizeof(buf));

    /* Get Report Descriptor Size */
    res = read(fd, &desc_size, sizeof(int));
    if (res < 0)
        perror("HIDIOCGRDESCSIZE");
    else
        printf("Report Descriptor Size: %d\n", desc_size);

    /* Get Report Descriptor */
    rpt_desc.size = desc_size;
    res = read(fd, &rpt_desc, sizeof(struct hidraw_report_descriptor));
    if (res < 0) {
        perror("HIDIOCGRDESC");
    } else {
        printf("Report Descriptor:\n");
        for (i = 0; i < rpt_desc.size; i++)
            printf("%hhx ", rpt_desc.value[i]);
        puts("\n");
    }

    /* Get Raw Name */
    res = read(fd, buf, sizeof(buf));
    if (res < 0)
        perror("HIDIOCGRAWNAME");
    else
        printf("Raw Name: %s\n", buf);

    /* Get Physical Location */
    res = read(fd, buf, sizeof(buf));
    if (res < 0)
        perror("HIDIOCGRAWPHYS");
    else
        printf("Raw Phys: %s\n", buf);

    /* Get Raw Info */
    res = read(fd, &info, sizeof(struct hidraw_devinfo));
    if (res < 0) {
        perror("HIDIOCGRAWINFO");
    } else {
        printf("Raw Info:\n");
        printf("\tbustype: %d (%s)\n",
            info.bustype, bus_str(info.bustype));
        printf("\tvendor: 0x%04hx\n", info.vendor);
        printf("\tproduct: 0x%04hx\n", info.product);
    }

    /* Set Feature */
    buf[0] = 0x9; /* Report Number */
    buf[1] = 0xff;
    buf[2] = 0xff;
    buf[3] = 0xff;
    write(out, buf, 4);
    res = read(fd, buf, 4);
    if (res < 0)
        perror("HIDIOCSFEATURE");
    else
        printf("ioctl HIDIOCGFEATURE returned: %d\n", res);

    /* Get Feature */
    buf[0] = 0x9; /* Report Number */
    write(out, buf, sizeof(buf));
    res = read(fd, buf, sizeof(buf));
    if (res < 0) {
        perror("HIDIOCGFEATURE");
    } else {
        printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
        printf("Report data (not containing the report number):\n\t");
        for (i = 0; i < res; i++)
            printf("%hhx ", buf[i]);
        puts("\n");
    }

    /* Send a Report to the Device */
    buf[0] = 0x1; /* Report Number */
    buf[1] = 0x77;
    res = write(out, buf, 2);
    if (res < 0) {
        printf("Error: %d\n", errno);
        perror("write");
    } else {
        printf("write() wrote %d bytes\n", res);
    }

    /* Get a report from the device */
    res = read(fd, buf, 16);
    if (res < 0) {
        perror("read");
    } else {
        printf("read() read %d bytes:\n\t", res);
        for (i = 0; i < res; i++)
            printf("%hhx ", buf[i]);
        puts("\n");
    }
    close(fd);
    close(out);
    return 0;
}

const char *
bus_str(int bus)
{
    switch (bus) {
    case BUS_USB:
        return "USB";
        break;
    case BUS_HIL:
        return "HIL";
        break;
    case BUS_BLUETOOTH:
        return "Bluetooth";
        break;
    case BUS_VIRTUAL:
        return "Virtual";
        break;
    default:
        return "Other";
        break;
    }
}

