summaryrefslogtreecommitdiffstats
path: root/app/kvm.c
blob: 3c8a9004b4fb4ffb921966f1b89d3f4aab4f25e6 (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
#include "project.h"


static enum
{
  STATE_SYNC = 0,
  STATE_ADDR = 1,
  STATE_ENDPOINT = 2,
  STATE_LEN = 3,
  STATE_DATA = 4,
} state = STATE_SYNC;


static uint8_t buf[16];
static int addr;
static int endpoint;
static unsigned int len;
static unsigned int ptr;

void
kvm_dispatch (void)
{
#if 0
  printf
    ("Addr %d, Write to ep %x, %d bytes %02x %02x %02x %02x ...\r\n",
     addr, endpoint, len, buf[0], buf[1], buf[2], buf[3]);
#endif

  if (len)
    usbd_ep_write_packet (usbd_dev, endpoint, buf, len);
}

void
kvm_recv (uint8_t d)
{
//  printf ("S%d V0x%02x\r\n", state, d);

  if (state == STATE_ADDR)
    {
      addr = d;
      usart_queue (d - 1);
      state = STATE_ENDPOINT;
      return;
    }
  else
    {
      usart_queue (d);
    }


  switch (state)
    {
    case STATE_SYNC:
      if (d != 0x5a)
        break;
      state = STATE_ADDR;
      break;
    case STATE_ENDPOINT:
      endpoint = d;
      if ((endpoint < 0x81) || (endpoint > 0x83))
        {
          state = STATE_SYNC;
        }
      else
        {
          state = STATE_LEN;
        }
      break;
    case STATE_LEN:
      len = d;
      ptr = 0;
      if (len > sizeof (buf))
        {
          state = STATE_SYNC;
        }
      else
        {
          state = STATE_DATA;
        }
      break;
    case STATE_DATA:
      buf[ptr++] = d;
      if (ptr >= len)
        {
          if (!addr)
            kvm_dispatch ();
          state = STATE_SYNC;
        }
      break;
    }
}