• 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 - port
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 #define KEEPALIVE_INTERVAL 20
26 
27 /* This is to get a timebase in seconds to test the sample */
28 #include <time.h>
29 time_t old_t;
start_ping_timer(void)30 void start_ping_timer(void)
31 {
32 	time(&old_t);
33 	old_t += KEEPALIVE_INTERVAL/2 + 1;
34 }
35 
time_to_ping(void)36 int time_to_ping(void)
37 {
38 time_t t;
39 
40 	time(&t);
41 	if(t >= old_t)
42 	  	return 1;
43 	return 0;
44 }
45 
46 /* This is in order to get an asynchronous signal to stop the sample,
47 as the code loops waiting for msgs on the subscribed topic.
48 Your actual code will depend on your hw and approach*/
49 #include <signal.h>
50 
51 int toStop = 0;
52 
cfinish(int sig)53 void cfinish(int sig)
54 {
55 	signal(SIGINT, NULL);
56 	toStop = 1;
57 }
58 
stop_init(void)59 void stop_init(void)
60 {
61 	signal(SIGINT, cfinish);
62 	signal(SIGTERM, cfinish);
63 }
64 /* */
65 
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
69 	int rc = 0;
70 	int mysock = 0;
71 	unsigned char buf[200];
72 	int buflen = sizeof(buf);
73 	int len = 0;
74 	char *host = MQTT_HOST;
75 	int port = MQTT_PORT;
76 
77 	stop_init();
78 	if (argc > 1)
79 		host = argv[1];
80 
81 	if (argc > 2)
82 		port = atoi(argv[2]);
83 
84 	mysock = transport_open(host, port);
85 	if(mysock < 0)
86 		return mysock;
87 
88 	printf("Sending to hostname %s port %d\n", host, port);
89 
90 	data.clientID.cstring = "me";
91 	data.keepAliveInterval = KEEPALIVE_INTERVAL;
92 	data.cleansession = 1;
93 	data.username.cstring = "testuser";
94 	data.password.cstring = "testpassword";
95 
96 	len = MQTTSerialize_connect(buf, buflen, &data);
97 	rc = transport_sendPacketBuffer(mysock, buf, len);
98 
99 	printf("Sent MQTT connect\n");
100 	/* wait for connack */
101 	if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
102 	{
103 		unsigned char sessionPresent, connack_rc;
104 
105 		if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
106 		{
107 			printf("Unable to connect, return code %d\n", connack_rc);
108 			goto exit;
109 		}
110 	}
111 	else
112 		goto exit;
113 
114 	printf("MQTT connected\n");
115 	start_ping_timer();
116 
117 	while (!toStop)
118 	{
119 		while(!time_to_ping());
120 		len = MQTTSerialize_pingreq(buf, buflen);
121 		transport_sendPacketBuffer(mysock, buf, len);
122 		printf("Ping...");
123 		if (MQTTPacket_read(buf, buflen, transport_getdata) == PINGRESP){
124 			printf("Pong\n");
125 			start_ping_timer();
126 		}
127 		else {
128 			printf("OOPS\n");
129 			goto exit;
130 		}
131 	}
132 
133 	printf("disconnecting\n");
134 	len = MQTTSerialize_disconnect(buf, buflen);
135 	rc = transport_sendPacketBuffer(mysock, buf, len);
136 
137 exit:
138 	transport_close(mysock);
139 	return 0;
140 }
141