Ejemplo n.º 1
0
Archivo: main.c Proyecto: oter/BSPTools
//*****************************************************************************
//
//! SetDefaultParameters();
//!
//!    @brief  This function Sets all variables required to send an 
//!     email
//!
//!     @param  None
//!
//!     @return 0 on success or error code 
//!  
//
//*****************************************************************************
long SetDefaultParameters(void)
{
    long lRetVal = -1;
    
    //Set Destination Email
    SlNetAppDestination_t destEmailAdd;
    memcpy(destEmailAdd.Email,RCPT_RFC,strlen(RCPT_RFC)+1);
    lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_DEST_EMAIL, \
                                    strlen(RCPT_RFC)+1,
                                    (unsigned char *)&destEmailAdd);
    ASSERT_ON_ERROR(lRetVal);

    //Subject Line
    SlNetAppEmailSubject_t emailSubject;
    memcpy(emailSubject.Value,EMAIL_SUB,strlen(EMAIL_SUB)+1);
    lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_SUBJECT, \
                                strlen(EMAIL_SUB)+1,
                                (unsigned char *)&emailSubject);
    ASSERT_ON_ERROR(lRetVal);

    SlNetAppSourceEmail_t sourceEmailId;
    memcpy(sourceEmailId.Username,USER,strlen(USER)+1);
    lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_SOURCE_EMAIL, \
                                strlen(USER)+1,
                                (unsigned char*)&sourceEmailId);
    ASSERT_ON_ERROR(lRetVal);

    SlNetAppSourcePassword_t sourceEmailPwd;
    memcpy(sourceEmailPwd.Password,PASS,strlen(PASS)+1);
    lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_PASSWORD, \
                                strlen(PASS)+1,
                                (unsigned char*)&sourceEmailPwd);
    ASSERT_ON_ERROR(lRetVal);

    SlNetAppEmailOpt_t eMailServerSetting;
    eMailServerSetting.Family = AF_INET;
    eMailServerSetting.Port = GMAIL_HOST_PORT;
    eMailServerSetting.Ip = SL_IPV4_VAL(74,125,129,108);
    eMailServerSetting.SecurityMethod = SL_SO_SEC_METHOD_SSLV3;
    eMailServerSetting.SecurityCypher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_MD5;

    lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_ADVANCED_OPT, \
                                sizeof(SlNetAppEmailOpt_t), \
                                (unsigned char*)&eMailServerSetting);
    ASSERT_ON_ERROR(lRetVal);

    strncpy(pcEmailmessage, DEFAULT_EMAIL_MSG, strlen(DEFAULT_EMAIL_MSG));
   return SUCCESS;
}
Ejemplo n.º 2
0
void WiFiClass::setDNS(IPAddress dns_server1)
{
    if (!_initialized) {
        init();
    }
    //
    //get current configuration
    //
    _NetCfgIpV4Args_t config = {0};
    unsigned char len = sizeof(_NetCfgIpV4Args_t);
    sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, NULL, &len, (unsigned char*)&config);
    
    //
    //Assign new ip address and new dns server to current config
    //and use netcfgset to set the new configuration in memory
    //
    config.ipV4DnsServer = (uint32_t)SL_IPV4_VAL(dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]);
    sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, 1, sizeof(_NetCfgIpV4Args_t), (unsigned char*)&config);
}
//****************************************************************************
//****************************************************************************
//****************************************************************************
//****************************************************************************
//****************************************************************************
//
//!	\brief pings to the default gateway and ip address of domain "www.ti.com"
//!
//! This function pings to the default gateway to ensure the wlan cannection,
//! then check for the internet connection, if present then get the ip address
//! of Domain name "www.ti.com" and pings to it
//!
//!	\param  ulDefaultGateway is the default gateway for the ap to which the
//!         device is connected
//!
//!	\return -1 for unsuccessful LAN connection, -2 for problem with internet
//!         conection and 0 for succesful ping to the Domain name
//
//****************************************************************************
int PingTest(unsigned long ulDefaultGateway)
{
    int iStatus = 0;
    SlPingStartCommand_t PingParams;
    SlPingReport_t PingReport;
    unsigned long ulIpAddr;
	char *pinghostname = "www.sina.com.cn";
//	unsigned short pinghostname_len = strlen(pinghostname);
	
    // Set the ping parameters
	PingParams.PingIntervalTime = 100;
	PingParams.PingSize = 5;
	PingParams.PingRequestTimeout = 1000;
	PingParams.TotalNumberOfAttempts = 10;
	PingParams.Flags = 0;//?if flags parameter is set to 0, ping will report back once all requested pings are done 
	PingParams.Ip = SL_AF_INET;
    // Fill the GW IP address, which is our AP address
	//PingParams.Ip = ulDefaultGateway;
	PingParams.Ip =SL_IPV4_VAL(192,168,1,1);
    // Check for LAN connection 
    sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET,
                 (SlPingReport_t*)&PingReport, SimpleLinkPingReport);

	
    while (!g_uiPingDone)
	{
		//looping till ping is running
              Z_DelayS(1);
    
        UART_PRINT("i am waiting ping end now \n\r"); 
    
	}
	
    
    g_uiPingDone = 0;


	if(g_uiPingPacketsRecv)
	{

	UART_PRINT("We were able to ping now \n\r"); 
	
		g_uiPingPacketsRecv = 0;
		// We were able to ping the AP

		/* Check for Internet connection */
		/* Querying for ti.com IP address */
		iStatus = sl_NetAppDnsGetHostByName(pinghostname, strlen(pinghostname), &ulIpAddr, SL_AF_INET);
		if (iStatus < 0)
		{

		UART_PRINT("Problem with Internet connection \n\r"); 
		
			// LAN connection is successful
			// Problem with Internet connection
			return -2;
		}


		// Replace the ping address to match ti.com IP address
		PingParams.Ip = ulIpAddr;
                
                UART_PRINT(pinghostname); 

		UART_PRINT(" ip is :  "); 
		Z_IPDispaly(&ulIpAddr);
                
                return 0;  ///not to ping  www.ti.com

		// Try to ping www.ti.com
		sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET,
                     (SlPingReport_t*)&PingReport, SimpleLinkPingReport);

		while (!g_uiPingDone)
		{
			//looping till ping is running
		}
        
        if (g_uiPingPacketsRecv)
		{
			// LAN connection is successful
			// Internet connection is successful
			g_uiPingPacketsRecv = 0;
			return 0;
		}
		else
		{
			// LAN connection is successful
			// Problem with Internet connection
			return -2;
		}
                

    }
	else
	{
		// Problem with LAN connection
		UART_PRINT(" Problem with LAN connection \n\r"); 
		return -1;
	}
}
int BsdTcpClient(unsigned short usPort)
{
  
      UART_PRINT("BsdTcpClient\r\n"); 
  
  while(serverCreatOK == 0) {
        //looping till simplelink starts
  Z_DelayMS(10);
  // ; 
   UART_PRINT("serverCreatOK == 0 \n\r"); 
 } 
 
 UART_PRINT("serverCreatOK == 1\r\n"); 
 
 
    int             iCounter;
    short           sTestBufLen;
    SlSockAddrIn_t  sAddr;
    int             iAddrSize;
    int             iSockID;
    int             iStatus;
    long            lLoopCount = 0;
    long            lBytesSent = 0;
        long            lNonBlocking = 0;                   //0 :non-blocking,

    // filling the buffer
    for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
    {
        g_cBsdBuf[iCounter] = (char)(iCounter % 10);
    }

    sTestBufLen  = BUF_SIZE;

    //filling the TCP server socket address
    sAddr.sin_family = SL_AF_INET;
    sAddr.sin_port = sl_Htons((unsigned short)PORT_NUM);
    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SL_IPV4_VAL(192,168,2,5));

    iAddrSize = sizeof(SlSockAddrIn_t);
    

    
    

    // creating a TCP socket
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    if( iSockID < 0 )
    {
        // error
       return -1;
    }
    
    Report("creating a TCP socket yes\r\n");
    
    iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking));
    
    
    Report("connecting to TCP server\r\n");

    // connecting to TCP server
    while(0>sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize)){
      Report("connecting to TCP server error\r\n");
     // vTaskSuspend(clientTaskHandle);
      vTaskResume((xTaskHandle)&ServerTaskHandle);
      
    }
    /*
    iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
    if( iStatus < 0 )
    {
        // error
    	return -1;
    }
    */
    
    Report("connecting to TCP server yes\r\n");
    
    

    // sending 1000 packets to the TCP server
    while (lLoopCount < 1)
    {
    	// sending packet
        iStatus = sl_Send(iSockID, g_cBsdBuf, sTestBufLen, 0 );
 /*       if( iStatus <= 0 )
        {
            // error
        	return -1;
        }
        */
        
vTaskResume((xTaskHandle)&ServerTaskHandle);

        lLoopCount++;
        lBytesSent += iStatus;
    }

    //closing the socket after sending 1000 packets
    sl_Close(iSockID);
    
    Report("closing the socket after sending 1000 packets\r\n");
    
    
    
    
     //filling the TCP server socket address
    sAddr.sin_family = SL_AF_INET;
    sAddr.sin_port = sl_Htons((unsigned short)1883);
    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SL_IPV4_VAL(9,186,88,87));

    iAddrSize = sizeof(SlSockAddrIn_t);   
    
    
    // creating a TCP socket
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    if( iSockID < 0 )
    {
        // error
       return -1;
    }
    
    Report("creating a TCP socket yes\r\n");
    
    iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking));
    
    
    Report("connecting to TCP server\r\n");

    // connecting to TCP server
    while(0>sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize)){
      Report("connecting to TCP server error\r\n");
      
    }
    /*
    iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
    if( iStatus < 0 )
    {
        // error
    	return -1;
    }
    */
    
    Report("connecting to TCP server yes\r\n");
    
    
    //filling the TCP server socket address
    sAddr.sin_family = SL_AF_INET;
    sAddr.sin_port = sl_Htons((unsigned short)1883);
    sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SL_IPV4_VAL(9,186,88,87));

    iAddrSize = sizeof(SlSockAddrIn_t);   
    
    
    // creating a TCP socket
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    if( iSockID < 0 )
    {
        // error
       return -1;
    }
    
    Report("creating a TCP socket yes\r\n");
    
    lNonBlocking =1;
    iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking));
    
    
    Report("connecting to TCP server\r\n");

    // connecting to TCP server
    
    while(0>sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize)){
      Report("connecting to TCP server error\r\n");
      
    }
    /*
    iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
    if( iStatus < 0 )
    {
        // error
    	return -1;
    }
    */
    
    Report("connecting to TCP server yes\r\n");
    
    
    
    
    
    

    return 0;
}
Ejemplo n.º 5
0
void wlan_sl_init (int8_t mode, const char *ssid, uint8_t ssid_len, uint8_t auth, const char *key, uint8_t key_len,
                   uint8_t channel, uint8_t antenna, bool add_mac) {

    // stop the servers
    wlan_servers_stop();

    // do a basic start
    wlan_first_start();

    // close any active connections
    wlan_sl_disconnect();

    // Remove all profiles
    ASSERT_ON_ERROR(sl_WlanProfileDel(0xFF));

    // Enable the DHCP client
    uint8_t value = 1;
    ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, 1, 1, &value));

    // Set PM policy to normal
    ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0));

    // Unregister mDNS services
    ASSERT_ON_ERROR(sl_NetAppMDNSUnRegisterService(0, 0));

    // Stop the internal HTTP server
    sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);

    // Remove all 64 filters (8 * 8)
    _WlanRxFilterOperationCommandBuff_t  RxFilterIdMask;
    memset ((void *)&RxFilterIdMask, 0 ,sizeof(RxFilterIdMask));
    memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
    ASSERT_ON_ERROR(sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)));

