The importc pragma can read the value from C headers. Is there any way to use it to determine if it is defined or not?
Example: read config.h and set value with/without "CONFIG_NET". (I want to set false if there is no definition in the header).
const hasNet = true (or false)
Motivation :
I want to run asyncdispatch in a non-network environment, but both ioselectors_select and ioselectors_epoll use functions that depend on the network stack.
I want to avoid calling network functions in a non-networked environment (e.g. UART only).
I considered this through trial and error.
As a result, I thought that allowing configuration without networking would have a significant impact on others, so I will deal with this by enabling only local network(lo).
To check if a C macro is defined:
let hasNet {.importc: """(
#ifdef CONFIG_NET
1
#else
0
#endif
)""",
header: "config.h".}: cint
To check if -d:nimNoNetwork is passed:
when defined(nimNoNetwork):
echo 1
else:
echo 2
I want to run asyncdispatch in a non-network environment, but both ioselectors_select and ioselectors_epoll use functions that depend on the network stack.
Unfortunately that's generally not possible as they're both based on calling select or epoll at their core. Some RTOS will provide a uart -> socket bridge to let you use network polling / socket descriptors. It's slightly less efficient but can be a nice workaround. Also file descriptors with a select like api would work. Zephyr for example support eventfd. Esp32 rtos has a uart socket adaptor.
-d:nimNoNet
Wouldnt wotk. The asyncdispatch library is built on socket api's as thats the primary method to multiplex data on desktop OS'es. Technically one could probably build an asyncdispatch variant based on say channels or queues. It would be interesting to be able to use generic queues as the backing io selector. But that'd require lot of work.
Though easier would be if NuttX has an io event api similar to select api (e.g. using integer file descriptors). There's also nim-taskpools which might provide another route. It should work on an RTOS.
@sls1005 The way you do this is to add code to determine if there is a new definition and then get it. That's a great technique !
@elcritch In the test, I confirmed that if I put "when defined(nuttx):" around the recv() called in selectInto() of ioselectors_select so that recv() is not used, it works without network. However, I don't want to make the beautifully written code dirty, so I decided to enable local loopback.
After all, without some kind of network interface, it is not possible to communicate with the outside world (usually, PPP is used with LTE).
However, I should have used "select" as initially advised. I was testing on a machine with 512 MiB of memory that dual-boots with Linux, so I didn't realize how much memory the EPOLL implementation would require.