- struct netconn *client;
- struct netbuf *rx_buf_conn;
- char *rx_buf;
- u16_t len;
-
- client = netconn_accept(server);
- if (!client)
- continue;
-
- //Update board status.
- sprintf(status.local_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->remote_ip.addr));
- sprintf(status.last_connected_ip, "%d.%d.%d.%d", IP_ADDR_TO_INT_TUPLE(client->pcb.ip->local_ip.addr));
- status.tot_req++;
-
- rx_buf_conn = netconn_recv(client);
- if (rx_buf_conn)
- {
- netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
- if (rx_buf)
- {
- memset(file_name, 0, sizeof(file_name));
- memset(tx_buf, 0, sizeof(tx_buf));
-
- get_fileName(rx_buf, file_name, sizeof(file_name));
-
- kprintf("%s\n", file_name);
- if (strlen(file_name) == 0)
- strcpy(file_name, "index.htm");
-
- if (!strcmp("bertos_jpg.jpg", file_name))
- {
- netconn_write(client, bertos_jpg, sizeof(bertos_jpg), NETCONN_NOCOPY);
- }
- else if (!strcmp("status", file_name))
- {
- sprintf((char *)tx_buf, "[ %s, %s, %d.%d, %ld, %d ]", status.local_ip, status.last_connected_ip,
- status.internal_temp / 10, status.internal_temp % 10,
- status.up_time, status.tot_req);
-
- netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
- }
- else if (SD_CARD_PRESENT())
- {
- bool sd_ok = sd_init(&sd, NULL, 0);
- if (sd_ok)
- {
- kprintf("Mount FAT filesystem.\n");
- result = f_mount(0, &fs);
- if (result != FR_OK)
- {
- kprintf("Mounting FAT volumes error[%d]\n", result);
- sd_ok = false;
- f_mount(0, NULL);
- }
-
- if (sd_ok)
- {
- result = fatfile_open(&in_file, file_name, FA_OPEN_EXISTING | FA_READ);
-
- size_t count = 0;
- if (result == FR_OK)
- {
- kprintf("Opened file '%s' size %ld\n", file_name, in_file.fat_file.fsize);
-
- netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY);
-
- while (count < in_file.fat_file.fsize)
- {
- int len = kfile_read(&in_file.fd, tx_buf, sizeof(tx_buf));
- netconn_write(client, tx_buf, len, NETCONN_COPY);
- count += len;
- }
-
- kfile_flush(&in_file.fd);
- kfile_close(&in_file.fd);
-
- kprintf("Sent: %d\n", count);
- }
- else
- {
- kprintf("Unable to open file: '%s' error[%d]\n", file_name, result);
- netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
- netconn_write(client, http_file_not_found, sizeof(http_file_not_found) - 1, NETCONN_NOCOPY);
- }
- }
- }
- f_mount(0, NULL);
- kprintf("Umount FAT filesystem.\n");
- }
- else
- {
- netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY);
- netconn_write(client, http_sd_not_present, sizeof(http_sd_not_present), NETCONN_NOCOPY);
- }
- }
- netconn_close(client);
- netbuf_delete(rx_buf_conn);
- }
- netconn_delete(client);