cc3000 udp client demo

Dependencies:   NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Committer:
Kojto
Date:
Tue Oct 08 14:56:50 2013 +0000
Revision:
2:72ff0379d7b5
Parent:
1:d9c04472edaf
Child:
3:2941c1b5d79e
Update host driver to rev 35

Who changed what in which revision?

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