summaryrefslogtreecommitdiff
path: root/branches/historical-pic-firmware/tools/snap_packet.h
blob: 28fd56663c3e1f5b0751f60aad42c443e0276997 (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
60
61
62
63
// -*- c++ -*-
#ifndef _snap_packet_h
#define _snap_packet_h

typedef unsigned char byte;

class SNAP;

class SNAPTimeoutException {
};

class SNAPPacket
{
public:

  /// An empty packet
  SNAPPacket();

  SNAPPacket(SNAP &snap) {
    readPacket(snap);
  }

  /// Read a packet
  bool readPacket(SNAP &snap);

  /// Create a reply packet
  SNAPPacket reply();

  /// Send our content
  bool send(SNAP &snap, bool verbose);

  /// Length helper functions
  int getLength() const {
    int l = hdb1 & 0x0f;
    if (l & 8) return 8 << (l & 7);
    return l;
  }
  void setLength(int length) {
    hdb1 = (hdb1 & 0xf0) | (length > 7 ? 8 : length);
  }

  bool isAck() {
    return ((hdb2 & 3) == 2);
  }

  bool isNak() {
    return ((hdb2 & 3) == 3);
  }

  byte sync;
  byte hdb2;
  byte hdb1;
  byte dab;
  byte sab;
  byte payload[16];
  byte crc;

private:
  byte computeCRC(byte dataval);
};


#endif