| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350 | 
							- #include "lwip/opt.h"
 
- #include "lwip/arch.h"
 
- #include "lwip/api.h"
 
- #include "lwip/tcp.h"
 
- #include "http_server.h"
 
- #include "web_params_api.h"
 
- #include "parameters.h"
 
- #include "urlcode.h"
 
- #include "trap_params.h"
 
- #include "fsdata.c"
 
- #include "settings_api.h"
 
- #include "netconf.h"
 
- #include "common_config.h"
 
- #include "testing.h"
 
- #include "rtc.h"
 
- #include "rng.h"
 
- #include "megatec.h"
 
- #include "log.h"
 
- #include "hal.h"
 
- #ifdef PRINTF_STDLIB
 
- #include <stdio.h>
 
- #endif
 
- #ifdef PRINTF_CUSTOM
 
- #include "tinystdio.h"
 
- #endif
 
- #include <string.h>
 
- #include <stdlib.h>
 
- #include "FreeRTOS.h"
 
- #include "task.h"
 
- #include "fr_timers.h"
 
- static int fs_open(char *name, struct fs_file *file);
 
- static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len);
 
- static err_t http_sent_history(void *arg, struct tcp_pcb *pcb, u16_t len);
 
- static err_t http_sent_log(void *arg, struct tcp_pcb *pcb, u16_t len);
 
- static void http_sent_log_err(void * arg, err_t err);
 
- static void send_data(struct tcp_pcb *pcb, struct http_state *hs);
 
- static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len);
 
- static uint32_t Parse_Content_Length(char *data, uint32_t len);
 
- static void HTTP_SetUserCookie(char *str, uint8_t user_id);
 
- static void HTTP_UpdateUserLoginTime(uint8_t user_id);
 
- static void HTTP_ForceUserLogout(uint8_t user_id);
 
- void LogoutTimerCallback(TimerHandle_t pxTimer);
 
- int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
 
- SET_PAGE_t SET_PAGE = SET_PAGE_IDLE;
 
- #define SEND_BUF_MAX_LEN   2000
 
- #define RECIVE_BUF_MAX_LEN   1500
 
- char sendBuf[SEND_BUF_MAX_LEN];
 
- uint16_t sendBufLoadLen = 0;
 
- uint16_t printLen = 0;
 
- //char printBuf[1000];
 
- char receiveBuf[RECIVE_BUF_MAX_LEN];
 
- uint16_t receivedBufLen = 0;
 
- #define MAX_POST_REQ_LEN 256
 
- char post_req_data[MAX_POST_REQ_LEN];
 
- uint32_t post_data_count;
 
- uint32_t log_post_reqn;
 
- /* Logout timeout, 30 minutes */
 
- #define WEB_LOGOUT_TIME  configTICK_RATE_HZ*60*30
 
- /* Max user active sessions count */
 
- #define WEB_USER_MAX_SESSION_COUNT  5
 
- typedef struct {
 
-     char cookie[MAX_WEB_COOKIE_LEN];
 
-     TimerHandle_t LogoutTimer;
 
- } auth_session_t;
 
- struct {
 
-     //auth_session_t session[WEB_USER_MAX_SESSION_COUNT];
 
-     char cookie[MAX_WEB_COOKIE_LEN];
 
-     TimerHandle_t LogoutTimer;
 
- } users[MAX_WEB_USERS];
 
- bool Authenticated = false;
 
- /* Level of currently logged-in user */
 
- uint8_t seclevel = 0xFF;
 
- static volatile uint32_t DataFlag2=0;
 
- static volatile uint32_t DataFlag=0;
 
- static volatile uint32_t size =0;
 
- static uint32_t TotalReceived=0;
 
- static volatile uint32_t TotalData=0;
 
- static uint32_t ContentLengthOffset =0, BrowserFlag=0;
 
- static const char Content_Length[17] =
 
- /* Content Length */
 
- {0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67,0x74, 0x68, 0x3a, 0x20, };
 
- const char HTTP_200_OK[] = "HTTP/1.1 200 OK\r\n\r\n";
 
- /* utf-8 marker to support MS Excel */
 
- const char UTF8_BOM[] = {0xEF, 0xBB, 0xBF, 0x00};
 
- unsigned long log_ptr = 0;
 
- unsigned long log_size = 0;
 
- bool fLogTransInprog = false;
 
- /**
 
-   * @brief  Общая структура настроек
 
-   */
 
- extern SETTINGS_t sSettings;
 
- /**
 
-   * @brief  closes tcp connection
 
-   * @param  pcb: pointer to a tcp_pcb struct
 
-   * @param  hs: pointer to a http_state struct
 
-   * @retval
 
-   */
 
- static void close_conn(struct tcp_pcb *pcb, struct http_state *hs)
 
- {
 
-   tcp_arg(pcb, NULL);
 
-   tcp_sent(pcb, NULL);
 
-   tcp_recv(pcb, NULL);
 
-   mem_free(hs);
 
-   tcp_close(pcb);
 
- }
 
- /**
 
-   * @brief callback function for handling TCP HTTP traffic
 
-   * @param arg: pointer to an argument structure to be passed to callback function
 
-   * @param pcb: pointer to a tcp_pcb structure
 
-   * @param p: pointer to a packet buffer
 
-   * @param err: LwIP error code
 
-   * @retval err
 
-   */
 
- static err_t http_recv(void *arg, struct tcp_pcb *pcb,  struct pbuf *p, err_t err)
 
- {
 
-   char *data, *ptr;
 
-   struct http_state *hs;
 
-   char CookieBuf[50];
 
-   char *CookiePtr = NULL;
 
-   char name[MAX_WEB_COOKIE_LEN];
 
-   char id[MAX_WEB_COOKIE_LEN];
 
-   uint8_t nameLen = 0, idLen = 0;
 
-   uint32_t DataOffset;
 
-   struct fs_file file = {0, 0};
 
-   uint32_t i;
 
-   
 
-   hs = arg;
 
-   if (err == ERR_OK && p != NULL)
 
-   {
 
-     tcp_recved(pcb, p->tot_len);
 
-   
 
- 	if (hs->file == NULL)
 
-     {
 
-       data = p->payload;
 
- /*	  
 
- 	  printLen = p->tot_len;
 
- 	  memcpy(printBuf, p->payload , printLen);
 
- 	  printf(printBuf);
 
- */	  
 
- 	  receivedBufLen = p->tot_len;
 
- 	  memcpy(receiveBuf, p->payload , receivedBufLen);
 
- 	  receiveBuf[receivedBufLen] = '\0';
 
- 	 // printf("receive %s \r\n", receiveBuf);
 
- 	  /* Get cookie "uname" value */
 
- 	  CookiePtr = strstr(receiveBuf, "uname=");
 
- 	  strncpy(CookieBuf, CookiePtr, 50);
 
- 	  //printf("********CookieBuf1= %s\r\n", CookieBuf);
 
- 	  memset(name, 0, MAX_WEB_COOKIE_LEN);
 
- 	  GetCookieValue(CookieBuf, "uname=", name, &nameLen);
 
- 	  //printf("********CookieBuf2= %s\r\n", CookieBuf);
 
- 	  //printf("********uname= %s\r\n", name);
 
- 	   /* Get cookie "id" value */
 
- 	   CookiePtr = strstr(receiveBuf, "id=");
 
- 	   strncpy(CookieBuf, CookiePtr, 50);
 
- 	   //printf("********CookieBuf1= %s\r\n", CookieBuf);
 
- 	   memset(id, 0, MAX_WEB_COOKIE_LEN);
 
- 	   GetCookieValue(CookieBuf, "id=", id, &idLen);
 
- 	   //printf("********CookieBuf2= %s\r\n", CookieBuf);
 
- 	   //printf("********id= %s\r\n", id);
 
- 	   /* Id of currently logged-in user */
 
- 	   uint8_t user_id;
 
- 	   if( DataFlag == 0 && DataFlag2 == 0 ){
 
- 	   /* Level of currently logged-in user */
 
- 	   seclevel = 0xFF;
 
- 	   for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
 
- 		   HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
 
- 		   if (strncmp(id, CookieBuf, idLen) == 0 ) {
 
- 			   GetUserLevelInt(user_id, &seclevel);
 
- 			   Authenticated = true;
 
- 			   break;
 
- 		   }
 
- 		   Authenticated = false;
 
- 		   seclevel = 0xFF;
 
- 	   }
 
- 	   }
 
- 	   if (DataFlag >= 1)
 
- 		 Authenticated = true;
 
- 	   else if(DataFlag2 >= 1)
 
- 	      Authenticated = true;
 
- 	   if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
 
- 	   {
 
- 		  HTTP_LOGIN(sendBuf, &sendBufLoadLen);
 
- 		  hs->file = sendBuf;
 
- 		  hs->left = sendBufLoadLen;
 
- 		  send_data(pcb, hs);
 
- 		  tcp_sent(pcb, http_sent);
 
- 	   }
 
- 	   else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
 
- 	  {
 
- 		  if (strncmp(data, "GET /main.css", 13) == 0) // +
 
- 		  {
 
- 			fs_open("/main.css", &file);
 
- 			 hs->file = file.data;
 
- 			 hs->left = file.len;
 
- 			 send_data(pcb, hs);
 
- 			 tcp_sent(pcb, http_sent);
 
- 		  }
 
- 		  else if (strncmp(data, "GET /rotek.png", 14) == 0) // +
 
- 		  {
 
- 			fs_open("/rotek.png", &file);
 
- 			 hs->file = file.data;
 
- 			 hs->left = file.len;
 
- 			 send_data(pcb, hs);
 
- 			 tcp_sent(pcb, http_sent);
 
- 		  }
 
- 		else if (strncmp(data, "GET /favicon.ico", 16) == 0) // ?
 
- 		{
 
- 			 fs_open("/favicon.ico", &file);
 
- 			 hs->file = file.data;
 
- 			 hs->left = file.len;
 
- 			 send_data(pcb, hs);
 
- 			 tcp_sent(pcb, http_sent);
 
- 		}
 
- 		else if (strncmp(data, "GET /role.js", 12) == 0)
 
- 		{
 
- 		  fs_open("/role.js", &file);
 
- 		  hs->file = file.data;
 
- 		  hs->left = file.len;
 
- 		  send_data(pcb, hs);
 
- 		  tcp_sent(pcb, http_sent);
 
- 		}
 
- 		  else if ((strncmp(data, "POST /login.cgi", 15) == 0) || (log_post_reqn > 0))
 
- 			{
 
- 			  uint32_t i, offset = 0, req_data_received = 0;
 
- 			  //printf("request 1: %d\r\n", receivedBufLen);
 
- 			  /* parse packet for Content-length field */
 
- 			  post_data_count = Parse_Content_Length(data, p->tot_len);
 
- 			  //printf("Content-length: %d\r\n", (int)post_data_count);
 
- 			  if (post_data_count < MAX_POST_REQ_LEN) {
 
- 				  memset(post_req_data, 0, MAX_POST_REQ_LEN);
 
- 					/* parse packet for "\r\n\r\n" */
 
- 					for (i = 0; i < receivedBufLen; i++)
 
- 					{
 
- 						if (strncmp ((char*)(data+i), "\r\n\r\n", 4) == 0)
 
- 						{
 
- 							offset = i+4;
 
- 							//printf("offset: %d\r\n", (int)offset);
 
- 							break;
 
- 						}
 
- 					}
 
- 					req_data_received = receivedBufLen - offset;
 
- 					//printf("req data received: %d\r\n", (int)req_data_received);
 
- 					/* Check if "\r\n\r\n" was found */
 
- 					if (offset != 0) {
 
- 						/* if data was splited in two packets */
 
- 						if (req_data_received < post_data_count) {
 
- 							/* Copy request data to buffer */
 
- 							snprintf(post_req_data, req_data_received, "%s", receiveBuf);
 
- 							//printf("copied: %d\r\n", (int)req_data_received);
 
- 							post_data_count -= req_data_received;
 
- 						}
 
- 						/* if data received completely */
 
- 						else {
 
- 							strncat(post_req_data, (char *)(data + offset), post_data_count);
 
- 							//printf("post_req_data: %s\r\n", post_req_data);
 
- 							if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) {
 
- 								  hs->file = sendBuf;
 
- 								  hs->left = sendBufLoadLen;
 
- 								  send_data(pcb, hs);
 
- 								  tcp_sent(pcb, http_sent);
 
- 							}
 
- 							else {
 
- 								/* Redirect to login page */
 
- 								fs_open("/login.html", &file);
 
- 								hs->file = file.data;
 
- 								hs->left = file.len;
 
- 								send_data(pcb, hs);
 
- 								tcp_sent(pcb, http_sent);
 
- 							}
 
- 							/* End reqest */
 
- 							post_data_count = 0;
 
- 							log_post_reqn = 0;
 
- 						}
 
- 					}
 
- 					/* request was fragmented before "\r\n\r\n" */
 
- 					else {
 
- 						//printf("no data found!\r\n");
 
- 						/* wait next packet */
 
- 						log_post_reqn++;
 
- 						/* wait max 2 requests */
 
- 						if (log_post_reqn > 1) {
 
- 							/* Redirect to login page */
 
- 							fs_open("/login.html", &file);
 
- 							hs->file = file.data;
 
- 							hs->left = file.len;
 
- 							send_data(pcb, hs);
 
- 							tcp_sent(pcb, http_sent);
 
- 	
 
- 							/* End reqest */
 
- 							post_data_count = 0;
 
- 							log_post_reqn = 0;
 
- 						}
 
- 					}
 
- 			  }
 
- 			  else {
 
- 				  printf("Too long POST request!\r\n");
 
- 				  /* Ignore request */
 
- 				  post_data_count = 0;
 
- 				  log_post_reqn = 0;
 
- 					/* Redirect to login page */
 
- 					fs_open("/login.html", &file);
 
- 					hs->file = file.data;
 
- 					hs->left = file.len;
 
- 					send_data(pcb, hs);
 
- 					tcp_sent(pcb, http_sent);
 
- 			  }
 
- 			}
 
- 			  else if (post_data_count > 0)
 
- 				{
 
- 				  strncat(post_req_data, data, post_data_count);
 
- 				  //printf("copied: %d\r\n", (int)post_data_count);
 
- 				  //printf("post_req_data: %s\r\n", post_req_data);
 
- 				  if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) {
 
- 						hs->file = sendBuf;
 
- 						hs->left = sendBufLoadLen;
 
- 						send_data(pcb, hs);
 
- 						tcp_sent(pcb, http_sent);
 
- 				  }
 
- 				  else {
 
- 					  /* Redirect to login page */
 
- 					  fs_open("/login.html", &file);
 
- 					  hs->file = file.data;
 
- 					  hs->left = file.len;
 
- 					  send_data(pcb, hs);
 
- 					  tcp_sent(pcb, http_sent);
 
- 				  }
 
- 				  /* End reqest */
 
- 				  post_data_count = 0;
 
- 				  log_post_reqn = 0;
 
- 				}
 
- 		  else
 
- 		  {
 
- 			  fs_open("/login.html", &file);
 
- 			  hs->file = file.data;
 
- 			  hs->left = file.len;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent);
 
- 		  }
 
