Esempio n. 1
0
/**
* Function used to initialize the IoTF client
* @param client - Reference to the Iotfclient
*
* @return int return code
*/
int connectiotf(Iotfclient *client)
{

	int rc = 0;
	if(strcmp(client->config.org,"quickstart") == 0){
		isQuickstart = 1 ;
	}

	const char* messagingUrl = ".messaging.internetofthings.ibmcloud.com";

	char hostname[strlen(client->config.org) + strlen(messagingUrl) + 1];
	
	sprintf(hostname, "%s%s", client->config.org, messagingUrl);

    //TODO : change to 8883 if registered, add support when available in MQTTClient
    int port = 1883;

    char clientId[strlen(client->config.org) + strlen(client->config.type) + strlen(client->config.id) + 5];
    sprintf(clientId, "d:%s:%s:%s", client->config.org, client->config.type, client->config.id);

	NewNetwork(&client->n);
	ConnectNetwork(&client->n, hostname, port);
	MQTTClient(&client->c, &client->n, 1000, buf, 100, readbuf, 100);
 
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
	data.willFlag = 0;
	data.MQTTVersion = 3;
	data.clientID.cstring = clientId;

	if(!isQuickstart) {
		printf("Connecting to registered service with org %s\n", client->config.org);
		data.username.cstring = "use-token-auth";
		printf ("the auth token being used is [%s]\n", client->config.authtoken);
		data.password.cstring = client->config.authtoken;
	}

	data.keepAliveInterval = 10;
	data.cleansession = 1;
	
	rc = MQTTConnect(&client->c, &data);

	if(!isQuickstart) {
		//Subscibe to all commands
		subscribeCommands(client);
	}

	return rc;
}
/*
* Function used to subscribe to all commands. This function is by default called when in registered mode.
*
* @return int return code
*/
int subscribeCommands_dm()
{
	int rc = -1;

	rc = subscribeCommands(&dmClient.deviceClient);
	if(rc >=0){
		Client *c;
		c= &(dmClient.deviceClient.c);
		// Call back handles all the requests and responses received from the Watson IoT platform
		rc = MQTTSubscribe(c, "iotdm-1/#", QOS0, onMessage);
		printf("Actions subscription :%d\n",rc);
	}else{
		printf("iotf subscribe failed ");
	}
	return rc;
}
/*
* Function used to subscribe to all commands. This function is by default called when in registered mode.
* 
* @param client reference to the ManagedDevice 
*
* @return int return code
*/
int subscribeCommands_dm(ManagedDevice *client)
{
	int rc = -1;

	rc = subscribeCommands(&deviceClient);
	if(rc >=0){
		Client *c;
		c= &(deviceClient.c);
		rc = MQTTSubscribe(c, "iotdm-1/response", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/observe", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/cancel", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/mgmt/initiate/device/reboot", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/mgmt/initiate/device/factory_reset", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/mgmt/initiate/firmware/download", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/mgmt/initiate/firmware/update", QOS0, messageCame);
		rc = MQTTSubscribe(c, "iotdm-1/device/update", QOS0, messageCame);
	}else{
		printf("iotf subscribe faild ");
	}
	return rc;
}