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.c@0:f8998d10763e, 2018-01-28 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Jan 28 11:44:11 2018 +0000
- Revision:
- 0:f8998d10763e
- Child:
- 2:14de8c14afd9
Separated the things common to all my sites: log page; net page; css etc.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 0:f8998d10763e | 1 | #include <stdio.h> |
| andrewboyson | 0:f8998d10763e | 2 | #include "http.h" |
| andrewboyson | 0:f8998d10763e | 3 | #include "page.h" |
| andrewboyson | 0:f8998d10763e | 4 | #include "mac.h" |
| andrewboyson | 0:f8998d10763e | 5 | #include "ip4addr.h" |
| andrewboyson | 0:f8998d10763e | 6 | #include "ip6addr.h" |
| andrewboyson | 0:f8998d10763e | 7 | |
| andrewboyson | 0:f8998d10763e | 8 | void PageAddHeader(const char* title, const char* style, const char* script) |
| andrewboyson | 0:f8998d10763e | 9 | { |
| andrewboyson | 0:f8998d10763e | 10 | HttpAddText("<!DOCTYPE html>\r\n" |
| andrewboyson | 0:f8998d10763e | 11 | "<html>\r\n" |
| andrewboyson | 0:f8998d10763e | 12 | "<head>\r\n"); |
| andrewboyson | 0:f8998d10763e | 13 | if (title) |
| andrewboyson | 0:f8998d10763e | 14 | { |
| andrewboyson | 0:f8998d10763e | 15 | HttpAddText(" <title>"); |
| andrewboyson | 0:f8998d10763e | 16 | HttpAddText(title); |
| andrewboyson | 0:f8998d10763e | 17 | HttpAddText("</title>\r\n"); |
| andrewboyson | 0:f8998d10763e | 18 | } |
| andrewboyson | 0:f8998d10763e | 19 | HttpAddText(" <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n"); |
| andrewboyson | 0:f8998d10763e | 20 | if (style) |
| andrewboyson | 0:f8998d10763e | 21 | { |
| andrewboyson | 0:f8998d10763e | 22 | HttpAddText(" <link rel='stylesheet' href='/"); |
| andrewboyson | 0:f8998d10763e | 23 | HttpAddText(style); |
| andrewboyson | 0:f8998d10763e | 24 | HttpAddText("' type='text/css'/>\r\n"); |
| andrewboyson | 0:f8998d10763e | 25 | } |
| andrewboyson | 0:f8998d10763e | 26 | if (script) |
| andrewboyson | 0:f8998d10763e | 27 | { |
| andrewboyson | 0:f8998d10763e | 28 | HttpAddText(" <script src='/"); |
| andrewboyson | 0:f8998d10763e | 29 | HttpAddText(script); |
| andrewboyson | 0:f8998d10763e | 30 | HttpAddText("' type='text/javascript'></script>\r\n"); |
| andrewboyson | 0:f8998d10763e | 31 | } |
| andrewboyson | 0:f8998d10763e | 32 | HttpAddText(" <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n" |
| andrewboyson | 0:f8998d10763e | 33 | " <link rel='icon' href='/favicon.ico' type='image/x-icon'/>\r\n" |
| andrewboyson | 0:f8998d10763e | 34 | "</head>\r\n" |
| andrewboyson | 0:f8998d10763e | 35 | "<body>\r\n"); |
| andrewboyson | 0:f8998d10763e | 36 | |
| andrewboyson | 0:f8998d10763e | 37 | } |
| andrewboyson | 0:f8998d10763e | 38 | |
| andrewboyson | 0:f8998d10763e | 39 | void PageAddNavItem(int highlight, char* href, char* title) |
| andrewboyson | 0:f8998d10763e | 40 | { |
| andrewboyson | 0:f8998d10763e | 41 | char *p; |
| andrewboyson | 0:f8998d10763e | 42 | HttpAddText("<li "); |
| andrewboyson | 0:f8998d10763e | 43 | if (highlight) p = "class='this'"; |
| andrewboyson | 0:f8998d10763e | 44 | else p = " "; |
| andrewboyson | 0:f8998d10763e | 45 | HttpAddText(p); |
| andrewboyson | 0:f8998d10763e | 46 | HttpAddText("><a href='"); |
| andrewboyson | 0:f8998d10763e | 47 | HttpAddText(href); |
| andrewboyson | 0:f8998d10763e | 48 | HttpAddText("'>"); |
| andrewboyson | 0:f8998d10763e | 49 | HttpAddText(title); |
| andrewboyson | 0:f8998d10763e | 50 | HttpAddText("</a></li>\r\n"); |
| andrewboyson | 0:f8998d10763e | 51 | |
| andrewboyson | 0:f8998d10763e | 52 | } |
| andrewboyson | 0:f8998d10763e | 53 | |
| andrewboyson | 0:f8998d10763e | 54 | void PageAddLabelledValue(char* label, float labelwidth, char* value) |
| andrewboyson | 0:f8998d10763e | 55 | { |
| andrewboyson | 0:f8998d10763e | 56 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 57 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 58 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 59 | HttpAddText(value); |
| andrewboyson | 0:f8998d10763e | 60 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 61 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 62 | } |
| andrewboyson | 0:f8998d10763e | 63 | |
| andrewboyson | 0:f8998d10763e | 64 | void PageAddLabelledMac(char* label, float labelwidth, char* mac) |
| andrewboyson | 0:f8998d10763e | 65 | { |
| andrewboyson | 0:f8998d10763e | 66 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 67 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 68 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 69 | MacHttp (mac); |
| andrewboyson | 0:f8998d10763e | 70 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 71 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 72 | } |
| andrewboyson | 0:f8998d10763e | 73 | |
| andrewboyson | 0:f8998d10763e | 74 | void PageAddLabelledIp4(char* label, float labelwidth, uint32_t ip) |
| andrewboyson | 0:f8998d10763e | 75 | { |
| andrewboyson | 0:f8998d10763e | 76 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 77 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 78 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 79 | Ip4AddressHttp (ip); |
| andrewboyson | 0:f8998d10763e | 80 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 81 | HttpAddText ("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 82 | } |
| andrewboyson | 0:f8998d10763e | 83 | |
| andrewboyson | 0:f8998d10763e | 84 | void PageAddLabelledIp6(char* label, float labelwidth, char* ip) |
| andrewboyson | 0:f8998d10763e | 85 | { |
| andrewboyson | 0:f8998d10763e | 86 | HttpAddF ("<div style='width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 87 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 88 | HttpAddText("<span style='float:right;'>"); |
| andrewboyson | 0:f8998d10763e | 89 | Ip6AddressHttp (ip); |
| andrewboyson | 0:f8998d10763e | 90 | HttpAddText("</span>"); |
| andrewboyson | 0:f8998d10763e | 91 | HttpAddText ("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 92 | } |
| andrewboyson | 0:f8998d10763e | 93 | void PageAddLabelledName(char* label, float labelwidth, char* name, char* suffix) |
| andrewboyson | 0:f8998d10763e | 94 | { |
| andrewboyson | 0:f8998d10763e | 95 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 96 | HttpAddText("<div>"); |
| andrewboyson | 0:f8998d10763e | 97 | snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 98 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 99 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 100 | HttpAddText("</div><span id='"); |
| andrewboyson | 0:f8998d10763e | 101 | HttpAddText(name); |
| andrewboyson | 0:f8998d10763e | 102 | HttpAddText("'></span>"); |
| andrewboyson | 0:f8998d10763e | 103 | HttpAddText(suffix); |
| andrewboyson | 0:f8998d10763e | 104 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 105 | } |
| andrewboyson | 0:f8998d10763e | 106 | void PageAddLabelledOnOff(char* label, float labelwidth, int value) |
| andrewboyson | 0:f8998d10763e | 107 | { |
| andrewboyson | 0:f8998d10763e | 108 | if (value) PageAddLabelledValue(label, labelwidth, "On"); |
| andrewboyson | 0:f8998d10763e | 109 | else PageAddLabelledValue(label, labelwidth, "Off"); |
| andrewboyson | 0:f8998d10763e | 110 | } |
| andrewboyson | 0:f8998d10763e | 111 | void PageAddLabelledInt(char* label, float labelwidth, int value) |
| andrewboyson | 0:f8998d10763e | 112 | { |
| andrewboyson | 0:f8998d10763e | 113 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 114 | snprintf(text, sizeof(text), "%d", value); |
| andrewboyson | 0:f8998d10763e | 115 | PageAddLabelledValue(label, labelwidth, text); |
| andrewboyson | 0:f8998d10763e | 116 | } |
| andrewboyson | 0:f8998d10763e | 117 | void PageAddTextInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, char* value) |
| andrewboyson | 0:f8998d10763e | 118 | { |
| andrewboyson | 0:f8998d10763e | 119 | HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 120 | |
| andrewboyson | 0:f8998d10763e | 121 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 122 | if (width < 0.01) |
| andrewboyson | 0:f8998d10763e | 123 | { |
| andrewboyson | 0:f8998d10763e | 124 | HttpAddText("<div>"); |
| andrewboyson | 0:f8998d10763e | 125 | } |
| andrewboyson | 0:f8998d10763e | 126 | else |
| andrewboyson | 0:f8998d10763e | 127 | { |
| andrewboyson | 0:f8998d10763e | 128 | snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", width); |
| andrewboyson | 0:f8998d10763e | 129 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 130 | } |
| andrewboyson | 0:f8998d10763e | 131 | snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>", labelwidth, label); |
| andrewboyson | 0:f8998d10763e | 132 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 133 | snprintf(text, sizeof(text), "<input type='text' name='%s' style='width:%.1fem;' value='%s'>", name, inputwidth, value); |
| andrewboyson | 0:f8998d10763e | 134 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 135 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 136 | HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n</form>\r\n"); |
| andrewboyson | 0:f8998d10763e | 137 | |
| andrewboyson | 0:f8998d10763e | 138 | } |
| andrewboyson | 0:f8998d10763e | 139 | void PageAddIntInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, int value) |
| andrewboyson | 0:f8998d10763e | 140 | { |
| andrewboyson | 0:f8998d10763e | 141 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 142 | snprintf(text, sizeof(text), "%d", value); |
| andrewboyson | 0:f8998d10763e | 143 | PageAddTextInput(action, width, label, labelwidth, name, inputwidth, text); |
| andrewboyson | 0:f8998d10763e | 144 | } |
| andrewboyson | 0:f8998d10763e | 145 | void PageAddCheckInput(char* action, char* label, char* name, char* button) |
| andrewboyson | 0:f8998d10763e | 146 | { |
| andrewboyson | 0:f8998d10763e | 147 | HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 148 | HttpAddText("<input type='hidden' name='"); HttpAddText(name); HttpAddText("'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 149 | HttpAddText(label); HttpAddText("\r\n"); |
| andrewboyson | 0:f8998d10763e | 150 | HttpAddText("<input type='submit' value='"); HttpAddText(button); HttpAddText("'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 151 | HttpAddText("</form>\r\n"); |
| andrewboyson | 0:f8998d10763e | 152 | } |
| andrewboyson | 0:f8998d10763e | 153 | void PageAddTm(struct tm* ptm) |
| andrewboyson | 0:f8998d10763e | 154 | { |
| andrewboyson | 0:f8998d10763e | 155 | char text[30]; |
| andrewboyson | 0:f8998d10763e | 156 | snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday); |
| andrewboyson | 0:f8998d10763e | 157 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 158 | switch(ptm->tm_wday) |
| andrewboyson | 0:f8998d10763e | 159 | { |
| andrewboyson | 0:f8998d10763e | 160 | case 0: HttpAddText("Sun"); break; |
| andrewboyson | 0:f8998d10763e | 161 | case 1: HttpAddText("Mon"); break; |
| andrewboyson | 0:f8998d10763e | 162 | case 2: HttpAddText("Tue"); break; |
| andrewboyson | 0:f8998d10763e | 163 | case 3: HttpAddText("Wed"); break; |
| andrewboyson | 0:f8998d10763e | 164 | case 4: HttpAddText("Thu"); break; |
| andrewboyson | 0:f8998d10763e | 165 | case 5: HttpAddText("Fri"); break; |
| andrewboyson | 0:f8998d10763e | 166 | case 6: HttpAddText("Sat"); break; |
| andrewboyson | 0:f8998d10763e | 167 | default: HttpAddText("???"); break; |
| andrewboyson | 0:f8998d10763e | 168 | } |
| andrewboyson | 0:f8998d10763e | 169 | snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| andrewboyson | 0:f8998d10763e | 170 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 171 | if (ptm->tm_isdst > 0) HttpAddText(" BST"); |
| andrewboyson | 0:f8998d10763e | 172 | else if (ptm->tm_isdst == 0) HttpAddText(" GMT"); |
| andrewboyson | 0:f8998d10763e | 173 | else HttpAddText(" UTC"); |
| andrewboyson | 0:f8998d10763e | 174 | } |
| andrewboyson | 0:f8998d10763e | 175 | void PageAddAjaxToggle(float labelwidth, char* label, char* id, char* request) |
| andrewboyson | 0:f8998d10763e | 176 | { |
| andrewboyson | 0:f8998d10763e | 177 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 178 | |
| andrewboyson | 0:f8998d10763e | 179 | snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 180 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 181 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 182 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 183 | |
| andrewboyson | 0:f8998d10763e | 184 | HttpAddText("<div class='toggle' id='"); |
| andrewboyson | 0:f8998d10763e | 185 | HttpAddText(id); |
| andrewboyson | 0:f8998d10763e | 186 | HttpAddText("' dir='ltr' onclick='AjaxRequest(\""); |
| andrewboyson | 0:f8998d10763e | 187 | HttpAddText(request); |
| andrewboyson | 0:f8998d10763e | 188 | HttpAddText("=1\")'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 189 | |
| andrewboyson | 0:f8998d10763e | 190 | HttpAddText("<div class='slot'></div><div class='knob'></div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 191 | |
| andrewboyson | 0:f8998d10763e | 192 | HttpAddText("</div></div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 193 | } |
| andrewboyson | 0:f8998d10763e | 194 | void PageAddAjaxHex(float labelwidth, char* label, float inputwidth, char* id, char* request) |
| andrewboyson | 0:f8998d10763e | 195 | { |
| andrewboyson | 0:f8998d10763e | 196 | char text[100]; |
| andrewboyson | 0:f8998d10763e | 197 | |
| andrewboyson | 0:f8998d10763e | 198 | snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth); |
| andrewboyson | 0:f8998d10763e | 199 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 200 | HttpAddText(label); |
| andrewboyson | 0:f8998d10763e | 201 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 202 | |
| andrewboyson | 0:f8998d10763e | 203 | HttpAddText("<input type='text' id='"); |
| andrewboyson | 0:f8998d10763e | 204 | HttpAddText(id); |
| andrewboyson | 0:f8998d10763e | 205 | HttpAddText("' style='width:"); |
| andrewboyson | 0:f8998d10763e | 206 | sprintf(text, "%.1fem;", inputwidth); |
| andrewboyson | 0:f8998d10763e | 207 | HttpAddText(text); |
| andrewboyson | 0:f8998d10763e | 208 | HttpAddText("' onchange='AjaxRequest(\""); |
| andrewboyson | 0:f8998d10763e | 209 | HttpAddText(request); |
| andrewboyson | 0:f8998d10763e | 210 | HttpAddText("=\" + this.value)'>\r\n"); |
| andrewboyson | 0:f8998d10763e | 211 | |
| andrewboyson | 0:f8998d10763e | 212 | HttpAddText("</div>\r\n"); |
| andrewboyson | 0:f8998d10763e | 213 | } |
| andrewboyson | 0:f8998d10763e | 214 | |
| andrewboyson | 0:f8998d10763e | 215 |