- 	  }
 
- 	  else if ( Authenticated == true ) {
 
- 	  if (strncmp(data, "GET /main.css", 13) == 0) // +
 
- 	  {
 
- 	    fs_open("/main.css", &file);
 
-         hs->file = file.data;
 
-         hs->left = file.len;
 
-         send_data(pcb, hs);
 
-         tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  else if (strncmp(data, "GET /rotek.png", 14) == 0) // +
 
- 	  {
 
- 	    fs_open("/rotek.png", &file);
 
-         hs->file = file.data;
 
-         hs->left = file.len;
 
-         send_data(pcb, hs);
 
-         tcp_sent(pcb, http_sent);
 
- 	  }
 
-       else if (strncmp(data, "GET /favicon.ico", 16) == 0) // ?                 
 
-       {                                                                     
 
-         fs_open("/favicon.ico", &file);                                     
 
-         hs->file = file.data;                                               
 
-         hs->left = file.len;                                                
 
-         send_data(pcb, hs);                                                 
 
-         tcp_sent(pcb, http_sent);                                           
 
-       }
 
- 	  else if (strncmp(data, "GET /main.js", 12) == 0) // +
 
- 	  {
 
- 	    fs_open("/main.js", &file);
 
-         hs->file = file.data;
 
-         hs->left = file.len;
 
-         send_data(pcb, hs);
 
-         tcp_sent(pcb, http_sent);
 
- 		
 
- 	  }
 
- 	  else if (strncmp(data, "GET /role.js", 12) == 0)
 
- 	  {
 
- 		fs_open("/role.js", &file);
 
- 		hs->file = file.data;
 
- 		hs->left = file.len;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  else if (strncmp(data, "GET /settings.html", 18) == 0) // +
 
- 	  {
 
- 		HTTP_UpdateUserLoginTime(user_id);
 
- 		if (seclevel == 0){
 
- 			fs_open("/settings.html", &file);
 
- 			hs->file = file.data;
 
- 			hs->left = file.len;
 
- 			send_data(pcb, hs);
 
- 			tcp_sent(pcb, http_sent);
 
- 		}
 
- 		else {
 
- 			fs_open("/index.html", &file);
 
- 			hs->file = file.data;
 
- 			hs->left = file.len;
 
- 			send_data(pcb, hs);
 
- 			tcp_sent(pcb, http_sent);
 
- 		}
 
- 	  }
 
- 	  else if (strncmp(data, "GET /info.html", 14) == 0) // +
 
- 	  {
 
- 		HTTP_UpdateUserLoginTime(user_id);
 
- 	    fs_open("/info.html", &file);
 
-         hs->file = file.data;
 
-         hs->left = file.len;
 
-         send_data(pcb, hs);
 
-         tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  else if (strncmp(data, "GET /history.html", 17) == 0)
 
- 	  {
 
- 		HTTP_UpdateUserLoginTime(user_id);
 
- 		fs_open("/history.html", &file);
 
- 		hs->file = file.data;
 
- 		hs->left = file.len;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  else if (strncmp(data, "GET /ups_history.html", 21) == 0)
 
- 	  {
 
- 		HTTP_UpdateUserLoginTime(user_id);
 
- 		fs_open("/ups_history.html", &file);
 
- 		hs->file = file.data;
 
- 		hs->left = file.len;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  else if (strncmp(data, "GET /getJson.cgi", 16) == 0) // +
 
- 	  {
 
- 		HTTP_GetParamsPage1(sendBuf);
 
- 		hs->file = sendBuf;
 
-         hs->left = strlen(sendBuf);
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }	  
 
- 	  else if (strncmp(data, "GET /settings.cgi", 17) == 0) // +
 
- 	  {
 
- 		if (seclevel == 0) {
 
- 			SET_PAGE = SET_PAGE_PAGE2;
 
- 			if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
 
- 			{
 
- 			  hs->file = sendBuf;
 
- 			  hs->left = sendBufLoadLen;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent);
 
- 			}
 
- 			/*else
 
- 			{
 
- 			  fs_open("/settings.html", &file);
 
- 			  hs->file = file.data;
 
- 			  hs->left = file.len;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent);
 
- 			}*/
 
- 		}
 
- 	  }
 
- 	  else if (strncmp(data, "POST /settings.cgi", 18) == 0  || (DataFlag2 >= 1))
 
- 	  {
 
- 		  if (seclevel == 0) {
 
- 			DataOffset = 0;
 
- 			/* POST Packet received */
 
- 			if (DataFlag2 == 0)
 
- 			{
 
- 			  BrowserFlag = 0;
 
- 			  TotalReceived = 0;
 
- 			  memset(sendBuf, 0, strlen(sendBuf));
 
- 			  /* parse packet for Content-length field */
 
- 			  size = Parse_Content_Length(data, p->tot_len);
 
- 			  /* parse packet for the octet-stream field */
 
- 			  for (i = 0; i < receivedBufLen; i++)
 
- 			  {
 
- 				  if (strncmp ((char*)(data+i), "managerIP", 8)==0)
 
- 				 {
 
- 				   DataOffset = i;
 
- 				   break;
 
- 				 }
 
- 			  }
 
- 			  /* case of MSIE8 : we do not receive data in the POST packet*/
 
- 			  if (DataOffset == 0)
 
- 			  {
 
- 				 DataFlag2++;
 
- 				 BrowserFlag = 1;
 
- 				 pbuf_free(p);
 
- 				 return ERR_OK;
 
- 			  }
 
- 			  /* case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
 
- 			  else
 
- 			  {
 
- 				//TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
 
- 				TotalReceived = receivedBufLen - DataOffset;
 
- 			  }
 
- 			}
 
- 			if (((DataFlag2 ==1)&&(BrowserFlag==1)) || ((DataFlag2 ==0)&&(BrowserFlag==0)))
 
- 			{
 
- 			   if ((DataFlag2 ==0)&&(BrowserFlag==0))
 
- 			   {
 
- 				 DataFlag2++;
 
- 			   }
 
- 			   else if ((DataFlag2 ==1)&&(BrowserFlag==1))
 
- 			   {
 
- 				 /* parse packet for the octet-stream field */
 
- 				 for (i = 0; i < receivedBufLen; i++)
 
- 				 {
 
- 					 if (strncmp ((char*)(data+i), "managerIP", 8)==0)
 
- 					 {
 
- 					   DataOffset = i;
 
- 					   break;
 
- 					 }
 
- 				 }
 
- 				 TotalReceived += receivedBufLen;
 
- 				 DataFlag2++;
 
- 			   }
 
- 			   TotalData =0 ;
 
- 			 }
 
- 			 /* DataFlag >1 => the packet is data only  */
 
- 			 else
 
- 			 {
 
- 			   TotalReceived +=receivedBufLen;
 
- 			 }
 
- 			 ptr = (char*)(data + DataOffset);
 
- 			 receivedBufLen-= DataOffset;
 
- 			 /* update Total data received counter */
 
- 			 TotalData +=receivedBufLen;
 
- 			 /* check if last data packet */
 
- 			 if (TotalReceived == size)
 
- 			 {
 
- 				 //DBG printf("State: Received %d bytes\r\n", (int)TotalReceived);
 
- 				 strncat(sendBuf,  ptr, receivedBufLen);
 
- 				 strncat(sendBuf,  " ", 1);
 
- 				 //ВBG printf("receive %s /r/n", sendBuf);
 
- 				 HTTP_SetSettings(sendBuf, strlen(sendBuf));
 
- 				 DataFlag2=0;
 
- 				 BrowserFlag = 0;
 
- 				 memset(sendBuf, 0, size);
 
- 				 strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
 
- 			  strcat(sendBuf, "\r\n\r\n");
 
- 			  strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/settings.html\"/></head></html>\r\n\r\n");
 
- 			  sendBufLoadLen = strlen(sendBuf);
 
- 			  hs->file = sendBuf;
 
- 			  hs->left = sendBufLoadLen;
 
- 				send_data(pcb, hs);
 
- 			   /* Tell TCP that we wish be to informed of data that has been
 
- 				  successfully sent by a call to the http_sent() function. */
 
- 			   tcp_sent(pcb, http_sent);
 
- 			}
 
- 			 /* not last data packet */
 
- 			 else
 
- 			 {
 
- 			   /* write data in flash */
 
- 			   if(receivedBufLen)
 
- 			   {
 
- 				 strncat(sendBuf,  ptr, receivedBufLen);
 
- 				 //memcpy(receiveBufTemp, ptr, receivedBufLen);
 
- 			   }
 
- 			 }
 
- 		  }
 
- 	  }
 
- 	  else if (strncmp(data, "GET /info.cgi", 13) == 0) // +
 
- 	  {
 
- 			if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
 
- 			{
 
- 			  hs->file = sendBuf;
 
- 			  hs->left = sendBufLoadLen;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent);
 
- 			}
 
- 		/*	else
 
- 			{
 
- 			  fs_open("/info.html", &file);
 
- 			  hs->file = file.data;
 
- 			  hs->left = file.len;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent);
 
- 			}*/
 
- 	  }
 
- 	  else if (strncmp(data, "POST /info.cgi", 14) == 0 || (DataFlag >= 1))
 
- 	  {
 
- 		  if (seclevel == 0) {
 
- 			DataOffset = 0;
 
- 			/* POST Packet received */
 
- 			if (DataFlag == 0)
 
- 			{
 
- 			  BrowserFlag = 0;
 
- 			  TotalReceived = 0;
 
- 			  memset(sendBuf, 0, strlen(sendBuf));
 
- 			  /* parse packet for Content-length field */
 
- 			  size = Parse_Content_Length(data, p->tot_len);
 
- 			  /* parse packet for the octet-stream field */
 
- 			  for (i = 0; i < receivedBufLen; i++)
 
- 			  {
 
- 				  if (strncmp ((char*)(data+i), "owner", 5)==0)
 
- 				 {
 
- 				   DataOffset = i;
 
- 				   break;
 
- 				 }
 
- 			  }
 
- 			  /* case of MSIE8 : we do not receive data in the POST packet*/
 
- 			  if (DataOffset == 0)
 
- 			  {
 
- 				 DataFlag++;
 
- 				 BrowserFlag = 1;
 
- 				 pbuf_free(p);
 
- 				 return ERR_OK;
 
- 			  }
 
- 			  /* case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
 
- 			  else
 
- 			  {
 
- 				//TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
 
- 				TotalReceived = receivedBufLen - DataOffset;
 
- 			  }
 
- 			}
 
- 			if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0)))
 
- 			{
 
- 			   if ((DataFlag ==0)&&(BrowserFlag==0))
 
- 			   {
 
- 				 DataFlag++;
 
- 			   }
 
- 			   else if ((DataFlag ==1)&&(BrowserFlag==1))
 
- 			   {
 
- 				 /* parse packet for the octet-stream field */
 
- 				 for (i = 0; i < receivedBufLen; i++)
 
- 				 {
 
- 					 if (strncmp ((char*)(data+i), "owner", 5)==0)
 
- 					 {
 
- 					   DataOffset = i;
 
- 					   break;
 
- 					 }
 
- 				 }
 
- 				 TotalReceived += receivedBufLen;
 
- 				 DataFlag++;
 
- 			   }
 
- 			   TotalData =0 ;
 
- 			 }
 
- 			 /* DataFlag >1 => the packet is data only  */
 
- 			 else
 
- 			 {
 
- 			   TotalReceived +=receivedBufLen;
 
- 			 }
 
- 			 ptr = (char*)(data + DataOffset);
 
- 			 receivedBufLen-= DataOffset;
 
- 			 /* update Total data received counter */
 
- 			 TotalData +=receivedBufLen;
 
- 			 /* check if last data packet */
 
- 			 if (TotalReceived == size)
 
- 			 {
 
- 				 strncat(sendBuf,  ptr, receivedBufLen);
 
- 				 strncat(sendBuf,  " ", 1);
 
- 				 HTTP_SetInfo(sendBuf, strlen(sendBuf));
 
- 				 DataFlag=0;
 
- 				 BrowserFlag = 0;
 
- 				 memset(sendBuf, 0, size);
 
- 				  strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
 
- 				  strcat(sendBuf, "\r\n\r\n");
 
- 				  strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/info.html\"/></head></html>\r\n\r\n");
 
- 				  sendBufLoadLen = strlen(sendBuf);
 
- 				  hs->file = sendBuf;
 
- 				  hs->left = sendBufLoadLen;
 
- 					send_data(pcb, hs);
 
- 				   /* Tell TCP that we wish be to informed of data that has been
 
- 					  successfully sent by a call to the http_sent() function. */
 
- 				   tcp_sent(pcb, http_sent);
 
- 			}
 
- 			 /* not last data packet */
 
- 			 else
 
- 			 {
 
- 			   /* write data in flash */
 
- 			   if(receivedBufLen)
 
- 			   {
 
- 				 strncat(sendBuf,  ptr, receivedBufLen);
 
- 				 //memcpy(receiveBufTemp, ptr, receivedBufLen);
 
- 			   }
 
- 			 }
 
- 		  }
 
- 	  }
 
- 	  else if (strncmp(data, "GET /history.cgi", 16) == 0)
 
- 		{
 
- 		  int res;
 
- 		  res = HTTP_HistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 		  if (res == SEND_REQUIRED_FILE)
 
- 		  {
 
- 			  hs->file = sendBuf;
 
- 			  hs->left = sendBufLoadLen;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent_history);
 
- 			  tcp_err(pcb, http_sent_log_err);
 
- 		  }
 
- 		  else if (res == SEND_REQUIRED_YES) {
 
- 			  hs->file = sendBuf;
 
- 			  hs->left = sendBufLoadLen;
 
- 			  send_data(pcb, hs);
 
- 			  tcp_sent(pcb, http_sent);
 
- 		  }
 
- 		}
 
- 	else if (strncmp(data, "GET /ups_history.cgi", 19) == 0)
 
