/
RaspiHelpers.c
296 lines (255 loc) · 8.3 KB
/
RaspiHelpers.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
/*
Copyright (c) 2018, Raspberry Pi (Trading) Ltd.
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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <memory.h>
#include <unistd.h>
#include <stdint.h>
#include "interface/vcos/vcos.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/mmal_parameters_camera.h"
#include "interface/mmal/util/mmal_connection.h"
#include "RaspiCLI.h"
#include "RaspiPreview.h"
#include "RaspiCamControl.h"
#include "RaspiCommonSettings.h"
#ifndef GIT_COMMIT_ID
#define GIT_COMMIT_ID "Not found"
#endif
#if (GIT_TAINTED > 0)
#define TAINTED " Tainted"
#else
#define TAINTED ""
#endif
static const char *app_name;
void print_app_details(FILE *fd)
{
if (!app_name)
app_name = "Un-named";
fprintf(fd, "\n\"%s\" Camera App (commit %s%s)\n\n", basename(app_name), GIT_COMMIT_ID, TAINTED);
}
void display_valid_parameters(char *name, void (*app_help)(char*))
{
print_app_details(stdout);
// This should be defined in the main app source code
if (app_help)
(*app_help)(name);
// general settings
raspicommonsettings_display_help();
// Help for preview options
raspipreview_display_help();
// Now display any help information from the camcontrol code
raspicamcontrol_display_help();
fprintf(stdout, "\n");
}
void get_sensor_defaults(int camera_num, char *camera_name, int *width, int *height )
{
MMAL_COMPONENT_T *camera_info;
MMAL_STATUS_T status;
// Default to the OV5647 setup
strncpy(camera_name, "OV5647", MMAL_PARAMETER_CAMERA_INFO_MAX_STR_LEN);
// Try to get the camera name and maximum supported resolution
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_CAMERA_INFO, &camera_info);
if (status == MMAL_SUCCESS)
{
MMAL_PARAMETER_CAMERA_INFO_T param;
param.hdr.id = MMAL_PARAMETER_CAMERA_INFO;
param.hdr.size = sizeof(param)-4; // Deliberately undersize to check firmware version
status = mmal_port_parameter_get(camera_info->control, ¶m.hdr);
if (status != MMAL_SUCCESS)
{
// Running on newer firmware
param.hdr.size = sizeof(param);
status = mmal_port_parameter_get(camera_info->control, ¶m.hdr);
if (status == MMAL_SUCCESS && param.num_cameras > camera_num)
{
// Take the parameters from the first camera listed.
if (*width == 0)
*width = param.cameras[camera_num].max_width;
if (*height == 0)
*height = param.cameras[camera_num].max_height;
strncpy(camera_name, param.cameras[camera_num].camera_name, MMAL_PARAMETER_CAMERA_INFO_MAX_STR_LEN);
camera_name[MMAL_PARAMETER_CAMERA_INFO_MAX_STR_LEN-1] = 0;
}
else
vcos_log_error("Cannot read camera info, keeping the defaults for OV5647");
}
else
{
// Older firmware
// Nothing to do here, keep the defaults for OV5647
}
mmal_component_destroy(camera_info);
}
else
{
vcos_log_error("Failed to create camera_info component");
}
// default to OV5647 if nothing detected..
if (*width == 0)
*width = 2592;
if (*height == 0)
*height = 1944;
}
void set_app_name(const char *name)
{
app_name = name;
}
const char *get_app_name()
{
return app_name;
}
/**
* Connect two specific ports together
*
* @param output_port Pointer the output port
* @param input_port Pointer the input port
* @param Pointer to a mmal connection pointer, reassigned if function successful
* @return Returns a MMAL_STATUS_T giving result of operation
*
*/
MMAL_STATUS_T connect_ports(MMAL_PORT_T *output_port, MMAL_PORT_T *input_port, MMAL_CONNECTION_T **connection)
{
MMAL_STATUS_T status;
status = mmal_connection_create(connection, output_port, input_port, MMAL_CONNECTION_FLAG_TUNNELLING | MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT);
if (status == MMAL_SUCCESS)
{
status = mmal_connection_enable(*connection);
if (status != MMAL_SUCCESS)
mmal_connection_destroy(*connection);
}
return status;
}
/**
* Checks if specified port is valid and enabled, then disables it
*
* @param port Pointer the port
*
*/
void check_disable_port(MMAL_PORT_T *port)
{
if (port && port->is_enabled)
mmal_port_disable(port);
}
/**
* Handler for sigint signals
*
* @param signal_number ID of incoming signal.
*
*/
void default_signal_handler(int signal_number)
{
if (signal_number == SIGUSR1)
{
// Handle but ignore - prevents us dropping out if started in none-signal mode
// and someone sends us the USR1 signal anyway
}
else
{
// Going to abort on all other signals
vcos_log_error("Aborting program\n");
exit(130);
}
}
/**
* Convert a MMAL status return value to a simple boolean of success
* ALso displays a fault if code is not success
*
* @param status The error code to convert
* @return 0 if status is success, 1 otherwise
*/
int mmal_status_to_int(MMAL_STATUS_T status)
{
if (status == MMAL_SUCCESS)
return 0;
else
{
switch (status)
{
case MMAL_ENOMEM :
vcos_log_error("Out of memory");
break;
case MMAL_ENOSPC :
vcos_log_error("Out of resources (other than memory)");
break;
case MMAL_EINVAL:
vcos_log_error("Argument is invalid");
break;
case MMAL_ENOSYS :
vcos_log_error("Function not implemented");
break;
case MMAL_ENOENT :
vcos_log_error("No such file or directory");
break;
case MMAL_ENXIO :
vcos_log_error("No such device or address");
break;
case MMAL_EIO :
vcos_log_error("I/O error");
break;
case MMAL_ESPIPE :
vcos_log_error("Illegal seek");
break;
case MMAL_ECORRUPT :
vcos_log_error("Data is corrupt \attention FIXME: not POSIX");
break;
case MMAL_ENOTREADY :
vcos_log_error("Component is not ready \attention FIXME: not POSIX");
break;
case MMAL_ECONFIG :
vcos_log_error("Component is not configured \attention FIXME: not POSIX");
break;
case MMAL_EISCONN :
vcos_log_error("Port is already connected ");
break;
case MMAL_ENOTCONN :
vcos_log_error("Port is disconnected");
break;
case MMAL_EAGAIN :
vcos_log_error("Resource temporarily unavailable. Try again later");
break;
case MMAL_EFAULT :
vcos_log_error("Bad address");
break;
default :
vcos_log_error("Unknown status error");
break;
}
return 1;
}
}
uint64_t get_microseconds64()
{
struct timespec spec;
uint64_t us;
clock_gettime(CLOCK_MONOTONIC_RAW, &spec);
us = spec.tv_sec * 1000000ULL;
us += spec.tv_nsec / 1000;
return us;
}