/*---------------------------------------------------------------------------*/
PROCESS_THREAD(mqtt_process, ev, data)
{
  static struct mqtt_connection *conn;

  PROCESS_BEGIN();

  while(1) {
    PROCESS_WAIT_EVENT();

    if(ev == mqtt_abort_now_event) {
      DBG("MQTT - Abort\n");
      conn = data;
      conn->state = MQTT_CONN_STATE_ABORT_IMMEDIATE;

      abort_connection(conn);
    }
    if(ev == mqtt_do_connect_tcp_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_connect_tcp_event!\n");
      connect_tcp(conn);
    }
    if(ev == mqtt_do_connect_mqtt_event) {
      conn = data;
      conn->socket.output_data_max_seg = conn->max_segment_size;
      DBG("MQTT - Got mqtt_do_connect_mqtt_event!\n");

      if(conn->out_buffer_sent == 1) {
        PT_INIT(&conn->out_proto_thread);
        while(connect_pt(&conn->out_proto_thread, conn) < PT_EXITED &&
              conn->state != MQTT_CONN_STATE_ABORT_IMMEDIATE) {
          PT_MQTT_WAIT_SEND();
        }
      }
    }
    if(ev == mqtt_do_disconnect_mqtt_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_disconnect_mqtt_event!\n");

      /* Send MQTT Disconnect if we are connected */
      if(conn->state == MQTT_CONN_STATE_SENDING_MQTT_DISCONNECT) {
        if(conn->out_buffer_sent == 1) {
          PT_INIT(&conn->out_proto_thread);
          while(disconnect_pt(&conn->out_proto_thread, conn) < PT_EXITED &&
                conn->state != MQTT_CONN_STATE_ABORT_IMMEDIATE) {
            PT_MQTT_WAIT_SEND();
          }
          abort_connection(conn);
          call_event(conn, MQTT_EVENT_DISCONNECTED, &ev);
        } else {
          process_post(&mqtt_process, mqtt_do_disconnect_mqtt_event, conn);
        }
      }
    }
    if(ev == mqtt_do_pingreq_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_pingreq_event!\n");

      if(conn->out_buffer_sent == 1 &&
         conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
        PT_INIT(&conn->out_proto_thread);
        while(pingreq_pt(&conn->out_proto_thread, conn) < PT_EXITED &&
              conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
          PT_MQTT_WAIT_SEND();
        }
      }
    }
    if(ev == mqtt_do_subscribe_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_subscribe_mqtt_event!\n");

      if(conn->out_buffer_sent == 1 &&
         conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
        PT_INIT(&conn->out_proto_thread);
        while(subscribe_pt(&conn->out_proto_thread, conn) < PT_EXITED &&
              conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
          PT_MQTT_WAIT_SEND();
        }
      }
    }
    if(ev == mqtt_do_unsubscribe_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_unsubscribe_mqtt_event!\n");

      if(conn->out_buffer_sent == 1 &&
         conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
        PT_INIT(&conn->out_proto_thread);
        while(unsubscribe_pt(&conn->out_proto_thread, conn) < PT_EXITED &&
              conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
          PT_MQTT_WAIT_SEND();
        }
      }
    }
    if(ev == mqtt_do_publish_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_publish_mqtt_event!\n");

      if(conn->out_buffer_sent == 1 &&
         conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
        PT_INIT(&conn->out_proto_thread);
        while(publish_pt(&conn->out_proto_thread, conn) < PT_EXITED &&
              conn->state == MQTT_CONN_STATE_CONNECTED_TO_BROKER) {
          PT_MQTT_WAIT_SEND();
        }
      }
    }
  }
  PROCESS_END();
}
Example #2
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(mqtt_process, ev, data)
{
  static struct mqtt_connection* conn;
  static uip_ipaddr_t* ipaddr = NULL;

  PROCESS_BEGIN();
  while(1) {
    PROCESS_WAIT_EVENT();

    if(ev == mqtt_do_connect_tcp_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_connect_tcp_event!\r\n");
      connect_tcp(conn);
    }
    if(ev == mqtt_do_connect_mqtt_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_connect_mqtt_event!\r\n");

      PT_INIT(&conn->out_proto_thread);
      while(connect_pt(&conn->out_proto_thread, conn) != PT_ENDED) {
        PT_MQTT_WAIT_SEND();
      }
    }
    if(ev == mqtt_do_disconnect_mqtt_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_disconnect_mqtt_event!\r\n");

      PT_INIT(&conn->out_proto_thread);
      while(disconnect_pt(&conn->out_proto_thread, conn) != PT_ENDED) {
        PT_MQTT_WAIT_SEND();
      }
    }
    if(ev == mqtt_do_pingreq_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_pingreq_event!\r\n");

      PT_INIT(&conn->out_proto_thread);
      while(pingreq_pt(&conn->out_proto_thread, conn) != PT_ENDED) {
        PT_MQTT_WAIT_SEND();
      }
    }
    if(ev == mqtt_do_subscribe_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_subscribe_mqtt_event!\r\n");

      PT_INIT(&conn->out_proto_thread);
      while(subscribe_pt(&conn->out_proto_thread, conn) != PT_ENDED) {
        PT_MQTT_WAIT_SEND();
      }
    }
    if(ev == mqtt_do_unsubscribe_event) {
        conn = data;
        DBG("MQTT - Got mqtt_do_unsubscribe_mqtt_event!\r\n");

        PT_INIT(&conn->out_proto_thread);
        while(unsubscribe_pt(&conn->out_proto_thread, conn) != PT_ENDED) {
          PT_MQTT_WAIT_SEND();
        }
      }
    if(ev == mqtt_do_publish_event) {
      conn = data;
      DBG("MQTT - Got mqtt_do_publish_mqtt_event!\r\n");

      PT_INIT(&conn->out_proto_thread);
      while(publish_pt(&conn->out_proto_thread, conn) != PT_ENDED) {
        PT_MQTT_WAIT_SEND();
      }
    }

#if 0
    if(ev == mqtt_do_publish_event) {

    }
#endif
    /* mDNS event - either found a hostname, or not. */
    if(ev == mdns_event_found) {
      if((char *)data != NULL && mdns_lookup((char *)data) != NULL) {
        ipaddr = mdns_lookup((char *)data);

        DBG("MQTT - Host found with ip %i %i %i %i TCP connecting...\r\n",
               ipaddr->u8[12],
               ipaddr->u8[13],
               ipaddr->u8[14],
               ipaddr->u8[15]);
      } else {
        printf("MQTT - Host not found, cannot continue.\r\n");
      }

      /* Find the connection(s) that are waiting for this lookup. Note that it
       * could possibly be more then one connection. */
      for(conn = list_head(mqtt_conn_list);
          conn != NULL;
          conn = list_item_next(conn)) {
        if( conn->state == MQTT_CONN_STATE_DNS_LOOKUP &&
            strcmp( (char*)data, conn->server_host ) == 0 ) {

          /* Update the connection of the DNS error or connect, depending on the
           * DNS lookup result.
           */
          if( ipaddr == NULL ) {
            conn->state = MQTT_CONN_STATE_DNS_ERROR;
            call_event(conn, MQTT_EVENT_DNS_ERROR, NULL);
          } else {
            uip_ipaddr_copy( &(conn->server_ip), ipaddr );
            process_post(&mqtt_process, mqtt_do_connect_tcp_event, conn);
          }
        }
      }
    }
  }
  PROCESS_END();
}