diff --git a/cwEuCon.cpp b/cwEuCon.cpp index 9861599..b07060e 100644 --- a/cwEuCon.cpp +++ b/cwEuCon.cpp @@ -308,7 +308,11 @@ namespace cw kSendHandshake_0_Id, // send [0x0a, ...] kWaitForBeat_1_Id, // wait for first heart beat -> then send [0x0c ...] kWaitForHandshake_2_Id, // wait for [0x0d ...] -> then send response_3_a - kRunning_3_Id + kResponse_3_A_Id, + kResponse_3_B_Id, + kResponse_4_A_Id, + kResponse_4_B_Id, + kRunning_Id }; typedef struct eucon_str @@ -344,39 +348,7 @@ namespace cw return rc; } - rc_t _sendHandshake_0( socket::handle_t sockH, const char* label ) - { - rc_t rc = kOkRC; - unsigned char buf[88]; - memset(buf,0,88); - buf[0] = 0x0a; - - // send the initial handshake - if((rc = socket::send( sockH, buf, 88 )) != kOkRC ) - { - rc = cwLogError(rc,"Initial TCP '%s' request failed.",label); - } - - return rc; - } - - rc_t _sendHandshake_1( socket::handle_t sockH ) - { - rc_t rc = kOkRC; - unsigned char buf[4]; - memset(buf,0,4); - buf[0] = 0x0c; - - // send the initial handshake - if((rc = socket::send( sockH, buf, 4 )) != kOkRC ) - { - rc = cwLogError(rc,"TCP '%s' request failed."); - } - - return rc; - } - - rc_t _send_response( const char* packet ) + rc_t _send_response( socket::handle_t sockH, const char* packet ) { rc_t rc = kOkRC; @@ -389,6 +361,45 @@ namespace cw return rc; } + rc_t _sendHandshake_0( socket::handle_t sockH, const char* label ) + { + /* + rc_t rc = kOkRC; + unsigned char buf[88]; + memset(buf,0,88); + buf[0] = 0x0a; + + // send the initial handshake + if((rc = socket::send( sockH, buf, 88 )) != kOkRC ) + { + rc = cwLogError(rc,"Initial TCP '%s' request failed.",label); + } + + return rc; + */ + return _send_response(sockH,RESPONSE_1); + } + + rc_t _sendHandshake_1( socket::handle_t sockH ) + { + /* + rc_t rc = kOkRC; + unsigned char buf[4]; + memset(buf,0,4); + buf[0] = 0x0c; + + // send the initial handshake + if((rc = socket::send( sockH, buf, 4 )) != kOkRC ) + { + rc = cwLogError(rc,"TCP '%s' request failed."); + } + + return rc; + */ + return _send_response(sockH,RESPONSE_2); + } + + void _udpReceiveCallback( void* arg, const void* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr ) { rc_t rc = kOkRC; @@ -471,9 +482,29 @@ namespace cw case kWaitForHandshake_2_Id: if( hdr == 0x0d ) { - p->protoState = kRunning_3_Id; + p->protoState = kResponse_3_A_Id; } break; + + case kResponse_3_A_Id: + _send_response(socketHandle(p->tcpH),RESPONSE_3_A); + p->protoState = kResponse_3_B_Id; + break; + + case kResponse_3_B_Id: + _send_response(socketHandle(p->tcpH),RESPONSE_3_B); + p->protoState = kResponse_4_A_Id; + break; + + case kResponse_4_A_Id: + _send_response(socketHandle(p->tcpH),RESPONSE_4_A); + p->protoState = kResponse_4_B_Id; + break; + + case kResponse_4_B_Id: + _send_response(socketHandle(p->tcpH),RESPONSE_4_B); + p->protoState = kRunning_Id; + break; } } diff --git a/dns_sd/dns_sd_print.cpp b/dns_sd/dns_sd_print.cpp index cf245a5..e9ac2f9 100644 --- a/dns_sd/dns_sd_print.cpp +++ b/dns_sd/dns_sd_print.cpp @@ -1,11 +1,9 @@ #include #include #include - #ifdef cwLINUX #include #endif - #ifdef ARDUINO #include #endif @@ -39,7 +37,8 @@ int _print_name( printCallback_t printCbFunc, const unsigned char* s, const uns x[1] = 0; rpt(printCbFunc,"%s",x); if( incrFl ) - ++n; + ++n; + } s += s[0]+1; diff --git a/dns_sd/fader.cpp b/dns_sd/fader.cpp index 28503f7..75b7bad 100644 --- a/dns_sd/fader.cpp +++ b/dns_sd/fader.cpp @@ -6,10 +6,26 @@ #include "rpt.h" #include "fader.h" +#ifdef cwLINUX +#include +#endif +#ifdef ARDUINO +#include +#endif + +fader::msgRef_t fader::_msgRefA[] = +{ + { 0x0a, 88 }, + { 0x0c, 6 }, + { 0x00, 8 }, + { 0x19, 1044 }, + { 0x04, 4 }, + { 0xff, 0 } // end-of-list sentinel (both id and byteN are invalid) +}; fader::fader( printCallback_t printCbFunc, const unsigned char faderMac[6], uint32_t faderInetAddr, hostCallback_t hostCbFunc, void* hostCbArg, unsigned ticksPerHeartBeat, unsigned chN ) - : _printCbFunc(printCbFunc), _inetAddr(faderInetAddr),_tickN(0),_chArray(nullptr),_hostCbFunc(hostCbFunc),_hostCbArg(hostCbArg),_protoState(kWaitForHandshake_0_Id),_ticksPerHeartBeat(ticksPerHeartBeat) + : _printCbFunc(printCbFunc), _inetAddr(faderInetAddr),_tickN(0),_chArray(nullptr),_hostCbFunc(hostCbFunc),_hostCbArg(hostCbArg),_protoState(kWaitForHandshake_0_Id),_ticksPerHeartBeat(ticksPerHeartBeat),_msgTypeId(0xff),_msgByteIdx(0),_msgByteN(0) { memcpy(_mac,faderMac,6); @@ -19,6 +35,8 @@ fader::fader( printCallback_t printCbFunc, const unsigned char faderMac[6], uint { _chArray[i].position = 0; _chArray[i].muteFl = false; + _chArray[i].incrFl = true; + _chArray[i].touchFl = false; } } @@ -27,28 +45,70 @@ fader::~fader() delete[] _chArray; } + + fader::rc_t fader::receive( const void* buf, unsigned bufByteN ) +{ + rc_t rc = kOkRC; + const uint8_t* b = (const uint8_t*)buf; // current msg ptr + const uint8_t* bend = b + bufByteN; // end of buffer ptr + + while(b0 && b[0] == 10 ) { _printCbFunc("HS 0 "); - _send_response_0(); + _send_response_0(); // send [ 0x0b ... ] _protoState = kWaitForHandshake_Tick_Id; } break; - case kWaitForHandshake_Tick_Id: + case kWaitForHandshake_Tick_Id: // wait for next tick() - send first heart-beat break; - case kWaitForHandshake_1_Id: + case kWaitForHandshake_1_Id: // wait for next message after heart-beat - send [ 0x0d, .... ] _printCbFunc("HS 1 "); _send_response_1(); _protoState = kWaitForHeartBeat_Id; @@ -77,7 +137,8 @@ fader::rc_t fader::tick() case kWaitForHandshake_1_Id: break; - case kWaitForHeartBeat_Id: + case kWaitForHeartBeat_Id: + //_auto_incr_fader(0); break; } @@ -104,7 +165,7 @@ fader::rc_t fader::physical_fader_moved( uint16_t chanIdx, uint16_t value ) return kOkRC; } -fader::rc_t fader::physical_mute_switched( uint16_t chanIdx, bool value ) +fader::rc_t fader::physical_mute_switched( uint16_t chanIdx, uint16_t value ) { (void)chanIdx; (void)value; @@ -163,3 +224,109 @@ void fader::_on_mute_receive( uint16_t chanIdx, bool value ) (void)value; } +void fader::_send_fader( uint16_t chIdx ) +{ + uint16_t buf[] = { htons(chIdx),htons(0), 0, htons(_chArray[chIdx].position) }; + _send(buf,sizeof(buf)); +} + +void fader::_send_touch( uint16_t chIdx, bool touchFl ) +{ + _chArray[chIdx].touchFl = touchFl; + uint16_t buf[] = { htons(chIdx),htons(1),0, htons((uint16_t)touchFl) }; + _send(buf,sizeof(buf)); +} + +void fader::_send_mute( uint16_t chIdx, bool muteFl ) +{ + _chArray[chIdx].muteFl = muteFl; + uint16_t buf[] = { htons(chIdx),htons(0x200),0, htons((uint16_t)(!muteFl)) }; + _send(buf,sizeof(buf)); +} + + +void fader::_auto_incr_fader( uint16_t chIdx ) +{ + ch_t* ch = _chArray+chIdx; + + if( ch->position == 0 && ch->touchFl==false ) + { + _send_touch(chIdx,true); + } + + if( ch->position > 1023 ) + { + ch->position = 1023; + ch->incrFl = false; + } + else + { + if( ch->position < 0 ) + { + ch->position = 0; + ch->incrFl = true; + _send_touch( chIdx, !ch->touchFl ); + _send_mute( chIdx, !ch->touchFl ); + } + } + + if( ch->touchFl ) + _send_fader(chIdx); + + ch->position += ch->incrFl ? 5 : -5; + +} + +uint8_t fader::_get_msg_byte_count( uint8_t msgTypeId ) +{ + for(int i=0; _msgRefA[i].byteN != 0; ++i) + if( msgTypeId == _msgRefA[i].id ) + return _msgRefA[i].byteN; + + return 0; +} + +void fader::_handleChMsg(const uint8_t* msg) +{ +} + +// called when a new msg is received, b[0] is the msg type id +void fader::_on_msg_complete( const uint8_t typeId ) +{ + switch( typeId ) + { + case 0x0a: + if( _protoState == kWaitForHandshake_0_Id ) + { + _printCbFunc("HS 0 "); + _send_response_0(); // send [ 0x0b ... ] + _protoState = kWaitForHandshake_Tick_Id; + } + break; + + case 0x0c: + break; + + case 0x00: + break; + + case 0x04: + break; + + case 0x19: + break; + + default: + rpt(_printCbFunc,"Unknown msg type."); + } + + // Any msg will trigger change of state from + // kWaitForHandshake_1_Id to kWaitForHeartBeat_Id + if( _protoState == kWaitForHandshake_1_Id ) + { + _printCbFunc("HS 1 "); + _send_response_1(); //send [ 0x0d, .... ] + _protoState = kWaitForHeartBeat_Id; + } + +} diff --git a/dns_sd/fader.h b/dns_sd/fader.h index 8d9a2e2..d71b10c 100644 --- a/dns_sd/fader.h +++ b/dns_sd/fader.h @@ -33,6 +33,7 @@ public: // based on host state changes. // Return kUnknownMsgRC if the received msg is not recognized. rc_t receive( const void* buf, unsigned bufByteN ); + rc_t receive_old( const void* buf, unsigned bufByteN ); // Called by the application to drive time dependent functions. // Return kTimeOut if the protocol state machine has timed out. @@ -42,7 +43,10 @@ public: // the controls physical state changes. rc_t physical_fader_touched( uint16_t chIdx ); rc_t physical_fader_moved( uint16_t chIdx, uint16_t newPosition ); - rc_t physical_mute_switched( uint16_t chIdx, bool newMuteFl ); + rc_t physical_mute_switched( uint16_t chIdx, uint16_t newMuteFl ); + + rc_t virtual_fader_moved( uint16_t chIdx, uint16_t newPosition ); + rc_t virtual_mute_switched( uint16_t chIdx, uint16_t newMuteFl ); private: typedef enum @@ -55,10 +59,22 @@ private: typedef struct { - uint16_t position; + int16_t position; bool muteFl; + bool incrFl; + bool touchFl; + } ch_t; + typedef struct + { + uint16_t id; // message prefix - identifies the type of message + uint16_t byteN; // length of the message in bytes + } msgRef_t; + + static msgRef_t _msgRefA[]; + + printCallback_t _printCbFunc; uint32_t _inetAddr; unsigned _tickN; @@ -70,13 +86,27 @@ private: unsigned char _mac[6]; unsigned _ticksPerHeartBeat; + uint8_t _msgTypeId; // + unsigned _msgByteIdx; // current index into the message being parsed. + unsigned _msgByteN; // count of bytes in the message currently being parsed + unsigned char _msg[8]; // + void _send_response_0(); void _send_response_1(); void _send_heartbeat(); void _send( const void* buf, unsigned bufByteN ); void _on_fader_receive( uint16_t chanIdx, uint16_t position ); void _on_mute_receive( uint16_t chanIdx, bool muteFl ); + void _send_fader( uint16_t chIdx ); + void _send_touch( uint16_t chIdx, bool touchFl ); + void _send_mute( uint16_t chIdx, bool muteFl ); + void _auto_incr_fader( uint16_t chIdx ); + + uint8_t _get_msg_byte_count( uint8_t msgTypeId ); + void _handleChMsg(const uint8_t* msg); + void _on_msg_complete( const uint8_t typeId ); + };