1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
32 #include "appevents.h"
42 #include "powermgmt.h"
45 #include "ipod_remote_tuner.h"
47 static volatile int iap_pollspeed
= 0;
48 static volatile bool iap_remotetick
= true;
49 static bool iap_setupflag
= false, iap_updateflag
= false;
50 static int iap_changedctr
= 0;
52 static unsigned long iap_remotebtn
= 0;
53 static int iap_repeatbtn
= 0;
54 static bool iap_btnrepeat
= false, iap_btnshuffle
= false;
56 unsigned char serbuf
[RX_BUFLEN
];
57 static int serbuf_i
= 0;
59 static unsigned char response
[TX_BUFLEN
];
60 static int responselen
;
62 static void iap_task(void)
66 count
+= iap_pollspeed
;
67 if (count
< (500/10)) return;
69 /* exec every 500ms if pollspeed == 1 */
71 queue_post(&button_queue
, SYS_IAP_PERIODIC
, 0);
74 void iap_setup(int ratenum
)
76 iap_bitrate_set(ratenum
);
78 iap_remotetick
= true;
79 iap_updateflag
= false;
82 iap_remotebtn
= BUTTON_NONE
;
83 tick_add_task(iap_task
);
84 add_event(PLAYBACK_EVENT_TRACK_CHANGE
, false, iap_track_changed
);
87 void iap_bitrate_set(int ratenum
)
98 serial_bitrate(19200);
101 serial_bitrate(38400);
104 serial_bitrate(57600);
115 parameters (0-n bytes)
116 checksum (length+mode+parameters+checksum == 0)
119 void iap_send_pkt(const unsigned char * data
, int len
)
123 if(len
> TX_BUFLEN
-4) len
= TX_BUFLEN
-4;
124 responselen
= len
+ 4;
129 chksum
= response
[2] = len
;
130 for(i
= 0; i
< len
; i
++)
133 response
[i
+3] = data
[i
];
136 response
[i
+3] = 0x100 - (chksum
& 0xFF);
138 for(i
= 0; i
< responselen
; i
++)
141 tx_writec(response
[i
]);
145 int iap_getc(unsigned char x
)
147 static unsigned char last_x
= 0;
148 static bool newpkt
= true;
149 static unsigned char chksum
= 0;
151 /* Restart if the sync word is seen */
152 if(x
== 0x55 && last_x
== 0xff/* && newpkt*/)
161 if(serbuf_i
>= RX_BUFLEN
)
164 serbuf
[serbuf_i
++] = x
;
169 /* Broadcast to queue if we have a complete message */
170 if(serbuf_i
&& (serbuf_i
== serbuf
[0]+2))
175 queue_post(&button_queue
, SYS_IAP_HANDLEPKT
, 0);
180 /* called by playback when the next track starts */
181 void iap_track_changed(void *ignored
)
187 void iap_periodic(void)
189 if(!iap_setupflag
) return;
190 if(!iap_pollspeed
) return;
192 unsigned char data
[] = {0x04, 0x00, 0x27, 0x04, 0x00, 0x00, 0x00, 0x00};
193 unsigned long time_elapsed
= audio_current_track()->elapsed
;
195 time_elapsed
+= wps_get_ff_rewind_count();
197 data
[3] = 0x04; /* playing */
199 /* If info has changed, don't flag it right away */
200 if(iap_changedctr
&& iap_changedctr
++ >= iap_pollspeed
* 2)
202 /* track info has changed */
204 data
[3] = 0x01; /* 0x02 has same effect? */
205 iap_updateflag
= true;
208 data
[4] = time_elapsed
>> 24;
209 data
[5] = time_elapsed
>> 16;
210 data
[6] = time_elapsed
>> 8;
211 data
[7] = time_elapsed
;
212 iap_send_pkt(data
, sizeof(data
));
215 void iap_set_remote_volume(void)
217 unsigned char data
[] = {0x03, 0x0D, 0x04, 0x00, 0x00};
218 data
[4] = (char)((global_settings
.volume
+58) * 4);
219 iap_send_pkt(data
, sizeof(data
));
222 void iap_handlepkt(void)
225 if(!iap_setupflag
) return;
226 if(serbuf
[0] == 0) return;
228 /* if we are waiting for a remote button to go out,
229 delay the handling of the new packet */
232 queue_post(&button_queue
, SYS_IAP_HANDLEPKT
, 0);
237 if (serbuf
[1] == 0x00)
243 /* ipod video send this */
244 unsigned char data
[] = {0x00, 0x25, 0x00, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00,0x01};
246 iap_send_pkt(data
, sizeof(data
));
252 /* ciphered authentication command */
253 /* Isn't used since we don't send the 0x00 0x17 command */
259 unsigned char data0
[] = {0x00, 0x16, 0x00};
260 iap_send_pkt(data0
, sizeof(data0
));
261 unsigned char data1
[] = {0x00, 0x27, 0x00};
262 iap_send_pkt(data1
, sizeof(data1
));
263 /* authentication ack, mandatory to enable some hardware */
264 unsigned char data2
[] = {0x00, 0x19, 0x00};
265 iap_send_pkt(data2
, sizeof(data2
));
266 if (radio_present
== 1)
268 /* get tuner capacities */
269 unsigned char data3
[] = {0x07, 0x01};
270 iap_send_pkt(data3
, sizeof(data3
));
272 iap_set_remote_volume();
278 unsigned char data
[] = {0x00, 0x02, 0x00, 0x13};
279 iap_send_pkt(data
, sizeof(data
));
281 if (serbuf
[6] == 0x35)
282 /* FM transmitter sends this: */
283 /* FF 55 0E 00 13 00 00 00 35 00 00 00 04 00 00 00 00 A6 (??)*/
285 unsigned char data2
[] = {0x00, 0x27, 0x00};
286 iap_send_pkt(data2
, sizeof(data2
));
287 unsigned char data3
[] = {0x05, 0x02};
288 iap_send_pkt(data3
, sizeof(data3
));
293 /* ipod fm remote sends this: */
294 /* FF 55 0E 00 13 00 00 00 8D 00 00 00 0E 00 00 00 03 41 */
295 if (serbuf
[6] |= 0x80)
297 unsigned char data4
[] = {0x00, 0x14};
298 iap_send_pkt(data4
, sizeof(data4
));
306 unsigned char data
[] = {0x00, 0x10, 0x00, 0x01, 0x05};
308 iap_send_pkt(data
, sizeof(data
));
315 /* ipod is supposed to work only with 5G and nano 2G */
316 /*{0x00, 0x0E, 0x00, 0x0B, 0x00, 0x05, 0x50, 0x41, 0x31, 0x34,
317 0x37, 0x4C, 0x4C, 0x00}; PA147LL (IPOD 5G 60 GO) */
318 unsigned char data
[] = {0x00, 0x0E, 0x00, 0x0B, 0x00, 0x10,
319 'R', 'O', 'C', 'K', 'B', 'O', 'X', 0x00};
320 iap_send_pkt(data
, sizeof(data
));
324 /* Ipod FM remote sends this: FF 55 02 00 09 F5 */
327 /* ipod5G firmware version */
328 unsigned char data
[] = {0x00, 0x0A, 0x01, 0x02, 0x01 };
329 iap_send_pkt(data
, sizeof(data
));
333 /* FM transmitter sends this: */
334 /* FF 55 02 00 05 F9 (mode switch: AiR mode) */
337 unsigned char data
[] = {0x00, 0x02, 0x06,
338 0x05, 0x00, 0x00, 0x0B, 0xB8, 0x28};
339 iap_send_pkt(data
, sizeof(data
));
340 unsigned char data2
[] = {0x00, 0x02, 0x00, 0x05};
341 iap_send_pkt(data2
, sizeof(data2
));
347 /* FM transmitter sends this: */
348 /* FF 55 06 00 01 05 00 02 01 F1 (mode switch) */
349 if(serbuf
[3] == 0x05)
352 unsigned char data
[] = {0x05, 0x02};
353 iap_send_pkt(data
, sizeof(data
));
355 /* FM remote sends this: */
356 /* FF 55 03 00 01 02 FA (1st thing sent) */
357 else if(serbuf
[3] == 0x02)
359 /* useful only for apple firmware */
364 /* default response is with cmd ok packet */
367 unsigned char data
[] = {0x00, 0x02, 0x00, 0x00};
368 data
[3] = serbuf
[2]; /* respond with cmd */
369 iap_send_pkt(data
, sizeof(data
));
375 else if (serbuf
[1] == 0x02)
377 if(serbuf
[2] != 0) return;
378 iap_remotebtn
= BUTTON_NONE
;
379 iap_remotetick
= false;
381 if(serbuf
[0] >= 3 && serbuf
[3] != 0)
384 iap_remotebtn
|= BUTTON_RC_PLAY
;
386 iap_remotebtn
|= BUTTON_RC_VOL_UP
;
388 iap_remotebtn
|= BUTTON_RC_VOL_DOWN
;
390 iap_remotebtn
|= BUTTON_RC_RIGHT
;
392 iap_remotebtn
|= BUTTON_RC_LEFT
;
394 else if(serbuf
[0] >= 4 && serbuf
[4] != 0)
396 if(serbuf
[4] & 1) /* play */
398 if (audio_status() != AUDIO_STATUS_PLAY
)
400 iap_remotebtn
|= BUTTON_RC_PLAY
;
402 iap_remotetick
= false;
406 if(serbuf
[4] & 2) /* pause */
408 if (audio_status() == AUDIO_STATUS_PLAY
)
410 iap_remotebtn
|= BUTTON_RC_PLAY
;
412 iap_remotetick
= false;
416 if((serbuf
[4] & 128) && !iap_btnshuffle
) /* shuffle */
418 iap_btnshuffle
= true;
419 if(!global_settings
.playlist_shuffle
)
421 global_settings
.playlist_shuffle
= 1;
423 settings_apply(false);
424 if (audio_status() & AUDIO_STATUS_PLAY
)
425 playlist_randomise(NULL
, current_tick
, true);
427 else if(global_settings
.playlist_shuffle
)
429 global_settings
.playlist_shuffle
= 0;
431 settings_apply(false);
432 if (audio_status() & AUDIO_STATUS_PLAY
)
433 playlist_sort(NULL
, true);
437 iap_btnshuffle
= false;
439 else if(serbuf
[0] >= 5 && serbuf
[5] != 0)
441 if((serbuf
[5] & 1) && !iap_btnrepeat
) /* repeat */
443 int oldmode
= global_settings
.repeat_mode
;
444 iap_btnrepeat
= true;
446 if (oldmode
== REPEAT_ONE
)
447 global_settings
.repeat_mode
= REPEAT_OFF
;
448 else if (oldmode
== REPEAT_ALL
)
449 global_settings
.repeat_mode
= REPEAT_ONE
;
450 else if (oldmode
== REPEAT_OFF
)
451 global_settings
.repeat_mode
= REPEAT_ALL
;
454 settings_apply(false);
455 if (audio_status() & AUDIO_STATUS_PLAY
)
456 audio_flush_and_reload_tracks();
459 iap_btnrepeat
= false;
461 if(serbuf
[5] & 16) /* ffwd */
463 iap_remotebtn
|= BUTTON_RC_RIGHT
;
465 if(serbuf
[5] & 32) /* frwd */
467 iap_remotebtn
|= BUTTON_RC_LEFT
;
472 else if (serbuf
[1] == 0x03)
476 /* some kind of status packet? */
479 unsigned char data
[] = {0x03, 0x02, 0x00, 0x00, 0x00, 0x00};
480 iap_send_pkt(data
, sizeof(data
));
487 unsigned char data
[] = {0x03, 0x00, 0x00, 0x08};
488 iap_send_pkt(data
, sizeof(data
));
494 /* request ipod volume */
495 if (serbuf
[3] == 0x04)
497 iap_set_remote_volume();
501 /* get volume from accessory */
503 if (serbuf
[3] == 0x04)
504 global_settings
.volume
= (-58)+((int)serbuf
[5]+1)/4;
505 sound_set_volume(global_settings
.volume
);
510 else if (serbuf
[1] == 0x04)
512 switch (((unsigned long)serbuf
[2] << 8) | serbuf
[3])
514 /* Get data updated??? flag */
517 unsigned char data
[] = {0x04, 0x00, 0x0A, 0x00};
518 data
[3] = iap_updateflag
? 0 : 1;
519 iap_send_pkt(data
, sizeof(data
));
522 /* Set data updated??? flag */
525 iap_updateflag
= serbuf
[4] ? 0 : 1;
526 /* respond with cmd ok packet */
527 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x0B};
528 iap_send_pkt(data
, sizeof(data
));
534 unsigned char data
[] = {0x04, 0x00, 0x13, 0x01, 0x0B};
535 iap_send_pkt(data
, sizeof(data
));
538 /* Get count of given types */
541 unsigned char data
[] = {0x04, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00};
542 unsigned long num
= 0;
543 switch(serbuf
[4]) /* type number */
545 case 0x01: /* total number of playlists */
548 case 0x05: /* total number of songs */
555 iap_send_pkt(data
, sizeof(data
));
558 /* Get time and status */
561 unsigned char data
[] = {0x04, 0x00, 0x1D, 0x00, 0x00, 0x00,
562 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
563 struct mp3entry
*id3
= audio_current_track();
564 unsigned long time_total
= id3
->length
;
565 unsigned long time_elapsed
= id3
->elapsed
;
566 int status
= audio_status();
567 data
[3] = time_total
>> 24;
568 data
[4] = time_total
>> 16;
569 data
[5] = time_total
>> 8;
570 data
[6] = time_total
;
571 data
[7] = time_elapsed
>> 24;
572 data
[8] = time_elapsed
>> 16;
573 data
[9] = time_elapsed
>> 8;
574 data
[10] = time_elapsed
;
575 if (status
== AUDIO_STATUS_PLAY
)
576 data
[11] = 0x01; /* play */
577 else if (status
& AUDIO_STATUS_PAUSE
)
578 data
[11] = 0x02; /* pause */
579 iap_send_pkt(data
, sizeof(data
));
582 /* Get current pos in playlist */
585 unsigned char data
[] = {0x04, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00};
586 long playlist_pos
= playlist_next(0);
587 playlist_pos
-= playlist_get_first_index(NULL
);
589 playlist_pos
+= playlist_amount();
590 data
[3] = playlist_pos
>> 24;
591 data
[4] = playlist_pos
>> 16;
592 data
[5] = playlist_pos
>> 8;
593 data
[6] = playlist_pos
;
594 iap_send_pkt(data
, sizeof(data
));
597 /* Get title of a song number */
599 /* Get artist of a song number */
601 /* Get album of a song number */
604 unsigned char data
[70] = {0x04, 0x00, 0xFF};
608 long tracknum
= (signed long)serbuf
[4] << 24 |
609 (signed long)serbuf
[5] << 16 |
610 (signed long)serbuf
[6] << 8 | serbuf
[7];
611 data
[2] = serbuf
[3] + 1;
612 memcpy(&id3
, audio_current_track(), sizeof(id3
));
613 tracknum
+= playlist_get_first_index(NULL
);
614 if(tracknum
>= playlist_amount())
615 tracknum
-= playlist_amount();
617 /* If the tracknumber is not the current one,
618 read id3 from disk */
619 if(playlist_next(0) != tracknum
)
621 struct playlist_track_info info
;
622 playlist_get_track_info(NULL
, tracknum
, &info
);
623 fd
= open(info
.filename
, O_RDONLY
);
624 memset(&id3
, 0, sizeof(struct mp3entry
));
625 get_metadata(&id3
, fd
, info
.filename
);
629 /* Return the requested track data */
633 len
= strlcpy((char *)&data
[3], id3
.title
, 64);
634 iap_send_pkt(data
, 4+len
);
637 len
= strlcpy((char *)&data
[3], id3
.artist
, 64);
638 iap_send_pkt(data
, 4+len
);
641 len
= strlcpy((char *)&data
[3], id3
.album
, 64);
642 iap_send_pkt(data
, 4+len
);
647 /* Set polling mode */
650 iap_pollspeed
= serbuf
[4] ? 1 : 0;
651 /*responsed with cmd ok packet */
652 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x26};
653 iap_send_pkt(data
, sizeof(data
));
656 /* AiR playback control */
659 /* respond with cmd ok packet */
660 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x29};
661 iap_send_pkt(data
, sizeof(data
));
664 case 0x01: /* play/pause */
665 iap_remotebtn
= BUTTON_RC_PLAY
;
667 iap_remotetick
= false;
670 case 0x02: /* stop */
671 iap_remotebtn
= BUTTON_RC_PLAY
|BUTTON_REPEAT
;
673 iap_remotetick
= false;
676 case 0x03: /* skip++ */
677 iap_remotebtn
= BUTTON_RC_RIGHT
;
679 iap_remotetick
= false;
681 case 0x04: /* skip-- */
682 iap_remotebtn
= BUTTON_RC_LEFT
;
684 iap_remotetick
= false;
686 case 0x05: /* ffwd */
687 iap_remotebtn
= BUTTON_RC_RIGHT
;
688 iap_remotetick
= false;
689 if(iap_pollspeed
) iap_pollspeed
= 5;
691 case 0x06: /* frwd */
692 iap_remotebtn
= BUTTON_RC_LEFT
;
693 iap_remotetick
= false;
694 if(iap_pollspeed
) iap_pollspeed
= 5;
696 case 0x07: /* end ffwd/frwd */
697 iap_remotebtn
= BUTTON_NONE
;
698 iap_remotetick
= false;
699 if(iap_pollspeed
) iap_pollspeed
= 1;
704 /* Get shuffle mode */
707 unsigned char data
[] = {0x04, 0x00, 0x2D, 0x00};
708 data
[3] = global_settings
.playlist_shuffle
? 1 : 0;
709 iap_send_pkt(data
, sizeof(data
));
712 /* Set shuffle mode */
715 if(serbuf
[4] && !global_settings
.playlist_shuffle
)
717 global_settings
.playlist_shuffle
= 1;
719 settings_apply(false);
720 if (audio_status() & AUDIO_STATUS_PLAY
)
721 playlist_randomise(NULL
, current_tick
, true);
723 else if(!serbuf
[4] && global_settings
.playlist_shuffle
)
725 global_settings
.playlist_shuffle
= 0;
727 settings_apply(false);
728 if (audio_status() & AUDIO_STATUS_PLAY
)
729 playlist_sort(NULL
, true);
733 /* respond with cmd ok packet */
734 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x2E};
735 iap_send_pkt(data
, sizeof(data
));
738 /* Get repeat mode */
741 unsigned char data
[] = {0x04, 0x00, 0x30, 0x00};
742 if(global_settings
.repeat_mode
== REPEAT_OFF
)
744 else if(global_settings
.repeat_mode
== REPEAT_ONE
)
748 iap_send_pkt(data
, sizeof(data
));
751 /* Set repeat mode */
754 int oldmode
= global_settings
.repeat_mode
;
756 global_settings
.repeat_mode
= REPEAT_OFF
;
757 else if (serbuf
[4] == 1)
758 global_settings
.repeat_mode
= REPEAT_ONE
;
759 else if (serbuf
[4] == 2)
760 global_settings
.repeat_mode
= REPEAT_ALL
;
762 if (oldmode
!= global_settings
.repeat_mode
)
765 settings_apply(false);
766 if (audio_status() & AUDIO_STATUS_PLAY
)
767 audio_flush_and_reload_tracks();
770 /* respond with cmd ok packet */
771 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x31};
772 iap_send_pkt(data
, sizeof(data
));
775 /* Get Max Screen Size for Picture Upload??? */
778 unsigned char data
[] = {0x04, 0x00, 0x34, 0x01, 0x36, 0x00, 0xA8, 0x01};
779 iap_send_pkt(data
, sizeof(data
));
782 /* Get number songs in current playlist */
785 unsigned char data
[] = {0x04, 0x00, 0x36, 0x00, 0x00, 0x00, 0x00};
786 unsigned long playlist_amt
= playlist_amount();
787 data
[3] = playlist_amt
>> 24;
788 data
[4] = playlist_amt
>> 16;
789 data
[5] = playlist_amt
>> 8;
790 data
[6] = playlist_amt
;
791 iap_send_pkt(data
, sizeof(data
));
794 /* Jump to track number in current playlist */
797 int paused
= (is_wps_fading() || (audio_status() & AUDIO_STATUS_PAUSE
));
798 long tracknum
= (signed long)serbuf
[4] << 24 |
799 (signed long)serbuf
[5] << 16 |
800 (signed long)serbuf
[6] << 8 | serbuf
[7];
802 audio_skip(tracknum
- playlist_next(0));
806 /* respond with cmd ok packet */
807 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x00};
810 iap_send_pkt(data
, sizeof(data
));
815 /* default response is with cmd ok packet */
816 unsigned char data
[] = {0x04, 0x00, 0x01, 0x00, 0x00, 0x00};
819 iap_send_pkt(data
, sizeof(data
));
825 else if (serbuf
[1] == 0x07)
829 /* tuner capabilities */
834 unsigned char data
[] = {0x00, 0x27, 0x00};
835 iap_send_pkt(data
, sizeof(data
));
838 /* actual tuner frequency */
841 /* tuner frequency from scan */
847 /* RDS station name 0x21 1E 00 + ASCII text*/
850 rmt_tuner_rds_data();
858 int remote_control_rx(void)
860 int btn
= iap_remotebtn
;
866 iap_remotebtn
= BUTTON_NONE
;
867 iap_remotetick
= true;
871 iap_remotetick
= true;