- 	{
 
- 	  int res;
 
- 	  res = HTTP_UpsHistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 	  if (res == SEND_REQUIRED_FILE)
 
- 	  {
 
- 		  hs->file = sendBuf;
 
- 		  hs->left = sendBufLoadLen;
 
- 		  send_data(pcb, hs);
 
- 		  tcp_sent(pcb, http_sent_log);
 
- 		  tcp_err(pcb, http_sent_log_err);
 
- 	  }
 
- 	  else if (res == SEND_REQUIRED_YES) {
 
- 		  hs->file = sendBuf;
 
- 		  hs->left = sendBufLoadLen;
 
- 		  send_data(pcb, hs);
 
- 		  tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	}
 
- 	  /* Тест  АКБ ИБП */
 
- 	  else if (strncmp(data, "GET /bat_test.cgi", 17) == 0)
 
- 	  {
 
- 		HTTP_UPSTest(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 		hs->file = sendBuf;
 
- 		hs->left = sendBufLoadLen;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  /* Выключение ИБП */
 
- 	  else if (strncmp(data, "GET /ups_power.cgi", 18) == 0)
 
- 	  {
 
- 		HTTP_UPSshutdown(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 		hs->file = sendBuf;
 
- 		hs->left = sendBufLoadLen;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  /* Сброс настроек и сохранине */
 
- 	  else if (strncmp(data, "GET /reset.cgi", 14) == 0)
 
- 	  {
 
- 		HTTP_ResetSettings();
 
- 		HTTP_SaveSettings();
 
- 		fs_open("/settings.html", &file);
 
- 		hs->file = file.data;
 
- 		hs->left = file.len;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  /* Перезагрузка контроллера */
 
- 	  else if (strncmp(data, "GET /reboot.cgi", 15) == 0)
 
- 	  {
 
- 		HTTP_Reboot();
 
- 	  }
 
- 	  /* Подтверждение новых сетевых настроек */
 
- 	  else if (strncmp(data, "GET /confirm.cgi", 16) == 0)
 
- 	  {
 
- 		SetWebReinitFlag(false);
 
- 		SetConfirmWebParamsFlag();
 
- 		fs_open("/index.html", &file);
 
- 		hs->file = file.data;
 
- 		hs->left = file.len;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  /* Проверка пароля, переход в bootloader */
 
- 	  else if (strncmp(data, "GET /fw_update.cgi", 18) == 0)
 
- 	  {
 
- 		HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 		hs->file = sendBuf;
 
- 		hs->left = sendBufLoadLen;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  /* Смена пароля пользователя */
 
- 	  else if (strncmp(data, "GET /changepwd.cgi", 18) == 0)
 
- 	  {
 
- 		  HTTP_ChangeUserPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 		  hs->file = sendBuf;
 
- 		  hs->left = sendBufLoadLen;
 
- 		  send_data(pcb, hs);
 
- 		  tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  // На производстве
 
- 	  else if (strncmp(data, "GET /setProdate.cgi", 19) == 0)
 
- 	  {
 
- 		HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
- 		hs->file = sendBuf;
 
- 		hs->left = sendBufLoadLen;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	  else
 
- 	  {
 
- 		HTTP_UpdateUserLoginTime(user_id);
 
- 		fs_open("/index.html", &file); // +
 
- 		hs->file = file.data;
 
- 		hs->left = file.len;
 
- 		send_data(pcb, hs);
 
- 		tcp_sent(pcb, http_sent);
 
- 	  }
 
- 	}
 
-     }
 
-     pbuf_free(p);
 
-    // close_conn(pcb,hs);
 
-   }
 
- /*  if (err == ERR_OK && p == NULL)
 
-   {
 
-     close_conn(pcb, hs);
 
-   }*/
 
-   return ERR_OK;
 
- } 
 
- /**
 
-   * @brief Error callback for log file transfer
 
-   */
 
- static void http_sent_log_err(void * arg, err_t err)
 
- {
 
-     (void)err;
 
-     (void)arg;
 
-     /* Clear file transfer in progress flag */
 
-     fLogTransInprog = false;
 
- }
 
- /**
 
-   * @brief Sent callback for log file transfer (messages as is, not ordered)
 
-   */
 
- static err_t http_sent_log(void *arg, struct tcp_pcb *pcb, u16_t len)
 
- {
 
-   struct http_state *hs;
 
-   uint32_t nbytes = 0;
 
-   static bool start = true;
 
-   (void)len;
 
-   hs = arg;
 
-   if (hs->left > 0)
 
-   {
 
-     send_data(pcb, hs);
 
-   }
 
-   else
 
-   {
 
- 	  memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
 
-       if (log_ptr + FILE_BUF_MAX_LEN_LOG <= log_size) {
 
-           nbytes = LOG_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN_LOG, start);
 
-       }
 
-       else if (log_ptr < log_size) {
 
-           nbytes = LOG_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
 
-       }
 
-       else {
 
-           nbytes = 0;
 
-       }
 
-       log_ptr += nbytes;
 
-       start = false;
 
-       if (nbytes == 0) {
 
-           /* File transfer finished. */
 
-           start = true;
 
-           close_conn(pcb, hs);
 
-           /* Clear file transfer in progress flag */
 
-           fLogTransInprog = false;
 
-           return ERR_OK;
 
-       }
 
-       hs->file = logFileBuf;
 
-       hs->left = nbytes;
 
-       send_data(pcb, hs);
 
-       tcp_sent(pcb, http_sent_log);
 
-   }
 
-   return ERR_OK;
 
- }
 
- /**
 
-   * @brief Sent callback for log file transfer (messages as is, not ordered)
 
-   */
 
- static err_t http_sent_history(void *arg, struct tcp_pcb *pcb, u16_t len)
 
- {
 
-   struct http_state *hs;
 
-   uint32_t nbytes = 0;
 
-   static bool start = true;
 
-   (void)len;
 
-   hs = arg;
 
-   if (hs->left > 0)
 
-   {
 
-     send_data(pcb, hs);
 
-   }
 
-   else
 
-   {
 
- 	  memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
 
-       if (log_ptr + FILE_BUF_MAX_LEN <= log_size) {
 
-           nbytes = History_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN, start);
 
-       }
 
-       else if (log_ptr < log_size) {
 
-           nbytes = History_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
 
-       }
 
-       else {
 
-           nbytes = 0;
 
-       }
 
-       log_ptr += nbytes;
 
-       start = false;
 
-       if (nbytes == 0) {
 
-           /* File transfer finished. */
 
-           start = true;
 
-           close_conn(pcb, hs);
 
-           /* Clear file transfer in progress flag */
 
-           fLogTransInprog = false;
 
-           return ERR_OK;
 
-       }
 
-       hs->file = logFileBuf;
 
-       hs->left = nbytes;
 
-       send_data(pcb, hs);
 
-       tcp_sent(pcb, http_sent_history);
 
-   }
 
-   return ERR_OK;
 
- }
 
- /**
 
-   * @brief  callback function for handling connection errors
 
-   * @param  arg: pointer to an argument to be passed to callback function
 
-   * @param  err: LwIP error code   
 
-   * @retval none
 
-   */
 
- static void conn_err(void *arg, err_t err)
 
- {
 
-   struct http_state *hs;
 
-   hs = arg;
 
-   mem_free(hs);
 
- }
 
- /**
 
-   * @brief callback function called after a successfull TCP data packet transmission  
 
-   * @param arg: pointer to an argument to be passed to callback function
 
-   * @param pcb: pointer on tcp_pcb structure
 
-   * @param len
 
-   * @retval err : LwIP error code
 
-   */
 
- static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
 
- {
 
-   struct http_state *hs;
 
-   hs = arg;
 
-   if (hs->left > 0)
 
-   {
 
-     send_data(pcb, hs);
 
-   }
 
-   else
 
-   {
 
-     close_conn(pcb, hs);      
 
-   }
 
-   return ERR_OK;
 
- }
 
- /**
 
-   * @brief sends data found in  member "file" of a http_state struct
 
-   * @param pcb: pointer to a tcp_pcb struct
 
-   * @param hs: pointer to a http_state struct
 
-   * @retval none
 
-   */
 
- static void send_data(struct tcp_pcb *pcb, struct http_state *hs)
 
- {
 
-   err_t err;
 
-   u16_t len;
 
-   /* We cannot send more data than space available in the send
 
-      buffer */
 
-   if (tcp_sndbuf(pcb) < hs->left)
 
-   {
 
-     len = tcp_sndbuf(pcb);
 
-   }
 
-   else
 
-   {
 
-     len = hs->left;
 
-   }
 
-   err = tcp_write(pcb, hs->file, len, 0);
 
-   if (err == ERR_OK)
 
-   {
 
-     hs->file += len;
 
-     hs->left -= len;
 
-   }
 
- }
 
- /**
 
-   * @brief tcp poll callback function
 
-   * @param arg: pointer to an argument to be passed to callback function
 
-   * @param pcb: pointer on tcp_pcb structure
 
-   * @retval err_t
 
-   */
 
- static err_t http_poll(void *arg, struct tcp_pcb *pcb)
 
- {
 
-   if (arg == NULL)
 
-   {
 
-     tcp_close(pcb);
 
-   }
 
-   else
 
-   {
 
-     send_data(pcb, (struct http_state *)arg);
 
-   }
 
-   return ERR_OK;
 
- }
 
- /**
 
-   * @brief  callback function on TCP connection setup ( on port 80)
 
-   * @param  arg: pointer to an argument structure to be passed to callback function
 
-   * @param  pcb: pointer to a tcp_pcb structure
 
-   * ¶m  err: Lwip stack error code
 
-   * @retval err
 
-   */
 
- static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
 
- {
 
-   struct http_state *hs;
 
-   /* Allocate memory for the structure that holds the state of the connection */
 
-   hs = mem_malloc(sizeof(struct http_state));
 
-   if (hs == NULL)
 
-   {
 
-     return ERR_MEM;
 
-   }
 
-   /* Initialize the structure. */
 
-   hs->file = NULL;
 
-   hs->left = 0;
 
-   /* Tell TCP that this is the structure we wish to be passed for our
 
-      callbacks. */
 
-   tcp_arg(pcb, hs);
 
-   /* Tell TCP that we wish to be informed of incoming data by a call
 
-      to the http_recv() function. */
 
-   tcp_recv(pcb, http_recv);
 
-   tcp_err(pcb, conn_err);
 
-   tcp_poll(pcb, http_poll, 10);
 
-   return ERR_OK;
 
- }
 
- /**
 
-   * @brief  Opens a file defined in fsdata.c ROM filesystem 
 
-   * @param  name : pointer to a file name
 
-   * @param  file : pointer to a fs_file structure  
 
-   * @retval  1 if success, 0 if fail
 
-   */
 
- static int fs_open(char *name, struct fs_file *file)
 
- {
 
-   struct fsdata_file_noconst *f;
 
-   for (f = (struct fsdata_file_noconst *)FS_ROOT; f != NULL; f = (struct fsdata_file_noconst *)f->next)
 
-   {
 
-     if (!strcmp(name, f->name))
 
-     {
 
-       file->data = f->data;
 
-       file->len = f->len;
 
-       return 1;
 
-     }
 
-   }
 
-   return 0;
 
- }
 
- /**
 
-   * @brief  Initialize the HTTP server (start its thread) 
 
-   * @param  none
 
-   * @retval None
 
-   */
 
- void HTTP_Init()
 
- {
 
- 	char buf[MAX_WEB_COOKIE_LEN];
 
- 	  uint8_t user_id;
 
-   //sys_thread_new("HTTP", http_server_netconn_thread, NULL, 3000, 2);
 
-   struct tcp_pcb *pcb;
 
-   /*create new pcb*/
 
-   pcb = tcp_new();
 
-   /* bind HTTP traffic to pcb */
 
-   tcp_bind(pcb, IP_ADDR_ANY, 80);
 
-   /* start listening on port 80 */
 
-   pcb = tcp_listen(pcb);
 
-   /* define callback function for TCP connection setup */
 
-   tcp_accept(pcb, http_accept);
 
-   for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
 
-      /* Flush user cookie by random value */
 
-      sprintf(buf, "%X", (unsigned int)GetRandomNumber());
 
-      HTTP_SetUserCookie(buf, user_id);
 
-      /* Create user logout timers */
 
-      users[user_id].LogoutTimer =
 
-              xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
 
-    }
 
- }
 
- /**
 
-   * @brief  
 
-   * @retval None
 
-   */
 
- int HTTP_SettingsPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-   char tempStr[30];
 
-   strncpy(tempStr, bufIn, 30);
 
-   
 
-   /* В запросе нет параметров, нужно формировать JSON ответ */
 
-   if (strpbrk(tempStr,"?") == 0)
 
-   {
 
- 	memset(bufOut, 0, SEND_BUF_MAX_LEN);
 
- 	
 
- 	HTTP_GetSettings(bufOut);
 
- 	
 
-     //printf(bufOut);
 
-     
 
-     *lenBufOut = strlen(bufOut);
 
- 	
 
- 	return SEND_REQUIRED_YES;
 
-   }
 
-   /* В запросе есть параметры, нужно парсить и сохранять настройки */
 
-   else
 
-   {
 
- 	//HTTP_SetSettings(bufIn, lenBufIn);
 
- 	
 
-     return SEND_REQUIRED_NO;
 
-   }
 
- }
 
- /**
 
-   * @brief  
 
-   * @retval None
 
-   */
 
- int HTTP_InfoPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-   char tempStr[30];
 
-   strncpy(tempStr, bufIn, 30);
 
-   
 
-   /* В запросе нет параметров, нужно формировать JSON ответ */
 
-   if (strpbrk(tempStr,"?") == 0)
 
-   {
 
- 	memset(bufOut, 0, SEND_BUF_MAX_LEN);
 
- 	
 
- 	HTTP_GetInfo(bufOut);
 
- 		
 
-     *lenBufOut = strlen(bufOut);
 
- 	
 
- 	return SEND_REQUIRED_YES;
 
-   }
 
-   /* В запросе есть параметры, нужно парсить и сохранять настройки */
 
-   else
 
-   {
 
- 	//HTTP_SetInfo(bufIn, lenBufIn);
 
- 	return SEND_REQUIRED_NO;
 
- /*	
 
- 	HTTP_SetSettings(bufIn, lenBufIn);
 
-     return SEND_REQUIRED_NO;
 
- */	
 
-   }
 
- }  
 
- int HTTP_HistoryPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-   uint8_t i, valueLen = 0;
 
-   char value[20];
 
-   uint32_t nbytes = 0;
 
-   (void)lenBufIn;
 
-   memset(bufOut, 0, FILE_BUF_MAX_LEN);
 
-   ClearParamString(bufIn);
 
-   memset(value, 0, 20);
 
-   GetParamValue(bufIn, "page=", value, &valueLen);
 
-   if (strcmp(value, "all") == 0)
 
-   {
 
- 	  if (!LOG_IsInit()) {
 
- 		  return SEND_REQUIRED_NO;
 
- 	  }
 
-       if (fLogTransInprog == false) {
 
-         // Send log as raw data
 
-         log_ptr = 0;
 
-         log_size = History_GetTotalSTRCount() * STRING_SIZE_HISTORY + sizeof(UTF8_BOM)-1;
 
-         sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Length:%lu\r\n\r\n%s", log_size, UTF8_BOM);
 
-         *lenBufOut = strlen(bufOut);
 
-         // Set file transfer in progress flag
 
-         fLogTransInprog = true;
 
-         return SEND_REQUIRED_FILE;
 
-      }
 
-      else {
 
-           // We send nothing if file transfer already in progress
 
-           return SEND_REQUIRED_NO;
 
-      }
 
-   }
 
-   else {
 
- 	  if (!LOG_IsInit()) {
 
- 		  return SEND_REQUIRED_NO;
 
- 	  }
 
- 	  else {
 
- 		  HTTP_GetHistoryPage(bufOut, atoi(value));
 
- 		  *lenBufOut = strlen(bufOut);
 
- 		  return SEND_REQUIRED_YES;
 
- 	  }
 
-   }
 
- }
 
