Map27
 All Classes Functions Variables Enumerations Enumerator Pages
PhysicalLayer.h
1 // PhysicalLayer.h
2 //
3 // Author: Mike McCauley (mikem@airspayce.com)
4 // Copyright (C) 2013 Mike McCauley
5 // $Id: PhysicalLayer.h,v 1.5 2013/07/13 11:28:33 mikem Exp $
6 
7 
8 #ifndef MAP27_PHYSICALLAYER_H
9 #define MAP27_PHYSICALLAYER_H
10 
11 #include <stdint.h>
12 
13 #include "Map27.h"
14 #include "FCS.h"
15 
16 class Map27Port;
17 class Map27DataLayer;
18 
19 // Special characters
20 #define STX 0x02
21 #define ETX 0x03
22 #define DLE 0x10
23 #define SYN 0x16
24 
25 // Physical Layer Message types
26 #define MAP27_PHYSICAL_TYPE_LR 0x01
27 #define MAP27_PHYSICAL_TYPE_LA 0x02
28 #define MAP27_PHYSICAL_TYPE_LT 0x04
29 
38 {
39 public:
42 
46  void setPort(Map27Port* port);
47 
50  Map27Port* port();
51 
56 
60 
63  void reset();
64 
69  bool configure_rs232_port(uint32_t baud = 9600);
70 
76  void send(uint8_t *buf, uint16_t len);
77 
81  virtual void sendToPort(uint8_t ch);
82 
88  void sendLinkRequest(uint8_t n1, uint8_t k, uint8_t version);
89 
94  void sendLinkAcknowledge(uint8_t n_r, uint8_t n_k);
95 
102  void sendLinkTransfer(uint8_t n_s, uint8_t ar, uint8_t* buf, uint16_t bufLen);
103 
109  void poll();
110 
113  void clearRxBuf();
114 
115 private:
116  void handleRx(uint8_t ch);
117  virtual void handleMessage();
118  bool appendRxBuffer(uint8_t ch);
119  virtual void handleLinkRequest(uint8_t n1, uint8_t k, uint8_t version);
120  virtual void handleLinkAcknowledge(uint8_t n_r, uint8_t n_k);
121  virtual void handleLinkTransfer(uint8_t n_s, uint8_t ar, uint8_t* buf, uint16_t bufLen);
122  void sendFrameStart();
123  void sendFrameData(uint8_t *buf, uint16_t len);
124  void sendFrameEnd();
125  void resetRx();
126 
127  // Receiver states
128  typedef enum
129  {
130  RX_STATE_IDLE = 1,
131  RX_STATE_SYN = 2,
132  RX_STATE_DLE = 3,
133  RX_STATE_DATA = 4,
134  RX_STATE_ESCAPE = 5,
135  RX_STATE_WAIT_FCS1 = 6,
136  RX_STATE_WAIT_FCS2 = 7
137  } PhysicalRxState;
138 
139  // Physical Layer header structures
140  typedef struct
141  {
142  uint8_t type; // MAP27_PHYSICAL_TYPE_LR
143  uint8_t n1; // Maximum length
144  uint8_t k; // Window size
145  uint8_t version; // MAP27_PHYSICAL_VERSION
146  } Map27PhysicalLR;
147 
148  typedef struct
149  {
150  uint8_t type; // MAP27_PHYSICAL_TYPE_LA
151  uint8_t n_r; // Rx sequence number N(R)
152  uint8_t n_k; // Rx credit number N(k)
153  uint8_t reserved; // 0
154  } Map27PhysicalLA;
155 
156  typedef struct
157  {
158  uint8_t type; // MAP27_PHYSICAL_TYPE_LT
159  uint8_t n_s; // Tx sequence number
160  uint8_t ar; // Acknowledgement request 0/1
161  uint8_t reserved; // 0
162  uint8_t data[0];
163  } Map27PhysicalLT;
164 
165  Map27Port* _port;
166  Map27DataLayer* _dataLayer;
167  Map27FCS _rxFcs;
168  Map27FCS _txFcs;
169  PhysicalRxState _rxState;
170  uint8_t _rxBuf[MAP27_MAX_MESSAGE_LEN];
171  uint16_t _rxBufLen;
172  uint16_t _rxRecdFcs;
173 };
174 
175 #endif
Manage the Physical Layer of the Map27 protocol stack.
Definition: PhysicalLayer.h:37
Class for calculating Map27 Frame Check Sequence.
Definition: FCS.h:22
void sendLinkRequest(uint8_t n1, uint8_t k, uint8_t version)
Definition: PhysicalLayer.cpp:209
void sendLinkAcknowledge(uint8_t n_r, uint8_t n_k)
Definition: PhysicalLayer.cpp:220
Manage the Data Layer of the Map27 protocol stack.
Definition: DataLayer.h:26
Abstract superclass for accessing RS232 port on the host.
Definition: Port.h:23
void setDataLayer(Map27DataLayer *dataLayer)
Definition: PhysicalLayer.cpp:46
void clearRxBuf()
Definition: PhysicalLayer.cpp:246
void send(uint8_t *buf, uint16_t len)
Definition: PhysicalLayer.cpp:100
Map27DataLayer * dataLayer()
Definition: PhysicalLayer.cpp:51
void poll()
Definition: PhysicalLayer.cpp:56
void setPort(Map27Port *port)
Definition: PhysicalLayer.cpp:30
void sendLinkTransfer(uint8_t n_s, uint8_t ar, uint8_t *buf, uint16_t bufLen)
Definition: PhysicalLayer.cpp:231
Map27PhysicalLayer()
Constructor.
Definition: PhysicalLayer.cpp:14
Map27Port * port()
Definition: PhysicalLayer.cpp:35
bool configure_rs232_port(uint32_t baud=9600)
Definition: PhysicalLayer.cpp:40
void reset()
Definition: PhysicalLayer.cpp:20
virtual void sendToPort(uint8_t ch)
Definition: PhysicalLayer.cpp:203