summaryrefslogtreecommitdiffstats
path: root/package/fonera-mp3/src/lib/mp3_socket_parser.c
blob: 1702282e8ce0a33c73166d3245c6eaa366501e13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* FOXMP3 
* Copyright (c) 2006 acmesystems.it - john@acmesystems.it
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*
* Feedback, Bugs...  info@acmesystems.it
*
*/ 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "mp3.h"

#define TOKEN_MAX 		16
#define TOKEN_SIZE 		256

static unsigned char mp3_parser_tokens[TOKEN_MAX][TOKEN_SIZE];

int mp3_parser_tokenize(unsigned char *in){
	int i = 0;
	char *token = in;
	char *tmp;
	do {
		tmp = strstr(token, " ");
		if(tmp){
			*tmp = '\0';
			strcpy(mp3_parser_tokens[i], token);
			tmp++;
			token = tmp;				
		} else {
			strcpy(mp3_parser_tokens[i], token);
		};
		i++;
	}while((i < TOKEN_MAX) && (tmp));
	return i;
};

extern int state_current;
void mp3_parser_incoming(unsigned char *in, unsigned char *out){
	int c =	mp3_parser_tokenize(in);
	int ret = 0;
	int t1;
	if(c){
		printf("Parsing command from frontend app -> %s ---  %d tokens\n", in, c);
		if((!strcmp(mp3_parser_tokens[0], "PLAY")) && (c == 2)){
			state_event(MP3_EVENT_FILE, state_new_event(mp3_parser_tokens[1], 0));
			ret = 1;
		} else if((!strcmp(mp3_parser_tokens[0], "STREAM")) 
				&& (c == 3)){
			if(!strcmp(mp3_parser_tokens[1], "pls")){
				state_event(MP3_EVENT_STREAM, state_new_event(mp3_parser_tokens[2], STREAM_PLS));
				ret = 1;
			} else if(!strcmp(mp3_parser_tokens[1], "url")){
				state_event(MP3_EVENT_STREAM, state_new_event(mp3_parser_tokens[2], STREAM_URL));
				ret = 1;
			}	
		} else if((!strcmp(mp3_parser_tokens[0], "VOLUME")) 
				&& (c == 2)){
			t1 = atoi(mp3_parser_tokens[1]);
			state_generic_event(MP3_EVENT_GENERIC_VOLUME, t1, NULL);
			ret = 1;
		} else if((!strcmp(mp3_parser_tokens[0], "STOP")) 
				&& (c == 1)){
			state_event(MP3_EVENT_STOP, NULL);
			ret = 1;
		} else if((!strcmp(mp3_parser_tokens[0], "STATE"))
				&& (c == 1)){
			state_generic_event(MP3_EVENT_GENERIC_STATE, 0, out);
			return;
		} else if((!strcmp(mp3_parser_tokens[0], "BASS")) 
				&& (c == 2)){
			t1 = atoi(mp3_parser_tokens[1]);
			state_generic_event(MP3_EVENT_GENERIC_BASS, t1, NULL);
			ret = 1;
		} 	
		if(ret){
			sprintf(out, "OK\n");
			printf("Command parsed ok.\n");
		} else {
			
			sprintf(out, "ERROR\n");
			printf("Command parsed with error.\n");
		};
	} else {
		printf("Got command from frontend with 0 tokens.\n");
	};
};