]> git.cryptolib.org Git - avr-crypto-lib.git/blob - serial-tools.c
b423299dac6decfb00096ff9c1562e8af8b267a1
[avr-crypto-lib.git] / serial-tools.c
1 /**
2  * 
3  * Author:      Daniel Otte
4  * Date:                16.05.2006
5  * 
6  * This tools should help to parse some input. 
7  * 
8  */
9
10 #include "config.h"
11 #include "uart.h"
12 #include <string.h>
13 #include <stdint.h>
14
15 int getnextwordn(char *s, int n){ /* words are seperated by spaces */
16         char c = ' ';
17         while ((c=uart_getc()) == ' ')
18                 ;
19         *s++ = c;
20         while (n && (*s++=uart_getc())!=' ')
21                 ;
22         *(s-1) = '\0';
23         return n;
24 }
25
26
27 void readhex2buffer(void* buffer, int n){
28         char c;
29         uint8_t i;
30         
31 //      DEBUG_S("\r\nDBG: n="); DEBUG_B(n&0xff); DEBUG_S("\r\n");
32         for(i=0; i<n; ++i){
33                 c = uart_getc();
34                 if ('0'<= c && '9'>=c){
35                         ((uint8_t*)buffer)[i] = c - '0';
36                 } else {
37                         c &= ~('A' ^ 'a'); /* make all uppercase */ 
38                         if ('A'<= c && 'F'>=c){
39                                 ((uint8_t*)buffer)[i] = c - 'A' + 10;
40                         } else {
41                                 /* oh shit, wrong char */
42                         }
43                 }
44                 
45                 ((uint8_t*)buffer)[i] <<= 4;
46                 
47                 c = uart_getc();
48                 if ('0'<= c && '9'>=c){
49                         ((uint8_t*)buffer)[i] |= c - '0';
50                 } else {
51                         c &= ~('A' ^ 'a'); /* make all uppercase */ 
52                         if ('A'<= c && 'F'>=c){
53                                 ((uint8_t*)buffer)[i] |= c - 'A' + 10;
54                         } else {
55                                 /* oh shit, wrong char */
56                         }
57                 }
58         } /* for i=0 .. n */
59 }
60
61 void uart_putptr(void* p){
62         uart_hexdump((void*) &p,2);
63 }
64