// BinaryFile.cpp: implementation of the CBinaryFile class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "BinaryFile.h" #include "serialport.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CBinaryFile::CBinaryFile() { } CBinaryFile::~CBinaryFile() { } bool CBinaryFile::Send(CSerialPort& port, DWORD address) { bool bSuccess; m_pPort=&port; m_Address=address; // // Kick off the send thread. This returns control to SendThread // below on a different thread. // bSuccess=CTransferrableFile::StartSend(); return bSuccess; } bool CBinaryFile::Receive(CSerialPort& port, DWORD address, DWORD length) { bool bSuccess; m_pPort=&port; m_Address=address; m_Length=length; // // Kick off the send thread. This returns control to SendThread // below on a different thread. // bSuccess=StartReceive(); return bSuccess; } bool CBinaryFile::SendThread() { DWORD fileStatus; bool bSuccess; // // we send entire binary file so set the file pointer to // the start of the file // fileStatus=::SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN); if (fileStatus==0xffffffff) { // // failed // return false; } // // enter binary mode // bSuccess=CTransferrableFile::StartBinaryWrite(*m_pPort); if (!bSuccess) { return false; } // // transfer body of file // bSuccess=CTransferrableFile::TransferDataFromFile(*m_pPort, m_Address, m_Length); if (!bSuccess) { return false; } // // leave binary mode // bSuccess=CTransferrableFile::EndBinaryWrite(*m_pPort); if (!bSuccess) { return false; } return true; } bool CBinaryFile::ReceiveThread() { DWORD fileStatus; bool bSuccess; // // we send entire binary file so set the file pointer to // the start of the file // fileStatus=::SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN); if (fileStatus==0xffffffff) { // // failed // return false; } // // enter binary mode // bSuccess=CTransferrableFile::StartBinaryRead(*m_pPort); if (!bSuccess) { return false; } // // transfer body of file // bSuccess=CTransferrableFile::TransferDataToFile(*m_pPort, m_Address, m_Length); if (!bSuccess) { return false; } // // leave binary mode // bSuccess=CTransferrableFile::EndBinaryRead(*m_pPort); if (!bSuccess) { return false; } return true; } bool CBinaryFile::OpenForReading(TCHAR* filename) { m_hFile=::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile==INVALID_HANDLE_VALUE) { // // error opening file // return false; } m_Length=CTransferrableFile::GetLength(); if (m_Length>2*0x100000) { // // >2Mb, cannot be a playstation file, return failure // return false; } else { return true; } } bool CBinaryFile::OpenForWriting(TCHAR* filename) { m_hFile=::CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile==INVALID_HANDLE_VALUE) { // // error opening file // return false; } return true; } bool CBinaryFile::StartReceive() { DWORD threadID; m_BytesTransferred=0; m_hThread=::CreateThread(NULL, 0, ReceiveStatic, (LPVOID)this, 0, &threadID); if (m_hThread==INVALID_HANDLE_VALUE) { return false; } else { return true; } } DWORD WINAPI CBinaryFile::ReceiveStatic(LPVOID thisPointer) { bool bSuccess; bSuccess=((CBinaryFile*)thisPointer)->ReceiveThread(); if (bSuccess) { return 0; } else { return 1; } }