Files
kunlun/plc/htbus/src/htbus_test.c
2024-09-28 14:24:04 +08:00

212 lines
5.6 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.
****************************************************************************/
/* os_shim header files */
#include "os_types_api.h"
#include "os_task_api.h"
#include "os_utils_api.h"
#include "os_timer_api.h"
/* iot common header files */
#include "iot_config_api.h"
#include "iot_io_api.h"
#include "iot_module_api.h"
#include "iot_errno_api.h"
#include "iot_task_api.h"
#include "iot_utils_api.h"
/* htbus internal header files */
#include "htbus_msg.h"
#include "htbus.h"
#include "htbus_proto.h"
/* define test item */
#define HTBUS_TEST_ID_COMM_RATE 1
#define HTBUS_TEST_ID HTBUS_TEST_ID_COMM_RATE
#define HTBUS_TEST_DATA_LEN (491)
/* htbus test context structure */
typedef struct {
uint32_t tx_cnt;
uint32_t rx_cnt;
uint32_t start_time;
uint32_t cnt;
timer_id_t trig_timer;
} htbus_test_ctxt_t;
#pragma pack(push) /* save the pack status */
#pragma pack(1) /* 1 byte align */
#define HTBUS_TEST_MSG_T_COMM_RATE 0
/* test massage head */
typedef struct {
/* massage type, see HTBUS_TEST_MSG_T_XXX */
uint8_t msg_type;
/* payload label */
uint8_t data[0];
} htbus_test_msg_hdr_t;
#pragma pack(pop) /* restore the pack status */
#if (IOT_HTBUS_EN)
htbus_test_ctxt_t g_htbus_test_ctxt = { 0 };
iot_pkt_t * htbus_alloc_test_pkt(uint8_t dst_tei, uint8_t msg_type,
uint16_t pad_len)
{
iot_pkt_t *pkt;
uint8_t *data;
htbus_test_msg_hdr_t *test_hdr;
htbus_sdu_hdr_t *sdu_hdr;
uint16_t size = sizeof( *sdu_hdr) + sizeof(*test_hdr) + pad_len;
pkt = iot_pkt_alloc(size, HTBUS_MID);
if (pkt == NULL) {
return NULL;
}
data = iot_pkt_data(pkt);
sdu_hdr = (htbus_sdu_hdr_t *)data;
sdu_hdr->src_tei = htbus_get_local_tei();
sdu_hdr->dst_tei = dst_tei;
sdu_hdr->sn = htbus_get_sdu_sn();
sdu_hdr->len = pad_len;
sdu_hdr->fn = HTBUS_PROTO_FN_COMM_TEST;
sdu_hdr->len += sizeof(*test_hdr);
test_hdr = (htbus_test_msg_hdr_t*)sdu_hdr->data;
test_hdr->msg_type = msg_type;
iot_pkt_put(pkt, size);
return pkt;
}
#if PLC_SUPPORT_CCO_ROLE
void htbus_test_cco_data_handle(htbus_sdu_hdr_t* sdu_hdr,
iot_pkt_t *pkt)
{
htbus_test_msg_hdr_t *test_hdr = (htbus_test_msg_hdr_t *)sdu_hdr->data;
switch (test_hdr->msg_type) {
case HTBUS_TEST_MSG_T_COMM_RATE:
{
g_htbus_test_ctxt.rx_cnt++;
break;
}
default:
break;
}
iot_pkt_free(pkt);
}
void htbus_test_cco_dump()
{
uint32_t cur_time = os_boot_time32();
uint32_t time_cons;
switch (HTBUS_TEST_ID) {
case HTBUS_TEST_ID_COMM_RATE:
{
time_cons = (uint32_t)(cur_time - g_htbus_test_ctxt.start_time);
float sr = (g_htbus_test_ctxt.tx_cnt + g_htbus_test_ctxt.rx_cnt) *
HTBUS_TEST_DATA_LEN * 8.0 / time_cons;
iot_printf("%s - comm rate test: sr=%fkbps, time_cons=%lums\n",
__FUNCTION__, sr, time_cons);
break;
}
default:
break;
}
}
void htbus_test_cco_hanlde(void)
{
uint32_t result = ERR_OK;
iot_pkt_t *pkt;
switch (HTBUS_TEST_ID) {
case HTBUS_TEST_ID_COMM_RATE:
{
pkt = htbus_alloc_test_pkt(HTBUS_TEI_BCAST,
HTBUS_TEST_MSG_T_COMM_RATE, HTBUS_TEST_DATA_LEN);
if (pkt == NULL) {
break;
}
result = htbus_tx_sdu(pkt);
if (result == ERR_OK) {
g_htbus_test_ctxt.tx_cnt++;
}
break;
}
default:
IOT_ASSERT(0);
break;
}
g_htbus_test_ctxt.cnt++;
if (g_htbus_test_ctxt.cnt >= 100) {
g_htbus_test_ctxt.cnt = 0;
htbus_test_cco_dump();
}
}
static void htbus_test_timer_func(timer_id_t timer_id, void * arg)
{
(void)timer_id;
(void)arg;
os_set_task_event_with_v(iot_task_get_os_task_h(p_htbus_glb->task_h),
1 << HTBUS_EVENT_TEST_TRIG);
}
void htbus_test_cco_starup(void)
{
if (!HTBUS_TEST_ID)
return;
g_htbus_test_ctxt.trig_timer = os_create_timer(IOT_DRIVER_MID, true,
htbus_test_timer_func, NULL);
IOT_ASSERT(g_htbus_test_ctxt.trig_timer);
os_start_timer(g_htbus_test_ctxt.trig_timer, 10);
g_htbus_test_ctxt.start_time = os_boot_time32();
iot_printf("%s\n",__FUNCTION__);
}
#endif
#if PLC_SUPPORT_STA_ROLE
void htbus_test_sta_data_handle(htbus_sdu_hdr_t* sdu_hdr, iot_pkt_t *pkt)
{
htbus_test_msg_hdr_t *test_hdr = (htbus_test_msg_hdr_t *)sdu_hdr->data;
switch (test_hdr->msg_type) {
case HTBUS_TEST_MSG_T_COMM_RATE:
{
sdu_hdr->dst_tei = sdu_hdr->src_tei;
sdu_hdr->src_tei = htbus_get_local_tei();
sdu_hdr->sn = htbus_get_sdu_sn();
iot_pkt_set_data(pkt, sdu_hdr);
iot_pkt_set_tail(pkt, (uint8_t *)sdu_hdr + sizeof(* sdu_hdr) +
sdu_hdr->len);
htbus_tx_sdu(pkt);
break;
}
default:
iot_pkt_free(pkt);
break;
}
}
#endif
#endif /* IOT_HTBUS_EN */