extended HFAL

This commit is contained in:
bg 2009-02-09 00:15:01 +00:00
parent d3db763eeb
commit 5fe96fa58f
20 changed files with 625 additions and 119 deletions

View File

@ -56,7 +56,7 @@ email: daniel.otte@rub.de
This function initializes the context with algorithm specific values.
3.3. *_nexBlock function
The *nextBlock function is the core of each hash function. It updates the hash
The *_nextBlock function is the core of each hash function. It updates the hash
state with a given message block. So this function uses a context pointer and
a message pointer as parameters. The size of a message block is fixed for each
hash function (mostly 512 bit). For the last block of a messages which may be

View File

@ -39,11 +39,11 @@ void hfal_hash_nextBlock(hfgen_ctx_t* ctx, const void* block){
f(ctx->ctx, block);
}
void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t size){
void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t length_b){
hf_lastBlock_fpt f;
hfdesc_t* x=ctx->desc_ptr;
f =(hf_lastBlock_fpt)pgm_read_word(&(x->lastBlock));
f(ctx->ctx, block, size);
f(ctx->ctx, block, length_b);
}
void hfal_hash_ctx2hash(void* dest, hfgen_ctx_t* ctx){
@ -64,20 +64,38 @@ void hfal_hash_free(hfgen_ctx_t* ctx){
void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg, uint32_t length_b){
void_fpt f;
uint16_t bs,bsb;
uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))];
f=(void_fpt)pgm_read_word(&(hash_descriptor->init));
((hf_init_fpt)f)(ctx);
bs=pgm_read_word(&(hash_descriptor->blocksize_b));
bsb=bs/8;
f=(void_fpt)pgm_read_word(&(hash_descriptor->nextBlock));
while(length_b>=bs){
((hf_nextBlock_fpt)f)(ctx, msg);
length_b -= bs;
msg = (uint8_t*)msg + bsb;
f = (void_fpt)pgm_read_word(&(hash_descriptor->mem));
if(f){
((hf_mem_fpt)f)(dest, msg, length_b);
}else{
uint16_t bs,bsb;
uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))];
f=(void_fpt)pgm_read_word(&(hash_descriptor->init));
((hf_init_fpt)f)(ctx);
bs=pgm_read_word(&(hash_descriptor->blocksize_b));
bsb=bs/8;
f=(void_fpt)pgm_read_word(&(hash_descriptor->nextBlock));
while(length_b>=bs){
((hf_nextBlock_fpt)f)(ctx, msg);
length_b -= bs;
msg = (uint8_t*)msg + bsb;
}
f=(void_fpt)pgm_read_word(&(hash_descriptor->lastBlock));
((hf_lastBlock_fpt)f)(ctx, msg, length_b);
f=(void_fpt)pgm_read_word(&(hash_descriptor->ctx2hash));
((hf_ctx2hash_fpt)f)(dest, ctx);
}
f=(void_fpt)pgm_read_word(&(hash_descriptor->lastBlock));
((hf_lastBlock_fpt)f)(ctx, msg, length_b);
f=(void_fpt)pgm_read_word(&(hash_descriptor->ctx2hash));
((hf_ctx2hash_fpt)f)(dest, ctx);
}
uint16_t hfal_hash_getBlocksize(const* hash_descriptor){
uint16_t ret;
ret = pgm_read_word(&(hash_descriptor->blocksize_b));
return ret;
}
uint16_t hfal_hash_getHashsize(const* hash_descriptor){
uint16_t ret;
ret = pgm_read_word(&(hash_descriptor->hashsize_b));
return ret;
}

View File

@ -25,9 +25,11 @@
uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx);
void hfal_hash_nextBlock(hfgen_ctx_t* ctx, const void* block);
void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t size);
void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t length_b);
void hfal_hash_ctx2hash(void* dest, hfgen_ctx_t* ctx);
void hfal_hash_free(hfgen_ctx_t* ctx);
void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg, uint32_t length_b);
uint16_t hfal_hash_getBlocksize(const* hash_descriptor);
uint16_t hfal_hash_getHashsize(const* hash_descriptor);
#endif /* HFAL_BASIC_H_ */

