examples/35_iot_cloud_tencent/packages/tencent-iot-sdk-v3.0.2/qcloud-iot-sdk-embedded-c/src/mqtt/src/mqtt_client_connect.c
static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTConnectParams *options, uint32_t *serialized_len) {
    IOT_FUNC_ENTRY;

    POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
    POINTER_SANITY_CHECK(options, QCLOUD_ERR_INVAL);
    STRING_PTR_SANITY_CHECK(options->client_id, QCLOUD_ERR_INVAL);
    POINTER_SANITY_CHECK(serialized_len, QCLOUD_ERR_INVAL);

    unsigned char *ptr = buf;
    unsigned char header = 0;
    unsigned char flags = 0;
    uint32_t rem_len = 0;
    int rc;

    long cur_timesec = HAL_Timer_current_sec() + MAX_ACCESS_EXPIRE_TIMEOUT / 1000;
    if (cur_timesec <= 0 || MAX_ACCESS_EXPIRE_TIMEOUT <= 0) {
    	cur_timesec = LONG_MAX;
    }
    long cur_timesec_bak = cur_timesec;
    int cur_timesec_len = 0;
    while(cur_timesec_bak != 0) {
    	cur_timesec_bak /= 10;
		++cur_timesec_len;
	}

    int username_len = strlen(options->client_id) + strlen(QCLOUD_IOT_DEVICE_SDK_APPID) + MAX_CONN_ID_LEN + cur_timesec_len + 4;
    options->username = (char*)HAL_Malloc(username_len);
    get_next_conn_id(options->conn_id);
	HAL_Snprintf(options->username, username_len, "%s;%s;%s;%ld", options->client_id, QCLOUD_IOT_DEVICE_SDK_APPID, options->conn_id, cur_timesec);

#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
     if (options->device_secret != NULL && options->username != NULL) {
    	 char                sign[41]   = {0};
    	 utils_hmac_sha1(options->username, strlen(options->username), sign, options->device_secret, options->device_secret_len);
    	 options->password = (char*) HAL_Malloc (51);
    	 if (options->password == NULL) IOT_FUNC_EXIT_RC(QCLOUD_ERR_INVAL);
		 HAL_Snprintf(options->password, 51, "%s;hmacsha1", sign);
     }
#endif

    rem_len = _get_packet_connect_rem_len(options);
    if (get_mqtt_packet_len(rem_len) > buf_len) {
        IOT_FUNC_EXIT_RC(QCLOUD_ERR_BUF_TOO_SHORT);
    }

    rc = mqtt_init_packet_header(&header, CONNECT, QOS0, 0, 0);
    if (QCLOUD_ERR_SUCCESS != rc) {
        IOT_FUNC_EXIT_RC(rc);
    }

    // 报文固定头部第一个字节
    mqtt_write_char(&ptr, header);

    // 报文固定头部剩余长度字段
    ptr += mqtt_write_packet_rem_len(ptr, rem_len);

    // 报文可变头部协议名 + 协议版本号
    if (4 == options->mqtt_version) {
        mqtt_write_utf8_string(&ptr, "MQTT");
        mqtt_write_char(&ptr, (unsigned char) 4);
    } else {
        mqtt_write_utf8_string(&ptr, "MQIsdp");
        mqtt_write_char(&ptr, (unsigned char) 3);
    }

    // 报文可变头部连接标识位
    
    flags |= (options->clean_session) ? MQTT_CONNECT_FLAG_CLEAN_SES : 0;
    flags |= (options->username != NULL) ? MQTT_CONNECT_FLAG_USERNAME : 0;

#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
	flags |= MQTT_CONNECT_FLAG_PASSWORD;
#endif
    
    mqtt_write_char(&ptr, flags);

    // 报文可变头部心跳周期/保持连接, 一个以秒为单位的时间间隔, 表示为一个16位的字
    mqtt_write_uint_16(&ptr, options->keep_alive_interval);

    // 有效负载部分: 客户端标识符
    mqtt_write_utf8_string(&ptr, options->client_id);

    // 用户名
    if ((flags & MQTT_CONNECT_FLAG_USERNAME) && options->username != NULL) {
        mqtt_write_utf8_string(&ptr, options->username);
        HAL_Free(options->username);
        options->username = NULL;
    }

    if ((flags & MQTT_CONNECT_FLAG_PASSWORD) && options->password != NULL) {
    	mqtt_write_utf8_string(&ptr, options->password);
    	HAL_Free(options->password);
    	options->password = NULL;
    }

    *serialized_len = (uint32_t) (ptr - buf);

    IOT_FUNC_EXIT_RC(QCLOUD_ERR_SUCCESS);
}
/**
  * Serializes the connect options into the buffer.
  * @param buf the buffer into which the packet will be serialized
  * @param len the length in bytes of the supplied buffer
  * @param options the options to be used to build the connect packet
  * @param serialized length
  * @return int indicating function execution status
  */
