libraries / Bridge / src / Process.hon commit Added link to project report (97a3ba0)
   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#ifndef PROCESS_H_
  20#define PROCESS_H_
  21
  22#include <Bridge.h>
  23
  24class Process : public Stream {
  25  public:
  26    // Constructor with a user provided BridgeClass instance
  27    Process(BridgeClass &_b = Bridge) :
  28      bridge(_b), started(false), buffered(0), readPos(0) { }
  29    ~Process();
  30
  31    void begin(const String &command);
  32    void addParameter(const String &param);
  33    unsigned int run();
  34    void runAsynchronously();
  35    boolean running();
  36    unsigned int exitValue();
  37    void close();
  38
  39    unsigned int runShellCommand(const String &command);
  40    void runShellCommandAsynchronously(const String &command);
  41
  42    operator bool () {
  43      return started;
  44    }
  45
  46    // Stream methods
  47    // (read from process stdout)
  48    int available();
  49    int read();
  50    int peek();
  51    // (write to process stdin)
  52    size_t write(uint8_t);
  53    void flush();
  54    // TODO: add optimized function for block write
  55
  56  private:
  57    BridgeClass &bridge;
  58    uint8_t handle;
  59    String *cmdline;
  60    boolean started;
  61
  62  private:
  63    void doBuffer();
  64    uint8_t buffered;
  65    uint8_t readPos;
  66    static const int BUFFER_SIZE = 64;
  67    uint8_t buffer[BUFFER_SIZE];
  68
  69};
  70
  71#endif