tcp client for cc3000, using mbed socket interface

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Thu Sep 26 20:31:40 2013 +0000
Revision:
0:2ffed535a426
Child:
2:c8cd9df34be9
initial version tcp client

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:2ffed535a426 1 /* mbed Microcontroller Library
Kojto 0:2ffed535a426 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 0:2ffed535a426 3 *
Kojto 0:2ffed535a426 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 0:2ffed535a426 5 * you may not use this file except in compliance with the License.
Kojto 0:2ffed535a426 6 * You may obtain a copy of the License at
Kojto 0:2ffed535a426 7 *
Kojto 0:2ffed535a426 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 0:2ffed535a426 9 *
Kojto 0:2ffed535a426 10 * Unless required by applicable law or agreed to in writing, software
Kojto 0:2ffed535a426 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 0:2ffed535a426 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 0:2ffed535a426 13 * See the License for the specific language governing permissions and
Kojto 0:2ffed535a426 14 * limitations under the License.
Kojto 0:2ffed535a426 15 */
Kojto 0:2ffed535a426 16 #include "mbed.h"
Kojto 0:2ffed535a426 17 #include "NVIC_set_all_priorities.h"
Kojto 0:2ffed535a426 18 #include "cc3000.h"
Kojto 0:2ffed535a426 19 #include "main.h"
Kojto 0:2ffed535a426 20
Kojto 0:2ffed535a426 21 #include "TCPSocketConnection.h"
Kojto 0:2ffed535a426 22 #include "TCPSocketServer.h"
Kojto 0:2ffed535a426 23
Kojto 0:2ffed535a426 24 using namespace mbed_cc3000;
Kojto 0:2ffed535a426 25
Kojto 0:2ffed535a426 26 #if (WIGO_BOARD)
Kojto 0:2ffed535a426 27 // Wi-Go battery charger control
Kojto 0:2ffed535a426 28 DigitalOut PWR_EN1(PTB2);
Kojto 0:2ffed535a426 29 DigitalOut PWR_EN2(PTB3);
Kojto 0:2ffed535a426 30 #endif
Kojto 0:2ffed535a426 31
Kojto 0:2ffed535a426 32 Serial pc(USBTX, USBRX);
Kojto 0:2ffed535a426 33 cc3000 wigo(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn);
Kojto 0:2ffed535a426 34 tUserFS user_info;
Kojto 0:2ffed535a426 35
Kojto 0:2ffed535a426 36
Kojto 0:2ffed535a426 37 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
Kojto 0:2ffed535a426 38 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
Kojto 0:2ffed535a426 39 #else
Kojto 0:2ffed535a426 40 const uint8_t smartconfigkey = 0;
Kojto 0:2ffed535a426 41 #endif
Kojto 0:2ffed535a426 42
Kojto 0:2ffed535a426 43 /** Print cc3000 information
Kojto 0:2ffed535a426 44 * \param none
Kojto 0:2ffed535a426 45 * \return none
Kojto 0:2ffed535a426 46 */
Kojto 0:2ffed535a426 47 void print_cc3000_info() {
Kojto 0:2ffed535a426 48 uint8_t myMAC[8];
Kojto 0:2ffed535a426 49
Kojto 0:2ffed535a426 50 printf("MAC address + cc3000 info\n");
Kojto 0:2ffed535a426 51 wigo.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 0:2ffed535a426 52 wigo.get_mac_address(myMAC);
Kojto 0:2ffed535a426 53 printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x\n\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
Kojto 0:2ffed535a426 54
Kojto 0:2ffed535a426 55 printf(" FTC %i\n",user_info.FTC);
Kojto 0:2ffed535a426 56 printf(" PP_version %i.%i\n",user_info.PP_version[0], user_info.PP_version[1]);
Kojto 0:2ffed535a426 57 printf(" SERV_PACK %i.%i\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
Kojto 0:2ffed535a426 58 printf(" DRV_VER %i.%i.%i\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
Kojto 0:2ffed535a426 59 printf(" FW_VER %i.%i.%i\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
Kojto 0:2ffed535a426 60 }
Kojto 0:2ffed535a426 61
Kojto 0:2ffed535a426 62 /** Connect to SSID with a timeout
Kojto 0:2ffed535a426 63 * \param ssid Name of SSID
Kojto 0:2ffed535a426 64 * \param key Password
Kojto 0:2ffed535a426 65 * \param sec_mode Security mode
Kojto 0:2ffed535a426 66 * \return none
Kojto 0:2ffed535a426 67 */
Kojto 0:2ffed535a426 68 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
Kojto 0:2ffed535a426 69 printf("Connecting to SSID: %s. Timeout is 10s.\n",ssid);
Kojto 0:2ffed535a426 70 if (wigo.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
Kojto 0:2ffed535a426 71 printf(" Connected\n");
Kojto 0:2ffed535a426 72 } else {
Kojto 0:2ffed535a426 73 printf(" Connection timed-out (error). Please restart.\n");
Kojto 0:2ffed535a426 74 while(1);
Kojto 0:2ffed535a426 75 }
Kojto 0:2ffed535a426 76 }
Kojto 0:2ffed535a426 77
Kojto 0:2ffed535a426 78 /** Connect to SSID without security
Kojto 0:2ffed535a426 79 * \param ssid Name of SSID
Kojto 0:2ffed535a426 80 * \return none
Kojto 0:2ffed535a426 81 */
Kojto 0:2ffed535a426 82 void connect_to_ssid(uint8_t *ssid) {
Kojto 0:2ffed535a426 83 wigo.connect_open((uint8_t *)ssid);
Kojto 0:2ffed535a426 84 }
Kojto 0:2ffed535a426 85
Kojto 0:2ffed535a426 86 void wigo_init(void) {
Kojto 0:2ffed535a426 87 /* KL25 specific code */
Kojto 0:2ffed535a426 88 // Wi-Go set current to 500mA since we're turning on the Wi-Fi
Kojto 0:2ffed535a426 89 SET_PWR_EN1;
Kojto 0:2ffed535a426 90 SET_PWR_EN2;
Kojto 0:2ffed535a426 91
Kojto 0:2ffed535a426 92 NVIC_set_all_irq_priorities(3);
Kojto 0:2ffed535a426 93 NVIC_SetPriority(SPI0_IRQn, 0x0); // Wi-Fi SPI interrupt must be higher priority than SysTick
Kojto 0:2ffed535a426 94 NVIC_SetPriority(PORTA_IRQn, 0x1);
Kojto 0:2ffed535a426 95 NVIC_SetPriority(SysTick_IRQn, 0x2); // SysTick set to lower priority than Wi-Fi SPI bus interrupt
Kojto 0:2ffed535a426 96 CLEAR_PCR_INTERRUPT;
Kojto 0:2ffed535a426 97 CLEAN_PORT_INTERRUPT;
Kojto 0:2ffed535a426 98 }
Kojto 0:2ffed535a426 99
Kojto 0:2ffed535a426 100 /** First time configuration
Kojto 0:2ffed535a426 101 * \param none
Kojto 0:2ffed535a426 102 * \return none
Kojto 0:2ffed535a426 103 */
Kojto 0:2ffed535a426 104 void do_FTC(void) {
Kojto 0:2ffed535a426 105 printf("Running First Time Configuration\n");
Kojto 0:2ffed535a426 106 wigo.start_smart_config(smartconfigkey);
Kojto 0:2ffed535a426 107 while (wigo.is_dhcp_configured() == false) {
Kojto 0:2ffed535a426 108 wait_ms(500);
Kojto 0:2ffed535a426 109 printf("Waiting for dhcp to be set.\n");
Kojto 0:2ffed535a426 110 }
Kojto 0:2ffed535a426 111 user_info.FTC = 1;
Kojto 0:2ffed535a426 112 wigo.set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 0:2ffed535a426 113 wigo._wlan.stop();
Kojto 0:2ffed535a426 114 printf("FTC finished.\n");
Kojto 0:2ffed535a426 115 }
Kojto 0:2ffed535a426 116
Kojto 0:2ffed535a426 117 /** Ping a site
Kojto 0:2ffed535a426 118 * \param none
Kojto 0:2ffed535a426 119 * \return int
Kojto 0:2ffed535a426 120 */
Kojto 0:2ffed535a426 121 int main() {
Kojto 0:2ffed535a426 122 wigo_init();
Kojto 0:2ffed535a426 123 pc.baud(115200);
Kojto 0:2ffed535a426 124
Kojto 0:2ffed535a426 125 wigo.start(0);
Kojto 0:2ffed535a426 126 printf("CC3000 tcp client demo.\n");
Kojto 0:2ffed535a426 127 print_cc3000_info();
Kojto 0:2ffed535a426 128
Kojto 0:2ffed535a426 129 printf("Attempting SSID Connection\n");
Kojto 0:2ffed535a426 130 #if (USE_SMART_CONFIG == 1)
Kojto 0:2ffed535a426 131 if (user_info.FTC == 1) {
Kojto 0:2ffed535a426 132 wigo._wlan.ioctl_set_connection_policy(0, 1, 1);
Kojto 0:2ffed535a426 133 } else {
Kojto 0:2ffed535a426 134 printf("Smart config is not set, starting configuration\n");
Kojto 0:2ffed535a426 135 do_FTC();
Kojto 0:2ffed535a426 136 printf("Smart config is set. Please restart your board.\n");
Kojto 0:2ffed535a426 137 while(1);
Kojto 0:2ffed535a426 138 }
Kojto 0:2ffed535a426 139 #else
Kojto 0:2ffed535a426 140 wigo._wlan.ioctl_set_connection_policy(0, 0, 0);
Kojto 0:2ffed535a426 141 #ifndef CC3000_TINY_DRIVER
Kojto 0:2ffed535a426 142 #ifdef AP_KEY
Kojto 0:2ffed535a426 143 connect_to_ssid(SSID,AP_KEY,AP_SECURITY);
Kojto 0:2ffed535a426 144 #else
Kojto 0:2ffed535a426 145 connect_to_ssid((uint8_t *)SSID);
Kojto 0:2ffed535a426 146 #endif
Kojto 0:2ffed535a426 147 #else
Kojto 0:2ffed535a426 148 connect_to_ssid((uint8_t *)SSID);
Kojto 0:2ffed535a426 149 #endif
Kojto 0:2ffed535a426 150 #endif
Kojto 0:2ffed535a426 151 printf("DHCP request\n");
Kojto 0:2ffed535a426 152 while (wigo.is_dhcp_configured() == false) {
Kojto 0:2ffed535a426 153 wait_ms(500);
Kojto 0:2ffed535a426 154 printf(" Waiting for dhcp to be set.\n");
Kojto 0:2ffed535a426 155 }
Kojto 0:2ffed535a426 156
Kojto 0:2ffed535a426 157 tNetappIpconfigRetArgs ipinfo2;
Kojto 0:2ffed535a426 158 wigo.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure
Kojto 0:2ffed535a426 159 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:2ffed535a426 160
Kojto 0:2ffed535a426 161 const char* ECHO_SERVER_ADDRESS = "192.168.1.10";
Kojto 0:2ffed535a426 162 const int ECHO_SERVER_PORT = 1895;
Kojto 0:2ffed535a426 163
Kojto 0:2ffed535a426 164 TCPSocketConnection socket;
Kojto 0:2ffed535a426 165 while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
Kojto 0:2ffed535a426 166 printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
Kojto 0:2ffed535a426 167 wait(1);
Kojto 0:2ffed535a426 168 }
Kojto 0:2ffed535a426 169
Kojto 0:2ffed535a426 170 char hello[] = "Hello World\n";
Kojto 0:2ffed535a426 171 socket.send_all(hello, sizeof(hello) - 1);
Kojto 0:2ffed535a426 172
Kojto 0:2ffed535a426 173 char buf[256];
Kojto 0:2ffed535a426 174 int n = socket.receive(buf, 256);
Kojto 0:2ffed535a426 175 buf[n] = '\0';
Kojto 0:2ffed535a426 176 printf("%s", buf);
Kojto 0:2ffed535a426 177
Kojto 0:2ffed535a426 178 socket.close();
Kojto 0:2ffed535a426 179 printf("Completed.\n");
Kojto 0:2ffed535a426 180
Kojto 0:2ffed535a426 181 }