Ejemplo n.º 1
0
/*
* 1:  mqtt instance have been init
* 0:  mqtt instance init success
*     IOT_MQTT_Construct success,  MQTT connected.
* -1: mqtt instance init fail
*/
int mqtt_init_instance(char *productKey, char *deviceName, char *deviceSecret, int maxMsgSize)
{
    if (mqtt_client)
        return 1;

    IOT_OpenLog("masterslave");
    IOT_SetLogLevel(IOT_LOG_DEBUG);

    iotx_conn_info_pt pconn_info;
    iotx_mqtt_param_t mqtt_params;

    int ret = IOT_SetupConnInfo(productKey, deviceName, deviceSecret, (void **)&pconn_info);
    if (ret != SUCCESS_RETURN)
        return -1;

    mqtt_rbuf = LITE_malloc(maxMsgSize);
    if (!mqtt_rbuf)
        return -1;

    mqtt_wbuf = LITE_malloc(maxMsgSize);
    if (!mqtt_wbuf)
        goto fail;

    /* Initialize MQTT parameter */
    memset(&mqtt_params, 0x0, sizeof(mqtt_params));

    mqtt_params.port      = pconn_info->port;
    mqtt_params.host      = pconn_info->host_name;
    mqtt_params.client_id = pconn_info->client_id;
    mqtt_params.username  = pconn_info->username;
    mqtt_params.password  = pconn_info->password;
    mqtt_params.pub_key   = pconn_info->pub_key;

    mqtt_params.request_timeout_ms    = 2000;
    mqtt_params.clean_session         = 0;
    mqtt_params.keepalive_interval_ms = 60000;
    mqtt_params.pread_buf             = mqtt_rbuf;
    mqtt_params.read_buf_size         = maxMsgSize;
    mqtt_params.pwrite_buf            = mqtt_wbuf;
    mqtt_params.write_buf_size        = maxMsgSize;

    mqtt_params.handle_event.h_fp     = event_handle;
    mqtt_params.handle_event.pcontext = NULL;

    /* Construct a MQTT client with specify parameter */
    mqtt_client = IOT_MQTT_Construct(&mqtt_params);
    if (!mqtt_client)
        goto fail;

    return 0;

fail:
    mqtt_deinit_instance();
    return -1;
}
Ejemplo n.º 2
0
int main()
{
    IOT_OpenLog("shadow");
    IOT_SetLogLevel(IOT_LOG_DEBUG);

    char *msg_buf = (char *)HAL_Malloc(SHADOW_MQTT_MSGLEN);
    char *msg_readbuf = (char *)HAL_Malloc(SHADOW_MQTT_MSGLEN);

    demo_device_shadow(msg_buf, msg_readbuf);

    HAL_Free(msg_buf);
    HAL_Free(msg_readbuf);

    SHADOW_TRACE("out of demo!");
    IOT_DumpMemoryStats(IOT_LOG_DEBUG);
    IOT_CloseLog();

    return 0;
}