From 469e6cd2280c0d42e37df77f331248d29a5b5a8b Mon Sep 17 00:00:00 2001 From: Adrien Destugues - PulkoMandy Date: Mon, 5 Nov 2012 11:21:12 +0100 Subject: [PATCH] Log serial input to a file. --- src/apps/serialconnect/Jamfile | 2 +- src/apps/serialconnect/SerialApp.cpp | 53 +++++++++++++++++++++++-- src/apps/serialconnect/SerialApp.h | 6 ++- src/apps/serialconnect/SerialWindow.cpp | 44 ++++++++++++++++---- src/apps/serialconnect/SerialWindow.h | 2 + 5 files changed, 94 insertions(+), 13 deletions(-) diff --git a/src/apps/serialconnect/Jamfile b/src/apps/serialconnect/Jamfile index 20043b14bc..1d04a0c251 100644 --- a/src/apps/serialconnect/Jamfile +++ b/src/apps/serialconnect/Jamfile @@ -16,6 +16,6 @@ Application SerialConnect : state.c unicode.c vterm.c - : be device $(HAIKU_LOCALE_LIBS) $(TARGET_LIBSUPC++) + : be device tracker $(HAIKU_LOCALE_LIBS) $(TARGET_LIBSUPC++) ; diff --git a/src/apps/serialconnect/SerialApp.cpp b/src/apps/serialconnect/SerialApp.cpp index 24b7d9e7c8..3a69305371 100644 --- a/src/apps/serialconnect/SerialApp.cpp +++ b/src/apps/serialconnect/SerialApp.cpp @@ -4,16 +4,21 @@ */ -#include - #include "SerialApp.h" +#include +#include + +#include +#include +#include + #include "SerialWindow.h" SerialApp::SerialApp() - : - BApplication(SerialApp::kApplicationSignature) + : BApplication(SerialApp::kApplicationSignature) + , fLogFile(NULL) { fWindow = new SerialWindow(); @@ -24,6 +29,12 @@ SerialApp::SerialApp() } +SerialApp::~SerialApp() +{ + delete fLogFile; +} + + void SerialApp::ReadyToRun() { fWindow->Show(); @@ -51,6 +62,19 @@ void SerialApp::MessageReceived(BMessage* message) // forward the message to the window, which will display the // incoming data fWindow->PostMessage(message); + + if (fLogFile) + { + const char* bytes; + ssize_t length; + message->FindData("data", B_RAW_TYPE, (const void**)&bytes, + &length); + if(fLogFile->Write(bytes, length) != length) + { + puts("### WRITE ERROR"); + } + } + break; } case kMsgDataWrite: @@ -62,6 +86,27 @@ void SerialApp::MessageReceived(BMessage* message) fSerialPort.Write(bytes, size); break; } + case kMsgLogfile: + { + entry_ref parent; + const char* filename; + + if (message->FindRef("directory", &parent) == B_OK + && message->FindString("name", &filename) == B_OK) + { + delete fLogFile; + BDirectory directory(&parent); + fLogFile = new BFile(&directory, filename, + B_WRITE_ONLY | B_CREATE_FILE | B_OPEN_AT_END); + status_t error = fLogFile->InitCheck(); + if(error != B_OK) + { + puts(strerror(error)); + } + } else { + debugger("Invalid BMessage received"); + } + } case kMsgSettings: { int32 baudrate; diff --git a/src/apps/serialconnect/SerialApp.h b/src/apps/serialconnect/SerialApp.h index 7dd7c50629..5aa79670cc 100644 --- a/src/apps/serialconnect/SerialApp.h +++ b/src/apps/serialconnect/SerialApp.h @@ -12,6 +12,7 @@ #include +class BFile; class SerialWindow; @@ -19,6 +20,7 @@ class SerialApp: public BApplication { public: SerialApp(); + ~SerialApp(); void ReadyToRun(); void MessageReceived(BMessage* message); @@ -26,6 +28,7 @@ class SerialApp: public BApplication BSerialPort fSerialPort; sem_id fSerialLock; SerialWindow* fWindow; + BFile* fLogFile; static status_t PollSerial(void*); @@ -34,9 +37,10 @@ class SerialApp: public BApplication enum messageConstants { - kMsgOpenPort = 'open', kMsgDataRead = 'dare', kMsgDataWrite = 'dawr', + kMsgLogfile = 'logf', + kMsgOpenPort = 'open', kMsgSettings = 'stty', }; diff --git a/src/apps/serialconnect/SerialWindow.cpp b/src/apps/serialconnect/SerialWindow.cpp index ed5f030962..ccc099c5e4 100644 --- a/src/apps/serialconnect/SerialWindow.cpp +++ b/src/apps/serialconnect/SerialWindow.cpp @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -19,9 +20,9 @@ SerialWindow::SerialWindow() - : - BWindow(BRect(100, 100, 400, 400), SerialWindow::kWindowTitle, + : BWindow(BRect(100, 100, 400, 400), SerialWindow::kWindowTitle, B_DOCUMENT_WINDOW, B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS) + , fLogFilePanel(NULL) { SetLayout(new BGroupLayout(B_VERTICAL, 0.0f)); @@ -32,17 +33,29 @@ SerialWindow::SerialWindow() AddChild(fTermView); fConnectionMenu = new BMenu("Connection"); - fConnectionMenu->SetRadioMode(true); - - BMenu* editMenu = new BMenu("Edit"); + BMenu* fileMenu = new BMenu("File"); BMenu* settingsMenu = new BMenu("Settings"); + fConnectionMenu->SetRadioMode(true); + menuBar->AddItem(fConnectionMenu); - menuBar->AddItem(editMenu); + menuBar->AddItem(fileMenu); menuBar->AddItem(settingsMenu); // TODO edit menu - what's in it ? + //BMenu* editMenu = new BMenu("Edit"); + //menuBar->AddItem(editMenu); + BMenuItem* logFile = new BMenuItem("Log to file" B_UTF8_ELLIPSIS, + new BMessage(kMsgLogfile)); + fileMenu->AddItem(logFile); + BMenuItem* xmodemSend = new BMenuItem("X/Y/ZModem send" B_UTF8_ELLIPSIS, + NULL); + fileMenu->AddItem(xmodemSend); + BMenuItem* xmodemReceive = new BMenuItem( + "X/Y/Zmodem receive" B_UTF8_ELLIPSIS, NULL); + fileMenu->AddItem(xmodemReceive); + // Configuring all this by menus may be a bit unhandy. Make a setting // window instead ? BMenu* baudRate = new BMenu("Baud rate"); @@ -159,7 +172,13 @@ SerialWindow::SerialWindow() CenterOnScreen(); } -#include + + +SerialWindow::~SerialWindow() +{ + delete fLogFilePanel; +} + void SerialWindow::MenusBeginning() { @@ -216,6 +235,17 @@ void SerialWindow::MessageReceived(BMessage* message) fTermView->PushBytes(bytes, length); break; } + case kMsgLogfile: + { + // Let's lazy init the file panel + if(fLogFilePanel == NULL) { + fLogFilePanel = new BFilePanel(B_SAVE_PANEL, &be_app_messenger, + NULL, B_FILE_NODE, false); + fLogFilePanel->SetMessage(message); + } + fLogFilePanel->Show(); + break; + } default: BWindow::MessageReceived(message); } diff --git a/src/apps/serialconnect/SerialWindow.h b/src/apps/serialconnect/SerialWindow.h index d8f20468ba..10591f6cbe 100644 --- a/src/apps/serialconnect/SerialWindow.h +++ b/src/apps/serialconnect/SerialWindow.h @@ -15,6 +15,7 @@ class SerialWindow: public BWindow { public: SerialWindow(); + ~SerialWindow(); void MenusBeginning(); void MessageReceived(BMessage* message); @@ -22,6 +23,7 @@ class SerialWindow: public BWindow private: TermView* fTermView; BMenu* fConnectionMenu; + BFilePanel* fLogFilePanel; static const char* kWindowTitle; };