Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun Jan 28 13:58:43 2018 +0000
Revision:
2:14de8c14afd9
Parent:
0:f8998d10763e
Child:
11:84121d7b47e9
Deferred title to derived nav

Who changed what in which revision?

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