49
hfal_md5.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_md5.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_md5.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "md5.h"
static const char md5_str[] PROGMEM = "MD5";
const hfdesc_t md5_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
md5_str,
sizeof(md5_ctx_t),
512,
128,
(hf_init_fpt)md5_init,
(hf_nextBlock_fpt)md5_nextBlock,
(hf_lastBlock_fpt)md5_lastBlock,
(hf_ctx2hash_fpt)md5_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)md5
};

36
hfal_md5.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_md5.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_md5.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#ifndef HFAL_MD5_H_
#define HFAL_MD5_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t md5_desc;
#endif /* HFAL_MD5_H_ */

49
hfal_sha1.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_sha1.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_sha1.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-04
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "sha1.h"
static const char sha1_str[] PROGMEM = "SHA-1";
const hfdesc_t sha1_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
sha1_str,
sizeof(sha1_ctx_t),
512,
160,
(hf_init_fpt)sha1_init,
(hf_nextBlock_fpt)sha1_nextBlock,
(hf_lastBlock_fpt)sha1_lastBlock,
(hf_ctx2hash_fpt)sha1_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)sha1
};

36
hfal_sha1.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_sha1.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_sha1.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-04
* \license GPLv3 or later
*
*/
#ifndef HFAL_SHA1_H_
#define HFAL_SHA1_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t sha1_desc;
#endif /* HFAL_SHA1_H_ */

View File

@ -44,6 +44,6 @@ const hfdesc_t sha256_desc PROGMEM = {
(hf_lastBlock_fpt)sha256_lastBlock,
(hf_ctx2hash_fpt)sha256_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)NULL
(hf_mem_fpt)sha256
};

49
hfal_twister224.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_twister224.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister224.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-04
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "twister-small.h"
static const char twister224_str[] PROGMEM = "Twister-224";
const hfdesc_t twister224_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
twister224_str,
sizeof(twister224_ctx_t),
512,
224,
(hf_init_fpt)twister224_init,
(hf_nextBlock_fpt)twister224_nextBlock,
(hf_lastBlock_fpt)twister224_lastBlock,
(hf_ctx2hash_fpt)twister224_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)twister224
};

36
hfal_twister224.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_twister224.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister224.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#ifndef HFAL_TWISTER224_H_
#define HFAL_TWISTER224_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t twister224_desc;
#endif /* HFAL_TWISTER224_H_ */

49
hfal_twister256.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_twister256.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister256.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "twister-small.h"
static const char twister256_str[] PROGMEM = "Twister-256";
const hfdesc_t twister256_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
twister256_str,
sizeof(twister256_ctx_t),
512,
256,
(hf_init_fpt)twister256_init,
(hf_nextBlock_fpt)twister256_nextBlock,
(hf_lastBlock_fpt)twister256_lastBlock,
(hf_ctx2hash_fpt)twister256_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)twister256
};

36
hfal_twister256.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_twister256.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister256.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#ifndef HFAL_TWISTER256_H_
#define HFAL_TWISTER256_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t twister256_desc;
#endif /* HFAL_TWISTER256_H_ */

49
hfal_twister384.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_twister384.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister384.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "twister384.h"
static const char twister384_str[] PROGMEM = "Twister-384";
const hfdesc_t twister384_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
twister384_str,
sizeof(twister384_ctx_t),
512,
384,
(hf_init_fpt)twister384_init,
(hf_nextBlock_fpt)twister384_nextBlock,
(hf_lastBlock_fpt)twister384_lastBlock,
(hf_ctx2hash_fpt)twister384_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)twister384
};

36
hfal_twister384.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_twister384.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister384.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#ifndef HFAL_TWISTER384_H_
#define HFAL_TWISTER384_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t twister384_desc;
#endif /* HFAL_TWISTER384_H_ */

