Subversion Repositories shark

Rev

Rev 1405 | Go to most recent revision | Details | Compare with Previous | 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");
1406 giacomo 141
    win_puts(&wr,"\n");
1405 giacomo 142
    /*...and send it!!! */
143
    udp_sendto(s,str,strlen(str)+2,&to);
1406 giacomo 144
    str[0] = 0;
1405 giacomo 145
  }
146
}
147
 
148
/* This function is called when the user presses CTRL-C (stops the systems) */
149
void esci(KEY_EVT *k)
150
{
151
  esc = TRUE;
152
  sys_end();
153
}
154
 
155
int main(int argc, char **argv)
156
{
157
  KEY_EVT k;
158
 
159
  struct net_model m = net_base;
160
 
161
  NRT_TASK_MODEL m_nrt;
162
 
163
  k.flag = CNTL_BIT;
164
  k.scan = KEY_C;  
165
  k.ascii = 'c';
166
  k.status = KEY_PRESSED;
167
  keyb_hook(k,esci,FALSE);
168
  k.flag = CNTR_BIT;
169
  k.scan = KEY_C;  
170
  k.ascii = 'c';
171
  k.status = KEY_PRESSED;
172
  keyb_hook(k,esci,FALSE);
173
 
174
  clear();
175
  cprintf(" S.Ha.R.K. Talk!                     Ver. 1.10\n");
176
 
177
  if (argc != 3) {
178
    cprintf("S.Ha.R.K. Talk usage: talk fromIP toIP\n");
179
    return 0;
180
  }
181
 
182
  strcpy(talk_myipaddr, argv[1]);
183
  strcpy(talk_toipaddr, argv[2]);
184
 
185
  /* We want a task for TX mutual exclusion */
186
  net_setmode(m, TXTASK);
187
  /* We use UDP/IP stack */
188
  net_setudpip(m, talk_myipaddr, "255.255.255.255");
189
  /* OK: let's start the NetLib! */
190
  if (net_init(&m) == 1) {
191
    cprintf("Net Init OK...\n");
192
  } else {
193
    cprintf("Net Init Failed...\n");
194
    sys_abort(300);
195
  }
196
 
197
 
198
  //dump_irq();
199
 
200
  cprintf("\n\n\n\tPress ENTER\n");
201
  while (!esc) {
202
    keyb_getcode(&k,BLOCK);
203
    if (k.ascii == 13) esc = TRUE;
204
  }
205
 
206
  esc = FALSE;
207
  clear();
208
  win_init(&dbg,0,20,60,3);
209
  win_frame(&dbg,BLACK,WHITE,"Debug",2);
210
 
211
  /* Start the sender and receiver tasks...*/
212
  nrt_task_default_model(m_nrt);
213
  task_activate(task_create("aaa",scrittore,&m_nrt,NULL));
214
  task_activate(task_create("bbb",write,&m_nrt,NULL));
215
 
216
  return 0;
217
}