Files
kunlun/app/iot_micro_cctt_app/cctt/iot_cctt_utils.h
2024-09-28 14:24:04 +08:00

206 lines
6.0 KiB
C
Executable File

/****************************************************************************
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_CCTT_UTILS_H
#define IOT_CCTT_UTILS_H
#pragma pack(push) // save the pack status
#pragma pack(1) // 1 byte align
typedef struct {
uint16_t preamble; // 0xAAAA
uint8_t cctt_id[4];
uint8_t app_id; // 0x00
uint8_t fn; // 0x44
uint8_t sub_fn; // 0x20
uint16_t seq;
uint8_t need_ack; // 0x01
uint8_t reserved; // 0xFF
uint16_t index;
uint8_t length[2]; // 0x00 0x06
uint8_t mac[6];
uint8_t crc16[2];
uint8_t tail; // 0xFF
} iot_cctt_tools_login_frame_t;
#pragma pack(pop)
typedef enum {
/* requested by "AT+REBOOT" command from at manager */
IOT_CCTT_REBOOT_REASON_MGR_AT_CMD_REQ = 0,
/* requested by iot application because of disconnect timeout */
IOT_CCTT_REBOOT_REASON_IOT_DISCONN_TO,
/* the number of cctt reboot reason */
IOT_CCTT_UTILS_REBOOT_REASON_NUM,
} iot_cctt_utils_reboot_cctt_reason_e;
typedef enum {
IOT_CCTT_GPIO_ID_ETH_RST = 0, /* ethernet reset pin */
IOT_CCTT_GPIO_ID_4G_RST, /* 4g module reset pin */
IOT_CCTT_GPIO_ID_EXT_WDI, /* externel watchdog kick */
IOT_CCTT_GPIO_ID_RSD_SWITCH_IN,/* rsd switch input pin */
IOT_CCTT_GPIO_ID_RSD_LED_OUT, /* rsd led output pin */
IOT_CCTT_GPIO_ID_RS485_CTL, /* rs485 control pin */
IOT_CCTT_GPIO_ID_RS485_LED, /* rs485 led output pin */
IOT_CCTT_GPIO_ID_UPGD_LED, /* upgrade led output pin */
IOT_CCTT_GPIO_ID_PLC_LED, /* plc state output pin */
IOT_CCTT_GPIO_ID_UPLINK_LED, /* uplink state output pin */
} iot_cctt_utils_gpio_id_e;
/**
* @brief : query cctt plc mac address
*
* @param p_mac : buffer to store mac address
* @return ERR_OK if success, error code otherwise
*/
uint8_t iot_cctt_utils_get_cctt_mac(uint8_t *p_mac)
;
/**
* @brief : get cctt ethernet mac address
* try to use mac address of cco firstly, if failed, use random mac
*
* @param p_mac : buffer to store mac address
* @return none
*/
void iot_cctt_utils_get_eth_mac(uint8_t *p_mac);
/**
* @brief : set cctt plc mac address
*
* @param p_mac : buffer to store mac address
* @return ERR_OK if success, error code otherwise
*/
uint8_t iot_cctt_utils_set_cctt_mac(uint8_t *p_mac)
;
/**
* @brief : build login frame
*
* @param p_buff : buffer to store login frame
* @param len : length of p_frame
* @return ERR_OK if success, error code otherwise
*/
uint8_t iot_cctt_utils_build_dbg_svr_login_frm(uint8_t appid,
uint8_t *p_buff,
uint32_t len);
/**
* @brief : check whether the data in p_buff is query frame from debug server
*
* @param p_buff : pointer to data to check
* @param len : length of data
* @return true if it's a query frame, false otherwise
*/
bool_t iot_cctt_utils_is_dbg_svr_query_frm(uint8_t *p_buff, uint32_t len);
/**
* @brief : dump data to log buffer
*
* @param descr : descrption of data
* @param p_data : data to dump
* @param len : length of data
* @return void
*/
void iot_cctt_utils_dump_bin(char *descr, uint8_t *p_data, uint32_t len);
/**
* @brief : smart dump data to log buffer, if data is all printable string
* dump as string format, if not, dump as hex format otherwise
*
* @param descr : descrption of data
* @param p_data : data to dump
* @param len : length of data
* @return void
*/
void iot_cctt_utils_smart_dump(char *descr, uint8_t *p_data, uint32_t len);
/**
* @brief : check if character is valid hex char
*
* @param c : character value to check
* @return 1 if hex char, 0 otherwise
*/
uint8_t iot_cctt_utils_char_is_hex(char c);
/**
* @brief : check if character is valid number char
*
* @param c : character value to check
* @return 1 if hex char, 0 otherwise
*/
uint8_t iot_cctt_utils_char_is_number(char c);
/**
* @brief : parse ip address from specified string
*
* @param p_str : string to parse
* @param p_ip : buffer to store ip address
* @return ERR_OK if success, error code otherwise
*/
uint8_t iot_cctt_utils_ip_str_parse(uint8_t *p_str, uint8_t *p_ip);
/**
* @brief: reboot cctt
*
* @param reason: reason of requesting to reboot cctt
* @return none
*/
void iot_cctt_utils_reboot_cctt(iot_cctt_utils_reboot_cctt_reason_e reason);
/**
* @brief: Convert seconds since 01-01-1970 00:00:00 to iot_time_tm_t date.
*
* @param time: seconds since 01-01-1970 00:00:00
* @param p_tm: iot_time_tm_t pointer to store converted time
* @return none
*/
void iot_cctt_utils_time64_to_tm(uint64_t time, iot_time_tm_t *p_tm);
/*
* @brief: Converts Gregorian date to seconds since 1970-01-01 00:00:00.
* Assumes input in normal date format, i.e. 1980-12-31 23:59:59
* => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
*
* @param p_tm : pointed to iot_time_tm_t
* @return seconds since 1970-01-01 00:00:00
*/
uint64_t iot_cctt_utils_tm_to_time64(iot_time_tm_t *p_tm);
/*
* @brief: query gpio pin number according to gpio id.
*
* @param gpio_id: gpio identifier, see iot_cctt_utils_gpio_id_e definition
* @return gpio number if success, 0xFF otherwise
*/
uint8_t iot_cctt_utils_get_gpio(iot_cctt_utils_gpio_id_e gpio_id);
#endif