Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sat Apr 06 11:32:45 2019 +0000
Revision:
84:4ed751de613e
Parent:
77:4689596a2f3f
Child:
86:f3c9beec4ee7
Added option for a suffix to an input

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:f8998d10763e 1 #include <stdio.h>
andrewboyson 30:6a08abbe6301 2 #include "http.h"
andrewboyson 30:6a08abbe6301 3 #include "page.h"
andrewboyson 30:6a08abbe6301 4 #include "page-derived.h"
andrewboyson 30:6a08abbe6301 5 #include "mac.h"
andrewboyson 0:f8998d10763e 6 #include "ip4addr.h"
andrewboyson 0:f8998d10763e 7 #include "ip6addr.h"
andrewboyson 0:f8998d10763e 8
andrewboyson 2:14de8c14afd9 9 void PageAddHeader(const char* site, const char* title, const char* style, const char* script)
andrewboyson 0:f8998d10763e 10 {
andrewboyson 0:f8998d10763e 11 HttpAddText("<!DOCTYPE html>\r\n"
andrewboyson 0:f8998d10763e 12 "<html>\r\n"
andrewboyson 0:f8998d10763e 13 "<head>\r\n");
andrewboyson 2:14de8c14afd9 14 if (site)
andrewboyson 0:f8998d10763e 15 {
andrewboyson 0:f8998d10763e 16 HttpAddText(" <title>");
andrewboyson 2:14de8c14afd9 17 HttpAddText(site);
andrewboyson 2:14de8c14afd9 18 if (title)
andrewboyson 2:14de8c14afd9 19 {
andrewboyson 2:14de8c14afd9 20 HttpAddText(" - ");
andrewboyson 2:14de8c14afd9 21 HttpAddText(title);
andrewboyson 2:14de8c14afd9 22 }
andrewboyson 0:f8998d10763e 23 HttpAddText("</title>\r\n");
andrewboyson 0:f8998d10763e 24 }
andrewboyson 0:f8998d10763e 25 HttpAddText(" <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 26 if (style)
andrewboyson 0:f8998d10763e 27 {
andrewboyson 0:f8998d10763e 28 HttpAddText(" <link rel='stylesheet' href='/");
andrewboyson 0:f8998d10763e 29 HttpAddText(style);
andrewboyson 0:f8998d10763e 30 HttpAddText("' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 31 }
andrewboyson 0:f8998d10763e 32 if (script)
andrewboyson 0:f8998d10763e 33 {
andrewboyson 0:f8998d10763e 34 HttpAddText(" <script src='/");
andrewboyson 0:f8998d10763e 35 HttpAddText(script);
andrewboyson 0:f8998d10763e 36 HttpAddText("' type='text/javascript'></script>\r\n");
andrewboyson 0:f8998d10763e 37 }
andrewboyson 0:f8998d10763e 38 HttpAddText(" <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
andrewboyson 0:f8998d10763e 39 " <link rel='icon' href='/favicon.ico' type='image/x-icon'/>\r\n"
andrewboyson 0:f8998d10763e 40 "</head>\r\n"
andrewboyson 0:f8998d10763e 41 "<body>\r\n");
andrewboyson 0:f8998d10763e 42
andrewboyson 0:f8998d10763e 43 }
andrewboyson 12:237a0f75b4d0 44 void PageAddH1(const char* site, const char* pageName)
andrewboyson 12:237a0f75b4d0 45 {
andrewboyson 12:237a0f75b4d0 46 HttpAddText("<h1>");
andrewboyson 12:237a0f75b4d0 47 HttpAddText(site);
andrewboyson 12:237a0f75b4d0 48 HttpAddText(" - ");
andrewboyson 12:237a0f75b4d0 49 HttpAddText(pageName);
andrewboyson 12:237a0f75b4d0 50 HttpAddText("</h1>\r\n");
andrewboyson 12:237a0f75b4d0 51 }
andrewboyson 12:237a0f75b4d0 52 void PageAddH2(const char* text)
andrewboyson 12:237a0f75b4d0 53 {
andrewboyson 12:237a0f75b4d0 54 HttpAddText("<h2>");
andrewboyson 12:237a0f75b4d0 55 HttpAddText(text);
andrewboyson 12:237a0f75b4d0 56 HttpAddText("</h2>\r\n");
andrewboyson 12:237a0f75b4d0 57 }
andrewboyson 12:237a0f75b4d0 58 void PageAddEnd()
andrewboyson 12:237a0f75b4d0 59 {
andrewboyson 12:237a0f75b4d0 60 HttpAddText("</body>\r\n"
andrewboyson 12:237a0f75b4d0 61 "</html>\r\n");
andrewboyson 12:237a0f75b4d0 62 }
andrewboyson 0:f8998d10763e 63
andrewboyson 0:f8998d10763e 64 void PageAddNavItem(int highlight, char* href, char* title)
andrewboyson 0:f8998d10763e 65 {
andrewboyson 0:f8998d10763e 66 char *p;
andrewboyson 0:f8998d10763e 67 HttpAddText("<li ");
andrewboyson 0:f8998d10763e 68 if (highlight) p = "class='this'";
andrewboyson 0:f8998d10763e 69 else p = " ";
andrewboyson 0:f8998d10763e 70 HttpAddText(p);
andrewboyson 0:f8998d10763e 71 HttpAddText("><a href='");
andrewboyson 0:f8998d10763e 72 HttpAddText(href);
andrewboyson 0:f8998d10763e 73 HttpAddText("'>");
andrewboyson 0:f8998d10763e 74 HttpAddText(title);
andrewboyson 0:f8998d10763e 75 HttpAddText("</a></li>\r\n");
andrewboyson 0:f8998d10763e 76
andrewboyson 0:f8998d10763e 77 }
andrewboyson 30:6a08abbe6301 78 void PageAddNav(int page)
andrewboyson 30:6a08abbe6301 79 {
andrewboyson 30:6a08abbe6301 80 HttpAddText("<nav><ul>\r\n");
andrewboyson 30:6a08abbe6301 81 PageAddNavDerived(page);
andrewboyson 46:1822fdbe6c0c 82 PageAddNavItem(page == CLOCK_PAGE, "/clock", "Clock");
andrewboyson 46:1822fdbe6c0c 83 PageAddNavItem(page == FAULT_PAGE, "/fault", "Fault");
andrewboyson 46:1822fdbe6c0c 84 PageAddNavItem(page == NET_PAGE, "/net", "Net");
andrewboyson 46:1822fdbe6c0c 85 PageAddNavItem(page == LOG_PAGE, "/log", "Log");
andrewboyson 46:1822fdbe6c0c 86 PageAddNavItem(page == TRACE_PAGE, "/trace", "Trace");
andrewboyson 46:1822fdbe6c0c 87 PageAddNavItem(page == FIRMWARE_PAGE, "/firmware", "Firmware");
andrewboyson 30:6a08abbe6301 88 HttpAddText("</ul></nav>\r\n");
andrewboyson 30:6a08abbe6301 89 }
andrewboyson 0:f8998d10763e 90
andrewboyson 77:4689596a2f3f 91 void PageAddLabelledPrefixSuffix(char* label, char* prefix, char* text, char* suffix)
andrewboyson 0:f8998d10763e 92 {
andrewboyson 77:4689596a2f3f 93 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 94 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 95 HttpAddF (" <div>%s%s%s</div>\r\n", prefix, text, suffix);
andrewboyson 77:4689596a2f3f 96 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 97 }
andrewboyson 77:4689596a2f3f 98 void PageAddLabelledText(char* label, char* text)
andrewboyson 77:4689596a2f3f 99 {
andrewboyson 77:4689596a2f3f 100 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 101 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 102 HttpAddF (" <div>%s</div>\r\n", text);
andrewboyson 0:f8998d10763e 103 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 104 }
andrewboyson 0:f8998d10763e 105
andrewboyson 77:4689596a2f3f 106 void PageAddLabelledMac(char* label, char* mac)
andrewboyson 0:f8998d10763e 107 {
andrewboyson 77:4689596a2f3f 108 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 109 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 110 HttpAddText(" <div>"); MacHttp(mac); HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 111 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 112 }
andrewboyson 77:4689596a2f3f 113
andrewboyson 77:4689596a2f3f 114 void PageAddLabelledIp4(char* label, uint32_t ip)
andrewboyson 77:4689596a2f3f 115 {
andrewboyson 77:4689596a2f3f 116 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 117 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 118 HttpAddText(" <div>"); Ip4AddressHttp(ip); HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 119 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 120 }
andrewboyson 0:f8998d10763e 121
andrewboyson 77:4689596a2f3f 122 void PageAddLabelledIp6(char* label, char* ip)
andrewboyson 0:f8998d10763e 123 {
andrewboyson 77:4689596a2f3f 124 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 125 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 126 HttpAddText(" <div>"); Ip6AddressHttp(ip); HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 127 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 128 }
andrewboyson 77:4689596a2f3f 129 void PageAddLabelledOnOff(char* label, bool value)
andrewboyson 0:f8998d10763e 130 {
andrewboyson 77:4689596a2f3f 131 if (value) PageAddLabelledText(label, "On");
andrewboyson 77:4689596a2f3f 132 else PageAddLabelledText(label, "Off");
andrewboyson 0:f8998d10763e 133 }
andrewboyson 77:4689596a2f3f 134 void PageAddLabelledInt(char* label, int value)
andrewboyson 0:f8998d10763e 135 {
andrewboyson 0:f8998d10763e 136 char text[30];
andrewboyson 11:84121d7b47e9 137 snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant.
andrewboyson 77:4689596a2f3f 138 PageAddLabelledText(label, text);
andrewboyson 0:f8998d10763e 139 }
andrewboyson 77:4689596a2f3f 140 void PageAddInputText(char* label, float inputwidth, char* value, char* action, char* name)
andrewboyson 0:f8998d10763e 141 {
andrewboyson 58:e5ab14ef6ea6 142 HttpAddF ("<form action='%s' method='get'>\r\n", action);
andrewboyson 77:4689596a2f3f 143 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 144 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 145 HttpAddF (" <input type='text' name='%s' style='width:%.1fem;' value='%s'>\r\n", name, inputwidth, value);
andrewboyson 77:4689596a2f3f 146 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 147 HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n");
andrewboyson 58:e5ab14ef6ea6 148 HttpAddF ("</form>\r\n");
andrewboyson 0:f8998d10763e 149
andrewboyson 0:f8998d10763e 150 }
andrewboyson 77:4689596a2f3f 151 void PageAddInputInt(char* label, float inputwidth, int value, char* action, char* name)
andrewboyson 0:f8998d10763e 152 {
andrewboyson 0:f8998d10763e 153 char text[30];
andrewboyson 0:f8998d10763e 154 snprintf(text, sizeof(text), "%d", value);
andrewboyson 77:4689596a2f3f 155 PageAddInputText(label, inputwidth, text, action, name);
andrewboyson 0:f8998d10763e 156 }
andrewboyson 77:4689596a2f3f 157 void PageAddInputButton(char* label, char* value, char* action, char* name)
andrewboyson 0:f8998d10763e 158 {
andrewboyson 77:4689596a2f3f 159 HttpAddF ("<form action='%s' method='get'>\r\n", action);
andrewboyson 77:4689596a2f3f 160 HttpAddF ("<input type='hidden' name='%s'>\r\n", name);
andrewboyson 77:4689596a2f3f 161 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 162 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 163 HttpAddF (" <input type='submit' value='%s'>\r\n", value);
andrewboyson 77:4689596a2f3f 164 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 165 HttpAddText("</form>\r\n");
andrewboyson 0:f8998d10763e 166 }
andrewboyson 77:4689596a2f3f 167 void PageAddAjaxInputToggle(char* label, char* id, char* name)
andrewboyson 0:f8998d10763e 168 {
andrewboyson 77:4689596a2f3f 169 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 170 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 171 HttpAddF (" <div class='toggle' id='%s' dir='ltr' onclick='AjaxRequest(\"%s=1\")'>\r\n", id, name);
andrewboyson 58:e5ab14ef6ea6 172 HttpAddText(" <div class='slot'></div><div class='knob'></div>\r\n");
andrewboyson 58:e5ab14ef6ea6 173 HttpAddText(" </div>\r\n");
andrewboyson 0:f8998d10763e 174 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 175 }
andrewboyson 77:4689596a2f3f 176 void PageAddAjaxLed(char* label, char* id)
andrewboyson 58:e5ab14ef6ea6 177 {
andrewboyson 77:4689596a2f3f 178 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 179 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 180 HttpAddF (" <div class='led' id='%s' dir='ltr'></div>\r\n", id);
andrewboyson 0:f8998d10763e 181 HttpAddText("</div>\r\n");
andrewboyson 58:e5ab14ef6ea6 182 }
andrewboyson 77:4689596a2f3f 183 void PageAddAjaxInput(char* label, float inputwidth, char* id, char* name)
andrewboyson 58:e5ab14ef6ea6 184 {
andrewboyson 77:4689596a2f3f 185 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 186 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 187 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>\r\n", inputwidth, id, name);
andrewboyson 0:f8998d10763e 188 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 189 }
andrewboyson 84:4ed751de613e 190 void PageAddAjaxInputSuffix(char* label, float inputwidth, char* id, char* name, char* suffix)
andrewboyson 84:4ed751de613e 191 {
andrewboyson 84:4ed751de613e 192 HttpAddText("<div class='line'>\r\n");
andrewboyson 84:4ed751de613e 193 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 84:4ed751de613e 194 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>%s\r\n", inputwidth, id, name, suffix);
andrewboyson 84:4ed751de613e 195 HttpAddText("</div>\r\n");
andrewboyson 84:4ed751de613e 196 }
andrewboyson 77:4689596a2f3f 197 void PageAddAjaxLabelled(char* label, char* id)
andrewboyson 57:8fa31ff4e773 198 {
andrewboyson 77:4689596a2f3f 199 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 200 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 201 HttpAddF (" <div id='%s'></div>\r\n", id);
andrewboyson 77:4689596a2f3f 202 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 203 }
andrewboyson 77:4689596a2f3f 204 void PageAddAjaxLabelledSuffix(char* label, char* id, char* suffix)
andrewboyson 77:4689596a2f3f 205 {
andrewboyson 77:4689596a2f3f 206 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 207 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 208 HttpAddF (" <div><span id='%s'></span>%s</div>\r\n", id, suffix);
andrewboyson 57:8fa31ff4e773 209 HttpAddText("</div>\r\n");
andrewboyson 57:8fa31ff4e773 210 }
andrewboyson 0:f8998d10763e 211
andrewboyson 0:f8998d10763e 212