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.
HttpSocket.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 00015 /** 00016 * @defgroup HttpSocket 00017 * This module performs all HTTP socket operations 00018 * 00019 * @{ 00020 */ 00021 00022 #include <string.h> 00023 #include "HttpDebug.h" 00024 #include "HttpSocket.h" 00025 #include "cc3100_simplelink.h" 00026 #include "cc3100_socket.h" 00027 00028 #include "osi.h" 00029 00030 using namespace mbed_cc3100; 00031 00032 cc3100 _cc3100_Module(NC, NC, PD_12, PD_13, PD_11, SPI(PC_3, PC_2, PB_10));//Seeed_Arch_Max irq, nHib, cs, mosi, miso, sck 00033 00034 #define OSI_DELAY(x) osi_Sleep(x); //os idle 00035 00036 int OpenTCPServerSocket(unsigned int uiPortNum) 00037 { 00038 int iSockDesc, iRetVal; 00039 sockaddr_in sServerAddress; 00040 SlSockNonblocking_t enableOption; 00041 enableOption.NonblockingEnabled = 1; 00042 00043 // 00044 // opens a secure socket 00045 // 00046 if(443 == uiPortNum) 00047 { 00048 iSockDesc = _cc3100_Module._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET); 00049 } 00050 else //non secure 00051 { 00052 iSockDesc = _cc3100_Module._socket.sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_IPPROTO_TCP); 00053 } 00054 00055 if( iSockDesc < 0 ) 00056 { 00057 return -1; 00058 } 00059 //non blocking socket - Enable nonblocking mode 00060 iRetVal = _cc3100_Module._socket.sl_SetSockOpt(iSockDesc,SOL_SOCKET,SL_SO_NONBLOCKING, &enableOption,sizeof(enableOption)); 00061 if(iRetVal < 0) 00062 { 00063 CloseTCPServerSocket(iSockDesc); 00064 return -1; 00065 } 00066 00067 if(443 == uiPortNum) 00068 { 00069 iRetVal = _cc3100_Module._socket.sl_SetSockOpt(iSockDesc, SL_SOL_SOCKET, SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME,SL_SSL_SRV_KEY, strlen(SL_SSL_SRV_KEY)); 00070 if( iRetVal < 0 ) 00071 { 00072 CloseTCPServerSocket(iSockDesc); 00073 return -1; 00074 } 00075 iRetVal = _cc3100_Module._socket.sl_SetSockOpt(iSockDesc, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME,SL_SSL_SRV_CERT, strlen(SL_SSL_SRV_CERT)); 00076 if( iRetVal < 0 ) 00077 { 00078 CloseTCPServerSocket(iSockDesc); 00079 return -1; 00080 } 00081 } 00082 00083 // 00084 // Bind - Assign a port to the socket 00085 // 00086 sServerAddress.sin_family = AF_INET; 00087 sServerAddress.sin_addr.s_addr = _cc3100_Module._socket.htonl(INADDR_ANY); 00088 sServerAddress.sin_port = _cc3100_Module._socket.htons(uiPortNum); 00089 if ( _cc3100_Module._socket.bind(iSockDesc, (sockaddr*)&sServerAddress, sizeof(sServerAddress)) != 0 ) 00090 { 00091 CloseTCPServerSocket(iSockDesc); 00092 return -1; 00093 } 00094 00095 return iSockDesc; 00096 } 00097 00098 int CreateTCPServerSocket(unsigned int uiPortNum) 00099 { 00100 int iSockDesc = -1; 00101 unsigned char connectRetries = 0; 00102 00103 while (connectRetries++ < SERVER_MAX_SETUP_RETRY_COUNT) 00104 { 00105 iSockDesc = OpenTCPServerSocket(uiPortNum); 00106 00107 if (iSockDesc < 0) 00108 { 00109 continue; 00110 } 00111 00112 if(_cc3100_Module._socket.listen(iSockDesc, HTTP_CORE_MAX_CONNECTIONS) != 0) 00113 { 00114 CloseTCPServerSocket(iSockDesc); 00115 iSockDesc = -1; 00116 continue; 00117 } 00118 else 00119 { 00120 connectRetries = 0; 00121 break; 00122 } 00123 } 00124 00125 return iSockDesc; 00126 } 00127 00128 int CloseTCPServerSocket(int iSockDesc) 00129 { 00130 int ittr = 0; 00131 00132 if(iSockDesc < 0) 00133 { 00134 return 0 ; 00135 } 00136 00137 do 00138 { 00139 if(_cc3100_Module._socket.sl_Close(iSockDesc) >= 0) 00140 { 00141 iSockDesc = -1; 00142 HttpDebug("Http server socket closed\n\r"); 00143 return 0; 00144 } 00145 else 00146 { 00147 HttpDebug("\n Http client socket close error\n\r"); 00148 OSI_DELAY(500);//wait 500ms 00149 } 00150 ittr++; 00151 }while(ittr < 3); 00152 00153 return -1; 00154 } 00155 00156 int CreateTCPClientSocket(int iSockDesc) 00157 { 00158 sockaddr sClientAddr; 00159 SlSocklen_t uiClientAddrLen = sizeof(sClientAddr); 00160 int sock = -1; 00161 SlTimeval_t timeVal; 00162 SlSockNonblocking_t enableOption; 00163 00164 sock = _cc3100_Module._socket.accept(iSockDesc, &sClientAddr, &uiClientAddrLen); 00165 if(sock >= 0) 00166 { 00167 enableOption.NonblockingEnabled = 0; 00168 00169 //Blocking socket - Enable blocking mode 00170 if(_cc3100_Module._socket.sl_SetSockOpt(sock,SOL_SOCKET,SL_SO_NONBLOCKING, &enableOption,sizeof(enableOption)) < 0) 00171 { 00172 CloseTCPClientSocket(sock); 00173 return -1; 00174 } 00175 00176 timeVal.tv_sec = 1; // 1 Seconds 00177 timeVal.tv_usec = 0; // Microseconds. 10000 microseconds resoultion 00178 if((_cc3100_Module._socket.sl_SetSockOpt(sock,SOL_SOCKET,SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal))) < 0) 00179 { 00180 CloseTCPClientSocket(sock); 00181 return -1; 00182 }// Enable receive timeout 00183 } 00184 return sock; 00185 } 00186 00187 int CloseTCPClientSocket(int iSockDesc) 00188 { 00189 int ittr = 0; 00190 00191 if(iSockDesc < 0) 00192 { 00193 return 0; 00194 } 00195 00196 do 00197 { 00198 if(_cc3100_Module._socket.sl_Close(iSockDesc) >= 0) 00199 { 00200 iSockDesc = -1; 00201 // HttpDebug("\n Http client socket closed\n\r"); 00202 00203 return 0; 00204 } 00205 else 00206 { 00207 HttpDebug("\n client socket close error\n\r"); 00208 OSI_DELAY(500);//wait 500ms 00209 } 00210 ittr++; 00211 }while(ittr < 3); 00212 00213 return -1; 00214 } 00215 00216 int ClientSocketSend(long socket, char * buffer, unsigned int len) 00217 { 00218 int send_len = 0, Ittr = 0; 00219 00220 do 00221 { 00222 send_len = (int)_cc3100_Module._socket.send((int)socket, buffer, (int)len, 0); 00223 00224 if(send_len > 0) 00225 { 00226 if(len != send_len) 00227 { 00228 HttpDebug("client Send length is not matching %d \n\r", send_len); 00229 send_len = -1; 00230 } 00231 return send_len; 00232 } 00233 else if(send_len != SL_EAGAIN) 00234 { 00235 HttpDebug("\n client socket send error %d\n\r", send_len); 00236 return -1; 00237 } 00238 00239 Ittr++; 00240 00241 } while((SL_EAGAIN == send_len)); 00242 00243 HttpDebug("\n client send time out %d\n\r", send_len); 00244 00245 return -1; 00246 } 00247 00248 00249 /// @} 00250
Generated on Tue Jul 12 2022 22:22:38 by 1.7.2