Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file main.c
Sergunb 0:8918a71cdbe9 3 * @brief Main routine
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 10 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 11 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 12 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 13 *
Sergunb 0:8918a71cdbe9 14 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 17 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 18 *
Sergunb 0:8918a71cdbe9 19 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 20 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 22 *
Sergunb 0:8918a71cdbe9 23 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 24 * @version 1.7.6
Sergunb 0:8918a71cdbe9 25 **/
Sergunb 0:8918a71cdbe9 26
Sergunb 0:8918a71cdbe9 27 //Dependencies
Sergunb 0:8918a71cdbe9 28 #include <stdlib.h>
Sergunb 0:8918a71cdbe9 29 #include "stm32f7xx.h"
Sergunb 0:8918a71cdbe9 30 #include "stm32f7xx_hal.h"
Sergunb 0:8918a71cdbe9 31 #include "stm32f7xx_nucleo_144.h"
Sergunb 0:8918a71cdbe9 32 #include "os_port.h"
Sergunb 0:8918a71cdbe9 33 #include "core/net.h"
Sergunb 0:8918a71cdbe9 34 #include "drivers/stm32f7xx_eth.h"
Sergunb 0:8918a71cdbe9 35 #include "drivers/lan8742.h"
Sergunb 0:8918a71cdbe9 36 #include "dhcp/dhcp_client.h"
Sergunb 0:8918a71cdbe9 37 #include "ipv6/slaac.h"
Sergunb 0:8918a71cdbe9 38 #include "http/http_server.h"
Sergunb 0:8918a71cdbe9 39 #include "http/mime.h"
Sergunb 0:8918a71cdbe9 40 #include "path.h"
Sergunb 0:8918a71cdbe9 41 #include "date_time.h"
Sergunb 0:8918a71cdbe9 42 #include "resource_manager.h"
Sergunb 0:8918a71cdbe9 43 #include "debug.h"
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 //Application configuration
Sergunb 0:8918a71cdbe9 46 #define APP_MAC_ADDR "00-AB-CD-EF-07-46"
Sergunb 0:8918a71cdbe9 47
Sergunb 0:8918a71cdbe9 48 #define APP_USE_DHCP ENABLED
Sergunb 0:8918a71cdbe9 49 #define APP_IPV4_HOST_ADDR "192.168.0.20"
Sergunb 0:8918a71cdbe9 50 #define APP_IPV4_SUBNET_MASK "255.255.255.0"
Sergunb 0:8918a71cdbe9 51 #define APP_IPV4_DEFAULT_GATEWAY "192.168.0.254"
Sergunb 0:8918a71cdbe9 52 #define APP_IPV4_PRIMARY_DNS "8.8.8.8"
Sergunb 0:8918a71cdbe9 53 #define APP_IPV4_SECONDARY_DNS "8.8.4.4"
Sergunb 0:8918a71cdbe9 54
Sergunb 0:8918a71cdbe9 55 #define APP_USE_SLAAC ENABLED
Sergunb 0:8918a71cdbe9 56 #define APP_IPV6_LINK_LOCAL_ADDR "fe80::746"
Sergunb 0:8918a71cdbe9 57 #define APP_IPV6_PREFIX "2001:db8::"
Sergunb 0:8918a71cdbe9 58 #define APP_IPV6_PREFIX_LENGTH 64
Sergunb 0:8918a71cdbe9 59 #define APP_IPV6_GLOBAL_ADDR "2001:db8::746"
Sergunb 0:8918a71cdbe9 60 #define APP_IPV6_ROUTER "fe80::1"
Sergunb 0:8918a71cdbe9 61 #define APP_IPV6_PRIMARY_DNS "2001:4860:4860::8888"
Sergunb 0:8918a71cdbe9 62 #define APP_IPV6_SECONDARY_DNS "2001:4860:4860::8844"
Sergunb 0:8918a71cdbe9 63
Sergunb 0:8918a71cdbe9 64 #define APP_HTTP_MAX_CONNECTIONS 8
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66 //Global variables
Sergunb 0:8918a71cdbe9 67 DhcpClientSettings dhcpClientSettings;
Sergunb 0:8918a71cdbe9 68 DhcpClientContext dhcpClientContext;
Sergunb 0:8918a71cdbe9 69 SlaacSettings slaacSettings;
Sergunb 0:8918a71cdbe9 70 SlaacContext slaacContext;
Sergunb 0:8918a71cdbe9 71 HttpServerSettings httpServerSettings;
Sergunb 0:8918a71cdbe9 72 HttpServerContext httpServerContext;
Sergunb 0:8918a71cdbe9 73 HttpConnection httpConnections[APP_HTTP_MAX_CONNECTIONS];
Sergunb 0:8918a71cdbe9 74
Sergunb 0:8918a71cdbe9 75 //Forward declaration of functions
Sergunb 0:8918a71cdbe9 76 error_t httpServerCgiCallback(HttpConnection *connection,
Sergunb 0:8918a71cdbe9 77 const char_t *param);
Sergunb 0:8918a71cdbe9 78
Sergunb 0:8918a71cdbe9 79 error_t httpServerUriNotFoundCallback(HttpConnection *connection,
Sergunb 0:8918a71cdbe9 80 const char_t *uri);
Sergunb 0:8918a71cdbe9 81
Sergunb 0:8918a71cdbe9 82
Sergunb 0:8918a71cdbe9 83 /**
Sergunb 0:8918a71cdbe9 84 * @brief System clock configuration
Sergunb 0:8918a71cdbe9 85 **/
Sergunb 0:8918a71cdbe9 86
Sergunb 0:8918a71cdbe9 87 void SystemClock_Config(void)
Sergunb 0:8918a71cdbe9 88 {
Sergunb 0:8918a71cdbe9 89 RCC_OscInitTypeDef RCC_OscInitStruct;
Sergunb 0:8918a71cdbe9 90 RCC_ClkInitTypeDef RCC_ClkInitStruct;
Sergunb 0:8918a71cdbe9 91
Sergunb 0:8918a71cdbe9 92 //Enable Power Control clock
Sergunb 0:8918a71cdbe9 93 __HAL_RCC_PWR_CLK_ENABLE();
Sergunb 0:8918a71cdbe9 94
Sergunb 0:8918a71cdbe9 95 //Enable HSE Oscillator and activate PLL with HSE as source
Sergunb 0:8918a71cdbe9 96 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
Sergunb 0:8918a71cdbe9 97 RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
Sergunb 0:8918a71cdbe9 98 RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
Sergunb 0:8918a71cdbe9 99 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Sergunb 0:8918a71cdbe9 100 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
Sergunb 0:8918a71cdbe9 101 RCC_OscInitStruct.PLL.PLLM = 8;
Sergunb 0:8918a71cdbe9 102 RCC_OscInitStruct.PLL.PLLN = 432;
Sergunb 0:8918a71cdbe9 103 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Sergunb 0:8918a71cdbe9 104 RCC_OscInitStruct.PLL.PLLQ = 9;
Sergunb 0:8918a71cdbe9 105 HAL_RCC_OscConfig(&RCC_OscInitStruct);
Sergunb 0:8918a71cdbe9 106
Sergunb 0:8918a71cdbe9 107 //Enable overdrive mode
Sergunb 0:8918a71cdbe9 108 HAL_PWREx_EnableOverDrive();
Sergunb 0:8918a71cdbe9 109
Sergunb 0:8918a71cdbe9 110 //Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
Sergunb 0:8918a71cdbe9 111 //clocks dividers
Sergunb 0:8918a71cdbe9 112 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
Sergunb 0:8918a71cdbe9 113 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Sergunb 0:8918a71cdbe9 114 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Sergunb 0:8918a71cdbe9 115 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Sergunb 0:8918a71cdbe9 116 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Sergunb 0:8918a71cdbe9 117 HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
Sergunb 0:8918a71cdbe9 118 }
Sergunb 0:8918a71cdbe9 119
Sergunb 0:8918a71cdbe9 120
Sergunb 0:8918a71cdbe9 121 /**
Sergunb 0:8918a71cdbe9 122 * @brief MPU configuration
Sergunb 0:8918a71cdbe9 123 **/
Sergunb 0:8918a71cdbe9 124
Sergunb 0:8918a71cdbe9 125 void MPU_Config(void)
Sergunb 0:8918a71cdbe9 126 {
Sergunb 0:8918a71cdbe9 127 MPU_Region_InitTypeDef MPU_InitStruct;
Sergunb 0:8918a71cdbe9 128
Sergunb 0:8918a71cdbe9 129 //Disable MPU
Sergunb 0:8918a71cdbe9 130 HAL_MPU_Disable();
Sergunb 0:8918a71cdbe9 131
Sergunb 0:8918a71cdbe9 132 //SRAM
Sergunb 0:8918a71cdbe9 133 MPU_InitStruct.Enable = MPU_REGION_ENABLE;
Sergunb 0:8918a71cdbe9 134 MPU_InitStruct.Number = MPU_REGION_NUMBER0;
Sergunb 0:8918a71cdbe9 135 MPU_InitStruct.BaseAddress = 0x20000000;
Sergunb 0:8918a71cdbe9 136 MPU_InitStruct.Size = MPU_REGION_SIZE_512KB;
Sergunb 0:8918a71cdbe9 137 MPU_InitStruct.SubRegionDisable = 0;
Sergunb 0:8918a71cdbe9 138 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
Sergunb 0:8918a71cdbe9 139 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
Sergunb 0:8918a71cdbe9 140 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
Sergunb 0:8918a71cdbe9 141 MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
Sergunb 0:8918a71cdbe9 142 MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
Sergunb 0:8918a71cdbe9 143 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
Sergunb 0:8918a71cdbe9 144 HAL_MPU_ConfigRegion(&MPU_InitStruct);
Sergunb 0:8918a71cdbe9 145
Sergunb 0:8918a71cdbe9 146 //SRAM2
Sergunb 0:8918a71cdbe9 147 MPU_InitStruct.Enable = MPU_REGION_ENABLE;
Sergunb 0:8918a71cdbe9 148 MPU_InitStruct.Number = MPU_REGION_NUMBER1;
Sergunb 0:8918a71cdbe9 149 MPU_InitStruct.BaseAddress = 0x2004C000;
Sergunb 0:8918a71cdbe9 150 MPU_InitStruct.Size = MPU_REGION_SIZE_16KB;
Sergunb 0:8918a71cdbe9 151 MPU_InitStruct.SubRegionDisable = 0;
Sergunb 0:8918a71cdbe9 152 MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
Sergunb 0:8918a71cdbe9 153 MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
Sergunb 0:8918a71cdbe9 154 MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
Sergunb 0:8918a71cdbe9 155 MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
Sergunb 0:8918a71cdbe9 156 MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
Sergunb 0:8918a71cdbe9 157 MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
Sergunb 0:8918a71cdbe9 158 HAL_MPU_ConfigRegion(&MPU_InitStruct);
Sergunb 0:8918a71cdbe9 159
Sergunb 0:8918a71cdbe9 160 //Enable MPU
Sergunb 0:8918a71cdbe9 161 HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
Sergunb 0:8918a71cdbe9 162 }
Sergunb 0:8918a71cdbe9 163
Sergunb 0:8918a71cdbe9 164
Sergunb 0:8918a71cdbe9 165 /**
Sergunb 0:8918a71cdbe9 166 * @brief LED blinking task
Sergunb 0:8918a71cdbe9 167 **/
Sergunb 0:8918a71cdbe9 168
Sergunb 0:8918a71cdbe9 169 void blinkTask(void *param)
Sergunb 0:8918a71cdbe9 170 {
Sergunb 0:8918a71cdbe9 171 //Endless loop
Sergunb 0:8918a71cdbe9 172 while(1)
Sergunb 0:8918a71cdbe9 173 {
Sergunb 0:8918a71cdbe9 174 BSP_LED_On(LED1);
Sergunb 0:8918a71cdbe9 175 osDelayTask(100);
Sergunb 0:8918a71cdbe9 176 BSP_LED_Off(LED1);
Sergunb 0:8918a71cdbe9 177 osDelayTask(900);
Sergunb 0:8918a71cdbe9 178 }
Sergunb 0:8918a71cdbe9 179 }
Sergunb 0:8918a71cdbe9 180
Sergunb 0:8918a71cdbe9 181
Sergunb 0:8918a71cdbe9 182 /**
Sergunb 0:8918a71cdbe9 183 * @brief Main entry point
Sergunb 0:8918a71cdbe9 184 * @return Unused value
Sergunb 0:8918a71cdbe9 185 **/
Sergunb 0:8918a71cdbe9 186
Sergunb 0:8918a71cdbe9 187 int_t main(void)
Sergunb 0:8918a71cdbe9 188 {
Sergunb 0:8918a71cdbe9 189 error_t error;
Sergunb 0:8918a71cdbe9 190 NetInterface *interface;
Sergunb 0:8918a71cdbe9 191 OsTask *task;
Sergunb 0:8918a71cdbe9 192 MacAddr macAddr;
Sergunb 0:8918a71cdbe9 193 #if (APP_USE_DHCP == DISABLED)
Sergunb 0:8918a71cdbe9 194 Ipv4Addr ipv4Addr;
Sergunb 0:8918a71cdbe9 195 #endif
Sergunb 0:8918a71cdbe9 196 #if (APP_USE_SLAAC == DISABLED)
Sergunb 0:8918a71cdbe9 197 Ipv6Addr ipv6Addr;
Sergunb 0:8918a71cdbe9 198 #endif
Sergunb 0:8918a71cdbe9 199
Sergunb 0:8918a71cdbe9 200 //MPU configuration
Sergunb 0:8918a71cdbe9 201 MPU_Config();
Sergunb 0:8918a71cdbe9 202 //HAL library initialization
Sergunb 0:8918a71cdbe9 203 HAL_Init();
Sergunb 0:8918a71cdbe9 204 //Configure the system clock
Sergunb 0:8918a71cdbe9 205 SystemClock_Config();
Sergunb 0:8918a71cdbe9 206
Sergunb 0:8918a71cdbe9 207 //Enable I-cache and D-cache
Sergunb 0:8918a71cdbe9 208 SCB_EnableICache();
Sergunb 0:8918a71cdbe9 209 SCB_EnableDCache();
Sergunb 0:8918a71cdbe9 210
Sergunb 0:8918a71cdbe9 211 //Initialize kernel
Sergunb 0:8918a71cdbe9 212 osInitKernel();
Sergunb 0:8918a71cdbe9 213 //Configure debug UART
Sergunb 0:8918a71cdbe9 214 debugInit(115200);
Sergunb 0:8918a71cdbe9 215
Sergunb 0:8918a71cdbe9 216 //Start-up message
Sergunb 0:8918a71cdbe9 217 TRACE_INFO("\r\n");
Sergunb 0:8918a71cdbe9 218 TRACE_INFO("**********************************\r\n");
Sergunb 0:8918a71cdbe9 219 TRACE_INFO("*** CycloneTCP Web Server Demo ***\r\n");
Sergunb 0:8918a71cdbe9 220 TRACE_INFO("**********************************\r\n");
Sergunb 0:8918a71cdbe9 221 TRACE_INFO("Copyright: 2010-2017 Oryx Embedded SARL\r\n");
Sergunb 0:8918a71cdbe9 222 TRACE_INFO("Compiled: %s %s\r\n", __DATE__, __TIME__);
Sergunb 0:8918a71cdbe9 223 TRACE_INFO("Target: STM32F746\r\n");
Sergunb 0:8918a71cdbe9 224 TRACE_INFO("\r\n");
Sergunb 0:8918a71cdbe9 225
Sergunb 0:8918a71cdbe9 226 //LED configuration
Sergunb 0:8918a71cdbe9 227 BSP_LED_Init(LED1);
Sergunb 0:8918a71cdbe9 228 BSP_LED_Init(LED2);
Sergunb 0:8918a71cdbe9 229 BSP_LED_Init(LED3);
Sergunb 0:8918a71cdbe9 230
Sergunb 0:8918a71cdbe9 231 //Clear LEDs
Sergunb 0:8918a71cdbe9 232 BSP_LED_Off(LED1);
Sergunb 0:8918a71cdbe9 233 BSP_LED_Off(LED2);
Sergunb 0:8918a71cdbe9 234 BSP_LED_Off(LED3);
Sergunb 0:8918a71cdbe9 235
Sergunb 0:8918a71cdbe9 236 //Initialize user button
Sergunb 0:8918a71cdbe9 237 BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
Sergunb 0:8918a71cdbe9 238
Sergunb 0:8918a71cdbe9 239 //TCP/IP stack initialization
Sergunb 0:8918a71cdbe9 240 error = netInit();
Sergunb 0:8918a71cdbe9 241 //Any error to report?
Sergunb 0:8918a71cdbe9 242 if(error)
Sergunb 0:8918a71cdbe9 243 {
Sergunb 0:8918a71cdbe9 244 //Debug message
Sergunb 0:8918a71cdbe9 245 TRACE_ERROR("Failed to initialize TCP/IP stack!\r\n");
Sergunb 0:8918a71cdbe9 246 }
Sergunb 0:8918a71cdbe9 247
Sergunb 0:8918a71cdbe9 248 //Configure the first Ethernet interface
Sergunb 0:8918a71cdbe9 249 interface = &netInterface[0];
Sergunb 0:8918a71cdbe9 250
Sergunb 0:8918a71cdbe9 251 //Set interface name
Sergunb 0:8918a71cdbe9 252 netSetInterfaceName(interface, "eth0");
Sergunb 0:8918a71cdbe9 253 //Set host name
Sergunb 0:8918a71cdbe9 254 netSetHostname(interface, "WebServerDemo");
Sergunb 0:8918a71cdbe9 255 //Select the relevant network adapter
Sergunb 0:8918a71cdbe9 256 netSetDriver(interface, &stm32f7xxEthDriver);
Sergunb 0:8918a71cdbe9 257 netSetPhyDriver(interface, &lan8742PhyDriver);
Sergunb 0:8918a71cdbe9 258 //Set host MAC address
Sergunb 0:8918a71cdbe9 259 macStringToAddr(APP_MAC_ADDR, &macAddr);
Sergunb 0:8918a71cdbe9 260 netSetMacAddr(interface, &macAddr);
Sergunb 0:8918a71cdbe9 261
Sergunb 0:8918a71cdbe9 262 //Initialize network interface
Sergunb 0:8918a71cdbe9 263 error = netConfigInterface(interface);
Sergunb 0:8918a71cdbe9 264 //Any error to report?
Sergunb 0:8918a71cdbe9 265 if(error)
Sergunb 0:8918a71cdbe9 266 {
Sergunb 0:8918a71cdbe9 267 //Debug message
Sergunb 0:8918a71cdbe9 268 TRACE_ERROR("Failed to configure interface %s!\r\n", interface->name);
Sergunb 0:8918a71cdbe9 269 }
Sergunb 0:8918a71cdbe9 270
Sergunb 0:8918a71cdbe9 271 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 272 #if (APP_USE_DHCP == ENABLED)
Sergunb 0:8918a71cdbe9 273 //Get default settings
Sergunb 0:8918a71cdbe9 274 dhcpClientGetDefaultSettings(&dhcpClientSettings);
Sergunb 0:8918a71cdbe9 275 //Set the network interface to be configured by DHCP
Sergunb 0:8918a71cdbe9 276 dhcpClientSettings.interface = interface;
Sergunb 0:8918a71cdbe9 277 //Disable rapid commit option
Sergunb 0:8918a71cdbe9 278 dhcpClientSettings.rapidCommit = FALSE;
Sergunb 0:8918a71cdbe9 279
Sergunb 0:8918a71cdbe9 280 //DHCP client initialization
Sergunb 0:8918a71cdbe9 281 error = dhcpClientInit(&dhcpClientContext, &dhcpClientSettings);
Sergunb 0:8918a71cdbe9 282 //Failed to initialize DHCP client?
Sergunb 0:8918a71cdbe9 283 if(error)
Sergunb 0:8918a71cdbe9 284 {
Sergunb 0:8918a71cdbe9 285 //Debug message
Sergunb 0:8918a71cdbe9 286 TRACE_ERROR("Failed to initialize DHCP client!\r\n");
Sergunb 0:8918a71cdbe9 287 }
Sergunb 0:8918a71cdbe9 288
Sergunb 0:8918a71cdbe9 289 //Start DHCP client
Sergunb 0:8918a71cdbe9 290 error = dhcpClientStart(&dhcpClientContext);
Sergunb 0:8918a71cdbe9 291 //Failed to start DHCP client?
Sergunb 0:8918a71cdbe9 292 if(error)
Sergunb 0:8918a71cdbe9 293 {
Sergunb 0:8918a71cdbe9 294 //Debug message
Sergunb 0:8918a71cdbe9 295 TRACE_ERROR("Failed to start DHCP client!\r\n");
Sergunb 0:8918a71cdbe9 296 }
Sergunb 0:8918a71cdbe9 297 #else
Sergunb 0:8918a71cdbe9 298 //Set IPv4 host address
Sergunb 0:8918a71cdbe9 299 ipv4StringToAddr(APP_IPV4_HOST_ADDR, &ipv4Addr);
Sergunb 0:8918a71cdbe9 300 ipv4SetHostAddr(interface, ipv4Addr);
Sergunb 0:8918a71cdbe9 301
Sergunb 0:8918a71cdbe9 302 //Set subnet mask
Sergunb 0:8918a71cdbe9 303 ipv4StringToAddr(APP_IPV4_SUBNET_MASK, &ipv4Addr);
Sergunb 0:8918a71cdbe9 304 ipv4SetSubnetMask(interface, ipv4Addr);
Sergunb 0:8918a71cdbe9 305
Sergunb 0:8918a71cdbe9 306 //Set default gateway
Sergunb 0:8918a71cdbe9 307 ipv4StringToAddr(APP_IPV4_DEFAULT_GATEWAY, &ipv4Addr);
Sergunb 0:8918a71cdbe9 308 ipv4SetDefaultGateway(interface, ipv4Addr);
Sergunb 0:8918a71cdbe9 309
Sergunb 0:8918a71cdbe9 310 //Set primary and secondary DNS servers
Sergunb 0:8918a71cdbe9 311 ipv4StringToAddr(APP_IPV4_PRIMARY_DNS, &ipv4Addr);
Sergunb 0:8918a71cdbe9 312 ipv4SetDnsServer(interface, 0, ipv4Addr);
Sergunb 0:8918a71cdbe9 313 ipv4StringToAddr(APP_IPV4_SECONDARY_DNS, &ipv4Addr);
Sergunb 0:8918a71cdbe9 314 ipv4SetDnsServer(interface, 1, ipv4Addr);
Sergunb 0:8918a71cdbe9 315 #endif
Sergunb 0:8918a71cdbe9 316 #endif
Sergunb 0:8918a71cdbe9 317
Sergunb 0:8918a71cdbe9 318 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 319 #if (APP_USE_SLAAC == ENABLED)
Sergunb 0:8918a71cdbe9 320 //Get default settings
Sergunb 0:8918a71cdbe9 321 slaacGetDefaultSettings(&slaacSettings);
Sergunb 0:8918a71cdbe9 322 //Set the network interface to be configured
Sergunb 0:8918a71cdbe9 323 slaacSettings.interface = interface;
Sergunb 0:8918a71cdbe9 324
Sergunb 0:8918a71cdbe9 325 //SLAAC initialization
Sergunb 0:8918a71cdbe9 326 error = slaacInit(&slaacContext, &slaacSettings);
Sergunb 0:8918a71cdbe9 327 //Failed to initialize SLAAC?
Sergunb 0:8918a71cdbe9 328 if(error)
Sergunb 0:8918a71cdbe9 329 {
Sergunb 0:8918a71cdbe9 330 //Debug message
Sergunb 0:8918a71cdbe9 331 TRACE_ERROR("Failed to initialize SLAAC!\r\n");
Sergunb 0:8918a71cdbe9 332 }
Sergunb 0:8918a71cdbe9 333
Sergunb 0:8918a71cdbe9 334 //Start IPv6 address autoconfiguration process
Sergunb 0:8918a71cdbe9 335 error = slaacStart(&slaacContext);
Sergunb 0:8918a71cdbe9 336 //Failed to start SLAAC process?
Sergunb 0:8918a71cdbe9 337 if(error)
Sergunb 0:8918a71cdbe9 338 {
Sergunb 0:8918a71cdbe9 339 //Debug message
Sergunb 0:8918a71cdbe9 340 TRACE_ERROR("Failed to start SLAAC!\r\n");
Sergunb 0:8918a71cdbe9 341 }
Sergunb 0:8918a71cdbe9 342 #else
Sergunb 0:8918a71cdbe9 343 //Set link-local address
Sergunb 0:8918a71cdbe9 344 ipv6StringToAddr(APP_IPV6_LINK_LOCAL_ADDR, &ipv6Addr);
Sergunb 0:8918a71cdbe9 345 ipv6SetLinkLocalAddr(interface, &ipv6Addr);
Sergunb 0:8918a71cdbe9 346
Sergunb 0:8918a71cdbe9 347 //Set IPv6 prefix
Sergunb 0:8918a71cdbe9 348 ipv6StringToAddr(APP_IPV6_PREFIX, &ipv6Addr);
Sergunb 0:8918a71cdbe9 349 ipv6SetPrefix(interface, 0, &ipv6Addr, APP_IPV6_PREFIX_LENGTH);
Sergunb 0:8918a71cdbe9 350
Sergunb 0:8918a71cdbe9 351 //Set global address
Sergunb 0:8918a71cdbe9 352 ipv6StringToAddr(APP_IPV6_GLOBAL_ADDR, &ipv6Addr);
Sergunb 0:8918a71cdbe9 353 ipv6SetGlobalAddr(interface, 0, &ipv6Addr);
Sergunb 0:8918a71cdbe9 354
Sergunb 0:8918a71cdbe9 355 //Set default router
Sergunb 0:8918a71cdbe9 356 ipv6StringToAddr(APP_IPV6_ROUTER, &ipv6Addr);
Sergunb 0:8918a71cdbe9 357 ipv6SetDefaultRouter(interface, 0, &ipv6Addr);
Sergunb 0:8918a71cdbe9 358
Sergunb 0:8918a71cdbe9 359 //Set primary and secondary DNS servers
Sergunb 0:8918a71cdbe9 360 ipv6StringToAddr(APP_IPV6_PRIMARY_DNS, &ipv6Addr);
Sergunb 0:8918a71cdbe9 361 ipv6SetDnsServer(interface, 0, &ipv6Addr);
Sergunb 0:8918a71cdbe9 362 ipv6StringToAddr(APP_IPV6_SECONDARY_DNS, &ipv6Addr);
Sergunb 0:8918a71cdbe9 363 ipv6SetDnsServer(interface, 1, &ipv6Addr);
Sergunb 0:8918a71cdbe9 364 #endif
Sergunb 0:8918a71cdbe9 365 #endif
Sergunb 0:8918a71cdbe9 366
Sergunb 0:8918a71cdbe9 367 //Get default settings
Sergunb 0:8918a71cdbe9 368 httpServerGetDefaultSettings(&httpServerSettings);
Sergunb 0:8918a71cdbe9 369 //Bind HTTP server to the desired interface
Sergunb 0:8918a71cdbe9 370 httpServerSettings.interface = &netInterface[0];
Sergunb 0:8918a71cdbe9 371 //Listen to port 80
Sergunb 0:8918a71cdbe9 372 httpServerSettings.port = HTTP_PORT;
Sergunb 0:8918a71cdbe9 373 //Client connections
Sergunb 0:8918a71cdbe9 374 httpServerSettings.maxConnections = APP_HTTP_MAX_CONNECTIONS;
Sergunb 0:8918a71cdbe9 375 httpServerSettings.connections = httpConnections;
Sergunb 0:8918a71cdbe9 376 //Specify the server's root directory
Sergunb 0:8918a71cdbe9 377 strcpy(httpServerSettings.rootDirectory, "/www/");
Sergunb 0:8918a71cdbe9 378 //Set default home page
Sergunb 0:8918a71cdbe9 379 strcpy(httpServerSettings.defaultDocument, "index.shtm");
Sergunb 0:8918a71cdbe9 380 //Callback functions
Sergunb 0:8918a71cdbe9 381 httpServerSettings.cgiCallback = httpServerCgiCallback;
Sergunb 0:8918a71cdbe9 382 httpServerSettings.uriNotFoundCallback = httpServerUriNotFoundCallback;
Sergunb 0:8918a71cdbe9 383
Sergunb 0:8918a71cdbe9 384 //HTTP server initialization
Sergunb 0:8918a71cdbe9 385 error = httpServerInit(&httpServerContext, &httpServerSettings);
Sergunb 0:8918a71cdbe9 386 //Failed to initialize HTTP server?
Sergunb 0:8918a71cdbe9 387 if(error)
Sergunb 0:8918a71cdbe9 388 {
Sergunb 0:8918a71cdbe9 389 //Debug message
Sergunb 0:8918a71cdbe9 390 TRACE_ERROR("Failed to initialize HTTP server!\r\n");
Sergunb 0:8918a71cdbe9 391 }
Sergunb 0:8918a71cdbe9 392
Sergunb 0:8918a71cdbe9 393 //Start HTTP server
Sergunb 0:8918a71cdbe9 394 error = httpServerStart(&httpServerContext);
Sergunb 0:8918a71cdbe9 395 //Failed to start HTTP server?
Sergunb 0:8918a71cdbe9 396 if(error)
Sergunb 0:8918a71cdbe9 397 {
Sergunb 0:8918a71cdbe9 398 //Debug message
Sergunb 0:8918a71cdbe9 399 TRACE_ERROR("Failed to start HTTP server!\r\n");
Sergunb 0:8918a71cdbe9 400 }
Sergunb 0:8918a71cdbe9 401
Sergunb 0:8918a71cdbe9 402 //Create a task to blink the LED
Sergunb 0:8918a71cdbe9 403 task = osCreateTask("Blink", blinkTask, NULL, 500, OS_TASK_PRIORITY_NORMAL);
Sergunb 0:8918a71cdbe9 404 //Failed to create the task?
Sergunb 0:8918a71cdbe9 405 if(task == OS_INVALID_HANDLE)
Sergunb 0:8918a71cdbe9 406 {
Sergunb 0:8918a71cdbe9 407 //Debug message
Sergunb 0:8918a71cdbe9 408 TRACE_ERROR("Failed to create task!\r\n");
Sergunb 0:8918a71cdbe9 409 }
Sergunb 0:8918a71cdbe9 410
Sergunb 0:8918a71cdbe9 411 //Start the execution of tasks
Sergunb 0:8918a71cdbe9 412 osStartKernel();
Sergunb 0:8918a71cdbe9 413
Sergunb 0:8918a71cdbe9 414 //This function should never return
Sergunb 0:8918a71cdbe9 415 return 0;
Sergunb 0:8918a71cdbe9 416 }
Sergunb 0:8918a71cdbe9 417
Sergunb 0:8918a71cdbe9 418
Sergunb 0:8918a71cdbe9 419 /**
Sergunb 0:8918a71cdbe9 420 * @brief CGI callback function
Sergunb 0:8918a71cdbe9 421 * @param[in] connection Handle referencing a client connection
Sergunb 0:8918a71cdbe9 422 * @param[in] param NULL-terminated string that contains the CGI parameter
Sergunb 0:8918a71cdbe9 423 * @return Error code
Sergunb 0:8918a71cdbe9 424 **/
Sergunb 0:8918a71cdbe9 425
Sergunb 0:8918a71cdbe9 426 error_t httpServerCgiCallback(HttpConnection *connection,
Sergunb 0:8918a71cdbe9 427 const char_t *param)
Sergunb 0:8918a71cdbe9 428 {
Sergunb 0:8918a71cdbe9 429 static uint_t pageCounter = 0;
Sergunb 0:8918a71cdbe9 430 uint_t length;
Sergunb 0:8918a71cdbe9 431 MacAddr macAddr;
Sergunb 0:8918a71cdbe9 432 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 433 Ipv4Addr ipv4Addr;
Sergunb 0:8918a71cdbe9 434 #endif
Sergunb 0:8918a71cdbe9 435 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 436 uint_t n;
Sergunb 0:8918a71cdbe9 437 Ipv6Addr ipv6Addr;
Sergunb 0:8918a71cdbe9 438 #endif
Sergunb 0:8918a71cdbe9 439
Sergunb 0:8918a71cdbe9 440 //Underlying network interface
Sergunb 0:8918a71cdbe9 441 NetInterface *interface = connection->socket->interface;
Sergunb 0:8918a71cdbe9 442
Sergunb 0:8918a71cdbe9 443 //Check parameter name
Sergunb 0:8918a71cdbe9 444 if(!strcasecmp(param, "PAGE_COUNTER"))
Sergunb 0:8918a71cdbe9 445 {
Sergunb 0:8918a71cdbe9 446 pageCounter++;
Sergunb 0:8918a71cdbe9 447 sprintf(connection->buffer, "%u time%s", pageCounter, (pageCounter >= 2) ? "s" : "");
Sergunb 0:8918a71cdbe9 448 }
Sergunb 0:8918a71cdbe9 449 else if(!strcasecmp(param, "BOARD_NAME"))
Sergunb 0:8918a71cdbe9 450 {
Sergunb 0:8918a71cdbe9 451 strcpy(connection->buffer, "Nucleo-F746ZG");
Sergunb 0:8918a71cdbe9 452 }
Sergunb 0:8918a71cdbe9 453 else if(!strcasecmp(param, "SYSTEM_TIME"))
Sergunb 0:8918a71cdbe9 454 {
Sergunb 0:8918a71cdbe9 455 systime_t time = osGetSystemTime();
Sergunb 0:8918a71cdbe9 456 formatSystemTime(time, connection->buffer);
Sergunb 0:8918a71cdbe9 457 }
Sergunb 0:8918a71cdbe9 458 else if(!strcasecmp(param, "MAC_ADDR"))
Sergunb 0:8918a71cdbe9 459 {
Sergunb 0:8918a71cdbe9 460 netGetMacAddr(interface, &macAddr);
Sergunb 0:8918a71cdbe9 461 macAddrToString(&macAddr, connection->buffer);
Sergunb 0:8918a71cdbe9 462 }
Sergunb 0:8918a71cdbe9 463 else if(!strcasecmp(param, "IPV4_ADDR"))
Sergunb 0:8918a71cdbe9 464 {
Sergunb 0:8918a71cdbe9 465 ipv4GetHostAddr(interface, &ipv4Addr);
Sergunb 0:8918a71cdbe9 466 ipv4AddrToString(ipv4Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 467 }
Sergunb 0:8918a71cdbe9 468 else if(!strcasecmp(param, "SUBNET_MASK"))
Sergunb 0:8918a71cdbe9 469 {
Sergunb 0:8918a71cdbe9 470 ipv4GetSubnetMask(interface, &ipv4Addr);
Sergunb 0:8918a71cdbe9 471 ipv4AddrToString(ipv4Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 472 }
Sergunb 0:8918a71cdbe9 473 else if(!strcasecmp(param, "DEFAULT_GATEWAY"))
Sergunb 0:8918a71cdbe9 474 {
Sergunb 0:8918a71cdbe9 475 ipv4GetDefaultGateway(interface, &ipv4Addr);
Sergunb 0:8918a71cdbe9 476 ipv4AddrToString(ipv4Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 477 }
Sergunb 0:8918a71cdbe9 478 else if(!strcasecmp(param, "IPV4_PRIMARY_DNS"))
Sergunb 0:8918a71cdbe9 479 {
Sergunb 0:8918a71cdbe9 480 ipv4GetDnsServer(interface, 0, &ipv4Addr);
Sergunb 0:8918a71cdbe9 481 ipv4AddrToString(ipv4Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 482 }
Sergunb 0:8918a71cdbe9 483 else if(!strcasecmp(param, "IPV4_SECONDARY_DNS"))
Sergunb 0:8918a71cdbe9 484 {
Sergunb 0:8918a71cdbe9 485 ipv4GetDnsServer(interface, 1, &ipv4Addr);
Sergunb 0:8918a71cdbe9 486 ipv4AddrToString(ipv4Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 487 }
Sergunb 0:8918a71cdbe9 488 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 489 else if(!strcasecmp(param, "LINK_LOCAL_ADDR"))
Sergunb 0:8918a71cdbe9 490 {
Sergunb 0:8918a71cdbe9 491 ipv6GetLinkLocalAddr(interface, &ipv6Addr);
Sergunb 0:8918a71cdbe9 492 ipv6AddrToString(&ipv6Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 493 }
Sergunb 0:8918a71cdbe9 494 else if(!strcasecmp(param, "GLOBAL_ADDR"))
Sergunb 0:8918a71cdbe9 495 {
Sergunb 0:8918a71cdbe9 496 ipv6GetGlobalAddr(interface, 0, &ipv6Addr);
Sergunb 0:8918a71cdbe9 497 ipv6AddrToString(&ipv6Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 498 }
Sergunb 0:8918a71cdbe9 499 else if(!strcasecmp(param, "IPV6_PREFIX"))
Sergunb 0:8918a71cdbe9 500 {
Sergunb 0:8918a71cdbe9 501 ipv6GetPrefix(interface, 0, &ipv6Addr, &n);
Sergunb 0:8918a71cdbe9 502 ipv6AddrToString(&ipv6Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 503 length = strlen(connection->buffer);
Sergunb 0:8918a71cdbe9 504 sprintf(connection->buffer + length, "/%u", n);
Sergunb 0:8918a71cdbe9 505 }
Sergunb 0:8918a71cdbe9 506 else if(!strcasecmp(param, "ROUTER"))
Sergunb 0:8918a71cdbe9 507 {
Sergunb 0:8918a71cdbe9 508 ipv6GetDefaultRouter(interface, 0, &ipv6Addr);
Sergunb 0:8918a71cdbe9 509 ipv6AddrToString(&ipv6Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 510 }
Sergunb 0:8918a71cdbe9 511 else if(!strcasecmp(param, "IPV6_PRIMARY_DNS"))
Sergunb 0:8918a71cdbe9 512 {
Sergunb 0:8918a71cdbe9 513 ipv6GetDnsServer(interface, 0, &ipv6Addr);
Sergunb 0:8918a71cdbe9 514 ipv6AddrToString(&ipv6Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 515 }
Sergunb 0:8918a71cdbe9 516 else if(!strcasecmp(param, "IPV6_SECONDARY_DNS"))
Sergunb 0:8918a71cdbe9 517 {
Sergunb 0:8918a71cdbe9 518 ipv6GetDnsServer(interface, 1, &ipv6Addr);
Sergunb 0:8918a71cdbe9 519 ipv6AddrToString(&ipv6Addr, connection->buffer);
Sergunb 0:8918a71cdbe9 520 }
Sergunb 0:8918a71cdbe9 521 #endif
Sergunb 0:8918a71cdbe9 522 else
Sergunb 0:8918a71cdbe9 523 {
Sergunb 0:8918a71cdbe9 524 return ERROR_INVALID_TAG;
Sergunb 0:8918a71cdbe9 525 }
Sergunb 0:8918a71cdbe9 526
Sergunb 0:8918a71cdbe9 527 //Get the length of the resulting string
Sergunb 0:8918a71cdbe9 528 length = strlen(connection->buffer);
Sergunb 0:8918a71cdbe9 529
Sergunb 0:8918a71cdbe9 530 //Send the contents of the specified environment variable
Sergunb 0:8918a71cdbe9 531 return httpWriteStream(connection, connection->buffer, length);
Sergunb 0:8918a71cdbe9 532 }
Sergunb 0:8918a71cdbe9 533
Sergunb 0:8918a71cdbe9 534
Sergunb 0:8918a71cdbe9 535 /**
Sergunb 0:8918a71cdbe9 536 * @brief URI not found callback
Sergunb 0:8918a71cdbe9 537 * @param[in] connection Handle referencing a client connection
Sergunb 0:8918a71cdbe9 538 * @param[in] uri NULL-terminated string containing the path to the requested resource
Sergunb 0:8918a71cdbe9 539 * @return Error code
Sergunb 0:8918a71cdbe9 540 **/
Sergunb 0:8918a71cdbe9 541
Sergunb 0:8918a71cdbe9 542 error_t httpServerUriNotFoundCallback(HttpConnection *connection,
Sergunb 0:8918a71cdbe9 543 const char_t *uri)
Sergunb 0:8918a71cdbe9 544 {
Sergunb 0:8918a71cdbe9 545 //Not implemented
Sergunb 0:8918a71cdbe9 546 return ERROR_NOT_FOUND;
Sergunb 0:8918a71cdbe9 547 }
Sergunb 0:8918a71cdbe9 548
Sergunb 0:8918a71cdbe9 549