Example #1
0
/*********************************************************************

Test: single-threaded client

*********************************************************************/
void singleThread_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
{
	MQTTClient_deliveryToken dt;
	MQTTClient_message pubmsg = MQTTClient_message_initializer;
	MQTTClient_message* m = NULL;
	char* topicName = NULL;
	int topicLen;
	int i = 0;
	int iterations = 50;
	int rc;

	MyLog(LOGA_DEBUG, "%d messages at QoS %d", iterations, qos);
	pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
	pubmsg.payloadlen = 11;
	pubmsg.qos = qos;
	pubmsg.retained = 0;

	for (i = 0; i< iterations; ++i)
	{
		if (i % 10 == 0)
			rc = MQTTClient_publish(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
		else
			rc = MQTTClient_publishMessage(c, test_topic, &pubmsg, &dt);
		assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);

		if (qos > 0)
		{
			rc = MQTTClient_waitForCompletion(c, dt, 20000L);
			assert("Good rc from waitforCompletion", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
		}

		rc = MQTTClient_receive(c, &topicName, &topicLen, &m, 10000);
		assert("Good rc from receive", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
		if (topicName)
		{
			MyLog(LOGA_DEBUG, "Message received on topic %s is %.*s", topicName, m->payloadlen, (char*)(m->payload));
			if (pubmsg.payloadlen != m->payloadlen ||
					memcmp(m->payload, pubmsg.payload, m->payloadlen) != 0)
			{
				failures++;
				MyLog(LOGA_INFO, "Error: wrong data - received lengths %d %d", pubmsg.payloadlen, m->payloadlen);
				break;
			}
			MQTTClient_free(topicName);
			MQTTClient_freeMessage(&m);
		}
		else
			printf("No message received within timeout period\n");
	}

	/* receive any outstanding messages */
	MQTTClient_receive(c, &topicName, &topicLen, &m, 1000);
	while (topicName)
	{
		printf("Message received on topic %s is %.*s.\n", topicName, m->payloadlen, (char*)(m->payload));
		MQTTClient_free(topicName);
		MQTTClient_freeMessage(&m);
		MQTTClient_receive(c, &topicName, &topicLen, &m, 1000);
	}
}
Example #2
0
static PyObject* mqttv3_receive(PyObject* self, PyObject *args)
{
	MQTTClient c;
	unsigned long timeout = 1000L;
	int rc;
	PyObject* temp = NULL;

	char* topicName;
	int topicLen;
	MQTTClient_message* message;

	if (!PyArg_ParseTuple(args, "k|k", &c, &timeout))
		return NULL;
	Py_BEGIN_ALLOW_THREADS rc = MQTTClient_receive(c, &topicName, &topicLen,
			&message, timeout);
	Py_END_ALLOW_THREADS
	if (message)
	{
		temp = Py_BuildValue("is#{ss#sisisisi}", rc, topicName, topicLen,
				"payload", message->payload, message->payloadlen, "qos",
				message->qos, "retained", message->retained, "dup",
				message->dup, "msgid", message->msgid);
		free(topicName);
		MQTTClient_freeMessage(&message);
	}
	else
		temp = Py_BuildValue("iz", rc, NULL);

	return temp;
}
Example #3
0
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;

    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = (char *) message->payload;
    char* payload = (char*)  malloc(message->payloadlen+1);
    for(i=0;i<message->payloadlen;i++) payload[i] = payloadptr[i];
    payload[message->payloadlen] = 0;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    char* token = strtok(payload, " ");
    int channel = atoi(token);
    token = strtok(NULL, " ");
    int cmd = 0;
    int level = 0;
    if (token != NULL) {
        cmd = atoi(token);
    }
    printf("Sending command %d on channel %d, level %d\n", cmd, channel, level);

    lw_cmd(level, channel, cmd, id);

    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    free(payload);
    return 1;
}
Example #4
0
// メッセージ到着時
int CConMQTT::iMsgArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	// 局所変数宣言
	int i;                 // ループカウンタ
	char* payloadptr;      // ペイロードの先頭アドレス


	// トピックとメッセージの表示
	printf("Message arrived\n");
	printf("     topic: %s\n", topicName);
	printf("   message: ");

	payloadptr = (char*)message->payload;
	for (i = 0; i<message->payloadlen; i++)
	{
		putchar(*payloadptr++);
	}
	putchar('\n');


	// 解放処理
	MQTTClient_freeMessage(&message);
	MQTTClient_free(topicName);

	return true;
}
Example #5
0
int messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
{
	char action[30];
	char alias[60];
	int ret = -1;
	int i;
	char* payloadptr;

	my_present.action = action;
	my_present.alias = alias;
	ret = get_present_info(topicName, m, &my_present);
	if (ret == 0)
		printf("action:%s alias:%s\n", my_present.action, my_present.alias);
	time_t t;
	time(&t);
	printf("Message arrived, date:%s", ctime(&t));
	printf("     qos: %i\n", m->qos);
	printf("     messageid: %"PRIu64"\n", m->msgid);
	printf("     topic: %s\n", topicName);
	printf("   message: ");

	payloadptr = m->payload;
	for(i = 0; i < m->payloadlen; i++)
	{
		putchar(*payloadptr++);
	}
	putchar('\n');
	MQTTClient_freeMessage(&m);
	MQTTClient_free(topicName);

	/* not expecting any messages */
	return 1;
}
Example #6
0
int messageArrived(void* context, char* topicName, int topicLen,
		MQTTClient_message* message)
{
	PyObject *result = NULL;
	CallbackEntry* e = context;
	int rc = -99;
	PyGILState_STATE gstate;

	//printf("messageArrived %s %s %d %.*s\n", PyString_AsString(e->context), topicName, topicLen,
	//	message->payloadlen, (char*)message->payload);

	gstate = PyGILState_Ensure();
	if (topicLen == 0)
		result = PyEval_CallFunction(e->ma, "Os{ss#sisisisi}", e->context,
				topicName, "payload", message->payload, message->payloadlen,
				"qos", message->qos, "retained", message->retained, "dup",
				message->dup, "msgid", message->msgid);
	else
		result = PyEval_CallFunction(e->ma, "Os#{ss#sisisisi}", e->context,
				topicName, topicLen, "payload", message->payload,
				message->payloadlen, "qos", message->qos, "retained",
				message->retained, "dup", message->dup, "msgid",
				message->msgid);
	if (result)
	{
		if (PyInt_Check(result))
			rc = (int) PyInt_AsLong(result);
		Py_DECREF(result);
	}
	PyGILState_Release(gstate);
	MQTTClient_free(topicName);
	MQTTClient_freeMessage(&message);
	return rc;
}
Example #7
0
int MQTTMessageArrived(void* context, char* topicName, int topicLength, MQTTClient_message* message)
{
    std::vector<char> payload((char*)message->payload, (char*)message->payload + message->payloadlen);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    GD::mqtt->messageReceived(payload);
    return 1;
}
Example #8
0
int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
{
	++test2_arrivedcount;
	MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
					test2_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
	MQTTClient_free(topicName);
	MQTTClient_freeMessage(&m);
	return 1;
}
Example #9
0
int multiThread_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
{
	++multiThread_arrivedcount;
	MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
					multiThread_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
	if (multiThread_pubmsg.payloadlen != m->payloadlen ||
					memcmp(m->payload, multiThread_pubmsg.payload, m->payloadlen) != 0)
	{
		failures++;
		MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", multiThread_pubmsg.payloadlen, m->payloadlen);
	}
	MQTTClient_free(topicName);
	MQTTClient_freeMessage(&m);
	return 1;
}
Example #10
0
// メッセージ到着時
int CConMQTT::msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	// 局所変数宣言
	int i;                 // ループカウンタ
	char* payloadptr;      // ペイロードの先頭アドレス
	int iLenTopic;         // トピック長
	int iMsg;              // メッセージ [0, 1]

	// トピックとメッセージの表示
	printf("Message arrived\n");
	printf("     topic: %s\n", topicName);
	printf("   message: ");

	payloadptr = (char*)message->payload;
	for (i = 0; i<message->payloadlen; i++)
	{
		putchar(*payloadptr++);
	}
	putchar('\n');

	// トピックの最後の文字を抽出
	iLenTopic = strlen(topicName);
	printf("--- %c --- (%d)\n", topicName[iLenTopic -1], iLenTopic);


	// メッセージを比較(メッセージは[0, 1]しか到着しない想定)
	// 1: enterの場合のみ、処理する
	payloadptr = (char*)message->payload;
	iMsg = atoi(payloadptr);

	if (1 == iMsg) {
		printf("[Message Arrived] Enter Sensor: %c\n", topicName[iLenTopic - 1]);
		//reinterpret_cast<CConMQTT*>(context)->vTestMethod();
		::PostMessage(
			reinterpret_cast<CConMQTT*>(context)->m_hWnd, 
			WM_USER_PROC, PROC_RECV_MQTT, 
			(LPARAM)&topicName[iLenTopic - 1]);
	}
	else {
		printf("[Message Arrived] Leave Sensor: %c\n", topicName[iLenTopic - 1]);
	}

	// 解放処理
	MQTTClient_freeMessage(&message);
	MQTTClient_free(topicName);

	return 1;
}
Example #11
0
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");
    payloadptr = static_cast<char*>(message->payload);
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
Example #12
0
// -------------------------------------------------------------------
// Fonction Call-Back reception d'un message MQTT
// -------------------------------------------------------------------
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    unsigned int i;
    char* payloadptr;
    char mqtt_rcv_message[500]; // 3x byte TL + 3x100 bytes V

    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        mqtt_rcv_message[i]=payloadptr[i];
    }

    // Reconstruction du message ALGOID
    processMqttMsg(mqtt_rcv_message, message->payloadlen, topicName, algoidMsgRXStack);

    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
