+/*
+ * Return the VR1 potentiometer voltage.
+ */
+static int cgi_resistor(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)recv_buf;
+ (void)recv_len;
+ (void)name;
+
+ uint16_t volt = ADC_RANGECONV(adc_read(1), 0, 3300);
+ kprintf("volt %d\n", volt);
+ sprintf((char *)tx_buf, "[ \"%d.%dV\" ]", volt / 1000, volt % 1000);
+
+ http_sendOk(client, HTTP_CONTENT_JSON);
+ netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
+ return 0;
+}
+
+
+#define CGI_LED_ID_KEY "n"
+#define CGI_LED_CMD_KEY "set"
+
+static char key_value[80];
+
+/*
+ * Reply to client the request string.
+ */
+static int cgi_led(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)recv_buf;
+ (void)recv_len;
+ (void)name;
+
+ char *query_str = strstr(name, "?") + 1;
+ size_t query_str_len = strlen(query_str);
+ int led_id;
+ int led_cmd;
+
+ int len = http_tokenizeGetRequest(query_str, query_str_len);
+
+ LOG_INFO("Found %d key/value pair\n", len);
+
+ if (http_getValue(query_str, query_str_len, CGI_LED_ID_KEY, key_value, sizeof(key_value)) < 0)
+ {
+ LOG_ERR("key %s, not found\n", CGI_LED_ID_KEY);
+ goto error;
+ }
+
+ LOG_INFO("Found key %s = %s\n", CGI_LED_ID_KEY, key_value);
+ led_id = atoi(key_value);
+
+
+ if (http_getValue(query_str, query_str_len, CGI_LED_CMD_KEY, key_value, sizeof(key_value)) < 0)
+ {
+ LOG_ERR("key %s, not found\n", CGI_LED_CMD_KEY);
+ goto error;
+ }
+
+ LOG_INFO("Found key %s = %s\n", CGI_LED_CMD_KEY, key_value);
+ led_cmd = atoi(key_value);
+
+ if (led_cmd)
+ {
+ LED_ON(led_id);
+ SET_LED_STATUS(status.led_status, led_id);
+ }
+ else
+ {
+ LED_OFF(led_id);
+ CLEAR_LED_STATUS(status.led_status, led_id);
+ }
+
+ sprintf((char *)tx_buf, "{\"n\":%d, \"set\":,%d}", led_id, led_cmd);
+
+ http_sendOk(client, HTTP_CONTENT_JSON);
+ netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
+ return 0;
+
+error:
+ http_sendInternalErr(client, HTTP_CONTENT_JSON);
+ return 0;
+}
+
+static int cgi_ledStatus(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)recv_buf;
+ (void)recv_len;
+ (void)name;
+
+ sprintf((char *)tx_buf, "{ \"0\":\"%d\", \"1\":\"%d\", \"2\":\"%d\"}",
+ GET_LED_STATUS(status.led_status, 0),
+ GET_LED_STATUS(status.led_status, 1),
+ GET_LED_STATUS(status.led_status, 2));
+
+ http_sendOk(client, HTTP_CONTENT_JSON);
+ netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
+ return 0;
+}
+
+
+#define CGI_MSG_CMD_KEY "msg"
+
+static int cgi_displayMsg(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)recv_buf;
+ (void)recv_len;
+ (void)name;
+
+ char *query_str = strstr(name, "?") + 1;
+ size_t query_str_len = strlen(query_str);
+
+ int len = http_tokenizeGetRequest(query_str, query_str_len);
+
+ LOG_INFO("Found %d key/value pair\n", len);
+
+ if (http_getValue(query_str, query_str_len, CGI_MSG_CMD_KEY, key_value, sizeof(key_value)) > 0)
+ {
+
+ LOG_INFO("Found key %s = %s\n", CGI_MSG_CMD_KEY, key_value);
+
+ gfx_bitmapClear(lcd_bitmap);
+ text_style(lcd_bitmap, STYLEF_BOLD | STYLEF_UNDERLINE, STYLEF_BOLD | STYLEF_UNDERLINE);
+ text_xprintf(lcd_bitmap, 0, 0, TEXT_CENTER | TEXT_FILL, "BeRTOS Simple Http Server");
+ text_style(lcd_bitmap, 0, STYLEF_MASK);
+ text_xprintf(lcd_bitmap, 2, 0, TEXT_CENTER | TEXT_FILL, "Your message:");
+ text_xprintf(lcd_bitmap, 10, 0, TEXT_CENTER, "%s", key_value);
+
+ lcd_hx8347_blitBitmap(lcd_bitmap);
+
+ http_sendOk(client, HTTP_CONTENT_JSON);
+ return 0;
+ }
+
+ http_sendInternalErr(client, HTTP_CONTENT_JSON);
+ return 0;
+}
+
+/*
+ * Reply to client the request string.
+ */
+static int cgi_echo(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)name;
+
+ http_sendOk(client, HTTP_CONTENT_PLAIN);
+ netconn_write(client, recv_buf, recv_len, NETCONN_COPY);
+ return 0;
+}
+
+/*
+ * Return to client a string that display the CHIP ID information.
+ * See datasheet for more detail.
+ */
+static int cgi_chipInfo(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)recv_buf;
+ (void)recv_len;
+ (void)name;
+
+ sprintf((char *)tx_buf, "{ \"core_name\":\"%s\", \"arch_name\":\"%s\", \"sram_size\":\"%s\",\"flash_size\":\"%s\", \"mem_boot_type\":\"%s\" }",
+ chipid_eproc_name(CHIPID_EPRCOC()),
+ chipid_archnames(CHIPID_ARCH()),
+ chipid_sramsize(CHIPID_SRAMSIZ()),
+ chipid_nvpsize(CHIPID_NVPSIZ()),
+ chipid_nvptype(CHIPID_NVTYP()));
+
+ http_sendOk(client, HTTP_CONTENT_JSON);
+ netconn_write(client, tx_buf, strlen((char *)tx_buf), NETCONN_COPY);
+
+ return 0;
+}
+
+
+static int cgi_error(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)client;
+ (void)name;
+ (void)recv_buf;
+ (void)recv_len;
+
+ return -1;
+}
+
+
+/*
+ * Default function that http server call every client request, if it doesn't match a cgi table.
+ * In this implementation all client request are associate to real file stored on FAT file
+ * sistem on SD card. If the file there is not on SD card the server reply to client the
+ * error File not found, and send an harcoded page. In the same way, the server reply
+ * error page if the SD card is not present.
+ *
+ */
+static int http_htmPageLoad(struct netconn *client, const char *name, char *recv_buf, size_t recv_len)
+{
+ (void)recv_buf;
+ (void)recv_len;
+
+ if (SD_CARD_PRESENT())
+ {
+
+ // SD fat filesystem context
+ Sd sd;
+ FATFS fs;
+ FatFile in_file;
+ FRESULT result;
+
+ bool sd_ok = sd_init(&sd, NULL, 0);
+ if (sd_ok)
+ {
+ LOG_INFO("Mount FAT filesystem.\n");
+ result = f_mount(0, &fs);
+ if (result != FR_OK)
+ {
+ LOG_ERR("Mounting FAT volumes error[%d]\n", result);
+ sd_ok = false;
+ f_mount(0, NULL);
+ }
+
+ if (sd_ok)
+ {
+ result = fatfile_open(&in_file, name, FA_OPEN_EXISTING | FA_READ);
+
+ size_t count = 0;
+ if (result == FR_OK)
+ {
+ LOG_INFO("Opened file '%s' size %ld\n", name, in_file.fat_file.fsize);
+
+ int type = http_searchContentType(name);
+ http_sendOk(client, type);
+
+ 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);
+
+ LOG_INFO("Sent: %d\n", count);
+ }
+ else
+ {
+ LOG_ERR("Unable to open file: '%s' error[%d]\n", name, result);
+ http_sendFileNotFound(client, HTTP_CONTENT_HTML);
+ netconn_write(client, http_file_not_found, http_file_not_found_len - 1, NETCONN_NOCOPY);
+ }
+ }
+ }
+ f_mount(0, NULL);
+ LOG_INFO("Umount FAT filesystem.\n");
+ }
+ else
+ {
+ http_sendFileNotFound(client, HTTP_CONTENT_HTML);
+ netconn_write(client, http_sd_not_present, http_sd_not_present_len, NETCONN_NOCOPY);
+ }
+
+ return 0;
+}
+
+
+/*
+ * Static cgi table where we associate callback to page.
+ */