bool BridgeManager::ReceiveDump(int chipType, int chipId, FILE *file) const { bool success; // Start file transfer BeginDumpPacket beginDumpPacket(chipType, chipId); success = SendPacket(&beginDumpPacket); if (!success) { InterfaceManager::PrintError("Failed to request dump!\n"); return (false); } DumpResponse dumpResponse; success = ReceivePacket(&dumpResponse); unsigned int dumpSize = dumpResponse.GetDumpSize(); if (!success) { InterfaceManager::PrintError("Failed to receive dump size!\n"); return (false); } unsigned int transferCount = dumpSize / ReceiveFilePartPacket::kDataSize; if (transferCount % ReceiveFilePartPacket::kDataSize != 0) transferCount++; vector<char> buffer(kDumpBufferSize * ReceiveFilePartPacket::kDataSize, 0); for (unsigned int i = 0; i < transferCount; i++) { DumpPartFileTransferPacket dumpPartPacket(i); success = SendPacket(&dumpPartPacket); if (!success) { InterfaceManager::PrintError("Failed to request dump part #%i!\n", i); return (false); } ReceiveFilePartPacket receiveFilePartPacket; success = ReceivePacket(&receiveFilePartPacket); if (!success) { InterfaceManager::PrintError("Failed to receive dump part #%i!\n", i); continue; return (true); } if (buffer.size() + receiveFilePartPacket.GetReceivedSize() > kDumpBufferSize * ReceiveFilePartPacket::kDataSize) { // Write the buffer to the output file fwrite(&buffer.front(), 1, buffer.size(), file); buffer.clear(); } // Copy the packet data into pitFile. unsigned char* pDataBegin = receiveFilePartPacket.GetData(); unsigned char* pDataEnd = receiveFilePartPacket.GetData() + receiveFilePartPacket.GetReceivedSize(); buffer.insert(buffer.end(), pDataBegin, pDataEnd); } if (!buffer.empty()) { // Write the buffer to the output file fwrite(&buffer.front(), 1, buffer.size(), file); } // End file transfer FileTransferPacket fileTransferPacket(FileTransferPacket::kRequestEnd); success = SendPacket(&fileTransferPacket); if (!success) { InterfaceManager::PrintError("Failed to send request to end dump transfer!\n"); return (false); } ResponsePacket responsePacket(ResponsePacket::kResponseTypeFileTransfer); success = ReceivePacket(&responsePacket); if (!success) { InterfaceManager::PrintError("Failed to receive end dump transfer verification!\n"); return (false); } return (true); }
bool BridgeManager::ReceiveDump(unsigned int chipType, unsigned int chipId, FILE *file) const { bool success; // Start file transfer BeginDumpPacket *beginDumpPacket = new BeginDumpPacket(chipType, chipId); success = SendPacket(beginDumpPacket); delete beginDumpPacket; if (!success) { Interface::PrintError("Failed to request dump!\n"); return (false); } DumpResponse *dumpResponse = new DumpResponse(); success = ReceivePacket(dumpResponse); unsigned int dumpSize = dumpResponse->GetDumpSize(); delete dumpResponse; if (!success) { Interface::PrintError("Failed to receive dump size!\n"); return (false); } unsigned int transferCount = dumpSize / ReceiveFilePartPacket::kDataSize; if (transferCount % ReceiveFilePartPacket::kDataSize != 0) transferCount++; char *buffer = new char[kDumpBufferSize * ReceiveFilePartPacket::kDataSize]; unsigned int bufferOffset = 0; for (unsigned int i = 0; i < transferCount; i++) { DumpPartFileTransferPacket *dumpPartPacket = new DumpPartFileTransferPacket(i); success = SendPacket(dumpPartPacket); delete dumpPartPacket; if (!success) { Interface::PrintError("Failed to request dump part #%d!\n", i); delete [] buffer; return (false); } ReceiveFilePartPacket *receiveFilePartPacket = new ReceiveFilePartPacket(); success = ReceivePacket(receiveFilePartPacket); if (!success) { Interface::PrintError("Failed to receive dump part #%d!\n", i); continue; delete receiveFilePartPacket; delete [] buffer; return (true); } if (bufferOffset + receiveFilePartPacket->GetReceivedSize() > kDumpBufferSize * ReceiveFilePartPacket::kDataSize) { // Write the buffer to the output file // bytesWritten is discarded (it's just there to stop GCC warnings) int bytesWritten = fwrite(buffer, 1, bufferOffset, file); bufferOffset = 0; } // Copy the packet data into pitFile. memcpy(buffer + bufferOffset, receiveFilePartPacket->GetData(), receiveFilePartPacket->GetReceivedSize()); bufferOffset += receiveFilePartPacket->GetReceivedSize(); delete receiveFilePartPacket; } if (bufferOffset != 0) { // Write the buffer to the output file // bytesWritten is discarded (it's just there to stop GCC warnings) int bytesWritten = fwrite(buffer, 1, bufferOffset, file); } delete [] buffer; // End file transfer FileTransferPacket *fileTransferPacket = new FileTransferPacket(FileTransferPacket::kRequestEnd); success = SendPacket(fileTransferPacket); delete fileTransferPacket; if (!success) { Interface::PrintError("Failed to send request to end dump transfer!\n"); return (false); } ResponsePacket *responsePacket = new ResponsePacket(ResponsePacket::kResponseTypeFileTransfer); success = ReceivePacket(responsePacket); delete responsePacket; if (!success) { Interface::PrintError("Failed to receive end dump transfer verification!\n"); return (false); } return (true); }