Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ping_nb.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 and nonblocking 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 enum states { IDLE, GETPONG }; 00067 00068 int main(int argc, char *argv[]) 00069 { 00070 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00071 int rc = 0; 00072 int mysock = 0; 00073 unsigned char buf[200]; 00074 int buflen = sizeof(buf); 00075 int len = 0; 00076 char *host = "m2m.eclipse.org"; 00077 int port = 1883; 00078 MQTTTransport mytransport; 00079 int state; 00080 00081 stop_init(); 00082 if (argc > 1) 00083 host = argv[1]; 00084 00085 if (argc > 2) 00086 port = atoi(argv[2]); 00087 00088 mysock = transport_open(host, port); 00089 if(mysock < 0) 00090 return mysock; 00091 00092 printf("Sending to hostname %s port %d\n", host, port); 00093 00094 mytransport.sck = &mysock; 00095 mytransport.getfn = transport_getdatanb; 00096 mytransport.state = 0; 00097 data.clientID.cstring = "me"; 00098 data.keepAliveInterval = KEEPALIVE_INTERVAL; 00099 data.cleansession = 1; 00100 data.username.cstring = "testuser"; 00101 data.password.cstring = "testpassword"; 00102 00103 len = MQTTSerialize_connect(buf, buflen, &data); 00104 rc = transport_sendPacketBuffer(mysock, buf, len); 00105 00106 printf("Sent MQTT connect\n"); 00107 /* wait for connack */ 00108 do { 00109 int frc; 00110 if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == CONNACK){ 00111 unsigned char sessionPresent, connack_rc; 00112 if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0){ 00113 printf("Unable to connect, return code %d\n", connack_rc); 00114 goto exit; 00115 } 00116 break; 00117 } 00118 else if (frc == -1) 00119 goto exit; 00120 } while (1); /* handle timeouts here */ 00121 00122 printf("MQTT connected\n"); 00123 start_ping_timer(); 00124 00125 state = IDLE; 00126 while (!toStop) { 00127 switch(state){ 00128 case IDLE: 00129 if(time_to_ping()){ 00130 len = MQTTSerialize_pingreq(buf, buflen); 00131 transport_sendPacketBuffer(mysock, buf, len); 00132 printf("Ping..."); 00133 state = GETPONG; 00134 } 00135 break; 00136 case GETPONG: 00137 if((rc=MQTTPacket_readnb(buf, buflen, &mytransport)) == PINGRESP){ 00138 printf("Pong\n"); 00139 start_ping_timer(); 00140 state = IDLE; 00141 } else if(rc == -1){ 00142 printf("OOPS\n"); 00143 goto exit; 00144 } 00145 break; 00146 } 00147 } 00148 00149 printf("disconnecting\n"); 00150 len = MQTTSerialize_disconnect(buf, buflen); 00151 rc = transport_sendPacketBuffer(mysock, buf, len); 00152 00153 exit: 00154 transport_close(mysock); 00155 00156 return 0; 00157 }
Generated on Wed Jul 13 2022 10:46:03 by
1.7.2