blob: fb499f3d172d97990797939f50b0081c7d5e992c [file] [log] [blame]
adamdunkels92622ca2003-07-30 23:10:49 +00001/*
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.
adamdunkels86ec98f2004-09-01 20:48:04 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkels92622ca2003-07-30 23:10:49 +000015 * 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 *
adamdunkels642ee782004-09-19 15:32:38 +000032 * $Id: wget.c,v 1.12 2004/09/19 15:32:38 adamdunkels Exp $
adamdunkels92622ca2003-07-30 23:10:49 +000033 *
34 */
35
36
37#include "ctk.h"
adamdunkels0a08fda2004-07-04 18:33:07 +000038#include "ek.h"
adamdunkels92622ca2003-07-30 23:10:49 +000039#include "webclient.h"
40#include "resolv.h"
41#include "petsciiconv.h"
adamdunkels2658b902004-02-24 09:53:44 +000042#include "uiplib.h"
adamdunkels92622ca2003-07-30 23:10:49 +000043#include "loader.h"
44
adamdunkels0a08fda2004-07-04 18:33:07 +000045#include "contiki.h"
46
adamdunkels2c7b6d52003-08-15 18:45:10 +000047#include "program-handler.h"
48
adamdunkels92622ca2003-07-30 23:10:49 +000049#include <c64.h>
50#include <cbm.h>
51#include <stdio.h>
52#include <string.h>
53#include <fcntl.h>
54
adamdunkels0dc09e42003-08-04 00:13:47 +000055#include "c64-dio.h"
adamdunkels92622ca2003-07-30 23:10:49 +000056
adamdunkels642ee782004-09-19 15:32:38 +000057#define USE_KERNAL 0
58
adamdunkels92622ca2003-07-30 23:10:49 +000059static struct ctk_window window;
60
adamdunkels0dc09e42003-08-04 00:13:47 +000061static struct ctk_label urllabel =
62 {CTK_LABEL(0, 1, 4, 1, "URL:")};
63static char url[80];
64static char urledit[80];
65struct ctk_textentry urltextentry =
66 {CTK_TEXTENTRY(5, 1, 29, 1, urledit, 78)};
67
adamdunkels92622ca2003-07-30 23:10:49 +000068
69static struct ctk_label savefilenamelabel =
adamdunkels0dc09e42003-08-04 00:13:47 +000070 {CTK_LABEL(0, 3, 14, 1, "Save filename:")};
adamdunkels92622ca2003-07-30 23:10:49 +000071static char savefilename[40];
72static struct ctk_textentry savefilenametextentry =
adamdunkels0dc09e42003-08-04 00:13:47 +000073 {CTK_TEXTENTRY(15, 3, 19, 1, savefilename, 38)};
adamdunkels92622ca2003-07-30 23:10:49 +000074
75static struct ctk_button filebutton =
adamdunkels0dc09e42003-08-04 00:13:47 +000076 {CTK_BUTTON(0, 5, 13, "Download file")};
adamdunkels92622ca2003-07-30 23:10:49 +000077
78static struct ctk_button d64button =
adamdunkels0dc09e42003-08-04 00:13:47 +000079 {CTK_BUTTON(17, 5, 18, "Download D64 disk")};
adamdunkels92622ca2003-07-30 23:10:49 +000080
81static struct ctk_label statustext =
adamdunkels0dc09e42003-08-04 00:13:47 +000082 {CTK_LABEL(0, 7, 36, 1, "")};
adamdunkels92622ca2003-07-30 23:10:49 +000083static char statusmsg[40];
84
85static struct ctk_window d64dialog;
86static struct ctk_label overwritelabel =
87 {CTK_LABEL(0, 1, 36, 1, "This will overwrite the entire disk!")};
88static struct ctk_label makesurelabel1 =
89 {CTK_LABEL(7, 3, 22, 1, "Make sure you have the")};
90static struct ctk_label makesurelabel2 =
91 {CTK_LABEL(6, 4, 24, 1, "right disk in the drive!")};
92static struct ctk_button overwritebutton =
93 {CTK_BUTTON(2, 6, 14, "Overwrite disk")};
94static struct ctk_button cancelbutton =
95 {CTK_BUTTON(26, 6, 6, "Cancel")};
96
adamdunkels0a08fda2004-07-04 18:33:07 +000097/*static DISPATCHER_SIGHANDLER(wget_sighandler, s, data);
adamdunkels92622ca2003-07-30 23:10:49 +000098static struct dispatcher_proc p =
99 {DISPATCHER_PROC("Web downloader", NULL, wget_sighandler, webclient_appcall)};
adamdunkels0a08fda2004-07-04 18:33:07 +0000100 static ek_id_t id;*/
101
102EK_EVENTHANDLER(wget_eventhandler, ev, data);
103EK_PROCESS(p, "Web downloader", EK_PRIO_NORMAL,
104 wget_eventhandler, NULL, NULL);
105static ek_id_t id = EK_ID_NONE;
adamdunkels92622ca2003-07-30 23:10:49 +0000106
107/* State */
108
109#define DLOAD_NONE 0
110#define DLOAD_FILE 1
111#define DLOAD_D64 2
112static u8_t dload_state;
113static unsigned long dload_bytes;
114
115
116
117struct drv_state {
118 u8_t track;
119 u8_t sect;
120};
121
122static struct drv_state ds;
123
124static char buffer[256];
125static 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 */
adamdunkels7cde6092003-08-24 22:35:22 +0000133LOADER_INIT_FUNC(wget_init, arg)
adamdunkels92622ca2003-07-30 23:10:49 +0000134{
adamdunkels7cde6092003-08-24 22:35:22 +0000135 if(arg != NULL) {
136 strncpy(url, arg, sizeof(url));
137 strncpy(urledit, arg, sizeof(urledit));
138 petsciiconv_topetscii(urledit, sizeof(urledit));
adamdunkels642ee782004-09-19 15:32:38 +0000139 } 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 */
adamdunkels7cde6092003-08-24 22:35:22 +0000144 }
145 arg_free(arg);
146
adamdunkels92622ca2003-07-30 23:10:49 +0000147 if(id == EK_ID_NONE) {
adamdunkels0a08fda2004-07-04 18:33:07 +0000148 id = ek_start(&p);
adamdunkels92622ca2003-07-30 23:10:49 +0000149
adamdunkels92622ca2003-07-30 23:10:49 +0000150 }
adamdunkels92622ca2003-07-30 23:10:49 +0000151}
152/*-----------------------------------------------------------------------------------*/
153static void
154show_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 */
166static void
167start_get(void)
168{
169 u16_t addr[2];
adamdunkels0dc09e42003-08-04 00:13:47 +0000170 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
adamdunkels92622ca2003-07-30 23:10:49 +0000225
226 /* First check if the host is an IP address. */
adamdunkels2658b902004-02-24 09:53:44 +0000227 if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) {
adamdunkels92622ca2003-07-30 23:10:49 +0000228
229 /* Try to lookup the hostname. If it fails, we initiate a hostname
adamdunkels0dc09e42003-08-04 00:13:47 +0000230 lookup and print out an informative message on the
231 statusbar. */
adamdunkels92622ca2003-07-30 23:10:49 +0000232 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/*-----------------------------------------------------------------------------------*/
adamdunkels0a08fda2004-07-04 18:33:07 +0000248EK_EVENTHANDLER(wget_eventhandler, ev, data)
adamdunkels92622ca2003-07-30 23:10:49 +0000249{
250 int ret;
251 static unsigned char i;
adamdunkels0a08fda2004-07-04 18:33:07 +0000252 EK_EVENTHANDLER_ARGS(ev, data);
adamdunkels92622ca2003-07-30 23:10:49 +0000253
adamdunkels0a08fda2004-07-04 18:33:07 +0000254 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) {
adamdunkels92622ca2003-07-30 23:10:49 +0000288 if(data == (void *)&filebutton) {
adamdunkelsf60873b2003-08-29 20:33:32 +0000289 /* ret = cbm_open(2, 8, 2, savefilename);
290 if(ret == -1) {*/
adamdunkels92622ca2003-07-30 23:10:49 +0000291 sprintf(statusmsg, "Open error with '%s'", savefilename);
292 show_statustext(statusmsg);
adamdunkelsf60873b2003-08-29 20:33:32 +0000293 /* } else {
adamdunkels0dc09e42003-08-04 00:13:47 +0000294 strncpy(url, urledit, sizeof(url));
295 petsciiconv_toascii(url, sizeof(url));
adamdunkels92622ca2003-07-30 23:10:49 +0000296 start_get();
297 dload_bytes = 0;
298 dload_state = DLOAD_FILE;
adamdunkelsf60873b2003-08-29 20:33:32 +0000299 }*/
adamdunkels92622ca2003-07-30 23:10:49 +0000300 } 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();
adamdunkels2c7b6d52003-08-15 18:45:10 +0000306
307 /* Turn of screensaver. */
308 program_handler_screensaver(NULL);
309
adamdunkels0dc09e42003-08-04 00:13:47 +0000310 strncpy(url, urledit, sizeof(url));
311 petsciiconv_toascii(url, sizeof(url));
adamdunkels92622ca2003-07-30 23:10:49 +0000312 start_get();
313 dload_bytes = 0;
314 dload_state = DLOAD_D64;
315 ds.track = 1;
316 ds.sect = 0;
317 bufferptr = 0;
adamdunkels642ee782004-09-19 15:32:38 +0000318 /* c64_dio_init(_curunit);*/
adamdunkels0dc09e42003-08-04 00:13:47 +0000319 /* c64_dio_init(8);*/
320 }
adamdunkels0a08fda2004-07-04 18:33:07 +0000321 } else if(ev == ctk_signal_hyperlink_activate) {
adamdunkels0dc09e42003-08-04 00:13:47 +0000322 if(dload_state == DLOAD_NONE) {
323 /* open_link(w->widget.hyperlink.url);*/
324 strncpy(urledit,
325 ((struct ctk_widget *)data)->widget.hyperlink.url, sizeof(urledit));
326 petsciiconv_topetscii(urledit, sizeof(urledit));
327 CTK_WIDGET_REDRAW(&urltextentry);
328 CTK_WIDGET_FOCUS(&window, &urltextentry);
adamdunkels92622ca2003-07-30 23:10:49 +0000329 }
adamdunkels0a08fda2004-07-04 18:33:07 +0000330 } else if(ev == resolv_event_found) {
adamdunkels92622ca2003-07-30 23:10:49 +0000331 /* Either found a hostname, or not. */
332 if((char *)data != NULL &&
333 resolv_lookup((char *)data) != NULL) {
334 start_get();
335 } else {
336 show_statustext("Host not found.");
337 }
adamdunkels0a08fda2004-07-04 18:33:07 +0000338 } else if(ev == ctk_signal_window_close) {
339 ek_exit();
adamdunkels92622ca2003-07-30 23:10:49 +0000340 id = EK_ID_NONE;
341 LOADER_UNLOAD();
342 }
343}
344/*-----------------------------------------------------------------------------------*/
345/* webclient_aborted():
346 *
347 * Callback function. Called from the webclient when the HTTP
348 * connection was abruptly aborted.
349 */
350void
351webclient_aborted(void)
352{
353 show_statustext("Connection reset by peer");
354}
355/*-----------------------------------------------------------------------------------*/
356/* webclient_timedout():
357 *
358 * Callback function. Called from the webclient when the HTTP
359 * connection timed out.
360 */
361void
362webclient_timedout(void)
363{
364 show_statustext("Connection timed out");
365 if(dload_state == DLOAD_FILE) {
366 cbm_close(2);
367 }
368
369}
370/*-----------------------------------------------------------------------------------*/
371/* webclient_closed():
372 *
373 * Callback function. Called from the webclient when the HTTP
374 * connection was closed after a request from the "webclient_close()"
375 * function. .
376 */
377void
378webclient_closed(void)
379{
380 show_statustext("Done.");
381}
382/*-----------------------------------------------------------------------------------*/
383/* webclient_closed():
384 *
385 * Callback function. Called from the webclient when the HTTP
386 * connection is connected.
387 */
388void
389webclient_connected(void)
390{
391 show_statustext("Request sent...");
392}
393/*-----------------------------------------------------------------------------------*/
adamdunkels92622ca2003-07-30 23:10:49 +0000394static u8_t
395next_sector(void)
396{
397 ++ds.sect;
398 if(ds.track < 18) {
399 if(ds.sect == 21) {
400 ++ds.track;
401 ds.sect = 0;
402 }
403 } else if(ds.track < 25) {
404 if(ds.sect == 19) {
405 ++ds.track;
406 ds.sect = 0;
407 }
408 } else if(ds.track < 31) {
409 if(ds.sect == 18) {
410 ++ds.track;
411 ds.sect = 0;
412 }
413 } else if(ds.track < 36) {
414 if(ds.sect == 17) {
415 ++ds.track;
416 ds.sect = 0;
417 }
418 }
419
420 if(ds.track == 36) {
421 return 1;
422 }
423 return 0;
424}
425/*-----------------------------------------------------------------------------------*/
426static void
adamdunkels642ee782004-09-19 15:32:38 +0000427x_open(u8_t f, u8_t d, u8_t cmd, u8_t *fname)
428{
429 u8_t ret;
430
431 ret = cbm_open(f, d, cmd, fname);
432 if(ret != 0) {
433 /* printf("open: error %d\n", ret);*/
434 /* ctk_label_set_text(&statuslabel, "Open err");
435 CTK_WIDGET_REDRAW(&statuslabel);*/
436 show_statustext("Open error");
437 }
438
439}
440static void
441write_sector(u8_t device, u8_t track, u8_t sect, void *mem)
442{
443 u16_t ret;
444 static u8_t cmd[32];
445
446 x_open(15, device, 15, NULL);
447 x_open(2, device, 2, "#");
448
449 ret = cbm_write(2, mem, 256);
450
451 sprintf(cmd, "u2: 2 0 %d %d", track, sect);
452 cbm_write(15, cmd, strlen(cmd));
453 /* printf("%s\n", cmd);*/
454
455
456 /* ret = 0;*/
457 if(ret == -1) {
458 sprintf(statusmsg, "Write error at %d:%d", track, sect);
459 show_statustext(statusmsg);
460 } else {
461 sprintf(statusmsg, "Wrote %d bytes to %d:%d", ret, track, sect);
462 show_statustext(statusmsg);
463 }
464 /* printf("write: wrote %d bytes\n", ret);*/
465
466 cbm_close(2);
467 cbm_close(15);
468}
469
470static void
adamdunkels92622ca2003-07-30 23:10:49 +0000471write_buffer(void)
472{
adamdunkels642ee782004-09-19 15:32:38 +0000473#if USE_KERNAL
474 write_sector(8, ds.track, ds.sect, buffer);
475#else
adamdunkels0dc09e42003-08-04 00:13:47 +0000476 c64_dio_write_block(ds.track, ds.sect, buffer);
adamdunkels642ee782004-09-19 15:32:38 +0000477#endif
adamdunkels92622ca2003-07-30 23:10:49 +0000478 if(next_sector() != 0) {
479 dload_state = DLOAD_NONE;
480 }
481}
482static void
483handle_d64_data(char *data, u16_t len)
484{
485 u16_t bufferlen;
486
487 while(dload_state == DLOAD_D64 &&
488 len > 0) {
489 bufferlen = sizeof(buffer) - bufferptr;
490 if(len < bufferlen) {
491 bufferlen = len;
492 }
493
494 memcpy(&buffer[bufferptr], data, bufferlen);
495
496 data += bufferlen;
497 bufferptr += bufferlen;
498 len -= bufferlen;
499
500 if(bufferptr == sizeof(buffer)) {
501 write_buffer();
502 bufferptr = 0;
503 }
504 }
505}
506/*-----------------------------------------------------------------------------------*/
507/* webclient_datahandler():
508 *
509 * Callback function. Called from the webclient module when HTTP data
510 * has arrived.
511 */
512void
513webclient_datahandler(char *data, u16_t len)
514{
515 int ret;
516
517 if(len > 0) {
518 dload_bytes += len;
519 sprintf(statusmsg, "Downloading (%lu bytes)", dload_bytes);
520 show_statustext(statusmsg);
521 if(dload_state == DLOAD_D64) {
522 handle_d64_data(data, len);
523 } else if(dload_state == DLOAD_FILE) {
524 ret = cbm_write(2, data, len);
525 if(ret != len) {
526 sprintf(statusmsg, "Wrote only %d bytes", ret);
527 show_statustext(statusmsg);
528 }
529 }
530 }
531
532 if(data == NULL) {
533 if(dload_state == DLOAD_FILE) {
534 cbm_close(2);
535 }
536 dload_state = DLOAD_NONE;
537 sprintf(statusmsg, "Finished downloading %lu bytes", dload_bytes);
538 show_statustext(statusmsg);
539 }
540}
541/*-----------------------------------------------------------------------------------*/