(pHandler));
16 | return true;
17 | }
18 |
19 | CConfigPostHandler::CConfigPostHandler(CHttpRequest* pRequest) {
20 | m_pRequest=pRequest;
21 | }
22 |
23 | CConfigPostHandler::~CConfigPostHandler() {
24 |
25 | }
26 |
27 | void CConfigPostHandler::onHeader(CHttpRequest *pRequest, const char *szName, const char *szValue) {
28 | }
29 |
30 | void CConfigPostHandler::onHeadersDone(CHttpRequest *pRequest, size_t nDataLength) {
31 |
32 | }
33 |
34 | void CConfigPostHandler::onData(CHttpRequest *pRequest, const uint8_t *pData, size_t nLength) {
35 | for (size_t i = 0; i < nLength; i++) {
36 | if (m_nEscapeLeft) {
37 | m_nEscapeLeft--;
38 | m_cEscape <<= 4;
39 | if (pData[i] >= '0' && pData[i] <= '9')
40 | m_cEscape |= (pData[i] - '0');
41 | else if (pData[i] >= 'a' && pData[i] <= 'z')
42 | m_cEscape |= (pData[i] - 'a') + 10;
43 | else if (pData[i] >= 'A' && pData[i] <= 'Z')
44 | m_cEscape |= (pData[i] - 'A') + 10;
45 | if (m_nEscapeLeft == 0)
46 | m_vBuffer.push_back(m_cEscape);
47 | } else if (pData[i] == '=') {
48 | DEBUG("Equal sign");
49 | if (m_bHasKey) {
50 | m_vBuffer.push_back(pData[i]);
51 | } else {
52 | m_szKey = std::string(m_vBuffer.begin(), m_vBuffer.end());
53 | m_vBuffer.clear();
54 | m_bHasKey = true;
55 | }
56 | } else if (pData[i] == '&') {
57 | DEBUG("Ampersand");
58 | if (!m_bHasKey) {
59 | m_szKey=std::string(m_vBuffer.begin(), m_vBuffer.end());
60 | m_vBuffer.clear();
61 | m_bHasKey=true;
62 | }
63 | std::string szValue(m_vBuffer.begin(), m_vBuffer.end());
64 | DEBUG("Insert %s = %s", m_szKey.c_str(), szValue.c_str());
65 | m_mValues[m_szKey]=szValue;
66 |
67 | m_vBuffer.clear();
68 | m_bHasKey = false;
69 | } else if (pData[i] == '%') {
70 | m_cEscape = 0;
71 | m_nEscapeLeft = 2;
72 | } else if (pData[i] == '+') {
73 | m_vBuffer.push_back(' ');
74 | } else {
75 | m_vBuffer.push_back(pData[i]);
76 | }
77 | }
78 | }
79 |
80 | void CConfigPostHandler::onDataDone(CHttpRequest *pRequest) {
81 | //Handle last bit of data
82 | if (!m_bHasKey) {
83 | m_szKey=std::string(m_vBuffer.begin(), m_vBuffer.end());
84 | m_vBuffer.clear();
85 | m_bHasKey=true;
86 | }
87 | std::string szValue(m_vBuffer.begin(), m_vBuffer.end());
88 | m_mValues[m_szKey]=szValue;
89 |
90 | m_vBuffer.clear();
91 | m_bHasKey = false;
92 |
93 | // Start output
94 | m_szOutput = "";
95 | m_szOutput += "Saving...";
96 | m_szOutput += "";
97 | m_szOutput += "";
98 |
99 | // Write out config
100 | const uint8_t pHeader[] = CONFIG_HEADER;
101 | m_pWriter = new CConfigWriter(CONFIG_START_SECTOR, CONFIG_SECTOR_DIRECTION);
102 | m_pWriter->writeBytes(pHeader,sizeof(pHeader)); // Header
103 | config_run(this);
104 | m_pWriter->writeUInt(ConfigSectionEnd);
105 | m_pWriter->flush(true);
106 | delete m_pWriter;
107 |
108 | m_szOutput += "Please wait for the unit to reboot...
";
109 |
110 | m_szOutput += "";
111 |
112 | pRequest->startHeaders(200,"OK");
113 | pRequest->sendHeader("Content-Type", "text/html");
114 | pRequest->sendHeader("Content-Length", m_szOutput.length());
115 | pRequest->sendHeader("Refresh","3;url=/");
116 | pRequest->endHeaders();
117 | pRequest->sendData((const uint8_t *)m_szOutput.c_str(), m_szOutput.length());
118 | }
119 |
120 | void CConfigPostHandler::onSent(CHttpRequest *pRequest) {
121 | pRequest->end(false);
122 | }
123 |
124 | void CConfigPostHandler::onDisconnected(CHttpRequest *pRequest) {
125 | delete this;
126 | system_restart();
127 | }
128 |
129 | std::string CConfigPostHandler::createOptionKey(const char *szName) {
130 | std::string strRetval;
131 | for (auto section : m_lSections) {
132 | strRetval += section;
133 | strRetval += ".";
134 | }
135 | strRetval+=szName;
136 | return strRetval;
137 | }
138 |
139 | void CConfigPostHandler::beginModule(const char *szName, const char *szDescription) {
140 | m_pWriter->writeUInt(ConfigSectionStart);
141 | m_pWriter->writeString(szName);
142 | m_lSections.push_back(szName);
143 | }
144 |
145 | void CConfigPostHandler::endModule() {
146 | m_pWriter->writeUInt(ConfigSectionEnd);
147 | if (!m_lSections.empty())
148 | m_lSections.pop_back();
149 | }
150 |
151 | void CConfigPostHandler::optionBool(const char *szName, const char *szDescription, bool *pbValue, bool bDefault) {
152 | const auto found = m_mValues.find(createOptionKey(szName));
153 | bool bValue = (found != m_mValues.end() && found->second.compare("1") == 0);
154 | if (bValue != bDefault) {
155 | m_pWriter->writeUInt(ConfigInteger);
156 | m_pWriter->writeString(szName);
157 | m_pWriter->writeUInt(bValue?1:0);
158 | }
159 | }
160 |
161 | void CConfigPostHandler::optionString(const char *szName, const char *szDescription, char *szValue, size_t nSize, const char *szDefault) {
162 | const auto found = m_mValues.find(createOptionKey(szName));
163 | if (found != m_mValues.end() && found->second.compare(szDefault)!=0) {
164 | std::string capped(found->second);
165 | if (capped.length() + 1 > nSize)
166 | capped=capped.substr(0,nSize-1);
167 | m_pWriter->writeUInt(ConfigString);
168 | m_pWriter->writeString(szName);
169 | m_pWriter->writeString(capped.c_str());
170 | }
171 | }
172 |
173 | void CConfigPostHandler::optionInt(const char *szName, const char *szDescription, void *pValue, size_t nSize, uint32_t nMin, uint32_t nMax, uint32_t nDefault) {
174 | auto found = m_mValues.find(createOptionKey(szName));
175 | if (found != m_mValues.end()) {
176 | uint32_t nValue = atoi(found->second.c_str());
177 | if (nValue < nMin)
178 | nValue = nMin;
179 | if (nValue > nMax)
180 | nValue = nMax;
181 | if (nValue != nDefault) {
182 | m_pWriter->writeUInt(ConfigInteger);
183 | m_pWriter->writeString(szName);
184 | m_pWriter->writeUInt(nValue);
185 | }
186 | }
187 | }
188 | void CConfigPostHandler::optionSelectBegin(const char *szName, const char *szDescription, unsigned int* pnValue, unsigned int nDefault) {
189 | auto found = m_mValues.find(createOptionKey(szName));
190 | if (found != m_mValues.end()) {
191 | unsigned int nValue = atoi(found->second.c_str());
192 | if (nValue != nDefault) {
193 | m_pWriter->writeUInt(ConfigInteger);
194 | m_pWriter->writeString(szName);
195 | m_pWriter->writeUInt(nValue);
196 | }
197 | }
198 | }
199 | void CConfigPostHandler::optionSelectItem(const char *szName, unsigned int nValue) {
200 |
201 | }
202 | void CConfigPostHandler::optionSelectEnd() {
203 | }
204 | void CConfigPostHandler::optionIpAddress(const char *szName, const char *szDescription, uint32_t *pAddress, uint32_t nDefault) {
205 | std::string szBaseKey(createOptionKey(szName));
206 | char szSuffix[]="[0]";
207 | uint32_t nValue = 0;
208 | for (unsigned int i=0; i<4; i++) {
209 | szSuffix[1] = '0' + i;
210 | auto found = m_mValues.find(szBaseKey + szSuffix);
211 | if (found != m_mValues.end())
212 | nValue |= ((unsigned int)atoi(found->second.c_str()) & 0xFF) << (i*8);
213 | }
214 | if (nValue != nDefault) {
215 | m_pWriter->writeUInt(ConfigInteger);
216 | m_pWriter->writeString(szName);
217 | m_pWriter->writeUInt(nValue);
218 | }
219 | }
220 | void CConfigPostHandler::optionFloat(const char *szName, const char *szDescription, float* pfValue, float fDefault) {
221 | auto found = m_mValues.find(createOptionKey(szName));
222 | if (found != m_mValues.end()) {
223 | float fValue = eln_atof(found->second.c_str());
224 | if (fValue != fDefault) {
225 | m_pWriter->writeUInt(ConfigFloat);
226 | m_pWriter->writeString(szName);
227 | m_pWriter->writeFloat(fValue);
228 | }
229 | }
230 | }
231 |
--------------------------------------------------------------------------------
/user/config/CConfigPostHandler.h:
--------------------------------------------------------------------------------
1 | #ifndef CONFIG_CCONFIGPOSTHANDLER_H
2 | #define CONFIG_CCONFIGPOSTHANDLER_H
3 | #include "httpd/CHttpRequest.h"
4 | #include "config/IConfigRunner.h"
5 | #include