From c0b065c3273be92d137f9b3ca414c111738d9f86 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Tue, 17 Dec 2024 17:11:40 +0100 Subject: simulation+todo: use frame instead of packet --- TODO.md | 4 +-- simulation.scm | 102 +++++++++++++++++++++++++++++++-------------------------- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/TODO.md b/TODO.md index 6a7305e..597098b 100644 --- a/TODO.md +++ b/TODO.md @@ -4,9 +4,9 @@ something acting as a Network Server - Channel control -> IMPORTANT helps us to make the smart channel allocator - Time-on-air -- Packets: +- Frames: - identify from/to - - identify packets (sequence number goes up in lorawan even in + - identify frames (sequence number goes up in lorawan even in retransmissions) - Fix interference calculation -> IMPORTANT - Signal strength, location and rssi (complex, just an extra) diff --git a/simulation.scm b/simulation.scm index 9ccf608..f5115e7 100644 --- a/simulation.scm +++ b/simulation.scm @@ -54,14 +54,18 @@ ;; 'unconfirmed-data (uplink) ;; 'confirmed-data (uplink) ;; 'ack (downlink) -(define-record-type - (make-packet id device-id channel-n uplink? body) - packet? - (id packet-id) - (device-id packet-device-id) - (channel-n packet-channel-n) - (uplink? packet-uplink?) ;; TODO: we could check this - (body packet-body)) +(define-record-type + (make-frame FCnt DeviceAddr mac-commands channel-n uplink? body) + frame? + + (FCnt frame-FCnt) + (DeviceAddr frame-DeviceAddr) + + (mac-commands frame-mac-commands) + (channel-n frame-channel-n) + (uplink? frame-uplink?) ;; TODO: we could check this + (body frame-body)) + (define-record-type (make-device channel thunk) @@ -79,6 +83,11 @@ ;; TODO: Unhardcode me (define time-on-air 0.01) (define to-confirm (make-atomic-box #f)) + (define NbTrans 3) + (define Cu 0) + (define Cd 0) + + (define retransmissions NbTrans) ;; decrement in each transmission ;; Handle the receive windows (define listening? (make-atomic-box #f)) @@ -91,36 +100,37 @@ (define (im-listening?) (atomic-box-ref listening?)) - (define (confirm packet-id) - (when (eq? packet-id (atomic-box-compare-and-swap! to-confirm packet-id #f)) + (define (confirm frame-FCnt) + (when (eq? frame-FCnt (atomic-box-compare-and-swap! to-confirm frame-FCnt #f)) (spawn-fiber - (lambda () "confirm confirmation packet")))) + (lambda () "confirm confirmation frame")))) (define (upstream) - (define current-packet 0) + (define current-frame 0) (forever (when (eq? #f (atomic-box-ref to-confirm)) (ll "Device ~a waiting for data" id) (sleep (rand-time))) ;; wait for more data (let* ((confirmed? #t) ;; random? - (packet (make-packet - current-packet + (frame (make-frame + current-frame id + '() channel #t (if confirmed? 'confirmed-data 'unconfirmed-data)))) (when confirmed? - (atomic-box-compare-and-swap! to-confirm #f current-packet)) - (put-message upstream-chn (make-event 'start packet)) + (atomic-box-compare-and-swap! to-confirm #f current-frame)) + (put-message upstream-chn (make-event 'start frame)) (sleep time-on-air) - (put-message upstream-chn (make-event 'end packet))) - (set! current-packet (1+ current-packet)) + (put-message upstream-chn (make-event 'end frame))) + (set! current-frame (1+ current-frame)) (sleep RECEIVE_DELAY1) (start-listening!) (sleep RX1) (stop-listening!) (when (atomic-box-ref to-confirm) - (sleep (- RECEIVE_DELAY2 RECEIVE_DELAY1)) + (sleep (- RECEIVE_DELAY2 RECEIVE_DELAY1 RX1)) (start-listening!) (sleep RX2) (stop-listening!)))) @@ -129,8 +139,8 @@ (forever (let ((msg (get-message downstream-chn))) (when (im-listening?) - (match (packet-body msg) - ('ack (confirm (packet-id msg)))))))) + (match (frame-body msg) + ('ack (confirm (frame-FCnt msg)))))))) (lambda () (spawn-fiber upstream) @@ -144,10 +154,10 @@ ;; TODO: answer in the second window?? (lambda () (sleep RECEIVE_DELAY1) - (let ((packet (make-packet seq-number to channel #f 'ack))) - (put-message downstream (make-event 'start packet)) + (let ((frame (make-frame seq-number to '() channel #f 'ack))) + (put-message downstream (make-event 'start frame)) (sleep time-on-air) ;; TODO: size / data-rate - (put-message downstream (make-event 'end packet)))))) + (put-message downstream (make-event 'end frame)))))) ;; Upstream: listen, and answer in new fibers (lambda () @@ -155,13 +165,13 @@ (let ((msg (get-message upstream))) (ll "Gateway ~a: Data #~a got from ~a" id - (packet-id msg) - (packet-device-id msg)) - (match (packet-body msg) + (frame-FCnt msg) + (frame-DeviceAddr msg)) + (match (frame-body msg) ('confirmed-data - (ack-confirmed-data (packet-device-id msg) - (packet-channel-n msg) ;; TODO: not right - (packet-id msg))) + (ack-confirmed-data (frame-DeviceAddr msg) + (frame-channel-n msg) ;; TODO: not right + (frame-FCnt msg))) ('unconfirmed-data #f)))))) @@ -169,40 +179,40 @@ (define started '()) - ;; TODO: this is broken, only accounts for the interference of the packet + ;; TODO: this is broken, only accounts for the interference of the frame ;; that was already being sent, and not from the new one that produced the ;; interference => both should be affected. - (define (interference? msg started-packets) - (any (lambda (x) (= (packet-channel-n msg) (packet-channel-n x))) - started-packets)) + (define (interference? msg started-frames) + (any (lambda (x) (= (frame-channel-n msg) (frame-channel-n x))) + started-frames)) (lambda () (forever (let ((ev (get-message in))) (match ev (($ 'start msg) - (ll "Device ~a started ~a of packet ~a" - (packet-device-id msg) - (packet-body msg) - (packet-id msg)) + (ll "Device ~a started ~a of frame ~a" + (frame-DeviceAddr msg) + (frame-body msg) + (frame-FCnt msg)) (set! started (cons msg started))) (($ 'end msg) - (ll "Device ~a finished ~a of packet ~a" - (packet-device-id msg) - (packet-body msg) - (packet-id msg)) + (ll "Device ~a finished ~a of frame ~a" + (frame-DeviceAddr msg) + (frame-body msg) + (frame-FCnt msg)) (set! started (remove! (lambda (x) - (and (= (packet-id msg) (packet-id x)) - (= (packet-device-id msg) (packet-device-id x)))) started)) + (and (= (frame-FCnt msg) (frame-FCnt x)) + (= (frame-DeviceAddr msg) (frame-DeviceAddr x)))) started)) (if (interference? msg started) ;; TODO: interferences are broken (ll "Interference!!!!!!") - (if (packet-uplink? msg) + (if (frame-uplink? msg) (hash-table-walk gateways (lambda (k gateway) (put-message (device-channel gateway) msg))) (put-message - (device-channel (hash-table-ref end-devices (packet-device-id msg))) + (device-channel (hash-table-ref end-devices (frame-DeviceAddr msg))) msg))))))))) -- cgit v1.2.3