Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Netconn server example is based on single "user" thread
- * which listens for new connections and accepts them.
- *
- * When a new client is accepted by server,
- * separate thread for client is created where
- * data is read, processed and send back to user
- */
- #include "netconn_server.h"
- #include "esp/esp.h"
- #include "fsdata.c"
- #include "lfs.h"
- #include "esp/esp_mem.h"
- #include "esp/esp_conn.h"
- extern lfs_t lfs;
- static lfs_file_t file_upload;
- static size_t DataOffset=0;
- static uint8_t conn_closed=0;
- htmlpageState htmlpage;
- static char transfer_filename[80];
- static uint32_t DataFlag=0;
- static uint32_t size=0;
- static uint32_t ContentLengthOffset =0,BrowserFlag=0;
- static uint32_t TotalData=0;
- static uint32_t TotalReceived=0;
- /* file must be allocated by caller and will be filled in by the function. */
- static int fs_open(char *name, struct fs_file *file);
- //static void netconn_server_processing_thread(void* const arg);
- static void netconn_server_process_get_requests(esp_netconn_p client, esp_pbuf_p pbuf);
- static void netconn_server_process_post_requests(esp_netconn_p client, esp_pbuf_p pbuf, size_t len);
- static void netconn_server_process_data_packets(esp_netconn_p client, esp_pbuf_p pbuf, size_t len);
- static const char http_crnl_2[4] =
- /* "\r\n--" */
- {0xd, 0xa,0x2d,0x2d};
- /**
- * \brief Main page response file
- */
- //static const uint8_t
- //resp_data_mainpage_top[] = ""
- //"HTTP/1.1 200 OK\r\n"
- //"Content-Type: text/html\r\n"
- //"\r\n"
- //"<html>"
- //" <head>"
- //" <link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />"
- //" <meta http-equiv=\"refresh\" content=\"1\" />"
- //" </head>"
- //" <body>"
- //" <p>Netconn driven website!</p>"
- //" <p>Total system up time: <b>";
- /**
- * \brief Bottom part of main page
- */
- //static const uint8_t
- //resp_data_mainpage_bottom[] = ""
- //" </b></p>"
- //" </body>"
- //"</html>";
- /**
- * \brief Style file response
- */
- //static const uint8_t
- //resp_data_style[] = ""
- //"HTTP/1.1 200 OK\r\n"
- //"Content-Type: text/css\r\n"
- //"\r\n"
- //"body { color: red; font-family: Tahoma, Arial; };";
- const char *netconn_get_string_error(espr_t res)
- {
- switch(res){
- case espCLOSED:
- return "Connection closed by client\r\n";
- case espTIMEOUT:
- return "Connection receive timeout occurs\r\n";
- case espPARERR:
- return "Wrong parameters on function call\r\n";
- case espERRMEM:
- return "Memory error occurred\r\n";
- case espCONT:
- return "There is still some command to be processed in current command\r\n";
- case espINPROG:
- return "Operation is in progress\r\n";
- case espERRNOIP:
- return "Station does not have IP address\r\n";
- case espERRNOFREECONN:
- return "There is no free connection available to start\r\n";
- case espERRCONNTIMEOUT:
- return "Timeout received when connection to access point\r\n";
- case espERRPASS:
- return "Invalid password for access point\r\n";
- case espERRNOAP:
- return "No access point found with specific SSID and MAC address\r\n";
- case espERRCONNFAIL:
- return "Connection failed to access point\r\n";
- case espERRWIFINOTCONNECTED:
- return "Wifi not connected to access point\r\n";
- case espERRNODEVICE:
- return "Device is not present\r\n";
- case espERRBLOCKING:
- return "Blocking mode command is not allowed\r\n";
- case espERR:
- return "Undocumented error\r\n";
- }
- return "";
- }
- /**
- * @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, (const char*)f->name))
- {
- file->data = f->data;
- file->len = f->len;
- return 1;
- }
- }
- return 0;
- }
- /**
- * \brief Netconn server thread implementation
- * \param[in] arg: User argument
- */
- void
- netconn_server_thread(void const* arg) {
- esp_netconn_p server, client;
- esp_pbuf_p pbuf;
- espr_t res;
- size_t len;
- /*
- * First create a new instance of netconn
- * connection and initialize system message boxes
- * to accept clients and packet buffers
- */
- server = esp_netconn_new(ESP_NETCONN_TYPE_TCP);
- if (server != NULL) {
- printf("Server netconn created\r\n");
- /*
- * Bind network connection to port 80
- */
- res = esp_netconn_bind(server, 80);
- if (res == espOK) {
- printf("Webserver listens on port 80\r\n");
- /* Start listening for incoming connections with maximal 1 client */
- res = esp_netconn_listen_with_max_conn(server, 1);
- while (1) {
- /*
- * Wait and accept new client connection.
- * Function will block thread until new client is connected to server
- */
- res = esp_netconn_accept(server, &client);
- if (res == espOK) {
- printf("Netconn new client connected.\r\n");
- /*
- * Client was accepted,
- * We are now expecting client will send to us some data.
- * Wait for data and block thread for that time
- */
- conn_closed = 0;
- while (!conn_closed) {
- res = esp_netconn_receive(client, &pbuf);
- if (res == espOK) {
- len = esp_pbuf_length(pbuf, 1);
- printf("Netconn data received, %d bytes\r\n", (int)len);
- /* dump data for debug purposes */
- uint8_t *dump_buf = (uint8_t *)esp_mem_alloc(len);
- if (dump_buf != NULL){
- esp_pbuf_copy(pbuf, dump_buf, len, 0);
- Dump(dump_buf,len);
- esp_mem_free(dump_buf);
- }
- /* process HTTP GET requests */
- if (esp_pbuf_strfind(pbuf, "GET /", 0) == 0) {
- netconn_server_process_get_requests(client, pbuf);
- }
- /* process HTTP POST requests */
- else if (esp_pbuf_strfind(pbuf, "POST /", 0) == 0) {
- netconn_server_process_post_requests(client, pbuf, len);
- }
- else if ((DataFlag >=1) && (htmlpage == FileUploadPage)) {
- netconn_server_process_data_packets(client, pbuf, len);
- }
- /* free memory after usage! */
- esp_pbuf_free(pbuf);
- }
- else {
- printf("[HTTP] %s", netconn_get_string_error(res));
- if (res == espCLOSED) {
- break;
- }
- esp_netconn_close(client);
- conn_closed=1;
- }
- } // END while (!conn_closed);
- } // END if (res == espOK)
- if (client != NULL){
- esp_netconn_delete(client);
- client = NULL;
- }
- } // END while (1)
- } else {
- printf("Netconn server cannot bind to port\r\n");
- }
- } else {
- printf("Cannot create server netconn\r\n");
- }
- esp_netconn_delete(server); /* Delete netconn structure */
- esp_sys_thread_terminate(NULL); /* Terminate current thread */
- }
- /**
- * \brief Thread to process single active connection
- * \param[in] arg: Thread argument
- */
- //static void
- //netconn_server_processing_thread(void* const arg) {
- // esp_netconn_p client;
- // esp_pbuf_p pbuf, p = NULL;
- // espr_t res;
- // size_t len;
- //
- // client = arg; /* Client handle is passed to argument */
- //
- // printf("A new connection accepted!\r\n"); /* Print simple message */
- //
- // do {
- // /*
- // * Client was accepted, we are now
- // * expecting client will send to us some data
- // *
- // * Wait for data and block thread for that time
- // */
- // res = esp_netconn_receive(client, &pbuf);
- //
- // if (res == espOK) {
- // len = esp_pbuf_length(pbuf, 1);
- // printf("Netconn data received, %d bytes\r\n", (int)len);
- //
- // uint8_t *dump_buf = (uint8_t *)esp_mem_alloc(len);
- // if (dump_buf != NULL){
- // esp_pbuf_copy(pbuf, dump_buf, len, 0);
- // Dump(dump_buf,len);
- // esp_mem_free(dump_buf);
- // }
- //
- // /*
- // * Check if all headers were received
- // */
- // if (p == NULL) {
- // p = pbuf; /* Set as first buffer */
- // } else {
- // esp_pbuf_cat(p, pbuf); /* Concatenate buffers together */
- // }
- // if (esp_pbuf_strfind(pbuf, "\r\n\r\n", 0) != ESP_SIZET_MAX) {
- //
- // /* process HTTP GET requests */
- // if (esp_pbuf_strfind(pbuf, "GET /", 0) != ESP_SIZET_MAX) {
- // netconn_server_process_get_requests(client, pbuf);
- // }
- // /* process HTTP POST requests */
- // else if (esp_pbuf_strfind(pbuf, "POST /", 0) != ESP_SIZET_MAX) {
- // netconn_server_process_post_requests(client, pbuf);
- // }
- // esp_netconn_close(client); /* Close netconn connection */
- // esp_pbuf_free(p); /* Do not forget to free memory after usage! */
- // p = NULL;
- // break;
- // }
- // }
- // } while (res == espOK);
- //
- // if (p != NULL) { /* Free received data */
- // esp_pbuf_free(p);
- // p = NULL;
- // }
- // esp_netconn_delete(client); /* Destroy client memory */
- // esp_sys_thread_terminate(NULL); /* Terminate this thread */
- //}
- /**
- * \brief process http get requests
- * \param[in] client: client conection
- * \param[in] pbuf: request information
- */
- static void
- netconn_server_process_get_requests(esp_netconn_p client, esp_pbuf_p pbuf){
- struct fs_file file = {0, 0};
- if (esp_pbuf_strfind(pbuf, "GET /resetmcu.cgi", 0) == 0){
- if (htmlpage == UploadDonePage){
- htmlpage = ResetDonePage;
- /* send reset.html page */
- if (fs_open("/reset.html", &file)){
- esp_netconn_write(client, file.data, file.len);
- esp_netconn_flush(client);
- }
- /* Close netconn connection */
- esp_netconn_close(client);
- conn_closed = 1;
- printf("Server netconn closed 1\r\n");
- /* Generate a software reset */
- NVIC_SystemReset();
- while(1);
- }
- else {
- /* Bad HTTP requests */
- printf ("[HTTP] Bad HTTP request\n");
- htmlpage = LoginPage;
- DataFlag = 0;
- BrowserFlag = 0;
- }
- }
- else if (esp_pbuf_strfind(pbuf, "GET /images/MRG.png", 0) == 0){
- if (htmlpage == LoginPage){
- /* send image MRG.png page */
- if (fs_open("/images/MRG.png", &file)){
- esp_netconn_write(client, file.data, file.len);
- esp_netconn_flush(client);
- }
- /* Close netconn connection */
- esp_netconn_close(client);
- conn_closed = 1;
- printf("Server netconn closed 2\r\n");
- }
- else {
- /* Bad HTTP requests */
- printf ("[HTTP] Bad HTTP request\n");
- htmlpage = LoginPage;
- DataFlag = 0;
- BrowserFlag = 0;
- }
- }
- else if (esp_pbuf_strfind(pbuf, "GET / HTTP", 0) == 0){
- htmlpage = LoginPage;
- DataFlag = 0;
- BrowserFlag = 0;
- /* send index.html page */
- if (fs_open("/index.html", &file)){
- esp_netconn_write(client, file.data, file.len);
- esp_netconn_flush(client);
- }
- /* Close netconn connection */
- esp_netconn_close(client);
- conn_closed = 1;
- printf("Server netconn closed 3\r\n");
- }
- }
- /**
- * \brief process http get requests
- * \param[in] client: client conection
- * \param[in] pbuf: request information
- */
- static void
- netconn_server_process_post_requests(esp_netconn_p client, esp_pbuf_p pbuf, size_t len){
- struct fs_file file = {0, 0};
- size_t offset;
- char *filename;
- /* process POST request for checking login */
- if (esp_pbuf_strfind(pbuf, "POST /checklogin.cgi", 0) == 0){
- if (htmlpage == LoginPage){
- /* parse packet for the username & password */
- char login[LOGIN_SIZE];
- sprintf((char *)login,"username=%s&password=%s",USERID,PASSWORD);
- offset = esp_pbuf_strfind(pbuf, login, 0);
- if (offset != ESP_SIZET_MAX) {
- /* correct user and password */
- htmlpage = FileUploadPage;
- filename = "/upload.html";
- } else {
- /* incorrect user and/or password -> reload index page */
- htmlpage = LoginPage;
- filename = "/index.html";
- }
- /* send requested page */
- if (fs_open(filename, &file)){
- esp_netconn_write(client, file.data, file.len);
- esp_netconn_flush(client);
- }
- /* Close netconn connection */
- esp_netconn_close(client);
- conn_closed = 1;
- printf("Server netconn closed 4\r\n");
- }
- else {
- /* Bad HTTP requests */
- printf ("[HTTP] Bad HTTP request\n");
- htmlpage = LoginPage;
- DataFlag = 0;
- BrowserFlag = 0;
- }
- }
- else
- /* process POST request for file upload and incoming data packets after POST request*/
- if ((esp_pbuf_strfind(pbuf, "POST /upload.cgi",0) == 0) && (htmlpage == FileUploadPage)) {
- /* POST Packet received */
- DataOffset = 0;
- BrowserFlag=0;
- TotalReceived =0;
- /* parse packet for Content-length field */
- offset = esp_pbuf_memfind(pbuf, "Content-Length: ", 16, 0);
- if (offset != ESP_SIZET_MAX){
- char sizestring[8];
- ContentLengthOffset = offset;
- TotalReceived = len - offset;
- esp_pbuf_copy(pbuf, sizestring, sizeof(sizestring), ContentLengthOffset+16);
- size = atoi(sizestring);
- }
- /* parse packet for the application/json field */
- offset = esp_pbuf_memfind(pbuf, "Content-Type: application/json", 30, 0);
- if (offset != ESP_SIZET_MAX){
- /* case of Mozilla Firefox : we receive data in the POST packet */
- DataOffset = offset + 34;
- }
- else {
- /* IE doenst send type as json. Instead it send as plain text */
- offset = esp_pbuf_memfind(pbuf, "Content-Type: text/plain", 24, 0);
- if (offset != ESP_SIZET_MAX){
- DataOffset = offset + 28;
- }
- }
- /* case of MSIE8 : we do not receive data in the POST packet */
- if (DataOffset==0){
- DataFlag++;
- BrowserFlag++;
- return;
- }
- netconn_server_process_data_packets(client, pbuf, len);
- }
- }
- /**
- * \brief process http get requests
- * \param[in] client: client conection
- * \param[in] pbuf: request information
- */
- static void
- netconn_server_process_data_packets(esp_netconn_p client, esp_pbuf_p pbuf, size_t len){
- struct fs_file file = {0, 0};
- size_t offset;
- if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0))) {
- if ((DataFlag ==0)&&(BrowserFlag==0)) {
- DataFlag+=2;
- }
- else if ((DataFlag ==1)&&(BrowserFlag==1)) {
- /* parse packet for the application/json field */
- offset = esp_pbuf_memfind(pbuf, "Content-Type: application/json", 30, 0);
- if (offset != ESP_SIZET_MAX){
- DataOffset = offset + 34;
- TotalReceived += len;
- DataFlag++;
- }
- else {
- /* IE doenst send type as json. Instead it send as plain text */
- offset = esp_pbuf_memfind(pbuf, "Content-Type: text/plain", 24, 0);
- if (offset != ESP_SIZET_MAX){
- DataOffset = offset + 28;
- TotalReceived += len;
- DataFlag++;
- }
- }
- }
- /* parse packet for the filename field */
- offset = esp_pbuf_memfind(pbuf, "filename=\"", 10, 0);
- if (offset != ESP_SIZET_MAX){
- size_t end_offset = esp_pbuf_memfind(pbuf, "\"\r\n", 3, offset+10);
- esp_pbuf_copy(pbuf, transfer_filename, end_offset-offset, offset+10);
- char *p = NULL;
- /* look for name terminator */
- p = strrchr(transfer_filename, '"');
- if (p != NULL)
- *p = 0;
- /* remove any path (IE), taking only filename itself */
- p = strrchr(transfer_filename, '\\');
- if (p == NULL)
- p = strrchr(transfer_filename, '/');
- if (p != NULL)
- strcpy(transfer_filename, p+1);
- }
- else {
- htmlpage = FileUploadPage;
- /* send requested page */
- if (fs_open("/upload.html", &file)){
- esp_netconn_write(client, file.data, file.len);
- esp_netconn_flush(client);
- }
- /* Close netconn connection */
- esp_netconn_close(client);
- conn_closed = 1;
- printf("Server netconn closed 7\r\n");
- DataFlag=0;
- return;
- }
- printf("[HTTP] Recebendo arquivo: %s\tSize=%d\r\n", transfer_filename, size);
- TotalData = 0;
- lfs_file_open(&lfs, &file_upload, "config.json", LFS_O_RDWR | LFS_O_CREAT);
- }
- /* DataFlag >1 => the packet is data only */
- else {
- TotalReceived +=len;
- DataOffset = 0;
- }
- //ptr = (char*)(data + DataOffset);
- len -= DataOffset;
- uint8_t *buffer = esp_mem_alloc(len);
- if (buffer == NULL){
- printf("Can't allocate memory.!!!!");
- return;
- }
- esp_pbuf_copy(pbuf, buffer, len, DataOffset);
- /* check if last data packet */
- if (TotalReceived >= size)
- {
- /* if last packet need to remove the http boundary tag */
- /* parse packet for "\r\n--" starting from end of data */
- offset = esp_pbuf_memfind(pbuf, http_crnl_2, 4, 0);
- if (offset != ESP_SIZET_MAX){
- len = offset;
- }
- /* write data in Flash */
- if (len){
- printf("[HTTP] Gravando no arquivo: %s\tsplit=%d\r\n", transfer_filename, len);
- lfs_file_write(&lfs, &file_upload, buffer, len);
- printf("[HTTP] Fechando arquivo: %s\r\n", transfer_filename);
- lfs_file_close(&lfs, &file_upload);
- /* update Total data received counter */
- TotalData +=len;
- }
- DataFlag=0;
- printf("[HTTP] Arquivo %s recebido com sucesso! [Size=%d]\r\n", transfer_filename, TotalData);
- htmlpage = UploadDonePage;
- /* send uploaddone.html page */
- if (fs_open("/uploaddone.html", &file)){
- esp_netconn_write(client, file.data, file.len);
- esp_netconn_flush(client);
- }
- /* Close netconn connection */
- esp_netconn_close(client);
- conn_closed = 1;
- printf("Server netconn closed 8\r\n");
- }
- /* not last data packet */
- else {
- /* write data in flash */
- if(len){
- printf("[HTTP] Gravando no arquivo: %s\tsplit=%d\r\n", transfer_filename, len);
- lfs_file_write(&lfs, &file_upload, buffer, len);
- /* update Total data received counter */
- TotalData +=len;
- }
- }
- printf("TotalReceived: %d \tTotalData: %d\r\n",TotalReceived, TotalData);
- esp_mem_free(buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement