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@58:e5ab14ef6ea6, 2019-03-08 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Mar 08 16:07:56 2019 +0000
- Revision:
- 58:e5ab14ef6ea6
- Parent:
- 57:8fa31ff4e773
- Child:
- 59:309e78f243dd
Tidied a load of routines in page module
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 | 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 | 0:f8998d10763e | 91 | void PageAddLabelledValue(char* label, float labelwidth, char* value) |
| andrewboyson | 0:f8998d10763e | 92 | { |
| andrewboyson | 0:f8998d10763e | 93 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 94 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 95 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 96 | HttpAddText(value); |
| andrewboyson | 0:f8998d10763e | 97 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 98 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 99 | } |
| andrewboyson | 0:f8998d10763e | 100 | |
| andrewboyson | 0:f8998d10763e | 101 | void PageAddLabelledMac(char* label, float labelwidth, char* mac) |
| andrewboyson | 0:f8998d10763e | 102 | { |
| andrewboyson | 0:f8998d10763e | 103 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 104 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 105 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 106 | MacHttp (mac); |
| andrewboyson | 0:f8998d10763e | 107 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 108 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 109 | } |
| andrewboyson | 0:f8998d10763e | 110 | |
| andrewboyson | 0:f8998d10763e | 111 | void PageAddLabelledIp4(char* label, float labelwidth, uint32_t ip) |
| andrewboyson | 0:f8998d10763e | 112 | { |
| andrewboyson | 0:f8998d10763e | 113 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 114 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 115 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 116 | Ip4AddressHttp (ip); |
| andrewboyson | 0:f8998d10763e | 117 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 118 | HttpAddText ("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 119 | } |
| andrewboyson | 0:f8998d10763e | 120 | |
| andrewboyson | 0:f8998d10763e | 121 | void PageAddLabelledIp6(char* label, float labelwidth, char* ip) |
| andrewboyson | 0:f8998d10763e | 122 | { |
| andrewboyson | 0:f8998d10763e | 123 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 124 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 125 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 126 | Ip6AddressHttp (ip); |
| andrewboyson | 0:f8998d10763e | 127 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 128 | HttpAddText ("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 129 | } |
| andrewboyson | 0:f8998d10763e | 130 | void PageAddLabelledOnOff(char* label, float labelwidth, int value) |
| andrewboyson | 0:f8998d10763e | 131 | { |
| andrewboyson | 0:f8998d10763e | 132 | if (value) PageAddLabelledValue(label, labelwidth, "On"); |
| andrewboyson | 0:f8998d10763e | 133 | else PageAddLabelledValue(label, labelwidth, "Off"); |
| andrewboyson | 0:f8998d10763e | 134 | } |
| andrewboyson | 0:f8998d10763e | 135 | void PageAddLabelledInt(char* label, float labelwidth, int value) |
| andrewboyson | 0:f8998d10763e | 136 | { |
| andrewboyson | 0:f8998d10763e | 137 | char text[30]; |
| andrewboyson | 11:84121d7b47e9 | 138 | snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant. |
| andrewboyson | 0:f8998d10763e | 139 | PageAddLabelledValue(label, labelwidth, text); |
| andrewboyson | 0:f8998d10763e | 140 | } |
| andrewboyson | 58:e5ab14ef6ea6 | 141 | void PageAddInputText(float labelwidth, char* label, float inputwidth, char* value, char* action, char* name) |
| andrewboyson | 0:f8998d10763e | 142 | { |
| andrewboyson | 58:e5ab14ef6ea6 | 143 | HttpAddF ("<form action='%s' method='get'>\r\n", action); |
| andrewboyson | 58:e5ab14ef6ea6 | 144 | HttpAddF (" <div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label); |
| andrewboyson | 58:e5ab14ef6ea6 | 145 | HttpAddF (" <input type='text' name='%s' style='width:%.1fem;' value='%s'>\r\n", name, inputwidth, value); |
| andrewboyson | 58:e5ab14ef6ea6 | 146 | HttpAddText(" <input type='submit' value='Set' style='display:none;'>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 147 | HttpAddF ("</form>\r\n"); |
| andrewboyson | 0:f8998d10763e | 148 | |
| andrewboyson | 0:f8998d10763e | 149 | } |
| andrewboyson | 58:e5ab14ef6ea6 | 150 | void PageAddInputInt(float labelwidth, char* label, float inputwidth, int value, char* action, char* name) |
| andrewboyson | 0:f8998d10763e | 151 | { |
| andrewboyson | 0:f8998d10763e | 152 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 153 | snprintf(text, sizeof(text), "%d", value); |
| andrewboyson | 58:e5ab14ef6ea6 | 154 | PageAddInputText(labelwidth, label, inputwidth, text, action, name); |
| andrewboyson | 0:f8998d10763e | 155 | } |
| andrewboyson | 58:e5ab14ef6ea6 | 156 | void PageAddInputButton(float labelwidth, char* label, char* value, char* action, char* name) |
| andrewboyson | 0:f8998d10763e | 157 | { |
| andrewboyson | 58:e5ab14ef6ea6 | 158 | HttpAddF("<form action='%s' method='get'>\r\n", action); |
| andrewboyson | 58:e5ab14ef6ea6 | 159 | HttpAddF("<input type='hidden' name='%s'>\r\n", name); |
| andrewboyson | 58:e5ab14ef6ea6 | 160 | if (labelwidth < 0.1) HttpAddF("%s\r\n", label); |
| andrewboyson | 58:e5ab14ef6ea6 | 161 | else HttpAddF("<div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label); |
| andrewboyson | 58:e5ab14ef6ea6 | 162 | HttpAddF("<input type='submit' value='%s'>\r\n", value); |
| andrewboyson | 0:f8998d10763e | 163 | HttpAddText("</form>\r\n"); |
| andrewboyson | 0:f8998d10763e | 164 | } |
| andrewboyson | 0:f8998d10763e | 165 | void PageAddTm(struct tm* ptm) |
| andrewboyson | 0:f8998d10763e | 166 | { |
| andrewboyson | 0:f8998d10763e | 167 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 168 | snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday); |
| andrewboyson | 0:f8998d10763e | 169 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 170 | switch(ptm->tm_wday) |
| andrewboyson | 0:f8998d10763e | 171 | { |
| andrewboyson | 0:f8998d10763e | 172 | case 0: HttpAddText("Sun"); break; |
| andrewboyson | 0:f8998d10763e | 173 | case 1: HttpAddText("Mon"); break; |
| andrewboyson | 0:f8998d10763e | 174 | case 2: HttpAddText("Tue"); break; |
| andrewboyson | 0:f8998d10763e | 175 | case 3: HttpAddText("Wed"); break; |
| andrewboyson | 0:f8998d10763e | 176 | case 4: HttpAddText("Thu"); break; |
| andrewboyson | 0:f8998d10763e | 177 | case 5: HttpAddText("Fri"); break; |
| andrewboyson | 0:f8998d10763e | 178 | case 6: HttpAddText("Sat"); break; |
| andrewboyson | 0:f8998d10763e | 179 | default: HttpAddText("???"); break; |
| andrewboyson | 0:f8998d10763e | 180 | } |
| andrewboyson | 0:f8998d10763e | 181 | snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| andrewboyson | 0:f8998d10763e | 182 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 183 | if (ptm->tm_isdst > 0) HttpAddText(" BST"); |
| andrewboyson | 0:f8998d10763e | 184 | else if (ptm->tm_isdst == 0) HttpAddText(" GMT"); |
| andrewboyson | 0:f8998d10763e | 185 | else HttpAddText(" UTC"); |
| andrewboyson | 0:f8998d10763e | 186 | } |
| andrewboyson | 58:e5ab14ef6ea6 | 187 | void PageAddAjaxInputToggle(float labelwidth, char* label, char* id, char* name) |
| andrewboyson | 0:f8998d10763e | 188 | { |
| andrewboyson | 58:e5ab14ef6ea6 | 189 | HttpAddText("<div>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 190 | HttpAddF (" <div style='display: inline-block; width:%.1fem;'>%s</div>\r\n", labelwidth, label); |
| andrewboyson | 58:e5ab14ef6ea6 | 191 | HttpAddF (" <div class='toggle' id='%s' dir='ltr' onclick='AjaxRequest(\"%s=1\")'>\r\n", id, name); |
| andrewboyson | 58:e5ab14ef6ea6 | 192 | HttpAddText(" <div class='slot'></div><div class='knob'></div>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 193 | HttpAddText(" </div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 194 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 195 | } |
| andrewboyson | 58:e5ab14ef6ea6 | 196 | void PageAddAjaxLed(float labelwidth, char* label, char* id) |
| andrewboyson | 58:e5ab14ef6ea6 | 197 | { |
| andrewboyson | 58:e5ab14ef6ea6 | 198 | HttpAddText("<div>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 199 | HttpAddF (" <div style='display:inline-block; width:%.1fem;'>%s</div>\r\n", labelwidth, label); |
| andrewboyson | 58:e5ab14ef6ea6 | 200 | HttpAddF (" <div class='led' id='%s' dir='ltr'></div>\r\n", id); |
| andrewboyson | 0:f8998d10763e | 201 | HttpAddText("</div>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 202 | } |
| andrewboyson | 58:e5ab14ef6ea6 | 203 | void PageAddAjaxInput(float labelwidth, char* label, float inputwidth, char* id, char* name) |
| andrewboyson | 58:e5ab14ef6ea6 | 204 | { |
| andrewboyson | 58:e5ab14ef6ea6 | 205 | HttpAddText("<div>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 206 | HttpAddF (" <div style='display: inline-block; width:%.1fem;'>%s</div>\r\n", labelwidth, label); |
| andrewboyson | 58:e5ab14ef6ea6 | 207 | HttpAddF (" <input type='text' id='%s' style='width:%.1fem;' onchange='AjaxRequest(\"%s=\" + this.value)'>\r\n", id, inputwidth, name); |
| andrewboyson | 0:f8998d10763e | 208 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 209 | } |
| andrewboyson | 57:8fa31ff4e773 | 210 | void PageAddAjaxLabelled(float labelwidth, char* label, char* id, char* suffix) |
| andrewboyson | 57:8fa31ff4e773 | 211 | { |
| andrewboyson | 58:e5ab14ef6ea6 | 212 | HttpAddText("<div>\r\n"); |
| andrewboyson | 58:e5ab14ef6ea6 | 213 | HttpAddF (" <div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label); |
| andrewboyson | 58:e5ab14ef6ea6 | 214 | HttpAddF (" <span id='%s'></span>%s\r\n", id, suffix); |
| andrewboyson | 57:8fa31ff4e773 | 215 | HttpAddText("</div>\r\n"); |
| andrewboyson | 57:8fa31ff4e773 | 216 | } |
| andrewboyson | 0:f8998d10763e | 217 | |
| andrewboyson | 0:f8998d10763e | 218 |