166 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			166 lines
		
	
	
		
			5.2 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.
 | |
| 
 | |
| ****************************************************************************/
 | |
| 
 | |
| #include "iot_io_api.h"
 | |
| #include "iot_pkt_api.h"
 | |
| #include "iot_plc_msg_api.h"
 | |
| #include "iot_errno_api.h"
 | |
| #include "iot_app_meta_api.h"
 | |
| #include "iot_app_meta_handle.h"
 | |
| #include "iot_app_meta_buffer_handle.h"
 | |
| 
 | |
| #define META_MAGIC                  (0x57555149) /* Aerospace C.Power */
 | |
| 
 | |
| /* recv plc msdu, check meta info */
 | |
| void iot_check_meta_data(iot_pkt_t *msdu_pkt)
 | |
| {
 | |
|     iot_meta_info_t *meta_info;
 | |
|     uint16_t meta_info_len = 0;
 | |
|     uint8_t *data = iot_pkt_data(msdu_pkt);
 | |
|     uint8_t *data_buf;
 | |
|     uint16_t len = iot_pkt_data_len(msdu_pkt);
 | |
|     iot_plc_msdu_recv_t *msdu;
 | |
| 
 | |
|     if (len > sizeof(iot_meta_info_t)) {
 | |
|         meta_info = (iot_meta_info_t*)(data + len - sizeof(iot_meta_info_t));
 | |
|         iot_cus_printf("meta_info->magic=%X\n", meta_info->magic);
 | |
|         if (meta_info->magic == META_MAGIC) {
 | |
|             meta_info_len = meta_info->used_len + sizeof(iot_meta_info_t);
 | |
|             msdu = (iot_plc_msdu_recv_t*)(data + sizeof(iot_plc_msg_header_t));
 | |
|             msdu->len = len - meta_info_len - sizeof(iot_plc_msg_header_t) -
 | |
|                 sizeof(iot_plc_msdu_recv_t);
 | |
|             data_buf = (uint8_t *)meta_info - meta_info->used_len;
 | |
|             iot_cus_printf("recv:%02X%02X%02X%02X%02X%02X meta_info_len=%d\n",
 | |
|                 meta_info->src_mac[0], meta_info->src_mac[1],
 | |
|                 meta_info->src_mac[2], meta_info->src_mac[3],
 | |
|                 meta_info->src_mac[3], meta_info->src_mac[5],
 | |
|                 meta_info_len);
 | |
|             if (meta_info->used_len) {
 | |
|                 iot_meta_cmd_handle(data_buf, meta_info->used_len);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     if (meta_info_len) {
 | |
|         //meta info handle and reduce msdu pkt
 | |
|         iot_pkt_shrink(msdu_pkt, meta_info_len);
 | |
|     }
 | |
| }
 | |
| 
 | |
| #if IOT_ENABLE_ADD_PADDING_INFO
 | |
| 
 | |
| static uint8_t meta_inited = 0;
 | |
| 
 | |
| /* add meta info to msdu pkt */
 | |
| void iot_plc_add_meta_info(iot_pkt_t *msdu_pkt, uint16_t meta_len)
 | |
| {
 | |
|     uint8_t *msdu_data;
 | |
|     uint16_t msdu_len;
 | |
|     uint16_t used_len = 0;
 | |
|     iot_meta_info_t *meta_info;
 | |
|     iot_plc_msdu_send_t *msdu;
 | |
| 
 | |
|     msdu_data = iot_pkt_data(msdu_pkt);
 | |
|     msdu_len = iot_pkt_data_len(msdu_pkt);
 | |
|     msdu = (iot_plc_msdu_send_t*)(msdu_data + sizeof(iot_plc_msg_header_t));
 | |
| 
 | |
|     /* msdu应该减去meta_len情况:
 | |
|        1.app 没有执行iot_meta_init进行初始化.
 | |
|        2.计算出的meta_len太小,meta头都放不下.
 | |
|        3.未从meta buffer获取到meta信息数据时
 | |
|     */
 | |
|     if (meta_inited == 0) {
 | |
|         /* meta not init or init fail */
 | |
|         goto out;
 | |
|     }
 | |
| 
 | |
|     if (meta_len < sizeof(iot_meta_info_t)) {
 | |
|         iot_cus_printf("meta_len too small\n");
 | |
|         goto out;
 | |
|     }
 | |
| 
 | |
|     used_len = iot_meta_get_cmd_from_buf(msdu_data + msdu_len,
 | |
|         meta_len - sizeof(iot_meta_info_t));
 | |
|     iot_cus_printf("meta_len=%d, meta used length is %d\n", meta_len, used_len);
 | |
| 
 | |
|     if (used_len == 0) {
 | |
|         goto out;
 | |
|     }
 | |
|     iot_pkt_put(msdu_pkt, meta_len);
 | |
|     meta_info = (iot_meta_info_t*)(msdu_data + msdu_len +
 | |
|         meta_len - sizeof(iot_meta_info_t));
 | |
|     meta_info->magic = META_MAGIC;
 | |
|     meta_info->used_len= meta_len - sizeof(iot_meta_info_t);
 | |
|     iot_mac_addr_cpy(meta_info->src_mac, msdu->src);
 | |
|     return;
 | |
| 
 | |
| out:
 | |
|     /* not add meta, need reduce meta len from msdu */
 | |
|     msdu->len -= meta_len;
 | |
|     return;
 | |
| }
 | |
| 
 | |
| uint8_t iot_meta_make_query_cmd(uint16_t id_group, uint16_t id)
 | |
| {
 | |
|     if (meta_inited == 0) {
 | |
|         /* meta not init or init fail */
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
|     return meta_make_query_cmd(id_group, id);
 | |
| }
 | |
| 
 | |
| uint8_t iot_meta_rcd_app_msdu_info(iot_plc_msdu_recv_t *msdu, uint8_t conn_type)
 | |
| {
 | |
|     if (meta_inited == 0) {
 | |
|         /* meta not init or init fail */
 | |
|         return 0;
 | |
|     }
 | |
|     return meta_rcd_app_msdu_info(msdu, conn_type);
 | |
| }
 | |
| 
 | |
| void iot_meta_init(uint8_t app_id, uint16_t module_id)
 | |
| {
 | |
|     iot_meta_buf_init(module_id);
 | |
|     iot_meta_info_init(app_id, module_id);
 | |
|     meta_inited = 1;
 | |
| }
 | |
| #else /* else IOT_ENABLE_ADD_PADDING_INFO */
 | |
| 
 | |
| void iot_plc_add_meta_info(iot_pkt_t *msdu_pkt, uint16_t meta_len)
 | |
| {
 | |
|     (void)msdu_pkt;
 | |
|     (void)meta_len;
 | |
| }
 | |
| 
 | |
| uint8_t iot_meta_make_query_cmd(uint16_t id_group, uint16_t id)
 | |
| {
 | |
|     (void)id_group;
 | |
|     (void)id;
 | |
|     return ERR_FAIL;
 | |
| }
 | |
| 
 | |
| uint8_t iot_meta_rcd_app_msdu_info(iot_plc_msdu_recv_t *msdu, uint8_t conn_type)
 | |
| {
 | |
|     (void)msdu;
 | |
|     (void)conn_type;
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| void iot_meta_init(uint8_t app_id, uint16_t module_id)
 | |
| {
 | |
|     (void)app_id;
 | |
|     (void)module_id;
 | |
| }
 | |
| #endif /* end IOT_ENABLE_ADD_PADDING_INFO */ |