size_t ut_WiflyControl_FwSetGradient(void) { led_cmd expectedOutgoingFrame; expectedOutgoingFrame.cmd = SET_GRADIENT; expectedOutgoingFrame.data.set_gradient.red_1 = 0x11; expectedOutgoingFrame.data.set_gradient.green_1 = 0x22; expectedOutgoingFrame.data.set_gradient.blue_1 = 0x33; expectedOutgoingFrame.data.set_gradient.red_2 = 0x33; expectedOutgoingFrame.data.set_gradient.green_2 = 0x22; expectedOutgoingFrame.data.set_gradient.blue_2 = 0x11; expectedOutgoingFrame.data.set_gradient.parallelAndOffset = 0xff; expectedOutgoingFrame.data.set_gradient.numberOfLeds = 10; expectedOutgoingFrame.data.set_gradient.fadeTmms = htons(1000); TestCaseBegin(); Control testee(0, 0); testee << FwCmdSetGradient {0xff112233,0xff332211, 1000, true, 10, 127}; TraceBuffer(ZONE_INFO, &g_SendFrame, sizeof(cmd_set_gradient) + 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, sizeof(cmd_set_gradient) + 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, sizeof(cmd_set_gradient) + 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwSetRtc(void) { tm timeinfo; time_t rawtime; rawtime = time(NULL); localtime_r(&rawtime, &timeinfo); led_cmd expectedOutgoingFrame = {0xff}; expectedOutgoingFrame.cmd = SET_RTC; expectedOutgoingFrame.data.set_rtc.tm_sec = (uns8) timeinfo.tm_sec; expectedOutgoingFrame.data.set_rtc.tm_min = (uns8) timeinfo.tm_min; expectedOutgoingFrame.data.set_rtc.tm_hour = (uns8) timeinfo.tm_hour; expectedOutgoingFrame.data.set_rtc.tm_mday = (uns8) timeinfo.tm_mday; expectedOutgoingFrame.data.set_rtc.tm_mon = (uns8) timeinfo.tm_mon; expectedOutgoingFrame.data.set_rtc.tm_year = (uns8) timeinfo.tm_year; expectedOutgoingFrame.data.set_rtc.tm_wday = (uns8) timeinfo.tm_wday; TestCaseBegin(); Control testee(0, 0); testee << FwCmdSetRtc {timeinfo}; TraceBuffer(ZONE_INFO, &g_SendFrame, sizeof(rtc_time) + 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, sizeof(rtc_time) + 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, sizeof(rtc_time) + 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwSetFade_2(void) { led_cmd expectedOutgoingFrame; expectedOutgoingFrame.cmd = SET_FADE; expectedOutgoingFrame.data.set_fade.addr[0] = 0x44; expectedOutgoingFrame.data.set_fade.addr[1] = 0x33; expectedOutgoingFrame.data.set_fade.addr[2] = 0x22; expectedOutgoingFrame.data.set_fade.addr[3] = 0x11; expectedOutgoingFrame.data.set_fade.red = 0x11; expectedOutgoingFrame.data.set_fade.green = 0x22; expectedOutgoingFrame.data.set_fade.blue = 0x33; expectedOutgoingFrame.data.set_fade.parallelFade = 0x01; //TODO why do we use fadeTmms == 1 for SetColor? expectedOutgoingFrame.data.set_fade.fadeTmms = htons(1000); TestCaseBegin(); Control testee(0, 0); // set color testee << FwCmdSetFade {0xff112233, 1000, 0x11223344, true}; TraceBuffer(ZONE_INFO, &g_SendFrame, sizeof(cmd_set_fade) + 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, sizeof(cmd_set_fade) + 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, sizeof(cmd_set_fade) + 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwSetColorDirectThreeLeds_2(void) { TestCaseBegin(); Control testee(0, 0); // three leds only, all set to yellow const uint32_t argb = 0xffffff00; const uint32_t addr = 0x00000007; uint8_t shortBuffer[3 * 3]; memset(shortBuffer, 0, sizeof(shortBuffer)); shortBuffer[0 * 3 + 0] = 0xff; //first led red shortBuffer[1 * 3 + 1] = 0xff; //second led green shortBuffer[2 * 3 + 2] = 0xff; //third led blue led_cmd expectedOutgoingFrame; expectedOutgoingFrame.cmd = SET_COLOR_DIRECT; memset(expectedOutgoingFrame.data.set_color_direct.ptr_led_array, 0, 3 * NUM_OF_LED); expectedOutgoingFrame.data.set_color_direct.ptr_led_array[0] = 0xff; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[1] = 0xff; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[2] = 0x00; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[3] = 0xff; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[4] = 0xff; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[5] = 0x00; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[6] = 0xff; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[7] = 0xff; expectedOutgoingFrame.data.set_color_direct.ptr_led_array[8] = 0x00; testee << FwCmdSetColorDirect {argb, addr}; TraceBuffer(ZONE_INFO, &g_SendFrame, sizeof(led_cmd) + 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, sizeof(led_cmd) + 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, sizeof(led_cmd))); TestCaseEnd(); }
size_t ut_WiflyControl_FwClearScript(void) { led_cmd expectedOutgoingFrame = {0xff}; expectedOutgoingFrame.cmd = CLEAR_SCRIPT; TestCaseBegin(); Control testee(0, 0); testee << FwCmdClearScript {}; TraceBuffer(ZONE_INFO, &g_SendFrame, 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwLoopOn(void) { led_cmd expectedOutgoingFrame = {0xff}; expectedOutgoingFrame.cmd = LOOP_ON; TestCaseBegin(); Control testee(0, 0); testee << FwCmdLoopOn {}; TraceBuffer(ZONE_INFO, &g_SendFrame, 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwSetWait(void) { led_cmd expectedOutgoingFrame = {0xff}; expectedOutgoingFrame.cmd = WAIT; expectedOutgoingFrame.data.wait.waitTmms = htons(1000); TestCaseBegin(); Control testee(0, 0); testee << FwCmdWait {1000}; TraceBuffer(ZONE_INFO, &g_SendFrame, sizeof(cmd_wait) + 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, sizeof(cmd_wait) + 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, sizeof(cmd_wait) + 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwGetVersion(void) { led_cmd expectedOutgoingFrame = {0xff}; expectedOutgoingFrame.cmd = GET_FW_VERSION; TestCaseBegin(); Control testee(0, 0); try { testee.FwGetVersion(); } catch(std::exception) { } TraceBuffer(ZONE_INFO, &g_SendFrame, 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, 1)); TestCaseEnd(); }
size_t ut_WiflyControl_FwLoopOff(void) { led_cmd expectedOutgoingFrame = {0xff}; expectedOutgoingFrame.cmd = LOOP_OFF; expectedOutgoingFrame.data.loopEnd.numLoops = 100; expectedOutgoingFrame.data.loopEnd.counter = 0; expectedOutgoingFrame.data.loopEnd.depth = 0; expectedOutgoingFrame.data.loopEnd.startIndex = 0; TestCaseBegin(); Control testee(0, 0); testee << FwCmdLoopOff {100}; TraceBuffer(ZONE_INFO, &g_SendFrame, 1, "%02x ", "IS :"); TraceBuffer(ZONE_INFO, &expectedOutgoingFrame, 1, "%02x ", "SOLL:"); CHECK(0 == memcmp(&g_SendFrame, &expectedOutgoingFrame, 1)); TestCaseEnd(); }
Endpoint BroadcastReceiver::GetNextRemote(timeval *timeout) throw (FatalError) { UdpSocket udpSock(INADDR_ANY, mPort, true, 1); sockaddr_storage remoteAddr; socklen_t remoteAddrLength = sizeof(remoteAddr); BroadcastMessage msg; const size_t bytesRead = udpSock.RecvFrom((uint8_t *)&msg, sizeof(msg), timeout, (sockaddr *)&remoteAddr, &remoteAddrLength); TraceBuffer(ZONE_VERBOSE, msg.deviceId, sizeof(msg.deviceId), "%c", "%zu bytes broadcast message received DeviceId: \n", bytesRead); if(msg.IsWiflyBroadcast(bytesRead)) { Trace(ZONE_INFO, "Broadcast detected\n"); Endpoint newRemote(remoteAddr, remoteAddrLength, msg.port, std::string((char *)&msg.deviceId[0])); newRemote.SetScore(1); return LockedInsert(newRemote) ? newRemote : Endpoint(); } return Endpoint(); }
//*********************************************************************** // // ENTRY: zNetReceive // // PURPOSE: To retrieve a block of data from the network. // // DESCRIPTION: Zeidon Core call this operation when it is expecting data // from the network. // // Core expects zNetReceive to create a buffer to hold the data // and set ppszReturnBuffer to point to the buffer. The created // buffer need be valid only until the next zNet... call-- // zNetReceive does not have to worry about keeping track of // data that has previously been retrieved. // // uLth specifies the number of bytes Core is expecting. If // there is not enough data from the network to satisfy Core, // then zNetReceive must return( zCALL_ERROR ). // // PARAMETERS: pszNetworkName - Unique internal 'Zeidon' name of the // network. // ppHandle - Network pointer to buffer created in // zNetStartup. // ppvConnPtr - Connection pointer to buffer created in // zNetOpenConnection. // ppszReturnBuff - Pointer to a pointer that will point to the // retrieved data. // uLth - Number of bytes Core is expecting to receive. // cDataType - Specifies whether data is a string // (zTYPE_STRING) or a blob (zTYPE_BLOB). // NOTE: Ignored for WinSock implementation. // // RETURNS: 0 - Data received OK. // zCALL_ERROR - Error. // //*********************************************************************** int PASCAL zNetReceive( LPSTR pszNetworkName, LPVOID * ppHandle, LPVOID * ppvConnPtr, LPSTR * ppszReturnBuffer, UINT uLth, char cDataType ) { LONG lTickCount = GetTickCount( ); LPSOCKETS lpSockets = (LPSOCKETS) *ppHandle; LPCONNECTION lpConn = (LPCONNECTION) *ppvConnPtr; if ( lpConn->nTraceLevel > 1 ) { TraceLineI( "(zwinsock) zNetReceive -- uLth = ", uLth ); } // Check the flag to see if the last send/receive command was a send. If // the last command was a send, then the send buffer might need to be // flushed. if ( !lpConn->bLastReceive ) { if ( lpConn->nTraceLevel > 1 ) { TraceLineS( "(zwinsock) flushing send buffer", "" ); } if ( zNetSend( pszNetworkName, ppHandle, ppvConnPtr, 0, 0, 0 ) != 0 ) return( zCALL_ERROR ); lpConn->bLastReceive = TRUE; if ( lpConn->nTraceLevel > 1 ) { TraceLineS( "(zwinsock) Done flushing send buffer", "" ); } } // If the buffer lth is 0, then we need to fill it up with network data. if ( lpConn->usBufferLth == 0 ) { int iBytes; if ( lpConn->nTraceLevel > 0 ) { TraceLineS( "(zwinsock) Filling empty receive buffer", "" ); } iBytes = recv( lpConn->sock, lpConn->pszBuffer, BUFFER_LTH, 0 ); if ( iBytes == SOCKET_ERROR || iBytes == 0 ) { TraceLineS( "(zwinsock) Error filling receive buffer", "" ); fnShowError( "recv" ); return( zCALL_ERROR ); } if ( lpConn->nTraceLevel > 0 ) { TraceLineI( "(zwinsock) Bytes read = ", iBytes ); if ( lpConn->nTraceLevel > 1 ) { TraceLineS( "(zwinsock) Tracing buffer...", "" ); TraceBuffer( "(zwinsock)", lpConn->pszBuffer, iBytes ); } } lpConn->pszNextByte = lpConn->pszBuffer; lpConn->usBufferLth = iBytes; } // It is possible that the incoming line didn't fit into the current buffer // because the buffer contained multiple lines of data. This means that // we need to consolidate the data from buffer with data that still hasn't // been retrieved. if ( uLth > lpConn->usBufferLth ) { int iBytes; if ( lpConn->nTraceLevel > 0 ) { TraceLineS( "(zwinsock) Trying to consolidate receive buffer", "" ); } // Copy the unused data to the beginning of the buffer. This should // free up room at the end of the buffer to retrieve data off the network. memcpy( lpConn->pszBuffer, lpConn->pszNextByte, lpConn->usBufferLth ); // Now retrieve data from the network, appending it to the end of the // data in the buffer. iBytes = recv( lpConn->sock, &lpConn->pszBuffer[ lpConn->usBufferLth ], BUFFER_LTH - lpConn->usBufferLth, 0 ); if ( iBytes == SOCKET_ERROR || iBytes == 0 ) { fnShowError( "recv" ); TraceLineS( "(zwinsock) Error trying to extend buffer", "" ); return( zCALL_ERROR ); } if ( lpConn->nTraceLevel > 0 ) { TraceLineI( "(zwinsock) Bytes read = ", iBytes ); if ( lpConn->nTraceLevel > 1 ) { TraceLineS( "(zwinsock) Tracing buffer...", "" ); TraceBuffer( "(zwinsock)", &lpConn->pszBuffer[ lpConn->usBufferLth ], iBytes ); } } lpConn->pszNextByte = lpConn->pszBuffer; lpConn->usBufferLth += iBytes; // Check again to see if the buffer contains all the data. If not, // set error and get out. if ( uLth > lpConn->usBufferLth ) { TraceLineS( "(zwinsock) Error -- buffer can't hold data", "" ); return( zCALL_ERROR ); } } *ppszReturnBuffer = lpConn->pszNextByte; lpConn->pszNextByte += uLth; lpConn->usBufferLth -= uLth; lpConn->lReceiveTickCount += GetTickCount( ) - lTickCount; return( 0 ); } // zNetReceive
//*********************************************************************** // // ENTRY: zNetSend // // PURPOSE: To send a block of data over the network. // // DESCRIPTION: Zeidon Core call this operation when it needs to send data // over the network. // // PARAMETERS: pszNetworkName - Unique internal 'Zeidon' name of the // network. // ppHandle - Network pointer to buffer created in // zNetStartup. // ppvConnPtr - Connection pointer to buffer created in // zNetOpenConnection. // pszBuffer - Pointer to send buffer. If pszBuffer is 0, // then flush (send) any local buffer. // uLth - Length of buffer. If uLth is 0, then // pszBuffer points to a null-terminated str. // cDataType - Specifies whether data is a string // (zTYPE_STRING) or a blob (zTYPE_BLOB). // NOTE: Ignored for WinSock implementation. // // RETURNS: 0 - Data sent OK. // zCALL_ERROR - Error. // //*********************************************************************** int PASCAL zNetSend( LPSTR pszNetworkName, LPVOID * ppHandle, LPVOID * ppvConnPtr, LPSTR pszBuffer, UINT uLth, char cDataType ) { int iBytesToSend; int iBytesSent; LONG lTickCount = GetTickCount( ); LPSOCKETS lpSockets = (LPSOCKETS) *ppHandle; LPCONNECTION lpConn = (LPCONNECTION) *ppvConnPtr; if ( lpConn->nTraceLevel > 1 ) { TraceLineI( "(zwinsock) zNetSend -- uLth = ", uLth ); } // If the last command was a receive (as opposed to a send) then we need // to reset the buffer for sending. if ( lpConn->bLastReceive ) { if ( lpConn->nTraceLevel > 1 ) { TraceLineS( "(zwinsock) Resetting buffer for sending", "" ); } lpConn->bLastReceive = FALSE; lpConn->pszNextByte = lpConn->pszBuffer; lpConn->usBufferLth = 0; } // Check to see if we need to flush the current buffer. if ( ( pszBuffer == 0 && lpConn->usBufferLth > 0 ) || ( lpConn->usBufferLth + uLth > BUFFER_LTH ) ) { if ( lpConn->nTraceLevel > 1 ) { TraceLineI( "(zwinsock) Sending buffer of length ", lpConn->usBufferLth ); TraceBuffer( "(zwinsock)", lpConn->pszBuffer, lpConn->usBufferLth ); } iBytesToSend = lpConn->usBufferLth; do { iBytesSent = send( lpConn->sock, lpConn->pszBuffer, iBytesToSend, 0 ); if ( iBytesSent == SOCKET_ERROR ) { TraceLineS( "(zwinsock) send -- Error!", "" ); fnShowError( "send" ); return( zCALL_ERROR ); } if ( lpConn->nTraceLevel > 1 ) { TraceLineI( "(zwinsock) Bytes sent for send ", iBytesSent ); } iBytesToSend -= iBytesSent; } while ( iBytesToSend > 0 ); lpConn->usBufferLth = 0; } // If pszBuffer is non-zero, then we need to add the buffer data to the // send buffer. if ( pszBuffer ) { if ( lpConn->nTraceLevel > 1 ) { TraceLineS( "(zwinsock) Adding bytes to send buffer", "" ); } memcpy( &lpConn->pszBuffer[ lpConn->usBufferLth ], pszBuffer, uLth ); lpConn->usBufferLth += uLth; } lpConn->lSendTickCount += GetTickCount( ) - lTickCount; return( 0 ); } // zNetSend