Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
/* httpd.c
Copyright (C) 2022-2026 Timo Kokkonen <tjko@iki.fi>
SPDX-License-Identifier: GPL-3.0-or-later
This file is part of FanPico.
FanPico is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FanPico is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FanPico. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "pico/stdlib.h"
#include "lwip/apps/fs.h"
#include "cJSON.h"
#include "fanpico.h"
typedef struct ssi_conn_state {
void *buf;
uint32_t buf_size;
uint32_t buf_left;
uint16_t next_part;
} ssi_conn_state_t;
void *fs_state_init(struct fs_file *file, const char *name)
{
ssi_conn_state_t *state = NULL;
log_msg(LOG_INFO, "fs_state_init(%p,'%s')", file, name);
if (strncmp(name, "/status.", 8))
return NULL;
if (!(state = malloc(sizeof(ssi_conn_state_t))))
return NULL;
state->buf = NULL;
state->buf_size = 0;
state->buf_left = 0;
state->next_part = 0;
return state;
}
void fs_state_free(struct fs_file *file, void *conn_state)
{
ssi_conn_state_t *state = (ssi_conn_state_t*)conn_state;
log_msg(LOG_INFO, "fs_state_free(%p,%p)", file, state);
if (state == NULL)
return;
if (state->buf)
free(state->buf);
free(state);
}
#define BUF_LEN 1024
u16_t csv_stats(char *insert, int insertlen, u16_t current_tag_part, u16_t *next_tag_part,
ssi_conn_state_t *state)
{
const struct fanpico_state *st = fanpico_state;
if (!state)
return 0;
if (current_tag_part == 0) {
/* Generate 'output' into a buffer that then will be fed in chunks to LwIP... */
char *buf;
double rpm, pwm;
char row[128];
if (!(buf = malloc(BUF_LEN)))
return 0;
buf[0] = 0;
for (int i = 0; i < FAN_COUNT; i++) {
rpm = st->fan_freq[i] * 60 / cfg->fans[i].rpm_factor;
snprintf(row, sizeof(row), "fan%d,\"%s\",%.0lf,%.2f,%.1f\n",
i+1,
cfg->fans[i].name,
rpm,
st->fan_freq[i],
st->fan_duty[i]);
strncatenate(buf, row, BUF_LEN);
}
for (int i = 0; i < MBFAN_COUNT; i++) {
rpm = st->mbfan_freq[i] * 60 / cfg->mbfans[i].rpm_factor;
snprintf(row, sizeof(row), "mbfan%d,\"%s\",%.0lf,%.2f,%.1f\n",
i+1,
cfg->mbfans[i].name,
rpm,
st->mbfan_freq[i],
st->mbfan_duty[i]);
strncatenate(buf, row, BUF_LEN);
}
for (int i = 0; i < SENSOR_COUNT; i++) {
pwm = sensor_get_duty(&cfg->sensors[i].map, st->temp[i]);
snprintf(row, sizeof(row), "sensor%d,\"%s\",%.1lf,%.1lf\n",
i+1,
cfg->sensors[i].name,
st->temp[i],
pwm);
strncatenate(buf, row, BUF_LEN);
}
for (int i = 0; i < VSENSOR_COUNT; i++) {
pwm = sensor_get_duty(&cfg->vsensors[i].map, st->vtemp[i]);
snprintf(row, sizeof(row), "vsensor%d,\"%s\",%.1lf,%.1lf,%.0f,%.0f\n",
i+1,
cfg->vsensors[i].name,
st->vtemp[i],
pwm,
st->vhumidity[i],
st->vpressure[i]);
strncatenate(buf, row, BUF_LEN);
}
snprintf(row, sizeof(row), "vref1,\"ADC Vref\",%.4lf\n",
cfg->adc_vref);
strncatenate(buf, row, BUF_LEN);
state->buf = buf;
state->buf_left = state->buf_size = strlen(buf);
state->next_part = 1;
}
/* Copy a part of the multi-part response into LwIP buffer ...*/
size_t count = (state->buf_left < insertlen - 1 ? state->buf_left : insertlen - 1);
if (count > 0) {
memcpy(insert, state->buf + (state->buf_size - state->buf_left), count);
state->buf_left -= count;
if (state->buf_left > 0)
*next_tag_part = state->next_part++;
}
return count;
}
u16_t json_stats(char *insert, int insertlen, u16_t current_tag_part, u16_t *next_tag_part,
ssi_conn_state_t *state)
{
const struct fanpico_state *st = fanpico_state;
cJSON *json = NULL;
size_t count = 0;
if (!state)
return 0;
if (current_tag_part == 0) {
/* Generate 'output' into a buffer that then will be fed in chunks to LwIP... */
cJSON *array, *o;
if (!(json = cJSON_CreateObject()))
goto panic;
/* Fans */
if (!(array = cJSON_CreateArray()))
goto panic;
for (int i = 0; i < FAN_COUNT; i++) {
double rpm = st->fan_freq[i] * 60 / cfg->fans[i].rpm_factor;
if (!(o = cJSON_CreateObject()))
goto panic;
cJSON_AddItemToObject(o, "fan", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->fans[i].name));
cJSON_AddItemToObject(o, "rpm", cJSON_CreateNumber(round_decimal(rpm, 0)));
cJSON_AddItemToObject(o, "frequency", cJSON_CreateNumber(round_decimal(st->fan_freq[i], 2)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(st->fan_duty[i], 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "fans", array);
/* MB Fans */
if (!(array = cJSON_CreateArray()))
goto panic;
for (int i = 0; i < MBFAN_COUNT; i++) {
double rpm = st->mbfan_freq[i] * 60 / cfg->mbfans[i].rpm_factor;
if (!(o = cJSON_CreateObject()))
goto panic;
cJSON_AddItemToObject(o, "mbfan", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->mbfans[i].name));
cJSON_AddItemToObject(o, "rpm", cJSON_CreateNumber(round_decimal(rpm, 0)));
cJSON_AddItemToObject(o, "frequency", cJSON_CreateNumber(round_decimal(st->mbfan_freq[i], 2)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(st->mbfan_duty[i], 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "mbfans", array);
/* Sensors */
if (!(array = cJSON_CreateArray()))
goto panic;
for (int i = 0; i < SENSOR_COUNT; i++) {
double pwm = sensor_get_duty(&cfg->sensors[i].map, st->temp[i]);
if (!(o = cJSON_CreateObject()))
goto panic;
cJSON_AddItemToObject(o, "sensor", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->sensors[i].name));
cJSON_AddItemToObject(o, "temperature", cJSON_CreateNumber(round_decimal(st->temp[i], 1)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(pwm, 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "sensors", array);
cJSON_AddItemToObject(json, "adc_vref", cJSON_CreateNumber(round_decimal(cfg->adc_vref,4)));
/* Virtual Sensors */
if (!(array = cJSON_CreateArray()))
goto panic;
for (int i = 0; i < VSENSOR_COUNT; i++) {
double pwm = sensor_get_duty(&cfg->vsensors[i].map, st->vtemp[i]);
if (!(o = cJSON_CreateObject()))
goto panic;
cJSON_AddItemToObject(o, "sensor", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->vsensors[i].name));
cJSON_AddItemToObject(o, "temperature", cJSON_CreateNumber(round_decimal(st->vtemp[i], 1)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(pwm, 1)));
if (st->vhumidity[i] >= 0.0)
cJSON_AddItemToObject(o, "humidity", cJSON_CreateNumber(round_decimal(st->vhumidity[i], 1)));
if (st->vpressure[i] >= 0.0)
cJSON_AddItemToObject(o, "pressure", cJSON_CreateNumber(round_decimal(st->vpressure[i], 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "vsensors", array);
if (!(state->buf = cJSON_Print(json)))
goto panic;
state->buf_left = state->buf_size = strlen(state->buf);
state->next_part = 1;
}
/* Copy a part of the multi-part response into LwIP buffer ...*/
count = (state->buf_left < insertlen - 1 ? state->buf_left : insertlen - 1);
if (count > 0) {
memcpy(insert, state->buf + (state->buf_size - state->buf_left), count);
state->buf_left -= count;
if (state->buf_left > 0)
*next_tag_part = state->next_part++;
}
panic:
if (json)
cJSON_Delete(json);
return count;
}
u16_t fanpico_ssi_handler(const char *tag, char *insert, int insertlen,
u16_t current_tag_part, u16_t *next_tag_part, void *conn_state)
{
const struct fanpico_state *st = fanpico_state;
ssi_conn_state_t *state = (ssi_conn_state_t*)conn_state;
size_t printed = 0;
/* printf("ssi_handler(\"%s\",%lx,%d,%u,%u,%p)\n", tag, (uint32_t)insert, insertlen, current_tag_part, *next_tag_part, conn_state); */
if (!strncmp(tag, "datetime", 8)) {
time_t t;
if (rtc_get_time(&t)) {
time_t_to_str(insert, insertlen, t);
printed = strnlen(insert, insertlen);
}
}
else if (!strncmp(tag, "uptime", 6)) {
uint32_t secs = to_us_since_boot(get_absolute_time()) / 1000000;
uint32_t mins = secs / 60;
uint32_t hours = mins / 60;
uint32_t days = hours / 24;
printed = snprintf(insert, insertlen, "%lu days %02lu:%02lu:%02lu",
days,
hours % 24,
mins % 60,
secs % 60);
}
else if (!strncmp(tag, "watchdog", 8)) {
printed = snprintf(insert, insertlen, "%s",
(rebooted_by_watchdog ? "☹" : "☺"));
}
else if (!strncmp(tag, "model", 5)) {
printed = snprintf(insert, insertlen, "%s", FANPICO_MODEL);
}
else if (!strncmp(tag, "version", 5)) {
printed = snprintf(insert, insertlen, "%s", FANPICO_VERSION);
}
else if (!strncmp(tag, "name", 4)) {
printed = snprintf(insert, insertlen, "%s", cfg->name);
}
else if (!strncmp(tag, "fanrow", 6)) {
uint8_t i = tag[6] - '1';
if (i < FAN_COUNT && cfg->http_fan_mask & (1 << i)) {
double rpm = st->fan_freq[i] * 60 / cfg->fans[i].rpm_factor;
printed = snprintf(insert, insertlen, "<tr><td>%d<td>%s<td>%0.0f<td align=\"right\">%0.0f %%",
i + 1,
cfg->fans[i].name,
rpm,
st->fan_duty[i]);
}
}
else if (!strncmp(tag, "mfanrow", 7)) {
uint8_t i = tag[7] - '1';
if (MBFAN_COUNT == 0 && i == 0) {
printed = snprintf(insert, insertlen, "<tr><td colspan=4>Not Available");
}
else if (i < MBFAN_COUNT && cfg->http_mbfan_mask & (1 << i)) {
double rpm = st->mbfan_freq[i] * 60 / cfg->mbfans[i].rpm_factor;
printed = snprintf(insert, insertlen, "<tr><td>%d<td>%s<td>%0.0f<td align=\"right\">%0.0f %%",
i + 1,
cfg->mbfans[i].name,
rpm,
st->mbfan_duty[i]);
}
}
else if (!strncmp(tag, "sensrow", 7)) {
uint8_t i = tag[7] - '1';
if (i < SENSOR_COUNT && cfg->http_sensor_mask & (1 << i)) {
printed = snprintf(insert, insertlen, "<tr><td>%d<td>%s<td align=\"right\">%0.1f ℃",
i + 1,
cfg->sensors[i].name,
st->temp[i]);
}
}
else if (!strncmp(tag, "vsenrow", 7)) {
uint8_t i = tag[7] - '1';
if (i < VSENSOR_COUNT && cfg->http_vsensor_mask & (1 << i)) {
char other[24], tmp[12];
other[0] = 0;
if (st->vhumidity[i] >= 0.0) {
snprintf(tmp, sizeof(tmp), "%0.0f %%rh", st->vhumidity[i]);
strncatenate(other, tmp, sizeof(other));
}
if (st->vpressure[i] >= 0.0) {
snprintf(tmp, sizeof(tmp), "%0.0f hPa", st->vpressure[i]);
if (strlen(other) > 0)
strncatenate(other, ", ", sizeof(other));
strncatenate(other, tmp, sizeof(other));
}
printed = snprintf(insert, insertlen,
"<tr><td>%d<td>%s<td align=\"right\">%0.1f ℃<td>%s",
i + 1, cfg->vsensors[i].name, st->vtemp[i], other);
}
}
else if (!strncmp(tag, "adcvref", 7)) {
printed = snprintf(insert, insertlen, "<td>Vref<td>ADC<td align=\"right\">%0.4f V",
cfg->adc_vref);
}
else if (!strncmp(tag, "csvstat", 7)) {
printed = csv_stats(insert, insertlen, current_tag_part, next_tag_part, state);
}
else if (!strncmp(tag, "jsonstat", 8)) {
printed = json_stats(insert, insertlen, current_tag_part, next_tag_part, state);
}
else if (!strncmp(tag, "refresh", 8)) {
/* generate "random" refresh time for a page, to help spread out the load... */
printed = snprintf(insert, insertlen, "%u", (uint)(30 + ((double)rand() / RAND_MAX) * 30));
}
/* Check if snprintf() output was truncated... */
printed = (printed >= insertlen ? insertlen - 1 : printed);
/* printf("printed=%u\n", printed); */
return printed;
}
260934f58This file is pinned to the selected release. Download the original to open it in your design tool.