-
Notifications
You must be signed in to change notification settings - Fork 0
/
communication.h
350 lines (297 loc) · 12.3 KB
/
communication.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* Target side application for the Rich Viewer.
Copyright (C) 2008 Supercomputing Systems AG
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*! @file communication.h
* @brief Header file for the TCP/UDP communication.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include "inc/oscar.h"
#ifndef COMMUNICATION_H
#define COMMUNICATION_H
/******************************************************************************
*
* Host - Target protocol definitions
*
******************************************************************************/
/*! @brief Version of the Common base protocol.*/
#define CBP_VERSION 2008121600
/*! @brief Version of the Feed protocol. */
#define FEED_VERSION 2008121600
/*! @brief TCP port to exchange commands with the host. */
#define TCP_CMD_PORT 49100
/*! @brief TCP image feed port number */
#define TCP_FEED_PORT 49099
/*! @brief socket error value */
#define SOCK_ERROR -1
/*! @brief Maximum message size (bytes) */
#define MAX_MSG_BODY_LENGTH 64*1024
/******************************************************************************
* Message header
******************************************************************************/
/************ Message types **************/
/*! @brief Command to get version information. */
#define MSG_CMD_GET_VER 1
/*! @brief Command to set config registers. */
#define MSG_CMD_SET_CONFIG 10
/*! @brief Command to read out the complete config register file. */
#define MSG_CMD_GET_COMPL_CONFIG 20
/*! @brief Message contains feed data. */
#define MSG_FEED_DATA 30
/***** Status codes in struct MsgHdr *****/
/*! @brief Status code for a request. */
#define STATUS_REQUEST 20
/*! @brief Status code for a successful reply. */
#define STATUS_REPLY_SUCC 21
/*! @brief Status code for a failed reply. */
#define STATUS_REPLY_FAIL 22
/*! @brief Status code for a feed message. */
#define STATUS_FEED 30
/*! @brief Dummy placeholder structure for message types without commands. */
typedef struct _Generic_Params
{
/*! @brief Message type specific parameter 0. */
uint32 param0;
/*! @brief Message type specific parameter 1. */
uint32 param1;
/*! @brief Message type specific parameter 2. */
uint32 param2;
/*! @brief Message type specific parameter 3. */
uint32 param3;
} Generic_Params;
/*! @brief MsgHdr parameters for the request message of the GetVersion
command. */
typedef Generic_Params GetVersionReq_Params;
/*! @brief MsgHdr parameters for the reply message of the GetVersion
command. */
typedef struct _GetVersionReply_Params
{
/*! @brief Version of the Common Base Protocol. */
uint32 CBPVersion;
/*! @brief Version of the Feed Protocol. */
uint32 FeedProtVersion;
/*! @brief Target software version (Version of this program. */
uint32 TargetSWVersion;
/*! @brief unused */
uint32 unused3;
} GetVersionReply_Params;
/*! @brief MsgHdr parameters for the request message of the SetConfig
command. */
typedef Generic_Params SetConfigReq_Params;
/*! @brief MsgHdr parameters for the reply message of the SetConfig
command. */
typedef Generic_Params SetConfigReply_Params;
/*! @brief MsgHdr parameters for the request message of the GetCompleteConfig
command. */
typedef Generic_Params GetComplConfigReq_Params;
/*! @brief MsgHdr parameters for the reply message of the GetCompleteConfig
command. */
typedef Generic_Params GetcomplConfigReply_Params;
/*! @brief MsgHdr parameters for the feed protocol message. */
typedef Generic_Params FeedData_Params;
/*! @brief The header shared by all messages (commands and feed data). */
struct MsgHdr
{
/*! @brief Length of the body following this header in bytes. */
uint32 bodyLength;
/*! @brief Message type identifier. */
uint32 msgType;
/*! @brief This field may be used to identify messages e.g. for a
sequence number. */
uint32 ident;
/*! @brief Identifies whether the message is a request, reply, etc. */
uint32 status;
/*! @brief 4 additional values that depend on the message type. */
union _msgParams
{
GetVersionReq_Params getVerReq;
GetVersionReply_Params getVerReply;
SetConfigReq_Params setConfReq;
SetConfigReply_Params setConfReply;
GetComplConfigReq_Params getComplConfReq;
GetComplConfigReq_Params getComplConfReply;
FeedData_Params feedDataParams;
Generic_Params genericParams;
} msgParams;
};
/******************************************************************************
* Feed protocol header
******************************************************************************/
/*! @brief Convert a 4 byte character string to an unsigned int. */
#define STR_TO_UINT(str) ((str[0] << 24) | (str[1] << 16) | (str[2] << 8) | (str[3]))
/*! @brief Pixel format descriptor for 8 bit bayer pattern. */
#define V4L2_PIX_FMT_SBGGR8 STR_TO_UINT("BA81")
/*! @brief Pixel format descriptor for 8 bit greyscale images. */
#define V4L2_PIX_FMT_GREY STR_TO_UINT("GREY")
/*! @brief The header for the image data in the feed protocol. */
struct FeedHdr
{
/*! @brief Sequence number to detect communication problems. */
uint32 seqNr;
/*! @brief Number of milliseconds since start up of the target. */
uint32 timeStamp;
/*! @brief Width of the image following this header. */
uint32 imgWidth;
/*! @brief Height of the image following this header. */
uint32 imgHeight;
/*! @brief 4-character human readable code to identify how the pixels
are stored (equivalen to V4L2 pixel format descriptor). */
uint32 pixFmt;
};
/******************************************************************************
* Message packet
******************************************************************************/
/*! @brief The message type used in this protocol with maximum size*/
struct CommMsg
{
/*! @brief Message header. */
struct MsgHdr hdr;
/*! @brief Message body. */
uint8 body[MAX_MSG_BODY_LENGTH];
};
/******************************************************************************
*
* Register file
*
******************************************************************************/
/*! @brief Represents one configuration parameter as an ID-value pair. */
struct CBP_PARAM
{
uint32 id;
uint32 val;
};
/******************************************************************************
*
* Data container
*
******************************************************************************/
/*! @brief The different states of a pending request. */
enum EnRequestState
{
REQ_STATE_IDLE,
REQ_STATE_ACK_PENDING,
REQ_STATE_NACK_PENDING
};
/*! @brief Contains all communication-relevant variables. */
struct COMM
{
/*! @brief Socket for incoming UDP command packets */
int cmdSock;
/*! @brief Socket for outgoing TCP data packets */
int feedSock;
/*! @brief Socket for outgoing TCP feed after connection to host */
int connFeedSock;
/*! @brief Socket for command traffic after connection to host. */
int connCmdSock;
/*! @brief Buffer for incoming command packets. */
struct CommMsg cmdMsg;
/*! @brief The state of the last command request. */
enum EnRequestState enReqState;
/*! @brief Temporary buffer containing the initialized message
header of the feed protocol.*/
struct FeedHdr feedHdr;
/*! @brief Pointer to the register file of the main program. */
struct CBP_PARAM *pRegFile;
/*! @brief Number of entries (registers) in the register file. */
uint32 nRegs;
};
/******************************************************************************
*
* function prototypes
*
******************************************************************************/
/*! @brief Build the maximum of two numbers. */
#define MAX(a, b) (a >= b ? a : b)
/*********************************************************************//*!
* @brief Set a register in the configuration register file and invoke
* all actions that need to be done after a write to that specific
* register.
* This function is implemented within the main program but accessed by
* the communication part.
*
* @param pHsm Pointer to the state machine.
* @param pReg Pointer to the register id/value pair.
*//*********************************************************************/
OSC_ERR SetConfigRegister(void *pMainState, struct CBP_PARAM *pReg);
/*********************************************************************//*!
* @brief Accepts incoming connection on the feed and command socket.
*
* Sockets that are already connected are ignored.
*
* @param pComm Pointer to the communication status structure.
* @param timeout_ms Timeout of this function in milliseconds
* @return SUCCESS, -ETIMEOUT, -EDEVICE
*//*********************************************************************/
OSC_ERR Comm_AcceptConnections(struct COMM *pComm, int timeout_ms);
/*********************************************************************//*!
* @brief Send a new image over the feed.
*
* Attempts to send a new image over the feed. Fills out the message header.
* The feed header containing the format and size information of the image has
* to be supplied and filled out by the caller. If the feed socket is not
* connected, a call to this function returns with -ETRY_AGAIN;
*
* @param pComm Pointer to the communication status structure.
* @param pImg Pointer to the image to be sent.
* @param imgSize Total length of the image data.
* @param pFeedHdr Pointer to a filled out feed header for the image data.
* @return SUCCESS or an appropriate error code.
*//*********************************************************************/
OSC_ERR Comm_SendImage(struct COMM *pComm, const void* pImg, uint32 imgSize, const struct FeedHdr *pFeedHdr);
/*********************************************************************//*!
* @brief Check for new commands from the host and handle them.
*
* Commands only reading out the register file can be handled locally.
* Commands that need to invoke the state machine do this with the function
* SetConfigRegister.
* @see SetConfigRegister
*
* @param pComm Pointer to the communication status structure.
* @param pHsm Pointer to state machine
* @param Timeout of this function in milliseconds
* @return SUCCESS or an appropriate error code.
*//*********************************************************************/
OSC_ERR Comm_HandleCommands(struct COMM *pComm, void *pHsm, uint32 timeout_ms);
/*********************************************************************//*!
* @brief Logs the contents of a message to the console.
*
* @param pMsg Pointer to the message to be printed.
*//*********************************************************************/
void Comm_PrintMsg(const struct CommMsg *pMsg);
/*********************************************************************//*!
* @brief Initialize the command and feed sockets to be ready to accept
* connections.
*
* @see Comm_DeInit
*
* @param pComm Pointer to the communication status structure.
* @return SUCCESS or a suitable error code.
*//*********************************************************************/
OSC_ERR Comm_Init(struct COMM *pComm);
/*********************************************************************//*!
* @brief Deinitialize the command and feed sockets.
* @param pComm Pointer to the communication status structure.
*//*********************************************************************/
void Comm_DeInit(struct COMM *pComm);
#endif /* COMMUNICATION_H */