Map27
 All Classes Functions Variables Enumerations Enumerator Pages
DataLayer.h
1 // DataLayer.h
2 //
3 // Author: Mike McCauley (mikem@airspayce.com)
4 // Copyright (C) 2013 Mike McCauley
5 // $Id: DataLayer.h,v 1.3 2013/07/13 11:28:33 mikem Exp $
6 
7 #ifndef MAP27_DATALAYER_H
8 #define MAP27_DATALAYER_H
9 
10 #include <stdint.h>
11 
12 #include "Map27.h"
13 #include "Timer.h"
14 
15 class Map27PhysicalLayer;
16 class Map27NetworkLayer;
17 
27 {
28 public:
31 
37  void poll();
38 
43 
47 
52 
56 
61 
62  void setBaud(uint32_t baud);
63 
67  uint32_t baud();
68 
70 
74  void power_on();
75 
78 
84  void link_request(uint8_t n1, uint8_t k, uint8_t version);
85 
90  void link_acknowledge(uint8_t n_r, uint8_t n_k);
91 
98  void link_transfer(uint8_t n_s, uint8_t ar, uint8_t* buf, uint16_t bufLen);
99 
103 
106  void network_layer_reset();
107 
111  void credit_value(uint8_t r_k);
112 
118  bool network_layer_packet(uint8_t* buf, uint16_t bufLen);
119 
123  bool isReady();
124 
125 protected:
126  void reset();
127  void resetLink();
128 
129  // Timer events A2.2.3
130  void link_establishment_timeout(); // T0
131  void retry_timeout(); // T1
132  void acknowledgement_timeout(); // T2
133  void activity_timeout(); // T3
134  void link_failure_detection_timeout(); // T4
135 
136  // Tasks A2.2.10
137  void adjust_link_parameters(uint8_t n1, uint8_t k, uint8_t version);
138  void decrement_retransmission_counter();
139  void decrement_send_credit();
140  void delete_acknowledged_packets(uint8_t n_r);
141  void increment_send_state();
142  void initialise_variables();
143  void initialise_rs232_port();
144  void maximise_link_parameters();
145  void record_send_credit(uint8_t n_r, uint8_t n_k);
146  void rewind_packet_number(uint8_t n_r);
147  void set_retransmission_counter(uint8_t c1xo);
148  void store_acknowledged_rx_sequence_number(uint8_t n_r);
149  void store_packet(uint8_t* buf, uint16_t bufLen);
150  void store_receive_credit();
151  void update_receive_credit(uint8_t r_k);
152  // Missing from list:
153  void increment_receive_state();
154  void decrement_receive_credit();
155 
156  // IFs A2.2.11
157  bool acknowledgement_inside_window(uint8_t n_r);
158  bool all_transmitted_packets_acknowledged(uint8_t n_r);
159  bool immediate_reply_requested(uint8_t ar);
160  bool packet_out_of_sequence(uint8_t n_s);
161  bool packet_outside_window(uint8_t n);
162  bool receive_credit_available();
163  bool received_parameters_acceptable(uint8_t n1, uint8_t k, uint8_t version);
164  bool repeated_link_acknowledge(uint8_t n_r);
165  bool retransmission_count_zero();
166  bool send_credit_available();
167 
169  virtual void output_link_ready();
170  virtual void output_link_failure();
171  virtual void output_packet_accepted();
172  virtual void output_packet_rejected();
173  virtual void output_network_layer_packet(uint8_t* buf, uint16_t bufLen);
174 
175  // Outputs to Physical Layer A2.2.6
176  virtual void output_link_request();
177  virtual void output_link_acknowledge();
178  virtual void output_link_transfer();
179 
180  // Callback glue
181  static void link_establishment_timer_callback_glue(void* target);
182  static void retry_timer_callback_glue(void* target);
183  static void acknowledgement_timer_callback_glue(void* target);
184  static void activity_timer_callback_glue(void* target);
185  static void link_failure_detection_timer_callback_glue(void* target);
186 
187  uint32_t calc_retry_timeout();
188  uint32_t calc_acknowledgement_timeout();
189 
190 private:
191  typedef enum
192  {
193  LINK_STATE_RESET_WAIT = 1,
194  LINK_STATE_LINK_WAIT,
195  LINK_STATE_READY,
196  } LinkState;
197 
198  Map27PhysicalLayer* _physicalLayer;
199  Map27NetworkLayer* _networkLayer;
200 
201  uint32_t _baud;
202  LinkState _link_state; // Current state of Data Layer
203 
204  // Negotiated link parameter variables
205  uint8_t _maximum_length; // Negotiated N1
206  uint8_t _window_size; // Neotiated k
207  uint8_t _version; // Negotiated VERSION
208 
209  // Variables and sequence numbers 4.3.2.4
210  uint8_t _send_state; // V(S)
211  uint8_t _receive_state; // V(R)
212  uint8_t _receive_credit; // R(k)
213  uint8_t _send_credit; // S(k)
214  uint8_t _stored_acknowledged_rx_sequence_number; // SN(R)
215  uint8_t _stored_receive_credit; // SR(k);
216  uint8_t _retransmission_count; // C1
217 
218  uint8_t _last_n_r; // rx_sequence_number (n_r) from last LA
219  uint8_t _second_last_n_r; // rx_sequence_number (n_r) from second last LA
220 
221  // Timers
222  Map27Timer _link_establishment_timer;
223  Map27Timer _retry_timer;
224  Map27Timer _acknowledgement_timer;
225  Map27Timer _activity_timer;
226  Map27Timer _link_failure_detection_timer;
227 
228  // Transmitter message queue
229  uint8_t _txQueueBuf[MAP27_MAX_MESSAGE_LEN][MAP27_WINDOW_SIZE]; // Message contents
230  uint8_t _txQueueBuflen[MAP27_WINDOW_SIZE];
231  uint8_t _txQueueLength; // Number of messages in queue
232  uint8_t _txBottom; // Index of oldest unacknowledged message in queue
233  uint8_t _txNext; // Index of next message to be sent.
234 };
235 
236 #endif
Manage the Physical Layer of the Map27 protocol stack.
Definition: PhysicalLayer.h:37
bool isReady()
Definition: DataLayer.cpp:698
void link_request(uint8_t n1, uint8_t k, uint8_t version)
Definition: DataLayer.cpp:160
virtual void output_link_ready()
Outputs to Network Layer A2.2.7.
Definition: DataLayer.cpp:663
Map27NetworkLayer * networkLayer()
Definition: DataLayer.cpp:90
Manage the Data Layer of the Map27 protocol stack.
Definition: DataLayer.h:26
void network_layer_reset()
Definition: DataLayer.cpp:113
Manage the Network Layer of the Map27 protocol stack.
Definition: NetworkLayer.h:281
void setBaud(uint32_t baud)
Definition: DataLayer.cpp:95
void link_transfer(uint8_t n_s, uint8_t ar, uint8_t *buf, uint16_t bufLen)
Definition: DataLayer.cpp:279
void link_acknowledge(uint8_t n_r, uint8_t n_k)
Definition: DataLayer.cpp:204
Map27PhysicalLayer * physicalLayer()
Definition: DataLayer.cpp:80
void setPhysicalLayer(Map27PhysicalLayer *physicalLayer)
Definition: DataLayer.cpp:75
uint32_t baud()
Definition: DataLayer.cpp:100
Class for one-shot millisecond timers.
Definition: Timer.h:37
void setNetworkLayer(Map27NetworkLayer *networkLayer)
Definition: DataLayer.cpp:85
Map27DataLayer()
Constructor.
Definition: DataLayer.cpp:42
void power_on()
Definition: DataLayer.cpp:105
bool network_layer_packet(uint8_t *buf, uint16_t bufLen)
Definition: DataLayer.cpp:410
void poll()
Definition: DataLayer.cpp:64
void credit_value(uint8_t r_k)
Definition: DataLayer.cpp:118