mp3_socket_parser.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * FOXMP3
  3. * Copyright (c) 2006 acmesystems.it - [email protected]
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
  18. *
  19. * Feedback, Bugs... [email protected]
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "mp3.h"
  26. #define TOKEN_MAX 16
  27. #define TOKEN_SIZE 256
  28. static unsigned char mp3_parser_tokens[TOKEN_MAX][TOKEN_SIZE];
  29. int mp3_parser_tokenize(unsigned char *in){
  30. int i = 0;
  31. char *token = in;
  32. char *tmp;
  33. do {
  34. tmp = strstr(token, " ");
  35. if(tmp){
  36. *tmp = '\0';
  37. strcpy(mp3_parser_tokens[i], token);
  38. tmp++;
  39. token = tmp;
  40. } else {
  41. strcpy(mp3_parser_tokens[i], token);
  42. };
  43. i++;
  44. }while((i < TOKEN_MAX) && (tmp));
  45. return i;
  46. };
  47. extern int state_current;
  48. void mp3_parser_incoming(unsigned char *in, unsigned char *out){
  49. int c = mp3_parser_tokenize(in);
  50. int ret = 0;
  51. int t1;
  52. if(c){
  53. printf("Parsing command from frontend app -> %s --- %d tokens\n", in, c);
  54. if((!strcmp(mp3_parser_tokens[0], "PLAY")) && (c == 2)){
  55. state_event(MP3_EVENT_FILE, state_new_event(mp3_parser_tokens[1], 0));
  56. ret = 1;
  57. } else if((!strcmp(mp3_parser_tokens[0], "STREAM"))
  58. && (c == 3)){
  59. if(!strcmp(mp3_parser_tokens[1], "pls")){
  60. state_event(MP3_EVENT_STREAM, state_new_event(mp3_parser_tokens[2], STREAM_PLS));
  61. ret = 1;
  62. } else if(!strcmp(mp3_parser_tokens[1], "url")){
  63. state_event(MP3_EVENT_STREAM, state_new_event(mp3_parser_tokens[2], STREAM_URL));
  64. ret = 1;
  65. }
  66. } else if((!strcmp(mp3_parser_tokens[0], "VOLUME"))
  67. && (c == 2)){
  68. t1 = atoi(mp3_parser_tokens[1]);
  69. state_generic_event(MP3_EVENT_GENERIC_VOLUME, t1, NULL);
  70. ret = 1;
  71. } else if((!strcmp(mp3_parser_tokens[0], "STOP"))
  72. && (c == 1)){
  73. state_event(MP3_EVENT_STOP, NULL);
  74. ret = 1;
  75. } else if((!strcmp(mp3_parser_tokens[0], "STATE"))
  76. && (c == 1)){
  77. state_generic_event(MP3_EVENT_GENERIC_STATE, 0, out);
  78. return;
  79. } else if((!strcmp(mp3_parser_tokens[0], "BASS"))
  80. && (c == 2)){
  81. t1 = atoi(mp3_parser_tokens[1]);
  82. state_generic_event(MP3_EVENT_GENERIC_BASS, t1, NULL);
  83. ret = 1;
  84. }
  85. if(ret){
  86. sprintf(out, "OK\n");
  87. printf("Command parsed ok.\n");
  88. } else {
  89. sprintf(out, "ERROR\n");
  90. printf("Command parsed with error.\n");
  91. };
  92. } else {
  93. printf("Got command from frontend with 0 tokens.\n");
  94. };
  95. };