summaryrefslogtreecommitdiff
path: root/simulation.scm
blob: 92db446ec5e28f4b79f75892674b9c5445716560 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
;; Copyright 2024, 2025 Ekaitz Zarraga <ekaitz@elenq.tech>
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(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)
  #:use-module (ice-9 getopt-long)
  #:use-module (fibers)
  #:use-module (fibers channels)
  #:use-module (fibers operations)
  #:use-module (fibers timers))

(define RX1 1) ; s (has to be between 1-15s)
(define RX2 (+ 2 RX1)) ; s
(define RECEIVE_DELAY1 3) ; s
(define RECEIVE_DELAY2 (+ 2 RECEIVE_DELAY1)) ; s
(define RETRANSMISSION_DELAY 2) ; 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)))

;; Synchronized logger
(define *output-channel* (make-channel))
(define (logger)
  (forever
    (let ((msg (get-message *output-channel*)))
      (display msg))))

(define (time->milliseconds time)
  (let* ((seconds (car time))
         (micros  (cdr time)))
    (+ (* 1000 seconds) (round (/ micros 1000)))))
(define %start-time (time->milliseconds (gettimeofday)))
(define (ll f . data)
  (spawn-fiber
    (lambda ()
      (put-message
        *output-channel*
        (format #f "~12'0d - ~?~%" (- (time->milliseconds (gettimeofday))
                                      %start-time) f data)))))

;; Synchronized id generator
(define *id-channel-in* (make-channel))
(define *id-channel-out* (make-channel))
(define (counter)
   (let loop ((id 0))
     (let ((msg (get-message *id-channel-in*)))
       (put-message *id-channel-out* id)
       (loop (1+ id)))))

