Exemple #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;
}
Exemple #2
0
int mqtt_client(void)
{
    int rc = 0, msg_len;
    void *pclient;
    iotx_conn_info_pt pconn_info;
    iotx_mqtt_param_t mqtt_params;
    iotx_mqtt_topic_info_t topic_msg;
    char msg_pub[128];
    char *msg_buf = NULL, *msg_readbuf = NULL;
    char device_name[24];
    char device_secret[64];
    char topic_update[64],topic_config[64],topic_control[64],topic_state[64];

    if (NULL == (msg_buf = (char *)HAL_Malloc(MSG_LEN_MAX))) {
        EXAMPLE_TRACE("not enough memory");
        rc = -1;
        goto do_exit;
    }

    if (NULL == (msg_readbuf = (char *)HAL_Malloc(MSG_LEN_MAX))) {
        EXAMPLE_TRACE("not enough memory");
        rc = -1;
        goto do_exit;
    }

    int fd_dv;
    int rdOrWrNum;
    char dvcSecretBuff[36];
    getClientID(device_name);
	if((fd_dv=open("./dvcSecret",O_RDWR | O_CREAT)) == -1)
	{
		PRINTF("open dvcSecret error");
		exit(1);
	}
	rdOrWrNum = read(fd_dv,dvcSecretBuff,36);
	dvcSecretBuff[rdOrWrNum] = '\0';
	if(rdOrWrNum == -1)
	{
		PRINTF("read dvcSecret error");
	}
	if(rdOrWrNum != 32)
	{
		getSecret(device_secret,device_name);
		lseek(fd_dv,0,SEEK_SET);
		rdOrWrNum = write(fd_dv,device_secret,strlen(device_secret));
		truncate("./dvcSecret", rdOrWrNum);
		close(fd_dv);
	}
	else
	{
		strcpy(device_secret,dvcSecretBuff);
	}
	if(snprintf(topic_update, sizeof(topic_update),"/%s/%s/update", PRODUCT_KEY,device_name) < 0)
	{
		PRINTF("topic too long!");
		return -1;
	}
	if(snprintf(topic_config, sizeof(topic_config),"/%s/%s/config", PRODUCT_KEY,device_name) < 0)
	{
		PRINTF("topic too long!");
		return -1;
	}
	if(snprintf(topic_control, sizeof(topic_control),"/%s/%s/control", PRODUCT_KEY,device_name) < 0)
	{
		PRINTF("topic too long!");
		return -1;
	}
	if(snprintf(topic_state, sizeof(topic_state),"/%s/%s/state", PRODUCT_KEY,device_name) < 0)
	{
			PRINTF("topic too long!");
			return -1;
	}
    /* Device AUTH */
    if (0 != IOT_SetupConnInfo(PRODUCT_KEY, device_name, device_secret, (void **)&pconn_info)) {
        EXAMPLE_TRACE("AUTH request failed!");
        rc = -1;
        goto do_exit;
    }

    /* 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 = msg_readbuf;
    mqtt_params.read_buf_size = MSG_LEN_MAX;
    mqtt_params.pwrite_buf = msg_buf;
    mqtt_params.write_buf_size = MSG_LEN_MAX;

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


    /* Construct a MQTT client with specify parameter */
    pclient = IOT_MQTT_Construct(&mqtt_params);
    if (NULL == pclient) {
        EXAMPLE_TRACE("MQTT construct failed");
        rc = -1;
        goto do_exit;
    }

    /* Subscribe the specific topic */
    rc = IOT_MQTT_Subscribe(pclient, topic_config, IOTX_MQTT_QOS1, configArrived, NULL);
    if (rc < 0) {
        IOT_MQTT_Destroy(&pclient);
        EXAMPLE_TRACE("IOT_MQTT_Subscribe() failed, rc = %d", rc);
        rc = -1;
        goto do_exit;
    }
    /* Subscribe the specific topic */
	rc = IOT_MQTT_Subscribe(pclient, topic_update, IOTX_MQTT_QOS1, updateArrived, NULL);
	if (rc < 0) {
		IOT_MQTT_Destroy(&pclient);
		EXAMPLE_TRACE("IOT_MQTT_Subscribe() failed, rc = %d", rc);
		rc = -1;
		goto do_exit;
	}
	/* Subscribe the specific topic */
	rc = IOT_MQTT_Subscribe(pclient, topic_control, IOTX_MQTT_QOS1, controlrrived, NULL);
	if (rc < 0) {
		IOT_MQTT_Destroy(&pclient);
		EXAMPLE_TRACE("IOT_MQTT_Subscribe() failed, rc = %d", rc);
		rc = -1;
		goto do_exit;
	}

    HAL_SleepMs(1000);

    /* open the version record file*/
    int fd_ver;
    char rd_buf[64];
    char *p = rd_buf;
    int rdNum  = 8;
    int rRdNum = 0;
    int rdSum  = 0;

	if((fd_ver=open("./unidoli.ver",O_RDONLY)) == -1)
	{
		PRINTF("open unidoli.ver error \n");
		exit(1);
	}
	do
	{
		p += rRdNum;
		rRdNum = read(fd_ver,p,rdNum);
		if(rRdNum == -1)
		{
			PRINTF("read unidoli.ver error\n");
		}
		rdSum += rRdNum;
	}
	while(rRdNum == rdNum);
	rd_buf[rdSum] = '\0';
	close(fd_ver);

	PRINTF("unidoli.ver : %s",rd_buf);

    /* Initialize topic information */
    memset(&topic_msg, 0x0, sizeof(iotx_mqtt_topic_info_t));
    snprintf(msg_pub, sizeof(msg_pub), "{\"version\":\"%s\"}", rd_buf);
    topic_msg.qos = IOTX_MQTT_QOS1;
    topic_msg.retain = 0;
    topic_msg.dup = 0;
    topic_msg.payload = (void *)msg_pub;
    topic_msg.payload_len = strlen(msg_pub);

    rc = IOT_MQTT_Publish(pclient, topic_state, &topic_msg);
    EXAMPLE_TRACE("rc = IOT_MQTT_Publish() = %d", rc);

    do
    {
    	if(0)    //turn of loop publish
    	{
			/* Generate topic message */
			msg_len = snprintf(msg_pub, sizeof(msg_pub), "{\"attr_name\":\"temperature\", \"attr_value\":\" \"}");
			if (msg_len < 0) {
				EXAMPLE_TRACE("Error occur! Exit program");
				rc = -1;
				break;
			}

			topic_msg.payload = (void *)msg_pub;
			topic_msg.payload_len = msg_len;

			rc = IOT_MQTT_Publish(pclient, topic_state, &topic_msg);
			if (rc < 0) {
				EXAMPLE_TRACE("error occur when publish");
				rc = -1;
				break;
			}
			#ifdef MQTT_ID2_CRYPTO
				EXAMPLE_TRACE("packet-id=%u, publish topic msg='0x%02x%02x%02x%02x'...",
							  (uint32_t)rc,
							  msg_pub[0], msg_pub[1], msg_pub[2], msg_pub[3]
							 );
			#else
				EXAMPLE_TRACE("packet-id=%u, publish topic msg=%s", (uint32_t)rc, msg_pub);
			#endif
    	}
        /* handle the MQTT packet received from TCP or SSL connection */
        IOT_MQTT_Yield(pclient, 200);

    } while (loop_mqtt);
    loop_mqtt = 1;

    IOT_MQTT_Unsubscribe(pclient, topic_config);
    IOT_MQTT_Unsubscribe(pclient, topic_update);
    IOT_MQTT_Unsubscribe(pclient, topic_control);
    HAL_SleepMs(200);
    IOT_MQTT_Destroy(&pclient);
do_exit:
    if (NULL != msg_buf) {
        HAL_Free(msg_buf);
    }

    if (NULL != msg_readbuf) {
        HAL_Free(msg_readbuf);
    }
    return rc;
}
Exemple #3
0
/*
 * initialization parameter: mqtt_params
 */
