-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.c
574 lines (477 loc) · 13.3 KB
/
webserver.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <libgen.h>
#include <pthread.h>
#include <semaphore.h>
#include <dirent.h>
#include <signal.h>
#include <wiringPi.h>
#include <math.h>
#include <stdbool.h>
#define BUFFERSIZE 1024
#define LINESIZE 80
#define PATHSIZE 80
#define FILESIZE 160
#define EXTSIZE 10
#define QLEN 10
#define NUMBER_OF_THREADS 100
#define min(x,y) (int)((((int)x)<((int)y))?((int)x):((int)y))
#define SENSOR 0 // SENSOR PIN
#define NB_SAMPLES_SENSOR 6
void gen_dirlist(char * html_response, char * path);
void * worker(void * arg);
void append(char *dest,int buffersize, char *src);
void lista_diretorio(char *path,char *buffer,int buffersize);
char * get_filename_ext(const char *filename);
int transferfile(char * path, int output_fd);
void gen_dirlist(char * html_response, char * path);
void * worker(void * arg);
void readSensor(int arg);
int myport;
char BASE[PATHSIZE];
int sd; // Socket descriptor
int status; // Estado da chamada
struct sockaddr_in mylocal_addr; // Meu endereço
char rxbuffer[1024];
struct sockaddr_in fromaddr;
int size;
int newsd;
struct sockaddr_in clientaddr;
char stringIP[20];
char txbuffer[1024];
char req[20];
char ver[10];
char str[50];
char path[PATHSIZE];
char file[FILESIZE];
sem_t mutex;
sem_t file_write;
sem_t sensor;
sem_t persistentFiles;
pthread_t threads[100];
int thread_count = 0;
int LUMINOSIDADE = 0; // Variavel de luminosidade (sensor)
void append(char *dest,int buffersize, char *src) {
int d;
int i;
d = strlen(dest);
for (i=0; i<min(strlen(src),buffersize-1-d); i++) dest[d+i] = src[i];
dest[d+i] = '\0';
}
// Lista diretorio
// path: diretorio a ser listado
// buffer: buffer que contera' a string com a sequencia ASCII
// resultado da listagem do diretorio (finalizada por '\0'
// bufffersize: tamanho do buffer
void lista_diretorio(char *path,char *buffer,int buffersize) {
DIR * dirp;
struct dirent * direntry;
dirp = opendir(path);
if (dirp ==NULL) {
perror("ERRO: chamada opendir(): Erro na abertura do diretorio: ");
snprintf(buffer,buffersize,"Erro na listagem diretorio!\n");
return;
}
buffer[0]='\0';
while (dirp) {
// Enquanto nao chegar ao fim do diretorio, leia cada entrada
direntry = readdir(dirp);
if (direntry == NULL) break; // chegou ao fim
else {
// ler entrada
append(buffer,buffersize,direntry->d_name);
append(buffer,buffersize,"\n");
}
}
closedir(dirp);
}
char * get_filename_ext(const char *filename) {
char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
int transferfile(char * path, int output_fd) {
int input_fd; // input file descriptor
int status;
int n;
char ext[EXTSIZE];
char buffer[BUFFERSIZE];
char str[50];
struct stat statp;
time_t rawtime;
struct tm * timeinfo;
input_fd = open(path,O_RDONLY);
if (input_fd < 0) {
strcpy(str, "HTTP/1.0 404 Not Found\n");
write(output_fd, str, strlen(str));
perror("ERRO chamada open(): Erro na abertura do arquivo: ");
return(-1);
}
strcpy(ext, get_filename_ext(basename(path)));
// Linha de status
if ((!strcmp(ext,"html") || !strcmp(ext,"jpg") || !strcmp(ext,"png") || !strcmp(ext,"css") || !strcmp(ext,"js"))==0) {
strcpy(str, "HTTP/1.0 400 Bad Request\n");
write(output_fd, str, strlen(str));
return(-1);
}
strcpy(str, "HTTP/1.0 200 OK\n");
printf("%s",str);
write (output_fd, str, strlen(str));
// Data
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (str,40,"Date: %a, %d %b %Y %T %Z\n",timeinfo);
printf("%s",str);
write (output_fd, str, strlen(str));
// Server
strcpy(str, "Server: Apache/1.3.0 (Unix)\n");
printf("%s",str);
write (output_fd, str, strlen(str));
// Obtem stats do arquivo
status = fstat(input_fd,&statp);
if (status != 0) {
perror("ERRO chamada stat(): Erro no acesso ao arquivo: ");
status = close(input_fd);
return(-1);
}
//Ultima modificaçao do arquivo
timeinfo = localtime (&statp.st_mtim.tv_sec);
strftime (str,40,"Last-Modified: %a, %d %b %Y %T %Z\n",timeinfo);
printf("%s",str);
write (output_fd, str, strlen(str));
// obtem tamanho do arquivo
sprintf(str,"Content-Length: %ld\n", statp.st_size);
printf("%s",str);
write(output_fd,str,strlen(str));
// Tipo de conteudo
if (!strcmp(ext, "html")) {
strcpy(str, "Content-Type: text/html; charset=UTF-8\n\n");
printf("%s",str);
}
else if (!strcmp(ext, "jpg")) {
strcpy(str, "Content-Type: image/jpeg\n\n");
printf("%s",str);
}
else if (!strcmp(ext, "png")) {
strcpy(str, "Content-Type: image/png\n\n");
printf("%s",str);
}
else if (!strcmp(ext, "css")) {
strcpy(str, "Content-Type: text/css\n\n");
printf("%s",str);
}
else if (!strcmp(ext, "js")) {
strcpy(str, "Content-Type: text/javascript\n\n");
printf("%s",str);
}
write(output_fd,str,strlen(str));
// le arquivo , por partes
do {
n = read(input_fd,buffer,BUFFERSIZE);
if (n<0) {
perror("ERRO: chamada read(): Erro na leitura do arquivo: ");
status = close(input_fd);
return(-1);
}
write(output_fd,buffer,n);
} while(n>0);
status = close(input_fd);
if (status == -1) {
perror("ERRO: chamada close(): Erro no fechamento do arquivo: " );
return(-1);
}
return(0);
}
int main() {
FILE * config;
int status;
char * nBASE;
pthread_t sensor1;
sem_init(&mutex, 0, 1);
sem_init(&file_write, 0, 1);
sem_init(&sensor, 0, 1);
sem_init(&persistentFiles, 0, 1);
signal(SIGPIPE,SIG_IGN);
//le arquivo de config.
config = fopen("config.txt", "r");
fgets(BASE, PATHSIZE, config);
myport = atoi(BASE);
printf("port = %d\r\n", myport);
fgets(BASE, PATHSIZE, config);
nBASE = strtok(BASE, "\r");
strcpy(BASE, nBASE);
printf("PATH=%s\n", BASE);
// Disparar threads de sensor e LED
pthread_create(&sensor1, NULL, (void*) readSensor,1);
// Conexão
sd = socket(PF_INET, SOCK_STREAM, 0);
if (sd == -1) {
perror ("Erro na chamada socket");
exit(1);
}
// Definição da porta onde o servidor vai aguardar
mylocal_addr.sin_family = AF_INET;
mylocal_addr.sin_addr.s_addr = INADDR_ANY;
mylocal_addr.sin_port = htons(myport);
status = bind(sd,
(struct sockaddr *) &mylocal_addr,
sizeof(struct sockaddr_in));
if(status == -1) perror("Erro na chamada bind");
// Abrir a porta onde o servidor vai escutar
status = listen(sd,QLEN);
if (status != 0) {
perror("Erro na chamada listen");
exit(1);
}
// Aceitar uma nova conexão TCP
size = sizeof(clientaddr);
while(1)
{
newsd = accept( sd,
(struct sockaddr *) &clientaddr,
(socklen_t *) &size);
if (newsd < 0) {
perror("Erro na chamada accept");
exit(1);
}
sem_wait(&mutex);
int * arg = malloc(sizeof(*arg));
*arg = newsd;
threads[thread_count] = pthread_create(&threads[thread_count], NULL, worker, arg);
thread_count++;
sem_post(&mutex);
sem_wait(&sensor);
printf("nivel de luminosidade: %d\n", LUMINOSIDADE);
sem_post(&sensor);
}
}
void httpSetVar(char * path, int mode) {
FILE * persistentConfig;
char data[7];
sprintf(data, "%d\r\n", mode);
sem_wait(&persistentFiles);
persistentConfig = fopen(path, "w");
if (persistentConfig != NULL) {
fwrite(data, 1, sizeof(data), persistentConfig);
fclose(persistentConfig);
sem_post(&persistentFiles);
printf("%s set as %s\r\n", path, data);
} else {
printf("httpSetVar error.\r\n");
}
}
bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}
void * worker(void * arg) {
int wsd = *((int *) arg);
int status;
char rxbuffer[BUFFERSIZE];
char filename[BUFFERSIZE];
int temp = 0;
printf("\r\n############ WORKER ############\r\n");
// Recepção mensagens
while (temp < 1) {
status = read(wsd,rxbuffer, sizeof(rxbuffer));
if (status <= 0) perror("Erro na chamada read");
printf("Mensagem recebida:\r\n%s\r\n", rxbuffer);
sscanf(rxbuffer, "%s %s %s", req, path, ver);
printf("REQUEST: %s\r\nPATH: %s\r\nVER:%s\r\n", req, path, ver);
// Verificar req GET
if (strcmp(req,"GET")!=0) {
strcpy(str, "HTTP/1.0 400 Bad Request\n");
printf("%s",str);
write (wsd, str, strlen(str));
perror("\tErro na request, nao é GET\r\n");
temp++;
continue;
}
// Verificar HTTP 1.0
if (strcmp(ver,"HTTP/1.0")!=0) {
strcpy(str, "HTTP/1.0 500 HTTP Version Not Supported\n");
printf("%s",str);
write (wsd, str, strlen(str));
perror("\tErro na versao HTTP, nao é 1.0\r\n");
temp++;
continue;
}
if (strcmp(path, "/www/setMode?mode=0")==0) {
httpSetVar("www/MODO.txt", 0);
strcpy(str, "HTTP/1.0 204 No Content\r\n\r\n");
printf("%s",str);
write (wsd, str, strlen(str));
temp++;
continue;
} else if (strcmp(path, "/www/setMode?mode=1")==0) {
httpSetVar("www/MODO.txt", 1);
strcpy(str, "HTTP/1.0 204 No Content\r\n\r\n");
printf("%s",str);
write (wsd, str, strlen(str));
temp++;
continue;
} else if (strcmp(path, "/www/setState?state=0")==0) {
httpSetVar("www/ESTADO.txt", 0);
strcpy(str, "HTTP/1.0 204 No Content\r\n\r\n");
printf("%s",str);
write (wsd, str, strlen(str));
temp++;
continue;
} else if (strcmp(path, "/www/setState?state=1")==0) {
httpSetVar("www/ESTADO.txt", 1);
strcpy(str, "HTTP/1.0 204 No Content\r\n\r\n");
printf("%s",str);
write (wsd, str, strlen(str));
temp++;
continue;
} else if (startsWith("/www/setIntensity?intensity=", path)==true) {
char * pch = strtok (path,"=");
pch = strtok(NULL, "=");
int intensity = atoi(pch);
printf("Intensity sent = %s -> %d\r\n", pch, intensity);
if (intensity >= 0 && intensity <= 100) {
httpSetVar("www/INTENSIDADE.txt", intensity);
strcpy(str, "HTTP/1.0 204 No Content\r\n\r\n");
printf("%s",str);
write (wsd, str, strlen(str));
temp++;
continue;
}
}
strcpy(filename, BASE);
strcat(filename, path);
printf("\tCaminho solicitado: %s\n", filename);
status = transferfile(filename, wsd);
temp++;
if (status == -1) {
FILE * tmp;
tmp = fopen( "temp.html", "w" );
if (tmp != NULL ) {
sem_wait(&file_write);
char html_response[BUFFERSIZE];
gen_dirlist(html_response, filename);
fputs( html_response, tmp );
fclose(tmp);
status = transferfile("temp.html",wsd);
sem_post(&file_write);
if (status == -1) printf("\tErro na transferencia do arquivo [listdir]. \r\n");
} else printf("\tErro na transferencia do arquivo [temp write]. \r\n");
}
status = close(wsd);
if (status == -1) perror("Erro na chamada close");
sem_wait(&mutex);
thread_count--;
sem_post(&mutex);
}
return NULL;
}
void gen_dirlist(char * html_response, char * path) {
char ls[BUFFERSIZE];
char link[BUFFERSIZE];
char * entry;
char * relativePath;
unsigned int i;
strcpy(html_response, "<html><head><title>Dir List</title></head><body>");
for (i = 0; i < strlen(BASE) || i < strlen(path); i++) {
if (BASE[i] != path[i]) break;
}
relativePath = &path[i];
printf("Caminho relativo: %s\r\n", relativePath);
printf("Listando o diretorio %s\r\n", path);
lista_diretorio(path, ls, BUFFERSIZE);
printf("%s\r\n", ls);
entry = strtok(ls, "\n");
while (entry != NULL) {
strcpy(link, "<p><a href=\"");
strcat(link, relativePath);
strcat(link, "/");
strcat(link, entry);
strcat(link, "\">");
strcat(link, entry);
strcat(link, "</a></p>");
strcat(html_response, link);
entry = strtok(NULL, "\n");
}
strcat(html_response, "</body></html>");
printf("HTML do diretorio: %s\r\n\r\n", html_response);
}
void readSensor(int arg)
{
int lumMin = 0;
int lumMax = 500000;
int count;
int k = 0;
int i = 0;
int q = 0;
int mean = 0;
int countList[NB_SAMPLES_SENSOR] = {0};
//printf("test0\n");
while (1)
{
//printf("test1\n");
count = 0;
wiringPiSetup () ;
pinMode(SENSOR,OUTPUT);
pullUpDnControl(SENSOR,PUD_OFF);
digitalWrite(SENSOR,0);
//printf("test2\n");
sleep(1);
pinMode(SENSOR,INPUT);
while(digitalRead(SENSOR) == 0)
{
//printf("%d",count);
count++;
//sleep(0.1);
}
//printf("valor de count: %d\n", count);
countList[k] = count;
k++;
if (k == NB_SAMPLES_SENSOR)
{
i = 1;
k = 0;
}
//printf("k = %d e i = %d\n", k, i);
/*for (q=0; q < NB_SAMPLES_SENSOR; q++)
{
printf("countList[%d] : %d", q, countList[q]);
}
*/
printf("lumMin = %d e lumMax = %d\n", lumMin, lumMax);
if (i == 1)
{
mean = 0;
for (q = 0; q < NB_SAMPLES_SENSOR; q++)
{
mean = mean + countList[q];
}
mean = mean / NB_SAMPLES_SENSOR;
if (mean < lumMax)
lumMax = count;
if (mean > lumMin)
lumMin = count;
sem_wait(&sensor);
if (lumMin-lumMax != 0)
{
LUMINOSIDADE = (lumMin-count)*100/(lumMin-lumMax);
}
if (LUMINOSIDADE < 0)
LUMINOSIDADE = 0;
else if (LUMINOSIDADE > 100)
LUMINOSIDADE = 100;
printf("nivel de luminosidade: %d\n\n", LUMINOSIDADE);
sem_post(&sensor);
}
usleep(500);
}
}