TI's CC3100 websocket camera demo with Arducam mini ov5642 and freertos. Should work with other M3's. Work in progress test demo.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HttpStatic.cpp Source File

HttpStatic.cpp

00001 //*****************************************************************************
00002 // Copyright (C) 2014 Texas Instruments Incorporated
00003 //
00004 // All rights reserved. Property of Texas Instruments Incorporated.
00005 // Restricted rights to use, duplicate or disclose this code are
00006 // granted through contract.
00007 // The program may not be used without the written permission of
00008 // Texas Instruments Incorporated or against the terms and conditions
00009 // stipulated in the agreement under which this program has been supplied,
00010 // and under no circumstances can it be used with non-TI connectivity device.
00011 //
00012 //*****************************************************************************
00013 
00014 #include "HttpStatic.h"
00015 #include <stdlib.h>
00016 
00017 #ifdef HTTP_CORE_ENABLE_STATIC
00018 
00019 #include "HttpRequest.h"
00020 #include "HttpResponse.h"
00021 #include "HttpCore.h"
00022 #include "cc3100.h"
00023 #include "cc3100_fs.h"
00024 #include "HttpDebug.h"
00025 #include "myBoardInit.h"
00026 #include "osi.h"
00027 
00028 using namespace mbed_cc3100;
00029 
00030 cc3100 _cc3100Module(NC, NC, p9, p10, p8, SPI(p5, p6, p7));//LPC1768  irq, nHib, cs, mosi, miso, sck
00031 
00032 
00033 /**
00034  * @addtogroup HttpStatic
00035  * @{
00036  */
00037 
00038 /**
00039  * @defgroup HttpStatic Static request handler module
00040  * This module implements static content processing for HTTP requests.
00041  * All requests are handled by looking up the URL's resource in the flash database, and returning the content in the response.
00042  * Note this module is only compiled if HTTP_CORE_ENABLE_STATIC is defined in HttpConfig.h
00043  *
00044  * @{
00045  */
00046 
00047 #define FILE_NAME_SIZE_MAX    (40)
00048 
00049 //Store File Name from HTTP Header
00050 char g_cFileName[FILE_NAME_SIZE_MAX];               //String storing filename
00051 int32_t glFileHandle;                           // file handle
00052 
00053 /**
00054  * Initialize HttpStatic module state for a new request, and identify the request
00055  * This function examines the specified resource string, and looks it up in the Flash Database.
00056  * If found, it commits to process this request by returning nonzero. Otherwise it returns zero.
00057  * @param uConnection The number of the connection. This value is guaranteed to satisfy: 0 <= uConnection < HTTP_CORE_MAX_CONNECTIONS
00058  * @param resource The resource part of the URL, as specified by the browser in the request, including any query string (and hash).
00059  *                 Note: The resource string exists ONLY during the call to this function. The string pointer should not be copied by this function.
00060  * @return nonzero if request is to be handled by this module. zero if not.
00061  */
00062 int HttpStatic_InitRequest(UINT16 uConnection, struct HttpBlob resource)
00063 {
00064     char *pcWWWFsDir = "www";
00065     memset(g_cFileName,'\0',40);
00066 
00067     if(resource.uLength ==1 && *(resource.pData)=='/')
00068     {
00069         strcpy(g_cFileName,"www/main.html");
00070     }
00071     else
00072     {   
00073         strcpy(g_cFileName,pcWWWFsDir);
00074         strncat(g_cFileName,(char*)resource.pData,resource.uLength);
00075     }
00076 
00077     if(_cc3100Module._fs.sl_FsOpen((unsigned char*)g_cFileName,FS_MODE_OPEN_READ,NULL,&glFileHandle)<0)
00078         return 0;
00079     else
00080     {
00081         return 1;
00082     }
00083 }
00084 
00085 /**
00086  * Process a static-content HTTP request
00087  * This function is called after a request was already initialized, and a Flash content entry was identified during a call to HttpStatic_InitRequest()
00088  * This function calls HttpResponse_*() to send the content data to the browser.
00089  * @param request Pointer to all data available about the request
00090  * @return nonzero if request was handled. zero if not.
00091  */
00092 int HttpStatic_ProcessRequest(struct HttpRequest* request)
00093 {
00094     struct HttpBlob location,contentType;
00095     struct HttpBlob* content = (struct HttpBlob*)malloc(sizeof(struct HttpBlob));
00096     unsigned int Offset = 0;
00097     SlFsFileInfo_t pFsFileInfo;
00098     UINT32 TotalLength;
00099     UINT8 HeaderFlag =0;
00100     UINT8 bRetVal = 1;
00101     UINT8 *buffer1 = NULL;
00102 
00103 
00104     if(content == NULL)
00105     {
00106         HttpDebug("content = NULL error\n\r");
00107         return 0;
00108     }
00109     location.pData = NULL;
00110     location.uLength = 0;
00111     contentType = location;
00112 
00113     /*  if HTTP_REQUEST_FLAG_METHOD_POST==1 (i.e. it is POST)
00114         HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_INTERNAL
00115         POST method is not supported for static pages   */
00116     if (request->uFlags & HTTP_REQUEST_FLAG_METHOD_POST)
00117     {
00118          /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL  */
00119          if(!HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_INTERNAL))
00120          {
00121             HttpDebug("HttpResponse_CannedError \n\r");
00122             bRetVal = 0;
00123             goto end;
00124          }
00125          else
00126          {
00127             bRetVal = 1;
00128             goto end;
00129          }
00130     }
00131 
00132     _cc3100Module._fs.sl_FsGetInfo((unsigned char *)g_cFileName, NULL, &pFsFileInfo);
00133     TotalLength = (&pFsFileInfo)->FileLen;
00134     
00135     while(TotalLength > 0)
00136     {
00137         
00138 //      HttpDebug("TotalLength = %d\n\r",TotalLength);
00139         content->uLength = ((TotalLength < 1000) ? (TotalLength):(1000));       
00140         buffer1 = (uint8_t*)realloc(buffer1, content->uLength);
00141         if(buffer1 == NULL)
00142         {
00143             HttpDebug("buffer1 = NULL Error \n\r");
00144             bRetVal = 0;
00145             goto end;
00146         }
00147         content->pData = buffer1;
00148 
00149         /* if got here than it is a GET method
00150         HttpResponse_Headers() responds to client with status HTTP_STATUS_OK */
00151         if(_cc3100Module._fs.sl_FsRead(glFileHandle, Offset, (unsigned char *) content->pData, content->uLength) < 0)
00152         {
00153             /* call HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL  */
00154             if(!HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_NOT_ACCEPTED))
00155             {
00156                 HttpDebug("500 ERROR_INTERNAL Error \n\r");
00157                 bRetVal = 0;
00158                 goto end;
00159             }
00160             else
00161             {
00162                 bRetVal = 1;
00163                 goto end;
00164             }
00165         }
00166         else
00167         {
00168             if(!HeaderFlag)
00169             {
00170                 if(!HttpResponse_Headers(request->uConnection, (uint16_t)HTTP_STATUS_OK, NULL,TotalLength, contentType, location))
00171                 {
00172                     HttpDebug("HeaderFlag Error \n\r");
00173                     bRetVal = 0;
00174                     goto end;
00175                 }
00176                 HeaderFlag = 1;
00177             }
00178 
00179             /* HttpResponse_Content() sends requested page to the client */
00180             if(!HttpResponse_Content(request->uConnection, *content))
00181             {
00182                 HttpDebug("HttpResponse_Content Error \n\r");
00183                 bRetVal = 0;
00184                 goto end;
00185             }
00186         }
00187 
00188         TotalLength -= content->uLength;
00189         Offset += content->uLength;
00190     }
00191 
00192     _cc3100Module._fs.sl_FsClose(glFileHandle,0,0,0);
00193 end:
00194     if(buffer1 != NULL)
00195     {
00196         free(buffer1);
00197     }
00198     free(content);
00199 
00200     return bRetVal;
00201 }
00202 
00203 /// @}
00204 
00205 #endif // HTTP_CORE_ENABLE_STATIC
00206 
00207 
00208