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