// packages/iot_mqtt/examples/demo.vk // Self-contained verification suite & runnable example for `iot_mqtt`, `iot_mqtt/modbus`, and `iot_mqtt/mdns`. use [ mqtt = iot_mqtt modbus = iot_mqtt/modbus mdns = iot_mqtt/mdns ] assert-true = fn [cond bool, label str] enum [ if not cond [ println (str/join ['[FAIL] ' label] '') os/exit 1 ] println (str/join ['[PASS] ' label] '') .ok ] // --- 1. MQTT 5.0 Variable-Byte Framing, Properties & Control Packet Codec --- v_b = mqtt/encode-varint 2097152 v_d = mqtt/decode-varint v_b 0 _ = assert-true ((len v_b) == 4 and v_d.value == 2097152) 'MQTT 4-byte Variable Byte Integer boundary roundtrip' props_v5 = mqtt/MqttProps { message_expiry_sec = 300 content_type = 'application/json' response_topic = 'cmd/plc-7/ack' correlation_data = (bytes/from-str 'corr-9001') user_props = { 'site' = 'plant-eu', 'fw' = '2.4.1' } } conn_wire = mqtt/encode-connect/user/pass/will-topic/will-payload/will-qos/will-retain/props 'sensor-plc-7' 'device' (bytes/from-str 's3cr3t') 'status/plc-7' (bytes/from-str 'offline') 1 true props_v5 dec_conn = mqtt/decode-packet conn_wire pub_wire = mqtt/encode-publish/qos/retain/packet-id/props 'telemetry/plant-eu/plc-7/temp' (bytes/from-str '{"c":72.4}') 1 true 42 props_v5 dec_pub = mqtt/decode-packet pub_wire sub_wire = mqtt/encode-subscribe 101 [ mqtt/TopicSub { filter = 'telemetry/+/plc-7/#', qos = 1 } mqtt/TopicSub { filter = '$SYS/broker/uptime', qos = 0 } ] dec_sub = mqtt/decode-packet sub_wire ack_wire = mqtt/encode-puback/reason 42 0x00 dec_ack = mqtt/decode-packet ack_wire ping_ok = (mqtt/decode-packet (mqtt/encode-pingreq)).pkt_type == 12 and (mqtt/decode-packet (mqtt/encode-pingresp)).pkt_type == 13 disc_ok = (mqtt/decode-packet (mqtt/encode-disconnect/reason 0x04)).reason_code == 4 pkt_ok_1 = dec_conn.pkt_type == 1 and dec_conn.proto_level == 5 and dec_conn.client_id == 'sensor-plc-7' and dec_conn.has_will and dec_conn.will_topic == 'status/plc-7' pkt_ok_2 = dec_pub.pkt_type == 3 and dec_pub.qos == 1 and dec_pub.retain and dec_pub.packet_id == 42 and (bytes/to-str dec_pub.payload) == '{"c":72.4}' pkt_ok_3 = dec_pub.props.message_expiry_sec == 300 and dec_pub.props.content_type == 'application/json' and (map/get dec_pub.props.user_props 'site') == 'plant-eu' pkt_ok_4 = (len dec_sub.subscriptions) == 2 and dec_ack.packet_id == 42 and ping_ok and disc_ok _ = assert-true (pkt_ok_1 and pkt_ok_2 and pkt_ok_3 and pkt_ok_4) 'MQTT 5.0 CONNECT, PUBLISH (with User Properties), SUBSCRIBE, PUBACK, PING, and DISCONNECT codec' // --- 2. OASIS MQTT Topic Wildcard Matcher (`+`, `#`, and `$SYS` Rule) --- tm_1 = (mqtt/topic-matches? 'sensors/+/temp' 'sensors/kiln-1/temp') and (not (mqtt/topic-matches? 'sensors/+/temp' 'sensors/kiln-1/zone-a/temp')) tm_2 = (mqtt/topic-matches? 'sensors/kiln-1/#' 'sensors/kiln-1') and (mqtt/topic-matches? 'sensors/kiln-1/#' 'sensors/kiln-1/zone-a/temp') tm_3 = (not (mqtt/topic-matches? '#' '$SYS/broker/uptime')) and (not (mqtt/topic-matches? '+/broker/uptime' '$SYS/broker/uptime')) tm_4 = mqtt/topic-matches? '$SYS/+/uptime' '$SYS/broker/uptime' _ = assert-true (tm_1 and tm_2 and tm_3 and tm_4) 'MQTT wildcard matcher (+ single-level, # multi-level, and OASIS $SYS leading-$ isolation)' // --- 3. Embeddable `MqttBroker`: Retained Messages, QoS 1 In-Flight ACKs & Last Will --- mut broker = mqtt/new-broker cack_pub = mqtt/broker-connect mut broker dec_conn _ = mqtt/broker-publish mut broker dec_pub // retained QoS 1 message published BEFORE subscriber joins! dash_conn = mqtt/decode-packet (mqtt/encode-connect 'scada-dashboard') _ = mqtt/broker-connect mut broker dash_conn suback = mqtt/broker-subscribe mut broker 'scada-dashboard' 77 [ mqtt/TopicSub { filter = 'telemetry/plant-eu/#', qos = 1 } mqtt/TopicSub { filter = 'status/+', qos = 1 } ] // Subscriber immediately receives the retained telemetry message with retain=true and QoS 1 inflight tracking initial_msgs = mqtt/broker-drain-client mut broker 'scada-dashboard' inflight_before = mqtt/broker-inflight-count broker 'scada-dashboard' ack_cleared = mqtt/broker-puback mut broker 'scada-dashboard' initial_msgs[0].packet_id inflight_after = mqtt/broker-inflight-count broker 'scada-dashboard' // Simulate unexpected TCP drop of `sensor-plc-7` -> broker publishes Last Will ('offline') to `status/plc-7`! will_fanout = mqtt/broker-disconnect/unexpected mut broker 'sensor-plc-7' true will_msgs = mqtt/broker-drain-client mut broker 'scada-dashboard' brk_ok_1 = cack_pub.reason_code == 0 and suback.reason_codes == [1 1] and (len initial_msgs) == 1 and initial_msgs[0].retain brk_ok_2 = inflight_before == 1 and ack_cleared and inflight_after == 0 brk_ok_3 = will_fanout == 1 and (len will_msgs) == 1 and will_msgs[0].topic == 'status/plc-7' and (bytes/to-str will_msgs[0].payload) == 'offline' _ = assert-true (brk_ok_1 and brk_ok_2 and brk_ok_3) 'MqttBroker retained message replay, QoS 1 in-flight PUBACK tracking, and Last Will delivery' // --- 4. Industrial Modbus TCP MBAP + PDU & `RegisterBank` Simulator --- mut bank = modbus/new-register-bank/coils/holding/input 64 128 64 _ = modbus/bank-set-input mut bank 4 2450 // 245.0 V sensor input w_multi = modbus/encode-write-multiple-registers 10 1 20 [1500 3200 4095] r_multi = modbus/decode-response (modbus/handle-modbus-tcp mut bank w_multi) w_coil = modbus/encode-write-single-coil 11 1 5 true _ = modbus/handle-modbus-tcp mut bank w_coil r_hold = modbus/decode-response (modbus/handle-modbus-tcp mut bank (modbus/encode-read-holding-registers 12 1 20 3)) r_inp = modbus/decode-response (modbus/handle-modbus-tcp mut bank (modbus/encode-read-input-registers 13 1 4 1)) r_coil = modbus/decode-response/expected-coils (modbus/handle-modbus-tcp mut bank (modbus/encode-read-coils 14 1 4 3)) 3 r_exc = modbus/decode-response (modbus/handle-modbus-tcp mut bank (modbus/encode-read-holding-registers 15 1 200 10)) mb_ok_1 = r_multi.quantity == 3 and r_hold.registers == [1500 3200 4095] and r_inp.registers == [2450] mb_ok_2 = r_coil.coils == [false true false] and r_exc.is_exception and r_exc.exception_code == (modbus/EX-ILLEGAL-DATA-ADDR) _ = assert-true (mb_ok_1 and mb_ok_2) 'Modbus TCP (0x01/0x03/0x04/0x05/0x10 + 0x80|fc exception) against RegisterBank' // --- 5. RFC 6762 / RFC 6763 mDNS & DNS-SD Announcement + Pointer-Compressed Parsing --- q_pkt = mdns/parse-mdns-packet (mdns/build-ptr-query '_veko._tcp.local') ann_wire = mdns/build-service-announcement 'edge-gw-01' '_veko._tcp.local' 'edge-gw-01.local' '192.168.10.42' 8883 { 'txtvers' = '1' 'proto' = 'mqtt5' } ann = mdns/parse-mdns-packet ann_wire rec_ptr = ann.answers[0] rec_srv = ann.answers[1] rec_txt = ann.answers[2] rec_a = ann.answers[3] mdns_ok_1 = (len q_pkt.questions) == 1 and q_pkt.questions[0].name == '_veko._tcp.local' and q_pkt.questions[0].qtype == (mdns/TYPE-PTR) mdns_ok_2 = ann.is_response and (len ann.answers) == 4 and rec_ptr.ptr_target == 'edge-gw-01._veko._tcp.local' mdns_ok_3 = rec_srv.srv_port == 8883 and rec_srv.srv_target == 'edge-gw-01.local' and rec_srv.cache_flush mdns_ok_4 = (map/get rec_txt.txt_map 'proto') == 'mqtt5' and rec_a.ipv4 == '192.168.10.42' _ = assert-true (mdns_ok_1 and mdns_ok_2 and mdns_ok_3 and mdns_ok_4) 'mDNS / DNS-SD _veko._tcp.local PTR/SRV/TXT/A announcement and RFC 1035 pointer decompression' println 'iot_mqtt: all verification checks passed'