blob: 63b4def8b60b1521fb9fc2d7ae3b62f0c16c70ff [file] [log] [blame]
kthacker6de67752006-04-17 15:02:26 +00001/*
2 * Copyright (c) 2004, 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 copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * This file is part of the Contiki OS
30 *
31 * $Id: slip-dump-drv.c,v 1.1 2006/04/17 15:02:42 kthacker Exp $
32 *
33 */
34
35#include "contiki.h"
36#include "rs232dev.h"
37
38#include "packet-service.h"
39
40#include "tcpdump.h"
41#include <string.h>
42#include "ctk.h"
43
44static void output(u8_t *hdr, u16_t hdrlen, u8_t *data, u16_t datalen);
45
46static const struct packet_service_state state =
47 {
48 PACKET_SERVICE_VERSION,
49 output
50 };
51
52EK_EVENTHANDLER(eventhandler, ev, data);
53EK_POLLHANDLER(pollhandler);
54EK_PROCESS(proc, PACKET_SERVICE_NAME ": SLIP", EK_PRIO_HIGH,
55 eventhandler, pollhandler, (void *)&state);
56
57#define DUMP_WIDTH 38
58#define DUMP_HEIGHT 20
59static struct ctk_window window;
60static char dump[DUMP_WIDTH * DUMP_HEIGHT];
61static struct ctk_label dumplabel =
62 {CTK_LABEL(0, 0, DUMP_WIDTH, DUMP_HEIGHT, dump)};
63static void
64dump_packet(void)
65{
66 memcpy(dump, &dump[DUMP_WIDTH], DUMP_WIDTH * (DUMP_HEIGHT - 1));
67 tcpdump_print(&dump[DUMP_WIDTH * (DUMP_HEIGHT - 1)], DUMP_WIDTH);
68 CTK_WIDGET_REDRAW(&dumplabel);
69}
70/*---------------------------------------------------------------------------*/
71LOADER_INIT_FUNC(slip_service_init, arg)
72{
73 arg_free(arg);
74 ek_service_start(PACKET_SERVICE_NAME, &proc);
75}
76/*---------------------------------------------------------------------------*/
77static void
78output(u8_t *hdr, u16_t hdrlen, u8_t *data, u16_t datalen)
79{
80 rs232dev_send();
81 dump_packet();
82}
83/*---------------------------------------------------------------------------*/
84EK_EVENTHANDLER(eventhandler, ev, data)
85{
86 switch(ev) {
87 case EK_EVENT_INIT:
88 case EK_EVENT_REPLACE:
89 ctk_window_new(&window, DUMP_WIDTH, DUMP_HEIGHT, "SLIP dump");
90 CTK_WIDGET_ADD(&window, &dumplabel);
91 ctk_window_open(&window);
92 rs232dev_init();
93 break;
94 case EK_EVENT_REQUEST_REPLACE:
95 ctk_window_close(&window);
96 ek_replace((struct ek_proc *)data, NULL);
97 rs232dev_unload();
98 LOADER_UNLOAD();
99 break;
100 case EK_EVENT_REQUEST_EXIT:
101 ctk_window_close(&window);
102 ek_exit();
103 rs232dev_unload();
104 LOADER_UNLOAD();
105 break;
106 default:
107 break;
108 }
109}
110/*---------------------------------------------------------------------------*/
111EK_POLLHANDLER(pollhandler)
112{
113 uip_len = rs232dev_poll();
114 if(uip_len > 0) {
115 dump_packet();
116 tcpip_input();
117 }
118
119}
120/*---------------------------------------------------------------------------*/