Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: HARTIK (HA-rd R-eal TI-me K-ernel)
3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *               Gerardo Lamastra <gerardo@sssup.it>
6
 *
7
 * Authors     : Paolo Gai <pj@hartik.sssup.it>
8
 * (see authors.txt for full list of hartik's authors)
9
 *
10
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
11
 *
12
 * http://www.sssup.it
13
 * http://retis.sssup.it
14
 * http://hartik.sssup.it
15
 */
16
 
17
/**
18
 ------------
19
 CVS :        $Id: testp.c,v 1.1.1.1 2002-09-02 09:37:48 pj Exp $
20
 
21
 File:        $File$
22
 Revision:    $Revision: 1.1.1.1 $
23
 Last update: $Date: 2002-09-02 09:37:48 $
24
 ------------
25
 
26
 Test 25 (P):
27
 
28
 This is the talkdx.c Hartik's example.
29
 
30
 File:        Talk.C
31
 Revision:    1.00
32
 Author:      Luca Abeni
33
 
34
 
35
   Simple Netlib demo: nothing of seriously real-time, only another Unix
36
   Talk clone.
37
   Read it to see how the UDP/IP layers of the networ library work.
38
 
39
**/
40
 
41
/*
42
 * Copyright (C) 2000 Paolo Gai
43
 *
44
 * This program is free software; you can redistribute it and/or modify
45
 * it under the terms of the GNU General Public License as published by
46
 * the Free Software Foundation; either version 2 of the License, or
47
 * (at your option) any later version.
48
 *
49
 * This program is distributed in the hope that it will be useful,
50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52
 * GNU General Public License for more details.
53
 *
54
 * You should have received a copy of the GNU General Public License
55
 * along with this program; if not, write to the Free Software
56
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
57
 *
58
 */
59
 
60
#include <kernel/kern.h>
61
//#include <mem.h>
62
//#include <stdlib.h>
63
#include <string.h>
64
//#include <cons.h>
65
#include <drivers/crtwin.h>
66
#include <drivers/keyb.h>
67
 
68
#include <drivers/udpip.h>
69
 
70
 
71
WIN dbg;
72
BYTE esc = FALSE;
73
 
74
char talk_myipaddr[20];
75
char talk_toipaddr[20];
76
 
77
/*
78
   This non real-time task reads UDP packets from the network and writes
79
   them in a window
80
*/
81
TASK scrittore(void)
82
{
83
    char str[2000];
84
    UDP_ADDR from, local;
85
    WIN displ;
86
    int s,n;
87
 
88
    /* Connect on the local port #100 */
89
    local.s_port = 100;
90
    s = udp_bind(&local, NULL);
91
 
92
    /* Open the window */
93
    win_init(&displ,0,0,79,6);
94
    win_frame(&displ,BLACK,WHITE,"Remote",2);
95
 
96
    while (1) {
97
        /* Clear the buffer for receiving the packet...*/
98
        memset(str, 0, 1999);
99
        /*...and receive the packet (block until a packet arrives */
100
        n = udp_recvfrom(s, str, &from);
101
        win_puts(&displ, str);
102
    }
103
}
104
 
105
/*
106
   This non real-time task reads strings from the keyoard and sends them
107
   to the remote host
108
*/
109
TASK write(void)
110
{
111
    WIN wr;
112
    UDP_ADDR to,local;
113
    char str[80];
114
    int s;
115
    IP_ADDR bindlist[5];
116
 
117
    /* Create a socket for transitting */
118
    ip_str2addr(talk_myipaddr,&(local.s_addr));
119
    local.s_port = 101;
120
 
121
    /*
122
       If we want the address of the remote host in the ARP table before
123
       begginning the transmission (to eliminate a possible source of
124
       unpredictability), we can use the bindlist, otherwise we set the
125
       second parameter of udp_bind to NULL
126
    */
127
    ip_str2addr(talk_toipaddr,&(bindlist[0]));
128
    memset(&(bindlist[1]), 0, sizeof(IP_ADDR));
129
    s = udp_bind(&local, /*bindlist*/NULL);
130
 
131
    win_init(&wr,0,7,79,6);
132
    win_frame(&wr,BLACK,WHITE,"Local",2);
133
    ip_str2addr(talk_toipaddr,&(to.s_addr));
134
    to.s_port = 100;
135
    sprintf(str,"Local IP address %d.%d.%d.%d\n", local.s_addr.ad[0],
136
                                local.s_addr.ad[1], local.s_addr.ad[2],
137
                                local.s_addr.ad[3]);
138
    win_puts(&dbg,str);
139
    sprintf(str,"Talk to %d.%d.%d.%d   ",to.s_addr.ad[0],to.s_addr.ad[1],
140
                                to.s_addr.ad[2],to.s_addr.ad[3]);
141
    win_puts(&dbg,str);
142
    while (1) {
143
        /* Get the string...*/
144
        win_gets(&wr,str,78);
145
        strcat(str,"\n");
146
        /*...and send it!!! */
147
        udp_sendto(s,str,strlen(str)+2,&to);
148
    }
149
}
150
 
151
/* This function is called when the user presses CTRL-C (stops the systems) */
152
void esci(KEY_EVT *k)
153
{
154
    esc = TRUE;
155
    cprintf("Ctrl-Brk pressed!\n");
156
    //sys_status(0xFFFF);
157
    sys_abort(500);
158
    //exit(1);
159
}
160
 
161
int main(void)
162
{
163
    KEY_EVT k;
164
 
165
    struct net_model m = net_base;
166
 
167
    NRT_TASK_MODEL m_nrt;
168
 
169
    k.flag = CNTL_BIT;
170
    k.scan = KEY_C;  
171
    k.ascii = 'c';
172
    keyb_hook(k,esci);
173
    k.flag = CNTR_BIT;
174
    k.scan = KEY_C;  
175
    k.ascii = 'c';
176
    keyb_hook(k,esci);
177
 
178
    clear();
179
    cprintf(" Hartik Talk!                      Ver. 1.00\n");
180
//    strcpy(talk_myipaddr, "193.205.82.46");
181
//    strcpy(talk_toipaddr, "193.205.82.44");
182
 
183
    strcpy(talk_myipaddr, "193.205.82.44");
184
    strcpy(talk_toipaddr, "193.205.82.46");
185
 
186
    /* We want a task for TX mutual exclusion */
187
    net_setmode(m, TXTASK);
188
    /* We use UDP/IP stack */
189
    net_setudpip(m, talk_myipaddr);
190
    /* OK: let's start the NetLib! */
191
    if (net_init(&m) == 1) {
192
                cprintf("Net Init OK...\n");
193
        } else {
194
                cprintf("Net Init Failed...\n");
195
                sys_abort(300);
196
        }
197
 
198
 
199
//dump_irq();
200
 
201
    cprintf("\n\n\n\tPress ENTER\n");
202
    while (!esc) {
203
        keyb_getcode(&k,BLOCK);
204
        if (k.ascii == 13) esc = TRUE;
205
    }
206
 
207
    esc = FALSE;
208
    clear();
209
    win_init(&dbg,0,20,60,3);
210
    win_frame(&dbg,BLACK,WHITE,"Debug",2);
211
 
212
    /* Start the sender and receiver tasks...*/
213
    //task_nopreempt();
214
    nrt_task_default_model(m_nrt);
215
    task_activate(task_create("aaa",scrittore,&m_nrt,NULL));
216
    task_activate(task_create("bbb",write,&m_nrt,NULL));
217
 
218
    //clear();
219
    //task_preempt();
220
 
221
    /*...and wait!!! */
222
    while (!esc) {
223
    }
224
    sys_end();
225
    //clear();
226
    return 0;
227
}