Files
hisi_SS524V100_SDK/mpp/sample/uart_fanet/ctrl_pnl.c
T

178 lines
4.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h> // gettimeofday
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <pthread.h>
#include "serial.h"
#include "uartinf.h"
#include <stdbool.h>
#include "parase.h"
char _rx_data[200];
static int socket_descriptor;
static struct sockaddr_in address;
bool first = false;
int socket_descriptor_recv;
struct sockaddr_in address_recv;
pthread_attr_t attr;
pthread_t pkts_thread;
int *switch_mode;
int *switch_memory_open(void) {
char buf[128];
int fd;
sprintf(buf, "/video_mode");
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 udp_init(char *ip, int port){
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr(ip);
address.sin_port=htons(port);
socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);//IPV4
return 0;
}
#if 0
void *net_recv(void *arg)
{
int i;
struct sockaddr_in clientAddr;
int len = sizeof(clientAddr);
int ret;
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
if (setsockopt(socket_descriptor, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
printf("socket option SO_RCVTIMEO not support\n");
//return -1;
}
char buffer_cmd[200];
while(1){
i = recvfrom(socket_descriptor, buffer_cmd, 200, 0, (struct sockaddr*)&clientAddr, &len);
if(i>0){
#if 0
printf("recvfrom len %d \n",i);
for(int j=0;j<=i;j++){
printf("%x ",buffer_cmd[j]);
}
printf("\n");
#endif
printf("recvfrom buffer_cmd[1]%x switch_mode[1] %x\n",buffer_cmd[1],switch_mode[1]);
if((buffer_cmd[0]==0xaa)&&(buffer_cmd[1]==switch_mode[1])&&(buffer_cmd[43]==0xff))
{
uart_write_len(ROUTE_UART,buffer_cmd,i);
}
}
else{
printf(" time_out to receive data\n");
}
}
}
int pthread_init()
{
int ret;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
ret = pthread_create(&pkts_thread, &attr, net_recv, NULL);
pthread_attr_destroy(&attr);
if(ret){
printf("create pthread1 error\n");
return 1;
}
}
#endif
int main (int argc, char *argv[])
{
int n_read;
int i;
int count=0;
char buffer_send[100];
int buffer_len;
//switch_mode= switch_memory_open();
n_read = uart_init(ROUTE_UART, "/dev/ttyAMA4", 115200, 'N');
if(n_read <=0 ) {
printf("open /dev/ttyAMA4 failed\n");
return -1;
}
printf("uart4\n");
udp_init(argv[1], atoi(argv[2]));
//pthread_init();
// sendto(socket_descriptor, buffer_send, buffer_len, 0, (struct sockaddr *)&address, sizeof(address));
// usleep(100*1000);
// }
#if 1
while(1){
n_read = uart_read(ROUTE_UART, _rx_data);
// count++;
if(n_read > 0 ){
printf("rx: %x %x %x len %d \n",_rx_data[0],_rx_data[1],_rx_data[10],n_read);
if((_rx_data[0]==0xBB)&&(_rx_data[24]==0xAC)){
printf("len %d rx: %x %x \n",n_read,_rx_data[0],_rx_data[24]);
sendto(socket_descriptor, _rx_data, n_read, 0, (struct sockaddr *)&address, sizeof(address));
}
else if((_rx_data[0]==0xCC)&&(_rx_data[42]==0xAD)){
printf("len %d rx: %x %x \n",n_read,_rx_data[0],_rx_data[32]);
sendto(socket_descriptor, _rx_data, n_read, 0, (struct sockaddr *)&address, sizeof(address));
}
else if((_rx_data[0]==0xDD)&&(_rx_data[30]==0xAE)){
printf("len %d rx: %x %x \n",n_read,_rx_data[0],_rx_data[30]);
sendto(socket_descriptor, _rx_data, n_read, 0, (struct sockaddr *)&address, sizeof(address));
}
}
usleep(10000);
//if(count >= 100){
// count=0;
// sendto(socket_descriptor, buffer_send, buffer_len, 0, (struct sockaddr *)&address, sizeof(address));
// }
}
#endif
}