Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | #ifndef _BCMOLT_CONV_H_ |
| 31 | #define _BCMOLT_CONV_H_ |
| 32 | |
| 33 | #ifndef CFE_BUILD |
| 34 | #include <bcmolt_utils.h> |
| 35 | #endif |
| 36 | |
| 37 | /* Macro for generating a generic conversion from type A to type B. |
| 38 | * Example: |
| 39 | * |
| 40 | * BCMOLT_TYPE2TYPE(bws_dba_control_id, bws_dba_control_cb, static) |
| 41 | * |
| 42 | * will expand to: |
| 43 | * |
| 44 | * typedef struct |
| 45 | * { |
| 46 | * bws_dba_control_id from; |
| 47 | * bws_dba_control_cb to; |
| 48 | * } |
| 49 | * bws_dba_control_id2bws_dba_control_cb_t; |
| 50 | * |
| 51 | * static bws_dba_control_id2bws_dba_control_cb_t bws_dba_control_id2bws_dba_control_cb[]; |
| 52 | * |
| 53 | * static inline bws_dba_control_cb bws_dba_control_id2bws_dba_control_cb_conv(bws_dba_control_id from) |
| 54 | * { |
| 55 | * const bws_dba_control_id2bws_dba_control_cb_t *arr = bws_dba_control_id2bws_dba_control_cb; |
| 56 | * for (; arr->from != (bws_dba_control_id)-1 && arr->from != from; arr++); |
| 57 | * return arr->to; |
| 58 | * } |
| 59 | */ |
| 60 | #define BCMOLT_TYPE2TYPE(from_type, to_type, scope) \ |
| 61 | typedef struct \ |
| 62 | { \ |
| 63 | from_type from; \ |
| 64 | to_type to; \ |
| 65 | } \ |
| 66 | from_type##2##to_type##_t; \ |
| 67 | scope from_type##2##to_type##_t from_type##2##to_type[]; \ |
| 68 | static inline to_type from_type##2##to_type##_conv(from_type from) \ |
| 69 | { \ |
| 70 | const from_type##2##to_type##_t *arr = from_type##2##to_type; \ |
| 71 | for (; arr->from != (from_type)-1 && arr->from != from; arr++); \ |
| 72 | return arr->to; \ |
| 73 | } |
| 74 | |
| 75 | /* Macro for generating a generic conversion from type A to a constant string. |
| 76 | * Example: |
| 77 | * |
| 78 | * BCMOLT_TYPE2STR(pon_mode, extern) |
| 79 | * |
| 80 | * will expand to: |
| 81 | * |
| 82 | * typedef struct |
| 83 | * { |
| 84 | * pon_mode from; |
| 85 | * const char *to; |
| 86 | * } |
| 87 | * pon_mode2str_t; |
| 88 | * |
| 89 | * extern pon_mode2str_t pon_mode2str[]; |
| 90 | * |
| 91 | * static inline const char *pon_mode2str_conv(pon_mode from) |
| 92 | * { |
| 93 | * const pon_mode2str_t *arr = pon_mode2str; |
| 94 | * for (; arr->from != (pon_mode)-1 && arr->from != from; arr++); |
| 95 | * return arr->to; |
| 96 | * } |
| 97 | */ |
| 98 | #define BCMOLT_TYPE2STR(from_type, scope) \ |
| 99 | typedef struct \ |
| 100 | { \ |
| 101 | from_type from; \ |
| 102 | const char *to; \ |
| 103 | } \ |
| 104 | from_type##2str_t; \ |
| 105 | scope from_type##2str_t from_type##2str[]; \ |
| 106 | static inline const char *from_type##2str_conv(from_type from) \ |
| 107 | { \ |
| 108 | const from_type##2str_t *arr = from_type##2str; \ |
| 109 | for (; arr->from != (from_type)-1 && arr->from != from; arr++); \ |
| 110 | return arr->to; \ |
| 111 | } |
| 112 | |
| 113 | /* Macro for generating a generic conversion from type A to an integer. |
| 114 | * Example: |
| 115 | * |
| 116 | * BCMOLT_TYPE2INT(ploam_ds_gpon_message_id, repetitions, extern) |
| 117 | * |
| 118 | * will expand to: |
| 119 | * |
| 120 | * typedef struct |
| 121 | * { |
| 122 | * ploam_ds_gpon_message_id from; |
| 123 | * int to; |
| 124 | * } |
| 125 | * ploam_ds_gpon_message_id2repetitions_t; |
| 126 | * |
| 127 | * extern ploam_ds_gpon_message_id2repetitions_t ploam_ds_gpon_message_id2repetitions[]; |
| 128 | * |
| 129 | * static inline int ploam_ds_gpon_message_id2repetitions_conv(ploam_ds_gpon_message_id from) |
| 130 | * { |
| 131 | * const ploam_ds_gpon_message_id2repetitions_t *arr = ploam_ds_gpon_message_id2repetitions; |
| 132 | * for (; arr->from != (ploam_ds_gpon_message_id)-1 && arr->from != from; arr++); |
| 133 | * return arr->to; |
| 134 | * } |
| 135 | */ |
| 136 | #define BCMOLT_TYPE2INT(from_type, to_name, scope) \ |
| 137 | typedef struct \ |
| 138 | { \ |
| 139 | from_type from; \ |
| 140 | int to; \ |
| 141 | } \ |
| 142 | from_type##2##to_name##_t; \ |
| 143 | scope from_type##2##to_name##_t from_type##2##to_name[]; \ |
| 144 | static inline int from_type##2##to_name##_conv(from_type from) \ |
| 145 | { \ |
| 146 | const from_type##2##to_name##_t *arr = from_type##2##to_name; \ |
| 147 | for (; arr->from != (from_type)-1 && arr->from != from; arr++); \ |
| 148 | return arr->to; \ |
| 149 | } |
| 150 | |
| 151 | /* Although we have BCMOLT_TYPE2STR, int2str_t is still required when the same generic pointer needs to point to 2 different types (e.g: one specific for GPON and |
| 152 | * the other specific for XGPON). */ |
| 153 | typedef struct |
| 154 | { |
| 155 | int from; |
| 156 | const char *to; |
| 157 | } |
| 158 | int2str_t; |
| 159 | |
| 160 | static inline const char *int2str(const int2str_t *arr, int from) |
| 161 | { |
| 162 | for (; arr->from != -1 && arr->from != from; arr++); |
| 163 | return arr->to; |
| 164 | } |
| 165 | |
| 166 | /* Although we have BCMOLT_TYPE2INT, int2int_t is still required when the same generic pointer needs to point to 2 different types (e.g: one specific for GPON and |
| 167 | * the other specific for XGPON). */ |
| 168 | typedef struct |
| 169 | { |
| 170 | int from; |
| 171 | int to; |
| 172 | } |
| 173 | int2int_t; |
| 174 | |
| 175 | static inline int int2int(const int2int_t *arr, int from) |
| 176 | { |
| 177 | for (; arr->from != -1 && arr->from != from; arr++); |
| 178 | return arr->to; |
| 179 | } |
| 180 | |
| 181 | #ifndef CFE_BUILD |
| 182 | char *bcmolt_strftime(char *time_str, time_t t, const char *timezone_str); |
| 183 | #endif |
| 184 | |
| 185 | #endif |
| 186 | |