[136] | 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 |
|
---|
| 21 | Process::~Process() {
|
---|
| 22 | close();
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | size_t Process::write(uint8_t c) {
|
---|
| 26 | uint8_t cmd[] = {'I', handle, c};
|
---|
| 27 | bridge.transfer(cmd, 3);
|
---|
| 28 | return 1;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | void Process::flush() {
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | int Process::available() {
|
---|
| 35 | // Look if there is new data available
|
---|
| 36 | doBuffer();
|
---|
| 37 | return buffered;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | int Process::read() {
|
---|
| 41 | doBuffer();
|
---|
| 42 | if (buffered == 0)
|
---|
| 43 | return -1; // no chars available
|
---|
| 44 | else {
|
---|
| 45 | buffered--;
|
---|
| 46 | return buffer[readPos++];
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | int Process::peek() {
|
---|
| 51 | doBuffer();
|
---|
| 52 | if (buffered == 0)
|
---|
| 53 | return -1; // no chars available
|
---|
| 54 | else
|
---|
| 55 | return buffer[readPos];
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void Process::doBuffer() {
|
---|
| 59 | // If there are already char in buffer exit
|
---|
| 60 | if (buffered > 0)
|
---|
| 61 | return;
|
---|
| 62 |
|
---|
| 63 | // Try to buffer up to 32 characters
|
---|
| 64 | readPos = 0;
|
---|
| 65 | uint8_t cmd[] = {'O', handle, sizeof(buffer)};
|
---|
| 66 | buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void Process::begin(const String &command) {
|
---|
| 70 | close();
|
---|
| 71 | cmdline = new String(command);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void Process::addParameter(const String ¶m) {
|
---|
| 75 | *cmdline += "\xFE";
|
---|
| 76 | *cmdline += param;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void Process::runAsynchronously() {
|
---|
| 80 | uint8_t cmd[] = {'R'};
|
---|
| 81 | uint8_t res[2];
|
---|
| 82 | bridge.transfer(cmd, 1, (uint8_t*)cmdline->c_str(), cmdline->length(), res, 2);
|
---|
| 83 | handle = res[1];
|
---|
| 84 |
|
---|
| 85 | delete cmdline;
|
---|
| 86 | cmdline = NULL;
|
---|
| 87 |
|
---|
| 88 | if (res[0] == 0) // res[0] contains error code
|
---|
| 89 | started = true;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | boolean Process::running() {
|
---|
| 93 | uint8_t cmd[] = {'r', handle};
|
---|
| 94 | uint8_t res[1];
|
---|
| 95 | bridge.transfer(cmd, 2, res, 1);
|
---|
| 96 | return (res[0] == 1);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | unsigned int Process::exitValue() {
|
---|
| 100 | uint8_t cmd[] = {'W', handle};
|
---|
| 101 | uint8_t res[2];
|
---|
| 102 | bridge.transfer(cmd, 2, res, 2);
|
---|
| 103 | return (res[0] << 8) + res[1];
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | unsigned int Process::run() {
|
---|
| 107 | runAsynchronously();
|
---|
| 108 | while (running())
|
---|
| 109 | delay(100);
|
---|
| 110 | return exitValue();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void Process::close() {
|
---|
| 114 | if (started) {
|
---|
| 115 | uint8_t cmd[] = {'w', handle};
|
---|
| 116 | bridge.transfer(cmd, 2);
|
---|
| 117 | }
|
---|
| 118 | started = false;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | unsigned int Process::runShellCommand(const String &command) {
|
---|
| 122 | runShellCommandAsynchronously(command);
|
---|
| 123 | while (running())
|
---|
| 124 | delay(100);
|
---|
| 125 | return exitValue();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void Process::runShellCommandAsynchronously(const String &command) {
|
---|
| 129 | begin("/bin/ash");
|
---|
| 130 | addParameter("-c");
|
---|
| 131 | addParameter(command);
|
---|
| 132 | runAsynchronously();
|
---|
| 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 |
|
---|