-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireless.c.save
executable file
·138 lines (117 loc) · 2.96 KB
/
wireless.c.save
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
#include "wireless.h"
// Wireless Variables
int newsockfd, n, tcp_isReconnecting = 0, tcp_hasReconnected = 1;
char buffer[BUFFER_SIZE];
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
void setupWireless(){
printf("[TCP] Setting Up\n");
printf("TCP_isreconnecting value: %d", tcp_isReconnecting);
fflush(stdout);
if(!tcp_isReconnecting){
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
printf("[TCP] ERROR opening socket\n");
fflush(stdout);
return;
}
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(WIRELESS_PORT);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
tcp_isReconnecting = 0;
printf("[TCP] ERROR on binding - %s\n", strerror(errno));
fflush(stdout);
return;
}
listen(sockfd, 3);
printf("[TCP] Waiting for Client\n");
}
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0){
tcp_isReconnecting = 0;
printf("[TCP] ERROR on accept - %s\n", strerror(errno));
return;
}
bzero(buffer, BUFFER_SIZE);
if(!tcp_isReconnecting){
printf("[TCP] Connected\n");
tcp_isConnected = 1;
}
}
//Re-Connection After 6 seconds
void *wireless_reconnect(){
while (1)
{
if(!tcp_isConnected && !tcp_isReconnecting){
tcp_isReconnecting = 1;
tcp_hasReconnected = 0;
printf("[TCP] Connection Lost\n");
closeWireless();
usleep(6000000);
setupWireless();
printf("[TCP] Attempting to Reconnect...\n");
// Send Reconnection Message
write(newsockfd, "Reconnecting", 12);
write(newsockfd, "\n", 1);
usleep(500000);
// Listen for Reconnection Message
n = read(newsockfd, buffer, BUFFER_SIZE);
strtok(buffer, "\n");
//strtok(buffer, "\r");
if(n > 0 && strcmp(buffer, "Reconnected") == 0){
printf("[TCP] Connection Restored\n");
tcp_isConnected = 1;
tcp_isReconnecting = 1;
}
}
}
}
void *wireless_read(){
do{
if (tcp_isConnected){
n = read(newsockfd, buffer, BUFFER_SIZE);
if (n > 0){
strtok(buffer, "\n");
//strtok(buffer, "\r");
strncpy(input, buffer, sizeof(buffer));
fprintf(stdout, "[TCP] Read: %s (%d Bytes)\n", buffer, n);
bzero(buffer, BUFFER_SIZE);
sender = 'P';
rpi_hasReceived = 1;
}else{
tcp_isConnected = 0;
usleep(10000000);
}
}
} while (1);
}
void *wireless_write(){
int status;
do{
if (tcp_isWriting && tcp_isConnected){
status = write(newsockfd, output, strlen(output));
//status += write(newsockfd, "\n", 1);
if (status > 0) {
tcp_isWriting = 0;
//fprintf(stdout, "[TCP] Sent: %s (%d Bytes)\n", output, status);
}else{
tcp_isConnected = 0;
usleep(10000000);
}
}
} while (1);
}
void closeWireless(){
if(tcp_isReconnecting){
shutdown(newsockfd, SHUT_RDWR);
close(newsockfd);
return;
}
shutdown(newsockfd, SHUT_RDWR);
close(newsockfd);
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
}