diff --git a/mpp/sample/lc6600_uart/ctrl_pnl.c b/mpp/sample/lc6600_uart/ctrl_pnl.c index b8acaf9..2351e8e 100644 --- a/mpp/sample/lc6600_uart/ctrl_pnl.c +++ b/mpp/sample/lc6600_uart/ctrl_pnl.c @@ -36,7 +36,7 @@ char freq2[10]; //char dbm1[5]; //char dbm2[5]; -#define RSSI_TIMEOUT_MS 4000 //超时时间 +#define RSSI_TIMEOUT_MS 3000 //超时时间 #define RSSI_NO_SIGNAL 0x55 //无信号值 @@ -309,9 +309,9 @@ int main (int argc, char *argv[]) int ip_len; int len; - n_read = uart_init(ROUTE_UART, "/dev/ttyAMA4", 115200, 'N'); + n_read = uart_init(ROUTE_UART, "/dev/ttyAMA2", 115200, 'N'); if(n_read <=0 ) { - printf("open /dev/ttyAMA4 failed\n"); + printf("open /dev/ttyAMA2 failed\n"); return -1; } diff --git a/mpp/sample/rtsp_switch/rtsp_switch_2.c b/mpp/sample/rtsp_switch/rtsp_switch_2.c new file mode 100644 index 0000000..ff35f98 --- /dev/null +++ b/mpp/sample/rtsp_switch/rtsp_switch_2.c @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +int switch_video; +unsigned char key_value; + + + + +int *switch_memory_open(void) { + char buf[128]; + int fd; + sprintf(buf, "/video_switch"); + fd = shm_open(buf, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); + if(fd < 0) { perror("shm_open"); exit(1); } + if (ftruncate(fd, 4096) == -1) { perror("ftruncate"); exit(1); } + int *retval = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + return retval; +} + +int main(int argc, char **argv) +{ + + + int ret; + int *switch_mode; + + int switch_video=0; + int switch_video_pre=0; + unsigned char buf[1024]; + unsigned char buf_kill_rtsp[1024]; + + switch_mode = switch_memory_open(); + + switch_video = switch_mode[0]; + switch_video_pre = switch_video; + + + /* 0->1 双全屏, 1->2 左全屏, 2->3 右全屏, 3->4 左放大, 4->5 右放大 */ + sprintf(buf,"./start_rtsp.sh %d",switch_video+1); + system(buf); + + + while(1) + { + switch_video = switch_mode[0]; + + /* 双全屏:任一路无信号切该路备路(6=通道0备路, 7=通道1备路) */ + if(switch_video==0 && switch_video_pre==0){ + if(switch_mode[2] == 0x55){ + printf("+++++++++++++++++++++++++1 time out\n"); + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",6); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",6); + system(buf); + } + + if(switch_mode[3] == 0x55){ + printf("+++++++++++++++++++++++++2 time out\n"); + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",7); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",7); + system(buf); + } + + if((switch_mode[2] == 0x55) || (switch_mode[3] == 0x55)){ + printf("+++++++++++++++++++++++++time out\n"); + usleep(6000000); + } + }else if(switch_video == 1 && switch_video_pre==1){ /* 左屏通道0全屏 */ + if(switch_mode[2] == 0x55){ + printf("+++++++++++++++++++++++++11 time out\n"); + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",2); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",2); + system(buf); + } + } + else if(switch_video == 2 && switch_video_pre==2) /* 右屏通道1全屏 */ + { if(switch_mode[3] == 0x55){ + printf("+++++++++++++++++++++++++22 time out\n"); + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",2); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",3); + system(buf); + } + + } + else if(switch_video == 3 && switch_video_pre==3) /* 左放大右隐藏 */ + { if(switch_mode[2] == 0x55){ + printf("+++++++++++++++++++++++++33 time out\n"); + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",2); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",4); + system(buf); + } + } + else if(switch_video == 4 && switch_video_pre==4) /* 右放大左隐藏 */ + { if(switch_mode[3] == 0x55){ + printf("+++++++++++++++++++++++++44 time out\n"); + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",2); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",5); + system(buf); + } + } + + + + if((switch_video != switch_video_pre )){ + + switch_video_pre = switch_video; + sprintf(buf_kill_rtsp,"./kill_rtsp.sh %d",1); + system(buf_kill_rtsp); + sprintf(buf,"./start_rtsp.sh %d",switch_video+1); + printf("+++++++++++++++video %s\n",buf); + + + system(buf); + + } + + usleep(3000000); + + } + +} diff --git a/mpp/sample/uart2net_mesh/ctrl_pnl.c b/mpp/sample/uart2net_mesh/ctrl_pnl.c index 4dc0764..57cfad7 100644 --- a/mpp/sample/uart2net_mesh/ctrl_pnl.c +++ b/mpp/sample/uart2net_mesh/ctrl_pnl.c @@ -32,6 +32,17 @@ #define CHECKSUM_END 30 /* 校验和覆盖 byte0..byte30(含) */ #define NET_FRAME_MAX_LEN 256 /* recvfrom 缓冲大小 */ +#define BYTE_NOSIG0 25 /* byte25 = 视频流0无信号标志(1=无信号,0=有信号) */ +#define BYTE_NOSIG1 26 /* byte26 = 视频流1无信号标志(1=无信号,0=有信号) */ +#define BYTE_RSSI1 28 /* 信号包=RSSI1;设备包=哨兵 */ +#define BYTE_RSSI2 29 /* 信号包=RSSI2;设备包=哨兵 */ +#define SIG_MARK 0x5A /* 设备包 byte28/29 哨兵值 */ +#define SIGNAL_INTERVAL_MS 100 /* 独立信号包发送间隔(ms) */ +#define FRAME_HEAD 0x11 /* 上行帧帧头(byte0),面板校验要求 */ +#define FRAME_TAIL 0x22 /* 上行帧帧尾(byte30),面板校验要求 */ +#define FRAME_HEAD_TX 0xAA /* 下行帧帧头(byte0),设备→面板 */ + + /* ===================== 共享内存 ===================== */ #define SHM_MSG_NAME "/video_switch" #define SHM_MODE_IDX 0 /* switch_mode -> shm[0](即 switch_video[0]) */ @@ -94,6 +105,11 @@ static int read_gpio72(void) return (val == '0') ? 1 : 0; /* 反相:GPIO=1 → 0,GPIO=0 → 1 */ } +static long elapsed_ms(struct timeval *start, struct timeval *end) +{ + return (end->tv_sec - start->tv_sec) * 1000L + + (end->tv_usec - start->tv_usec) / 1000L; +} int init_net_recv(int port){ @@ -229,16 +245,18 @@ void *net_recv(void *arg) } - /* 2) 校验和 = byte0..byte30 求和取低8位 */ + /* 构造下行帧:byte0=0xAA 帧头,byte1..29 原样,byte30=0xBB 帧尾 */ + frame_out[0] = FRAME_HEAD_TX; /* [0] 帧头 0xAA */ + memcpy(frame_out + 1, buffer_cmd + 1, PAYLOAD_LEN - 1); /* [1..29] 原样 */ + frame_out[PAYLOAD_LEN] = NET_TAIL; /* [30] 帧尾 0xBB */ + + /* 校验和 = byte0..byte30 求和取低8位,写入 byte31 */ sum = 0; for (k = 0; k <= CHECKSUM_END; k++) - sum += (unsigned char)buffer_cmd[k]; + sum += (unsigned char)frame_out[k]; checksum = sum & 0xFF; + frame_out[PAYLOAD_LEN + 1] = checksum; /* [31] 校验和 */ - /* 3) 转发:去掉 byte30/31,byte0..29 原样 + 帧尾 + 校验和(仍 32 字节) */ - memcpy(frame_out, buffer_cmd, PAYLOAD_LEN); /* 0..29,共 30 字节 */ - frame_out[PAYLOAD_LEN] = NET_TAIL; /* [30] 帧尾 */ - frame_out[PAYLOAD_LEN + 1] = checksum; /* [31] 校验和 */ #if 0 /* 打印发出的包 */ @@ -287,9 +305,12 @@ int main (int argc, char *argv[]) char buffer_send[100]; int buffer_len; - n_read = uart_init(ROUTE_UART, "/dev/ttyAMA2", 115200, 'N'); + struct timeval tv_sig = {0, 0}, tv_now; + char sig_frame[NET_FRAME_LEN]; + + n_read = uart_init(ROUTE_UART, "/dev/ttyAMA4", 115200, 'N'); if(n_read <=0 ) { - printf("open /dev/ttyAMA2 failed\n"); + printf("open /dev/ttyAMA4 failed\n"); return -1; } @@ -307,31 +328,74 @@ int main (int argc, char *argv[]) pthread_init(); - while(1){ - n_read = uart_read(ROUTE_UART, _rx_data); - if(n_read > 0 ){ - if (n_read >= 32) { /* 完整 32 字节帧才处理 */ - int k, sum = 0; + while(1){ + n_read = uart_read(ROUTE_UART, _rx_data); + if(n_read > 0 ){ + int k, sum = 0; - _rx_data[27] = (char)read_gpio72(); /* byte27 = GPIO9_0 反相值 */ - - if (buffer_param) { /* 写入:byte28/29 = RSSI 信号强度 */ - _rx_data[28] = (char)buffer_param[1]; - _rx_data[29] = (char)buffer_param[2]; - printf("RSSI1 =%d RSSI2 =%d",buffer_param[1],buffer_param[2]); + /* 收包校验:完整 32 字节帧,帧头 0x11、帧尾 0x22、byte31 校验和正确,否则丢弃 */ + if (n_read < NET_FRAME_LEN) { + printf("drop bad frame: len=%d\n", n_read); + continue; + } + if ((unsigned char)_rx_data[0] != FRAME_HEAD || + (unsigned char)_rx_data[CHECKSUM_END] != FRAME_TAIL) { + printf("drop bad frame: head=%02x tail=%02x\n", + (unsigned char)_rx_data[0], (unsigned char)_rx_data[CHECKSUM_END]); + continue; + } + for (k = 0; k <= CHECKSUM_END; k++) + sum += (unsigned char)_rx_data[k]; + if ((unsigned char)_rx_data[31] != (sum & 0xFF)) { + printf("drop bad frame: checksum=%02x expect=%02x\n", + (unsigned char)_rx_data[31], (unsigned char)(sum & 0xFF)); + continue; + } + + /* byte28/29 固定哨兵值,RSSI 改由独立信号包承载 */ + _rx_data[BYTE_RSSI1] = (char)SIG_MARK; + _rx_data[BYTE_RSSI2] = (char)SIG_MARK; + + + /* 和校验:byte0..byte30 求和取低 8 位,写入 byte31 */ + sum = 0; + for (k = 0; k <= CHECKSUM_END; k++) + sum += (unsigned char)_rx_data[k]; + _rx_data[31] = (char)(sum & 0xFF); + + sendto(socket_descriptor, _rx_data, NET_FRAME_LEN, 0, (struct sockaddr *)&address, sizeof(address)); + } + + + /* 独立信号包:与串口数据无关,每 SIGNAL_INTERVAL_MS 发一次;其余字节填 0 */ + gettimeofday(&tv_now, NULL); + if (tv_sig.tv_sec == 0 || elapsed_ms(&tv_sig, &tv_now) >= SIGNAL_INTERVAL_MS) { + int k, sum = 0; + + memset(sig_frame, 0, NET_FRAME_LEN); + sig_frame[0] = (char)FRAME_HEAD; /* byte0 = 0x11 */ + + /* byte25/26 = 视频流0/1无信号,byte27 = GPIO */ + sig_frame[BYTE_NOSIG0] = (shm_msg && shm_msg[2] == 0x55) ? 1 : 0; + sig_frame[BYTE_NOSIG1] = (shm_msg && shm_msg[3] == 0x55) ? 1 : 0; + sig_frame[27] = (char)read_gpio72(); + + if (buffer_param) { + sig_frame[BYTE_RSSI1] = (char)buffer_param[1]; + sig_frame[BYTE_RSSI2] = (char)buffer_param[2]; + //打印发出的信号值 + printf("signal: RSSI1=%d RSSI2=%d\n", buffer_param[1], buffer_param[2]); + } + sig_frame[PAYLOAD_LEN] = (char)FRAME_TAIL; /* byte30 = 0x22 */ + + for (k = 0; k <= CHECKSUM_END; k++) + sum += (unsigned char)sig_frame[k]; + sig_frame[CHECKSUM_END + 1] = (char)(sum & 0xFF); + + sendto(socket_descriptor, sig_frame, NET_FRAME_LEN, 0, (struct sockaddr *)&address, sizeof(address)); + tv_sig = tv_now; } - /* 和校验:byte0..byte30 求和取低 8 位,写入 byte31 */ - for (k = 0; k <= 30; k++) - sum += (unsigned char)_rx_data[k]; - _rx_data[31] = (char)(sum & 0xFF); } - sendto(socket_descriptor, _rx_data, n_read, 0, (struct sockaddr *)&address, sizeof(address)); - } -} - - - - } diff --git a/mpp/sample/uart2net_mesh/uart2net b/mpp/sample/uart2net_mesh/uart2net new file mode 100644 index 0000000..00c3f04 Binary files /dev/null and b/mpp/sample/uart2net_mesh/uart2net differ