summaryrefslogtreecommitdiffstats
path: root/arm.c
blob: 3e6510b43d9a95baac4ed21f63f50741038b74ed (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
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>



int happy = 0;

int valid_fn (unsigned char fn)
{
  switch (fn) {
  case 0x30:
  case 0x31:
  case 0x32:
  case 0x33:
  case 0x34:
  case 0x35:
  case 0x36:
  case 0x37:
  case 0x38:
  case 0x08:
  case 0x39:
  case 0x09:
  case 0x43:
  case 0x45:
  case 0x4E:
  case 0x4F:
  case 0x50:
  case 0x40:
  case 0x3F:
  case 0x23:
  case 0x26:
  case 0x41:
  case 0x58:
  case 0x4C:
  case 0x56:
  case 0x76:
  case 0x49:
    return 1;
  }

  return 0;
}

int ackable (unsigned char fn)
{
  switch (fn) {


  case 0x09:
  case 0x39:
  case 0x08:
  case 0x38:
  case 0x40:
  case 0x43:
  case 0x58:
    return 0;
  }

  return 1;
}


void ack (int fd)
{
  char buf[3];

  buf[0] = 0;
  buf[1] = 0x38;
  buf[2] = 0xff ^ buf[0] ^ buf[1];

  write (fd, buf, sizeof (buf));

}

int rx_pkt (int fd, unsigned char *pkt, unsigned len)
{
  unsigned char parity = 0xff;
  unsigned i;

  for (i = 0; i < len; ++i)
    parity ^= pkt[i];

  if (parity) return -1;

  happy++;

  for (i = 2; i < len - 1; ++i) {
    if ((pkt[i] > 31) && (pkt[i] < 127))
      putchar (pkt[i]);
    else
      printf ("\\x%02x", pkt[i]);
  }

  printf ("\n");

  if ((pkt[0] & 0x40) && ackable (pkt[1])) ack (fd);

  return 0;
}



int rx (int fd)
{
  static unsigned char pkt[67];
  static unsigned ptr;
  unsigned char byte;
  unsigned len;

  if (read (fd, &byte, 1) != 1) {
    ptr = 0;
    return -1;
  }

  pkt[ptr++] = byte;

  if (ptr > 2 && !valid_fn (pkt[1])) {
    ptr = 0;
    return 0;
  }

  len = pkt[0] & 0x3f;

  if (ptr == (len + 3)) {
    ptr = 0;
    return rx_pkt (fd, pkt, len + 3);
  }

  return 0;
}



int transact (int fd, int ack, unsigned char fn, void *data, unsigned len, int timeout)
{
  struct timeval tv = {0};
  int rc;
  fd_set rfds;
  unsigned char buf[67], parity;
  unsigned i;

  happy = 0;

  tv.tv_sec = timeout;

  buf[0] = len;

  if (ack) buf[0] |= 0x40;

  buf[1] = fn;

  memcpy (&buf[2], data, len);

  parity = 0xff;

  for (i = 0; i < (len + 2); ++i)
    parity ^= buf[i];

  buf[i] = parity;
  i++;


  write (fd, buf, i);

  fwrite (data, 1, len, stdout);

  printf (" => ");
  fflush (stdout);



  while (!happy) {

    FD_ZERO (&rfds);

    FD_SET (fd, &rfds);

    rc = select (fd + 1, &rfds, NULL, NULL, &tv);

    if (!rc) break;

    rx (fd);

  }

  return happy;
}



int main (int argc, char *argv[])
{
  struct sockaddr_in sin;
  int fd;

  unsigned char buf[128];

  fd = socket (PF_INET, SOCK_STREAM, 0);

  sin.sin_family = AF_INET;
  sin.sin_port = htons (10005);
  inet_aton ("10.32.52.66", &sin.sin_addr);

  connect (fd, (struct sockaddr *)&sin, sizeof (sin));



  transact (fd, 1, 0x3f, "543210", 6, 4);
  transact (fd, 1, 0x43, "SA", 2, 4);

  transact (fd, 1, 0x3f, "543210", 6, 4);
  transact (fd, 1, 0x43, "SA91", 4, 4);

  transact (fd, 1, 0x3f, "543210", 6, 4);
  transact (fd, 1, 0x43, "SA92", 4, 4);

  if (argc == 2) {
    sprintf (buf, "SA1*%s", argv[1]);
    transact (fd, 1, 0x3f, "543210", 6, 4);
    transact (fd, 1, 0x43, buf, strlen (buf), 4);
  }

  close (fd);
}