-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen-to-pebble.c
289 lines (250 loc) · 8.52 KB
/
listen-to-pebble.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <stdbool.h>
#include <time.h>
#define MSG_SIZE 300
extern pthread_mutex_t lock;
extern float max, min, average, latest;
extern int arduino;
extern int sock; // socket descriptor
extern int received;
extern int connection;
extern char alarm_status;
int TEMP_LENGTH = 80;
double WAIT_TIME = .1;
char reply[MSG_SIZE];
/*int message_received() {*/
/*time_t tick = time(NULL);*/
/*while (!received && difftime(time(NULL), tick) < WAIT_TIME);*/
/*printf("Received: %d\n", received);*/
/*int ret_val = received;*/
/*pthread_mutex_lock(&lock);*/
/*received = 0;*/
/*pthread_mutex_unlock(&lock);*/
/*return ret_val;*/
/*}*/
void start_server(void *argv_void)
{
char** argv = (char**)argv_void;
// structs to represent the server and client
struct sockaddr_in server_addr,client_addr;
// 1. socket: creates a socket descriptor that you later use to make other system calls
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
int reuse_addr = 1;
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&reuse_addr,sizeof(int)) == -1) {
perror("Setsockopt");
exit(1);
}
// configure the server
int PORT_NUMBER = atoi(argv[1]);
server_addr.sin_port = htons(PORT_NUMBER); // specify port number
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
// 2. bind: use the socket and associate it with the port number
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
// 3. listen: indicates that we want to listen to the port to which we bound;
// second arg is number of allowed connections
if (listen(sock, 5) == -1) { // 2nd arg is 5 in original
perror("Listen");
exit(1);
}
// once you get here, the server is set up and about to start listening
printf("\nServer configured to listen on port %d\n", PORT_NUMBER);
fflush(stdout);
}
/*
Thread that starts a server and continously listens to Pebble Watch.
Able to handle Pebble-Arduino temperature commands.
argv[1] should contain port to listen to.
*/
void* listen_to_pebble(void* argv) {
bool isCelsius = true; //handles which mode the temperature is in. Default is Celsius
struct sockaddr_in server_addr,client_addr;
int fd = -1;
fd_set set;
/*
Initializes the server.
Returns the location of the sock.
*/
start_server(argv);
/*
Continously waits for Pebble message using select.
Also listens to STDIN for "q" command to quit.
*/
while(1) {
FD_ZERO(&set);
FD_SET(0, &set);
FD_SET(sock, &set);
// 4. accept: wait here until we get a connection on that port
if(select(sock+1, &set, NULL, NULL, NULL) == -1)
{
perror("select");
exit(1);
}
if (FD_ISSET(0, &set)) {
char buff [MSG_SIZE];
fgets(buff, sizeof(buff), stdin);
if (strcmp(buff, "q\n") == 0) {
break;
}
// FOR DEBUGGING ONLY
int send_message = 0;
switch (buff[0]) {
case 'c':
puts("Send signal to change temperature units.");
send_message = 1;
break;
case 'a':
puts("Send signal to arm alarm.");
send_message = 1;
break;
case 'd':
puts("Send signal to disarm alarm.");
send_message = 1;
break;
}
if (send_message) {
char msg [2] = {buff[0], '\0'};
write(arduino, msg, 1);
if (connection) {
puts("Arduino received message.");
} else {
puts("No response from Arduino.");
}
}
} else {
int sin_size = sizeof(struct sockaddr_in);
fd = accept(sock, (struct sockaddr *)&client_addr,(socklen_t *)&sin_size);
if (fd != -1) {
printf("\nServer got a connection from (%s, %d)\n",
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
// buffer to read data into
char request[MSG_SIZE];
// 5. recv: read incoming message into buffer
int bytes_received = recv(fd,request,1024,0);
// null-terminate the string
request[bytes_received] = '\0';
printf("Here comes the message:\n");
printf("%s\n", request);
pthread_mutex_lock(&lock);
float temp_max = max;
float temp_avg = average;
float temp_min = min;
float temp_latest = latest;
char *metric = "c ";
pthread_mutex_unlock(&lock);
//Handle conversions if mode is in Farenheit
if(!isCelsius) {
temp_max = temp_max * 1.8 + 32;
temp_min = temp_min * 1.8 + 32;
temp_avg = temp_avg * 1.8 + 32;
temp_latest = temp_latest * 1.8 + 32;
metric = "f ";
}
/*
Creates a JSON reply for pebble.
There must be a default reply, otherwise Pebble will keep pinging.
*/
char* begin_reply = "{\n\"name\": \"";
char* end_reply = "\"\n}\n";
bzero(reply, MSG_SIZE);
strcat(reply, begin_reply);
char *command = strdup(request);
char *token = strsep(&command, " ");
token = strsep(&command, " ");
int print_communication = 1;
if(strlen(request) != 0) {
if (!connection) {
strcat(reply, "Lost connection with Arduino");
} else {
if(strncmp(request, "GET", 3) == 0) {
char* main_reply = reply + strlen(reply);
if(strcmp(token, "/high") == 0) {
snprintf(main_reply, TEMP_LENGTH, "%5.2f", temp_max);
strcat(reply, metric);
} else if (strcmp(token, "/average") == 0) {
snprintf(main_reply, TEMP_LENGTH, "%5.2f", temp_avg);
strcat(reply, metric);
} else if (strcmp(token, "/low") == 0) {
snprintf(main_reply, TEMP_LENGTH, "%5.2f", temp_min);
strcat(reply, metric);
} else if (strcmp(token, "/latest") == 0) {
snprintf(main_reply, TEMP_LENGTH, "%5.2f", temp_latest);
strcat(reply, metric);
} else if (strcmp(token, "/alarm") == 0) {
snprintf(main_reply, 2, "%c", alarm_status);
} else if (strcmp(token, "/ping") == 0) {
strcat(reply, token);
print_communication = 0;
} else {
strcat(reply, "Invalid GET request");
}
} else if(strncmp(request, "POST", 3) == 0) {
char main_reply [MSG_SIZE];
char signal [2];
signal[1] = '\0';
if(strcmp(token, "/change") == 0) {
isCelsius = !isCelsius;
signal[0] = 'c';
strcpy(main_reply, "Temp metric changed");
} else if (strcmp(token, "/disarm") == 0) {
signal[0] = 'd';
strcpy(main_reply, "Alarm disarmed");
} else if (strcmp(token, "/arm") == 0) {
signal[0] = 'a';
strcpy(main_reply, "Arming alarm");
} else if (strcmp(token, "/on") == 0) {
signal[0] = 'n';
strcpy(main_reply, "Turn on display");
} else if (strcmp(token, "/off") == 0) {
signal[0] = 'f';
strcpy(main_reply, "Turn off display");
} else {
strcpy(main_reply, "Invalid POST request");
}
write(arduino, signal, 1);
if (connection) {
strcat(reply, main_reply);
} else {
}
} else {
strcat(reply,"Only able to handle GET and POST requests");
}
}
} else {
strcat(reply, "no message received from pebble");
}
strcat(reply, end_reply);
printf("\nConnection: %d\n", connection);
puts("\nREPLY:");
puts(reply);
// 6. send: send the message over the socket
// note that the second argument is a char*, and the third is the number of chars
send(fd, reply, strlen(reply), 0);
// 7. close: close the connection
close(fd);
}
}
} // end while
// 8. close: close the socket
close(sock);
printf("Server shutting down\n");
return 0;
}