-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.cpp
344 lines (242 loc) · 8.17 KB
/
ipc.cpp
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
/*! @file ipc.c
* @brief Implements the IPC handling of the template application.
*/
#include <iostream>
using namespace std;
#include <leancv.h>
#include "includes.h"
#include "ipc.h"
#include "cgi/cgi.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
#ifdef OSC_HOST
#define HTTP_DIR "/var/www/"
//#define HTTP_DIR ""
#else
#define HTTP_DIR "/home/httpd/"
#endif
CIPC::CIPC(CCamera& camera) : m_camera(camera), m_bInit(false) {
img_count=0;
}
CIPC::~CIPC() {
}
OSC_ERR CIPC::Init() {
if(m_bInit) return(EALREADY_INITIALIZED);
m_fd=-1;
struct sockaddr_un addr;
addr.sun_family=AF_UNIX;
OscAssert_w(strlen(CGI_SOCKET_PATH) < sizeof(addr.sun_path), "Path too long");
strcpy(addr.sun_path, CGI_SOCKET_PATH);
unlink(addr.sun_path);
m_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
OscAssert_w(m_socket_fd >= 0, "Cannot open a socket.");
OscAssert_w(fcntl(m_socket_fd, F_SETFL, O_NONBLOCK) == 0
, "Error setting O_NONBLOCK: %s", strerror(errno));
OscAssert_w(bind(m_socket_fd, (struct sockaddr *) &addr, SUN_LEN(&addr)) == 0
, "Error binding the socket: %s", strerror(errno));
OscAssert_w(chmod(addr.sun_path, SERV_SOCKET_PERMISSIONS) >= 0
, "Unable to set access permissions of "
"socket file node \"%s\"! (%s)", addr.sun_path, strerror(errno));
OscAssert_w(listen(m_socket_fd, 5) == 0
, "Error listening on the socket: %s", strerror(errno));
/* init the web-settings */
m_web_settings.exposure_time=INIT_EXPOSURE_TIME;
m_bInit=true;
return(SUCCESS);
}
OSC_ERR CIPC::handleIpcRequests() {
if(!m_bInit) return(EGENERAL);
struct sockaddr remoteAddr;
unsigned int remoteAddrLen = sizeof(struct sockaddr);
m_fd = accept(m_socket_fd, &remoteAddr, &remoteAddrLen);
OSC_ERR err=SUCCESS;
if(m_fd<0) {
OscAssert_w(errno == EAGAIN, "Error accepting a connection: %s", strerror(errno));
} else {
if(fcntl(m_fd, F_SETFL, O_NONBLOCK) != 0) {
close(m_fd);
OscMark_format("Error setting O_NONBLOCK: %s", strerror(errno));
return(EASSERT);
}
ssize_t numRead, remaining=BUFFER_SIZE;
char buffer[BUFFER_SIZE+1];
while((numRead = read(m_fd, buffer+BUFFER_SIZE-remaining, remaining)) != 0) {
if(numRead > 0) {
remaining-=numRead;
}
}
buffer[BUFFER_SIZE-remaining]=0;
m_bHeader_written=false;
if(remaining < BUFFER_SIZE) {
ProcessRequest(buffer);
}
WriteHtmlHeader(HEADER_TEXT_PLAIN); //will be written if not written already
if(m_fd!=-1) close(m_fd);
m_fd=-1;
}
return(err);
}
void CIPC::WriteHtmlHeader(HTML_HEADER_TYPE type, int content_length) {
if(m_bHeader_written) return;
switch(type) {
case HEADER_IMAGE_BMP:
m_bHeader_written=true;
sprintf(m_buffer,
"Content-Length: %i\r\n" \
"Content-Type: image/ x-ms-bmp\r\n" \
"\r\n"
, content_length);
IpcWrite(m_buffer, strlen(m_buffer));
break;
case HEADER_TEXT_PLAIN:
m_bHeader_written=true;
const char* b="Status: 200 OK\n" \
"Content-Type: text/plain\n\n";
IpcWrite(b, strlen(b));
break;
}
}
void CIPC::ProcessRequest(char* request) {
char * header;
OscLog(INFO, "IPC Request:%s\n", request);
if(!(header=ReadHeader(&request))) return; /* wrong formated */
//Note: call WriteHtmlHeader BEFORE calling writeArgument
if(strcmp(header, "SetOptions") == 0) {
while (*request) {
char * key, * value;
if(ReadArgument(&request, &key, &value)==SUCCESS) {
if(strcmp(key, "autoExposure") == 0) {
m_camera.setAutoExposure(strtol(value, NULL, 10));
} else if (strcmp(key, "exposureTime") == 0) {
m_web_settings.exposure_time = strtol(value, NULL, 10)*1000;
OscCamSetShutterWidth(m_web_settings.exposure_time);
} else if (strcmp(key, "colorType") == 0) {
if (strcmp(value, "none") == 0)
m_camera.setColorType(ColorType_none);
else if (strcmp(value, "gray") == 0)
m_camera.setColorType( ColorType_gray);
else if (strcmp(value, "raw") == 0)
m_camera.setColorType(ColorType_raw);
else if (strcmp(value, "debayered") == 0)
m_camera.setColorType(ColorType_debayered);
} else if(strcmp(key, "perspective") == 0) {
m_camera.setPerspective((EnOscCamPerspective)atoi(value));
}
} else {
*request=0;
}
}
} else if (strcmp(header, "GetImageInfo") == 0) {
const char * pEnumBuf = NULL;
WriteHtmlHeader(HEADER_TEXT_PLAIN);
WriteArgument("width", m_camera.getROI().width);
WriteArgument("height", m_camera.getROI().height);
WriteArgument("exposureTime", (m_web_settings.exposure_time+500)/1000);
if (m_camera.getColorType() == ColorType_none) {
pEnumBuf = "none";
} else if (m_camera.getColorType() == ColorType_gray) {
pEnumBuf = "gray";
} else if (m_camera.getColorType() == ColorType_raw) {
pEnumBuf = "raw";
} else if (m_camera.getColorType() == ColorType_debayered) {
pEnumBuf = "debayered";
}
WriteArgument("colorType", pEnumBuf);
WriteArgument("perspective", m_camera.getPerspective());
WriteArgument("autoExposure", m_camera.getAutoExposure() ? 1 : 0);
} else if (strncmp(header, "GetImage", 8) == 0) {
/* read current picture and capture next */
IplImage* img=m_camera.ReadLatestPicture();
OSC_ERR e=m_camera.CapturePicture();
if(e!=SUCCESS) OscLog(ERROR, "Could not Capture Picture (Error=%i)", e);
if(img) {
++img_count;
uint32 startCyc=OscSupCycGet();
m_img_process.DoProcess(&img);
uint32 delta_time_us=OscSupCycToMicroSecs(OscSupCycGet() - startCyc);
OscLog(INFO, "Image processing required %uus\n", delta_time_us);
if(WriteBMP(img) !=SUCCESS) {
OscLog(ERROR, "Image could not be sent\n");
}
} else {
OscLog(ERROR, "Could not Read Latest Picture\n");
}
} else if (strcmp(header, "GetSystemInfo") == 0) {
WriteHtmlHeader(HEADER_TEXT_PLAIN);
struct OscSystemInfo * pInfo;
OscCfgGetSystemInfo(&pInfo);
WriteArgument("cameraModel", pInfo->hardware.board.revision);
WriteArgument("imageSensor", (pInfo->hardware.imageSensor.hasBayernPattern ? (char*)"Color" : (char*)"Grayscale"));
WriteArgument("uClinuxVersion", pInfo->software.uClinux.version);
}
}
char* CIPC::strtrim(char * str) {
char * end = strchr(str, 0) - 1;
while (*str != 0 && strchr(" \t\n", *str) != NULL)
str += 1;
while (end > str && strchr(" \t\n", *end) != NULL)
end -= 1;
*(end + 1) = 0;
return str;
}
char* CIPC::ReadHeader(char ** request) {
char* newline = strchr(*request, '\n');
if(!newline) return(NULL);
*newline = 0;
char* ret = strtrim(*request);
*request = newline + 1;
return(ret);
}
OSC_ERR CIPC::ReadArgument(char ** pBuffer, char ** pKey, char ** pValue) {
char * newline, * colon;
if(!(newline = strchr(*pBuffer, '\n'))) return(EINVALID_PARAMETER);
*newline = 0;
if(!(colon = strchr(*pBuffer, ':'))) return(EINVALID_PARAMETER);
*colon = 0;
*pKey = strtrim(*pBuffer);
*pValue = strtrim(colon + 1);
*pBuffer = newline + 1;
return(SUCCESS);
}
OSC_ERR CIPC::WriteArgument(const char * pKey, int value) {
int n = snprintf(m_buffer, m_buffer_count, "%s: %i\n", pKey, value);
if( IpcWrite(m_buffer, (size_t)n) < 0) return(EFILE_ERROR);
return(SUCCESS);
}
OSC_ERR CIPC::WriteArgument(const char * pKey, const char * pValue) {
int n = snprintf(m_buffer, m_buffer_count, "%s: %s\n", pKey, pValue);
if( IpcWrite(m_buffer, (size_t)n) < 0) return(EFILE_ERROR);
return(SUCCESS);
}
OSC_ERR CIPC::WriteBMP(const IplImage* img) {
char* header;
int header_size=lcvBmpHeader(img, &header);
if(header_size <= 0) return(EGENERAL);
if(img->width % 4 != 0) return(EINVALID_PARAMETER);
WriteHtmlHeader(HEADER_IMAGE_BMP, header_size+img->imageSize);
//write image header
if(IpcWrite(header, (size_t)header_size) <= 0) return(EGENERAL);
//write image data
for(int row=0; row<img->height; ++row) {
if(IpcWrite(img->imageData + img->widthStep*(img->height-row-1), (size_t)img->widthStep) <=0)
return(EGENERAL);
}
return(SUCCESS);
}
int CIPC::IpcWrite(const void* buf, size_t count) {
if(m_fd < 0) return(m_fd);
int ret;
while((ret=write(m_fd, buf, count)) == -1
&& (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR)) {
//resource temp. unavailable -> just try again
usleep(1000);
}
if(ret==-1) {
close(m_fd);
m_fd=-1;
}
return(ret);
}