tests: fix test programs using old uart API
[aversive.git] / modules / ihm / rdline / test / main.c
1 /*  
2  *  Copyright Droids Corporation (2007)
3  *  Olivier MATZ <zer0@droids-corp.org>
4  * 
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.
9  *
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.
14  *
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
18  *
19  *  Revision : $Id: main.c,v 1.1.2.3 2007-11-15 11:18:00 zer0 Exp $
20  *
21  *
22  */
23
24 /* test program for rdline, works on AVR and HOST... but there are a
25  * lot of defines... ;) */
26
27 #include <stdio.h>
28 #include <string.h>
29
30 //#define DEBUG_SOCKET
31
32
33 #ifdef HOST_VERSION
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <inttypes.h>
38 #include <termios.h>
39 #include <ctype.h>
40
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #endif
46
47 #include <aversive/wait.h>
48 #include <uart.h>
49 #include "rdline.h"
50
51 /* globals */
52 struct rdline rdl;
53 char prompt[RDLINE_PROMPT_SIZE];
54 int cpt=0;
55
56
57 #ifdef DEBUG_SOCKET /* debug... keep it because it is nice */
58
59 int s = -1;
60
61 void sock_printf(const char * fmt, ...)
62 {
63         va_list ap;
64         char buf[BUFSIZ];
65         int n;
66
67         va_start(ap, fmt);
68         n=vsnprintf(buf, BUFSIZ, fmt, ap);
69         if (s>0) write(s, buf, n);
70         va_end(ap);
71 }
72
73 void dump_it(struct cirbuf * cbuf)
74 {
75         int i;
76         char e;
77
78         sock_printf("sta=%2.2d end=%2.2d len=%2.2d/%2.2d { ", 
79                  cbuf->start, cbuf->end,
80                  CIRBUF_GET_LEN(cbuf),
81                  CIRBUF_GET_MAXLEN(cbuf));
82
83         sock_printf("[ ");
84         CIRBUF_FOREACH(cbuf, i, e) {
85                 sock_printf("%2.2x, ", e&0xFF);
86         }
87         sock_printf("]\n");
88 }
89
90 #else
91
92 void sock_printf(const char * fmt, ...) {}
93 void dump_it(struct cirbuf * cbuf) {}
94
95 #endif /* DEBUG_SOCKET */
96
97
98 #ifdef HOST_VERSION
99 void
100 write_char(char c) {
101         write(1, &c, 1);
102 }
103 #else
104 void
105 write_char(char c) {
106         uart_send(0, c);
107 }
108
109 static void
110 rx(char c)
111 {
112         int8_t ret;
113         ret = rdline_char_in(&rdl, c);
114         if (ret == 1) {
115                 rdline_add_history(&rdl, rdline_get_buffer(&rdl));
116                 snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++);
117                 rdline_newline(&rdl, prompt);
118         }
119         else if (ret == -2) {
120                 rdline_stop(&rdl);
121                 printf("END\n");
122         }
123 }
124
125 #endif
126
127
128 void display_buffer(const char * buf, uint8_t size) 
129 {
130         printf("**** GOT  (%d) >> %s", size, buf);
131 }
132
133 const char * dummy_complete[] = {
134         "toto",
135         "titi",
136         "pouet",
137         "coin",
138 };
139
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,
144                      int * state)
145 {
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]);
151                 (*state) ++;
152                 return 1;
153         }
154         return 0;
155 #else
156         dstbuf[0] = 'x';
157         dstbuf[1] = 'y';
158         dstbuf[2] = 'z';
159         dstbuf[3] = '\0';
160         return 2;
161 #endif
162 }
163
164
165
166 int main(void)
167 {
168 #ifdef HOST_VERSION
169         struct termios oldterm, term;
170         char buf[BUFSIZ];
171         int n, i;
172         int8_t ret;
173 #endif
174 #ifdef DEBUG_SOCKET
175         struct sockaddr_in sin_ci;
176
177
178         s = socket(PF_INET, SOCK_STREAM, 0);
179         if (s < 0) {
180                 printf("socket() failed\n");
181         }
182
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);
187 #ifndef __linux__
188         sin_ci.sin_len = sizeof(sin_ci);
189 #endif
190
191         if (s > 0 && connect(s, (struct sockaddr *)&sin_ci, sizeof(sin_ci)) < 0) {
192                 printf("connect() failed\n");
193                 s = -1;
194         }
195 #endif /* DEBUG_SOCKET */
196
197 #ifdef HOST_VERSION
198         tcgetattr(0, &oldterm);
199         memcpy(&term, &oldterm, sizeof(term));
200         term.c_lflag &= ~(ICANON | ECHO | ISIG);
201         tcsetattr(0, TCSANOW, &term);
202         setbuf(stdin, NULL);
203 #else
204         uart_init();
205         fdevopen(uart0_dev_send, uart0_dev_recv);
206
207         wait_ms(5000);
208         printf("Start\n");
209         uart_register_rx_event(0, rx);
210         
211         sei();
212 #endif
213
214
215         /* common init */
216         rdline_init(&rdl, write_char, display_buffer, complete_buffer);
217         snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++); 
218         rdline_newline(&rdl, prompt);
219
220
221         /* loop to send chars on host */
222 #ifdef HOST_VERSION
223         while ((n=read(0, buf, BUFSIZ-1)) > 0) {
224                 buf[n] = 0;
225
226                 for (i=0 ; i<n ; i++) {
227                         sock_printf("%o ", buf[i]&0xff);
228                 }
229                 sock_printf(" RECV\n");
230                 for (i=0 ; i<n ; i++) {
231                         ret = rdline_char_in(&rdl, buf[i]);
232                         if (ret == 1) {
233                                 rdline_add_history(&rdl, rdline_get_buffer(&rdl));
234                                 snprintf(prompt, sizeof(prompt), "toto[%d] > ", cpt++);
235                                 rdline_newline(&rdl, prompt);
236                         }
237                         else if (ret == -2) {
238                                 tcsetattr(0, TCSANOW, &oldterm);
239                                 printf("\n");
240                                 return 0;
241                         }
242                 }
243         }
244
245         tcsetattr(0, TCSANOW, &oldterm);
246         printf("\n");
247
248         /* irq driven on avr, see rx() */
249 #else
250         while(1);
251 #endif
252
253         return 0;
254 }