1 /*
2     OW -- One-Wire filesystem
3     version 0.4 7/2/2003
4 
5     Function naming scheme:
6     OW -- Generic call to interaface
7     LI -- LINK commands
8     L1 -- 2480B commands
9     FS -- filesystem commands
10     UT -- utility functions
11 
12     Written 2003 Paul H Alfille
13         Fuse code based on "fusexmp" {GPL} by Miklos Szeredi, mszeredi@inf.bme.hu
14         Serial code based on "xt" {GPL} by David Querbach, www.realtime.bc.ca
15         in turn based on "miniterm" by Sven Goldt, goldt@math.tu.berlin.de
16     GPL license
17     This program is free software; you can redistribute it and/or
18     modify it under the terms of the GNU General Public License
19     as published by the Free Software Foundation; either version 2
20     of the License, or (at your option) any later version.
21 
22     This program is distributed in the hope that it will be useful,
23     but WITHOUT ANY WARRANTY; without even the implied warranty of
24     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25     GNU General Public License for more details.
26 */
27 
28 #ifndef OW_CONNECTION_H			/* tedious wrapper */
29 #define OW_CONNECTION_H
30 
31 /*
32 Jan 21, 2007 from the IANA:
33 owserver        4304/tcp   One-Wire Filesystem Server
34 owserver        4304/udp   One-Wire Filesystem Server
35 #                          Paul Alfille <paul.alfille@gmail.com> January 2007
36 
37 
38 See: http://www.iana.org/assignments/port-numbers
39 */
40 #define DEFAULT_SERVER_PORT       "4304"
41 #define DEFAULT_FTP_PORT          "21"
42 #define DEFAULT_HA7_PORT          "80"
43 #define DEFAULT_ENET_PORT         "8080"
44 #define DEFAULT_ENET2_PORT        "8080"
45 #define DEFAULT_LINK_PORT         "10001"
46 #define DEFAULT_XPORT_CONTROL_PORT "9999"
47 #define DEFAULT_XPORT_PORT        "10001"
48 #define DEFAULT_ETHERWEATHER_PORT "15862"
49 
50 #define HA7_DISCOVERY_PORT        "4567"
51 #define HA7_DISCOVERY_ADDRESS     "224.1.2.3"
52 
53 #define ENET2_DISCOVERY_PORT      30303
54 #define ENET2_DISCOVERY_ADDRESS   "255.255.255.255"
55 
56 #include "ow.h"
57 #include "ow_counters.h"
58 #include <sys/ioctl.h>
59 #include "ow_transaction.h"
60 
61 /* reset types */
62 #include "ow_reset.h"
63 
64 /* bus serach */
65 #include "ow_search.h"
66 
67 /* connect to a bus master */
68 #include "ow_detect.h"
69 
70 /* address for i2c or usb */
71 #include "ow_parse_address.h"
72 
73 /* w1 sequence defines */
74 #include "ow_w1_seq.h"
75 
76 #if OW_USB						/* conditional inclusion of USB */
77 /* Special libusb 0.1x search */
78 #include "ow_usb_cycle.h"
79 #endif /* OW_USB */
80 
81 /* large enough for arrays of 2048 elements of ~49 bytes each */
82 #define MAX_OWSERVER_PROTOCOL_PAYLOAD_SIZE  100050
83 
84 /* com port fifo info */
85 /* The UART_FIFO_SIZE defines the amount of bytes that are written before
86  * reading the reply. Any positive value should work and 16 is probably low
87  * enough to avoid losing bytes in even most extreme situations on all modern
88  * UARTs, and values bigger than that will probably work on almost any
89  * system... The value affects readout performance asymptotically, value of 1
90  * should give equivalent performance with the old implementation.
91  *   -- Jari Kirma
92  *
93  * Note: Each bit sent to the 1-wire net requeires 1 byte to be sent to
94  *       the uart.
95  */
96 #define UART_FIFO_SIZE 160
97 
98 /** USB bulk endpoint FIFO size
99   Need one for each for read and write
100   This is for alt setting "3" -- 64 bytes, 1msec polling
101 */
102 #define USB_FIFO_EACH 64
103 #define USB_FIFO_READ 0
104 #define USB_FIFO_WRITE USB_FIFO_EACH
105 #define USB_FIFO_SIZE ( USB_FIFO_EACH + USB_FIFO_EACH )
106 #define HA7_FIFO_SIZE 128
107 #define W1_FIFO_SIZE 128
108 #define LINK_FIFO_SIZE UART_FIFO_SIZE
109 #define HA7E_FIFO_SIZE UART_FIFO_SIZE
110 #define ENET_FIFO_SIZE 128
111 #define ENET2_FIFO_SIZE 128
112 #define HA5_FIFO_SIZE UART_FIFO_SIZE
113 #define LINKE_FIFO_SIZE 1500
114 #define I2C_FIFO_SIZE 1
115 
116 #if USB_FIFO_SIZE > UART_FIFO_SIZE
117 #define MAX_FIFO_SIZE USB_FIFO_SIZE
118 #else
119 #define MAX_FIFO_SIZE UART_FIFO_SIZE
120 #endif
121 
122 /* Debugging interface to showing all bus traffic
123  * You need to configure compile with
124  * ./configure --enable-owtraffic
125  * */
126 #include "ow_traffic.h"
127 
128 /* -------------------------------------------- */
129 /* Interface-specific routines ---------------- */
130 #include "ow_bus_routines.h"
131 
132 /* -------------------------------------------- */
133 /* Inbound connections (bus masters) ---------- */
134 #include "ow_port_in.h"
135 #include "ow_connection_in.h"
136 
137 /* -------------------------------------------- */
138 /* Outbound connections (ownet clients) ---------- */
139 #include "ow_connection_out.h"
140 
141 /* This bug-fix/workaround function seem to be fixed now... At least on
142  * the platforms I have tested it on... printf() in owserver/src/c/owserver.c
143  * returned very strange result on c->busmode before... but not anymore */
144 int BusIsServer(struct connection_in *in);
145 
146 // mode bit flags for level
147 #define MODE_NORMAL                    0x00
148 #define MODE_STRONG5                   0x01
149 #define MODE_PROGRAM                   0x02
150 #define MODE_BREAK                     0x04
151 
152 // 1Wire Bus Speed Setting Constants
153 #define ONEWIREBUSSPEED_REGULAR        0x00
154 #define ONEWIREBUSSPEED_FLEXIBLE       0x01	/* Only used for USB adapter */
155 #define ONEWIREBUSSPEED_OVERDRIVE      0x02
156 
157 /* Serial port */
158 GOOD_OR_BAD COM_open(struct connection_in *in);
159 GOOD_OR_BAD serial_open(struct connection_in *in);
160 GOOD_OR_BAD tcp_open(struct connection_in *in);
161 
162 GOOD_OR_BAD COM_test( struct connection_in * connection );
163 
164 void COM_set_standard( struct connection_in *connection) ;
165 
166 void COM_close(struct connection_in *in);
167 
168 void COM_free(struct connection_in *in);
169 void serial_free(struct connection_in *in);
170 void tcp_free(struct connection_in *in);
171 
172 void COM_flush( const struct connection_in *in);
173 void COM_break(struct connection_in *in);
174 GOOD_OR_BAD telnet_break(struct connection_in *in) ;
175 
176 GOOD_OR_BAD COM_change( struct connection_in *connection) ;
177 GOOD_OR_BAD serial_change(struct connection_in *connection) ;
178 GOOD_OR_BAD telnet_change(struct connection_in *in) ;
179 
180 GOOD_OR_BAD serial_powercycle(struct connection_in *connection) ;
181 
182 GOOD_OR_BAD COM_write( const BYTE * data, size_t length, struct connection_in *connection);
183 GOOD_OR_BAD COM_write_simple( const BYTE * data, size_t length, struct connection_in *connection);
184 GOOD_OR_BAD telnet_write_binary( const BYTE * buf, const size_t size, struct connection_in *in);
185 
186 GOOD_OR_BAD COM_read( BYTE * data, size_t length, struct connection_in *connection);
187 SIZE_OR_ERROR COM_read_with_timeout( BYTE * data, size_t length, struct connection_in *connection ) ;
188 
189 void COM_slurp( struct connection_in *in);
190 GOOD_OR_BAD telnet_purge(struct connection_in *in) ;
191 
192 GOOD_OR_BAD telnet_read(BYTE * buf, const size_t size, struct connection_in *in) ;
193 
194 
195 void FreeInAll(void);
196 void RemoveIn( struct connection_in * conn ) ;
197 
198 void FreeOutAll(void);
199 void DelIn(struct connection_in *in);
200 
201 struct connection_in *LinkIn(struct connection_in *in, struct port_in * head);
202 
203 void Add_InFlight( GOOD_OR_BAD (*nomatch)(struct port_in * trial,struct port_in * existing), struct port_in * new_pin );
204 void Del_InFlight( GOOD_OR_BAD (*nomatch)(struct port_in * trial,struct port_in * existing), struct port_in * new_pin );
205 
206 struct connection_in *find_connection_in(int nr);
207 int SetKnownBus( int bus_number, struct parsedname * pn) ;
208 
209 struct connection_out *NewOut(void);
210 
211 /* Bonjour registration */
212 void ZeroConf_Announce(struct connection_out *out);
213 void OW_Browse(struct connection_in *in);
214 
215 GOOD_OR_BAD TestConnection(const struct parsedname *pn);
216 
217 void ZeroAdd(const char * name, const char * type, const char * domain, const char * host, const char * service) ;
218 void ZeroDel(const char * name, const char * type, const char * domain ) ;
219 
220 #define STAT_ADD1_BUS( err, in )     STATLOCK; ++((in)->bus_stat[err]) ; STATUNLOCK
221 
222 #endif							/* OW_CONNECTION_H */
223