LaunchAPPL/Serial: send type/creator code

This commit is contained in:
Wolfgang Thaller 2018-05-05 18:53:27 +02:00
parent ca17e9ff0c
commit 569ef3d65e
3 changed files with 46 additions and 15 deletions

View File

@ -3,6 +3,7 @@
#include "Utilities.h"
#include "Stream.h"
#include "ReliableStream.h"
#include "ServerProtocol.h"
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
@ -162,21 +163,28 @@ bool SerialLauncher::Go(int timeout)
do
{
rStream.reset(1);
rStream.reset(1);
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)
stream.wait();
stream.wait();
} while(!rStream.resetResponseArrived());
std::cerr << "Connected." << std::endl;
{
RemoteCommand cmd = RemoteCommand::launchApp;
write(&cmd, 1);
write(std::string(app.type).data(), 4);
write(std::string(app.creator).data(), 4);
std::ostringstream rsrcOut;
app.resources.writeFork(rsrcOut);
std::string rsrc = rsrcOut.str();
std::string& data = app.data;
std::cerr << "Transfering " << (data.size() + rsrc.size() + 1023) / 1024 << " KB." << std::endl;
tmp = htonl(data.size());
write(&tmp, 4);
@ -192,12 +200,12 @@ bool SerialLauncher::Go(int timeout)
read(&tmp, 4);
uint32_t result = ntohl(tmp);
std::cerr << "Finished." << std::endl;
if(result == 0)
{
read(&tmp, 4);
uint32_t size = ntohl(tmp);
outputBytes.resize(size);
if(size > 0)
read(outputBytes.data(), size);

View File

@ -0,0 +1,8 @@
#pragma once
#include <stdint.h>
enum class RemoteCommand : uint8_t
{
launchApp = 1
};

View File

@ -32,6 +32,7 @@
#include "AppLauncher.h"
#include <ReliableStream.h>
#include "ServerProtocol.h"
#include <Processes.h>
#include <string.h>
#include <stdio.h>
@ -263,44 +264,58 @@ public:
enum class State
{
size,
command,
header,
data,
rsrc,
launch,
wait,
respond
};
State state = State::size;
State state = State::command;
RemoteCommand command;
void onReset()
{
SetStatus(AppStatus::ready, 0, 0);
state = State::size;
state = State::command;
}
size_t onReceive(const uint8_t* p, size_t n)
{
switch(state)
{
case State::size:
case State::command:
{
if(n < 8)
if(n < 1)
return 0;
dataSize = *(const uint32_t*)p;
rsrcSize = *(const uint32_t*)(p+4);
command = (RemoteCommand)p[0];
if(command == RemoteCommand::launchApp)
state = State::header;
return 1;
}
case State::header:
{
if(n < 16)
return 0;
OSType type = *(const OSType*)(p+0);
OSType creator = *(const OSType*)(p+4);
dataSize = *(const uint32_t*)(p+8);
rsrcSize = *(const uint32_t*)(p+12);
SetStatus(AppStatus::downloading, 0, dataSize + rsrcSize);
printf("Data Size: %u / %u\n", dataSize, rsrcSize);
FSDelete("\pRetro68App", 0);
Create("\pRetro68App", 0, '????', 'APPL');
Create("\pRetro68App", 0, creator, type);
OpenDF("\pRetro68App", 0, &refNum);
FSDelete("\pout", 0);
Create("\pout", 0, 'ttxt', 'TEXT');
state = State::data;
remainingSize = dataSize;
return 8;
return 16;
}
case State::data:
@ -536,7 +551,7 @@ int main()
}
else
{
server.state = LaunchServer::State::size;
server.state = LaunchServer::State::command;
SetStatus(AppStatus::ready, 0, 0);
}
}
@ -566,7 +581,7 @@ int main()
}
if(outSizeRemaining == 0 && rStream.allDataArrived())
{
server.state = LaunchServer::State::size;
server.state = LaunchServer::State::command;
SetStatus(AppStatus::ready, 0, 0);
rStream.reset(0);
}