Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.49 KB | None | 0 0
  1. //*****************************************************************************
  2. //
  3. // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  4. //
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above copyright
  14. // notice, this list of conditions and the following disclaimer in the
  15. // documentation and/or other materials provided with the
  16. // distribution.
  17. //
  18. // Neither the name of Texas Instruments Incorporated nor the names of
  19. // its contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. //
  34. //*****************************************************************************
  35.  
  36.  
  37. //*****************************************************************************
  38. //
  39. // Application Name - UDP Socket
  40. // Application Overview - This particular application illustrates how this
  41. // device can be used as a client or server for UDP
  42. // communication.
  43. // Application Details -
  44. // http://processors.wiki.ti.com/index.php/CC32xx_UDP_Socket_Application
  45. // or
  46. // docs\examples\CC32xx_UDP_Socket_Application.pdf
  47. //
  48. //*****************************************************************************
  49.  
  50.  
  51. //****************************************************************************
  52. //
  53. //! \addtogroup udp_socket
  54. //! @{
  55. //
  56. //****************************************************************************
  57.  
  58. #include <stdlib.h>
  59. #include <string.h>
  60.  
  61. // simplelink includes
  62. #include "simplelink.h"
  63. #include "wlan.h"
  64.  
  65. // driverlib includes
  66. #include "hw_ints.h"
  67. #include "hw_types.h"
  68. #include "hw_memmap.h"
  69. #include "rom.h"
  70. #include "rom_map.h"
  71. #include "interrupt.h"
  72. #include "prcm.h"
  73. #include "utils.h"
  74. #include "uart.h"
  75.  
  76. // common interface includes
  77. #include "udma_if.h"
  78. #include "common.h"
  79. #ifndef NOTERM
  80. #include "uart_if.h"
  81. #endif
  82.  
  83. #include "pinmux.h"
  84.  
  85.  
  86. #define APPLICATION_NAME "UDP Socket"
  87. #define APPLICATION_VERSION "1.1.1"
  88.  
  89. #define IP_ADDR 0xc0a80064 /* 192.168.0.100 */
  90. #define PORT_NUM 5001
  91. #define BUF_SIZE 1400
  92. #define UDP_PACKET_COUNT 1000
  93.  
  94. // Application specific status/error codes
  95. typedef enum{
  96. // Choosing -0x7D0 to avoid overlap w/ host-driver's error codes
  97. SOCKET_CREATE_ERROR = -0x7D0,
  98. BIND_ERROR = SOCKET_CREATE_ERROR - 1,
  99. SEND_ERROR = BIND_ERROR - 1,
  100. RECV_ERROR = SEND_ERROR -1,
  101. SOCKET_CLOSE = RECV_ERROR -1,
  102. DEVICE_NOT_IN_STATION_MODE = SOCKET_CLOSE - 1,
  103. STATUS_CODE_MAX = -0xBB8
  104. }e_AppStatusCodes;
  105.  
  106.  
  107.  
  108. //****************************************************************************
  109. // LOCAL FUNCTION PROTOTYPES
  110. //****************************************************************************
  111. int BsdUdpClient(unsigned short usPort);
  112. int BsdUdpServer(unsigned short usPort);
  113. static long WlanConnect();
  114. static void DisplayBanner();
  115. static void BoardInit();
  116. static void InitializeAppVariables();
  117. static long ConfigureSimpleLinkToDefaultState();
  118.  
  119.  
  120. //*****************************************************************************
  121. // GLOBAL VARIABLES -- Start
  122. //*****************************************************************************
  123. volatile unsigned long g_ulStatus = 0;//SimpleLink Status
  124. unsigned long g_ulGatewayIP = 0; //Network Gateway IP address
  125. unsigned char g_ucConnectionSSID[SSID_LEN_MAX+1]; //Connection SSID
  126. unsigned char g_ucConnectionBSSID[BSSID_LEN_MAX]; //Connection BSSID
  127. unsigned long g_ulDestinationIp = IP_ADDR; // Client IP address
  128. unsigned int g_uiPortNum = PORT_NUM;
  129. volatile unsigned long g_ulPacketCount = UDP_PACKET_COUNT;
  130. unsigned char g_ucSimplelinkstarted = 0;
  131. unsigned long g_ulIpAddr = 0;
  132. char g_cBsdBuf[BUF_SIZE];
  133.  
  134. #if defined(ccs) || defined(gcc)
  135. extern void (* const g_pfnVectors[])(void);
  136. #endif
  137. #if defined(ewarm)
  138. extern uVectorEntry __vector_table;
  139. #endif
  140. //*****************************************************************************
  141. // GLOBAL VARIABLES -- End
  142. //*****************************************************************************
  143.  
  144.  
  145.  
  146. //*****************************************************************************
  147. // SimpleLink Asynchronous Event Handlers -- Start
  148. //*****************************************************************************
  149.  
  150.  
  151. //*****************************************************************************
  152. //
  153. //! \brief The Function Handles WLAN Events
  154. //!
  155. //! \param[in] pWlanEvent - Pointer to WLAN Event Info
  156. //!
  157. //! \return None
  158. //!
  159. //*****************************************************************************
  160. void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)
  161. {
  162. if(!pWlanEvent)
  163. {
  164. return;
  165. }
  166.  
  167. switch(pWlanEvent->Event)
  168. {
  169. case SL_WLAN_CONNECT_EVENT:
  170. {
  171. SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  172.  
  173. //
  174. // Information about the connected AP (like name, MAC etc) will be
  175. // available in 'slWlanConnectAsyncResponse_t'-Applications
  176. // can use it if required
  177. //
  178. // slWlanConnectAsyncResponse_t *pEventData = NULL;
  179. // pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;
  180. //
  181.  
  182. // Copy new connection SSID and BSSID to global parameters
  183. memcpy(g_ucConnectionSSID,pWlanEvent->EventData.
  184. STAandP2PModeWlanConnected.ssid_name,
  185. pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_len);
  186. memcpy(g_ucConnectionBSSID,
  187. pWlanEvent->EventData.STAandP2PModeWlanConnected.bssid,
  188. SL_BSSID_LENGTH);
  189.  
  190. UART_PRINT("[WLAN EVENT] STA Connected to the AP: %s , "
  191. "BSSID: %x:%x:%x:%x:%x:%x\n\r",
  192. g_ucConnectionSSID,g_ucConnectionBSSID[0],
  193. g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  194. g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  195. g_ucConnectionBSSID[5]);
  196. }
  197. break;
  198.  
  199. case SL_WLAN_DISCONNECT_EVENT:
  200. {
  201. slWlanConnectAsyncResponse_t* pEventData = NULL;
  202.  
  203. CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  204. CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  205.  
  206. pEventData = &pWlanEvent->EventData.STAandP2PModeDisconnected;
  207.  
  208. // If the user has initiated 'Disconnect' request,
  209. //'reason_code' is SL_WLAN_DISCONNECT_USER_INITIATED_DISCONNECTION
  210. if(SL_WLAN_DISCONNECT_USER_INITIATED_DISCONNECTION == pEventData->reason_code)
  211. {
  212. UART_PRINT("[WLAN EVENT]Device disconnected from the AP: %s,"
  213. "BSSID: %x:%x:%x:%x:%x:%x on application's request \n\r",
  214. g_ucConnectionSSID,g_ucConnectionBSSID[0],
  215. g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  216. g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  217. g_ucConnectionBSSID[5]);
  218. }
  219. else
  220. {
  221. UART_PRINT("[WLAN ERROR]Device disconnected from the AP AP: %s,"
  222. "BSSID: %x:%x:%x:%x:%x:%x on an ERROR..!! \n\r",
  223. g_ucConnectionSSID,g_ucConnectionBSSID[0],
  224. g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  225. g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  226. g_ucConnectionBSSID[5]);
  227. }
  228. memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));
  229. memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));
  230. }
  231. break;
  232.  
  233. default:
  234. {
  235. UART_PRINT("[WLAN EVENT] Unexpected event [0x%x]\n\r",
  236. pWlanEvent->Event);
  237. }
  238. break;
  239. }
  240. }
  241.  
  242. //*****************************************************************************
  243. //
  244. //! \brief This function handles network events such as IP acquisition, IP
  245. //! leased, IP released etc.
  246. //!
  247. //! \param[in] pNetAppEvent - Pointer to NetApp Event Info
  248. //!
  249. //! \return None
  250. //!
  251. //*****************************************************************************
  252. void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
  253. {
  254. if(!pNetAppEvent)
  255. {
  256. return;
  257. }
  258.  
  259. switch(pNetAppEvent->Event)
  260. {
  261. case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
  262. {
  263. SlIpV4AcquiredAsync_t *pEventData = NULL;
  264.  
  265. SET_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  266.  
  267. //Ip Acquired Event Data
  268. pEventData = &pNetAppEvent->EventData.ipAcquiredV4;
  269.  
  270. g_ulIpAddr = pEventData->ip;
  271.  
  272. //Gateway IP address
  273. g_ulGatewayIP = pEventData->gateway;
  274.  
  275. UART_PRINT("[NETAPP EVENT] IP Acquired: IP=%d.%d.%d.%d , "
  276. "Gateway=%d.%d.%d.%d\n\r",
  277.  
  278. SL_IPV4_BYTE(g_ulIpAddr,3),
  279. SL_IPV4_BYTE(g_ulIpAddr,2),
  280. SL_IPV4_BYTE(g_ulIpAddr,1),
  281. SL_IPV4_BYTE(g_ulIpAddr,0),
  282. SL_IPV4_BYTE(g_ulGatewayIP,3),
  283. SL_IPV4_BYTE(g_ulGatewayIP,2),
  284. SL_IPV4_BYTE(g_ulGatewayIP,1),
  285. SL_IPV4_BYTE(g_ulGatewayIP,0));
  286. }
  287. break;
  288.  
  289. default:
  290. {
  291. UART_PRINT("[NETAPP EVENT] Unexpected event [0x%x] \n\r",
  292. pNetAppEvent->Event);
  293. }
  294. break;
  295. }
  296. }
  297.  
  298.  
  299. //*****************************************************************************
  300. //
  301. //! \brief This function handles HTTP server events
  302. //!
  303. //! \param[in] pServerEvent - Contains the relevant event information
  304. //! \param[in] pServerResponse - Should be filled by the user with the
  305. //! relevant response information
  306. //!
  307. //! \return None
  308. //!
  309. //****************************************************************************
  310. void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent,
  311. SlHttpServerResponse_t *pHttpResponse)
  312. {
  313. // Unused in this application
  314. }
  315.  
  316. //*****************************************************************************
  317. //
  318. //! \brief This function handles General Events
  319. //!
  320. //! \param[in] pDevEvent - Pointer to General Event Info
  321. //!
  322. //! \return None
  323. //!
  324. //*****************************************************************************
  325. void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
  326. {
  327. if(!pDevEvent)
  328. {
  329. return;
  330. }
  331.  
  332. //
  333. // Most of the general errors are not FATAL are are to be handled
  334. // appropriately by the application
  335. //
  336. UART_PRINT("[GENERAL EVENT] - ID=[%d] Sender=[%d]\n\n",
  337. pDevEvent->EventData.deviceEvent.status,
  338. pDevEvent->EventData.deviceEvent.sender);
  339. }
  340.  
  341.  
  342. //*****************************************************************************
  343. //
  344. //! This function handles socket events indication
  345. //!
  346. //! \param[in] pSock - Pointer to Socket Event Info
  347. //!
  348. //! \return None
  349. //!
  350. //*****************************************************************************
  351. void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
  352. {
  353. //
  354. // This application doesn't work w/ socket - Events are not expected
  355. //
  356.  
  357. }
  358.  
  359.  
  360. //*****************************************************************************
  361. // SimpleLink Asynchronous Event Handlers -- End
  362. //*****************************************************************************
  363.  
  364.  
  365. //*****************************************************************************
  366. //
  367. //! This function initializes the application variables
  368. //!
  369. //! \param[in] None
  370. //!
  371. //! \return None
  372. //!
  373. //*****************************************************************************
  374. static void InitializeAppVariables()
  375. {
  376. g_ulStatus = 0;
  377. g_ulGatewayIP = 0;
  378. memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));
  379. memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));
  380. g_ulDestinationIp = IP_ADDR;
  381. g_uiPortNum = PORT_NUM;
  382. g_ulPacketCount = UDP_PACKET_COUNT;
  383. }
  384.  
  385. //*****************************************************************************
  386. //! \brief This function puts the device in its default state. It:
  387. //! - Set the mode to STATION
  388. //! - Configures connection policy to Auto and AutoSmartConfig
  389. //! - Deletes all the stored profiles
  390. //! - Enables DHCP
  391. //! - Disables Scan policy
  392. //! - Sets Tx power to maximum
  393. //! - Sets power policy to normal
  394. //! - Unregister mDNS services
  395. //! - Remove all filters
  396. //!
  397. //! \param none
  398. //! \return On success, zero is returned. On error, negative is returned
  399. //*****************************************************************************
  400. static long ConfigureSimpleLinkToDefaultState()
  401. {
  402. SlVersionFull ver = {0};
  403. _WlanRxFilterOperationCommandBuff_t RxFilterIdMask = {0};
  404.  
  405. unsigned char ucVal = 1;
  406. unsigned char ucConfigOpt = 0;
  407. unsigned char ucConfigLen = 0;
  408. unsigned char ucPower = 0;
  409.  
  410. long lRetVal = -1;
  411. long lMode = -1;
  412.  
  413. lMode = sl_Start(0, 0, 0);
  414. ASSERT_ON_ERROR(lMode);
  415.  
  416. // If the device is not in station-mode, try configuring it in station-mode
  417. if (ROLE_STA != lMode)
  418. {
  419. if (ROLE_AP == lMode)
  420. {
  421. // If the device is in AP mode, we need to wait for this event
  422. // before doing anything
  423. while(!IS_IP_ACQUIRED(g_ulStatus))
  424. {
  425. #ifndef SL_PLATFORM_MULTI_THREADED
  426. _SlNonOsMainLoopTask();
  427. #endif
  428. }
  429. }
  430.  
  431. // Switch to STA role and restart
  432. lRetVal = sl_WlanSetMode(ROLE_STA);
  433. ASSERT_ON_ERROR(lRetVal);
  434.  
  435. lRetVal = sl_Stop(0xFF);
  436. ASSERT_ON_ERROR(lRetVal);
  437.  
  438. lRetVal = sl_Start(0, 0, 0);
  439. ASSERT_ON_ERROR(lRetVal);
  440.  
  441. // Check if the device is in station again
  442. if (ROLE_STA != lRetVal)
  443. {
  444. // We don't want to proceed if the device is not coming up in STA-mode
  445. return DEVICE_NOT_IN_STATION_MODE;
  446. }
  447. }
  448.  
  449. // Get the device's version-information
  450. ucConfigOpt = SL_DEVICE_GENERAL_VERSION;
  451. ucConfigLen = sizeof(ver);
  452. lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt,
  453. &ucConfigLen, (unsigned char *)(&ver));
  454. ASSERT_ON_ERROR(lRetVal);
  455.  
  456. UART_PRINT("Host Driver Version: %s\n\r",SL_DRIVER_VERSION);
  457. UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r",
  458. ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3],
  459. ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1],
  460. ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3],
  461. ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1],
  462. ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]);
  463.  
  464. // Set connection policy to Auto + SmartConfig
  465. // (Device's default connection policy)
  466. lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
  467. SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
  468. ASSERT_ON_ERROR(lRetVal);
  469.  
  470. // Remove all profiles
  471. lRetVal = sl_WlanProfileDel(0xFF);
  472. ASSERT_ON_ERROR(lRetVal);
  473.  
  474.  
  475.  
  476. //
  477. // Device in station-mode. Disconnect previous connection if any
  478. // The function returns 0 if 'Disconnected done', negative number if already
  479. // disconnected Wait for 'disconnection' event if 0 is returned, Ignore
  480. // other return-codes
  481. //
  482. lRetVal = sl_WlanDisconnect();
  483. if(0 == lRetVal)
  484. {
  485. // Wait
  486. while(IS_CONNECTED(g_ulStatus))
  487. {
  488. #ifndef SL_PLATFORM_MULTI_THREADED
  489. _SlNonOsMainLoopTask();
  490. #endif
  491. }
  492. }
  493.  
  494. // Enable DHCP client
  495. lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);
  496. ASSERT_ON_ERROR(lRetVal);
  497.  
  498. // Disable scan
  499. ucConfigOpt = SL_SCAN_POLICY(0);
  500. lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0);
  501. ASSERT_ON_ERROR(lRetVal);
  502.  
  503. // Set Tx power level for station mode
  504. // Number between 0-15, as dB offset from max power - 0 will set max power
  505. ucPower = 0;
  506. lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
  507. WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower);
  508. ASSERT_ON_ERROR(lRetVal);
  509.  
  510. // Set PM policy to normal
  511. lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);
  512. ASSERT_ON_ERROR(lRetVal);
  513.  
  514. // Unregister mDNS services
  515. lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
  516. ASSERT_ON_ERROR(lRetVal);
  517.  
  518. // Remove all 64 filters (8*8)
  519. memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
  520. lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask,
  521. sizeof(_WlanRxFilterOperationCommandBuff_t));
  522. ASSERT_ON_ERROR(lRetVal);
  523.  
  524. lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  525. ASSERT_ON_ERROR(lRetVal);
  526.  
  527. InitializeAppVariables();
  528.  
  529. return lRetVal; // Success
  530. }
  531.  
  532.  
  533. //****************************************************************************
  534. //
  535. //! \brief Parse the input IP address from the user
  536. //!
  537. //! \param[in] ucCMD (char pointer to input string)
  538. //!
  539. //! \return 0 : if correct IP, -1 : incorrect IP
  540. //
  541. //****************************************************************************
  542. int IpAddressParser(char *ucCMD)
  543. {
  544. int i=0;
  545. unsigned int uiUserInputData;
  546. unsigned long ulUserIpAddress = 0;
  547. char *ucInpString;
  548. ucInpString = strtok(ucCMD, ".");
  549. uiUserInputData = (int)strtoul(ucInpString,0,10);
  550. while(i<4)
  551. {
  552. //
  553. // Check Whether IP is valid
  554. //
  555. if((ucInpString != NULL) && (uiUserInputData < 256))
  556. {
  557. ulUserIpAddress |= uiUserInputData;
  558. if(i < 3)
  559. ulUserIpAddress = ulUserIpAddress << 8;
  560. ucInpString=strtok(NULL,".");
  561. uiUserInputData = (int)strtoul(ucInpString,0,10);
  562. i++;
  563. }
  564. else
  565. {
  566. return -1;
  567. }
  568. }
  569. g_ulDestinationIp = ulUserIpAddress;
  570. return SUCCESS;
  571. }
  572.  
  573. //*****************************************************************************
  574. //
  575. //! UserInput
  576. //!
  577. //! This function
  578. //! 1. Function for reading the user input for UDP RX/TX
  579. //!
  580. //! \return none
  581. //
  582. //*****************************************************************************
  583. long UserInput()
  584. {
  585. int iInput = 0;
  586. char acCmdStore[50];
  587. int lRetVal;
  588. int iRightInput = 0;
  589. unsigned long ulUserInputData = 0;
  590.  
  591. UART_PRINT("Default settings: SSID Name: %s, PORT = %d, Packet Count = %d, "
  592. "Destination IP: %d.%d.%d.%d\n\r",
  593. SSID_NAME, g_uiPortNum, g_ulPacketCount,
  594. SL_IPV4_BYTE(g_ulDestinationIp,3),
  595. SL_IPV4_BYTE(g_ulDestinationIp,2),
  596. SL_IPV4_BYTE(g_ulDestinationIp,1),
  597. SL_IPV4_BYTE(g_ulDestinationIp,0));
  598.  
  599. do
  600. {
  601. UART_PRINT("\r\nOptions:\r\n1. Send UDP packets.\r\n2. Receive UDP "
  602. "packets.\r\n3. Settings.\r\n4. Exit\r\n");
  603. UART_PRINT("Enter the option to use: ");
  604. lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
  605. if(lRetVal == 0)
  606. {
  607. //
  608. // No input. Just an enter pressed probably. Display a prompt.
  609. //
  610. UART_PRINT("\n\n\rEnter Valid Input.");
  611. }
  612. else
  613. {
  614. iInput = (int)strtoul(acCmdStore,0,10);
  615. if(iInput == 1)
  616. {
  617. UART_PRINT("Run iperf command \"iperf.exe -u -s -i 1\" and "
  618. "press Enter\n\r");
  619. //
  620. // Wait to receive a character over UART
  621. //
  622. MAP_UARTCharGet(CONSOLE);
  623. UART_PRINT("Sending UDP packets...\n\r");
  624.  
  625. // Before proceeding, please make sure to have a server
  626. // waiting on PORT_NUM
  627. lRetVal = BsdUdpClient(g_uiPortNum);
  628. ASSERT_ON_ERROR(lRetVal);
  629. }
  630. else if(iInput == 2)
  631. {
  632. UART_PRINT("Run iperf command \"iperf.exe -u -c %d.%d.%d.%d -i 1 "
  633. "-t 100000\" and press Enter\n\r",
  634. SL_IPV4_BYTE(g_ulIpAddr,3), SL_IPV4_BYTE(g_ulIpAddr,2),
  635. SL_IPV4_BYTE(g_ulIpAddr,1), SL_IPV4_BYTE(g_ulIpAddr,0));
  636.  
  637. //
  638. // Wait to receive a character over UART
  639. //
  640. MAP_UARTCharGet(CONSOLE);
  641. UART_PRINT("Receiving UDP packets...\n\r");
  642.  
  643. // After calling this function, you can start sending data
  644. // to CC3200 IP address on PORT_NUM
  645. lRetVal = BsdUdpServer(g_uiPortNum);
  646. ASSERT_ON_ERROR(lRetVal);
  647. }
  648. else if(iInput == 3)
  649. {
  650. iRightInput = 0;
  651. do
  652. {
  653. UART_PRINT("\n\rSetting Options:\n\r1. PORT\n\r2. Packet "
  654. "Count\n\r3. Destination IP\n\r4. Main Menu\r\n");
  655. UART_PRINT("Enter the option to use: ");
  656. lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
  657. if(lRetVal == 0)
  658. {
  659. //
  660. // No input. Just an enter pressed probably. Display prompt.
  661. //
  662. UART_PRINT("\n\n\rEnter Valid Input.");
  663. }
  664. else
  665. {
  666.  
  667. iInput = (int)strtoul(acCmdStore,0,10);
  668. //SettingInput(iInput);
  669. switch(iInput)
  670. {
  671. case 1:
  672. do
  673. {
  674. UART_PRINT("Enter new Port: ");
  675. lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
  676. if(lRetVal == 0)
  677. {
  678. //
  679. // No input. Just an enter pressed probably.
  680. // Display a prompt.
  681. //
  682. UART_PRINT("\n\rEnter Valid Input.");
  683. iRightInput = 0;
  684. }
  685. else
  686. {
  687. ulUserInputData = (int)strtoul(acCmdStore,0,10);
  688. if(ulUserInputData <= 0 || ulUserInputData > 65535)
  689. {
  690. UART_PRINT("\n\rWrong Input");
  691. iRightInput = 0;
  692. }
  693. else
  694. {
  695. g_uiPortNum = ulUserInputData;
  696. iRightInput = 1;
  697. }
  698. }
  699.  
  700. UART_PRINT("\r\n");
  701. }while(!iRightInput);
  702.  
  703. iRightInput = 0;
  704. break;
  705. case 2:
  706. do
  707. {
  708. UART_PRINT("Enter Packet Count: ");
  709. lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
  710. if(lRetVal == 0)
  711. {
  712. //
  713. // No input. Just an enter pressed probably.
  714. // Display a prompt.
  715. //
  716. UART_PRINT("\n\rEnter Valid Input.");
  717. iRightInput = 0;
  718. }
  719. else
  720. {
  721. ulUserInputData = (int)strtoul(acCmdStore,0,10);
  722. if(ulUserInputData <= 0 || ulUserInputData > 9999999)
  723. {
  724. UART_PRINT("\n\rWrong Input");
  725. iRightInput = 0;
  726. }
  727. else
  728. {
  729. g_ulPacketCount = ulUserInputData;
  730. iRightInput = 1;
  731. }
  732. }
  733.  
  734. UART_PRINT("\r\n");
  735. }while(!iRightInput);
  736. iRightInput = 0;
  737. break;
  738. case 3:
  739. do
  740. {
  741. UART_PRINT("Enter Destination IP: ");
  742. lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
  743. if(lRetVal == 0)
  744. {
  745. //
  746. // No input. Just an enter pressed probably.
  747. // Display a prompt.
  748. //
  749. UART_PRINT("\n\rEnter Valid Input.");
  750. iRightInput = 0;
  751. }
  752. else
  753. {
  754. if(IpAddressParser(acCmdStore) < 0)
  755. {
  756. UART_PRINT("\n\rWrong Input");
  757. iRightInput = 0;
  758. }
  759. else
  760. {
  761. iRightInput = 1;
  762. }
  763. }
  764.  
  765. UART_PRINT("\r\n");
  766. }while(!iRightInput);
  767. iRightInput = 0;
  768. break;
  769. case 4:
  770. iRightInput = 1;
  771. break;
  772.  
  773. default:
  774. break;
  775.  
  776. }
  777.  
  778. }
  779. }while(!iRightInput);
  780.  
  781. }
  782. else if(iInput == 4)
  783. {
  784. break;
  785. }
  786. else
  787. {
  788. UART_PRINT("\n\n\rWrong Input");
  789. }
  790. }
  791. UART_PRINT("\n\r");
  792. }while(1);
  793.  
  794. return 0 ;
  795.  
  796. }
  797.  
  798. //****************************************************************************
  799. //
  800. //! \brief Opening a UDP client side socket and sending data
  801. //!
  802. //! This function opens a UDP socket and tries to connect to a Server IP_ADDR
  803. //! waiting on port PORT_NUM.
  804. //! Then the function will send 1000 UDP packets to the server.
  805. //!
  806. //! \param[in] port number on which the server will be listening on
  807. //!
  808. //! \return 0 on success, -1 on Error.
  809. //
  810. //****************************************************************************
  811. int BsdUdpClient(unsigned short usPort)
  812. {
  813. int iCounter;
  814. short sTestBufLen;
  815. SlSockAddrIn_t sAddr;
  816. int iAddrSize;
  817. int iSockID;
  818. int iStatus;
  819. unsigned long lLoopCount = 0;
  820.  
  821. // filling the buffer
  822. for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
  823. {
  824. g_cBsdBuf[iCounter] = (char)(iCounter % 10);
  825. }
  826.  
  827. sTestBufLen = BUF_SIZE;
  828.  
  829. //filling the UDP server socket address
  830. sAddr.sin_family = SL_AF_INET;
  831. sAddr.sin_port = sl_Htons((unsigned short)usPort);
  832. sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp);
  833.  
  834. iAddrSize = sizeof(SlSockAddrIn_t);
  835.  
  836. // creating a UDP socket
  837. iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);
  838. if( iSockID < 0 )
  839. {
  840. // error
  841. ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
  842. }
  843.  
  844. // for a UDP connection connect is not required
  845. // sending 1000 packets to the UDP server
  846. while (lLoopCount < g_ulPacketCount)
  847. {
  848. g_cBsdBuf[0] = lLoopCount >> 24 & 0xFF;
  849. g_cBsdBuf[1] = lLoopCount >> 16 & 0xFF;
  850. g_cBsdBuf[2] = lLoopCount >> 8 & 0xFF;
  851. g_cBsdBuf[3] = lLoopCount & 0xFF;
  852.  
  853. // sending packet
  854. iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0,
  855. (SlSockAddr_t *)&sAddr, iAddrSize);
  856. if( iStatus <= 0 )
  857. {
  858. // error
  859. sl_Close(iSockID);
  860. ASSERT_ON_ERROR(SEND_ERROR);
  861. }
  862. lLoopCount++;
  863. }
  864.  
  865. UART_PRINT("Sent %u packets successfully\n\r",g_ulPacketCount);
  866.  
  867. //closing the socket after sending 1000 packets
  868. sl_Close(iSockID);
  869.  
  870. return SUCCESS;
  871. }
  872.  
  873. //****************************************************************************
  874. //
  875. //! \brief Opening a UDP server side socket and receiving data
  876. //!
  877. //! This function opens a UDP socket in Listen mode and waits for an incoming
  878. //! UDP connection.
  879. //! If a socket connection is established then the function will try to
  880. //! read 1000 UDP packets from the connected client.
  881. //!
  882. //! \param[in] port number on which the server will be listening on
  883. //!
  884. //! \return 0 on success, Negative value on Error.
  885. //
  886. //****************************************************************************
  887. int BsdUdpServer(unsigned short usPort)
  888. {
  889. SlSockAddrIn_t sAddr;
  890. SlSockAddrIn_t sLocalAddr;
  891. int iCounter;
  892. int iAddrSize;
  893. int iSockID;
  894. int iStatus;
  895. long lLoopCount = 0;
  896. short sTestBufLen;
  897.  
  898. // filling the buffer
  899. for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
  900. {
  901. g_cBsdBuf[iCounter] = (char)(iCounter % 10);
  902. }
  903.  
  904. sTestBufLen = BUF_SIZE;
  905. //filling the UDP server socket address
  906. sLocalAddr.sin_family = SL_AF_INET;
  907. sLocalAddr.sin_port = sl_Htons((unsigned short)usPort);
  908. sLocalAddr.sin_addr.s_addr = 0;
  909.  
  910. iAddrSize = sizeof(SlSockAddrIn_t);
  911.  
  912. // creating a UDP socket
  913. iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);
  914. if( iSockID < 0 )
  915. {
  916. // error
  917. ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
  918. }
  919.  
  920. // binding the UDP socket to the UDP server address
  921. iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
  922. if( iStatus < 0 )
  923. {
  924. // error
  925. sl_Close(iSockID);
  926. ASSERT_ON_ERROR(BIND_ERROR);
  927. }
  928.  
  929. // no listen or accept is required as UDP is connectionless protocol
  930. /// waits for 1000 packets from a UDP client
  931. while (lLoopCount < g_ulPacketCount)
  932. {
  933. iStatus = sl_RecvFrom(iSockID, g_cBsdBuf, sTestBufLen, 0,
  934. ( SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize );
  935.  
  936. if( iStatus < 0 )
  937. {
  938. // error
  939. sl_Close(iSockID);
  940. ASSERT_ON_ERROR(RECV_ERROR);
  941. }
  942. lLoopCount++;
  943. }
  944.  
  945. UART_PRINT("Recieved %u packets successfully\n\r",g_ulPacketCount);
  946.  
  947. //closing the socket after receiving 1000 packets
  948. sl_Close(iSockID);
  949.  
  950. return SUCCESS;
  951. }
  952.  
  953. //****************************************************************************
  954. //
  955. //! \brief Connecting to a WLAN Accesspoint
  956. //!
  957. //! This function connects to the required AP (SSID_NAME) with Security
  958. //! parameters specified in te form of macros at the top of this file
  959. //!
  960. //! \param[in] None
  961. //!
  962. //! \return status value
  963. //!
  964. //! \warning If the WLAN connection fails or we don't aquire an IP
  965. //! address, It will be stuck in this function forever.
  966. //
  967. //****************************************************************************
  968. static long WlanConnect()
  969. {
  970. SlSecParams_t secParams = {0};
  971. long lRetVal = 0;
  972.  
  973. secParams.Key = (signed char*)SECURITY_KEY;
  974. secParams.KeyLen = strlen(SECURITY_KEY);
  975. secParams.Type = SECURITY_TYPE;
  976.  
  977. lRetVal = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, \
  978. &secParams, 0);
  979. ASSERT_ON_ERROR(lRetVal);
  980.  
  981. while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
  982. {
  983. // Wait for WLAN Event
  984. #ifndef SL_PLATFORM_MULTI_THREADED
  985. _SlNonOsMainLoopTask();
  986. #endif
  987.  
  988. }
  989.  
  990. return SUCCESS;
  991. }
  992.  
  993. //*****************************************************************************
  994. //
  995. //! Application startup display on UART
  996. //!
  997. //! \param none
  998. //!
  999. //! \return none
  1000. //!
  1001. //*****************************************************************************
  1002. static void
  1003. DisplayBanner(char * AppName)
  1004. {
  1005. UART_PRINT("\n\n\n\r");
  1006. UART_PRINT("\t\t *************************************************\n\r");
  1007. UART_PRINT("\t\t CC3200 %s Application \n\r", AppName);
  1008. UART_PRINT("\t\t *************************************************\n\r");
  1009. UART_PRINT("\n\n\n\r");
  1010. }
  1011.  
  1012.  
  1013. //*****************************************************************************
  1014. //
  1015. //! Board Initialization & Configuration
  1016. //!
  1017. //! \param None
  1018. //!
  1019. //! \return None
  1020. //
  1021. //*****************************************************************************
  1022. static void
  1023. BoardInit(void)
  1024. {
  1025. /* In case of TI-RTOS vector table is initialize by OS itself */
  1026. #ifndef USE_TIRTOS
  1027. //
  1028. // Set vector table base
  1029. //
  1030. #if defined(ccs) || defined(gcc)
  1031. MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
  1032. #endif
  1033. #if defined(ewarm)
  1034. MAP_IntVTableBaseSet((unsigned long)&__vector_table);
  1035. #endif
  1036. #endif
  1037. //
  1038. // Enable Processor
  1039. //
  1040. MAP_IntMasterEnable();
  1041. MAP_IntEnable(FAULT_SYSTICK);
  1042.  
  1043. PRCMCC3200MCUInit();
  1044. }
  1045.  
  1046. //****************************************************************************
  1047. // MAIN FUNCTION
  1048. //****************************************************************************
  1049. void main()
  1050. {
  1051. long lRetVal = -1;
  1052.  
  1053. //
  1054. // Board Initialization
  1055. //
  1056. BoardInit();
  1057.  
  1058. //
  1059. // uDMA Initialization
  1060. //
  1061. UDMAInit();
  1062.  
  1063. //
  1064. // Configure the pinmux settings for the peripherals exercised
  1065. //
  1066. PinMuxConfig();
  1067.  
  1068. //
  1069. // Configuring UART
  1070. //
  1071. InitTerm();
  1072.  
  1073. //
  1074. // Display banner
  1075. //
  1076. DisplayBanner(APPLICATION_NAME);
  1077.  
  1078. InitializeAppVariables();
  1079.  
  1080. //
  1081. // Following function configure the device to default state by cleaning
  1082. // the persistent settings stored in NVMEM (viz. connection profiles &
  1083. // policies, power policy etc)
  1084. //
  1085. // Applications may choose to skip this step if the developer is sure
  1086. // that the device is in its desired state at start of applicaton
  1087. //
  1088. // Note that all profiles and persistent settings that were done on the
  1089. // device will be lost
  1090. //
  1091. lRetVal = ConfigureSimpleLinkToDefaultState();
  1092.  
  1093. if(lRetVal < 0)
  1094. {
  1095. if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
  1096. UART_PRINT("Failed to configure the device in its default state \n\r");
  1097.  
  1098. LOOP_FOREVER();
  1099. }
  1100.  
  1101. UART_PRINT("Device is configured in default state \n\r");
  1102.  
  1103. //
  1104. // Asumption is that the device is configured in station mode already
  1105. // and it is in its default state
  1106. //
  1107. lRetVal = sl_Start(0, 0, 0);
  1108. if (lRetVal < 0 || lRetVal != ROLE_STA)
  1109. {
  1110. UART_PRINT("Failed to start the device \n\r");
  1111. LOOP_FOREVER();
  1112. }
  1113.  
  1114. UART_PRINT("Device started as STATION \n\r");
  1115.  
  1116. UART_PRINT("Connecting to AP: %s ...\r\n",SSID_NAME);
  1117.  
  1118. //
  1119. //Connecting to WLAN AP
  1120. //
  1121. lRetVal = WlanConnect();
  1122. if(lRetVal < 0)
  1123. {
  1124. UART_PRINT("Failed to establish connection w/ an AP \n\r");
  1125. LOOP_FOREVER();
  1126. }
  1127.  
  1128. UART_PRINT("Connected to AP: %s \n\r",SSID_NAME);
  1129.  
  1130. UART_PRINT("Device IP: %d.%d.%d.%d\n\r\n\r",
  1131. SL_IPV4_BYTE(g_ulIpAddr,3),
  1132. SL_IPV4_BYTE(g_ulIpAddr,2),
  1133. SL_IPV4_BYTE(g_ulIpAddr,1),
  1134. SL_IPV4_BYTE(g_ulIpAddr,0));
  1135.  
  1136. #ifdef USER_INPUT_ENABLE
  1137. lRetVal = UserInput();
  1138. if(lRetVal < 0)
  1139. {
  1140. ERR_PRINT(lRetVal);
  1141. LOOP_FOREVER();
  1142. }
  1143.  
  1144. #else
  1145. lRetVal = BsdUdpClient(PORT_NUM);
  1146. if(lRetVal < 0)
  1147. {
  1148. ERR_PRINT(lRetVal);
  1149. LOOP_FOREVER();
  1150. }
  1151.  
  1152. lRetVal = BsdUdpServer(PORT_NUM);
  1153. if(lRetVal < 0)
  1154. {
  1155. ERR_PRINT(lRetVal);
  1156. LOOP_FOREVER();
  1157. }
  1158. #endif
  1159.  
  1160. UART_PRINT("Exiting Application ...\n\r");
  1161.  
  1162. //
  1163. // power off the network processor
  1164. //
  1165. lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  1166.  
  1167. while (1)
  1168. {
  1169. _SlNonOsMainLoopTask();
  1170. }
  1171. }
  1172.  
  1173. //*****************************************************************************
  1174. //
  1175. // Close the Doxygen group.
  1176. //! @}
  1177. //
  1178. //*****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement