1## Overall internal design
2
3In short this Phy is composed of the following main components:
4
5* A list of current (and future) transmissions in the air
6* For each device interface: A state machine where transitions/operations
7  can either be done immediately or be scheduled for a future time
8* A list of (timed) events (function queue)
9
10### The pending transmissions list
11
12The pending list of transmissions (Tx list,
13[pending_tx_list.c](../src/p2G4_pending_tx_list.c)/
14[h](../src/p2G4_pending_tx_rx_list.h))
15is an array with one entry per device (`tx_l_c_t`).
16Where each element keeps a current or future transmission parameters,
17as well as an indication of the transmission being currently active.<br>
18An interface is provided to modify (register) and entry, set it to active,
19and clear it.<br>
20
21### Functions queue
22
23The list of events/function queue,
24([func_queue.h](../src/p2G4_func_queue.h),
25[funcqueue.c](../src/p2G4_func_queue.c))
26is used by each of the device interfaces to schedule future operations.
27This queue has one entry per interface, which consists just of a function to be
28called and the simulated time when the call should be done.<br>
29
30This queue provides functions to modify (add) an entry, to remove it,
31as well as to get the time of the next event.<br>
32Each time a new event is queued the list finds which is the next event
33which needs to be executed. The queue is ordered first by time, and then by
34event type (`f_index_t`).<br>
35You can see more details in [p2G4_func_queue.h](../src/p2G4_func_queue.h).
36
37### Device interface state machine
38
39Each device interface implements the same state machine, which in short works
40as follows:
41
421. The next operation is requested from the device.
43   Depending on what the operation is, either one of the substatemachines events
44   is started (Rx, Tx, Wait, RSSI, CCA), or the device interface is disabled
45   (DISCONNECT), or the whole simulation is ended (TERMINATE)
462. The substate machine transitions over time (using the function queue)
47   until it eventually comes back to 1.
48
49You can find more details about each substate machine in
50[README_device_interface.md](README_device_interface.md)
51
52## Overall workings
53
54The Phy starts by parsing the command line parameters, intializing all its
55components, and loading and initializing the channel and modem libraries.
56
57Then it will block until receiving the next request from each device.
58
59Note that this is the basic way of working of the Phy:
60It cannot let time pass until it knows what is it that all devices want to do.
61It cannot know if a reception will be succesfull until it knows what all others
62devices will be doing during that reception time.
63
64Once it knows what it is that all devices want to do, it can let time pass
65until it handles the first (earliest) device needs, and then it must wait until
66that device requests what it wants next.
67
68It will then repeat this process again and again, until the simulation end time
69has been reached, or all devices have disconnected.
70