</>EmbeddedInterview
← All problems · Drivers & Peripherals

Check Required Task Heartbeats

Easy

A watchdog supervisor may feed the watchdog only when all required task heartbeat bits have been observed in the current window. Return 1 if healthy. Implement int healthy(uint32_t observed,uint32_t required).

Choose C or C++ and write your code on the right and press Run (or Ctrl + Enter). Hidden tests check your functions and print the result below the editor.

Mask observed events with required events.

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>

int healthy(uint32_t observed,uint32_t required)
{
    return (observed & required)==required;
}

Extra heartbeat bits do not affect the decision. In firmware, synchronize snapshot and reset.

solution.c
OutputPress Run to compile and test your code.
Show the hidden tests
/* ---- tests ---- */
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
int main(void)
{
    
    CHECK(healthy(7,5)==1);
    CHECK(healthy(1,5)==0);
    CHECK(healthy(0,0)==1);
    puts(fails ? "Some tests failed" : "All tests passed");
    return fails != 0;
}