cc3000 tcp server demo (please check mbed socket interface for python script to test this demo) mbed.org/handbook/Socket

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Thu Oct 03 17:51:34 2013 +0200
Revision:
3:3f53082b16ec
Parent:
1:ea2af255043f
Child:
4:9d543391017c
ending - unix, init(), update to the new demo template

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:1cadde6a47e3 1 /* mbed Microcontroller Library
Kojto 0:1cadde6a47e3 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 0:1cadde6a47e3 3 *
Kojto 0:1cadde6a47e3 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 0:1cadde6a47e3 5 * you may not use this file except in compliance with the License.
Kojto 0:1cadde6a47e3 6 * You may obtain a copy of the License at
Kojto 0:1cadde6a47e3 7 *
Kojto 0:1cadde6a47e3 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 0:1cadde6a47e3 9 *
Kojto 0:1cadde6a47e3 10 * Unless required by applicable law or agreed to in writing, software
Kojto 0:1cadde6a47e3 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 0:1cadde6a47e3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 0:1cadde6a47e3 13 * See the License for the specific language governing permissions and
Kojto 0:1cadde6a47e3 14 * limitations under the License.
Kojto 0:1cadde6a47e3 15 */
Kojto 0:1cadde6a47e3 16 #include "mbed.h"
Kojto 0:1cadde6a47e3 17 #include "cc3000.h"
Kojto 0:1cadde6a47e3 18 #include "main.h"
Kojto 0:1cadde6a47e3 19
Kojto 0:1cadde6a47e3 20 #include "TCPSocketConnection.h"
Kojto 0:1cadde6a47e3 21 #include "TCPSocketServer.h"
Kojto 0:1cadde6a47e3 22
Kojto 0:1cadde6a47e3 23 using namespace mbed_cc3000;
Kojto 0:1cadde6a47e3 24
Kojto 0:1cadde6a47e3 25 tUserFS user_info;
Kojto 0:1cadde6a47e3 26
Kojto 3:3f53082b16ec 27 /* cc3000 module declaration specific for user's board. Check also init() */
Kojto 3:3f53082b16ec 28 #if (MY_BOARD == WIGO)
Kojto 3:3f53082b16ec 29 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn);
Kojto 3:3f53082b16ec 30 Serial pc(USBTX, USBRX);
Kojto 3:3f53082b16ec 31 #elif (MY_BOARD == WIFI_DIPCORTEX)
Kojto 3:3f53082b16ec 32 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), PIN_INT0_IRQn);
Kojto 3:3f53082b16ec 33 Serial pc(UART_TX, UART_RX);
Kojto 3:3f53082b16ec 34 #else
Kojto 3:3f53082b16ec 35
Kojto 3:3f53082b16ec 36 #endif
Kojto 0:1cadde6a47e3 37
Kojto 0:1cadde6a47e3 38 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
Kojto 0:1cadde6a47e3 39 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
Kojto 0:1cadde6a47e3 40 #else
Kojto 0:1cadde6a47e3 41 const uint8_t smartconfigkey = 0;
Kojto 0:1cadde6a47e3 42 #endif
Kojto 0:1cadde6a47e3 43
Kojto 3:3f53082b16ec 44 /**
Kojto 3:3f53082b16ec 45 * \brief Print cc3000 information
Kojto 3:3f53082b16ec 46 * \param none
Kojto 0:1cadde6a47e3 47 * \return none
Kojto 0:1cadde6a47e3 48 */
Kojto 0:1cadde6a47e3 49 void print_cc3000_info() {
Kojto 0:1cadde6a47e3 50 uint8_t myMAC[8];
Kojto 0:1cadde6a47e3 51
Kojto 3:3f53082b16ec 52 printf("MAC address + cc3000 info \r\n");
Kojto 3:3f53082b16ec 53 wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 3:3f53082b16ec 54 wifi.get_mac_address(myMAC);
Kojto 3:3f53082b16ec 55 printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
Kojto 0:1cadde6a47e3 56
Kojto 3:3f53082b16ec 57 printf(" FTC %i \r\n",user_info.FTC);
Kojto 3:3f53082b16ec 58 printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]);
Kojto 3:3f53082b16ec 59 printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
Kojto 3:3f53082b16ec 60 printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
Kojto 3:3f53082b16ec 61 printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
Kojto 0:1cadde6a47e3 62 }
Kojto 0:1cadde6a47e3 63
Kojto 3:3f53082b16ec 64 /**
Kojto 3:3f53082b16ec 65 * \brief Connect to SSID with a timeout
Kojto 3:3f53082b16ec 66 * \param ssid Name of SSID
Kojto 3:3f53082b16ec 67 * \param key Password
Kojto 3:3f53082b16ec 68 * \param sec_mode Security mode
Kojto 0:1cadde6a47e3 69 * \return none
Kojto 0:1cadde6a47e3 70 */
Kojto 0:1cadde6a47e3 71 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
Kojto 0:1cadde6a47e3 72 printf("Connecting to SSID: %s. Timeout is 10s.\n",ssid);
Kojto 3:3f53082b16ec 73 if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
Kojto 0:1cadde6a47e3 74 printf(" Connected\n");
Kojto 0:1cadde6a47e3 75 } else {
Kojto 0:1cadde6a47e3 76 printf(" Connection timed-out (error). Please restart.\n");
Kojto 0:1cadde6a47e3 77 while(1);
Kojto 0:1cadde6a47e3 78 }
Kojto 0:1cadde6a47e3 79 }
Kojto 0:1cadde6a47e3 80
Kojto 3:3f53082b16ec 81 /**
Kojto 3:3f53082b16ec 82 * \brief Connect to SSID without security
Kojto 3:3f53082b16ec 83 * \param ssid Name of SSID
Kojto 0:1cadde6a47e3 84 * \return none
Kojto 0:1cadde6a47e3 85 */
Kojto 3:3f53082b16ec 86 void connect_to_ssid(char *ssid) {
Kojto 3:3f53082b16ec 87 wifi.connect_open((uint8_t *)ssid);
Kojto 0:1cadde6a47e3 88 }
Kojto 0:1cadde6a47e3 89
Kojto 3:3f53082b16ec 90 /**
Kojto 3:3f53082b16ec 91 * \brief First time configuration
Kojto 3:3f53082b16ec 92 * \param none
Kojto 0:1cadde6a47e3 93 * \return none
Kojto 0:1cadde6a47e3 94 */
Kojto 0:1cadde6a47e3 95 void do_FTC(void) {
Kojto 3:3f53082b16ec 96 printf("Running First Time Configuration \r\n");
Kojto 3:3f53082b16ec 97 wifi.start_smart_config(smartconfigkey);
Kojto 3:3f53082b16ec 98 while (wifi.is_dhcp_configured() == false) {
Kojto 0:1cadde6a47e3 99 wait_ms(500);
Kojto 3:3f53082b16ec 100 printf("Waiting for dhcp to be set. \r\n");
Kojto 0:1cadde6a47e3 101 }
Kojto 0:1cadde6a47e3 102 user_info.FTC = 1;
Kojto 3:3f53082b16ec 103 wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 3:3f53082b16ec 104 wifi._wlan.stop();
Kojto 0:1cadde6a47e3 105 printf("FTC finished.\n");
Kojto 0:1cadde6a47e3 106 }
Kojto 0:1cadde6a47e3 107
Kojto 3:3f53082b16ec 108 /**
Kojto 3:3f53082b16ec 109 * \brief TCP server demo
Kojto 0:1cadde6a47e3 110 * \param none
Kojto 0:1cadde6a47e3 111 * \return int
Kojto 0:1cadde6a47e3 112 */
Kojto 0:1cadde6a47e3 113 int main() {
Kojto 3:3f53082b16ec 114 init(); /* board dependent init */
Kojto 0:1cadde6a47e3 115 pc.baud(115200);
Kojto 0:1cadde6a47e3 116
Kojto 3:3f53082b16ec 117 wifi.start(0);
Kojto 0:1cadde6a47e3 118 printf("CC3000 tcp server demo.\n");
Kojto 0:1cadde6a47e3 119 print_cc3000_info();
Kojto 0:1cadde6a47e3 120
Kojto 3:3f53082b16ec 121 printf("Attempting SSID Connection. \r\n");
Kojto 0:1cadde6a47e3 122 #if (USE_SMART_CONFIG == 1)
Kojto 0:1cadde6a47e3 123 if (user_info.FTC == 1) {
Kojto 3:3f53082b16ec 124 wifi._wlan.ioctl_set_connection_policy(0, 1, 1);
Kojto 0:1cadde6a47e3 125 } else {
Kojto 3:3f53082b16ec 126 printf("Smart config is not set, starting configuration. \r\n");
Kojto 0:1cadde6a47e3 127 do_FTC();
Kojto 3:3f53082b16ec 128 printf("Smart config is set. Please restart your board. \r\n");
Kojto 0:1cadde6a47e3 129 while(1);
Kojto 0:1cadde6a47e3 130 }
Kojto 0:1cadde6a47e3 131 #else
Kojto 3:3f53082b16ec 132 wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
Kojto 0:1cadde6a47e3 133 #ifndef CC3000_TINY_DRIVER
Kojto 0:1cadde6a47e3 134 #ifdef AP_KEY
Kojto 3:3f53082b16ec 135 connect_to_ssid(SSID, AP_KEY, AP_SECURITY);
Kojto 0:1cadde6a47e3 136 #else
Kojto 3:3f53082b16ec 137 connect_to_ssid(SSID);
Kojto 0:1cadde6a47e3 138 #endif
Kojto 0:1cadde6a47e3 139 #else
Kojto 3:3f53082b16ec 140 connect_to_ssid(SSID);
Kojto 0:1cadde6a47e3 141 #endif
Kojto 0:1cadde6a47e3 142 #endif
Kojto 3:3f53082b16ec 143 printf("DHCP request \r\n");
Kojto 3:3f53082b16ec 144 while (wifi.is_dhcp_configured() == false) {
Kojto 0:1cadde6a47e3 145 wait_ms(500);
Kojto 3:3f53082b16ec 146 printf(" Waiting for dhcp to be set. \r\n");
Kojto 0:1cadde6a47e3 147 }
Kojto 0:1cadde6a47e3 148
Kojto 0:1cadde6a47e3 149 tNetappIpconfigRetArgs ipinfo2;
Kojto 3:3f53082b16ec 150 wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 3:3f53082b16ec 151 printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
Kojto 0:1cadde6a47e3 152
Kojto 0:1cadde6a47e3 153 const int ECHO_SERVER_PORT = 1895;
Kojto 0:1cadde6a47e3 154
Kojto 0:1cadde6a47e3 155 TCPSocketServer server;
Kojto 0:1cadde6a47e3 156 server.bind(ECHO_SERVER_PORT);
Kojto 0:1cadde6a47e3 157 server.listen();
Kojto 3:3f53082b16ec 158
Kojto 0:1cadde6a47e3 159 while (true) {
Kojto 3:3f53082b16ec 160 printf("\nWait for new connection...\r\n");
Kojto 0:1cadde6a47e3 161 TCPSocketConnection client;
Kojto 0:1cadde6a47e3 162 server.accept(client);
Kojto 0:1cadde6a47e3 163 client.set_blocking(false, 1500); // Timeout after (1.5)s
Kojto 3:3f53082b16ec 164
Kojto 3:3f53082b16ec 165 printf("Connection from: %s \r\n", client.get_address());
Kojto 0:1cadde6a47e3 166 char buffer[256];
Kojto 0:1cadde6a47e3 167 while (true) {
Kojto 0:1cadde6a47e3 168 int n = client.receive(buffer, sizeof(buffer));
Kojto 0:1cadde6a47e3 169 if (n <= 0) break;
Kojto 3:3f53082b16ec 170
Kojto 0:1cadde6a47e3 171 client.send_all(buffer, n);
Kojto 0:1cadde6a47e3 172 if (n <= 0) break;
Kojto 0:1cadde6a47e3 173 }
Kojto 3:3f53082b16ec 174
Kojto 0:1cadde6a47e3 175 client.close();
Kojto 0:1cadde6a47e3 176 }
Kojto 0:1cadde6a47e3 177 }
Kojto 0:1cadde6a47e3 178