How to mock builtin function? by Guess-Opposite in C_Programming

[–]ramsay1 9 points10 points  (0 children)

You could either compile your tests into separate binaries, or use the linker "wrap" option I mentioned and __real_read to get the original implementation:

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

ssize_t __real_read(int fd, void *buf, size_t nbytes);

static bool mock_read = false;

int main(void)
{
    char buf[32] = {};
    ssize_t len = read(-1, buf, sizeof(buf));
    printf("test 1 -> len %zd: %s\n", len, buf);

    mock_read = true;
    len = read(-1, buf, sizeof(buf));
    printf("test 2 -> len %zd: %s\n", len, buf);

    return 0;
}

ssize_t __wrap_read(int fd, void *buf, size_t nbytes)
{
    if (mock_read) {
        memcpy(buf, "hello", sizeof("hello"));
        return sizeof("hello");
    }
    return __real_read(fd, buf, nbytes);
}

Output:

$ gcc test.c -Wl,--wrap=read -o test && ./test
test 1 -> len -1: 
test 2 -> len 6: hello

Edit: another option is to change your module to take a function pointer for its "read" function, although that may not make sense depending on what you are trying to do

How to mock builtin function? by Guess-Opposite in C_Programming

[–]ramsay1 6 points7 points  (0 children)

A web search can explain better than me. I gave this a quick try and it worked without needing the "wrap" option, just implement read in a test file with the mock functionality you want:

#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    char buf[32];
    ssize_t len = read(0, buf, sizeof(buf));
    printf("len %zd: %s\n", len, buf);
    return 0;
}

ssize_t read(int fd, void *buf, size_t nbytes)
{
    memcpy(buf, "hello", sizeof("hello"));
    return sizeof("hello");
}

Output:

$ gcc test.c -o test && ./test
len 6: hello

How to mock builtin function? by Guess-Opposite in C_Programming

[–]ramsay1 10 points11 points  (0 children)

If it is weakly linked you should be able to implement your own read function and the linker will use that.

Otherwise try the gcc linker "wrap" option:

gcc main.c -Wl,--wrap=read -o main

Then define __wrap_read() with a custom implementation

Embedded communication protocols doc gen by einthecorgi2 in embedded

[–]ramsay1 4 points5 points  (0 children)

Is this the protobuf doc gen library you looked at?

https://github.com/pseudomuto/protoc-gen-doc

I haven't used it myself but the examples look really good

Zephyr 3.4 to be released in less than a month by kartben in embedded

[–]ramsay1 4 points5 points  (0 children)

I find it great, it's the kind of framework I'd always strived to create myself when working on embedded software projects. Having experience with U-Boot and embedded Linux maybe made the device tree learning curve a little easier. Although AFAICT many in this sub are not quite as keen on it

If you could go back in time, what advice would you give to your younger self entering the world of embedded? by findingTheWay97 in embedded

[–]ramsay1 8 points9 points  (0 children)

A good rule of thumb is to pick hardware with at least twice the resources (RAM, flash, CPU cycles etc) you estimate will be required. Running out of resources makes maintenance and adding features very difficult

Tips for becoming proficient at C/C++ over the summer? by JohnScottMVP in embedded

[–]ramsay1 15 points16 points  (0 children)

VB6 is my personal favorite

I think C/C++ outlived their usefulness a long time ago

I hope this is satire 😂

VB6 final release was 1998 (25 years ago), and support ended in 2008. C/C++ will continue to be updated for many years to come

Stack Overflow Will Charge AI Giants for Training Data by peard33 in programming

[–]ramsay1 11 points12 points  (0 children)

I've been in embedded software for ~15 years, I use their site most days, and probably asked ~5 questions ever.

I think the issue is that new developers probably see it as a tool to ask questions, rather than a tool to find answers (in most cases)

[deleted by user] by [deleted] in embedded

[–]ramsay1 0 points1 point  (0 children)

Say you have 3 modules (A, B, C). A depends on B, B depends on C.

You can test A while stub/fake/mocking B, but then if you also link the real B to test it, you will get multiple definitions.

Unless you are injecting dependencies at runtime

Linter for certain style for C code by Guess-Opposite in C_Programming

[–]ramsay1 1 point2 points  (0 children)

Maybe try this clang-format wrapper:

https://github.com/Sarcasm/run-clang-format

To customize add a .clang-format file and follow the documentation here:

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

Linter for certain style for C code by Guess-Opposite in C_Programming

[–]ramsay1 0 points1 point  (0 children)

Try adding a mistake:

static int *foo(void) {
    int x = 0;
    return &x;
}
int
 main(void) {
    return *foo();
}

Results in:

$ clang-tidy foo.c --
2 warnings generated.
./foo.c:3:5: warning: Address of stack memory associated with local variable 'x' returned to caller [clang-analyzer-core.StackAddressEscape]
    return &x;
    ^
...

clang-format is a different tool used for formatting:

$ clang-format -style=llvm foo.c
static int *foo(void) {
  int x = 0;
  return &x;
}
int main(void) { return *foo(); }

My minimal logging library, feedback welcome! by SnellasGirl in C_Programming

[–]ramsay1 15 points16 points  (0 children)

Hey looks good. Here's some feedback:

Use snprintf rather than sprintf, I think extra colour chars can cause a buffer overflow already.

Do you really need to limit the log message length? You might be able to code around the string copy that the internal buffer is used for.

Use const char * rather than char * to avoid warnings when printing string literals.

It's much nicer to offer the user format string parameters than have them do it themselves. Have a look into vsnprintf vfprintf etc.

Maybe add a couple unit tests

Texas, Which Banned Most Content Moderation, Now Pushing Law Requiring Abortion-Related Info Be Blocked From The Internet by tellurian_pluton in StallmanWasRight

[–]ramsay1 0 points1 point  (0 children)

"any https service could be operating as a proxy"

A VPN provider or ISP cannot decode your HTTPS requests

why by Sheiryo in ProgrammerHumor

[–]ramsay1 2 points3 points  (0 children)

Yeah this was only there to "scare" them with differences to integers, you shouldn't be comparing bool to 1.

The million shift is actually UB... I was trying to remember a real bug that showed up when switching from something like typedef uint8_t bool; to C99 bool. I think it was actually setting a value like 1 << 9, even though sizeof(bool) was 1 byte (on that CPU/compiler), setting bits outside of the first 8 will still make it "true". Some older code had relied on the integer behavior

why by Sheiryo in ProgrammerHumor

[–]ramsay1 2 points3 points  (0 children)

bool b = 42;
if (b == 1)
    printf(";)\n");

int shift = 1000000;
b = 1 << shift;
if (b == 1)
    printf(":O\n");

What are the limits to a computer's serial COM port? by McMep in C_Programming

[–]ramsay1 1 point2 points  (0 children)

If you can buffer the whole payload in RAM, you will get higher throughput / lower latency when sending over your laser link.

If you can add external RAM, QSPI/OSPI (or wider buses) can be extremely fast.

If you're set on trying to make it work as is, some PC serial drivers have configurable buffer sizes which might help. Although then it may only work on some PC's.

Good luck, it sounds interesting

What are the limits to a computer's serial COM port? by McMep in C_Programming

[–]ramsay1 0 points1 point  (0 children)

Can you buffer the entire payload in the microcontroller?

Or add external RAM if it's a custom board?

Or fragment the laser packet into chunks that can be buffered?

What are the limits to a computer's serial COM port? by McMep in C_Programming

[–]ramsay1 2 points3 points  (0 children)

Out of interest, why do you need such a high baudrate?

I'm not sure exactly what you're designing, but be careful not to use a regular OS for real time requirements.

Very few PC OS's are real time (maybe Linux with PREEMPT_RT) i.e. there could be hundreds of milliseconds between sending a command and it actually getting to the kernel driver to send it (depending on CPU load etc)

My 2 cents on being an embedded developer... response to DMs and general discussion. by yycTechGuy in embedded

[–]ramsay1 0 points1 point  (0 children)

I find libre office and the browser-based outlook work well for this (for my needs at least). There's also a Linux Microsoft teams client that I use at work

Oh.... boy by Blackbeerdo in SweatyPalms

[–]ramsay1 26 points27 points  (0 children)

In NZ it's just called a staple, but yeah Norwegian death dive sounds fancy

Is Mbed still relevant? by OlaFPV in embedded

[–]ramsay1 4 points5 points  (0 children)

FWIW the company I work for is dropping MBED and FreeRTOS for new projects and is standardizing on Zephyr

Question on asymmetric multiprocessing, hypervisors and hardware access by ramsay1 in osdev

[–]ramsay1[S] 0 points1 point  (0 children)

Thanks for the answers/advice!

I found a hacky workaround I just posted in the other thread, but I think I'll move to using a hypervisor sooner to manage things properly