-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetherdream.h
220 lines (186 loc) · 5.83 KB
/
netherdream.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef ETHERDREAM_H
#define ETHERDREAM_H
#define _POSIX_C_SOURCE 199309L
#define _DARWIN_C_SOURCE 1
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#endif
#include <thread>
#include <mutex>
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include "libs/common/protocol.h"
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#define BUFFER_POINTS_PER_FRAME 16000
#define BUFFER_NFRAMES 2
#define MAX_LATE_ACKS 64
#define MIN_SEND_POINTS 40
#define DEFAULT_TIMEOUT 2000000
#define DEBUG_THRESHOLD_POINTS 800
struct etherdream_point
{
int16_t x;
int16_t y;
uint16_t r;
uint16_t g;
uint16_t b;
uint16_t i;
uint16_t u1;
uint16_t u2;
};
struct etherdream_conn
{
int dc_sock;
char dc_read_buf[1024];
int dc_read_buf_size;
dac_response resp;
long long dc_last_ack_time;
struct
{
struct queue_command queue;
struct data_command_header header;
struct dac_point data[1000];
} __attribute__((packed)) dc_local_buffer;
int dc_begin_sent;
int ackbuf[MAX_LATE_ACKS];
int ackbuf_prod;
int ackbuf_cons;
int unacked_points;
int pending_meta_acks;
};
struct buffer_item
{
struct dac_point data[BUFFER_POINTS_PER_FRAME];
int points;
int pps;
int repeatcount;
int idx;
};
enum dac_state
{
ST_DISCONNECTED,
ST_READY,
ST_RUNNING,
ST_BROKEN,
ST_SHUTDOWN
};
struct netherdream
{
std::mutex mutex;
std::condition_variable loop_cond;
struct buffer_item buffer[BUFFER_NFRAMES];
int frame_buffer_read;
int frame_buffer_fullness;
int bounce_count;
std::thread workerthread;
struct in_addr addr;
struct etherdream_conn conn;
unsigned long dac_id;
int sw_revision;
char mac_address[6];
char version[32];
enum dac_state state;
netherdream* next;
};
/* etherdream_lib_start()
*
* Initialize the Ether Dream library and start a background thread to listen
* for DAC broadcasts. This should be called exactly once at program startup.
*
* Returns 0 on success, -1 on failure.
*/
int etherdream_lib_start(void);
/* etherdream_dac_count()
*
* Return the number of detected DACs since etherdream_lib_start() was called.
* Ether Dream DACs broadcast once per second, so calling code should wait a
* little over a second after etherdream_lib_start() to ensure that all DACs
* on the network are seen.
*/
int etherdream_dac_count(void);
/* etherdream_get(idx)
*
* Return the [i]'th detected DAC. This function accepts either an integer index
* (0-based) or an ID value as returned by etherdream_get_id(). Returns NULL
* if the requested DAC is not available.
*/
struct netherdream* etherdream_get(unsigned long idx);
/* etherdream_get_id(d)
*
* Return the ID value (equal to the second half of the MAC address, when
* represented in hex) of the given Ether Dream. Does not require that a
* connection to d has been established.
*/
unsigned long etherdream_get_id(struct netherdream* d);
/* etherdream_get_in_addr(d)
*
* Return the IP address of the given Ether Dream. Does not require that
* a connection to d has been established.
*/
const struct in_addr* etherdream_get_in_addr(struct netherdream* d);
/* etherdream_connect(d)
*
* Open a connection to d. This must be called before most other etherdream_
* functions can be used.
*/
int etherdream_connect(struct netherdream* d);
/* etherdream_is_connected(d)
*
* Returns 1 if the network connection to d is connected, 0 if not.
*/
int etherdream_is_connected(struct netherdream* d);
/* etherdream_is_ready(d)
*
* Return 1 if the local buffer for d can accept more frames, 0 if not, -1 on
* error (if the connection to d has not been opened or has failed).
*/
int etherdream_is_ready(struct netherdream* d);
/* etherdream_wait_for_ready(d)
*
* Block the invoking thread until more data can be written to d. Returns 0 on
* success, -1 if the connection to d is not open or has failed.
*/
int etherdream_wait_for_ready(struct netherdream* d);
/* etherdream_write(d, pts, npts, pps, repeatcount)
*
* Write a "frame" consisting of pts (length npts) to d.
*
* If repeatcount is -1, pts will be sent to the laser repeatedly until new
* data is received or until etherdream_stop is called. Otherwise, the points
* will be sent repeatedly at most npts times, and then the stream will
* automatically stop. pps specifies the output rate (30000 is a common value).
* repeatcount must not be 0.
*
* The Ether Dream uses a continuous streaming protocol, so if new frames are
* continuously sent, frame boundaries are not visible; however, to reduce
* overhead, frames should be reasonably large (at least 50-100 points).
*/
int etherdream_write(struct netherdream* d, const struct etherdream_point* pts,
int npts, int pps, int repeatcount);
/* etherdream_stop(d)
*
* Stop output from d as soon as the current frame is finished.
*/
int etherdream_stop(struct netherdream* d);
/* etherdream_disconnect(d)
*
* Close the TCP connection to d.
*/
void etherdream_disconnect(struct netherdream* d);
#ifdef __cplusplus
} // extern "c"
#endif
#endif // NETHERDREAM_H