controls.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This software is released under the MIT License.
  5. * https://opensource.org/licenses/MIT
  6. *
  7. */
  8. #include "squeezelite.h"
  9. #include "config.h"
  10. #include "audio_controls.h"
  11. static log_level loglevel = lINFO;
  12. #define DOWN_OFS 0x10000
  13. #define UP_OFS 0x20000
  14. // numbers are simply 0..9 but are not used
  15. // arrow_right.down = 0001000e seems to be missing ...
  16. enum { BUTN_POWER_FRONT = 0X0A, BUTN_ARROW_UP, BUTN_ARROW_DOWN, BUTN_ARROW_LEFT, BUTN_KNOB_PUSH, BUTN_SEARCH,
  17. BUTN_REW, BUTN_FWD, BUTN_PLAY, BUTN_ADD, BUTN_BRIGHTNESS, BUTN_NOW_PLAYING,
  18. BUTN_PAUSE = 0X17, BUTN_BROWSE, BUTN_VOLUP_FRONT, BUTN_VOLDOWN_FRONT, BUTN_SIZE, BUTN_VISUAL, BUTN_VOLUMEMODE,
  19. BUTN_PRESET_1 = 0X23, BUTN_PRESET_2, BUTN_PRESET_3, BUTN_PRESET_4, BUTN_PRESET_5, BUTN_PRESET_6, BUTN_SNOOZE,
  20. BUTN_KNOB_LEFT = 0X5A, BUTN_KNOB_RIGHT };
  21. #define BUTN_ARROW_RIGHT BUTN_KNOB_PUSH
  22. #pragma pack(push, 1)
  23. struct BUTN_header {
  24. char opcode[4];
  25. u32_t length;
  26. u32_t jiffies;
  27. u32_t button;
  28. };
  29. struct IR_header {
  30. char opcode[4];
  31. u32_t length;
  32. u32_t jiffies;
  33. u8_t format; // unused
  34. u8_t bits; // unused
  35. u32_t code;
  36. };
  37. #pragma pack(pop)
  38. static in_addr_t server_ip;
  39. static u16_t server_hport;
  40. static u16_t server_cport;
  41. static int cli_sock = -1;
  42. static u8_t mac[6];
  43. static void (*chained_notify)(in_addr_t, u16_t, u16_t);
  44. static bool raw_mode;
  45. static void cli_send_cmd(char *cmd);
  46. /****************************************************************************************
  47. * Send BUTN
  48. */
  49. static void sendBUTN(int code, bool pressed) {
  50. struct BUTN_header pkt_header;
  51. memset(&pkt_header, 0, sizeof(pkt_header));
  52. memcpy(&pkt_header.opcode, "BUTN", 4);
  53. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  54. pkt_header.jiffies = htonl(gettime_ms());
  55. pkt_header.button = htonl(code + (pressed ? DOWN_OFS : UP_OFS));
  56. LOG_INFO("sending BUTN code %04x %s", code, pressed ? "down" : "up");
  57. LOCK_P;
  58. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  59. UNLOCK_P;
  60. }
  61. /****************************************************************************************
  62. * Send IR
  63. */
  64. static void sendIR(u16_t addr, u16_t cmd) {
  65. struct IR_header pkt_header;
  66. memset(&pkt_header, 0, sizeof(pkt_header));
  67. memcpy(&pkt_header.opcode, "IR ", 4);
  68. pkt_header.length = htonl(sizeof(pkt_header) - 8);
  69. pkt_header.jiffies = htonl(gettime_ms());
  70. pkt_header.format = pkt_header.bits = 0;
  71. pkt_header.code = htonl((addr << 16) | cmd);
  72. LOG_INFO("sending IR code %04x", (addr << 16) | cmd);
  73. LOCK_P;
  74. send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
  75. UNLOCK_P;
  76. }
  77. static void lms_volume_up(bool pressed) {
  78. if (raw_mode) {
  79. sendBUTN(BUTN_VOLUP_FRONT, pressed);
  80. } else {
  81. cli_send_cmd("button volup");
  82. }
  83. }
  84. static void lms_volume_down(bool pressed) {
  85. if (raw_mode) {
  86. sendBUTN(BUTN_VOLDOWN_FRONT, pressed);
  87. } else {
  88. cli_send_cmd("button voldown");
  89. }
  90. }
  91. static void lms_toggle(bool pressed) {
  92. if (raw_mode) {
  93. sendBUTN(BUTN_PAUSE, pressed);
  94. } else {
  95. cli_send_cmd("pause");
  96. }
  97. }
  98. static void lms_pause(bool pressed) {
  99. if (raw_mode) {
  100. sendBUTN(BUTN_PAUSE, pressed);
  101. } else {
  102. cli_send_cmd("pause 1");
  103. }
  104. }
  105. static void lms_play(bool pressed) {
  106. if (raw_mode) {
  107. sendBUTN(BUTN_PLAY, pressed);
  108. } else {
  109. cli_send_cmd("button play.single");
  110. }
  111. }
  112. static void lms_stop(bool pressed) {
  113. cli_send_cmd("button stop");
  114. }
  115. static void lms_rew(bool pressed) {
  116. if (raw_mode) {
  117. sendBUTN(BUTN_REW, pressed);
  118. } else {
  119. cli_send_cmd("button rew.repeat");
  120. }
  121. }
  122. static void lms_fwd(bool pressed) {
  123. if (raw_mode) {
  124. sendBUTN(BUTN_FWD, pressed);
  125. } else {
  126. cli_send_cmd("button fwd.repeat");
  127. }
  128. }
  129. static void lms_prev(bool pressed) {
  130. if (raw_mode) {
  131. sendBUTN(BUTN_REW, pressed);
  132. } else {
  133. cli_send_cmd("button rew");
  134. }
  135. }
  136. static void lms_next(bool pressed) {
  137. if (raw_mode) {
  138. sendBUTN(BUTN_FWD, pressed);
  139. } else {
  140. cli_send_cmd("button fwd");
  141. }
  142. }
  143. static void lms_up(bool pressed) {
  144. if (raw_mode) {
  145. sendBUTN(BUTN_ARROW_UP, pressed);
  146. } else {
  147. cli_send_cmd("button arrow_up");
  148. }
  149. }
  150. static void lms_down(bool pressed) {
  151. if (raw_mode) {
  152. sendBUTN(BUTN_ARROW_DOWN, pressed);
  153. } else {
  154. cli_send_cmd("button arrow_down");
  155. }
  156. }
  157. static void lms_left(bool pressed) {
  158. if (raw_mode) {
  159. sendBUTN(BUTN_ARROW_LEFT, pressed);
  160. } else {
  161. cli_send_cmd("button arrow_left");
  162. }
  163. }
  164. static void lms_right(bool pressed) {
  165. if (raw_mode) {
  166. sendBUTN(BUTN_ARROW_RIGHT, pressed);
  167. } else {
  168. cli_send_cmd("button arrow_right");
  169. }
  170. }
  171. static void lms_knob_left(bool pressed) {
  172. if (raw_mode) {
  173. sendBUTN(BUTN_KNOB_LEFT, pressed);
  174. } else {
  175. cli_send_cmd("button knob_left");
  176. }
  177. }
  178. static void lms_knob_right(bool pressed) {
  179. if (raw_mode) {
  180. sendBUTN(BUTN_KNOB_RIGHT, pressed);
  181. } else {
  182. cli_send_cmd("button knob_right");
  183. }
  184. }
  185. static void lms_knob_push(bool pressed) {
  186. if (raw_mode) {
  187. sendBUTN(BUTN_KNOB_PUSH, pressed);
  188. } else {
  189. cli_send_cmd("button knob_push");
  190. }
  191. }
  192. const actrls_t LMS_controls = {
  193. lms_volume_up, lms_volume_down, // volume up, volume down
  194. lms_toggle, lms_play, // toggle, play
  195. lms_pause, lms_stop, // pause, stop
  196. lms_rew, lms_fwd, // rew, fwd
  197. lms_prev, lms_next, // prev, next
  198. lms_up, lms_down,
  199. lms_left, lms_right,
  200. lms_knob_left, lms_knob_right, lms_knob_push,
  201. };
  202. /****************************************************************************************
  203. *
  204. */
  205. static void cli_send_cmd(char *cmd) {
  206. char packet[96];
  207. int len;
  208. len = sprintf(packet, "%02x:%02x:%02x:%02x:%02x:%02x %s\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], cmd);
  209. LOG_DEBUG("sending command %s at %s:%hu", packet, inet_ntoa(server_ip), server_cport);
  210. if (send(cli_sock, packet, len, MSG_DONTWAIT) < 0) {
  211. LOG_WARN("cannot send CLI %s", packet);
  212. }
  213. // need to empty the RX buffer otherwise we'll lock the TCP/IP stack
  214. len = recv(cli_sock, packet, 96, MSG_DONTWAIT);
  215. }
  216. /****************************************************************************************
  217. * Notification when server changes
  218. */
  219. static void notify(in_addr_t ip, u16_t hport, u16_t cport) {
  220. struct sockaddr_in addr;
  221. socklen_t addrlen = sizeof(addr);
  222. server_ip = ip;
  223. server_hport = hport;
  224. server_cport = cport;
  225. addr.sin_family = AF_INET;
  226. addr.sin_addr.s_addr = server_ip;
  227. addr.sin_port = htons(server_cport);
  228. // close existing CLI connection and open new one
  229. if (cli_sock >= 0) closesocket(cli_sock);
  230. cli_sock = socket(AF_INET, SOCK_STREAM, 0);
  231. if (connect(cli_sock, (struct sockaddr *) &addr, addrlen) < 0) {
  232. LOG_ERROR("unable to connect to server %s:%hu with cli", inet_ntoa(server_ip), server_cport);
  233. cli_sock = -1;
  234. }
  235. LOG_INFO("notified server %s hport %hu cport %hu", inet_ntoa(ip), hport, cport);
  236. if (chained_notify) (*chained_notify)(ip, hport, cport);
  237. }
  238. /****************************************************************************************
  239. * IR handler
  240. */
  241. static bool ir_handler(u16_t addr, u16_t cmd) {
  242. sendIR(addr, cmd);
  243. return true;
  244. }
  245. /****************************************************************************************
  246. * Initialize controls - shall be called once from output_init_embedded
  247. */
  248. void sb_controls_init(void) {
  249. char *p = config_alloc_get_default(NVS_TYPE_STR, "lms_ctrls_raw", "n", 0);
  250. raw_mode = p && (*p == '1' || *p == 'Y' || *p == 'y');
  251. free(p);
  252. LOG_INFO("initializing audio (buttons/rotary/ir) controls (raw:%u)", raw_mode);
  253. get_mac(mac);
  254. actrls_set_default(LMS_controls, raw_mode, NULL, ir_handler);
  255. chained_notify = server_notify;
  256. server_notify = notify;
  257. }