void dumpBuffer(unsigned char *buffer) { dumpBuffer(buffer, str_len((char *)buffer)); }
int main(void) { initUSART(); _delay_ms(200); uint8_t i; uint8_t tempCharStorage; while (1) { dumpBuffer(); printString(" Demo: adding characters to buffer\n"); uint8_t coolString[] = "Howdy"; i = 0; while(i < sizeof(coolString) - 1){ bufferWrite(coolString[i]); ++i; dumpBuffer(); } printString(" Demo: reading out the first three characters\n"); for (i = 0; i<3 ; i++){ bufferRead(&tempCharStorage); transmitByte(tempCharStorage); printString("\n"); dumpBuffer(); } printString(" Demo: adding more characters to buffer\n"); uint8_t anotherString[] = "Hello"; i = 0; while(i < sizeof(anotherString) - 1){ bufferWrite(anotherString[i]); ++i; dumpBuffer(); } // And read it back out using return code printString(" Demo: reading everything back out\n"); while (bufferRead(&tempCharStorage) == BUFFER_OK){ transmitByte(tempCharStorage); } printString("\n"); dumpBuffer(); printString(" Demo: empty! (newest = oldest)\n"); // Fill up buffer, using return code i = 0; while(bufferWrite('A'+i) == BUFFER_OK){ ++i; dumpBuffer(); } printString(" Note: never fills up whole buffer\n"); printString(" it's full when first index equals last\n"); return 0; } /* End event loop */ return 0; /* This line is never reached */ }
// Test harness, small images // !!! Here Alpha FF is total opacity. Alpha 0 is total transparency. int main( ) { // A 3x3 image where the center pixel will be synthesized unsigned char image[42] = { 0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0, // 3*RGBA and 2 trailing pad byte = 14 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0, 0,0,0,0, 0,0,0,0, 0,0,0,8, 0,0 }; unsigned char mask[12] = { 0,0, 0, 0, // 3*mask and trailing pad byte = 4 0,0xFF, 0, 0, // 0xFF == totally selected 0,0, 0, 0 }; // A 1x3 image where the middle pixel will be synthesized. // First pixel opaque // Third pixel transparent unsigned char image2[14] = { 128,128,128,0xFF, 1,1,1,1, 0,0,0,0, 0,0 // 3*RGBA and 2 trailing pad byte = 14 }; unsigned char mask2[4] = { 0,0xFF,0, 0 // 3*mask and 1 trailing pad byte = 4 }; unsigned char mask3[8] = { 0, 0,0, 0, // 3*mask and 1 trailing pad byte = 4 0,0xFF,0, 0 }; unsigned char maskEmpty[4] = { 0,0,0, 0 // 3*mask and 1 trailing pad byte = 4 }; // A 1x3 image where the all pixel transparent and the middle pixel will be synthesized. unsigned char image3[14] = { 128,128,128,0, 1,1,1,1, 0,0,0,0, 0,0 // 3*RGBA and 2 trailing pad byte }; // A 2x3 RGB image unsigned char imageRGB[22] = { 128,128,128, 1,1,1, 2,2,2, 5,5, // 3*RGB and 2 trailing pad byte = 11 64,64,64, 4,4,4, 3,3,3, 7,7 }; // 1x3 Grey alpha // First pixel opaque, second to be synthesized, third transparent unsigned char imageGrayA[8] = { 128,0xFF, 64,1, 1,0, 0,0 // 3*GA and 2 trailing pad byte = 8 }; // 1x3 Grey unsigned char imageGray[5] = { 128, 64, 1, 0,0 // 3*GA and 2 trailing pad byte = 8 }; // !!! Note width, height, rowBytes in that order ImageBuffer testImage = { (unsigned char*) &image, 3, 3, 14 }; ImageBuffer testImage2 = { (unsigned char*) &image2, 3, 1, 14 }; ImageBuffer testImage3 = { (unsigned char*) &image3, 3, 1, 14 }; ImageBuffer testImageRGB = { (unsigned char*) &imageRGB, 3, 2, 11 }; ImageBuffer testImageGrayA = { (unsigned char*) &imageGrayA, 3, 1, 8 }; ImageBuffer testImageGray = { (unsigned char*) &imageGray, 3, 1, 5 }; ImageBuffer testMask= { (unsigned char*) &mask, 3, 3, 4 }; ImageBuffer testMask2= { (unsigned char*) &mask2, 3, 1, 4 }; ImageBuffer testMask3= { (unsigned char*) &mask3, 3, 2, 4 }; ImageBuffer testMaskBad= { (unsigned char*) &mask3, 1, 1, 1 }; ImageBuffer testMaskEmpty= { (unsigned char*) &maskEmpty, 3, 1, 4 }; TImageSynthParameters parameters; setDefaultParams(¶meters); { // !!!! Set this to 1 to test cancellation !!!! int cancelFlag = 0; printf("\nTest center pixel synthesized but alpha unchanged (OR test cancellation).\n"); printf("Before\n"); dumpBuffer(&testImage, 4); imageSynth(&testImage, &testMask, T_RGBA, ¶meters, progressCallback, (void*) 0, &cancelFlag); } // dumpImage(45); // 45 pixelels 3x3x5 printf("After\n"); dumpBuffer(&testImage, 4); /* (1,1) should be changed to 0,0,0,1 i.e. the alpha byte 1 should not be changed but the color should be 0,0,0 from unmasked and partially opaque region. Other pixels are not changed. */ // Note these tests destroy the input image, which can't be used twice test("Test mix of full transparency and opaque", &testImage2, &testMask2, T_RGBA, 4, "80 80 80 ff 80 80 80 01 00 00 00 00", ¶meters); test("Test RGB w/o alpha", &testImageRGB, &testMask3, T_RGB, 3, "80 80 80 01 01 01 02 02 02\n40 40 40 01 01 01 03 03 03", ¶meters); test("Test Gray w/ alpha", &testImageGrayA, &testMask2, T_GrayA, 2, "80 ff 80 01 01 00", ¶meters); // Also tests passing NULL parameters for defaults test("Test Gray w/o alpha", &testImageGray, &testMask2, T_Gray, 1, "80 01 01", (TImageSynthParameters*) NULL); // Tests of error conditions. // Errors that don't leak memory. // Rest of tests should return errors. test("All transparent", &testImage3, &testMask2, T_RGBA, 4, "should be unchanged.", ¶meters); parameters.patchSize = 65; test("patchSize parameter out of range", &testImageGray, &testMask2, T_Gray, 1, "80 01 01", ¶meters); parameters.patchSize = 10; test("mask not same size as image", &testImageGray, &testMaskBad, T_Gray, 1, "80 01 01", ¶meters); test("target empty (mask empty)", &testImageGray, &testMaskEmpty, T_Gray, 1, "80 01 01", ¶meters); return(0); // The rest are programming errors that leak test("Invalid image format", &testImageGray, &testMask2, 666, 1, "80 01 01", ¶meters); // TODO matchContextType; out of range IMAGE_SYNTH_ERROR_MATCH_CONTEXT_TYPE_RANGE return(0); }
//////////////////////////////////////////////////////////////////////////////// //! Run the Cuda part of the computation //////////////////////////////////////////////////////////////////////////////// void computeIsosurface() { int threads = 128; dim3 grid(numVoxels / threads, 1, 1); // get around maximum grid size of 65535 in each dimension if (grid.x > 65535) { grid.y = grid.x / 32768; grid.x = 32768; } // calculate number of vertices need per voxel launch_classifyVoxel(grid, threads, d_voxelVerts, d_voxelOccupied, d_volume, gridSize, gridSizeShift, gridSizeMask, numVoxels, voxelSize, isoValue); #if DEBUG_BUFFERS printf("voxelVerts:\n"); dumpBuffer(d_voxelVerts, numVoxels, sizeof(uint)); #endif #if SKIP_EMPTY_VOXELS // scan voxel occupied array cudppScan(scanplan, d_voxelOccupiedScan, d_voxelOccupied, numVoxels); #if DEBUG_BUFFERS printf("voxelOccupiedScan:\n"); dumpBuffer(d_voxelOccupiedScan, numVoxels, sizeof(uint)); #endif // read back values to calculate total number of non-empty voxels // since we are using an exclusive scan, the total is the last value of // the scan result plus the last value in the input array { uint lastElement, lastScanElement; cutilSafeCall(cudaMemcpy((void *) &lastElement, (void *) (d_voxelOccupied + numVoxels-1), sizeof(uint), cudaMemcpyDeviceToHost)); cutilSafeCall(cudaMemcpy((void *) &lastScanElement, (void *) (d_voxelOccupiedScan + numVoxels-1), sizeof(uint), cudaMemcpyDeviceToHost)); activeVoxels = lastElement + lastScanElement; } if (activeVoxels==0) { // return if there are no full voxels totalVerts = 0; return; } // compact voxel index array launch_compactVoxels(grid, threads, d_compVoxelArray, d_voxelOccupied, d_voxelOccupiedScan, numVoxels); cutilCheckMsg("compactVoxels failed"); #endif // SKIP_EMPTY_VOXELS // scan voxel vertex count array cudppScan(scanplan, d_voxelVertsScan, d_voxelVerts, numVoxels); #if DEBUG_BUFFERS printf("voxelVertsScan:\n"); dumpBuffer(d_voxelVertsScan, numVoxels, sizeof(uint)); #endif // readback total number of vertices { uint lastElement, lastScanElement; cutilSafeCall(cudaMemcpy((void *) &lastElement, (void *) (d_voxelVerts + numVoxels-1), sizeof(uint), cudaMemcpyDeviceToHost)); cutilSafeCall(cudaMemcpy((void *) &lastScanElement, (void *) (d_voxelVertsScan + numVoxels-1), sizeof(uint), cudaMemcpyDeviceToHost)); totalVerts = lastElement + lastScanElement; } // generate triangles, writing to vertex buffers if (!g_bQAReadback) { size_t num_bytes; // DEPRECATED: cutilSafeCall(cudaGLMapBufferObject((void**)&d_pos, posVbo)); cutilSafeCall(cudaGraphicsMapResources(1, &cuda_posvbo_resource, 0)); cutilSafeCall(cudaGraphicsResourceGetMappedPointer((void**)&d_pos, &num_bytes, cuda_posvbo_resource)); // DEPRECATED: cutilSafeCall(cudaGLMapBufferObject((void**)&d_normal, normalVbo)); cutilSafeCall(cudaGraphicsMapResources(1, &cuda_normalvbo_resource, 0)); cutilSafeCall(cudaGraphicsResourceGetMappedPointer((void**)&d_normal, &num_bytes, cuda_normalvbo_resource)); } #if SKIP_EMPTY_VOXELS dim3 grid2((int) ceil(activeVoxels / (float) NTHREADS), 1, 1); #else dim3 grid2((int) ceil(numVoxels / (float) NTHREADS), 1, 1); #endif while(grid2.x > 65535) { grid2.x/=2; grid2.y*=2; } #if SAMPLE_VOLUME launch_generateTriangles2(grid2, NTHREADS, d_pos, d_normal, d_compVoxelArray, d_voxelVertsScan, d_volume, gridSize, gridSizeShift, gridSizeMask, voxelSize, isoValue, activeVoxels, maxVerts); #else launch_generateTriangles(grid2, NTHREADS, d_pos, d_normal, d_compVoxelArray, d_voxelVertsScan, gridSize, gridSizeShift, gridSizeMask, voxelSize, isoValue, activeVoxels, maxVerts); #endif if (!g_bQAReadback) { // DEPRECATED: cutilSafeCall(cudaGLUnmapBufferObject(normalVbo)); cutilSafeCall(cudaGraphicsUnmapResources(1, &cuda_normalvbo_resource, 0)); // DEPRECATED: cutilSafeCall(cudaGLUnmapBufferObject(posVbo)); cutilSafeCall(cudaGraphicsUnmapResources(1, &cuda_posvbo_resource, 0)); } }
// LCOV_EXCL_START void displaySqlBuffer(SqlBuffer *sbuf, Lng32 sbuflen, ostream &os) { os << "Display an SQL Buffer:" << endl; os << " Buffer Size : " << sbuf->get_buffer_size() << endl; os << " Used Size : " << sbuf->get_used_size() << endl; os << " Free Size : " << (sbuf->get_buffer_size() - sbuf->get_used_size()) << endl; switch (sbuf->bufType()) { case SqlBufferBase::NORMAL_: os << " Buffer Type : Normal" << endl; break; case SqlBufferBase::DENSE_: os << " Buffer Type : Dense" << endl; break; case SqlBufferBase::OLT_: os << " Buffer Type : OLT" << endl; break; default: os << " Buffer Type : Unknown (" << sbuf->bufType() << ")" << endl; break; } os << " Packed? : " << TF_STRING(sbuf->packed()) << endl; os << " Empty? : " << TF_STRING(sbuf->isFull()) << endl; os << " Full? : " << TF_STRING(sbuf->isEmpty()) << endl; os << " Free? : " << TF_STRING(sbuf->isFree()) << endl; Lng32 numtuppdescs = sbuf->getTotalTuppDescs(); os << " Total Tupp Descs : " << numtuppdescs << endl; os << " Processed Tupp Descs : " << sbuf->getProcessedTuppDescs() << endl; #ifdef SQL_BUFFER_SIGNATURE char csig[100]; convertInt64ToAscii(sbuf->getSignature(), csig); os << " Signature : " << csig << endl; #endif tupp_descriptor *td; char *ctupp; ControlInfo *ci; for (Lng32 i = 1; i <= numtuppdescs; i++) { td = sbuf->getTuppDescriptor(i); if (td == NULL) { break; } ctupp = td->getTupleAddress(); if (td->isDataTuple()) { ServerDebug(""); ServerDebug("Data Tuple:"); os << " Tupp(" << i << ") Ref Count : " << td->getReferenceCount() << endl; os << " Tupp(" << i << ") Allocated Size: " << td->getAllocatedSize() << endl; dumpBuffer((unsigned char *)ctupp, td->getAllocatedSize()); } else if (td->isStatsTuple()) { ServerDebug(""); ServerDebug("Stats Tuple:"); os << " Tupp(" << i << ") Ref Count : " << td->getReferenceCount() << endl; os << " Tupp(" << i << ") Allocated Size: " << td->getAllocatedSize() << endl; } else if (td->isDiagsTuple()) { ServerDebug(""); ServerDebug("Diags Tuple:"); os << " Tupp(" << i << ") Ref Count : " << td->getReferenceCount() << endl; os << " Tupp(" << i << ") Allocated Size: " << td->getAllocatedSize() << endl; } else if (td->isControlTuple()) { ServerDebug(""); ServerDebug("Control Tuple:"); os << " Tupp(" << i << ") Ref Count : " << td->getReferenceCount() << endl; os << " Tupp(" << i << ") Allocated Size: " << td->getAllocatedSize() << endl; ci = (ControlInfo *)td->getTupleAddress(); up_state us = ci->getUpState(); down_state ds = ci->getDownState(); os << " Tupp(" << i << ") UpState : " << endl; os << " Tupp(" << i << ") Parent Index : " << us.parentIndex << endl; os << " Tupp(" << i << ") Down Index : " << us.downIndex << endl; switch (us.status) { case ex_queue::Q_NO_DATA: { os << " Tupp(" << i << ") Status : Q_NO_DATA" << endl; break; } case ex_queue::Q_OK_MMORE: { os << " Tupp(" << i << ") Status : Q_OK_MMORE" << endl; break; } case ex_queue::Q_SQLERROR: { os << " Tupp(" << i << ") Status : Q_SQLERROR" << endl; break; } case ex_queue::Q_INVALID: { os << " Tupp(" << i<< ") Status : Q_INVALID" << endl; break; } case ex_queue::Q_GET_DONE: { os << " Tupp(" << i << ") Status : Q_GET_DONE" << endl; break; } default: { os << " Tupp(" << i << ") Request : Unknown (" << us.status << ")" << endl; break; } } os << " Tupp(" << i << ") Match Nbr : " << us.getMatchNo() << endl; os << " Tupp(" << i << ") DownState : " << endl; switch (ds.request) { case ex_queue::GET_N: { os << " Tupp(" << i << ") Request : GET_N" << endl; break; } case ex_queue::GET_ALL: { os << " Tupp(" << i << ") Request : GET_ALL" << endl; break; } case ex_queue::GET_NOMORE: { os << " Tupp(" << i << ") Request : GET_NOMORE" << endl; break; } case ex_queue::GET_EMPTY: { os << " Tupp(" << i << ") Request : GET_EMPTY" << endl; break; } case ex_queue::GET_EOD: { os << " Tupp(" << i << ") Request : GET_EOD" << endl; break; } case ex_queue::GET_NEXT_N: { os << " Tupp(" << i << ") Request : GET_NEXT_N" << endl; break; } case ex_queue::GET_NEXT_N_MAYBE_WAIT: { os << " Tupp(" << i << ") Request : GET_NEXT_N_MAYBE_WAIT" << endl; break; } case ex_queue::GET_NEXT_0_MAYBE_WAIT: { os << " Tupp(" << i << ") Request : GET_NEXT_0_MAYBE_WAIT" << endl; break; } default: { os << " Tupp(" << i << ") Request : Unknown (" << ds.request << ")" << endl; break; } } os << " Tupp(" << i << ") Request : " << ds.request << endl; os << " Tupp(" << i << ") Request Value : " << ds.requestValue << endl; os << " Tupp(" << i << ") Nbr Get Nexts : " << ds.numGetNextsIssued << endl; os << " Tupp(" << i << ") Parent Index : " << ds.parentIndex << endl; os << " Tupp(" << i << ") Buff Seq Nbr : " << ci->getBufferSequenceNumber() << endl; os << " Tupp(" << i << ") Diags Area? : " << TF_STRING(ci->getIsDiagsAreaPresent()) << endl; os << " Tupp(" << i << ") Ext Diags Area? : " << TF_STRING(ci->getIsExtDiagsAreaPresent()) << endl; os << " Tupp(" << i << ") Data Row? : " << TF_STRING(ci->getIsDataRowPresent()) << endl; os << " Tupp(" << i << ") Stats Area? : " << TF_STRING(ci->getIsStatsAreaPresent()) << endl; os << " Tupp(" << i << ") ExtStats Area? : " << TF_STRING(ci->getIsExtStatsAreaPresent()) << endl; } // if (td->isControlTuple()) else { // some other type of tuple... ServerDebug(""); ServerDebug("Unknown Tuple:"); os << " Tupp(" << i << ") Ref Count : " << td->getReferenceCount() << endl; os << " Tupp(" << i << ") Allocated Size: " << td->getAllocatedSize() << endl; } } // for (long i = 1; i <= numtuppdescs; i++) os << endl; } // displaySqlBuffer
void Connection::handleReceive( const ConnectionPtr& cnx, SharedBuffer& buffer, bool isFirstPart, const sys::error_code& error, size_t size ) { if ( error || size == 0 ) { TRACE( "Error reading data: " << error.message() ); return; } if ( isFirstPart ) { uint32_t msgSize = readMessageSize( buffer ); TRACE( "Msg size: " << msgSize << " -- Read Size: " << size ); if ( msgSize > size ) { // Schedule a complete read buffer.resize( msgSize ); asio::async_read( m_socket, asio::buffer( buffer.data() + size, msgSize - size ), bind( &Connection::handleReceive, this, cnx, buffer, false, ph::error, ph::bytes_transferred ) ); return; } } TRACE( "Got the complete message" ); /// Do something.... Message msg; readMessageWithSize( buffer, msg ); switch ( msg.type() ) { case Message::NodeList: { TRACE( "Node list received: " ); m_membership.receivedNodeList( msg.nodelist() ); receiveMessage(); break; } case Message::Ping: { TRACE( "Ping msg received" ); SharedBuffer buffer; if ( !msg.ping().has_nodelist() ) buffer = CreatePongMsg(); else { m_membership.receivedNodeList( msg.ping().nodelist() ); DEBUG( "Resending the node list with pong message" ); buffer = CreatePongMsg( m_membership.nodeList() ); } asio::async_write( m_socket, buffer, bind( &Connection::handleSend, this, cnx, ph::error, ph::bytes_transferred ) ); break; } default: { ERROR( "Invalid msg received." ); dumpBuffer( std::cerr, buffer.data(), buffer.size() ); break; } } }
static int sip_startline_parse(SIPMsg *msg, const char *buff, char *end, char **lineEnd) { char *next; char *start; int length; int numOfLineBreaks; start = (char *) buff; numOfLineBreaks = sip_find_linebreak(start, end, &next); if (numOfLineBreaks < 1) { /*No CRLF */ DEBUG_WRAP(DebugMessage(DEBUG_SIP, "No CRLF, check failed\n")); return SIP_FAILURE; } /*Exclude CRLF from start line*/ length = next - start - numOfLineBreaks; DEBUG_WRAP(DebugMessage(DEBUG_SIP, "Start line: %.*s \n", length, start)); DEBUG_WRAP(DebugMessage(DEBUG_SIP, "End of Start line \n")); /*Should at least have SIP/2.0 */ if (length < SIP_MIN_MSG_LEN) { DEBUG_WRAP(DebugMessage(DEBUG_SIP, "Message too short, check failed\n")); return SIP_FAILURE; } *lineEnd = next; // This is a response if (0 == strncmp((const char *) buff, (const char *) SIP_KEYWORD, SIP_KEYWORD_LEN)) { char *space; unsigned long statusCode; /*Process response*/ msg->method = NULL; msg->uri = NULL; /*Check SIP version number, end with SP*/ if (!(sip_is_valid_version(buff + SIP_KEYWORD_LEN) && (*(buff + SIP_VERSION_LEN) == ' '))) { ALERT(SIP_EVENT_INVALID_VERSION,SIP_EVENT_INVALID_VERSION_STR); } space = strchr(buff, ' '); if (space == NULL) return SIP_FAILURE; statusCode = _dpd.SnortStrtoul(space + 1, NULL, 10); if (( statusCode > MAX_STAT_CODE) || (statusCode < MIN_STAT_CODE )) { ALERT(SIP_EVENT_BAD_STATUS_CODE,SIP_EVENT_BAD_STATUS_CODE_STR) msg->status_code = MAX_STAT_CODE + 1; } else msg->status_code = (uint16_t)statusCode; DEBUG_WRAP(DebugMessage(DEBUG_SIP, "Status code: %d \n", msg->status_code)); } else /* This might be a request*/ { char *space; char *version; int length; SIPMethodNode *method; /*Process request*/ if (NULL ==sip_eval_config) return SIP_FAILURE; msg->status_code = 0; // Parse the method space = memchr(buff, ' ', end - buff); if (space == NULL) return SIP_FAILURE; length = space - buff; msg->method = (char*)buff; msg->methodLen = length; DEBUG_WRAP(DebugMessage(DEBUG_SIP, "method: %.*s\n", msg->methodLen, msg->method)); #ifdef DUMP_BUFFER dumpBuffer(METHOD_DUMP,msg->method,msg->methodLen); #endif method = SIP_FindMethod (sip_eval_config->methods, msg->method, msg->methodLen); if (method) { msg->methodFlag = method->methodFlag; DEBUG_WRAP(DebugMessage(DEBUG_SIP, "Found the method: %s, Flag: 0x%x\n", method->methodName, method->methodFlag)); } // parse the uri if (space + 1 > end) return SIP_FAILURE; msg->uri = space + 1; space = memchr(space + 1, ' ', end - msg->uri); if (space == NULL) return SIP_FAILURE; msg->uriLen = space - msg->uri; #ifdef DUMP_BUFFER dumpBuffer(URI_DUMP,msg->uri,msg->uriLen); #endif DEBUG_WRAP(DebugMessage(DEBUG_SIP, "uri: %.*s, length: %u\n", msg->uriLen, msg->uri, msg->uriLen)); if(0 == msg->uriLen) ALERT(SIP_EVENT_EMPTY_REQUEST_URI,SIP_EVENT_EMPTY_REQUEST_URI_STR) else if (sip_eval_config->maxUriLen && (msg->uriLen > sip_eval_config->maxUriLen)) ALERT(SIP_EVENT_BAD_URI,SIP_EVENT_BAD_URI_STR); version = space + 1; if (version + SIP_VERSION_LEN > end) return SIP_FAILURE; if (0 != strncmp((const char *) version, (const char *) SIP_KEYWORD, SIP_KEYWORD_LEN)) return SIP_FAILURE; /*Check SIP version number, end with CRLF*/ if (!sip_is_valid_version(*lineEnd - SIP_VERSION_NUM_LEN - numOfLineBreaks)) { ALERT(SIP_EVENT_INVALID_VERSION,SIP_EVENT_INVALID_VERSION_STR); } if (NULL == method) { ALERT(SIP_EVENT_UNKOWN_METHOD, SIP_EVENT_UNKOWN_METHOD_STR); return SIP_FAILURE; } } return SIP_SUCCESS; }
int main() { unsigned char cBufWrite[BUF_SIZE]; unsigned char * pcBufRead = NULL; char * pcBufLD[MAX_DEVICES + 1]; char cBufLD[MAX_DEVICES][64]; DWORD dwRxSize = 0, dwRxSizeTemp = 0; DWORD dwBytesWritten, dwBytesRead; FT_STATUS ftStatus; FT_HANDLE ftHandle[MAX_DEVICES]; int iNumDevs = 0; int i, j; int iDevicesOpen = 0; printf("Program start.\n"); for (i = 0; i < MAX_DEVICES; i++) { pcBufLD[i] = cBufLD[i]; } pcBufLD[MAX_DEVICES] = NULL; FT_SetVIDPID(1027, 59530); // use our VID and PID ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER); if (ftStatus != FT_OK) { printf("Error: FT_ListDevices(%d)\n", (int) ftStatus); return 1; } for (i = 0; ((i < MAX_DEVICES) && (i < iNumDevs)); i++) { printf("Device %d Serial Number - %s\n", i, cBufLD[i]); } for (j = 0; j < BUF_SIZE; j++) { cBufWrite[j] = j; } printf("Number of devices: %d.\n", iNumDevs); for (i = 0; ((i < MAX_DEVICES) && (i < iNumDevs)); i++) { /* Setup */ if ((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK) { /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("Error FT_OpenEx(%d), device %d\n", (int) ftStatus, i); printf("Use lsmod to check if ftdi_sio (and usbserial) are present.\n"); printf("If so, unload them using rmmod, as they conflict with ftd2xx.\n"); return 1; } printf("Opened device %s\n", cBufLD[i]); iDevicesOpen++; if ((ftStatus = FT_SetBaudRate(ftHandle[i], 9600)) != FT_OK) { printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", (int) ftStatus, cBufLD[i]); break; } // printf("Calling FT_Write with this write-buffer:\n"); // dumpBuffer(cBufWrite, BUF_SIZE, 1); /* Write */ /* ftStatus = FT_Write(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesWritten); if (ftStatus != FT_OK) { printf("Error FT_Write(%d)\n", (int)ftStatus); break; } if (dwBytesWritten != (DWORD)BUF_SIZE) { printf("FT_Write only wrote %d (of %d) bytes\n", (int)dwBytesWritten, BUF_SIZE); break; } sleep(1); */ /* Read */ // load buffer dwRxSize = 0; dwRxSizeTemp = 0; printf("Fill Buffer (Size: %d)...\n", BUF_SIZE); while ((dwRxSize < BUF_SIZE) && (ftStatus == FT_OK)) { ftStatus = FT_GetQueueStatus(ftHandle[i], &dwRxSize); // show progress if (dwRxSize > dwRxSizeTemp) { dwRxSizeTemp = dwRxSize; printf("Buffer %d/%d\n", dwRxSize, BUF_SIZE); } } // buffer is filled if (ftStatus == FT_OK) { // preset buffer pcBufRead = realloc(pcBufRead, dwRxSize); memset(pcBufRead, 0xFF, dwRxSize); /* printf("Calling FT_Read with this read-buffer (prefilled, initializied):\n"); dumpBuffer(pcBufRead, dwRxSize, 1); */ ftStatus = FT_Read(ftHandle[i], pcBufRead, dwRxSize, &dwBytesRead); if (ftStatus != FT_OK) { printf("Error FT_Read(%d)\n", (int) ftStatus); break; } if (dwBytesRead != dwRxSize) { printf("FT_Read only read %d (of %d) bytes\n", (int) dwBytesRead, (int) dwRxSize); break; } printf("FT_Read read %d bytes. Read-buffer is now:\n", (int) dwBytesRead); printf("list in hex"); dumpBuffer(pcBufRead, (int) dwBytesRead, 1); printf("list in hex"); dumpBuffer(pcBufRead, (int) dwBytesRead, 2); printf("list in dec"); dumpBuffer(pcBufRead, (int) dwBytesRead, 3); printf("list in combined"); dumpBuffer(pcBufRead, (int) dwBytesRead, 4); /* if (0 != memcmp(cBufWrite, pcBufRead, BUF_SIZE)) { printf("Error: read-buffer does not match write-buffer.\n"); break; } printf("%s test passed.\n", cBufLD[i]); */ } else { printf("Error FT_GetQueueStatus(%d)\n", (int) ftStatus); } } iDevicesOpen = i; /* Cleanup */ for (i = 0; i < iDevicesOpen; i++) { FT_Close(ftHandle[i]); printf("Closed device %s\n", cBufLD[i]); } if (pcBufRead) free(pcBufRead); printf("Program end.\n"); return 0; }