Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sat Apr 27 09:25:28 2019 +0000
Revision:
103:91194cc19bbb
Parent:
101:07234e772d31
Child:
105:43ef124233cd
Renamed everything from Http to Web

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 99:5aa33c306167 85 #ifdef INCLUDE_1_WIRE
andrewboyson 99:5aa33c306167 86 PageAddNavItem(page == ONE_WIRE_PAGE, "/1wire", "1-Wire" );
andrewboyson 99:5aa33c306167 87 #endif
andrewboyson 86:f3c9beec4ee7 88 PageAddNavItem(page == CLOCK_PAGE, "/clock", "Clock" );
andrewboyson 86:f3c9beec4ee7 89 PageAddNavItem(page == FAULT_PAGE, "/fault", "Fault" );
andrewboyson 86:f3c9beec4ee7 90 PageAddNavItem(page == NET_PAGE, "/net", "Net" );
andrewboyson 86:f3c9beec4ee7 91 PageAddNavItem(page == NET4_PAGE, "/net4", "Net IPv4" );
andrewboyson 86:f3c9beec4ee7 92 PageAddNavItem(page == NET6_PAGE, "/net6", "Net IPv6" );
andrewboyson 86:f3c9beec4ee7 93 PageAddNavItem(page == TRACE_PAGE, "/trace", "Net Trace");
andrewboyson 86:f3c9beec4ee7 94 PageAddNavItem(page == LOG_PAGE, "/log", "Log" );
andrewboyson 86:f3c9beec4ee7 95 PageAddNavItem(page == FIRMWARE_PAGE, "/firmware", "Firmware" );
andrewboyson 30:6a08abbe6301 96 HttpAddText("</ul></nav>\r\n");
andrewboyson 30:6a08abbe6301 97 }
andrewboyson 0:f8998d10763e 98
andrewboyson 77:4689596a2f3f 99 void PageAddLabelledPrefixSuffix(char* label, char* prefix, char* text, char* suffix)
andrewboyson 0:f8998d10763e 100 {
andrewboyson 77:4689596a2f3f 101 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 102 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 103 HttpAddF (" <div>%s%s%s</div>\r\n", prefix, text, suffix);
andrewboyson 77:4689596a2f3f 104 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 105 }
andrewboyson 77:4689596a2f3f 106 void PageAddLabelledText(char* label, char* text)
andrewboyson 77:4689596a2f3f 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 HttpAddF (" <div>%s</div>\r\n", text);
andrewboyson 0:f8998d10763e 111 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 112 }
andrewboyson 0:f8998d10763e 113
andrewboyson 77:4689596a2f3f 114 void PageAddLabelledMac(char* label, char* mac)
andrewboyson 0:f8998d10763e 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>"); MacHttp(mac); HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 119 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 120 }
andrewboyson 77:4689596a2f3f 121
andrewboyson 77:4689596a2f3f 122 void PageAddLabelledIp4(char* label, uint32_t ip)
andrewboyson 77:4689596a2f3f 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>"); Ip4AddressHttp(ip); HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 127 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 128 }
andrewboyson 0:f8998d10763e 129
andrewboyson 77:4689596a2f3f 130 void PageAddLabelledIp6(char* label, char* ip)
andrewboyson 0:f8998d10763e 131 {
andrewboyson 77:4689596a2f3f 132 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 133 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 134 HttpAddText(" <div>"); Ip6AddressHttp(ip); HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 135 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 136 }
andrewboyson 77:4689596a2f3f 137 void PageAddLabelledOnOff(char* label, bool value)
andrewboyson 0:f8998d10763e 138 {
andrewboyson 77:4689596a2f3f 139 if (value) PageAddLabelledText(label, "On");
andrewboyson 77:4689596a2f3f 140 else PageAddLabelledText(label, "Off");
andrewboyson 0:f8998d10763e 141 }
andrewboyson 77:4689596a2f3f 142 void PageAddLabelledInt(char* label, int value)
andrewboyson 0:f8998d10763e 143 {
andrewboyson 0:f8998d10763e 144 char text[30];
andrewboyson 11:84121d7b47e9 145 snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant.
andrewboyson 77:4689596a2f3f 146 PageAddLabelledText(label, text);
andrewboyson 0:f8998d10763e 147 }
andrewboyson 77:4689596a2f3f 148 void PageAddInputText(char* label, float inputwidth, char* value, char* action, char* name)
andrewboyson 0:f8998d10763e 149 {
andrewboyson 58:e5ab14ef6ea6 150 HttpAddF ("<form action='%s' method='get'>\r\n", action);
andrewboyson 77:4689596a2f3f 151 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 152 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 153 HttpAddF (" <input type='text' name='%s' style='width:%.1fem;' value='%s'>\r\n", name, inputwidth, value);
andrewboyson 77:4689596a2f3f 154 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 155 HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n");
andrewboyson 58:e5ab14ef6ea6 156 HttpAddF ("</form>\r\n");
andrewboyson 0:f8998d10763e 157
andrewboyson 0:f8998d10763e 158 }
andrewboyson 77:4689596a2f3f 159 void PageAddInputInt(char* label, float inputwidth, int value, char* action, char* name)
andrewboyson 0:f8998d10763e 160 {
andrewboyson 0:f8998d10763e 161 char text[30];
andrewboyson 0:f8998d10763e 162 snprintf(text, sizeof(text), "%d", value);
andrewboyson 77:4689596a2f3f 163 PageAddInputText(label, inputwidth, text, action, name);
andrewboyson 0:f8998d10763e 164 }
andrewboyson 77:4689596a2f3f 165 void PageAddInputButton(char* label, char* value, char* action, char* name)
andrewboyson 0:f8998d10763e 166 {
andrewboyson 77:4689596a2f3f 167 HttpAddF ("<form action='%s' method='get'>\r\n", action);
andrewboyson 77:4689596a2f3f 168 HttpAddF ("<input type='hidden' name='%s'>\r\n", name);
andrewboyson 77:4689596a2f3f 169 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 170 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 171 HttpAddF (" <input type='submit' value='%s'>\r\n", value);
andrewboyson 77:4689596a2f3f 172 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 173 HttpAddText("</form>\r\n");
andrewboyson 0:f8998d10763e 174 }
andrewboyson 77:4689596a2f3f 175 void PageAddAjaxInputToggle(char* label, char* id, char* name)
andrewboyson 0:f8998d10763e 176 {
andrewboyson 77:4689596a2f3f 177 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 178 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 90:9cc77a16b6c5 179 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 180 HttpAddText(" <div class='slot'></div><div class='knob'></div>\r\n");
andrewboyson 58:e5ab14ef6ea6 181 HttpAddText(" </div>\r\n");
andrewboyson 0:f8998d10763e 182 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 183 }
andrewboyson 77:4689596a2f3f 184 void PageAddAjaxLed(char* label, char* id)
andrewboyson 58:e5ab14ef6ea6 185 {
andrewboyson 77:4689596a2f3f 186 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 187 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 58:e5ab14ef6ea6 188 HttpAddF (" <div class='led' id='%s' dir='ltr'></div>\r\n", id);
andrewboyson 0:f8998d10763e 189 HttpAddText("</div>\r\n");
andrewboyson 58:e5ab14ef6ea6 190 }
andrewboyson 77:4689596a2f3f 191 void PageAddAjaxInput(char* label, float inputwidth, char* id, char* name)
andrewboyson 58:e5ab14ef6ea6 192 {
andrewboyson 77:4689596a2f3f 193 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 194 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 195 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>\r\n", inputwidth, id, name);
andrewboyson 0:f8998d10763e 196 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 197 }
andrewboyson 84:4ed751de613e 198 void PageAddAjaxInputSuffix(char* label, float inputwidth, char* id, char* name, char* suffix)
andrewboyson 84:4ed751de613e 199 {
andrewboyson 84:4ed751de613e 200 HttpAddText("<div class='line'>\r\n");
andrewboyson 84:4ed751de613e 201 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 84:4ed751de613e 202 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>%s\r\n", inputwidth, id, name, suffix);
andrewboyson 84:4ed751de613e 203 HttpAddText("</div>\r\n");
andrewboyson 84:4ed751de613e 204 }
andrewboyson 77:4689596a2f3f 205 void PageAddAjaxLabelled(char* label, char* id)
andrewboyson 57:8fa31ff4e773 206 {
andrewboyson 77:4689596a2f3f 207 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 208 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 209 HttpAddF (" <div id='%s'></div>\r\n", id);
andrewboyson 77:4689596a2f3f 210 HttpAddText("</div>\r\n");
andrewboyson 77:4689596a2f3f 211 }
andrewboyson 77:4689596a2f3f 212 void PageAddAjaxLabelledSuffix(char* label, char* id, char* suffix)
andrewboyson 77:4689596a2f3f 213 {
andrewboyson 77:4689596a2f3f 214 HttpAddText("<div class='line'>\r\n");
andrewboyson 77:4689596a2f3f 215 HttpAddF (" <div>%s</div>\r\n", label);
andrewboyson 77:4689596a2f3f 216 HttpAddF (" <div><span id='%s'></span>%s</div>\r\n", id, suffix);
andrewboyson 57:8fa31ff4e773 217 HttpAddText("</div>\r\n");
andrewboyson 57:8fa31ff4e773 218 }
andrewboyson 99:5aa33c306167 219 void PageAddAjaxInputLabelId(char* labelId, float inputwidth, char* id, char* name)
andrewboyson 99:5aa33c306167 220 {
andrewboyson 99:5aa33c306167 221 HttpAddText("<div class='line'>\r\n");
andrewboyson 99:5aa33c306167 222 HttpAddF (" <div id='%s'></div>\r\n", labelId);
andrewboyson 99:5aa33c306167 223 HttpAddF (" <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxRequest(\"%s=\" + this.value)'>\r\n", inputwidth, id, name);
andrewboyson 99:5aa33c306167 224 HttpAddText("</div>\r\n");
andrewboyson 99:5aa33c306167 225 }
andrewboyson 0:f8998d10763e 226
andrewboyson 0:f8998d10763e 227