A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 489 lines 15 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (c) 2011 by Bertrik Sikken 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 2 15 * of the License, or (at your option) any later version. 16 * 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 18 * KIND, either express or implied. 19 * 20 ****************************************************************************/ 21#include <stdbool.h> 22#include <stdint.h> 23#include <string.h> 24#include <system.h> 25#include <kernel.h> 26#include "rds.h" 27#include "time.h" 28#include "timefuncs.h" 29#include "settings.h" 30#include "string-extra.h" 31 32#define TIMED_OUT(tick) \ 33 TIME_AFTER(current_tick, (tick)) 34#define SET_TIMEOUT(tick, duration) \ 35 ({ (tick) = current_tick + (duration); }) 36 37/* Driver keeps strings in native character format, translating on demand */ 38static char ps_copy[9]; /* copy of final message */ 39static long ps_copy_tmo; /* timeout to discard programme service name */ 40static char rt_copy[65]; /* copy of final message */ 41static long rt_copy_tmo; /* time to discard radio text */ 42static uint16_t pi_code; /* current programme identifier code */ 43static time_t ct_data; /* date/time (not robust; not essential) */ 44 45/* timeout before text times out */ 46#define TEXT_TIMEOUT (30 * HZ) 47 48/* timeout before RDS is considered idle and is reset */ 49#define RDS_TIMEOUT (10 * HZ) 50static long rds_timeout; /* timeout until rds is thought idle */ 51static bool rds_active; /* if active, timeouts are monitored */ 52 53#if (CONFIG_RDS & RDS_CFG_PROCESS) 54/* timeout before group segment obsolescence */ 55#define GROUP0_TIMEOUT (2 * HZ) 56#define GROUP2_TIMEOUT (10 * HZ) 57 58/* programme identification (not robust; not really used anyway) */ 59static uint16_t pi_last; /* previously read code */ 60 61/* programme service name */ 62static char ps_data[2][9]; /* round-robin driver work queue */ 63static int ps_segment; /* next expected segment */ 64static long ps_timeout; /* timeout to receive full group */ 65static int ps_data_idx; /* ps_data[0 or 1] */ 66#define PS_DATA_INC(x) ps_data[ps_data_idx ^= (x)] 67 68/* radio text */ 69static char rt_data[2][65]; /* round-robin driver work queue */ 70static int rt_segment; /* next expected segment */ 71static long rt_timeout; /* timeout to receive full group */ 72static int rt_abflag; /* message change flag */ 73static int rt_data_idx; /* rt_data[0 or 1] */ 74#define RT_DATA_INC(x) rt_data[rt_data_idx ^= (x)] 75#endif /* (CONFIG_RDS & RDS_CFG_PROCESS) */ 76 77/* RDS code table G0 to UTF-8 translation */ 78static const uint16_t rds_tbl_g0[0x100-0x20] = 79{ 80 /* codes 0x00 .. 0x1F are omitted because they are identities and not 81 * actually spec'ed as part of the character maps anyway */ 82 /* 0 1 2 3 4 5 6 7 */ 83 0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027, /* 20 */ 84 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, /* 28 */ 85 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, /* 30 */ 86 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, /* 38 */ 87 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, /* 40 */ 88 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, /* 48 */ 89 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, /* 50 */ 90 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x2015, 0x005F, /* 58 */ 91 0x2016, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, /* 60 */ 92 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, /* 68 */ 93 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, /* 70 */ 94 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x203E, 0x0020, /* 78 */ 95 0x00E1, 0x00E0, 0x00E9, 0x00E8, 0x00ED, 0x00EC, 0x00F3, 0x00F2, /* 80 */ 96 0x00FA, 0x00F9, 0x00D1, 0x00C7, 0x015E, 0x00DF, 0x00A1, 0x0132, /* 88 */ 97 0x00E2, 0x00E4, 0x00EA, 0x00EB, 0x00EE, 0x00EF, 0x00F4, 0x00F6, /* 90 */ 98 0x00FB, 0x00FC, 0x00F1, 0x00E7, 0x015F, 0x01E7, 0x0131, 0x0133, /* 98 */ 99 0x00AA, 0x03B1, 0x00A9, 0x2030, 0x01E6, 0x011B, 0x0148, 0x0151, /* A0 */ 100 0x03C0, 0x20A0, 0x00A3, 0x0024, 0x2190, 0x2191, 0x2192, 0x2193, /* A8 */ 101 0x00BA, 0x00B9, 0x00B2, 0x00B3, 0x00B1, 0x0130, 0x0144, 0x0171, /* B0 */ 102 0x00B5, 0x00BF, 0x00F7, 0x00B0, 0x00BC, 0x00BD, 0x00BE, 0x00A7, /* B8 */ 103 0x00C1, 0x00C0, 0x00C9, 0x00C8, 0x00CD, 0x00CC, 0x00D3, 0x00D2, /* C0 */ 104 0x00DA, 0x00D9, 0x0158, 0x010C, 0x0160, 0x017D, 0x0110, 0x013F, /* C8 */ 105 0x00C2, 0x00C4, 0x00CA, 0x00CB, 0x00CE, 0x00CF, 0x00D4, 0x00D6, /* D0 */ 106 0x00DB, 0x00DC, 0x0159, 0x010D, 0x0161, 0x017E, 0x0111, 0x0140, /* D8 */ 107 0x00C3, 0x00C5, 0x00C6, 0x0152, 0x0177, 0x00DD, 0x00D5, 0x00D8, /* E0 */ 108 0x00DE, 0x014A, 0x0154, 0x0106, 0x015A, 0x0179, 0x0166, 0x00F0, /* E8 */ 109 0x00E3, 0x00E5, 0x00E6, 0x0153, 0x0175, 0x00FD, 0x00F5, 0x00F8, /* F0 */ 110 0x00FE, 0x014B, 0x0155, 0x0107, 0x015B, 0x017A, 0x0167, 0x0020, /* F8 */ 111}; 112 113/* could add tables G1 and G2 without much trouble */ 114 115/* write one UTF-8 character; returns original 'dst' if insufficient space */ 116static char * convert_rds_char(char *dst, unsigned int c, size_t dstsize) 117{ 118 unsigned int u = c >= 0x20 ? (rds_tbl_g0 - 0x20)[c] : c; 119 120 if (LIKELY(u <= 0x7F)) { 121 /* U+0000 .. U+007F -> 0xxx xxxx */ 122 if (dstsize > 1) { 123 *dst++ = u; 124 } 125 } 126 else if (u <= 0x7FF) { 127 /* U+0080 .. U+07FF -> 110x xxxx 10 xx xxxx */ 128 if (dstsize > 2) { 129 *dst++ = 0xC0 | (u >> 6); 130 *dst++ = 0x80 | (u & 0x3F); 131 } 132 } 133 else /* if (u <= 0xFFFF) */ { 134 /* U+0800 .. U+FFFF -> 1110 xxxx 10xx xxxx 10xx xxxx */ 135 if (dstsize > 3) { 136 *dst++ = 0xE0 | (u >> 12); 137 *dst++ = 0x80 | ((u >> 6) & 0x3F); 138 *dst++ = 0x80 | (u & 0x3F); 139 } 140 } 141#if 0 /* No four-byte characters are used right now */ 142 else { 143 /* U+10000 .. U+10FFFF -> 11110xxx 10xx xxxx 10xx xxxx 10xx xxxx */ 144 if (dstsize > 4) { 145 *dst++ = 0xF0 | (c >> 18); 146 *dst++ = 0x80 | ((c >> 12) & 0x3F); 147 *dst++ = 0x80 | ((c >> 6) & 0x3F); 148 *dst++ = 0x80 | (c & 0x3F); 149 } 150 } 151#endif /* 0 */ 152 return dst; 153} 154 155/* Copy RDS character string with conversion to UTF-8 156 * Acts like strlcpy but won't split multibyte characters */ 157static size_t copy_rds_string(char *dst, const char *src, size_t dstsize) 158{ 159 char *p = dst; 160 unsigned int c; 161 162 while ((c = (unsigned char)*src++)) { 163 char *q = p; 164 165 p = convert_rds_char(q, c, dstsize); 166 if (p == q) { 167 dst -= dstsize; 168 break; 169 } 170 171 dstsize -= p - q; 172 } 173 174 if (dstsize > 0) { 175 *p = '\0'; 176 } 177 178 return p - dst; 179} 180 181/* indicate recent processing activity */ 182static void register_activity(void) 183{ 184 SET_TIMEOUT(rds_timeout, RDS_TIMEOUT); 185 rds_active = true; 186} 187 188/* resets the rds parser */ 189void rds_reset(void) 190{ 191 /* reset general info */ 192 pi_code = 0; 193 ct_data = 0; 194 ps_copy[0] = '\0'; 195 rt_copy[0] = '\0'; 196 rds_active = false; 197 198#if (CONFIG_RDS & RDS_CFG_PROCESS) 199 /* reset driver info */ 200 pi_last = 0; 201 ps_segment = 0; 202 rt_segment = 0; 203#endif /* (CONFIG_RDS & RDS_CFG_PROCESS) */ 204} 205 206/* initialises the rds parser */ 207void rds_init(void) 208{ 209 rds_reset(); 210} 211 212/* sync RDS state */ 213void rds_sync(void) 214{ 215 if (rds_active) { 216 if (TIMED_OUT(rds_timeout)) { 217 rds_reset(); 218 } 219 else { 220 if (TIMED_OUT(ps_copy_tmo)) { 221 ps_copy[0] = '\0'; 222 } 223 if (TIMED_OUT(rt_copy_tmo)) { 224 rt_copy[0] = '\0'; 225 } 226 } 227 } 228} 229 230static void rds_set_time(time_t time) 231{ 232 ct_data = time; 233#ifdef CONFIG_RTC 234 if (ct_data && global_settings.sync_rds_time) { 235 struct tm *tm = gmtime(&ct_data); 236 237 set_time(tm); 238 } 239#endif 240} 241 242#if (CONFIG_RDS & RDS_CFG_PROCESS) 243/* handles a group 0 packet, returns true if a new message was received */ 244static void handle_group0(const uint16_t data[4]) 245{ 246 int segment, pos; 247 char *ps; 248 249 segment = data[1] & 3; 250 251 if (segment == 0) { 252 ps_segment = 0; 253 } 254 else if (segment != ps_segment || TIMED_OUT(ps_timeout)) { 255 ps_segment = 0; 256 return; 257 } 258 259 /* store data */ 260 pos = segment * 2; 261 ps = PS_DATA_INC(0); 262 ps[pos + 0] = (data[3] >> 8) & 0xFF; 263 ps[pos + 1] = (data[3] >> 0) & 0xFF; 264 265 if (++ps_segment < 4) { 266 /* don't have all segments yet */ 267 SET_TIMEOUT(ps_timeout, GROUP0_TIMEOUT); 268 return; 269 } 270 271 ps[8] = '\0'; 272 273 /* two messages in a row must be the same */ 274 if (memcmp(ps, PS_DATA_INC(1), 8) == 0) { 275 memcpy(ps_copy, ps, 9); 276 SET_TIMEOUT(ps_copy_tmo, TEXT_TIMEOUT); 277 } 278} 279 280/* handles a radio text characters, returns true if end-of-line found */ 281static bool handle_rt(int *pos_p, char c) 282{ 283 char *rt = RT_DATA_INC(0); 284 285 switch (c) { 286 case 0x0D: /* end of line */ 287 return true; 288 case 0x0A: /* optional line break */ 289 case 0x0B: /* end of headline */ 290 c = ' '; 291 default: /* regular character */ 292 rt[(*pos_p)++] = c; 293 case 0x00 ... 0x09: /* unprintable */ 294 case 0x0C: 295 case 0x0E ... 0x1E: 296 case 0x1F: /* soft hyphen */ 297 return false; 298 } 299} 300 301/* handles a group 2 packet, returns true if a new message was received */ 302static void handle_group2(const uint16_t data[4]) 303{ 304 int abflag, segment, version, pos; 305 char *rt; 306 bool done = false; 307 308 /* reset parsing if the message type changed */ 309 abflag = (data[1] >> 4) & 1; 310 segment = data[1] & 0xF; 311 version = (data[1] >> 11) & 1; 312 313 if (abflag != rt_abflag || segment == 0) { 314 rt_abflag = abflag; 315 rt_segment = 0; 316 } 317 else if (segment != rt_segment || TIMED_OUT(rt_timeout)) { 318 rt_segment = 0; 319 return; 320 } 321 322 /* store data */ 323 if (version == 0) { 324 pos = segment * 4; 325 done = done || handle_rt(&pos, (data[2] >> 8) & 0xFF); 326 done = done || handle_rt(&pos, (data[2] >> 0) & 0xFF); 327 done = done || handle_rt(&pos, (data[3] >> 8) & 0xFF); 328 done = done || handle_rt(&pos, (data[3] >> 0) & 0xFF); 329 } else { 330 pos = segment * 2; 331 done = done || handle_rt(&pos, (data[3] >> 8) & 0xFF); 332 done = done || handle_rt(&pos, (data[3] >> 0) & 0xFF); 333 } 334 335 /* there are two cases for completion: 336 * - we got all 16 segments 337 * - we found an end of line */ 338 if (++rt_segment < 16 && !done) { 339 SET_TIMEOUT(rt_timeout, GROUP2_TIMEOUT); 340 return; 341 } 342 343 rt = RT_DATA_INC(0); 344 rt[pos++] = '\0'; 345 346 /* two messages in a row must be the same */ 347 if (memcmp(rt, RT_DATA_INC(1), pos) == 0) { 348 memcpy(rt_copy, rt, pos); 349 SET_TIMEOUT(rt_copy_tmo, TEXT_TIMEOUT); 350 } 351} 352 353/* handles a group 4a packet (clock-time) */ 354static void handle_group4a(const uint16_t data[4]) 355{ 356 int daycode = ((data[1] << 15) & 0x18000) | 357 ((data[2] >> 1) & 0x07FFF); 358 int hour = ((data[2] << 4) & 0x10) | 359 ((data[3] >> 12) & 0x0F); 360 int minute = ((data[3] >> 6) & 0x3F); 361 int offset_sig = (data[3] >> 5) & 1; 362 int offset_abs = data[3] & 0x1F; 363 364 if (daycode < 55927) { 365 /* invalid date, before 2012-01-01 */ 366 return; 367 } 368 if ((hour >= 24) || (minute >= 60)) { 369 /* invalid time */ 370 return; 371 } 372 if (offset_abs > 24) { 373 /* invalid local time offset */ 374 return; 375 } 376 377 /* convert modified julian day + time to UTC */ 378 time_t seconds = daycode - 40587; 379 if (seconds < 24854) { 380 seconds *= 86400; 381 seconds += hour * 3600; 382 seconds += minute * 60; 383 seconds += ((offset_sig == 0) ? offset_abs : -offset_abs) * 1800; 384 rds_set_time(seconds); 385 } 386} 387 388/* processes one rds packet */ 389void rds_process(const uint16_t data[4]) 390{ 391 int group; 392 393 /* process programme identification (PI) code */ 394 uint16_t pi = data[0]; 395 if (pi == pi_last) { 396 pi_code = pi; 397 } 398 pi_last = pi; 399 400 /* handle rds data based on group */ 401 group = (data[1] >> 11) & 0x1F; 402 switch (group) { 403 404 case 0: /* group 0A: basic info */ 405 case 1: /* group 0B: basic info */ 406 handle_group0(data); 407 break; 408 409 case 4: /* group 2A: radio text */ 410 case 5: /* group 2B: radio text */ 411 handle_group2(data); 412 break; 413 414 case 8: /* group 4A: clock-time */ 415 handle_group4a(data); 416 break; 417 } 418 419 register_activity(); 420} 421#endif /* (CONFIG_RDS & RDS_CFG_PROCESS) */ 422 423#if (CONFIG_RDS & RDS_CFG_PUSH) 424/* pushes preprocesed RDS information */ 425void rds_push_info(enum rds_info_id info_id, uintptr_t data, size_t size) 426{ 427 switch (info_id) { 428#if 0 429 case RDS_INFO_CODETABLE: 430 /* nothing doing for now */ 431 break; 432#endif 433 case RDS_INFO_PI: 434 pi_code = (uint16_t)data; 435 break; 436 case RDS_INFO_PS: 437 strmemcpy(ps_copy, (const char *)data, MIN(size, sizeof (ps_copy)-1)); 438 SET_TIMEOUT(ps_copy_tmo, TEXT_TIMEOUT); 439 break; 440 case RDS_INFO_RT: 441 strmemcpy(rt_copy, (const char *)data, MIN(size, sizeof (rt_copy)-1)); 442 SET_TIMEOUT(rt_copy_tmo, TEXT_TIMEOUT); 443 break; 444 case RDS_INFO_CT: 445 rds_set_time((time_t)data); 446 break; 447 448 default:; 449 } 450 451 register_activity(); 452} 453#endif /* (CONFIG_RDS & RDS_CFG_PUSH) */ 454 455/* read fully-processed RDS data */ 456size_t rds_pull_info(enum rds_info_id info_id, uintptr_t data, size_t size) 457{ 458 rds_sync(); 459 460 switch (info_id) { 461#if 0 462 case RDS_INFO_CODETABLE: 463 /* nothing doing for now */ 464 break; 465#endif 466 case RDS_INFO_PI: 467 if (size >= sizeof (uint16_t)) { 468 *(uint16_t *)data = pi_code; 469 } 470 size = sizeof (uint16_t); 471 break; 472 case RDS_INFO_PS: 473 size = copy_rds_string((char *)data, ps_copy, size); 474 break; 475 case RDS_INFO_RT: 476 size = copy_rds_string((char *)data, rt_copy, size); 477 break; 478 case RDS_INFO_CT: 479 if (size >= sizeof (time_t)) { 480 *(time_t *)data = ct_data; 481 } 482 size = sizeof (time_t); 483 break; 484 485 default: 486 size = 0; 487 } 488 return size; 489}