Contiki 3.x
shell-udpsend.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include <string.h>
36 #include <stddef.h>
37 
38 #include "contiki.h"
39 #include "shell.h"
40 #include "contiki-net.h"
41 
42 /*---------------------------------------------------------------------------*/
43 PROCESS(shell_udpsend_process, "udpsend");
44 SHELL_COMMAND(udpsend_command,
45  "udpsend",
46  "udpsend <host> <remote port> [local port]: send UDP data",
47  &shell_udpsend_process);
48 /*---------------------------------------------------------------------------*/
49 
50 #define MAX_SERVERLEN 16
51 
52 static struct uip_udp_conn *udpconn;
53 
54 static uip_ipaddr_t serveraddr;
55 static char server[MAX_SERVERLEN + 1];
56 
57 static unsigned char running;
58 
59 /*---------------------------------------------------------------------------*/
60 static void
61 send_line(char *line, int len)
62 {
63  uip_udp_packet_send(udpconn, line, len);
64 }
65 /*---------------------------------------------------------------------------*/
66 static void
67 newdata(char *data, uint16_t len)
68 {
69  shell_output(&udpsend_command, data, len, "", 0);
70 }
71 /*---------------------------------------------------------------------------*/
72 PROCESS_THREAD(shell_udpsend_process, ev, data)
73 {
74  const char *next, *nextptr;
75  struct shell_input *input;
76  uint16_t port, local_port;
77 
78  PROCESS_BEGIN();
79 
80  next = strchr(data, ' ');
81  if(next == NULL) {
82  shell_output_str(&udpsend_command,
83  "udpsend <server> <port> [localport]: server as address", "");
84  PROCESS_EXIT();
85  }
86  if(next - (char *)data > sizeof(server)) {
87  shell_output_str(&udpsend_command, "Too long input", "");
88  PROCESS_EXIT();
89  }
90  strncpy(server, data, sizeof(server));
91  /* NULL-terminate the server string. */
92  server[next - (char *)data] = 0;
93  ++next;
94  port = shell_strtolong(next, &nextptr);
95 
96  uiplib_ipaddrconv(server, &serveraddr);
97  udpconn = udp_new(&serveraddr, uip_htons(port), NULL);
98 
99  if(next != nextptr) {
100  local_port = shell_strtolong(nextptr, &nextptr);
101  udp_bind(udpconn, uip_htons(local_port));
102  }
103  running = 1;
104 
105 
106  while(running) {
108 
109  if(ev == shell_event_input) {
110  input = data;
111  if(input->len1 + input->len2 == 0) {
112  PROCESS_EXIT();
113  }
114 
115  if(input->len1 > 0) {
116  send_line(input->data1, input->len1);
117  }
118  } else if(ev == tcpip_event) {
119  if(uip_newdata()) {
120  newdata(uip_appdata, uip_datalen());
121  }
122 #if 0
123  } else if(ev == resolv_event_found) {
124  /* Either found a hostname, or not. */
125  if((char *)data != NULL &&
126  resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
127  uip_ipaddr_copy(serveraddr, ipaddr);
128  telnet_connect(&s, server, serveraddr, nick);
129  } else {
130  shell_output_str(&udpsend_command, "Host not found.", "");
131  }
132 #endif /* 0 */
133  }
134  }
135 
136  PROCESS_END();
137 }
138 /*---------------------------------------------------------------------------*/
139 void
140 shell_udpsend_init(void)
141 {
142  shell_register_command(&udpsend_command);
143 }
144 /*---------------------------------------------------------------------------*/
Hostname is fresh and usable.
Definition: resolv.h:68
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
CCIF struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
#define uip_newdata()
Is new incoming data available?
Definition: uip.h:738
Main header file for the Contiki shell
#define NULL
The null pointer.
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
Definition: shell.c:395
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
CCIF process_event_t resolv_event_found
Event that is broadcasted when a DNS name has been resolved.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
Structure for shell input data.
Definition: shell.h:365
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
Definition: uip.h:651
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:75
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
Definition: tcpip.h:262
Representation of a uIP UDP connection.
Definition: uip.h:1394
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74