00001
00114 #include <compiler.h>
00115 #include "board.h"
00116 #include "gpio.h"
00117 #include "adc.h"
00118
00119 #include "wl_api.h"
00120 #include "wl_cm.h"
00121
00122 #include "lwip/init.h"
00123 #include "lwip/dhcp.h"
00124 #include "lwip/tcp.h"
00125 #include "netif/etharp.h"
00126 #include "netif/wlif.h"
00127
00128 #include "startup.h"
00129 #include "trace.h"
00130 #include "fw_download.h"
00131 #include "timer.h"
00132 #include "wl_util.h"
00133 #include "util.h"
00134 #include "cmd_wl.h"
00135 #include "httpd.h"
00136 #include "ping.h"
00137 #include "ttcp.h"
00138 #include "gui.h"
00139
00140 #include "http_server_gui.h"
00141
00142 struct http_server {
00143 struct netif *netif;
00144 uint8_t wl_init_complete;
00145 };
00146
00150 static void
00151 tcp_tmr_cb(void *ctx)
00152 {
00153 tcp_tmr();
00154 }
00155
00156
00160 static void
00161 etharp_tmr_cb(void *ctx)
00162 {
00163 etharp_tmr();
00164 }
00165
00166
00170 static void
00171 dhcp_fine_tmr_cb(void *ctx)
00172 {
00173 dhcp_fine_tmr();
00174 }
00175
00176
00180 static void
00181 dhcp_coarse_tmr_cb(void *ctx)
00182 {
00183 dhcp_coarse_tmr();
00184 }
00185
00186
00190 static void
00191 wl_cm_conn_cb(struct wl_network_t* net, void* ctx)
00192 {
00193 struct http_server* hs = ctx;
00194
00195 gui_link_up_cb();
00196 printk("link up, connected to \"%s\"\n", ssid2str(&net->ssid));
00197 printk("requesting dhcp ... ");
00198
00199 dhcp_start(hs->netif);
00200 }
00201
00202
00206 static void
00207 wl_cm_disconn_cb(void* ctx)
00208 {
00209 struct http_server* hs = ctx;
00210
00211 gui_link_down_cb();
00212
00213 if (netif_is_up(hs->netif)) {
00214 printk("link down, release dhcp\n");
00215 dhcp_release(hs->netif);
00216 dhcp_stop(hs->netif);
00217 } else {
00218 printk("link down\n");
00219 }
00220 }
00221
00222
00226 static void
00227 ip_status_cb(struct netif* netif)
00228 {
00229 if (netif_is_up(netif)) {
00230 gui_status_up_cb();
00231 printk("bound to %s\n", ip2str(netif->ip_addr));
00232 printk("starting httpd ... ");
00233 if (httpd_start() == ERR_OK)
00234 printk("ok\n");
00235 else
00236 printk("fail\n");
00237
00238 }
00239 else {
00240 gui_status_down_cb();
00241 printk("stopping httpd\n");
00242 httpd_stop();
00243 }
00244 }
00245
00246
00250 void
00251 adc_init(void)
00252 {
00253 static const gpio_map_t ADC_GPIO_MAP = {
00254 { AVR32_ADC_AD_0_PIN, AVR32_ADC_AD_0_FUNCTION },
00255 };
00256 volatile avr32_adc_t *adc = &AVR32_ADC;
00257
00258 gpio_enable_module(ADC_GPIO_MAP,
00259 sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));
00260 AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
00261 adc_configure(adc);
00262 adc_enable(adc, 0);
00263 }
00264
00265
00269 void
00270 led_init(void)
00271 {
00272 gpio_enable_gpio_pin(LED0_GPIO);
00273 gpio_enable_gpio_pin(LED1_GPIO);
00274 gpio_enable_gpio_pin(LED2_GPIO);
00275 gpio_enable_gpio_pin(LED3_GPIO);
00276 LED_Toggle(LED0);
00277 LED_Toggle(LED1);
00278 LED_Toggle(LED2);
00279 LED_Toggle(LED3);
00280 }
00281
00282
00286 void
00287 poll(struct http_server* hs)
00288 {
00289
00290 timer_poll();
00291
00292
00293 console_poll();
00294
00295
00296 wl_poll(timer_get_ms());
00297
00298
00299 wlif_poll(hs->netif);
00300
00301 #ifdef WITH_GUI
00302 gui_exec(timer_get_ms());
00303 #endif
00304 }
00305
00306
00310 void
00311 wl_init_complete_cb(void* ctx)
00312 {
00313 struct http_server *hs = ctx;
00314 struct ip_addr ipaddr, netmask, gw;
00315 wl_err_t wl_status;
00316
00317 IP4_ADDR(&gw, 0,0,0,0);
00318 IP4_ADDR(&ipaddr, 0,0,0,0);
00319 IP4_ADDR(&netmask, 0,0,0,0);
00320
00321
00322 hs->netif = netif_add(hs->netif, &ipaddr, &netmask, &gw, NULL,
00323 wlif_init,
00324 ethernet_input );
00325 ASSERT(hs->netif, "failed to add netif");
00326 netif_set_default(hs->netif);
00327 netif_set_status_callback(hs->netif, ip_status_cb);
00328
00329
00330 timer_sched_timeout_cb(5000, TIMEOUT_PERIODIC,
00331 etharp_tmr_cb, hs);
00332 timer_sched_timeout_cb(TCP_TMR_INTERVAL, TIMEOUT_PERIODIC,
00333 tcp_tmr_cb, hs);
00334 timer_sched_timeout_cb(DHCP_FINE_TIMER_MSECS, TIMEOUT_PERIODIC,
00335 dhcp_fine_tmr_cb, hs);
00336 timer_sched_timeout_cb(DHCP_COARSE_TIMER_MSECS, TIMEOUT_PERIODIC,
00337 dhcp_coarse_tmr_cb, hs);
00338
00339
00340 console_init();
00341 console_add_cmd("scan", cmd_scan, NULL);
00342 console_add_cmd("connect", cmd_connect, NULL);
00343 console_add_cmd("setkey", cmd_setkey, NULL);
00344 console_add_cmd("status", cmd_status, NULL);
00345 console_add_cmd("powersave", cmd_power, NULL);
00346 console_add_cmd("psconf", cmd_psconf, NULL);
00347 console_add_cmd("ping", cmd_ping, NULL);
00348 console_add_cmd("ttcp", cmd_ttcp, NULL);
00349 #ifdef WITH_WPA
00350 console_add_cmd("wpass", cmd_setpass, NULL);
00351 console_add_cmd("dpass", cmd_delpass, NULL);
00352 #endif
00353
00354
00355 wl_status = wl_cm_start(NULL, wl_cm_conn_cb, wl_cm_disconn_cb, hs);
00356 ASSERT(wl_status == WL_SUCCESS, "failed to init wl conn mgr");
00357
00358
00359 adc_init();
00360 led_init();
00361
00362 gui_start();
00363 }
00364
00368 int
00369 main(void)
00370 {
00371 wl_err_t wl_status;
00372 int status;
00373 struct http_server *hs;
00374
00375 startup_init();
00376
00377 hs = calloc(1, sizeof(struct http_server));
00378 ASSERT(hs, "out of memory");
00379
00380 hs->netif = calloc(1, sizeof(struct netif));
00381 ASSERT(hs->netif, "out of memory");
00382
00383 timer_init(NULL, NULL);
00384 lwip_init();
00385
00386 status = fw_download_init();
00387 ASSERT(status == 0, "failed to prepare for firmware download\n");
00388
00389 wl_status = wl_init(hs, fw_download_cb, wl_init_complete_cb);
00390 switch (wl_status) {
00391 case WL_SUCCESS:
00392
00393 break;
00394
00395 case WL_CARD_FAILURE:
00396 printk("Could not detect wl device, aborting\n");
00397 return -1;
00398
00399 case WL_FIRMWARE_INVALID:
00400 printk("Invalid firmware data, aborting\n");
00401 return -1;
00402
00403 default:
00404 printk("Failed to start wl initialization\n");
00405 return -1;
00406 }
00407
00408
00409 for (;;)
00410 poll(hs);
00411 }