49
hfal_twister512.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_twister512.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister512.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "twister-big.h"
static const char twister512_str[] PROGMEM = "Twister-512";
const hfdesc_t twister512_desc PROGMEM = {
HFDESC_TYPE_HASHFUNCTION,
0,
twister512_str,
sizeof(twister512_ctx_t),
512,
512,
(hf_init_fpt)twister512_init,
(hf_nextBlock_fpt)twister512_nextBlock,
(hf_lastBlock_fpt)twister512_lastBlock,
(hf_ctx2hash_fpt)twister512_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)twister512
};

36
hfal_twister512.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_twister512.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_twister512.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-02-09
* \license GPLv3 or later
*
*/
#ifndef HFAL_TWISTER512_H_
#define HFAL_TWISTER512_H_
#include <avr/pgmspace.h>
#include "hashfunction_descriptor.h"
extern const hfdesc_t twister512_desc;
#endif /* HFAL_TWISTER512_H_ */

View File

@ -659,7 +659,7 @@ sha1_nextBlock_mainloop_core: /* ther core function; T=ROTL5(a) ....*/
adc T4, tmp1
/* T = ROTL(a,5) + e + kt + w[s] */
/* wo Z-4 gerade auf kt zeigt ... */
/* Z-4 is just pointing to kt ... */
movw r28, r26 /* copy X in Y */
adiw r30, 3*4 /* now Z points to the rigth locatin in our jump-vector-table */
lsr r31
@ -887,7 +887,6 @@ sha1_init_vloop:
dec r22
brne sha1_init_vloop
ldi r22, 8
clr r1 /* this should not be needed */
sha1_init_lloop:
st X+, r1
dec r22
@ -900,96 +899,4 @@ sha1_init_vector:
.int 0x98badcfe;
.int 0x10325476;
.int 0xc3d2e1f0;
/*
;###########################################################
.global rotl32
; === ROTL32 ===
; function that rotates a 32 bit word to the left
; param1: the 32-bit word to rotate
; given in r25,r24,r23,r22 (r25 is most significant)
; param2: an 8-bit value telling how often to rotate
; given in r20
; modifys: r21, r22
rotl32:
cpi r20, 8
brlo bitrotl
mov r21, r25
mov r25, r24
mov r24, r23
mov r23, r22
mov r22, r21
subi r20, 8
rjmp rotr32
bitrotl:
clr r21
clc
bitrotl_loop:
tst r20
breq fixrotl
rol r22
rol r23
rol r24
rol r25
rol r21
dec r20
rjmp bitrotl_loop
fixrotl:
or r22, r21
ret
;###########################################################
.global rotr32
; === ROTR32 ===
; function that rotates a 32 bit word to the right
; param1: the 32-bit word to rotate
; given in r25,r24,r23,22 (r25 is most significant)
; param2: an 8-bit value telling how often to rotate
; given in r20
; modifys: r21, r22
rotr32:
cpi r20, 8
brlo bitrotr
mov r21, r22
mov r22, r23
mov r23, r24
mov r24, r25
mov r25, r21
subi r20, 8
rjmp rotr32
bitrotr:
clr r21
clc
bitrotr_loop:
tst r20
breq fixrotr
ror r25
ror r24
ror r23
ror r22
ror r21
dec r20
rjmp bitrotr_loop
fixrotr:
or r25, r21
ret
;###########################################################
.global change_endian32
; === change_endian32 ===
; function that changes the endianess of a 32-bit word
; param1: the 32-bit word
; given in r25,r24,r23,22 (r25 is most significant)
; modifys: r21, r22
change_endian32:
movw r20, r22 ; (r22,r23) --> (r20,r21)
mov r22, r25
mov r23, r24
mov r24, r21
mov r25, r20
ret
*/

View File

