2018-04-20 22:03:29 +00:00
|
|
|
#include "Serial.h"
|
|
|
|
#include "Launcher.h"
|
|
|
|
#include "Utilities.h"
|
|
|
|
#include "Stream.h"
|
2018-04-22 09:23:08 +00:00
|
|
|
#include "ReliableStream.h"
|
2018-05-05 16:53:27 +00:00
|
|
|
#include "ServerProtocol.h"
|
2018-04-24 20:06:32 +00:00
|
|
|
#include <termios.h>
|
2018-04-20 22:03:29 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <poll.h>
|
2018-05-05 16:41:38 +00:00
|
|
|
#include <chrono>
|
2018-04-23 23:42:36 +00:00
|
|
|
#include <iostream>
|
2018-04-20 22:03:29 +00:00
|
|
|
|
2018-05-05 16:41:38 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
using namespace std::literals::chrono_literals;
|
2018-04-20 22:03:29 +00:00
|
|
|
|
|
|
|
class SerialStream : public Stream
|
|
|
|
{
|
|
|
|
static const long kReadBufferSize = 4096;
|
|
|
|
uint8_t readBuffer[kReadBufferSize];
|
|
|
|
public:
|
|
|
|
int fd;
|
2018-05-05 16:41:38 +00:00
|
|
|
int baud;
|
2018-04-20 22:03:29 +00:00
|
|
|
|
|
|
|
virtual void write(const void* p, size_t n) override;
|
|
|
|
|
|
|
|
void wait();
|
|
|
|
|
|
|
|
SerialStream(po::variables_map &options);
|
|
|
|
~SerialStream();
|
|
|
|
};
|
|
|
|
|
|
|
|
class SerialLauncher : public Launcher
|
|
|
|
{
|
|
|
|
SerialStream stream;
|
2018-04-22 09:23:08 +00:00
|
|
|
ReliableStream rStream;
|
2018-04-24 05:57:43 +00:00
|
|
|
std::vector<char> outputBytes;
|
2018-05-05 21:45:59 +00:00
|
|
|
bool upgradeMode = false;
|
2018-04-20 22:03:29 +00:00
|
|
|
public:
|
|
|
|
SerialLauncher(po::variables_map& options);
|
|
|
|
virtual ~SerialLauncher();
|
|
|
|
|
|
|
|
virtual bool Go(int timeout = 0);
|
2018-04-24 05:57:43 +00:00
|
|
|
virtual void DumpOutput();
|
2018-04-20 22:03:29 +00:00
|
|
|
|
|
|
|
private:
|
2018-04-23 23:42:36 +00:00
|
|
|
void write(const void *p, size_t n);
|
2018-04-20 22:03:29 +00:00
|
|
|
ssize_t read(void * p, size_t n);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
SerialStream::SerialStream(po::variables_map &options)
|
|
|
|
{
|
|
|
|
std::string port = options["serial-port"].as<std::string>();
|
2018-05-05 16:41:38 +00:00
|
|
|
baud = options["serial-baud"].as<int>();
|
2018-04-20 22:03:29 +00:00
|
|
|
fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY );
|
|
|
|
if(fd < 0)
|
|
|
|
throw std::runtime_error("Cannot open serial port.\n");
|
|
|
|
|
|
|
|
struct termios tios;
|
|
|
|
tcgetattr(fd,&tios);
|
|
|
|
|
2018-04-24 20:06:32 +00:00
|
|
|
tios.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
|
|
|
|
|
|
|
|
switch(baud)
|
|
|
|
{
|
|
|
|
case 9600: tios.c_cflag |= B9600; break;
|
|
|
|
case 19200: tios.c_cflag |= B19200; break;
|
|
|
|
case 38400: tios.c_cflag |= B38400; break;
|
|
|
|
case 57600: tios.c_cflag |= B57600; break;
|
|
|
|
case 115200: tios.c_cflag |= B115200; break;
|
|
|
|
case 230400: tios.c_cflag |= B230400; break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("Unsupported baud rate.\n");
|
|
|
|
}
|
|
|
|
|
2018-04-20 22:03:29 +00:00
|
|
|
tios.c_iflag = 0;//IGNPAR | ICRNL;
|
|
|
|
tios.c_lflag = 0;
|
|
|
|
tios.c_oflag = 0;
|
|
|
|
tios.c_cc[VTIME] = 0; /* inter-character timer unused */
|
2018-04-22 09:23:08 +00:00
|
|
|
tios.c_cc[VMIN] = 1; /* blocking read until 1 chars received */
|
2018-04-20 22:03:29 +00:00
|
|
|
tcsetattr(fd,TCSANOW,&tios);
|
|
|
|
usleep(500000);
|
|
|
|
}
|
|
|
|
SerialStream::~SerialStream()
|
|
|
|
{
|
2018-04-23 23:42:36 +00:00
|
|
|
tcdrain(fd);
|
|
|
|
usleep(500000);
|
2018-04-20 22:03:29 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialStream::write(const void* p, size_t n)
|
|
|
|
{
|
|
|
|
while(n)
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLOUT;
|
|
|
|
pfd.revents = 0;
|
|
|
|
poll(&pfd, 1, 1000);
|
|
|
|
if(pfd.revents & POLLOUT)
|
|
|
|
{
|
|
|
|
ssize_t written = ::write(fd, p, n);
|
|
|
|
if(written > 0)
|
|
|
|
{
|
|
|
|
p = (const void*) ( (const char*)p + written );
|
|
|
|
n -= written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SerialStream::wait()
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
pfd.revents = 0;
|
|
|
|
poll(&pfd, 1, 1000);
|
|
|
|
if(pfd.revents & POLLIN)
|
|
|
|
{
|
|
|
|
ssize_t n = ::read(fd, readBuffer, kReadBufferSize);
|
|
|
|
if(n > 0)
|
|
|
|
{
|
2018-04-22 09:23:08 +00:00
|
|
|
notifyReceive(readBuffer, n);
|
2018-04-20 22:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SerialLauncher::SerialLauncher(po::variables_map &options)
|
2018-04-23 23:42:36 +00:00
|
|
|
: Launcher(options), stream(options), rStream(&stream)
|
2018-04-20 22:03:29 +00:00
|
|
|
{
|
2018-05-05 21:45:59 +00:00
|
|
|
if(options.count("upgrade-server"))
|
|
|
|
upgradeMode = true;
|
2018-04-20 22:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SerialLauncher::~SerialLauncher()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-24 05:57:43 +00:00
|
|
|
ssize_t SerialLauncher::read(void *p0, size_t n)
|
2018-04-20 22:03:29 +00:00
|
|
|
{
|
2018-04-24 05:57:43 +00:00
|
|
|
uint8_t* p = (uint8_t*)p0;
|
|
|
|
ssize_t gotBytes = rStream.read(p, n);
|
|
|
|
while(gotBytes < n)
|
2018-04-20 22:03:29 +00:00
|
|
|
{
|
2018-04-22 09:23:08 +00:00
|
|
|
rStream.flushWrite();
|
2018-04-20 22:03:29 +00:00
|
|
|
stream.wait();
|
2018-04-24 05:57:43 +00:00
|
|
|
gotBytes += rStream.read(p + gotBytes, n - gotBytes);
|
2018-04-20 22:03:29 +00:00
|
|
|
}
|
2018-04-24 05:57:43 +00:00
|
|
|
return gotBytes;
|
2018-04-20 22:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
void SerialLauncher::write(const void *p, size_t n)
|
|
|
|
{
|
|
|
|
while(!rStream.readyToWrite())
|
|
|
|
stream.wait();
|
|
|
|
rStream.write(p, n);
|
|
|
|
}
|
|
|
|
|
2018-04-20 22:03:29 +00:00
|
|
|
bool SerialLauncher::Go(int timeout)
|
|
|
|
{
|
|
|
|
uint32_t tmp;
|
|
|
|
|
2018-05-05 16:41:38 +00:00
|
|
|
do
|
|
|
|
{
|
2018-05-05 16:53:27 +00:00
|
|
|
rStream.reset(1);
|
2018-05-05 16:41:38 +00:00
|
|
|
std::cerr << "Connecting... (" << stream.baud << " baud)" << std::endl;
|
|
|
|
using clock = std::chrono::steady_clock;
|
|
|
|
auto startTime = clock::now();
|
|
|
|
while(!rStream.resetResponseArrived() && clock::now() - startTime < 5s)
|
2018-05-05 16:53:27 +00:00
|
|
|
stream.wait();
|
2018-05-05 16:41:38 +00:00
|
|
|
} while(!rStream.resetResponseArrived());
|
2018-04-23 23:42:36 +00:00
|
|
|
|
2018-05-05 16:41:38 +00:00
|
|
|
std::cerr << "Connected." << std::endl;
|
2018-04-23 23:42:36 +00:00
|
|
|
|
2018-04-20 22:03:29 +00:00
|
|
|
{
|
2018-05-05 21:45:59 +00:00
|
|
|
RemoteCommand cmd = upgradeMode ? RemoteCommand::upgradeLauncher : RemoteCommand::launchApp;
|
2018-05-05 16:53:27 +00:00
|
|
|
write(&cmd, 1);
|
|
|
|
|
|
|
|
write(std::string(app.type).data(), 4);
|
|
|
|
write(std::string(app.creator).data(), 4);
|
|
|
|
|
2018-04-20 22:03:29 +00:00
|
|
|
std::ostringstream rsrcOut;
|
|
|
|
app.resources.writeFork(rsrcOut);
|
|
|
|
std::string rsrc = rsrcOut.str();
|
|
|
|
std::string& data = app.data;
|
2018-05-05 16:53:27 +00:00
|
|
|
std::cerr << "Transfering " << (data.size() + rsrc.size() + 1023) / 1024 << " KB." << std::endl;
|
2018-04-20 22:03:29 +00:00
|
|
|
|
|
|
|
tmp = htonl(data.size());
|
|
|
|
write(&tmp, 4);
|
|
|
|
tmp = htonl(rsrc.size());
|
|
|
|
write(&tmp, 4);
|
|
|
|
|
|
|
|
write(data.data(), data.size());
|
|
|
|
write(rsrc.data(), rsrc.size());
|
|
|
|
}
|
2018-04-23 23:42:36 +00:00
|
|
|
while(!rStream.allDataArrived())
|
|
|
|
stream.wait();
|
2018-05-05 16:41:38 +00:00
|
|
|
std::cerr << "Running Appliation..." << std::endl;
|
2018-04-20 22:03:29 +00:00
|
|
|
read(&tmp, 4);
|
2018-04-24 05:57:43 +00:00
|
|
|
uint32_t result = ntohl(tmp);
|
2018-05-05 21:45:59 +00:00
|
|
|
std::cerr << "Finished (result = " << result << ")." << std::endl;
|
2018-05-05 16:53:27 +00:00
|
|
|
|
2018-04-24 05:57:43 +00:00
|
|
|
if(result == 0)
|
|
|
|
{
|
|
|
|
read(&tmp, 4);
|
|
|
|
uint32_t size = ntohl(tmp);
|
|
|
|
outputBytes.resize(size);
|
|
|
|
if(size > 0)
|
|
|
|
read(outputBytes.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result == 0;
|
2018-04-20 22:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 05:57:43 +00:00
|
|
|
void SerialLauncher::DumpOutput()
|
|
|
|
{
|
|
|
|
std::cout.write(outputBytes.data(), outputBytes.size());
|
|
|
|
}
|
2018-04-20 22:03:29 +00:00
|
|
|
|
|
|
|
void Serial::GetOptions(options_description &desc)
|
|
|
|
{
|
|
|
|
desc.add_options()
|
|
|
|
("serial-port", po::value<std::string>()->default_value("/dev/ttyUSB0"), "serial port to use")
|
2018-04-24 20:06:32 +00:00
|
|
|
("serial-baud", po::value<int>()->default_value(19200), "serial port speed")
|
2018-04-20 22:03:29 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Serial::CheckOptions(variables_map &options)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Launcher> Serial::MakeLauncher(variables_map &options)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<Launcher>(new SerialLauncher(options));
|
|
|
|
}
|