00001
00028 #include <ctype.h>
00029 #include <board.h>
00030 #include "wl_api.h"
00031 #include "wl_cm.h"
00032
00033 #include "lwip/netif.h"
00034
00035 #include "http_server_gui.h"
00036 #include "gui.h"
00037 #if BOARD == EVK1104 || BOARD == EVK1105
00038 #include "et024006dhu.h"
00039 #else
00040 #define WHITE 0
00041 #endif
00042 #include "clocks.h"
00043 #include "board.h"
00044 #include "wl_util.h"
00045 #include "util.h"
00046
00047 #include <string.h>
00048
00049 #define GUI_MAX_NETWORKS 16
00050
00051 #define MAX_KEY_LEN 64
00052 char ascii_key[MAX_KEY_LEN];
00053
00054 char gui_print_buf[80];
00055 static struct wl_network_t gui_network_list[GUI_MAX_NETWORKS];
00056 static int gui_network_cnt = 0;
00057 #define gui_printk(line, args...) do { \
00058 extern char gui_print_buf[80]; \
00059 sniprintf(gui_print_buf, sizeof(gui_print_buf), args); \
00060 gui_set_title(gui_print_buf, line); \
00061 } while(0)
00062
00063 static uint8_t ascii_to_key(char *outp, const char *inp) {
00064 char buf[3];
00065 int len;
00066 buf[2] = '\0';
00067 len = strlen(inp);
00068 if (len % 2) {
00069 printk("Invalid length\n");
00070 }
00071 len = 0;
00072 while (*inp) {
00073 if (! isxdigit(*inp) || ! isxdigit(*(inp+1)) || len > MAX_KEY_LEN) {
00074 return 0;
00075 }
00076 buf[0] = *inp++;
00077 buf[1] = *inp++;
00078 *outp++ = strtol(buf, NULL, 16);
00079 len++;
00080 }
00081 return len;
00082 }
00083
00087 static void
00088 gui_scan_cb(void)
00089 {
00090 struct wl_network_t* wl_network_list;
00091 uint8_t wl_network_cnt, i;
00092
00093 gui_clear_scroll_box();
00094 gui_set_bg_color(WHITE);
00095 gui_add_scroll_box_item("Scan Contents:", sizeof "Scan Contents:");
00096
00097 wl_get_network_list(&wl_network_list, &wl_network_cnt);
00098 if (wl_network_cnt == 0)
00099 gui_printk(1, "No networks found");
00100 else
00101 gui_printk(1, " ");
00102
00103 gui_network_cnt = wl_network_cnt > GUI_MAX_NETWORKS ? GUI_MAX_NETWORKS :
00104 wl_network_cnt;
00105
00106 memcpy(gui_network_list, wl_network_list,
00107 sizeof(struct wl_network_t) * gui_network_cnt);
00108
00109 for (i = 0; i < gui_network_cnt; i++) {
00110 struct wl_network_t* net = &gui_network_list[i];
00111 char print_buf[80];
00112 char ssid[WL_SSID_MAX_LENGTH + 1];
00113
00114 memset(ssid, 0, sizeof(ssid));
00115 strncpy(ssid, net->ssid.ssid, net->ssid.len);
00116 siprintf(print_buf, "%s (%ld) %s", ssid,
00117 net->rssi, enc_type2str(net->enc_type));
00118 printk("%s\n", print_buf);
00119 gui_add_scroll_box_item(print_buf, strlen(print_buf));
00120 }
00121
00122 gui_inc_scroll_cursor();
00123 }
00124
00125
00126
00130 static void
00131 gui_connect_cb(void)
00132 {
00133 int index;
00134 struct wl_network_t* selected_net;
00135 struct wl_network_t* current_net = wl_get_current_network();
00136 char key[20];
00137
00138 index = gui_scroll_idx() - 1;
00139 if (index < 0 || index > gui_network_cnt)
00140 return;
00141 selected_net = &gui_network_list[index];
00142 if (equal_bssid(&selected_net->bssid, ¤t_net->bssid))
00143 return;
00144
00145 if (selected_net->enc_type != ENC_TYPE_NONE)
00146 {
00147 int len;
00148
00149
00150 if (!gui_getstring(ascii_key, sizeof ascii_key))
00151 return;
00152 if (selected_net->enc_type == ENC_TYPE_WEP)
00153 {
00154 struct wl_mac_addr_t bssid;
00155
00156 memset(&bssid.octet, 0xff, sizeof bssid.octet);
00157 len = ascii_to_key(key, ascii_key);
00158 if (wl_add_wep_key(0, len, key, &bssid) != WL_SUCCESS)
00159 {
00160 printk("%s : Failed to add wep key\n", __func__);
00161 gui_printk(1, "Failed to add key");
00162 }
00163 wl_set_auth_mode(AUTH_MODE_SHARED_KEY);
00164 wl_set_default_wep_key(0);
00165 }
00166 else
00167 {
00168
00169 if (wl_set_passphrase(selected_net,
00170 ascii_key,
00171 strlen(ascii_key),
00172 ENC_TYPE_AUTO,
00173 AUTH_MODE_AUTO)
00174 != WL_SUCCESS) {
00175 printk("%s : Failed to add passphrase\n", __func__);
00176 gui_printk(1, "Failed to add passphrase");
00177 }
00178 }
00179 printk("key accepted\n");
00180 gui_getstring_onoff(FALSE);
00181
00182 }
00183 gui_printk(0, "Connecting ... ");
00184 gui_printk(1, " ");
00185 wl_cm_set_network(&selected_net->ssid, NULL);
00186 }
00187
00188 #if BOARD == EVK1100
00189
00190 static void
00191 go_back_cb(void)
00192 {
00193 gui_restore_buttons();
00194 gui_infobox_onoff(FALSE);
00195 }
00196 #endif
00197
00201 void
00202 gui_status_up_cb(void)
00203 {
00204 char str[WL_SSID_MAX_LENGTH + 1];
00205 struct wl_network_t *net = wl_get_current_network();
00206
00207 memset(str, 0, sizeof(str));
00208 strncpy(str, net->ssid.ssid, net->ssid.len);
00209 gui_printk(0, "Connected to \"%s\"", str);
00210 gui_printk(1, "My ip address is %s", ip2str(netif_default->ip_addr));
00211 #if BOARD == EVK1100
00212 gui_save_buttons();
00213 gui_clear_infobox(0);
00214
00215 gui_add_infobox_item(str, 0);
00216
00217 gui_add_infobox_item(ip2str(netif_default->ip_addr), 1);
00218 gui_infobox_onoff(TRUE);
00219 gui_set_button(0, "Back", sizeof "Back", go_back_cb);
00220 #endif
00221 }
00222
00223
00227 void gui_status_down_cb(void)
00228 {
00229
00230 }
00231
00232
00236 void gui_link_up_cb(void)
00237 {
00238 char str[WL_SSID_MAX_LENGTH + 1];
00239 struct wl_network_t *net = wl_get_current_network();
00240
00241 memset(str, 0, sizeof(str));
00242 strncpy(str, net->ssid.ssid, net->ssid.len);
00243 gui_printk(0, "Connected to \"%s\"", str);
00244 gui_printk(1, "Requesting dhcp ... ");
00245 }
00246
00247
00251 void gui_link_down_cb(void)
00252 {
00253 gui_printk(0, "Connecting ... ");
00254 gui_printk(1, " ");
00255 }
00256
00257
00261 void
00262 gui_start(void)
00263 {
00264 pm_freq_param_t pm_freq_param = {
00265 .cpu_f = FCPU_HZ,
00266 .pba_f = FPBA_HZ,
00267 .osc0_f = FOSC0,
00268 .osc0_startup = OSC0_STARTUP
00269 };
00270 gui_init(&pm_freq_param);
00271 gui_draw(1);
00272 gui_printk(0, "Disconnected");
00273 gui_set_button(0, "Scan", sizeof "Scan", gui_scan_cb);
00274 gui_set_button(1, "Connect", sizeof "Connect", gui_connect_cb);
00275 gui_set_button(2, "Up", sizeof "Up", gui_dec_scroll_cursor);
00276 gui_set_button(3, "Down", sizeof "Down", gui_inc_scroll_cursor);
00277 gui_clear_scroll_box();
00278 gui_set_bg_color(WHITE);
00279 gui_add_scroll_box_item("Scan Contents:", sizeof "Scan Contents:");
00280 }