@ -39,18 +39,32 @@ cli_rx_fpt cli_rx = NULL;
cli_tx_fpt cli_tx = NULL;
uint8_t cli_echo=1;
/**
* \brief output a character to the console
*
*/
void cli_putc(char c){
if(cli_tx)
cli_tx(c);
}
/**
* \brief get a character from the console
* Gets a character from the console input and blocks
* until a character is recieved
*/
uint16_t cli_getc(void){
if(cli_rx)
return cli_rx();
return ((uint16_t)-1);
}
/**
* \brief get a character from the console
* Gets a char from the console input (like cli_getc())
* and echos it back to the console if echo is enabled.
*/
uint16_t cli_getc_cecho(void){
char c;
if(cli_rx){
@ -62,6 +76,9 @@ uint16_t cli_getc_cecho(void){
return ((uint16_t)-1);
}
/**
* \brief ouputs a zero-terminated string from ram to the console
*/
void cli_putstr(char* s){
if(!cli_tx)
return;
@ -69,6 +86,10 @@ void cli_putstr(char* s){
cli_tx(*s++);
}
/**
* \brief ouputs a zero-terminated string from flash to the console
*/
void cli_putstr_P(PGM_P s){
char c;
if(!cli_tx)
@ -81,6 +102,13 @@ void cli_putstr_P(PGM_P s){
}
}
/**
* \brief reads a line or max n characters from the console
* Writes characters from the console into the supplyed buffer until a '\r'
* character is recieved or until n character a read (whatever happens first).
* The string will always be terminated by a '\0' character, so the buffer
* should have at least a size of n+1.
*/
uint8_t cli_getsn(char* s, uint16_t n){
char c;
if(n==0)
@ -92,6 +120,11 @@ uint8_t cli_getsn(char* s, uint16_t n){
return (c=='\r')?0:1;
}
/**
* \brief dumps the contents of a buffer to the console
* Dumps length bytes from data to the console ouput. The dump
* will have 2*n continous hexadecimal characters.
*/
void cli_hexdump(void* data, uint16_t length){
char hex_tab[] = {'0', '1', '2', '3',
'4', '5', '6', '7',
@ -106,6 +139,11 @@ void cli_hexdump(void* data, uint16_t length){
}
}
/**
* \brief dumps the contents of a buffer to the console
* Like cli_hexdump but bytes are seperated with a single space
* on the console output.
*/
void cli_hexdump2(void* data, uint16_t length){
char hex_tab[] = {'0', '1', '2', '3',
'4', '5', '6', '7',
@ -121,6 +159,7 @@ void cli_hexdump2(void* data, uint16_t length){
}
}
static
void cli_auto_help(uint16_t maxcmdlength, PGM_VOID_P cmdlist){
cmdlist_entry_t item;

View File

@ -33,10 +33,18 @@ typedef void (*cli_tx_fpt)(char);
#define CMDLIST_ENTRY_SIZE 8
typedef struct {
PGM_P cmd_name; /* string containing the function name */
PGM_P cmd_param_str; /* param descriptor string */
void_fpt cmd_function; /* function pointer */
void_fpt options;
uint16_t option_flags;
PGM_VOID_P options[];
} cmdoption_t;
#define CLI_OPTION_DESC 0x01
#define CLI_OPTION_MANP 0x02
typedef struct {
PGM_P cmd_name; /* string containing the function name */
PGM_P cmd_param_str; /* param descriptor string */
void_fpt cmd_function; /* function pointer */
cmdoption_t* options;
} cmdlist_entry_t;
extern cli_rx_fpt cli_rx;
@ -44,6 +52,7 @@ extern cli_tx_fpt cli_tx;
extern uint8_t cli_echo;
void cli_putc(char c);
uint16_t cli_getc(void);
uint16_t cli_getc_cecho(void);

View File

@ -58,6 +58,27 @@
#define RXC RXC0
#endif
#ifdef AT90USB162
#define UCSRB UCSR1B
#define UCSRC UCSR1C
#define UDR UDR1
#define UBRRH UBRR1H
#define UBRRL UBRR1L
#define URSEL UMSEL10
#define USART_UDRE_vect USART1_UDRE_vect
#define USART_RXC_vect USART1_RX_vect
#define UDRIE UDRIE1
#define TXEN TXEN1
#define UMSEL UMSEL1
#define RXEN RXEN1
#define RXCIE RXCIE1
#define UCSZ0 UCSZ10
#define UCSRA UCSR1A
#define UDRE UDRE1
#define RXC RXC1
#endif
#ifdef UART_XON_XOFF
#ifdef UART_INTERRUPT