summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-12-18 14:30:13 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-12-18 14:30:13 +0100
commitc28dbf40debd25786276e6b5601236730f010997 (patch)
treee5bba73702029671a8968d933418f05257072a4e
parent38331a0e9f356ad6de4789938e1604df0b8fa184 (diff)
simulation: gateways: get radio events
-rw-r--r--simulation.scm49
1 files changed, 27 insertions, 22 deletions
diff --git a/simulation.scm b/simulation.scm
index a710fa0..ee71d10 100644
--- a/simulation.scm
+++ b/simulation.scm
@@ -1,6 +1,7 @@
(define-module (simulation)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
+ #:use-module (srfi srfi-11)
#:use-module (srfi srfi-69)
#:use-module (ice-9 atomic)
#:use-module (ice-9 match)
@@ -46,6 +47,7 @@
;; type can be:
;; '[up/down]link-start
;; '[up/down]link-end
+;; 'interference
(define-record-type <radio-event>
(make-radio-event type channel-n frame)
radio-event?
@@ -156,31 +158,27 @@
(define (make-gateway id upstream downstream)
(define time-on-air 0.01) ;s (TODO)
+ (define radio-interference-queue '())
- (define (ack-confirmed-data to channel seq-number)
- (spawn-fiber
- ;; TODO: answer in the second window??
- (lambda ()
- (sleep RECEIVE_DELAY1)
- (let ((frame (make-frame seq-number to '() 'ack)))
- (put-message downstream (make-radio-event 'downlink-start channel frame))
- (sleep time-on-air) ;; TODO: size / data-rate
- (put-message downstream (make-radio-event 'downlink-end channel frame))))))
+ (define (forward-frame x)
+ (ll "forwarding ~a" x))
;; Upstream: listen, and answer in new fibers
(lambda ()
(forever
- (let* ((msg (get-message upstream))) ;; TODO: make this get events instead
- (ll "Gateway ~a: Data #~a got from ~a"
- id
- (frame-FCnt msg)
- (frame-DeviceAddr msg))
- (match (frame-body msg)
- ('confirmed-data
- (ack-confirmed-data (frame-DeviceAddr msg)
- 1
- (frame-FCnt msg))) ;; TODO: wrong!
- ('unconfirmed-data #f))))))
+ (let* ((ev (get-message upstream)))
+ (match ev
+ (($ <radio-event> 'uplink-start channel-n frame) #f)
+ (($ <radio-event> 'interference channel-n frame)
+ (set! radio-interference-queue (cons ev radio-interference-queue)))
+ (($ <radio-event> 'uplink-end channel-n frame)
+ (let-values (((mine not-mine)
+ (partition (lambda (x) (eq? (radio-event-frame x)
+ (radio-event-frame ev)))
+ radio-interference-queue)))
+ (set! radio-interference-queue not-mine)
+ (when (null? mine) ;; TODO
+ (forward-frame frame)))))))))
(define (make-radio in end-devices gateways)
@@ -214,6 +212,9 @@
(frame-FCnt frame)
channel-n)
(use-lorawan-channel! channel-n ev)
+ (hash-table-walk gateways
+ (lambda (k gateway)
+ (put-message (device-channel gateway) ev)))
(when (interference? channel-n)
;; TODO: send an 'interference event to the device
(ll "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")))
@@ -224,6 +225,10 @@
(frame-FCnt frame)
channel-n)
(use-lorawan-channel! channel-n ev)
+ (put-message
+ (device-channel (hash-table-ref end-devices
+ (frame-DeviceAddr frame)))
+ ev)
(when (interference? channel-n)
;; TODO: send an 'interference event to the device
(ll "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")))
@@ -236,7 +241,7 @@
(release-lorawan-channel! channel-n ev)
(hash-table-walk gateways
(lambda (k gateway)
- (put-message (device-channel gateway) frame))))
+ (put-message (device-channel gateway) ev))))
(($ <radio-event> 'downlink-end channel-n frame)
(ll "Device ~a ended uplink-frame #~a on channel ~a"
@@ -247,7 +252,7 @@
(put-message
(device-channel (hash-table-ref end-devices
(frame-DeviceAddr frame)))
- frame)))))))
+ ev)))))))
(define (make-network-server upstream gateways end-devices)