-
Notifications
You must be signed in to change notification settings - Fork 11
/
mjsxj02hl.c
executable file
·198 lines (170 loc) · 8.24 KB
/
mjsxj02hl.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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sysexits.h>
#include <string.h>
#include "./logger/logger.h"
#include "./localsdk/localsdk.h"
#include "./localsdk/init.h"
#include "./configs/configs.h"
#include "./mqtt/mqtt.h"
#include "./rtsp/rtsp.h"
// Signal callback
void signal_callback(int signal) {
LOGGER(LOGGER_LEVEL_DEBUG, "Function is called...");
// Enable orange LED
if(APP_CFG.general.led) {
if(local_sdk_indicator_led_option(true, false) == LOCALSDK_OK) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "local_sdk_indicator_led_option(true, false)");
else {
LOGGER(LOGGER_LEVEL_WARNING, "%s error!", "local_sdk_indicator_led_option(true, false)");
signal = EX_SOFTWARE;
}
}
// MQTT free
if(mqtt_free(true)) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "mqtt_free(true)");
else {
LOGGER(LOGGER_LEVEL_WARNING, "%s error!", "mqtt_free(true)");
signal = EX_SOFTWARE;
}
// RTSP free
if(rtsp_free()) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "rtsp_free()");
else {
LOGGER(LOGGER_LEVEL_WARNING, "%s error!", "rtsp_free()");
signal = EX_SOFTWARE;
}
// All free
if(all_free()) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "all_free()");
else {
LOGGER(LOGGER_LEVEL_WARNING, "%s error!", "all_free()");
signal = EX_SOFTWARE;
}
LOGGER(LOGGER_LEVEL_DEBUG, "Function completed (signal = %d).", signal);
exit(signal);
}
// Factory reset callback
int factory_reset_callback() {
LOGGER(LOGGER_LEVEL_DEBUG, "Function is called...");
int result = LOCALSDK_OK;
if(system("mjsxj02hl --factory-reset") == EX_OK) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "system(mjsxj02hl --factory-reset)");
else {
LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "system(mjsxj02hl --factory-reset)");
result = LOCALSDK_ERROR;
}
LOGGER(LOGGER_LEVEL_DEBUG, "Function completed (result = %s).", (result == LOCALSDK_OK ? "LOCALSDK_OK" : "LOCALSDK_ERROR"));
return result;
}
// Main function
int main(int argc, char **argv) {
LOGGER(LOGGER_LEVEL_DEBUG, "Function is called...");
// Initialize pseudo-random number generator
srand(time(NULL));
// Default path of config file
char *config_filename = "/usr/app/share/mjsxj02hl.conf";
// Running with arguments
if(argc > 1) {
if(strcmp(argv[1], "--config") == 0) { // Set config path
if(argc == 3) {
config_filename = argv[2];
} else {
printf("Error: missing filename! Use the --help option for more information.\n");
return EX_USAGE;
}
} else if(strcmp(argv[1], "--factory-reset") == 0) { // Factory reset
printf("Reset to factory settings...\n");
system("rm -rf /configs/*");
system("reboot");
return EX_OK;
} else if(strcmp(argv[1], "--get-image") == 0) { // Get image
if(argc == 3) {
if(system("pidof -o %PPID mjsxj02hl > /dev/null") == EX_OK) {
if(local_sdk_video_get_jpeg(LOCALSDK_VIDEO_SECONDARY_CHANNEL, argv[2]) == LOCALSDK_OK) {
return EX_OK;
} else {
printf("Error: local_sdk_video_get_jpeg() failed!\n");
return EX_SOFTWARE;
}
} else {
printf("Error: main thread of mjsxj02hl application is not running!\n");
return EX_UNAVAILABLE;
}
} else {
printf("Error: missing filename! Use the --help option for more information.\n");
return EX_USAGE;
}
} else if(strcmp(argv[1], "--help") == 0) { // Help
printf("Usage: mjsxj02hl [<action> [options...]]\n");
printf("\n");
printf("Running without arguments starts the main thread of the application.\n");
printf("\n");
printf(" --config <filename> Specify the location of the configuration file for the main thread of application.\n");
printf("\n");
printf(" --factory-reset Reset device settings to default values. Attention: this action cannot be undone!\n");
printf("\n");
printf(" --get-image <filename> Output the camera image to a file. Requires a running main thread of mjsxj02hl application.\n");
printf("\n");
printf(" --help Display this message.\n");
printf("\n");
printf("Report an error or help in the development of the project you can on the page %s\n", "https://github.com/kasitoru/mjsxj02hl_application");
return EX_OK;
} else {
printf("Error: unknown action! Use the --help option for more information.\n");
return EX_USAGE;
}
}
// Firmware version
char *fw_ver = firmware_version();
LOGGER(LOGGER_LEVEL_FORCED, "Firmware version: %s", fw_ver);
free(fw_ver);
// Device id
char *dev_id = device_id();
LOGGER(LOGGER_LEVEL_FORCED, "Device ID: %s", dev_id);
free(dev_id);
// Main thread
if(configs_init(config_filename)) { // Init configs
LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "configs_init()");
if(all_init()) { // Init all systems
LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "all_init()");
// Register signals
if(signal(SIGINT, signal_callback) != SIG_ERR) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "signal(SIGINT)"); // SIGINT
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "signal(SIGINT)");
if(signal(SIGABRT, signal_callback) != SIG_ERR) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "signal(SIGABRT)"); // SIGABRT
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "signal(SIGABRT)");
if(signal(SIGTERM, signal_callback) != SIG_ERR) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "signal(SIGTERM)"); // SIGTERM
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "signal(SIGTERM)");
// Onboard LED indicator
if(APP_CFG.general.led) { // Enable blue LED
if(local_sdk_indicator_led_option(false, true) == LOCALSDK_OK) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "local_sdk_indicator_led_option(false, true)");
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "local_sdk_indicator_led_option()");
} else { // Disable LEDs
if(local_sdk_indicator_led_option(false, false) == LOCALSDK_OK) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "local_sdk_indicator_led_option(false, false)");
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "local_sdk_indicator_led_option()");
}
// Factory reset callback
if(local_sdk_setup_keydown_set_callback(3000, factory_reset_callback) == LOCALSDK_OK) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "local_sdk_setup_keydown_set_callback()");
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "local_sdk_setup_keydown_set_callback()");
// RTSP server
if(rtsp_init()) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "rtsp_init()");
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "rtsp_init()");
// MQTT client
if(mqtt_init()) LOGGER(LOGGER_LEVEL_DEBUG, "%s success.", "mqtt_init()");
else LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "mqtt_init()");
// Endless waiting
while(true) {
sleep(1);
}
} else {
LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "all_init()");
return EX_SOFTWARE;
}
} else {
LOGGER(LOGGER_LEVEL_ERROR, "%s error!", "configs_init()");
return EX_CONFIG;
}
LOGGER(LOGGER_LEVEL_DEBUG, "Function completed (signal = %d).", EX__BASE);
return EX__BASE;
}