aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/KINETIS/FRDM-KL25Z/USB_HID/Client/test-usb-hid.c
blob: 241b129f5ed520c0f95531e5e3dfb2a517de5eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*

  Copyright (c) 2014 Guillaume Duc <guillaume@guiduc.org>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

*/

#include <linux/types.h>
#include <linux/input.h>
#include <linux/hidraw.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <assert.h>

#define USB_HID_IN_REPORT_SIZE 1
#define USB_HID_OUT_REPORT_SIZE 1

struct usb_hid_in_report_s
{
  uint8_t sequence_number;
};

struct usb_hid_out_report_s
{
  uint8_t sequence_number;
};

static uint8_t usb_hid_in_report_buf[USB_HID_IN_REPORT_SIZE];
// +1 for the report index
static uint8_t usb_hid_out_report_buf[USB_HID_OUT_REPORT_SIZE + 1];

static struct usb_hid_in_report_s *usb_hid_in_report =
  (struct usb_hid_in_report_s *) usb_hid_in_report_buf;

static struct usb_hid_out_report_s *usb_hid_out_report =
  (struct usb_hid_out_report_s *) (&usb_hid_out_report_buf[1]);

static int usb_hid_fd;
static uint8_t wkup_pb_old_value = 0;

static void
read_in_report ()
{
  int res, i;

  printf ("read()\n");
  res = read (usb_hid_fd, usb_hid_in_report_buf, USB_HID_IN_REPORT_SIZE);
  if (res < 0)
    {
      perror ("read");
      exit (EXIT_FAILURE);
    }
  else
    {
      printf ("read() read %d bytes:\t", res);
      for (i = 0; i < res; i++)
	printf ("%02hhx ", usb_hid_in_report_buf[i]);
      printf ("\n");
    }
}

static void
send_out_report ()
{
  int res;

  usb_hid_out_report_buf[0] = 0;

  res =
    write (usb_hid_fd, usb_hid_out_report_buf, USB_HID_OUT_REPORT_SIZE + 1);
  if (res < 0)
    {
      perror ("write");
      exit (EXIT_FAILURE);
    }

  usb_hid_out_report->sequence_number++;
}

static void
usb_hid_init (const char *dev_name)
{
  int i, res;
  int desc_size = 0;
  char buf[256];

  struct hidraw_report_descriptor rpt_desc;
  struct hidraw_devinfo info;

  usb_hid_fd = open (dev_name, O_RDWR);

  if (usb_hid_fd < 0)
    {
      perror ("Unable to open device");
      exit (EXIT_FAILURE);
    }

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

  // Get Report Descriptor Size
  res = ioctl (usb_hid_fd, HIDIOCGRDESCSIZE, &desc_size);
  if (res < 0)
    perror ("HIDIOCGRDESCSIZE");
  else
    printf ("Report Descriptor Size: %d\n", desc_size);

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

int
main (int argc, char **argv)
{
  if (argc < 2)
    {
      fprintf (stderr, "Usage: %s /dev/hidrawX\n", argv[0]);
      return EXIT_FAILURE;
    }

  memset (usb_hid_out_report_buf, 0, sizeof (usb_hid_out_report_buf));

  usb_hid_init (argv[1]);
  usb_hid_out_report->sequence_number = 4;
  send_out_report ();

  while (1)
    {
      read_in_report ();

      if (usb_hid_in_report->sequence_number == 40)
	{
	  usb_hid_out_report->sequence_number = usb_hid_in_report->sequence_number / 2;
	  send_out_report ();
	}

    }

  close (usb_hid_fd);

  return EXIT_SUCCESS;
}