Tech & IT/프로그래밍

HAL 뼈대가 Test 코드 및 예제

해피콧 2009. 12. 11. 10:31
'); }
'); }
예제 코드 : code warrier용


1. VC++로 확인 Test 예제 
#include <stdio.h>
#include <stdlib.h>

int flash_open(char *str) { printf("[ Flash Open : %s ]\n", str) ; return 0 ; }
int flash_read() { printf("[ Flash Read ]\n") ; return 0 ; }
int flash_write() { printf("[ Flash Write ]\n") ; return 0 ; }
int flash_close() { printf("[ Flash Close ]\n") ; return 0 ; }
int flash_ioctl() { printf("[ Flash Ioctl ]\n") ; return 0 ; }

int serial_open(char *str) { printf("[ Serial Open : %s ]\n", str) ; return 0 ;}
int serial_read() { printf("[ Serial Read ]\n") ; return 0 ; }
int serial_write() { printf("[ Serial Write ]\n") ; return 0 ; }
int serial_close() { printf("[ Serial Close ]\n") ; return 0 ; }
int serial_ioctl() { printf("[ Serial Ioctl ]\n") ; return 0 ; }


typedef struct _device_operations
{
char *dev_name ;
int M_num ;
int (*open)() ;
int (*close)() ;
int (*ioctl)() ;
int (*read)() ;
int (*write)() ;
} device_operations;

typedef struct _device_info
{
int  flag ;
int  minor ;
int  mode ;
char *inbuf ;
char *str ;
int  buf_size ;
device_operations *device_op ;
} device_info;

device_operations flash_operation = { "Flash", 10, 
flash_open, flash_close, flash_ioctl, flash_read, flash_write } ;
device_operations serial_operation = { "serial", 15, 
serial_open, serial_close, serial_ioctl, serial_read, serial_write } ;


device_operations *device_oper_table[20] = { '\0' };
device_info   device_info_table[40] = { '\0' };

int  register_operation(int dev_num, device_operations *pdop) ;
int  unregister_operation(int dev_num, device_operations *pdop) ;

int  open(device_info *info, int dev_num, char *str) 
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].flag == 0)
break ;
device_info_table[u] = *info ;
device_info_table[u].flag = 1 ;
device_info_table[u].str = str ;
device_info_table[u].device_op = device_oper_table[dev_num] ;
device_info_table[u].device_op->open(str) ;
return 0 ;
}

int read(int dev_num, int minor)
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].minor == minor)
if(device_info_table[u].device_op->M_num == dev_num) {
printf("[ %s : %s ]\t", 
device_info_table[u].inbuf, device_info_table[u].str) ;
device_info_table[u].device_op->read() ;
return 0 ;
}
return -1 ;
}

int write(int dev_num, int minor)
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].minor == minor)
if(device_info_table[u].device_op->M_num == dev_num) {
printf("[ %s : %s ]\t", 
device_info_table[u].inbuf, device_info_table[u].str) ;   
device_info_table[u].device_op->write() ;
return 0 ;
}
return -1 ;
}

int close(int dev_num, int minor)
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].minor == minor)
if(device_info_table[u].device_op->M_num == dev_num) {
printf("[ %s : %s ]\t", 
device_info_table[u].inbuf, device_info_table[u].str) ;    
device_info_table[u].device_op->close() ;
device_info_table[u].flag = 0 ;
return 0 ;
}
return -1 ;
}

 


void main()
{
device_info flash0 = { 0, 0, 0x7f23, "Flash 0", 0, } ;
device_info flash1 = { 0, 1, 0xff46, "Flash 1", 0, } ;
device_info serial0 = { 0, 0, 0x45f2, "Serial 0", 0, } ; 
device_info serial1 = { 0, 1, 0x65ea, "Serial 1", 0, } ;

register_operation(10, &flash_operation) ;  
register_operation(15, &serial_operation) ;

open(&flash0, 10, "Arminia-209") ;  
open(&flash1, 10, "GL-209") ;  
open(&serial0, 15, "Ellias-404") ;  
open(&serial1, 15, "Deillous-404") ;
printf("\n") ;
read(15, 1) ;    read(10, 0) ;
write(10, 1) ;
close(15, 0) ;

unregister_operation(10, '\0') ;
unregister_operation(15, '\0') ;
}


int  register_operation(int dev_num, device_operations *pdop)
{
device_oper_table[dev_num] = pdop ;
return 0 ;
}

int  unregister_operation(int dev_num, device_operations *pdop)
{
device_oper_table[dev_num] = pdop ;
return 0 ;
}