2 * Copyright Droids Corporation (2007)
3 * Olivier MATZ <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: main.c,v 1.1.2.3 2007-11-15 11:18:00 zer0 Exp $
24 /* test program for rdline, works on AVR and HOST... but there are a
25 * lot of defines... ;) */
30 //#define DEBUG_SOCKET
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
47 #include <aversive/wait.h>
53 char prompt[RDLINE_PROMPT_SIZE];
57 #ifdef DEBUG_SOCKET /* debug... keep it because it is nice */
61 void sock_printf(const char * fmt, ...)
68 n=vsnprintf(buf, BUFSIZ, fmt, ap);
69 if (s>0) write(s, buf, n);
73 void dump_it(struct cirbuf * cbuf)
78 sock_printf("sta=%2.2d end=%2.2d len=%2.2d/%2.2d { ",
79 cbuf->start, cbuf->end,
81 CIRBUF_GET_MAXLEN(cbuf));
84 CIRBUF_FOREACH(cbuf, i, e) {
85 sock_printf("%2.2x, ", e&0xFF);
92 void sock_printf(const char * fmt, ...) {}
93 void dump_it(struct cirbuf * cbuf) {}
95 #endif /* DEBUG_SOCKET */
113 ret = rdline_char_in(&rdl, c);
115 rdline_add_history(&rdl, rdline_get_buffer(&rdl));
116 snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++);
117 rdline_newline(&rdl, prompt);
119 else if (ret == -2) {
128 void display_buffer(const char * buf, uint8_t size)
130 printf("**** GOT (%d) >> %s", size, buf);
133 const char * dummy_complete[] = {
140 #define TEST_COMPLETION 1
141 //#define TEST_COMPLETION 2
142 int8_t complete_buffer(const char * buf, uint8_t size,
143 char * dstbuf, uint8_t dstsize,
146 sock_printf("complete -> %d\n", *state);
147 #if TEST_COMPLETION == 1
148 if (*state < (sizeof(dummy_complete)/sizeof(const char *))) {
149 /* pourri mais bon c'est temporaire */
150 strcpy(dstbuf, dummy_complete[*state]);
169 struct termios oldterm, term;
175 struct sockaddr_in sin_ci;
178 s = socket(PF_INET, SOCK_STREAM, 0);
180 printf("socket() failed\n");
183 memset(&sin_ci, 0, sizeof(sin_ci));
184 sin_ci.sin_family = AF_INET;
185 sin_ci.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
186 sin_ci.sin_port = htons(31337);
188 sin_ci.sin_len = sizeof(sin_ci);
191 if (s > 0 && connect(s, (struct sockaddr *)&sin_ci, sizeof(sin_ci)) < 0) {
192 printf("connect() failed\n");
195 #endif /* DEBUG_SOCKET */
198 tcgetattr(0, &oldterm);
199 memcpy(&term, &oldterm, sizeof(term));
200 term.c_lflag &= ~(ICANON | ECHO | ISIG);
201 tcsetattr(0, TCSANOW, &term);
205 fdevopen(uart0_dev_send, uart0_dev_recv);
209 uart0_register_rx_event(rx);
216 rdline_init(&rdl, write_char, display_buffer, complete_buffer);
217 snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++);
218 rdline_newline(&rdl, prompt);
221 /* loop to send chars on host */
223 while ((n=read(0, buf, BUFSIZ-1)) > 0) {
226 for (i=0 ; i<n ; i++) {
227 sock_printf("%o ", buf[i]&0xff);
229 sock_printf(" RECV\n");
230 for (i=0 ; i<n ; i++) {
231 ret = rdline_char_in(&rdl, buf[i]);
233 rdline_add_history(&rdl, rdline_get_buffer(&rdl));
234 snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++);
235 rdline_newline(&rdl, prompt);
237 else if (ret == -2) {
238 tcsetattr(0, TCSANOW, &oldterm);
245 tcsetattr(0, TCSANOW, &oldterm);
248 /* irq driven on avr, see rx() */