/**********************************************************                     
 *                                                                              
 *          * SATELLITE TRANSMIT SYSTEM *                               
 *                                                                              
 *      P.NAME      : ascbcd                                                    
 *                                                                              
 *      FUNCTION    : ASCII-CODE TO BCD-CODE                                    
 *                                                                              
 *      CALLING SEQUENCE                                                        
 *          ascbcd( asc , bcd , leng , ir , ispos , iepos )                     
 *                                                                              
 *      RETURN VALUE                                                            
 *          int     :  0 normal end                                             
 *                  : -1 error  end                                             
 *                                                                              
 *      ARGUMENT                                                                
 *          asc     : (IN) (*char) data start pointer(ascii)                    
 *          bcd     : (OUT)(*int)  output bcd data                              
 *          leng    : (IN) (*inT)  change character length                      
 *          ir      : (OUT)(*int)  return                                       
 *                      0 : normal                                              
 *                     -1 : error                                               
 *          ispos   : (IN) (*int)  input  data start pointer (ascii)            
 *          iepos   : (IN) (*int)  output data start pointer (bcd)              
 *                                                                              
 *      DATE        : 90.10.18 S.OGATA                                          
 *                    2012/11/15  Ver2.0 y.yoneda  インデントの調整             
 *                                                                              
 *********************************************************/                     
#include        <ctype.h>                                                      
#include        <inttypes.h>                                                   
#include        "Com_type.h"                                                    
#include        "Com_bcd.h"                                                    

static int32_t loc_atoi( int32_t data );

int                                                                             
com_ascbcd_( const char *asc , uint8_t *bcd , int *leng , int *ir , int *ispos , int *iepos ) {                                 
/* char asc[]; */                                                            
/* uint8_t bcd[] ; */                                                            
/* int  *leng , *ir , *ispos , *iepos ; */                                            

    union uni un;                                                               
    int32_t   start_point , end_point , asc_point , w1 , w2 , i ;                   
	int	order;
                                /* ERROR CHECK */                               
    if( *leng < 1 || *ispos < 1 || *iepos < 1 )                                 
    {                                                                           
        *ir = -1 ;                                                              
        return( -1 ) ;                                                          
    }                                                                           
                                /* DATA SET */                                  
    asc_point   = *ispos + *leng - 2 ;                                          
    start_point = *iepos - 1 ;                                                  
    end_point   = *leng / 2 + start_point - 1 ;                                 
                                /* 4BIT DATA */                                 
    if( *leng % 2 != 0 )                                                        
    {                                                                           
        un.bit               = bcd[ end_point + 1 ] ;                           
        if( ( w1 = loc_atoi( (int32_t)asc[ asc_point-- ] ) ) == -1  )               
        {                                                                       
            *ir = -1 ;                                                          
            return( -1 ) ;                                                      
        }                                                                       
        order = 1;
        if(*((char *) &order)){
          	un.st.bit_low = (uint32_t)w1;		/* Chg Ver2.0 */
        } else {
	  		un.st.bit_high = (uint32_t)w1;		/* Chg Ver2.0 */
		}
        bcd[ end_point + 1 ] = un.bit ;                                         
    }                                                                           
                                /* LENGTH = 1 : END */                          
    if( end_point < 0 )                                                         
    {                                                                           
        *ir = 0 ;                                                               
        return( 0 ) ;                                                           
    }                                                                           
                                /* DATA CHANGE */                               
    for( i=end_point ; i>=start_point ; i-- )                                   
    {                                                                           
        if( ( w2 = (int32_t)loc_atoi( (int32_t)asc[ asc_point-- ] ) ) == -1 ||          
            ( w1 = (int32_t)loc_atoi( (int32_t)asc[ asc_point-- ] ) ) == -1 )           
        {                                                                       
            *ir = -1 ;                                                          
            return( -1 ) ;                                                      
        }                                                                       
        order = 1;
        if(*((char *) &order)){
        	un.st.bit_high = (uint32_t)w2;		/* Chg Ver2.0 */
        	un.st.bit_low  = (uint32_t)w1;		/* Chg Ver2.0 */
        } else {
        	un.st.bit_high = (uint32_t)w1;		/* Chg Ver2.0 */
        	un.st.bit_low  = (uint32_t)w2;		/* Chg Ver2.0 */
        }
		bcd[ i ] = un.bit ;                                               
    }                                                                           
    *ir = 0 ;                                                                   
    return( 0 ) ;                                                               
}                                                                               
                                /* ASCII TO BINARY */                           
static int32_t                                                                  
loc_atoi( data )                                                                
int32_t data ;                                                                      
{                                                                               
    if( isdigit(data) == 0 )                                                    
    {                                                                           
        return( -1 ) ;                                                          
    }                                                                           
    return( data - '0' ) ;                                                      
}