Serializes the connect options into the buffer.
Serializes the connect options into the buffer .
static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTConnectParams *options, uint32_t *serialized_len) {
    IOT_FUNC_ENTRY;
    POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
    POINTER_SANITY_CHECK(options, QCLOUD_ERR_INVAL);
    STRING_PTR_SANITY_CHECK(options->client_id, QCLOUD_ERR_INVAL);
    POINTER_SANITY_CHECK(serialized_len, QCLOUD_ERR_INVAL);
    unsigned char *ptr = buf;
    unsigned char header = 0;
    unsigned char flags = 0;
    uint32_t rem_len = 0;
    int rc;
    long cur_timesec = HAL_Timer_current_sec() + MAX_ACCESS_EXPIRE_TIMEOUT / 1000;
    if (cur_timesec <= 0 || MAX_ACCESS_EXPIRE_TIMEOUT <= 0) {
    	cur_timesec = LONG_MAX;
    }
    long cur_timesec_bak = cur_timesec;
    int cur_timesec_len = 0;
    while(cur_timesec_bak != 0) {
    	cur_timesec_bak /= 10;
		++cur_timesec_len;
	}
    int username_len = strlen(options->client_id) + strlen(QCLOUD_IOT_DEVICE_SDK_APPID) + MAX_CONN_ID_LEN + cur_timesec_len + 4;
    options->username = (char*)HAL_Malloc(username_len);
    get_next_conn_id(options->conn_id);
	HAL_Snprintf(options->username, username_len, "%s;%s;%s;%ld", options->client_id, QCLOUD_IOT_DEVICE_SDK_APPID, options->conn_id, cur_timesec);
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
     if (options->device_secret != NULL && options->username != NULL) {
    	 char                sign[41]   = {0};
    	 utils_hmac_sha1(options->username, strlen(options->username), sign, options->device_secret, options->device_secret_len);
    	 options->password = (char*) HAL_Malloc (51);
    	 if (options->password == NULL) IOT_FUNC_EXIT_RC(QCLOUD_ERR_INVAL);
		 HAL_Snprintf(options->password, 51, "%s;hmacsha1", sign);
     }
#endif
    rem_len = _get_packet_connect_rem_len(options);
    if (get_mqtt_packet_len(rem_len) > buf_len) {
        IOT_FUNC_EXIT_RC(QCLOUD_ERR_BUF_TOO_SHORT);
    }
    rc = mqtt_init_packet_header(&header, CONNECT, QOS0, 0, 0);
    if (QCLOUD_ERR_SUCCESS != rc) {
        IOT_FUNC_EXIT_RC(rc);
    }
    mqtt_write_char(&ptr, header);
    ptr += mqtt_write_packet_rem_len(ptr, rem_len);
    if (4 == options->mqtt_version) {
        mqtt_write_utf8_string(&ptr, "MQTT");
        mqtt_write_char(&ptr, (unsigned char) 4);
    } else {
        mqtt_write_utf8_string(&ptr, "MQIsdp");
        mqtt_write_char(&ptr, (unsigned char) 3);
    }
    flags |= (options->clean_session) ? MQTT_CONNECT_FLAG_CLEAN_SES : 0;
    flags |= (options->username != NULL) ? MQTT_CONNECT_FLAG_USERNAME : 0;
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)
	flags |= MQTT_CONNECT_FLAG_PASSWORD;