- int HTTP_UpsHistoryPage(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-   uint8_t i, valueLen = 0;
 
-   char value[20];
 
-   uint32_t nbytes = 0;
 
-   (void)lenBufIn;
 
-   memset(bufOut, 0, FILE_BUF_MAX_LEN);
 
-   ClearParamString(bufIn);
 
-   memset(value, 0, 20);
 
-   GetParamValue(bufIn, "page=", value, &valueLen);
 
-   if (strcmp(value, "all") == 0)
 
-   {
 
- 	  if (!LOG_IsInit()) {
 
- 		  return SEND_REQUIRED_NO;
 
- 	  }
 
-       if (fLogTransInprog == false) {
 
-         // Send log as raw data
 
-         log_ptr = 0;
 
-         log_size = LOG_GetTotalSTRCount() * STRING_SIZE + sizeof(UTF8_BOM)-1;
 
-         sprintf(bufOut, "HTTP/1.1 200 OK\r\nContent-Length:%lu\r\n\r\n%s", log_size, UTF8_BOM);
 
-         *lenBufOut = strlen(bufOut);
 
-         // Set file transfer in progress flag
 
-         fLogTransInprog = true;
 
-         return SEND_REQUIRED_FILE;
 
-      }
 
-      else {
 
-           // We send nothing if file transfer already in progress
 
-           return SEND_REQUIRED_NO;
 
-      }
 
-   }
 
-   else {
 
- 	  if (!LOG_IsInit()) {
 
- 		  return SEND_REQUIRED_NO;
 
- 	  }
 
- 	  else {
 
- 		  HTTP_GetUpsHistoryPage(bufOut, atoi(value));
 
- 		  *lenBufOut = strlen(bufOut);
 
- 		  return SEND_REQUIRED_YES;
 
- 	  }
 
-   }
 
- }
 
-   
 
- /**
 
-   * @brief  Установка даты производства
 
-   */
 
- // TODO Убрать заглушку!
 
- void HTTP_Prodate(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-   uint8_t valueLen = 0;
 
-   char value[20];
 
-   
 
-   memset(bufOut, 0, SEND_BUF_MAX_LEN);
 
-   
 
-   ClearParamString(bufIn);
 
-   
 
-   memset(value, 0, 20);
 
-   GetParamValue(bufIn, "prodate=", value, &valueLen);
 
-   /*
 
-   printf("Prodate: ");
 
-   printf(value);
 
-   printf("\r\n");
 
-   */
 
-   
 
-   /* Устанавливаем дату производства */
 
-   SETTINGS_SetProDate(value, valueLen);
 
-   
 
-   /* Пока отправляем true */
 
-   strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\n\r\nTrue");
 
-   *lenBufOut = strlen(bufOut);
 
-   
 
-   TEST_SetServerFlag();
 
- }
 
- /**
 
-   * @brief  
 
-   * @retval None
 
-   */
 
- void HTTP_SetSettings(char *buf, uint16_t lenBuf)
 
- {
 
-   uint8_t valueLen = 0;
 
-   const uint8_t len = MAX_WEB_PARAM_LEN;
 
-   char value[MAX_WEB_PARAM_LEN];
 
-   char str[MAX_WEB_PARAM_LEN];
 
-   //printf(buf);
 
-   
 
-   //ClearParamString(buf);
 
-   
 
-   memset(value, 0, len);
 
-   memset(str, 0, MAX_WEB_PARAM_LEN);
 
-   
 
-   /* SNMP */
 
-   GetParamValue(buf, "read_community=", value, &valueLen);
 
-   SetReadCommunity(value);
 
-   memset(value, 0, len);
 
-   
 
-   GetParamValue(buf, "write_community=", value, &valueLen);
 
-   SetWriteCommunity(value);
 
-   memset(value, 0, len);
 
-       
 
-   GetParamValue(buf, "managerIP=", value, &valueLen);
 
-   SetManagerIp(value);
 
-   memset(value, 0, len);
 
-   GetParamValue(buf, "managerIP2=", value, &valueLen);
 
-   SetManagerIp2(value);
 
-   memset(value, 0, len);
 
-   GetParamValue(buf, "managerIP3=", value, &valueLen);
 
-   SetManagerIp3(value);
 
-   memset(value, 0, len);
 
-   
 
-   GetParamValue(buf, "managerIP4=", value, &valueLen);
 
-   SetManagerIp4(value);
 
-   memset(value, 0, len);
 
-   GetParamValue(buf, "managerIP5=", value, &valueLen);
 
-   SetManagerIp5(value);
 
-   memset(value, 0, len);
 
-   /* Сетевые параметры */
 
-   GetParamValue(buf, "dhcp=", value, &valueLen);
 
-   SetDhcpStateStr(value);
 
-   
 
-   if (strncmp(value, "on", 2) != 0)  // Если dhcp off устанавливаем параметры
 
-     {
 
-     memset(value, 0, len);
 
-     GetParamValue(buf, "ipaddr=", value, &valueLen);
 
-     SetIPStr(value);
 
-     memset(value, 0, len);
 
-   
 
-     GetParamValue(buf, "gw=", value, &valueLen);
 
-     SetGatewayStr(value);
 
-     memset(value, 0, len);
 
-   
 
-     GetParamValue(buf, "mask=", value, &valueLen);
 
-     SetMaskStr(value);
 
-     memset(value, 0, len);
 
-   }
 
-   memset(value, 0, len);
 
-   GetParamValue(buf, "swauth=", value, &valueLen);
 
-   SetAuthEnableStateStr(value);
 
-   if (strncmp(value, "on", 2) == 0){
 
- 		/* параметры RADIUS*/
 
- 	  memset(value, 0, len);
 
- 		GetParamValue(buf, "rs_enabled=", value, &valueLen);
 
- 		SetRDSEnableStateStr(value);
 
- 		if (strncmp(value, "on", 2) == 0)  // Если raddius off устанавливаем параметры
 
- 		{
 
- 			memset(value, 0, len);
 
- 			GetParamValue(buf, "rs_server=", value, &valueLen);
 
- 			SetRDSIpStr(value);
 
- 			memset(value, 0, len);
 
- 			GetParamValue(buf, "rs_port=", value, &valueLen);
 
- 			SetRDSPortStr(value);
 
- 			memset(value, 0, len);
 
- 			GetParamValue(buf, "rs_pwd=", value, &valueLen);
 
- 			SetRDSPasswordkStr(value);
 
- 			memset(value, 0, len);
 
- 			GetParamValue(buf, "rs_key=", value, &valueLen);
 
- 			SetRDSKeyAccesstStr(value);
 
- 			memset(value, 0, len);
 
- 		}
 
-   }
 
-   memset(value, 0, len);
 
-   // Параметры реле и сухих контактов
 
-   GetParamValue(buf, "di1=", value, &valueLen);
 
-   SetDINTypeActStr(value, 0);
 
-   memset(value, 0, len);
 
-   GetParamValue(buf, "ro1=", value, &valueLen);
 
-   SetROTypeActStr(value, 0);
 
-   memset(value, 0, len);
 
-   GetParamValue(buf, "ro2=", value, &valueLen);
 
-   SetROTypeActStr(value, 1);
 
-   memset(value, 0, len);
 
-   // Параметры даты и времени
 
-   GetParamValue(buf, "ntp=", value, &valueLen);
 
-   SetSntpStateStr(value);
 
-   if (strncmp(value, "1", 1) == 0)  // Если ntp on устанавливаем параметры
 
-   {
 
-     memset(value, 0, len);
 
-     GetParamValue(buf, "ntpservip=", value, &valueLen);
 
-     SetSntpServerIpStr(value);
 
-     memset(value, 0, len);
 
-   }
 
-   else if (strncmp(value, "0", 1) == 0){
 
-       GetParamValue(buf, "date=", value, &valueLen);
 
-       SetDateStr(value);
 
-       memset(value, 0, len);
 
-       GetParamValue(buf, "time=", value, &valueLen);
 
-       url_decode(str, sizeof(str), value);
 
-       SetTimeStr(str);
 
-       memset(value, 0, len);
 
-   }
 
-   GetParamValue(buf, "utc=", value, &valueLen);
 
-   SetSntpTimeZoneStr(value);
 
-   memset(value, 0, len);
 
-   
 
-   /* Если параметры WEB изменились выставляем флаг, сохраняем настройки и перезагружаемся */
 
-   if (GetStateWebReinit() == true)
 
-   {	
 
- 	SetWebReinitFlag(true);
 
- 	HTTP_SaveSettings();
 
-     /* Блокируем управление ключем на тау секунд*/
 
-     //IO_KeyBlockOn();
 
-     vTaskDelay(1010);
 
-     Reboot();
 
-   }	
 
-   
 
-   HTTP_SaveSettings();
 
- }
 
- /**
 
-   * @brief  
 
-   * @retval None
 
-   */
 
- void HTTP_SetInfo(char *buf, uint16_t lenBuf)
 
- {
 
-   uint8_t valueLen = 0;
 
-   const uint8_t len = 110;
 
-   char value[110];
 
-   char str[110];
 
-  // ClearParamString(buf);
 
-   
 
-   memset(value, 0, len);
 
-   
 
-   /* Владелец */
 
-   GetParamValue(buf, "owner=", value, &valueLen);
 
-   url_decode(str, sizeof(str), value);
 
-   SetOwner(str);
 
-   memset(value, 0, len);
 
-   
 
-   /* Владелец */
 
-   GetParamValue(buf, "sysLocation=", value, &valueLen);
 
-   url_decode(str, sizeof(str), value);
 
-   SetLocation(str);
 
-   memset(value, 0, len);
 
-   
 
-   /* Комментарий */
 
-   GetParamValue(buf, "comment=", value, &valueLen);
 
-   url_decode(str, sizeof(str), value);
 
-   SetComment(str);
 
-   memset(value, 0, len);
 
-  
 
-   HTTP_SaveSettings();
 
- }
 
- /**
 
-   * @brief  Запуск/останов теста UPS
 
-   */
 
- void HTTP_UPSTest(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
- 	  uint8_t valueLen = 0;
 
- 	  char tempValue[20];
 
- 	  char tempValue2[20];
 
- 	  int8_t res = 0;
 
- 	  memset(tempValue, 0, 20);
 
- 	  memset(tempValue2, 0, 20);
 
- 	  strcpy(bufOut, HTTP_200_OK);
 
- 	  GetParamValue(bufIn, "func=", tempValue, &valueLen);
 
- 	  if (strcmp(tempValue, "stop") == 0){
 
- 		  res = ups_metac_service_pdu(ups_cancel_test);
 
- 		  if(res == 1 || res == 0){
 
- 			  strcat(bufOut, "Тест остановлен!");
 
- 			  log_event_data(LOG_TEST_UPS, "Администратор(Останов)");
 
- 		  }
 
- 		  if(res == -1)
 
- 			  strcat(bufOut, "Тест не удалось остановить!");
 
- 		  *lenBufOut = strlen(bufOut);
 
- 	  }
 
- 	  else if (strcmp(tempValue, "discharge") == 0){
 
- 		  res = ups_metac_service_pdu(ups_test_low_bat);
 
- 		  if(res == 1 || res == 0){
 
- 			  strcat(bufOut, "Тест запущен!");
 
- 			  log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
 
- 		  }
 
- 		  if(res == -1)
 
- 			  strcat(bufOut, "Тест не удалось запустить!");
 
- 		  *lenBufOut = strlen(bufOut);
 
- 	  }
 
- 	  else if (strncmp(tempValue, "time", 6) == 0){
 
- 		  GetParamValue(bufIn, "=", tempValue2, &valueLen);
 
- 		  TimeParam = atoi(tempValue2);
 
- 		  res = ups_metac_service_pdu(ups_test_time);
 
- 		  if(res == 1 || res == 0){
 
- 			  strcat(bufOut, "Тест запущен!");
 
- 			  log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
 
- 		  }
 
- 		  if(res == -1)
 
- 			  strcat(bufOut, "Тест не удалось запустить!");
 
- 		  *lenBufOut = strlen(bufOut);
 
- 	  }
 
- }
 
- /**
 
-   * @brief  Выклюение UPS
 
-   */
 
- void HTTP_UPSshutdown(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
- 	  uint8_t valueLen = 0;
 
- 	  char *valueLenEnd = 0;
 
- 	  char tempValue[50];
 
- 	  char tempValue2[50];
 
- 	  int8_t res = 0;
 
- 	  memset(tempValue, 0, 50);
 
- 	  strcpy(bufOut, HTTP_200_OK);
 
- 	  GetParamValue(bufIn, "func=", tempValue, &valueLen);
 
- 	  if (strcmp(tempValue, "reboot") == 0){
 
- 		  res = ups_metac_service_pdu(ups_cancel_shut_down);
 
- 		  if(res == 1){
 
- 			  log_event_data(LOG_SHUTDOWN_UPS, "Администратор(Останов)");
 
- 			  strcat(bufOut, "Выключение нагрузки ИБП отменено!");
 
- 		  }
 
- 		  else
 
- 			  strcat(bufOut, "Выключение нагрузки ИБП не удалось отменить!");
 
- 		  *lenBufOut = strlen(bufOut);
 
- 	  }
 
- 	  else if (strncmp(tempValue, "off", 5) == 0){
 
- 		  memset(tempValue2, 0, 50);
 
- 		  GetParamValue(bufIn, "after=", tempValue2, &valueLen);
 
- 		  TimeParamFloat = atof(tempValue2);
 
- 		  res = ups_metac_service_pdu(ups_shutdown);
 
- 		  if(res == 1){
 
- 			  strcat(bufOut, "Отключение нагрузки ИБП!");
 
- 			  log_event_data(LOG_SHUTDOWN_UPS, "Администратор");
 
- 		  }else
 
- 			  strcat(bufOut, "Отключение нагрузки ИБП не удалось!");
 
- 		  *lenBufOut = strlen(bufOut);
 
- 	  }
 
- }
 
