From 965d2cbf7302c504fef01ab8951407bc1a40b430 Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@bug.st>
Date: Wed, 12 Jun 2013 12:42:38 +0200
Subject: Factored Process class

---
 libraries/Bridge/Bridge.cpp  | 48 --------------------------------------------
 libraries/Bridge/Bridge.h    | 15 --------------
 libraries/Bridge/Process.cpp | 39 ++++++++++++++++++++++++++---------
 libraries/Bridge/Process.h   |  1 +
 4 files changed, 31 insertions(+), 72 deletions(-)

(limited to 'libraries/Bridge')

diff --git a/libraries/Bridge/Bridge.cpp b/libraries/Bridge/Bridge.cpp
index 6f3e987..5802156 100644
--- a/libraries/Bridge/Bridge.cpp
+++ b/libraries/Bridge/Bridge.cpp
@@ -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);
diff --git a/libraries/Bridge/Bridge.h b/libraries/Bridge/Bridge.h
index 0c86175..f748bbf 100644
--- a/libraries/Bridge/Bridge.h
+++ b/libraries/Bridge/Bridge.h
@@ -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);
diff --git a/libraries/Bridge/Process.cpp b/libraries/Bridge/Process.cpp
index 59fe2b7..6b86d54 100644
--- a/libraries/Bridge/Process.cpp
+++ b/libraries/Bridge/Process.cpp
@@ -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];
+//}
+
diff --git a/libraries/Bridge/Process.h b/libraries/Bridge/Process.h
index ac22b15..2877545 100644
--- a/libraries/Bridge/Process.h
+++ b/libraries/Bridge/Process.h
@@ -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;
-- 
cgit v1.2.3-18-g5258