• 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 
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28 	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
29 	int rc = 0;
30 	char buf[200];
31 	int buflen = sizeof(buf);
32 	int mysock = 0;
33 	MQTTString topicString = MQTTString_initializer;
34 	char* payload = "mypayload";
35 	int payloadlen = strlen(payload);
36 	int len = 0;
37 	char *host = MQTT_HOST;
38 	int port = MQTT_PORT;
39 
40 	if (argc > 1)
41 		host = argv[1];
42 
43 	if (argc > 2)
44 		port = atoi(argv[2]);
45 
46 	mysock = transport_open(host,port);
47 	if(mysock < 0)
48 		return mysock;
49 
50 	printf("Sending to hostname %s port %d\n", host, port);
51 
52 	data.clientID.cstring = "me";
53 	data.keepAliveInterval = 20;
54 	data.cleansession = 1;
55 	data.username.cstring = "testuser";
56 	data.password.cstring = "testpassword";
57 	data.MQTTVersion = 4;
58 
59 	len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data);
60 
61 	topicString.cstring = MQTT_TOPIC;
62 	len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0, 0, 0, 0, topicString, (unsigned char *)payload, payloadlen);
63 
64 	len += MQTTSerialize_disconnect((unsigned char *)(buf + len), buflen - len);
65 
66 	rc = transport_sendPacketBuffer(mysock, (unsigned char*)buf, len);
67 	if (rc == len)
68 		printf("Successfully published\n");
69 	else
70 		printf("Publish failed\n");
71 
72 exit:
73 	transport_close(mysock);
74 
75 	return 0;
76 }
77