- /**
 
-   * @brief  Проверка пароля для перехода в режим bootloader
 
-   * @retval None
 
-   */
 
- void HTTP_ConfirmBootPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-   char tempStr[50];
 
-   strncpy(tempStr, bufIn, 50);
 
-   char value[20];
 
-   uint8_t valueLen;
 
-   
 
-   memset(value, 0, 20);
 
-   
 
-   //if (GetParamValue(tempStr, "password=", value, &valueLen))
 
-   {
 
- 	//if (strcmp(BOOTLOADER_PASWORD, value) == 0)
 
- 	{  
 
- 	 // *bufOut = '1';
 
- 	  /* Запускаем задачу отложенной перезагрузки. Контроллер должен успеть
 
-        отправить ответ серверу о статусе пароля */
 
- 	  HTTP_StartResetTask(true); 
 
- 	}  
 
- /*	else
 
- 	  *bufOut = '0';*/
 
- 	
 
- 	//*lenBufOut = 1;
 
-   }
 
- }
 
- /**
 
-   * @brief  Проверка пароля для входа в Web
 
-   * @retval None
 
-   */
 
- int HTTP_ConfirmWebPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
- 	  char tempStr[50];
 
- 	  char login[20];
 
- 	  char password[20];
 
- 	  uint8_t valueLen, user_id;
 
- 	  char *strPtr = 0;
 
- 	  char WebPassword[MAX_WEB_PASSWD_LEN];
 
- 	  char WebLogin[MAX_WEB_LOGIN_LEN];
 
- 	  char buf[40];
 
- 	  memset(login, 0, 20);
 
- 	  memset(password, 0, 20);
 
- 	  memset(tempStr, 0, 50);
 
- 	  /* Get first 50 bytes of string */
 
- 	  strncpy(tempStr, bufIn, 49);
 
- 	  /* Add " " to the string in order GetParamValue() can be able to parse the param */
 
- 	  strcat(tempStr, " ");
 
- 	  if (GetParamValue(tempStr, "login=", login, &valueLen) &&
 
- 	      GetParamValue(tempStr, "password=", password, &valueLen))
 
- 	  {
 
- 	      for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
 
- 	        GetUserLogin(user_id, WebLogin, &valueLen);
 
- 	        GetUserPassword(user_id, WebPassword, &valueLen);
 
- 	        /* Check login and password */
 
- 	        if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
 
- 	            (strncmp(WebPassword, password, MAX_WEB_PASSWD_LEN) == 0)) {
 
- 	            /* Login and pass are valid */
 
- 	            /* Check user's login session */
 
- 	            /* If "user" has logged in */
 
- 	            if (user_id >= 1) {
 
- 	            }
 
-             /* TODO replace global flag with user-pass-cookie */
 
-             Authenticated = true;
 
-             /* Generate cookie */
 
-             sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
 
-             /* Set users cookie */
 
-             HTTP_SetUserCookie(tempStr, user_id);
 
-             HTTP_UpdateUserLoginTime(user_id);
 
-             /* Send login and cookie back */
 
-             strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
 
-             strcat(bufOut, WebLogin);
 
-             strcat(bufOut, "\r\nSet-Cookie: id=");
 
-             strcat(bufOut, tempStr);
 
-             sprintf(tempStr, "%d", user_id);
 
-             strcat(bufOut, "\r\nSet-Cookie: role=");
 
-             strcat(bufOut, tempStr);
 
-             if(sSettings.sRADIUS.Auth_enable)
 
- 				strcat(bufOut, "\r\nSet-Cookie: auth=1");
 
- 			else
 
- 				strcat(bufOut, "\r\nSet-Cookie: auth=0");
 
-             strcat(bufOut, "\r\n\r\n");
 
-             strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
 
-             *lenBufOut = strlen(bufOut);
 
-             switch (user_id) {
 
-                 case 0:
 
-                     snprintf(buf, sizeof(buf), "Администратор");
 
-                     break;
 
-                 case 1:
 
-                     snprintf(buf, sizeof(buf), "Пользователь");
 
-                     break;
 
-                 default:
 
-                     snprintf(buf, sizeof(buf), "", login);
 
-                     break;
 
-             }
 
-             log_event_data(LOG_LOGIN, buf);
 
-             /* Запускаем задачу-таймер логаута. */
 
-             /* TODO отправить ответ серверу о статусе пароля */
 
-             return SEND_REQUIRED_YES;
 
-         }
 
-         /*
 
-         else {
 
-             continue;
 
-         }
 
-         */
 
-       }
 
-     }
 
-     /* No valid login and pass found */
 
-     /* TODO replace global flag with user-pass-cookie*/
 
-     Authenticated = false;
 
-     /* Wrong login or pass, return */
 
-     return SEND_REQUIRED_NO;
 
- }
 
- void HTTP_LOGIN(char *bufOut, uint16_t *lenBufOut)
 
- {
 
- 	char tempStr[50];
 
- 	uint8_t valueLen;
 
- 	char WebLogin[MAX_WEB_LOGIN_LEN];
 
- 	GetUserLogin(ADMIN, WebLogin, &valueLen);
 
- 	memset(tempStr, 0, 50);
 
- 	/* TODO replace global flag with user-pass-cookie */
 
- 	Authenticated = true;
 
- 	/* Generate cookie */
 
- 	sprintf(tempStr, "%X", (unsigned int)GetRandomNumber());
 
- 	/* Set users cookie */
 
- 	HTTP_SetUserCookie(tempStr, ADMIN);
 
- 	HTTP_UpdateUserLoginTime(ADMIN);
 
- 	/* Send login and cookie back */
 
- 	strcpy(bufOut, "HTTP/1.0 200 OK\r\nContent-Type:text/html\r\nSet-Cookie: uname=");
 
- 	strcat(bufOut, WebLogin);
 
- 	strcat(bufOut, "\r\nSet-Cookie: id=");
 
- 	strcat(bufOut, tempStr);
 
- 	strcat(bufOut, "\r\nSet-Cookie: role=0");
 
- 	if(sSettings.sRADIUS.Auth_enable)
 
- 		strcat(bufOut, "\r\nSet-Cookie: auth=1");
 
- 	else
 
- 		strcat(bufOut, "\r\nSet-Cookie: auth=0");
 
- 	strcat(bufOut, "\r\n\r\n");
 
- 	strcat(bufOut,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/index.html\"/></head></html>\r\n\r\n");
 
- 	*lenBufOut = strlen(bufOut);
 
- }
 
- /**
 
-   * @brief  
 
-   * @retval None
 
-   */
 
- uint8_t GetParamValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
 
- {
 
-   char *beginValue = 0;
 
-   char *endValue = 0;
 
-   int  len = 0;
 
-   char *strPtr = 0;
 
-   
 
-   strPtr = strstr(inStr, paramName);
 
-   
 
-   if (strPtr != 0)
 
-   {
 
-     beginValue = strpbrk(strPtr,"=");
 
-     endValue = strpbrk(strPtr,"&");
 
-     if (endValue == 0)
 
-       endValue = strpbrk(strPtr," ");
 
-     len = endValue - beginValue - 1;
 
-     strncpy(paramValue, beginValue + 1, len);
 
-     *endValue = '0';
 
-     *beginValue = '0';
 
- 	*paramLen = len;
 
- 	return 1;
 
-   }
 
-   else
 
-   {	
 
- 	*paramLen = 0;
 
- 	return 0;
 
-   }
 
- }
 
- /**
 
-   * @brief  
 
-   * @retval None
 
-   */
 
- uint8_t GetCookieValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
 
- {
 
-   char *beginValue = 0;
 
-   char *endValue = 0;
 
-   int  len = 0;
 
-   char *strPtr = 0;
 
-   strPtr = strstr(inStr, paramName);
 
-   if (strPtr != 0)
 
-   {
 
-     beginValue = strpbrk(strPtr,"=");
 
-     endValue = strpbrk(strPtr,";");
 
-     if (endValue == 0)
 
-       endValue = strpbrk(strPtr,"\n");
 
-     len = endValue - beginValue - 1;
 
-     strncpy(paramValue, beginValue + 1, len);
 
-     *endValue = '0';
 
-     *beginValue = '0';
 
-     *paramLen = len;
 
-     return 1;
 
-   }
 
-   else
 
-   {
 
-     *paramLen = 0;
 
-     return 0;
 
-   }
 
- }
 
- /**
 
-   * @brief
 
-   * @retval None
 
-   */
 
- /*
 
- uint8_t GetParamValueInEnd(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen)
 
- {
 
-   char *beginValue = 0;
 
-   char *endValue = 0;
 
-   int  len = 0;
 
-   char *strPtr = 0;
 
-   
 
-   strPtr = strstr(inStr, paramName);
 
-   
 
-   if (strPtr != 0)
 
-   {
 
-     beginValue = strpbrk(strPtr,"=");
 
-     endValue = strpbrk(strPtr," ");
 
-     len = endValue - beginValue - 1;
 
-     strncpy(paramValue, beginValue + 1, len);
 
-     *endValue = '0';
 
-     *beginValue = '0';
 
- 	*paramLen = len;
 
- 	return 1;
 
-   }
 
-   else
 
-   {	
 
- 	*paramLen = 0;
 
- 	return 0;
 
-   }
 
- }
 
- */
 
- void ClearParamString(char *inBuf)
 
- {
 
-   uint16_t len;
 
-   char *str;
 
-   
 
-   str = strstr(inBuf, "HTTP");
 
-   
 
-   if (str != 0)
 
-   {
 
-     len = str - inBuf;
 
- 	memset(str, 0, RECIVE_BUF_MAX_LEN - len - 1);
 
-   }
 
- }
 
- /**
 
-   * @brief Чтение Cookie пользователя
 
-   */
 
- static void HTTP_GetUserCookie(uint8_t user_id, char *str, uint8_t *len)
 
- {
 
-     sprintf(str, "%s", users[user_id].cookie);
 
-     *len = strlen(str);
 
- }
 
- /**
 
-   * @brief Установка Cookie пользователя
 
-   */
 
- static void HTTP_SetUserCookie(char *str, uint8_t user_id)
 
- {
 
-     strcpy(users[user_id].cookie, str);
 
- }
 
- /**
 
-   * @brief Обновление времени последней активности пользователя
 
-   */
 
- static void HTTP_UpdateUserLoginTime(uint8_t user_id)
 
- {
 
-     xTimerStart(users[user_id].LogoutTimer, 0);
 
- }
 
- /**
 
-   * @brief  Extract the Content_Length data from HTML data
 
-   * @param  data : pointer on receive packet buffer
 
-   * @param  len  : buffer length
 
-   * @retval size : Content_length in numeric format
 
-   */
 
- static uint32_t Parse_Content_Length(char *data, uint32_t len)
 
- {
 
-   uint32_t i=0,size=0, S=1;
 
-   int32_t j=0;
 
-   char sizestring[6], *ptr;
 
-   ContentLengthOffset =0;
 
-   /* find Content-Length data in packet buffer */
 
-   for (i=0;i<len;i++)
 
-   {
 
-     if (strncmp ((char*)(data+i), Content_Length, 16)==0)
 
-     {
 
-       ContentLengthOffset = i+16;
 
-       break;
 
-     }
 
-   }
 
-   /* read Content-Length value */
 
-   if (ContentLengthOffset)
 
-   {
 
-     i=0;
 
-     ptr = (char*)(data + ContentLengthOffset);
 
-     while(*(ptr+i)!=0x0d)
 
-     {
 
-       sizestring[i] = *(ptr+i);
 
-       i++;
 
-       ContentLengthOffset++;
 
-     }
 
-     if (i>0)
 
-     {
 
-       /* transform string data into numeric format */
 
-       for(j=i-1;j>=0;j--)
 
-       {
 
-         size += (sizestring[j]-0x30)*S;
 
-         S=S*10;
 
-       }
 
-     }
 
-   }
 
-   return size;
 
- }
 
- /**
 
-   * @brief Принудительный логаут пользователя
 
-   */
 
- static void HTTP_ForceUserLogout(uint8_t user_id)
 
- {
 
-     char cookie[MAX_WEB_COOKIE_LEN];
 
-     /* Flush user cookie by random value */
 
-     sprintf(cookie, "%X", (unsigned int)GetRandomNumber());
 
-     HTTP_SetUserCookie(cookie, user_id);
 
- }
 
- /**
 
-   * @brief >Callback таймера логаута пользователя
 
-   */
 
- void LogoutTimerCallback(TimerHandle_t pxTimer) {
 
-     uint8_t user_id = (uint8_t)pvTimerGetTimerID( pxTimer );
 
- 	if( sSettings.sRADIUS.Auth_enable )
 
- 		HTTP_ForceUserLogout(user_id);
 
- }
 
- /**
 
-   * @brief  Смена пароля пользователя
 
-   * @retval None
 
-   */
 
- int HTTP_ChangeUserPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut)
 
- {
 
-     char tempStr[110];
 
-     char value[20];
 
-     char login[20];
 
-     char password[20];
 
-     uint8_t valueLen, valueLen2, user_id;
 
-     char WebLogin[MAX_WEB_LOGIN_LEN];
 
-     (void)lenBufIn;
 
-     memset(login, 0, 20);
 
-     memset(password, 0, 20);
 
-     memset(tempStr, 0, 50);
 
-     memset(value, 0, 20);
 
-     ClearParamString(bufIn);
 
-     strncpy(tempStr, bufIn, 110);
 
-     strcpy(bufOut, HTTP_200_OK);
 
-     if (GetParamValue(tempStr, "username=", login, &valueLen) &&
 
-         GetParamValue(tempStr, "oldpass=", password, &valueLen))
 
-     {
 
-           for (user_id = 0; user_id < MAX_WEB_USERS; user_id++)
 
-           {
 
-         	  memset(value, 0, 20);
 
- 			  memset(WebLogin, 0, MAX_WEB_LOGIN_LEN);
 
- 			 GetUserLogin(user_id, WebLogin, &valueLen);
 
- 			 GetUserPassword(user_id, value, &valueLen2);
 
- 			 /* Check login and password */
 
- 			 if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
 
- 				 (memcmp(password, value, 11) == 0))
 
- 			 {
 
- 				 memset(password, 0, 20);
 
- 				 if (GetParamValue(tempStr, "newpass=", password, &valueLen))
 
- 				 {
 
- 					 memcpy(sSettings.sAuth[user_id].password, password, 11);
 
- 					 HTTP_SaveSettings();
 
- 					 log_event_data(LOG_PSW_CHANGE, "Администратор");
 
- 					 strcat(bufOut, "Пароль успешно изменён");
 
- 					 *lenBufOut = strlen(bufOut);
 
- 					 return SEND_REQUIRED_YES;
 
- 				 }
 
- 				 else {
 
- 					  strcat(bufOut, "Введены некорректные данные!");
 
- 					  *lenBufOut = strlen(bufOut);
 
- 					  return SEND_REQUIRED_YES;
 
- 				 }
 
- 			 }
 
-           }
 
-           strcat(bufOut, "Введён неверный пароль!");
 
-           *lenBufOut = strlen(bufOut);
 
-           return SEND_REQUIRED_YES;
 
-   }
 
