Refactor to use new protocol module and sipo.
[bertos.git] / bertos / cfg / macros.h
index 073ad55fc84a30ad3ea952d4aa4f15a04414e27a..4af10246f2ad6e646a44f242259cf8d920b12b3e 100644 (file)
 /** Check if \a x is an integer power of 2. */
 #define IS_POW2(x)     (!(bool)((x) & ((x)-1)))
 
+/** Check if \a x is aligned to \a byte_count bytes */
+#define IS_ALIGNED(x, byte_count) ((uintptr_t)(const void *)(x) % (byte_count) == 0)
+
 /** Calculate a compile-time log2 for a uint8_t */
 #define UINT8_LOG2(x) \
        ((x) < 2 ? 0 : \
@@ -406,7 +409,59 @@ INLINE bool is_aligned(const void *addr, size_t size)
        return ((size_t)addr & (size - 1)) == 0;
 }
 
+/**
+ * Convert one 32bit bcd numbert to int.
+ */
+#define BCD_TO_INT_32BIT(bcd)  \
+       ((uint32_t )((bcd) & 0xf) * 1 +  \
+       (((bcd) >> 4) & 0xf)  * 10 +      \
+       (((bcd) >> 8) & 0xf)  * 100 +     \
+       (((bcd) >> 12) & 0xf) * 1000 +   \
+       (((bcd) >> 16) & 0xf) * 10000 +   \
+       (((bcd) >> 20) & 0xf) * 100000 +  \
+       (((bcd) >> 24) & 0xf) * 1000000 + \
+       (((bcd) >> 28) & 0xf) * 10000000) \
+
+/**
+ * Extract chunk of bit from gived array (uint32_t type).
+ * \param resp array of bit 32bit aligned
+ * \param start bit position in array
+ * \param size of bit chuck from start
+ * \return uint32_t chunk value.
+ */
+#define UNSTUFF_BITS(resp, start, size)                   \
+    ({                              \
+        const uint32_t __size = size;                \
+        const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
+        const uint32_t __off = 3 - ((start) / 32);           \
+        const uint32_t __shft = (start) & 31;            \
+        uint32_t __res;                      \
+                                    \
+        __res = resp[__off] >> __shft;              \
+        if (__size + __shft > 32)               \
+            __res |= resp[__off-1] << ((32 - __shft) % 32); \
+        __res & __mask;                     \
+    })
+
+
 /** \} */ //defgroup macros
 
+
+/**
+ * Macro to unpack the ip addres from lwip format in 4 int
+ * \param addr is struct ip_addr ip_addr;
+ * \return for int variable separated from comma
+ *
+ * \code
+ * //Upack into 4 int the lwip ip address
+ * LOG_INFO("dhcp ok: ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(netif.ip_addr.addr));
+ * \endcode
+ */
+#define IP_ADDR_TO_INT_TUPLE(addr) \
+               (int)((addr) >>  0 & 0xff), \
+               (int)((addr) >>  8 & 0xff), \
+               (int)((addr) >> 16 & 0xff), \
+               (int)((addr) >> 24 & 0xff)
+
 #endif /* MACROS_H */