xref: /haiku/src/apps/serialconnect/FileSender.cpp (revision f1af46a2c041ce6b4af24fd2c9cfce26dbab1bef)
18bff17cfSAdrien Destugues /*
28bff17cfSAdrien Destugues  * Copyright 2017, Adrien Destugues, pulkomandy@pulkomandy.tk
38bff17cfSAdrien Destugues  * Distributed under terms of the MIT license.
48bff17cfSAdrien Destugues  */
58bff17cfSAdrien Destugues 
68bff17cfSAdrien Destugues 
78bff17cfSAdrien Destugues #include "FileSender.h"
88bff17cfSAdrien Destugues 
98bff17cfSAdrien Destugues #include "SerialApp.h"
108bff17cfSAdrien Destugues 
118bff17cfSAdrien Destugues #include <DataIO.h>
128bff17cfSAdrien Destugues #include <Message.h>
138bff17cfSAdrien Destugues #include <SerialPort.h>
148bff17cfSAdrien Destugues 
158bff17cfSAdrien Destugues 
~FileSender()16*f1af46a2SAdrien Destugues FileSender::~FileSender()
17*f1af46a2SAdrien Destugues {
18*f1af46a2SAdrien Destugues }
19*f1af46a2SAdrien Destugues 
20*f1af46a2SAdrien Destugues 
RawSender(BDataIO * source,BSerialPort * sink,BHandler * listener)218bff17cfSAdrien Destugues RawSender::RawSender(BDataIO* source, BSerialPort* sink, BHandler* listener)
228bff17cfSAdrien Destugues {
238bff17cfSAdrien Destugues 	// FIXME doing this all here in the constructor is not good. We need to
248bff17cfSAdrien Destugues 	// do things asynchronously instead so as not to lock the application
258bff17cfSAdrien Destugues 	// thread.
268bff17cfSAdrien Destugues 	off_t sourceSize;
278bff17cfSAdrien Destugues 	off_t position;
288bff17cfSAdrien Destugues 
298bff17cfSAdrien Destugues 	BPositionIO* pos = dynamic_cast<BPositionIO*>(source);
308bff17cfSAdrien Destugues 	if (pos)
318bff17cfSAdrien Destugues 		pos->GetSize(&sourceSize);
328bff17cfSAdrien Destugues 	else
338bff17cfSAdrien Destugues 		sourceSize = 0;
348bff17cfSAdrien Destugues 	position = 0;
358bff17cfSAdrien Destugues 
368bff17cfSAdrien Destugues 	BMessenger messenger(listener);
378bff17cfSAdrien Destugues 
388bff17cfSAdrien Destugues 	uint8_t buffer[256];
398bff17cfSAdrien Destugues 	for (;;) {
408bff17cfSAdrien Destugues 		ssize_t s = source->Read(&buffer, sizeof(buffer));
418bff17cfSAdrien Destugues 		if (s <= 0)
428bff17cfSAdrien Destugues 			return;
438bff17cfSAdrien Destugues 
448bff17cfSAdrien Destugues 		sink->Write(buffer, s);
458bff17cfSAdrien Destugues 		position += s;
468bff17cfSAdrien Destugues 
478bff17cfSAdrien Destugues 		BMessage msg(kMsgProgress);
488bff17cfSAdrien Destugues 		msg.AddInt32("pos", position);
498bff17cfSAdrien Destugues 		msg.AddInt32("size", sourceSize);
508bff17cfSAdrien Destugues 		msg.AddString("info", "Sending" B_UTF8_ELLIPSIS);
518bff17cfSAdrien Destugues 		messenger.SendMessage(&msg);
528bff17cfSAdrien Destugues 
538bff17cfSAdrien Destugues 		//usleep(20000);
548bff17cfSAdrien Destugues 	}
558bff17cfSAdrien Destugues }
568bff17cfSAdrien Destugues 
578bff17cfSAdrien Destugues 
~RawSender()588bff17cfSAdrien Destugues RawSender::~RawSender()
598bff17cfSAdrien Destugues {
608bff17cfSAdrien Destugues }
618bff17cfSAdrien Destugues 
628bff17cfSAdrien Destugues 
638bff17cfSAdrien Destugues bool
BytesReceived(const uint8_t * data,size_t length)648bff17cfSAdrien Destugues RawSender::BytesReceived(const uint8_t* data, size_t length)
658bff17cfSAdrien Destugues {
668bff17cfSAdrien Destugues 	// Nothing to do with received bytes, this protocol has no kind of
678bff17cfSAdrien Destugues 	// acknowledgement from remote side.
688bff17cfSAdrien Destugues 	return true;
698bff17cfSAdrien Destugues }
70