]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/serial-tools.c
new cli system
[avr-crypto-lib.git] / test_src / serial-tools.c
1 /* serial-tools.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * 
21  * Author:      Daniel Otte
22  * Date:                16.05.2006
23  * 
24  * This tools should help to parse some input. 
25  * 
26  */
27
28 #include "config.h"
29 #include "uart.h"
30 #include <string.h>
31 #include <stdint.h>
32
33 int getnextwordn(char *s, int n){ /* words are seperated by spaces, lf or cr */
34         char c = ' ';
35         do{
36                 c=uart_getc(); 
37         }while(c==' ' || c=='\r' || c=='\n');
38         *s++ = c;
39         do{ 
40           *s++ = c = uart_getc();
41         }while(c!=' ' && c!='\r' && c!='\n' && --n);
42         *(s-1) = '\0';
43         return n;
44 }
45
46
47 void readhex2buffer(void* buffer, int n){
48         char c;
49         uint8_t i;
50         
51 //      DEBUG_S("\r\nDBG: n="); DEBUG_B(n&0xff); DEBUG_S("\r\n");
52         for(i=0; i<n; ++i){
53                 c = uart_getc();
54                 if ('0'<= c && '9'>=c){
55                         ((uint8_t*)buffer)[i] = c - '0';
56                 } else {
57                         c &= ~('A' ^ 'a'); /* make all uppercase */ 
58                         if ('A'<= c && 'F'>=c){
59                                 ((uint8_t*)buffer)[i] = c - 'A' + 10;
60                         } else {
61                                 /* oh shit, wrong char */
62                         }
63                 }
64                 
65                 ((uint8_t*)buffer)[i] <<= 4;
66                 
67                 c = uart_getc();
68                 if ('0'<= c && '9'>=c){
69                         ((uint8_t*)buffer)[i] |= c - '0';
70                 } else {
71                         c &= ~('A' ^ 'a'); /* make all uppercase */ 
72                         if ('A'<= c && 'F'>=c){
73                                 ((uint8_t*)buffer)[i] |= c - 'A' + 10;
74                         } else {
75                                 /* oh shit, wrong char */
76                         }
77                 }
78         } /* for i=0 .. n */
79 }
80
81 void uart_putptr(void* p){
82         uart_hexdump((void*) &p,2);
83 }
84