MBED implementation of JAudioStream
JAudioStream.cpp
- Committer:
- ollie8
- Date:
- 2015-08-19
- Revision:
- 7:9dfba830a0c8
- Parent:
- 6:7e366261cd93
- Child:
- 8:65d8e8882907
File content as of revision 7:9dfba830a0c8:
#include "JAudioStream.h" #define LOG_LEVEL_INFO #include "logger.h" JAudioStream::JAudioStream() { cont = "cont"; cond = "cond"; dcon = "dcon"; dctd = "dctd"; rqst = "rqst"; begn = "begn"; comt = "comt"; hrbt = "hrbt"; connected = false; inTransmission = false; } bool JAudioStream::connect(char* ip, short port, char* name) { eth.init(); eth.connect(); socket.init(); socket.set_blocking(false, 500); remotePort = port; remoteIp = ip; endPoint.set_address(ip, port); char message[MESSAGE_SIZE*2]; buildMeassge(cont, REQUEST_RESPONSE_MODE, name, message); send(message, MESSAGE_SIZE*2); return connected; } bool JAudioStream::read(char *buffer, int size) { TRACE("Received %s", buffer); if (socket.receiveFrom(endPoint, buffer, size)) { return true; } return false; } bool JAudioStream::isConnected() { return connected; } void JAudioStream::request(char amount) { char message[MESSAGE_SIZE]; buildMeassge(rqst, amount, message); send(message, MESSAGE_SIZE); } void JAudioStream::disconnect() { char message[MESSAGE_SIZE]; buildMeassge(dcon, -1, message); send(message, MESSAGE_SIZE); } void JAudioStream::buildMeassge(const char* cmd, int param, char* message) { char bParam[MESSAGE_COMP_SIZE]; intTocharArr(param, bParam); char i; for (i=0; i<MESSAGE_COMP_SIZE; i++) { message[i] = cmd[i]; } 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) { char bParam[MESSAGE_COMP_SIZE]; char i; for (i=0; i<MESSAGE_COMP_SIZE; i++) { message[i] = cmd[i]; } intTocharArr(param, bParam); 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++) { message[i] = data[i-MESSAGE_SIZE]; } } void JAudioStream::intTocharArr(int value, char* buff) { buff[0] = (value >> 24) & 0xFF; buff[1] = (value >> 16) & 0xFF; buff[2] = (value >> 8) & 0xFF; buff[3] = value; } void JAudioStream::send(char* data, int size ) { TRACE("Sending %s", data); socket.sendTo(endPoint, data, size); } char* JAudioStream::getNowPlaying() { return nowPlaying; } void JAudioStream::receive() { char resp[MAX_PACKT]; //if (!inTransmission) { if (read(resp, MAX_PACKT)) { if (memcmp(resp, cond, 4) == 0) { connected = true; } else if (memcmp(resp, dctd, 4) == 0) { connected = false; } else if (memcmp(resp, hrbt, 4) == 0) { char message[MESSAGE_SIZE]; buildMeassge(hrbt, -1, message); send(message, MESSAGE_SIZE); } else if (memcmp(resp, begn, 4) == 0) { memmove(&nowPlaying[0], &resp[4], 20); inTransmission = true; } else if (memcmp(resp, comt, 4) == 0) { inTransmission = false; } } //} }