forked from HowieXue/Linux_Gateway-ioT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_client_receive.c~
128 lines (107 loc) · 2.8 KB
/
pthread_client_receive.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
#include "link_list.h"
#include "data_global.h"
#define LEN_ENV 12
#define LEN_RFID 4
extern int fd_socket;
extern linklist linkHead;
extern linklist slinkHead;
extern pthread_mutex_t mutex_linklist;
extern pthread_mutex_t mutex_slinklist;
extern pthread_mutex_t mutex_analysis;
extern pthread_mutex_t mutex_global;
extern pthread_mutex_t mutex_sms;
extern pthread_mutex_t mutex_buzzer;
extern pthread_mutex_t mutex_client_send;
extern pthread_cond_t cond_analysis;
extern pthread_cond_t cond_client_send;
extern pthread_cond_t cond_sqlite;
extern pthread_cond_t cond_refresh;
extern pthread_cond_t cond_buzzer;
extern pthread_cond_t cond_sms;
void serial_init(int fd)
{
struct termios options;
tcgetattr(fd, &options);
options.c_cflag |= ( CLOCAL | CREAD );
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= CS8;
options.c_cflag &= ~CSTOPB;
options.c_iflag |= IGNPAR;
options.c_iflag &= ~(ICRNL | IXON);
options.c_oflag = 0;
options.c_lflag = 0;
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(fd,TCSANOW,&options);
}
int socket_connect() //socket创建并连接远程终端函数
{
#if 1
if ((dev_uart_fd = open (DEV_ZIGBEE, O_RDWR)) < 0)
{
perror ("open arm-gate_com3 is error");
exit (-1);
}
serial_init (dev_uart_fd); //init com3
printf ("pthread_transfer is ok\n");
#endif
struct sockaddr_in address;
int len, result;
fd_socket = socket(AF_INET, SOCK_STREAM,0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(REMOTE_IP);
address.sin_port = htons(PORT);
len = sizeof(address);
do{
result = connect(fd_socket, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("oops: socket connect failed");
//exit(1);
}
printf("Socket connect successed !!!\n\n");
}
while(result==-1);
return 0;
}
void *pthread_client_receive (void *arg)
{
signal(SIGPIPE, SIG_IGN);
linklist node;
link_datatype buf;
int len;
printf ("pthread_client_send is ok\n");
socket_connect();
int i;
char socket_buf[100];
while(1)
{
len = read( fd_socket, socket_buf, 100);
if(len <= 0)
{
printf("recive data from socket fail !\n");
perror("recvfrom:");
exit(EXIT_FAILURE);
}
else
{
printf("\nrecieve data from socket sueccess, data_len= %d\n",len);
for(i=0;i<len;i++)
{
printf("%x ",socket_buf[i]);
}
len = write(fd_uart, socket_buf, len); //send the data recived from socket to uart1
if(len < 0)
{
printf("\nwrite data to uart fail\n");
}
else
{
printf("\nwrite data to uart success,data_len=%d\n",len);
}
}
}
}
return 0;
}