-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireless.c
executable file
·143 lines (121 loc) · 3.07 KB
/
wireless.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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");
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));
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 = 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(strcmp(buffer, "Reconnected") == 0){
printf("[TCP] Connection Restored\n");
tcp_isConnected = 1;
tcp_isReconnected = 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{
//strcpy(output,"SE");
//tcp_isWriting = 1;
//tcp_isConnected = 1;
//printf("\nEnter the string to be sent: ");
//scanf("%s",output);
if (tcp_isWriting && tcp_isConnected){
strcat(output,"\n");
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);
}