Example #13
0
static void delivered(void *context, MQTTClient_deliveryToken dt)
{
	PMQTTTAPI_HANDLE_DATA transportState = (PMQTTTAPI_HANDLE_DATA)context;

	if (context != NULL)
	{
		//First try to find the message on the list. 
		PDLIST_ENTRY currentListEntry;
		currentListEntry = transportState->messagesSent.Flink;

		if (currentListEntry != NULL)
		{
			while (currentListEntry != &transportState->messagesSent)
			{
				MQTTAPI_MESSAGE_SEND_LIST *mqttMessageDetailEntry = containingRecord(currentListEntry, MQTTAPI_MESSAGE_SEND_LIST, entry);

				//Identify here if the message is the one arrived. 
				if (mqttMessageDetailEntry->dt == dt)
				{
					(void)DList_RemoveEntryList(currentListEntry); //First remove the item from Waiting for Ack List.

					if (transportState->dcCallback != NULL)
					{
						/* Codes_SRS_MQTTAPI_04_009: [The context parameter is a point to the original value passed by MQTTAPI_PublishMessage, which contains message-specific context.] */
						/* Codes_SRS_MQTTAPI_04_052: [result contains the status of the message sent by publish.] */
						transportState->dcCallback(mqttMessageDetailEntry->context, MQTTAPI_CONFIRMATION_OK);
					}

					STRING_delete(mqttMessageDetailEntry->topicName);
					MQTTClient_freeMessage(&(mqttMessageDetailEntry->messageToSend));
					free(mqttMessageDetailEntry);
					break;
				}
				currentListEntry = currentListEntry->Flink;
			}
		}
		else
		{
			LogError("Error trying to access items on messagesSEnt list. Possible not initialized List.\r\n");
		}
	}
}
Example #14
0
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;

    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = message->payload;
    for(i=0; i<message->payloadlen; i++)
    {
        putchar(*payloadptr++);
    }
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    //writePortTest(NULL,NULL);
    char *sendData	=	"nc";
    GW_sendAndReceiveRequest();
    return 1;
}
Example #15
0
int PICNTRL::messageArrived(void *context, char* topicName, int topicLen, MQTTClient_message *message) {
   PICNTRL* myThis = (PICNTRL*)context;
   char buffer[TEMP_BUFFER_SIZE];
   int limit = TEMP_BUFFER_SIZE -1;
   if (message->payloadlen < limit) { 
      limit = message->payloadlen;
   }

   char* tempStart = strstr(topicName, "light");
   if (NULL != tempStart) {
      if (NULL != strstr(tempStart+1, "on")) { 
         digitalWrite(myThis->_lightPin, HIGH );
      } 

      if (NULL != strstr(tempStart+1, "off")) { 
         digitalWrite(myThis->_lightPin, LOW);
      } 
   }

   MQTTClient_freeMessage(&message);

   return 1;
}
//Default function in MQTT paho (Runs when msg is received)
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    char messageText[message->payloadlen];

    printf("Message arrived\n");
    printf("     topic: %s\n", topicName);
    printf("   message: ");

    payloadptr = message->payload;
    for(i=0; i < message->payloadlen; i++)
    {
     //   putchar(*payloadptr++);
	  messageText[i] = *payloadptr++;
    }
	  messageText[i] = '\0';
    lightLED(messageText[0]);
    printf("%s",messageText);
    putchar('\n');
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}
Example #17
0
/* Codes_SRS_MQTTAPI_04_006: [The MQTTAPI_Message parameter contains the structure for the received message, with message payload.]  */
static int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	MQTTAPI_MESSAGE_RECEIVED_LIST* mqttAPIMessgeEntryList;
	int result;
	PMQTTTAPI_HANDLE_DATA mqttApiHandleData = (PMQTTTAPI_HANDLE_DATA)context;
	(void)(topicLen, topicName);

	if (mqttApiHandleData == NULL)
	{
		LogError("Context parameter cannot be null. Ignoring message \r\n");
		result = 1;
	}
	else if (mqttApiHandleData->maCallBack == NULL)
	{
		/* Codes_SRS_MQTTAPI_04_061: [If maCallBack is not set, MQTTAPI_MessageArrived shall return 0 to ignore received messages.]  */
		result = 1;
	}
	else if (message == NULL)
	{
		LogError("message parameter cannot be null. Returning error, so it can be sent again.\r\n");
		result = 0;
	}
	else if ((mqttAPIMessgeEntryList = (MQTTAPI_MESSAGE_RECEIVED_LIST*)malloc(sizeof(MQTTAPI_MESSAGE_RECEIVED_LIST))) == NULL)
	{
		LogError("Memory Allocation Failure for MQTTAPI_Message entry list.\r\n");
		result = 0;
	}
	else if ((mqttAPIMessgeEntryList->messageReceived = (MQTTAPI_Message*)malloc(sizeof(MQTTAPI_Message))) == NULL)
	{
		LogError("Memory Allocation Failure for MQTTAPI_Message.\r\n");
		free(mqttAPIMessgeEntryList);
		result = 0;
	}
	else
	{
		mqttAPIMessgeEntryList->messageReceived->payloadlen = message->payloadlen;		
		
		if ((mqttAPIMessgeEntryList->messageReceived->payload = (unsigned char*)malloc(mqttAPIMessgeEntryList->messageReceived->payloadlen)) == NULL)
		{
			LogError("Memory allocation error.\r\n");
			free(mqttAPIMessgeEntryList->messageReceived);
			free(mqttAPIMessgeEntryList);
			result = 0;
		}
		else
		{
			(void)memcpy(mqttAPIMessgeEntryList->messageReceived->payload, message->payload, message->payloadlen);
			if (Lock(mqttApiHandleData->LockHandle) == LOCK_OK)
			{
				DList_InsertHeadList(&(mqttApiHandleData->messagesReceived), &(mqttAPIMessgeEntryList->entry));
				Unlock(mqttApiHandleData->LockHandle);
				result = 1;
			}
			else
			{
				LogError("Problem Aquiring Lock to add message received on the list.\r\n");
				free(mqttAPIMessgeEntryList->messageReceived->payload);
				free(mqttAPIMessgeEntryList->messageReceived);
				free(mqttAPIMessgeEntryList);
				result = 0;
			}
		}
	}

	//We just free the MQTTClient Message if it was succesfully received, otherwise MQTTClient will call this with a corrupted message.
	if (message != NULL && result == 1)
	{
		MQTTClient_freeMessage(&message);
	}

	return result;
}
Example #18
0
void MQTTAPI_Destroy(MQTTAPI_HANDLE instance)
{
	PMQTTTAPI_HANDLE_DATA mqttHandleData = instance;

	/* Codes_SRS_MQTTAPI_04_021: [If parameter instance is NULL then MQTTAPI_Destroy shall take no action.] */
	if (mqttHandleData != NULL)
	{
		/* Codes_SRS_MQTTAPI_04_022: [MQTTAPI_Destroy shall free all resources used by MQTTAPI_HANDLE.]  */
		STRING_delete(mqttHandleData->device_id);
		STRING_delete(mqttHandleData->device_key);
		STRING_delete(mqttHandleData->sasTokenSr);
		/* Codes_SRS_MQTTAPI_04_049: [If the instance is connected, MQTTAPI_Destroy shall disconnect the instance] */
		if (mqttHandleData->subscribed)
		{
			MQTTAPI_Unsubscribe(mqttHandleData->subscribedTopicHandleData);
		}

		if (mqttHandleData->connected)
		{
			MQTTClient_disconnect(mqttHandleData->client, 0);
		}

		/* Codes_SRS_MQTTAPI_04_053: [If there are messages to be sent MQTTAPI_Destroy shall signalize the user that the message couldn’t be sent by reason of MQTTAPI_CONFIRMATION_BECAUSE_DESTROY]  */
		{
			PDLIST_ENTRY received;
			while ((received = DList_RemoveHeadList(&(mqttHandleData->messagesReceived))) != &(mqttHandleData->messagesReceived))
			{
				MQTTAPI_MESSAGE_RECEIVED_LIST* temp = containingRecord(received, MQTTAPI_MESSAGE_RECEIVED_LIST, entry);
				if (mqttHandleData->maCallBack != NULL)
				{
					if (!mqttHandleData->maCallBack(mqttHandleData->maCallbackContext, temp->messageReceived))
					{
						LogError("Client could not handle message, dropping it.");
					}
				}

				free(temp->messageReceived->payload);
				free(temp->messageReceived);
				free(temp);
			}
		}

		{
			PDLIST_ENTRY messageToSend;
			while ((messageToSend = DList_RemoveHeadList(&(mqttHandleData->messagesToSend))) != &(mqttHandleData->messagesToSend))
			{
				MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(messageToSend, MQTTAPI_MESSAGE_SEND_LIST, entry);

				if (mqttHandleData->dcCallback != NULL)
				{
					mqttHandleData->dcCallback(temp->context, MQTTAPI_CONFIRMATION_BECAUSE_DESTROY);
					MQTTClient_freeMessage(&(temp->messageToSend));
					free(temp->messageToSend);
					STRING_delete(temp->topicName);
					free(temp);
				}
			}
		}

		{
			PDLIST_ENTRY currentListEntry;
			while ((currentListEntry = DList_RemoveHeadList(&(mqttHandleData->messagesSent))) != &(mqttHandleData->messagesSent))
			{
				MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(currentListEntry, MQTTAPI_MESSAGE_SEND_LIST, entry);

				if (mqttHandleData->dcCallback != NULL)
				{
					mqttHandleData->dcCallback(temp->context, MQTTAPI_CONFIRMATION_BECAUSE_DESTROY);
					MQTTClient_freeMessage(&(temp->messageToSend));
					free(temp->messageToSend);
					STRING_delete(temp->topicName);
					free(temp);
				}
			}
		}

		MQTTClient_destroy(mqttHandleData->client);
		Lock_Deinit(mqttHandleData->LockHandle);
		free(mqttHandleData);
	}
}
Example #19
0
int main(int argc, char** argv)
{
	MQTTClient client;
	MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
	char topic[128];
	int rc = 0;
	char url[100];

	create_push_topic(topic, "message");

	printf("topic is %s\n", topic);

  if (strchr(topic, '#') || strchr(topic, '+'))
		opts.showtopics = 1;
  if (opts.showtopics)
		printf("topic is %s\n", topic);

	sprintf(url, "%s:%s", opts.host, opts.port);

	rc = MQTTClient_create(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);

	signal(SIGINT, cfinish);
	signal(SIGTERM, cfinish);

	conn_opts.keepAliveInterval = 10;
	conn_opts.reliable = 0;
	conn_opts.cleansession = 0;
	conn_opts.username = opts.token;
	conn_opts.password = opts.appid;
	
	myconnect(&client, &conn_opts);
	
	rc = MQTTClient_subscribe(client, topic, opts.qos);

	while (!toStop)
	{
		char* topicName = NULL;
		int topicLen;
		MQTTClient_message* message = NULL;
		
		rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
		if (message)
		{
			if (opts.showtopics)
				printf("%s\t", topicName);
      if (opts.nodelimiter)
				printf("%.*s", message->payloadlen, (char*)message->payload);
			else
				printf("%.*s%s", message->payloadlen, (char*)message->payload, opts.delimiter);
			fflush(stdout);
			MQTTClient_freeMessage(&message);
			MQTTClient_free(topicName);
		}
		if (rc != 0)
			myconnect(&client, &conn_opts);
	}
	
	printf("Stopping\n");

	MQTTClient_disconnect(client, 0);

 	MQTTClient_destroy(&client);

	return 0;
}
Example #20
0
File: TT3Dlg.cpp Project: 12019/TT3
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
	//char cS[100];
	char * pBuffer = (char *)malloc(message->payloadlen + 1);
	char * pHash = (char *)malloc(20);
	char * pEnoch = (char *)malloc(20);
	
	//strncpy_s(cS, (char *)message->payload, min(message->payloadlen, sizeof(cS)));
	strncpy(pBuffer, (char *)message->payload, message->payloadlen);
	//cS[message->payloadlen] = 0x0;
	*(pBuffer+message->payloadlen) = 0x0;

	CWnd *tA = theApp.GetMainWnd();
	
	// Adjust counter
	CTT3Dlg *tA2 = (CTT3Dlg *)tA;
	//check if alert must play
	if (tA2->iAlert > 0) {
		// find substring match
		int i = 0;
		char * pAnswer;
		while (i < tA2->iAlert) {
			if (!strcmp(tA2->ALERTME[i][0], "1")) {
				if (strstr(pBuffer, tA2->ALERTME[i][1])) {
					Beep(atoi(tA2->ALERTME[i][2]), atoi(tA2->ALERTME[i][3]));
					// run exe
					char * cmdline = (char *)malloc(2000);
					PROCESS_INFORMATION processInformation;
					STARTUPINFO startupInfo;
					memset(&processInformation, 0, sizeof(processInformation));
					memset(&startupInfo, 0, sizeof(startupInfo));
					startupInfo.cb = sizeof(startupInfo);

					strcpy(cmdline, tA2->ALERTME[i][4]);
					if (strlen(cmdline) > 0) {
						strcat(cmdline, " ");
						strcat(cmdline, topicName);
						strcat(cmdline, ",");
						strcat(cmdline, pBuffer);
						if (!CreateProcess(NULL, cmdline, NULL, NULL, false, 0, NULL, NULL, &startupInfo, &processInformation)) {
							char * pErrMsg = (char *)malloc(500);
							strcpy(pErrMsg, "Unknown application ");
							strcat(pErrMsg, tA2->ALERTME[i][4]);
							MessageBox(NULL, pErrMsg, "Alert Error", MB_OK);
							free(pErrMsg);
						}
					}
				}
			}
			i++;
		}
	}
	CListCtrl *pListctrl = (CListCtrl *)tA->GetDlgItem(IDC_LIST3);
	CButton *pButton1 = (CButton*) tA->GetDlgItem(IDC_CHECK4);

	tA->SetDlgItemInt(IDC_EDIT14, ++tA2->iReceived,FALSE);
	tA->SetDlgItemInt(IDC_EDIT18, tA2->iReceived - tA2->iReceivedRemoved, FALSE);

	// Check maximum messages allowed in listbox
	
	if (pButton1->GetCheck() == BST_CHECKED) {
		int iMaxAllowed  = tA->GetDlgItemInt(IDC_EDIT13,NULL,FALSE);
		int iCurrentRows = pListctrl->GetItemCount();
		while (iCurrentRows >= iMaxAllowed) {
			// Delete oldest one
			pListctrl->DeleteItem(iCurrentRows - 1);
			tA2->iReceivedRemoved++;
			iCurrentRows--;
		}
		tA->SetDlgItemInt(IDC_EDIT15, tA2->iReceivedRemoved,FALSE);
		tA->SetDlgItemInt(IDC_EDIT18, tA2->iReceived - tA2->iReceivedRemoved,FALSE);
	}

	LVITEM lvi;
	
	lvi.mask = LVIF_TEXT;
	lvi.iItem = 0;
	
	lvi.iSubItem = 0;
	_itoa(iHash++, pHash, 10);
	lvi.pszText = pHash;
	pListctrl->InsertItem(&lvi);
	
	lvi.iSubItem = 1;
	time_t timer = time(NULL);
	lvi.pszText = ctime(&timer);
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 2;
	lvi.pszText = topicName;
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 3;
	lvi.pszText = pBuffer;
	pListctrl->SetItem(&lvi);

	lvi.iSubItem = 4;
	itoa(timer, pEnoch, 10);
	lvi.pszText = pEnoch;
	pListctrl->SetItem(&lvi);

	pListctrl->SetColumnWidth(0, LVSCW_AUTOSIZE);
	pListctrl->SetColumnWidth(1, LVSCW_AUTOSIZE);
	pListctrl->SetColumnWidth(3, LVSCW_AUTOSIZE);

	// Set Column width for topic
	int iTW = pListctrl->GetStringWidth(topicName);
	if (iTW > tA2->iTopicWidth && iTW < 400) {
		tA2->iTopicWidth = iTW;
		pListctrl->SetColumnWidth(2, iTW+15);
	}

	//Beep on receive if checkbox selected
	pButton1 = (CButton*) tA->GetDlgItem(IDC_CHECK2);
	if (pButton1->GetCheck() == BST_CHECKED) {
		Beep(300, 200);
	} 

	// See if topic is in subscriptions list
	CListBox *pListbox = (CListBox *)tA->GetDlgItem(IDC_LIST1);
	if (pListbox->FindStringExact(0, topicName) == LB_ERR) {
		pListbox->InsertString(0, topicName);
		tA->SetDlgItemText(IDC_EDIT11, "Consider saving setup");
	}
	
	free(pHash); free(pEnoch); free(pBuffer);
	MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
	
    return 1;
}
Example #21
0
void MQTTAPI_DoWork(MQTTAPI_HANDLE instance)
{
	PMQTTTAPI_HANDLE_DATA handle = instance;

	/* Codes_SRS_MQTTAPI_04_050: [if parameter instance is NULL then MQTTAPI_DoWork shall not perform any action.] */
	if (handle != NULL)
	{

		if (!checkAndTryToConnect(handle))
		{
			LogError("Client Not Connected and could not connect.\r\n");
		}
		else
		{
			if (Lock(handle->LockHandle) == LOCK_OK)
			{

				//message
				{
					PDLIST_ENTRY received;
					while ((received = DList_RemoveHeadList(&(handle->messagesReceived))) != &(handle->messagesReceived))
					{
						MQTTAPI_MESSAGE_RECEIVED_LIST* temp = containingRecord(received, MQTTAPI_MESSAGE_RECEIVED_LIST, entry);
						if (handle->maCallBack != NULL)
						{
							if (!handle->maCallBack(handle->maCallbackContext, temp->messageReceived))
							{
								LogError("Client could not handle message, dropping it.");
							}
						}

						free(temp->messageReceived->payload);
						free(temp->messageReceived);
						free(temp);
					}
				}
				Unlock(handle->LockHandle);
			}
			else
			{
				LogError("Could not aquire lock for message.\r\n");
			}

			//Event
			{
				PDLIST_ENTRY messageToSend;
				while ((messageToSend = DList_RemoveHeadList(&(handle->messagesToSend))) != &(handle->messagesToSend))
				{
					MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(messageToSend, MQTTAPI_MESSAGE_SEND_LIST, entry);

					if (MQTTClient_publishMessage(handle->client, STRING_c_str(temp->topicName), temp->messageToSend, &(temp->dt)) != MQTTCLIENT_SUCCESS)
					{
						handle->dcCallback(temp->context, MQTTAPI_CONFIRMATION_ERROR);
						MQTTClient_freeMessage(&(temp->messageToSend));
						free(temp->messageToSend);
						STRING_delete(temp->topicName);
						free(temp);
					}
					else
					{
						DList_InsertTailList(&(handle->messagesSent), &(temp->entry));
					}
				}
			}
		}
	}
	
}