File size: 1,353 Bytes
c722e6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | // B.EXE - run once: connect, publish one message containing the DOS clock
// time, disconnect, exit.
// Usage: B <broker> <port> <topic> (or MQTT_HOST/MQTT_PORT/MQTT_TOPIC in the environment)
#include "mqtt.h"
int main( int argc, char *argv[] ) {
uint8_t pkt[256];
char who[16], now[16], text[64];
const char *host; uint16_t port; const char *topic;
if ( parseTarget( argc, argv, &host, &port, &topic ) ) {
printf( "Usage: B <broker> <port> <topic>\n or set MQTT_HOST, MQTT_PORT and MQTT_TOPIC\n" );
return 1;
}
if ( netInit( ) ) { printf( "TCP/IP init failed (is the packet driver loaded?)\n" ); return 2; }
TcpSocket *s = openBroker( host, port );
if ( !s ) netShutdown( 3 );
makeClientId( who, "dosB" );
int n = buildConnect( pkt, who );
s->send( pkt, n );
if ( waitFor( s, 2, 10000 ) ) { printf( "MQTT: CONNECT refused or timed out\n" ); netShutdown( 4 ); }
stamp( now );
sprintf( text, "Hello from DOS B at %s", now );
n = buildPublish( pkt, topic, text );
s->send( pkt, n );
printf( "[%s] B sent: %s\n", now, text );
pkt[0] = 0xE0; pkt[1] = 0; // DISCONNECT
s->send( pkt, 2 );
sleepTicks( 400, 1 ); // let the data and FIN go out
s->close( );
sleepTicks( 300, 1 );
TcpSocketMgr::freeSocket( s );
netShutdown( 0 );
return 0;
}
|