summaryrefslogtreecommitdiffstats
path: root/src/common/port/port_base.h
blob: 3ae4787fe8d572be89406db758417a26aadb4a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#ifndef PORT_BASE_H
#define PORT_BASE_H

#include "port.h"
#include "common/global/log.h"

namespace Port
{

class Base : public Log::Base
{
public:
  Base(Log::Base &base) : Log::Base(&base) {}
  virtual PortType type() const { return description().type; }
  virtual Description description() const = 0;
  bool open();
  void close();

  virtual void log(Log::LineType kind, const QString &message);
  virtual void log(Log::DebugLevel level, const QString &message);
  void logData(const QString &s);

  enum { DEFAULT_TIMEOUT = 3000 }; // 3s
  bool sendChar(char c, uint timeout = DEFAULT_TIMEOUT) { return send(&c, 1, timeout); }
  bool send(const char *data, uint size, uint timeout = DEFAULT_TIMEOUT);
  bool send(const QMemArray<uchar> &a,  uint timeout = DEFAULT_TIMEOUT) { return send((const char *)a.data(), a.count(), timeout); }
  bool receiveChar(char &c, uint timeout = DEFAULT_TIMEOUT);
  bool receive(uint size, QString &s, uint timeout = DEFAULT_TIMEOUT);
  bool receive(uint size, QMemArray<uchar> &a, uint timeout = DEFAULT_TIMEOUT);

  virtual bool setPinOn(uint pin, bool on, LogicType type);
  virtual bool readPin(uint pin, LogicType type, bool &value);
  virtual QValueVector<PinData> pinData(IODir dir) const;
  virtual bool isGroundPin(uint pin) const;
  virtual uint groundPin() const;
  virtual IODir ioDir(uint pin) const;

protected:
  virtual bool internalOpen() = 0;
  virtual void internalClose() = 0;
  virtual bool internalSend(const char *, uint, uint) { qFatal("Not implemented"); return false; }
  virtual bool internalReceive(uint, char *, uint) { qFatal("Not implemented"); return false; }
  virtual void setSystemError(const QString &message) = 0;

private:
  bool _closing;
};

} // namespace

#endif