diff options
Diffstat (limited to 'app/httpd.c')
-rw-r--r-- | app/httpd.c | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/app/httpd.c b/app/httpd.c new file mode 100644 index 0000000..5c96c44 --- /dev/null +++ b/app/httpd.c @@ -0,0 +1,297 @@ +#include "project.h" + +#if 0 +#define FS_FILE_FLAGS_HEADER_INCLUDED 0x01 +#define FS_FILE_FLAGS_HEADER_PERSISTENT 0x02 +#define FS_FILE_FLAGS_HEADER_HTTPVER_1_1 0x04 +#define FS_FILE_FLAGS_SSI 0x08 +#endif + + +static char html_buf[8192]; + +EPOCH alarm; + +static uint32_t make_alarm (void) +{ + uint64_t abs = ref_get(); + EPOCH e, ad; + double ra, lra; + UTC u; + ST l; + char buf[128]; + + PTB_INIT (html_buf); + PTB_ADD ("<html>\n"); + PTB_ADD ("<body>\n"); + PTB_ADD ("<form method='get' action='/index.html'><input type='submit' value='back' /></form>\n"); + + PTB_ADD ("<h2>Alarm</h2>\n"); + + u = time_epoch_to_utc (alarm); + utc_to_str (buf, &u); + ra = time_utc_to_ra (u); + lra = ra_normalize (ra + gps_lon); + l = time_ra_to_st (lra); + + PTB_ADD ("<pre>\n"); + + e = ref_decompose (abs); + ad = time_epoch_sub (alarm, e); + + if (ad.s >= 0) { + unsigned d, h, m, s; + s = ad.s; + m = s / 60; + s -= m * 60; + h = m / 60; + m -= h * 60; + d = h / 24; + h -= d * 24; + + PTB_ADD (" in %d days, %02d:%02d:%02d\n", d, h, m, s); + } + + PTB_ADD (" UTC: %s\n", buf); + PTB_ADD (" RA: %.9f\n", ra); + PTB_ADD ("</pre>\n"); + + PTB_ADD ("<table>\n"); + PTB_ADD ("<tr><td><form method='get' action='/alarm_set.cgi'><td>LRA:</td><td><input type='text' name='lra' value='%.9f' size='32' maxlength='32' /></td><td><input type='submit' value='set' /></td></form></tr>\n", lra); + PTB_ADD ("<tr><td><form method='get' action='/alarm_set.cgi'><td>LST:</td><td><input type='text' name='lst' value='%02d:%02d:%02d.%06d' size='32' maxlength='32' /></td><td><input type='submit' value='set' /></td></form></tr>\n", l.hour, l.minute, l.second, l.nanosecond / 1000); + PTB_ADD ("</table>\n"); + + PTB_ADD ("</body>\n"); + PTB_ADD ("</html>\n"); + return PTB_LEN; +} + + + +static uint32_t make_index (void) +{ + uint64_t abs = ref_get(); + EPOCH e, ad; + double ra, lra; + UTC u; + ST l; + char buf[128]; + + PTB_INIT (html_buf); + PTB_ADD ("<html>\n"); + PTB_ADD ("<head><meta http-equiv='refresh' content='5; url=/'></head>\n"); + PTB_ADD ("<body>\n"); + PTB_ADD ("<form method='get' action='/index.html'><input type='submit' value='refresh' /></form>\n"); + PTB_ADD ("<h2>Now</h2>\n"); + + e = ref_decompose (abs); + u = time_epoch_to_utc (e); + utc_to_str (buf, &u); + ra = time_utc_to_ra (u); + lra = ra + gps_lon; + l = time_ra_to_st (lra); + + + + PTB_ADD ("<pre>\n"); + PTB_ADD (" UTC: %s\n", buf); + PTB_ADD (" RA: %.9f\n", ra); + PTB_ADD (" LRA: %.9f\n", lra); + PTB_ADD (" LST: %02d:%02d:%02d.%06d\n", l.hour, l.minute, l.second, l.nanosecond / 1000); + PTB_ADD ("</pre>\n"); + PTB_ADD ("\n"); + PTB_ADD ("<h2>Alarm</h2>\n"); + + u = time_epoch_to_utc (alarm); + utc_to_str (buf, &u); + ra = time_utc_to_ra (u); + lra = ra_normalize (ra + gps_lon); + l = time_ra_to_st (lra); + + PTB_ADD ("<pre>\n"); + + ad = time_epoch_sub (alarm, e); + + if (ad.s >= 0) { + unsigned d, h, m, s; + s = ad.s; + m = s / 60; + s -= m * 60; + h = m / 60; + m -= h * 60; + d = h / 24; + h -= d * 24; + + PTB_ADD (" in %d days, %02d:%02d:%02d\n", d, h, m, s); + } + + PTB_ADD (" UTC: %s\n", buf); + PTB_ADD (" RA: %.9f\n", ra); + PTB_ADD (" LRA: %.9f\n", lra); + PTB_ADD (" LST: %02d:%02d:%02d.%06d\n", l.hour, l.minute, l.second, l.nanosecond / 1000); + PTB_ADD ("</pre>\n"); + + PTB_ADD ("<form method='get' action='/alarm.html'><input type='submit' value='set' /></form>\n"); + + PTB_ADD ("<h2>GPS</h2>\n"); + PTB_ADD ("<pre>\n"); + PTB_ADD ("%s\n", gps_info); + PTB_ADD (" %s\n", gps_pos); + PTB_ADD (" %s\n", gps_svin_info); + PTB_ADD ("\n"); + + PTB_ADD ("%s", gps_svinfo); + PTB_ADD ("</pre>\n"); + PTB_ADD ("</body>\n"); + PTB_ADD ("</html>\n"); + + return PTB_LEN; +} + + +static int cgi_set_lra (double lra) +{ + uint64_t abs = ref_get(); + EPOCH e = ref_decompose (abs); + double ra = ra_normalize (lra - gps_lon); + alarm = time_ra_to_next_epoch (e, ra); + + return 0; +} + + + +static int cgi_set_lst (char *lst_str) +{ + double ra = 0; + char *a2, *a3; + char *s = NULL, *m = NULL, *h = NULL; + unsigned i = 0; + + printf ("set lst = %s\n", lst_str); + + a2 = index (lst_str, ':'); + + if (!a2) { + h = NULL; + m = NULL; + s = lst_str; + } else { + a2++; + a3 = index (a2, ':'); + + if (!a3) { + m = lst_str; + s = a2; + } else { + a3++; + h = lst_str; + m = a2; + s = a3; + } + } + + ra = atof (s); + + + if (h) + i += atoi (h); + + i *= 60; + + if (m) i += atoi (m); + + i *= 60; + + ra += (double) i; + + ra /= 240.; + + return cgi_set_lra (ra); +} + +static char *uri_unescape (char *c) +{ + char *ret = c, *ptr; + + for (ptr = c; *c; ++c, ++ptr) { + + if (*c != '%') { + *ptr = *c; + continue; + } + + if (!c[1] || !c[2]) break; + + *ptr = (xtoi (c[1]) << 4) | xtoi (c[2]); + + c += 2; + } + + * ptr = 0; + + return ret; +} + + +static const char *cgi_set_handler (int idx, int num_params, char *params[], char *values[]) +{ + do { + if (num_params != 1)break; + + if (!strcmp (params[0], "lra")) { + if (cgi_set_lra (atof (values[0]))) + break; + } else if (!strcmp (params[0], "lst")) { + if (cgi_set_lst (uri_unescape (values[0]))) + break; + } else + break; + + return "/index.html"; + + } while (0); + + return "/error.html"; +} + + +int fs_open_custom (struct fs_file *file, const char *name) +{ + int len; + + if (!strcmp (name, "/index.html")) + len = make_index(); + else if (!strcmp (name, "/alarm.html")) + len = make_alarm(); + else { + printf ("looking for url=%s, found nothing\n", name); + return 0; + } + + file->data = (const char *)html_buf; + file->len = len; + file->index = file->len; + file->pextension = NULL; + file->flags = 0; + + + return 1; +} + + + + +void fs_close_custom (struct fs_file *file) +{ +} + + + +static tCGI cgi_handlers[] = {{"/alarm_set.cgi", cgi_set_handler}}; +void cgi_init (void) +{ + http_set_cgi_handlers (cgi_handlers, sizeof (cgi_handlers) / sizeof (cgi_handlers[0])); +} + + |