Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 44.71 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_memmap.h"
  66. #include "hw_ints.h"
  67. #include "rom.h"
  68. #include "rom_map.h"
  69. #include "interrupt.h"
  70. #include "prcm.h"
  71. #include "utils.h"
  72. #include "uart.h"
  73.  
  74. //Common interface includes
  75. #include "pin_mux_config.h"
  76. #include "gpio_if.h"
  77. #include "common.h"
  78. #include "uart_if.h"
  79.  
  80. #include "timer_if.h"
  81. #include "timer.h"
  82. #include "utils.h"
  83.  
  84. #include "Adafruit_GFX.h"
  85. #include "Adafruit_SSD1351.h"
  86. #include "spi.h"
  87.  
  88. #include "gpio.h"
  89. #include "systick.h"
  90.  
  91. #include <stdio.h>
  92. #include <http/client/httpcli.h>
  93. #include <http/client/common.h>
  94.  
  95. #include "socket.h"
  96.  
  97. #define MAX_URI_SIZE 128
  98. #define URI_SIZE MAX_URI_SIZE + 1
  99.  
  100. extern int cursor_x;
  101. extern int cursor_y;
  102.  
  103. #define SPI_IF_BIT_RATE 100000
  104. #define TIME_CUTOFF 90000
  105.  
  106. #define CHARACTER_WIDTH 6
  107. #define LINE_HEIGHT 10
  108. #define MAX_CHARS 23
  109. #define MAX_LINES 13
  110.  
  111. #define MIDPOINT 7 * LINE_HEIGHT
  112.  
  113. float p = 3.1415926;
  114.  
  115. // Color definitions
  116. #define BLACK           0x0000
  117. #define BLUE            0x001F
  118. #define GREEN           0x07E0
  119. #define CYAN            0x07FF
  120. #define RED             0xF800
  121. #define MAGENTA         0xF81F
  122. #define YELLOW          0xFFE0
  123. #define WHITE           0xFFFF
  124.  
  125. #define TR_BUFF_SIZE     100
  126.  
  127. #define APPLICATION_NAME        "SSL"
  128. #define APPLICATION_VERSION     "1.1.1"
  129.  
  130. #define SERVER_NAME                "maker.ifttt.com"
  131. #define GOOGLE_DST_PORT             443
  132.  
  133. #define SL_SSL_CA_CERT "/cert/rootCA.der"
  134. #define SL_SSL_PRIVATE "/cert/private.der"
  135. #define SL_SSL_CLIENT  "/cert/cert.der"
  136.  
  137. //NEED TO UPDATE THIS FOR IT TO WORK!
  138. #define DATE                29    /* Current Date */
  139. #define MONTH               2     /* Month 1-12 */
  140. #define YEAR                2016  /* Current year */
  141. #define HOUR                13    /* Time - hours */
  142. #define MINUTE              39    /* Time - minutes */
  143. #define SECOND              0     /* Time - seconds */
  144.  
  145. #define POSTHEADER "POST /trigger/play_song/with/key/jUo1sD-OMqHqEUxFUJO53caZba1T5Tsyyg7vsYPOVli"
  146. #define HOSTHEADER "Host: maker.ifttt.com\r\n"
  147. #define CHEADER "Connection: Keep-Alive\r\n"
  148. #define CTHEADER "Content-Type: application/json; charset=utf-8\r\n"
  149. #define CLHEADER1 "Content-Length: "
  150. #define CLHEADER2 "\r\n\r\n"
  151. #define DATA1 "{\"value1\":\""
  152. #define DATA2 "\"}\r\n"
  153.  
  154.  
  155. // Application specific status/error codes
  156. typedef enum{
  157.     // Choosing -0x7D0 to avoid overlap w/ host-driver's error codes
  158.     LAN_CONNECTION_FAILED = -0x7D0,
  159.     INTERNET_CONNECTION_FAILED = LAN_CONNECTION_FAILED - 1,
  160.     DEVICE_NOT_IN_STATION_MODE = INTERNET_CONNECTION_FAILED - 1,
  161.  
  162.     STATUS_CODE_MAX = -0xBB8
  163. }e_AppStatusCodes;
  164.  
  165. typedef struct
  166. {
  167.    /* time */
  168.    unsigned long tm_sec;
  169.    unsigned long tm_min;
  170.    unsigned long tm_hour;
  171.    /* date */
  172.    unsigned long tm_day;
  173.    unsigned long tm_mon;
  174.    unsigned long tm_year;
  175.    unsigned long tm_week_day; //not required
  176.    unsigned long tm_year_day; //not required
  177.    unsigned long reserved[3];
  178. }SlDateTime;
  179.  
  180.  
  181. // Good lord
  182. char decodeIRSignal(int d[]) {
  183.     if (d[0] != 1 || d[1] != 0 || d[2] != 0 || d[3] != 0 || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 1 || d[8] != 0 || d[9] != 1 || d[10] != 1 || d[11] != 1 || d[12] != 1 || d[13] != 1 || d[14] != 1 || d[15] != 0 || d[16] != 1) {
  184.         return '-'; // error!
  185.     }
  186.  
  187.     if (
  188.         d[17] == 1 &&
  189.         d[18] == 0 &&
  190.         d[19] == 0 &&
  191.         d[20] == 0 &&
  192.         d[21] == 0 &&
  193.         d[22] == 0 &&
  194.         d[23] == 0 &&
  195.         d[24] == 0 &&
  196.         d[25] == 0 &&
  197.         d[26] == 1 &&
  198.         d[27] == 1 &&
  199.         d[28] == 1 &&
  200.         d[29] == 1 &&
  201.         d[30] == 1 &&
  202.         d[31] == 1 &&
  203.         d[32] == 1) {
  204.         return '.'; // 1
  205.     }
  206.  
  207.     if (
  208.         d[17] == 0 &&
  209.         d[18] == 1 &&
  210.         d[19] == 0 &&
  211.         d[20] == 0 &&
  212.         d[21] == 0 &&
  213.         d[22] == 0 &&
  214.         d[23] == 0 &&
  215.         d[24] == 0 &&
  216.         d[25] == 1 &&
  217.         d[26] == 0 &&
  218.         d[27] == 1 &&
  219.         d[28] == 1 &&
  220.         d[29] == 1 &&
  221.         d[30] == 1 &&
  222.         d[31] == 1 &&
  223.         d[32] == 1) {
  224.         return 'a'; // 2
  225.     }
  226.  
  227.     if (
  228.         d[17] == 1 &&
  229.         d[18] == 1 &&
  230.         d[19] == 0 &&
  231.         d[20] == 0 &&
  232.         d[21] == 0 &&
  233.         d[22] == 0 &&
  234.         d[23] == 0 &&
  235.         d[24] == 0 &&
  236.         d[25] == 0 &&
  237.         d[26] == 0 &&
  238.         d[27] == 1 &&
  239.         d[28] == 1 &&
  240.         d[29] == 1 &&
  241.         d[30] == 1 &&
  242.         d[31] == 1 &&
  243.         d[32] == 1) {
  244.         return 'd'; // 3
  245.     }
  246.  
  247.     if (
  248.         d[17] == 0 &&
  249.         d[18] == 0 &&
  250.         d[19] == 1 &&
  251.         d[20] == 0 &&
  252.         d[21] == 0 &&
  253.         d[22] == 0 &&
  254.         d[23] == 0 &&
  255.         d[24] == 0 &&
  256.         d[25] == 1 &&
  257.         d[26] == 1 &&
  258.         d[27] == 0 &&
  259.         d[28] == 1 &&
  260.         d[29] == 1 &&
  261.         d[30] == 1 &&
  262.         d[31] == 1 &&
  263.         d[32] == 1) {
  264.         return 'g'; // 4
  265.     }
  266.  
  267.     if (
  268.         d[17] == 1 &&
  269.         d[18] == 0 &&
  270.         d[19] == 1 &&
  271.         d[20] == 0 &&
  272.         d[21] == 0 &&
  273.         d[22] == 0 &&
  274.         d[23] == 0 &&
  275.         d[24] == 0 &&
  276.         d[25] == 0 &&
  277.         d[26] == 1 &&
  278.         d[27] == 0 &&
  279.         d[28] == 1 &&
  280.         d[29] == 1 &&
  281.         d[30] == 1 &&
  282.         d[31] == 1 &&
  283.         d[32] == 1) {
  284.         return 'j'; // 5
  285.     }
  286.  
  287.     if (
  288.         d[17] == 0 &&
  289.         d[18] == 1 &&
  290.         d[19] == 1 &&
  291.         d[20] == 0 &&
  292.         d[21] == 0 &&
  293.         d[22] == 0 &&
  294.         d[23] == 0 &&
  295.         d[24] == 0 &&
  296.         d[25] == 1 &&
  297.         d[26] == 0 &&
  298.         d[27] == 0 &&
  299.         d[28] == 1 &&
  300.         d[29] == 1 &&
  301.         d[30] == 1 &&
  302.         d[31] == 1 &&
  303.         d[32] == 1) {
  304.         return 'm'; // 6
  305.     }
  306.  
  307.     if (
  308.         d[17] == 1 &&
  309.         d[18] == 1 &&
  310.         d[19] == 1 &&
  311.         d[20] == 0 &&
  312.         d[21] == 0 &&
  313.         d[22] == 0 &&
  314.         d[23] == 0 &&
  315.         d[24] == 0 &&
  316.         d[25] == 0 &&
  317.         d[26] == 0 &&
  318.         d[27] == 0 &&
  319.         d[28] == 1 &&
  320.         d[29] == 1 &&
  321.         d[30] == 1 &&
  322.         d[31] == 1 &&
  323.         d[32] == 1) {
  324.         return 'p'; // 7
  325.     }
  326.  
  327.     if(
  328.         d[17] == 0 &&
  329.         d[18] == 0 &&
  330.         d[19] == 0 &&
  331.         d[20] == 0 &&
  332.         d[21] == 1 &&
  333.         d[22] == 0 &&
  334.         d[23] == 0 &&
  335.         d[24] == 0 &&
  336.         d[25] == 1 &&
  337.         d[26] == 1 &&
  338.         d[27] == 1 &&
  339.         d[28] == 1 &&
  340.         d[29] == 0 &&
  341.         d[30] == 1 &&
  342.         d[31] == 1 &&
  343.         d[32] == 1) {
  344.         return '9'; // mute
  345.     }
  346.  
  347.     if (
  348.         d[17] == 0 &&
  349.         d[18] == 0 &&
  350.         d[19] == 0 &&
  351.         d[20] == 1 &&
  352.         d[21] == 0 &&
  353.         d[22] == 0 &&
  354.         d[23] == 0 &&
  355.         d[24] == 0 &&
  356.         d[25] == 1 &&
  357.         d[26] == 1 &&
  358.         d[27] == 1 &&
  359.         d[28] == 0 &&
  360.         d[29] == 1 &&
  361.         d[30] == 1 &&
  362.         d[31] == 1 &&
  363.         d[32] == 1) {
  364.         return 't'; // 8
  365.     }
  366.  
  367.     if (
  368.         d[17] == 1 &&
  369.         d[18] == 0 &&
  370.         d[19] == 0 &&
  371.         d[20] == 1 &&
  372.         d[21] == 0 &&
  373.         d[22] == 0 &&
  374.         d[23] == 0 &&
  375.         d[24] == 0 &&
  376.         d[25] == 0 &&
  377.         d[26] == 1 &&
  378.         d[27] == 1 &&
  379.         d[28] == 0 &&
  380.         d[29] == 1 &&
  381.         d[30] == 1 &&
  382.         d[31] == 1 &&
  383.         d[32] == 1) {
  384.         return 'w'; // 9
  385.     }
  386.  
  387.     if (
  388.         d[17] == 0 &&
  389.         d[18] == 0 &&
  390.         d[19] == 0 &&
  391.         d[20] == 0 &&
  392.         d[21] == 0 &&
  393.         d[22] == 0 &&
  394.         d[23] == 0 &&
  395.         d[24] == 0 &&
  396.         d[25] == 1 &&
  397.         d[26] == 1 &&
  398.         d[27] == 1 &&
  399.         d[28] == 1 &&
  400.         d[29] == 1 &&
  401.         d[30] == 1 &&
  402.         d[31] == 1 &&
  403.         d[32] == 1) {
  404.         return ' '; // 0
  405.     }
  406.  
  407.     if (
  408.         d[17] == 1 &&
  409.         d[18] == 0 &&
  410.         d[19] == 0 &&
  411.         d[20] == 1 &&
  412.         d[21] == 1 &&
  413.         d[22] == 0 &&
  414.         d[23] == 0 &&
  415.         d[24] == 0 &&
  416.         d[25] == 0 &&
  417.         d[26] == 1 &&
  418.         d[27] == 1 &&
  419.         d[28] == 0 &&
  420.         d[29] == 0 &&
  421.         d[30] == 1 &&
  422.         d[31] == 1 &&
  423.         d[32] == 1) {
  424.         return '-'; // up
  425.     }
  426.  
  427.     if (
  428.         d[17] == 1 &&
  429.         d[18] == 0 &&
  430.         d[19] == 1 &&
  431.         d[20] == 1 &&
  432.         d[21] == 1 &&
  433.         d[22] == 0 &&
  434.         d[23] == 0 &&
  435.         d[24] == 0 &&
  436.         d[25] == 0 &&
  437.         d[26] == 1 &&
  438.         d[27] == 0 &&
  439.         d[28] == 0 &&
  440.         d[29] == 0 &&
  441.         d[30] == 1 &&
  442.         d[31] == 1 &&
  443.         d[32] == 1) {
  444.         return '-'; // down
  445.     }
  446.  
  447.     if (
  448.         d[17] == 0 &&
  449.         d[18] == 1 &&
  450.         d[19] == 1 &&
  451.         d[20] == 0 &&
  452.         d[21] == 0 &&
  453.         d[22] == 0 &&
  454.         d[23] == 1 &&
  455.         d[24] == 0 &&
  456.         d[25] == 1 &&
  457.         d[26] == 0 &&
  458.         d[27] == 0 &&
  459.         d[28] == 1 &&
  460.         d[29] == 1 &&
  461.         d[30] == 1 &&
  462.         d[31] == 0 &&
  463.         d[32] == 1) {
  464.         return '-'; // left
  465.     }
  466.  
  467.     if (
  468.         d[17] == 1 &&
  469.         d[18] == 1 &&
  470.         d[19] == 1 &&
  471.         d[20] == 0 &&
  472.         d[21] == 0 &&
  473.         d[22] == 0 &&
  474.         d[23] == 1 &&
  475.         d[24] == 0 &&
  476.         d[25] == 0 &&
  477.         d[26] == 0 &&
  478.         d[27] == 0 &&
  479.         d[28] == 1 &&
  480.         d[29] == 1 &&
  481.         d[30] == 1 &&
  482.         d[31] == 0 &&
  483.         d[32] == 1) {
  484.         return '-'; // right
  485.     }
  486.  
  487.     if (
  488.         d[17] == 0 &&
  489.         d[18] == 1 &&
  490.         d[19] == 0 &&
  491.         d[20] == 1 &&
  492.         d[21] == 1 &&
  493.         d[22] == 0 &&
  494.         d[23] == 0 &&
  495.         d[24] == 0 &&
  496.         d[25] == 1 &&
  497.         d[26] == 0 &&
  498.         d[27] == 1 &&
  499.         d[28] == 0 &&
  500.         d[29] == 0 &&
  501.         d[30] == 1 &&
  502.         d[31] == 1 &&
  503.         d[32] == 1) {
  504.         return '-'; // volume up
  505.     }
  506.  
  507.     if (
  508.         d[17] == 0 &&
  509.         d[18] == 1 &&
  510.         d[19] == 1 &&
  511.         d[20] == 1 &&
  512.         d[21] == 1 &&
  513.         d[22] == 0 &&
  514.         d[23] == 0 &&
  515.         d[24] == 0 &&
  516.         d[25] == 1 &&
  517.         d[26] == 0 &&
  518.         d[27] == 0 &&
  519.         d[28] == 0 &&
  520.         d[29] == 0 &&
  521.         d[30] == 1 &&
  522.         d[31] == 1 &&
  523.         d[32] == 1) {
  524.         return '-'; // volume down
  525.     }
  526.  
  527.     if (
  528.         d[17] == 1 &&
  529.         d[18] == 1 &&
  530.         d[19] == 0 &&
  531.         d[20] == 1 &&
  532.         d[21] == 1 &&
  533.         d[22] == 0 &&
  534.         d[23] == 0 &&
  535.         d[24] == 0 &&
  536.         d[25] == 0 &&
  537.         d[26] == 0 &&
  538.         d[27] == 1 &&
  539.         d[28] == 0 &&
  540.         d[29] == 0 &&
  541.         d[30] == 1 &&
  542.         d[31] == 1 &&
  543.         d[32] == 1) {
  544.         return '-'; // channel up
  545.     }
  546.  
  547.     if (
  548.         d[17] == 1 &&
  549.         d[18] == 1 &&
  550.         d[19] == 1 &&
  551.         d[20] == 1 &&
  552.         d[21] == 1 &&
  553.         d[22] == 0 &&
  554.         d[23] == 0 &&
  555.         d[24] == 0 &&
  556.         d[25] == 0 &&
  557.         d[26] == 0 &&
  558.         d[27] == 0 &&
  559.         d[28] == 0 &&
  560.         d[29] == 0 &&
  561.         d[30] == 1 &&
  562.         d[31] == 1 &&
  563.         d[32] == 1) {
  564.         return '-'; // channel down
  565.     }
  566.  
  567.     return '-';
  568. }
  569.  
  570.  
  571. static int bottom_x = 0;
  572. static int bottom_y = MIDPOINT;
  573.  
  574. void resetBottomCursor() {
  575.     bottom_x = 0;
  576.     bottom_y = MIDPOINT;
  577. }
  578.  
  579. void clearBottom() {
  580.     resetBottomCursor();
  581.     fillRect(0, HEIGHT / 2, WIDTH, HEIGHT / 2, MAGENTA);
  582. }
  583.  
  584. void goBackOne() {
  585.     bottom_x -= CHARACTER_WIDTH;
  586.     if (bottom_x < 0) {
  587.         bottom_x = (MAX_CHARS - 1) * CHARACTER_WIDTH;
  588.         bottom_y -= LINE_HEIGHT;
  589.     }
  590. }
  591.  
  592. void writeToBottom(unsigned long character) {
  593.     switch (character) {
  594.     case 13:
  595.         bottom_y += LINE_HEIGHT;
  596.         bottom_x = 0;
  597.         break;
  598.     case 8:
  599.         goBackOne();
  600.         writeToBottom(' ');
  601.         goBackOne();
  602.         break;
  603.     default:
  604.         drawChar(bottom_x, bottom_y, character, BLACK, MAGENTA, 1);
  605.         bottom_x += CHARACTER_WIDTH;
  606.         if (bottom_x == MAX_CHARS * CHARACTER_WIDTH) {
  607.             bottom_x = 0;
  608.             bottom_y += LINE_HEIGHT;
  609.         }
  610.     }
  611. }
  612.  
  613. static int top_x, top_y;
  614.  
  615. void resetCursor() {
  616.     top_x = top_y = 0;
  617. }
  618.  
  619. void clearDisplay() {
  620.     fillScreen(BLACK);
  621.     resetCursor();
  622.     resetBottomCursor();
  623. }
  624.  
  625. void goBackTop() {
  626.     top_x -= CHARACTER_WIDTH;
  627.     if (top_x < 0) {
  628.         top_x = (MAX_CHARS - 1) * CHARACTER_WIDTH;
  629.         top_y -= LINE_HEIGHT;
  630.     }
  631. }
  632.  
  633. void clearTop() {
  634.     resetCursor();
  635.     fillRect(0, 0, WIDTH, HEIGHT / 2, BLACK);
  636. }
  637.  
  638. void writeCharToDisplay(unsigned long character) {
  639.     switch (character) {
  640.     case 13:
  641.         top_x = 0;
  642.         top_y += LINE_HEIGHT;
  643.         break;
  644.     case 8:
  645.         goBackTop();
  646.         writeCharToDisplay(' ');
  647.         goBackTop();
  648.         break;
  649.     default:
  650.         drawChar(top_x, top_y, character, WHITE, BLACK, 1);
  651.         top_x += CHARACTER_WIDTH;
  652.         if (top_x >= MAX_CHARS * CHARACTER_WIDTH) {
  653.             top_x = 0;
  654.             top_y += LINE_HEIGHT;
  655.             if (top_y >= LINE_HEIGHT * MAX_LINES) {
  656.                 clearDisplay();
  657.             }
  658.         }
  659.     }
  660. }
  661.  
  662.  
  663. tBoolean needsPrinting = false;
  664. tBoolean wrote = false;
  665.  
  666. unsigned long character;
  667. unsigned long c;
  668.  
  669. int textMessageIndex = 0;
  670. char textMessage[160]; // How realistic!
  671.  
  672. void terminalInterruptHandler() {
  673.     unsigned long ulUserData = 0;
  674.     ulUserData = MAP_UARTCharGet(UARTA0_BASE);
  675.  
  676.     wrote = true;
  677.     c = ulUserData;
  678.  
  679.     MAP_UARTCharPut(UARTA0_BASE, ulUserData);
  680.     MAP_UARTCharPut(UARTA1_BASE, ulUserData);
  681.  
  682.     MAP_UARTIntClear(UARTA0_BASE, UART_INT_RX);
  683. }
  684.  
  685. int pulse = 0;
  686. tBoolean ignore = true;
  687. unsigned long previous = 0;
  688.  
  689. char prevChar = '0';
  690. char currentChar = '0';
  691.  
  692. tBoolean print = false;
  693. int decodeArray[33];
  694.  
  695. int letterOffset = 0;
  696. void appendToTextMessage(char newChar) {
  697.     if (textMessageIndex >= 160) { return; }
  698.     textMessage[textMessageIndex] = newChar;
  699.     textMessageIndex++;
  700. }
  701.  
  702. int sock_id = -1;
  703. static int http_post(int, char*, int);
  704.  
  705. char* msg = "Shut up Christy";
  706. tBoolean send_flag = false;
  707.  
  708. void sendTextMessage() {
  709.     http_post(sock_id, textMessage, textMessageIndex);
  710.  
  711.     send_flag = false;
  712.  
  713.     textMessageIndex = 0;
  714.     clearTop();
  715. }
  716.  
  717. int deltas[50];
  718. int deltaIndex = 0;
  719.  
  720.  
  721. void IRHandler() {
  722.     unsigned long pin = GPIOPinRead(GPIOA0_BASE, 0x8);
  723.     if (pin == 0) {
  724.         // Falling edge
  725.         if (ignore) {
  726.             // Reset
  727.             pulse = 0;
  728.             ignore = false;
  729.             GPIOIntClear(GPIOA0_BASE, 0x8);
  730.             return;
  731.         }
  732.  
  733.         unsigned long current = MAP_TimerValueGet(TIMERA2_BASE, TIMER_A);
  734.         unsigned long delta = current < previous ? previous - current : 0xFFFFFF + previous - current;
  735.  
  736.         decodeArray[pulse] = delta > TIME_CUTOFF ? 1 : 0;
  737.         deltas[pulse] = delta;
  738.         pulse = pulse + 1;
  739.         if (pulse % 33 == 0) {
  740.             pulse = 0;
  741.             currentChar = decodeIRSignal(decodeArray);
  742.             if (currentChar == '.') {
  743.                 textMessageIndex--;
  744.                 goBackTop();
  745.                 writeCharToDisplay(' ');
  746.                 goBackTop();
  747.                 prevChar = '0';
  748.                 letterOffset = 0;
  749.             } else if (currentChar == '-') {
  750.                 return;
  751.             } else if (currentChar == '9') { // Press enter
  752.                 send_flag = true;
  753.                 prevChar = '0';
  754.                 letterOffset = 0;
  755.             } else if (currentChar == prevChar) { // duplicate character
  756.                 writeCharToDisplay(8);
  757.                 letterOffset += 1;
  758.                 letterOffset = letterOffset % (currentChar == 'w' || currentChar == 'p' ? 4 : 3);
  759.                 writeCharToDisplay(currentChar + letterOffset);
  760.             } else if (prevChar == '0') { // startup / fresh
  761.                 prevChar = currentChar;
  762.                 letterOffset = 0;
  763.                 writeCharToDisplay(currentChar + letterOffset);
  764.             } else { // different char, same time period
  765.                 appendToTextMessage(prevChar + letterOffset);
  766.                 prevChar = currentChar;
  767.                 letterOffset = 0;
  768.                 writeCharToDisplay(prevChar);
  769.             }
  770.  
  771.             Timer_IF_Start(TIMERA1_BASE, TIMER_A, 1000);
  772.         }
  773.     } else {
  774.         // Rising Edge
  775.         ignore = false;
  776.         previous = MAP_TimerValueGet(TIMERA2_BASE, TIMER_A);
  777.         Timer_IF_Start(TIMERA0_BASE, TIMER_A, 30);
  778.     }
  779.  
  780.     GPIOIntClear(GPIOA0_BASE, 0x8);
  781. }
  782.  
  783. void SPIInit() {
  784.      MAP_PRCMPeripheralReset(PRCM_GSPI);
  785.      MAP_SPIReset(GSPI_BASE);
  786.      MAP_SPIFIFOEnable(GSPI_BASE, SPI_TX_FIFO | SPI_RX_FIFO);
  787.      MAP_SPIConfigSetExpClk(GSPI_BASE,
  788.                          MAP_PRCMPeripheralClockGet(PRCM_GSPI),
  789.                          SPI_IF_BIT_RATE,
  790.                          SPI_MODE_MASTER,
  791.                          SPI_SUB_MODE_0,
  792.                          (SPI_SW_CTRL_CS |
  793.                          SPI_4PIN_MODE |
  794.                          SPI_TURBO_OFF |
  795.                          SPI_CS_ACTIVELOW |
  796.                          SPI_WL_8));
  797.      MAP_SPIEnable(GSPI_BASE);
  798. }
  799.  
  800. void GPIOInterruptConfig() {
  801.     IntPrioritySet(GPIO_BOTH_EDGES, INT_PRIORITY_LVL_0);
  802.     GPIOIntTypeSet(GPIOA0_BASE, 0x8, GPIO_BOTH_EDGES);
  803.     GPIOIntRegister(GPIOA0_BASE, IRHandler);
  804.     GPIOIntClear(GPIOA0_BASE, 0x8);
  805.     GPIOIntEnable(GPIOA0_BASE, 0x8);
  806. }
  807.  
  808. void IgnoreTimerHandler() {
  809.     ignore = true; // Ignore next falling edge
  810.     Timer_IF_InterruptClear(TIMERA0_BASE);
  811. }
  812.  
  813. void CharacterTimerHandler() {
  814.     if (currentChar != '9' && currentChar != '.') {
  815.         appendToTextMessage(currentChar + letterOffset);
  816.     }
  817.     prevChar = '0';
  818.     letterOffset = 0;
  819.     Timer_IF_InterruptClear(TIMERA1_BASE);
  820. }
  821.  
  822. void TimerConfig() {
  823.     Timer_IF_Init(PRCM_TIMERA0, TIMERA0_BASE, TIMER_CFG_ONE_SHOT, TIMER_A, 0);
  824.     Timer_IF_IntSetup(TIMERA0_BASE, TIMER_A, IgnoreTimerHandler);
  825.  
  826.     Timer_IF_Init(PRCM_TIMERA1, TIMERA1_BASE, TIMER_CFG_ONE_SHOT, TIMER_A, 0);
  827.     Timer_IF_IntSetup(TIMERA1_BASE, TIMER_A, CharacterTimerHandler);
  828.  
  829.     Timer_IF_Init(PRCM_TIMERA2, TIMERA2_BASE, TIMER_CFG_PERIODIC, TIMER_A, 0);
  830.     Timer_IF_Start(TIMERA2_BASE, TIMER_A, 0xaaaaaa);
  831. }
  832.  
  833.  
  834.  
  835. //*****************************************************************************
  836. //                 GLOBAL VARIABLES -- Start
  837. //*****************************************************************************
  838. volatile unsigned long  g_ulStatus = 0;//SimpleLink Status
  839. unsigned long  g_ulPingPacketsRecv = 0; //Number of Ping Packets received
  840. unsigned long  g_ulGatewayIP = 0; //Network Gateway IP address
  841. unsigned char  g_ucConnectionSSID[SSID_LEN_MAX+1]; //Connection SSID
  842. unsigned char  g_ucConnectionBSSID[BSSID_LEN_MAX]; //Connection BSSID
  843. signed char    *g_Host = SERVER_NAME;
  844. SlDateTime g_time;
  845. #if defined(ccs) || defined(gcc)
  846. extern void (* const g_pfnVectors[])(void);
  847. #endif
  848. #if defined(ewarm)
  849. extern uVectorEntry __vector_table;
  850. #endif
  851. //*****************************************************************************
  852. //                 GLOBAL VARIABLES -- End
  853. //*****************************************************************************
  854.  
  855.  
  856. //****************************************************************************
  857. //                      LOCAL FUNCTION PROTOTYPES
  858. //****************************************************************************
  859. static long WlanConnect();
  860. static int set_time();
  861. static void BoardInit(void);
  862. static long InitializeAppVariables();
  863. static int tls_connect();
  864. static int connectToAccessPoint();
  865.  
  866. //*****************************************************************************
  867. // SimpleLink Asynchronous Event Handlers -- Start
  868. //*****************************************************************************
  869.  
  870.  
  871. //*****************************************************************************
  872. //
  873. //! \brief The Function Handles WLAN Events
  874. //!
  875. //! \param[in]  pWlanEvent - Pointer to WLAN Event Info
  876. //!
  877. //! \return None
  878. //!
  879. //*****************************************************************************
  880. void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)
  881. {
  882.     if(!pWlanEvent)
  883.     {
  884.         return;
  885.     }
  886.  
  887.     switch(pWlanEvent->Event)
  888.     {
  889.         case SL_WLAN_CONNECT_EVENT:
  890.         {
  891.             SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  892.  
  893.             //
  894.             // Information about the connected AP (like name, MAC etc) will be
  895.             // available in 'slWlanConnectAsyncResponse_t'.
  896.             // Applications can use it if required
  897.             //
  898.             //  slWlanConnectAsyncResponse_t *pEventData = NULL;
  899.             // pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;
  900.             //
  901.  
  902.             // Copy new connection SSID and BSSID to global parameters
  903.             memcpy(g_ucConnectionSSID,pWlanEvent->EventData.
  904.                    STAandP2PModeWlanConnected.ssid_name,
  905.                    pWlanEvent->EventData.STAandP2PModeWlanConnected.ssid_len);
  906.             memcpy(g_ucConnectionBSSID,
  907.                    pWlanEvent->EventData.STAandP2PModeWlanConnected.bssid,
  908.                    SL_BSSID_LENGTH);
  909.  
  910.             UART_PRINT("[WLAN EVENT] STA Connected to the AP: %s , "
  911.                        "BSSID: %x:%x:%x:%x:%x:%x\n\r",
  912.                        g_ucConnectionSSID,g_ucConnectionBSSID[0],
  913.                        g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  914.                        g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  915.                        g_ucConnectionBSSID[5]);
  916.         }
  917.         break;
  918.  
  919.         case SL_WLAN_DISCONNECT_EVENT:
  920.         {
  921.             slWlanConnectAsyncResponse_t*  pEventData = NULL;
  922.  
  923.             CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
  924.             CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  925.  
  926.             pEventData = &pWlanEvent->EventData.STAandP2PModeDisconnected;
  927.  
  928.             // If the user has initiated 'Disconnect' request,
  929.             //'reason_code' is SL_USER_INITIATED_DISCONNECTION
  930.             if(SL_USER_INITIATED_DISCONNECTION == pEventData->reason_code)
  931.             {
  932.                 UART_PRINT("[WLAN EVENT]Device disconnected from the AP: %s,"
  933.                     "BSSID: %x:%x:%x:%x:%x:%x on application's request \n\r",
  934.                            g_ucConnectionSSID,g_ucConnectionBSSID[0],
  935.                            g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  936.                            g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  937.                            g_ucConnectionBSSID[5]);
  938.             }
  939.             else
  940.             {
  941.                 UART_PRINT("[WLAN ERROR]Device disconnected from the AP AP: %s, "
  942.                            "BSSID: %x:%x:%x:%x:%x:%x on an ERROR..!! \n\r",
  943.                            g_ucConnectionSSID,g_ucConnectionBSSID[0],
  944.                            g_ucConnectionBSSID[1],g_ucConnectionBSSID[2],
  945.                            g_ucConnectionBSSID[3],g_ucConnectionBSSID[4],
  946.                            g_ucConnectionBSSID[5]);
  947.             }
  948.             memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));
  949.             memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));
  950.         }
  951.         break;
  952.  
  953.         default:
  954.         {
  955.             UART_PRINT("[WLAN EVENT] Unexpected event [0x%x]\n\r",
  956.                        pWlanEvent->Event);
  957.         }
  958.         break;
  959.     }
  960. }
  961.  
  962. //*****************************************************************************
  963. //
  964. //! \brief This function handles network events such as IP acquisition, IP
  965. //!           leased, IP released etc.
  966. //!
  967. //! \param[in]  pNetAppEvent - Pointer to NetApp Event Info
  968. //!
  969. //! \return None
  970. //!
  971. //*****************************************************************************
  972. void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
  973. {
  974.     if(!pNetAppEvent)
  975.     {
  976.         return;
  977.     }
  978.  
  979.     switch(pNetAppEvent->Event)
  980.     {
  981.         case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
  982.         {
  983.             SlIpV4AcquiredAsync_t *pEventData = NULL;
  984.  
  985.             SET_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
  986.  
  987.             //Ip Acquired Event Data
  988.             pEventData = &pNetAppEvent->EventData.ipAcquiredV4;
  989.  
  990.             //Gateway IP address
  991.             g_ulGatewayIP = pEventData->gateway;
  992.  
  993.             UART_PRINT("[NETAPP EVENT] IP Acquired: IP=%d.%d.%d.%d , "
  994.                        "Gateway=%d.%d.%d.%d\n\r",
  995.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,3),
  996.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,2),
  997.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,1),
  998.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.ip,0),
  999.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,3),
  1000.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,2),
  1001.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,1),
  1002.             SL_IPV4_BYTE(pNetAppEvent->EventData.ipAcquiredV4.gateway,0));
  1003.         }
  1004.         break;
  1005.  
  1006.         default:
  1007.         {
  1008.             UART_PRINT("[NETAPP EVENT] Unexpected event [0x%x] \n\r",
  1009.                        pNetAppEvent->Event);
  1010.         }
  1011.         break;
  1012.     }
  1013. }
  1014.  
  1015.  
  1016. //*****************************************************************************
  1017. //
  1018. //! \brief This function handles HTTP server events
  1019. //!
  1020. //! \param[in]  pServerEvent - Contains the relevant event information
  1021. //! \param[in]    pServerResponse - Should be filled by the user with the
  1022. //!                                      relevant response information
  1023. //!
  1024. //! \return None
  1025. //!
  1026. //****************************************************************************
  1027. void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent,
  1028.                                   SlHttpServerResponse_t *pHttpResponse)
  1029. {
  1030.     // Unused in this application
  1031. }
  1032.  
  1033. //*****************************************************************************
  1034. //
  1035. //! \brief This function handles General Events
  1036. //!
  1037. //! \param[in]     pDevEvent - Pointer to General Event Info
  1038. //!
  1039. //! \return None
  1040. //!
  1041. //*****************************************************************************
  1042. void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
  1043. {
  1044.     if(!pDevEvent)
  1045.     {
  1046.         return;
  1047.     }
  1048.  
  1049.     //
  1050.     // Most of the general errors are not FATAL are are to be handled
  1051.     // appropriately by the application
  1052.     //
  1053.     UART_PRINT("[GENERAL EVENT] - ID=[%d] Sender=[%d]\n\n",
  1054.                pDevEvent->EventData.deviceEvent.status,
  1055.                pDevEvent->EventData.deviceEvent.sender);
  1056. }
  1057.  
  1058.  
  1059. //*****************************************************************************
  1060. //
  1061. //! This function handles socket events indication
  1062. //!
  1063. //! \param[in]      pSock - Pointer to Socket Event Info
  1064. //!
  1065. //! \return None
  1066. //!
  1067. //*****************************************************************************
  1068. void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
  1069. {
  1070.     if(!pSock)
  1071.     {
  1072.         return;
  1073.     }
  1074.  
  1075.     switch( pSock->Event )
  1076.     {
  1077.         case SL_SOCKET_TX_FAILED_EVENT:
  1078.             switch( pSock->socketAsyncEvent.SockTxFailData.status)
  1079.             {
  1080.                 case SL_ECLOSE:
  1081.                     UART_PRINT("[SOCK ERROR] - close socket (%d) operation "
  1082.                                 "failed to transmit all queued packets\n\n",
  1083.                                     pSock->socketAsyncEvent.SockTxFailData.sd);
  1084.                     break;
  1085.                 default:
  1086.                     UART_PRINT("[SOCK ERROR] - TX FAILED  :  socket %d , reason "
  1087.                                 "(%d) \n\n",
  1088.                                 pSock->socketAsyncEvent.SockTxFailData.sd, pSock->socketAsyncEvent.SockTxFailData.status);
  1089.                   break;
  1090.             }
  1091.             break;
  1092.  
  1093.         default:
  1094.             UART_PRINT("[SOCK EVENT] - Unexpected Event [%x0x]\n\n",pSock->Event);
  1095.           break;
  1096.     }
  1097.  
  1098. }
  1099.  
  1100.  
  1101. //*****************************************************************************
  1102. // SimpleLink Asynchronous Event Handlers -- End
  1103. //*****************************************************************************
  1104.  
  1105.  
  1106. //*****************************************************************************
  1107. //
  1108. //! \brief This function initializes the application variables
  1109. //!
  1110. //! \param    0 on success else error code
  1111. //!
  1112. //! \return None
  1113. //!
  1114. //*****************************************************************************
  1115. static long InitializeAppVariables()
  1116. {
  1117.     g_ulStatus = 0;
  1118.     g_ulGatewayIP = 0;
  1119.     g_Host = SERVER_NAME;
  1120.     memset(g_ucConnectionSSID,0,sizeof(g_ucConnectionSSID));
  1121.     memset(g_ucConnectionBSSID,0,sizeof(g_ucConnectionBSSID));
  1122.     return SUCCESS;
  1123. }
  1124.  
  1125.  
  1126. //*****************************************************************************
  1127. //! \brief This function puts the device in its default state. It:
  1128. //!           - Set the mode to STATION
  1129. //!           - Configures connection policy to Auto and AutoSmartConfig
  1130. //!           - Deletes all the stored profiles
  1131. //!           - Enables DHCP
  1132. //!           - Disables Scan policy
  1133. //!           - Sets Tx power to maximum
  1134. //!           - Sets power policy to normal
  1135. //!           - Unregister mDNS services
  1136. //!           - Remove all filters
  1137. //!
  1138. //! \param   none
  1139. //! \return  On success, zero is returned. On error, negative is returned
  1140. //*****************************************************************************
  1141. static long ConfigureSimpleLinkToDefaultState()
  1142. {
  1143.     SlVersionFull   ver = {0};
  1144.     _WlanRxFilterOperationCommandBuff_t  RxFilterIdMask = {0};
  1145.  
  1146.     unsigned char ucVal = 1;
  1147.     unsigned char ucConfigOpt = 0;
  1148.     unsigned char ucConfigLen = 0;
  1149.     unsigned char ucPower = 0;
  1150.  
  1151.     long lRetVal = -1;
  1152.     long lMode = -1;
  1153.  
  1154.     lMode = sl_Start(0, 0, 0);
  1155.     ASSERT_ON_ERROR(lMode);
  1156.  
  1157.     // If the device is not in station-mode, try configuring it in station-mode
  1158.     if (ROLE_STA != lMode)
  1159.     {
  1160.         if (ROLE_AP == lMode)
  1161.         {
  1162.             // If the device is in AP mode, we need to wait for this event
  1163.             // before doing anything
  1164.             while(!IS_IP_ACQUIRED(g_ulStatus))
  1165.             {
  1166. #ifndef SL_PLATFORM_MULTI_THREADED
  1167.               _SlNonOsMainLoopTask();
  1168. #endif
  1169.             }
  1170.         }
  1171.  
  1172.         // Switch to STA role and restart
  1173.         lRetVal = sl_WlanSetMode(ROLE_STA);
  1174.         ASSERT_ON_ERROR(lRetVal);
  1175.  
  1176.         lRetVal = sl_Stop(0xFF);
  1177.         ASSERT_ON_ERROR(lRetVal);
  1178.  
  1179.         lRetVal = sl_Start(0, 0, 0);
  1180.         ASSERT_ON_ERROR(lRetVal);
  1181.  
  1182.         // Check if the device is in station again
  1183.         if (ROLE_STA != lRetVal)
  1184.         {
  1185.             // We don't want to proceed if the device is not coming up in STA-mode
  1186.             return DEVICE_NOT_IN_STATION_MODE;
  1187.         }
  1188.     }
  1189.    
  1190.     // Get the device's version-information
  1191.     ucConfigOpt = SL_DEVICE_GENERAL_VERSION;
  1192.     ucConfigLen = sizeof(ver);
  1193.     lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt,
  1194.                                 &ucConfigLen, (unsigned char *)(&ver));
  1195.     ASSERT_ON_ERROR(lRetVal);
  1196.    
  1197.     UART_PRINT("Host Driver Version: %s\n\r",SL_DRIVER_VERSION);
  1198.     UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r",
  1199.     ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3],
  1200.     ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1],
  1201.     ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3],
  1202.     ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1],
  1203.     ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]);
  1204.  
  1205.     // Set connection policy to Auto + SmartConfig
  1206.     //      (Device's default connection policy)
  1207.     lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
  1208.                                 SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
  1209.     ASSERT_ON_ERROR(lRetVal);
  1210.  
  1211.     // Remove all profiles
  1212.     lRetVal = sl_WlanProfileDel(0xFF);
  1213.     ASSERT_ON_ERROR(lRetVal);
  1214.  
  1215.    
  1216.  
  1217.     //
  1218.     // Device in station-mode. Disconnect previous connection if any
  1219.     // The function returns 0 if 'Disconnected done', negative number if already
  1220.     // disconnected Wait for 'disconnection' event if 0 is returned, Ignore
  1221.     // other return-codes
  1222.     //
  1223.     lRetVal = sl_WlanDisconnect();
  1224.     if(0 == lRetVal)
  1225.     {
  1226.         // Wait
  1227.         while(IS_CONNECTED(g_ulStatus))
  1228.         {
  1229. #ifndef SL_PLATFORM_MULTI_THREADED
  1230.               _SlNonOsMainLoopTask();
  1231. #endif
  1232.         }
  1233.     }
  1234.  
  1235.     // Enable DHCP client
  1236.     lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal);
  1237.     ASSERT_ON_ERROR(lRetVal);
  1238.  
  1239.     // Disable scan
  1240.     ucConfigOpt = SL_SCAN_POLICY(0);
  1241.     lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0);
  1242.     ASSERT_ON_ERROR(lRetVal);
  1243.  
  1244.     // Set Tx power level for station mode
  1245.     // Number between 0-15, as dB offset from max power - 0 will set max power
  1246.     ucPower = 0;
  1247.     lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
  1248.             WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower);
  1249.     ASSERT_ON_ERROR(lRetVal);
  1250.  
  1251.     // Set PM policy to normal
  1252.     lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0);
  1253.     ASSERT_ON_ERROR(lRetVal);
  1254.  
  1255.     // Unregister mDNS services
  1256.     lRetVal = sl_NetAppMDNSUnRegisterService(0, 0);
  1257.     ASSERT_ON_ERROR(lRetVal);
  1258.  
  1259.     // Remove  all 64 filters (8*8)
  1260.     memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
  1261.     lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask,
  1262.                        sizeof(_WlanRxFilterOperationCommandBuff_t));
  1263.     ASSERT_ON_ERROR(lRetVal);
  1264.  
  1265.     lRetVal = sl_Stop(SL_STOP_TIMEOUT);
  1266.     ASSERT_ON_ERROR(lRetVal);
  1267.  
  1268.     InitializeAppVariables();
  1269.    
  1270.     return lRetVal; // Success
  1271. }
  1272.  
  1273.  
  1274. //*****************************************************************************
  1275. //
  1276. //! Board Initialization & Configuration
  1277. //!
  1278. //! \param  None
  1279. //!
  1280. //! \return None
  1281. //
  1282. //*****************************************************************************
  1283. static void BoardInit(void)
  1284. {
  1285. /* In case of TI-RTOS vector table is initialize by OS itself */
  1286. #ifndef USE_TIRTOS
  1287.   //
  1288.   // Set vector table base
  1289.   //
  1290. #if defined(ccs)
  1291.     MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
  1292. #endif
  1293. #if defined(ewarm)
  1294.     MAP_IntVTableBaseSet((unsigned long)&__vector_table);
  1295. #endif
  1296. #endif
  1297.     //
  1298.     // Enable Processor
  1299.     //
  1300.     MAP_IntMasterEnable();
  1301.     MAP_IntEnable(FAULT_SYSTICK);
  1302.  
  1303.     PRCMCC3200MCUInit();
  1304. }
  1305.  
  1306.  
  1307. //****************************************************************************
  1308. //
  1309. //! \brief Connecting to a WLAN Accesspoint
  1310. //!
  1311. //!  This function connects to the required AP (SSID_NAME) with Security
  1312. //!  parameters specified in te form of macros at the top of this file
  1313. //!
  1314. //! \param  None
  1315. //!
  1316. //! \return  0 on success else error code
  1317. //!
  1318. //! \warning    If the WLAN connection fails or we don't aquire an IP
  1319. //!            address, It will be stuck in this function forever.
  1320. //
  1321. //****************************************************************************
  1322. static long WlanConnect()
  1323. {
  1324.     SlSecParams_t secParams = {0};
  1325.     long lRetVal = 0;
  1326.  
  1327.     secParams.Key = SECURITY_KEY;
  1328.     secParams.KeyLen = strlen(SECURITY_KEY);
  1329.     secParams.Type = SECURITY_TYPE;
  1330.  
  1331.     lRetVal = sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
  1332.     ASSERT_ON_ERROR(lRetVal);
  1333.  
  1334.     // Wait for WLAN Event
  1335.     while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
  1336.     {
  1337.         // Toggle LEDs to Indicate Connection Progress
  1338.         _SlNonOsMainLoopTask();
  1339.         GPIO_IF_LedOff(MCU_IP_ALLOC_IND);
  1340.         MAP_UtilsDelay(800000);
  1341.         _SlNonOsMainLoopTask();
  1342.         GPIO_IF_LedOn(MCU_IP_ALLOC_IND);
  1343.         MAP_UtilsDelay(800000);
  1344.     }
  1345.  
  1346.     return SUCCESS;
  1347.  
  1348. }
  1349.  
  1350. //*****************************************************************************
  1351. //
  1352. //! This function updates the date and time of CC3200.
  1353. //!
  1354. //! \param None
  1355. //!
  1356. //! \return
  1357. //!     0 for success, negative otherwise
  1358. //!
  1359. //*****************************************************************************
  1360.  
  1361. static int set_time()
  1362. {
  1363.     long retVal;
  1364.  
  1365.     g_time.tm_day = DATE;
  1366.     g_time.tm_mon = MONTH;
  1367.     g_time.tm_year = YEAR;
  1368.     g_time.tm_sec = HOUR;
  1369.     g_time.tm_hour = MINUTE;
  1370.     g_time.tm_min = SECOND;
  1371.  
  1372.     retVal = sl_DevSet(SL_DEVICE_GENERAL_CONFIGURATION,
  1373.                           SL_DEVICE_GENERAL_CONFIGURATION_DATE_TIME,
  1374.                           sizeof(SlDateTime),(unsigned char *)(&g_time));
  1375.  
  1376.     ASSERT_ON_ERROR(retVal);
  1377.     return SUCCESS;
  1378. }
  1379.  
  1380. ////*****************************************************************************
  1381. ////
  1382. ////! This function demonstrates how certificate can be used with SSL.
  1383. ////! The procedure includes the following steps:
  1384. ////! 1) connect to an open AP
  1385. ////! 2) get the server name via a DNS request
  1386. ////! 3) define all socket options and point to the CA certificate
  1387. ////! 4) connect to the server via TCP
  1388. ////!
  1389. ////! \param None
  1390. ////!
  1391. ////! \return  0 on success else error code
  1392. ////! \return  LED1 is turned solid in case of success
  1393. ////!    LED2 is turned solid in case of failure
  1394. ////!
  1395. ////*****************************************************************************
  1396. static int tls_connect()
  1397. {
  1398.     SlSockAddrIn_t    Addr;
  1399.     int    iAddrSize;
  1400.     unsigned char    ucMethod = SL_SO_SEC_METHOD_TLSV1_2;
  1401.     unsigned int uiIP,uiCipher = SL_SEC_MASK_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA;
  1402.     long lRetVal = -1;
  1403.     int iSockID;
  1404.  
  1405.     lRetVal = sl_NetAppDnsGetHostByName(g_Host, strlen((const char *)g_Host),
  1406.                                     (unsigned long*)&uiIP, SL_AF_INET);
  1407.  
  1408.     if(lRetVal < 0)
  1409.     {
  1410.         UART_PRINT("Device couldn't retrive the host name \n\r");
  1411.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1412.         return lRetVal;
  1413.     }
  1414.  
  1415.     Addr.sin_family = SL_AF_INET;
  1416.     Addr.sin_port = sl_Htons(GOOGLE_DST_PORT);
  1417.     Addr.sin_addr.s_addr = sl_Htonl(uiIP);
  1418.     iAddrSize = sizeof(SlSockAddrIn_t);
  1419.     //
  1420.     // opens a secure socket
  1421.     //
  1422.     iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET);
  1423.     if( iSockID < 0 )
  1424.     {
  1425.         UART_PRINT("Device unable to create secure socket \n\r");
  1426.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1427.         return lRetVal;
  1428.     }
  1429.  
  1430.     //
  1431.     // configure the socket as TLS1.2
  1432.     //
  1433.     lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECMETHOD, &ucMethod,\
  1434.                                sizeof(ucMethod));
  1435.     if(lRetVal < 0)
  1436.     {
  1437.         UART_PRINT("Device couldn't set socket options \n\r");
  1438.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1439.         return lRetVal;
  1440.     }
  1441.     //
  1442.     //configure the socket as ECDHE RSA WITH AES256 CBC SHA
  1443.     //
  1444.     lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &uiCipher,\
  1445.                            sizeof(uiCipher));
  1446.     if(lRetVal < 0)
  1447.     {
  1448.         UART_PRINT("Device couldn't set socket options \n\r");
  1449.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1450.         return lRetVal;
  1451.     }
  1452.  
  1453.     //
  1454.     //configure the socket with CA certificate - for server verification
  1455.     //
  1456.     lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \
  1457.                            SL_SO_SECURE_FILES_CA_FILE_NAME, \
  1458.                            SL_SSL_CA_CERT, \
  1459.                            strlen(SL_SSL_CA_CERT));
  1460.  
  1461.     if(lRetVal < 0)
  1462.     {
  1463.         UART_PRINT("Device couldn't set socket options \n\r");
  1464.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1465.         return lRetVal;
  1466.     }
  1467.  
  1468.     //configure the socket with Client Certificate - for server verification
  1469.     //
  1470.     lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \
  1471.                 SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, \
  1472.                                     SL_SSL_CLIENT, \
  1473.                            strlen(SL_SSL_CLIENT));
  1474.  
  1475.     if(lRetVal < 0)
  1476.     {
  1477.         UART_PRINT("Device couldn't set socket options \n\r");
  1478.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1479.         return lRetVal;
  1480.     }
  1481.  
  1482.     //configure the socket with Private Key - for server verification
  1483.     //
  1484.     lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \
  1485.             SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, \
  1486.             SL_SSL_PRIVATE, \
  1487.                            strlen(SL_SSL_PRIVATE));
  1488.  
  1489.     if(lRetVal < 0)
  1490.     {
  1491.         UART_PRINT("Device couldn't set socket options \n\r");
  1492.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1493.         return lRetVal;
  1494.     }
  1495.  
  1496.  
  1497.     /* connect to the peer device - Google server */
  1498.     lRetVal = sl_Connect(iSockID, ( SlSockAddr_t *)&Addr, iAddrSize);
  1499.  
  1500.     if(lRetVal < 0)
  1501.     {
  1502.         UART_PRINT("Device couldn't connect to AWS server \n\r");
  1503.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1504.         return lRetVal;
  1505.     }
  1506.     else{
  1507.         UART_PRINT("Device has connected to the website:");
  1508.         UART_PRINT(SERVER_NAME);
  1509.         UART_PRINT("\n\r");
  1510.     }
  1511.  
  1512.     GPIO_IF_LedOff(MCU_RED_LED_GPIO);
  1513.     GPIO_IF_LedOn(MCU_GREEN_LED_GPIO);
  1514.     return iSockID;
  1515. }
  1516.  
  1517. int connectToAccessPoint(){
  1518.     long lRetVal = -1;
  1519.     GPIO_IF_LedConfigure(LED1|LED3);
  1520.  
  1521.     GPIO_IF_LedOff(MCU_RED_LED_GPIO);
  1522.     GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);
  1523.  
  1524.     lRetVal = InitializeAppVariables();
  1525.     ASSERT_ON_ERROR(lRetVal);
  1526.  
  1527.     //
  1528.     // Following function configure the device to default state by cleaning
  1529.     // the persistent settings stored in NVMEM (viz. connection profiles &
  1530.     // policies, power policy etc)
  1531.     //
  1532.     // Applications may choose to skip this step if the developer is sure
  1533.     // that the device is in its default state at start of applicaton
  1534.     //
  1535.     // Note that all profiles and persistent settings that were done on the
  1536.     // device will be lost
  1537.     //
  1538.     lRetVal = ConfigureSimpleLinkToDefaultState();
  1539.     if(lRetVal < 0)
  1540.     {
  1541.       if (DEVICE_NOT_IN_STATION_MODE == lRetVal)
  1542.           UART_PRINT("Failed to configure the device in its default state \n\r");
  1543.  
  1544.       return lRetVal;
  1545.     }
  1546.  
  1547.     UART_PRINT("Device is configured in default state \n\r");
  1548.  
  1549.     CLR_STATUS_BIT_ALL(g_ulStatus);
  1550.  
  1551.     ///
  1552.     // Assumption is that the device is configured in station mode already
  1553.     // and it is in its default state
  1554.     //
  1555.     lRetVal = sl_Start(0, 0, 0);
  1556.     if (lRetVal < 0 || ROLE_STA != lRetVal)
  1557.     {
  1558.         UART_PRINT("Failed to start the device \n\r");
  1559.         return lRetVal;
  1560.     }
  1561.  
  1562.     UART_PRINT("Device started as STATION \n\r");
  1563.  
  1564.     //
  1565.     //Connecting to WLAN AP
  1566.     //
  1567.     lRetVal = WlanConnect();
  1568.     if(lRetVal < 0)
  1569.     {
  1570.         UART_PRINT("Failed to establish connection w/ an AP \n\r");
  1571.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1572.         return lRetVal;
  1573.     }
  1574.  
  1575.     UART_PRINT("Connection established w/ AP and IP is aquired \n\r");
  1576.     return 0;
  1577. }
  1578.  
  1579. //*****************************************************************************
  1580. //
  1581. //! Main
  1582. //!
  1583. //! \param  none
  1584. //!
  1585. //! \return None
  1586. //!
  1587. //*****************************************************************************
  1588.  
  1589. void main()
  1590. {
  1591.     long lRetVal = -1;
  1592.     //
  1593.     // Initialize board configuration
  1594.     //
  1595.     BoardInit();
  1596.  
  1597.     PinMuxConfig();
  1598.  
  1599.     printf("SPI Init...\n");
  1600.     SPIInit();
  1601.     printf("Adafruit_Init...\n");
  1602.     Adafruit_Init();
  1603.     printf("GPIO Config...\n");
  1604.     GPIOInterruptConfig();
  1605.     printf("Timer config...\n");
  1606.     TimerConfig();
  1607.  
  1608.     clearDisplay();
  1609.     InitTerm();
  1610.  
  1611.     lRetVal = connectToAccessPoint();
  1612.     lRetVal = set_time();
  1613.     if (lRetVal < 0) {
  1614.         LOOP_FOREVER();
  1615.     }
  1616.  
  1617.     lRetVal = tls_connect();
  1618.     if (lRetVal < 0) {
  1619.         ERR_PRINT(lRetVal);
  1620.     }
  1621.  
  1622.     sock_id = lRetVal;
  1623.  
  1624.     while(1) {
  1625.         if (send_flag) {
  1626.             sendTextMessage();
  1627.         }
  1628.     }
  1629.  
  1630.     sl_Stop(SL_STOP_TIMEOUT);
  1631.     LOOP_FOREVER();
  1632. }
  1633. //*****************************************************************************
  1634. //
  1635. // Close the Doxygen group.
  1636. //! @}
  1637. //
  1638. //*****************************************************************************
  1639.  
  1640. static int http_post(int iTLSSockID, char *message, int len){
  1641.     char acSendBuff[512];
  1642.     char acRecvbuff[1460];
  1643.     char* pcBufHeaders;
  1644.     int lRetVal = 0;
  1645.  
  1646.     pcBufHeaders = acSendBuff;
  1647.     strcpy(pcBufHeaders, POSTHEADER);
  1648.     pcBufHeaders += strlen(POSTHEADER);
  1649.     strcpy(pcBufHeaders, " HTTP/1.1\r\n");
  1650.     pcBufHeaders += strlen(" HTTP/1.1\r\n");
  1651.     strcpy(pcBufHeaders, HOSTHEADER);
  1652.     pcBufHeaders += strlen(HOSTHEADER);
  1653.     strcpy(pcBufHeaders, CHEADER);
  1654.     pcBufHeaders += strlen(CHEADER);
  1655.     strcpy(pcBufHeaders, CTHEADER);
  1656.     pcBufHeaders += strlen(CTHEADER);
  1657.     strcpy(pcBufHeaders, CLHEADER1);
  1658.     pcBufHeaders += strlen(CLHEADER1);
  1659.  
  1660.     char buff[10];
  1661.     sprintf(buff, "%d", len + strlen(DATA1) + strlen(DATA2) - 2);
  1662.  
  1663.     strcpy(pcBufHeaders, buff);
  1664.     pcBufHeaders += strlen(buff);
  1665.  
  1666.     strcpy(pcBufHeaders, CLHEADER2);
  1667.     pcBufHeaders += strlen(CLHEADER2);
  1668.  
  1669.     strcpy(pcBufHeaders, DATA1);
  1670.     pcBufHeaders += strlen(DATA1);
  1671.  
  1672.     strcpy(pcBufHeaders, message);
  1673.     pcBufHeaders += strlen(message);
  1674.  
  1675.     strcpy(pcBufHeaders, DATA2);
  1676.     pcBufHeaders += strlen(DATA2);
  1677.  
  1678.     //
  1679.     // Send the packet to the server */
  1680.     //
  1681.     lRetVal = sl_Send(iTLSSockID, acSendBuff, strlen(acSendBuff), 0);
  1682.     if(lRetVal < 0)
  1683.     {
  1684.         UART_PRINT("POST failed. Error Number: %i\n\r",lRetVal);
  1685.         sl_Close(iTLSSockID);
  1686.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1687.         return lRetVal;
  1688.     }
  1689.     lRetVal = sl_Recv(iTLSSockID, &acRecvbuff[0], sizeof(acRecvbuff), 0);
  1690.     if(lRetVal < 0)
  1691.     {
  1692.         UART_PRINT("Received failed. Error Number: %i\n\r",lRetVal);
  1693.         //sl_Close(iSSLSockID);
  1694.         GPIO_IF_LedOn(MCU_RED_LED_GPIO);
  1695.            return lRetVal;
  1696.     }
  1697.     else
  1698.     {
  1699.         acRecvbuff[lRetVal+1] = '\0';
  1700.         UART_PRINT(acRecvbuff);
  1701.     }
  1702.  
  1703.     return 0;
  1704. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement