Re: [dev] organizing programs

2023-05-14 Thread NRK
On Sun, May 14, 2023 at 09:55:16PM +0200, Sebastiano Tronto wrote:
> I could not find any tool that was simple enough for my taste, so I
> rolled my own[0].
>
> [0] https://git.tronto.net/sdep

Not too bad, and since the source was pretty small, I decided to take a
glance. Here's some unsolicited review:


9 #define min(X,Y) (Xhttps://c-faq.com/cpp/safemacros.html


11 /*
12  * Maximum number of characters in a line. The rest will be 
truncated.
13  * Change this if you need very long lines.
14  */
15 static const int MAXLEN = 1;

`const` in C doesn't mean "constant expression", and so if you use
`buf[MAXLEN]` you'll get a VLA (compile with -Wvla and you should see warning
about it). Either use a #define or an enum.

https://c-faq.com/ansi/constasconst.html


55 static Options default_op();

`f()` does not do what you might think. A function without an argument list
takes *unspeficied* amount of arguments, not zero arguments (a historical
baggage). These have been obsolete since C99 and newer clang version defaults
to erroring out on it, see: https://wiki.gentoo.org/wiki/Modern_C_porting

Explicitly use `f(void)` instead.


71  next->ev.text = malloc(sizeof(char) * l);

sizeof(char) is *defined* to be always 1. So it's not really doing anything:
https://c-faq.com/malloc/sizeofchar.html


72  strncpy(next->ev.text, text, l);

`strncpy` doesn't nul-terminate in case the soruce is bigger than the dest.
Additionally strncpy will *always* write `n` bytes even if the soruce fits into
the dest. This is rarely the semantic people want and 99% of the time I see
strncpy used it's typically either bugger, misused, or enefficient.

There is no standard "copy and truncate if needed" function. Closest you can
find would be memccpy:

if (memccpy(dest, src, '\0', n) == NULL)
dest[n - 1] = '\0';

You can wrap this in function or roll your own (TIP: if you have your string 
copy
function return a pointer to the nul-byte in dest, then you can use it for
efficient concat: https://www.symas.com/post/the-sad-state-of-c-strings).


216 static char *
217 strtrim(char *t)
218 {
219 char *s;
220 
221 for (s = [strlen(t)-1]; s != t && isspace(*s); *s = '\0', 
s--);
222 for (; *t != '\0' && isspace(*t); t++);

The entire ctype library is badly designed because it only accepts input that's
either EOF or [0, UCHAR_MAX]. From the manpage:

| These functions check whether c, which must have the value of an unsigned char
| or EOF (otherwise the behavior is undefined)

Either cast the argument to `unsigned char` or just roll your own. (Also
keep in mind that `plain char` can be either signed or unsigned
depending on the implementation).

Also the name `strtrim` steps into the namespace reserved by .
You can rename it to `str_trim` to avoid it.


And that's mostly it from a quick glance. Slightly relevant:
http://www.catb.org/~esr/writings/unix-koans/ten-thousand.html

- NRK



Re: [dev] organizing programs

2023-05-14 Thread Sebastiano Tronto
Hello,

LM  wrote:
> I've been looking into todo programs, task schedulers and related
> organizing programs. [...]
> 
> I'd be curious to know what tools other people use on the list to
> handle organizational jobs such as time and task scheduling, todo
> lists, habit tracking, displaying/printing calendars, etc.  [...]

For TODO lists I have always used plain text files, either a single file
or a directory with multiple files when things got out of hand. I have
read about using mail inboxes as todo-list, I'd like to try once; but
for now it is just an entry in my TODO.txt (seriously).

The only thing I was missing from plain text files were notifications /
reminders for time-sensitive stuff. I could not find any tool that was
simple enough for my taste, so I rolled my own[0]. It is a C program that
reads line formatted as "[date] [text]" from stdin and prints out those
whose date matches the current minute to stdout. I have a script running
every 60 seconds that feeds a file with my task list to this program and
pipes the output to herbe[1] (or to notify-send). I have some shortcuts
to add pre-formatted entries to my tasks file, but usually I just edit
it with vi.

For calendar I just use cal(1) in a pop-up terminal[2][3].

Best,
Sebastiano

[0] https://git.tronto.net/sdep
[1] https://github.com/dudik/herbe
[2] https://git.tronto.net/scripts/file/popup-cal12.html
[3] https://git.tronto.net/scripts/file/popup-cal3.html



Re: [dev] Simpler WiFi alternatives

2023-05-14 Thread Josuah Demangeon
To anyone who was genuinely interested in the topic:

Since Arduino was pointed out, something on that level that is very widely used 
is the ESP32:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-ethernet-kit.html

It happens that WiFi is cheap: a piece of PCB trace does an antenna, and a tiny 
inductor acts as a balun, that's it, you mostly have your WiFi transceiver.

The other way around is also possible: An MCU supporting Ethernet, and adding 
WiFi to it:

https://community.st.com/s/question/0D53W05qaHySAI/adding-wifi-to-stm32

Maybe some MCU with both Ethernet and a WiFi interface would work too:

https://www.nxp.com/products/wireless/wi-fi-plus-bluetooth-plus-802-15-4/wireless-mcu-with-integrated-radiobr-1x1-wi-fi-6-plus-bluetooth-low-energy-5-3-radios:RW610

Ultimately, looking at what an existing product has could be insightful:

https://mikrotik.com/product/RB911-5HnD

> These days even embedded systems will likely use an RTOS which may
> include an existing protocol stack, e.g. RTEMS [2] uses a port of the
> FreeBSD stack.

RTEMS is doing great work! Never tried it though.

Zephyr is getting a lot of contributions/popularity.
Possibly because it got a fancy website and is having the Linux Foundation logo 
on it.
Possibly because a few vendors (Nordic in particular) dropped their own 
proprietary
RTOS in favor of it.

There might eventually be examples on how to plug things together and get 
something like that working.

Last but not least, it is always possible to try to find a part with both 
ethernet and wifi support:

https://octopart.com/electronic-parts/integrated-circuits-ics/embedded-processors-and-controllers/microcontrollers
https://octopart.com/electronic-parts/integrated-circuits-ics/embedded-processors-and-controllers/microprocessors


Sagar Acharya  wrote:
> Yep. That is true. I didn't think of that at all! But then, why do current 
> WiFi, etc. work at 2.4GHz, if device speeds aren't at those levels?
> 
> I'll try wireless transmissions of files at 10MHz then in the lower frequency 
> regions. Then by replacing hardware, maybe I'll just have to change a single 
> number!
> 
> I already use just wired connections but today's extreme use of mobile phones 
> has made securing wireless networks a compulsion!
> Thanking you
> Sagar Acharya
> http://humaaraartha.in 
> 
> 
> 
> 13 May 2023, 15:20 by d...@dbrooke.me.uk:
> 
> > A simpler alternative to WiFi is to use wires 8-)
> >
> > Seriously, the radio frequency techniques necessary to provide
> > sufficient throughput for today's applications are inevitably complex.
> >
> > On Fri, May 12, 2023 at 05:43:04PM +0200, Sagar Acharya wrote:
> >
> >> What I'm trying to find is a simple C program which can be run on Arduino 
> >> and analog pins connected to an antenna.
> >>
> >
> > The lowest frequency at which WiFi operates is the 2.4GHz band, an
> > Arduino (there are many variants, of which some will be faster) is
> > likely to be clocked at 10s of MHz so will be unable to generate
> > suitable radio frequency signals at its analogue pins.
> >
> > Back in the '90s I was running TCP/IP over amateur radio at low data
> > rates (9600 bps) using KA9Q NOS [1] which was a single C program for
> > DOS. It's probably close to the minimum needed for a router, although it
> > does typically include some application level features.
> >
> > These days even embedded systems will likely use an RTOS which may
> > include an existing protocol stack, e.g. RTEMS [2] uses a port of the
> > FreeBSD stack.
> >
> > David
> >
> > [1] http://www.ka9q.net/code/ka9qnos/
> > [2] https://www.rtems.org/



Re: [dev] Simpler WiFi alternatives

2023-05-14 Thread David Brooke
On Sat, May 13, 2023 at 05:18:53PM +0200, Sagar Acharya wrote:
> Yep. That is true. I didn't think of that at all! But then, why do current 
> WiFi, etc. work at 2.4GHz, if device speeds aren't at those levels?

The choice of radio frequency will be based on suitability (available
bandwidth, propagation characteristics etc.) and regulatory matters
(world-wide availability etc.). The 5GHz band is also widely used now.

Typically the lower layers of the protocol stack (including the radio
for wireless and the electrical interface for wired) will use a
dedicated chip set, so the microprocessor device need only concern
itself with higher level data. For low speed systems there are even chip
sets (e.g. WIZnet [1]) which offload as high as the TCP layer, though
these are less flexible than handling TCP/IP in software.

David

[1] https://www.wiznet.io/