This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How does NRF_MESH_ASSERT work?

Hello, when reading the source code for a mesh node provisionee, I stumbled upon this MACRO function:

NRF_MESH_ASSERT

Edit: it is in ble_mesh_v0.9.1-Alpha\mesh\src\core\packet_mgr.c

it is defined as:

 #define NRF_MESH_ASSERT(cond)   if (!(cond))                        \
     {                                                                   \
         uint32_t pc;                                                    \
         GET_PC(pc);                                                     \
         if (m_assertion_handler)                                        \
         {                                                               \
             m_assertion_handler(pc);                                    \
         }                                                               \
         else                                                            \
         {                                                               \
             HARD_FAULT();                                               \
         }                                                               \
     }

There are quite a few lines of and within this function which confuse me due to their difficulties (and in any case, I'm not a huge fan of MACRO functions) but I'll just ask about 2 places which puzzles me the most:

#Question #1:

the

GET_PC(pc); 

is defined as:

#define GET_PC(__pc) do {                       \
        __pc = __current_pc();                  \
    } while (0)

Would I be correct in assuming that

__current_pc()

is some sort of compiler function or lower-level library function which returns the realtime/ during run-time PC counter value?

#Question #2:

What is the purpose of this function? To make sure last_block_size is actually greater than 0?

Please provide help, thank you!

Parents
  • Hi Mitch,

    Q1: Yes, the purpose of the __current_pc() function is to return the program counter at the time of the assert.

    Q2: I assume that you mean the NRF_MESH_ASSERT macro, it used to verify that the provided condition is true, if not then the program execution can not continue, e.g.

    NRF_MESH_ASSERT(p_evt != NULL);
    

    where the macro is used to verify that the p_evt pointer is not a NULL pointer. If it is a null pointer then the program should continue its execution and we end up in the assertion handler.

    Wikipedia provides a pretty decent explanation:

    An Assertion is a statement that a predicate (Boolean-valued function, a true–false expression)is expected to always be true at that point in the code. If an assertion evaluates to false at run time, an assertion failure results, which typically causes the program to crash, or to throw an assertion exception.

    Best regards

    Bjørn

Reply
  • Hi Mitch,

    Q1: Yes, the purpose of the __current_pc() function is to return the program counter at the time of the assert.

    Q2: I assume that you mean the NRF_MESH_ASSERT macro, it used to verify that the provided condition is true, if not then the program execution can not continue, e.g.

    NRF_MESH_ASSERT(p_evt != NULL);
    

    where the macro is used to verify that the p_evt pointer is not a NULL pointer. If it is a null pointer then the program should continue its execution and we end up in the assertion handler.

    Wikipedia provides a pretty decent explanation:

    An Assertion is a statement that a predicate (Boolean-valued function, a true–false expression)is expected to always be true at that point in the code. If an assertion evaluates to false at run time, an assertion failure results, which typically causes the program to crash, or to throw an assertion exception.

    Best regards

    Bjørn

Children
No Data
Related