00001
00028 #include <compiler.h>
00029 #include "board.h"
00030 #include "gpio.h"
00031 #include "adc.h"
00032
00033 #include "wl_api.h"
00034 #include "wl_cm.h"
00035
00036 #include "lwip/init.h"
00037 #include "lwip/dhcp.h"
00038 #include "lwip/tcp.h"
00039 #include "netif/etharp.h"
00040 #include "netif/wlif.h"
00041
00042 #include "startup.h"
00043 #include "trace.h"
00044 #include "fw_download.h"
00045 #include "timer.h"
00046 #include "wl_util.h"
00047 #include "util.h"
00048 #include "cmd_wl.h"
00049 #include "httpd.h"
00050 #include "ping.h"
00051 #include "ttcp.h"
00052 #include "gui.h"
00053
00054 #include "http_server_gui.h"
00055
00056 struct http_server {
00057 struct netif *netif;
00058 uint8_t wl_init_complete;
00059 };
00060
00061
00062
00063
00064
00068 static void
00069 tcp_tmr_cb(void *ctx)
00070 {
00071 tcp_tmr();
00072 }
00073
00074
00078 static void
00079 etharp_tmr_cb(void *ctx)
00080 {
00081 etharp_tmr();
00082 }
00083
00084
00088 static void
00089 dhcp_fine_tmr_cb(void *ctx)
00090 {
00091 dhcp_fine_tmr();
00092 }
00093
00094
00098 static void
00099 dhcp_coarse_tmr_cb(void *ctx)
00100 {
00101 dhcp_coarse_tmr();
00102 }
00103
00104
00108 static void
00109 wl_cm_conn_cb(struct wl_network_t* net, void* ctx)
00110 {
00111 struct http_server* hs = ctx;
00112
00113 gui_link_up_cb();
00114 printk("link up, connected to \"%s\"\n", net->ssid.ssid);
00115 printk("requesting dhcp ... ");
00116
00117 dhcp_start(hs->netif);
00118 }
00119
00120
00124 static void
00125 wl_cm_disconn_cb(void* ctx)
00126 {
00127 struct http_server* hs = ctx;
00128
00129 gui_link_down_cb();
00130
00131 if (netif_is_up(hs->netif)) {
00132 printk("link down, release dhcp\n");
00133 dhcp_release(hs->netif);
00134 dhcp_stop(hs->netif);
00135 } else {
00136 printk("link down\n");
00137 }
00138 }
00139
00140
00144 static void
00145 ip_status_cb(struct netif* netif)
00146 {
00147 if (netif_is_up(netif)) {
00148 gui_status_up_cb();
00149 printk("bound to %s\n", ip2str(netif->ip_addr));
00150 printk("starting httpd ... ");
00151 if (httpd_start() == ERR_OK)
00152 printk("ok\n");
00153 else
00154 printk("fail\n");
00155
00156 }
00157 else {
00158 gui_status_down_cb();
00159 printk("stopping httpd\n");
00160 httpd_stop();
00161 }
00162 }
00163
00164
00168 void
00169 adc_init(void)
00170 {
00171 static const gpio_map_t ADC_GPIO_MAP = {
00172 { AVR32_ADC_AD_0_PIN, AVR32_ADC_AD_0_FUNCTION },
00173 };
00174 volatile avr32_adc_t *adc = &AVR32_ADC;
00175
00176 gpio_enable_module(ADC_GPIO_MAP,
00177 sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));
00178 AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
00179 adc_configure(adc);
00180 adc_enable(adc, 0);
00181 }
00182
00183
00187 void
00188 led_init(void)
00189 {
00190 gpio_enable_gpio_pin(LED0_GPIO);
00191 gpio_enable_gpio_pin(LED1_GPIO);
00192 gpio_enable_gpio_pin(LED2_GPIO);
00193 gpio_enable_gpio_pin(LED3_GPIO);
00194 LED_Toggle(LED0);
00195 LED_Toggle(LED1);
00196 LED_Toggle(LED2);
00197 LED_Toggle(LED3);
00198 }
00199
00200
00204 void
00205 poll(struct http_server* hs)
00206 {
00207
00208 timer_poll();
00209
00210
00211 console_poll();
00212
00213
00214 wl_poll(timer_get_ms());
00215
00216 #ifdef WITH_GUI
00217 gui_exec(timer_get_ms());
00218 #endif
00219 }
00220
00221
00225 void
00226 wl_init_complete_cb(void* ctx)
00227 {
00228 struct http_server *hs = ctx;
00229 struct ip_addr ipaddr, netmask, gw;
00230 wl_err_t wl_status;
00231
00232 IP4_ADDR(&gw, 0,0,0,0);
00233 IP4_ADDR(&ipaddr, 0,0,0,0);
00234 IP4_ADDR(&netmask, 0,0,0,0);
00235
00236
00237 hs->netif = netif_add(hs->netif, &ipaddr, &netmask, &gw, NULL,
00238 wlif_init,
00239 ethernet_input );
00240 ASSERT(hs->netif, "failed to add netif");
00241 netif_set_default(hs->netif);
00242 netif_set_status_callback(hs->netif, ip_status_cb);
00243
00244
00245 timer_sched_timeout_cb(5000, TIMEOUT_PERIODIC,
00246 etharp_tmr_cb, hs);
00247 timer_sched_timeout_cb(TCP_TMR_INTERVAL, TIMEOUT_PERIODIC,
00248 tcp_tmr_cb, hs);
00249 timer_sched_timeout_cb(DHCP_FINE_TIMER_MSECS, TIMEOUT_PERIODIC,
00250 dhcp_fine_tmr_cb, hs);
00251 timer_sched_timeout_cb(DHCP_COARSE_TIMER_MSECS, TIMEOUT_PERIODIC,
00252 dhcp_coarse_tmr_cb, hs);
00253
00254
00255
00256
00257 console_init();
00258 console_add_cmd("scan", cmd_scan, NULL);
00259 console_add_cmd("connect", cmd_connect, NULL);
00260 console_add_cmd("setkey", cmd_setkey, NULL);
00261 console_add_cmd("status", cmd_status, NULL);
00262 console_add_cmd("powersave", cmd_power, NULL);
00263 console_add_cmd("psconf", cmd_psconf, NULL);
00264 console_add_cmd("ping", cmd_ping, NULL);
00265 console_add_cmd("ttcp", cmd_ttcp, NULL);
00266 #ifdef WITH_WPA
00267 console_add_cmd("wpass", cmd_setpass, NULL);
00268 console_add_cmd("dpass", cmd_delpass, NULL);
00269 #endif
00270
00271
00272 wl_status = wl_cm_start(NULL, wl_cm_conn_cb, wl_cm_disconn_cb, hs);
00273 ASSERT(wl_status == WL_SUCCESS, "failed to init wl conn mgr");
00274
00275
00276 adc_init();
00277 led_init();
00278
00279 gui_start();
00280 }
00281
00282
00283
00287 int
00288 main(void)
00289 {
00290 wl_err_t wl_status;
00291 int status;
00292 struct http_server *hs;
00293
00294 startup_init();
00295
00296 hs = calloc(1, sizeof(struct http_server));
00297 ASSERT(hs, "out of memory");
00298
00299 hs->netif = calloc(1, sizeof(struct netif));
00300 ASSERT(hs->netif, "out of memory");
00301
00302 timer_init(NULL, NULL);
00303 lwip_init();
00304
00305 status = fw_download_init();
00306 ASSERT(status == 0, "failed to prepare for firmware download\n");
00307
00308 wl_status = wl_init(hs, fw_download_cb, wl_init_complete_cb);
00309 switch (wl_status) {
00310 case WL_SUCCESS:
00311
00312 break;
00313
00314 case WL_CARD_FAILURE:
00315 printk("Could not detect wl device, aborting\n");
00316 return -1;
00317
00318 case WL_FIRMWARE_LENGTH:
00319 printk("Invalid firmware length, aborting\n");
00320 return -1;
00321
00322 case WL_FIRMWARE_INVALID:
00323 printk("Invalid firmware data, aborting\n");
00324 return -1;
00325
00326 default:
00327 printk("Failed to start wl initialization\n");
00328 return -1;
00329 }
00330
00331
00332 for (;;)
00333 poll(hs);
00334 }