kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote |
| 15 | * products derived from this software without specific prior |
| 16 | * written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 19 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 24 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * This file is part of the Contiki desktop environment |
| 31 | * |
| 32 | * $Id: wget.c,v 1.1 2006/04/17 15:18:24 kthacker Exp $ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include "ctk.h" |
| 38 | #include "ek.h" |
| 39 | #include "webclient.h" |
| 40 | #include "resolv.h" |
| 41 | #include "petsciiconv.h" |
| 42 | #include "uiplib.h" |
| 43 | #include "loader.h" |
| 44 | |
| 45 | #include "contiki.h" |
| 46 | |
| 47 | #include "program-handler.h" |
| 48 | |
| 49 | #include <c64.h> |
| 50 | #include <cbm.h> |
| 51 | #include <stdio.h> |
| 52 | #include <string.h> |
| 53 | #include <fcntl.h> |
| 54 | |
| 55 | #include "c64-dio.h" |
| 56 | |
| 57 | #define USE_KERNAL 0 |
| 58 | |
| 59 | static struct ctk_window window; |
| 60 | |
| 61 | static struct ctk_label urllabel = |
| 62 | {CTK_LABEL(0, 1, 4, 1, "URL:")}; |
| 63 | static char url[80]; |
| 64 | static char urledit[80]; |
| 65 | struct ctk_textentry urltextentry = |
| 66 | {CTK_TEXTENTRY(5, 1, 29, 1, urledit, 78)}; |
| 67 | |
| 68 | |
| 69 | static struct ctk_label savefilenamelabel = |
| 70 | {CTK_LABEL(0, 3, 14, 1, "Save filename:")}; |
| 71 | static char savefilename[40]; |
| 72 | static struct ctk_textentry savefilenametextentry = |
| 73 | {CTK_TEXTENTRY(15, 3, 19, 1, savefilename, 38)}; |
| 74 | |
| 75 | static struct ctk_button filebutton = |
| 76 | {CTK_BUTTON(0, 5, 13, "Download file")}; |
| 77 | |
| 78 | static struct ctk_button d64button = |
| 79 | {CTK_BUTTON(17, 5, 18, "Download D64 disk")}; |
| 80 | |
| 81 | static struct ctk_label statustext = |
| 82 | {CTK_LABEL(0, 7, 36, 1, "")}; |
| 83 | static char statusmsg[40]; |
| 84 | |
| 85 | static struct ctk_window d64dialog; |
| 86 | static struct ctk_label overwritelabel = |
| 87 | {CTK_LABEL(0, 1, 36, 1, "This will overwrite the entire disk!")}; |
| 88 | static struct ctk_label makesurelabel1 = |
| 89 | {CTK_LABEL(7, 3, 22, 1, "Make sure you have the")}; |
| 90 | static struct ctk_label makesurelabel2 = |
| 91 | {CTK_LABEL(6, 4, 24, 1, "right disk in the drive!")}; |
| 92 | static struct ctk_button overwritebutton = |
| 93 | {CTK_BUTTON(2, 6, 14, "Overwrite disk")}; |
| 94 | static struct ctk_button cancelbutton = |
| 95 | {CTK_BUTTON(26, 6, 6, "Cancel")}; |
| 96 | |
| 97 | /*static DISPATCHER_SIGHANDLER(wget_sighandler, s, data); |
| 98 | static struct dispatcher_proc p = |
| 99 | {DISPATCHER_PROC("Web downloader", NULL, wget_sighandler, webclient_appcall)}; |
| 100 | static ek_id_t id;*/ |
| 101 | |
| 102 | EK_EVENTHANDLER(wget_eventhandler, ev, data); |
| 103 | EK_PROCESS(p, "Web downloader", EK_PRIO_NORMAL, |
| 104 | wget_eventhandler, NULL, NULL); |
| 105 | static ek_id_t id = EK_ID_NONE; |
| 106 | |
| 107 | /* State */ |
| 108 | |
| 109 | #define DLOAD_NONE 0 |
| 110 | #define DLOAD_FILE 1 |
| 111 | #define DLOAD_D64 2 |
| 112 | static u8_t dload_state; |
| 113 | static unsigned long dload_bytes; |
| 114 | |
| 115 | |
| 116 | |
| 117 | struct drv_state { |
| 118 | u8_t track; |
| 119 | u8_t sect; |
| 120 | }; |
| 121 | |
| 122 | static struct drv_state ds; |
| 123 | |
| 124 | static char buffer[256]; |
| 125 | static u16_t bufferptr; |
| 126 | |
| 127 | /*-----------------------------------------------------------------------------------*/ |
| 128 | /* wget_init(); |
| 129 | * |
| 130 | * Initializes and starts the web browser. Called either at startup or |
| 131 | * to open the browser window. |
| 132 | */ |
| 133 | LOADER_INIT_FUNC(wget_init, arg) |
| 134 | { |
| 135 | if(arg != NULL) { |
| 136 | strncpy(url, arg, sizeof(url)); |
| 137 | strncpy(urledit, arg, sizeof(urledit)); |
| 138 | petsciiconv_topetscii(urledit, sizeof(urledit)); |
| 139 | } else { |
| 140 | #ifdef WGET_CONF_URL |
| 141 | strncpy(url, WGET_CONF_URL, sizeof(url)); |
| 142 | strncpy(urledit, WGET_CONF_URL, sizeof(urledit)); |
| 143 | #endif /* WGET_CONF_URL */ |
| 144 | } |
| 145 | arg_free(arg); |
| 146 | |
| 147 | if(id == EK_ID_NONE) { |
| 148 | id = ek_start(&p); |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | /*-----------------------------------------------------------------------------------*/ |
| 153 | static void |
| 154 | show_statustext(char *text) |
| 155 | { |
| 156 | ctk_label_set_text(&statustext, text); |
| 157 | CTK_WIDGET_REDRAW(&statustext); |
| 158 | } |
| 159 | /*-----------------------------------------------------------------------------------*/ |
| 160 | /* open_url(): |
| 161 | * |
| 162 | * Called when the URL present in the global "url" variable should be |
| 163 | * opened. It will call the hostname resolver as well as the HTTP |
| 164 | * client requester. |
| 165 | */ |
| 166 | static void |
| 167 | start_get(void) |
| 168 | { |
| 169 | u16_t addr[2]; |
| 170 | unsigned char i; |
| 171 | static char host[32]; |
| 172 | char *file; |
| 173 | register char *urlptr; |
| 174 | unsigned short port; |
| 175 | |
| 176 | /* Trim off any spaces in the end of the url. */ |
| 177 | urlptr = url + strlen(url) - 1; |
| 178 | while(*urlptr == ' ' && urlptr > url) { |
| 179 | *urlptr = 0; |
| 180 | --urlptr; |
| 181 | } |
| 182 | |
| 183 | /* Don't even try to go further if the URL is empty. */ |
| 184 | if(urlptr == url) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* See if the URL starts with http://, otherwise prepend it. */ |
| 189 | if(strncmp(url, http_http, 7) != 0) { |
| 190 | while(urlptr >= url) { |
| 191 | *(urlptr + 7) = *urlptr; |
| 192 | --urlptr; |
| 193 | } |
| 194 | strncpy(url, http_http, 7); |
| 195 | } |
| 196 | |
| 197 | /* Find host part of the URL. */ |
| 198 | urlptr = &url[7]; |
| 199 | for(i = 0; i < sizeof(host); ++i) { |
| 200 | if(*urlptr == 0 || |
| 201 | *urlptr == '/' || |
| 202 | *urlptr == ' ' || |
| 203 | *urlptr == ':') { |
| 204 | host[i] = 0; |
| 205 | break; |
| 206 | } |
| 207 | host[i] = *urlptr; |
| 208 | ++urlptr; |
| 209 | } |
| 210 | |
| 211 | /* XXX: Here we should find the port part of the URL, but this isn't |
| 212 | currently done because of laziness from the programmer's side |
| 213 | :-) */ |
| 214 | |
| 215 | /* Find file part of the URL. */ |
| 216 | while(*urlptr != '/' && *urlptr != 0) { |
| 217 | ++urlptr; |
| 218 | } |
| 219 | if(*urlptr == '/') { |
| 220 | file = urlptr; |
| 221 | } else { |
| 222 | file = "/"; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* First check if the host is an IP address. */ |
| 227 | if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) { |
| 228 | |
| 229 | /* Try to lookup the hostname. If it fails, we initiate a hostname |
| 230 | lookup and print out an informative message on the |
| 231 | statusbar. */ |
| 232 | if(resolv_lookup(host) == NULL) { |
| 233 | resolv_query(host); |
| 234 | show_statustext("Resolving host..."); |
| 235 | return; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* The hostname we present in the hostname table, so we send out the |
| 240 | initial GET request. */ |
| 241 | if(webclient_get(host, 80, file) == 0) { |
| 242 | show_statustext("Out of memory error."); |
| 243 | } else { |
| 244 | show_statustext("Connecting..."); |
| 245 | } |
| 246 | } |
| 247 | /*-----------------------------------------------------------------------------------*/ |
| 248 | EK_EVENTHANDLER(wget_eventhandler, ev, data) |
| 249 | { |
| 250 | int ret; |
| 251 | static unsigned char i; |
| 252 | EK_EVENTHANDLER_ARGS(ev, data); |
| 253 | |
| 254 | if(ev == EK_EVENT_INIT) { |
| 255 | /* Create the main window. */ |
| 256 | ctk_window_new(&window, 36, 8, "Web downloader"); |
| 257 | |
| 258 | |
| 259 | CTK_WIDGET_ADD(&window, &urllabel); |
| 260 | CTK_WIDGET_ADD(&window, &urltextentry); |
| 261 | |
| 262 | CTK_WIDGET_ADD(&window, &savefilenamelabel); |
| 263 | CTK_WIDGET_ADD(&window, &savefilenametextentry); |
| 264 | |
| 265 | /* CTK_WIDGET_ADD(&window, &filebutton);*/ |
| 266 | |
| 267 | CTK_WIDGET_ADD(&window, &d64button); |
| 268 | |
| 269 | CTK_WIDGET_ADD(&window, &statustext); |
| 270 | |
| 271 | dload_state = DLOAD_NONE; |
| 272 | |
| 273 | memset(savefilename, 0, sizeof(savefilename)); |
| 274 | memset(url, 0, sizeof(url)); |
| 275 | |
| 276 | ctk_dialog_new(&d64dialog, 36, 8); |
| 277 | CTK_WIDGET_ADD(&d64dialog, &overwritelabel); |
| 278 | CTK_WIDGET_ADD(&d64dialog, &makesurelabel1); |
| 279 | CTK_WIDGET_ADD(&d64dialog, &makesurelabel2); |
| 280 | CTK_WIDGET_ADD(&d64dialog, &overwritebutton); |
| 281 | CTK_WIDGET_ADD(&d64dialog, &cancelbutton); |
| 282 | |
| 283 | |
| 284 | ctk_window_open(&window); |
| 285 | } else if(ev == tcpip_event) { |
| 286 | webclient_appcall(data); |
| 287 | } else if(ev == ctk_signal_button_activate) { |
| 288 | if(data == (void *)&filebutton) { |
| 289 | /* ret = cbm_open(2, 8, 2, savefilename); |
| 290 | if(ret == -1) {*/ |
| 291 | sprintf(statusmsg, "Open error with '%s'", savefilename); |
| 292 | show_statustext(statusmsg); |
| 293 | /* } else { |
| 294 | strncpy(url, urledit, sizeof(url)); |
| 295 | petsciiconv_toascii(url, sizeof(url)); |
| 296 | start_get(); |
| 297 | dload_bytes = 0; |
| 298 | dload_state = DLOAD_FILE; |
| 299 | }*/ |
| 300 | } else if(data == (void *)&d64button) { |
| 301 | ctk_dialog_open(&d64dialog); |
| 302 | } else if(data == (void *)&cancelbutton) { |
| 303 | ctk_dialog_close(); |
| 304 | } else if(data == (void *)&overwritebutton) { |
| 305 | ctk_dialog_close(); |
| 306 | |
| 307 | /* Turn of screensaver. */ |
| 308 | program_handler_screensaver(NULL); |
| 309 | |
| 310 | strncpy(url, urledit, sizeof(url)); |
| 311 | petsciiconv_toascii(url, sizeof(url)); |
| 312 | start_get(); |
| 313 | dload_bytes = 0; |
| 314 | dload_state = DLOAD_D64; |
| 315 | ds.track = 1; |
| 316 | ds.sect = 0; |
| 317 | bufferptr = 0; |
| 318 | #if USE_KERNAL |
| 319 | #else |
| 320 | c64_dio_init(_curunit); |
| 321 | #endif |
| 322 | |
| 323 | /* c64_dio_init(8);*/ |
| 324 | } |
| 325 | } else if(ev == ctk_signal_hyperlink_activate) { |
| 326 | if(dload_state == DLOAD_NONE) { |
| 327 | /* open_link(w->widget.hyperlink.url);*/ |
| 328 | strncpy(urledit, |
| 329 | ((struct ctk_widget *)data)->widget.hyperlink.url, sizeof(urledit)); |
| 330 | petsciiconv_topetscii(urledit, sizeof(urledit)); |
| 331 | CTK_WIDGET_REDRAW(&urltextentry); |
| 332 | CTK_WIDGET_FOCUS(&window, &urltextentry); |
| 333 | } |
| 334 | } else if(ev == resolv_event_found) { |
| 335 | /* Either found a hostname, or not. */ |
| 336 | if((char *)data != NULL && |
| 337 | resolv_lookup((char *)data) != NULL) { |
| 338 | start_get(); |
| 339 | } else { |
| 340 | show_statustext("Host not found."); |
| 341 | } |
| 342 | } else if(ev == ctk_signal_window_close) { |
| 343 | ek_exit(); |
| 344 | id = EK_ID_NONE; |
| 345 | LOADER_UNLOAD(); |
| 346 | } |
| 347 | } |
| 348 | /*-----------------------------------------------------------------------------------*/ |
| 349 | /* webclient_aborted(): |
| 350 | * |
| 351 | * Callback function. Called from the webclient when the HTTP |
| 352 | * connection was abruptly aborted. |
| 353 | */ |
| 354 | void |
| 355 | webclient_aborted(void) |
| 356 | { |
| 357 | show_statustext("Connection reset by peer"); |
| 358 | } |
| 359 | /*-----------------------------------------------------------------------------------*/ |
| 360 | /* webclient_timedout(): |
| 361 | * |
| 362 | * Callback function. Called from the webclient when the HTTP |
| 363 | * connection timed out. |
| 364 | */ |
| 365 | void |
| 366 | webclient_timedout(void) |
| 367 | { |
| 368 | show_statustext("Connection timed out"); |
| 369 | if(dload_state == DLOAD_FILE) { |
| 370 | cbm_close(2); |
| 371 | } |
| 372 | |
| 373 | } |
| 374 | /*-----------------------------------------------------------------------------------*/ |
| 375 | /* webclient_closed(): |
| 376 | * |
| 377 | * Callback function. Called from the webclient when the HTTP |
| 378 | * connection was closed after a request from the "webclient_close()" |
| 379 | * function. . |
| 380 | */ |
| 381 | void |
| 382 | webclient_closed(void) |
| 383 | { |
| 384 | show_statustext("Done."); |
| 385 | } |
| 386 | /*-----------------------------------------------------------------------------------*/ |
| 387 | /* webclient_closed(): |
| 388 | * |
| 389 | * Callback function. Called from the webclient when the HTTP |
| 390 | * connection is connected. |
| 391 | */ |
| 392 | void |
| 393 | webclient_connected(void) |
| 394 | { |
| 395 | show_statustext("Request sent..."); |
| 396 | } |
| 397 | /*-----------------------------------------------------------------------------------*/ |
| 398 | static u8_t |
| 399 | next_sector(void) |
| 400 | { |
| 401 | ++ds.sect; |
| 402 | if(ds.track < 18) { |
| 403 | if(ds.sect == 21) { |
| 404 | ++ds.track; |
| 405 | ds.sect = 0; |
| 406 | } |
| 407 | } else if(ds.track < 25) { |
| 408 | if(ds.sect == 19) { |
| 409 | ++ds.track; |
| 410 | ds.sect = 0; |
| 411 | } |
| 412 | } else if(ds.track < 31) { |
| 413 | if(ds.sect == 18) { |
| 414 | ++ds.track; |
| 415 | ds.sect = 0; |
| 416 | } |
| 417 | } else if(ds.track < 36) { |
| 418 | if(ds.sect == 17) { |
| 419 | ++ds.track; |
| 420 | ds.sect = 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if(ds.track == 36) { |
| 425 | return 1; |
| 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | /*-----------------------------------------------------------------------------------*/ |
| 430 | static void |
| 431 | x_open(u8_t f, u8_t d, u8_t cmd, u8_t *fname) |
| 432 | { |
| 433 | u8_t ret; |
| 434 | |
| 435 | ret = cbm_open(f, d, cmd, fname); |
| 436 | if(ret != 0) { |
| 437 | /* printf("open: error %d\n", ret);*/ |
| 438 | /* ctk_label_set_text(&statuslabel, "Open err"); |
| 439 | CTK_WIDGET_REDRAW(&statuslabel);*/ |
| 440 | show_statustext("Open error"); |
| 441 | } |
| 442 | |
| 443 | } |
| 444 | static void |
| 445 | write_sector(u8_t device, u8_t track, u8_t sect, void *mem) |
| 446 | { |
| 447 | u16_t ret; |
| 448 | static u8_t cmd[32]; |
| 449 | |
| 450 | x_open(15, device, 15, NULL); |
| 451 | x_open(2, device, 2, "#"); |
| 452 | |
| 453 | ret = cbm_write(2, mem, 256); |
| 454 | |
| 455 | sprintf(cmd, "u2: 2 0 %d %d", track, sect); |
| 456 | cbm_write(15, cmd, strlen(cmd)); |
| 457 | /* printf("%s\n", cmd);*/ |
| 458 | |
| 459 | |
| 460 | /* ret = 0;*/ |
| 461 | if(ret == -1) { |
| 462 | sprintf(statusmsg, "Write error at %d:%d", track, sect); |
| 463 | show_statustext(statusmsg); |
| 464 | } else { |
| 465 | sprintf(statusmsg, "Wrote %d bytes to %d:%d", ret, track, sect); |
| 466 | show_statustext(statusmsg); |
| 467 | } |
| 468 | /* printf("write: wrote %d bytes\n", ret);*/ |
| 469 | |
| 470 | cbm_close(2); |
| 471 | cbm_close(15); |
| 472 | } |
| 473 | |
| 474 | static void |
| 475 | write_buffer(void) |
| 476 | { |
| 477 | #if USE_KERNAL |
| 478 | write_sector(8, ds.track, ds.sect, buffer); |
| 479 | #else |
| 480 | c64_dio_write_block(ds.track, ds.sect, buffer); |
| 481 | #endif |
| 482 | if(next_sector() != 0) { |
| 483 | dload_state = DLOAD_NONE; |
| 484 | } |
| 485 | } |
| 486 | static void |
| 487 | handle_d64_data(char *data, u16_t len) |
| 488 | { |
| 489 | u16_t bufferlen; |
| 490 | |
| 491 | while(dload_state == DLOAD_D64 && |
| 492 | len > 0) { |
| 493 | bufferlen = sizeof(buffer) - bufferptr; |
| 494 | if(len < bufferlen) { |
| 495 | bufferlen = len; |
| 496 | } |
| 497 | |
| 498 | memcpy(&buffer[bufferptr], data, bufferlen); |
| 499 | |
| 500 | data += bufferlen; |
| 501 | bufferptr += bufferlen; |
| 502 | len -= bufferlen; |
| 503 | |
| 504 | if(bufferptr == sizeof(buffer)) { |
| 505 | write_buffer(); |
| 506 | bufferptr = 0; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | /*-----------------------------------------------------------------------------------*/ |
| 511 | /* webclient_datahandler(): |
| 512 | * |
| 513 | * Callback function. Called from the webclient module when HTTP data |
| 514 | * has arrived. |
| 515 | */ |
| 516 | void |
| 517 | webclient_datahandler(char *data, u16_t len) |
| 518 | { |
| 519 | int ret; |
| 520 | |
| 521 | if(len > 0) { |
| 522 | dload_bytes += len; |
| 523 | sprintf(statusmsg, "Downloading (%lu bytes)", dload_bytes); |
| 524 | show_statustext(statusmsg); |
| 525 | if(dload_state == DLOAD_D64) { |
| 526 | handle_d64_data(data, len); |
| 527 | } else if(dload_state == DLOAD_FILE) { |
| 528 | ret = cbm_write(2, data, len); |
| 529 | if(ret != len) { |
| 530 | sprintf(statusmsg, "Wrote only %d bytes", ret); |
| 531 | show_statustext(statusmsg); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if(data == NULL) { |
| 537 | if(dload_state == DLOAD_FILE) { |
| 538 | cbm_close(2); |
| 539 | } |
| 540 | dload_state = DLOAD_NONE; |
| 541 | sprintf(statusmsg, "Finished downloading %lu bytes", dload_bytes); |
| 542 | show_statustext(statusmsg); |
| 543 | } |
| 544 | } |
| 545 | /*-----------------------------------------------------------------------------------*/ |