From be5d371dbc6c0498b6bcdef58b96e178e2ecda51 Mon Sep 17 00:00:00 2001 From: n2460 Date: Thu, 6 Jun 2013 06:28:41 +0000 Subject: [PATCH] =?UTF-8?q?sample:=E3=82=B7=E3=83=A3=E3=83=BC=E3=83=97?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E3=81=AE=E3=82=B5=E3=83=B3=E3=83=97=E3=83=AB?= =?UTF-8?q?=E3=82=92=E6=94=B9=E3=82=81=E3=81=A6=E4=BD=9C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@213 ff987cc8-cf2f-4642-8568-d52cce064691 --- trunk/Makefile.sharp | 23 +------ trunk/sample.c | 147 +++++++++++++++++++++++++++++++++++++++++++ trunk/util.c | 97 ++++++++++++++++++++++++++++ trunk/util.h | 28 +++++++++ 4 files changed, 274 insertions(+), 21 deletions(-) create mode 100644 trunk/sample.c create mode 100644 trunk/util.c create mode 100644 trunk/util.h diff --git a/trunk/Makefile.sharp b/trunk/Makefile.sharp index c4ae3b0..455d4fd 100644 --- a/trunk/Makefile.sharp +++ b/trunk/Makefile.sharp @@ -1,32 +1,15 @@ - -# Linux 上でビルドする場合は、DEV_CYGWINをコメントアウトしてください。 -#DEV_CYGWIN = TRUE - TARGET = gen_id -SRCS = main.c +SRCS = sample.c util.c OBJS = $(notdir $(SRCS:.c=.o)) -ifeq ($(DEV_CYGWIN),TRUE) - -CC := C:/Cygwin/bin/gcc-3 -LD = C:/Cygwin/bin/gcc-3 -CFLAGS += -mno-cygwin -DDEV_CYGWIN -Wall -I./ -LDFLAGS += -Wl,--subsystem,console -mwindows -mno-cygwin -L./ -LDLIBS += -lgenid -TARGET_DEL = $(TARGET).exe - -else # DEV_CYGWIN - CC := /usr/bin/gcc LD = /usr/bin/gcc LDFLAGS += -Wl -L./ -LDLIBS += -ldl -lnsl -lgenid +LDLIBS += -lgenid TARGET_DEL = $(TARGET) -endif # DEV_CYGWIN - .SUFFIXES: all: $(TARGET) @@ -40,5 +23,3 @@ $(TARGET): $(OBJS) .PHONY: clean clobber clean clobber: $(RM) $(OBJS) $(TARGET_DEL) - - diff --git a/trunk/sample.c b/trunk/sample.c new file mode 100644 index 0000000..e3fa41d --- /dev/null +++ b/trunk/sample.c @@ -0,0 +1,147 @@ +#include + +#include "cr_generate_id.h" +#include "util.h" + +int main(int argc, char* argv[]) +{ + int i; + int ret_code = 0; + + u8 id_buf[CR_ID_BUF_SIZE]; // 出力される ID の格納されるバッファ + u32 device_id[CR_NUM_OF_DEVICEID]; // 生成関数に入力する ID + int bonding_option = -1; // ボンディングオプション + + // 時間計測用 + double time_start = 0; + double time_end = 0; + long double time_total = 0; + int time_count = 0; + + // カウンタ + u32 counter0 = 0; + u64 counter1 = 0; + u64 counter2 = 0; + + // 引数チェック + if ( argc == 1 ) + { + printf("Usage : %s bonding_option\n", argv[0]); + printf("\tbonding_option : 00 01 10\n", argv[0]); + return 0; + } + if ( argc >= 2 && strlen(argv[1]) >= 2 ) + { + u8 bo_hi = argv[1][0] - '0'; + u8 bo_lo = argv[1][1] - '0'; + bonding_option = bo_hi*2 + bo_lo; + } + if ( bonding_option < 0 || 2 <= bonding_option ) + { + printf("Invalid bonding_option\n"); + return -1; + } + + printf("Now Initializing ... "); + fflush(stdout); + + // cr_generate_id を使用する前に呼び出す + ret_code = cr_generate_id_initialize( id_buf ); + if ( ret_code != CR_GENID_SUCCESS ) + { + printf( "error : cr_generate_id_initialize\n" ); + return -1; + } + printf("done.\n"); + + // 入力のための初期化 + keyboard_initialize(); + + for ( i = 1; i < 0xffffffff; i++ ) + { + u64 unit; + + // counter0 は 1 ずつ加算 + counter0 = i; + if( counter0 == 0 ) + { + counter0 = 1; + } + + // counter1 は、"1〜4 の乱数値" を加算 + unit = (u64)( ( rand() & 0x03 ) + 1 ); + counter1 += unit; + + // counter2 は、"0 以外の 32bit 乱数値" を加算 + do { + unit = ((u64)rand() & 0xffff) | ( ((u64)rand() & 0xffff) << 16 ); + } while( unit == 0 ); + counter2 += unit; + + // counter から device_id を作成 + device_id[0] = counter0; + device_id[1] = (u32)(counter1 & 0xffffffff ); + device_id[2] = (u32)( (counter1 >> 32) & 0xffffffff ); + device_id[3] = (u32)(counter2 & 0xffffffff ); + device_id[4] = (u32)( (counter2 >> 32) & 0xffffffff ); + + // ID 生成 + time_start = gettimeofday_sec(); + ret_code = cr_generate_id( device_id, id_buf, bonding_option ); + if( ret_code != CR_GENID_SUCCESS ) + { + fprintf(stderr, "cr_generate_id failed c=0x%08x\n", counter0); + } + else + { + time_end = gettimeofday_sec(); + time_total += (long double)(time_end - time_start); + time_count++; + } + + counter0++; + counter1 += (u64)rand(); + counter2 += (u64)rand(); + + if ( keyboard_is_hit() != 0 ) + { + char c = keyboard_get_char(); + // p で ID, eFuseID, 処理時間を表示 + // s で eFuseID を保存 + // q で終了 + switch ( c ) + { + case 'p' : + printf("ID[0] = 0x%08x\n", device_id[0]); + printf("ID[1] = 0x%08x%08x\n", device_id[2], device_id[1] ); + printf("ID[2] = 0x%08x%08x\n", device_id[4], device_id[3] ); + debug_print_array( "eFuseID", id_buf, sizeof(id_buf) ); + printf("time av. = %8.8f sec\n", (double)(time_total/(long double)time_count)); + break; + + case 's' : + debug_file_output( device_id[0], "bin", id_buf, sizeof(id_buf) ); + printf("./0x%08x.%s is saved.\n", device_id[0], "bin"); + break; + + case 'q' : + goto end; + } + } + } + +end: + // 入力終了 + keyboard_finalize(); + + // cr_generate_id を使用した後に呼び出す + if ( cr_generate_id_finalize( id_buf ) != CR_GENID_SUCCESS ) + { + printf( "error : cr_generate_id_finalize\n" ); + return 0; // error + } + + printf( "end of main.\n" ); + + return 0; +} diff --git a/trunk/util.c b/trunk/util.c new file mode 100644 index 0000000..9ce94ab --- /dev/null +++ b/trunk/util.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +#include "util.h" + +static struct termios initial_setting, new_setting; +static int peek_character = -1; + +void keyboard_initialize( void ) +{ + tcgetattr( 0, &initial_setting ); + new_setting = initial_setting; + new_setting.c_lflag &= ~ICANON; + new_setting.c_lflag &= ~ECHO; + new_setting.c_lflag &= ~ISIG; + new_setting.c_cc[VMIN] = 0; + new_setting.c_cc[VTIME] = 0; + tcsetattr( 0, TCSANOW, &initial_setting ); +} + +void keyboard_finalize( void ) +{ + tcsetattr( 0, TCSANOW, &initial_setting ); +} + +int keyboard_is_hit( void ) +{ + char ch; + int nread; + + if ( peek_character != -1 ) + { + return 1; + } + new_setting.c_cc[VMIN] = 0; + tcsetattr( 0, TCSANOW, &new_setting ); + nread = read( 0, &ch, 1 ); + new_setting.c_cc[VMIN] = 1; + tcsetattr( 0, TCSANOW, &new_setting ); + + if ( nread == 1 ) + { + peek_character = ch; + return 1; + } + + return 0; +} + +char keyboard_get_char( void ) +{ + char ch; + + if ( peek_character != -1 ) + { + ch = peek_character; + peek_character = -1; + return ch; + } + read( 0, &ch, 1 ); + return ch; +} + +void debug_print_array( const char *label, const unsigned char *data, int length ) +{ + int i; + printf( "%s", label ); + for( i = 0 ; i < length; i++ ) + { + if( (i % 16) == 0 ) + { + printf("\n "); + } + printf("%02X ", data[ i ] ); + } + printf("\n"); +} + +void debug_file_output( unsigned int id, char *suffix, const unsigned char *data, int length ) +{ + FILE *fp; + char file_name[256]; + + sprintf( file_name, "./0x%08x.%s", id, suffix ); + fp = fopen( file_name, "wb" ); + fwrite( data, length, 1, fp ); + fclose( fp ); +} + +double gettimeofday_sec(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec + (double)tv.tv_usec*1e-6; +} diff --git a/trunk/util.h b/trunk/util.h new file mode 100644 index 0000000..0106353 --- /dev/null +++ b/trunk/util.h @@ -0,0 +1,28 @@ +#ifndef _CR_GENERATE_ID_SAMPLE_UTIL_H +#define _CR_GENERATE_ID_SAMPLE_UTIL_H + +// キーボードの初期化処理 +void keyboard_initialize( void ); + +// キーボードの終了処理 +void keyboard_finalize( void ); + +// キーボードの(どこかのキーが)が押されたかどうかを返す +// 0 : 押されてない +// 1 : 押されている +int keyboard_is_hit( void ); + +// キーボードの押されているキーを ASCII の値で返す +char keyboard_get_char( void ); + +// 配列を 16 進表記で表示する +void debug_print_array( const char *label, const unsigned char *data, int length ); + +// 配列をバイナリファイルファイルとしてカレントディレクトリに書き出す +// 特にエラーハンドリングしていないので注意 +void debug_file_output( unsigned int id, char *suffix, const unsigned char *data, int length ); + +// 時刻取得し、秒数で返す +double gettimeofday_sec( void ); + +#endif /* _CR_GENERATE_ID_SAMPLE_UTIL_H_ */