/ MQTT 数据发送函数static OSStatus tcp_mqtt_send(struct mqtt_comm_t *mqtt_comm_p, uint8_t data[], uint32_t len, int msBlock, uint8_t apiflag){ mqtt_comm_p->sendbuf = data; mqtt_comm_p->sendlen = len; mqtt_comm_p->apiflags = apiflag; sys_log("tcp_mqtt_send,sendlen: %d.\r\n", mqtt_comm_p->sendlen); tcp_send_data(mqtt_comm_p); if( xSemaphoreTake(mqtt_comm_p->sendsema, msBlock) != pdTRUE ) { sys_log("tcp send timeout.\r\n"); return TCP_MQTT_SEND_ERR; } if (mqtt_comm_p->sendlen != 0) return TCP_MQTT_SEND_ERR; else return NoErr;}// TCP 数据发送函数void tcp_send_data(struct mqtt_comm_t *mqtt_comm_p){ err_t err; uint32_t len; len = mqtt_comm_p->sendlen; len = (len > tcp_sndbuf(mqtt_comm_p->tpcb) ) ? tcp_sndbuf(mqtt_comm_p->tpcb) : len; // 连接断开 if ( (mqtt_comm_p->tpcb == NULL ) || (mqtt_comm_p->tpcb->state != ESTABLISHED) ) { xSemaphoreGive(mqtt_comm_p->sendsema); sys_log("tcp_send_data: tpcb error.\r\n"); return; } // 发送缓冲区满 if(tcp_sndbuf(mqtt_comm_p->tpcb) == 0) { vTaskDelay(1); return; } do { err = tcp_write(mqtt_comm_p->tpcb, mqtt_comm_p->sendbuf, len, mqtt_comm_p->apiflags); if (err == ERR_MEM) len /= 2; } while (err == ERR_MEM && len > 1); if (err == ERR_OK) { mqtt_comm_p->sendlen -= len; mqtt_comm_p->sendbuf += len; } else sys_log("[tcp_send_data]: tcp_write failed.\r\n"); }err_t tcp_send_cb(void *arg, struct tcp_pcb *tpcb, u16_t len){ struct mqtt_comm_t *mqtt_comm_p = arg; if ( mqtt_comm_p->sendlen > 0 ) { tcp_send_data(mqtt_comm_p); } else { xSemaphoreGive(mqtt_comm_p->sendsema); } return ERR_OK;}