1dae0a4e0SAdrien Destugues - PulkoMandy /* 2dae0a4e0SAdrien Destugues - PulkoMandy * Copyright 2012, Adrien Destugues, pulkomandy@gmail.com 3dae0a4e0SAdrien Destugues - PulkoMandy * Distributed under the terms of the MIT licence. 4dae0a4e0SAdrien Destugues - PulkoMandy */ 5dae0a4e0SAdrien Destugues - PulkoMandy 6dae0a4e0SAdrien Destugues - PulkoMandy 7f441fd03SAdrien Destugues - PulkoMandy #include <stdio.h> 8f441fd03SAdrien Destugues - PulkoMandy 9dae0a4e0SAdrien Destugues - PulkoMandy #include "SerialApp.h" 10dae0a4e0SAdrien Destugues - PulkoMandy 11dae0a4e0SAdrien Destugues - PulkoMandy #include "SerialWindow.h" 12dae0a4e0SAdrien Destugues - PulkoMandy 13dae0a4e0SAdrien Destugues - PulkoMandy 14dae0a4e0SAdrien Destugues - PulkoMandy SerialApp::SerialApp() 15dae0a4e0SAdrien Destugues - PulkoMandy : 16dae0a4e0SAdrien Destugues - PulkoMandy BApplication(SerialApp::kApplicationSignature) 17dae0a4e0SAdrien Destugues - PulkoMandy { 18dae0a4e0SAdrien Destugues - PulkoMandy fWindow = new SerialWindow(); 19dae0a4e0SAdrien Destugues - PulkoMandy 20f441fd03SAdrien Destugues - PulkoMandy fSerialLock = create_sem(0, "Serial port lock"); 21dae0a4e0SAdrien Destugues - PulkoMandy thread_id id = spawn_thread(PollSerial, "Serial port poller", 22dae0a4e0SAdrien Destugues - PulkoMandy B_LOW_PRIORITY, this); 23dae0a4e0SAdrien Destugues - PulkoMandy resume_thread(id); 24dae0a4e0SAdrien Destugues - PulkoMandy } 25dae0a4e0SAdrien Destugues - PulkoMandy 26dae0a4e0SAdrien Destugues - PulkoMandy 27dae0a4e0SAdrien Destugues - PulkoMandy void SerialApp::ReadyToRun() 28dae0a4e0SAdrien Destugues - PulkoMandy { 29dae0a4e0SAdrien Destugues - PulkoMandy fWindow->Show(); 30dae0a4e0SAdrien Destugues - PulkoMandy } 31dae0a4e0SAdrien Destugues - PulkoMandy 32dae0a4e0SAdrien Destugues - PulkoMandy 33dae0a4e0SAdrien Destugues - PulkoMandy void SerialApp::MessageReceived(BMessage* message) 34dae0a4e0SAdrien Destugues - PulkoMandy { 35dae0a4e0SAdrien Destugues - PulkoMandy switch(message->what) 36dae0a4e0SAdrien Destugues - PulkoMandy { 37dae0a4e0SAdrien Destugues - PulkoMandy case kMsgOpenPort: 38dae0a4e0SAdrien Destugues - PulkoMandy { 39dae0a4e0SAdrien Destugues - PulkoMandy const char* portName; 40dae0a4e0SAdrien Destugues - PulkoMandy message->FindString("port name", &portName); 41f441fd03SAdrien Destugues - PulkoMandy fSerialPort.Open(portName); 42f441fd03SAdrien Destugues - PulkoMandy release_sem(fSerialLock); 43dae0a4e0SAdrien Destugues - PulkoMandy break; 44dae0a4e0SAdrien Destugues - PulkoMandy } 45dae0a4e0SAdrien Destugues - PulkoMandy case kMsgDataRead: 46dae0a4e0SAdrien Destugues - PulkoMandy { 47f441fd03SAdrien Destugues - PulkoMandy // forward the message to the window, which will display the 48f441fd03SAdrien Destugues - PulkoMandy // incoming data 49f441fd03SAdrien Destugues - PulkoMandy fWindow->PostMessage(message); 50dae0a4e0SAdrien Destugues - PulkoMandy break; 51dae0a4e0SAdrien Destugues - PulkoMandy } 52f441fd03SAdrien Destugues - PulkoMandy case kMsgDataWrite: 53f441fd03SAdrien Destugues - PulkoMandy { 54f441fd03SAdrien Destugues - PulkoMandy const char* bytes; 55f441fd03SAdrien Destugues - PulkoMandy ssize_t size; 56f441fd03SAdrien Destugues - PulkoMandy 57*47b44bbeSPhilippe Houdoin message->FindData("data", B_RAW_TYPE, (const void**)&bytes, &size); 58f441fd03SAdrien Destugues - PulkoMandy fSerialPort.Write(bytes, size); 59f441fd03SAdrien Destugues - PulkoMandy } 60dae0a4e0SAdrien Destugues - PulkoMandy default: 61dae0a4e0SAdrien Destugues - PulkoMandy BApplication::MessageReceived(message); 62dae0a4e0SAdrien Destugues - PulkoMandy } 63dae0a4e0SAdrien Destugues - PulkoMandy } 64dae0a4e0SAdrien Destugues - PulkoMandy 65dae0a4e0SAdrien Destugues - PulkoMandy 66dae0a4e0SAdrien Destugues - PulkoMandy /* static */ 67dae0a4e0SAdrien Destugues - PulkoMandy status_t SerialApp::PollSerial(void*) 68dae0a4e0SAdrien Destugues - PulkoMandy { 69dae0a4e0SAdrien Destugues - PulkoMandy SerialApp* application = (SerialApp*)be_app; 70dae0a4e0SAdrien Destugues - PulkoMandy char buffer[256]; 71dae0a4e0SAdrien Destugues - PulkoMandy 72dae0a4e0SAdrien Destugues - PulkoMandy for(;;) 73dae0a4e0SAdrien Destugues - PulkoMandy { 74dae0a4e0SAdrien Destugues - PulkoMandy ssize_t bytesRead; 75dae0a4e0SAdrien Destugues - PulkoMandy 76f441fd03SAdrien Destugues - PulkoMandy bytesRead = application->fSerialPort.Read(buffer, 256); 77dae0a4e0SAdrien Destugues - PulkoMandy if (bytesRead == B_FILE_ERROR) 78dae0a4e0SAdrien Destugues - PulkoMandy { 79dae0a4e0SAdrien Destugues - PulkoMandy // Port is not open - wait for it and start over 80f441fd03SAdrien Destugues - PulkoMandy acquire_sem(application->fSerialLock); 81f441fd03SAdrien Destugues - PulkoMandy } else if (bytesRead > 0) { 82dae0a4e0SAdrien Destugues - PulkoMandy // We read something, forward it to the app for handling 83dae0a4e0SAdrien Destugues - PulkoMandy BMessage* serialData = new BMessage(kMsgDataRead); 84f441fd03SAdrien Destugues - PulkoMandy serialData->AddData("data", B_RAW_TYPE, buffer, bytesRead); 85dae0a4e0SAdrien Destugues - PulkoMandy be_app_messenger.SendMessage(serialData); 86dae0a4e0SAdrien Destugues - PulkoMandy } 87dae0a4e0SAdrien Destugues - PulkoMandy } 88*47b44bbeSPhilippe Houdoin 89*47b44bbeSPhilippe Houdoin // Should not reach this line anyway... 90*47b44bbeSPhilippe Houdoin return B_OK; 91dae0a4e0SAdrien Destugues - PulkoMandy } 92dae0a4e0SAdrien Destugues - PulkoMandy 93dae0a4e0SAdrien Destugues - PulkoMandy const char* SerialApp::kApplicationSignature 94dae0a4e0SAdrien Destugues - PulkoMandy = "application/x-vnd.haiku.SerialConnect"; 95dae0a4e0SAdrien Destugues - PulkoMandy 96dae0a4e0SAdrien Destugues - PulkoMandy 97dae0a4e0SAdrien Destugues - PulkoMandy int main(int argc, char** argv) 98dae0a4e0SAdrien Destugues - PulkoMandy { 99dae0a4e0SAdrien Destugues - PulkoMandy SerialApp app; 100dae0a4e0SAdrien Destugues - PulkoMandy app.Run(); 101dae0a4e0SAdrien Destugues - PulkoMandy } 102