-   else {
 
-       strcat(bufOut, "Введены некорректные данные!");
 
-       *lenBufOut = strlen(bufOut);
 
-       return SEND_REQUIRED_YES;
 
-   }
 
- }
 
- // -----------------------------------------------------------------------------
 
- //
 
- //
 
- //  SSL тесты
 
- //
 
- //
 
- // -----------------------------------------------------------------------------
 
- //#include "stm32f4xx.h"
 
- #include "rtc.h"
 
- /* PolarSSL includes */
 
- #include "polarssl/net.h"
 
- #include "polarssl/ssl.h"
 
- #include "polarssl/havege.h"
 
- #include "polarssl/certs.h"
 
- #include "polarssl/x509.h"
 
- /* Private typedef -----------------------------------------------------------*/
 
- /* Private define ------------------------------------------------------------*/
 
- #define DEBUG_LEVEL 1  /* Set DEBUG_LEVEL if you want to enable SSL debug
 
-                          option, this should be set to 2, 3, 4 or 5 */
 
-                          
 
- #define HTTP_RESPONSE "<p><p>Successful connection using: %s\r\n"
 
- /* Format of dynamic web page */
 
- #define PAGE_START \
 
- "<html>\
 
- <head>\
 
- </head>\
 
- <BODY onLoad=\"window.setTimeout("location.href='index.html'",1000)\" bgcolor=\"#FFFFFF\" text=\"#000000\">\
 
- <font size=\"4\" color=\"#0000FF\"><b>STM32F407xx : SSL Server Demo not using HW Crypto :<)</font></b></i>\
 
- <br><br><pre>\r\nPage Hits = "
 
- #define PAGE_END \
 
- " \r\n</pre><br><br><hr>\
 
- <font size=\"3\" color=\"#808080\">All rights reserved ©2017 ROTEK\
 
- \r\n</font></BODY>\
 
- </html>"
 
- /* Private macro -------------------------------------------------------------*/
 
- /* Private variables ---------------------------------------------------------*/
 
- /* SSL structures */
 
- rng_state rngs;
 
- ssl_context ssl;
 
- ssl_session ssn;
 
- x509_cert srvcert;
 
- rsa_context rsa;
 
- uint32_t nPageHits = 0;
 
- /* Private functions ---------------------------------------------------------*/
 
- /*
 
-  * Computing a "safe" DH-1024 prime can take a very
 
-  * long time, so a precomputed value is provided below.
 
-  * You may run dh_genprime to generate a new value.
 
-  */
 
- char *my_dhm_P = 
 
-   "E4004C1F94182000103D883A448B3F80" \
 
-   "2CE4B44A83301270002C20D0321CFD00" \
 
-   "11CCEF784C26A400F43DFB901BCA7538" \
 
-   "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
 
-   "F6AC8E1DA6BCC3B4E1F96B0564965300" \
 
-   "FFA1D0B601EB2800F489AA512C4B248C" \
 
-   "01F76949A60BB7F00A40B1EAB64BDD48" \
 
-   "E8A700D60B7F1200FA8E77B0A979DABF";
 
- char *my_dhm_G = "4";
 
- /*
 
-  * Sorted by order of preference
 
-  */
 
- int my_ciphersuites[] =
 
- {
 
-   SSL_EDH_RSA_AES_256_SHA,
 
-   SSL_EDH_RSA_CAMELLIA_256_SHA,
 
-   SSL_EDH_RSA_AES_128_SHA,
 
-   SSL_EDH_RSA_CAMELLIA_128_SHA,
 
-   SSL_EDH_RSA_DES_168_SHA,
 
-   SSL_RSA_AES_256_SHA,
 
-   SSL_RSA_CAMELLIA_256_SHA,
 
-   SSL_RSA_AES_128_SHA,
 
-   SSL_RSA_CAMELLIA_128_SHA,
 
-   SSL_RSA_DES_168_SHA,
 
-   SSL_RSA_RC4_128_SHA,
 
-   SSL_RSA_RC4_128_MD5,
 
-   0
 
- };
 
- void my_debug(void *ctx, int level, const char *str)
 
- {
 
-   if(level < DEBUG_LEVEL)
 
-   {
 
-     printf("\r%s", str);  
 
-   }
 
- }
 
- /*
 
-  * These session callbacks use a simple chained list
 
-  * to store and retrieve the session information.
 
-  */
 
- ssl_session *s_list_1st = NULL;
 
- ssl_session *cur, *prv;
 
- static int my_get_session(ssl_context *ssl)
 
- {
 
-   time_t t = RTC_GetUnixTime(); //time(NULL);
 
-   if(ssl->resume == 0)
 
-     return(1);
 
-   cur = s_list_1st;
 
-   prv = NULL;
 
-   while(cur != NULL)
 
-   {
 
-     prv = cur;
 
-     cur = cur->next;
 
-     if(ssl->timeout != 0 && t - prv->start > ssl->timeout)
 
-       continue;
 
-     if( ssl->session->ciphersuite != prv->ciphersuite ||
 
-         ssl->session->length != prv->length)
 
-       continue;
 
-     if(memcmp( ssl->session->id, prv->id, prv->length ) != 0)
 
-       continue;
 
-     memcpy(ssl->session->master, prv->master, 48);
 
-       return(0);
 
-   }
 
-   return(1);
 
- }
 
- static int my_set_session(ssl_context *ssl)
 
- {
 
-   time_t t = RTC_GetUnixTime(); //time(NULL);
 
-   cur = s_list_1st;
 
-   prv = NULL;
 
-   while(cur != NULL)
 
-   {
 
-     if(ssl->timeout != 0 && t - cur->start > ssl->timeout)
 
-       break; /* expired, reuse this slot */
 
-     if(memcmp( ssl->session->id, cur->id, cur->length ) == 0)
 
-       break; /* client reconnected */
 
-     prv = cur;
 
-     cur = cur->next;
 
-   }
 
-   if(cur == NULL)
 
-   {
 
-     cur = (ssl_session *) malloc(sizeof(ssl_session));
 
-     if(cur == NULL)
 
-       return(1);
 
-     if(prv == NULL)
 
-       s_list_1st = cur;
 
-     else  prv->next  = cur;
 
-   }
 
-   memcpy(cur, ssl->session, sizeof(ssl_session));
 
-   return(0);
 
- }
 
- /**
 
-   * @brief  SSL Server task.
 
-   * @param  pvParameters not used
 
-   * @retval None
 
-   */
 
- void ssl_server(void *pvParameters)
 
