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@31:b5ca802195a7, 2019-01-18 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Jan 18 11:48:07 2019 +0000
- Revision:
- 31:b5ca802195a7
- Parent:
- 30:6a08abbe6301
- Child:
- 46:1822fdbe6c0c
Tidied name of PageAddAjaxHex to PageAddAjaxInput
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 | 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 | 30:6a08abbe6301 | 82 | PageAddNavItem(page == CLOCK_PAGE, "/clock", "Clock"); |
| andrewboyson | 30:6a08abbe6301 | 83 | PageAddNavItem(page == FAULT_PAGE, "/fault", "Fault"); |
| andrewboyson | 30:6a08abbe6301 | 84 | PageAddNavItem(page == NET_PAGE, "/net", "Net"); |
| andrewboyson | 30:6a08abbe6301 | 85 | PageAddNavItem(page == LOG_PAGE, "/log", "Log"); |
| andrewboyson | 30:6a08abbe6301 | 86 | PageAddNavItem(page == TRACE_PAGE, "/trace", "Trace"); |
| andrewboyson | 30:6a08abbe6301 | 87 | HttpAddText("</ul></nav>\r\n"); |
| andrewboyson | 30:6a08abbe6301 | 88 | } |
| andrewboyson | 0:f8998d10763e | 89 | |
| andrewboyson | 0:f8998d10763e | 90 | void PageAddLabelledValue(char* label, float labelwidth, char* value) |
| andrewboyson | 0:f8998d10763e | 91 | { |
| andrewboyson | 0:f8998d10763e | 92 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 93 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 94 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 95 | HttpAddText(value); |
| andrewboyson | 0:f8998d10763e | 96 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 97 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 98 | } |
| andrewboyson | 0:f8998d10763e | 99 | |
| andrewboyson | 0:f8998d10763e | 100 | void PageAddLabelledMac(char* label, float labelwidth, char* mac) |
| andrewboyson | 0:f8998d10763e | 101 | { |
| andrewboyson | 0:f8998d10763e | 102 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 103 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 104 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 105 | MacHttp (mac); |
| andrewboyson | 0:f8998d10763e | 106 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 107 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 108 | } |
| andrewboyson | 0:f8998d10763e | 109 | |
| andrewboyson | 0:f8998d10763e | 110 | void PageAddLabelledIp4(char* label, float labelwidth, uint32_t ip) |
| andrewboyson | 0:f8998d10763e | 111 | { |
| andrewboyson | 0:f8998d10763e | 112 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 113 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 114 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 115 | Ip4AddressHttp (ip); |
| andrewboyson | 0:f8998d10763e | 116 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 117 | HttpAddText ("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 118 | } |
| andrewboyson | 0:f8998d10763e | 119 | |
| andrewboyson | 0:f8998d10763e | 120 | void PageAddLabelledIp6(char* label, float labelwidth, char* ip) |
| andrewboyson | 0:f8998d10763e | 121 | { |
| andrewboyson | 0:f8998d10763e | 122 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 123 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 124 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 125 | Ip6AddressHttp (ip); |
| andrewboyson | 0:f8998d10763e | 126 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 127 | HttpAddText ("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 128 | } |
| andrewboyson | 0:f8998d10763e | 129 | void PageAddLabelledName(char* label, float labelwidth, char* name, char* suffix) |
| andrewboyson | 0:f8998d10763e | 130 | { |
| andrewboyson | 0:f8998d10763e | 131 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 132 | HttpAddText("<div>"); |
| andrewboyson | 0:f8998d10763e | 133 | snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 134 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 135 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 136 | HttpAddText("</div><span id='"); |
| andrewboyson | 0:f8998d10763e | 137 | HttpAddText(name); |
| andrewboyson | 0:f8998d10763e | 138 | HttpAddText("'></span>"); |
| andrewboyson | 0:f8998d10763e | 139 | HttpAddText(suffix); |
| andrewboyson | 0:f8998d10763e | 140 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 141 | } |
| andrewboyson | 0:f8998d10763e | 142 | void PageAddLabelledOnOff(char* label, float labelwidth, int value) |
| andrewboyson | 0:f8998d10763e | 143 | { |
| andrewboyson | 0:f8998d10763e | 144 | if (value) PageAddLabelledValue(label, labelwidth, "On"); |
| andrewboyson | 0:f8998d10763e | 145 | else PageAddLabelledValue(label, labelwidth, "Off"); |
| andrewboyson | 0:f8998d10763e | 146 | } |
| andrewboyson | 0:f8998d10763e | 147 | void PageAddLabelledInt(char* label, float labelwidth, int value) |
| andrewboyson | 0:f8998d10763e | 148 | { |
| andrewboyson | 0:f8998d10763e | 149 | char text[30]; |
| andrewboyson | 11:84121d7b47e9 | 150 | snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant. |
| andrewboyson | 0:f8998d10763e | 151 | PageAddLabelledValue(label, labelwidth, text); |
| andrewboyson | 0:f8998d10763e | 152 | } |
| andrewboyson | 0:f8998d10763e | 153 | void PageAddTextInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, char* value) |
| andrewboyson | 0:f8998d10763e | 154 | { |
| andrewboyson | 0:f8998d10763e | 155 | HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 156 | |
| andrewboyson | 0:f8998d10763e | 157 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 158 | if (width < 0.01) |
| andrewboyson | 0:f8998d10763e | 159 | { |
| andrewboyson | 0:f8998d10763e | 160 | HttpAddText("<div>"); |
| andrewboyson | 0:f8998d10763e | 161 | } |
| andrewboyson | 0:f8998d10763e | 162 | else |
| andrewboyson | 0:f8998d10763e | 163 | { |
| andrewboyson | 0:f8998d10763e | 164 | snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", width); |
| andrewboyson | 0:f8998d10763e | 165 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 166 | } |
| andrewboyson | 0:f8998d10763e | 167 | snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>", labelwidth, label); |
| andrewboyson | 0:f8998d10763e | 168 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 169 | snprintf(text, sizeof(text), "<input type='text' name='%s' style='width:%.1fem;' value='%s'>", name, inputwidth, value); |
| andrewboyson | 0:f8998d10763e | 170 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 171 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 172 | HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n</form>\r\n"); |
| andrewboyson | 0:f8998d10763e | 173 | |
| andrewboyson | 0:f8998d10763e | 174 | } |
| andrewboyson | 0:f8998d10763e | 175 | void PageAddIntInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, int value) |
| andrewboyson | 0:f8998d10763e | 176 | { |
| andrewboyson | 0:f8998d10763e | 177 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 178 | snprintf(text, sizeof(text), "%d", value); |
| andrewboyson | 0:f8998d10763e | 179 | PageAddTextInput(action, width, label, labelwidth, name, inputwidth, text); |
| andrewboyson | 0:f8998d10763e | 180 | } |
| andrewboyson | 0:f8998d10763e | 181 | void PageAddCheckInput(char* action, char* label, char* name, char* button) |
| andrewboyson | 0:f8998d10763e | 182 | { |
| andrewboyson | 0:f8998d10763e | 183 | HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 184 | HttpAddText("<input type='hidden' name='"); HttpAddText(name); HttpAddText("'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 185 | HttpAddText(label); HttpAddText("\r\n"); |
| andrewboyson | 0:f8998d10763e | 186 | HttpAddText("<input type='submit' value='"); HttpAddText(button); HttpAddText("'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 187 | HttpAddText("</form>\r\n"); |
| andrewboyson | 0:f8998d10763e | 188 | } |
| andrewboyson | 0:f8998d10763e | 189 | void PageAddTm(struct tm* ptm) |
| andrewboyson | 0:f8998d10763e | 190 | { |
| andrewboyson | 0:f8998d10763e | 191 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 192 | snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday); |
| andrewboyson | 0:f8998d10763e | 193 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 194 | switch(ptm->tm_wday) |
| andrewboyson | 0:f8998d10763e | 195 | { |
| andrewboyson | 0:f8998d10763e | 196 | case 0: HttpAddText("Sun"); break; |
| andrewboyson | 0:f8998d10763e | 197 | case 1: HttpAddText("Mon"); break; |
| andrewboyson | 0:f8998d10763e | 198 | case 2: HttpAddText("Tue"); break; |
| andrewboyson | 0:f8998d10763e | 199 | case 3: HttpAddText("Wed"); break; |
| andrewboyson | 0:f8998d10763e | 200 | case 4: HttpAddText("Thu"); break; |
| andrewboyson | 0:f8998d10763e | 201 | case 5: HttpAddText("Fri"); break; |
| andrewboyson | 0:f8998d10763e | 202 | case 6: HttpAddText("Sat"); break; |
| andrewboyson | 0:f8998d10763e | 203 | default: HttpAddText("???"); break; |
| andrewboyson | 0:f8998d10763e | 204 | } |
| andrewboyson | 0:f8998d10763e | 205 | snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| andrewboyson | 0:f8998d10763e | 206 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 207 | if (ptm->tm_isdst > 0) HttpAddText(" BST"); |
| andrewboyson | 0:f8998d10763e | 208 | else if (ptm->tm_isdst == 0) HttpAddText(" GMT"); |
| andrewboyson | 0:f8998d10763e | 209 | else HttpAddText(" UTC"); |
| andrewboyson | 0:f8998d10763e | 210 | } |
| andrewboyson | 0:f8998d10763e | 211 | void PageAddAjaxToggle(float labelwidth, char* label, char* id, char* request) |
| andrewboyson | 0:f8998d10763e | 212 | { |
| andrewboyson | 0:f8998d10763e | 213 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 214 | |
| andrewboyson | 0:f8998d10763e | 215 | snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 216 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 217 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 218 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 219 | |
| andrewboyson | 0:f8998d10763e | 220 | HttpAddText("<div class='toggle' id='"); |
| andrewboyson | 0:f8998d10763e | 221 | HttpAddText(id); |
| andrewboyson | 0:f8998d10763e | 222 | HttpAddText("' dir='ltr' onclick='AjaxRequest(\""); |
| andrewboyson | 0:f8998d10763e | 223 | HttpAddText(request); |
| andrewboyson | 0:f8998d10763e | 224 | HttpAddText("=1\")'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 225 | |
| andrewboyson | 0:f8998d10763e | 226 | HttpAddText("<div class='slot'></div><div class='knob'></div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 227 | |
| andrewboyson | 0:f8998d10763e | 228 | HttpAddText("</div></div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 229 | } |
| andrewboyson | 31:b5ca802195a7 | 230 | void PageAddAjaxInput(float labelwidth, char* label, float inputwidth, char* id, char* request) |
| andrewboyson | 0:f8998d10763e | 231 | { |
| andrewboyson | 0:f8998d10763e | 232 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 233 | |
| andrewboyson | 0:f8998d10763e | 234 | snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 235 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 236 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 237 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 238 | |
| andrewboyson | 0:f8998d10763e | 239 | HttpAddText("<input type='text' id='"); |
| andrewboyson | 0:f8998d10763e | 240 | HttpAddText(id); |
| andrewboyson | 0:f8998d10763e | 241 | HttpAddText("' style='width:"); |
| andrewboyson | 0:f8998d10763e | 242 | sprintf(text, "%.1fem;", inputwidth); |
| andrewboyson | 0:f8998d10763e | 243 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 244 | HttpAddText("' onchange='AjaxRequest(\""); |
| andrewboyson | 0:f8998d10763e | 245 | HttpAddText(request); |
| andrewboyson | 0:f8998d10763e | 246 | HttpAddText("=\" + this.value)'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 247 | |
| andrewboyson | 0:f8998d10763e | 248 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 249 | } |
| andrewboyson | 0:f8998d10763e | 250 | |
| andrewboyson | 0:f8998d10763e | 251 |