Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed
doTCPIP.cpp@0:ce33c63eefe5, 2013-09-08 (annotated)
- Committer:
- Kojto
- Date:
- Sun Sep 08 18:31:33 2013 +0000
- Revision:
- 0:ce33c63eefe5
initial version of new simple socket
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Kojto | 0:ce33c63eefe5 | 1 | #include "doTCPIP.h" |
| Kojto | 0:ce33c63eefe5 | 2 | #include "cc3000.h" |
| Kojto | 0:ce33c63eefe5 | 3 | #include "main.h" |
| Kojto | 0:ce33c63eefe5 | 4 | |
| Kojto | 0:ce33c63eefe5 | 5 | using namespace mbed_cc3000; |
| Kojto | 0:ce33c63eefe5 | 6 | |
| Kojto | 0:ce33c63eefe5 | 7 | extern cc3000 wigo; |
| Kojto | 0:ce33c63eefe5 | 8 | |
| Kojto | 0:ce33c63eefe5 | 9 | //volatile uint8_t newData; |
| Kojto | 0:ce33c63eefe5 | 10 | // int32_t server_running; |
| Kojto | 0:ce33c63eefe5 | 11 | //uint8_t ForceFixedSSID; |
| Kojto | 0:ce33c63eefe5 | 12 | //int8_t runSmartConfig; // Flag indicating whether user requested to perform Smart Config |
| Kojto | 0:ce33c63eefe5 | 13 | //volatile uint32_t ulCC3000Connected; |
| Kojto | 0:ce33c63eefe5 | 14 | uint8_t ConnectUsingSmartConfig; |
| Kojto | 0:ce33c63eefe5 | 15 | uint8_t myMAC[8]; |
| Kojto | 0:ce33c63eefe5 | 16 | tUserFS userFS; |
| Kojto | 0:ce33c63eefe5 | 17 | |
| Kojto | 0:ce33c63eefe5 | 18 | int8_t requestBuffer[REQ_BUFFER_SIZE]; |
| Kojto | 0:ce33c63eefe5 | 19 | //int32_t LAN_Connected = 0; |
| Kojto | 0:ce33c63eefe5 | 20 | |
| Kojto | 0:ce33c63eefe5 | 21 | uint8_t SmartConfigProfilestored = 0xff; |
| Kojto | 0:ce33c63eefe5 | 22 | |
| Kojto | 0:ce33c63eefe5 | 23 | #ifndef CC3000_UNENCRYPTED_SMART_CONFIG |
| Kojto | 0:ce33c63eefe5 | 24 | const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; |
| Kojto | 0:ce33c63eefe5 | 25 | #else |
| Kojto | 0:ce33c63eefe5 | 26 | const uint8_t smartconfigkey = NULL; |
| Kojto | 0:ce33c63eefe5 | 27 | #endif |
| Kojto | 0:ce33c63eefe5 | 28 | |
| Kojto | 0:ce33c63eefe5 | 29 | /** \brief Flag indicating whether to print CC3000 Connection info */ |
| Kojto | 0:ce33c63eefe5 | 30 | |
| Kojto | 0:ce33c63eefe5 | 31 | //Device name - used for Smart config in order to stop the Smart phone configuration process |
| Kojto | 0:ce33c63eefe5 | 32 | uint8_t DevServname[] = "CC3000"; |
| Kojto | 0:ce33c63eefe5 | 33 | //volatile uint32_t SendmDNSAdvertisment; |
| Kojto | 0:ce33c63eefe5 | 34 | |
| Kojto | 0:ce33c63eefe5 | 35 | |
| Kojto | 0:ce33c63eefe5 | 36 | /* TODO */ |
| Kojto | 0:ce33c63eefe5 | 37 | void create_tcp_connection(int32_t port) |
| Kojto | 0:ce33c63eefe5 | 38 | { |
| Kojto | 0:ce33c63eefe5 | 39 | const char python_msg[] = "Hello Python\n"; |
| Kojto | 0:ce33c63eefe5 | 40 | int32_t stat; |
| Kojto | 0:ce33c63eefe5 | 41 | int32_t sock; |
| Kojto | 0:ce33c63eefe5 | 42 | //new TCP socket descriptor |
| Kojto | 0:ce33c63eefe5 | 43 | int32_t newsock; |
| Kojto | 0:ce33c63eefe5 | 44 | //destination address |
| Kojto | 0:ce33c63eefe5 | 45 | sockaddr destAddr; |
| Kojto | 0:ce33c63eefe5 | 46 | //local address |
| Kojto | 0:ce33c63eefe5 | 47 | sockaddr LocalAddr; |
| Kojto | 0:ce33c63eefe5 | 48 | socklen_t addrlen; |
| Kojto | 0:ce33c63eefe5 | 49 | memset(&LocalAddr, 0, 8); |
| Kojto | 0:ce33c63eefe5 | 50 | LocalAddr.family = AF_INET; |
| Kojto | 0:ce33c63eefe5 | 51 | LocalAddr.data[0] = (port >> 8) & 0xff; |
| Kojto | 0:ce33c63eefe5 | 52 | LocalAddr.data[1] = port & 0xff; |
| Kojto | 0:ce33c63eefe5 | 53 | memset (&LocalAddr.data[2], 0, 4); |
| Kojto | 0:ce33c63eefe5 | 54 | sock = wigo._socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| Kojto | 0:ce33c63eefe5 | 55 | while(sock == -1) sock = wigo._socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP); |
| Kojto | 0:ce33c63eefe5 | 56 | wigo._socket.bind(sock,&LocalAddr,sizeof(sockaddr)); |
| Kojto | 0:ce33c63eefe5 | 57 | wigo._socket.listen(sock, 1); |
| Kojto | 0:ce33c63eefe5 | 58 | addrlen = sizeof(destAddr); |
| Kojto | 0:ce33c63eefe5 | 59 | while(1) |
| Kojto | 0:ce33c63eefe5 | 60 | { |
| Kojto | 0:ce33c63eefe5 | 61 | newsock = -2; |
| Kojto | 0:ce33c63eefe5 | 62 | printf("Server waiting for connection to Python\n"); |
| Kojto | 0:ce33c63eefe5 | 63 | LED_D2_ON; |
| Kojto | 0:ce33c63eefe5 | 64 | while((newsock == -1) || (newsock == -2)) |
| Kojto | 0:ce33c63eefe5 | 65 | { |
| Kojto | 0:ce33c63eefe5 | 66 | newsock = wigo._socket.accept(sock,&destAddr, &addrlen); |
| Kojto | 0:ce33c63eefe5 | 67 | } |
| Kojto | 0:ce33c63eefe5 | 68 | printf("Connected\n"); |
| Kojto | 0:ce33c63eefe5 | 69 | //receive TCP data |
| Kojto | 0:ce33c63eefe5 | 70 | if(newsock >= 0) |
| Kojto | 0:ce33c63eefe5 | 71 | { |
| Kojto | 0:ce33c63eefe5 | 72 | wigo._socket.recv(newsock, requestBuffer,20,0); |
| Kojto | 0:ce33c63eefe5 | 73 | printf("Input = %s\n", requestBuffer); |
| Kojto | 0:ce33c63eefe5 | 74 | stat = -2; |
| Kojto | 0:ce33c63eefe5 | 75 | stat = wigo._socket.send(newsock, python_msg, strlen(python_msg), 0); |
| Kojto | 0:ce33c63eefe5 | 76 | printf("status= %d\n", stat); |
| Kojto | 0:ce33c63eefe5 | 77 | LED_D2_OFF; |
| Kojto | 0:ce33c63eefe5 | 78 | } else printf("bad socket= %d\n", newsock); |
| Kojto | 0:ce33c63eefe5 | 79 | wigo._socket.closesocket(newsock); |
| Kojto | 0:ce33c63eefe5 | 80 | printf("Done, press any key to repeat\n"); |
| Kojto | 0:ce33c63eefe5 | 81 | getchar(); |
| Kojto | 0:ce33c63eefe5 | 82 | // printf("\x1B[2J"); //VT100 erase screen |
| Kojto | 0:ce33c63eefe5 | 83 | // printf("\x1B[H"); //VT100 home |
| Kojto | 0:ce33c63eefe5 | 84 | } |
| Kojto | 0:ce33c63eefe5 | 85 | } |
| Kojto | 0:ce33c63eefe5 | 86 | |
| Kojto | 0:ce33c63eefe5 | 87 | void start_smart_config() { |
| Kojto | 0:ce33c63eefe5 | 88 | wigo.start_smart_config(smartconfigkey); |
| Kojto | 0:ce33c63eefe5 | 89 | } |
| Kojto | 0:ce33c63eefe5 | 90 | |
| Kojto | 0:ce33c63eefe5 | 91 | void init_tcp(void) |
| Kojto | 0:ce33c63eefe5 | 92 | { |
| Kojto | 0:ce33c63eefe5 | 93 | int t; |
| Kojto | 0:ce33c63eefe5 | 94 | tNetappIpconfigRetArgs ipinfo2; |
| Kojto | 0:ce33c63eefe5 | 95 | |
| Kojto | 0:ce33c63eefe5 | 96 | check_dhcp(); |
| Kojto | 0:ce33c63eefe5 | 97 | //while(checkWiFiConnected() == false); |
| Kojto | 0:ce33c63eefe5 | 98 | // If connectivity is good, run the primary functionality |
| Kojto | 0:ce33c63eefe5 | 99 | // while(1) |
| Kojto | 0:ce33c63eefe5 | 100 | // { |
| Kojto | 0:ce33c63eefe5 | 101 | // if (checkWiFiConnected()) break; |
| Kojto | 0:ce33c63eefe5 | 102 | // wait(1); |
| Kojto | 0:ce33c63eefe5 | 103 | // } |
| Kojto | 0:ce33c63eefe5 | 104 | // printf("Connected\n"); |
| Kojto | 0:ce33c63eefe5 | 105 | // If we're not blocked by accept or others, obtain the latest status |
| Kojto | 0:ce33c63eefe5 | 106 | wigo.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure |
| Kojto | 0:ce33c63eefe5 | 107 | printf("\n*** Wi-Go board DHCP assigned IP Address = %d.%d.%d.%d\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]); |
| Kojto | 0:ce33c63eefe5 | 108 | LED_D3_ON; |
| Kojto | 0:ce33c63eefe5 | 109 | |
| Kojto | 0:ce33c63eefe5 | 110 | t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname)); |
| Kojto | 0:ce33c63eefe5 | 111 | printf("mDNS Status= %x\n", t); |
| Kojto | 0:ce33c63eefe5 | 112 | } |
| Kojto | 0:ce33c63eefe5 | 113 | |
| Kojto | 0:ce33c63eefe5 | 114 | void run_tcp_server(void) |
| Kojto | 0:ce33c63eefe5 | 115 | { |
| Kojto | 0:ce33c63eefe5 | 116 | LED_D3_OFF; |
| Kojto | 0:ce33c63eefe5 | 117 | LED_D2_OFF; |
| Kojto | 0:ce33c63eefe5 | 118 | printf("\n\nStarting TCP/IP Server\n"); |
| Kojto | 0:ce33c63eefe5 | 119 | init_tcp(); |
| Kojto | 0:ce33c63eefe5 | 120 | create_tcp_connection(TCPIP_PORT); |
| Kojto | 0:ce33c63eefe5 | 121 | } |
| Kojto | 0:ce33c63eefe5 | 122 | |
| Kojto | 0:ce33c63eefe5 | 123 | bool check_dhcp(void) |
| Kojto | 0:ce33c63eefe5 | 124 | { |
| Kojto | 0:ce33c63eefe5 | 125 | int32_t t; |
| Kojto | 0:ce33c63eefe5 | 126 | //static uint8_t obtainIpInfoFlag = FALSE; |
| Kojto | 0:ce33c63eefe5 | 127 | |
| Kojto | 0:ce33c63eefe5 | 128 | // Wait until for dhcp |
| Kojto | 0:ce33c63eefe5 | 129 | while (wigo.is_dhcp_configured() == false) |
| Kojto | 0:ce33c63eefe5 | 130 | { |
| Kojto | 0:ce33c63eefe5 | 131 | wait_ms(500); |
| Kojto | 0:ce33c63eefe5 | 132 | printf("waiting\n"); |
| Kojto | 0:ce33c63eefe5 | 133 | } |
| Kojto | 0:ce33c63eefe5 | 134 | |
| Kojto | 0:ce33c63eefe5 | 135 | // Check if we are in a connected state. If so, set flags and LED |
| Kojto | 0:ce33c63eefe5 | 136 | //if (wigo.is_connected() == true) |
| Kojto | 0:ce33c63eefe5 | 137 | //{ |
| Kojto | 0:ce33c63eefe5 | 138 | //if (obtainIpInfoFlag == FALSE) |
| Kojto | 0:ce33c63eefe5 | 139 | //{ |
| Kojto | 0:ce33c63eefe5 | 140 | // obtainIpInfoFlag = TRUE; // Set flag so we don't constantly turn the LED on |
| Kojto | 0:ce33c63eefe5 | 141 | //LED_D3_ON; |
| Kojto | 0:ce33c63eefe5 | 142 | //If Smart Config was performed, we need to send complete notification to the configure (Smart Phone App) |
| Kojto | 0:ce33c63eefe5 | 143 | // if (ConnectUsingSmartConfig==1) |
| Kojto | 0:ce33c63eefe5 | 144 | // { |
| Kojto | 0:ce33c63eefe5 | 145 | // ConnectUsingSmartConfig = 0; |
| Kojto | 0:ce33c63eefe5 | 146 | // SmartConfigProfilestored = SMART_CONFIG_SET; |
| Kojto | 0:ce33c63eefe5 | 147 | // } |
| Kojto | 0:ce33c63eefe5 | 148 | |
| Kojto | 0:ce33c63eefe5 | 149 | //} |
| Kojto | 0:ce33c63eefe5 | 150 | t = wigo._socket.mdnsAdvertiser(1, DevServname, sizeof(DevServname)); |
| Kojto | 0:ce33c63eefe5 | 151 | printf("mDNS Status= %x\n", t); |
| Kojto | 0:ce33c63eefe5 | 152 | return true; |
| Kojto | 0:ce33c63eefe5 | 153 | //} |
| Kojto | 0:ce33c63eefe5 | 154 | //return FALSE; |
| Kojto | 0:ce33c63eefe5 | 155 | } |
| Kojto | 0:ce33c63eefe5 | 156 | |
| Kojto | 0:ce33c63eefe5 | 157 | void do_FTC(void) |
| Kojto | 0:ce33c63eefe5 | 158 | { |
| Kojto | 0:ce33c63eefe5 | 159 | printf("Running First Time Configuration\n"); |
| Kojto | 0:ce33c63eefe5 | 160 | // server_running = 1; |
| Kojto | 0:ce33c63eefe5 | 161 | //runSmartConfig = 1; |
| Kojto | 0:ce33c63eefe5 | 162 | start_smart_config(); |
| Kojto | 0:ce33c63eefe5 | 163 | init_tcp(); |
| Kojto | 0:ce33c63eefe5 | 164 | RED_OFF; |
| Kojto | 0:ce33c63eefe5 | 165 | GREEN_OFF; |
| Kojto | 0:ce33c63eefe5 | 166 | BLUE_OFF; |
| Kojto | 0:ce33c63eefe5 | 167 | userFS.FTC = 1; |
| Kojto | 0:ce33c63eefe5 | 168 | wigo._nvmem.write( NVMEM_USER_FILE_1_FILEID, sizeof(userFS), 0, (uint8_t *) &userFS); |
| Kojto | 0:ce33c63eefe5 | 169 | //runSmartConfig = 0; |
| Kojto | 0:ce33c63eefe5 | 170 | SmartConfigProfilestored = SMART_CONFIG_SET; |
| Kojto | 0:ce33c63eefe5 | 171 | wigo._wlan.stop(); |
| Kojto | 0:ce33c63eefe5 | 172 | printf("FTC finished\n"); |
| Kojto | 0:ce33c63eefe5 | 173 | } |
| Kojto | 0:ce33c63eefe5 | 174 | |
| Kojto | 0:ce33c63eefe5 | 175 | |
| Kojto | 0:ce33c63eefe5 | 176 |


SimpleLink Wi-Fi CC3000