Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f347195

Browse files
author
bazooka07
committedJan 29, 2020
Add /getStationsFrom command in websocket for downloading stations by batch
1 parent 393ec80 commit f347195

File tree

3 files changed

+115
-39
lines changed

3 files changed

+115
-39
lines changed
 

‎build/KaRadio32.bin

800 Bytes
Binary file not shown.

‎main/include/interface.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/******************************************************************************
2-
*
2+
*
33
* Copyright 2018 karawin (http://www.karawin.fr)
44
*
55
*******************************************************************************/
@@ -18,9 +18,10 @@
1818

1919
#define RELEASE "1.9"
2020
#define REVISION "6"
21+
#define WS_SOCKET_VERSION "2"
2122

2223
uint32_t checkUart(uint32_t speed);
23-
extern unsigned short adcdiv;
24+
extern unsigned short adcdiv;
2425
void switchCommand(void );
2526
void checkCommand(int size, char* s);
2627
esp_log_level_t getLogLevel();
@@ -52,12 +53,11 @@ void setHostname(char* s);
5253
telnetWrite(2*MAXDATAT,fmt, ##__VA_ARGS__); \
5354
addonParse(fmt, ##__VA_ARGS__);\
5455
} while (0)
55-
56+
5657
#define kprintfl(fmt, ...) do { \
5758
printf(fmt, ##__VA_ARGS__); \
5859
telnetWrite(1024,fmt, ##__VA_ARGS__); \
5960
addonParse(fmt, ##__VA_ARGS__);\
6061
} while (0)
61-
62+
6263
#endif
63-

‎main/webserver.c

Lines changed: 110 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -304,54 +304,131 @@ static void theme() {
304304
}
305305

306306
// 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) {
309308
//wsvol
310309
ESP_LOGV(TAG,"websocketHandle: %s",payload);
311310
char value[6] = "";
312-
if (strstr((char*)payload,"wsvol=")!= NULL)
313-
{
311+
if (strstr((char*)payload,"wsvol=")!= NULL) {
314312
char answer[17];
315313
if (strstr((char*)payload,"&") != NULL)
316314
*strstr((char*)payload,"&")=0;
317315
else return;
318316
// setVolume(payload+6);
319317
sprintf(answer,"{\"wsvol\":\"%s\"}",payload+6);
320318
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)) {
323320
int uid = atoi(value);
324321
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}";
326397
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));
335401
} 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);
347428
}
348-
infree(buf);
349-
infree(si);
350429
}
351430
return;
352-
}
353-
else if (strstr((char*)payload,"startSleep=")!= NULL)
354-
{
431+
} else if (strstr((char*)payload,"startSleep=")!= NULL) {
355432
if (strstr((char*)payload,"&") != NULL)
356433
*strstr((char*)payload,"&")=0;
357434
else return;
@@ -1023,8 +1100,7 @@ static bool httpServerHandleConnection(int conn, char* buf, uint16_t buflen) {
10231100
websocketAccept(conn,buf,buflen);
10241101
ESP_LOGD(TAG,"websocketAccept socket: %d",conn);
10251102
return false;
1026-
} else
1027-
{
1103+
} else {
10281104
c += 4;
10291105
char* c_end = strstr(c, "HTTP");
10301106
if(c_end == NULL) return true;
@@ -1078,7 +1154,7 @@ static bool httpServerHandleConnection(int conn, char* buf, uint16_t buflen) {
10781154
param = strstr(c,"version") ;
10791155
if (param != NULL) {
10801156
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);
10821158
printf("Version:%s\n",vr);
10831159
respOk(conn,vr);
10841160
return true;

0 commit comments

Comments
 (0)
Please sign in to comment.