controls.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * (c) Philippe G. 2019, philippe_44@outlook.com
  3. *
  4. * This program 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. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include "squeezelite.h"
  19. #include "audio_controls.h"
  20. static log_level loglevel = lINFO;
  21. static in_addr_t server_ip;
  22. static u16_t server_hport;
  23. static u16_t server_cport;
  24. static u8_t mac[6];
  25. static void (*chained_notify)(in_addr_t, u16_t, u16_t);
  26. static void cli_send_cmd(char *cmd);
  27. static void lms_volume_up(void) {
  28. cli_send_cmd("button volup");
  29. }
  30. static void lms_volume_down(void) {
  31. cli_send_cmd("button voldown");
  32. }
  33. static void lms_toggle(void) {
  34. cli_send_cmd("pause");
  35. }
  36. static void lms_pause(void) {
  37. cli_send_cmd("pause 1");
  38. }
  39. static void lms_play(void) {
  40. cli_send_cmd("button play.single");
  41. }
  42. static void lms_stop(void) {
  43. cli_send_cmd("button stop");
  44. }
  45. static void lms_rew(void) {
  46. cli_send_cmd("button rew.repeat");
  47. }
  48. static void lms_fwd(void) {
  49. cli_send_cmd("button fwd.repeat");
  50. }
  51. static void lms_prev(void) {
  52. cli_send_cmd("button rew");
  53. }
  54. static void lms_next(void) {
  55. cli_send_cmd("button fwd");
  56. }
  57. static void lms_up(void) {
  58. cli_send_cmd("button arrow_up");
  59. }
  60. static void lms_down(void) {
  61. cli_send_cmd("button arrow_down");
  62. }
  63. static void lms_left(void) {
  64. cli_send_cmd("button arrow_left");
  65. }
  66. static void lms_right(void) {
  67. cli_send_cmd("button arrow_right");
  68. }
  69. static void lms_push(void) {
  70. cli_send_cmd("button knob_push");
  71. }
  72. const actrls_t LMS_controls = {
  73. lms_volume_up, lms_volume_down, // volume up, volume down
  74. lms_toggle, lms_play, // toggle, play
  75. lms_pause, lms_stop, // pause, stop
  76. lms_rew, lms_fwd, // rew, fwd
  77. lms_prev, lms_next, // prev, next
  78. lms_push,
  79. lms_up, lms_down,
  80. lms_left, lms_right,
  81. };
  82. /****************************************************************************************
  83. *
  84. */
  85. static void cli_send_cmd(char *cmd) {
  86. char packet[64];
  87. struct sockaddr_in addr;
  88. socklen_t addrlen = sizeof(addr);
  89. int len, sock = socket(AF_INET, SOCK_STREAM, 0);
  90. addr.sin_family = AF_INET;
  91. addr.sin_addr.s_addr = server_ip;
  92. addr.sin_port = htons(server_cport);
  93. if (connect(sock, (struct sockaddr *) &addr, addrlen) < 0) {
  94. LOG_ERROR("unable to connect to server %s:%hu with cli", inet_ntoa(server_ip), server_cport);
  95. return;
  96. }
  97. len = sprintf(packet, "%02x:%02x:%02x:%02x:%02x:%02x %s\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], cmd);
  98. LOG_DEBUG("sending command %s at %s:%hu", packet, inet_ntoa(server_ip), server_cport);
  99. if (send(sock, packet, len, 0) < 0) {
  100. LOG_WARN("cannot send CLI %s", packet);
  101. }
  102. closesocket(sock);
  103. }
  104. /****************************************************************************************
  105. * Notification when server changes
  106. */
  107. static void notify(in_addr_t ip, u16_t hport, u16_t cport) {
  108. server_ip = ip;
  109. server_hport = hport;
  110. server_cport = cport;
  111. LOG_INFO("notified server %s hport %hu cport %hu", inet_ntoa(ip), hport, cport);
  112. if (chained_notify) (*chained_notify)(ip, hport, cport);
  113. }
  114. /****************************************************************************************
  115. * Initialize controls - shall be called once from output_init_embedded
  116. */
  117. void sb_controls_init(void) {
  118. LOG_INFO("initializing CLI controls");
  119. get_mac(mac);
  120. actrls_set_default(LMS_controls, NULL);
  121. chained_notify = server_notify;
  122. server_notify = notify;
  123. }