1 /***************************************************************************/ /** 2 * @file sl_constants.h 3 ******************************************************************************* 4 * # License 5 * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b> 6 ******************************************************************************* 7 * 8 * SPDX-License-Identifier: Zlib 9 * 10 * The licensor of this software is Silicon Laboratories Inc. 11 * 12 * This software is provided 'as-is', without any express or implied 13 * warranty. In no event will the authors be held liable for any damages 14 * arising from the use of this software. 15 * 16 * Permission is granted to anyone to use this software for any purpose, 17 * including commercial applications, and to alter it and redistribute it 18 * freely, subject to the following restrictions: 19 * 20 * 1. The origin of this software must not be misrepresented; you must not 21 * claim that you wrote the original software. If you use this software 22 * in a product, an acknowledgment in the product documentation would be 23 * appreciated but is not required. 24 * 2. Altered source versions must be plainly marked as such, and must not be 25 * misrepresented as being the original software. 26 * 3. This notice may not be removed or altered from any source distribution. 27 * 28 ******************************************************************************/ 29 30 #pragma once 31 32 #include "sl_additional_status.h" 33 #include <stdint.h> 34 #include <stdlib.h> 35 #include <stdarg.h> 36 #include <stdio.h> 37 38 #define SL_STATUS_ENUM(prefix, name, value) prefix##_##name = (prefix##_ENUM_OFFSET + value) 39 #define SL_STATUS_SHARED_ENUM(prefix, name) prefix##_##name = (SL_##name) 40 41 #ifdef __CC_ARM 42 #define BREAKPOINT() __asm__("bkpt #0") 43 #else 44 #define BREAKPOINT() __asm__("bkpt") 45 #endif 46 47 #define SL_IPV4_ADDRESS_LENGTH 4 48 #define SL_IPV6_ADDRESS_LENGTH 16 49 50 #ifndef UNUSED_VARIABLE 51 #define UNUSED_VARIABLE(x) (void)(x) 52 #endif // UNUSED_VARIABLE 53 #ifndef UNUSED_PARAMETER 54 #define UNUSED_PARAMETER(x) (void)(x) 55 #endif // UNUSED_PARAMETER 56 57 #define ARRAY_COUNT(x) (sizeof(x) / sizeof *(x)) 58 59 #ifndef FUZZING 60 #define SL_ASSERT(condition, ...) \ 61 do { \ 62 if (!(condition)) { \ 63 BREAKPOINT(); \ 64 } \ 65 } while (0) 66 #else 67 #define SL_ASSERT(condition, ...) \ 68 do { \ 69 if (!(condition)) { \ 70 } \ 71 } while (0) 72 #endif 73 74 #define SL_WAIT_FOREVER 0xFFFFFFFF 75 #define SL_INVALID_POINTER ((void *)0xEFFFFFFF) // This can point to any location that will trigger an exception 76 77 // Defines for log tags 78 #define ERROR_TAG "ERROR" 79 #define WARNING_TAG "WARNING" 80 #define DEBUG_TAG "DEBUG" 81 #define INFO_TAG "INFO" 82 83 // Defines for error logging 84 #define PRINT_ERROR_LOGS 0 85 86 #define PRINT_STATUS(tag, status) printf("\r\n%s %s:%d: 0x%lu \r\n", tag, __FILE__, __LINE__, (unsigned long)status); 87 88 #define SL_CHECK_STATUS(x) \ 89 do { \ 90 if (x != SL_SUCCESS) { \ 91 goto status_check_fail; \ 92 } \ 93 } while (0) 94 95 #define SL_CHECK_STATUS_AND_LOG(x, log) \ 96 do { \ 97 if (x != SL_SUCCESS) { \ 98 SL_LOG(log); \ 99 goto status_check_fail; \ 100 } \ 101 } while (0) 102 103 // Macros to help work with pointers / allocations 104 #define SL_VERIFY_POINTER_OR_EXIT(pointer) \ 105 do { \ 106 if ((pointer) == NULL) { \ 107 goto exit; \ 108 } \ 109 } while (0) 110 111 #define SL_VERIFY_POINTER_OR_GOTO(pointer, label) \ 112 do { \ 113 if ((pointer) == NULL) { \ 114 goto label; \ 115 } \ 116 } while (0) 117 118 #define SL_VERIFY_POINTER_OR_RETURN(pointer, status) \ 119 do { \ 120 if ((pointer) == NULL) { \ 121 if (PRINT_ERROR_LOGS) { \ 122 PRINT_STATUS(ERROR_TAG, status) \ 123 } \ 124 return (status); \ 125 } \ 126 } while (0) 127 128 #define SL_VERIFY_SUCCESS_OR_CONTINUE(x) \ 129 do { \ 130 if (x != SL_STATUS_OK) { \ 131 continue; \ 132 } \ 133 } while (0) 134 135 #define SL_VERIFY_SUCCESS_OR_EXIT(x) \ 136 do { \ 137 if (x != SL_STATUS_OK) { \ 138 goto exit; \ 139 } \ 140 } while (0) 141 142 #define SL_VERIFY_SUCCESS_OR_RETURN(x) \ 143 do { \ 144 if ((x) != SL_STATUS_OK) { \ 145 return (x); \ 146 } \ 147 } while (0) 148 149 #define SL_CLEANUP_MALLOC(pointer) \ 150 do { \ 151 if ((pointer) != NULL) { \ 152 free(pointer); \ 153 pointer = NULL; \ 154 } \ 155 } while (0) 156 157 #define SL_VERIFY_PARAMETER(condition) \ 158 do { \ 159 if (!(condition)) { \ 160 return SL_BAD_ARG; \ 161 } \ 162 } while (0) 163 164 #define VERIFY_STATUS_AND_RETURN(status) \ 165 do { \ 166 if (status != SL_STATUS_OK) { \ 167 if (PRINT_ERROR_LOGS) { \ 168 PRINT_STATUS(ERROR_TAG, status) \ 169 } \ 170 return status; \ 171 } \ 172 } while (0) 173 174 #define VERIFY_STATUS_AND_GOTO(status, goto_label) \ 175 do { \ 176 if (status != SL_STATUS_OK) { \ 177 if (PRINT_ERROR_LOGS) { \ 178 PRINT_STATUS(ERROR_TAG, status) \ 179 } \ 180 goto goto_label; \ 181 } \ 182 } while (0) 183 184 #define PRINT_ERROR_STATUS(tag, status) printf("\r\n%s %s:%d: 0x%x \r\n", tag, __FILE__, __LINE__, (unsigned int)status) 185 186 #ifdef PRINT_DEBUG_LOG 187 extern void sl_debug_log(const char *format, ...); 188 #define SL_DEBUG_LOG(format, ...) \ 189 do { \ 190 sl_debug_log("%s:%s:%d:" format "\r\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__); \ 191 } while (0) 192 #else 193 extern void sl_redirect_log(const char *format, ...); 194 #define SL_DEBUG_LOG(format, ...) \ 195 do { \ 196 sl_redirect_log("%s:%s:%d:" format "\r\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__); \ 197 } while (0) 198 #endif 199 200 #define SL_COMPILE_TIME_ASSERT(condition, comment) typedef char assertion_failed__##comment[2 * !!(condition)-1]; 201 202 typedef uint32_t sl_duration_t; 203 204 typedef void (*sl_event_handler_t)(void); 205