controls.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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("pause 0");
  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 actrls_t controls = {
  58. lms_volume_up, lms_volume_down, // volume up, volume down
  59. lms_toggle, lms_play, // toggle, play
  60. lms_pause, lms_stop, // pause, stop
  61. lms_rew, lms_fwd, // rew, fwd
  62. lms_prev, lms_next, // prev, next
  63. };
  64. /****************************************************************************************
  65. *
  66. */
  67. static void cli_send_cmd(char *cmd) {
  68. char packet[64];
  69. struct sockaddr_in addr;
  70. socklen_t addrlen = sizeof(addr);
  71. int len, sock = socket(AF_INET, SOCK_STREAM, 0);
  72. addr.sin_family = AF_INET;
  73. addr.sin_addr.s_addr = server_ip;
  74. addr.sin_port = htons(server_cport);
  75. if (connect(sock, (struct sockaddr *) &addr, addrlen) < 0) {
  76. LOG_ERROR("unable to connect to server %s:%hu with cli", inet_ntoa(server_ip), server_cport);
  77. return;
  78. }
  79. len = sprintf(packet, "%02x:%02x:%02x:%02x:%02x:%02x %s\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], cmd);
  80. LOG_DEBUG("sending command %s at %s:%hu", packet, inet_ntoa(server_ip), server_cport);
  81. if (send(sock, packet, len, 0) < 0) {
  82. LOG_WARN("cannot send CLI %s", packet);
  83. }
  84. close(sock);
  85. }
  86. /****************************************************************************************
  87. * Notification when server changes
  88. */
  89. static void notify(in_addr_t ip, u16_t hport, u16_t cport) {
  90. server_ip = ip;
  91. server_hport = hport;
  92. server_cport = cport;
  93. LOG_INFO("notified server %s hport %hu cport %hu", inet_ntoa(ip), hport, cport);
  94. if (chained_notify) (*chained_notify)(ip, hport, cport);
  95. }
  96. /****************************************************************************************
  97. * Initialize controls - shall be called once from output_init_embedded
  98. */
  99. void cli_controls_init(void) {
  100. get_mac(mac);
  101. actrls_set_default(controls);
  102. chained_notify = server_notify;
  103. server_notify = notify;
  104. }