Files
kunlun/app/smart_grid/inc/internal/iot_cco_buf.h
2024-09-28 14:24:04 +08:00

232 lines
8.3 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_CCO_BUF_H
#define IOT_CCO_BUF_H
/* iot common includes */
#include "iot_queue_api.h"
#include "iot_mem_pool_api.h"
#include "iot_utils_api.h"
#include "iot_pkt_api.h"
/* os shim includes */
#include "os_types_api.h"
/* smart grid internal header files */
#include "iot_sg_fr.h"
#ifdef __cplusplus
extern "C" {
#endif
/* priority of entry in the buf. IOT_BUF_ENTRY_PRIO_0 means highest priority */
#define IOT_BUF_ENTRY_PRIO_0 0
#define IOT_BUF_ENTRY_PRIO_1 1
#define IOT_BUF_ENTRY_PRIO_2 2
#define IOT_BUF_ENTRY_PRIO_3 3
#define IOT_BUF_MAX_PRIO 3
#define IOT_BUF_PRIO_CNT 4
/* priority for meter reading / secondary reg / state changed report */
#define IOT_BUF_WORK_PRIO IOT_BUF_ENTRY_PRIO_1
/* data type definition */
#define IOT_BUF_DT_INVALID 0 /* invalid data type */
#define IOT_BUF_DT_READ_METER 1 /* sg read meter command */
#define IOT_BUF_DT_SG_METER_DATA 2 /* sg read meter data */
#define IOT_BUF_DT_EVT_RPT 3 /* event report data */
#define IOT_BUF_DT_SEC_NODE_RPT 4 /* secondary node report data */
#define IOT_BUF_DT_STATE_CHG_RPT 5 /* state changed report */
#define IOT_BUF_DT_NODE_STATE_CHG_RPT 6 /* node state changed report */
#define IOT_BUF_DT_EXT_TASK 7 /* sg extension task */
#define IOT_BUF_DT_EXT_TASK_DATA 8 /* sg extension task data */
#define IOT_BUF_OP_ADD 0 /* add operation */
#define IOT_BUF_OP_REMOVE 1 /* remove operation */
/* Buffer entry header. A buf may have large size than iot_buf_pool_entry.
* iot_buf_pool_entry located at the head of the buffer.
* Access the rest space with buf_data pointer.
*/
typedef struct _iot_buf_pool_entry {
/* list head */
iot_list_head_t list_head;
/* age of the entry. It's updated periodically.
* The entry shall be removed when its age reach certain value. */
uint16_t age;
/* In some case like meter reading, CCTT allow 60s to read meter,
* if it take 20s for CCo to get meter data, there are only 40s
* for CCo to report the data to CCTT.
* used_age is 20s here.
*/
uint16_t used_age;
/* age of entry when it's send last time. */
uint16_t last_send_age;
/* send flag */
uint16_t send_flag : 1,
/* emergency flag */
emergency : 1,
/* send wait flag */
send_wait : 1,
/* send count */
send_count : 13;
/* packet type */
uint8_t data_type;
/* priority of this entry */
uint8_t prio;
/* app data */
uint8_t *data;
/* length of app data */
uint16_t len;
/* iot_pkt_t containing data */
iot_pkt_t *pkt;
/* pointer to buf data */
uint8_t buf_data[0];
}iot_buf_pool_entry_t;
/* declare this data type */
struct _iot_buf_pool;
/* callback method on entry count changing */
typedef void(*iot_buf_pool_change_func_t)(struct _iot_buf_pool *buf,
uint32_t op, uint32_t entry_cnt, iot_buf_pool_entry_t *entry);
typedef struct _iot_buf_pool {
/* max entry count */
uint16_t max_entry_limit;
/* free entry count */
uint16_t free_entry_cnt;
/* on adding/removing entry */
iot_buf_pool_change_func_t on_change;
/* list head for buf list */
iot_list_head_t list_head[IOT_BUF_PRIO_CNT];
/* memory pool for the buffer */
iot_mem_pool_t buf_pool;
}iot_buf_pool_t;
/* callback method on buf pool entry.
* Stop iterating on buf pool entry when this method return non-zero.
*/
typedef uint32_t (*iot_buf_loop_func_t)(iot_buf_pool_entry_t *entry,
uint32_t arg);
/* callback method on buf pool entry.
* check buf data send requirement.
* @retval: ERR_OK for send requirement.
ERR_FAIL for stop to check requirement.
ERR_AGAIN for continue to next check.
*/
typedef uint8_t(*iot_sg_cco_check_entry_func_t)(iot_buf_pool_entry_t *entry,
void *param);
/**
* @brief create a iot_buf_pool_t
* @param result: the iot_buf_pool to store the new created
* @param entry_cnt: max entry count in the buffer
* @param entry_size: size of each entry
* @param cb: callback on adding/removing entry
* @retval: ERR_OK for succeed case
* other value for failed case. see ERR_XXX.
*/
uint32_t iot_buf_pool_create(iot_buf_pool_t *result, uint16_t entry_cnt,
uint16_t entry_size, iot_buf_pool_change_func_t cb);
/**
* @brief: destroy a iot_buf_pool
* @param buf: the buf pool to be destroyed
*/
void iot_buf_pool_destroy(iot_buf_pool_t *buf);
/**
* @brief get entry count in a buf pool entry.
* @param buf: the buf pool whose free count buf will be retrieved
* @retval: number of free entry count
*/
uint16_t iot_buf_pool_get_entry_cnt(iot_buf_pool_t *buf);
/**
* @brief get count of free a buf pool entry.
* @param buf: the buf pool whose free count buf will be retrieved
* @retval: number of free entry count
*/
uint16_t iot_buf_pool_get_free_entry_cnt(iot_buf_pool_t *buf);
/**
* @brief: add a buf_entry to buf pool
* @param buf: the buf pool
* @param entry:the entry to be added to buf pool
* @retval: ERR_OK - if the entry is added successfully
* ERR_NOMEM - if failed because of no entry in the iot_buf_pool
*/
uint32_t iot_buf_pool_add_entry(iot_buf_pool_t *buf,
iot_buf_pool_entry_t *entry);
/**
* @brief remove an entry from buf pool
* @param buf: the buf whose entry will be removed
* @param entry: the entry to be removed
*/
void iot_buf_pool_rm_entry(iot_buf_pool_t *buf, iot_buf_pool_entry_t *entry);
/**
* @brief remove all entries from buf pool
* @param buf: the buf whose entry will be removed
*/
void iot_buf_pool_clear(iot_buf_pool_t *buf);
/**
* @brief get an entry from buf pool to send.
* @param buf: the buf pool from which to get an entry to send.
* @param entry_pos: iot_buf_pool_entry_t start position.
* Search for available entry from entry_pos->next.
* @param cb callback on check if need to send.If cb return ERR_OK
* can be sent.
* @param param: parameter to be passed to the callback
* @param prio_style:flag to mark if open strict priority style.
* @retval: the entry to send
*/
iot_buf_pool_entry_t* iot_buf_pool_get_entry_to_send(iot_buf_pool_t *buf,
iot_buf_pool_entry_t *entry_pos, iot_sg_cco_check_entry_func_t cb,
void *param, uint8_t prio_style);
/**
* @brief: loop over a given buffer and perform operation on each entry
* @param buf: the buffer pool from which to loop on
* @param cb: a callback method to be executed on each entry
* @param arg: extra argument for cb
* @retval: the buf pool entry on which cb return non-zero.
*/
iot_buf_pool_entry_t* iot_buf_pool_loop(iot_buf_pool_t *buf,
iot_buf_loop_func_t cb, uint32_t arg);
/**
* @brief: init a buf pool entry
* @param entry: the entry to be initialized
* @param data_type: data type of the entry
* @param prio: the priority of the entry
* @param emergency: emergency flag
* @param pkt: the iot_pkt to be add to buf pool
*/
void iot_buf_pool_entry_init(iot_buf_pool_entry_t* entry, uint8_t data_type,
uint8_t prio, uint8_t emergency, iot_pkt_t *pkt);
#ifdef __cplusplus
}
#endif
#endif /* IOT_CCO_BUF_H */