summaryrefslogtreecommitdiff
path: root/src/taler-coin-acceptor.c
blob: fde4a8a0148f8d528d3c1e6571d9afb936607b09 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 This file is part of TALER
 Copyright (C) 2022 Taler Systems SA

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

 You should have received a copy of the GNU General Public License
along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/

/**
 * @brief Program to talk to the coin acceptor via USB-RS232.
 *
 * @author Christian Grothoff
 */
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <libusb-1.0/libusb.h>

// FIXME: make this configurable later...
/* future technology devices international */
#define VENDOR_ID      0x0403
/* FT232 Serial UART IC */
#define PRODUCT_ID     0x6001

#define USB_CTL_REQ_SET_LINE_CODING 0x20

#define LEO_ONE_STOP_BIT 0x00
#define LEO_ONEHALF_STOP_BIT 0x01
#define LEO_TWO_STOP_BIT 0x02

#define LEO_PARITY_NONE 0x00
#define LEO_PARITY_ODD 0x01
#define LEO_PARITY_EVEN 0x02
#define LEO_PARITY_MARK 0x03
#define LEO_PARITY_SPACE 0x04


#define ACM_CTRL_DTR   0x01
#define ACM_CTRL_RTS   0x02

/**
 * Handle to USB device.
 */
static struct libusb_device_handle *device_handle;

/* The Endpoint address are hard coded. You should use lsusb -v to find
 * the values corresponding to your device.
 */
static int ep_in_addr  = 0x83;
static int ep_out_addr = 0x02;


static int
read_char (void)
{
  int actual_length;
  unsigned char data;
  int rc;

  rc = libusb_bulk_transfer (device_handle,
                             ep_in_addr,
                             &data,
                             sizeof (data),
                             &actual_length,
                             1000 /* timeout? */);
  if (rc == LIBUSB_ERROR_TIMEOUT)
  {
    fprintf (stderr,
             "TIMEOUT\n");
    return EOF;
  }
  if (rc < 0)
  {
    fprintf (stderr,
             "Error %s while waiting for char\n",
             libusb_error_name (rc));
    return EOF;
  }
  return data;
}


int
main (int argc,
      char **argv)
{
  int rc;

  rc = libusb_init (NULL);
  if (rc < 0)
  {
    fprintf (stderr,
             "Error initializing usb: %s\n",
             libusb_error_name (rc));
    return 1;
  }

  libusb_set_debug (NULL,
                    3);
  device_handle = libusb_open_device_with_vid_pid (NULL,
                                                   VENDOR_ID,
                                                   PRODUCT_ID);
  if (NULL == device_handle)
  {
    fprintf (stderr,
             "Error finding USB device\n");
    libusb_exit (NULL);
    return 1;
  }

  /* Maybe there is someone else attached, detach other drivers from
     all the USB interfaces. NOTE: not sure if there are *2*!  */
  for (int if_num = 0; if_num < 2; if_num++)
  {
    if (libusb_kernel_driver_active (device_handle,
                                     if_num))
    {
      libusb_detach_kernel_driver (device_handle,
                                   if_num);
    }
    rc = libusb_claim_interface (device_handle,
                                 if_num);
    if (rc < 0)
    {
      fprintf (stderr,
               "Error claiming interface: %s\n",
               libusb_error_name (rc));
      libusb_close (device_handle);
      libusb_exit (NULL);
      return 1;
    }
  }
#if 1
  rc = libusb_control_transfer (device_handle,
                                0x21,
                                0x22,
                                ACM_CTRL_DTR | ACM_CTRL_RTS,
                                0,
                                NULL,
                                0,
                                0);
  if (rc < 0)
  {
    fprintf (stderr,
             "Error during control transfer: %s\n",
             libusb_error_name (rc));
    libusb_close (device_handle);
    libusb_exit (NULL);
    return 1;
  }
#endif

  /* - set line encoding: here 9600 8N1
   * 9600 = 0x2580 ~> 0x80, 0x25 in little endian
   */
  {
    struct LineEncodingOptions
    {
      /**
       * In *little* endian.
       */
      uint32_t baud_rate;
      uint8_t b_char_format;
      uint8_t b_parity_type;
      uint8_t b_data_bits;
    } leo = {
      .baud_rate = __builtin_bswap32 (htonl (4800)),
      .b_char_format = LEO_ONE_STOP_BIT,
      .b_parity_type = LEO_PARITY_ODD,
      .b_data_bits = 0x08
    };

    rc = libusb_control_transfer (device_handle,
                                  0x21,
                                  USB_CTL_REQ_SET_LINE_CODING,
                                  0,
                                  0,
                                  (unsigned char *) &leo,
                                  sizeof(leo),
                                  0);
    if (rc < 0)
    {
      fprintf (stderr,
               "Error during control transfer: %s\n",
               libusb_error_name (rc));
      libusb_close (device_handle);
      libusb_exit (NULL);
      return 1;
    }
  }

  /* FIXME: proper event loop... */
  while (1)
  {
    int in = read_char ();

    if (-1 != in)
      fprintf (stdout,
               "Received: %d\n",
               in);
  }

  libusb_release_interface (device_handle,
                            0);
  libusb_close (device_handle);
  libusb_exit (NULL);
  return 0;
}