├── .gitignore ├── LICENSE ├── README.md ├── testclass.pas ├── test.pas ├── mqttclass.pas └── mosquitto.pas /.gitignore: -------------------------------------------------------------------------------- 1 | *.ppu 2 | *.o 3 | *.dylib 4 | *.dll 5 | *.h 6 | 7 | test 8 | testclass 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2018-2019 Karoly Balogh 3 | 4 | Permission to use, copy, modify, and/or distribute this software for 5 | any purpose with or without fee is hereby granted, provided that the 6 | above copyright notice and this permission notice appear in all copies. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 9 | WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 10 | WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 11 | THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR 12 | CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 14 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 15 | CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mosquitto-p 2 | 3 | Free Pascal conversions of the libmosquitto header file `mosquitto.h`, 4 | as part of the Eclipse Mosquitto project, and some Pascal examples 5 | and language integration. 6 | 7 | This allows using the libmosquitto MQTT client library, part of the 8 | Mosquitto MQTT Broker project from Free Pascal applications. 9 | 10 | For the original sources, see: 11 | https://github.com/eclipse/mosquitto 12 | 13 | ### Source Files 14 | 15 | * `mosquitto.pas` - conversion of the C `mosquitto.h` header to Pascal, 16 | provides the same API as the C version 17 | * `mqttclass.pas` - Object Pascal wrapper class to ease the integration 18 | of libmosquitto into Object Oriented applications 19 | * `test.pas` - Test code for the Pascal header unit with C-like API 20 | * `testclass.pas` - Test code for the Object Pascal wrapper class 21 | 22 | ### mqttclass.pas 23 | 24 | It is a fully featured Object Pascal class to handle MQTT connections via 25 | libmosquitto. Apart from providing an OOP-style API, it maps the low-level 26 | C types used by libmosquitto itself to higher level Pascal types. For 27 | example, you can pass a `String` instead of a `PChar/char *` everywhere. 28 | Thanks to Free Pascal's native threading features it can be fully 29 | asynchronous and behave equally on all platforms. This feature also works 30 | on Windows, without depending on pthreads on this platform, unlike 31 | libmosquitto itself. 32 | 33 | ### License 34 | 35 | The Eclipse Mosquitto project is licensed under the Eclipse Public License 1.0. 36 | 37 | The contents of this repository are covered by the ISC License, see the 38 | `LICENSE` file for details. 39 | -------------------------------------------------------------------------------- /testclass.pas: -------------------------------------------------------------------------------- 1 | { 2 | Subscribing to all topics and printing out incoming messages 3 | Simple test code for the mqttclass unit 4 | 5 | Copyright (c) 2019 Karoly Balogh 6 | 7 | See the LICENSE file for licensing details. 8 | } 9 | 10 | {$MODE OBJFPC} 11 | program testclass; 12 | 13 | uses 14 | {$IFDEF HASUNIX} 15 | cthreads, 16 | {$ENDIF} 17 | ctypes, mosquitto, mqttclass; 18 | 19 | type 20 | TMyMQTTConnection = class(TMQTTConnection) 21 | procedure MyOnMessage(const payload: Pmosquitto_message); 22 | end; 23 | 24 | procedure TMyMQTTConnection.MyOnMessage(const payload: Pmosquitto_message); 25 | var 26 | msg: ansistring; 27 | begin 28 | msg:=''; 29 | with payload^ do 30 | begin 31 | { Note that MQTT messages can be binary, but for this test case we just 32 | assume they're printable text, as a test } 33 | SetLength(msg,payloadlen); 34 | Move(payload^,msg[1],payloadlen); 35 | writeln('Topic: [',topic,'] - Message: [',msg,']'); 36 | end; 37 | end; 38 | 39 | var 40 | mqtt: TMyMQTTConnection; 41 | config: TMQTTConfig; 42 | 43 | begin 44 | writeln('Press ENTER to quit.'); 45 | 46 | FillChar(config, sizeof(config), 0); 47 | with config do 48 | begin 49 | port:=1883; 50 | hostname:='localhost'; 51 | keepalives:=60; 52 | end; 53 | 54 | { use MOSQ_LOG_NODEBUG to disable debug logging } 55 | mqtt:=TMyMQTTConnection.Create('TEST',config,MOSQ_LOG_ALL); 56 | try 57 | { This could also go to a custom constructor of the class, 58 | for more complicated setups. } 59 | mqtt.OnMessage:=@mqtt.MyOnMessage; 60 | 61 | mqtt.Connect; 62 | mqtt.Subscribe('#',0); { Subscribe to all topics } 63 | 64 | readln; 65 | except 66 | end; 67 | mqtt.Free; 68 | 69 | end. 70 | -------------------------------------------------------------------------------- /test.pas: -------------------------------------------------------------------------------- 1 | { 2 | Subscribing to all topics and printing out incoming messages 3 | Simple test code for the libmosquitto C API interface unit 4 | 5 | Copyright (c) 2019 Karoly Balogh 6 | 7 | See the LICENSE file for licensing details. 8 | } 9 | 10 | program test; 11 | 12 | uses 13 | mosquitto, ctypes; 14 | 15 | const 16 | MQTT_HOST = 'localhost'; 17 | MQTT_PORT = 1883; 18 | 19 | var 20 | major, minor, revision: cint; 21 | mq: Pmosquitto; 22 | 23 | 24 | procedure mqtt_on_log(mosq: Pmosquitto; obj: pointer; level: cint; const str: pchar); cdecl; 25 | begin 26 | writeln(str); 27 | end; 28 | 29 | procedure mqtt_on_message(mosq: Pmosquitto; obj: pointer; const message: Pmosquitto_message); cdecl; 30 | var 31 | msg: ansistring; 32 | begin 33 | msg:=''; 34 | with message^ do 35 | begin 36 | { Note that MQTT messages can be binary, but for this test case 37 | we just assume they're printable text } 38 | SetLength(msg,payloadlen); 39 | Move(payload^,msg[1],payloadlen); 40 | writeln('Topic: [',topic,'] - Message: [',msg,']'); 41 | end; 42 | end; 43 | 44 | { Here we really just use libmosquitto's C API directly, so see its documentation. } 45 | begin 46 | if mosquitto_lib_init <> MOSQ_ERR_SUCCESS then 47 | begin 48 | writeln('Failed.'); 49 | halt(1); 50 | end; 51 | mosquitto_lib_version(@major,@minor,@revision); 52 | 53 | writeln('Running against libmosquitto ',major,'.',minor,'.',revision); 54 | 55 | mq:=mosquitto_new(nil, true, nil); 56 | if assigned(mq) then 57 | begin 58 | mosquitto_log_callback_set(mq, @mqtt_on_log); 59 | mosquitto_message_callback_set(mq, @mqtt_on_message); 60 | 61 | mosquitto_connect(mq, MQTT_HOST, MQTT_PORT, 60); 62 | mosquitto_subscribe(mq, nil, '#', 1); 63 | 64 | while mosquitto_loop(mq, 100, 1) = MOSQ_ERR_SUCCESS do 65 | begin 66 | { This should ideally handle some keypress or something... } 67 | end; 68 | 69 | mosquitto_disconnect(mq); 70 | mosquitto_destroy(mq); 71 | mq:=nil; 72 | end 73 | else 74 | writeln('ERROR: Cannot create a mosquitto instance.'); 75 | 76 | mosquitto_lib_cleanup; 77 | end. 78 | -------------------------------------------------------------------------------- /mqttclass.pas: -------------------------------------------------------------------------------- 1 | { 2 | 3 | Copyright (c) 2018-2019 Karoly Balogh 4 | 5 | Permission to use, copy, modify, and/or distribute this software for 6 | any purpose with or without fee is hereby granted, provided that the 7 | above copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 | WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 | WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 12 | THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR 13 | CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 15 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 16 | CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | 18 | } 19 | 20 | {$MODE OBJFPC} 21 | unit mqttclass; 22 | 23 | interface 24 | 25 | uses 26 | classes, ctypes, mosquitto; 27 | 28 | type 29 | mqtt_logfunc = procedure(const msg: ansistring); 30 | 31 | function mqtt_init: boolean; overload; 32 | function mqtt_init(const verbose: boolean): boolean; overload; 33 | function mqtt_loglevel_to_str(const loglevel: cint): ansistring; 34 | procedure mqtt_setlogfunc(logfunc: mqtt_logfunc); 35 | 36 | type 37 | TMQTTOnMessageEvent = procedure(const payload: Pmosquitto_message) of object; 38 | TMQTTOnPublishEvent = procedure(const mid: cint) of object; 39 | TMQTTOnSubscribeEvent = procedure(mid: cint; qos_count: cint; const granted_qos: pcint) of object; 40 | TMQTTOnUnsubscribeEvent = procedure(mid: cint) of object; 41 | TMQTTOnConnectEvent = procedure(const rc: cint) of object; 42 | TMQTTOnDisconnectEvent = procedure(const rc: cint) of object; 43 | TMQTTOnLogEvent = procedure(const level: cint; const str: ansistring) of object; 44 | 45 | const 46 | MOSQ_LOG_NODEBUG = MOSQ_LOG_ALL - MOSQ_LOG_DEBUG; 47 | 48 | type 49 | TMQTTConnectionState = ( mqttNone, mqttConnecting, mqttConnected, mqttReconnecting, mqttDisconnected ); 50 | 51 | type 52 | TMQTTConfig = record 53 | ssl: boolean; 54 | ssl_cacertfile: ansistring; 55 | hostname: ansistring; 56 | port: word; 57 | username: ansistring; 58 | password: ansistring; 59 | keepalives: longint; 60 | reconnect_delay: longint; 61 | reconnect_backoff: boolean; 62 | client_id: ansistring; 63 | end; 64 | 65 | type 66 | { This junk uses FPC-supplied threading, because mosquitto for whatever retarded reason 67 | on Windows comes w/o threading enabled by default (CB) } 68 | TMQTTConnection = class(TThread) 69 | protected 70 | FName: string; 71 | FMosquitto: Pmosquitto; 72 | FConfig: TMQTTConfig; 73 | 74 | FOnMessage: TMQTTOnMessageEvent; 75 | FOnPublish: TMQTTOnPublishEvent; 76 | FOnSubscribe: TMQTTOnSubscribeEvent; 77 | FOnUnsubscribe: TMQTTOnUnsubscribeEvent; 78 | FOnConnect: TMQTTOnConnectEvent; 79 | FOnDisconnect: TMQTTOnDisconnectEvent; 80 | FOnLog: TMQTTOnLogEvent; 81 | FMQTTLogLevel: cint; { allowed log levels of the mqttclass } 82 | FMOSQLogLevel: cint; { allowed log levels of the underlying mosquitto lib } 83 | 84 | FAutoReconnect: boolean; 85 | FReconnectTimer: TDateTime; 86 | FReconnectPeriod: longint; 87 | FReconnectDelay: longint; 88 | FReconnectBackoff: boolean; 89 | 90 | FMQTTState: TMQTTConnectionState; 91 | FMQTTStateLock: TRTLCriticalSection; 92 | 93 | procedure SetupReconnectDelay; 94 | function ReconnectDelayExpired: boolean; 95 | 96 | function GetMQTTState: TMQTTConnectionState; 97 | procedure SetMQTTState(state: TMQTTConnectionState); 98 | procedure Execute; override; 99 | public 100 | constructor Create(const name: string; const config: TMQTTConfig); 101 | constructor Create(const name: string; const config: TMQTTConfig; const loglevel: cint); 102 | destructor Destroy; override; 103 | function Connect: cint; 104 | function Reconnect: cint; 105 | function Subscribe(var mid: cint; const sub: ansistring; qos: cint): cint; 106 | function Subscribe(const sub: ansistring; qos: cint): cint; 107 | function Publish(var mid: cint; const topic: ansistring; payloadlen: cint; var payload; qos: cint; retain: cbool): cint; 108 | function Publish(const topic: ansistring; payloadlen: cint; var payload; qos: cint; retain: cbool): cint; 109 | function Publish(var mid: cint; const topic: ansistring; const payload: ansistring; qos: cint; retain: cbool): cint; 110 | function Publish(const topic: ansistring; const payload: ansistring; qos: cint; retain: cbool): cint; 111 | 112 | procedure Log(const level: cint; const message: ansistring); 113 | 114 | property State: TMQTTConnectionState read GetMQTTState write SetMQTTState; 115 | property AutoReconnect: boolean read FAutoReconnect write FAutoReconnect; 116 | property OnMessage: TMQTTOnMessageEvent read FOnMessage write FOnMessage; 117 | property OnPublish: TMQTTOnPublishEvent read FOnPublish write FOnPublish; 118 | property OnSubscribe: TMQTTOnSubscribeEvent read FOnSubscribe write FOnSubscribe; 119 | property OnUnsubscribe: TMQTTOnUnsubscribeEvent read FOnUnsubscribe write FOnUnsubscribe; 120 | property OnConnect: TMQTTOnConnectEvent read FOnConnect write FOnConnect; 121 | property OnDisconnect: TMQTTOnDisconnectEvent read FOnDisconnect write FOnDisconnect; 122 | property OnLog: TMQTTOnLogEvent read FOnLog write FOnLog; 123 | 124 | property MQTTLogLevel: cint read FMQTTLogLevel write FMQTTLogLevel; 125 | property MOSQLogLevel: cint read FMOSQLogLevel write FMOSQLogLevel; 126 | end; 127 | 128 | implementation 129 | 130 | 131 | uses 132 | sysutils, dateutils; 133 | 134 | const 135 | DEFAULT_RECONNECT_PERIOD_MS = 100; 136 | DEFAULT_RECONNECT_DELAY_MS = 60 * 1000; 137 | 138 | var 139 | logger: mqtt_logfunc; 140 | 141 | procedure mqtt_on_message(mosq: Pmosquitto; obj: pointer; const message: Pmosquitto_message); cdecl; forward; 142 | procedure mqtt_on_publish(mosq: Pmosquitto; obj: pointer; mid: cint); cdecl; forward; 143 | procedure mqtt_on_subscribe(mosq: Pmosquitto; obj: pointer; mid: cint; qos_count: cint; const granted_qos: pcint); cdecl; forward; 144 | procedure mqtt_on_unsubscribe(mosq: Pmosquitto; obj: pointer; mid: cint); cdecl; forward; 145 | procedure mqtt_on_connect(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; forward; 146 | procedure mqtt_on_disconnect(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; forward; 147 | procedure mqtt_on_log(mosq: Pmosquitto; obj: pointer; level: cint; const str: pchar); cdecl; forward; 148 | 149 | procedure mqtt_setlogfunc(logfunc: mqtt_logfunc); 150 | begin 151 | logger:=logfunc; 152 | end; 153 | 154 | procedure mqtt_log(const msg: ansistring); 155 | begin 156 | writeln(msg); 157 | end; 158 | 159 | constructor TMQTTConnection.Create(const name: String; const config: TMQTTConfig); 160 | begin 161 | Create(name, config, MOSQ_LOG_ALL); 162 | end; 163 | 164 | constructor TMQTTConnection.Create(const name: String; const config: TMQTTConfig; const loglevel: cint); 165 | var 166 | rc: cint; 167 | begin 168 | inherited Create(true); 169 | 170 | FName:=name; 171 | FConfig:=config; 172 | 173 | FMosquitto:=mosquitto_new(PChar(@FConfig.client_id[1]), true, self); 174 | if FMosquitto=nil then 175 | raise Exception.Create('mosquitto instance creation failure'); 176 | 177 | FAutoReconnect:=true; 178 | InitCriticalSection(FMQTTStateLock); 179 | State:=mqttNone; 180 | 181 | MQTTLogLevel:=loglevel; 182 | MOSQLogLevel:=loglevel; 183 | 184 | FReconnectTimer:=Now; 185 | FReconnectPeriod:=DEFAULT_RECONNECT_PERIOD_MS; 186 | FReconnectDelay:=DEFAULT_RECONNECT_DELAY_MS; 187 | if FConfig.reconnect_delay <> 0 then 188 | FReconnectDelay:=FConfig.reconnect_delay * 1000 189 | else 190 | FAutoReconnect:=false; 191 | FReconnectBackoff:=FConfig.reconnect_backoff; 192 | 193 | mosquitto_threaded_set(Fmosquitto, true); 194 | 195 | mosquitto_log_callback_set(FMosquitto, @mqtt_on_log); 196 | mosquitto_message_callback_set(FMosquitto, @mqtt_on_message); 197 | mosquitto_publish_callback_set(FMosquitto, @mqtt_on_publish); 198 | mosquitto_subscribe_callback_set(FMosquitto, @mqtt_on_subscribe); 199 | mosquitto_unsubscribe_callback_set(FMosquitto, @mqtt_on_unsubscribe); 200 | mosquitto_connect_callback_set(FMosquitto, @mqtt_on_connect); 201 | mosquitto_disconnect_callback_set(FMosquitto, @mqtt_on_disconnect); 202 | 203 | if FConfig.ssl then 204 | begin 205 | if (FConfig.ssl_cacertfile <> '') then 206 | begin 207 | Log(MOSQ_LOG_INFO,'TLS CERT: '+FConfig.ssl_cacertfile); 208 | rc:=mosquitto_tls_set(Fmosquitto, PChar(FConfig.ssl_cacertfile), nil, nil, nil, nil); 209 | if rc <> MOSQ_ERR_SUCCESS then 210 | begin 211 | Log(MOSQ_LOG_ERR,'TLS Setup: '+mosquitto_strerror(rc)); 212 | raise Exception.Create('TLS Setup: '+mosquitto_strerror(rc)); 213 | end; 214 | end 215 | else 216 | Log(MOSQ_LOG_WARNING,'SSL enabled, but no CERT file specified. Skipping TLS setup...'); 217 | end; 218 | 219 | if FConfig.username <> '' then 220 | mosquitto_username_pw_set(Fmosquitto, PChar(FConfig.username), PChar(FConfig.password)); 221 | 222 | Start; { ... the thread } 223 | end; 224 | 225 | destructor TMQTTConnection.Destroy; 226 | begin 227 | mosquitto_disconnect(FMosquitto); 228 | 229 | if not Suspended then 230 | begin 231 | Terminate; { ... the thread } 232 | WaitFor; 233 | end; 234 | 235 | mosquitto_destroy(FMosquitto); 236 | FMosquitto:=nil; 237 | 238 | DoneCriticalSection(FMQTTStateLock); 239 | 240 | inherited; 241 | end; 242 | 243 | function TMQTTConnection.Connect: cint; 244 | begin 245 | Log(MOSQ_LOG_INFO,'Connecting to ['+FConfig.hostname+':'+IntToStr(FConfig.port)+'] - SSL:'+BoolToStr(FConfig.SSL,true)); 246 | result:=mosquitto_connect_async(Fmosquitto, PChar(FConfig.hostname), FConfig.port, FConfig.keepalives); 247 | if result = MOSQ_ERR_SUCCESS then 248 | State:=mqttConnecting 249 | else 250 | begin 251 | State:=mqttDisconnected; 252 | Log(MOSQ_LOG_ERR,'Connection failed with: '+mosquitto_strerror(result)); 253 | end; 254 | end; 255 | 256 | function TMQTTConnection.Reconnect: cint; 257 | begin 258 | Log(MOSQ_LOG_INFO,'Reconnecting to ['+FConfig.hostname+':'+IntToStr(FConfig.port)+'] - SSL:'+BoolToStr(FConfig.SSL,true)); 259 | result:=mosquitto_reconnect_async(Fmosquitto); 260 | if result = MOSQ_ERR_SUCCESS then 261 | State:=mqttConnecting 262 | else 263 | begin 264 | State:=mqttDisconnected; 265 | Log(MOSQ_LOG_ERR,'Reconnection failed with: '+mosquitto_strerror(result)); 266 | end; 267 | end; 268 | 269 | function TMQTTConnection.Subscribe(var mid: cint; const sub: ansistring; qos: cint): cint; 270 | begin 271 | result:=mosquitto_subscribe(Fmosquitto, @mid, PChar(sub), qos); 272 | end; 273 | 274 | function TMQTTConnection.Subscribe(const sub: ansistring; qos: cint): cint; 275 | begin 276 | result:=mosquitto_subscribe(Fmosquitto, nil, PChar(sub), qos); 277 | end; 278 | 279 | 280 | function TMQTTConnection.Publish(var mid: cint; const topic: ansistring; payloadlen: cint; var payload; qos: cint; retain: cbool): cint; 281 | begin 282 | result:=mosquitto_publish(Fmosquitto, @mid, PChar(topic), payloadlen, @payload, qos, retain); 283 | end; 284 | 285 | function TMQTTConnection.Publish(const topic: ansistring; payloadlen: cint; var payload; qos: cint; retain: cbool): cint; 286 | begin 287 | result:=mosquitto_publish(Fmosquitto, nil, PChar(topic), payloadlen, @payload, qos, retain); 288 | end; 289 | 290 | function TMQTTConnection.Publish(var mid: cint; const topic: ansistring; const payload: ansistring; qos: cint; retain: cbool): cint; 291 | begin 292 | result:=mosquitto_publish(Fmosquitto, @mid, PChar(topic), length(payload), PChar(payload), qos, retain); 293 | end; 294 | 295 | function TMQTTConnection.Publish(const topic: ansistring; const payload: ansistring; qos: cint; retain: cbool): cint; 296 | begin 297 | result:=mosquitto_publish(Fmosquitto, nil, PChar(topic), length(payload), PChar(payload), qos, retain); 298 | end; 299 | 300 | 301 | procedure TMQTTConnection.Log(const level: cint; const message: ansistring); 302 | var 303 | tmp: ansistring; 304 | begin 305 | if (MQTTLogLevel and level) > 0 then 306 | begin 307 | writestr(tmp,'[MQTT] [',FName,'] ',mqtt_loglevel_to_str(level),' ',message); 308 | logger(tmp); 309 | end; 310 | end; 311 | 312 | 313 | function TMQTTConnection.GetMQTTState: TMQTTConnectionState; 314 | begin 315 | EnterCriticalSection(FMQTTStateLock); 316 | result:=FMQTTState; 317 | LeaveCriticalSection(FMQTTStateLock); 318 | end; 319 | 320 | procedure TMQTTConnection.SetMQTTState(state: TMQTTConnectionState); 321 | var 322 | tmp: ansistring; 323 | begin 324 | EnterCriticalSection(FMQTTStateLock); 325 | if FMQTTState <> state then 326 | begin 327 | writestr(tmp,'State change: ',FMQTTState,' -> ',state); 328 | Log(MOSQ_LOG_DEBUG,tmp); 329 | FMQTTState:=state; 330 | end; 331 | LeaveCriticalSection(FMQTTStateLock); 332 | end; 333 | 334 | procedure TMQTTConnection.SetupReconnectDelay; 335 | begin 336 | if FReconnectBackoff then 337 | begin 338 | { This is kind of a kludge, but I've got no better idea for a simple 339 | solution - if there was no reconnection attempt for the double of 340 | the reconnect delay, we reset the backoff on the next attempt. (CB) } 341 | if MillisecondsBetween(FReconnectTimer, Now) > (FReconnectDelay * 2) then 342 | FReconnectPeriod:=DEFAULT_RECONNECT_PERIOD_MS 343 | else 344 | FReconnectPeriod:=FReconnectPeriod * 2; 345 | end 346 | else 347 | FReconnectPeriod:=FReconnectDelay; 348 | 349 | if FReconnectPeriod > FReconnectDelay then 350 | FReconnectPeriod:=FReconnectDelay; 351 | 352 | FReconnectTimer:=Now; 353 | Log(MOSQ_LOG_INFO,'Next reconnection attempt in '+FloatToStr(FReconnectPeriod / 1000)+' seconds.'); 354 | end; 355 | 356 | function TMQTTConnection.ReconnectDelayExpired: boolean; 357 | begin 358 | result:=MillisecondsBetween(FReconnectTimer, Now) > FReconnectPeriod; 359 | end; 360 | 361 | 362 | procedure TMQTTConnection.Execute; 363 | begin 364 | try 365 | Log(MOSQ_LOG_DEBUG,'Entering subthread...'); 366 | { OK, so this piece has to manage the entire reconnecting logic, because 367 | libmosquitto only has the reconnection logic and state machine, if the 368 | code uses the totally blocking mosquitto_loop_forever(), which has a 369 | few quirks to say the least, plus due to its blocking nature, has 370 | a few interoperability issues with Pascal threading... (CB) } 371 | while not (Terminated and (State in [mqttDisconnected, mqttReconnecting, mqttNone ] )) do 372 | begin 373 | case State of 374 | mqttNone: 375 | Sleep(100); 376 | mqttDisconnected: 377 | if AutoReconnect then 378 | begin 379 | SetupReconnectDelay; 380 | State:=mqttReconnecting; 381 | end 382 | else 383 | begin 384 | Log(MOSQ_LOG_INFO,'Automatic reconnection disabled, going standby.'); 385 | State:=mqttNone; 386 | end; 387 | mqttReconnecting: 388 | begin 389 | if ReconnectDelayExpired then 390 | Reconnect 391 | else 392 | Sleep(100); 393 | end; 394 | mqttConnected, mqttConnecting: 395 | mosquitto_loop(Fmosquitto, 100, 1); 396 | end; 397 | end; 398 | State:=mqttNone; 399 | except 400 | { FIX ME: this really needs something better } 401 | Log(MOSQ_LOG_ERR,'Exception in subthread, leaving...'); 402 | end; 403 | Log(MOSQ_LOG_DEBUG,'Exiting subthread.'); 404 | end; 405 | 406 | 407 | 408 | 409 | function mqtt_loglevel_to_str(const loglevel: cint): ansistring; 410 | begin 411 | mqtt_loglevel_to_str:='UNKNOWN'; 412 | case loglevel of 413 | MOSQ_LOG_INFO: mqtt_loglevel_to_str:='INFO'; 414 | MOSQ_LOG_NOTICE: mqtt_loglevel_to_str:='NOTICE'; 415 | MOSQ_LOG_WARNING: mqtt_loglevel_to_str:='WARNING'; 416 | MOSQ_LOG_ERR: mqtt_loglevel_to_str:='ERROR'; 417 | MOSQ_LOG_DEBUG: mqtt_loglevel_to_str:='DEBUG'; 418 | end; 419 | end; 420 | 421 | 422 | procedure mqtt_on_message(mosq: Pmosquitto; obj: pointer; const message: Pmosquitto_message); cdecl; 423 | var 424 | Fmosquitto: TMQTTConnection absolute obj; 425 | begin 426 | if assigned(Fmosquitto) and assigned(FMosquitto.OnMessage) then 427 | Fmosquitto.OnMessage(message); 428 | end; 429 | 430 | procedure mqtt_on_publish(mosq: Pmosquitto; obj: pointer; mid: cint); cdecl; 431 | var 432 | Fmosquitto: TMQTTConnection absolute obj; 433 | begin 434 | if assigned(Fmosquitto) then 435 | with FMosquitto do 436 | begin 437 | Log(MOSQ_LOG_DEBUG,'Publish ID: '+IntToStr(mid)); 438 | if assigned(OnPublish) then 439 | OnPublish(mid); 440 | end; 441 | end; 442 | 443 | procedure mqtt_on_subscribe(mosq: Pmosquitto; obj: pointer; mid: cint; qos_count: cint; const granted_qos: pcint); cdecl; 444 | var 445 | FMosquitto: TMQTTConnection absolute obj; 446 | begin 447 | if assigned(FMosquitto) and assigned(FMosquitto.OnSubscribe) then 448 | Fmosquitto.OnSubscribe(mid, qos_count, granted_qos); 449 | end; 450 | 451 | procedure mqtt_on_unsubscribe(mosq: Pmosquitto; obj: pointer; mid: cint); cdecl; 452 | var 453 | Fmosquitto: TMQTTConnection absolute obj; 454 | begin 455 | if assigned(FMosquitto) and assigned(FMosquitto.OnUnsubscribe) then 456 | FMosquitto.OnUnsubscribe(mid); 457 | end; 458 | 459 | procedure mqtt_on_connect(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; 460 | var 461 | FMosquitto: TMQTTConnection absolute obj; 462 | begin 463 | if assigned(FMosquitto) then 464 | with FMosquitto do 465 | begin 466 | Log(MOSQ_LOG_INFO,'Broker connection: '+mosquitto_strerror(rc)); 467 | State:=mqttConnected; 468 | if assigned(OnConnect) then 469 | OnConnect(rc); 470 | end; 471 | end; 472 | 473 | procedure mqtt_on_disconnect(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; 474 | const 475 | disconnect_reason: array[boolean] of string[31] = ('Connection lost', 'Normal termination'); 476 | var 477 | Fmosquitto: TMQTTConnection absolute obj; 478 | begin 479 | if assigned(FMosquitto) then 480 | with FMosquitto do 481 | begin 482 | Log(MOSQ_LOG_INFO,'Broker disconnected: '+disconnect_reason[rc = 0]); 483 | State:=mqttDisconnected; 484 | if assigned(OnDisconnect) then 485 | OnDisconnect(rc); 486 | end; 487 | end; 488 | 489 | procedure mqtt_on_log(mosq: Pmosquitto; obj: pointer; level: cint; const str: pchar); cdecl; 490 | var 491 | Fmosquitto: TMQTTConnection absolute obj; 492 | tmp: ansistring; 493 | begin 494 | tmp:=''; 495 | 496 | if assigned(Fmosquitto) then 497 | with Fmosquitto do 498 | begin 499 | if (MOSQLogLevel and level) > 0 then 500 | begin 501 | writestr(tmp,'[MOSQUITTO] [',FName,'] ',mqtt_loglevel_to_str(level),' ',str); 502 | logger(tmp) 503 | end; 504 | if assigned(OnLog) then 505 | OnLog(level, str); 506 | end 507 | else 508 | begin 509 | writestr(tmp,'[MOSQUITTO] [UNNAMED] ',mqtt_loglevel_to_str(level),' ',str); 510 | logger(tmp); 511 | end; 512 | end; 513 | 514 | var 515 | libinited: boolean; 516 | 517 | 518 | function mqtt_init: boolean; 519 | begin 520 | result:=mqtt_init(true); 521 | end; 522 | 523 | function mqtt_init(const verbose: boolean): boolean; 524 | var 525 | major, minor, revision: cint; 526 | begin 527 | result:=libinited; 528 | if not libinited then 529 | begin 530 | if verbose then 531 | logger('[MOSQUITTO] mosquitto init failed.'); 532 | exit; 533 | end; 534 | 535 | if verbose then 536 | begin 537 | mosquitto_lib_version(@major,@minor,@revision); 538 | 539 | logger('[MQTT] Compiled against mosquitto header version '+IntToStr(LIBMOSQUITTO_MAJOR)+'.'+IntToStr(LIBMOSQUITTO_MINOR)+'.'+IntToStr(LIBMOSQUITTO_REVISION)); 540 | logger('[MQTT] Running against libmosquitto version '+IntToStr(major)+'.'+IntToStr(minor)+'.'+IntToStr(revision)); 541 | end; 542 | end; 543 | 544 | initialization 545 | libinited:=mosquitto_lib_init = MOSQ_ERR_SUCCESS; 546 | logger:=@mqtt_log; 547 | finalization 548 | if libinited then 549 | mosquitto_lib_cleanup; 550 | end. 551 | -------------------------------------------------------------------------------- /mosquitto.pas: -------------------------------------------------------------------------------- 1 | {$MODE FPC} 2 | {$PACKRECORDS C} 3 | unit mosquitto; 4 | 5 | {* 6 | Copyright (c) 2010-2019 Roger Light 7 | 8 | All rights reserved. This program and the accompanying materials 9 | are made available under the terms of the Eclipse Public License v1.0 10 | and Eclipse Distribution License v1.0 which accompany this distribution. 11 | 12 | The Eclipse Public License is available at 13 | http://www.eclipse.org/legal/epl-v10.html 14 | and the Eclipse Distribution License is available at 15 | http://www.eclipse.org/org/documents/edl-v10.php. 16 | 17 | Contributors: 18 | Roger Light - initial implementation and documentation. 19 | *} 20 | 21 | {* 22 | * Free Pascal header conversion 23 | * Copyright (c) 2018-2019 Karoly Balogh 24 | * 25 | * http://github.com/chainq/mosquitto-p 26 | *} 27 | 28 | interface 29 | 30 | uses 31 | ctypes; 32 | 33 | { This is a kludge, because apparently GCC is confused about 34 | how C booleans should work, and optimizes code away inside 35 | libmosquitto on some platforms, which breaks the 'non zero 36 | means true in C' assumption of FPC. (CB) } 37 | type 38 | cbool = boolean; { longbool in ctypes } 39 | pcbool = ^cbool; 40 | 41 | const 42 | {$IFDEF HASUNIX} 43 | {$IFDEF DARWIN} 44 | {$LINKLIB mosquitto} 45 | libmosq_NAME = 'libmosquitto.dynlib'; 46 | {$ELSE} 47 | libmosq_NAME = 'libmosquitto.so'; 48 | {$ENDIF} 49 | {$ELSE} 50 | {$IFDEF MSWINDOWS} 51 | libmosq_NAME = 'mosquitto.dll'; 52 | {$ELSE} 53 | {$ERROR Unsupported platform.} 54 | {$ENDIF MSWINDOWS} 55 | {$ENDIF HASUNIX} 56 | 57 | 58 | const 59 | LIBMOSQUITTO_MAJOR = 1; 60 | LIBMOSQUITTO_MINOR = 5; 61 | LIBMOSQUITTO_REVISION = 8; 62 | {* LIBMOSQUITTO_VERSION_NUMBER looks like 1002001 for e.g. version 1.2.1. *} 63 | LIBMOSQUITTO_VERSION_NUMBER = (LIBMOSQUITTO_MAJOR*1000000+LIBMOSQUITTO_MINOR*1000+LIBMOSQUITTO_REVISION); 64 | 65 | {* Log types *} 66 | const 67 | MOSQ_LOG_NONE = $00; 68 | MOSQ_LOG_INFO = $01; 69 | MOSQ_LOG_NOTICE = $02; 70 | MOSQ_LOG_WARNING = $04; 71 | MOSQ_LOG_ERR = $08; 72 | MOSQ_LOG_DEBUG = $10; 73 | MOSQ_LOG_SUBSCRIBE = $20; 74 | MOSQ_LOG_UNSUBSCRIBE = $40; 75 | MOSQ_LOG_WEBSOCKETS = $80; 76 | MOSQ_LOG_ALL = $FFFF; 77 | 78 | {* Error values *} 79 | const 80 | MOSQ_ERR_CONN_PENDING = -1; 81 | MOSQ_ERR_SUCCESS = 0; 82 | MOSQ_ERR_NOMEM = 1; 83 | MOSQ_ERR_PROTOCOL = 2; 84 | MOSQ_ERR_INVAL = 3; 85 | MOSQ_ERR_NO_CONN = 4; 86 | MOSQ_ERR_CONN_REFUSED = 5; 87 | MOSQ_ERR_NOT_FOUND = 6; 88 | MOSQ_ERR_CONN_LOST = 7; 89 | MOSQ_ERR_TLS = 8; 90 | MOSQ_ERR_PAYLOAD_SIZE = 9; 91 | MOSQ_ERR_NOT_SUPPORTED = 10; 92 | MOSQ_ERR_AUTH = 11; 93 | MOSQ_ERR_ACL_DENIED = 12; 94 | MOSQ_ERR_UNKNOWN = 13; 95 | MOSQ_ERR_ERRNO = 14; 96 | MOSQ_ERR_EAI = 15; 97 | MOSQ_ERR_PROXY = 16; 98 | MOSQ_ERR_PLUGIN_DEFER = 17; 99 | MOSQ_ERR_MALFORMED_UTF8 = 18; 100 | MOSQ_ERR_KEEPALIVE = 19; 101 | MOSQ_ERR_LOOKUP = 20; 102 | 103 | 104 | {* Error values *} 105 | const 106 | MOSQ_OPT_PROTOCOL_VERSION = 1; 107 | MOSQ_OPT_SSL_CTX = 2; 108 | MOSQ_OPT_SSL_CTX_WITH_DEFAULTS = 3; 109 | 110 | 111 | {* MQTT specification restricts client ids to a maximum of 23 characters *} 112 | const 113 | MOSQ_MQTT_ID_MAX_LENGTH = 23; 114 | 115 | const 116 | MQTT_PROTOCOL_V31 = 3; 117 | MQTT_PROTOCOL_V311 = 4; 118 | 119 | type 120 | PPmosquitto_message = ^Pmosquitto_message; 121 | Pmosquitto_message = ^Tmosquitto_message; 122 | Tmosquitto_message = record 123 | mid: cint; 124 | topic: pchar; 125 | payload: pointer; 126 | payloadlen: cint; 127 | qos: cint; 128 | retain: cbool; 129 | end; 130 | 131 | type 132 | Pmosquitto = ^Tmosquitto; 133 | Tmosquitto = type array of byte; 134 | 135 | 136 | {* 137 | * Topic: Threads 138 | * libmosquitto provides thread safe operation, with the exception of 139 | * which is not thread safe. 140 | * 141 | * If your application uses threads you must use to 142 | * tell the library this is the case, otherwise it makes some optimisations 143 | * for the single threaded case that may result in unexpected behaviour for 144 | * the multi threaded case. 145 | *} 146 | {*************************************************** 147 | * Important note 148 | * 149 | * The following functions that deal with network operations will return 150 | * MOSQ_ERR_SUCCESS on success, but this does not mean that the operation has 151 | * taken place. An attempt will be made to write the network data, but if the 152 | * socket is not available for writing at that time then the packet will not be 153 | * sent. To ensure the packet is sent, call mosquitto_loop() (which must also 154 | * be called to process incoming network data). 155 | * This is especially important when disconnecting a client that has a will. If 156 | * the broker does not receive the DISCONNECT command, it will assume that the 157 | * client has disconnected unexpectedly and send the will. 158 | * 159 | * mosquitto_connect() 160 | * mosquitto_disconnect() 161 | * mosquitto_subscribe() 162 | * mosquitto_unsubscribe() 163 | * mosquitto_publish() 164 | ***************************************************} 165 | 166 | {* 167 | * Function: mosquitto_lib_version 168 | * 169 | * Can be used to obtain version information for the mosquitto library. 170 | * This allows the application to compare the library version against the 171 | * version it was compiled against by using the LIBMOSQUITTO_MAJOR, 172 | * LIBMOSQUITTO_MINOR and LIBMOSQUITTO_REVISION defines. 173 | * 174 | * Parameters: 175 | * major - an integer pointer. If not NULL, the major version of the 176 | * library will be returned in this variable. 177 | * minor - an integer pointer. If not NULL, the minor version of the 178 | * library will be returned in this variable. 179 | * revision - an integer pointer. If not NULL, the revision of the library will 180 | * be returned in this variable. 181 | * 182 | * Returns: 183 | * LIBMOSQUITTO_VERSION_NUMBER, which is a unique number based on the major, 184 | * minor and revision values. 185 | * See Also: 186 | * , 187 | *} 188 | 189 | function mosquitto_lib_version(major: pcint; minor: pcint; revision: pcint): cint; cdecl; external libmosq_NAME; 190 | 191 | {* 192 | * Function: mosquitto_lib_init 193 | * 194 | * Must be called before any other mosquitto functions. 195 | * 196 | * This function is *not* thread safe. 197 | * 198 | * Returns: 199 | * MOSQ_ERR_SUCCESS - always 200 | * 201 | * See Also: 202 | * , 203 | *} 204 | function mosquitto_lib_init: cint; cdecl; external libmosq_NAME; 205 | 206 | {* 207 | * Function: mosquitto_lib_cleanup 208 | * 209 | * Call to free resources associated with the library. 210 | * 211 | * Returns: 212 | * MOSQ_ERR_SUCCESS - always 213 | * 214 | * See Also: 215 | * , 216 | *} 217 | function mosquitto_lib_cleanup: cint; cdecl; external libmosq_NAME; 218 | 219 | {* 220 | * Function: mosquitto_new 221 | * 222 | * Create a new mosquitto client instance. 223 | * 224 | * Parameters: 225 | * id - String to use as the client id. If NULL, a random client id 226 | * will be generated. If id is NULL, clean_session must be true. 227 | * clean_session - set to true to instruct the broker to clean all messages 228 | * and subscriptions on disconnect, false to instruct it to 229 | * keep them. See the man page mqtt(7) for more details. 230 | * Note that a client will never discard its own outgoing 231 | * messages on disconnect. Calling or 232 | * will cause the messages to be resent. 233 | * Use to reset a client to its 234 | * original state. 235 | * Must be set to true if the id parameter is NULL. 236 | * obj - A user pointer that will be passed as an argument to any 237 | * callbacks that are specified. 238 | * 239 | * Returns: 240 | * Pointer to a struct mosquitto on success. 241 | * NULL on failure. Interrogate errno to determine the cause for the failure: 242 | * - ENOMEM on out of memory. 243 | * - EINVAL on invalid input parameters. 244 | * 245 | * See Also: 246 | * , , 247 | *} 248 | function mosquitto_new(const id: PChar; clean_session: cbool; obj: Pointer): Pmosquitto; cdecl; external libmosq_NAME; 249 | 250 | {* 251 | * Function: mosquitto_destroy 252 | * 253 | * Use to free memory associated with a mosquitto client instance. 254 | * 255 | * Parameters: 256 | * mosq - a struct mosquitto pointer to free. 257 | * 258 | * See Also: 259 | * , 260 | *} 261 | procedure mosquitto_destroy(mosq: Pmosquitto); cdecl; external libmosq_NAME; 262 | 263 | {* 264 | * Function: mosquitto_reinitialise 265 | * 266 | * This function allows an existing mosquitto client to be reused. Call on a 267 | * mosquitto instance to close any open network connections, free memory 268 | * and reinitialise the client with the new parameters. The end result is the 269 | * same as the output of . 270 | * 271 | * Parameters: 272 | * mosq - a valid mosquitto instance. 273 | * id - string to use as the client id. If NULL, a random client id 274 | * will be generated. If id is NULL, clean_session must be true. 275 | * clean_session - set to true to instruct the broker to clean all messages 276 | * and subscriptions on disconnect, false to instruct it to 277 | * keep them. See the man page mqtt(7) for more details. 278 | * Must be set to true if the id parameter is NULL. 279 | * obj - A user pointer that will be passed as an argument to any 280 | * callbacks that are specified. 281 | * 282 | * Returns: 283 | * MOSQ_ERR_SUCCESS - on success. 284 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 285 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 286 | * 287 | * See Also: 288 | * , 289 | *} 290 | function mosquitto_reinitialise(mosq: Pmosquitto; const id: Pchar; clean_session: cbool; obj: Pointer): cint; cdecl; external libmosq_NAME; 291 | 292 | {* 293 | * Function: mosquitto_will_set 294 | * 295 | * Configure will information for a mosquitto instance. By default, clients do 296 | * not have a will. This must be called before calling . 297 | * 298 | * Parameters: 299 | * mosq - a valid mosquitto instance. 300 | * topic - the topic on which to publish the will. 301 | * payloadlen - the size of the payload (bytes). Valid values are between 0 and 302 | * 268,435,455. 303 | * payload - pointer to the data to send. If payloadlen > 0 this must be a 304 | * valid memory location. 305 | * qos - integer value 0, 1 or 2 indicating the Quality of Service to be 306 | * used for the will. 307 | * retain - set to true to make the will a retained message. 308 | * 309 | * Returns: 310 | * MOSQ_ERR_SUCCESS - on success. 311 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 312 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 313 | * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large. 314 | * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8. 315 | *} 316 | function mosquitto_will_set(mosq: Pmosquitto; const topic: pchar; payloadlen: cint; const payload: pointer; qos: cint; retain: cbool): cint; cdecl; external libmosq_NAME; 317 | 318 | {* 319 | * Function: mosquitto_will_clear 320 | * 321 | * Remove a previously configured will. This must be called before calling 322 | * . 323 | * 324 | * Parameters: 325 | * mosq - a valid mosquitto instance. 326 | * 327 | * Returns: 328 | * MOSQ_ERR_SUCCESS - on success. 329 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 330 | *} 331 | function mosquitto_will_clear(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 332 | 333 | {* 334 | * Function: mosquitto_username_pw_set 335 | * 336 | * Configure username and password for a mosquitton instance. This is only 337 | * supported by brokers that implement the MQTT spec v3.1. By default, no 338 | * username or password will be sent. 339 | * If username is NULL, the password argument is ignored. 340 | * This must be called before calling mosquitto_connect(). 341 | * 342 | * This is must be called before calling . 343 | * 344 | * Parameters: 345 | * mosq - a valid mosquitto instance. 346 | * username - the username to send as a string, or NULL to disable 347 | * authentication. 348 | * password - the password to send as a string. Set to NULL when username is 349 | * valid in order to send just a username. 350 | * 351 | * Returns: 352 | * MOSQ_ERR_SUCCESS - on success. 353 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 354 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 355 | *} 356 | function mosquitto_username_pw_set(mosq: Pmosquitto; const username: pchar; const password: pchar): cint; cdecl; external libmosq_NAME; 357 | 358 | {* 359 | * Function: mosquitto_connect 360 | * 361 | * Connect to an MQTT broker. 362 | * 363 | * Parameters: 364 | * mosq - a valid mosquitto instance. 365 | * host - the hostname or ip address of the broker to connect to. 366 | * port - the network port to connect to. Usually 1883. 367 | * keepalive - the number of seconds after which the broker should send a PING 368 | * message to the client if no other messages have been exchanged 369 | * in that time. 370 | * 371 | * Returns: 372 | * MOSQ_ERR_SUCCESS - on success. 373 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 374 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 375 | * contains the error code, even on Windows. 376 | * Use strerror_r() where available or FormatMessage() on 377 | * Windows. 378 | * 379 | * See Also: 380 | * , , , , 381 | *} 382 | function mosquitto_connect(mosq: Pmosquitto; const host: pchar; port: cint; keepalive: cint): cint; cdecl; external libmosq_NAME; 383 | 384 | {* 385 | * Function: mosquitto_connect_bind 386 | * 387 | * Connect to an MQTT broker. This extends the functionality of 388 | * by adding the bind_address parameter. Use this function 389 | * if you need to restrict network communication over a particular interface. 390 | * 391 | * Parameters: 392 | * mosq - a valid mosquitto instance. 393 | * host - the hostname or ip address of the broker to connect to. 394 | * port - the network port to connect to. Usually 1883. 395 | * keepalive - the number of seconds after which the broker should send a PING 396 | * message to the client if no other messages have been exchanged 397 | * in that time. 398 | * bind_address - the hostname or ip address of the local network interface to 399 | * bind to. 400 | * 401 | * Returns: 402 | * MOSQ_ERR_SUCCESS - on success. 403 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 404 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 405 | * contains the error code, even on Windows. 406 | * Use strerror_r() where available or FormatMessage() on 407 | * Windows. 408 | * 409 | * See Also: 410 | * , , 411 | *} 412 | function mosquitto_connect_bind(mosq: Pmosquitto; const host: pchar; port: cint; keepalive: cint; const bind_address: pchar): cint; cdecl; external libmosq_NAME; 413 | 414 | {* 415 | * Function: mosquitto_connect_async 416 | * 417 | * Connect to an MQTT broker. This is a non-blocking call. If you use 418 | * your client must use the threaded interface 419 | * . If you need to use , you must use 420 | * to connect the client. 421 | * 422 | * May be called before or after . 423 | * 424 | * Parameters: 425 | * mosq - a valid mosquitto instance. 426 | * host - the hostname or ip address of the broker to connect to. 427 | * port - the network port to connect to. Usually 1883. 428 | * keepalive - the number of seconds after which the broker should send a PING 429 | * message to the client if no other messages have been exchanged 430 | * in that time. 431 | * 432 | * Returns: 433 | * MOSQ_ERR_SUCCESS - on success. 434 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 435 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 436 | * contains the error code, even on Windows. 437 | * Use strerror_r() where available or FormatMessage() on 438 | * Windows. 439 | * 440 | * See Also: 441 | * , , , , 442 | *} 443 | function mosquitto_connect_async(mosq: Pmosquitto; const host: pchar; port: cint; keepalive: cint): cint; cdecl; external libmosq_NAME; 444 | 445 | {* 446 | * Function: mosquitto_connect_bind_async 447 | * 448 | * Connect to an MQTT broker. This is a non-blocking call. If you use 449 | * your client must use the threaded interface 450 | * . If you need to use , you must use 451 | * to connect the client. 452 | * 453 | * This extends the functionality of by adding the 454 | * bind_address parameter. Use this function if you need to restrict network 455 | * communication over a particular interface. 456 | * 457 | * May be called before or after . 458 | * 459 | * Parameters: 460 | * mosq - a valid mosquitto instance. 461 | * host - the hostname or ip address of the broker to connect to. 462 | * port - the network port to connect to. Usually 1883. 463 | * keepalive - the number of seconds after which the broker should send a PING 464 | * message to the client if no other messages have been exchanged 465 | * in that time. 466 | * bind_address - the hostname or ip address of the local network interface to 467 | * bind to. 468 | * 469 | * Returns: 470 | * MOSQ_ERR_SUCCESS - on success. 471 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 472 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 473 | * contains the error code, even on Windows. 474 | * Use strerror_r() where available or FormatMessage() on 475 | * Windows. 476 | * 477 | * See Also: 478 | * , , 479 | *} 480 | function mosquitto_connect_bind_async(mosq: Pmosquitto; const host: pchar; port: cint; keepalive: cint; const bind_address: pchar): cint; cdecl; external libmosq_NAME; 481 | 482 | {* 483 | * Function: mosquitto_connect_srv 484 | * 485 | * Connect to an MQTT broker. This is a non-blocking call. If you use 486 | * your client must use the threaded interface 487 | * . If you need to use , you must use 488 | * to connect the client. 489 | * 490 | * This extends the functionality of by adding the 491 | * bind_address parameter. Use this function if you need to restrict network 492 | * communication over a particular interface. 493 | * 494 | * May be called before or after . 495 | * 496 | * Parameters: 497 | * mosq - a valid mosquitto instance. 498 | * host - the hostname or ip address of the broker to connect to. 499 | * keepalive - the number of seconds after which the broker should send a PING 500 | * message to the client if no other messages have been exchanged 501 | * in that time. 502 | * bind_address - the hostname or ip address of the local network interface to 503 | * bind to. 504 | * 505 | * Returns: 506 | * MOSQ_ERR_SUCCESS - on success. 507 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 508 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 509 | * contains the error code, even on Windows. 510 | * Use strerror_r() where available or FormatMessage() on 511 | * Windows. 512 | * 513 | * See Also: 514 | * , , 515 | *} 516 | function mosquitto_connect_srv(mosq: Pmosquitto; const host: pchar; keepalive: cint; const bind_address: pchar): cint; cdecl; external libmosq_NAME; 517 | 518 | {* 519 | * Function: mosquitto_reconnect 520 | * 521 | * Reconnect to a broker. 522 | * 523 | * This function provides an easy way of reconnecting to a broker after a 524 | * connection has been lost. It uses the values that were provided in the 525 | * call. It must not be called before 526 | * . 527 | * 528 | * Parameters: 529 | * mosq - a valid mosquitto instance. 530 | * 531 | * Returns: 532 | * MOSQ_ERR_SUCCESS - on success. 533 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 534 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 535 | * 536 | * Returns: 537 | * MOSQ_ERR_SUCCESS - on success. 538 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 539 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 540 | * contains the error code, even on Windows. 541 | * Use strerror_r() where available or FormatMessage() on 542 | * Windows. 543 | * 544 | * See Also: 545 | * , , 546 | *} 547 | function mosquitto_reconnect(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 548 | 549 | {* 550 | * Function: mosquitto_reconnect_async 551 | * 552 | * Reconnect to a broker. Non blocking version of . 553 | * 554 | * This function provides an easy way of reconnecting to a broker after a 555 | * connection has been lost. It uses the values that were provided in the 556 | * or calls. It must not be 557 | * called before . 558 | * 559 | * Parameters: 560 | * mosq - a valid mosquitto instance. 561 | * 562 | * Returns: 563 | * MOSQ_ERR_SUCCESS - on success. 564 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 565 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 566 | * 567 | * Returns: 568 | * MOSQ_ERR_SUCCESS - on success. 569 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 570 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 571 | * contains the error code, even on Windows. 572 | * Use strerror_r() where available or FormatMessage() on 573 | * Windows. 574 | * 575 | * See Also: 576 | * , 577 | *} 578 | function mosquitto_reconnect_async(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 579 | 580 | {* 581 | * Function: mosquitto_disconnect 582 | * 583 | * Disconnect from the broker. 584 | * 585 | * Parameters: 586 | * mosq - a valid mosquitto instance. 587 | * 588 | * Returns: 589 | * MOSQ_ERR_SUCCESS - on success. 590 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 591 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 592 | *} 593 | function mosquitto_disconnect(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 594 | 595 | {* 596 | * Function: mosquitto_publish 597 | * 598 | * Publish a message on a given topic. 599 | * 600 | * Parameters: 601 | * mosq - a valid mosquitto instance. 602 | * mid - pointer to an int. If not NULL, the function will set this 603 | * to the message id of this particular message. This can be then 604 | * used with the publish callback to determine when the message 605 | * has been sent. 606 | * Note that although the MQTT protocol doesn't use message ids 607 | * for messages with QoS=0, libmosquitto assigns them message ids 608 | * so they can be tracked with this parameter. 609 | * topic - null terminated string of the topic to publish to. 610 | * payloadlen - the size of the payload (bytes). Valid values are between 0 and 611 | * 268,435,455. 612 | * payload - pointer to the data to send. If payloadlen > 0 this must be a 613 | * valid memory location. 614 | * qos - integer value 0, 1 or 2 indicating the Quality of Service to be 615 | * used for the message. 616 | * retain - set to true to make the message retained. 617 | * 618 | * Returns: 619 | * MOSQ_ERR_SUCCESS - on success. 620 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 621 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 622 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 623 | * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 624 | * broker. 625 | * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large. 626 | * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 627 | * See Also: 628 | * 629 | *} 630 | function mosquitto_publish(mosq: Pmosquitto; mid: pcint; const topic: pchar; payloadlen: cint; const payload: pointer; qos: cint; retain: cbool): cint; cdecl; external libmosq_NAME; 631 | 632 | {* 633 | * Function: mosquitto_subscribe 634 | * 635 | * Subscribe to a topic. 636 | * 637 | * Parameters: 638 | * mosq - a valid mosquitto instance. 639 | * mid - a pointer to an int. If not NULL, the function will set this to 640 | * the message id of this particular message. This can be then used 641 | * with the subscribe callback to determine when the message has been 642 | * sent. 643 | * sub - the subscription pattern. 644 | * qos - the requested Quality of Service for this subscription. 645 | * 646 | * Returns: 647 | * MOSQ_ERR_SUCCESS - on success. 648 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 649 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 650 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 651 | * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 652 | *} 653 | function mosquitto_subscribe(mosq: Pmosquitto; mid: pcint; const sub: pchar; qos: cint): cint; cdecl; external libmosq_NAME; 654 | 655 | {* 656 | * Function: mosquitto_unsubscribe 657 | * 658 | * Unsubscribe from a topic. 659 | * 660 | * Parameters: 661 | * mosq - a valid mosquitto instance. 662 | * mid - a pointer to an int. If not NULL, the function will set this to 663 | * the message id of this particular message. This can be then used 664 | * with the unsubscribe callback to determine when the message has been 665 | * sent. 666 | * sub - the unsubscription pattern. 667 | * 668 | * Returns: 669 | * MOSQ_ERR_SUCCESS - on success. 670 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 671 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 672 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 673 | * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 674 | *} 675 | function mosquitto_unsubscribe(mosq: Pmosquitto; mid: pcint; const sub: pchar): cint; cdecl; external libmosq_NAME; 676 | 677 | {* 678 | * Function: mosquitto_message_copy 679 | * 680 | * Copy the contents of a mosquitto message to another message. 681 | * Useful for preserving a message received in the on_message() callback. 682 | * 683 | * Parameters: 684 | * dst - a pointer to a valid mosquitto_message struct to copy to. 685 | * src - a pointer to a valid mosquitto_message struct to copy from. 686 | * 687 | * Returns: 688 | * MOSQ_ERR_SUCCESS - on success. 689 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 690 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 691 | * 692 | * See Also: 693 | * 694 | *} 695 | function mosquitto_message_copy(dst: Pmosquitto_message; const src: Pmosquitto_message): cint; cdecl; external libmosq_NAME; 696 | 697 | {* 698 | * Function: mosquitto_message_free 699 | * 700 | * Completely free a mosquitto_message struct. 701 | * 702 | * Parameters: 703 | * message - pointer to a mosquitto_message pointer to free. 704 | * 705 | * See Also: 706 | * , 707 | *} 708 | procedure mosquitto_message_free(message: PPmosquitto_message); cdecl; external libmosq_NAME; 709 | 710 | {* 711 | * Function: mosquitto_message_free_contents 712 | * 713 | * Free a mosquitto_message struct contents, leaving the struct unaffected. 714 | * 715 | * Parameters: 716 | * message - pointer to a mosquitto_message struct to free its contents. 717 | * 718 | * See Also: 719 | * , 720 | *} 721 | procedure mosquitto_message_free_contents(mosquitto_message: Pmosquitto_message); cdecl; external libmosq_NAME; 722 | 723 | {* 724 | * Function: mosquitto_loop 725 | * 726 | * The main network loop for the client. You must call this frequently in order 727 | * to keep communications between the client and broker working. If incoming 728 | * data is present it will then be processed. Outgoing commands, from e.g. 729 | * , are normally sent immediately that their function is 730 | * called, but this is not always possible. will also attempt 731 | * to send any remaining outgoing messages, which also includes commands that 732 | * are part of the flow for messages with QoS>0. 733 | * 734 | * An alternative approach is to use to run the client 735 | * loop in its own thread. 736 | * 737 | * This calls select() to monitor the client network socket. If you want to 738 | * integrate mosquitto client operation with your own select() call, use 739 | * , , and 740 | * . 741 | * 742 | * Threads: 743 | * 744 | * Parameters: 745 | * mosq - a valid mosquitto instance. 746 | * timeout - Maximum number of milliseconds to wait for network activity 747 | * in the select() call before timing out. Set to 0 for instant 748 | * return. Set negative to use the default of 1000ms. 749 | * max_packets - this parameter is currently unused and should be set to 1 for 750 | * future compatibility. 751 | * 752 | * Returns: 753 | * MOSQ_ERR_SUCCESS - on success. 754 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 755 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 756 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 757 | * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 758 | * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 759 | * broker. 760 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 761 | * contains the error code, even on Windows. 762 | * Use strerror_r() where available or FormatMessage() on 763 | * Windows. 764 | * See Also: 765 | * , , 766 | *} 767 | function mosquitto_loop(mosq: Pmosquitto; timeout: cint; max_packets: cint): cint; cdecl; external libmosq_NAME; 768 | 769 | {* 770 | * Function: mosquitto_loop_forever 771 | * 772 | * This function call loop() for you in an infinite blocking loop. It is useful 773 | * for the case where you only want to run the MQTT client loop in your 774 | * program. 775 | * 776 | * It handles reconnecting in case server connection is lost. If you call 777 | * mosquitto_disconnect() in a callback it will return. 778 | * 779 | * Parameters: 780 | * mosq - a valid mosquitto instance. 781 | * timeout - Maximum number of milliseconds to wait for network activity 782 | * in the select() call before timing out. Set to 0 for instant 783 | * return. Set negative to use the default of 1000ms. 784 | * max_packets - this parameter is currently unused and should be set to 1 for 785 | * future compatibility. 786 | * 787 | * Returns: 788 | * MOSQ_ERR_SUCCESS - on success. 789 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 790 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 791 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 792 | * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 793 | * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 794 | * broker. 795 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 796 | * contains the error code, even on Windows. 797 | * Use strerror_r() where available or FormatMessage() on 798 | * Windows. 799 | * 800 | * See Also: 801 | * , 802 | *} 803 | function mosquitto_loop_forever(mosq: Pmosquitto; timeout: cint; max_packets: cint): cint; cdecl; external libmosq_NAME; 804 | 805 | {* 806 | * Function: mosquitto_loop_start 807 | * 808 | * This is part of the threaded client interface. Call this once to start a new 809 | * thread to process network traffic. This provides an alternative to 810 | * repeatedly calling yourself. 811 | * 812 | * Parameters: 813 | * mosq - a valid mosquitto instance. 814 | * 815 | * Returns: 816 | * MOSQ_ERR_SUCCESS - on success. 817 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 818 | * MOSQ_ERR_NOT_SUPPORTED - if thread support is not available. 819 | * 820 | * See Also: 821 | * , , , 822 | *} 823 | function mosquitto_loop_start(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 824 | 825 | {* 826 | * Function: mosquitto_loop_stop 827 | * 828 | * This is part of the threaded client interface. Call this once to stop the 829 | * network thread previously created with . This call 830 | * will block until the network thread finishes. For the network thread to end, 831 | * you must have previously called or have set the force 832 | * parameter to true. 833 | * 834 | * Parameters: 835 | * mosq - a valid mosquitto instance. 836 | * force - set to true to force thread cancellation. If false, 837 | * must have already been called. 838 | * 839 | * Returns: 840 | * MOSQ_ERR_SUCCESS - on success. 841 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 842 | * MOSQ_ERR_NOT_SUPPORTED - if thread support is not available. 843 | * 844 | * See Also: 845 | * , 846 | *} 847 | function mosquitto_loop_stop(mosq: Pmosquitto; force: cbool): cint; cdecl external libmosq_NAME; 848 | 849 | {* 850 | * Function: mosquitto_socket 851 | * 852 | * Return the socket handle for a mosquitto instance. Useful if you want to 853 | * include a mosquitto client in your own select() calls. 854 | * 855 | * Parameters: 856 | * mosq - a valid mosquitto instance. 857 | * 858 | * Returns: 859 | * The socket for the mosquitto client or -1 on failure. 860 | *} 861 | function mosquitto_socket(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 862 | 863 | {* 864 | * Function: mosquitto_loop_read 865 | * 866 | * Carry out network read operations. 867 | * This should only be used if you are not using mosquitto_loop() and are 868 | * monitoring the client network socket for activity yourself. 869 | * 870 | * Parameters: 871 | * mosq - a valid mosquitto instance. 872 | * max_packets - this parameter is currently unused and should be set to 1 for 873 | * future compatibility. 874 | * 875 | * Returns: 876 | * MOSQ_ERR_SUCCESS - on success. 877 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 878 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 879 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 880 | * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 881 | * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 882 | * broker. 883 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 884 | * contains the error code, even on Windows. 885 | * Use strerror_r() where available or FormatMessage() on 886 | * Windows. 887 | * 888 | * See Also: 889 | * , , 890 | *} 891 | function mosquitto_loop_read(mosq: Pmosquitto; max_packets: cint): cint; cdecl; external libmosq_NAME; 892 | 893 | {* 894 | * Function: mosquitto_loop_write 895 | * 896 | * Carry out network write operations. 897 | * This should only be used if you are not using mosquitto_loop() and are 898 | * monitoring the client network socket for activity yourself. 899 | * 900 | * Parameters: 901 | * mosq - a valid mosquitto instance. 902 | * max_packets - this parameter is currently unused and should be set to 1 for 903 | * future compatibility. 904 | * 905 | * Returns: 906 | * MOSQ_ERR_SUCCESS - on success. 907 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 908 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 909 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 910 | * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 911 | * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 912 | * broker. 913 | * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 914 | * contains the error code, even on Windows. 915 | * Use strerror_r() where available or FormatMessage() on 916 | * Windows. 917 | * 918 | * See Also: 919 | * , , , 920 | *} 921 | function mosquitto_loop_write(mosq: Pmosquitto; max_packets: cint): cint; cdecl; external libmosq_NAME; 922 | 923 | {* 924 | * Function: mosquitto_loop_misc 925 | * 926 | * Carry out miscellaneous operations required as part of the network loop. 927 | * This should only be used if you are not using mosquitto_loop() and are 928 | * monitoring the client network socket for activity yourself. 929 | * 930 | * This function deals with handling PINGs and checking whether messages need 931 | * to be retried, so should be called fairly frequently. 932 | * 933 | * Parameters: 934 | * mosq - a valid mosquitto instance. 935 | * 936 | * Returns: 937 | * MOSQ_ERR_SUCCESS - on success. 938 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 939 | * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 940 | * 941 | * See Also: 942 | * , , 943 | *} 944 | function mosquitto_loop_misc(mosq: Pmosquitto): cint; cdecl; external libmosq_NAME; 945 | 946 | {* 947 | * Function: mosquitto_want_write 948 | * 949 | * Returns true if there is data ready to be written on the socket. 950 | * 951 | * Parameters: 952 | * mosq - a valid mosquitto instance. 953 | * 954 | * See Also: 955 | * , , 956 | *} 957 | function mosquitto_want_write(mosq: Pmosquitto): cbool; cdecl; external libmosq_NAME; 958 | 959 | {* 960 | * Function: mosquitto_threaded_set 961 | * 962 | * Used to tell the library that your application is using threads, but not 963 | * using . The library operates slightly differently when 964 | * not in threaded mode in order to simplify its operation. If you are managing 965 | * your own threads and do not use this function you will experience crashes 966 | * due to race conditions. 967 | * 968 | * When using , this is set automatically. 969 | * 970 | * Parameters: 971 | * mosq - a valid mosquitto instance. 972 | * threaded - true if your application is using threads, false otherwise. 973 | *} 974 | function mosquitto_threaded_set(mosq: Pmosquitto; threaded: cbool): cint; cdecl; external libmosq_NAME; 975 | 976 | {* 977 | * Function: mosquitto_opts_set 978 | * 979 | * Used to set options for the client. 980 | * 981 | * Parameters: 982 | * mosq - a valid mosquitto instance. 983 | * option - the option to set. 984 | * value - the option specific value. 985 | * 986 | * Options: 987 | * MOSQ_OPT_PROTOCOL_VERSION 988 | * Value must be an int, set to either MQTT_PROTOCOL_V31 or 989 | * MQTT_PROTOCOL_V311. Must be set before the client connects. 990 | * Defaults to MQTT_PROTOCOL_V31. 991 | * 992 | * MOSQ_OPT_SSL_CTX 993 | * Pass an openssl SSL_CTX to be used when creating TLS connections 994 | * rather than libmosquitto creating its own. This must be called 995 | * before connecting to have any effect. If you use this option, the 996 | * onus is on you to ensure that you are using secure settings. 997 | * Setting to NULL means that libmosquitto will use its own SSL_CTX 998 | * if TLS is to be used. 999 | * This option is only available for openssl 1.1.0 and higher. 1000 | * 1001 | * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS 1002 | * Value must be an int set to 1 or 0. If set to 1, then the user 1003 | * specified SSL_CTX passed in using MOSQ_OPT_SSL_CTX will have the 1004 | * default options applied to it. This means that you only need to 1005 | * change the values that are relevant to you. If you use this 1006 | * option then you must configure the TLS options as normal, i.e. 1007 | * you should use to configure the cafile/capath 1008 | * as a minimum. 1009 | * This option is only available for openssl 1.1.0 and higher. 1010 | *} 1011 | function mosquitto_opts_set(mosq: Pmosquitto; option: cint; value: pointer): cint; cdecl; external libmosq_NAME; 1012 | 1013 | 1014 | {* 1015 | * Function: mosquitto_tls_set 1016 | * 1017 | * Configure the client for certificate based SSL/TLS support. Must be called 1018 | * before . 1019 | * 1020 | * Cannot be used in conjunction with . 1021 | * 1022 | * Define the Certificate Authority certificates to be trusted (ie. the server 1023 | * certificate must be signed with one of these certificates) using cafile. 1024 | * 1025 | * If the server you are connecting to requires clients to provide a 1026 | * certificate, define certfile and keyfile with your client certificate and 1027 | * private key. If your private key is encrypted, provide a password callback 1028 | * function or you will have to enter the password at the command line. 1029 | * 1030 | * Parameters: 1031 | * mosq - a valid mosquitto instance. 1032 | * cafile - path to a file containing the PEM encoded trusted CA 1033 | * certificate files. Either cafile or capath must not be NULL. 1034 | * capath - path to a directory containing the PEM encoded trusted CA 1035 | * certificate files. See mosquitto.conf for more details on 1036 | * configuring this directory. Either cafile or capath must not 1037 | * be NULL. 1038 | * certfile - path to a file containing the PEM encoded certificate file 1039 | * for this client. If NULL, keyfile must also be NULL and no 1040 | * client certificate will be used. 1041 | * keyfile - path to a file containing the PEM encoded private key for 1042 | * this client. If NULL, certfile must also be NULL and no 1043 | * client certificate will be used. 1044 | * pw_callback - if keyfile is encrypted, set pw_callback to allow your client 1045 | * to pass the correct password for decryption. If set to NULL, 1046 | * the password must be entered on the command line. 1047 | * Your callback must write the password into "buf", which is 1048 | * "size" bytes long. The return value must be the length of the 1049 | * password. "userdata" will be set to the calling mosquitto 1050 | * instance. The mosquitto userdata member variable can be 1051 | * retrieved using . 1052 | * 1053 | * Returns: 1054 | * MOSQ_ERR_SUCCESS - on success. 1055 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1056 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1057 | * 1058 | * See Also: 1059 | * , , 1060 | * , 1061 | *} 1062 | type 1063 | Tpw_callback = function(buf: pchar; size: cint; rwflag: cint; userdata: pointer): cint; cdecl; 1064 | 1065 | function mosquitto_tls_set(mosq: Pmosquitto; 1066 | const cafile: pchar; const capath: pchar; 1067 | const certfile: pchar; const keyfile: pchar; 1068 | pw_callback: Tpw_callback): cint; cdecl; external libmosq_NAME; 1069 | 1070 | {* 1071 | * Function: mosquitto_tls_insecure_set 1072 | * 1073 | * Configure verification of the server hostname in the server certificate. If 1074 | * value is set to true, it is impossible to guarantee that the host you are 1075 | * connecting to is not impersonating your server. This can be useful in 1076 | * initial server testing, but makes it possible for a malicious third party to 1077 | * impersonate your server through DNS spoofing, for example. 1078 | * Do not use this function in a real system. Setting value to true makes the 1079 | * connection encryption pointless. 1080 | * Must be called before . 1081 | * 1082 | * Parameters: 1083 | * mosq - a valid mosquitto instance. 1084 | * value - if set to false, the default, certificate hostname checking is 1085 | * performed. If set to true, no hostname checking is performed and 1086 | * the connection is insecure. 1087 | * 1088 | * Returns: 1089 | * MOSQ_ERR_SUCCESS - on success. 1090 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1091 | * 1092 | * See Also: 1093 | * 1094 | *} 1095 | function mosquitto_tls_insecure_set(mosq: Pmosquitto; value: cbool): cint; cdecl; external libmosq_NAME; 1096 | 1097 | {* 1098 | * Function: mosquitto_tls_opts_set 1099 | * 1100 | * Set advanced SSL/TLS options. Must be called before . 1101 | * 1102 | * Parameters: 1103 | * mosq - a valid mosquitto instance. 1104 | * cert_reqs - an integer defining the verification requirements the client 1105 | * will impose on the server. This can be one of: 1106 | * * SSL_VERIFY_NONE (0): the server will not be verified in any way. 1107 | * * SSL_VERIFY_PEER (1): the server certificate will be verified 1108 | * and the connection aborted if the verification fails. 1109 | * The default and recommended value is SSL_VERIFY_PEER. Using 1110 | * SSL_VERIFY_NONE provides no security. 1111 | * tls_version - the version of the SSL/TLS protocol to use as a string. If NULL, 1112 | * the default value is used. The default value and the 1113 | * available values depend on the version of openssl that the 1114 | * library was compiled against. For openssl >= 1.0.1, the 1115 | * available options are tlsv1.2, tlsv1.1 and tlsv1, with tlv1.2 1116 | * as the default. For openssl < 1.0.1, only tlsv1 is available. 1117 | * ciphers - a string describing the ciphers available for use. See the 1118 | * "openssl ciphers" tool for more information. If NULL, the 1119 | * default ciphers will be used. 1120 | * 1121 | * Returns: 1122 | * MOSQ_ERR_SUCCESS - on success. 1123 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1124 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1125 | * 1126 | * See Also: 1127 | * 1128 | *} 1129 | function mosquitto_tls_opts_set(mosq: Pmosquitto; cert_reqs: cint; const tls_version: pchar; const ciphers: pchar): cint; cdecl; external libmosq_NAME; 1130 | 1131 | {* 1132 | * Function: mosquitto_tls_psk_set 1133 | * 1134 | * Configure the client for pre-shared-key based TLS support. Must be called 1135 | * before . 1136 | * 1137 | * Cannot be used in conjunction with . 1138 | * 1139 | * Parameters: 1140 | * mosq - a valid mosquitto instance. 1141 | * psk - the pre-shared-key in hex format with no leading "0x". 1142 | * identity - the identity of this client. May be used as the username 1143 | * depending on the server settings. 1144 | * ciphers - a string describing the PSK ciphers available for use. See the 1145 | * "openssl ciphers" tool for more information. If NULL, the 1146 | * default ciphers will be used. 1147 | * 1148 | * Returns: 1149 | * MOSQ_ERR_SUCCESS - on success. 1150 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1151 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1152 | * 1153 | * See Also: 1154 | * 1155 | *} 1156 | function mosquitto_tls_psk_set(mosq: Pmosquitto; const psk: pchar; const identity: pchar; const ciphers: pchar): cint; cdecl; external libmosq_NAME; 1157 | 1158 | {* 1159 | * Function: mosquitto_connect_callback_set 1160 | * 1161 | * Set the connect callback. This is called when the broker sends a CONNACK 1162 | * message in response to a connection. 1163 | * 1164 | * Parameters: 1165 | * mosq - a valid mosquitto instance. 1166 | * on_connect - a callback function in the following form: 1167 | * void callback(mosq: Pmosquitto, void *obj, int rc) 1168 | * 1169 | * Callback Parameters: 1170 | * mosq - the mosquitto instance making the callback. 1171 | * obj - the user data provided in 1172 | * rc - the return code of the connection response, one of: 1173 | * 1174 | * * 0 - success 1175 | * * 1 - connection refused (unacceptable protocol version) 1176 | * * 2 - connection refused (identifier rejected) 1177 | * * 3 - connection refused (broker unavailable) 1178 | * * 4-255 - reserved for future use 1179 | *} 1180 | type 1181 | Ton_connect_callback = procedure(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; 1182 | 1183 | procedure mosquitto_connect_callback_set(mosq: Pmosquitto; on_connect: Ton_connect_callback); cdecl; external libmosq_NAME; 1184 | 1185 | 1186 | {* 1187 | * Function: mosquitto_connect_with_flags_callback_set 1188 | * 1189 | * Set the connect callback. This is called when the broker sends a CONNACK 1190 | * message in response to a connection. 1191 | * 1192 | * Parameters: 1193 | * mosq - a valid mosquitto instance. 1194 | * on_connect - a callback function in the following form: 1195 | * void callback(struct mosquitto *mosq, void *obj, int rc) 1196 | * 1197 | * Callback Parameters: 1198 | * mosq - the mosquitto instance making the callback. 1199 | * obj - the user data provided in 1200 | * rc - the return code of the connection response, one of: 1201 | * flags - the connect flags. 1202 | * 1203 | * * 0 - success 1204 | * * 1 - connection refused (unacceptable protocol version) 1205 | * * 2 - connection refused (identifier rejected) 1206 | * * 3 - connection refused (broker unavailable) 1207 | * * 4-255 - reserved for future use 1208 | *} 1209 | type 1210 | Ton_connect_with_flags_callback = procedure(mosq: Pmosquitto; obj: pointer; _unknown1: cint; _unknown2: cint); cdecl; 1211 | 1212 | procedure mosquitto_connect_with_flags_callback_set(mosq: Pmosquitto; on_connect: Ton_connect_with_flags_callback); cdecl; external libmosq_NAME; 1213 | 1214 | {* 1215 | * Function: mosquitto_disconnect_callback_set 1216 | * 1217 | * Set the disconnect callback. This is called when the broker has received the 1218 | * DISCONNECT command and has disconnected the client. 1219 | * 1220 | * Parameters: 1221 | * mosq - a valid mosquitto instance. 1222 | * on_disconnect - a callback function in the following form: 1223 | * void callback(mosq: Pmosquitto, void *obj) 1224 | * 1225 | * Callback Parameters: 1226 | * mosq - the mosquitto instance making the callback. 1227 | * obj - the user data provided in 1228 | * rc - integer value indicating the reason for the disconnect. A value of 0 1229 | * means the client has called . Any other value 1230 | * indicates that the disconnect is unexpected. 1231 | *} 1232 | type 1233 | Ton_disconnect_callback = procedure(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; 1234 | 1235 | procedure mosquitto_disconnect_callback_set(mosq: Pmosquitto; on_disconnect: Ton_disconnect_callback); cdecl; external libmosq_NAME; 1236 | 1237 | {* 1238 | * Function: mosquitto_publish_callback_set 1239 | * 1240 | * Set the publish callback. This is called when a message initiated with 1241 | * has been sent to the broker successfully. 1242 | * 1243 | * Parameters: 1244 | * mosq - a valid mosquitto instance. 1245 | * on_publish - a callback function in the following form: 1246 | * void callback(mosq: Pmosquitto, void *obj, int mid) 1247 | * 1248 | * Callback Parameters: 1249 | * mosq - the mosquitto instance making the callback. 1250 | * obj - the user data provided in 1251 | * mid - the message id of the sent message. 1252 | *} 1253 | type 1254 | Ton_publish_callback = procedure(mosq: Pmosquitto; obj: pointer; rc: cint); cdecl; 1255 | 1256 | procedure mosquitto_publish_callback_set(mosq: Pmosquitto; on_publish: Ton_publish_callback); cdecl; external libmosq_NAME; 1257 | 1258 | {* 1259 | * Function: mosquitto_message_callback_set 1260 | * 1261 | * Set the message callback. This is called when a message is received from the 1262 | * broker. 1263 | * 1264 | * Parameters: 1265 | * mosq - a valid mosquitto instance. 1266 | * on_message - a callback function in the following form: 1267 | * void callback(mosq: Pmosquitto, void *obj, const struct mosquitto_message *message) 1268 | * 1269 | * Callback Parameters: 1270 | * mosq - the mosquitto instance making the callback. 1271 | * obj - the user data provided in 1272 | * message - the message data. This variable and associated memory will be 1273 | * freed by the library after the callback completes. The client 1274 | * should make copies of any of the data it requires. 1275 | * 1276 | * See Also: 1277 | * 1278 | *} 1279 | type 1280 | Ton_message_callback = procedure(mosq: Pmosquitto; obj: pointer; const message: Pmosquitto_message); cdecl; 1281 | 1282 | procedure mosquitto_message_callback_set(mosq: Pmosquitto; on_message: Ton_message_callback); cdecl; external libmosq_NAME; 1283 | 1284 | {* 1285 | * Function: mosquitto_subscribe_callback_set 1286 | * 1287 | * Set the subscribe callback. This is called when the broker responds to a 1288 | * subscription request. 1289 | * 1290 | * Parameters: 1291 | * mosq - a valid mosquitto instance. 1292 | * on_subscribe - a callback function in the following form: 1293 | * void callback(mosq: Pmosquitto, void *obj, int mid, int qos_count, const int *granted_qos) 1294 | * 1295 | * Callback Parameters: 1296 | * mosq - the mosquitto instance making the callback. 1297 | * obj - the user data provided in 1298 | * mid - the message id of the subscribe message. 1299 | * qos_count - the number of granted subscriptions (size of granted_qos). 1300 | * granted_qos - an array of integers indicating the granted QoS for each of 1301 | * the subscriptions. 1302 | *} 1303 | type 1304 | Ton_subscribe_callback = procedure(mosq: Pmosquitto; obj: pointer; mid: cint; qos_count: cint; const granted_qos: pcint); cdecl; 1305 | 1306 | procedure mosquitto_subscribe_callback_set(mosq: Pmosquitto; on_subscribe: Ton_subscribe_callback); cdecl; external libmosq_NAME; 1307 | 1308 | {* 1309 | * Function: mosquitto_unsubscribe_callback_set 1310 | * 1311 | * Set the unsubscribe callback. This is called when the broker responds to a 1312 | * unsubscription request. 1313 | * 1314 | * Parameters: 1315 | * mosq - a valid mosquitto instance. 1316 | * on_unsubscribe - a callback function in the following form: 1317 | * void callback(mosq: Pmosquitto, void *obj, int mid) 1318 | * 1319 | * Callback Parameters: 1320 | * mosq - the mosquitto instance making the callback. 1321 | * obj - the user data provided in 1322 | * mid - the message id of the unsubscribe message. 1323 | *} 1324 | type 1325 | Ton_unsubscribe_callback = procedure(mosq: Pmosquitto; obj: pointer; mid: cint); cdecl; 1326 | 1327 | procedure mosquitto_unsubscribe_callback_set(mosq: Pmosquitto; on_unsubscribe: Ton_unsubscribe_callback); cdecl; external libmosq_NAME; 1328 | 1329 | {* 1330 | * Function: mosquitto_log_callback_set 1331 | * 1332 | * Set the logging callback. This should be used if you want event logging 1333 | * information from the client library. 1334 | * 1335 | * mosq - a valid mosquitto instance. 1336 | * on_log - a callback function in the following form: 1337 | * void callback(mosq: Pmosquitto, void *obj, int level, const char *str) 1338 | * 1339 | * Callback Parameters: 1340 | * mosq - the mosquitto instance making the callback. 1341 | * obj - the user data provided in 1342 | * level - the log message level from the values: 1343 | * MOSQ_LOG_INFO 1344 | * MOSQ_LOG_NOTICE 1345 | * MOSQ_LOG_WARNING 1346 | * MOSQ_LOG_ERR 1347 | * MOSQ_LOG_DEBUG 1348 | * str - the message string. 1349 | *} 1350 | type 1351 | Ton_log_callback = procedure(mosq: Pmosquitto; obj: pointer; level: cint; const str: pchar); cdecl; 1352 | 1353 | procedure mosquitto_log_callback_set(mosq: Pmosquitto; on_log: Ton_log_callback); cdecl; external libmosq_NAME; 1354 | 1355 | {* 1356 | * Function: mosquitto_reconnect_delay_set 1357 | * 1358 | * Control the behaviour of the client when it has unexpectedly disconnected in 1359 | * or after . The default 1360 | * behaviour if this function is not used is to repeatedly attempt to reconnect 1361 | * with a delay of 1 second until the connection succeeds. 1362 | * 1363 | * Use reconnect_delay parameter to change the delay between successive 1364 | * reconnection attempts. You may also enable exponential backoff of the time 1365 | * between reconnections by setting reconnect_exponential_backoff to true and 1366 | * set an upper bound on the delay with reconnect_delay_max. 1367 | * 1368 | * Example 1: 1369 | * delay=2, delay_max=10, exponential_backoff=False 1370 | * Delays would be: 2, 4, 6, 8, 10, 10, ... 1371 | * 1372 | * Example 2: 1373 | * delay=3, delay_max=30, exponential_backoff=True 1374 | * Delays would be: 3, 6, 12, 24, 30, 30, ... 1375 | * 1376 | * Parameters: 1377 | * mosq - a valid mosquitto instance. 1378 | * reconnect_delay - the number of seconds to wait between 1379 | * reconnects. 1380 | * reconnect_delay_max - the maximum number of seconds to wait 1381 | * between reconnects. 1382 | * reconnect_exponential_backoff - use exponential backoff between 1383 | * reconnect attempts. Set to true to enable 1384 | * exponential backoff. 1385 | * 1386 | * Returns: 1387 | * MOSQ_ERR_SUCCESS - on success. 1388 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1389 | *} 1390 | function mosquitto_reconnect_delay_set(mosq: Pmosquitto; reconnect_delay: cuint; reconnect_delay_max: cuint; reconnect_exponential_backoff: cbool): cint; cdecl; external libmosq_NAME; 1391 | 1392 | {* 1393 | * Function: mosquitto_max_inflight_messages_set 1394 | * 1395 | * Set the number of QoS 1 and 2 messages that can be "in flight" at one time. 1396 | * An in flight message is part way through its delivery flow. Attempts to send 1397 | * further messages with will result in the messages being 1398 | * queued until the number of in flight messages reduces. 1399 | * 1400 | * A higher number here results in greater message throughput, but if set 1401 | * higher than the maximum in flight messages on the broker may lead to 1402 | * delays in the messages being acknowledged. 1403 | * 1404 | * Set to 0 for no maximum. 1405 | * 1406 | * Parameters: 1407 | * mosq - a valid mosquitto instance. 1408 | * max_inflight_messages - the maximum number of inflight messages. Defaults 1409 | * to 20. 1410 | * 1411 | * Returns: 1412 | * MOSQ_ERR_SUCCESS - on success. 1413 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1414 | *} 1415 | function mosquitto_max_inflight_messages_set(mosq: Pmosquitto; max_inflight_messages: cuint): cint; cdecl; external libmosq_NAME; 1416 | 1417 | {* 1418 | * Function: mosquitto_message_retry_set 1419 | * 1420 | * This function now has no effect. 1421 | *} 1422 | procedure mosquitto_message_retry_set(mosq: Pmosquitto; message_retry: cuint); cdecl; external libmosq_NAME; 1423 | 1424 | {* 1425 | * Function: mosquitto_user_data_set 1426 | * 1427 | * When is called, the pointer given as the "obj" parameter 1428 | * will be passed to the callbacks as user data. The 1429 | * function allows this obj parameter to be updated at any time. This function 1430 | * will not modify the memory pointed to by the current user data pointer. If 1431 | * it is dynamically allocated memory you must free it yourself. 1432 | * 1433 | * Parameters: 1434 | * mosq - a valid mosquitto instance. 1435 | * obj - A user pointer that will be passed as an argument to any callbacks 1436 | * that are specified. 1437 | *} 1438 | procedure mosquitto_user_data_set(mosq: Pmosquitto; obj: pointer); cdecl; external libmosq_NAME; 1439 | 1440 | {* ============================================================================= 1441 | * 1442 | * Section: SOCKS5 proxy functions 1443 | * 1444 | * ============================================================================= 1445 | *} 1446 | 1447 | {* 1448 | * Function: mosquitto_socks5_set 1449 | * 1450 | * Configure the client to use a SOCKS5 proxy when connecting. Must be called 1451 | * before connecting. "None" and "username/password" authentication is 1452 | * supported. 1453 | * 1454 | * Parameters: 1455 | * mosq - a valid mosquitto instance. 1456 | * host - the SOCKS5 proxy host to connect to. 1457 | * port - the SOCKS5 proxy port to use. 1458 | * username - if not NULL, use this username when authenticating with the proxy. 1459 | * password - if not NULL and username is not NULL, use this password when 1460 | * authenticating with the proxy. 1461 | *} 1462 | function mosquitto_socks5_set(mosq: Pmosquitto; const host: pchar; port: cint; const username: pchar; const password: pchar): cint; cdecl; external libmosq_NAME; 1463 | 1464 | {* ============================================================================= 1465 | * 1466 | * Section: Utility functions 1467 | * 1468 | * ============================================================================= 1469 | *} 1470 | 1471 | {* 1472 | * Function: mosquitto_strerror 1473 | * 1474 | * Call to obtain a const string description of a mosquitto error number. 1475 | * 1476 | * Parameters: 1477 | * mosq_errno - a mosquitto error number. 1478 | * 1479 | * Returns: 1480 | * A constant string describing the error. 1481 | *} 1482 | function mosquitto_strerror(mosq_errno: cint): pchar; cdecl; external libmosq_NAME; 1483 | 1484 | {* 1485 | * Function: mosquitto_connack_string 1486 | * 1487 | * Call to obtain a const string description of an MQTT connection result. 1488 | * 1489 | * Parameters: 1490 | * connack_code - an MQTT connection result. 1491 | * 1492 | * Returns: 1493 | * A constant string describing the result. 1494 | *} 1495 | function mosquitto_connack_string(connack_code: cint): pchar; cdecl; external libmosq_NAME; 1496 | 1497 | {* 1498 | * Function: mosquitto_sub_topic_tokenise 1499 | * 1500 | * Tokenise a topic or subscription string into an array of strings 1501 | * representing the topic hierarchy. 1502 | * 1503 | * For example: 1504 | * 1505 | * subtopic: "a/deep/topic/hierarchy" 1506 | * 1507 | * Would result in: 1508 | * 1509 | * topics[0] = "a" 1510 | * topics[1] = "deep" 1511 | * topics[2] = "topic" 1512 | * topics[3] = "hierarchy" 1513 | * 1514 | * and: 1515 | * 1516 | * subtopic: "/a/deep/topic/hierarchy/" 1517 | * 1518 | * Would result in: 1519 | * 1520 | * topics[0] = NULL 1521 | * topics[1] = "a" 1522 | * topics[2] = "deep" 1523 | * topics[3] = "topic" 1524 | * topics[4] = "hierarchy" 1525 | * 1526 | * Parameters: 1527 | * subtopic - the subscription/topic to tokenise 1528 | * topics - a pointer to store the array of strings 1529 | * count - an int pointer to store the number of items in the topics array. 1530 | * 1531 | * Returns: 1532 | * MOSQ_ERR_SUCCESS - on success 1533 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1534 | * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 1535 | * 1536 | * Example: 1537 | * 1538 | * > char **topics; 1539 | * > int topic_count; 1540 | * > int i; 1541 | * > 1542 | * > mosquitto_sub_topic_tokenise("$SYS/broker/uptime", &topics, &topic_count); 1543 | * > 1544 | * > for(i=0; i printf("%d: %s\n", i, topics[i]); 1546 | * > ) 1547 | * 1548 | * See Also: 1549 | * 1550 | *} 1551 | function mosquitto_sub_topic_tokenise(const subtopic: pchar; var topics: ppchar; count: pcint): cint; cdecl; external libmosq_NAME; 1552 | 1553 | {* 1554 | * Function: mosquitto_sub_topic_tokens_free 1555 | * 1556 | * Free memory that was allocated in . 1557 | * 1558 | * Parameters: 1559 | * topics - pointer to string array. 1560 | * count - count of items in string array. 1561 | * 1562 | * Returns: 1563 | * MOSQ_ERR_SUCCESS - on success 1564 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1565 | * 1566 | * See Also: 1567 | * 1568 | *} 1569 | function mosquitto_sub_topic_tokens_free(var topics: ppchar; count: cint): cint; cdecl; external libmosq_NAME; 1570 | 1571 | {* 1572 | * Function: mosquitto_topic_matches_sub 1573 | * Function: mosquitto_topic_matches_sub2 1574 | * 1575 | * Check whether a topic matches a subscription. 1576 | * 1577 | * For example: 1578 | * 1579 | * foo/bar would match the subscription foo/# or +/bar 1580 | * non/matching would not match the subscription non/+/+ 1581 | * 1582 | * Parameters: 1583 | * sub - subscription string to check topic against. 1584 | * sublen - length in bytes of sub string 1585 | * topic - topic to check. 1586 | * topiclen - length in bytes of topic string 1587 | * result - bool pointer to hold result. Will be set to true if the topic 1588 | * matches the subscription. 1589 | * 1590 | * Returns: 1591 | * MOSQ_ERR_SUCCESS - on success 1592 | * MOSQ_ERR_INVAL - if the input parameters were invalid. 1593 | * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1594 | *} 1595 | function mosquitto_topic_matches_sub(const sub: pchar; const topic: pchar; result: pcbool): cint; cdecl; external libmosq_NAME; 1596 | function mosquitto_topic_matches_sub2(const sub: pchar; sublen: csize_t; const topic: pchar; topiclen: csize_t; result: pcbool): cint; cdecl; external libmosq_NAME; 1597 | 1598 | {* 1599 | * Function: mosquitto_pub_topic_check 1600 | * 1601 | * Check whether a topic to be used for publishing is valid. 1602 | * 1603 | * This searches for + or # in a topic and checks its length. 1604 | * 1605 | * This check is already carried out in and 1606 | * , there is no need to call it directly before them. It 1607 | * may be useful if you wish to check the validity of a topic in advance of 1608 | * making a connection for example. 1609 | * 1610 | * Parameters: 1611 | * topic - the topic to check 1612 | * topiclen - length of the topic in bytes 1613 | * 1614 | * Returns: 1615 | * MOSQ_ERR_SUCCESS - for a valid topic 1616 | * MOSQ_ERR_INVAL - if the topic contains a + or a #, or if it is too long. 1617 | * MOSQ_ERR_MALFORMED_UTF8 - if sub or topic is not valid UTF-8 1618 | * 1619 | * See Also: 1620 | * 1621 | *} 1622 | function mosquitto_pub_topic_check(const topic: pchar): cint; cdecl; external libmosq_NAME; 1623 | function mosquitto_pub_topic_check2(const topic: pchar; topiclen: csize_t): cint; cdecl; external libmosq_NAME; 1624 | 1625 | {* 1626 | * Function: mosquitto_sub_topic_check 1627 | * 1628 | * Check whether a topic to be used for subscribing is valid. 1629 | * 1630 | * This searches for + or # in a topic and checks that they aren't in invalid 1631 | * positions, such as with foo/#/bar, foo/+bar or foo/bar#, and checks its 1632 | * length. 1633 | * 1634 | * This check is already carried out in and 1635 | * , there is no need to call it directly before them. 1636 | * It may be useful if you wish to check the validity of a topic in advance of 1637 | * making a connection for example. 1638 | * 1639 | * Parameters: 1640 | * topic - the topic to check 1641 | * topiclen - the length in bytes of the topic 1642 | * 1643 | * Returns: 1644 | * MOSQ_ERR_SUCCESS - for a valid topic 1645 | * MOSQ_ERR_INVAL - if the topic contains a + or a # that is in an 1646 | * invalid position, or if it is too long. 1647 | * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8 1648 | * 1649 | * See Also: 1650 | * 1651 | *} 1652 | function mosquitto_sub_topic_check(const topic: pchar): cint; cdecl; external libmosq_NAME; 1653 | function mosquitto_sub_topic_check2(const topic: pchar; topiclen: csize_t): cint; cdecl; external libmosq_NAME; 1654 | 1655 | 1656 | type 1657 | Plibmosquitto_will = ^Tlibmosquitto_will; 1658 | Tlibmosquitto_will = record 1659 | topic: pchar; 1660 | payload: pointer; 1661 | payloadlen: cint; 1662 | qos: cint; 1663 | retain: cbool; 1664 | end; 1665 | 1666 | 1667 | type 1668 | Plibmosquitto_auth = ^Tlibmosquitto_auth; 1669 | Tlibmosquitto_auth = record 1670 | username: pchar; 1671 | password: pchar; 1672 | end; 1673 | 1674 | type 1675 | Ttls_pw_callback = function(buf: pchar; size: cint; rwflag: cint; userdata: pointer): cint; cdecl; 1676 | 1677 | type 1678 | Plibmosquitto_tls = ^Tlibmosquitto_tls; 1679 | Tlibmosquitto_tls = record 1680 | cafile: pchar; 1681 | capath: pchar; 1682 | certfile: pchar; 1683 | keyfile: pchar; 1684 | ciphers: pchar; 1685 | tls_version: pchar; 1686 | pw_callback: Ttls_pw_callback; 1687 | cert_reqs: cint; 1688 | end; 1689 | 1690 | {* 1691 | * Function: mosquitto_subscribe_simple 1692 | * 1693 | * Helper function to make subscribing to a topic and retrieving some messages 1694 | * very straightforward. 1695 | * 1696 | * This connects to a broker, subscribes to a topic, waits for msg_count 1697 | * messages to be received, then returns after disconnecting cleanly. 1698 | * 1699 | * Parameters: 1700 | * messages - pointer to a "struct mosquitto_message *". The received 1701 | * messages will be returned here. On error, this will be set to 1702 | * NULL. 1703 | * msg_count - the number of messages to retrieve. 1704 | * want_retained - if set to true, stale retained messages will be treated as 1705 | * normal messages with regards to msg_count. If set to 1706 | * false, they will be ignored. 1707 | * topic - the subscription topic to use (wildcards are allowed). 1708 | * qos - the qos to use for the subscription. 1709 | * host - the broker to connect to. 1710 | * port - the network port the broker is listening on. 1711 | * client_id - the client id to use, or NULL if a random client id should be 1712 | * generated. 1713 | * keepalive - the MQTT keepalive value. 1714 | * clean_session - the MQTT clean session flag. 1715 | * username - the username string, or NULL for no username authentication. 1716 | * password - the password string, or NULL for an empty password. 1717 | * will - a libmosquitto_will struct containing will information, or NULL for 1718 | * no will. 1719 | * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL 1720 | * for no use of TLS. 1721 | * 1722 | * 1723 | * Returns: 1724 | * MOSQ_ERR_SUCCESS - on success 1725 | * >0 - on error. 1726 | *} 1727 | function mosquitto_subscribe_simple(messages: PPmosquitto_message; 1728 | msg_count: cint; 1729 | want_retained: cbool; 1730 | const topic: pchar; 1731 | qos: cint; 1732 | const host: pchar; 1733 | port: cint; 1734 | const client_id: pchar; 1735 | keepalive: cint; 1736 | clean_session: cbool; 1737 | const username: pchar; 1738 | const password: pchar; 1739 | const will: Plibmosquitto_will; 1740 | const tls: Plibmosquitto_tls): cint; cdecl; external libmosq_NAME; 1741 | 1742 | {* 1743 | * Function: mosquitto_subscribe_callback 1744 | * 1745 | * Helper function to make subscribing to a topic and processing some messages 1746 | * very straightforward. 1747 | * 1748 | * This connects to a broker, subscribes to a topic, then passes received 1749 | * messages to a user provided callback. If the callback returns a 1, it then 1750 | * disconnects cleanly and returns. 1751 | * 1752 | * Parameters: 1753 | * callback - a callback function in the following form: 1754 | * int callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) 1755 | * Note that this is the same as the normal on_message callback, 1756 | * except that it returns an int. 1757 | * userdata - user provided pointer that will be passed to the callback. 1758 | * topic - the subscription topic to use (wildcards are allowed). 1759 | * qos - the qos to use for the subscription. 1760 | * host - the broker to connect to. 1761 | * port - the network port the broker is listening on. 1762 | * client_id - the client id to use, or NULL if a random client id should be 1763 | * generated. 1764 | * keepalive - the MQTT keepalive value. 1765 | * clean_session - the MQTT clean session flag. 1766 | * username - the username string, or NULL for no username authentication. 1767 | * password - the password string, or NULL for an empty password. 1768 | * will - a libmosquitto_will struct containing will information, or NULL for 1769 | * no will. 1770 | * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL 1771 | * for no use of TLS. 1772 | * 1773 | * 1774 | * Returns: 1775 | * MOSQ_ERR_SUCCESS - on success 1776 | * >0 - on error. 1777 | *} 1778 | function mosquitto_subscribe_callback(callback: Ton_message_callback; 1779 | userdata: pointer; 1780 | const topic: pchar; 1781 | qos: cint; 1782 | const host: pchar; 1783 | port: cint; 1784 | const client_id: pchar; 1785 | keepalive: cint; 1786 | clean_session: cbool; 1787 | const username: pchar; 1788 | const password: pchar; 1789 | const will: Plibmosquitto_will; 1790 | const tls: Plibmosquitto_tls): cint; cdecl; external libmosq_NAME; 1791 | 1792 | {* 1793 | * Function: mosquitto_validate_utf8 1794 | * 1795 | * Helper function to validate whether a UTF-8 string is valid, according to 1796 | * the UTF-8 spec and the MQTT additions. 1797 | * 1798 | * Parameters: 1799 | * str - a string to check 1800 | * len - the length of the string in bytes 1801 | * 1802 | * Returns: 1803 | * MOSQ_ERR_SUCCESS - on success 1804 | * MOSQ_ERR_INVAL - if str is NULL or len<0 or len>65536 1805 | * MOSQ_ERR_MALFORMED_UTF8 - if str is not valid UTF-8 1806 | *} 1807 | function mosquitto_validate_utf8(const str: pchar; len: cint): cint; cdecl; external libmosq_NAME; 1808 | 1809 | {* Function: mosquitto_userdata 1810 | * 1811 | * Retrieve the "userdata" variable for a mosquitto client. 1812 | * 1813 | * Parameters: 1814 | * mosq - a valid mosquitto instance. 1815 | * 1816 | * Returns: 1817 | * A pointer to the userdata member variable. 1818 | *} 1819 | function mosquitto_userdata(mosq: Pmosquitto): pointer; cdecl; external libmosq_NAME; 1820 | 1821 | 1822 | implementation 1823 | 1824 | end. 1825 | --------------------------------------------------------------------------------