forked from raspberrypi/userland
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRaspiMCmds.c
433 lines (421 loc) · 13.1 KB
/
RaspiMCmds.c
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Copyright (c) 2015, Broadcom Europe Ltd
Copyright (c) 2015, Silvan Melchior
Copyright (c) 2015, Robert Tidey
Copyright (c) 2015, James Hughes
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file RaspiMCmds.c
* Process Pipe Commands for RaspiMJPEG
*
* \date 9th Aprl 2015
* \Author: Silvan Melchior / Robert Tidey
*
* Description
*
* Pipe Commands all start with 2 char identifier
* Usage information in README_RaspiMJPEG.md
*/
//#define MOTION_INTERNAL
#include "RaspiMJPEG.h"
void process_cmd(char *readbuf, int length) {
typedef enum pipe_cmd_type{ca,im,tl,px,bo,tv,vi,an,as,at,ac,ab,sh,co,br,sa,is,vs,rl,ec,em,wb,ag,mm,ie,ce,ro,fl,ri,ss,qu,pv,bi,ru,md,sc,rs,bu,mn,mt,mi,ms,mb,me,mc,mx,mf,mz,vm,vp,wd,sy,um,cn,st,ls,qp,hp} pipe_cmd_type;
char pipe_cmds[] = "ca,im,tl,px,bo,tv,vi,an,as,at,ac,ab,sh,co,br,sa,is,vs,rl,ec,em,wb,ag,mm,ie,ce,ro,fl,ri,ss,qu,pv,bi,ru,md,sc,rs,bu,mn,mt,mi,ms,mb,me,mc,mx,mf,mz,vm,vp,wd,sy,um,cn,st,ls,qp,hp";
pipe_cmd_type pipe_cmd;
int parcount;
char pars[128][10];
long int par0;
char cmd[3];
char par[MAX_COMMAND_LEN];
char *parstring=0, *temp, *settingsback;
int key = -1;
if (length < 2 || length > (MAX_COMMAND_LEN - 2)) return;
//Get cmd
strncpy(cmd, readbuf, 2);
cmd[2] = 0;
//find 2 letter command and translate into enum
temp = strstr(pipe_cmds, cmd);
if (temp == NULL) {
printf("cmd not found in\n", cmd);
return;
}
pipe_cmd = (pipe_cmd_type)((temp - pipe_cmds) / 3);
if(length > 3) {
strcpy(par, readbuf + 3);
par[length-3] = 0;
//extract space separated numeric parameters
// and make separate string parameter (strtok changes the original)
asprintf(&parstring, "%s", par);
parcount = 0;
temp = strtok(par, " ");
while(parcount<10 && temp != NULL) {
strcpy(pars[parcount], temp);
parcount++;
temp = strtok(NULL, " ");
}
par0 = strtol(pars[0], NULL, 10);
} else {
par0 = 0;
}
switch(pipe_cmd) {
case ca:
if(par0 == 1) {
if (parcount > 1) {
long vtime = strtol(pars[1], NULL, 10);
video_stoptime = time(NULL) + vtime;
video_stoptimeEnd = video_stoptime;
printLog("Capturing %d seconds\n", vtime);
}
if (cfg_val[c_video_split] > 0) {
video_stoptime = time(NULL) + cfg_val[c_video_split];
printLog("Capturing with split of %d seconds\n", cfg_val[c_video_split]);
}
start_video(0);
} else {
stop_video(0);
reset_motion_state();
}
break;
case im:
capt_img();
break;
case tl:
if(par0) {
timelapse = 1;
lapse_cnt = 1;
updateStatus();
printLog("Timelapse started\n");
}
else {
image2_cnt++;
timelapse = 0;
reset_motion_state();
updateStatus();
printLog("Timelapse stopped\n");
}
break;
case px:
stop_all();
addUserValue(c_video_width, pars[0]);
addUserValue(c_video_height, pars[1]);
addUserValue(c_video_fps, pars[2]);
addUserValue(c_MP4Box_fps, pars[3]);
addUserValue(c_image_width, pars[4]);
addUserValue(c_image_height, pars[5]);
addUserValue(c_fps_divider, pars[6]);
start_all(0);
break;
case qp:
stop_all();
addUserValue(c_minimise_frag, pars[0]);
addUserValue(c_initial_quant, pars[1]);
addUserValue(c_encode_qp, pars[2]);
start_all(0);
break;
case bo:
addUserValue(c_MP4Box, pars[0]);
break;
case tv:
addUserValue(c_tl_interval, pars[0]);
break;
case vi:
addUserValue(c_video_split, pars[0]);
break;
case an:
addUserValue(c_annotation, parstring);
break;
case as:
addUserValue(c_anno_text_size, pars[0]);
break;
case at:
addUserValue(c_anno3_custom_text_colour, pars[0]);
addUserValue(c_anno3_custom_text_Y, pars[1]);
addUserValue(c_anno3_custom_text_U, pars[2]);
addUserValue(c_anno3_custom_text_V, pars[3]);
break;
case ac:
addUserValue(c_anno3_custom_background_colour, pars[0]);
addUserValue(c_anno3_custom_background_Y, pars[1]);
addUserValue(c_anno3_custom_background_U, pars[2]);
addUserValue(c_anno3_custom_background_V, pars[3]);
break;
case ab:
addUserValue(c_anno_background, pars[0]);
break;
case sh:
key = c_sharpness;
break;
case co:
key = c_contrast;
break;
case br:
key = c_brightness;
break;
case sa:
key = c_saturation;
break;
case is:
key = c_iso;
break;
case vs:
key = c_video_stabilisation;
break;
case rl:
key = c_raw_layer;
break;
case ec:
key = 1000 + c_exposure_compensation;
break;
case em:
key = 1000 + c_exposure_mode;
break;
case wb:
key = 1000 + c_white_balance;
break;
case ag:
addUserValue(c_autowbgain_b, pars[1]);
key = c_autowbgain_r;
break;
case mm:
key = 1000 + c_metering_mode;
break;
case ie:
key = 1000 + c_image_effect;
break;
case ce:
addUserValue(c_colour_effect_u, pars[1]);
addUserValue(c_colour_effect_v, pars[2]);
key = c_colour_effect_en;
break;
case ro:
key = c_rotation;
break;
case fl:
if(par0 & 1) addUserValue(c_hflip, "1"); else addUserValue(c_hflip, "0");
if((par0 >> 1) & 1) addUserValue(c_vflip, "1"); else addUserValue(c_vflip, "0");
cam_set(c_hflip);
break;
case ri:
addUserValue(c_sensor_region_y, pars[1]);
addUserValue(c_sensor_region_w, pars[2]);
addUserValue(c_sensor_region_h, pars[3]);
key = c_sensor_region_x;
break;
case ss:
key = c_shutter_speed;
break;
case qu:
key = c_image_quality;
break;
case pv:
stop_all();
addUserValue(c_quality, pars[0]);
addUserValue(c_width, pars[1]);
addUserValue(c_divider, pars[2]);
start_all(0);
break;
case bi:
stop_all();
addUserValue(c_video_bitrate, pars[0]);
start_all(0);
break;
case st:
stop_all();
addUserValue(c_stat_pass, pars[0]);
start_all(0);
break;
case wd:
addUserValue(c_watchdog_interval, pars[0]);
addUserValue(c_watchdog_errors, pars[1]);
break;
case ru:
exec_macro(cfg_stru[c_do_cmd], readbuf);
if (par0 == 0) {
stop_all();
idle = 1;
printLog("Stream halted\n");
} else {
start_all(1);
idle = 0;
printLog("Stream continued\n");
}
updateStatus();
break;
case mx:
key = c_motion_external;
//If switching to internal with motion detection on then try to kill external motion
if (cfg_val[c_motion_detection] != 0 && !par0) {
if(system("killall motion 2> /dev/null") == -1) error("Could not stop external motion", 1);
printLog("External motion detection stopped\n");
}
break;
case md:
exec_macro(cfg_stru[c_do_cmd], readbuf);
stop_all();
if (cfg_val[c_motion_external] == 1) {
if(par0 == 0) {
if(system("killall motion 2> /dev/null") == -1) error("Could not stop external motion", 1);
printLog("External motion detection stopped\n");
}
else {
if (cfg_val[c_motion_detection] == 0) {
if(system("motion") == -1) error("Could not start external motion", 1);
printLog("External motion detection started\n");
} else {
printLog("Motion already running. md 1 ignored\n");
}
}
} else {
if(par0 == 0) {
printLog("Internal motion detection stopped\n");
}
else {
printLog("Internal motion detection started\n");
}
}
cfg_val[c_motion_detection] = par0?1:0;
start_all(0);
updateStatus();
break;
case sc:
set_counts();
printLog("Scan for highest count\n");
break;
case rs:
printLog("Reset settings. Backed up and cleared to defaults\n");
stop_all();
asprintf(&settingsback, "%s.bak", cfg_stru[c_user_config]);
saveUserConfig(settingsback);
if (settingsback != 0) free(settingsback);
read_config("/etc/raspimjpeg", 1);
saveUserConfig(cfg_stru[c_user_config]);
start_all(0);
break;
case bu:
key = c_video_buffer;
break;
case vp:
stop_all();
addUserValue(c_vector_preview, pars[0]);
start_all(0);
break;
case mn:
key = c_motion_noise;
break;
case mt:
key = c_motion_threshold;
break;
case mi:
key = c_motion_image + 1000;
break;
case ms:
key = c_motion_initframes;
break;
case mb:
key = c_motion_startframes;
break;
case me:
key = c_motion_stopframes;
break;
case mc:
key = c_motion_clip;
break;
case mf:
key = c_motion_file;
break;
case mz:
mask_disable((int)par0);
break;
case vm:
key = c_vector_mode;
break;
case sy:
exec_macro(parstring, NULL);
break;
case um:
temp = strchr(parstring, ' ');
if(temp) addUserValue(c_error_soft + par0, (temp+1));
break;
case cn:
stop_all();
addUserValue(c_camera_num, pars[0]);
start_all(0);
break;
case ls:
key = c_log_size;
break;
case hp:
key = c_hdmi_preview;
printLog("Setting HDMI Preview to %s\n", par0 ? "ON" : "OFF");
cam_set_preview(par0);
break;
default:
printLog("Unrecognised pipe command\n");
break;
}
//Action any key settings
if (key >= 0) {
if (key < 1000) {
addUserValue(key, pars[0]);
cam_set(key);
} else {
addUserValue(key - 1000, parstring);
cam_set(key - 1000);
}
}
saveUserConfig(cfg_stru[c_user_config]);
if (parstring != 0) free(parstring);
}
void exec_macro(char *macro, char *filename) {
char *cmd, *macropath;
char *s;
char async;
if (macro != NULL) {
if (filename != NULL && *macro == '&') {
asprintf(¯opath,"%s/%s", cfg_stru[c_macros_path], macro + 1);
async = '&';
} else {
asprintf(¯opath,"%s/%s", cfg_stru[c_macros_path], macro);
async = ' ';
}
s = strchr(macropath, ' ');
if (s != NULL) *s = 0;
if (access(macropath, F_OK ) != -1) {
if (s != NULL) *s = ' ';
if (filename != NULL)
asprintf(&cmd,"%s \"%s\" %c", macropath, filename, async);
else
asprintf(&cmd,"%s &", macropath);
printLog("Executing macro %s\n", cmd);
system(cmd);
free(cmd);
} else if (filename == NULL) {
printLog("Can't find macro %s\n", macropath);
}
if (s != NULL) *s = ' ';
free(macropath);
} else {
printLog("Missing macro definition\n");
}
}