Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun Apr 28 08:37:44 2019 +0000
Revision:
105:43ef124233cd
Parent:
103:91194cc19bbb
Removed Server name

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