Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.74 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 - SSL Demo
  40. // Application Overview - This is a sample application demonstrating the
  41. // use of secure sockets on a CC3200 device.The
  42. // application connects to an AP and
  43. // tries to establish a secure connection to the
  44. // Google server.
  45. // Application Details -
  46. // docs\examples\CC32xx_SSL_Demo_Application.pdf
  47. // or
  48. // http://processors.wiki.ti.com/index.php/CC32xx_SSL_Demo_Application
  49. //
  50. //*****************************************************************************
  51.  
  52.  
  53. //*****************************************************************************
  54. //
  55. //! \addtogroup ssl
  56. //! @{
  57. //
  58. //*****************************************************************************
  59.  
  60. // Simplelink includes
  61. #include "simplelink.h"
  62.  
  63. //Driverlib includes
  64. #include "hw_types.h"
  65. #include "hw_ints.h"
  66. #include "rom.h"
  67. #include "rom_map.h"
  68. #include "interrupt.h"
  69. #include "prcm.h"
  70. #include "utils.h"
  71. #include "uart.h"
  72.  
  73. //Common interface includes
  74. #include "pinmux.h"
  75. #include "gpio_if.h"
  76. #include "common.h"
  77. #include "uart_if.h"
  78.  
  79. #include <stdio.h>
  80.  
  81. #define MAX_URI_SIZE 128
  82. #define URI_SIZE MAX_URI_SIZE + 1
  83.  
  84.  
  85. #define APPLICATION_NAME "SSL"
  86. #define APPLICATION_VERSION "1.1.1"
  87.  
  88. #define SERVER_NAME "a33mfwrwnjicue.iot.us-east-1.amazonaws.com"
  89. #define GOOGLE_DST_PORT 8443
  90.  
  91. #define SL_SSL_CA_CERT "/cert/rootCA.der"
  92. #define SL_SSL_PRIVATE "/cert/private.der"
  93. #define SL_SSL_CLIENT "/cert/cert.der"
  94.  
  95. //NEED TO UPDATE THIS FOR IT TO WORK!
  96. #define DATE 29 /* Current Date */
  97. #define MONTH 2 /* Month 1-12 */
  98. #define YEAR 2016 /* Current year */
  99. #define HOUR 13 /* Time - hours */
  100. #define MINUTE 39 /* Time - minutes */
  101. #define SECOND 0 /* Time - seconds */
  102.  
  103. #define POSTHEADER "POST /things/AlexCC3200/shadow"
  104. #define HOSTHEADER "Host: a33mfwrwnjicue.iot.us-east-1.amazonaws.com\r\n"
  105. #define CHEADER "Connection: Keep-Alive\r\n"
  106. #define CTHEADER "Content-Type: application/json; charset=utf-8\r\n"
  107. #define CLHEADER1 "Content-Length: "
  108. #define CLHEADER2 "\r\n"
  109. #define DATA1 "{}"
  110.  
  111.  
  112. //#define DATA1 "{ \
  113. // \"Status\": { \
  114. // \"input\": { \
  115. // \"curr\":\"Hello\", \"prev\": \"hello!\" \
  116. // } \
  117. // } \
  118. // }"
  119. //#define DATA1 "{\"Status\": {\"CC3200 Sensor\",\"Temp\":"
  120. //#define DATA2 ",\"Humidity\":50,\"Location\":\"YourLocation\",\"Room\":\"YourRoom\",\"Info\":\"Sent from CC3200 LaunchPad\"}"
  121.  
  122. // Application specific status/error codes
  123. typedef enum{
  124. // Choosing -0x7D0 to avoid overlap w/ host-driver's error codes
  125. LAN_CONNECTION_FAILED = -0x7D0,
  126. INTERNET_CONNECTION_FAILED = LAN_CONNECTION_FAILED - 1,
  127. DEVICE_NOT_IN_STATION_MODE = INTERNET_CONNECTION_FAILED - 1,
  128.  
  129. STATUS_CODE_MAX = -0xBB8
  130. }e_AppStatusCodes;
  131.  
  132. typedef struct
  133. {
  134. /* time */
  135. unsigned long tm_sec;
  136. unsigned long tm_min;
  137. unsigned long tm_hour;
  138. /* date */
  139. unsigned long tm_day;
  140. unsigned long tm_mon;
  141. unsigned long tm_year;
  142. unsigned long tm_week_day; //not required
  143. unsigned long tm_year_day; //not required
  144. unsigned long reserved[3];
  145. }SlDateTime;
  146.  
  147.  
  148. //*****************************************************************************
  149. // GLOBAL VARIABLES -- Start
  150. //*****************************************************************************
  151. volatile unsigned long g_ulStatus = 0;//SimpleLink Status
  152. unsigned long g_ulPingPacketsRecv = 0; //Number of Ping Packets received
  153. unsigned long g_ulGatewayIP = 0; //Network Gateway IP address
  154. unsigned char g_ucConnectionSSID[SSID_LEN_MAX+1]; //Connection SSID
  155. unsigned char g_ucConnectionBSSID[BSSID_LEN_MAX]; //Connection BSSID
  156. signed char *g_Host = SERVER_NAME;
  157. SlDateTime g_time;
  158. #if defined(ccs) || defined(gcc)
  159. extern void (* const g_pfnVectors[])(void);
  160. #endif
  161. #if defined(ewarm)
  162. extern uVectorEntry __vector_table;
  163. #endif
  164. //*****************************************************************************
  165. // GLOBAL VARIABLES -- End
  166. //*****************************************************************************
  167.  
  168.  
  169. //****************************************************************************
  170. // LOCAL FUNCTION PROTOTYPES
  171. //****************************************************************************
  172. static long WlanConnect();
  173. static int set_time();
  174. static void BoardInit(void);
  175. static long InitializeAppVariables();
  176. static int tls_connect();
  177. static int connectToAccessPoint();
  178. static int http_post(int);
  179.  
  180. //*****************************************************************************
  181. // SimpleLink Asynchronous Event Handlers -- Start
  182. //*****************************************************************************
  183.  
  184.  
  185. //*****************************************************************************
  186. //
  187. //! \brief The Function Handles WLAN Events
  188. //!
  189. //! \param[in] pWlanEvent - Pointer to WLAN Event Info
  190. //!
  191. //! \return None
  192. //!
  193. //*****************************************************************************
  194. void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)
  195. {
  196. if(!pWlanEvent)
  197. {
  198. return;
  199. }
  200.  
  201. switch(pWlanEvent->Event)
  202. {
  203. case SL_WLAN_CONNECT_EVENT:
  204. {
  205. SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  206.  
  207. //
  208. // Information about the connected AP (like name, MAC etc) will be
  209. // available in 'slWlanConnectAsyncResponse_t'.
  210. // Applications can use it if required
  211. //
  212. // slWlanConnectAsyncResponse_t *pEventData = NULL;
  213. // pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;
  214. //
  215.  
  216. // Copy new connection SSID and BSSID to global parameters
  217. memcpy(g_ucConnectionSSID,pWlanEvent->EventData.
  218. STAandP2PModeWlanConnected.ssid_name,
  219. pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_len);
  220. memcpy(g_ucConnectionBSSID,
  221. pWlanEvent->EventData.STAandP2PModeWlanConnected.bssid,
  222. SL_BSSID_LENGTH);
  223.  
  224. UART_PRINT("[WLAN EVENT] STA Connected to the AP: %s , "
  225. "BSSID: %x:%x:%x:%x:%x:%x\n\r",
  226. g_ucConnectionSSID,g_ucConnectionBSSID[0],
  227. g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  228. g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  229. g_ucConnectionBSSID[5]);
  230. }
  231. break;
  232.  
  233. case SL_WLAN_DISCONNECT_EVENT:
  234. {
  235. slWlanConnectAsyncResponse_t* pEventData = NULL;
  236.  
  237. CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  238. CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  239.  
  240. pEventData = &pWlanEvent->EventData.STAandP2PModeDisconnected;
  241.  
  242. // If the user has initiated 'Disconnect' request,
  243. //'reason_code' is SL_USER_INITIATED_DISCONNECTION
  244. if(SL_USER_INITIATED_DISCONNECTION == pEventData->reason_code)
  245. {
  246. UART_PRINT("[WLAN EVENT]Device disconnected from the AP: %s,"
  247. "BSSID: %x:%x:%x:%x:%x:%x on application's request \n\r",
  248. g_ucConnectionSSID,g_ucConnectionBSSID[0],
  249. g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  250. g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  251. g_ucConnectionBSSID[5]);
  252. }
  253. else
  254. {
  255. UART_PRINT("[WLAN ERROR]Device disconnected from the AP AP: %s, "
  256. "BSSID: %x:%x:%x:%x:%x:%x on an ERROR..!! \n\r",
  257. g_ucConnectionSSID,g_ucConnectionBSSID[0],
  258. g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  259. g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  260. g_ucConnectionBSSID[5]);
  261. }
  262. memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));
  263. memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));
  264. }
  265. break;
  266.  
  267. default:
  268. {
  269. UART_PRINT("[WLAN EVENT] Unexpected event [0x%x]\n\r",
  270. pWlanEvent->Event);
  271. }
  272. break;
  273. }
  274. }
  275.  
  276. //*****************************************************************************
  277. //
  278. //! \brief This function handles network events such as IP acquisition, IP
  279. //! leased, IP released etc.
  280. //!
  281. //! \param[in] pNetAppEvent - Pointer to NetApp Event Info
  282. //!
  283. //! \return None
  284. //!
  285. //*****************************************************************************
  286. void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
  287. {
  288. if(!pNetAppEvent)
  289. {
  290. return;
  291. }
  292.  
  293. switch(pNetAppEvent->Event)
  294. {
  295. case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
  296. {
  297. SlIpV4AcquiredAsync_t *pEventData = NULL;
  298.  
  299. SET_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  300.  
  301. //Ip Acquired Event Data
  302. pEventData = &pNetAppEvent->EventData.ipAcquiredV4;
  303.  
  304. //Gateway IP address
  305. g_ulGatewayIP = pEventData->gateway;
  306.  
  307. UART_PRINT("[NETAPP EVENT] IP Acquired: IP=%d.%d.%d.%d , "
  308. "Gateway=%d.%d.%d.%d\n\r",
  309. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,3),
  310. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,2),
  311. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,1),
  312. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,0),
  313. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,3),
  314. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,2),
  315. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,1),
  316. SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,0));
  317. }
  318. break;
  319.  
  320. default:
  321. {
  322. UART_PRINT("[NETAPP EVENT] Unexpected event [0x%x] \n\r",
  323. pNetAppEvent->Event);
  324. }
  325. break;
  326. }
  327. }
  328.  
  329.  
  330. //*****************************************************************************
  331. //
  332. //! \brief This function handles HTTP server events
  333. //!
  334. //! \param[in] pServerEvent - Contains the relevant event information
  335. //! \param[in] pServerResponse - Should be filled by the user with the
  336. //! relevant response information
  337. //!
  338. //! \return None
  339. //!
  340. //****************************************************************************
  341. void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent,
  342. SlHttpServerResponse_t *pHttpResponse)
  343. {
  344. // Unused in this application
  345. }
  346.  
  347. //*****************************************************************************
  348. //
  349. //! \brief This function handles General Events
  350. //!
  351. //! \param[in] pDevEvent - Pointer to General Event Info
  352. //!
  353. //! \return None
  354. //!
  355. //*****************************************************************************
  356. void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
  357. {
  358. if(!pDevEvent)
  359. {
  360. return;
  361. }
  362.  
  363. //
  364. // Most of the general errors are not FATAL are are to be handled
  365. // appropriately by the application
  366. //
  367. UART_PRINT("[GENERAL EVENT] - ID=[%d] Sender=[%d]\n\n",
  368. pDevEvent->EventData.deviceEvent.status,
  369. pDevEvent->EventData.deviceEvent.sender);
  370. }
  371.  
  372.  
  373. //*****************************************************************************
  374. //
  375. //! This function handles socket events indication
  376. //!
  377. //! \param[in] pSock - Pointer to Socket Event Info
  378. //!
  379. //! \return None
  380. //!
  381. //*****************************************************************************
  382. void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
  383. {
  384. if(!pSock)
  385. {
  386. return;
  387. }
  388.  
  389. switch( pSock->Event )
  390. {
  391. case SL_SOCKET_TX_FAILED_EVENT:
  392. switch( pSock->socketAsyncEvent.SockTxFailData.status)
  393. {
  394. case SL_ECLOSE:
  395. UART_PRINT("[SOCK ERROR] - close socket (%d) operation "
  396. "failed to transmit all queued packets\n\n",
  397. pSock->socketAsyncEvent.SockTxFailData.sd);
  398. break;
  399. default:
  400. UART_PRINT("[SOCK ERROR] - TX FAILED : socket %d , reason "
  401. "(%d) \n\n",
  402. pSock->socketAsyncEvent.SockTxFailData.sd, pSock->socketAsyncEvent.SockTxFailData.status);
  403. break;
  404. }
  405. break;
  406.  
  407. default:
  408. UART_PRINT("[SOCK EVENT] - Unexpected Event [%x0x]\n\n",pSock->Event);
  409. break;
  410. }
  411.  
  412. }
  413.  
  414.  
  415. //*****************************************************************************
  416. // SimpleLink Asynchronous Event Handlers -- End
  417. //*****************************************************************************
  418.  
  419.  
  420. //*****************************************************************************
  421. //
  422. //! \brief This function initializes the application variables
  423. //!
  424. //! \param 0 on success else error code
  425. //!
  426. //! \return None
  427. //!
  428. //*****************************************************************************
  429. static long InitializeAppVariables()
  430. {
  431. g_ulStatus = 0;
  432. g_ulGatewayIP = 0;
  433. g_Host = SERVER_NAME;
  434. memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));
  435. memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));
  436. return SUCCESS;
  437. }
  438.  
  439.  
  440. //*****************************************************************************
  441. //! \brief This function puts the device in its default state. It:
  442. //! - Set the mode to STATION
  443. //! - Configures connection policy to Auto and AutoSmartConfig
  444. //! - Deletes all the stored profiles
  445. //! - Enables DHCP
  446. //! - Disables Scan policy
  447. //! - Sets Tx power to maximum
  448. //! - Sets power policy to normal
  449. //! - Unregister mDNS services
  450. //! - Remove all filters
  451. //!
  452. //! \param none
  453. //! \return On success, zero is returned. On error, negative is returned
  454. //*****************************************************************************
  455. static long ConfigureSimpleLinkToDefaultState()
  456. {
  457. SlVersionFull ver = {0};
  458. _WlanRxFilterOperationCommandBuff_t RxFilterIdMask = {0};
  459.  
  460. unsigned char ucVal = 1;
  461. unsigned char ucConfigOpt = 0;
  462. unsigned char ucConfigLen = 0;
  463. unsigned char ucPower = 0;
  464.  
  465. long lRetVal = -1;
  466. long lMode = -1;
  467.  
  468. lMode = sl_Start(0, 0, 0);
  469. ASSERT_ON_ERROR(lMode);
  470.  
  471. // If the device is not in station-mode, try configuring it in station-mode
  472. if (ROLE_STA != lMode)
  473. {
  474. if (ROLE_AP == lMode)
  475. {
  476. // If the device is in AP mode, we need to wait for this event
  477. // before doing anything
  478. while(!IS_IP_ACQUIRED(g_ulStatus))
  479. {
  480. #ifndef SL_PLATFORM_MULTI_THREADED
  481. _SlNonOsMainLoopTask();
  482. #endif
  483. }
  484. }
  485.  
  486. // Switch to STA role and restart
  487. lRetVal = sl_WlanSetMode(ROLE_STA);
  488. ASSERT_ON_ERROR(lRetVal);
  489.  
  490. lRetVal = sl_Stop(0xFF);
  491. ASSERT_ON_ERROR(lRetVal);
  492.  
  493. lRetVal = sl_Start(0, 0, 0);
  494. ASSERT_ON_ERROR(lRetVal);
  495.  
  496. // Check if the device is in station again
  497. if (ROLE_STA != lRetVal)
  498. {
  499. // We don't want to proceed if the device is not coming up in STA-mode
  500. return DEVICE_NOT_IN_STATION_MODE;
  501. }
  502. }
  503.  
  504. // Get the device's version-information
  505. ucConfigOpt = SL_DEVICE_GENERAL_VERSION;
  506. ucConfigLen = sizeof(ver);
  507. lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt,
  508. &ucConfigLen, (unsigned char *)(&ver));
  509. ASSERT_ON_ERROR(lRetVal);
  510.  
  511. UART_PRINT("Host Driver Version: %s\n\r",SL_DRIVER_VERSION);
  512. UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r",
  513. ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3],
  514. ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1],
  515. ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3],
  516. ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1],
  517. ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]);
  518.  
  519. // Set connection policy to Auto + SmartConfig
  520. // (Device's default connection policy)
  521. lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
  522. SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
  523. ASSERT_ON_ERROR(lRetVal);
  524.  
  525. // Remove all profiles
  526. lRetVal = sl_WlanProfileDel(0xFF);
  527. ASSERT_ON_ERROR(lRetVal);
  528.  
  529.  
  530.  
  531. //
  532. // Device in station-mode. Disconnect previous connection if any
  533. // The function returns 0 if 'Disconnected done', negative number if already
  534. // disconnected Wait for 'disconnection' event if 0 is returned, Ignore
  535. // other return-codes
  536. //
  537. lRetVal = sl_WlanDisconnect();
  538. if(0 == lRetVal)
  539. {
  540. // Wait
  541. while(IS_CONNECTED(g_ulStatus))
  542. {
  543. #ifndef SL_PLATFORM_MULTI_THREADED
  544. _SlNonOsMainLoopTask();
  545. #endif
  546. }
  547. }
  548.  
  549. // Enable DHCP client
  550. lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);
  551. ASSERT_ON_ERROR(lRetVal);
  552.  
  553. // Disable scan
  554. ucConfigOpt = SL_SCAN_POLICY(0);
  555. lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0);
  556. ASSERT_ON_ERROR(lRetVal);
  557.  
  558. // Set Tx power level for station mode
  559. // Number between 0-15, as dB offset from max power - 0 will set max power
  560. ucPower = 0;
  561. lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
  562. WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower);
  563. ASSERT_ON_ERROR(lRetVal);
  564.  
  565. // Set PM policy to normal
  566. lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);
  567. ASSERT_ON_ERROR(lRetVal);
  568.  
  569. // Unregister mDNS services
  570. lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
  571. ASSERT_ON_ERROR(lRetVal);
  572.  
  573. // Remove all 64 filters (8*8)
  574. memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
  575. lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask,
  576. sizeof(_WlanRxFilterOperationCommandBuff_t));
  577. ASSERT_ON_ERROR(lRetVal);
  578.  
  579. lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  580. ASSERT_ON_ERROR(lRetVal);
  581.  
  582. InitializeAppVariables();
  583.  
  584. return lRetVal; // Success
  585. }
  586.  
  587.  
  588. //*****************************************************************************
  589. //
  590. //! Board Initialization & Configuration
  591. //!
  592. //! \param None
  593. //!
  594. //! \return None
  595. //
  596. //*****************************************************************************
  597. static void BoardInit(void)
  598. {
  599. /* In case of TI-RTOS vector table is initialize by OS itself */
  600. #ifndef USE_TIRTOS
  601. //
  602. // Set vector table base
  603. //
  604. #if defined(ccs)
  605. MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
  606. #endif
  607. #if defined(ewarm)
  608. MAP_IntVTableBaseSet((unsigned long)&__vector_table);
  609. #endif
  610. #endif
  611. //
  612. // Enable Processor
  613. //
  614. MAP_IntMasterEnable();
  615. MAP_IntEnable(FAULT_SYSTICK);
  616.  
  617. PRCMCC3200MCUInit();
  618. }
  619.  
  620.  
  621. //****************************************************************************
  622. //
  623. //! \brief Connecting to a WLAN Accesspoint
  624. //!
  625. //! This function connects to the required AP (SSID_NAME) with Security
  626. //! parameters specified in te form of macros at the top of this file
  627. //!
  628. //! \param None
  629. //!
  630. //! \return 0 on success else error code
  631. //!
  632. //! \warning If the WLAN connection fails or we don't aquire an IP
  633. //! address, It will be stuck in this function forever.
  634. //
  635. //****************************************************************************
  636. static long WlanConnect()
  637. {
  638. SlSecParams_t secParams = {0};
  639. long lRetVal = 0;
  640.  
  641. secParams.Key = SECURITY_KEY;
  642. secParams.KeyLen = strlen(SECURITY_KEY);
  643. secParams.Type = SECURITY_TYPE;
  644.  
  645. lRetVal = sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
  646. ASSERT_ON_ERROR(lRetVal);
  647.  
  648. // Wait for WLAN Event
  649. while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
  650. {
  651. // Toggle LEDs to Indicate Connection Progress
  652. _SlNonOsMainLoopTask();
  653. GPIO_IF_LedOff(MCU_IP_ALLOC_IND);
  654. MAP_UtilsDelay(800000);
  655. _SlNonOsMainLoopTask();
  656. GPIO_IF_LedOn(MCU_IP_ALLOC_IND);
  657. MAP_UtilsDelay(800000);
  658. }
  659.  
  660. return SUCCESS;
  661.  
  662. }
  663.  
  664. //*****************************************************************************
  665. //
  666. //! This function updates the date and time of CC3200.
  667. //!
  668. //! \param None
  669. //!
  670. //! \return
  671. //! 0 for success, negative otherwise
  672. //!
  673. //*****************************************************************************
  674.  
  675. static int set_time()
  676. {
  677. long retVal;
  678.  
  679. g_time.tm_day = DATE;
  680. g_time.tm_mon = MONTH;
  681. g_time.tm_year = YEAR;
  682. g_time.tm_sec = HOUR;
  683. g_time.tm_hour = MINUTE;
  684. g_time.tm_min = SECOND;
  685.  
  686. retVal = sl_DevSet(SL_DEVICE_GENERAL_CONFIGURATION,
  687. SL_DEVICE_GENERAL_CONFIGURATION_DATE_TIME,
  688. sizeof(SlDateTime),(unsigned char *)(&g_time));
  689.  
  690. ASSERT_ON_ERROR(retVal);
  691. return SUCCESS;
  692. }
  693.  
  694. //*****************************************************************************
  695. //
  696. //! This function demonstrates how certificate can be used with SSL.
  697. //! The procedure includes the following steps:
  698. //! 1) connect to an open AP
  699. //! 2) get the server name via a DNS request
  700. //! 3) define all socket options and point to the CA certificate
  701. //! 4) connect to the server via TCP
  702. //!
  703. //! \param None
  704. //!
  705. //! \return 0 on success else error code
  706. //! \return LED1 is turned solid in case of success
  707. //! LED2 is turned solid in case of failure
  708. //!
  709. //*****************************************************************************
  710. static int tls_connect()
  711. {
  712. SlSockAddrIn_t Addr;
  713. int iAddrSize;
  714. unsigned char ucMethod = SL_SO_SEC_METHOD_TLSV1_2;
  715. unsigned int uiIP,uiCipher = SL_SEC_MASK_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA;
  716. long lRetVal = -1;
  717. int iSockID;
  718.  
  719. lRetVal = sl_NetAppDnsGetHostByName(g_Host, strlen((const char *)g_Host),
  720. (unsigned long*)&uiIP, SL_AF_INET);
  721.  
  722. if(lRetVal < 0)
  723. {
  724. UART_PRINT("Device couldn't retrive the host name \n\r");
  725. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  726. return lRetVal;
  727. }
  728.  
  729. Addr.sin_family = SL_AF_INET;
  730. Addr.sin_port = sl_Htons(GOOGLE_DST_PORT);
  731. Addr.sin_addr.s_addr = sl_Htonl(uiIP);
  732. iAddrSize = sizeof(SlSockAddrIn_t);
  733. //
  734. // opens a secure socket
  735. //
  736. iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET);
  737. if( iSockID < 0 )
  738. {
  739. UART_PRINT("Device unable to create secure socket \n\r");
  740. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  741. return lRetVal;
  742. }
  743.  
  744. //
  745. // configure the socket as TLS1.2
  746. //
  747. lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECMETHOD, &ucMethod,\
  748. sizeof(ucMethod));
  749. if(lRetVal < 0)
  750. {
  751. UART_PRINT("Device couldn't set socket options \n\r");
  752. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  753. return lRetVal;
  754. }
  755. //
  756. //configure the socket as ECDHE RSA WITH AES256 CBC SHA
  757. //
  758. lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &uiCipher,\
  759. sizeof(uiCipher));
  760. if(lRetVal < 0)
  761. {
  762. UART_PRINT("Device couldn't set socket options \n\r");
  763. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  764. return lRetVal;
  765. }
  766.  
  767. //
  768. //configure the socket with CA certificate - for server verification
  769. //
  770. lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \
  771. SL_SO_SECURE_FILES_CA_FILE_NAME, \
  772. SL_SSL_CA_CERT, \
  773. strlen(SL_SSL_CA_CERT));
  774.  
  775. if(lRetVal < 0)
  776. {
  777. UART_PRINT("Device couldn't set socket options \n\r");
  778. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  779. return lRetVal;
  780. }
  781.  
  782. //configure the socket with Client Certificate - for server verification
  783. //
  784. lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \
  785. SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, \
  786. SL_SSL_CLIENT, \
  787. strlen(SL_SSL_CLIENT));
  788.  
  789. if(lRetVal < 0)
  790. {
  791. UART_PRINT("Device couldn't set socket options \n\r");
  792. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  793. return lRetVal;
  794. }
  795.  
  796. //configure the socket with Private Key - for server verification
  797. //
  798. lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \
  799. SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, \
  800. SL_SSL_PRIVATE, \
  801. strlen(SL_SSL_PRIVATE));
  802.  
  803. if(lRetVal < 0)
  804. {
  805. UART_PRINT("Device couldn't set socket options \n\r");
  806. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  807. return lRetVal;
  808. }
  809.  
  810.  
  811. /* connect to the peer device - Google server */
  812. lRetVal = sl_Connect(iSockID, ( SlSockAddr_t *)&Addr, iAddrSize);
  813.  
  814. if(lRetVal < 0)
  815. {
  816. UART_PRINT("Device couldn't connect to AWS server \n\r");
  817. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  818. return lRetVal;
  819. }
  820. else{
  821. UART_PRINT("Device has connected to the website:");
  822. UART_PRINT(SERVER_NAME);
  823. UART_PRINT("\n\r");
  824. }
  825.  
  826. GPIO_IF_LedOff(MCU_RED_LED_GPIO);
  827. GPIO_IF_LedOn(MCU_GREEN_LED_GPIO);
  828. return iSockID;
  829. }
  830.  
  831. int connectToAccessPoint(){
  832. long lRetVal = -1;
  833. GPIO_IF_LedConfigure(LED1|LED3);
  834.  
  835. GPIO_IF_LedOff(MCU_RED_LED_GPIO);
  836. GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);
  837.  
  838. lRetVal = InitializeAppVariables();
  839. ASSERT_ON_ERROR(lRetVal);
  840.  
  841. //
  842. // Following function configure the device to default state by cleaning
  843. // the persistent settings stored in NVMEM (viz. connection profiles &
  844. // policies, power policy etc)
  845. //
  846. // Applications may choose to skip this step if the developer is sure
  847. // that the device is in its default state at start of applicaton
  848. //
  849. // Note that all profiles and persistent settings that were done on the
  850. // device will be lost
  851. //
  852. lRetVal = ConfigureSimpleLinkToDefaultState();
  853. if(lRetVal < 0)
  854. {
  855. if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
  856. UART_PRINT("Failed to configure the device in its default state \n\r");
  857.  
  858. return lRetVal;
  859. }
  860.  
  861. UART_PRINT("Device is configured in default state \n\r");
  862.  
  863. CLR_STATUS_BIT_ALL(g_ulStatus);
  864.  
  865. ///
  866. // Assumption is that the device is configured in station mode already
  867. // and it is in its default state
  868. //
  869. lRetVal = sl_Start(0, 0, 0);
  870. if (lRetVal < 0 || ROLE_STA != lRetVal)
  871. {
  872. UART_PRINT("Failed to start the device \n\r");
  873. return lRetVal;
  874. }
  875.  
  876. UART_PRINT("Device started as STATION \n\r");
  877.  
  878. //
  879. //Connecting to WLAN AP
  880. //
  881. lRetVal = WlanConnect();
  882. if(lRetVal < 0)
  883. {
  884. UART_PRINT("Failed to establish connection w/ an AP \n\r");
  885. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  886. return lRetVal;
  887. }
  888.  
  889. UART_PRINT("Connection established w/ AP and IP is aquired \n\r");
  890. return 0;
  891. }
  892.  
  893. //*****************************************************************************
  894. //
  895. //! Main
  896. //!
  897. //! \param none
  898. //!
  899. //! \return None
  900. //!
  901. //*****************************************************************************
  902. void main()
  903. {
  904. long lRetVal = -1;
  905. //
  906. // Initialize board configuration
  907. //
  908. BoardInit();
  909.  
  910. PinMuxConfig();
  911.  
  912. InitTerm();
  913. //Connect the CC3200 to the local access point
  914. lRetVal = connectToAccessPoint();
  915. //Set time so that encryption can be used
  916. lRetVal = set_time();
  917. if(lRetVal < 0)
  918. {
  919. UART_PRINT("Unable to set time in the device");
  920. LOOP_FOREVER();
  921. }
  922. //Connect to the website with TLS encryption
  923. lRetVal = tls_connect();
  924. if(lRetVal < 0)
  925. {
  926. ERR_PRINT(lRetVal);
  927. }
  928. http_post(lRetVal);
  929.  
  930. sl_Stop(SL_STOP_TIMEOUT);
  931. LOOP_FOREVER();
  932. }
  933. //*****************************************************************************
  934. //
  935. // Close the Doxygen group.
  936. //! @}
  937. //
  938. //*****************************************************************************
  939.  
  940. static int http_post(int iTLSSockID){
  941. char acSendBuff[512];
  942. char acRecvbuff[1460];
  943. char* pcBufHeaders;
  944. int lRetVal = 0;
  945.  
  946. pcBufHeaders = acSendBuff;
  947. strcpy(pcBufHeaders, POSTHEADER);
  948. pcBufHeaders += strlen(POSTHEADER);
  949. strcpy(pcBufHeaders, " HTTP/1.1\r\n");
  950. pcBufHeaders += strlen(" HTTP/1.1\r\n");
  951. strcpy(pcBufHeaders, HOSTHEADER);
  952. pcBufHeaders += strlen(HOSTHEADER);
  953. strcpy(pcBufHeaders, CHEADER);
  954. pcBufHeaders += strlen(CHEADER);
  955. strcpy(pcBufHeaders, "\r\n\r\n");
  956. pcBufHeaders += strlen("\r\n\r\n");
  957. strcpy(pcBufHeaders, DATA1);
  958. pcBufHeaders += strlen(DATA1);
  959. // pcBufHeaders += strlen(DATA2);
  960. // strcpy(pcBufHeaders, DATA2);
  961.  
  962. printf("Christy is dumb [%s]\n", acSendBuff);
  963.  
  964. int testDataLength = strlen(
  965. );
  966.  
  967. //
  968. // Send the packet to the server */
  969. //
  970. lRetVal = sl_Send(iTLSSockID, acSendBuff, strlen(acSendBuff), 0);
  971. if(lRetVal < 0)
  972. {
  973. UART_PRINT("POST failed. Error Number: %i\n\r",lRetVal);
  974. sl_Close(iTLSSockID);
  975. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  976. return lRetVal;
  977. }
  978. lRetVal = sl_Recv(iTLSSockID, &acRecvbuff[0], sizeof(acRecvbuff), 0);
  979. if(lRetVal < 0)
  980. {
  981. UART_PRINT("Received failed. Error Number: %i\n\r",lRetVal);
  982. //sl_Close(iSSLSockID);
  983. GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  984. return lRetVal;
  985. }
  986. else
  987. {
  988. acRecvbuff[lRetVal+1] = '\0';
  989. UART_PRINT(acRecvbuff);
  990. }
  991.  
  992. return 0;
  993. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement