Subversion Repositories shark

Rev

Rev 1191 | Details | Compare with Previous | Last modification | View Log | RSS feed

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