Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ping.c Source File

ping.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Sergio R. Caprile - port
00016  *******************************************************************************/
00017 
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <stdlib.h>
00021 
00022 #include "MQTTPacket.h"
00023 #include "transport.h"
00024 
00025 #define KEEPALIVE_INTERVAL 20
00026 
00027 /* This is to get a timebase in seconds to test the sample */
00028 #include <time.h>
00029 time_t old_t;
00030 void start_ping_timer(void)
00031 {
00032     time(&old_t);
00033     old_t += KEEPALIVE_INTERVAL/2 + 1;
00034 }
00035 
00036 int time_to_ping(void)
00037 {
00038 time_t t;
00039 
00040     time(&t);
00041     if(t >= old_t)
00042         return 1;
00043     return 0;
00044 }
00045 
00046 /* This is in order to get an asynchronous signal to stop the sample,
00047 as the code loops waiting for msgs on the subscribed topic.
00048 Your actual code will depend on your hw and approach*/
00049 #include <signal.h>
00050 
00051 int toStop = 0;
00052 
00053 void cfinish(int sig)
00054 {
00055     signal(SIGINT, NULL);
00056     toStop = 1;
00057 }
00058 
00059 void stop_init(void)
00060 {
00061     signal(SIGINT, cfinish);
00062     signal(SIGTERM, cfinish);
00063 }
00064 /* */
00065 
00066 int main(int argc, char *argv[])
00067 {
00068     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00069     int rc = 0;
00070     int mysock = 0;
00071     unsigned char buf[200];
00072     int buflen = sizeof(buf);
00073     int len = 0;
00074     char *host = "m2m.eclipse.org";
00075     int port = 1883;
00076 
00077     stop_init();
00078     if (argc > 1)
00079         host = argv[1];
00080 
00081     if (argc > 2)
00082         port = atoi(argv[2]);
00083 
00084     mysock = transport_open(host, port);
00085     if(mysock < 0)
00086         return mysock;
00087 
00088     printf("Sending to hostname %s port %d\n", host, port);
00089 
00090     data.clientID.cstring = "me";
00091     data.keepAliveInterval = KEEPALIVE_INTERVAL;
00092     data.cleansession = 1;
00093     data.username.cstring = "testuser";
00094     data.password.cstring = "testpassword";
00095 
00096     len = MQTTSerialize_connect(buf, buflen, &data);
00097     rc = transport_sendPacketBuffer(mysock, buf, len);
00098 
00099     printf("Sent MQTT connect\n");
00100     /* wait for connack */
00101     if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
00102     {
00103         unsigned char sessionPresent, connack_rc;
00104 
00105         if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
00106         {
00107             printf("Unable to connect, return code %d\n", connack_rc);
00108             goto exit;
00109         }
00110     }
00111     else
00112         goto exit;
00113 
00114     printf("MQTT connected\n");
00115     start_ping_timer();
00116 
00117     while (!toStop)
00118     {
00119         while(!time_to_ping());
00120         len = MQTTSerialize_pingreq(buf, buflen);
00121         transport_sendPacketBuffer(mysock, buf, len);
00122         printf("Ping...");
00123         if (MQTTPacket_read(buf, buflen, transport_getdata) == PINGRESP){
00124             printf("Pong\n");
00125             start_ping_timer();
00126         }
00127         else {
00128             printf("OOPS\n");
00129             goto exit;
00130         }
00131         
00132     }
00133 
00134     printf("disconnecting\n");
00135     len = MQTTSerialize_disconnect(buf, buflen);
00136     rc = transport_sendPacketBuffer(mysock, buf, len);
00137 
00138 exit:
00139     transport_close(mysock);
00140 
00141     return 0;
00142 }