int mqtt_client_example(void)
{
    int rc = 0;
    iotx_conn_info_pt pconn_info;
    iotx_mqtt_param_t mqtt_params;

    if (msg_buf != NULL) {
        return rc;
    }

    if (NULL == (msg_buf = (char *)aos_malloc(MSG_LEN_MAX))) {
        LOG("not enough memory");
        rc = -1;
        release_buff();
        return rc;
    }

    if (NULL == (msg_readbuf = (char *)aos_malloc(MSG_LEN_MAX))) {
        LOG("not enough memory");
        rc = -1;
        release_buff();
        return rc;
    }

    /* Device AUTH */
    if (0 != IOT_SetupConnInfo(PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, (void **)&pconn_info)) {
        LOG("AUTH request failed!");
        rc = -1;
        release_buff();
        return rc;
    }

    /* 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 = msg_readbuf;
    mqtt_params.read_buf_size = MSG_LEN_MAX;
    mqtt_params.pwrite_buf = msg_buf;
    mqtt_params.write_buf_size = MSG_LEN_MAX;

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


    /* Construct a MQTT client with specify parameter */

    gpclient = IOT_MQTT_Construct(&mqtt_params); 
    if (NULL == gpclient) {
        LOG("MQTT construct failed");
        rc = -1;
        release_buff();
        //aos_unregister_event_filter(EV_SYS,  mqtt_service_event, gpclient);
    } else{
        aos_register_event_filter(EV_SYS,  mqtt_service_event, gpclient);
    }

    return rc;
}