You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef _GPXE_SETTINGS_H
  2. #define _GPXE_SETTINGS_H
  3. /** @file
  4. *
  5. * Configuration settings
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/dhcp.h>
  10. #include <gpxe/tables.h>
  11. struct config_setting;
  12. /**
  13. * A configuration context
  14. *
  15. * This identifies the context within which settings are inspected and
  16. * changed. For example, the context might be global, or might be
  17. * restricted to the settings stored in NVS on a particular device.
  18. */
  19. struct config_context {
  20. /** DHCP options block, or NULL
  21. *
  22. * If NULL, all registered DHCP options blocks will be used.
  23. */
  24. struct dhcp_option_block *options;
  25. };
  26. /**
  27. * A configuration setting type
  28. *
  29. * This represents a type of configuration setting (e.g. string, IPv4
  30. * address, etc.).
  31. */
  32. struct config_setting_type {
  33. /** Name
  34. *
  35. * This is the name exposed to the user (e.g. "string").
  36. */
  37. const char *name;
  38. /** Description */
  39. const char *description;
  40. /** Show value of setting
  41. *
  42. * @v context Configuration context
  43. * @v setting Configuration setting
  44. * @v buf Buffer to contain value
  45. * @v len Length of buffer
  46. * @ret rc Return status code
  47. */
  48. int ( * show ) ( struct config_context *context,
  49. struct config_setting *setting,
  50. char *buf, size_t len );
  51. /** Set value of setting
  52. *
  53. * @v context Configuration context
  54. * @v setting Configuration setting
  55. * @v value Setting value (as a string)
  56. * @ret rc Return status code
  57. */
  58. int ( * set ) ( struct config_context *context,
  59. struct config_setting *setting,
  60. const char *value );
  61. };
  62. /** Declare a configuration setting type */
  63. #define __config_setting_type \
  64. __table ( struct config_setting_type, config_setting_types, 01 )
  65. /**
  66. * A configuration setting
  67. *
  68. * This represents a single configuration setting (e.g. "hostname").
  69. */
  70. struct config_setting {
  71. /** Name
  72. *
  73. * This is the human-readable name for the setting. Where
  74. * possible, it should match the name used in dhcpd.conf (see
  75. * dhcp-options(5)).
  76. */
  77. const char *name;
  78. /** Description */
  79. const char *description;
  80. /** DHCP option tag
  81. *
  82. * This is the DHCP tag used to identify the option in DHCP
  83. * packets and stored option blocks.
  84. */
  85. unsigned int tag;
  86. /** Configuration setting type
  87. *
  88. * This identifies the type of setting (e.g. string, IPv4
  89. * address, etc.).
  90. */
  91. struct config_setting_type *type;
  92. };
  93. /** Declare a configuration setting */
  94. #define __config_setting __table ( struct config_setting, config_settings, 01 )
  95. /**
  96. * Show value of setting
  97. *
  98. * @v context Configuration context
  99. * @v setting Configuration setting
  100. * @v buf Buffer to contain value
  101. * @v len Length of buffer
  102. * @ret rc Return status code
  103. */
  104. static inline int show_setting ( struct config_context *context,
  105. struct config_setting *setting,
  106. char *buf, size_t len ) {
  107. return setting->type->show ( context, setting, buf, len );
  108. }
  109. extern int set_setting ( struct config_context *context,
  110. struct config_setting *setting,
  111. const char *value );
  112. /**
  113. * Clear setting
  114. *
  115. * @v context Configuration context
  116. * @v setting Configuration setting
  117. * @ret rc Return status code
  118. */
  119. static inline int clear_setting ( struct config_context *context,
  120. struct config_setting *setting ) {
  121. delete_dhcp_option ( context->options, setting->tag );
  122. return 0;
  123. }
  124. /* Function prototypes */
  125. extern int show_named_setting ( struct config_context *context,
  126. const char *name, char *buf, size_t len );
  127. extern int set_named_setting ( struct config_context *context,
  128. const char *name, const char *value );
  129. /**
  130. * Clear named setting
  131. *
  132. * @v context Configuration context
  133. * @v name Configuration setting name
  134. * @ret rc Return status code
  135. */
  136. static inline int clear_named_setting ( struct config_context *context,
  137. const char *name ) {
  138. return set_named_setting ( context, name, NULL );
  139. }
  140. #endif /* _GPXE_SETTINGS_H */