#endif
    mqtt_write_char(&ptr, flags);
    mqtt_write_uint_16(&ptr, options->keep_alive_interval);
    mqtt_write_utf8_string(&ptr, options->client_id);
    if ((flags & MQTT_CONNECT_FLAG_USERNAME) && options->username != NULL) {
        mqtt_write_utf8_string(&ptr, options->username);
        HAL_Free(options->username);
        options->username = NULL;
    }
    if ((flags & MQTT_CONNECT_FLAG_PASSWORD) && options->password != NULL) {
    	mqtt_write_utf8_string(&ptr, options->password);
    	HAL_Free(options->password);
    	options->password = NULL;
    }
    *serialized_len = (uint32_t) (ptr - buf);
    IOT_FUNC_EXIT_RC(QCLOUD_ERR_SUCCESS);
}
static int _serialize_connect_packet ( unsigned char * buf , size_t buf_len , MQTTConnectParams * options , uint32_t * serialized_len ) { IOT_FUNC_ENTRY ; POINTER_SANITY_CHECK ( buf , QCLOUD_ERR_INVAL ) ; POINTER_SANITY_CHECK ( options , QCLOUD_ERR_INVAL ) ; STRING_PTR_SANITY_CHECK ( options -> client_id , QCLOUD_ERR_INVAL ) ; POINTER_SANITY_CHECK ( serialized_len , QCLOUD_ERR_INVAL ) ; unsigned char * ptr = buf ; unsigned char header = 0 ; unsigned char flags = 0 ; uint32_t rem_len = 0 ; int rc ; long cur_timesec = HAL_Timer_current_sec ( ) + MAX_ACCESS_EXPIRE_TIMEOUT / 1000 ; if ( cur_timesec <= 0 || MAX_ACCESS_EXPIRE_TIMEOUT <= 0 ) { cur_timesec = LONG_MAX ; } long cur_timesec_bak = cur_timesec ; int cur_timesec_len = 0 ; while ( cur_timesec_bak != 0 ) { cur_timesec_bak /= 10 ; ++ cur_timesec_len ; } int username_len = strlen ( options -> client_id ) + strlen ( QCLOUD_IOT_DEVICE_SDK_APPID ) + MAX_CONN_ID_LEN + cur_timesec_len + 4 ; options -> username = ( char * ) HAL_Malloc ( username_len ) ; get_next_conn_id ( options -> conn_id ) ; HAL_Snprintf ( options -> username , username_len , " " , options -> client_id , QCLOUD_IOT_DEVICE_SDK_APPID , options -> conn_id , cur_timesec ) ; #if defined ( AUTH_WITH_NOTLS ) && defined ( AUTH_MODE_KEY ) if ( options -> device_secret != NULL && options -> username != NULL ) { char sign [ 41 ] = { 0 } ; utils_hmac_sha1 ( options -> username , strlen ( options -> username ) , sign , options -> device_secret , options -> device_secret_len ) ; options -> password = ( char * ) HAL_Malloc ( 51 ) ; if ( options -> password == NULL ) IOT_FUNC_EXIT_RC ( QCLOUD_ERR_INVAL ) ; HAL_Snprintf ( options -> password , 51 , " " , sign ) ; } #endif rem_len = _get_packet_connect_rem_len ( options ) ; if ( get_mqtt_packet_len ( rem_len ) > buf_len ) { IOT_FUNC_EXIT_RC ( QCLOUD_ERR_BUF_TOO_SHORT ) ; } rc = mqtt_init_packet_header ( & header , CONNECT , QOS0 , 0 , 0 ) ; if ( QCLOUD_ERR_SUCCESS != rc ) { IOT_FUNC_EXIT_RC ( rc ) ; } mqtt_write_char ( & ptr , header ) ; ptr += mqtt_write_packet_rem_len ( ptr , rem_len ) ; if ( 4 == options -> mqtt_version ) { mqtt_write_utf8_string ( & ptr , " " ) ; mqtt_write_char ( & ptr , ( unsigned char ) 4 ) ; } else { mqtt_write_utf8_string ( & ptr , " " ) ; mqtt_write_char ( & ptr , ( unsigned char ) 3 ) ; } flags |= ( options -> clean_session ) ? MQTT_CONNECT_FLAG_CLEAN_SES : 0 ; flags |= ( options -> username != NULL ) ? MQTT_CONNECT_FLAG_USERNAME : 0 ; #if defined ( AUTH_WITH_NOTLS ) && defined ( AUTH_MODE_KEY ) flags |= MQTT_CONNECT_FLAG_PASSWORD ; #endif mqtt_write_char ( & ptr , flags ) ; mqtt_write_uint_16 ( & ptr , options -> keep_alive_interval ) ; mqtt_write_utf8_string ( & ptr , options -> client_id ) ; if ( ( flags & MQTT_CONNECT_FLAG_USERNAME ) && options -> username != NULL ) { mqtt_write_utf8_string ( & ptr , options -> username ) ; HAL_Free ( options -> username ) ; options -> username = NULL ; } if ( ( flags & MQTT_CONNECT_FLAG_PASSWORD ) && options -> password != NULL ) { mqtt_write_utf8_string ( & ptr , options -> password ) ; HAL_Free ( options -> password ) ; options -> password = NULL ; } * serialized_len = ( uint32_t ) ( ptr - buf ) ; IOT_FUNC_EXIT_RC ( QCLOUD_ERR_SUCCESS ) ; }
Serializes the connect options into the buffer.
Serializes the connect options into the buffer .
// 报文固定头部第一个字节 // 报文固定头部剩余长度字段 // 报文可变头部协议名 + 协议版本号 // 报文可变头部连接标识位 // 报文可变头部心跳周期/保持连接, 一个以秒为单位的时间间隔, 表示为一个16位的字 // 有效负载部分: 客户端标识符 // 用户名