Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
page/page.c
- Committer:
- andrewboyson
- Date:
- 2019-03-08
- Revision:
- 59:309e78f243dd
- Parent:
- 58:e5ab14ef6ea6
- Child:
- 77:4689596a2f3f
File content as of revision 59:309e78f243dd:
#include <stdio.h>
#include "http.h"
#include "page.h"
#include "page-derived.h"
#include "mac.h"
#include "ip4addr.h"
#include "ip6addr.h"
void PageAddHeader(const char* site, const char* title, const char* style, const char* script)
{
HttpAddText("<!DOCTYPE html>\r\n"
"<html>\r\n"
"<head>\r\n");
if (site)
{
HttpAddText(" <title>");
HttpAddText(site);
if (title)
{
HttpAddText(" - ");
HttpAddText(title);
}
HttpAddText("</title>\r\n");
}
HttpAddText(" <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
if (style)
{
HttpAddText(" <link rel='stylesheet' href='/");
HttpAddText(style);
HttpAddText("' type='text/css'/>\r\n");
}
if (script)
{
HttpAddText(" <script src='/");
HttpAddText(script);
HttpAddText("' type='text/javascript'></script>\r\n");
}
HttpAddText(" <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
" <link rel='icon' href='/favicon.ico' type='image/x-icon'/>\r\n"
"</head>\r\n"
"<body>\r\n");
}
void PageAddH1(const char* site, const char* pageName)
{
HttpAddText("<h1>");
HttpAddText(site);
HttpAddText(" - ");
HttpAddText(pageName);
HttpAddText("</h1>\r\n");
}
void PageAddH2(const char* text)
{
HttpAddText("<h2>");
HttpAddText(text);
HttpAddText("</h2>\r\n");
}
void PageAddEnd()
{
HttpAddText("</body>\r\n"
"</html>\r\n");
}
void PageAddNavItem(int highlight, char* href, char* title)
{
char *p;
HttpAddText("<li ");
if (highlight) p = "class='this'";
else p = " ";
HttpAddText(p);
HttpAddText("><a href='");
HttpAddText(href);
HttpAddText("'>");
HttpAddText(title);
HttpAddText("</a></li>\r\n");
}
void PageAddNav(int page)
{
HttpAddText("<nav><ul>\r\n");
PageAddNavDerived(page);
PageAddNavItem(page == CLOCK_PAGE, "/clock", "Clock");
PageAddNavItem(page == FAULT_PAGE, "/fault", "Fault");
PageAddNavItem(page == NET_PAGE, "/net", "Net");
PageAddNavItem(page == LOG_PAGE, "/log", "Log");
PageAddNavItem(page == TRACE_PAGE, "/trace", "Trace");
PageAddNavItem(page == FIRMWARE_PAGE, "/firmware", "Firmware");
HttpAddText("</ul></nav>\r\n");
}
void PageAddLabelledValue(float labelwidth, char* label, char* value)
{
HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
HttpAddText(label);
HttpAddText("<span style='float:right;'>");
HttpAddText(value);
HttpAddText("</span>");
HttpAddText("</div>\r\n");
}
void PageAddLabelledMac(float labelwidth, char* label, char* mac)
{
HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
HttpAddText(label);
HttpAddText("<span style='float:right;'>");
MacHttp (mac);
HttpAddText("</span>");
HttpAddText("</div>\r\n");
}
void PageAddLabelledIp4(float labelwidth, char* label, uint32_t ip)
{
HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
HttpAddText(label);
HttpAddText("<span style='float:right;'>");
Ip4AddressHttp (ip);
HttpAddText("</span>");
HttpAddText ("</div>\r\n");
}
void PageAddLabelledIp6(float labelwidth, char* label, char* ip)
{
HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
HttpAddText(label);
HttpAddText("<span style='float:right;'>");
Ip6AddressHttp (ip);
HttpAddText("</span>");
HttpAddText ("</div>\r\n");
}
void PageAddLabelledOnOff(float labelwidth, char* label, int value)
{
if (value) PageAddLabelledValue(labelwidth, label, "On");
else PageAddLabelledValue(labelwidth, label, "Off");
}
void PageAddLabelledInt(float labelwidth, char* label, int value)
{
char text[30];
snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant.
PageAddLabelledValue(labelwidth, label, text);
}
void PageAddInputText(float labelwidth, char* label, float inputwidth, char* value, char* action, char* name)
{
HttpAddF ("<form action='%s' method='get'>\r\n", action);
HttpAddF (" <div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label);
HttpAddF (" <input type='text' name='%s' style='width:%.1fem;' value='%s'>\r\n", name, inputwidth, value);
HttpAddText(" <input type='submit' value='Set' style='display:none;'>\r\n");
HttpAddF ("</form>\r\n");
}
void PageAddInputInt(float labelwidth, char* label, float inputwidth, int value, char* action, char* name)
{
char text[30];
snprintf(text, sizeof(text), "%d", value);
PageAddInputText(labelwidth, label, inputwidth, text, action, name);
}
void PageAddInputButton(float labelwidth, char* label, char* value, char* action, char* name)
{
HttpAddF("<form action='%s' method='get'>\r\n", action);
HttpAddF("<input type='hidden' name='%s'>\r\n", name);
if (labelwidth < 0.1) HttpAddF("%s\r\n", label);
else HttpAddF("<div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label);
HttpAddF("<input type='submit' value='%s'>\r\n", value);
HttpAddText("</form>\r\n");
}
void PageAddAjaxInputToggle(float labelwidth, char* label, char* id, char* name)
{
HttpAddText("<div>\r\n");
HttpAddF (" <div style='display: inline-block; width:%.1fem;'>%s</div>\r\n", labelwidth, label);
HttpAddF (" <div class='toggle' id='%s' dir='ltr' onclick='AjaxRequest(\"%s=1\")'>\r\n", id, name);
HttpAddText(" <div class='slot'></div><div class='knob'></div>\r\n");
HttpAddText(" </div>\r\n");
HttpAddText("</div>\r\n");
}
void PageAddAjaxLed(float labelwidth, char* label, char* id)
{
HttpAddText("<div>\r\n");
HttpAddF (" <div style='display:inline-block; width:%.1fem;'>%s</div>\r\n", labelwidth, label);
HttpAddF (" <div class='led' id='%s' dir='ltr'></div>\r\n", id);
HttpAddText("</div>\r\n");
}
void PageAddAjaxInput(float labelwidth, char* label, float inputwidth, char* id, char* name)
{
HttpAddText("<div>\r\n");
HttpAddF (" <div style='display: inline-block; width:%.1fem;'>%s</div>\r\n", labelwidth, label);
HttpAddF (" <input type='text' id='%s' style='width:%.1fem;' onchange='AjaxRequest(\"%s=\" + this.value)'>\r\n", id, inputwidth, name);
HttpAddText("</div>\r\n");
}
void PageAddAjaxLabelled(float labelwidth, char* label, char* id, char* suffix)
{
HttpAddText("<div>\r\n");
HttpAddF (" <div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label);
HttpAddF (" <span id='%s'></span>%s\r\n", id, suffix);
HttpAddText("</div>\r\n");
}