|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _SERIAL_CONVERGENCE_LAYER_H_ |
|
18 #define _SERIAL_CONVERGENCE_LAYER_H_ |
|
19 |
|
20 #include <oasys/io/TTY.h> |
|
21 #include <oasys/serialize/Serialize.h> |
|
22 |
|
23 #include "StreamConvergenceLayer.h" |
|
24 |
|
25 namespace dtn { |
|
26 |
|
27 /** |
|
28 * The Serial Convergence Layer. |
|
29 */ |
|
30 class SerialConvergenceLayer : public StreamConvergenceLayer { |
|
31 public: |
|
32 /** |
|
33 * Current version of the protocol. |
|
34 */ |
|
35 static const u_int8_t SERIALCL_VERSION = 0x01; |
|
36 |
|
37 /** |
|
38 * Byte sent on the wire to synchronize the two ends. |
|
39 */ |
|
40 static const u_char SYNC = '.'; |
|
41 |
|
42 /** |
|
43 * Constructor. |
|
44 */ |
|
45 SerialConvergenceLayer(); |
|
46 |
|
47 /** |
|
48 * Tunable link parameter structure. |
|
49 */ |
|
50 class SerialLinkParams : public StreamLinkParams { |
|
51 public: |
|
52 bool hexdump_; ///< Log a hexdump of all traffic |
|
53 std::string initstr_; ///< String to initialize the tty |
|
54 u_int ispeed_; ///< Input speed on the tty |
|
55 u_int ospeed_; ///< Output speed on the tty |
|
56 u_int sync_interval_; ///< Interval to send initial sync bits |
|
57 |
|
58 protected: |
|
59 // See comment in LinkParams for why this is protected |
|
60 SerialLinkParams(bool init_defaults); |
|
61 friend class SerialConvergenceLayer; |
|
62 }; |
|
63 |
|
64 /** |
|
65 * Default link parameters. |
|
66 */ |
|
67 static SerialLinkParams default_link_params_; |
|
68 |
|
69 protected: |
|
70 /// @{ Virtual from ConvergenceLayer |
|
71 bool set_link_defaults(int argc, const char* argv[], |
|
72 const char** invalidp); |
|
73 void dump_link(const LinkRef& link, oasys::StringBuffer* buf); |
|
74 /// @} |
|
75 |
|
76 /// @{ Virtual from ConnectionConvergenceLayer |
|
77 virtual LinkParams* new_link_params(); |
|
78 virtual bool parse_link_params(LinkParams* params, |
|
79 int argc, const char** argv, |
|
80 const char** invalidp); |
|
81 virtual bool parse_nexthop(const LinkRef& link, LinkParams* params); |
|
82 virtual CLConnection* new_connection(const LinkRef& link, |
|
83 LinkParams* params); |
|
84 /// @} |
|
85 |
|
86 /** |
|
87 * Helper class (and thread) that manages an established |
|
88 * connection with a peer daemon. |
|
89 * |
|
90 * Although the same class is used in both cases, a particular |
|
91 * Connection is either a receiver or a sender, as indicated by |
|
92 * the direction variable. Note that to deal with NAT, the side |
|
93 * which does the active connect is not necessarily the sender. |
|
94 */ |
|
95 class Connection : public StreamConvergenceLayer::Connection { |
|
96 public: |
|
97 /** |
|
98 * Constructor for a connection. |
|
99 */ |
|
100 Connection(SerialConvergenceLayer* cl, |
|
101 const LinkRef& link, |
|
102 SerialLinkParams* params); |
|
103 |
|
104 /** |
|
105 * Destructor. |
|
106 */ |
|
107 virtual ~Connection(); |
|
108 |
|
109 /** |
|
110 * Virtual from SerializableObject |
|
111 */ |
|
112 virtual void serialize(oasys::SerializeAction *a); |
|
113 |
|
114 protected: |
|
115 friend class SerialConvergenceLayer; |
|
116 |
|
117 /// @{ Virtual from CLConnection |
|
118 virtual void connect(); |
|
119 virtual void disconnect(); |
|
120 virtual void initialize_pollfds(); |
|
121 virtual void handle_poll_timeout(); |
|
122 virtual void handle_poll_activity(); |
|
123 /// @} |
|
124 |
|
125 /// @{ virtual from StreamConvergenceLayer::Connection |
|
126 void send_data(); |
|
127 /// @} |
|
128 |
|
129 /// Hook for handle_poll_activity to receive data |
|
130 void recv_data(); |
|
131 |
|
132 /// Send a sync byte |
|
133 void send_sync(); |
|
134 |
|
135 /** |
|
136 * Utility function to downcast the params_ pointer that's |
|
137 * stored in the CLConnection parent class. |
|
138 */ |
|
139 SerialLinkParams* serial_lparams() |
|
140 { |
|
141 SerialLinkParams* ret = dynamic_cast<SerialLinkParams*>(params_); |
|
142 ASSERT(ret != NULL); |
|
143 return ret; |
|
144 } |
|
145 |
|
146 oasys::TTY* tty_; ///< The tty |
|
147 struct pollfd* tty_pollfd_; ///< Poll structure for the tty |
|
148 bool synced_; ///< Whether the SYNC has completed |
|
149 }; |
|
150 }; |
|
151 |
|
152 } // namespace dtn |
|
153 |
|
154 #endif /* _SERIAL_CONVERGENCE_LAYER_H_ */ |