(define (new-id)
  (put-message *id-channel-in* #t)
  (get-message *id-channel-out*))


;; For timer operations
(define (seconds->time-unit s)
  (* internal-time-units-per-second s))

;; Virtual channel is a combination of radio frequency and data-rate/sf
;; Radio communication that happens in the same radio channel (same freq) but
;; with different spreading factor does not trigger an interference
(define-record-type <vchannel>
  (make-vchannel freq dr)
  vchannel?
  (freq vchannel-freq) ; kHz
  (dr vchannel-dr))

;; Window channel and DR parameters:
;; These are default values. `RXParamSetupReq` MAC command can be used to
;; change them
(define RX2-vchannel-EU863-870 (make-vchannel 869525 0))
(define RX2-vchannel-US902-928 (make-vchannel 923300 8))
(define RX1DROffset 0)

(define (upstream-vchannel->RX1-vchannel-EU863-870 vchan RX1DROffset)
  (define (upstream-dr->downstream-dr dr)
    (match dr
      ((? (lambda (x) (< x 8)))
       (if (> RX1DROffset dr) 0 (- dr RX1DROffset)))
      ((or 8 10)
       (if (= RX1DROffset 0) 1 0))
      ((or 9 11)
       (match RX1DROffset (0 2) (1 1) (_ 0)))))
  (make-vchannel (vchannel-freq vchan)
                 (upstream-dr->downstream-dr (vchannel-dr vchan))))

(define (upstream-vchannel->RX1-vchannel-US903-928 vchan RX1DROffset)
  (define (upstream-dr->downstream-dr dr)
    (match dr
      ((or 0 5)    (if (= 3 RX1DROffset) 8 (- 10 RX1DROffset)))
      ((or 1 2 3)  (- (+ 10 dr) RX1DROffset))
      (6           (- 11 RX1DROffset))
      (4           (if (= 0 RX1DROffset) 13 (- 13 RX1DROffset)))))
  (make-vchannel (vchannel-freq vchan)
                 (upstream-dr->downstream-dr (vchannel-dr vchan))))

(define (product f l1 l2)
  (concatenate (map (lambda (x) (map (lambda (y) (f x y)) l2)) l1)))

;; Minimum set of vchannels for EU
(define initial-vchannels-EU863-870
  (product make-vchannel (list 868100 868300 868500) (iota 6)))

;; All the defined vchannels for the US
(define vchannels-US903-928-uplink
  (append (product make-vchannel (iota 64 902300 200) (iota 4))
          (map make-vchannel (iota 8 903000 1600) (iota 8 4 0))))
(define vchannels-US903-928-downlink
  (product make-vchannel (iota 8 923300 600) (iota (- 13 7) 8)))

;; For End-Device <--> Radio <--> Gateway
;; type can be:
;; '[up/down]link-start
;; '[up/down]link-end
;; 'interference
(define-record-type <radio-event>
  (make-radio-event type id vchannel frame)
  radio-event?
  (type radio-event-type)
  (id radio-event-id)  ;; Match the start-interference-end events
  (vchannel radio-event-vchannel)
  (frame radio-event-frame))

;; For Gateway <--> Network Server
(define-record-type <network-event>
  (make-network-event gateway-id frame)
  network-event?
  (gateway-id network-event-gateway-id)
  (frame network-event-frame))

;; body can be:
;; 'unconfirmed-data (uplink)
;; 'confirmed-data (uplink)
;; 'ack (downlink)
(define-record-type <frame>
  (make-frame FCnt DeviceAddr mac-commands body)
  frame?

  (FCnt frame-FCnt)
  (DeviceAddr frame-DeviceAddr)

  (mac-commands frame-mac-commands)
  (body frame-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-vchannel upstream-chn downstream-chn)
  (define window (make-channel))
  ;; Activates/deactivates the message sink to avoid blocking on messages we
  ;; don't need
  (define internal-com (make-channel))
  ;; Make all atomic
  (define vchannel initial-vchannel)

  ;; TODO: Unhardcode me
  (define time-on-air 0.01)

  (define (process-downlink! frame)
    ;; TODO
    (ll "Device ~a got downlink frame ~a" id frame))

  (define (send-uplink-frame frame-number device-addr confirmed?)
    (let* ((event-id id)
           (frame (make-frame
                    frame-number
                    id
                    '()
                    (if confirmed? 'confirmed-data 'unconfirmed-data))))
      (put-message upstream-chn
                   (make-radio-event 'uplink-start event-id vchannel frame))
      (sleep time-on-air)
      (put-message upstream-chn
                   (make-radio-event 'uplink-end event-id vchannel frame))))


  (define (receive-window vchannel time)
    (define (listening-to? vchn)
      (equal? vchannel vchn))

    (define (detect-preamble)
      (let wait-for-downlink-start ()
        (let ((msg (perform-operation
                     (choice-operation
                       (wrap-operation (sleep-operation time)
                                       (lambda _ 'time-is-out))
                       (get-operation downstream-chn)))))
          (match msg
            ;; We got the preamble in time
            (($ <radio-event> 'downlink-start message-id (? listening-to? chn))
             (ll "Device ~a got preamble" id)
             message-id)
            ;; No preamble in time
            ('time-is-out #f)
            ;; Current message is not a preamble, continue
            (_ (wait-for-downlink-start))))))

    (define (demodulate message-id)
      (let wait-for-downlink-end ((interference? #f))
        (let ((part-of-same-message? (lambda (i) (= message-id i)))
              (msg (get-message downstream-chn)))
          (match msg
            (($ <radio-event> 'interference
                (= part-of-same-message? id)
                (= listening-to? chn)
                frame)
             (wait-for-downlink-end #t)) ;; Got interference
            (($ <radio-event> 'downlink-end
                (= part-of-same-message? id)
                (= listening-to? chn)
                frame)
             (and (not interference?) frame))
            (_ (wait-for-downlink-end interference?))))))

    (put-message internal-com 'wait)
    (let* ((preamble-id (detect-preamble))
           (result      (and preamble-id (demodulate preamble-id))))
      (put-message internal-com 'continue)
      result))

  (define (device-operation)
    (define (wait-until t)
      (perform-operation
        (timer-operation t)))

    (define current-frame 0)
    (forever
      (ll "Device ~a waiting for data" id)
      (sleep (rand-time)) ;; wait for more data

      (send-uplink-frame current-frame id #t)
      (set! current-frame (1+ current-frame)) ;; TODO: improve
      (let* ((now       (get-internal-real-time))
             (RX1-start (+ now (seconds->time-unit RECEIVE_DELAY1)))
             (RX2-start (+ now (seconds->time-unit RECEIVE_DELAY2)))
             (downlink  (or (begin (wait-until RX1-start)
                                   (receive-window vchannel RX1))
                            (if (< (get-internal-real-time) RX2-start)
                              (begin (wait-until RX2-start)
                                     (receive-window vchannel RX2))
                              #f))))
        (when downlink (process-downlink! downlink)))))

  (define (downstream-sink)
    (forever
      (let ((ev (perform-operation
                  (choice-operation (get-operation internal-com)
                                    (get-operation downstream-chn)))))
        (match ev
          ('wait
           (ll "RX window started")
           (get-message internal-com)
           (ll "RX window finished"))
          (_ #f)))))

  (lambda ()
    (spawn-fiber device-operation)
    (spawn-fiber downstream-sink)))

(define (make-gateway id in radio network)
  (define time-on-air 0.01) ;s (TODO)
  (define pending-interferences '())

  (define (send-to-device frame)
    (ll "Trying to downlink")
    (spawn-fiber
      (lambda ()
        ;; TODO: choose channel properly
        (let ((event-id (new-id))
              (vchannel (make-vchannel 50000000 0))) ;; TODO: hehe invented!
          (ll "Gateway ~a sending downlink ~a" id frame)
          (put-message radio
            (make-radio-event 'downlink-start event-id vchannel frame))
          (sleep time-on-air)
          (put-message radio
            (make-radio-event 'downlink-end   event-id vchannel frame))))))

  (define (send-to-network-server x)
    (ll "Gateway ~a forwarding ~a" id x)
    (put-message network (make-network-event id x)))

  ;; Upstream: listen, and answer in new fibers
  (lambda ()
    (forever
      (let* ((ev (get-message in)))
        (match ev
          (($ <network-event> id frame)
           (send-to-device frame))
          (($ <radio-event> 'uplink-start event-id vchannel frame) #f)
          (($ <radio-event> 'interference event-id vchannel frame)
           (set! pending-interferences (cons ev pending-interferences)))
          (($ <radio-event> 'uplink-end event-id vchannel frame)
           (let-values (((mine not-mine)
                         (partition (lambda (x) (eq? (radio-event-id x)
                                                     (radio-event-id ev)))
                                    pending-interferences)))
             (set! pending-interferences not-mine)
             (when (null? mine) ;; TODO
               (send-to-network-server frame)))))))))


(define (make-radio in end-devices gateways)
  "Fiber for radio resource allocation/control."

  ;; We can access it only from one fiber! Careful!
  (define lorawan-vchannels (make-hash-table))
  (define (interferences ev chn)
    (let ((res (hash-table-ref/default lorawan-vchannels chn '())))
      (if (equal? res (list ev)) '() res)))
  (define (use-vchannel! chn start-event)
    (hash-table-update!/default lorawan-vchannels chn
                        (lambda (event-list)
                          (cons start-event event-list))
                        '()))
  (define (release-vchannel! chn end-event)
    (hash-table-update! lorawan-vchannels chn
      (lambda (event-list)
        (remove! (lambda (x) (eq? (radio-event-id x)
                                  (radio-event-id end-event)))
                 event-list))))
  (define (radio-event->interference ev)
    "Make a new radio-event of type 'interference from another radio-event"
    (match ev
      (($ <radio-event> type id vchannel frame)
       (make-radio-event 'interference id vchannel frame))))

  (lambda ()
    (forever
      (let ((ev (get-message in)))
        (match ev
          (($ <radio-event> 'uplink-start event-id vchannel frame)
           (ll "Device ~a started uplink-frame #~a on freq ~a DR ~a"
               (frame-DeviceAddr frame)
               (frame-FCnt frame)
               (vchannel-freq vchannel)
               (vchannel-dr vchannel))
           (use-vchannel! vchannel ev)
           (let ((ints (interferences ev vchannel)))
             (hash-table-walk gateways
               (lambda (k gateway)
                 (spawn-fiber
                   (lambda ()
                     (put-message (device-channel gateway) ev)
                     (for-each
                       (lambda (ev)
                         (ll "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
                         (put-message (device-channel gateway)
                                      (radio-event->interference ev)))
                       ints)))))))

          (($ <radio-event> 'downlink-start event-id vchannel frame)
           (ll "Device ~a started downlink-frame #~a on freq ~a DR ~a"
               (frame-DeviceAddr frame)
               (frame-FCnt frame)
               (vchannel-freq vchannel)
               (vchannel-dr vchannel))
           (use-vchannel! vchannel ev)
           (let ((chan (device-channel
                         (hash-table-ref end-devices (frame-DeviceAddr frame))))
                 (ints (interferences ev vchannel)))
             (put-message chan ev)
             (for-each
               (lambda (ev)
                 (ll "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
                 (put-message chan (radio-event->interference ev)))
               ints)))

          (($ <radio-event> 'uplink-end event-id vchannel frame)
           (ll "Device ~a ended uplink-frame #~a on freq ~a DR ~a"
               (frame-DeviceAddr frame)
               (frame-FCnt frame)
               (vchannel-freq vchannel)
               (vchannel-dr vchannel))
           (release-vchannel! vchannel ev)
           (hash-table-walk gateways
             (lambda (k gateway)
               (spawn-fiber
                 (lambda ()
                   (put-message (device-channel gateway) ev))))))

          (($ <radio-event> 'downlink-end event-id vchannel frame)
           (ll "Device ~a ended downlink-frame #~a on freq ~a DR ~a"
               (frame-DeviceAddr frame)
               (frame-FCnt frame)
               (vchannel-freq vchannel)
               (vchannel-dr vchannel))
           (release-vchannel! vchannel ev)
           (put-message
             (device-channel (hash-table-ref end-devices
                                             (frame-DeviceAddr frame)))
             ev)))))))


(define (make-network-server upstream gateways end-devices)

  (lambda ()
    (forever
      (match (get-message upstream)
        (($ <network-event> gateway-id frame)
         (ll "Network event happend!")
         ;; TODO answer properly
         (spawn-fiber
           (lambda ()
             (sleep RECEIVE_DELAY1)
             (put-message
               (device-channel (hash-table-ref gateways gateway-id))
               (make-network-event
                 gateway-id
                 (make-frame (frame-FCnt frame)
                             (frame-DeviceAddr frame)
                             #f
                             'hello))))))))))



(define (run-simulation end-device-count gateway-count)
  ;; We need synchronized logger and counter running in fibers
  (spawn-fiber logger)
  (spawn-fiber counter)

  (let* ((radio-chn   (make-channel))
         (end-devices (make-hash-table))
         (gateways    (make-hash-table))
         (network-chn (make-channel)))

    (for-each
      (lambda (id)
        (let ((chn (make-channel)))
          (hash-table-set!
            end-devices id
            (make-device chn
                         (make-class-a
                           id
                           ;; TODO: generalize for the US too?
                           (list-ref initial-vchannels-EU863-870
                                     (modulo id (length initial-vchannels-EU863-870)))
                           radio-chn
                           chn)))))
      (iota end-device-count gateway-count))

    (for-each
      (lambda (id)
        (let ((chn (make-channel)))
          (hash-table-set!
            gateways id
            (make-device chn (make-gateway id chn radio-chn network-chn)))))
      (iota gateway-count 0))

    (spawn-fiber (make-network-server network-chn gateways end-devices))

    (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))))))

(define (main args)
  (define help
"
LoRaWAN interference simulator:

USAGE:
~/guile simulation.scm -d END_DEVICE_COUNT -g GATWEWAY_COUNT

OPTIONS:
~/-g, --gateways~25tGateway count to add to the simulation
~/-d, --end-devices~25tEnd Device count to add to the simulation
~/-h, --help~25tShow this help~&
")
  (define option-spec
    '((gateways     (single-char #\g) (value #t))
      (end-devices  (single-char #\d) (value #t))
      (help         (single-char #\h) (value #f))))

  (let* ((options        (getopt-long args option-spec))
         (end-devices-op (option-ref options 'end-devices #f))
         (gateways-op    (option-ref options 'gateways #f)))
    (when (option-ref options 'help #f)
      (format #t help)
      (exit 0))
    (unless end-devices-op
      (format #t "ERROR: No end-device count provided~%")
      (exit 1))
    (unless gateways-op
      (format #t "ERROR: No gateway count provided~%")
      (exit 1))
    (let* ((end-devices (and end-devices-op (string->number end-devices-op)))
           (gateways    (and gateways-op (string->number gateways-op))))
      (unless (integer? end-devices)
        (format #t "ERROR: -d, --end-devices: expecting integer~%")
        (exit 1))
      (unless (integer? gateways)
        (format #t "ERROR: -g, --gateways: expecting integer~%")
        (exit 1))

      (run-fibers
        (lambda () (run-simulation end-devices gateways))
        #:drain? #t))))

(main (command-line))