void Main_Task(uint_32 initial_data) { HTTPD_STRUCT * server; int_32 error; SEC_Initialize(); _task_create(0, IO_TASK, 0); // start io task SEC_InitializeNetworking(5, 6, 4, DEMOCFG_ENABLE_DHCP); /* Set up Trival File System to server webpages */ if ((error = _io_tfs_install("tfs:", tfs_data))) { printf("\nTFS install returned: %08x\n", error); } server = httpd_server_init((HTTPD_ROOT_DIR_STRUCT*)root_dir, "\\mqx.html"); HTTPD_SET_PARAM_CGI_TBL(server, (HTTPD_CGI_LINK_STRUCT*)cgi_lnk_tbl); #if HTTPDCFG_POLL_MODE while (1) { /* Poll Webserver */ httpd_server_poll(server, -1); } #else httpd_server_run(server); _task_block(); #endif }
/** server task - httpd run in one task and poll each session */ static void httpd_server_task(pointer init_ptr, pointer creator) { HTTPD_STRUCT *server = (HTTPD_STRUCT*)init_ptr; HTTPD_ASSERT(server); if (!server) goto ERROR; RTCS_task_resume_creator(creator, RTCS_OK); while (1) { httpd_server_poll(server, 1); } ERROR: RTCS_task_resume_creator(creator, (uint_32)RTCS_ERROR); }
/*TASK*----------------------------------------------------------------- * * Function Name : main_task * Returned Value : void * Comments : * *END------------------------------------------------------------------*/ void main_task(uint_32 temp) { int_32 error; HTTPD_STRUCT *server[BSP_ENET_DEVICE_COUNT]; extern const HTTPD_CGI_LINK_STRUCT cgi_lnk_tbl[]; extern const TFS_DIR_ENTRY tfs_data[]; HTTPD_PARAMS_STRUCT *params[BSP_ENET_DEVICE_COUNT]; _enet_address address[BSP_ENET_DEVICE_COUNT]; uint_32 phy_addr[BSP_ENET_DEVICE_COUNT]; uint_32 ip_addr[BSP_ENET_DEVICE_COUNT]; uint_32 i = 0; char* indexes[BSP_ENET_DEVICE_COUNT]; uint_8 n_devices = BSP_ENET_DEVICE_COUNT; #if DEMOCFG_USE_WIFI ENET_ESSID ssid; uint_32 mode;// = DEMOCFG_NW_MODE; uint_32 sectype;// = DEMOCFG_SECURITY; ENET_MEDIACTL_PARAM param; #endif #if RTCSCFG_ENABLE_IP6 IPCFG6_GET_ADDR_DATA data[RTCSCFG_IP6_IF_ADDRESSES_MAX]; char prn_addr6[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; uint_32 n = 0; uint_32 j = 0; #endif _int_install_unexpected_isr(); /*---------------- Initialize devices and variables ----------------------*/ _RTCSPCB_init = 4; _RTCSPCB_grow = 2; _RTCSPCB_max = 8; _RTCSTASK_stacksize = 4500; /*--------------------------- Init end -----------------------------------*/ PORT_PCR_REG(PORTC_BASE_PTR, 3) = PORT_PCR_MUX(5); // SIM_SOPT2 |= 3<<5; // 1k LPO error = RTCS_create(); if (error != RTCS_OK) { printf("RTCS failed to initialize, error = 0x%X\n", error); _task_block(); } _IP_forward = TRUE; for (i = 0; (i < n_devices) && (n_devices != 0); i++) { IPCFG_IP_ADDRESS_DATA ip_data; phy_addr[i] = i; ip_addr[i] = IPADDR(A,B,C+i,D); ENET_get_mac_address (phy_addr[i], ip_addr[i], address[i]); /* Init ENET device */ error = ipcfg_init_device (phy_addr[i], address[i]); if (error != RTCS_OK) { printf("IPCFG: Device n.%d init failed. Error = 0x%X\n", i, error); _task_set_error(MQX_OK); n_devices--; i--; continue; } #if RTCSCFG_ENABLE_IP4 ip_data.ip = ip_addr[i]; ip_data.mask = ENET_IPMASK; ip_data.gateway = 0; /* Bind IPv4 address */ //error = ipcfg_bind_staticip (phy_addr[i], &ip_data); error = ipcfg_bind_autoip (phy_addr[i], &ip_data); if (error != RTCS_OK) { printf("\nIPCFG: Failed to bind IP address. Error = 0x%X", error); _task_block(); } #endif /* RTCSCFG_ENABLE_IP4 */ indexes[i] = (char*) _mem_alloc(sizeof("\\index_x.html")); } error = _io_tfs_install("tfs:", tfs_data); #if DEBUG__MESSAGES printf("Preparing http servers...\n"); #endif for (i = 0; i < n_devices; i++) { params[i] = httpd_default_params(NULL); params[i]->af = HTTP_INET_AF; //IPv4+IPv6, set connection family from config.h #if RTCSCFG_ENABLE_IP6 params[i]->if_scope_id = HTTP_SCOPE_ID; // set interface number here. 0 is any . #endif if (params[i]) { sprintf(indexes[i], "\\index.html", i); params[i]->root_dir = (HTTPD_ROOT_DIR_STRUCT*)root_dir; params[i]->index_page = indexes[i]; printf("Starting http server No.%d on IP", i); /* ** If there is only one interface listen on any IP address ** so address can change in runtime (DHCP etc.) */ #if RTCSCFG_ENABLE_IP4 if ((i == 0) && (n_devices == 1)) { params[i]->address = INADDR_ANY; } else { params[i]->address = ip_addr[i]; } /* Print active ip addresses */ printf(" %d.%d.%d.%d", IPBYTES(ip_addr[i])); #endif #if RTCSCFG_ENABLE_IP6 while(!ipcfg6_get_addr_info_n(i, n, &data[n])) { n++; } while(j < n) { if(inet_ntop(AF_INET6, &data[j++].ip_addr, prn_addr6, sizeof(prn_addr6))) { /* Print active ip addresses */ #if RTCSCFG_ENABLE_IP4 printf("%s %s", (HTTP_INET_AF & AF_INET) ? " and" : "", prn_addr6); #else printf(" %s", prn_addr6); #endif } } #endif #if PSP_MQX_CPU_IS_MCF5223X params[i]->max_ses = 1; #else params[i]->max_ses = _RTCS_socket_part_max - 1; #endif server[i] = httpd_init(params[i]); } HTTPD_SET_PARAM_CGI_TBL(server[i], (HTTPD_CGI_LINK_STRUCT*)cgi_lnk_tbl); #if HTTPD_SEPARATE_TASK || !HTTPDCFG_POLL_MODE printf("..."); error = httpd_server_run(server[i]); if (error) { printf("[FAIL]\n"); } else { printf("[OK]\n"); } #endif } /* user stuff come here */ #if HTTPD_SEPARATE_TASK || !HTTPDCFG_POLL_MODE _task_create(0, SHELL_TASK, 0); _task_block(); #else printf("Servers polling started.\n") while (1) { for (i = 0; i < n_devices; i++) { httpd_server_poll(server[i], 1); } /* user stuff come here - only non blocking calls */ } #endif }