Common stuff for all my devices' web server pages: css, login, log, ipv4, ipv6, firmware update, clock, reset info etc.

Dependents:   oldheating gps motorhome heating

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers web-add.c Source File

web-add.c

00001 #include <stdio.h>
00002 
00003 #include "http.h"
00004 #include "web-nav-base.h"
00005 #include "web-nav-this.h"
00006 #include "web-site-name.h"
00007 #include "mac.h"
00008 #include "ip4addr.h"
00009 #include "ip6addr.h"
00010 
00011 void WebAddNavItem(int highlight, const char* href, const char* title)
00012 {
00013     char *p;
00014     HttpAddText("<li ");
00015     if (highlight) p = "class='this'";
00016     else           p = "            ";
00017     HttpAddText(p);
00018     HttpAddText("><a href='");
00019     HttpAddText(href);
00020     HttpAddText("'>");
00021     HttpAddText(title);
00022     HttpAddText("</a></li>\r\n");
00023 }
00024 void WebAddNav(int page)
00025 {
00026     HttpAddText("<a class='tab-shortcut' href='#main-content'>Skip to content</a>\r\n");
00027 
00028     HttpAddText("<nav><ul>\r\n");
00029     WebNavThis(page);
00030     WebNavBase(page);
00031     HttpAddText("</ul></nav>\r\n");
00032 }
00033 
00034 void WebAddHeader(const char* title, const char* style, const char* script)
00035 {
00036     HttpAddText("<!DOCTYPE html>\r\n"
00037                      "<html>\r\n"
00038                      "<head>\r\n");
00039     HttpAddText("   <title>");
00040     HttpAddText(WEB_SITE_NAME);
00041     if (title)
00042     {
00043         HttpAddText(" - ");
00044         HttpAddText(title);
00045     }
00046     HttpAddText("</title>\r\n");
00047 
00048     HttpAddText("   <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
00049     if (style)
00050     {
00051         HttpAddText("   <link rel='stylesheet' href='/");
00052         HttpAddText(style);
00053         HttpAddText("' type='text/css'/>\r\n");
00054     }
00055     if (script)
00056     {
00057         HttpAddText("   <script src='/");
00058         HttpAddText(script);
00059         HttpAddText("' type='text/javascript'></script>\r\n");
00060     }
00061     HttpAddText("   <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
00062                      "   <link rel='icon'       href='/favicon.ico' type='image/x-icon'/>\r\n"
00063                      "</head>\r\n"
00064                      "<body>\r\n");
00065 
00066 }
00067 void WebAddH1(const char* pageName)
00068 {
00069     HttpAddText("<h1 id='main-content'>");
00070     HttpAddText(WEB_SITE_NAME);
00071     HttpAddText(" - ");
00072     HttpAddText(pageName);
00073     HttpAddText("</h1>\r\n");
00074 }
00075 void WebAddH2(const char* text)
00076 {
00077     HttpAddText("<h2>");
00078     HttpAddText(text);
00079     HttpAddText("</h2>\r\n");
00080 }
00081 void WebAddEnd()
00082 {
00083     HttpAddText("</body>\r\n"
00084                 "</html>\r\n");
00085 }
00086 
00087 void WebAddLabelledPrefixSuffix(const char* label, const char* prefix, const char* text, const char* suffix)
00088 {
00089     HttpAddText("<div class='line'>\r\n");
00090     HttpAddF   ("  <div>%s</div>\r\n", label);
00091     HttpAddF   ("  <div>%s%s%s</div>\r\n", prefix, text, suffix);
00092     HttpAddText("</div>\r\n");
00093 }
00094 void WebAddLabelledText(const char* label, const char* text)
00095 {
00096     HttpAddText("<div class='line'>\r\n");
00097     HttpAddF   ("  <div>%s</div>\r\n", label);
00098     HttpAddF   ("  <div>%s</div>\r\n", text);
00099     HttpAddText("</div>\r\n");
00100 }
00101 
00102 void WebAddLabelledMac(const char* label, const char* mac)
00103 {
00104     HttpAddText("<div class='line'>\r\n");
00105     HttpAddF   ("  <div>%s</div>\r\n", label);
00106     HttpAddText("  <div>"); MacHttp(mac); HttpAddText("</div>\r\n");
00107     HttpAddText("</div>\r\n");
00108 }
00109 
00110 void WebAddLabelledIp4(const char* label, uint32_t ip)
00111 {
00112     HttpAddText("<div class='line'>\r\n");
00113     HttpAddF   ("  <div>%s</div>\r\n", label);
00114     HttpAddText("  <div>"); Ip4AddrHttp(ip); HttpAddText("</div>\r\n");
00115     HttpAddText("</div>\r\n");
00116 }
00117 
00118 void WebAddLabelledIp6(const char* label, const char* ip)
00119 {
00120     HttpAddText("<div class='line'>\r\n");
00121     HttpAddF   ("  <div>%s</div>\r\n", label);
00122     HttpAddText("  <div>"); Ip6AddrHttp(ip); HttpAddText("</div>\r\n");
00123     HttpAddText("</div>\r\n");
00124 }
00125 void WebAddLabelledOnOff(const char* label, bool value)
00126 {
00127     if (value) WebAddLabelledText(label, "On");
00128     else       WebAddLabelledText(label, "Off");
00129 }
00130 void WebAddLabelledLed(const char* label, bool value)
00131 {
00132     HttpAddText("<div class='line'>\r\n");
00133     HttpAddF   ("  <div>%s</div>\r\n", label);
00134     HttpAddF   ("  <div class='led' dir='%s'></div>\r\n", value ? "rtl" : "ltr");
00135     HttpAddText("</div>\r\n");
00136 }
00137 void WebAddLabelledInt(const char* label, int value)
00138 {
00139     char text[30];
00140     snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant. 
00141     WebAddLabelledText(label, text);
00142 }
00143 void WebAddInputText(const char* label, float inputwidth, const char* value, const char* action, const char* name)
00144 {
00145     HttpAddF   ("<form action='%s' method='get'>\r\n", action);
00146     HttpAddText("<div class='line'>\r\n");
00147     HttpAddF   ("  <div>%s</div>\r\n", label);
00148     HttpAddF   ("  <input type='text' name='%s' style='width:%.1fem;' value='%s'>\r\n", name, inputwidth, value);
00149     HttpAddText("</div>\r\n");
00150     HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n");
00151     HttpAddF   ("</form>\r\n");
00152 
00153 }
00154 void WebAddInputInt(const char* label, float inputwidth, int value, const char* action, const char* name)
00155 {    
00156     char text[30];
00157     snprintf(text, sizeof(text), "%d", value);
00158     WebAddInputText(label, inputwidth, text, action, name);
00159 }
00160 void WebAddInputButton(const char* label, const char* value, const char* action, const char* name)
00161 {
00162     HttpAddF   ("<form action='%s' method='get'>\r\n", action);
00163     HttpAddF   ("<input type='hidden' name='%s'>\r\n", name);
00164     HttpAddText("<div class='line'>\r\n");
00165     HttpAddF   ("  <div>%s</div>\r\n", label);
00166     HttpAddF   ("  <input type='submit' value='%s'>\r\n", value);
00167     HttpAddText("</div>\r\n");
00168     HttpAddText("</form>\r\n");
00169 }
00170 void WebAddAjaxInputToggle(const char* label, const char* id, const char* name)
00171 {
00172     HttpAddText("<div class='line'>\r\n");
00173     HttpAddF   ("  <div>%s</div>\r\n", label);
00174     HttpAddF   ("  <div class='toggle' id='%s' tabindex='0' dir='ltr' onclick='AjaxSendNameValue(\"%s\", \"1\")' onkeydown='return event.keyCode != 13 || AjaxSendNameValue(\"%s\", \"1\")'>\r\n", id, name, name);
00175     HttpAddText("    <div class='slot'></div><div class='knob'></div>\r\n");
00176     HttpAddText("  </div>\r\n");
00177     HttpAddText("</div>\r\n");
00178 }
00179 void WebAddAjaxLed(const char* label, const char* id)
00180 {
00181     HttpAddText("<div class='line'>\r\n");
00182     HttpAddF   ("  <div>%s</div>\r\n", label);
00183     HttpAddF   ("  <div class='led' id='%s' dir='ltr'></div>\r\n", id);
00184     HttpAddText("</div>\r\n");
00185 }
00186 void WebAddAjaxButton(const char* caption, const char* name)
00187 {
00188     HttpAddF   ("<button onclick='AjaxSendNameValue(\"%s\", \"1\")'>%s</button>\r\n", name, caption);
00189 }
00190 void WebAddAjaxInput(const char* label, float inputwidth, const char* id, const char* name)
00191 {
00192     HttpAddText("<div class='line'>\r\n");
00193     HttpAddF   ("  <div>%s</div>\r\n", label);
00194     HttpAddF   ("  <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxSendNameValue(\"%s\", this.value)'>\r\n", inputwidth, id, name);
00195     HttpAddText("</div>\r\n");
00196 }
00197 void WebAddAjaxInputSuffix(const char* label, float inputwidth, const char* id, const char* name, const char* suffix)
00198 {
00199     HttpAddText("<div class='line'>\r\n");
00200     HttpAddF   ("  <div>%s</div>\r\n", label);
00201     HttpAddF   ("  <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxSendNameValue(\"%s\", this.value)'>%s\r\n", inputwidth, id, name, suffix);
00202     HttpAddText("</div>\r\n");
00203 }
00204 void WebAddAjaxLabelled(const char* label, const char* id)
00205 {
00206     HttpAddText("<div class='line'>\r\n");
00207     HttpAddF   ("  <div>%s</div>\r\n", label);
00208     HttpAddF   ("  <div id='%s'></div>\r\n", id);
00209     HttpAddText("</div>\r\n");
00210 }
00211 void WebAddAjaxLabelledSuffix(const char* label, const char* id, const char* suffix)
00212 {
00213     HttpAddText("<div class='line'>\r\n");
00214     HttpAddF   ("  <div>%s</div>\r\n", label);
00215     HttpAddF   ("  <div><span id='%s'></span>%s</div>\r\n", id, suffix);
00216     HttpAddText("</div>\r\n");
00217 }
00218 void WebAddAjaxInputLabelId(const char* labelId, float inputwidth, const char* id, const char* name)
00219 {
00220     HttpAddText("<div class='line'>\r\n");
00221     HttpAddF   ("  <div id='%s'></div>\r\n", labelId);
00222     HttpAddF   ("  <input type='text' style='width:%.1fem;' id='%s' onchange='AjaxSendNameValue(\"%s\", this.value)'>\r\n", inputwidth, id, name);
00223     HttpAddText("</div>\r\n");
00224 }
00225 
00226