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 | |
| 31 | /******************************************************************* |
| 32 | * bcmcli.h |
| 33 | * |
| 34 | * CLI engine |
| 35 | * |
| 36 | *******************************************************************/ |
| 37 | |
| 38 | #ifndef BCMCLI_H |
| 39 | |
| 40 | #define BCMCLI_H |
| 41 | |
| 42 | #include <bcmos_system.h> |
| 43 | #include <bcmcli_session.h> |
| 44 | #include <bcmolt_buf.h> |
| 45 | |
| 46 | /** \defgroup bcm_cli Broadcom CLI Engine |
| 47 | * Broadcom CLI engine is used for all configuration and status monitoring.\n |
| 48 | * It doesn't have built-in scripting capabilities (logical expressions, loops), |
| 49 | * but can be used in combination with any available scripting language.\n |
| 50 | * Broadcom CLI supports the following features:\n |
| 51 | * - parameter number and type validation (simplifies command handlers development) |
| 52 | * - parameter value range checking |
| 53 | * - mandatory and optional parameters |
| 54 | * - positional and named parameters |
| 55 | * - parameters with default values |
| 56 | * - enum parameters can have arbitrary values |
| 57 | * - automatic command help generation |
| 58 | * - automatic or user-defined command shortcuts |
| 59 | * - command handlers return completion status to enable scripting |
| 60 | * - multiple sessions |
| 61 | * - session access rights |
| 62 | * - extendible. Supports user-defined parameter types |
| 63 | * - relatively low stack usage |
| 64 | * @{ |
| 65 | */ |
| 66 | |
| 67 | #ifdef __cplusplus |
| 68 | extern "C" { |
| 69 | #endif /* __cplusplus */ |
| 70 | |
| 71 | #define BCMCLI_MAX_SEARCH_SUBSTR_LENGTH 80 |
| 72 | |
| 73 | #define BCMCLI_ARRAY_EMPTY "-" |
| 74 | #define BCMCLI_PARM_NO_VALUE "_" |
| 75 | #define BCMCLI_ENUM_MASK_DEL_STR "+" |
| 76 | |
| 77 | /** Monitor entry handle |
| 78 | */ |
| 79 | typedef struct bcmcli_entry bcmcli_entry; |
| 80 | |
| 81 | /* if BCMCLI_PARM_USERIO flag is set: |
| 82 | low_val: t_userscanf_f function |
| 83 | high_val: t_userprintf_f function |
| 84 | */ |
| 85 | |
| 86 | /** Function parameter structure */ |
| 87 | typedef struct bcmcli_cmd_parm bcmcli_cmd_parm; |
| 88 | |
| 89 | /** Parameter type */ |
| 90 | typedef enum |
| 91 | { |
| 92 | BCMCLI_PARM_NONE, |
| 93 | BCMCLI_PARM_DECIMAL, /**< Decimal number */ |
| 94 | BCMCLI_PARM_DECIMAL64, /**< Signed 64-bit decimal */ |
| 95 | BCMCLI_PARM_UDECIMAL, /**< Unsigned decimal number */ |
| 96 | BCMCLI_PARM_UDECIMAL64, /**< Unsigned 64-bit decimal number */ |
| 97 | BCMCLI_PARM_HEX, /**< Hexadecimal number */ |
| 98 | BCMCLI_PARM_HEX64, /**< 64-bit hexadecimal number */ |
| 99 | BCMCLI_PARM_NUMBER, /**< Decimal number or hex number prefixed by 0x */ |
| 100 | BCMCLI_PARM_NUMBER64, /**< 64bit decimal number or hex number prefixed by 0x */ |
| 101 | BCMCLI_PARM_UNUMBER, /**< Unsigned decimal number or hex number prefixed by 0x */ |
| 102 | BCMCLI_PARM_UNUMBER64, /**< Unsigned 64bit decimal number or hex number prefixed by 0x */ |
| 103 | BCMCLI_PARM_FLOAT, /**< IEEE 32-bit floating-point number */ |
| 104 | BCMCLI_PARM_DOUBLE, /**< IEEE 64-bit floating point number */ |
| 105 | BCMCLI_PARM_STRING, /**< String */ |
| 106 | BCMCLI_PARM_ENUM, /**< Enumeration */ |
| 107 | BCMCLI_PARM_ENUM_MASK, /**< Bitmask created from enumeration values */ |
| 108 | BCMCLI_PARM_IP, /**< IP address n.n.n.n */ |
| 109 | BCMCLI_PARM_IPV6, /**< IPv6 address */ |
| 110 | BCMCLI_PARM_MAC, /**< MAC address xx:xx:xx:xx:xx:xx */ |
| 111 | BCMCLI_PARM_BUFFER, /**< Byte array */ |
| 112 | |
| 113 | BCMCLI_PARM_USERDEF /**< User-defined parameter. User must provide scan_cb */ |
| 114 | } bcmcli_parm_type; |
| 115 | |
| 116 | /** Numeric type used for storing enum values. */ |
| 117 | typedef long bcmcli_enum_number; |
| 118 | |
| 119 | /** Enum attribute value. |
| 120 | * |
| 121 | * Enum values is an array of bcmcli_enum_val terminated by element with name==NULL |
| 122 | * |
| 123 | */ |
| 124 | typedef struct bcmcli_enum_val |
| 125 | { |
| 126 | const char *name; /**< Enum symbolic name */ |
| 127 | bcmcli_enum_number val; /**< Enum internal value */ |
| 128 | bcmcli_cmd_parm *parms; /**< Extension parameter table for enum-selector */ |
| 129 | } bcmcli_enum_val; |
| 130 | #define BCMCLI_MAX_ENUM_VALUES 128 /**< Max number of enum values */ |
| 131 | #define BCMCLI_ENUM_LAST { NULL, 0} /**< Last entry in enum table */ |
| 132 | |
| 133 | /** Boolean values (true/false, yes/no, on/off) |
| 134 | * |
| 135 | */ |
| 136 | extern bcmcli_enum_val bcmcli_enum_bool_table[]; |
| 137 | |
| 138 | /* Monitor data types */ |
| 139 | typedef long bcmcli_number; /**< Type underlying BCMCLI_PARM_NUMBER, BCMCLI_PARM_DECIMAL */ |
| 140 | typedef long bcmcli_unumber; /**< Type underlying BCMCLI_PARM_HEX, BCMCLI_PARM_UDECIMAL */ |
| 141 | typedef long bcmcli_number64; /**< Type underlying BCMCLI_PARM_NUMBER64, BCMCLI_PARM_DECIMAL64 */ |
| 142 | typedef long bcmcli_unumber64; /**< Type underlying BCMCLI_PARM_HEX64, BCMCLI_PARM_UDECIMAL64 */ |
| 143 | |
| 144 | /** Parameter value */ |
| 145 | typedef union bcmcli_parm_value |
| 146 | { |
| 147 | long number; /**< Signed number */ |
| 148 | unsigned long unumber; /**< Unsigned number */ |
| 149 | long long number64; /**< Signed 64-bit number */ |
| 150 | unsigned long long unumber64; /**< Unsigned 64-bit number */ |
| 151 | const char *string; /**< 0-terminated string */ |
| 152 | double d; /**< Double-precision floating point number */ |
| 153 | bcmos_mac_address mac; /**< MAC address */ |
| 154 | bcmolt_buf buffer; /**< Buffer: used for BCMCLI_PARM_BUFFER */ |
| 155 | bcmcli_enum_number enum_val; /**< Enum value (number) */ |
| 156 | } bcmcli_parm_value; |
| 157 | |
| 158 | /** User-defined scan function. |
| 159 | * The function is used for parsing user-defined parameter types |
| 160 | * Returns: 0-ok, <=error |
| 161 | * |
| 162 | */ |
| 163 | typedef bcmos_errno (*bcmcli_scan_cb)(const bcmcli_cmd_parm *parm, bcmcli_parm_value *value, |
| 164 | const char *string_val); |
| 165 | |
| 166 | /** User-defined print function. |
| 167 | * The function is used for printing user-defined parameter types |
| 168 | * |
| 169 | */ |
| 170 | typedef void (*bcmcli_format_cb)(const bcmcli_cmd_parm *parm, bcmcli_parm_value value, |
| 171 | char *buffer, int size); |
| 172 | |
| 173 | |
| 174 | /** Function parameter structure */ |
| 175 | struct bcmcli_cmd_parm |
| 176 | { |
| 177 | const char *name; /**< Parameter name. Shouldn't be allocated on stack! */ |
| 178 | const char *description; /**< Parameter description. Shouldn't be allocated on stack! */ |
| 179 | bcmcli_parm_type type; /**< Parameter type */ |
| 180 | uint8_t flags; /**< Combination of BCMCLI_PARM_xx flags */ |
| 181 | #define BCMCLI_PARM_FLAG_NONE 0x00 /**< For use instead of magic number 0 when no flags apply */ |
| 182 | #define BCMCLI_PARM_FLAG_OPTIONAL 0x01 /**< Parameter is optional */ |
| 183 | #define BCMCLI_PARM_FLAG_DEFVAL 0x02 /**< Default value is set */ |
| 184 | #define BCMCLI_PARM_FLAG_RANGE 0x04 /**< Range is set */ |
| 185 | #define BCMCLI_PARM_FLAG_EOL 0x20 /**< String from the current parser position till EOL */ |
| 186 | #define BCMCLI_PARM_FLAG_SELECTOR 0x40 /**< Parameter selects other parameters */ |
| 187 | #define BCMCLI_PARM_FLAG_ASSIGNED 0x80 /**< Internal flag: parameter is assigned */ |
| 188 | |
| 189 | bcmcli_number low_val; /**< Low val for range checking */ |
| 190 | bcmcli_number hi_val; /**< Hi val for range checking */ |
| 191 | bcmcli_parm_value value; /**< Value */ |
| 192 | bcmcli_enum_val *enum_table; /**< Table containing { enum_name, enum_value } pairs */ |
| 193 | bcmcli_scan_cb scan_cb; /**< User-defined scan function for BCMCLI_PARM_USERDEF parameter type */ |
| 194 | bcmcli_format_cb format_cb; /**< User-defined format function for BCMCLI_PARM_USERDEF parameter type */ |
| 195 | uint32_t max_array_size; /**< Max array size for array-parameter */ |
| 196 | uint32_t array_size; /**< Actual array size for array-parameter */ |
| 197 | bcmcli_parm_value *values; /**< Array values */ |
| 198 | void *user_data; /**< User data - passed transparently to command handler */ |
| 199 | }; |
| 200 | |
| 201 | /** Command parameter list terminator */ |
| 202 | #define BCMCLI_PARM_LIST_TERMINATOR { .name=NULL, .type=BCMCLI_PARM_NONE } |
| 203 | |
| 204 | /** Helper macro: make simple parameter |
| 205 | * \param[in] _name Parameter name |
| 206 | * \param[in] _descr Parameter description |
| 207 | * \param[in] _type Parameter type |
| 208 | * \param[in] _flags Parameter flags |
| 209 | */ |
| 210 | #define BCMCLI_MAKE_PARM(_name, _descr, _type, _flags) \ |
| 211 | { .name=(_name), .description=(_descr), .type=(_type), .flags=(_flags) } |
| 212 | |
| 213 | /** Helper macro: make simple parameter |
| 214 | * \param[in] _name Parameter name |
| 215 | * \param[in] _descr Parameter description |
| 216 | * \param[in] _type Parameter type |
| 217 | * \param[in] _flags Parameter flags |
| 218 | * \param[in] _size Max array size |
| 219 | * \param[in] _values Array values buffer |
| 220 | */ |
| 221 | #define BCMCLI_MAKE_PARM_ARRAY(_name, _descr, _type, _flags, _size, _values) \ |
| 222 | { .name=(_name), .description=(_descr), .type=(_type), .flags=(_flags),\ |
| 223 | .max_array_size=(_size), .values=(_values) } |
| 224 | |
| 225 | /** Helper macro: make simple parameter for arrays of enums |
| 226 | * \param[in] _name Parameter name |
| 227 | * \param[in] _descr Parameter description |
| 228 | * \param[in] _type Parameter type |
| 229 | * \param[in] _flags Parameter flags |
| 230 | * \param[in] _size Max array size |
| 231 | * \param[in] _values Array values buffer |
| 232 | * \param[in] _enum_table An array of enums that may be in the array |
| 233 | */ |
| 234 | #define BCMCLI_MAKE_PARM_ENUM_ARRAY(_name, _descr, _type, _flags, _size, _values, _enum_table) \ |
| 235 | { .name=(_name), .description=(_descr), .type=(_type), .flags=(_flags),\ |
| 236 | .max_array_size=(_size), .values=(_values), .enum_table=(_enum_table) } |
| 237 | |
| 238 | /** Helper macro: make range parameter |
| 239 | * \param[in] _name Parameter name |
| 240 | * \param[in] _descr Parameter description |
| 241 | * \param[in] _type Parameter type |
| 242 | * \param[in] _flags Parameter flags |
| 243 | * \param[in] _min Min value |
| 244 | * \param[in] _max Max value |
| 245 | */ |
| 246 | #define BCMCLI_MAKE_PARM_RANGE(_name, _descr, _type, _flags, _min, _max) \ |
| 247 | { .name=(_name), .description=(_descr), .type=(_type), .flags=(_flags) | BCMCLI_PARM_FLAG_RANGE, \ |
| 248 | .low_val=(_min), .hi_val=(_max) } |
| 249 | |
| 250 | /** Helper macro: make range parameter for arrays with range |
| 251 | * \param[in] _name Parameter name |
| 252 | * \param[in] _descr Parameter description |
| 253 | * \param[in] _type Parameter type |
| 254 | * \param[in] _flags Parameter flags |
| 255 | * \param[in] _size Max array size |
| 256 | * \param[in] _values Array values buffer |
| 257 | * \param[in] _min Min value |
| 258 | * \param[in] _max Max value |
| 259 | */ |
| 260 | #define BCMCLI_MAKE_PARM_ARRAY_RANGE(_name, _descr, _type, _flags, _size, _values, _min, _max) \ |
| 261 | { .name=(_name), .description=(_descr), .type=(_type), .flags=(_flags) | BCMCLI_PARM_FLAG_RANGE,\ |
| 262 | .max_array_size=(_size), .values=(_values), .low_val=(_min), .hi_val=(_max) } |
| 263 | |
| 264 | /** Helper macro: make parameter with default value |
| 265 | * \param[in] _name Parameter name |
| 266 | * \param[in] _descr Parameter description |
| 267 | * \param[in] _type Parameter type |
| 268 | * \param[in] _flags Parameter flags |
| 269 | * \param[in] _dft Default value |
| 270 | */ |
| 271 | #define BCMCLI_MAKE_PARM_DEFVAL(_name, _descr, _type, _flags, _dft) \ |
| 272 | { .name=(_name), .description=(_descr), .type=(_type), .flags=(_flags) | BCMCLI_PARM_FLAG_DEFVAL, \ |
| 273 | .value = {_dft} } |
| 274 | |
| 275 | /** Helper macro: make range parameter with default value |
| 276 | * \param[in] _name Parameter name |
| 277 | * \param[in] _descr Parameter description |
| 278 | * \param[in] _type Parameter type |
| 279 | * \param[in] _flags Parameter flags |
| 280 | * \param[in] _min Min value |
| 281 | * \param[in] _max Max value |
| 282 | * \param[in] _dft Default value |
| 283 | */ |
| 284 | #define BCMCLI_MAKE_PARM_RANGE_DEFVAL(_name, _descr, _type, _flags, _min, _max, _dft) \ |
| 285 | { .name=(_name), .description=(_descr), .type=(_type), \ |
| 286 | .flags=(_flags) | BCMCLI_PARM_FLAG_RANGE | BCMCLI_PARM_FLAG_DEFVAL, \ |
| 287 | .low_val=(_min), .hi_val=(_max), .value = {_dft} } |
| 288 | |
| 289 | /** Helper macro: make enum parameter |
| 290 | * \param[in] _name Parameter name |
| 291 | * \param[in] _descr Parameter description |
| 292 | * \param[in] _values Enum values table |
| 293 | * \param[in] _flags Parameter flags |
| 294 | */ |
| 295 | #define BCMCLI_MAKE_PARM_ENUM(_name, _descr, _values, _flags) \ |
| 296 | { .name=(_name), .description=(_descr), .type=BCMCLI_PARM_ENUM, .flags=(_flags), .enum_table=(_values)} |
| 297 | |
| 298 | /** Helper macro: make enum parameter with default value |
| 299 | * \param[in] _name Parameter name |
| 300 | * \param[in] _descr Parameter description |
| 301 | * \param[in] _values Enum values table |
| 302 | * \param[in] _flags Parameter flags |
| 303 | * \param[in] _dft Default value |
| 304 | */ |
| 305 | #define BCMCLI_MAKE_PARM_ENUM_DEFVAL(_name, _descr, _values, _flags, _dft) \ |
| 306 | { .name=(_name), .description=(_descr), .type=BCMCLI_PARM_ENUM, .flags=(_flags) | BCMCLI_PARM_FLAG_DEFVAL,\ |
| 307 | .enum_table=(_values), .value={.string=_dft} } |
| 308 | |
| 309 | /** Helper macro: make enum mask parameter |
| 310 | * \param[in] _name Parameter name |
| 311 | * \param[in] _descr Parameter description |
| 312 | * \param[in] _values Enum values table |
| 313 | * \param[in] _flags Parameter flags |
| 314 | */ |
| 315 | #define BCMCLI_MAKE_PARM_ENUM_MASK(_name, _descr, _values, _flags) \ |
| 316 | { .name=(_name), .description=(_descr), .type=BCMCLI_PARM_ENUM_MASK, .flags=(_flags), .enum_table=(_values)} |
| 317 | |
| 318 | /** Helper macro: make enum_mask parameter with default value |
| 319 | * \param[in] _name Parameter name |
| 320 | * \param[in] _descr Parameter description |
| 321 | * \param[in] _values Enum values table |
| 322 | * \param[in] _flags Parameter flags |
| 323 | * \param[in] _dft Default value |
| 324 | */ |
| 325 | #define BCMCLI_MAKE_PARM_ENUM_MASK_DEFVAL(_name, _descr, _values, _flags, _dft) \ |
| 326 | { .name=(_name), .description=(_descr), .type=BCMCLI_PARM_ENUM_MASK, .flags=(_flags) | BCMCLI_PARM_FLAG_DEFVAL,\ |
| 327 | .enum_table=(_values), .value={.string=_dft} } |
| 328 | |
| 329 | /** Helper macro: make enum-selector parameter |
| 330 | * \param[in] _name Parameter name |
| 331 | * \param[in] _descr Parameter description |
| 332 | * \param[in] _values Selector values table |
| 333 | * \param[in] _flags Parameter flags |
| 334 | */ |
| 335 | #define BCMCLI_MAKE_PARM_SELECTOR(_name, _descr, _values, _flags) \ |
| 336 | { .name=(_name), .description=(_descr), .type=BCMCLI_PARM_ENUM, .flags=(_flags) | BCMCLI_PARM_FLAG_SELECTOR,\ |
| 337 | .enum_table=(_values) } |
| 338 | |
| 339 | /** Helper macro: make buffer parameter |
| 340 | * \param[in] _name Parameter name |
| 341 | * \param[in] _descr Parameter description |
| 342 | * \param[in] _flags Parameter flags |
| 343 | * \param[in] _buf Memory buffer associated with the parameter |
| 344 | * \param[in] _size Buffer size |
| 345 | */ |
| 346 | #define BCMCLI_MAKE_PARM_BUFFER(_name, _descr, _flags, _buf, _size) \ |
| 347 | { .name=(_name), .description=(_descr), .type=BCMCLI_PARM_BUFFER, \ |
| 348 | .flags=(_flags), .value.buffer = {.start = _buf, .curr = _buf, .len = _size} } |
| 349 | |
| 350 | /** Register command without parameters helper */ |
| 351 | #define BCMCLI_MAKE_CMD_NOPARM(dir, cmd, help, cb) \ |
| 352 | {\ |
| 353 | bcmos_errno bcmcli_cmd_add_err = bcmcli_cmd_add(dir, cmd, cb, help, BCMCLI_ACCESS_ADMIN, NULL, NULL);\ |
| 354 | BUG_ON(BCM_ERR_OK != bcmcli_cmd_add_err);\ |
| 355 | } |
| 356 | |
| 357 | /** Register command helper */ |
| 358 | #define BCMCLI_MAKE_CMD(dir, cmd, help, cb, parms...) \ |
| 359 | { \ |
| 360 | static bcmcli_cmd_parm cmd_parms[]={ \ |
| 361 | parms, \ |
| 362 | BCMCLI_PARM_LIST_TERMINATOR \ |
| 363 | }; \ |
| 364 | bcmos_errno bcmcli_cmd_add_err = bcmcli_cmd_add(dir, cmd, cb, help, BCMCLI_ACCESS_ADMIN, NULL, cmd_parms); \ |
| 365 | BUG_ON(BCM_ERR_OK != bcmcli_cmd_add_err);\ |
| 366 | } |
| 367 | |
| 368 | /** Optional custom directory handlers */ |
| 369 | typedef void (*bcmcli_dir_enter_leave_cb)(bcmcli_session *session, bcmcli_entry *dir, int is_enter); |
| 370 | |
| 371 | /** Optional command or directory help callback |
| 372 | * \param[in] session Session handle |
| 373 | * \param[in] h Command or directory handle |
| 374 | * \param[in] parms Parameter(s) - the rest of the command string. |
| 375 | * Can be used for example to get help on individual parameters |
| 376 | */ |
| 377 | typedef void (*bcmcli_help_cb)(bcmcli_session *session, bcmcli_entry *h, const char *parms); |
| 378 | |
| 379 | |
| 380 | /** Extra parameters of monitor directory. |
| 381 | * See \ref bcmcli_dir_add |
| 382 | * |
| 383 | */ |
| 384 | typedef struct bcmcli_dir_extra_parm |
| 385 | { |
| 386 | void *user_priv; /**< private data passed to enter_leave_cb */ |
| 387 | bcmcli_dir_enter_leave_cb enter_leave_cb; /**< callback function to be called when session enters/leavs the directory */ |
| 388 | bcmcli_help_cb help_cb; /**< Help function called to print directory help instead of the automatic help */ |
| 389 | } bcmcli_dir_extra_parm; |
| 390 | |
| 391 | |
| 392 | /** Extra parameters of monitor command. |
| 393 | * See \ref bcmcli_cmd_add |
| 394 | * |
| 395 | */ |
| 396 | typedef struct bcmcli_cmd_extra_parm |
| 397 | { |
| 398 | bcmcli_help_cb help_cb; /**< Optional help callback. Can be used for more sophisticated help, e.g., help for specific parameters */ |
| 399 | uint32_t flags; /**< Command flags */ |
| 400 | #define BCMCLI_CMD_FLAG_NO_NAME_PARMS 0x00000001 /**< No named parms. Positional only. Can be useful if parameter value can contain ',' */ |
| 401 | void (*free_parms)(bcmcli_cmd_parm *parms); /* Optional user-defined free */ |
| 402 | } bcmcli_cmd_extra_parm; |
| 403 | |
| 404 | |
| 405 | /** Monitor command handler prototype */ |
| 406 | typedef bcmos_errno (*bcmcli_cmd_cb)(bcmcli_session *session, const bcmcli_cmd_parm parm[], uint16_t n_parms); |
| 407 | |
| 408 | /** CLI command logging mode */ |
| 409 | typedef enum |
| 410 | { |
| 411 | BCMCLI_LOG_NONE, /**< Disable logging */ |
| 412 | BCMCLI_LOG_CLI, /**< Log commands as is and rc as CLI comment*/ |
| 413 | BCMCLI_LOG_C_COMMENT /**< Log as C comments */ |
| 414 | } bcmcli_log_mode; |
| 415 | |
| 416 | /** Add subdirectory to the parent directory |
| 417 | * |
| 418 | * \param[in] parent Parent directory handle. NULL=root |
| 419 | * \param[in] name Directory name |
| 420 | * \param[in] help Help string |
| 421 | * \param[in] access_right Access rights |
| 422 | * \param[in] extras Optional directory descriptor. Mustn't be allocated on the stack. |
| 423 | * \return new directory handle or NULL in case of failure |
| 424 | */ |
| 425 | bcmcli_entry *bcmcli_dir_add(bcmcli_entry *parent, const char *name, |
| 426 | const char *help, bcmcli_access_right access_right, |
| 427 | const bcmcli_dir_extra_parm *extras); |
| 428 | |
| 429 | |
| 430 | /** Scan directory tree and look for directory named "name". |
| 431 | * |
| 432 | * \param[in] parent Directory sub-tree root. NULL=root |
| 433 | * \param[in] name Name of directory to be found |
| 434 | * \return directory handle if found or NULL if not found |
| 435 | */ |
| 436 | bcmcli_entry *bcmcli_dir_find(bcmcli_entry *parent, const char *name ); |
| 437 | |
| 438 | |
| 439 | /** Scan directory tree and look for command named "name". |
| 440 | * |
| 441 | * \param[in] parent Directory sub-tree root. NULL=root |
| 442 | * \param[in] name Name of command to be found |
| 443 | * \return command handle if found or NULL if not found |
| 444 | */ |
| 445 | bcmcli_entry *bcmcli_cmd_find(bcmcli_entry *parent, const char *name ); |
| 446 | |
| 447 | |
| 448 | /** Get token name |
| 449 | * \param[in] token Directory or command token |
| 450 | * \return directory token name |
| 451 | */ |
| 452 | const char *bcmcli_token_name(bcmcli_entry *token); |
| 453 | |
| 454 | /** Find the CLI parameter with the specified name (case insensitive). |
| 455 | * \param[in] session CLI session |
| 456 | * \param[in] name Parameter name |
| 457 | * \return The CLI parameter that was found, or NULL if not found |
| 458 | */ |
| 459 | bcmcli_cmd_parm *bcmcli_find_named_parm(bcmcli_session *session, const char *name); |
| 460 | |
| 461 | /** Find the first CLI parameter whose name starts with the specified string (case insensitive). |
| 462 | * \param[in] session CLI session |
| 463 | * \param[in] prefix Parameter name prefix |
| 464 | * \return The CLI parameter that was found, or NULL if not found |
| 465 | */ |
| 466 | bcmcli_cmd_parm *bcmcli_find_parm_by_prefix(bcmcli_session *session, const char *prefix); |
| 467 | |
| 468 | /** Query parameter value status. |
| 469 | * The function can be used for scalar and array parameters |
| 470 | * \param[in] session CLI session |
| 471 | * \param[in] parm Parameter from the array passed to the CLI command handler |
| 472 | * or returned by bcmcli_find_named_parm() |
| 473 | * \param[in] value_index value_index - for array parameters |
| 474 | * \return BCMOS_TRUE if the parameter value is set, BCMOS_FALSE otherwise |
| 475 | */ |
| 476 | bcmos_bool bcmcli_parm_value_is_set(bcmcli_session *session, bcmcli_cmd_parm *parm, uint32_t value_index); |
| 477 | |
| 478 | /** Add CLI command |
| 479 | * |
| 480 | * \param[in] dir Handle of directory to add command to. NULL=root |
| 481 | * \param[in] name Command name |
| 482 | * \param[in] cmd_cb Command handler |
| 483 | * \param[in] help Help string |
| 484 | * \param[in] access_right Access rights |
| 485 | * \param[in] extras Optional extras |
| 486 | * \param[in] parms Optional parameters array. Must not be allocated on the stack! |
| 487 | * If parms!=NULL, the last parameter in the array must have name==NULL. |
| 488 | * \return |
| 489 | * 0 =OK\n |
| 490 | * <0 =error code |
| 491 | */ |
| 492 | bcmos_errno bcmcli_cmd_add(bcmcli_entry *dir, const char *name, bcmcli_cmd_cb cmd_cb, |
| 493 | const char *help, bcmcli_access_right access_right, |
| 494 | const bcmcli_cmd_extra_parm *extras, bcmcli_cmd_parm parms[]); |
| 495 | |
| 496 | |
| 497 | /** Destroy token (command or directory) |
| 498 | * \param[in] token Directory or command token. NULL=root |
| 499 | */ |
| 500 | void bcmcli_token_destroy(bcmcli_entry *token); |
| 501 | |
| 502 | /** Parse and execute input string. |
| 503 | * input_string can contain multiple commands delimited by ';' |
| 504 | * |
| 505 | * \param[in] session Session handle |
| 506 | * \param[in] input_string String to be parsed |
| 507 | * \return |
| 508 | * =0 - OK \n |
| 509 | * -EINVAL - parsing error\n |
| 510 | * other - return code - as returned from command handler. |
| 511 | * It is recommended to return -EINTR to interrupt monitor loop. |
| 512 | */ |
| 513 | bcmos_errno bcmcli_parse(bcmcli_session *session, char *input_string); |
| 514 | |
| 515 | /** Read input and parse iteratively until EOF or bcmcli_is_stopped() |
| 516 | * |
| 517 | * \param[in] session Session handle |
| 518 | * \return |
| 519 | * =0 - OK \n |
| 520 | */ |
| 521 | bcmos_errno bcmcli_driver(bcmcli_session *session); |
| 522 | |
| 523 | /** Stop monitor driver. |
| 524 | * The function stops \ref bcmcli_driver |
| 525 | * \param[in] session Session handle |
| 526 | */ |
| 527 | void bcmcli_stop(bcmcli_session *session); |
| 528 | |
| 529 | /** Returns 1 if monitor session is stopped |
| 530 | * \param[in] session Session handle |
| 531 | * \returns 1 if monitor session stopped by bcmcli_stop()\n |
| 532 | * 0 otherwise |
| 533 | */ |
| 534 | bcmos_bool bcmcli_is_stopped(bcmcli_session *session); |
| 535 | |
| 536 | /** Get current directory for the session, |
| 537 | * \param[in] session Session handle |
| 538 | * \return The current directory handle |
| 539 | */ |
| 540 | bcmcli_entry *bcmcli_dir_get(bcmcli_session *session ); |
| 541 | |
| 542 | /** Set current directory for the session. |
| 543 | * \param[in] session Session handle |
| 544 | * \param[in] dir Directory that should become current |
| 545 | * \return |
| 546 | * =0 - OK |
| 547 | * <0 - error |
| 548 | */ |
| 549 | bcmos_errno bcmcli_dir_set(bcmcli_session *session, bcmcli_entry *dir); |
| 550 | |
| 551 | /** Get parameter number given its name. |
| 552 | * The function is intended for use by command handlers |
| 553 | * \param[in] session Session handle |
| 554 | * \param[in] parm_name Parameter name |
| 555 | * \return |
| 556 | * >=0 - parameter number\n |
| 557 | * <0 - parameter with this name doesn't exist |
| 558 | */ |
| 559 | int bcmcli_parm_number(bcmcli_session *session, const char *parm_name); |
| 560 | |
| 561 | /** Get parameter by name |
| 562 | * The function is intended for use by command handlers |
| 563 | * \param[in] session Session handle |
| 564 | * \param[in] parm_name Parameter name |
| 565 | * \return |
| 566 | * parameter pointer or NULL if not found |
| 567 | */ |
| 568 | bcmcli_cmd_parm *bcmcli_parm_get(bcmcli_session *session, const char *parm_name); |
| 569 | |
| 570 | /** Check if parameter is set |
| 571 | * The function is intended for use by command handlers |
| 572 | * \param[in] session Session handle |
| 573 | * \param[in] parm Parameter name |
| 574 | * \return |
| 575 | * TRUE if parameter is set, FALSE otherwise |
| 576 | */ |
| 577 | static inline bcmos_bool bcmcli_parm_is_set(bcmcli_session *session, const bcmcli_cmd_parm *parm) |
| 578 | { |
| 579 | return (parm->flags & BCMCLI_PARM_FLAG_ASSIGNED) ? BCMOS_TRUE : BCMOS_FALSE; |
| 580 | } |
| 581 | |
| 582 | /** Check if parameter is set |
| 583 | * \param[in] session Session handle |
| 584 | * \param[in] parm_number Parameter number |
| 585 | * \return |
| 586 | * 0 if parameter is set\n |
| 587 | * BCM_ERR_NOENT if parameter is not set |
| 588 | * BCM_ERR_PARM if parm_number is invalid |
| 589 | */ |
| 590 | bcmos_errno bcmcli_parm_check(bcmcli_session *session, int parm_number); |
| 591 | |
| 592 | |
| 593 | /** Get enum's string value given its internal value |
| 594 | * \param[in] table Enum table |
| 595 | * \param[in] value Internal value |
| 596 | * \return |
| 597 | * enum string value or NULL if internal value is invalid |
| 598 | */ |
| 599 | static inline const char *bcmcli_enum_stringval(const bcmcli_enum_val table[], long value) |
| 600 | { |
| 601 | while(table->name) |
| 602 | { |
| 603 | if (table->val==value) |
| 604 | return table->name; |
| 605 | ++table; |
| 606 | } |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /** Get enum's parameter string value given its internal value |
| 612 | * \param[in] session Session handle |
| 613 | * \param[in] parm_number Parameter number |
| 614 | * \param[in] value Internal value |
| 615 | * \return |
| 616 | * enum string value or NULL if parameter is not enum or |
| 617 | * internal value is invalid |
| 618 | */ |
| 619 | const char *bcmcli_enum_parm_stringval(bcmcli_session *session, int parm_number, long value); |
| 620 | |
| 621 | |
| 622 | /** Print CLI parameter value |
| 623 | * \param[in] session Session handle |
| 624 | * \param[in] parm Parameter |
| 625 | */ |
| 626 | void bcmcli_parm_print(bcmcli_session *session, const bcmcli_cmd_parm *parm); |
| 627 | |
| 628 | |
| 629 | /** strncpy flavour that always add 0 terminator |
| 630 | * \param[in] dst Destination string |
| 631 | * \param[in] src Source string |
| 632 | * \param[in] dst_size Destination buffer size |
| 633 | * \return dst |
| 634 | */ |
| 635 | static inline char *bcmcli_strncpy(char *dst, const char *src, uint32_t dst_size) |
| 636 | { |
| 637 | strncpy(dst, src, dst_size-1); |
| 638 | dst[dst_size-1] = 0; |
| 639 | return dst; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | /** strncat flavour that limits size of destination buffer |
| 644 | * \param[in] dst Destination string |
| 645 | * \param[in] src Source string |
| 646 | * \param[in] dst_size Destination buffer size |
| 647 | * \return dst |
| 648 | */ |
| 649 | static inline char *bcmcli_strncat(char *dst, const char *src, uint32_t dst_size) |
| 650 | { |
| 651 | uint32_t dst_len = strlen(dst); |
| 652 | return strncat(dst, src, dst_size-dst_len-1); |
| 653 | } |
| 654 | |
| 655 | /* Redefine bcmcli_session_print --> bcmcli_print */ |
| 656 | #define bcmcli_print bcmcli_session_print |
| 657 | |
| 658 | /** Enable / disable CLI command logging |
| 659 | * \param[in] mode Logging flags |
| 660 | * \param[in] log Log session. Must be set if mode != BCMCLI_CMD_LOG_NONE |
| 661 | * \return 0=OK or error <0 |
| 662 | */ |
| 663 | bcmos_errno bcmcli_log_set(bcmcli_log_mode mode, bcmcli_session *log); |
| 664 | |
| 665 | /** Write string to CLI log. |
| 666 | * The function is ignored if CLI logging is not enabled using bcmcli_log_set() |
| 667 | * \param[in] format printf-like format followed by arguments |
| 668 | */ |
| 669 | void bcmcli_log(const char *format, ...); |
| 670 | |
| 671 | #ifdef __cplusplus |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | /** @} end bcm_cli group */ |
| 676 | |
| 677 | #endif /* #ifndef BCM_CLI_H */ |