Passed tests on Raven 1284p in 3 seconds with 56KB program memory disk
[contiki-2.x.git] / apps / shell / shell-tcpsend.c
blob1d2eac5b04e5f4ae3bef0f7ae871a7b91b6ea242
1 /*
2 * Copyright (c) 2004, Adam Dunkels.
3 * All rights reserved.
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. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * This file is part of the Contiki operating system.
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: shell-tcpsend.c,v 1.4 2009/03/17 21:49:44 adamdunkels Exp $
36 #include <string.h>
37 #include <stddef.h>
39 #include "contiki.h"
40 #include "shell.h"
41 #include "telnet.h"
43 #ifndef MIN
44 #define MIN(a, b) ((a) < (b)? (a) : (b))
45 #endif /* MIN */
47 /*---------------------------------------------------------------------------*/
48 PROCESS(shell_tcpsend_process, "tcpsend");
49 SHELL_COMMAND(tcpsend_command,
50 "tcpsend",
51 "tcpsend <host> <port>: open a TCP connection",
52 &shell_tcpsend_process);
53 /*---------------------------------------------------------------------------*/
55 #define MAX_SERVERLEN 16
57 static uip_ipaddr_t serveraddr;
58 static char server[MAX_SERVERLEN + 1];
60 static struct telnet_state s;
62 static unsigned char running;
64 #define MAX_LINELEN 80
66 static char outputline[MAX_LINELEN];
67 static uint8_t sending;
68 /*---------------------------------------------------------------------------*/
69 void
70 telnet_text_output(struct telnet_state *s, char *text1, char *text2)
72 char buf1[MAX_SERVERLEN];
73 int len;
75 strncpy(buf1, text1, sizeof(buf1));
76 len = strlen(buf1);
77 if(len < sizeof(buf1) - 1) {
78 buf1[len] = ' ';
79 buf1[len + 1] = 0;
81 shell_output_str(&tcpsend_command, buf1, text2);
83 /*---------------------------------------------------------------------------*/
84 void
85 telnet_newdata(struct telnet_state *s, char *data, u16_t len)
87 shell_output(&tcpsend_command, data, len, "", 0);
89 /*---------------------------------------------------------------------------*/
90 static void
91 send_line(struct telnet_state *s, char *data, int len)
93 if(!sending) {
94 strncpy(outputline, data, MIN(sizeof(outputline), len));
95 telnet_send(s, data, len);
96 sending = 1;
97 } else {
98 shell_output_str(&tcpsend_command, "Cannot send data, still sending previous data", "");
101 /*---------------------------------------------------------------------------*/
102 void
103 telnet_sent(struct telnet_state *s)
105 sending = 0;
107 /*---------------------------------------------------------------------------*/
108 void
109 telnet_closed(struct telnet_state *s)
111 telnet_text_output(s, server, "connection closed");
112 running = 0;
114 /*---------------------------------------------------------------------------*/
115 void
116 telnet_aborted(struct telnet_state *s)
118 telnet_text_output(s, server, "connection aborted");
119 running = 0;
121 /*---------------------------------------------------------------------------*/
122 void
123 telnet_timedout(struct telnet_state *s)
125 telnet_text_output(s, server, "connection timed out");
126 running = 0;
128 /*---------------------------------------------------------------------------*/
129 void
130 telnet_connected(struct telnet_state *s)
132 telnet_text_output(s, server, "connected");
134 /*---------------------------------------------------------------------------*/
135 PROCESS_THREAD(shell_tcpsend_process, ev, data)
137 char *next;
138 const char *dummy;
139 struct shell_input *input;
140 uint16_t port;
142 PROCESS_BEGIN();
144 next = strchr(data, ' ');
145 if(next == NULL) {
146 shell_output_str(&tcpsend_command,
147 "tcpsend <server> <port>: server as address", "");
148 PROCESS_EXIT();
150 *next = 0;
151 ++next;
152 strncpy(server, data, sizeof(server));
153 port = shell_strtolong(next, &dummy);
155 running = 1;
157 uiplib_ipaddrconv(server, (u8_t *)&serveraddr);
158 telnet_connect(&s, &serveraddr, port);
159 while(running) {
160 PROCESS_WAIT_EVENT();
162 if(ev == shell_event_input) {
163 input = data;
164 if(input->len1 + input->len2 == 0) {
165 PROCESS_EXIT();
168 if(input->len1 > 0) {
169 send_line(&s, input->data1, input->len1);
171 } else if(ev == tcpip_event) {
172 telnet_app(data);
173 #if 0
174 } else if(ev == resolv_event_found) {
175 /* Either found a hostname, or not. */
176 if((char *)data != NULL &&
177 resolv_lookup((char *)data) != NULL) {
178 uip_ipaddr_copy(serveraddr, ipaddr);
179 telnet_connect(&s, server, serveraddr, nick);
180 } else {
181 shell_output_str(&tcpsend_command, "Host not found.", "");
183 #endif /* 0 */
187 PROCESS_END();
189 /*---------------------------------------------------------------------------*/
190 void
191 shell_tcpsend_init(void)
193 shell_register_command(&tcpsend_command);
195 /*---------------------------------------------------------------------------*/