added libjson-c. added driver, webinterface and userspace daemon for the
[openwrt/svn-archive/archive.git] / package / fonera-mp3 / src / lib / mp3_states.c
1 /*
2 * FOXMP3
3 * Copyright (c) 2006 acmesystems.it - john@acmesystems.it
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... info@acmesystems.it
20 *
21 */
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/poll.h>
27 #include <signal.h>
28 #include "mp3.h"
29
30 typedef struct _MP3_STATE {
31 struct {
32 unsigned char volume;
33 unsigned char bass;
34 unsigned char treble;
35 } decoder;
36 struct {
37 unsigned char name[2048];
38 MP3_FILE_ID3 id3;
39 } file;
40 struct {
41 unsigned char url[2048];
42 unsigned char path[256];
43 unsigned int port;
44 } stream;
45 MP3_PLAYTIME playtime;
46 } MP3_STATE;
47
48 static MP3_STATE mp3_state;
49
50 void state_startup_enter(int state_last, int event, EVENT_PARAM *param){
51 mp3_state.decoder.volume = 0x30;
52 mp3_state.decoder.bass = 1;
53 mp3_state.file.name[0] = '\0';
54 mp3_state.stream.url[0] = '\0';
55 mp3_state.stream.path[0] = '\0';
56 mp3_state.stream.port = 0;
57 mp3_playtime_init(&mp3_state.playtime);
58 mp3_init();
59 mp3_nix_socket_setup();
60 mp3_tcp_socket_setup();
61 };
62
63 void state_idle_enter(int state_last, int event, EVENT_PARAM *param){
64 mp3_stop();
65 mp3_playtime_stop();
66 };
67
68 void state_file_startup_enter(int state_last, int event, EVENT_PARAM *param){
69 if(mp3_file_setup(param->text, &mp3_state.file.id3) == MP3_OK){
70 strcpy(mp3_state.file.name, param->text);
71 } else {
72 state_event(MP3_EVENT_ERROR, NULL);
73 };
74 };
75
76 void state_file_startup_leave(int state_new, int event){
77 if(state_new == MP3_STATE_FILE_HANDLE){
78 mp3_play();
79 mp3_playtime_start();
80 mp3_nix_socket_write("START FILE\n");
81 };
82 };
83
84 void state_file_handle_enter(int state_last, int event, EVENT_PARAM *param){
85 int ret = mp3_file_handle();
86 if(ret == MP3_ERROR){
87 state_event(MP3_EVENT_ERROR, NULL);
88 };
89 if(ret == MP3_END){
90 state_event(MP3_EVENT_END, NULL);
91 };
92
93 };
94
95 void state_file_handle_leave(int state_new, int event){
96 if(state_new != MP3_STATE_FILE_HANDLE){
97 mp3_playtime_stop();
98 mp3_file_cleanup();
99 mp3_nix_socket_write("STOP FILE\n");
100 };
101 };
102
103 void state_stream_startup_enter(int state_last, int event, EVENT_PARAM *param){
104 if(mp3_stream_setup(param->text, param->numeric, mp3_state.stream.url,
105 mp3_state.stream.path, &mp3_state.stream.port) != MP3_OK){
106 state_event(MP3_EVENT_ERROR, NULL);
107 };
108 };
109
110 void state_stream_startup_leave(int state_new, int event){
111 if(state_new == MP3_STATE_STREAM_HANDLE){
112 mp3_play();
113 mp3_playtime_start();
114 mp3_nix_socket_write("START STREAM\n");
115 };
116 };
117
118 void state_stream_handle_enter(int state_last, int event, EVENT_PARAM *param){
119 if(mp3_stream_handle() == MP3_ERROR){
120 state_event(MP3_EVENT_ERROR, NULL);
121 }
122 };
123
124 void state_stream_handle_leave(int state_new, int event){
125 if(state_new != MP3_STATE_STREAM_HANDLE){
126 mp3_stream_cleanup();
127 mp3_playtime_stop();
128 mp3_nix_socket_write("STOP STREAM\n");
129 }
130 };
131
132 extern STATE states[MAX_STATE_COUNT];
133 void state_error_enter(int state_last, int event, EVENT_PARAM *param){
134 if(param){
135 printf("Error in state %s -> %s\n", states[state_last].name, param->text);
136 mp3_nix_socket_write("ERROR Error in state %s -> %s\n", states[state_last].name, param->text);
137 } else {
138 printf("Unknown error in state %s\n", states[state_last].name);
139 };
140 };
141
142 void state_shutdown_enter(int state_last, int event, EVENT_PARAM *param){
143 printf("Entering state SHUTDOWN ...\n");
144 printf("Shutting down player ...\n");
145 mp3_nix_socket_cleanup();
146 mp3_tcp_socket_cleanup();
147 printf("Quitting statemachine ...\n");
148 exit(0);
149 };
150 extern int state_current;
151 void state_generic_event(unsigned int event, unsigned char in_int,
152 unsigned char *out_uchar){
153 switch(event){
154 case MP3_EVENT_GENERIC_VOLUME:
155 mp3_state.decoder.volume = in_int;
156 mp3_set_volume(mp3_state.decoder.volume,
157 mp3_state.decoder.volume);
158 mp3_nix_socket_write("VOLUME %d\n", mp3_state.decoder.volume);
159 break;
160 case MP3_EVENT_GENERIC_BASS:
161 mp3_state.decoder.bass = in_int;
162 mp3_bass(8, mp3_state.decoder.treble,
163 8, mp3_state.decoder.bass);
164 mp3_nix_socket_write("BASS %d\n", mp3_state.decoder.bass);
165 break;
166 case MP3_EVENT_GENERIC_STATE:
167 out_uchar[0] = '\0';
168 sprintf(out_uchar, "%sVOLUME %d\n",
169 out_uchar, mp3_state.decoder.volume);
170 sprintf(out_uchar, "%sBASS %d\n",
171 out_uchar, mp3_state.decoder.bass);
172 sprintf(out_uchar, "%sTREBLE %d\n",
173 out_uchar, mp3_state.decoder.treble);
174 sprintf(out_uchar, "%sPLAYTIME %d\n",
175 out_uchar,
176 (mp3_state.playtime.playtime_mins * 60) + mp3_state.playtime.playtime_secs);
177 switch(state_current){
178 case MP3_STATE_IDLE:
179 sprintf(out_uchar, "%sSTATE MP3_STATE_IDLE\n", out_uchar);
180 break;
181 case MP3_STATE_FILE_START:
182 case MP3_STATE_FILE_HANDLE:
183 sprintf(out_uchar, "%sFILE %s\n", out_uchar,
184 mp3_state.file.name);
185 if(strlen(mp3_state.file.id3.artist)){
186 sprintf(out_uchar, "%sID3_ARTIST %s\n",
187 out_uchar,
188 mp3_state.file.id3.artist);
189 };
190 if(strlen(mp3_state.file.id3.album)){
191 sprintf(out_uchar, "%sID3_ALBUM %s\n",
192 out_uchar,
193 mp3_state.file.id3.album);
194 };
195 if(strlen(mp3_state.file.id3.album)){
196 sprintf(out_uchar, "%sID3_TRACK %s\n",
197 out_uchar,
198 mp3_state.file.id3.track);
199 };
200 sprintf(out_uchar, "%sSTATE MP3_STATE_FILE\n", out_uchar);
201 break;
202 case MP3_STATE_STREAM_START:
203 case MP3_STATE_STREAM_HANDLE:
204 sprintf(out_uchar, "%sSTREAM %s:%d%s\n", out_uchar,
205 mp3_state.stream.url,
206 mp3_state.stream.port,
207 mp3_state.stream.path);
208 sprintf(out_uchar, "%sSTATE MP3_STATE_STREAM\n", out_uchar);
209 break;
210 default:
211 sprintf(out_uchar, "STATE Unkonwn\n");
212 break;
213 };
214 sprintf(out_uchar, "%sOK\n", out_uchar);
215 break;
216 case MP3_EVENT_GENERIC_PLAYTIME:
217 // printf("%02d:%02d\n", mp3_state.playtime.playtime_mins,
218 // mp3_state.playtime.playtime_secs);
219 break;
220 default:
221 break;
222 };
223 };
224