Factored Process class

This commit is contained in:
Cristian Maglie 2013-06-12 12:42:38 +02:00
parent 9e59262f64
commit 965d2cbf73
4 changed files with 31 additions and 72 deletions

View File

@ -58,54 +58,6 @@ void BridgeClass::begin() {
while (true);
}
uint8_t BridgeClass::runCommand(String &command, uint8_t &err) {
uint8_t cmd[] = {'R'};
uint8_t res[2];
transfer(cmd, 1, (uint8_t*)command.c_str(), command.length(), res, 2);
err = res[0];
return res[1];
}
bool BridgeClass::commandIsRunning(uint8_t handle) {
uint8_t cmd[] = {'r', handle};
uint8_t res[1];
transfer(cmd, 2, res, 1);
return (res[0] == 1);
}
unsigned int BridgeClass::commandExitValue(uint8_t handle) {
uint8_t cmd[] = {'W', handle};
uint8_t res[2];
transfer(cmd, 2, res, 2);
return (res[0] << 8) + res[1];
}
void BridgeClass::cleanCommand(uint8_t handle) {
uint8_t cmd[] = {'w', handle};
transfer(cmd, 2);
}
unsigned int BridgeClass::commandOutputAvailable(uint8_t handle) {
uint8_t cmd[] = {'o', handle};
uint8_t res[1];
transfer(cmd, 2, res, 1);
return res[0];
}
unsigned int BridgeClass::readCommandOutput(uint8_t handle,
uint8_t *buffer, unsigned int size) {
if (size > 255)
size = 255;
uint8_t cmd[] = {'O', handle, size};
return transfer(cmd, 3, buffer, size);
}
void BridgeClass::writeCommandInput(uint8_t handle,
const uint8_t *buff, unsigned int size) {
uint8_t cmd[] = {'I', handle};
transfer(cmd, 2, buff, size, NULL, 0);
}
unsigned int BridgeClass::readMessage(uint8_t *buff, unsigned int size) {
uint8_t tmp[] = { 'm' };
return transfer(tmp, 1, buff, size);

View File

@ -27,21 +27,6 @@ public:
BridgeClass(Stream &_stream);
void begin();
// Methods to handle processes on the linux side
uint8_t runCommand(String &command, uint8_t &err);
bool commandIsRunning(uint8_t handle);
unsigned int commandExitValue(uint8_t handle);
void cleanCommand(uint8_t handle);
unsigned int commandOutputAvailable(uint8_t handle);
unsigned int readCommandOutput(uint8_t handle, uint8_t *buff, unsigned int size);
unsigned int readCommandOutput(uint8_t handle, char *buff, unsigned int size)
{ return readCommandOutput(handle, reinterpret_cast<uint8_t *>(buff), size); }
void writeCommandInput(uint8_t handle, const uint8_t *buff, unsigned int size);
void writeCommandInput(uint8_t handle, const char *buff, unsigned int size)
{ writeCommandInput(handle, reinterpret_cast<const uint8_t *>(buff), size); }
// Methods to handle mailbox messages
unsigned int readMessage(uint8_t *buffer, unsigned int size);
void writeMessage(const uint8_t *buffer, unsigned int size);

View File

@ -23,7 +23,8 @@ Process::~Process() {
}
size_t Process::write(uint8_t c) {
bridge.writeCommandInput(handle, &c, 1);
uint8_t cmd[] = {'I', handle, c};
bridge.transfer(cmd, 3);
return 1;
}
@ -61,7 +62,8 @@ void Process::doBuffer() {
// Try to buffer up to 32 characters
readPos = 0;
buffered = bridge.readCommandOutput(handle, buffer, sizeof(buffer));
uint8_t cmd[] = {'O', handle, sizeof(buffer)};
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
}
void Process::begin(String &command) {
@ -85,21 +87,30 @@ void Process::addParameter(String &param) {
}
void Process::runAsynchronously() {
uint8_t err;
handle = bridge.runCommand(*cmdline, err);
uint8_t cmd[] = {'R'};
uint8_t res[2];
bridge.transfer(cmd, 1, (uint8_t*)cmdline->c_str(), cmdline->length(), res, 2);
handle = res[1];
delete cmdline;
cmdline = NULL;
if (err==0)
if (res[0]==0) // res[0] contains error code
started = true;
}
boolean Process::running() {
return bridge.commandIsRunning(handle);
uint8_t cmd[] = {'r', handle};
uint8_t res[1];
bridge.transfer(cmd, 2, res, 1);
return (res[0] == 1);
}
unsigned int Process::exitValue() {
return bridge.commandExitValue(handle);
uint8_t cmd[] = {'W', handle};
uint8_t res[2];
bridge.transfer(cmd, 2, res, 2);
return (res[0] << 8) + res[1];
}
unsigned int Process::run() {
@ -110,8 +121,18 @@ unsigned int Process::run() {
}
void Process::close() {
if (started)
bridge.cleanCommand(handle);
if (started) {
uint8_t cmd[] = {'w', handle};
bridge.transfer(cmd, 2);
}
started = false;
}
// This method is currently unused
//static unsigned int __commandOutputAvailable(uint8_t handle) {
// uint8_t cmd[] = {'o', handle};
// uint8_t res[1];
// Bridge.transfer(cmd, 2, res, 1);
// return res[0];
//}

View File

@ -49,6 +49,7 @@ public:
// (write to process stdin)
size_t write(uint8_t);
void flush();
// TODO: add optimized function for block write
private:
BridgeClass &bridge;