dosmqtt / src /mqtt.h
HelloSun's picture
Real DOS networking: FreeDOS + NE2000 + mTCP MQTT clients on v86, TCP-to-WSS bridge
c722e6d verified
Raw History Blame Contribute Delete
6.62 kB
// DOS MQTT 3.1.1 client helpers on top of the mTCP TCP/IP library.
// Copyright (C) 2026. Licensed under GPL-3.0-or-later (links against mTCP, GPL-3.0-or-later).
#ifndef MQTTDOS_H
#define MQTTDOS_H
#include <dos.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "trace.h"
#include "utils.h"
#include "packet.h"
#include "arp.h"
#include "udp.h"
#include "dns.h"
#include "tcp.h"
#include "tcpsockm.h"
#include "timer.h"
#define RECV_BUFFER_SIZE (1024)
volatile uint8_t CtrlBreakDetected = 0;
void __interrupt __far ctrlBreakHandler( ) { CtrlBreakDetected = 1; }
void __interrupt __far ctrlCHandler( ) { /* Ctrl-C is a legal character: do nothing */ }
static uint8_t rx[512];
static int rxLen = 0;
// "HH:MM:SS" from the DOS real-time clock.
static void stamp( char *out ) {
struct dostime_t t;
_dos_gettime( &t );
sprintf( out, "%02d:%02d:%02d", t.hour, t.minute, t.second );
}
static uint16_t pseudoPort( void ) {
struct dostime_t t;
_dos_gettime( &t );
return (uint16_t)( 3000 + ( (uint16_t)t.second * 100 + t.hsecond ) % 20000 );
}
static void sleepTicks( uint32_t ms, int pump ) {
clockTicks_t start = TIMER_GET_CURRENT( );
while ( Timer_diff( start, TIMER_GET_CURRENT( ) ) < TIMER_MS_TO_TICKS( ms ) ) {
if ( pump ) Utils::processPackets( );
}
}
static void netShutdown( int rc ) {
Utils::endStack( );
exit( rc );
}
static int netInit( void ) {
if ( Utils::parseEnv( ) != 0 ) return -1;
if ( Utils::initStack( 2, TCP_SOCKET_RING_SIZE, ctrlBreakHandler, ctrlCHandler ) ) return -1;
return 0;
}
static int resolveHost( const char *name, IpAddr_t addr ) {
int8_t rc = Dns::resolve( name, addr, 1 );
if ( rc < 0 ) return -1;
while ( !CtrlBreakDetected && Dns::isQueryPending( ) ) {
Utils::processPackets( );
Dns::drivePendingQuery( );
}
rc = Dns::resolve( name, addr, 0 );
return ( rc == 0 ) ? 0 : -1;
}
// Resolve, then open a TCP connection (10 second timeout).
static TcpSocket *openBroker( const char *host, uint16_t port ) {
IpAddr_t addr;
printf( "DNS: resolving %s ...\n", host );
if ( resolveHost( host, addr ) ) { printf( "DNS: failed\n" ); return NULL; }
printf( "DNS: %s = %d.%d.%d.%d\n", host, addr[0], addr[1], addr[2], addr[3] );
TcpSocket *s = TcpSocketMgr::getSocket( );
s->setRecvBuffer( RECV_BUFFER_SIZE );
uint16_t lport = pseudoPort( );
printf( "TCP: connecting to port %u from local port %u ...\n", port, lport );
if ( s->connect( lport, addr, port, 10000 ) != 0 ) {
printf( "TCP: connect failed\n" );
TcpSocketMgr::freeSocket( s );
return NULL;
}
printf( "TCP: connected\n" );
return s;
}
// Target from the command line (<broker> <port> <topic>) or, if no arguments are
// given, from the DOS environment: MQTT_HOST, MQTT_PORT (default 1883), MQTT_TOPIC.
static int parseTarget( int argc, char *argv[], const char **host, uint16_t *port, const char **topic ) {
if ( argc >= 4 ) {
*host = argv[1]; *port = (uint16_t)atoi( argv[2] ); *topic = argv[3];
return 0;
}
const char *h = getenv( "MQTT_HOST" );
const char *p = getenv( "MQTT_PORT" );
const char *t = getenv( "MQTT_TOPIC" );
if ( !h || !t ) return -1;
*host = h; *port = p ? (uint16_t)atoi( p ) : 1883; *topic = t;
return 0;
}
// ---- MQTT 3.1.1 packet building (QoS 0 only) ----
static int putVarLen( uint8_t *p, int len ) {
int n = 0;
do {
uint8_t b = len % 128;
len /= 128;
if ( len > 0 ) b |= 0x80;
p[n++] = b;
} while ( len > 0 );
return n;
}
static int putStr( uint8_t *p, const char *s ) {
int n = (int)strlen( s );
p[0] = (uint8_t)( n >> 8 );
p[1] = (uint8_t)n;
memcpy( p + 2, s, n );
return n + 2;
}
static int buildConnect( uint8_t *out, const char *clientId ) {
uint8_t body[64];
int n = 0;
n += putStr( body + n, "MQTT" );
body[n++] = 4; // protocol level 3.1.1
body[n++] = 0x02; // clean session
body[n++] = 0; // keepalive 60 s
body[n++] = 60;
n += putStr( body + n, clientId );
out[0] = 0x10;
int h = putVarLen( out + 1, n );
memcpy( out + 1 + h, body, n );
return 1 + h + n;
}
static int buildSubscribe( uint8_t *out, const char *topic, uint16_t id ) {
uint8_t body[128];
int n = 0;
body[n++] = (uint8_t)( id >> 8 );
body[n++] = (uint8_t)id;
n += putStr( body + n, topic );
body[n++] = 0; // requested QoS 0
out[0] = 0x82;
int h = putVarLen( out + 1, n );
memcpy( out + 1 + h, body, n );
return 1 + h + n;
}
static int buildPublish( uint8_t *out, const char *topic, const char *payload ) {
uint8_t body[200];
int n = putStr( body, topic );
int pl = (int)strlen( payload );
memcpy( body + n, payload, pl );
n += pl;
out[0] = 0x30;
int h = putVarLen( out + 1, n );
memcpy( out + 1 + h, body, n );
return 1 + h + n;
}
// ---- incoming stream parsing ----
static void pumpRx( TcpSocket *s ) {
Utils::processPackets( );
if ( rxLen >= (int)sizeof( rx ) ) rxLen = 0; // oversized garbage: resync
int16_t r = s->recv( rx + rxLen, sizeof( rx ) - rxLen );
if ( r > 0 ) rxLen += r;
}
// Returns bytes of one complete packet at the head of rx[], or 0 if incomplete.
static int nextPacket( uint8_t *type, uint8_t **body, int *bodyLen ) {
if ( rxLen < 2 ) return 0;
int mult = 1, len = 0, i = 1;
uint8_t b;
do {
if ( i >= rxLen ) return 0;
b = rx[i++];
len += ( b & 127 ) * mult;
mult *= 128;
} while ( ( b & 128 ) && i < 5 );
if ( rxLen < i + len ) return 0;
*type = rx[0] >> 4;
*body = rx + i;
*bodyLen = len;
return i + len;
}
static void dropPacket( int n ) {
memmove( rx, rx + n, rxLen - n );
rxLen -= n;
}
// Wait up to timeoutMs for a packet of the wanted type (3=PUBLISH is not consumed here).
// Returns 0 on success, -1 on timeout/close.
static int waitFor( TcpSocket *s, uint8_t want, uint32_t timeoutMs ) {
clockTicks_t start = TIMER_GET_CURRENT( );
while ( Timer_diff( start, TIMER_GET_CURRENT( ) ) < TIMER_MS_TO_TICKS( timeoutMs ) ) {
if ( CtrlBreakDetected ) return -1;
pumpRx( s );
uint8_t type; uint8_t *body; int bl;
int n = nextPacket( &type, &body, &bl );
if ( n ) {
int ok = ( type == want );
if ( ok && want == 2 && ( bl < 2 || body[1] != 0 ) ) ok = 0; // CONNACK refused
dropPacket( n );
if ( ok ) return 0;
if ( want == 2 ) return -1;
}
if ( s->isRemoteClosed( ) ) return -1;
}
return -1;
}
static void makeClientId( char *out, const char *prefix ) {
struct dostime_t t;
_dos_gettime( &t );
sprintf( out, "%s%02d%02d%02d%02d", prefix, t.hour, t.minute, t.second, t.hsecond );
}
#endif