90 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 | 
						|
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | 
						|
 | 
						|
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | 
						|
be copied by any method or incorporated into another program without
 | 
						|
the express written consent of Aerospace C.Power. This Information or any portion
 | 
						|
thereof remains the property of Aerospace C.Power. The Information contained herein
 | 
						|
is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | 
						|
liability for its use in any way and conveys no license or title under
 | 
						|
any patent or copyright and makes no representation or warranty that this
 | 
						|
Information is free from patent or copyright infringement.
 | 
						|
 | 
						|
****************************************************************************/
 | 
						|
 | 
						|
#include "os_types.h"
 | 
						|
 | 
						|
#define BIG_LITTLE_ENDIAN_SWAP16(a)  ((((uint16_t)(a) & 0xff00) >> 8) | \
 | 
						|
                                      (((uint16_t)(a) & 0x00ff) << 8))
 | 
						|
 | 
						|
#define BIG_LITTLE_ENDIAN_SWAP32(a)  ((((uint32_t)(a) & 0xff000000) >> 24)| \
 | 
						|
                                      (((uint32_t)(a) & 0x00ff0000) >> 8) | \
 | 
						|
                                      (((uint32_t)(a) & 0x0000ff00) << 8) | \
 | 
						|
                                      (((uint32_t)(a) & 0x000000ff) << 24))
 | 
						|
 | 
						|
#define LITTLE_ENDIAN              0
 | 
						|
#define BIG_ENDIAN                 1
 | 
						|
#define UNKNOWN_ENDIAN             0xffffffff
 | 
						|
 | 
						|
static uint32_t g_cpu_endian = UNKNOWN_ENDIAN;
 | 
						|
 | 
						|
/*
 | 
						|
 * check_cpu_endian() - check cpu's endianess
 | 
						|
 * return:
 | 
						|
        - 1 bigendian
 | 
						|
        - 0 little endian
 | 
						|
 */
 | 
						|
static uint32_t inline check_cpu_endian()
 | 
						|
{
 | 
						|
    union{
 | 
						|
        uint32_t i;
 | 
						|
        uint8_t s[4];
 | 
						|
    }c;
 | 
						|
 | 
						|
    c.i = 0x12345678;
 | 
						|
    return (0x12 == c.s[0]);
 | 
						|
}
 | 
						|
 | 
						|
uint32_t iot_htonl(uint32_t h)
 | 
						|
{
 | 
						|
    if(g_cpu_endian == UNKNOWN_ENDIAN){
 | 
						|
        g_cpu_endian = check_cpu_endian();
 | 
						|
    }
 | 
						|
    return (g_cpu_endian == BIG_ENDIAN)? h : BIG_LITTLE_ENDIAN_SWAP32(h);
 | 
						|
}
 | 
						|
 | 
						|
uint32_t iot_ntohl(uint32_t n)
 | 
						|
{
 | 
						|
    if(g_cpu_endian == UNKNOWN_ENDIAN){
 | 
						|
        g_cpu_endian = check_cpu_endian();
 | 
						|
    }
 | 
						|
    return (g_cpu_endian == BIG_ENDIAN) ? n : BIG_LITTLE_ENDIAN_SWAP32(n);
 | 
						|
}
 | 
						|
 | 
						|
uint16_t iot_htons(uint16_t h)
 | 
						|
{
 | 
						|
    if(g_cpu_endian == UNKNOWN_ENDIAN){
 | 
						|
        g_cpu_endian = check_cpu_endian();
 | 
						|
    }
 | 
						|
    return (g_cpu_endian == BIG_ENDIAN) ? h : BIG_LITTLE_ENDIAN_SWAP16(h);
 | 
						|
}
 | 
						|
 | 
						|
uint16_t iot_ntohs(uint16_t n)
 | 
						|
{
 | 
						|
    if(g_cpu_endian == UNKNOWN_ENDIAN){
 | 
						|
        g_cpu_endian = check_cpu_endian();
 | 
						|
    }
 | 
						|
    return (g_cpu_endian == BIG_ENDIAN) ? n : BIG_LITTLE_ENDIAN_SWAP16(n);
 | 
						|
}
 | 
						|
 | 
						|
uint64_t iot_htonll(uint64_t h)
 | 
						|
{
 | 
						|
    return (((uint64_t) iot_htonl((uint32_t)h)) << 32) + iot_htonl(h >> 32);
 | 
						|
}
 | 
						|
 | 
						|
uint64_t iot_ntohll(uint64_t n)
 | 
						|
{
 | 
						|
    return (((uint64_t) iot_ntohl((uint32_t)n)) << 32) + iot_ntohl(n >> 32);
 | 
						|
}
 |