/** * @brief This function processes the add port to IGD(Internet Gateway Device). * @return 0: success, -2: Invalid UPnP Step, -1: reply packet timeout, 1: received xml parse error, other: UPnP error code */ signed short AddPortProcess( SOCKET sockfd, /**< a socket number. */ const char* protocol, /**< a procotol name. "TCP" or "UDP" */ const unsigned int extertnal_port, /**< an external port number. */ const char* internal_ip, /**< an internal ip address. */ const unsigned int internal_port, /**< an internal port number. */ const char* description /**< a description of this portforward. */ ) { short len=0; long endTime=0; uint32 ipaddr; uint16 port; // Check UPnP Step if(UPnP_Step < 2) return -2; // Make "Add Port" XML(SOAP) memset(content, '\0', MAX_BUFFER); MakeSOAPAddControl(content, protocol, extertnal_port, internal_ip, internal_port, description); // Make HTTP POST Header memset(send_buffer, '\0', MAX_BUFFER); len = strlen(content); MakePOSTHeader(send_buffer, len, ADD_PORT); strcat(send_buffer, content); #ifdef UPNP_DEBUG printf("%s\r\n", send_buffer); #endif ipaddr = inet_addr((uint8*)descIP); ipaddr = swapl(ipaddr); port = ATOI(descPORT, 10); // Connect to IGD(Internet Gateway Device) if(TCPClientOpen(sockfd, PORT_UPNP, (uint8*)&ipaddr, port) == FAIL) printf("TCP Socket Error!!\r\n"); // Send "Add Port" Message while(GetTCPSocketStatus(sockfd) != STATUS_ESTABLISHED); TCPSend(sockfd, (void *)send_buffer, strlen(send_buffer)); // Receive Reply memset(recv_buffer, '\0', MAX_BUFFER); Delay_ms(500); endTime = my_time + 3; while (TCPRecv(sockfd, (void *)recv_buffer, MAX_BUFFER) <= 0 && my_time < endTime); // Check Receive Buffer of W5200 if(my_time >= endTime){ // Check Timeout TCPClose(sockfd); return -1; } // TCP Socket Close TCPClose(sockfd); #ifdef UPNP_DEBUG printf("\r\nReceiveData\r\n%s\r\n", recv_buffer); #endif // Parse Replied Message return parseAddPort(recv_buffer); }
/** * Opens a connection to HTTP host * \param sock TCP_SOCKET to use. <I>Please, remember to use the "&" operator</I> * \param httphost name of HTTP host, or its IP address * \param httpport name of HTTP port * \return None */ void HTTPOpen(TCP_SOCKET* sock, char* httphost, char* httpport) { return TCPClientOpen(sock, httphost, httpport); }
/******************************************************************************* * Function Name : Main_Menu * Description : Display/Manage a Menu on HyperTerminal Window * Input : None * Output : None * Return : None *******************************************************************************/ void Main_Menu(void) { static char choice[3]; static char msg[256]; static bool bTreat; static u8 Sip[4]; static char key = 0; static wiz_NetInfo netinfo; while (1) { /* Display Menu on HyperTerminal Window */ bTreat = (bool)RESET ; SerialPutString("\r\n====================== STM32-Discovery ===================\r\n"); SerialPutString("This Application is basic example of UART interface with\r\n"); SerialPutString("Windows Hyper Terminal. \r\n"); SerialPutString("\r\n==========================================================\r\n"); SerialPutString(" APPLICATION MENU :\r\n"); SerialPutString("\r\n==========================================================\r\n\n"); SerialPutString(" 1 - Set LD1 on \r\n"); SerialPutString(" 2 - Set LD1 off \r\n"); SerialPutString(" 3 - Show network setting\r\n"); SerialPutString(" 4 - Set network setting\r\n"); SerialPutString(" 5 - Run TCP Loopback\r\n"); SerialPutString(" 6 - Run UDP Loopback\r\n"); SerialPutString(" 7 - DNS test\r\n"); SerialPutString(" 8 - Twitter\r\n"); SerialPutString("Enter your choice : "); GetInputString(choice); /* Set LD1 on */ if (strcmp(choice,"1")== 0) { bTreat = (bool)SET; LED3_onoff(ON); LED4_onoff(ON); } /* Set LD1 off */ if ((strcmp(choice,"2") == 0)) { bTreat = (bool)SET; LED3_onoff(OFF); LED4_onoff(OFF); } if (strcmp(choice,"3") == 0) { bTreat = (bool)SET; GetNetInfo(&netinfo); printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]); printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]); printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]); printf("\r\nDNS server : %d.%d.%d.%d", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]); } if (strcmp(choice,"4") == 0) { bTreat = (bool)SET; // IP address SerialPutString("\r\nIP address : "); GetInputString(msg); if(!VerifyIPAddress(msg, netinfo.IP)) { SerialPutString("\aInvalid."); } // Subnet mask SerialPutString("\r\nSubnet mask : "); GetInputString(msg); if(!VerifyIPAddress(msg, netinfo.Subnet)) { SerialPutString("\aInvalid."); } // gateway address SerialPutString("\r\nGateway address : "); GetInputString(msg); if(!VerifyIPAddress(msg, netinfo.Gateway)) { SerialPutString("\aInvalid."); } // DNS address SerialPutString("\r\nDNS address : "); GetInputString(msg); if(!VerifyIPAddress(msg, netinfo.DNSServerIP)) { SerialPutString("\aInvalid."); } printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]); printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]); printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]); printf("\r\nDNS server : %d.%d.%d.%d", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]); SetNetInfo(&netinfo); } if (strcmp(choice,"5") == 0) { bTreat = (bool)SET; SerialPutString("\r\nRun TCP loopback"); printf("\r\nRun TCP loopback, port number [%d] is listened", (u16)TCP_LISTEN_PORT); SerialPutString("\r\nTo Exit, press [Q]"); while(1) { if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) { SerialPutString("\r\n Stop "); break; } loopback_tcps(7, (u16)TCP_LISTEN_PORT); } } if (strcmp(choice,"6") == 0) { bTreat = (bool)SET; SerialPutString("\r\nRun UDP loopback"); printf("\r\nRun UDP loopback, port number [%d] is listened", (u16)UDP_LISTEN_PORT); SerialPutString("\r\nTo Exit, press [Q]"); while(1) { if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) { SerialPutString("\r\n Stop "); break; } loopback_udp(7, (u16)UDP_LISTEN_PORT); } } if (strcmp(choice,"7")== 0) { bTreat = (bool)SET; SerialPutString("\r\nServer address : "); GetInputString(msg); SerialPutString("URL = "); SerialPutString(msg); if (dns_query(SOCK_DNS, (void *)msg, Sip) == 1) { printf("\r\nSIP : %d.%d.%d.%d", (u16)Sip[0],(u16)Sip[1],(u16)Sip[2],(u16)Sip[3]); }else { SerialPutString("\n\r DNS fail"); } } if (strcmp(choice,"8")== 0) { bTreat = (bool)SET; // DNS if (dns_query(SOCK_DNS, LIB_DOMAIN, Sip) == 1) { printf("\r\nSIP : %d.%d.%d.%d", (u16)Sip[0],(u16)Sip[1],(u16)Sip[2],(u16)Sip[3]); while(1) { /* sprintf(msg, "Hello, World [%d] from wiz820io", twit_cnt++); SerialPutString("\r\nSend Twitter Message: "); SerialPutString(msg); */ SerialPutString("\r\nType a message: "); GetInputString(msg); Twitter_Post(SOCK_TWITTER, (void *)msg, Sip); SerialPutString("\r\nIf you want send another message? [YES]: any key, [NO]: Q"); key = GetKey(); if (key == 'Q') { SerialPutString("\r\n Stop "); break; } } }else { SerialPutString("\r\nDNS error"); } } if (strcmp(choice,"9") == 0) { uint8 l_ip[4] = {222, 98, 173, 237}; bTreat = (bool)SET; SerialPutString("\r\nRun TCP loopback [Client]"); SerialPutString("\r\nTo Exit, press [Q]"); while(1) { if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) { SerialPutString("\r\n Stop "); break; } TCPClientOpen(7, 5000, l_ip, 5000); Delay_ms(3000); TCPClose(7); Delay_ms(3000); } } /* OTHERS CHOICE*/ if (bTreat == (bool)RESET) { SerialPutString(" wrong choice \r\n"); } } /* While(1)*/ }/* Main_Menu */