/** * @brief Client send over callback function. * @param arg: contain the ip link information * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_sent_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reverse; //spam INFO("TCP: Sent\r\n"); if(client->connState == MQTT_DATA){ if(client->publishedCb) client->publishedCb((uint32_t*)client); } system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client); }
/** * @brief Client send over callback function. * @param arg: contain the ip link information * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_sent_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reverse; INFO("TCP: Sent\r\n"); client->sendTimeout = 0; if(client->connState == MQTT_DATA && client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH){ if(client->publishedCb) client->publishedCb((uint32_t*)client); } system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client); }
/** * @brief Client send over callback function. * @param arg: contain the ip link information * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_sent_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reserve; INFO("TCP: Sent\r\n"); client->sendTimeout = 0; client->keepAliveTick =0; if ((client->connState == MQTT_DATA || client->connState == MQTT_KEEPALIVE_SEND) && client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH) { if (client->publishedCb) client->publishedCb((uint32_t*)client); } xSemaphoreGive(client->mqttTaskSem); }
/** * @brief Client send over callback function. * @param arg: contain the ip link information * @retval None */ void FUNCTION_ATTRIBUTE mqtt_tcpclient_sent_cb(void *arg, int8_t error_no) { struct pando_tcp_conn *pCon = (struct pando_tcp_conn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reverse; if(error_no == 0) { INFO("TCP: Sent OK\r\n"); client->sendTimeout = 0; if(client->connState == MQTT_DATA && client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH){ if(client->publishedCb) client->publishedCb((uint32_t*)client); } } else if(error_no == -1) { INFO("TCP: sent failed!"); client->sendTimeout = 0; client->connState = TCP_RECONNECT_REQ; } MQTT_Task(client); }
/** * @brief Client received callback function. * @param arg: contain the ip link information * @param pdata: received data * @param len: the lenght of received data * @retval None */ void ICACHE_FLASH_ATTR mqtt_tcpclient_recv(void *arg, char *pdata, unsigned short len) { uint8_t msg_type; uint8_t msg_qos; uint16_t msg_id; struct espconn *pCon = (struct espconn*)arg; MQTT_Client *client = (MQTT_Client *)pCon->reverse; //spam INFO("TCP: data received\r\n"); if(len < MQTT_BUF_SIZE && len > 0){ memcpy(client->mqtt_state.in_buffer, pdata, len); switch(client->connState){ case MQTT_CONNECT_SENDING: if(mqtt_get_type(client->mqtt_state.in_buffer) != MQTT_MSG_TYPE_CONNACK){ //spam INFO("MQTT: Invalid packet\r\n"); if(client->security){ espconn_secure_disconnect(client->pCon); } else { espconn_disconnect(client->pCon); } } else { //spam INFO("MQTT: Connected to %s:%d\r\n", client->host, client->port); client->connState = MQTT_DATA; if(client->connectedCb) client->connectedCb((uint32_t*)client); } break; case MQTT_DATA: client->mqtt_state.message_length_read = len; client->mqtt_state.message_length = mqtt_get_total_length(client->mqtt_state.in_buffer, client->mqtt_state.message_length_read); msg_type = mqtt_get_type(client->mqtt_state.in_buffer); msg_qos = mqtt_get_qos(client->mqtt_state.in_buffer); msg_id = mqtt_get_id(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_length); switch(msg_type) { case MQTT_MSG_TYPE_SUBACK: //spam if(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_SUBSCRIBE && client->mqtt_state.pending_msg_id == msg_id) //spam INFO("MQTT: Subscribe successful to %s:%d\r\n", client->host, client->port); break; case MQTT_MSG_TYPE_UNSUBACK: //spam if(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && client->mqtt_state.pending_msg_id == msg_id) //spam INFO("MQTT: UnSubscribe successful\r\n"); break; case MQTT_MSG_TYPE_PUBLISH: if(msg_qos == 1) client->mqtt_state.outbound_message = mqtt_msg_puback(&client->mqtt_state.mqtt_connection, msg_id); else if(msg_qos == 2) client->mqtt_state.outbound_message = mqtt_msg_pubrec(&client->mqtt_state.mqtt_connection, msg_id); deliver_publish(client, client->mqtt_state.in_buffer, client->mqtt_state.message_length_read); break; case MQTT_MSG_TYPE_PUBACK: if(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_msg_id == msg_id){ //spam INFO("MQTT: Publish successful\r\n"); if(client->publishedCb) client->publishedCb((uint32_t*)client); } break; case MQTT_MSG_TYPE_PUBREC: client->mqtt_state.outbound_message = mqtt_msg_pubrel(&client->mqtt_state.mqtt_connection, msg_id); break; case MQTT_MSG_TYPE_PUBREL: client->mqtt_state.outbound_message = mqtt_msg_pubcomp(&client->mqtt_state.mqtt_connection, msg_id); break; case MQTT_MSG_TYPE_PUBCOMP: if(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_msg_id == msg_id){ //spam INFO("MQTT: Public successful\r\n"); if(client->publishedCb) client->publishedCb((uint32_t*)client); } break; case MQTT_MSG_TYPE_PINGREQ: client->mqtt_state.outbound_message = mqtt_msg_pingresp(&client->mqtt_state.mqtt_connection); break; case MQTT_MSG_TYPE_PINGRESP: // Ignore break; } // NOTE: this is done down here and not in the switch case above // because the PSOCK_READBUF_LEN() won't work inside a switch // statement due to the way protothreads resume. if(msg_type == MQTT_MSG_TYPE_PUBLISH) { uint16_t len; // adjust message_length and message_length_read so that // they only account for the publish data and not the rest of the // message, this is done so that the offset passed with the // continuation event is the offset within the publish data and // not the offset within the message as a whole. len = client->mqtt_state.message_length_read; mqtt_get_publish_data(client->mqtt_state.in_buffer, &len); len = client->mqtt_state.message_length_read - len; client->mqtt_state.message_length -= len; client->mqtt_state.message_length_read -= len; if(client->mqtt_state.message_length_read < client->mqtt_state.message_length) { //client->connState = MQTT_PUBLISH_RECV; } } break; case MQTT_PUBLISH_RECV: /* * Long publish message, not implement yet * TODO: Implement method used deliver_publish_continuation */ break; } } system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client); }