summaryrefslogtreecommitdiff
path: root/simulation.scm
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-12-17 17:11:40 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-12-17 17:11:40 +0100
commitc0b065c3273be92d137f9b3ca414c111738d9f86 (patch)
treee3b12f6eae9b903824bfc59a87ffa9fd758d7376 /simulation.scm
parent6032def92070383f70373d6175898aa9ca34cc6d (diff)
simulation+todo: use frame instead of packet
Diffstat (limited to 'simulation.scm')
-rw-r--r--simulation.scm102
1 files changed, 56 insertions, 46 deletions
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 <packet>
- (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 <frame>
+ (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 <device>
(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
(($ <event> '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)))
(($ <event> '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)))))))))