#if MICROPY_HW_ANTENNA_DIVERSITY
    // set the antenna type
    wlan_set_antenna (antenna);
#endif

    // switch to the requested mode
    wlan_set_mode(mode);

    // stop and start again (we need to in the propper mode from now on)
    wlan_reenable(mode);

    // Set Tx power level for station or AP mode
    // Number between 0-15, as dB offset from max power - 0 will set max power
    uint8_t ucPower = 0;
    if (mode == ROLE_AP) {
        ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_AP_TX_POWER, sizeof(ucPower),
                                   (unsigned char *)&ucPower));

        // configure all parameters
        wlan_set_ssid (ssid, ssid_len, add_mac);
        wlan_set_security (auth, key, key_len);
        wlan_set_channel (channel);

        // set the country
        _u8*  country = (_u8*)"EU";
        ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, country));

        SlNetCfgIpV4Args_t ipV4;
        ipV4.ipV4          = (_u32)SL_IPV4_VAL(192,168,1,1);            // _u32 IP address
        ipV4.ipV4Mask      = (_u32)SL_IPV4_VAL(255,255,255,0);          // _u32 Subnet mask for this AP
        ipV4.ipV4Gateway   = (_u32)SL_IPV4_VAL(192,168,1,1);            // _u32 Default gateway address
        ipV4.ipV4DnsServer = (_u32)SL_IPV4_VAL(192,168,1,1);            // _u32 DNS server address
        ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4,
                                     sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4));

        SlNetAppDhcpServerBasicOpt_t dhcpParams;
        dhcpParams.lease_time      =  4096;                             // lease time (in seconds) of the IP Address
        dhcpParams.ipv4_addr_start =  SL_IPV4_VAL(192,168,1,2);         // first IP Address for allocation.
        dhcpParams.ipv4_addr_last  =  SL_IPV4_VAL(192,168,1,254);       // last IP Address for allocation.
        ASSERT_ON_ERROR(sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID));      // Stop DHCP server before settings
        ASSERT_ON_ERROR(sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT,
                                     sizeof(SlNetAppDhcpServerBasicOpt_t), (_u8* )&dhcpParams));  // set parameters
        ASSERT_ON_ERROR(sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID));     // Start DHCP server with new settings

        // stop and start again
        wlan_reenable(mode);
    } else { // STA and P2P modes
        ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER,
                                   sizeof(ucPower), (unsigned char *)&ucPower));
        // set connection policy to Auto + Fast (tries to connect to the last connected AP)
        ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 1, 0, 0, 0), NULL, 0));
    }

    // set current time and date (needed to validate certificates)
    wlan_set_current_time (pyb_rtc_get_seconds());

    // start the servers before returning
    wlan_servers_start();
}