// 创建事件包的步骤 // 1. 使用create_params_block添加第一个参数 // 2. add_next_param直到添加完全部参数 // 3. create_event_package创建数据包,并delete_params_block来释放params_block struct sub_device_buffer * FUNCTION_ATTRIBUTE construct_sub_device_event() { struct sub_device_buffer *event_buffer = NULL; struct TLVs *params_block = NULL; uint16_t event_num = 0x0911; uint16_t flag = 0x0011; uint32_t event_param1 = 0xa1b2c3d4; uint8_t event_param2 = 0xc1; char event_param3[] = "test data"; event_buffer = create_event_package(flag); if (NULL == event_buffer) { pd_printf("Create event package failed."); return NULL; } params_block = create_params_block(); if (params_block == NULL) { pd_printf("Create first tlv param failed.\n"); return NULL; } if (add_next_param(params_block, TLV_TYPE_UINT8, sizeof(event_param2), &event_param2)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } if (add_next_param(params_block, TLV_TYPE_UINT32, sizeof(event_param1), &event_param1)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } if (add_next_param(params_block, TLV_TYPE_BYTES, sizeof(event_param3), event_param3)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } // 3. create event with flag, priority and tlv params add_event(event_buffer, event_num, 0, params_block); delete_params_block(params_block); finish_package(event_buffer); show_package(event_buffer->buffer, event_buffer->buffer_length); return event_buffer; }
static void ICACHE_FLASH_ATTR send_current_status() { struct sub_device_buffer* data_buffer; data_buffer = create_data_package(0); if(NULL == data_buffer) { PRINTF("create data package error\n"); return; } pando_object* obj = NULL; pando_objects_iterator* it = create_pando_objects_iterator(); while(obj = pando_objects_iterator_next(it)){ PARAMS* params = create_params_block(); if (params == NULL) { PRINTF("Create params block failed.\n"); return ; } obj->pack(params); int ret = add_next_property(data_buffer, obj->no, params); if (ret != 0) { PRINTF("add_next_property failed."); } delete_params_block(params); } delete_pando_objects_iterator(it); channel_send_to_device(PANDO_CHANNEL_PORT_1, data_buffer->buffer, data_buffer->buffer_length); show_package(data_buffer->buffer, data_buffer->buffer_length); delete_device_package(data_buffer); }
struct sub_device_buffer * FUNCTION_ATTRIBUTE construct_sub_device_data() { struct sub_device_buffer *data_buffer = NULL; struct TLVs *params_block = NULL; int ret = 0; uint16_t property_num = 0x1516; uint16_t property_num2 = 0x1122; uint16_t flag = 0; uint32_t data_param1 = 0xa1b2c3d4; uint8_t data_param2 = 0xc1; char data_param3[] = "test data"; //create buffer, remember delete data_buffer = create_data_package(flag); if (data_buffer == NULL) { pd_printf("Create data package failed."); } params_block = create_params_block(); if (params_block == NULL) { pd_printf("Create first tlv param failed.\n"); return NULL; } if (add_next_uint32(params_block, data_param1)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } if (add_next_uint8(params_block, data_param2)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } if (add_next_bytes(params_block, strlen(data_param3), data_param3)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } add_next_property(data_buffer, property_num, params_block); // once the params block has been added to data package //you must delete it even you are going to add another block to the package delete_params_block(params_block); //add different property params_block = create_params_block(); if (params_block == NULL) { pd_printf("Create first tlv param failed.\n"); return NULL; } if (add_next_uint8(params_block, data_param2)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } if (add_next_bytes(params_block, strlen(data_param3), data_param3)) { delete_params_block(params_block); pd_printf("Add next tlv param failed.\n"); return NULL; } ret = add_next_property(data_buffer, property_num2, params_block); if (ret != 0) { delete_params_block(params_block); pd_printf("add_next_property failed."); return NULL; } //do not forget to delete params block again delete_params_block(params_block); finish_package(data_buffer); show_package(data_buffer->buffer, data_buffer->buffer_length); return data_buffer; }