emu_server.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. MSP430 Emulator
  3. Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)
  4. "MSP430 Emulator" is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. "MSP430 Emulator" is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef _EMU_SERVER_H_
  16. #define _EMU_SERVER_H_
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <stdint.h>
  22. #include <signal.h>
  23. #include <stdbool.h>
  24. #include <pthread.h>
  25. #include <libwebsockets.h>
  26. #include "../../main.h"
  27. #include "packet_queue.h"
  28. enum {
  29. CONTROL_PACKET_OPCODE = 0x00,
  30. CONSOLE_PACKET_OPCODE = 0x01,
  31. SERIAL_PACKET_OPCODE = 0x02,
  32. };
  33. enum {
  34. P1_0_ON_PACKET = 0x00,
  35. P1_0_OFF_PACKET = 0x01,
  36. P1_1_ON_PACKET = 0x02,
  37. P1_1_OFF_PACKET = 0x03,
  38. P1_2_ON_PACKET = 0x04,
  39. P1_2_OFF_PACKET = 0x05,
  40. P1_3_ON_PACKET = 0x06,
  41. P1_3_OFF_PACKET = 0x07,
  42. P1_4_ON_PACKET = 0x08,
  43. P1_4_OFF_PACKET = 0x09,
  44. P1_5_ON_PACKET = 0x0A,
  45. P1_5_OFF_PACKET = 0x0B,
  46. P1_6_ON_PACKET = 0x0C,
  47. P1_6_OFF_PACKET = 0x0D,
  48. P1_7_ON_PACKET = 0x0E,
  49. P1_7_OFF_PACKET = 0x0F,
  50. UPDATE_REG_R0_PACKET = 0x10,
  51. UPDATE_REG_R1_PACKET = 0x11,
  52. UPDATE_REG_R2_PACKET = 0x12,
  53. UPDATE_REG_R3_PACKET = 0x13,
  54. UPDATE_REG_R4_PACKET = 0x14,
  55. UPDATE_REG_R5_PACKET = 0x15,
  56. UPDATE_REG_R6_PACKET = 0x16,
  57. UPDATE_REG_R7_PACKET = 0x17,
  58. UPDATE_REG_R8_PACKET = 0x18,
  59. UPDATE_REG_R9_PACKET = 0x19,
  60. UPDATE_REG_R10_PACKET = 0x1A,
  61. UPDATE_REG_R11_PACKET = 0x1B,
  62. UPDATE_REG_R12_PACKET = 0x1C,
  63. UPDATE_REG_R13_PACKET = 0x1D,
  64. UPDATE_REG_R14_PACKET = 0x1E,
  65. UPDATE_REG_R15_PACKET = 0x1F,
  66. UPDATE_ALL_REGS_PACKET = 0x20,
  67. SERVO_MOTOR = 0x21,
  68. };
  69. int callback_http (
  70. struct lws *wsi,
  71. enum lws_callback_reasons reason,
  72. void *user, void *in, size_t len);
  73. int callback_emu (
  74. struct lws *wsi,
  75. //enum lws_callback_reasons reason,
  76. enum lws_callback_reasons reason,
  77. void *user, void *in, size_t len);
  78. //static struct libwebsocket_protocols protocols[] = {
  79. static struct lws_protocols protocols [] = {
  80. /* first protocol must always be HTTP handler */
  81. {
  82. "http-only", // name
  83. callback_http, // callback
  84. 0 // per_session_data_size
  85. },
  86. {
  87. "emu-protocol",
  88. callback_emu,
  89. 0,
  90. 1024 * 4, // rx buffer size
  91. 0, // id
  92. 0 // user context data
  93. },
  94. {
  95. NULL, NULL, 0 /* End of list */
  96. }
  97. };
  98. struct Server {
  99. // Pending Packets Queue
  100. Packet *pending_packets_head;
  101. Packet *pending_packets_tail;
  102. uint32_t packets_queued;
  103. // Other
  104. bool spin_lock;
  105. };
  106. void print_console (Emulator *emu, const char *buf);
  107. void print_serial (Emulator *emu, char *buf);
  108. void send_control (Emulator *emu, uint8_t opcode,
  109. void *data, size_t size);
  110. void *web_server (void *ctxt);
  111. #endif