/// Read a 2-byte integer from file (big endian) short MakeFont::ReadShort(wxInputStream* stream) { short i16; stream->Read(&i16, 2); return wxINT16_SWAP_ON_LE(i16); }
void *CudpdrvRxTread::Entry() { pcap_t *fp; char errbuf[ PCAP_ERRBUF_SIZE ]; uint8_t packet[ 512 ]; // First log on to the host and get configuration // variables if ( m_srv.doCmdOpen( m_pobj->m_host, m_pobj->m_port, m_pobj->m_username, m_pobj->m_password ) <= 0 ) { return NULL; } // Find the channel id uint32_t ChannelID; m_srv.doCmdGetChannelID( &ChannelID ); // We want to use our own Ethernet based GUID for this interface wxString strGUID; m_pobj->m_localGUIDrx.toString( strGUID ); m_srv.doCmdSetGUID( (char *)strGUID.ToAscii() ); // Open the adapter if ( (fp = pcap_open_live( m_pobj->m_interface.ToAscii(), // name of the device 65536, // portion of the packet to capture. It doesn't matter in this case 1, // promiscuous mode (nonzero means promiscuous) 1000, // read timeout errbuf // error buffer ) ) == NULL ) { //fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]); return NULL; } // Enter receive loop to start to log events m_srv.doCmdEnterReceiveLoop(); int rv; vscpEvent event; while ( !TestDestroy() && !m_pobj->m_bQuit ) { if ( CANAL_ERROR_SUCCESS == ( rv = m_srv.doCmdBlockReceive( &event, 1000 ) ) ) { // As we are on a different VSCP interface we need to filter the events we sent out // ourselves. if ( m_pobj->m_ChannelIDtx == event.obid ) { continue; } // Set mac destination to broadcast ff:ff:ff:ff:ff:ff packet[ 0 ] = 0xff; packet[ 1 ] = 0xff; packet[ 2 ] = 0xff; packet[ 3 ] = 0xff; packet[ 4 ] = 0xff; packet[ 5 ] = 0xff; // set mac source to configured value - 6..11 memcpy( packet + 6, m_pobj->m_localMac, 6 ); // Set the type - always 0x2574 (9598) packet[ 12 ] = 0x25; packet[ 13 ] = 0x7e; // rawEthernet frame version packet[ 14 ] = 0x00; // Head packet[ 15 ] = ( event.head & VSCP_HEADER_PRIORITY_MASK ); packet[ 16 ] = 0x00; packet[ 17 ] = 0x00; packet[ 18 ] = 0x00; // LSB // VSCP sub source address For this interface it's 0x0000 packet[ 19 ] = 0x00; packet[ 20 ] = 0x00; // Timestamp uint32_t timestamp = wxINT32_SWAP_ON_LE( event.timestamp ); packet[ 21 ] = ( timestamp & 0xff000000 ) >> 24; packet[ 22 ] = ( timestamp & 0x00ff0000 ) >> 16; packet[ 23 ] = ( timestamp & 0x0000ff00 ) >> 8; packet[ 24 ] = ( timestamp & 0x000000ff ); // obid uint32_t obid = wxINT32_SWAP_ON_LE( event.obid ); packet[ 25 ] = ( obid & 0xff000000 ) >> 24; packet[ 26 ] = ( obid & 0x00ff0000 ) >> 16; packet[ 27 ] = ( obid & 0x0000ff00 ) >> 8; packet[ 28 ] = ( obid & 0x000000ff ); // VSCP Class uint16_t vscp_class = wxINT16_SWAP_ON_LE( event.vscp_class ); packet[ 29 ] = ( vscp_class & 0xff00 ) >> 8; packet[ 30 ] = ( vscp_class & 0xff ); // VSCP Type uint16_t vscp_type = wxINT16_SWAP_ON_LE( event.vscp_type ); packet[ 31 ] = ( vscp_type & 0xff00 ) >> 8; packet[ 32 ] = ( vscp_class & 0xff ); // Size packet[ 33 ] = event.sizeData >> 8; packet[ 34 ] = event.sizeData & 0xff; // VSCP Data for ( int idx=0; idx < event.sizeData; idx++ ) { packet[ 35 + idx ] = event.pdata[ idx ]; } // Send the packet if ( 0 != pcap_sendpacket( fp, packet, 32 + 1 + event.sizeData ) ) { //fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp)); // An error sending the frame - we do nothing // TODO: Send error frame back to daemon???? } // We are done with the event - remove data if any if ( NULL != event.pdata ) { delete [] event.pdata; event.pdata = NULL; } } // Event received } // work loop // Close the ethernet interface pcap_close( fp ); // Close the channel m_srv.doCmdClose(); return NULL; }
void Extract(bool bits16, bool sign, bool stereo, bool bigendian, bool offset, char *rawData, int dataSize, float *data1, float *data2, int *len1, int *len2) { int rawCount = 0; int dataCount1 = 0; int dataCount2 = 0; int i; *len1 = 0; *len2 = 0; if (offset && bits16) { /* Special case so as to not flip stereo channels during analysis */ if (stereo && !bigendian) { rawData += 3; dataSize -= 3; } else { rawData++; dataSize--; } } if (bits16) { if (sign && bigendian) while (rawCount + 1 < dataSize) { /* 16-bit signed BE */ data1[dataCount1] = (wxINT16_SWAP_ON_LE(*((signed short *) &rawData[rawCount]))) / 32768.0; dataCount1++; rawCount += 2; } if (!sign && bigendian) while (rawCount + 1 < dataSize) { /* 16-bit unsigned BE */ data1[dataCount1] = (wxUINT16_SWAP_ON_LE(*((unsigned short *) &rawData[rawCount]))) / 32768.0 - 1.0; dataCount1++; rawCount += 2; } if (sign && !bigendian) while (rawCount + 1 < dataSize) { /* 16-bit signed LE */ data1[dataCount1] = (wxINT16_SWAP_ON_BE(*((signed short *) &rawData[rawCount]))) / 32768.0; dataCount1++; rawCount += 2; } if (!sign && !bigendian) while (rawCount + 1 < dataSize) { /* 16-bit unsigned LE */ data1[dataCount1] = (wxUINT16_SWAP_ON_BE(*((unsigned short *) &rawData[rawCount]))) / 32768.0 - 1.0; dataCount1++; rawCount += 2; } } else { /* 8-bit */ if (sign) { while (rawCount < dataSize) { /* 8-bit signed */ data1[dataCount1++] = (*(signed char *) (&rawData[rawCount++])) / 128.0; } } else { while (rawCount < dataSize) { /* 8-bit unsigned */ data1[dataCount1++] = (*(unsigned char *) &rawData[rawCount++]) / 128.0 - 1.0; } } } if (stereo) { dataCount1 /= 2; for(i=0; i<dataCount1; i++) { data2[i] = data1[2*i+1]; data1[i] = data1[2*i]; } dataCount2 = dataCount1; } *len1 = dataCount1; *len2 = dataCount2; }