Advertisement
Guest User

Untitled

a guest
Dec 21st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.07 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. // Application Name - UDP Socket
  39. // Application Overview - This particular application illustrates how this
  40. // device can be used as a client or server for UDP
  41. // communication.
  42. // Application Details -
  43. // http://processors.wiki.ti.com/index.php/CC32xx_UDP_Socket_Application
  44. // or
  45. // docs\examples\CC32xx_UDP_Socket_Application.pdf
  46. //
  47. //*****************************************************************************
  48.  
  49. //****************************************************************************
  50. //
  51. //! \addtogroup udp_socket
  52. //! @{
  53. //
  54. //****************************************************************************
  55.  
  56. #include <stdlib.h>
  57. #include <string.h>
  58.  
  59. // simplelink includes
  60. #include "simplelink.h"
  61. #include "wlan.h"
  62.  
  63. // driverlib includes
  64. #include "hw_ints.h"
  65. #include "hw_types.h"
  66. #include "hw_memmap.h"
  67. #include "hw_gpio.h"
  68. #include "rom.h"
  69. #include "rom_map.h"
  70. #include "interrupt.h"
  71. #include "prcm.h"
  72. #include "utils.h"
  73. #include "uart.h"
  74. #include "gpio_if.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. unsigned char GPIO9 = 0x09;
  86. unsigned int g_uiPortGPIO9;
  87. unsigned char g_ucPinGPIO9;
  88. char outputSettings = 0x00;
  89.  
  90. #define APPLICATION_NAME "UDP Socket"
  91. #define APPLICATION_VERSION "1.1.1"
  92. #define SSID_NAME "pumukel"
  93.  
  94. #define IP_ADDR 0xc0a80064 /* 192.168.0.100 */
  95. #define PORT_NUM 5001
  96. #define BUF_SIZE 1400
  97. #define UDP_PACKET_COUNT 1
  98. #define BETWEEN(value, min, max) (value < max && value > min)
  99. // Application specific status/error codes
  100. typedef enum
  101. {
  102. // Choosing -0x7D0 to avoid overlap w/ host-driver's error codes
  103. SOCKET_CREATE_ERROR = -0x7D0,
  104. BIND_ERROR = SOCKET_CREATE_ERROR - 1,
  105. SEND_ERROR = BIND_ERROR - 1,
  106. RECV_ERROR = SEND_ERROR - 1,
  107. SOCKET_CLOSE = RECV_ERROR - 1,
  108. DEVICE_NOT_IN_STATION_MODE = SOCKET_CLOSE - 1,
  109. STATUS_CODE_MAX = -0xBB8
  110. } e_AppStatusCodes;
  111.  
  112. //****************************************************************************
  113. // LOCAL FUNCTION PROTOTYPES
  114. //****************************************************************************
  115. int BsdUdpClient(unsigned short usPort);
  116. int BsdUdpServer(unsigned short usPort);
  117. static long WlanConnect();
  118. static void DisplayBanner();
  119. static void BoardInit();
  120. static void InitializeAppVariables();
  121. static long ConfigureSimpleLinkToDefaultState();
  122.  
  123. //*****************************************************************************
  124. // GLOBAL VARIABLES -- Start
  125. //*****************************************************************************
  126. volatile unsigned long g_ulStatus = 0; //SimpleLink Status
  127. unsigned long g_ulGatewayIP = 0xc0a8E701; //Network Gateway IP address
  128. unsigned char g_ucConnectionSSID[SSID_LEN_MAX + 1]; //Connection SSID
  129. unsigned char g_ucConnectionBSSID[BSSID_LEN_MAX]; //Connection BSSID
  130. unsigned long g_ulDestinationIp = IP_ADDR; // Client IP address
  131. unsigned int g_uiPortNum = PORT_NUM;
  132. volatile unsigned long g_ulPacketCount = UDP_PACKET_COUNT;
  133. unsigned char g_ucSimplelinkstarted = 0;
  134. unsigned long g_ulIpAddr = 0;
  135. char g_cBsdBuf[BUF_SIZE];
  136. unsigned char checkUDP;
  137.  
  138. #if defined(ccs) || defined(gcc)
  139. extern void (* const g_pfnVectors[])(void);
  140. #endif
  141. #if defined(ewarm)
  142. extern uVectorEntry __vector_table;
  143. #endif
  144. //*****************************************************************************
  145. // GLOBAL VARIABLES -- End
  146. //*****************************************************************************
  147.  
  148. //*****************************************************************************
  149. // SimpleLink Asynchronous Event Handlers -- Start
  150. //*****************************************************************************
  151.  
  152. //*****************************************************************************
  153. //
  154. //! \brief The Function Handles WLAN Events
  155. //!
  156. //! \param[in] pWlanEvent - Pointer to WLAN Event Info
  157. //!
  158. //! \return None
  159. //!
  160. //*****************************************************************************
  161. void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)
  162. {
  163. if (!pWlanEvent)
  164. {
  165. return;
  166. }
  167.  
  168. switch (pWlanEvent->Event)
  169. {
  170. case SL_WLAN_CONNECT_EVENT:
  171. {
  172. SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  173.  
  174. //
  175. // Information about the connected AP (like name, MAC etc) will be
  176. // available in 'slWlanConnectAsyncResponse_t'-Applications
  177. // can use it if required
  178. //
  179. // slWlanConnectAsyncResponse_t *pEventData = NULL;
  180. // pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;
  181. //
  182.  
  183. // Copy new connection SSID and BSSID to global parameters
  184. memcpy(g_ucConnectionSSID,
  185. pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_name,
  186. pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_len);
  187. memcpy(g_ucConnectionBSSID,
  188. pWlanEvent->EventData.STAandP2PModeWlanConnected.bssid,
  189. SL_BSSID_LENGTH);
  190.  
  191. UART_PRINT("[WLAN EVENT] STA Connected to the AP: %s , "
  192. "BSSID: %x:%x:%x:%x:%x:%x\n\r",
  193. g_ucConnectionSSID, g_ucConnectionBSSID[0],
  194. g_ucConnectionBSSID[1], g_ucConnectionBSSID[2],
  195. g_ucConnectionBSSID[3], g_ucConnectionBSSID[4],
  196. g_ucConnectionBSSID[5]);
  197. }
  198. break;
  199.  
  200. case SL_WLAN_DISCONNECT_EVENT:
  201. {
  202. slWlanConnectAsyncResponse_t* pEventData = NULL;
  203.  
  204. CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  205. CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  206.  
  207. pEventData = &pWlanEvent->EventData.STAandP2PModeDisconnected;
  208.  
  209. // If the user has initiated 'Disconnect' request,
  210. //'reason_code' is SL_WLAN_DISCONNECT_USER_INITIATED_DISCONNECTION
  211. if (SL_WLAN_DISCONNECT_USER_INITIATED_DISCONNECTION
  212. == pEventData->reason_code)
  213. {
  214. UART_PRINT("[WLAN EVENT]Device disconnected from the AP: %s,"
  215. "BSSID: %x:%x:%x:%x:%x:%x on application's request \n\r",
  216. g_ucConnectionSSID, g_ucConnectionBSSID[0],
  217. g_ucConnectionBSSID[1], g_ucConnectionBSSID[2],
  218. g_ucConnectionBSSID[3], g_ucConnectionBSSID[4],
  219. g_ucConnectionBSSID[5]);
  220. }
  221. else
  222. {
  223. UART_PRINT("[WLAN ERROR]Device disconnected from the AP AP: %s,"
  224. "BSSID: %x:%x:%x:%x:%x:%x on an ERROR..!! \n\r",
  225. g_ucConnectionSSID, g_ucConnectionBSSID[0],
  226. g_ucConnectionBSSID[1], g_ucConnectionBSSID[2],
  227. g_ucConnectionBSSID[3], g_ucConnectionBSSID[4],
  228. g_ucConnectionBSSID[5]);
  229. }
  230. memset(g_ucConnectionSSID, 0, sizeof(g_ucConnectionSSID));
  231. memset(g_ucConnectionBSSID, 0, sizeof(g_ucConnectionBSSID));
  232. }
  233. break;
  234.  
  235. default:
  236. {
  237. UART_PRINT("[WLAN EVENT] Unexpected event [0x%x]\n\r",
  238. pWlanEvent->Event);
  239. }
  240. break;
  241. }
  242. }
  243.  
  244. //*****************************************************************************
  245. //
  246. //! \brief This function handles network events such as IP acquisition, IP
  247. //! leased, IP released etc.
  248. //!
  249. //! \param[in] pNetAppEvent - Pointer to NetApp Event Info
  250. //!
  251. //! \return None
  252. //!
  253. //*****************************************************************************
  254. void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
  255. {
  256. if (!pNetAppEvent)
  257. {
  258. return;
  259. }
  260.  
  261. switch (pNetAppEvent->Event)
  262. {
  263. case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
  264. {
  265. SlIpV4AcquiredAsync_t *pEventData = NULL;
  266.  
  267. SET_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  268.  
  269. //Ip Acquired Event Data
  270. pEventData = &pNetAppEvent->EventData.ipAcquiredV4;
  271.  
  272. g_ulIpAddr = pEventData->ip;
  273.  
  274. //Gateway IP address
  275. g_ulGatewayIP = pEventData->gateway;
  276.  
  277. UART_PRINT("[NETAPP EVENT] IP Acquired: IP=%d.%d.%d.%d , "
  278. "Gateway=%d.%d.%d.%d\n\r",
  279.  
  280. SL_IPV4_BYTE(g_ulIpAddr, 3),
  281. SL_IPV4_BYTE(g_ulIpAddr, 2), SL_IPV4_BYTE(g_ulIpAddr, 1),
  282. SL_IPV4_BYTE(g_ulIpAddr, 0), 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. //! \brief This function handles HTTP server events
  301. //!
  302. //! \param[in] pServerEvent - Contains the relevant event information
  303. //! \param[in] pServerResponse - Should be filled by the user with the
  304. //! relevant response information
  305. //!
  306. //! \return None
  307. //!
  308. //****************************************************************************
  309. void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent,
  310. SlHttpServerResponse_t *pHttpResponse)
  311. {
  312. // Unused in this application
  313. }
  314.  
  315. //*****************************************************************************
  316. //
  317. //! \brief This function handles General Events
  318. //!
  319. //! \param[in] pDevEvent - Pointer to General Event Info
  320. //!
  321. //! \return None
  322. //!
  323. //*****************************************************************************
  324. void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
  325. {
  326. if (!pDevEvent)
  327. {
  328. return;
  329. }
  330.  
  331. //
  332. // Most of the general errors are not FATAL are are to be handled
  333. // appropriately by the application
  334. //
  335. UART_PRINT("[GENERAL EVENT] - ID=[%d] Sender=[%d]\n\n",
  336. pDevEvent->EventData.deviceEvent.status,
  337. pDevEvent->EventData.deviceEvent.sender);
  338. }
  339.  
  340. //*****************************************************************************
  341. //
  342. //! This function handles socket events indication
  343. //!
  344. //! \param[in] pSock - Pointer to Socket Event Info
  345. //!
  346. //! \return None
  347. //!
  348. //*****************************************************************************
  349. void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
  350. {
  351. //
  352. // This application doesn't work w/ socket - Events are not expected
  353. //
  354.  
  355. }
  356.  
  357. //*****************************************************************************
  358. // SimpleLink Asynchronous Event Handlers -- End
  359. //*****************************************************************************
  360.  
  361. //*****************************************************************************
  362. //
  363. //! This function initializes the application variables
  364. //!
  365. //! \param[in] None
  366. //!
  367. //! \return None
  368. //!
  369. //*****************************************************************************
  370. static void InitializeAppVariables()
  371. {
  372. g_ulStatus = 0;
  373. g_ulGatewayIP = 0;
  374. memset(g_ucConnectionSSID, 0, sizeof(g_ucConnectionSSID));
  375. memset(g_ucConnectionBSSID, 0, sizeof(g_ucConnectionBSSID));
  376. g_ulDestinationIp = IP_ADDR;
  377. g_uiPortNum = PORT_NUM;
  378. g_ulPacketCount = UDP_PACKET_COUNT;
  379. }
  380.  
  381. //*****************************************************************************
  382. //! \brief This function puts the device in its default state. It:
  383. //! - Set the mode to STATION
  384. //! - Configures connection policy to Auto and AutoSmartConfig
  385. //! - Deletes all the stored profiles
  386. //! - Enables DHCP
  387. //! - Disables Scan policy
  388. //! - Sets Tx power to maximum
  389. //! - Sets power policy to normal
  390. //! - Unregister mDNS services
  391. //! - Remove all filters
  392. //!
  393. //! \param none
  394. //! \return On success, zero is returned. On error, negative is returned
  395. //*****************************************************************************
  396. static long ConfigureSimpleLinkToDefaultState()
  397. {
  398. SlVersionFull ver = { 0 };
  399. _WlanRxFilterOperationCommandBuff_t RxFilterIdMask = { 0 };
  400.  
  401. unsigned char ucVal = 1;
  402. unsigned char ucConfigOpt = 0;
  403. unsigned char ucConfigLen = 0;
  404. unsigned char ucPower = 0;
  405.  
  406. long lRetVal = -1;
  407. long lMode = -1;
  408.  
  409. lMode = sl_Start(0, 0, 0);
  410. ASSERT_ON_ERROR(lMode);
  411.  
  412. // If the device is not in station-mode, try configuring it in station-mode
  413. if (ROLE_STA != lMode)
  414. {
  415. if (ROLE_AP == lMode)
  416. {
  417. // If the device is in AP mode, we need to wait for this event
  418. // before doing anything
  419. while (!IS_IP_ACQUIRED(g_ulStatus))
  420. {
  421. #ifndef SL_PLATFORM_MULTI_THREADED
  422. _SlNonOsMainLoopTask();
  423. #endif
  424. }
  425. }
  426.  
  427. // Switch to STA role and restart
  428. lRetVal = sl_WlanSetMode(ROLE_STA);
  429. ASSERT_ON_ERROR(lRetVal);
  430.  
  431. lRetVal = sl_Stop(0xFF);
  432. ASSERT_ON_ERROR(lRetVal);
  433.  
  434. lRetVal = sl_Start(0, 0, 0);
  435. ASSERT_ON_ERROR(lRetVal);
  436.  
  437. // Check if the device is in station again
  438. if (ROLE_STA != lRetVal)
  439. {
  440. // We don't want to proceed if the device is not coming up in STA-mode
  441. return DEVICE_NOT_IN_STATION_MODE;
  442. }
  443. }
  444.  
  445. // Get the device's version-information
  446. ucConfigOpt = SL_DEVICE_GENERAL_VERSION;
  447. ucConfigLen = sizeof(ver);
  448. lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt,
  449. &ucConfigLen, (unsigned char *) (&ver));
  450. ASSERT_ON_ERROR(lRetVal);
  451.  
  452. UART_PRINT("Host Driver Version: %s\n\r", SL_DRIVER_VERSION);
  453. UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r",
  454. ver.NwpVersion[0], ver.NwpVersion[1], ver.NwpVersion[2],
  455. ver.NwpVersion[3], ver.ChipFwAndPhyVersion.FwVersion[0],
  456. ver.ChipFwAndPhyVersion.FwVersion[1],
  457. ver.ChipFwAndPhyVersion.FwVersion[2],
  458. ver.ChipFwAndPhyVersion.FwVersion[3],
  459. ver.ChipFwAndPhyVersion.PhyVersion[0],
  460. ver.ChipFwAndPhyVersion.PhyVersion[1],
  461. ver.ChipFwAndPhyVersion.PhyVersion[2],
  462. 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. // Device in station-mode. Disconnect previous connection if any
  476. // The function returns 0 if 'Disconnected done', negative number if already
  477. // disconnected Wait for 'disconnection' event if 0 is returned, Ignore
  478. // other return-codes
  479. //
  480. lRetVal = sl_WlanDisconnect();
  481. if (0 == lRetVal)
  482. {
  483. // Wait
  484. while (IS_CONNECTED(g_ulStatus))
  485. {
  486. #ifndef SL_PLATFORM_MULTI_THREADED
  487. _SlNonOsMainLoopTask();
  488. #endif
  489. }
  490. }
  491.  
  492. // Enable DHCP client
  493. lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, 1, 1, &ucVal);
  494. ASSERT_ON_ERROR(lRetVal);
  495.  
  496. // Disable scan
  497. ucConfigOpt = SL_SCAN_POLICY(0);
  498. lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN, ucConfigOpt, NULL, 0);
  499. ASSERT_ON_ERROR(lRetVal);
  500.  
  501. // Set Tx power level for station mode
  502. // Number between 0-15, as dB offset from max power - 0 will set max power
  503. ucPower = 0;
  504. lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
  505. WLAN_GENERAL_PARAM_OPT_STA_TX_POWER,
  506. 1, (unsigned char *) &ucPower);
  507. ASSERT_ON_ERROR(lRetVal);
  508.  
  509. // Set PM policy to normal
  510. lRetVal = sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0);
  511. ASSERT_ON_ERROR(lRetVal);
  512.  
  513. // Unregister mDNS services
  514. lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
  515. ASSERT_ON_ERROR(lRetVal);
  516.  
  517. // Remove all 64 filters (8*8)
  518. memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
  519. lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *) &RxFilterIdMask,
  520. sizeof(_WlanRxFilterOperationCommandBuff_t));
  521. ASSERT_ON_ERROR(lRetVal);
  522.  
  523. lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  524. ASSERT_ON_ERROR(lRetVal);
  525.  
  526. InitializeAppVariables();
  527.  
  528. return lRetVal; // Success
  529. }
  530.  
  531. //****************************************************************************
  532. //
  533. //! \brief Parse the input IP address from the user
  534. //!
  535. //! \param[in] ucCMD (char pointer to input string)
  536. //!
  537. //! \return 0 : if correct IP, -1 : incorrect IP
  538. //
  539. //****************************************************************************
  540. int IpAddressParser(char *ucCMD)
  541. {
  542. int i = 0;
  543. unsigned int uiUserInputData;
  544. unsigned long ulUserIpAddress = 0;
  545. char *ucInpString;
  546. ucInpString = strtok(ucCMD, ".");
  547. uiUserInputData = (int) strtoul(ucInpString, 0, 10);
  548. while (i < 4)
  549. {
  550. //
  551. // Check Whether IP is valid
  552. //
  553. if ((ucInpString != NULL) && (uiUserInputData < 256))
  554. {
  555. ulUserIpAddress |= uiUserInputData;
  556. if (i < 3)
  557. ulUserIpAddress = ulUserIpAddress << 8;
  558. ucInpString = strtok(NULL, ".");
  559. uiUserInputData = (int) strtoul(ucInpString, 0, 10);
  560. i++;
  561. }
  562. else
  563. {
  564. return -1;
  565. }
  566. }
  567. g_ulDestinationIp = ulUserIpAddress;
  568. return SUCCESS;
  569. }
  570.  
  571.  
  572. //****************************************************************************
  573. //
  574. //! \brief Opening a UDP client side socket and sending data
  575. //!
  576. //! This function opens a UDP socket and tries to connect to a Server IP_ADDR
  577. //! waiting on port PORT_NUM.
  578. //! Then the function will send 1000 UDP packets to the server.
  579. //!
  580. //! \param[in] port number on which the server will be listening on
  581. //!
  582. //! \return 0 on success, -1 on Error.
  583. //
  584. //****************************************************************************
  585. int BsdUdpClient(unsigned short usPort)
  586. {
  587. int iCounter;
  588. short sTestBufLen;
  589. SlSockAddrIn_t sAddr;
  590. int iAddrSize;
  591. int iSockID;
  592. int iStatus;
  593. unsigned long lLoopCount = 0;
  594.  
  595. // filling the buffer
  596. for (iCounter = 0; iCounter < BUF_SIZE; iCounter++)
  597. {
  598. g_cBsdBuf[iCounter] = (char) (iCounter % 10);
  599. }
  600.  
  601. sTestBufLen = BUF_SIZE;
  602.  
  603. //filling the UDP server socket address
  604. sAddr.sin_family = SL_AF_INET;
  605. sAddr.sin_port = sl_Htons((unsigned short) usPort);
  606. sAddr.sin_addr.s_addr = sl_Htonl((unsigned int) g_ulDestinationIp);
  607.  
  608. iAddrSize = sizeof(SlSockAddrIn_t);
  609.  
  610. // creating a UDP socket
  611. iSockID = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
  612. if (iSockID < 0)
  613. {
  614. // error
  615. ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
  616. }
  617.  
  618. // for a UDP connection connect is not required
  619. // sending 1000 packets to the UDP server
  620. while (lLoopCount < g_ulPacketCount)
  621. {
  622. g_cBsdBuf[0] = lLoopCount >> 24 & 0xFF;
  623. g_cBsdBuf[1] = lLoopCount >> 16 & 0xFF;
  624. g_cBsdBuf[2] = lLoopCount >> 8 & 0xFF;
  625. g_cBsdBuf[3] = lLoopCount & 0xFF;
  626.  
  627. // sending packet
  628. iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0,
  629. (SlSockAddr_t *) &sAddr, iAddrSize);
  630. if (iStatus <= 0)
  631. {
  632. // error
  633. sl_Close(iSockID);
  634. ASSERT_ON_ERROR(SEND_ERROR);
  635. }
  636. lLoopCount++;
  637. }
  638.  
  639. UART_PRINT("Sent %u packets successfully\n\r", g_ulPacketCount);
  640.  
  641. //closing the socket after sending 1000 packets
  642. sl_Close(iSockID);
  643.  
  644. return SUCCESS;
  645. }
  646.  
  647. //****************************************************************************
  648. //
  649. //! \brief Opening a UDP server side socket and receiving data
  650. //!
  651. //! This function opens a UDP socket in Listen mode and waits for an incoming
  652. //! UDP connection.
  653. //! If a socket connection is established then the function will try to
  654. //! read 1000 UDP packets from the connected client.
  655. //!
  656. //! \param[in] port number on which the server will be listening on
  657. //!
  658. //! \return 0 on success, Negative value on Error.
  659. //
  660. //****************************************************************************
  661.  
  662. int BsdUdpServer(unsigned short usPort)
  663. {
  664. SlSockAddrIn_t sAddr;
  665. SlSockAddrIn_t sLocalAddr;
  666. int iCounter;
  667. int iAddrSize;
  668. int iSockID;
  669. int iStatus;
  670. long lLoopCount = 0;
  671. short sTestBufLen;
  672. int waarde;
  673. int bufferteller;
  674.  
  675. // filling the buffer
  676. for (iCounter = 0; iCounter < BUF_SIZE; iCounter++)
  677. {
  678. g_cBsdBuf[iCounter] = (char) (iCounter % 10);
  679. }
  680.  
  681. sTestBufLen = BUF_SIZE;
  682. //filling the UDP server socket address
  683. sLocalAddr.sin_family = SL_AF_INET;
  684. sLocalAddr.sin_port = sl_Htons((unsigned short) usPort);
  685. sLocalAddr.sin_addr.s_addr = 0;
  686.  
  687. iAddrSize = sizeof(SlSockAddrIn_t);
  688.  
  689. // creating a UDP socket
  690. iSockID = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
  691. if (iSockID < 0)
  692. {
  693. // error
  694. ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
  695. }
  696.  
  697. // binding the UDP socket to the UDP server address
  698. iStatus = sl_Bind(iSockID, (SlSockAddr_t *) &sLocalAddr, iAddrSize);
  699. if (iStatus < 0)
  700. {
  701. // error
  702. sl_Close(iSockID);
  703. ASSERT_ON_ERROR(BIND_ERROR);
  704. }
  705. // no listen or accept is required as UDP is connectionless protocol
  706. /// waits for 1000 packets from a UDP client
  707. while (lLoopCount < g_ulPacketCount)
  708. {
  709. iStatus = sl_RecvFrom(iSockID, g_cBsdBuf, sTestBufLen, 0,
  710. (SlSockAddr_t *) &sAddr,
  711. (SlSocklen_t*) &iAddrSize);
  712.  
  713. if (iStatus < 0)
  714. {
  715. // error
  716. sl_Close(iSockID);
  717. ASSERT_ON_ERROR(RECV_ERROR);
  718. }
  719. lLoopCount++;
  720. }
  721.  
  722. bufferteller = 0;
  723. waarde = 0;
  724. //wanneer de buffer waarde ontvangen wordt zal deze opgeteld worden wanneer deze geen spaties bevat
  725. //De -48 is voor de ascii code om te zetten naar decimale waarde
  726. while (g_cBsdBuf[bufferteller] != ' ')
  727. {
  728. waarde = waarde * 10 + ((int) (g_cBsdBuf[bufferteller]) - 48);
  729. bufferteller++;
  730. }
  731. UART_PRINT("Received %u packets successfully with value %s \n\r",
  732. g_ulPacketCount, g_cBsdBuf);
  733.  
  734. switch (waarde)
  735. {
  736. case 1:
  737. //wanneer de ontvangen waarde 1 is zal het eerste lampje branden (P64 of GPIO9)
  738. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000002<<2)) = 0X00000002;
  739. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000004<<2)) = 0X00000000;
  740. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000008<<2)) = 0X00000000;
  741. break;
  742. case 2:
  743. //wanneer de ontvangen waarde 2 is zal het tweede lampje branden (P1 of GPI10)
  744. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000002<<2)) = 0X00000000;
  745. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000004<<2)) = 0X00000004;
  746. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000008<<2)) = 0X00000000;
  747. break;
  748. case 3:
  749. //wanneer de ontvangen waarde 3 is zal het derde lampje branden (P2 of GPI11)
  750. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000002<<2)) = 0X00000000;
  751. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000004<<2)) = 0X00000000;
  752. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000008<<2)) = 0X00000008;
  753. break;
  754. default:
  755. //wanneer de ontvangen waarde 0 is zullen alle ledjes uitgaan
  756. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000002<<2)) = 0X00000000;
  757. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000004<<2)) = 0X00000000;
  758. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000008<<2)) = 0X00000000;
  759. }
  760.  
  761. sl_Close(iSockID);
  762.  
  763. return SUCCESS;
  764. }
  765.  
  766. //****************************************************************************
  767. //
  768. //! \brief Connecting to a WLAN Accesspoint
  769. //!
  770. //! This function connects to the required AP (SSID_NAME) with Security
  771. //! parameters specified in te form of macros at the top of this file
  772. //!
  773. //! \param[in] None
  774. //!
  775. //! \return status value
  776. //!
  777. //! \warning If the WLAN connection fails or we don't aquire an IP
  778. //! address, It will be stuck in this function forever.
  779. //
  780. //****************************************************************************
  781. static long WlanConnect()
  782. {
  783. SlSecParams_t secParams = { 0 };
  784. long lRetVal = 0;
  785.  
  786. secParams.Key = (signed char*) SECURITY_KEY;
  787. secParams.KeyLen = strlen(SECURITY_KEY);
  788. secParams.Type = SECURITY_TYPE;
  789.  
  790. lRetVal = sl_WlanConnect((signed char*) SSID_NAME, strlen(SSID_NAME), 0,
  791. &secParams, 0);
  792. ASSERT_ON_ERROR(lRetVal);
  793.  
  794. while ((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
  795. {
  796. // Wait for WLAN Event
  797. #ifndef SL_PLATFORM_MULTI_THREADED
  798. _SlNonOsMainLoopTask();
  799. #endif
  800.  
  801. }
  802.  
  803. return SUCCESS;
  804. }
  805.  
  806. //*****************************************************************************
  807. //
  808. //! Application startup display on UART
  809. //!
  810. //! \param none
  811. //!
  812. //! \return none
  813. //!
  814. //*****************************************************************************
  815. static void DisplayBanner(char * AppName)
  816. {
  817. UART_PRINT("\n\n\n\r");
  818. UART_PRINT("\t\t *************************************************\n\r");
  819. UART_PRINT("\t\t CC3200 %s Application \n\r", AppName);
  820. UART_PRINT("\t\t *************************************************\n\r");
  821. UART_PRINT("\n\n\n\r");
  822. }
  823.  
  824. //*****************************************************************************
  825. //
  826. //! Board Initialization & Configuration
  827. //!
  828. //! \param None
  829. //!
  830. //! \return None
  831. //
  832. //*****************************************************************************
  833. static void BoardInit(void)
  834. {
  835.  
  836. /* In case of TI-RTOS vector table is initialize by OS itself */
  837. #ifndef USE_TIRTOS
  838. //
  839. // Set vector table base
  840. //
  841. #if defined(ccs) || defined(gcc)
  842. MAP_IntVTableBaseSet((unsigned long) &g_pfnVectors[0]);
  843. #endif
  844. #if defined(ewarm)
  845. MAP_IntVTableBaseSet((unsigned long)&__vector_table);
  846. #endif
  847. #endif
  848. //
  849. // Enable Processor
  850. //
  851. MAP_IntMasterEnable();
  852. MAP_IntEnable(FAULT_SYSTICK);
  853.  
  854. PRCMCC3200MCUInit();
  855.  
  856. }
  857.  
  858. //****************************************************************************
  859. // MAIN FUNCTION
  860. //****************************************************************************
  861. void main()
  862. {
  863.  
  864. long lRetVal = -1;
  865.  
  866. //
  867. // Board Initialization
  868. //
  869. BoardInit();
  870.  
  871. //
  872. // uDMA Initialization
  873. //
  874. UDMAInit();
  875.  
  876. //
  877. // Configure the pinmux settings for the peripherals exercised
  878. //
  879. PinMuxConfig();
  880.  
  881. InitTerm();
  882.  
  883. //
  884. // Display banner
  885. //
  886. DisplayBanner(APPLICATION_NAME);
  887.  
  888. InitializeAppVariables();
  889.  
  890. //
  891. // Following function configure the device to default state by cleaning
  892. // the persistent settings stored in NVMEM (viz. connection profiles &
  893. // policies, power policy etc)
  894. //
  895. // Applications may choose to skip this step if the developer is sure
  896. // that the device is in its desired state at start of applicaton
  897. //
  898. // Note that all profiles and persistent settings that were done on the
  899. // device will be lost
  900. //
  901. lRetVal = ConfigureSimpleLinkToDefaultState();
  902.  
  903. if (lRetVal < 0)
  904. {
  905. if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
  906. UART_PRINT(
  907. "Failed to configure the device in its default state \n\r");
  908.  
  909. LOOP_FOREVER()
  910. ;
  911. }
  912.  
  913. UART_PRINT("Device is configured in default state \n\r");
  914.  
  915. //
  916. // Asumption is that the device is configured in station mode already
  917. // and it is in its default state
  918. //
  919. lRetVal = sl_Start(0, 0, 0);
  920. if (lRetVal < 0 || lRetVal != ROLE_STA)
  921. {
  922. UART_PRINT("Failed to start the device \n\r");
  923. LOOP_FOREVER()
  924. ;
  925. }
  926.  
  927. UART_PRINT("Device started as STATION \n\r");
  928.  
  929. UART_PRINT("Connecting to AP: %s ...\r\n", SSID_NAME);
  930.  
  931. //
  932. //Connecting to WLAN AP
  933. //
  934. lRetVal = WlanConnect();
  935. if (lRetVal < 0)
  936. {
  937. UART_PRINT("Failed to establish connection w/ an AP \n\r");
  938. LOOP_FOREVER()
  939. ;
  940. }
  941.  
  942. UART_PRINT("Connected to AP: %s \n\r", SSID_NAME);
  943.  
  944. UART_PRINT("Device IP: %d.%d.%d.%d\n\r\n\r", SL_IPV4_BYTE(g_ulIpAddr, 3),
  945. SL_IPV4_BYTE(g_ulIpAddr, 2), SL_IPV4_BYTE(g_ulIpAddr, 1),
  946. SL_IPV4_BYTE(g_ulIpAddr, 0));
  947. //de registers van de drie leds worden declareerd
  948. // set port A1.1 as output (RED LED)
  949. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DIR) |= 0x00000002;
  950. // switch off the pin
  951. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000002<<2)) = 0x00000000;
  952. // set port A1.2 as output (YELLOW LED)
  953. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DIR) |= 0x00000004;
  954. // switch off the pin
  955. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000004<<2)) = 0x00000000;
  956. // set port A1.3 as output (GREEN LED)
  957. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DIR) |= 0x00000008;
  958. // switch off the pin
  959. HWREG(GPIOA1_BASE + GPIO_O_GPIO_DATA + (0x00000008<<2)) = 0x00000000;
  960.  
  961. while (1)
  962. {
  963. //
  964. // Wait to receive a character over UART
  965. //
  966. UART_PRINT("(*)\n\r");
  967.  
  968. // After calling this function, you can start sending data
  969. // to CC3200 IP address on PORT_NUM
  970. lRetVal = BsdUdpServer(g_uiPortNum);
  971. ASSERT_ON_ERROR(lRetVal);
  972. }
  973.  
  974. UART_PRINT("Exiting Application ...\n\r");
  975.  
  976. //
  977. // power off the network processor
  978. //
  979. lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  980.  
  981. while (1)
  982. {
  983. _SlNonOsMainLoopTask();
  984. }
  985. }
  986.  
  987. //*****************************************************************************
  988. //
  989. // Close the Doxygen group.
  990. //! @}
  991. //
  992. //*****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement