• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2014 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  *    http://www.eclipse.org/legal/epl-v10.html
10  * and the Eclipse Distribution License is available at
11  *   http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  *    Ian Craggs - initial API and implementation and/or initial documentation
15  *    Sergio R. Caprile - clarifications and/or documentation extension
16  *******************************************************************************/
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 
22 #include "MQTTPacket.h"
23 #include "transport.h"
24 
25 /* This is in order to get an asynchronous signal to stop the sample,
26 as the code loops waiting for msgs on the subscribed topic.
27 Your actual code will depend on your hw and approach*/
28 #include <signal.h>
29 
30 int toStop = 0;
31 
cfinish(int sig)32 void cfinish(int sig)
33 {
34 	signal(SIGINT, NULL);
35 	toStop = 1;
36 }
37 
stop_init(void)38 void stop_init(void)
39 {
40 	signal(SIGINT, cfinish);
41 	signal(SIGTERM, cfinish);
42 }
43 /* */
44 
main(int argc,char * argv[])45 int main(int argc, char *argv[])
46 {
47 	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
48 	int rc = 0;
49 	int mysock = 0;
50 	unsigned char buf[200];
51 	int buflen = sizeof(buf);
52 	int msgid = 1;
53 	MQTTString topicString = MQTTString_initializer;
54 	int req_qos = 0;
55 	char* payload = "mypayload";
56 	int payloadlen = strlen(payload);
57 	int len = 0;
58 	char *host = MQTT_HOST;
59 	int port = MQTT_PORT;
60 
61 	stop_init();
62 	if (argc > 1)
63 		host = argv[1];
64 
65 	if (argc > 2)
66 		port = atoi(argv[2]);
67 
68 	mysock = transport_open(host, port);
69 	if(mysock < 0)
70 		return mysock;
71 
72 	printf("Sending to hostname %s port %d\n", host, port);
73 
74 	data.clientID.cstring = "me";
75 	data.keepAliveInterval = 20;
76 	data.cleansession = 1;
77 	data.username.cstring = "testuser";
78 	data.password.cstring = "testpassword";
79 
80 	len = MQTTSerialize_connect(buf, buflen, &data);
81 	rc = transport_sendPacketBuffer(mysock, buf, len);
82 
83 	/* wait for connack */
84 	if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
85 	{
86 		unsigned char sessionPresent, connack_rc;
87 
88 		if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
89 		{
90 			printf("Unable to connect, return code %d\n", connack_rc);
91 			goto exit;
92 		}
93 	}
94 	else
95 		goto exit;
96 
97 	/* subscribe */
98 	topicString.cstring = MQTT_TOPIC;
99 	len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
100 
101 	rc = transport_sendPacketBuffer(mysock, buf, len);
102 	if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) 	/* wait for suback */
103 	{
104 		unsigned short submsgid;
105 		int subcount;
106 		int granted_qos;
107 
108 		rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
109 		if (granted_qos != 0)
110 		{
111 			printf("granted qos != 0, %d\n", granted_qos);
112 			goto exit;
113 		}
114 	}
115 	else
116 		goto exit;
117 
118 	/* loop getting msgs on subscribed topic */
119 	topicString.cstring = "pubtopic";
120 	while (!toStop)
121 	{
122 		/* transport_getdata() has a built-in 1 second timeout,
123 		your mileage will vary */
124 		if (MQTTPacket_read(buf, buflen, transport_getdata) == PUBLISH)
125 		{
126 			unsigned char dup;
127 			int qos;
128 			unsigned char retained;
129 			unsigned short msgid;
130 			int payloadlen_in;
131 			unsigned char* payload_in;
132 			int rc;
133 			MQTTString receivedTopic;
134 
135 			rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
136 					&payload_in, &payloadlen_in, buf, buflen);
137 			printf("message arrived %.*s\n", payloadlen_in, payload_in);
138 		}
139 
140 		printf("publishing reading\n");
141 		len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen);
142 		rc = transport_sendPacketBuffer(mysock, buf, len);
143 	}
144 
145 	printf("disconnecting\n");
146 	len = MQTTSerialize_disconnect(buf, buflen);
147 	rc = transport_sendPacketBuffer(mysock, buf, len);
148 
149 exit:
150 	transport_close(mysock);
151 
152 	return 0;
153 }
154