156 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			156 lines
		
	
	
		
			3.9 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.
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								****************************************************************************/
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifndef IOT_UTILS_H
							 | 
						||
| 
								 | 
							
								#define IOT_UTILS_H
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* exports includes */
							 | 
						||
| 
								 | 
							
								#include "iot_utils_api.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								extern "C" {
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * iot_mac_addr_hash() - calculate hash value based upon mac address
							 | 
						||
| 
								 | 
							
								 * @add:    pointer to mac address to be caculated
							 | 
						||
| 
								 | 
							
								 * @mask:   mask to be applied to the calculated value
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * return:  calculated hash value
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static inline uint32_t iot_mac_addr_hash(uint8_t* addr, uint32_t mask)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint32_t sum = 0, i = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    while (i < IOT_MAC_ADDR_LEN)
							 | 
						||
| 
								 | 
							
								        sum += addr[i++];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    sum &= mask;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return sum;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * iot_meter_addr_hash() - calculate hash value based upon meter address
							 | 
						||
| 
								 | 
							
								 * @add:    pointer to meter address to be caculated
							 | 
						||
| 
								 | 
							
								 * @mask:   mask to be applied to the calculated value
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * return:  calculated hash value
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static inline uint32_t iot_meter_addr_hash(uint8_t* addr, uint32_t mask)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint32_t sum = 0, i = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    while (i < IOT_METER_ADDR_LEN)
							 | 
						||
| 
								 | 
							
								        sum += addr[i++];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    sum &= mask;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return sum;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * iot_calc_m_ratio() - calculate the multiplex ratio for r1 * r2
							 | 
						||
| 
								 | 
							
								 * @r1:     ratio 1 value
							 | 
						||
| 
								 | 
							
								 * @r2:     ratio 2 value
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * return:  calculated ratio value
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static inline uint8_t iot_calc_m_ratio(uint8_t r1, uint8_t r2)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint8_t ret;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    ret = (uint8_t)((((uint32_t)(r1)) * ((uint32_t)(r2))) / 100);
							 | 
						||
| 
								 | 
							
								    if (ret > 100)
							 | 
						||
| 
								 | 
							
								        ret = 100;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return ret;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * iot_calc_gcd() - calculate the greatest common divisor of two values
							 | 
						||
| 
								 | 
							
								 * @a:     value 1
							 | 
						||
| 
								 | 
							
								 * @b:     value 2
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * return:  calculated greatest common divisor
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static inline uint32_t iot_calc_gcd(uint16_t a, uint16_t b)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    if (a == 0 || b == 0)
							 | 
						||
| 
								 | 
							
								        return 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    while (a != b) {
							 | 
						||
| 
								 | 
							
								        if (a > b)
							 | 
						||
| 
								 | 
							
								            a -= b;
							 | 
						||
| 
								 | 
							
								        else
							 | 
						||
| 
								 | 
							
								            b -= a;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return a;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * iot_calc_lcm() - calculate the least common multiple of two values
							 | 
						||
| 
								 | 
							
								 * @a:     value 1
							 | 
						||
| 
								 | 
							
								 * @b:     value 2
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * return:  calculated least common multiple
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static inline uint32_t iot_calc_lcm(uint16_t a, uint16_t b)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint32_t tmp1, tmp2;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (a == 0 || b == 0)
							 | 
						||
| 
								 | 
							
								        return 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    tmp1 = a * b;
							 | 
						||
| 
								 | 
							
								    tmp2 = iot_calc_gcd(a, b);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return (tmp1 / tmp2);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef int (*iot_timeout_func)(int);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* wait time for fun process */
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief   wait_timeout_fun - wait wait_time_ms for fun
							 | 
						||
| 
								 | 
							
								 * @param func_pointer  [wait function pointer]
							 | 
						||
| 
								 | 
							
								 * @param arg           [obj_func(arg)]
							 | 
						||
| 
								 | 
							
								 * @param target_value  [wait return flag]
							 | 
						||
| 
								 | 
							
								 * @param wait_time_ms  [wait time (ms)]
							 | 
						||
| 
								 | 
							
								 * @return              [ERR_OK or ERR_TIMEOVER]
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint8_t iot_wait_timeout_fun(iot_timeout_func obj_func, \
							 | 
						||
| 
								 | 
							
								    int arg, int target_value, uint32_t wait_time_ms);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef int (*iot_timeout_func2)(void *);
							 | 
						||
| 
								 | 
							
								/* wait time for fun process */
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @brief   iot_wait_timeout_fun2 - wait wait_time_ms for fun
							 | 
						||
| 
								 | 
							
								 * @param func_pointer  [wait function pointer]
							 | 
						||
| 
								 | 
							
								 * @param arg           [obj_func(arg)]
							 | 
						||
| 
								 | 
							
								 * @param target_value  [wait return flag]
							 | 
						||
| 
								 | 
							
								 * @param wait_time_ms  [wait time (ms)]
							 | 
						||
| 
								 | 
							
								 * @return              [ERR_OK or ERR_TIMEOVER]
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint8_t iot_wait_timeout_fun2(iot_timeout_func2 obj_func, \
							 | 
						||
| 
								 | 
							
								    void *arg, int target_value, uint32_t wait_time_ms);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* IOT_UTILS_H */
							 |