Fixed the Boomshine plugin not to crash when there are no more levels to play
[kugel-rb.git] / apps / iap.c
blob29cee22ab5abda99d960782435298b9447c327f7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr & Nick Robinson
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <memory.h>
23 #include <string.h>
25 #include "iap.h"
26 #include "button.h"
27 #include "config.h"
28 #include "cpu.h"
29 #include "system.h"
30 #include "kernel.h"
31 #include "serial.h"
32 #include "appevents.h"
34 #include "playlist.h"
35 #include "playback.h"
36 #include "audio.h"
37 #include "settings.h"
38 #include "metadata.h"
39 #include "wps.h"
41 #include "action.h"
43 #define RX_BUFLEN 260
44 #define TX_BUFLEN 128
46 static volatile int iap_pollspeed = 0;
47 static volatile bool iap_remotetick = true;
48 static bool iap_setupflag = false, iap_updateflag = false;
49 static int iap_changedctr = 0;
51 static unsigned long iap_remotebtn = 0;
52 static int iap_repeatbtn = 0;
53 static bool iap_btnrepeat = false, iap_btnshuffle = false;
55 unsigned char serbuf[RX_BUFLEN];
56 static int serbuf_i = 0;
58 static unsigned char response[TX_BUFLEN];
59 static int responselen;
61 static void iap_task(void)
63 static int count = 0;
65 count += iap_pollspeed;
66 if (count < (500/10)) return;
68 /* exec every 500ms if pollspeed == 1 */
69 count = 0;
70 queue_post(&button_queue, SYS_IAP_PERIODIC, 0);
73 void iap_setup(int ratenum)
75 iap_bitrate_set(ratenum);
76 iap_pollspeed = 0;
77 iap_remotetick = true;
78 iap_updateflag = false;
79 iap_changedctr = 0;
80 iap_setupflag = true;
81 iap_remotebtn = BUTTON_NONE;
82 tick_add_task(iap_task);
83 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, iap_track_changed);
86 void iap_bitrate_set(int ratenum)
88 switch(ratenum)
90 case 0:
91 serial_bitrate(0);
92 break;
93 case 1:
94 serial_bitrate(9600);
95 break;
96 case 2:
97 serial_bitrate(19200);
98 break;
99 case 3:
100 serial_bitrate(38400);
101 break;
102 case 4:
103 serial_bitrate(57600);
104 break;
108 /* Message format:
109 0xff
110 0x55
111 length
112 mode
113 command (2 bytes)
114 parameters (0-n bytes)
115 checksum (length+mode+parameters+checksum == 0)
118 static void iap_send_pkt(const unsigned char * data, int len)
120 int i, chksum;
122 if(len > TX_BUFLEN-4) len = TX_BUFLEN-4;
123 responselen = len + 4;
125 response[0] = 0xFF;
126 response[1] = 0x55;
128 chksum = response[2] = len;
129 for(i = 0; i < len; i ++)
131 chksum += data[i];
132 response[i+3] = data[i];
135 response[i+3] = 0x100 - (chksum & 0xFF);
137 for(i = 0; i < responselen; i ++)
139 while (!tx_rdy()) ;
140 tx_writec(response[i]);
144 int iap_getc(unsigned char x)
146 static unsigned char last_x = 0;
147 static bool newpkt = true;
148 static unsigned char chksum = 0;
150 /* Restart if the sync word is seen */
151 if(x == 0x55 && last_x == 0xff/* && newpkt*/)
153 serbuf[0] = 0;
154 serbuf_i = 0;
155 chksum = 0;
156 newpkt = false;
158 else
160 if(serbuf_i >= RX_BUFLEN)
161 serbuf_i = 0;
163 serbuf[serbuf_i++] = x;
164 chksum += x;
166 last_x = x;
168 /* Broadcast to queue if we have a complete message */
169 if(serbuf_i && (serbuf_i == serbuf[0]+2))
171 serbuf_i = 0;
172 newpkt = true;
173 if(chksum == 0)
174 queue_post(&button_queue, SYS_IAP_HANDLEPKT, 0);
176 return newpkt;
179 /* called by playback when the next track starts */
180 void iap_track_changed(void *ignored)
182 (void)ignored;
183 iap_changedctr = 1;
186 void iap_periodic(void)
188 if(!iap_setupflag) return;
189 if(!iap_pollspeed) return;
191 unsigned char data[] = {0x04, 0x00, 0x27, 0x04, 0x00, 0x00, 0x00, 0x00};
192 unsigned long time_elapsed = audio_current_track()->elapsed;
194 time_elapsed += wps_get_ff_rewind_count();
196 data[3] = 0x04; // playing
198 /* If info has changed, don't flag it right away */
199 if(iap_changedctr && iap_changedctr++ >= iap_pollspeed * 2)
201 /* track info has changed */
202 iap_changedctr = 0;
203 data[3] = 0x01; // 0x02 has same effect?
204 iap_updateflag = true;
207 data[4] = time_elapsed >> 24;
208 data[5] = time_elapsed >> 16;
209 data[6] = time_elapsed >> 8;
210 data[7] = time_elapsed;
211 iap_send_pkt(data, sizeof(data));
214 void iap_handlepkt(void)
217 if(!iap_setupflag) return;
218 if(serbuf[0] == 0) return;
220 /* if we are waiting for a remote button to go out,
221 delay the handling of the new packet */
222 if(!iap_remotetick)
224 queue_post(&button_queue, SYS_IAP_HANDLEPKT, 0);
225 return;
228 /* Handle Mode 0 */
229 if (serbuf[1] == 0x00)
231 switch (serbuf[2])
233 /* get model info */
234 case 0x0D:
236 unsigned char data[] = {0x00, 0x0E, 0x00, 0x0B, 0x00, 0x10,
237 'R', 'O', 'C', 'K', 'B', 'O', 'X', 0x00};
238 iap_send_pkt(data, sizeof(data));
239 break;
241 /* No idea ??? */
242 case 0x0F:
244 unsigned char data[] = {0x00, 0x10, 0x00, 0x01, 0x05};
245 iap_send_pkt(data, sizeof(data));
246 break;
248 /* FM transmitter sends this: FF 55 06 00 01 05 00 02 01 F1 (mode switch) */
249 case 0x01:
251 if(serbuf[3] == 0x05)
253 sleep(HZ/3);
254 unsigned char data[] = {0x05, 0x02};
255 iap_send_pkt(data, sizeof(data));
257 break;
259 /* FM transmitter sends this: FF 55 0E 00 13 00 00 00 35 00 00 00 04 00 00 00 00 A6 (???)*/
260 case 0x13:
262 unsigned char data[] = {0x00, 0x02, 0x00, 0x13};
263 iap_send_pkt(data, sizeof(data));
264 unsigned char data2[] = {0x00, 0x27, 0x00};
265 iap_send_pkt(data2, sizeof(data2));
266 unsigned char data3[] = {0x05, 0x02};
267 iap_send_pkt(data3, sizeof(data3));
268 break;
270 /* FM transmitter sends this: FF 55 02 00 05 F9 (mode switch: AiR mode) */
271 case 0x05:
273 unsigned char data[] = {0x00, 0x02, 0x06, 0x05, 0x00, 0x00, 0x0B, 0xB8, 0x28};
274 iap_send_pkt(data, sizeof(data));
275 unsigned char data2[] = {0x00, 0x02, 0x00, 0x05};
276 iap_send_pkt(data2, sizeof(data2));
277 break;
279 /* default response is with cmd ok packet */
280 default:
282 unsigned char data[] = {0x00, 0x02, 0x00, 0x00};
283 data[3] = serbuf[2]; //respond with cmd
284 iap_send_pkt(data, sizeof(data));
285 break;
289 /* Handle Mode 2 */
290 else if (serbuf[1] == 0x02)
292 if(serbuf[2] != 0) return;
293 iap_remotebtn = BUTTON_NONE;
294 iap_remotetick = false;
296 if(serbuf[0] >= 3 && serbuf[3] != 0)
298 if(serbuf[3] & 1)
299 iap_remotebtn |= BUTTON_RC_PLAY;
300 if(serbuf[3] & 2)
301 iap_remotebtn |= BUTTON_RC_VOL_UP;
302 if(serbuf[3] & 4)
303 iap_remotebtn |= BUTTON_RC_VOL_DOWN;
304 if(serbuf[3] & 8)
305 iap_remotebtn |= BUTTON_RC_RIGHT;
306 if(serbuf[3] & 16)
307 iap_remotebtn |= BUTTON_RC_LEFT;
309 else if(serbuf[0] >= 4 && serbuf[4] != 0)
311 if(serbuf[4] & 1) /* play */
313 if (audio_status() != AUDIO_STATUS_PLAY)
315 iap_remotebtn |= BUTTON_RC_PLAY;
316 iap_repeatbtn = 2;
317 iap_remotetick = false;
318 iap_changedctr = 1;
321 if(serbuf[4] & 2) /* pause */
323 if (audio_status() == AUDIO_STATUS_PLAY)
325 iap_remotebtn |= BUTTON_RC_PLAY;
326 iap_repeatbtn = 2;
327 iap_remotetick = false;
328 iap_changedctr = 1;
331 if((serbuf[4] & 128) && !iap_btnshuffle) /* shuffle */
333 iap_btnshuffle = true;
334 if(!global_settings.playlist_shuffle)
336 global_settings.playlist_shuffle = 1;
337 settings_save();
338 settings_apply(false);
339 if (audio_status() & AUDIO_STATUS_PLAY)
340 playlist_randomise(NULL, current_tick, true);
342 else if(global_settings.playlist_shuffle)
344 global_settings.playlist_shuffle = 0;
345 settings_save();
346 settings_apply(false);
347 if (audio_status() & AUDIO_STATUS_PLAY)
348 playlist_sort(NULL, true);
351 else
352 iap_btnshuffle = false;
354 else if(serbuf[0] >= 5 && serbuf[5] != 0)
356 if((serbuf[5] & 1) && !iap_btnrepeat) /* repeat */
358 int oldmode = global_settings.repeat_mode;
359 iap_btnrepeat = true;
361 if (oldmode == REPEAT_ONE)
362 global_settings.repeat_mode = REPEAT_OFF;
363 else if (oldmode == REPEAT_ALL)
364 global_settings.repeat_mode = REPEAT_ONE;
365 else if (oldmode == REPEAT_OFF)
366 global_settings.repeat_mode = REPEAT_ALL;
368 settings_save();
369 settings_apply(false);
370 if (audio_status() & AUDIO_STATUS_PLAY)
371 audio_flush_and_reload_tracks();
373 else
374 iap_btnrepeat = false;
376 if(serbuf[5] & 16) /* ffwd */
378 iap_remotebtn |= BUTTON_RC_RIGHT;
380 if(serbuf[5] & 32) /* frwd */
382 iap_remotebtn |= BUTTON_RC_LEFT;
386 /* Handle Mode 3 */
387 else if (serbuf[1] == 0x03)
389 switch(serbuf[2])
391 /* some kind of status packet? */
392 case 0x01:
394 unsigned char data[] = {0x03, 0x02, 0x00, 0x00, 0x00, 0x00};
395 iap_send_pkt(data, sizeof(data));
396 break;
400 /* Handle Mode 4 */
401 else if (serbuf[1] == 0x04)
403 switch (((unsigned long)serbuf[2] << 8) | serbuf[3])
405 /* Get data updated??? flag */
406 case 0x0009:
408 unsigned char data[] = {0x04, 0x00, 0x0A, 0x00};
409 data[3] = iap_updateflag ? 0 : 1;
410 iap_send_pkt(data, sizeof(data));
411 break;
413 /* Set data updated??? flag */
414 case 0x000B:
416 iap_updateflag = serbuf[4] ? 0 : 1;
417 /* respond with cmd ok packet */
418 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x0B};
419 iap_send_pkt(data, sizeof(data));
420 break;
422 /* Get iPod size? */
423 case 0x0012:
425 unsigned char data[] = {0x04, 0x00, 0x13, 0x01, 0x0B};
426 iap_send_pkt(data, sizeof(data));
427 break;
429 /* Get count of given types */
430 case 0x0018:
432 unsigned char data[] = {0x04, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00};
433 unsigned long num = 0;
434 switch(serbuf[4]) /* type number */
436 case 0x01: /* total number of playlists */
437 num = 1;
438 break;
439 case 0x05: /* total number of songs */
440 num = 1;
442 data[3] = num >> 24;
443 data[4] = num >> 16;
444 data[5] = num >> 8;
445 data[6] = num;
446 iap_send_pkt(data, sizeof(data));
447 break;
449 /* Get time and status */
450 case 0x001C:
452 unsigned char data[] = {0x04, 0x00, 0x1D, 0x00, 0x00, 0x00,
453 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
454 struct mp3entry *id3 = audio_current_track();
455 unsigned long time_total = id3->length;
456 unsigned long time_elapsed = id3->elapsed;
457 int status = audio_status();
458 data[3] = time_total >> 24;
459 data[4] = time_total >> 16;
460 data[5] = time_total >> 8;
461 data[6] = time_total;
462 data[7] = time_elapsed >> 24;
463 data[8] = time_elapsed >> 16;
464 data[9] = time_elapsed >> 8;
465 data[10] = time_elapsed;
466 if (status == AUDIO_STATUS_PLAY)
467 data[11] = 0x01; /* play */
468 else if (status & AUDIO_STATUS_PAUSE)
469 data[11] = 0x02; /* pause */
470 iap_send_pkt(data, sizeof(data));
471 break;
473 /* Get current pos in playlist */
474 case 0x001E:
476 unsigned char data[] = {0x04, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00};
477 long playlist_pos = playlist_next(0);
478 playlist_pos -= playlist_get_first_index(NULL);
479 if(playlist_pos < 0)
480 playlist_pos += playlist_amount();
481 data[3] = playlist_pos >> 24;
482 data[4] = playlist_pos >> 16;
483 data[5] = playlist_pos >> 8;
484 data[6] = playlist_pos;
485 iap_send_pkt(data, sizeof(data));
486 break;
488 /* Get title of a song number */
489 case 0x0020:
490 /* Get artist of a song number */
491 case 0x0022:
492 /* Get album of a song number */
493 case 0x0024:
495 unsigned char data[70] = {0x04, 0x00, 0xFF};
496 struct mp3entry id3;
497 int fd;
498 size_t len;
499 long tracknum = (signed long)serbuf[4] << 24 |
500 (signed long)serbuf[5] << 16 |
501 (signed long)serbuf[6] << 8 | serbuf[7];
502 data[2] = serbuf[3] + 1;
503 memcpy(&id3, audio_current_track(), sizeof(id3));
504 tracknum += playlist_get_first_index(NULL);
505 if(tracknum >= playlist_amount())
506 tracknum -= playlist_amount();
508 /* If the tracknumber is not the current one,
509 read id3 from disk */
510 if(playlist_next(0) != tracknum)
512 struct playlist_track_info info;
513 playlist_get_track_info(NULL, tracknum, &info);
514 fd = open(info.filename, O_RDONLY);
515 memset(&id3, 0, sizeof(struct mp3entry));
516 get_metadata(&id3, fd, info.filename);
517 close(fd);
520 /* Return the requested track data */
521 switch(serbuf[3])
523 case 0x20:
524 len = strlcpy((char *)&data[3], id3.title, 64);
525 iap_send_pkt(data, 4+len);
526 break;
527 case 0x22:
528 len = strlcpy((char *)&data[3], id3.artist, 64);
529 iap_send_pkt(data, 4+len);
530 break;
531 case 0x24:
532 len = strlcpy((char *)&data[3], id3.album, 64);
533 iap_send_pkt(data, 4+len);
534 break;
536 break;
538 /* Set polling mode */
539 case 0x0026:
541 iap_pollspeed = serbuf[4] ? 1 : 0;
542 /*responsed with cmd ok packet */
543 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x26};
544 iap_send_pkt(data, sizeof(data));
545 break;
547 /* AiR playback control */
548 case 0x0029:
550 /* respond with cmd ok packet */
551 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x29};
552 iap_send_pkt(data, sizeof(data));
553 switch(serbuf[4])
555 case 0x01: /* play/pause */
556 iap_remotebtn = BUTTON_RC_PLAY;
557 iap_repeatbtn = 2;
558 iap_remotetick = false;
559 iap_changedctr = 1;
560 break;
561 case 0x02: /* stop */
562 iap_remotebtn = BUTTON_RC_PLAY|BUTTON_REPEAT;
563 iap_repeatbtn = 2;
564 iap_remotetick = false;
565 iap_changedctr = 1;
566 break;
567 case 0x03: /* skip++ */
568 iap_remotebtn = BUTTON_RC_RIGHT;
569 iap_repeatbtn = 2;
570 iap_remotetick = false;
571 break;
572 case 0x04: /* skip-- */
573 iap_remotebtn = BUTTON_RC_LEFT;
574 iap_repeatbtn = 2;
575 iap_remotetick = false;
576 break;
577 case 0x05: /* ffwd */
578 iap_remotebtn = BUTTON_RC_RIGHT;
579 iap_remotetick = false;
580 if(iap_pollspeed) iap_pollspeed = 5;
581 break;
582 case 0x06: /* frwd */
583 iap_remotebtn = BUTTON_RC_LEFT;
584 iap_remotetick = false;
585 if(iap_pollspeed) iap_pollspeed = 5;
586 break;
587 case 0x07: /* end ffwd/frwd */
588 iap_remotebtn = BUTTON_NONE;
589 iap_remotetick = false;
590 if(iap_pollspeed) iap_pollspeed = 1;
591 break;
593 break;
595 /* Get shuffle mode */
596 case 0x002C:
598 unsigned char data[] = {0x04, 0x00, 0x2D, 0x00};
599 data[3] = global_settings.playlist_shuffle ? 1 : 0;
600 iap_send_pkt(data, sizeof(data));
601 break;
603 /* Set shuffle mode */
604 case 0x002E:
606 if(serbuf[4] && !global_settings.playlist_shuffle)
608 global_settings.playlist_shuffle = 1;
609 settings_save();
610 settings_apply(false);
611 if (audio_status() & AUDIO_STATUS_PLAY)
612 playlist_randomise(NULL, current_tick, true);
614 else if(!serbuf[4] && global_settings.playlist_shuffle)
616 global_settings.playlist_shuffle = 0;
617 settings_save();
618 settings_apply(false);
619 if (audio_status() & AUDIO_STATUS_PLAY)
620 playlist_sort(NULL, true);
624 /* respond with cmd ok packet */
625 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x2E};
626 iap_send_pkt(data, sizeof(data));
627 break;
629 /* Get repeat mode */
630 case 0x002F:
632 unsigned char data[] = {0x04, 0x00, 0x30, 0x00};
633 if(global_settings.repeat_mode == REPEAT_OFF)
634 data[3] = 0;
635 else if(global_settings.repeat_mode == REPEAT_ONE)
636 data[3] = 1;
637 else
638 data[3] = 2;
639 iap_send_pkt(data, sizeof(data));
640 break;
642 /* Set repeat mode */
643 case 0x0031:
645 int oldmode = global_settings.repeat_mode;
646 if (serbuf[4] == 0)
647 global_settings.repeat_mode = REPEAT_OFF;
648 else if (serbuf[4] == 1)
649 global_settings.repeat_mode = REPEAT_ONE;
650 else if (serbuf[4] == 2)
651 global_settings.repeat_mode = REPEAT_ALL;
653 if (oldmode != global_settings.repeat_mode)
655 settings_save();
656 settings_apply(false);
657 if (audio_status() & AUDIO_STATUS_PLAY)
658 audio_flush_and_reload_tracks();
661 /* respond with cmd ok packet */
662 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x31};
663 iap_send_pkt(data, sizeof(data));
664 break;
666 /* Get Max Screen Size for Picture Upload??? */
667 case 0x0033:
669 unsigned char data[] = {0x04, 0x00, 0x34, 0x01, 0x36, 0x00, 0xA8, 0x01};
670 iap_send_pkt(data, sizeof(data));
671 break;
673 /* Get number songs in current playlist */
674 case 0x0035:
676 unsigned char data[] = {0x04, 0x00, 0x36, 0x00, 0x00, 0x00, 0x00};
677 unsigned long playlist_amt = playlist_amount();
678 data[3] = playlist_amt >> 24;
679 data[4] = playlist_amt >> 16;
680 data[5] = playlist_amt >> 8;
681 data[6] = playlist_amt;
682 iap_send_pkt(data, sizeof(data));
683 break;
685 /* Jump to track number in current playlist */
686 case 0x0037:
688 int paused = (is_wps_fading() || (audio_status() & AUDIO_STATUS_PAUSE));
689 long tracknum = (signed long)serbuf[4] << 24 |
690 (signed long)serbuf[5] << 16 |
691 (signed long)serbuf[6] << 8 | serbuf[7];
692 audio_pause();
693 audio_skip(tracknum - playlist_next(0));
694 if (!paused)
695 audio_resume();
697 /* respond with cmd ok packet */
698 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x00};
699 data[4] = serbuf[2];
700 data[5] = serbuf[3];
701 iap_send_pkt(data, sizeof(data));
702 break;
704 default:
706 /* default response is with cmd ok packet */
707 unsigned char data[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x00};
708 data[4] = serbuf[2];
709 data[5] = serbuf[3];
710 iap_send_pkt(data, sizeof(data));
711 break;
715 serbuf[0] = 0;
718 int remote_control_rx(void)
720 int btn = iap_remotebtn;
721 if(iap_repeatbtn)
723 iap_repeatbtn--;
724 if(!iap_repeatbtn)
726 iap_remotebtn = BUTTON_NONE;
727 iap_remotetick = true;
730 else
731 iap_remotetick = true;
733 return btn;