Example #1
0
void QMQTT::ClientPrivate::sendConnect()
{
    QString magic(PROTOCOL_MAGIC);
    quint8 header = CONNECT;
    quint8 flags = 0;

    //header
    Frame frame(SETQOS(header, QOS1));

    //flags
    flags = FLAG_CLEANSESS(flags, _cleanSession ? 1 : 0 );
    flags = FLAG_WILL(flags, willTopic().isEmpty() ? 0 : 1);
    if (!willTopic().isEmpty())
    {
        flags = FLAG_WILLQOS(flags, willQos());
        flags = FLAG_WILLRETAIN(flags, willRetain() ? 1 : 0);
    }
    if (!username().isEmpty())
    {
        flags = FLAG_USERNAME(flags, 1);
    }
    if (!password().isEmpty())
    {
        flags = FLAG_PASSWD(flags, 1);
    }

    //payload
    frame.writeString(magic);
    frame.writeChar(PROTOCOL_VERSION_MAJOR);
    frame.writeChar(flags);
    frame.writeInt(_keepAlive);
    if(_clientId.isEmpty()) {
        _clientId = randomClientId();
    }
    frame.writeString(_clientId);
    if(!willTopic().isEmpty())
    {
        frame.writeString(willTopic());
        if(!willMessage().isEmpty())
        {
            frame.writeString(willMessage());
        }
    }
    if (!_username.isEmpty())
    {
        frame.writeString(_username);
    }
    if (!_password.isEmpty()){
        frame.writeString(_password);
    }
    _network->sendFrame(frame);
}
Example #2
0
void ClientPrivate::sendConnect()
{
    QString magic(PROTOCOL_MAGIC);
    quint8 header = CONNECT;
    quint8 flags = 0;

    qCDebug(client) << "sendConnect....";

    //header
    Frame frame(SETQOS(header, MQTT_QOS1));

    qCDebug(client) << "CONNECT header: " << frame.header();

    //flags
    flags = FLAG_CLEANSESS(flags, cleansess ? 1 : 0 );
    flags = FLAG_WILL(flags, will ? 1 : 0);
    if (will) {
        flags = FLAG_WILLQOS(flags, will->qos());
        flags = FLAG_WILLRETAIN(flags, will->retain() ? 1 : 0);
    }
    if (!username.isEmpty()) {
        flags = FLAG_USERNAME(flags, 1);
    }
    if (!password.isEmpty()) {
        flags = FLAG_PASSWD(flags, 1);
    }

    //payload
    frame.writeString(magic);
    frame.writeChar(MQTT_PROTO_MAJOR);
    frame.writeChar(flags);
    frame.writeInt(keepalive);
    if(clientId.isEmpty()) {
        clientId = randomClientId();
    }
    frame.writeString(clientId);
    if(will != NULL) {
        frame.writeString(will->topic());
        frame.writeString(will->message());
    }
    if (!username.isEmpty()) {
        frame.writeString(username);
    }
    if (!password.isEmpty()) {
        frame.writeString(password);
    }
    network->sendFrame(frame);
}
Example #3
0
void mqtt_send_hello(struct espconn *connection, const char *clientid, bool cleansess)
{
    char message[PAYLOAD_MAX];
    int  plen = 0;
    int  message_offset = PAYLOAD_OFF;
    char *payload = message + message_offset;

    // length of protocol name
    plen += mqtt_write_int16_bullshit(payload + plen, 6);

    // protocol name
    os_memcpy(payload + plen, "MQIsdp", 6);
    plen += 6;

    // protocol_level
    payload[plen] = 3;
    plen += 1;

    // connect_flags
    uint8_t connect_flags  = 0;
    FLAG_CLEANSESS(connect_flags, cleansess ? 1 : 0 );
    payload[plen] = connect_flags;
    plen += 1;

    // keep_alive
    plen += mqtt_write_int16_bullshit(payload + plen, 0);

    // length of clientid
    uint16_t clientidlen = strlen(clientid);
    plen += mqtt_write_int16_bullshit(payload + plen, clientidlen);

    //clientid
    os_memcpy(payload + plen, clientid, clientidlen);
    plen += clientidlen;

    mqtt_bake_message_frame(message, &message_offset, &plen, MQTT_MSG_CONNECT);
    espconn_sent(connection, message + message_offset, plen);
}