@@ -304,54 +304,131 @@ static void theme() {
304
304
}
305
305
306
306
// treat the received message of the websocket
307
- void websockethandle (int socket , wsopcode_t opcode , uint8_t * payload , size_t length )
308
- {
307
+ void websockethandle (int socket , wsopcode_t opcode , uint8_t * payload , size_t length ) {
309
308
//wsvol
310
309
ESP_LOGV (TAG ,"websocketHandle: %s" ,payload );
311
310
char value [6 ] = "" ;
312
- if (strstr ((char * )payload ,"wsvol=" )!= NULL )
313
- {
311
+ if (strstr ((char * )payload ,"wsvol=" )!= NULL ) {
314
312
char answer [17 ];
315
313
if (strstr ((char * )payload ,"&" ) != NULL )
316
314
* strstr ((char * )payload ,"&" )= 0 ;
317
315
else return ;
318
316
// setVolume(payload+6);
319
317
sprintf (answer ,"{\"wsvol\":\"%s\"}" ,payload + 6 );
320
318
websocketlimitedbroadcast (socket ,answer , strlen (answer ));
321
- }
322
- else if (getSParameterFromResponse (value , 6 , "getStation=" , (char * )payload , length )) {
319
+ } else if (getSParameterFromResponse (value , 6 , "getStationsFrom=" , (char * )payload , length )) {
323
320
int uid = atoi (value );
324
321
if (uid >=0 && uid < 255 ) {
325
- char * buf ;
322
+ int max_length = 1024 ;
323
+ char * output ;
324
+ output = inmalloc (max_length );
325
+ if (output == NULL ) {
326
+ ESP_LOGE (TAG ," %s malloc fails (%d bytes)" ,"getStation" , max_length );
327
+ } else {
328
+ /*
329
+ struct shoutcast_info {
330
+ char domain[73]; //url
331
+ char file[116]; //path
332
+ char name[64];
333
+ int8_t ovol; // offset volume
334
+ uint16_t port; //port
335
+ };
336
+ * */
337
+ int json_length = 335 ; // 73 + 116 + 64 + 8 + 16 + 68 - 12 + 2
338
+ char * buf ;
339
+ buf = inmalloc (json_length );
340
+ if (buf == NULL ) {
341
+ ESP_LOGE (TAG ," %s malloc fails (%d bytes)" , "getStation" , json_length );
342
+ } else {
343
+ strcpy (output , "{\"stations\":[" );
344
+ bool not_first = false;
345
+ bool isFull = false;
346
+ for (int uid_cc = uid , sta_max = 255 ; uid_cc < sta_max ; uid_cc ++ ) {
347
+ struct shoutcast_info * si = getStation (uid_cc ); // getStation uses malloc() !!!!
348
+ if (si != NULL && strlen (si -> domain ) > 0 ) {
349
+ if (strlen (si -> domain ) > sizeof (si -> domain )) si -> domain [sizeof (si -> domain )- 1 ] = 0 ; //truncate if any (rom crash)
350
+ if (strlen (si -> file ) > sizeof (si -> file )) si -> file [sizeof (si -> file )- 1 ] = 0 ; //truncate if any (rom crash)
351
+ if (strlen (si -> name ) > sizeof (si -> name )) si -> name [sizeof (si -> name )- 1 ] = 0 ; //truncate if any (rom crash)
352
+ // jsonGSTAT: {"station":%u,"Name":"%s","URL":"%s","File":"%s","Port":%u,"ovol":%u};
353
+ sprintf (buf , "{\"station\":%u,\"Name\":\"%s\",\"URL\":\"%s\",\"File\":\"%s\"" ,
354
+ uid_cc ,
355
+ si -> name ,
356
+ si -> domain ,
357
+ si -> file
358
+ );
359
+ char uval [12 ];
360
+ if (si -> port != 80 && si -> port != 0 ) {
361
+ sprintf (uval , ",\"Port\":%u" , si -> port );
362
+ strcat (buf , uval );
363
+ }
364
+ if (si -> ovol > 0 ) {
365
+ sprintf (uval , ",\"ovol\":%u" , si -> ovol );
366
+ strcat (buf , uval );
367
+ }
368
+ strcat (buf , "}" );
369
+
370
+ isFull = (strlen (output ) + strlen (buf ) >= max_length - 10 );
371
+ if (!isFull ) {
372
+ if (not_first ) {
373
+ strcat (output , "," );
374
+ }
375
+ strcat (output , buf );
376
+ not_first = true;
377
+ }
378
+ }
379
+ infree (si );
380
+ if (isFull ) { break ; }
381
+ }
382
+
383
+ infree (buf );
384
+
385
+ strcat (output , "]}" );
386
+ ESP_LOGV (TAG ,"getStation Buf len:%d : %s" , strlen (output ), output );
387
+ websocketwrite (socket , output , strlen (output ));
388
+ }
389
+ infree (output );
390
+ }
391
+ }
392
+ return ;
393
+ } else if (getSParameterFromResponse (value , 6 , "getStation=" , (char * )payload , length )) {
394
+ int uid = atoi (value );
395
+ if (uid >=0 && uid < 255 ) {
396
+ char noStation = "{\"Name\":\"\",\"URL\":\"\",\"File\":\"\",\"Port\":0,\"ovol\":0}" ;
326
397
struct shoutcast_info * si = getStation (uid );
327
- if (strlen (si -> domain ) > sizeof (si -> domain )) si -> domain [sizeof (si -> domain )- 1 ] = 0 ; //truncate if any (rom crash)
328
- if (strlen (si -> file ) > sizeof (si -> file )) si -> file [sizeof (si -> file )- 1 ] = 0 ; //truncate if any (rom crash)
329
- if (strlen (si -> name ) > sizeof (si -> name )) si -> name [sizeof (si -> name )- 1 ] = 0 ; //truncate if any (rom crash)
330
- // jsonGSTAT: {"Name":"%s","URL":"%s","File":"%s","Port":%u,"ovol":%u}
331
- int json_length = - 2 * 5 + 8 + 1 + strlen (jsonGSTAT ) + strlen (si -> domain ) + strlen (si -> file ) + strlen (si -> name );
332
- buf = inmalloc (json_length );
333
- if (buf == NULL ) {
334
- ESP_LOGE (TAG ," %s malloc fails (%d bytes)" ,"getStation" , json_length );
398
+ if (si == NULL ) {
399
+ // A response must always sent for getting the next station
400
+ websocketwrite (socket , noStation , strlen (noStation ));
335
401
} else {
336
- for (int i = 0 ; i < sizeof (buf ); i ++ ) buf [i ] = 0 ;
337
- sprintf (buf , jsonGSTAT ,
338
- uid ,
339
- si -> name ,
340
- si -> domain ,
341
- si -> file ,
342
- si -> port ,
343
- si -> ovol
344
- );
345
- ESP_LOGV (TAG ,"getStation Buf len:%d : %s" ,strlen (buf ),buf );
346
- websocketwrite (socket , buf , strlen (buf ));
402
+ if (strlen (si -> domain ) > sizeof (si -> domain )) si -> domain [sizeof (si -> domain )- 1 ] = 0 ; //truncate if any (rom crash)
403
+ if (strlen (si -> file ) > sizeof (si -> file )) si -> file [sizeof (si -> file )- 1 ] = 0 ; //truncate if any (rom crash)
404
+ if (strlen (si -> name ) > sizeof (si -> name )) si -> name [sizeof (si -> name )- 1 ] = 0 ; //truncate if any (rom crash)
405
+ // jsonGSTAT: {"Name":"%s","URL":"%s","File":"%s","Port":%u,"ovol":%u}
406
+ int json_length = - 2 * 5 + 8 + 1 + strlen (jsonGSTAT ) + strlen (si -> domain ) + strlen (si -> file ) + strlen (si -> name );
407
+ char * buf ;
408
+ buf = inmalloc (json_length );
409
+ if (buf == NULL ) {
410
+ ESP_LOGE (TAG ," %s malloc fails (%d bytes)" ,"getStation" , json_length );
411
+ websocketwrite (socket , noStation , strlen (noStation ));
412
+ } else {
413
+ // for(int i = 0; i<sizeof(buf); i++) buf[i] = 0;
414
+ sprintf (buf , jsonGSTAT ,
415
+ uid ,
416
+ si -> name ,
417
+ si -> domain ,
418
+ si -> file ,
419
+ si -> port ,
420
+ si -> ovol
421
+ );
422
+ ESP_LOGV (TAG ,"getStation Buf len:%d : %s" ,strlen (buf ),buf );
423
+ // A response must always sent for getting the next station
424
+ websocketwrite (socket , buf , strlen (buf ));
425
+ infree (buf );
426
+ }
427
+ infree (si );
347
428
}
348
- infree (buf );
349
- infree (si );
350
429
}
351
430
return ;
352
- }
353
- else if (strstr ((char * )payload ,"startSleep=" )!= NULL )
354
- {
431
+ } else if (strstr ((char * )payload ,"startSleep=" )!= NULL ) {
355
432
if (strstr ((char * )payload ,"&" ) != NULL )
356
433
* strstr ((char * )payload ,"&" )= 0 ;
357
434
else return ;
@@ -1023,8 +1100,7 @@ static bool httpServerHandleConnection(int conn, char* buf, uint16_t buflen) {
1023
1100
websocketAccept (conn ,buf ,buflen );
1024
1101
ESP_LOGD (TAG ,"websocketAccept socket: %d" ,conn );
1025
1102
return false;
1026
- } else
1027
- {
1103
+ } else {
1028
1104
c += 4 ;
1029
1105
char * c_end = strstr (c , "HTTP" );
1030
1106
if (c_end == NULL ) return true;
@@ -1078,7 +1154,7 @@ static bool httpServerHandleConnection(int conn, char* buf, uint16_t buflen) {
1078
1154
param = strstr (c ,"version" ) ;
1079
1155
if (param != NULL ) {
1080
1156
char vr [30 ];// = malloc(30);
1081
- sprintf (vr ,"Release: %s, Revision: %s\n" , RELEASE ,REVISION );
1157
+ sprintf (vr ,"Release: %s, Revision: %s (%s)" , RELEASE , REVISION , WS_SOCKET_VERSION );
1082
1158
printf ("Version:%s\n" ,vr );
1083
1159
respOk (conn ,vr );
1084
1160
return true;
0 commit comments