Subversion Repositories shark

Rev

Rev 1406 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1405 giacomo 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/shark_keyb26.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],tmp[5],ch;
104
  int s;
105
  IP_ADDR bindlist[5];
106
 
107
  win_init(&wr,0,7,79,6);
108
  win_frame(&wr,BLACK,WHITE,"Local",2);
109
 
110
  /* Create a socket for transitting */
111
  ip_str2addr(talk_myipaddr,&(local.s_addr));
112
  local.s_port = 101;
113
 
114
  /*
115
    If we want the address of the remote host in the ARP table before
116
    begginning the transmission (to eliminate a possible source of
117
    unpredictability), we can use the bindlist, otherwise we set the
118
    second parameter of udp_bind to NULL
119
  */
120
  ip_str2addr(talk_toipaddr,&(bindlist[0]));
121
  memset(&(bindlist[1]), 0, sizeof(IP_ADDR));
122
  s = udp_bind(&local, /*bindlist*/NULL);
123
 
124
  ip_str2addr(talk_toipaddr,&(to.s_addr));
125
  to.s_port = 100;
126
  sprintf(str,"Local IP address %d.%d.%d.%d\n", local.s_addr.ad[0],
127
          local.s_addr.ad[1], local.s_addr.ad[2],
128
          local.s_addr.ad[3]);
129
  win_puts(&dbg,str);
130
  sprintf(str,"Talk to %d.%d.%d.%d   ",to.s_addr.ad[0],to.s_addr.ad[1],
131
          to.s_addr.ad[2],to.s_addr.ad[3]);
132
  win_puts(&dbg,str);
133
  while (1) {
134
    /* Get the string...*/
135
    while((ch = keyb_getch(BLOCK)) != 13) {
136
        sprintf(tmp,"%c",ch);
137
        strcat(str,tmp);
138
        win_puts(&wr,tmp);
139
    }
140
    strcat(str,"\n");
141
    /*...and send it!!! */
142
    udp_sendto(s,str,strlen(str)+2,&to);
143
  }
144
}
145
 
146
/* This function is called when the user presses CTRL-C (stops the systems) */
147
void esci(KEY_EVT *k)
148
{
149
  esc = TRUE;
150
  sys_end();
151
}
152
 
153
int main(int argc, char **argv)
154
{
155
  KEY_EVT k;
156
 
157
  struct net_model m = net_base;
158
 
159
  NRT_TASK_MODEL m_nrt;
160
 
161
  k.flag = CNTL_BIT;
162
  k.scan = KEY_C;  
163
  k.ascii = 'c';
164
  k.status = KEY_PRESSED;
165
  keyb_hook(k,esci,FALSE);
166
  k.flag = CNTR_BIT;
167
  k.scan = KEY_C;  
168
  k.ascii = 'c';
169
  k.status = KEY_PRESSED;
170
  keyb_hook(k,esci,FALSE);
171
 
172
  clear();
173
  cprintf(" S.Ha.R.K. Talk!                     Ver. 1.10\n");
174
 
175
  if (argc != 3) {
176
    cprintf("S.Ha.R.K. Talk usage: talk fromIP toIP\n");
177
    return 0;
178
  }
179
 
180
  strcpy(talk_myipaddr, argv[1]);
181
  strcpy(talk_toipaddr, argv[2]);
182
 
183
  /* We want a task for TX mutual exclusion */
184
  net_setmode(m, TXTASK);
185
  /* We use UDP/IP stack */
186
  net_setudpip(m, talk_myipaddr, "255.255.255.255");
187
  /* OK: let's start the NetLib! */
188
  if (net_init(&m) == 1) {
189
    cprintf("Net Init OK...\n");
190
  } else {
191
    cprintf("Net Init Failed...\n");
192
    sys_abort(300);
193
  }
194
 
195
 
196
  //dump_irq();
197
 
198
  cprintf("\n\n\n\tPress ENTER\n");
199
  while (!esc) {
200
    keyb_getcode(&k,BLOCK);
201
    if (k.ascii == 13) esc = TRUE;
202
  }
203
 
204
  esc = FALSE;
205
  clear();
206
  win_init(&dbg,0,20,60,3);
207
  win_frame(&dbg,BLACK,WHITE,"Debug",2);
208
 
209
  /* Start the sender and receiver tasks...*/
210
  nrt_task_default_model(m_nrt);
211
  task_activate(task_create("aaa",scrittore,&m_nrt,NULL));
212
  task_activate(task_create("bbb",write,&m_nrt,NULL));
213
 
214
  return 0;
215
}