- {
 
-     int ret, len;
 
-     int listen_fd;
 
-     int client_fd;
 
-     char buf[1024];
 
-     
 
-     char *ptr;
 
-     char CookieBuf[50];
 
-     char *CookiePtr = NULL;
 
-     char name[MAX_WEB_COOKIE_LEN];
 
-     char id[MAX_WEB_COOKIE_LEN];
 
-     uint8_t nameLen = 0, idLen = 0;
 
-     uint32_t DataOffset;
 
-     struct fs_file file = {0, 0};
 
-     uint32_t i;
 
-     
 
-     memset(&srvcert, 0, sizeof(x509_cert));
 
-     
 
-     // 1. Load the certificates and private RSA key 
 
-     //printf("\n\r Loading the server certificates and key...");
 
-     
 
-   
 
-     // This demonstration program uses embedded test certificates.
 
-     // Instead, you may want to use x509parse_crtfile() to read the
 
-     // server and CA certificates, as well as x509parse_keyfile().
 
-  
 
-     ret = x509parse_crt(&srvcert, (unsigned char *) test_srv_crt, strlen(test_srv_crt));
 
-     if(ret != 0)
 
-     {
 
-         //printf(" failed\n  !  x509parse_crt returned %d\n\r", ret);
 
-         goto exit;
 
-     }
 
-     ret = x509parse_crt(&srvcert, (unsigned char *) test_ca_crt, strlen(test_ca_crt));
 
-     if(ret != 0)
 
-     {
 
-         //printf(" failed\n  !  x509parse_crt returned %d\n\r", ret);
 
-         goto exit;
 
-     }
 
-     rsa_init( &rsa, RSA_PKCS_V15, 0 );
 
-     ret =  x509parse_key(&rsa, (unsigned char *) test_srv_key, strlen(test_srv_key), NULL, 0);
 
-     if( ret != 0 )
 
-     {
 
-         //printf(" failed\n  !  x509parse_key returned %d\n\r", ret);
 
-         goto exit;
 
-     }
 
-     
 
-     // 2. Setup the listening TCP socket 
 
-     //printf("\n\r Bind to https port ...");
 
-   
 
-     // Bind the connection to https port : 443 
 
-     ret = net_bind(&listen_fd, NULL, 443); 
 
-     if(ret != 0)
 
-     {
 
-         //printf(" failed\n  ! net_bind returned %d\n\r", ret);
 
-         goto exit;
 
-     }
 
-     //printf(" ok\n\r");
 
-     
 
-     // 3. Wait until a client connects 
 
-     for(;;)
 
-     {
 
-         //printf("\n\r Waiting for a remote connection ...");
 
-         ret = net_accept(listen_fd, &client_fd, NULL);
 
-         if(ret != 0)
 
-         {
 
-             //printf(" failed\n  ! net_accept returned %d\n\n", ret);
 
-             goto exit;
 
-         }
 
-         //printf(" ok\n");
 
-         // 4. Initialize the session data 
 
-         //printf("\n\r Setting up the RNG and SSL data....");
 
-     
 
-         // Initialize the SSL context 
 
-         ret = ssl_init(&ssl);
 
-         if(ret != 0)
 
-         {
 
-             //printf(" failed\n  ! ssl_init returned %d\n\n", ret);
 
-             goto accept;
 
-         }
 
-         //printf(" ok\n");
 
-         // Set the current session as SSL server 
 
-         ssl_set_endpoint(&ssl, SSL_IS_SERVER);
 
-         // No certificate verification 
 
-         ssl_set_authmode(&ssl, SSL_VERIFY_NONE);
 
-         // Set the random number generator callback function 
 
-         ssl_set_rng(&ssl, RandVal, &rngs); 
 
-         // Set the debug callback function 
 
-         //ssl_set_dbg(&ssl, my_debug, stdout);
 
-         // Set read and write callback functions 
 
-         ssl_set_bio(&ssl, net_recv, &client_fd, net_send, &client_fd);
 
-         // Set the session callback functions 
 
-         ssl_set_scb(&ssl, my_get_session, my_set_session);
 
-         // The list of ciphersuites to be used in this session 
 
-         ssl_set_ciphersuites(&ssl, my_ciphersuites);
 
-         // Set the session resuming flag, timeout and session context 
 
-         ssl_set_session(&ssl, 1, 0, &ssn);
 
-         memset(&ssn, 0, sizeof(ssl_session));
 
-         // Set the data required to verify peer certificate 
 
-         ssl_set_ca_chain(&ssl, srvcert.next, NULL, NULL);
 
-         // Set own certificate and private key 
 
-         ssl_set_own_cert(&ssl, &srvcert, &rsa);
 
-         // Set the Diffie-Hellman public P and G values 
 
-         ssl_set_dh_param(&ssl, my_dhm_P, my_dhm_G);
 
-         // 5. Handshake protocol 
 
-         //printf("\n\r Performing the SSL/TLS handshake...");
 
-         // Perform the SSL handshake protocol 
 
-         while((ret = ssl_handshake(&ssl)) != 0)
 
-         {
 
-             if(ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE)
 
-             {
 
-                 //printf(" failed\n  ! ssl_handshake returned %d\n\n", ret);
 
-                 goto accept;
 
-             }
 
-         }
 
-         //printf(" ok\n");
 
-         // 6. Read the HTTP Request 
 
-         //printf("\n\r <= Read from client :");
 
-         do
 
-         {
 
-             receivedBufLen = RECIVE_BUF_MAX_LEN - 1;
 
-             memset(receiveBuf, 0, RECIVE_BUF_MAX_LEN);
 
-             // Read decrypted application data 
 
-             ret = ssl_read(&ssl, (unsigned char*)receiveBuf, receivedBufLen);
 
-             if(ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE)
 
-                 continue;
 
-             if(ret <= 0)
 
-             {
 
-                 switch(ret)
 
-                 {
 
-                 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
 
-                     printf("\n\r connection was closed \n");
 
-                 break;
 
-                 case POLARSSL_ERR_NET_CONN_RESET:
 
-                     printf("\n\r connection was reset by peer\n");
 
-                 break;
 
-                 default:
 
-                     //printf("\n\r ssl_read returned %d\n", ret);
 
-                 break;
 
-                 }
 
-                 break;
 
-             }
 
-             receivedBufLen = ret;
 
-             // Display the length of read data 
 
-             //printf("\n\r Successfully read %d bytes from client \n\r",len);
 
-         }while(0);
 
-     // -------------------------------------------------------------------------
 
-         
 
-         //receivedBufLen = p->tot_len;
 
-         //memcpy(receiveBuf, p->payload , receivedBufLen);
 
-         receiveBuf[receivedBufLen] = '\0';
 
-         // printf("receive %s \r\n", receiveBuf);
 
-         // Get cookie "uname" value 
 
-         CookiePtr = strstr(receiveBuf, "uname=");
 
-         strncpy(CookieBuf, CookiePtr, 50);
 
-         //printf("********CookieBuf1= %s\r\n", CookieBuf);
 
-         memset(name, 0, MAX_WEB_COOKIE_LEN);
 
-         GetCookieValue(CookieBuf, "uname=", name, &nameLen);
 
-         //printf("********CookieBuf2= %s\r\n", CookieBuf);
 
-         //printf("********uname= %s\r\n", name);
 
-         // Get cookie "id" value 
 
-         CookiePtr = strstr(receiveBuf, "id=");
 
-         strncpy(CookieBuf, CookiePtr, 50);
 
-         //printf("********CookieBuf1= %s\r\n", CookieBuf);
 
-         memset(id, 0, MAX_WEB_COOKIE_LEN);
 
-         GetCookieValue(CookieBuf, "id=", id, &idLen);
 
-         //printf("********CookieBuf2= %s\r\n", CookieBuf);
 
-         //printf("********id= %s\r\n", id);
 
-         // Id of currently logged-in user 
 
-         uint8_t user_id;
 
-         
 
-         if( DataFlag == 0 && DataFlag2 == 0) {
 
-         // Level of currently logged-in user 
 
-         seclevel = 0xFF;
 
-         for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
 
-             HTTP_GetUserCookie(user_id, CookieBuf, &idLen);
 
-             if (strncmp(id, CookieBuf, idLen) == 0 ) {
 
-                 GetUserLevelInt(user_id, &seclevel);
 
-                 Authenticated = true;
 
-                 break;
 
-             }
 
-             Authenticated = false;
 
-             seclevel = 0xFF;
 
-         }
 
-         }
 
-         if (DataFlag >= 1)
 
-             Authenticated = true;
 
-         else if(DataFlag2 >= 1)
 
-             Authenticated = true;
 
-        
 
-         if ( Authenticated == false && sSettings.sRADIUS.Auth_enable == false)
 
-         {
 
-             HTTP_LOGIN(sendBuf, &sendBufLoadLen);
 
-             ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-         }
 
-         else if ( Authenticated == false )//&& sSettings.sRADIUS.Auth_enable == true
 
-         {
 
-             if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
 
-             {
 
-                 fs_open("/main.css", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
 
-             {
 
-                 fs_open("/rotek.png", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?
 
-             {
 
-                 fs_open("/favicon.ico", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
 
-             {
 
-                 fs_open("/role.js", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if ((strncmp(receiveBuf, "POST /login.cgi", 15) == 0) || (log_post_reqn > 0))
 
-             {
 
-                 uint32_t i, offset = 0, req_data_received = 0;
 
-                 
 
-                 post_data_count = Parse_Content_Length(receiveBuf, receivedBufLen);
 
- 			  
 
-                 if (post_data_count < MAX_POST_REQ_LEN) 
 
-                 {
 
-                     memset(post_req_data, 0, MAX_POST_REQ_LEN);
 
- 					
 
- 					for (i = 0; i < receivedBufLen; i++)
 
- 					{
 
- 						if (strncmp ((char*)(receiveBuf+i), "\r\n\r\n", 4) == 0)
 
- 						{
 
- 							offset = i+4;
 
- 							break;
 
- 						}
 
- 					}
 
- 					req_data_received = receivedBufLen - offset;
 
- 					
 
- 					if (offset != 0) 
 
-                     {
 
-                         if (req_data_received < post_data_count) 
 
-                         {
 
-                             snprintf(post_req_data, req_data_received, "%s", receiveBuf);
 
-                             post_data_count -= req_data_received;
 
-                         }
 
-                         else 
 
-                         {
 
-                             strncat(post_req_data, (char *)(receiveBuf + offset), post_data_count);
 
- 						
 
-                             if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) 
 
-                             {
 
-                                 ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                             }
 
-                             else 
 
-                             {
 
-                                 fs_open("/login.html", &file);
 
-                                 ssl_sendframes(&ssl, file.data, file.len);
 
-                             }
 
-                             post_data_count = 0;
 
-                             log_post_reqn = 0;
 
-                         }
 
- 					}
 
- 					/* request was fragmented before "\r\n\r\n" */
 
- 					else 
 
-                     {
 
- 						log_post_reqn++;
 
- 						/* wait max 2 requests */
 
- 						if (log_post_reqn > 1) 
 
-                         {
 
- 							/* Redirect to login page */
 
- 							fs_open("/login.html", &file);
 
-                             ssl_sendframes(&ssl, file.data, file.len);
 
- 							/* End reqest */
 
- 							post_data_count = 0;
 
- 							log_post_reqn = 0;
 
- 						}
 
- 					}
 
-                 }
 
-                 else 
 
-                 {
 
-                     //printf("Too long POST request!\r\n");
 
-                     /* Ignore request */
 
-                     post_data_count = 0;
 
-                     log_post_reqn = 0;
 
-                     /* Redirect to login page */
 
-                     fs_open("/login.html", &file);
 
-                     ssl_sendframes(&ssl, file.data, file.len);
 
-                 }
 
-             }
 
- 			else if (post_data_count > 0)
 
- 			{
 
-                 strncat(post_req_data, receiveBuf, post_data_count);
 
-                 if (HTTP_ConfirmWebPwd(post_req_data, sendBuf, strlen(post_req_data), &sendBufLoadLen) == SEND_REQUIRED_YES) 
 
-                 {
 
-                     ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                 }
 
- 				else 
 
-                 {
 
- 					fs_open("/login.html", &file);
 
-                     ssl_sendframes(&ssl, file.data, file.len);
 
- 				}
 
-                 post_data_count = 0;
 
- 				log_post_reqn = 0;
 
- 			}
 
-             else
 
-             {
 
-                 fs_open("/login.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
- 			}
 
-         }
 
-         else if ( Authenticated == true ) 
 
-         {
 
-             if (strncmp(receiveBuf, "GET /main.css", 13) == 0) // +
 
-             {
 
-                 fs_open("/main.css", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /rotek.png", 14) == 0) // +
 
-             {
 
-                 fs_open("/rotek.png", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /favicon.ico", 16) == 0) // ?                 
 
-             {                                                                     
 
-                 fs_open("/favicon.ico", &file);                                     
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /main.js", 12) == 0) // +
 
-             {
 
-                 fs_open("/main.js", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /role.js", 12) == 0)
 
-             {
 
-                 fs_open("/role.js", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /settings.html", 18) == 0) // +
 
-             {
 
-                 HTTP_UpdateUserLoginTime(user_id);
 
-                 if (seclevel == 0)
 
-                 {
 
-                     fs_open("/settings.html", &file);
 
-                     ssl_sendframes(&ssl, file.data, file.len);
 
-                 }
 
-                 else 
 
-                 {
 
-                     fs_open("/index.html", &file);
 
-                     ssl_sendframes(&ssl, file.data, file.len);
 
- 				}
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /info.html", 14) == 0) // +
 
-             {
 
-                 HTTP_UpdateUserLoginTime(user_id);
 
-                 fs_open("/info.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /history.html", 17) == 0)
 
-             {
 
-                 HTTP_UpdateUserLoginTime(user_id);
 
-                 fs_open("/history.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /ups_history.html", 21) == 0)
 
-             {
 
-                 HTTP_UpdateUserLoginTime(user_id);
 
-                 fs_open("/ups_history.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /getJson.cgi", 16) == 0) // +
 
-             {
 
-                 HTTP_GetParamsPage1(sendBuf);
 
-                 ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-             }	  
 
-             else if (strncmp(receiveBuf, "GET /settings.cgi", 17) == 0) // +
 
-             {
 
-                 if (seclevel == 0) {
 
-                     SET_PAGE = SET_PAGE_PAGE2;
 
-                     if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
 
-                     {
 
-                         ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                     }
 
-                 }
 
-             }
 
-             else if (strncmp(receiveBuf, "POST /settings.cgi", 18) == 0)
 
-             {
 
-                 if (seclevel == 0) 
 
-                 {
 
-                     DataOffset = 0;
 
-                     // POST Packet received 
 
-                     if (DataFlag2 == 0)
 
-                     {
 
-                         BrowserFlag = 0;
 
-                         TotalReceived = 0;
 
-                         memset(sendBuf, 0, strlen(sendBuf));
 
-                         // parse packet for Content-length field 
 
-                         size = Parse_Content_Length(receiveBuf, receivedBufLen);
 
-                         // parse packet for the octet-stream field 
 
-                         for (i = 0; i < receivedBufLen; i++)
 
-                         {
 
-                             if (strncmp ((char*)(receiveBuf+i), "managerIP", 8)==0)
 
-                             {
 
-                                 DataOffset = i;
 
-                                 break;
 
-                             }
 
-                         }     
 
-                         /* case of MSIE8 : we do not receive data in the POST packet*/
 
-                         if (DataOffset == 0)
 
-                         {
 
-                             DataFlag2++;
 
-                             BrowserFlag = 1;
 
-                             //pbuf_free(p);
 
-                             return;
 
-                         }
 
-                         // case of Mozilla Firefox v3.6 : we receive data in the POST packet
 
-                         else
 
-                         {
 
-                             //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
 
-                             TotalReceived = receivedBufLen - DataOffset;
 
-                         }
 
-                     }
 
-                     if (((DataFlag2 ==1)&&(BrowserFlag==1)) || ((DataFlag2 ==0)&&(BrowserFlag==0)))
 
-                     {
 
-                         if ((DataFlag2 ==0)&&(BrowserFlag==0))
 
-                         {
 
-                             DataFlag2++;
 
-                         }
 
-                         else if ((DataFlag2 ==1)&&(BrowserFlag==1))
 
-                         {
 
-                         // parse packet for the octet-stream field */
 
-                             for (i = 0; i < receivedBufLen; i++)
 
-                             {
 
-                                 if (strncmp ((char*)(receiveBuf+i), "managerIP", 8)==0)
 
-                                 {
 
-                                     DataOffset = i;
 
-                                     break;
 
-                                 }
 
-                             }
 
-                             TotalReceived += receivedBufLen;
 
-                             DataFlag2++;
 
-                         }
 
-                         TotalData = 0;
 
-                     }
 
-                     // DataFlag >1 => the packet is data only  
 
-                     else
 
-                     {
 
-                         TotalReceived +=receivedBufLen;
 
-                     }
 
-                     ptr = (char*)(receiveBuf + DataOffset);
 
-                     receivedBufLen-= DataOffset;
 
-                     // update Total data received counter 
 
-                     TotalData +=receivedBufLen;
 
-                     // check if last data packet 
 
-                     if (TotalReceived == size)
 
-                     {
 
-                         //DBG printf("State: Received %d bytes\r\n", (int)TotalReceived);
 
-                         strncat(sendBuf,  ptr, receivedBufLen);
 
-                         strncat(sendBuf,  " ", 1);
 
-                         //ВBG printf("receive %s /r/n", sendBuf);
 
-                         HTTP_SetSettings(sendBuf, strlen(sendBuf));
 
-                         DataFlag2=0;
 
-                         BrowserFlag = 0;
 
-                         memset(sendBuf, 0, size);
 
-                         strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
 
-                         strcat(sendBuf, "\r\n\r\n");
 
-                         strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/settings.html\"/></head></html>\r\n\r\n");
 
-                         sendBufLoadLen = strlen(sendBuf);
 
-                         ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                     }
 
-                     // not last data packet 
 
-                     else
 
-                     {
 
-                         // write data in flash 
 
-                         if(receivedBufLen)
 
-                         {
 
-                             strncat(sendBuf,  ptr, receivedBufLen);
 
-                             //memcpy(receiveBufTemp, ptr, receivedBufLen);
 
-                         }
 
-                     }
 
-                 }  
 
-             } 
 
-             else if (strncmp(receiveBuf, "GET /info.cgi", 13) == 0) // +
 
-             {
 
-                 if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
 
-                 {
 
-                     ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                 }
 
-             }
 
-             else if (strncmp(receiveBuf, "POST /info.cgi", 14) == 0)
 
-             {
 
-                 if (seclevel == 0) 
 
-                 {
 
-                     DataOffset = 0;
 
-                     // POST Packet received 
 
-                     if (DataFlag == 0)
 
-                     {
 
-                         BrowserFlag = 0;
 
-                         TotalReceived = 0;
 
-                         memset(sendBuf, 0, strlen(sendBuf));
 
-                         // parse packet for Content-length field 
 
-                         size = Parse_Content_Length(receiveBuf, receivedBufLen);
 
-                         // parse packet for the octet-stream field 
 
-                         for (i = 0; i < receivedBufLen; i++)
 
-                         {
 
-                             if (strncmp ((char*)(receiveBuf+i), "owner", 5)==0)
 
-                             {
 
-                                 DataOffset = i;
 
-                                 break;
 
-                             }
 
-                         }
 
-                         // case of MSIE8 : we do not receive data in the POST packet
 
-                         if (DataOffset == 0)
 
-                         {
 
-                             DataFlag++;
 
-                             BrowserFlag = 1;
 
-                             //pbuf_free(p);
 
-                             return;
 
-                         }
 
-                         // case of Mozilla Firefox v3.6 : we receive data in the POST packet*/
 
-                         else
 
-                         {
 
-                             //TotalReceived = receivedBufLen - (ContentLengthOffset + 4);
 
-                             TotalReceived = receivedBufLen - DataOffset;
 
-                         }
 
-                     }
 
-                     if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0)))
 
-                     {
 
-                         if ((DataFlag ==0)&&(BrowserFlag==0))
 
-                         {
 
-                             DataFlag++;
 
-                         }
 
-                         else if ((DataFlag ==1)&&(BrowserFlag==1))
 
-                         {
 
-                             // parse packet for the octet-stream field */
 
-                             for (i = 0; i < receivedBufLen; i++)
 
-                             {
 
-                                 if (strncmp ((char*)(receiveBuf+i), "owner", 5)==0)
 
-                                 {
 
-                                     DataOffset = i;
 
-                                     break;
 
-                                 }
 
-                             }
 
-                             TotalReceived += receivedBufLen;
 
-                             DataFlag++;
 
-                         }
 
-                         TotalData =0 ;
 
-                     }
 
-                     // DataFlag >1 => the packet is data only  
 
-                     else
 
-                     {
 
-                         TotalReceived +=receivedBufLen;
 
-                     }
 
-     
 
-                     ptr = (char*)(receiveBuf + DataOffset);
 
-                     receivedBufLen-= DataOffset;
 
-                     // update Total data received counter 
 
-                     TotalData +=receivedBufLen;
 
-                     // check if last data packet 
 
-                     if (TotalReceived == size)
 
-                     {
 
-                         strncat(sendBuf,  ptr, receivedBufLen);
 
-                         strncat(sendBuf,  " ", 1);
 
-                         HTTP_SetInfo(sendBuf, strlen(sendBuf));
 
-                         DataFlag = 0;
 
-                         BrowserFlag = 0;
 
-                         memset(sendBuf, 0, size);
 
-                         strcpy(sendBuf, "HTTP/1.1 200 OK\r\n");
 
-                         strcat(sendBuf, "\r\n\r\n");
 
-                         strcat(sendBuf,"<!DOCTYPE html><html lang=""><head><meta http-equiv=\"refresh\" content=\"0;url=/info.html\"/></head></html>\r\n\r\n");
 
-                         sendBufLoadLen = strlen(sendBuf);
 
-                         ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                     }
 
-                     // not last data packet 
 
-                     else
 
-                     {
 
-                         // write data in flash 
 
-                         if(receivedBufLen)
 
-                         {
 
-                             strncat(sendBuf,  ptr, receivedBufLen);
 
-                             //memcpy(receiveBufTemp, ptr, receivedBufLen);
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /history.cgi", 16) == 0)
 
-             {
 
-                 int res;
 
-                 res = HTTP_HistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 if (res == SEND_REQUIRED_FILE)
 
-                 {
 
-                     ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                     
 
-                     //send_data(pcb, hs);
 
-                     //tcp_sent(pcb, http_sent_history);
 
-                     HTTP_SendHistory();
 
-                     //tcp_err(pcb, http_sent_log_err);
 
-                 }
 
-                 else if (res == SEND_REQUIRED_YES) 
 
-                 {
 
-                     ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                 }
 
-             }
 
-             else if (strncmp(receiveBuf, "GET /ups_history.cgi", 19) == 0)
 
-             {
 
-                 int res;
 
-                 res = HTTP_UpsHistoryPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 if (res == SEND_REQUIRED_FILE)
 
-                 {
 
-                     ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                     
 
-                     //send_data(pcb, hs);
 
-                     //tcp_sent(pcb, http_sent_log);
 
-                     HTTP_SendLog();
 
-                     //tcp_err(pcb, http_sent_log_err);
 
-                 }
 
-                 else if (res == SEND_REQUIRED_YES) 
 
-                 {
 
-                     ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-                 }
 
-             }
 
-             /* Тест  АКБ ИБП */
 
-             else if (strncmp(receiveBuf, "GET /bat_test.cgi", 17) == 0)
 
-             {
 
-                 HTTP_UPSTest(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-             }
 
-             /* Выключение ИБП */
 
-             else if (strncmp(receiveBuf, "GET /ups_power.cgi", 18) == 0)
 
-             {
 
-                 HTTP_UPSshutdown(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-             }
 
-             /* Сброс настроек и сохранине */
 
-             else if (strncmp(receiveBuf, "GET /reset.cgi", 14) == 0)
 
-             {
 
-                 HTTP_ResetSettings();
 
-                 HTTP_SaveSettings();
 
-                 fs_open("/settings.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             /* Перезагрузка контроллера */
 
-             else if (strncmp(receiveBuf, "GET /reboot.cgi", 15) == 0)
 
-             {
 
-                 HTTP_Reboot();
 
-             }
 
-             /* Подтверждение новых сетевых настроек */
 
-             else if (strncmp(receiveBuf, "GET /confirm.cgi", 16) == 0)
 
-             {
 
-                 SetWebReinitFlag(false);
 
-                 SetConfirmWebParamsFlag();
 
-                 fs_open("/index.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             /* Проверка пароля, переход в bootloader */
 
-             else if (strncmp(receiveBuf, "GET /fw_update.cgi", 18) == 0)
 
-             {
 
-                 HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-             }
 
-             /* Смена пароля пользователя */
 
-             else if (strncmp(receiveBuf, "GET /changepwd.cgi", 18) == 0)
 
-             {
 
-                 HTTP_ChangeUserPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-             // На производстве
 
-             else if (strncmp(receiveBuf, "GET /setProdate.cgi", 19) == 0)
 
-             {
 
-                 HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-                 ssl_sendframes(&ssl, sendBuf, sendBufLoadLen);
 
-             }
 
-             else
 
-             {
 
-                 HTTP_UpdateUserLoginTime(user_id);
 
-                 fs_open("/index.html", &file); // +
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-         }
 
-     //}
 
-         
 
-             
 
-             
 
-         
 
-         /*
 
-         if (strncmp(buf, "GET /main.css", 13) == 0) // +
 
-         {
 
-             fs_open("/main.css", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         
 
-         else if (strncmp(buf, "GET /rotek.png", 14) == 0) // +
 
-         {
 
-             fs_open("/rotek.png", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         else if (strncmp(buf, "GET /favicon.ico", 16) == 0) // ?                 
 
-         {                                                                     
 
-             fs_open("/favicon.ico", &file);                                     
 
-             ssl_sendframes(&ssl, file.data, file.len);                                          
 
-         }
 
-         else if (strncmp(buf, "GET /main.js", 12) == 0) // +
 
-         {
 
-             fs_open("/main.js", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         else if (strncmp(buf, "GET /getJson.cgi", 16) == 0) // +
 
-         {
 
-             HTTP_GetParamsPage1(sendBuf);
 
-             ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-         }	
 
-         else if (strncmp(buf, "GET /settings.html", 18) == 0) // +
 
-         {
 
-             fs_open("/settings.html", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         else if (strncmp(buf, "GET /info.html", 14) == 0) // +
 
-         {
 
-             fs_open("/info.html", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         else if (strncmp(buf, "GET /getJson.cgi", 16) == 0) // +
 
-         {
 
-             HTTP_GetParamsPage1(sendBuf);
 
-             ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-         }	  
 
-         else if (strncmp(buf, "GET /settings.cgi", 17) == 0) // +
 
-         {
 
-             SET_PAGE = SET_PAGE_PAGE2;
 
- 		
 
-             if (HTTP_SettingsPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
 
-             {
 
-                 ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-             }
 
-             else
 
-             {
 
-                 fs_open("/settings.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-         }
 
-         else if (strncmp(buf, "GET /info.cgi", 13) == 0) // +
 
-         {
 
-             if (HTTP_InfoPage(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen) == SEND_REQUIRED_YES)
 
-             {
 
-                 ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-             }
 
-             else
 
-             {
 
-                 fs_open("/info.html", &file);
 
-                 ssl_sendframes(&ssl, file.data, file.len);
 
-             }
 
-         }
 
-         // Сброс настроек и сохранине 
 
-         else if (strncmp(buf, "GET /reset.cgi", 14) == 0)
 
-         {
 
-             HTTP_ResetSettings();
 
-             HTTP_SaveSettings();
 
- 		
 
-             fs_open("/settings.html", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         // Перезагрузка контроллера 
 
-         else if (strncmp(buf, "GET /reboot.cgi", 15) == 0)
 
-         {
 
-             HTTP_Reboot();
 
-         }	
 
-         // Подтверждение новых сетевых настроек 
 
-         else if (strncmp(buf, "GET /confirm.cgi", 16) == 0)
 
-         {
 
-             SetWebReinitFlag(false);
 
-             SetConfirmWebParamsFlag();
 
- 		
 
-             fs_open("/index.html", &file);
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-         // Проверка пароля, переход в bootloader 
 
-         else if (strncmp(buf, "POST /checkpwd.cgi", 18) == 0)
 
-         {
 
-             HTTP_ConfirmBootPwd(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-             ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-         }	
 
-         // На производстве
 
-         else if (strncmp(buf, "GET /setProdate.cgi", 19) == 0)
 
-         {
 
-             HTTP_Prodate(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-             ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-         }
 
-         // На производстве
 
-         else if (strncmp(buf, "GET /progon.cgi", 15) == 0)
 
-         {
 
-             HTTP_Progon(receiveBuf, sendBuf, receivedBufLen, &sendBufLoadLen);
 
-             ssl_sendframes(&ssl, sendBuf, strlen(sendBuf));
 
-         }
 
-         else
 
-         {
 
-             fs_open("/index.html", &file); 
 
-             ssl_sendframes(&ssl, file.data, file.len);
 
-         }
 
-     // -------------------------------------------------------------------------
 
-     */
 
-     
 
-     // Для теста
 
-     
 
-     // 7. Write the Response 
 
-     //printf("\n\r => Write to client :\n\r");
 
-     // Send the dynamic html page 
 
-     //ssl_DynPage(&ssl);
 
-         
 
-     // Close the connection 
 
-     ssl_close_notify(&ssl);
 
-     goto accept;
 
- exit:
 
-     // Close and delete the current session data: certificate, RSA key and SSL session 
 
-     x509_free(&srvcert);
 
-     rsa_free(&rsa);
 
-     cur = s_list_1st;
 
-     while(cur != NULL)
 
-     {
 
-       prv = cur;
 
-       cur = cur->next;
 
-       memset(prv, 0, sizeof(ssl_session));
 
-       free(prv);
 
-     }
 
-     memset(&ssl, 0, sizeof(ssl_context));
 
- accept:
 
-     // Wait 200s until next retry 
 
-     vTaskDelay(200); 
 
-     // Close and free the SSL context 
 
-     net_close(client_fd);
 
-     ssl_free(&ssl);
 
-   }
 
- }           
 
- /**
 
-   * @brief  Create and send a dynamic Web Page.  This page contains the list of 
 
-   *         running tasks and the number of page hits. 
 
-   * @param  ssl the SSL context
 
-   * @retval None
 
-   */
 
- void ssl_DynPage(ssl_context *ssl)
 
- {
 
-   portCHAR buf[2024];
 
-   portCHAR pagehits[10];
 
-   portCHAR getcipher[100];
 
-   uint32_t len = 0;
 
-   memset(buf, 0, 2024);
 
-   // Update the hit count 
 
-   nPageHits++;
 
-   sprintf( pagehits, "%d", nPageHits );
 
-   sprintf( (char *) getcipher, HTTP_RESPONSE, ssl_get_ciphersuite(ssl));   
 
-   
 
-   // Generate the dynamic page 
 
-   strcpy(buf, PAGE_START);
 
-   // Page header 
 
-   strcat(buf, pagehits);
 
-   strcat((char *) buf, "<br><pre>** The list of tasks and their status **");
 
-   strcat((char *) buf, "<br><pre>---------------------------------------------"); 
 
-   strcat((char *) buf, "<br>Name          State  Priority  Stack   Num" );
 
-   //strcat((char *) buf, "<br>---------------------------------------------"); 
 
-   strcat((char *) buf, "<br>---------------------------------------------<br>"); 
 
-     
 
-   // The list of tasks and their status 
 
-   vTaskList((signed char *)buf + strlen(buf));
 
-   strcat((char *) buf, "<br>---------------------------------------------"); 
 
-   strcat((char *) buf, "<br>B : Blocked, R : Ready, D : Deleted, S : Suspended");
 
-   // The current cipher used 
 
-   strcat(buf, getcipher);
 
-   // Page footer 
 
-   strcat(buf, PAGE_END);
 
-   
 
-   // Send the dynamically generated page 
 
-   len = ssl_write(ssl, (unsigned char *)buf, strlen(buf));
 
-   // Display the length of application data 
 
-   //printf( "\n Successfully write %d bytes to client\n\r", len);
 
- }
 
- /**
 
-   * @brief  This function is used to send messages with size upper 16k bytes.
 
-   * @param  ssl  SSL context
 
-   * @param  data buffer holding the data
 
-   * @param  len  how many bytes must be written
 
-   * @retval None
 
-   */
 
- #define FRAME_SIZE   (1000)
 
- void ssl_sendframes( ssl_context *ssl, char *data, int datalen )
 
- {
 
-   int index = 0;
 
-   int k = 0;
 
-   int lastframe, nbrframes;
 
-   // Calculate the number of frames 
 
-   nbrframes = datalen / FRAME_SIZE; 
 
-   
 
-   // Send nbrframes frames 
 
-   while(nbrframes > 0)
 
-   {
 
-     index = k * FRAME_SIZE;
 
-     ssl_write( ssl, (unsigned char *)(data + index), FRAME_SIZE );
 
-     nbrframes--;
 
-     k++;
 
-   }
 
-   // Send the last frame 
 
-   index = k * FRAME_SIZE;
 
-   lastframe = datalen % FRAME_SIZE ;
 
-   ssl_write( ssl, (unsigned char *)(data + index), lastframe );
 
- }
 
- /**
 
-   * @brief  Returns a 32-bit random number.
 
-   * @param  arg not used
 
-   * @retval 32-bit random number
 
-   */
 
- int RandVal(void* arg)
 
- {
 
-   return (int)GetRandomNumber();
 
- }
 
- /**
 
-   * @brief Send callback for log file transfer (messages as is, not ordered)
 
-   */
 
- void HTTP_SendHistory(void)
 
- {
 
-     uint32_t nbytes = 0;
 
-     static bool start = true;
 
-     memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
 
-     
 
-     if (log_ptr + FILE_BUF_MAX_LEN <= log_size) {
 
-         nbytes = History_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN, start);
 
-     }
 
-     else if (log_ptr < log_size) {
 
-         nbytes = History_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
 
-     }
 
-     else {
 
-         nbytes = 0;
 
-     }
 
-     log_ptr += nbytes;
 
-     start = false;
 
-     
 
-     if (nbytes == 0) {
 
-         // File transfer finished. 
 
-         start = true;
 
-         // Clear file transfer in progress flag 
 
-         fLogTransInprog = false;
 
-         return;
 
-     }
 
-     ssl_sendframes(&ssl, logFileBuf, nbytes);
 
-     HTTP_SendHistory();
 
- }
 
- /**
 
-   * @brief Sent callback for log file transfer (messages as is, not ordered)
 
-   */
 
- void HTTP_SendLog(void)
 
- {
 
-     uint32_t nbytes = 0;
 
-     static bool start = true;
 
- 	memset(logFileBuf, 0, FILE_BUF_MAX_LEN);
 
-     if (log_ptr + FILE_BUF_MAX_LEN_LOG <= log_size) {
 
-         nbytes = LOG_GetData(log_ptr, logFileBuf, FILE_BUF_MAX_LEN_LOG, start);
 
-     }
 
-     else if (log_ptr < log_size) {
 
-         nbytes = LOG_GetData(log_ptr, logFileBuf, (log_size - log_ptr), start);
 
-     }
 
-     else {
 
-         nbytes = 0;
 
-     }
 
-     log_ptr += nbytes;
 
-     start = false;
 
-     if (nbytes == 0) {
 
-         // File transfer finished. 
 
-         start = true;
 
-         // Clear file transfer in progress flag 
 
-         fLogTransInprog = false;
 
-         return;
 
-     }
 
-     ssl_sendframes(&ssl, logFileBuf, nbytes);
 
-     HTTP_SendLog();
 
-   
 
-     return;
 
- }
 
- /**
 
-   * @brief  Initialize the HTTPS server (start its thread) 
 
-   */
 
- void HTTPS_Init()
 
- {
 
- 	char buf[MAX_WEB_COOKIE_LEN];
 
- 	uint8_t user_id;
 
-     
 
-     for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
 
-         // Flush user cookie by random value 
 
-         sprintf(buf, "%X", (unsigned int)GetRandomNumber());
 
-         HTTP_SetUserCookie(buf, user_id);
 
-         // Create user logout timers 
 
-         users[user_id].LogoutTimer =
 
-              xTimerCreate("LogoutTmr", WEB_LOGOUT_TIME, pdFALSE, ( void * ) user_id, LogoutTimerCallback);
 
-     }
 
- }
 
 
  |