Port of TI's CC3100 Websock camera demo. Using FreeRTOS, mbedTLS, also parts of Arducam for cams ov5642 and 0v2640. Can also use MT9D111. Work in progress. Be warned some parts maybe a bit flacky. This is for Seeed Arch max only, for an M3, see the demo for CM3 using the 0v5642 aducam mini.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers httpserverapp.cpp Source File

httpserverapp.cpp

00001 //*****************************************************************************
00002 // httpserver_app.c
00003 //
00004 // camera application macro & APIs
00005 //
00006 // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
00007 //
00008 //
00009 //  Redistribution and use in source and binary forms, with or without
00010 //  modification, are permitted provided that the following conditions
00011 //  are met:
00012 //
00013 //    Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 //    Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the
00019 //    distribution.
00020 //
00021 //    Neither the name of Texas Instruments Incorporated nor the names of
00022 //    its contributors may be used to endorse or promote products derived
00023 //    from this software without specific prior written permission.
00024 //
00025 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026 //  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027 //  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00028 //  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00029 //  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00030 //  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00031 //  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00032 //  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00033 //  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00034 //  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035 //  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 //*****************************************************************************
00038 //*****************************************************************************
00039 //
00040 //! \addtogroup Httpserverapp
00041 //! @{
00042 //
00043 //*****************************************************************************
00044 
00045 #include <string.h>
00046 #include <stdlib.h>
00047 
00048 // SimpleLink include
00049 #include "cc3100_simplelink.h"
00050 #include "cc3100_sl_common.h"
00051 
00052 // Free-RTOS/TI-RTOS include
00053 #include "FreeRTOS.h"
00054 #include "osi.h"
00055 //#include "semphr.h"
00056 
00057 // HTTP lib includes
00058 #include "HttpCore.h"
00059 #include "HttpRequest.h"
00060 
00061 #include "websocketapp.h"
00062 #include "httpserverapp.h"
00063 #include "camera_app.h"
00064 #include "i2cconfig.h"
00065 #include "mt9d111.h"
00066 #include "cli_uart.h"
00067 #include "app_config.h"
00068 #include "myBoardInit.h"
00069 #include "HttpDebug.h"
00070 
00071 using namespace mbed_cc3100;
00072 
00073 cc3100 _cc3100_Mod(NC, NC, PD_12, PD_13, PD_11, SPI(PC_3, PC_2, PB_10));//Seeed_Arch_Max  irq, nHib, cs, mosi, miso, sck
00074 
00075 /****************************************************************************/
00076 /*              MACROS                                      */
00077 /****************************************************************************/
00078 
00079 
00080 /****************************************************************************
00081                               Global variables
00082 ****************************************************************************/
00083 char *g_Buffer;
00084 UINT8 g_success = 0;
00085 int g_close = 0;
00086 UINT16 g_uConnection;
00087 OsiTaskHandle g_iCameraTaskHdl = 0;
00088 
00089 void WebSocketCloseSessionHandler()
00090 {
00091     g_close = 1;
00092 }
00093 
00094 void CameraAppTask(void *param)
00095 {
00096     UINT8 Opcode = 0x02;
00097     struct HttpBlob Write;
00098 #ifdef ENABLE_JPEG
00099 //  InitCameraComponents(640, 480);
00100     InitCameraComponents(320, 240);
00101 #else
00102     InitCameraComponents(240, 256);
00103 #endif    
00104     
00105     while(1)
00106     {
00107         if(g_close == 0)
00108         {
00109             Write.uLength = StartCamera((char **)&Write.pData);
00110             if(!sl_WebSocketSend(g_uConnection, Write, Opcode))
00111             {
00112                 Uart_Write((uint8_t*)"sl_WebSocketSend failed.\n\r");
00113                 while(1);
00114             }
00115         }
00116     }
00117 
00118 }
00119 
00120 
00121 /*!
00122  *  \brief                  This websocket Event is called when WebSocket Server receives data
00123  *                          from client.
00124  *
00125  *
00126  *  \param[in]  uConnection Websocket Client Id
00127  *  \param[in] *ReadBuffer      Pointer to the buffer that holds the payload.
00128  *
00129  *  \return                 none.
00130  *                      
00131  */
00132 void WebSocketRecvEventHandler(UINT16 uConnection, char *ReadBuffer)
00133 {
00134     char *camera = "capture";
00135 
00136     /*
00137      * UINT8 Opcode;
00138      * struct HttpBlob Write;
00139     */
00140 
00141     g_uConnection = uConnection;
00142 
00143     g_Buffer = ReadBuffer;
00144     g_close = 0;
00145     if (!strcmp(ReadBuffer,camera))
00146     {
00147         if(!g_iCameraTaskHdl)
00148         {
00149             osi_TaskCreate(CameraAppTask,
00150                                    "CameraApp",
00151                                     1024,
00152                                     NULL,
00153                                     CAMERA_SERVICE_PRIORITY,
00154                                     &g_iCameraTaskHdl);
00155         }
00156 
00157     }
00158     //Free memory as we are not using anywhere later
00159     free(g_Buffer);
00160     g_Buffer = NULL;
00161     /* Enter websocket application code here */
00162     return;
00163 }
00164 
00165 
00166 /*!
00167  *  \brief                      This websocket Event indicates successful handshake with client
00168  *                              Once this is called the server can start sending data packets over websocket using
00169  *                              the sl_WebSocketSend API.
00170  *
00171  *
00172  *  \param[in] uConnection          Websocket Client Id
00173  *
00174  *  \return                     none
00175  */
00176 void WebSocketHandshakeEventHandler(UINT16 uConnection)
00177 {
00178     g_success = 1;
00179     g_uConnection = uConnection;
00180 }
00181 
00182 
00183 //****************************************************************************
00184 //
00185 //! Task function start the device and crete a TCP server showcasing the smart
00186 //! plug
00187 //!
00188 //****************************************************************************
00189 
00190 
00191 void HttpServerAppTask(void * param)
00192 {
00193     int32_t lRetVal = -1;
00194     
00195     //Start SimpleLink in AP Mode
00196     lRetVal = _cc3100_Mod.Network_AP_InitDriver();
00197 //  lRetVal = _cc3100_Mod.Network_IF_InitDriver(ROLE_AP);
00198     if(lRetVal < 0)
00199     {
00200         Uart_Write((uint8_t*)"Start SimpleLink in AP Mode Failed \n\r");
00201         LOOP_FOREVER();
00202     }   
00203     osi_Sleep(2);
00204     //Stop Internal HTTP Server
00205     lRetVal = _cc3100_Mod._netapp.sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);
00206     if(lRetVal < 0)
00207     {
00208         Uart_Write((uint8_t*)"Stop Internal HTTP Server Failed \n\r");
00209         LOOP_FOREVER();
00210     }   
00211     Uart_Write((uint8_t*)"HttpServerAppTask \n\r");
00212     //Run APPS HTTP Server
00213     HttpServerInitAndRun(NULL);
00214     
00215     Uart_Write((uint8_t*)"HttpServerAppTask has returned for some reason\n\r");
00216     LOOP_FOREVER();
00217 
00218 }
00219 
00220 //*****************************************************************************
00221 //
00222 // Close the Doxygen group.
00223 //! @}
00224 //
00225 //*****************************************************************************
00226