Skip to content

Commit

Permalink
Better autodiscovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiraut committed Mar 26, 2019
1 parent c939440 commit 665b1cd
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 61 deletions.
78 changes: 60 additions & 18 deletions SqueezeESP32.ino
Expand Up @@ -64,6 +64,7 @@

slimproto * vislimCli = 0;
WiFiClient client;
int viCnxAttempt = -1;

WiFiUDP udp;

Expand Down Expand Up @@ -107,6 +108,11 @@ while (i<plugin_size)
#endif //VS1053_MODULE
void setup()
{



viCnxAttempt = 0;

Serial.begin(115200);
delay(1000);
Serial.println("Connecting to WiFi");
Expand Down Expand Up @@ -154,58 +160,94 @@ void setup()
Serial.println("Connected");
*/

// start UDP server
udp.begin(UPD_PORT);
udp.begin(UPD_PORT);
}


void loop()
{
Serial.println("Search for LMS server...");

//Send udp packet for autodiscovery
udp.beginPacket("255.255.255.255",UPD_PORT);
udp.printf("e");
udp.endPacket();


// Reset addr of LMS
if(viCnxAttempt == -1)
LMS_addr = IPAddress(0,0,0,0);

delay(2000);

if(udp.parsePacket()> 0)
// If no LMS found yet
if(LMS_addr[0] == 0)
{
LMS_addr = udp.remoteIP();
Serial.print("Found LMS server @ ");
Serial.println(LMS_addr);
Serial.println("Search for LMS server...");
for(int nbSend = 0; nbSend < 10; nbSend++)
{
// start UDP server
//Send udp packet for autodiscovery
udp.flush();
udp.beginPacket("255.255.255.255",UPD_PORT);
udp.printf("e");
udp.endPacket();

delay(2000);

if(udp.parsePacket()> 0)
{
char upd_packet;
upd_packet = udp.read();

if(upd_packet == 'E')
{
LMS_addr = udp.remoteIP();
Serial.print("Found LMS server @ ");
Serial.println(LMS_addr);
//udp.stop();
break; // LMS found we can go to the next step
}
}
else
delay(2000);
}
}

if(LMS_addr[0] != 0)
{
Serial.println("Connecting to server...");
Serial.print("Connecting to server @");
Serial.print(LMS_addr);
Serial.println("...");

// DEBUG server 3484
// Real server 3483

viCnxAttempt++;

if (!client.connect(LMS_addr, 3483)) {
Serial.println("connection failed, pause and try connect...");

viCnxAttempt++;
if(viCnxAttempt > 30)
viCnxAttempt = -1; // Will erase LMS addr in the next attempt

delay(2000);
return;
}


viCnxAttempt = 0;

if(vislimCli) delete vislimCli,vislimCli = 0;

#ifdef VS1053_MODULE
vislimCli = new slimproto(LMS_addr.toString(), client, &viplayer);
vislimCli = new slimproto(LMS_addr.toString(), & client, &viplayer);
#else
vislimCli = new slimproto(LMS_addr.toString(), client);
#endif

Serial.println("Connection Ok, send hello to LMS");
reponseHelo * HeloRsp = new reponseHelo(client);
reponseHelo * HeloRsp = new reponseHelo(&client);
HeloRsp->sendResponse();

while(client.connected())
{
vislimCli->HandleMessages();
if(!vislimCli->HandleMessages())
break;
vislimCli->HandleAudio();
}
}
Expand Down
79 changes: 43 additions & 36 deletions slimproto.cpp
Expand Up @@ -6,7 +6,7 @@
#define VOLUME 80


responseBase::responseBase(WiFiClient pClient)
responseBase::responseBase(WiFiClient * pClient)
{
vcClient = pClient;
}
Expand All @@ -21,24 +21,24 @@ responseBase::~responseBase()
void responseBase::sendResponse()
{
vcResponse.sizeResponse = sizeof(vcResponse);
vcClient.write((char*) &vcResponse, sizeof(vcResponse));
vcClient->write((char*) &vcResponse, sizeof(vcResponse));
}
*/


reponseHelo::reponseHelo(WiFiClient pClient) : responseBase(pClient)
reponseHelo::reponseHelo(WiFiClient * pClient) : responseBase(pClient)
{
}

void reponseHelo::sendResponse()
{
vcResponse.sizeResponse = __builtin_bswap32(sizeof(vcResponse) - 8); // N'inclus pas la commande ni la taille.
vcClient.write((char*) &vcResponse, sizeof(vcResponse));
vcClient->write((char*) &vcResponse, sizeof(vcResponse));
}



reponseSTAT::reponseSTAT(WiFiClient pClient) : responseBase(pClient)
reponseSTAT::reponseSTAT(WiFiClient * pClient) : responseBase(pClient)
{
// Clear the reponse struct
memset((void *) &vcResponse,'\0', sizeof(vcResponse));
Expand All @@ -50,28 +50,31 @@ void reponseSTAT::sendResponse()
memcpy((void *) vcResponse.opcode, "STAT", 4);

vcResponse.sizeResponse = __builtin_bswap32(sizeof(vcResponse) - 8); // N'inclus pas la commande ni la taille.
vcClient.write((char*) &vcResponse, sizeof(vcResponse));
vcClient->write((char*) &vcResponse, sizeof(vcResponse));
}

/**
* Constructor
**/

#define RINGBFSIZ 60000

#ifdef ADAFRUIT_VS1053
slimproto::slimproto(String pAdrLMS, WiFiClient pClient, Adafruit_VS1053 * pPlayer)
#else
slimproto::slimproto(String pAdrLMS, WiFiClient pClient, VS1053 * pPlayer)
slimproto::slimproto(String pAdrLMS, WiFiClient * pClient, VS1053 * pPlayer)
#endif
{
vcCommandSize = 0;

vcPlayerStat = StopStatus; /* 0 = stop , 1 = play , 2 = pause */

// Initialize the ringbuffer for audio
vcRingBuffer = new stRingBuffer(RINGBFSIZ);

u32_t viFreeMem = system_get_free_heap_size();

Serial.print("Free Memory for RingBuffer : "), Serial.print(viFreeMem), Serial.println(" bytes");

// Initialize the ringbuffer for audio
//vcRingBuffer = new stRingBuffer(RINGBFSIZ > viFreeMem ? viFreeMem-5000 : RINGBFSIZ); // if not enought memory for the ringbuffer use free memory minus 5ko
vcRingBuffer = new stRingBuffer(RINGBFSIZ); // if not enought memory for the ringbuffer use free memory minus 5ko
vcAdrLMS = pAdrLMS;

vcClient = pClient;
Expand All @@ -84,7 +87,7 @@ EndTimeCurrentSong = StartTimeCurrentSong = 0;

}

slimproto::slimproto(WiFiClient pClient)
slimproto::slimproto(WiFiClient * pClient)
{
vcCommandSize = 0;

Expand All @@ -99,20 +102,21 @@ EndTimeCurrentSong = StartTimeCurrentSong = 0;

slimproto::~slimproto()
{
if(vcRingBuffer) delete vcRingBuffer, vcRingBuffer = 0;
}

int slimproto::HandleMessages()
{
uint8_t viBuffer;
int viSizeRead;

if(vcClient.connected())
if(vcClient->connected())
{
if(vcCommandSize == 0 && vcClient.available() >= 2)
if(vcCommandSize == 0 && vcClient->available() >= 2)
{
uint8_t viExtractSize[2];

vcClient.read(viExtractSize,2);
vcClient->read(viExtractSize,2);

// Convert string size into integer
vcCommandSize = (viExtractSize[0] << 8) | viExtractSize[1];
Expand All @@ -124,26 +128,26 @@ int viSizeRead;

if(vcCommandSize > 250)
{
uint8_t availableSize = vcClient.available();
uint8_t availableSize = vcClient->available();

Serial.println("Expected command size to big ??!!??");
Serial.print("Available size : "),Serial.println(availableSize);
uint8_t viExtractCommand[availableSize];
viSizeRead = vcClient.read(viExtractCommand,availableSize);
viSizeRead = vcClient->read(viExtractCommand,availableSize);
PrintByteArray(viExtractCommand,viSizeRead );
vcCommandSize = 0;
}


if(vcCommandSize && vcClient.available() >= vcCommandSize)
if(vcCommandSize && vcClient->available() >= vcCommandSize)
{
uint8_t viExtractCommand[vcCommandSize];

viSizeRead = vcClient.read(viExtractCommand,vcCommandSize);
viSizeRead = vcClient->read(viExtractCommand,vcCommandSize);

if(viSizeRead != vcCommandSize)
{
Serial.println("Pas lu autant que prévu");
Serial.println("Not enought data as expected !!!");
}

if(vcCommandSize != 172)
Expand All @@ -153,6 +157,20 @@ int viSizeRead;
vcCommandSize = 0;
}
}

// Send Stat Message if last one is more than 60 seconds
if(millis() - LastStatMsg >= ( 60 * 1000))
{
Serial.println("No Stat request from 60 seconds, Is there any probem ?");

//reponseSTAT viResponse(vcClient);
//viResponse.vcResponse.elapsed_seconds = 0;
//viResponse.sendResponse();
//LastStatMsg = millis();
return false;
}

return true;
}


Expand Down Expand Up @@ -307,7 +325,7 @@ LastStatMsg = millis();
//debug
PrintByteArray((byte *)&viResponse.vcResponse, sizeof(viResponse.vcResponse));

//vcClient.write(staT, sizeof(staT));
//vcClient->write(staT, sizeof(staT));
}


Expand Down Expand Up @@ -402,19 +420,19 @@ Serial.println(viUrl);
reponseSTAT viResponseSTMc(vcClient);
memcpy((void *) viResponseSTMc.vcResponse.event, "STMc", 4);
viResponseSTMc.sendResponse();
//vcClient.write(staC, sizeof(staC));
//vcClient->write(staC, sizeof(staC));
// Send connected

reponseSTAT viResponseSTMe(vcClient);
memcpy((void *) viResponseSTMe.vcResponse.event, "STMe", 4);
viResponseSTMe.sendResponse();
//vcClient.write(staE, sizeof(staE));
//vcClient->write(staE, sizeof(staE));

// Send HTTP headers received from stream connection
reponseSTAT viResponseSTMh(vcClient);
memcpy((void *) viResponseSTMh.vcResponse.event, "STMh", 4);
viResponseSTMh.sendResponse();
//vcClient.write(staH, sizeof(staH));
//vcClient->write(staH, sizeof(staH));

// Send Track Started
reponseSTAT viResponseSTMs(vcClient);
Expand Down Expand Up @@ -543,18 +561,7 @@ else
Serial.println("]");
Serial.print("Size : ");
Serial.println(pSize);
}


// Send Stat Message if last one is more than 5 seconds
if(millis() - LastStatMsg >= ( 5 * 1000))
{
reponseSTAT viResponse(vcClient);
viResponse.vcResponse.elapsed_seconds = 0;
viResponse.sendResponse();
LastStatMsg = millis();
}

}
}

void slimproto::ExtractCommand(byte * pBuf, int pSize)
Expand Down

0 comments on commit 665b1cd

Please sign in to comment.