MBED implementation of JAudioStream
Revision 2:22ee01f24346, committed 2015-08-13
- Comitter:
- ollie8
- Date:
- Thu Aug 13 16:19:50 2015 +0000
- Parent:
- 1:b49a6e72f353
- Child:
- 3:51a66c975910
- Commit message:
- Now talking to server and responds with heartbeats
Changed in this revision
JAudioStream.cpp | Show annotated file Show diff for this revision Revisions of this file |
JAudioStream.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/JAudioStream.cpp Thu Aug 13 13:32:51 2015 +0000 +++ b/JAudioStream.cpp Thu Aug 13 16:19:50 2015 +0000 @@ -1,37 +1,39 @@ #include "JAudioStream.h" +#define DEBUG +#include "logger.h" JAudioStream::JAudioStream() { + cont = "cont"; + cond = "cond"; + dcon = "dcon"; + rqst = "rqst"; + begn = "begn"; + comt = "comt"; + hrbt = "hrbt"; connected = false; inTransmission = false; } -void JAudioStream::begin(char* mac, char* ip, short port) { +bool JAudioStream::connect(char* ip, short port, char* name) { eth.init(); eth.connect(); udp.init(); udp.set_blocking(true); -} - -bool JAudioStream::connect(char* ip, short port, char* name) { remotePort = port; remoteIp = ip; endPoint.set_address(ip, port); char size = MESSAGE_SIZE*2; char message[size]; buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); - send(message, size); - char resp[MAX_PACKT]; - if (read(resp, MAX_PACKT, 500)) { - if (memcmp(resp, cond, 4) == 0) { - connected = true; - } - } + send(message, size); return connected; } -bool JAudioStream::read(char *buffer, short amount, short timeOut) { +bool JAudioStream::read(char *buffer) { bool result = false; - if (udp.receiveFrom(endPoint,buffer, MAX_PACKT) > 0) { + INFO("Reading..."); + if (udp.receiveFrom(endPoint, buffer, sizeof(buffer)) > 0) { + INFO("Received: %s", buffer); result = true; } return result; @@ -42,7 +44,7 @@ return true; } else { char resp[MAX_PACKT]; - if (read(resp, MAX_PACKT, 500)) { + if (read(resp)) { if (memcmp(resp, begn, 4) == 0) { memmove(&nowPlaying[0], &resp[4], 20); inTransmission = true; @@ -69,30 +71,30 @@ send(message, MESSAGE_SIZE); } -void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { +void JAudioStream::buildMeassge(char* cmd, int param, char* message) { + char bParam[MESSAGE_COMP_SIZE]; char i; - for(i=0; i<MESSAGE_COMP_SIZE; i++) { - message[i] = cmd[i]; + for (i=0; i<MESSAGE_COMP_SIZE; i++) { + message[i] = cmd[i]; } - char bParam[MESSAGE_COMP_SIZE]; intTocharArr(param, bParam); - for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { - message[i] = bParam[i-MESSAGE_COMP_SIZE]; + for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { + message[i] = bParam[i-MESSAGE_COMP_SIZE]; } } -void JAudioStream::buildMeassge(const char* cmd, int param, char* data, char* message) { +void JAudioStream::buildMeassge(char* cmd, int param, char* data, char* message) { + char bParam[MESSAGE_COMP_SIZE]; char i; - for(i=0; i<MESSAGE_COMP_SIZE; i++) { - message[i] = cmd[i]; + for (i=0; i<MESSAGE_COMP_SIZE; i++) { + message[i] = cmd[i]; } - char bParam[MESSAGE_COMP_SIZE]; intTocharArr(param, bParam); - for(i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { - message[i] = bParam[i-MESSAGE_COMP_SIZE]; + for (i=MESSAGE_COMP_SIZE; i<MESSAGE_SIZE; i++) { + message[i] = bParam[i-MESSAGE_COMP_SIZE]; } char dataSize = MESSAGE_SIZE*2; - for(i=MESSAGE_SIZE; i<dataSize; i++) { + for (i=MESSAGE_SIZE; i<dataSize; i++) { message[i] = data[i-MESSAGE_SIZE]; } } @@ -105,9 +107,23 @@ } void JAudioStream::send(char* data, short len) { + INFO("Sending: %s", data); udp.sendTo(endPoint, data, len); } char* JAudioStream::getNowPlaying() { return nowPlaying; } + +void JAudioStream::receive() { + char resp[MAX_PACKT]; + if (read(resp)) { + if (memcmp(resp, cond, 4) == 0) { + connected = true; + } else if (memcmp(resp, hrbt, 4) == 0) { + char message[MESSAGE_SIZE]; + buildMeassge(hrbt, -1, message); + send(message, MESSAGE_SIZE); + } + } +}
--- a/JAudioStream.h Thu Aug 13 13:32:51 2015 +0000 +++ b/JAudioStream.h Thu Aug 13 16:19:50 2015 +0000 @@ -10,16 +10,15 @@ #define REQUEST_RESPONSE_MODE 1 #define PORT 7777 -const char* cont = "cont"; -const char* cond = "cond"; -const char* dcon = "dcon"; -const char* rqst = "rqst"; -const char* begn = "begn"; -const char* comt = "comt"; - class JAudioStream { private: - // use DHCP + char* cont; + char* cond; + char* dcon; + char* rqst; + char* begn; + char* comt; + char* hrbt; EthernetInterface eth; UDPSocket udp; Endpoint endPoint; @@ -27,21 +26,21 @@ short remotePort; volatile bool connected; volatile bool inTransmission; - void buildMeassge(const char*, int, char*); - void buildMeassge(const char*, int, char*, char*); + void buildMeassge(char*, int, char*); + void buildMeassge(char*, int, char*, char*); void intTocharArr(int, char*); void send(char*, short); char* nowPlaying; public: JAudioStream(); - void begin(char *, char *, short); bool connect(char *, short, char *); - bool read(char*, short, short); + bool read(char*); bool isConnected(); char* getNowPlaying(); void request(char); void disconnect(); bool available(); + void receive(); }; #endif \ No newline at end of file