nethid.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Network HID library
  3. * Very simple network program that reads for HID codes
  4. * and forwards them to the HID fifo (after some parsing in case of an escape character)
  5. */
  6. // uses wiz5500.c
  7. // uses hidfifo.c
  8. // Port for network bootloader
  9. #define NETHID_PORT 3222
  10. // Socket to listen to (0-7)
  11. #define NETHID_SOCKET 7
  12. word NETHID_isInitialized = 0;
  13. // Buffer to read the RX buffer from W5500
  14. char NETHID_rxBuf[WIZNET_MAX_RBUF];
  15. // Handle session for socket s
  16. // Data can contain multiple buffered inputs
  17. void NETHID_handleSession(word s)
  18. {
  19. // Size of received data
  20. // Double check to reduce error rate
  21. word rsize = 0;
  22. word rsizeValidate = 0;
  23. do {
  24. rsize = wiz_get_sock_reg_16(s, WIZNET_SnRX_RSR);
  25. if (rsize != 0)
  26. {
  27. // twice on purpose
  28. rsizeValidate = wiz_get_sock_reg_16(s, WIZNET_SnRX_RSR);
  29. rsizeValidate = wiz_get_sock_reg_16(s, WIZNET_SnRX_RSR);
  30. }
  31. }
  32. while (rsize != rsizeValidate);
  33. if (rsize == 0)
  34. {
  35. // ignore, probably no data yet
  36. return;
  37. }
  38. // receive data
  39. wiz_read_recv_data(s, NETHID_rxBuf, rsize);
  40. NETHID_rxBuf[rsize] = 0; //terminate
  41. // echo data back to confirm receive
  42. wiz_write_data(s, NETHID_rxBuf, rsize);
  43. // loop through the received data
  44. word isEscaped = 0;
  45. word ignoreThis = 0;
  46. word i;
  47. for (i = 0; i < rsize; i++)
  48. {
  49. if (ignoreThis)
  50. {
  51. ignoreThis--;
  52. }
  53. else
  54. {
  55. if (isEscaped)
  56. {
  57. isEscaped = 0;
  58. switch (NETHID_rxBuf[i])
  59. {
  60. case 65: // up
  61. HID_FifoWrite(258);
  62. break;
  63. case 66: // down
  64. HID_FifoWrite(259);
  65. break;
  66. case 67: // right
  67. HID_FifoWrite(257);
  68. break;
  69. case 68: // left
  70. HID_FifoWrite(256);
  71. break;
  72. }
  73. }
  74. else
  75. {
  76. // check for escape code
  77. if (NETHID_rxBuf[i] == 27)
  78. {
  79. ignoreThis = 1; // skip the next one
  80. isEscaped = 1;
  81. // do real escape if this is the final character in buffer
  82. // since we do not expect anything quickly after an escape
  83. if (i == rsize-1)
  84. {
  85. HID_FifoWrite(NETHID_rxBuf[i]);
  86. }
  87. }
  88. else if (NETHID_rxBuf[i] == 127) // backspace
  89. {
  90. HID_FifoWrite(8);
  91. }
  92. else // no parsing needed
  93. {
  94. HID_FifoWrite(NETHID_rxBuf[i]);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. // Initialize netHID on socket s
  101. void NETHID_init(word s)
  102. {
  103. // Open socket in TCP Server mode
  104. wiz_init_socket_tcp_host(s, NETHID_PORT);
  105. NETHID_isInitialized = 1;
  106. }
  107. // Check for a change in the socket status
  108. // Handles change if exists
  109. void NETHID_loop(word s)
  110. {
  111. // Get status for socket s
  112. word sxStatus = wiz_get_sock_reg_8(s, WIZNET_SnSR);
  113. if (sxStatus == WIZNET_SOCK_CLOSED)
  114. {
  115. // Open the socket when closed
  116. wiz_init_socket_tcp_host(s, NETHID_PORT);
  117. }
  118. else if (sxStatus == WIZNET_SOCK_ESTABLISHED)
  119. {
  120. // Handle session when a connection is established
  121. NETHID_handleSession(s);
  122. }
  123. else if (sxStatus == WIZNET_SOCK_LISTEN)
  124. {
  125. // Keep on listening
  126. }
  127. else if (sxStatus == WIZNET_SOCK_SYNSENT || sxStatus == WIZNET_SOCK_SYNRECV || sxStatus == WIZNET_SOCK_FIN_WAIT || sxStatus == WIZNET_SOCK_TIME_WAIT)
  128. {
  129. // Do nothing in these cases
  130. }
  131. else
  132. {
  133. /*
  134. uprintln("Got unknown status:");
  135. uprintlnHex(sxStatus);
  136. */
  137. // In other cases, reset the socket
  138. wiz_init_socket_tcp_host(s, NETHID_PORT);
  139. }
  140. return;
  141. }