blob: 09cc16d9935340157822add64e0b0d023c04e307 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
(define-module (simulation)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-69)
#:use-module (ice-9 atomic)
#:use-module (ice-9 match)
#:use-module (fibers)
#:use-module (fibers channels)
#:use-module (fibers timers))
(define RX1 1) ; s (has to be between 1-15s)
(define RX2 (1+ RX1)) ; s
(define RECEIVE_DELAY1 1) ; s
(define RECEIVE_DELAY2 (1+ RECEIVE_DELAY1)) ; s
;; From RP002 (Regional Parameters):
;; MAC commands exist in the LoRaWAN® specification to change the value of
;; RECEIVE_DELAY1 (using RXTimingSetupReq, RXTimingSetupAns) as well as
;; ADR_ACK_LIMIT and ADR_ACK_DELAY (using ADRParamSetupReq, ADRParamSetupAns).
;; Also, RXTimingSettings are transmitted to the end device along with the
;; JOIN_ACCEPT message in OTAA mode.
;; TODO: put a time limit as a parameter
(define-syntax-rule (forever exp ...)
(let loop ()
(begin exp ...)
(loop)))
(define (ll f . data)
(let ((now (gettimeofday)))
(format #t "~a~a - ~?~%" (car now) (cdr now) f data)))
;; type can be:
;; 'start
;; 'end
(define-record-type <event>
(make-event type data)
event?
(type event-type)
(data event-data))
;; body can be:
;; '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 <device>
(make-device channel thunk)
device?
(channel device-channel)
(thunk device-thunk))
(define (rand-time)
(random 2))
(define (make-class-a id initial-channel upstream-chn downstream-chn)
;; Make all atomic
(define channel initial-channel)
;; TODO: Unhardcode me
(define time-on-air 0.01)
(define to-confirm (make-atomic-box #f))
;; Handle the receive windows
(define listening? (make-atomic-box #f))
(define (start-listening!)
(ll "Device ~a started listening" id)
(atomic-box-set! listening? #t))
(define (stop-listening!)
(ll "Device ~a stopped listening" id)
(atomic-box-set! listening? #f))
(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))
(spawn-fiber
(lambda () "confirm confirmation packet"))))
(define (upstream)
(define current-packet 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
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))
(sleep time-on-air)
(put-message upstream-chn (make-event 'end packet)))
(set! current-packet (1+ current-packet))
(sleep RECEIVE_DELAY1)
(start-listening!)
(sleep RX1)
(stop-listening!)
(when (atomic-box-ref to-confirm)
(sleep (- RECEIVE_DELAY2 RECEIVE_DELAY1))
(start-listening!)
(sleep RX2)
(stop-listening!))))
(define (downstream)
(forever
(let ((msg (get-message downstream-chn)))
(when (im-listening?)
(match (packet-body msg)
('ack (confirm (packet-id msg))))))))
(lambda ()
(spawn-fiber upstream)
(spawn-fiber downstream)))
(define (make-gateway id upstream downstream)
(define time-on-air 0.01) ;s (TODO)
(define (ack-confirmed-data to channel seq-number)
(spawn-fiber
;; 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))
(sleep time-on-air) ;; TODO: size / data-rate
(put-message downstream (make-event 'end packet))))))
;; Upstream: listen, and answer in new fibers
(lambda ()
(forever
(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)
('confirmed-data
(ack-confirmed-data (packet-device-id msg)
(packet-channel-n msg) ;; TODO: not right
(packet-id msg)))
('unconfirmed-data #f))))))
(define (make-radio in end-devices gateways)
(define started '())
;; TODO: this is broken, only accounts for the interference of the packet
;; 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))
(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))
(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))
(set! started
(remove! (lambda (x)
(and (= (packet-id msg) (packet-id x))
(= (packet-device-id msg) (packet-device-id x)))) started))
(if (interference? msg started) ;; TODO: interferences are broken
(ll "Interference!!!!!!")
(if (packet-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)))
msg)))))))))
(define (run-simulation)
(let* ((radio-chn (make-channel))
(end-devices (make-hash-table))
(gateways (make-hash-table)))
(for-each
(lambda (id)
(let ((chn (make-channel)))
(hash-table-set!
end-devices id
(make-device chn (make-class-a id 1 radio-chn chn)))))
(iota 1))
(for-each
(lambda (id)
(let ((chn (make-channel)))
(hash-table-set!
gateways id
(make-device chn (make-gateway id chn radio-chn)))))
(iota 1 2))
(spawn-fiber (make-radio radio-chn end-devices gateways))
(hash-table-walk end-devices
(lambda (_ device) (spawn-fiber (device-thunk device))))
(hash-table-walk gateways
(lambda (_ device) (spawn-fiber (device-thunk device))))))
(run-fibers
run-simulation
#:drain? #t)
|