1/* 2 Copyright (c) 2013 Arduino LLC. All right reserved. 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17*/ 18 19#include <Process.h> 20 21Process::~Process() { 22close(); 23} 24 25size_tProcess::write(uint8_t c) { 26uint8_t cmd[] = {'I', handle, c}; 27 bridge.transfer(cmd,3); 28return1; 29} 30 31voidProcess::flush() { 32} 33 34intProcess::available() { 35// Look if there is new data available 36doBuffer(); 37return buffered; 38} 39 40intProcess::read() { 41doBuffer(); 42if(buffered ==0) 43return-1;// no chars available 44else{ 45 buffered--; 46return buffer[readPos++]; 47} 48} 49 50intProcess::peek() { 51doBuffer(); 52if(buffered ==0) 53return-1;// no chars available 54else 55return buffer[readPos]; 56} 57 58voidProcess::doBuffer() { 59// If there are already char in buffer exit 60if(buffered >0) 61return; 62 63// Try to buffer up to 32 characters 64 readPos =0; 65uint8_t cmd[] = {'O', handle,sizeof(buffer)}; 66 buffered = bridge.transfer(cmd,3, buffer,sizeof(buffer)); 67} 68 69voidProcess::begin(const String &command) { 70close(); 71 cmdline =newString(command); 72} 73 74voidProcess::addParameter(const String ¶m) { 75*cmdline +="\xFE"; 76*cmdline += param; 77} 78 79voidProcess::runAsynchronously() { 80uint8_t cmd[] = {'R'}; 81uint8_t res[2]; 82 bridge.transfer(cmd,1, (uint8_t*)cmdline->c_str(), cmdline->length(), res,2); 83 handle = res[1]; 84 85delete cmdline; 86 cmdline = NULL; 87 88if(res[0] ==0)// res[0] contains error code 89 started =true; 90} 91 92boolean Process::running() { 93uint8_t cmd[] = {'r', handle}; 94uint8_t res[1]; 95 bridge.transfer(cmd,2, res,1); 96return(res[0] ==1); 97} 98 99unsigned intProcess::exitValue() { 100uint8_t cmd[] = {'W', handle}; 101uint8_t res[2]; 102 bridge.transfer(cmd,2, res,2); 103return(res[0] <<8) + res[1]; 104} 105 106unsigned intProcess::run() { 107runAsynchronously(); 108while(running()) 109delay(100); 110returnexitValue(); 111} 112 113voidProcess::close() { 114if(started) { 115uint8_t cmd[] = {'w', handle}; 116 bridge.transfer(cmd,2); 117} 118 started =false; 119} 120 121unsigned intProcess::runShellCommand(const String &command) { 122runShellCommandAsynchronously(command); 123while(running()) 124delay(100); 125returnexitValue(); 126} 127 128voidProcess::runShellCommandAsynchronously(const String &command) { 129begin("/bin/ash"); 130addParameter("-c"); 131addParameter(command); 132runAsynchronously(); 133} 134 135// This method is currently unused 136//static unsigned int __commandOutputAvailable(uint8_t handle) { 137// uint8_t cmd[] = {'o', handle}; 138// uint8_t res[1]; 139// Bridge.transfer(cmd, 2, res, 1); 140// return res[0]; 141//} 142