sorcery.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Sorcery Data Access Layer API
  20. * \author Joshua Colp <jcolp@digium.com>
  21. * \ref AstSorcery
  22. */
  23. /*!
  24. * \page AstSorcery Data Access Layer API
  25. *
  26. * Sorcery is a unifying data access layer which utilizes the configuration framework,
  27. * realtime, and astdb to allow object creation, retrieval, updating, and deletion.
  28. *
  29. * \par Initialization
  30. *
  31. * Usage of sorcery is accomplished by first opening a sorcery structure. This structure holds
  32. * all information about the object types, object fields, and object mappings. All API functions
  33. * require the sorcery structure to operate. When sorcery is no longer needed the structure can
  34. * be unreferenced using \ref ast_sorcery_unref
  35. *
  36. * Once opened the sorcery structure must have object mappings applied to it. This maps the
  37. * object types to their respective wizards (object storage modules). If the developer would like
  38. * to allow the user to configure this using the sorcery.conf configuration file the
  39. * \ref ast_sorcery_apply_config API call can be used to read in the configuration file and apply the
  40. * mappings. \ref ast_sorcery_open will automatically call \ref ast_sorcery_apply_config to allow
  41. * for configuration of objects using the same category name as the module that is opening the
  42. * sorcery instance. Direct calls to \ref ast_sorcery_apply_config should only be performed if a
  43. * module wishes to allow for additional configuration sections in sorcery.conf to be used.
  44. * If the storage of the object types are such that a default wizard can be used this can
  45. * be applied using the \ref ast_sorcery_apply_default API call. Note that the default mappings will not
  46. * override configured mappings. They are only used in the case where no configured mapping exists.
  47. *
  48. * Configuring object mappings implicitly creates a basic version of an object type. The object type
  49. * must be fully registered, however, using the \ref ast_sorcery_object_register API call before any
  50. * objects of the type can be allocated, created, or retrieved.
  51. *
  52. * Once the object type itself has been fully registered the individual fields within the object must
  53. * be registered using the \ref ast_sorcery_object_field_register API call. Note that not all fields *need*
  54. * be registered. Only fields that should be accessible using the sorcery API have to be registered.
  55. *
  56. * \par Creating Objects
  57. *
  58. * Before an object can be created within the sorcery API it must first be allocated using the
  59. * \ref ast_sorcery_alloc API call. This allocates a new instance of the object, sets sorcery specific
  60. * details, and applies default values to the object. A unique identifier can optionally be specified
  61. * when allocating an object. If it is not provided one will be automatically generated. Allocating
  62. * an object does not create it within any object storage mechanisms that are configured for the
  63. * object type. Creation must explicitly be done using the \ref ast_sorcery_create API call. This API call
  64. * passes the object to each configured object storage mechanism for the object type until one
  65. * successfully persists the object.
  66. *
  67. * \par Retrieving Objects
  68. *
  69. * To retrieve a single object using its unique identifier the \ref ast_sorcery_retrieve_by_id API call
  70. * can be used.
  71. *
  72. * To retrieve potentially multiple objects using specific fields the \ref ast_sorcery_retrieve_by_fields
  73. * API call can be used. The behavior of this API call is controlled using different flags. If the
  74. * AST_RETRIEVE_FLAG_MULTIPLE flag is used a container will be returned which contains all matching objects.
  75. * To retrieve all objects the AST_RETRIEVE_FLAG_ALL flag can be specified. Note that when specifying this flag
  76. * you do not need to pass any fields.
  77. *
  78. * Both API calls return shared objects. Modification of the object can not occur until it has been copied.
  79. *
  80. * \par Updating Objects
  81. *
  82. * As retrieved objects may be shared the first step to updating the object with new details is creating a
  83. * copy using the \ref ast_sorcery_copy API call. This will return a new object which is specific to the caller.
  84. * Any field within the object may be modified as needed. Once changes are done the changes can be committed
  85. * using the \ref ast_sorcery_update API call. Note that as the copied object is specific to the caller it must
  86. * be unreferenced after use.
  87. *
  88. * \par Deleting Objects
  89. *
  90. * To delete an object simply call the \ref ast_sorcery_delete API call with an object retrieved using the
  91. * ast_sorcery_retrieve_by_* API calls or a copy returned from \ref ast_sorcery_copy.
  92. */
  93. #ifndef _ASTERISK_SORCERY_H
  94. #define _ASTERISK_SORCERY_H
  95. #if defined(__cplusplus) || defined(c_plusplus)
  96. extern "C" {
  97. #endif
  98. #include "asterisk/config_options.h"
  99. #include "asterisk/uuid.h"
  100. /*! \brief Maximum size of an object type */
  101. #define MAX_OBJECT_TYPE 64
  102. /*! \brief Maximum length of an object field name */
  103. #define MAX_OBJECT_FIELD 128
  104. /*!
  105. * \brief Retrieval flags
  106. */
  107. enum ast_sorcery_retrieve_flags {
  108. /*! \brief Default retrieval flags */
  109. AST_RETRIEVE_FLAG_DEFAULT = 0,
  110. /*! \brief Return all matching objects */
  111. AST_RETRIEVE_FLAG_MULTIPLE = (1 << 0),
  112. /*! \brief Perform no matching, return all objects */
  113. AST_RETRIEVE_FLAG_ALL = (1 << 1),
  114. };
  115. /*!
  116. * \brief Field handler flags
  117. */
  118. enum ast_sorcery_field_handler_flags {
  119. /*! \brief Try both handlers, string first */
  120. AST_HANDLER_PREFER_STRING,
  121. /*! \brief Try both handlers, list first */
  122. AST_HANDLER_PREFER_LIST,
  123. /*! \brief Use string handler only */
  124. AST_HANDLER_ONLY_STRING,
  125. /*! \brief Use list handler only */
  126. AST_HANDLER_ONLY_LIST,
  127. };
  128. /*! \brief Forward declaration for the sorcery main structure and wizard structure */
  129. struct ast_sorcery;
  130. struct ast_sorcery_wizard;
  131. /*!
  132. * \brief A callback function for translating a value into a string
  133. *
  134. * \param obj Object to get value from
  135. * \param args Where the field is
  136. * \param buf Pointer to the buffer that the handler has created which contains the field value
  137. *
  138. * \retval 0 success
  139. * \retval -1 failure
  140. */
  141. typedef int (*sorcery_field_handler)(const void *obj, const intptr_t *args, char **buf);
  142. /*!
  143. * \brief A callback function for translating multiple values into an ast_variable list
  144. *
  145. * \param obj Object to get values from
  146. * \param fields Pointer to store the list of fields
  147. *
  148. * \retval 0 success
  149. * \retval -1 failure
  150. */
  151. typedef int (*sorcery_fields_handler)(const void *obj, struct ast_variable **fields);
  152. /*!
  153. * \brief A callback function for performing a transformation on an object set
  154. *
  155. * \param set The existing object set
  156. *
  157. * \retval non-NULL new object set if changed
  158. * \retval NULL if no changes present
  159. *
  160. * \note The returned ast_variable list must be *new*. You can not return the input set.
  161. */
  162. typedef struct ast_variable *(*sorcery_transform_handler)(struct ast_variable *set);
  163. /*!
  164. * \brief A callback function for when an object set is successfully applied to an object
  165. *
  166. * \note On a failure return, the state of the object is left undefined. It is a bad
  167. * idea to try to use this object.
  168. *
  169. * \param sorcery Sorcery structure in use
  170. * \param obj The object itself
  171. * \retval 0 Success
  172. * \retval non-zero Failure
  173. */
  174. typedef int (*sorcery_apply_handler)(const struct ast_sorcery *sorcery, void *obj);
  175. /*!
  176. * \brief A callback function for copying the contents of one object to another
  177. *
  178. * \param src The source object
  179. * \param dst The destination object
  180. *
  181. * \retval 0 success
  182. * \retval -1 failure
  183. */
  184. typedef int (*sorcery_copy_handler)(const void *src, void *dst);
  185. /*!
  186. * \brief A callback function for generating a changeset between two objects
  187. *
  188. * \param original The original object
  189. * \param modified The modified object
  190. * \param changes The changeset
  191. *
  192. * \param 0 success
  193. * \param -1 failure
  194. */
  195. typedef int (*sorcery_diff_handler)(const void *original, const void *modified, struct ast_variable **changes);
  196. /*! \brief Interface for the global sorcery observer */
  197. struct ast_sorcery_global_observer {
  198. /*! \brief Callback after an instance is created */
  199. void (*instance_created)(const char *name, struct ast_sorcery *sorcery);
  200. /*! \brief Callback after an wizard is registered */
  201. void (*wizard_registered)(const char *name,
  202. const struct ast_sorcery_wizard *wizard);
  203. /*! \brief Callback before an instance is destroyed */
  204. void (*instance_destroying)(const char *name, struct ast_sorcery *sorcery);
  205. /*! \brief Callback before a wizard is unregistered */
  206. void (*wizard_unregistering)(const char *name,
  207. const struct ast_sorcery_wizard *wizard);
  208. };
  209. /*! \brief Interface for the sorcery instance observer */
  210. struct ast_sorcery_instance_observer {
  211. /*! \brief Callback before instance is loaded/reloaded */
  212. void (*instance_loading)(const char *name, const struct ast_sorcery *sorcery,
  213. int reloaded);
  214. /*! \brief Callback after instance is loaded/reloaded */
  215. void (*instance_loaded)(const char *name, const struct ast_sorcery *sorcery,
  216. int reloaded);
  217. /*! \brief Callback after a wizard is mapped to an object_type */
  218. void (*wizard_mapped)(const char *name, struct ast_sorcery *sorcery,
  219. const char *object_type, struct ast_sorcery_wizard *wizard,
  220. const char *wizard_args, void *wizard_data);
  221. /*! \brief Callback after any object_type is registered */
  222. void (*object_type_registered)(const char *name, struct ast_sorcery *sorcery,
  223. const char *object_type);
  224. /*! \brief Callback before any object_type is loaded/reloaded */
  225. void (*object_type_loading)(const char *name, const struct ast_sorcery *sorcery,
  226. const char *object_type, int reloaded);
  227. /*! \brief Callback after any object_type is loaded/reloaded */
  228. void (*object_type_loaded)(const char *name, const struct ast_sorcery *sorcery,
  229. const char *object_type, int reloaded);
  230. };
  231. /*! \brief Interface for the sorcery wizard observer */
  232. struct ast_sorcery_wizard_observer {
  233. /*! \brief Callback before a wizard is loaded/reloaded for any type */
  234. void (*wizard_loading)(const char *name, const struct ast_sorcery_wizard *wizard,
  235. const char *object_type, int reloaded);
  236. /*! \brief Callback after a wizard is loaded/reloaded for any type */
  237. void (*wizard_loaded)(const char *name, const struct ast_sorcery_wizard *wizard,
  238. const char *object_type, int reloaded);
  239. };
  240. /*! \brief Interface for a sorcery wizard */
  241. struct ast_sorcery_wizard {
  242. /*! \brief Name of the wizard */
  243. const char *name;
  244. /*! \brief Pointer to the Asterisk module this wizard is implemented by */
  245. struct ast_module *module;
  246. /*! \brief Callback for opening a wizard */
  247. void *(*open)(const char *data);
  248. /*! \brief Optional callback for loading persistent objects */
  249. void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
  250. /*! \brief Optional callback for reloading persistent objects */
  251. void (*reload)(void *data, const struct ast_sorcery *sorcery, const char *type);
  252. /*! \brief Callback for creating an object */
  253. int (*create)(const struct ast_sorcery *sorcery, void *data, void *object);
  254. /*! \brief Callback for retrieving an object using an id */
  255. void *(*retrieve_id)(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
  256. /*! \brief Callback for retrieving multiple objects using a regex on their id */
  257. void (*retrieve_regex)(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
  258. /*! \brief Optional callback for retrieving an object using fields */
  259. void *(*retrieve_fields)(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
  260. /*! \brief Optional callback for retrieving multiple objects using some optional field criteria */
  261. void (*retrieve_multiple)(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields);
  262. /*! \brief Callback for updating an object */
  263. int (*update)(const struct ast_sorcery *sorcery, void *data, void *object);
  264. /*! \brief Callback for deleting an object */
  265. int (*delete)(const struct ast_sorcery *sorcery, void *data, void *object);
  266. /*! \brief Callback for closing a wizard */
  267. void (*close)(void *data);
  268. /*! \brief Optional callback for retrieving multiple objects by matching their id with a prefix */
  269. void (*retrieve_prefix)(const struct ast_sorcery *sorcery,
  270. void *data,
  271. const char *type,
  272. struct ao2_container *objects,
  273. const char *prefix,
  274. const size_t prefix_len);
  275. };
  276. /*! \brief Interface for a sorcery object type observer */
  277. struct ast_sorcery_observer {
  278. /*! \brief Callback for when an object is created */
  279. void (*created)(const void *object);
  280. /*! \brief Callback for when an object is updated */
  281. void (*updated)(const void *object);
  282. /*! \brief Callback for when an object is deleted */
  283. void (*deleted)(const void *object);
  284. /*! \brief Callback for when an object type is loaded/reloaded */
  285. void (*loaded)(const char *object_type);
  286. };
  287. /*! \brief Opaque structure for internal sorcery object */
  288. struct ast_sorcery_object;
  289. /*! \brief Structure which contains details about a sorcery object */
  290. struct ast_sorcery_object_details {
  291. /*! \brief Pointer to internal sorcery object information */
  292. struct ast_sorcery_object *object;
  293. };
  294. /*! \brief Macro which must be used at the beginning of each sorcery capable object */
  295. #define SORCERY_OBJECT(details) \
  296. struct { \
  297. struct ast_sorcery_object_details details; \
  298. } \
  299. /*!
  300. * \brief Initialize the sorcery API
  301. *
  302. * \retval 0 success
  303. * \retval -1 failure
  304. */
  305. int ast_sorcery_init(void);
  306. /*!
  307. * \brief Register a sorcery wizard
  308. *
  309. * \param interface Pointer to a wizard interface
  310. * \param module Pointer to the module implementing the interface
  311. *
  312. * \retval 0 success
  313. * \retval -1 failure
  314. */
  315. int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module);
  316. /*!
  317. * \brief Register a sorcery wizard
  318. *
  319. * \param interface Pointer to a wizard interface
  320. * \param module Pointer to the module implementing the interface
  321. *
  322. * \retval 0 success
  323. * \retval -1 failure
  324. */
  325. int __ast_sorcery_wizard_register_with_prefix(const struct ast_sorcery_wizard *interface, struct ast_module *module);
  326. /*!
  327. * \brief See \ref __ast_sorcery_wizard_register()
  328. */
  329. #define ast_sorcery_wizard_register(interface) __ast_sorcery_wizard_register_with_prefix(interface, ast_module_info ? ast_module_info->self : NULL)
  330. /*!
  331. * \brief Unregister a sorcery wizard
  332. *
  333. * \param interface Pointer to the wizard interface
  334. *
  335. * \retval 0 success
  336. * \retval -1 failure
  337. */
  338. int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface);
  339. /*!
  340. * \brief Open a new sorcery structure
  341. *
  342. * \param module The module name (AST_MODULE)
  343. *
  344. * When called, this will automatically also call __ast_sorcery_apply_config()
  345. * with the module name as the configuration section.
  346. *
  347. * \retval non-NULL success
  348. * \retval NULL if allocation failed
  349. */
  350. struct ast_sorcery *__ast_sorcery_open(const char *module);
  351. #define ast_sorcery_open() __ast_sorcery_open(AST_MODULE)
  352. /*!
  353. * \brief Retrieves an existing sorcery instance by module name
  354. *
  355. * \param module The module name
  356. *
  357. * \retval non-NULL success
  358. * \retval NULL if no instance was found
  359. *
  360. * \note The returned instance has its reference count incremented. The caller
  361. * must decrement the count when they're finished with it.
  362. *
  363. */
  364. struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module);
  365. enum ast_sorcery_apply_result {
  366. /*! Sorcery wizard failed to apply. */
  367. AST_SORCERY_APPLY_FAIL = -1,
  368. /*! Sorcery wizard applied successfully. */
  369. AST_SORCERY_APPLY_SUCCESS = 0,
  370. /*! Sorcery wizard has already been applied to the object type. */
  371. AST_SORCERY_APPLY_DUPLICATE = 1,
  372. /*! Default sorcery wizard is unnecessary since a wizard has already been applied to the object type. */
  373. AST_SORCERY_APPLY_DEFAULT_UNNECESSARY = 2,
  374. /*! No sorcery.conf configuration file was found to apply. */
  375. AST_SORCERY_APPLY_NO_CONFIGURATION = 3,
  376. };
  377. /*!
  378. * \brief Apply configured wizard mappings
  379. *
  380. * \param sorcery Pointer to a sorcery structure
  381. * \param name Name of the category to use within the configuration file, normally the module name
  382. * \param module The module name (AST_MODULE)
  383. *
  384. * This function is called automatically by __ast_sorcery_open() using the module name as the
  385. * configuration category. The only reason you should call this function is if your module
  386. * wishes to apply configuration from additional sections of sorcery.conf.
  387. *
  388. * If a configuration section attempts to apply the same sorcery wizard to an object type
  389. * more than once, the wizard will only be applied one time.
  390. *
  391. * \return What happened when attempting to apply the config.
  392. */
  393. enum ast_sorcery_apply_result __ast_sorcery_apply_config(struct ast_sorcery *sorcery,
  394. const char *name, const char *module);
  395. #define ast_sorcery_apply_config(sorcery, name) \
  396. __ast_sorcery_apply_config((sorcery), (name), AST_MODULE)
  397. /*!
  398. * \brief Apply default object wizard mappings
  399. *
  400. * \param sorcery Pointer to a sorcery structure
  401. * \param type Type of object to apply to
  402. * \param module The name of the module, typically AST_MODULE
  403. * \param name Name of the wizard to use
  404. * \param data Data to be passed to wizard
  405. *
  406. * \return What occurred when applying the default
  407. *
  408. * \note This should be called *after* applying configuration sourced mappings
  409. *
  410. * \note Only a single default can exist per object type
  411. */
  412. enum ast_sorcery_apply_result __ast_sorcery_apply_default(struct ast_sorcery *sorcery,
  413. const char *type, const char *module, const char *name, const char *data);
  414. #define ast_sorcery_apply_default(sorcery, type, name, data) \
  415. __ast_sorcery_apply_default((sorcery), (type), AST_MODULE, (name), (data))
  416. /*!
  417. * \brief Apply additional object wizard mappings
  418. *
  419. * \param sorcery Pointer to a sorcery structure
  420. * \param type Type of object to apply to
  421. * \param module The name of the module, typically AST_MODULE
  422. * \param name Name of the wizard to use
  423. * \param data Data to be passed to wizard
  424. * \param caching Wizard should cache
  425. *
  426. * \return What occurred when applying the mapping
  427. *
  428. * \note This should be called *after* applying default mappings
  429. */
  430. enum ast_sorcery_apply_result __ast_sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
  431. const char *type, const char *module, const char *name, const char *data, unsigned int caching);
  432. /*!
  433. * \brief Apply additional object wizard mappings
  434. *
  435. * \param sorcery Pointer to a sorcery structure
  436. * \param type Type of object to apply to
  437. * \param module The name of the module, typically AST_MODULE
  438. * \param name Name of the wizard to use
  439. * \param data Data to be passed to wizard
  440. *
  441. * \return What occurred when applying the mapping
  442. *
  443. * \note This should be called *after* applying default mappings
  444. */
  445. #define ast_sorcery_apply_wizard_mapping(sorcery, type, name, data, caching) \
  446. __ast_sorcery_apply_wizard_mapping((sorcery), (type), AST_MODULE, (name), (data), (caching));
  447. /*!
  448. * \brief Pre-defined locations to insert at
  449. */
  450. enum ast_sorcery_wizard_position {
  451. AST_SORCERY_WIZARD_POSITION_LAST = -1,
  452. AST_SORCERY_WIZARD_POSITION_FIRST = 0,
  453. };
  454. /*!
  455. * \brief Insert an additional object wizard mapping at a specific position
  456. * in the wizard list
  457. *
  458. * \param sorcery Pointer to a sorcery structure
  459. * \param type Type of object to apply to
  460. * \param module The name of the module, typically AST_MODULE
  461. * \param name Name of the wizard to use
  462. * \param data Data to be passed to wizard
  463. * \param caching Wizard should cache
  464. * \param position An index to insert to or one of ast_sorcery_wizard_position
  465. *
  466. * \return What occurred when applying the mapping
  467. *
  468. * \note This should be called *after* applying default mappings
  469. * \note Wizards can be retrieved by using ast_sorcery_get_wizard_mapping_count
  470. * and iterating over them using ast_sorcery_get_wizard_mapping.
  471. *
  472. * \since 13.4.0
  473. */
  474. enum ast_sorcery_apply_result __ast_sorcery_insert_wizard_mapping(struct ast_sorcery *sorcery,
  475. const char *type, const char *module, const char *name, const char *data,
  476. unsigned int caching, int position);
  477. /*!
  478. * \brief Insert an additional object wizard mapping at a specific position
  479. * in the wizard list
  480. *
  481. * \param sorcery Pointer to a sorcery structure
  482. * \param type Type of object to apply to
  483. * \param module The name of the module, typically AST_MODULE
  484. * \param name Name of the wizard to use
  485. * \param data Data to be passed to wizard
  486. * \param position One of ast_sorcery_wizard_position
  487. *
  488. * \return What occurred when applying the mapping
  489. *
  490. * \note This should be called *after* applying default mappings
  491. * \since 13.4.0
  492. */
  493. #define ast_sorcery_insert_wizard_mapping(sorcery, type, name, data, caching, position) \
  494. __ast_sorcery_insert_wizard_mapping((sorcery), (type), AST_MODULE, (name), (data), \
  495. (caching), (position))
  496. /*!
  497. * \brief Remove an object wizard mapping
  498. *
  499. * \param sorcery Pointer to a sorcery structure
  500. * \param type Type of object to remove from
  501. * \param module The name of the module, typically AST_MODULE
  502. * \param name The name of the wizard to remove
  503. *
  504. * \retval 0 success
  505. * \retval -1 failure
  506. *
  507. * \since 13.4.0
  508. */
  509. int __ast_sorcery_remove_wizard_mapping(struct ast_sorcery *sorcery,
  510. const char *type, const char *module, const char *name);
  511. /*!
  512. * \brief Remove an object wizard mapping
  513. *
  514. * \param sorcery Pointer to a sorcery structure
  515. * \param type Type of object to remove from
  516. * \param name The name of the wizard to remove
  517. *
  518. * \retval 0 success
  519. * \retval -1 failure
  520. *
  521. * \since 13.4.0
  522. */
  523. #define ast_sorcery_remove_wizard_mapping(sorcery, type, name) \
  524. __ast_sorcery_remove_wizard_mapping((sorcery), (type), AST_MODULE, (name))
  525. /*!
  526. * \brief Return the number of wizards mapped to an object type
  527. *
  528. * \param sorcery Pointer to a sorcery structure
  529. * \param type Type of object
  530. *
  531. * \return Number of wizards or -1 for error
  532. * \since 13.4.0
  533. */
  534. int ast_sorcery_get_wizard_mapping_count(struct ast_sorcery *sorcery,
  535. const char *type);
  536. /*!
  537. * \brief By index, return a wizard mapped to an object type
  538. *
  539. * \param sorcery Pointer to a sorcery structure
  540. * \param type Type of object
  541. * \param index Index of the wizard
  542. * \param wizard A pointer to receive the wizard pointer
  543. * \param data A pointer to receive the data pointer
  544. *
  545. * \retval 0 success
  546. * \retval -1 failure
  547. *
  548. * \warning The wizard will have its reference count bumped so you must
  549. * call ao2_cleanup when you're done with it.
  550. *
  551. * \note The wizard and data returned are valid only for this object type
  552. * and only while the wizard is applied to the object type.
  553. *
  554. * \since 13.4.0
  555. */
  556. int ast_sorcery_get_wizard_mapping(struct ast_sorcery *sorcery,
  557. const char *type, int index, struct ast_sorcery_wizard **wizard, void **data);
  558. /*!
  559. * \brief Unregister an object type
  560. *
  561. * \param sorcery Pointer to a sorcery structure
  562. * \param type Type of object
  563. *
  564. * \retval 0 success
  565. * \retval -1 failure
  566. */
  567. int ast_sorcery_object_unregister(struct ast_sorcery *sorcery, const char *type);
  568. /*!
  569. * \brief Register an object type
  570. *
  571. * \param sorcery Pointer to a sorcery structure
  572. * \param type Type of object
  573. * \param hidden All objects of this type are internal and should not be manipulated by users
  574. * \param reloadable All objects of this type are reloadable
  575. * \param alloc Required object allocation callback
  576. * \param transform Optional transformation callback
  577. * \param apply Optional object set apply callback
  578. *
  579. * \note In general, this function should not be used directly. One of the various
  580. * macro'd versions should be used instead.
  581. *
  582. * \retval 0 success
  583. * \retval -1 failure
  584. */
  585. int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, unsigned int reloadable, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply);
  586. /*!
  587. * \brief Register an object type
  588. *
  589. * \param sorcery Pointer to a sorcery structure
  590. * \param type Type of object
  591. * \param alloc Required object allocation callback
  592. * \param transform Optional transformation callback
  593. * \param apply Optional object set apply callback
  594. *
  595. * \retval 0 success
  596. * \retval -1 failure
  597. */
  598. #define ast_sorcery_object_register(sorcery, type, alloc, transform, apply) \
  599. __ast_sorcery_object_register((sorcery), (type), 0, 1, (alloc), (transform), (apply))
  600. /*!
  601. * \brief Register an object type that is not reloadable
  602. *
  603. * \param sorcery Pointer to a sorcery structure
  604. * \param type Type of object
  605. * \param alloc Required object allocation callback
  606. * \param transform Optional transformation callback
  607. * \param apply Optional object set apply callback
  608. *
  609. * \retval 0 success
  610. * \retval -1 failure
  611. */
  612. #define ast_sorcery_object_register_no_reload(sorcery, type, alloc, transform, apply) \
  613. __ast_sorcery_object_register((sorcery), (type), 0, 0, (alloc), (transform), (apply))
  614. /*!
  615. * \brief Register an internal, hidden object type
  616. *
  617. * \param sorcery Pointer to a sorcery structure
  618. * \param type Type of object
  619. * \param alloc Required object allocation callback
  620. * \param transform Optional transformation callback
  621. * \param apply Optional object set apply callback
  622. *
  623. * \retval 0 success
  624. * \retval -1 failure
  625. */
  626. #define ast_sorcery_internal_object_register(sorcery, type, alloc, transform, apply) \
  627. __ast_sorcery_object_register((sorcery), (type), 1, 1, (alloc), (transform), (apply))
  628. /*!
  629. * \brief Set the high and low alert water marks of the sorcery object type.
  630. * \since 13.10.0
  631. *
  632. * \param sorcery Pointer to a sorcery structure
  633. * \param type Type of object
  634. * \param low_water New queue low water mark. (-1 to set as 90% of high_water)
  635. * \param high_water New queue high water mark.
  636. *
  637. * \retval 0 on success.
  638. * \retval -1 on error (water marks not changed).
  639. */
  640. int ast_sorcery_object_set_congestion_levels(struct ast_sorcery *sorcery, const char *type, long low_water, long high_water);
  641. /*!
  642. * \brief Set the copy handler for an object type
  643. *
  644. * \param sorcery Pointer to a sorcery structure
  645. * \param type Type of object
  646. * \param copy Copy handler
  647. */
  648. void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy);
  649. /*!
  650. * \brief Set the diff handler for an object type
  651. *
  652. * \param sorcery Pointer to a sorcery structure
  653. * \param type Type of object
  654. * \param diff Diff handler
  655. */
  656. void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff);
  657. /*!
  658. * \brief Register a regex for multiple fields within an object
  659. *
  660. * \param sorcery Pointer to a sorcery structure
  661. * \param type Type of object
  662. * \param regex A regular expression pattern for the fields
  663. * \param config_handler A custom handler for translating the string representation of the fields
  664. * \param sorcery_handler A custom handler for translating the native representation of the fields
  665. *
  666. * \retval 0 success
  667. * \retval -1 failure
  668. */
  669. int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler,
  670. sorcery_fields_handler sorcery_handler);
  671. /*!
  672. * \brief Register a field within an object
  673. *
  674. * \param sorcery Pointer to a sorcery structure
  675. * \param type Type of object
  676. * \param name Name of the field
  677. * \param default_val Default value of the field
  678. * \param config_handler A custom handler for translating the string representation of the fields
  679. * \param sorcery_handler A custom handler for translating the native representation of the fields
  680. * \param multiple_handler A custom handler for translating the native representation of the fields
  681. * \param opt_type Option type
  682. * \param flags Option type specific flags
  683. * \param no_doc Field should not be documented
  684. * \param alias Interpret and apply field value only
  685. *
  686. * \retval 0 success
  687. * \retval -1 failure
  688. */
  689. int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type,
  690. const char *name, const char *default_val, enum aco_option_type opt_type,
  691. aco_option_handler config_handler, sorcery_field_handler sorcery_handler,
  692. sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc,
  693. unsigned int alias, size_t argc, ...);
  694. /*!
  695. * \brief Register a field within an object
  696. *
  697. * \param sorcery Pointer to a sorcery structure
  698. * \param type Type of object
  699. * \param name Name of the field
  700. * \param default_val Default value of the field
  701. * \param opt_type Option type
  702. * \param flags Option type specific flags
  703. *
  704. * \retval 0 success
  705. * \retval -1 failure
  706. */
  707. #define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags, ...) \
  708. __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 0, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
  709. /*!
  710. * \brief Register a field within an object as an alias
  711. *
  712. * \param sorcery Pointer to a sorcery structure
  713. * \param type Type of object
  714. * \param name Name of the field
  715. * \param default_val Default value of the field
  716. * \param opt_type Option type
  717. * \param flags Option type specific flags
  718. *
  719. * \retval 0 success
  720. * \retval -1 failure
  721. */
  722. #define ast_sorcery_object_field_register_alias(sorcery, type, name, default_val, opt_type, flags, ...) \
  723. __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 1, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
  724. /*!
  725. * \brief Register a field within an object without documentation
  726. *
  727. * \param sorcery Pointer to a sorcery structure
  728. * \param type Type of object
  729. * \param name Name of the field
  730. * \param default_val Default value of the field
  731. * \param opt_type Option type
  732. * \param flags Option type specific flags
  733. *
  734. * \retval 0 success
  735. * \retval -1 failure
  736. */
  737. #define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags, ...) \
  738. __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 1, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
  739. /*!
  740. * \brief Register a field within an object with custom handlers
  741. *
  742. * \param sorcery Pointer to a sorcery structure
  743. * \param type Type of object
  744. * \param name Name of the field
  745. * \param default_val Default value of the field
  746. * \param config_handler Custom configuration handler
  747. * \param sorcery_handler Custom sorcery handler
  748. * \param multiple_handler Custom multiple handler
  749. * \param flags Option type specific flags
  750. *
  751. * \retval 0 success
  752. * \retval -1 failure
  753. */
  754. #define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
  755. __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 0, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
  756. /*!
  757. * \brief Register a field within an object with custom handlers as an alias
  758. *
  759. * \param sorcery Pointer to a sorcery structure
  760. * \param type Type of object
  761. * \param name Name of the field
  762. * \param default_val Default value of the field
  763. * \param config_handler Custom configuration handler
  764. * \param sorcery_handler Custom sorcery handler
  765. * \param flags Option type specific flags
  766. *
  767. * \retval 0 success
  768. * \retval -1 failure
  769. */
  770. #define ast_sorcery_object_field_register_custom_alias(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
  771. __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 1, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
  772. /*!
  773. * \brief Register a field within an object with custom handlers without documentation
  774. *
  775. * \param sorcery Pointer to a sorcery structure
  776. * \param type Type of object
  777. * \param name Name of the field
  778. * \param default_val Default value of the field
  779. * \param config_handler Custom configuration handler
  780. * \param sorcery_handler Custom sorcery handler
  781. * \param multiple_handler Custom multiple handler
  782. * \param flags Option type specific flags
  783. *
  784. * \retval 0 success
  785. * \retval -1 failure
  786. */
  787. #define ast_sorcery_object_field_register_custom_nodoc(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
  788. __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 1, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
  789. /*!
  790. * \brief Inform any wizards to load persistent objects
  791. *
  792. * \param sorcery Pointer to a sorcery structure
  793. */
  794. void ast_sorcery_load(const struct ast_sorcery *sorcery);
  795. /*!
  796. * \brief Inform any wizards of a specific object type to load persistent objects
  797. *
  798. * \param sorcery Pointer to a sorcery structure
  799. * \param type Name of the object type to load
  800. */
  801. void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type);
  802. /*!
  803. * \brief Inform any wizards to reload persistent objects
  804. *
  805. * \param sorcery Pointer to a sorcery structure
  806. */
  807. void ast_sorcery_reload(const struct ast_sorcery *sorcery);
  808. /*!
  809. * \brief Inform any wizards of a specific object type to reload persistent objects
  810. *
  811. * \param sorcery Pointer to a sorcery structure
  812. * \param type Name of the object type to reload
  813. */
  814. void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type);
  815. /*!
  816. * \brief Increase the reference count of a sorcery structure
  817. *
  818. * \param sorcery Pointer to a sorcery structure
  819. */
  820. void ast_sorcery_ref(struct ast_sorcery *sorcery);
  821. /*!
  822. * \brief Create an object set (KVP list) for an object
  823. *
  824. * \param sorcery Pointer to a sorcery structure
  825. * \param object Pointer to a sorcery object
  826. * \param flags Flags indicating which handler to use and in what order.
  827. *
  828. * \retval non-NULL success
  829. * \retval NULL if error occurred
  830. *
  831. * \note The returned ast_variable list must be destroyed using ast_variables_destroy
  832. */
  833. struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
  834. const void *object, enum ast_sorcery_field_handler_flags flags);
  835. /*!
  836. * \brief Create an object set (KVP list) for an object
  837. *
  838. * \param sorcery Pointer to a sorcery structure
  839. * \param object Pointer to a sorcery object
  840. *
  841. * \retval non-NULL success
  842. * \retval NULL if error occurred
  843. *
  844. * \note The returned ast_variable list must be destroyed using ast_variables_destroy
  845. *
  846. * \note This function attempts to use a field's sorcery_fields_handler first and if that
  847. * doesn't exist or fails, a field's sorcery_field_handler is used. The difference is
  848. * that the former may return multiple list entries for the same field and the latter will only
  849. * return 1. It's up to the field itself to determine what the appropriate content is.
  850. */
  851. #define ast_sorcery_objectset_create(sorcery, object) \
  852. ast_sorcery_objectset_create2(sorcery, object, AST_HANDLER_PREFER_LIST)
  853. /*!
  854. * \brief Create an object set in JSON format for an object
  855. *
  856. * \param sorcery Pointer to a sorcery structure
  857. * \param object Pointer to a sorcery object
  858. *
  859. * \retval non-NULL success
  860. * \retval NULL if error occurred
  861. *
  862. * \note The returned ast_json object must be unreferenced using ast_json_unref
  863. */
  864. struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object);
  865. /*!
  866. * \brief Apply an object set (KVP list) to an object
  867. *
  868. * \param sorcery Pointer to a sorcery structure
  869. * \param object Pointer to a sorcery object
  870. * \param objectset Object set itself
  871. *
  872. * \retval 0 success
  873. * \retval -1 failure
  874. *
  875. * \note This operation is *not* atomic. If this fails it is possible for the object to be left with a partially
  876. * applied object set.
  877. */
  878. int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset);
  879. /*!
  880. * \brief Create a changeset given two object sets
  881. *
  882. * \param original Original object set
  883. * \param modified Modified object set
  884. * \param changes Pointer to hold any changes between the object sets
  885. *
  886. * \retval 0 success
  887. * \retval -1 failure
  888. *
  889. * \note The returned ast_variable list must be destroyed using ast_variables_destroy
  890. */
  891. int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes);
  892. /*!
  893. * \brief Allocate a generic sorcery capable object
  894. *
  895. * \param size Size of the object
  896. * \param destructor Optional destructor function
  897. *
  898. * \retval non-NULL success
  899. * \retval NULL failure
  900. */
  901. void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor);
  902. /*!
  903. * \brief Allocate an object
  904. *
  905. * \param sorcery Pointer to a sorcery structure
  906. * \param type Type of object to allocate
  907. * \param id Optional unique identifier, if none is provided one will be generated
  908. *
  909. * \retval non-NULL success
  910. * \retval NULL failure
  911. */
  912. void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id);
  913. /*!
  914. * \brief Create a copy of an object
  915. *
  916. * \param sorcery Pointer to a sorcery structure
  917. * \param object Existing object
  918. *
  919. * \retval non-NULL success
  920. * \retval NULL failure
  921. */
  922. void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object);
  923. /*!
  924. * \brief Create a changeset of two objects
  925. *
  926. * \param sorcery Pointer to a sorcery structure
  927. * \param original Original object
  928. * \param modified Modified object
  929. * \param changes Pointer which will be populated with changes if any exist
  930. *
  931. * \retval 0 success
  932. * \retval -1 failure
  933. *
  934. * \note The returned ast_variable list must be destroyed using ast_variables_destroy
  935. *
  936. * \note While the objects must be of the same type they do not have to be the same object
  937. */
  938. int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes);
  939. /*!
  940. * \brief Add a global observer to sorcery
  941. *
  942. * \param callbacks Implementation of the global observer interface
  943. *
  944. * \retval 0 success
  945. * \retval -1 failure
  946. *
  947. * \note You must be ready to accept observer invocations before this function is called
  948. */
  949. int ast_sorcery_global_observer_add(const struct ast_sorcery_global_observer *callbacks);
  950. /*!
  951. * \brief Remove a global observer from sorcery.
  952. *
  953. * A global observer is notified...
  954. * After a new wizard is registered.
  955. * After a new sorcery instance is opened.
  956. * Before an instance is destroyed.
  957. * Before a wizard is unregistered.
  958. *
  959. * \param callbacks Implementation of the global observer interface
  960. */
  961. void ast_sorcery_global_observer_remove(const struct ast_sorcery_global_observer *callbacks);
  962. /*!
  963. * \brief Add an observer to a sorcery instance
  964. *
  965. * \param sorcery Pointer to a sorcery structure
  966. * \param callbacks Implementation of the instance observer interface
  967. *
  968. * An instance observer is notified...
  969. * Before an instance is loaded or reloaded.
  970. * After an instance is loaded or reloaded.
  971. * After a wizard is mapped to an object type.
  972. * After an object type is registered.
  973. * Before an object type is loaded or reloaded.
  974. * After an object type is loaded or reloaded.
  975. *
  976. * \retval 0 success
  977. * \retval -1 failure
  978. *
  979. * \note You must be ready to accept observer invocations before this function is called
  980. */
  981. int ast_sorcery_instance_observer_add(struct ast_sorcery *sorcery,
  982. const struct ast_sorcery_instance_observer *callbacks);
  983. /*!
  984. * \brief Remove an observer from a sorcery instance
  985. *
  986. * \param sorcery Pointer to a sorcery structure
  987. * \param callbacks Implementation of the instance observer interface
  988. */
  989. void ast_sorcery_instance_observer_remove(struct ast_sorcery *sorcery,
  990. const struct ast_sorcery_instance_observer *callbacks);
  991. /*!
  992. * \brief Add an observer to a sorcery wizard
  993. *
  994. * \param sorcery Pointer to a previously registered wizard structure
  995. * \param callbacks Implementation of the wizard observer interface
  996. *
  997. * A wizard observer is notified...
  998. * Before a wizard is loaded or reloaded.
  999. * After a wizard is loaded or reloaded.
  1000. *
  1001. * \retval 0 success
  1002. * \retval -1 failure
  1003. *
  1004. * \note You must be ready to accept observer invocations before this function is called
  1005. */
  1006. int ast_sorcery_wizard_observer_add(struct ast_sorcery_wizard *wizard,
  1007. const struct ast_sorcery_wizard_observer *callbacks);
  1008. /*!
  1009. * \brief Remove an observer from a sorcery wizard.
  1010. *
  1011. * \param sorcery Pointer to a sorcery structure
  1012. * \param callbacks Implementation of the wizard observer interface
  1013. */
  1014. void ast_sorcery_wizard_observer_remove(struct ast_sorcery_wizard *wizard,
  1015. const struct ast_sorcery_wizard_observer *callbacks);
  1016. /*!
  1017. * \brief Add an observer to a specific object type
  1018. *
  1019. * \param sorcery Pointer to a sorcery structure
  1020. * \param type Type of object that should be observed
  1021. * \param callbacks Implementation of the observer interface
  1022. *
  1023. * \retval 0 success
  1024. * \retval -1 failure
  1025. *
  1026. * \note You must be ready to accept observer invocations before this function is called
  1027. */
  1028. int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks);
  1029. /*!
  1030. * \brief Remove an observer from a specific object type
  1031. *
  1032. * \param sorcery Pointer to a sorcery structure
  1033. * \param type Type of object that should no longer be observed
  1034. * \param callbacks Implementation of the observer interface
  1035. *
  1036. * \retval 0 success
  1037. * \retval -1 failure
  1038. */
  1039. void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks);
  1040. /*!
  1041. * \brief Create and potentially persist an object using an available wizard
  1042. *
  1043. * \param sorcery Pointer to a sorcery structure
  1044. * \param object Pointer to a sorcery object
  1045. *
  1046. * \retval 0 success
  1047. * \retval -1 failure
  1048. */
  1049. int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object);
  1050. /*!
  1051. * \brief Retrieve an object using its unique identifier
  1052. *
  1053. * \param sorcery Pointer to a sorcery structure
  1054. * \param type Type of object to retrieve
  1055. * \param id Unique object identifier
  1056. *
  1057. * \retval non-NULL if found
  1058. * \retval NULL if not found
  1059. */
  1060. void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id);
  1061. /*!
  1062. * \brief Retrieve an object or multiple objects using specific fields
  1063. * \since 13.9.0
  1064. *
  1065. * \param sorcery Pointer to a sorcery structure
  1066. * \param type Type of object to retrieve
  1067. * \param flags Flags to control behavior
  1068. * \param fields Optional object fields and values to match against
  1069. *
  1070. * \retval non-NULL if found
  1071. * \retval NULL if not found
  1072. *
  1073. * \note If the AST_RETRIEVE_FLAG_MULTIPLE flag is specified the returned value will be an
  1074. * ao2_container that must be unreferenced after use.
  1075. *
  1076. * \note If the AST_RETRIEVE_FLAG_ALL flag is used you may omit fields to retrieve all objects
  1077. * of the given type.
  1078. *
  1079. * \note The fields parameter can contain realtime-style expressions in variable->name.
  1080. * All operators defined for ast_strings_match can be used except for regex as
  1081. * there's no common support for regex in the realtime backends at this time.
  1082. * If multiple variables are in the fields list, all must match for an object to
  1083. * be returned. See ast_strings_match for more information.
  1084. *
  1085. * Example:
  1086. *
  1087. * The following code can be significantly faster when a realtime backend is in use
  1088. * because the expression "qualify_frequency > 0" is passed to the database to limit
  1089. * the number of rows returned.
  1090. *
  1091. * struct ast_variable *var = ast_variable_new("qualify_frequency >", "0", "");
  1092. * struct ao2_container *aors;
  1093. *
  1094. * if (!var) {
  1095. * return;
  1096. * }
  1097. *
  1098. * aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
  1099. * "aor", AST_RETRIEVE_FLAG_MULTIPLE, var);
  1100. *
  1101. */
  1102. void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields);
  1103. /*!
  1104. * \brief Retrieve multiple objects using a regular expression on their id
  1105. *
  1106. * \param sorcery Pointer to a sorcery structure
  1107. * \param type Type of object to retrieve
  1108. * \param regex Regular expression
  1109. *
  1110. * \retval non-NULL if error occurs
  1111. * \retval NULL success
  1112. *
  1113. * \note The provided regex is treated as extended case sensitive.
  1114. */
  1115. struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex);
  1116. /*!
  1117. * \brief Retrieve multiple objects whose id begins with the specified prefix
  1118. * \since 13.19.0
  1119. *
  1120. * \param sorcery Pointer to a sorcery structure
  1121. * \param type Type of object to retrieve
  1122. * \param prefix Object id prefix
  1123. * \param prefix_len The length of prefix in bytes
  1124. *
  1125. * \retval non-NULL if error occurs
  1126. * \retval NULL success
  1127. *
  1128. * \note The prefix is matched in a case sensitive manner.
  1129. */
  1130. struct ao2_container *ast_sorcery_retrieve_by_prefix(const struct ast_sorcery *sorcery, const char *type, const char *prefix, const size_t prefix_len);
  1131. /*!
  1132. * \brief Update an object
  1133. *
  1134. * \param sorcery Pointer to a sorcery structure
  1135. * \param object Pointer to a sorcery object
  1136. *
  1137. * \retval 0 success
  1138. * \retval -1 failure
  1139. */
  1140. int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object);
  1141. /*!
  1142. * \brief Delete an object
  1143. *
  1144. * \param sorcery Pointer to a sorcery structure
  1145. * \param object Pointer to a sorcery object
  1146. *
  1147. * \retval 0 success
  1148. * \retval -1 failure
  1149. */
  1150. int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object);
  1151. /*!
  1152. * \brief Decrease the reference count of a sorcery structure
  1153. *
  1154. * \param sorcery Pointer to a sorcery structure
  1155. */
  1156. void ast_sorcery_unref(struct ast_sorcery *sorcery);
  1157. /*!
  1158. * \brief Get the unique identifier of a sorcery object
  1159. *
  1160. * \param object Pointer to a sorcery object
  1161. *
  1162. * \retval unique identifier
  1163. */
  1164. const char *ast_sorcery_object_get_id(const void *object);
  1165. /*!
  1166. * \brief Get the type of a sorcery object
  1167. *
  1168. * \param object Pointer to a sorcery object
  1169. *
  1170. * \retval type of object
  1171. */
  1172. const char *ast_sorcery_object_get_type(const void *object);
  1173. /*!
  1174. * \brief Get an extended field value from a sorcery object
  1175. *
  1176. * \param object Pointer to a sorcery object
  1177. * \param name Name of the extended field value
  1178. *
  1179. * \retval non-NULL if found
  1180. * \retval NULL if not found
  1181. *
  1182. * \note The returned string does NOT need to be freed and is guaranteed to remain valid for the lifetime of the object
  1183. */
  1184. const char *ast_sorcery_object_get_extended(const void *object, const char *name);
  1185. /*!
  1186. * \brief Set an extended field value on a sorcery object
  1187. *
  1188. * \param object Pointer to a sorcery object
  1189. * \param name Name of the extended field
  1190. * \param value Value of the extended field
  1191. *
  1192. * \retval 0 success
  1193. * \retval -1 failure
  1194. *
  1195. * \note The field name MUST begin with '@' to indicate it is an extended field.
  1196. * \note If the extended field already exists it will be overwritten with the new value.
  1197. */
  1198. int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value);
  1199. /*!
  1200. * \brief ao2 object comparator based on sorcery id.
  1201. */
  1202. int ast_sorcery_object_id_compare(void *obj, void *arg, int flags);
  1203. /*!
  1204. * \brief ao2 object sorter based on sorcery id.
  1205. */
  1206. int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags);
  1207. /*!
  1208. * \brief ao2 object hasher based on sorcery id.
  1209. */
  1210. int ast_sorcery_object_id_hash(const void *obj, int flags);
  1211. /*!
  1212. * \brief Get the sorcery object type given a type name.
  1213. *
  1214. * \param sorcery The sorcery from which to retrieve the object type
  1215. * \param type The type name
  1216. */
  1217. struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
  1218. const char *type);
  1219. /*!
  1220. * \brief Determine if a particular object field has been registered with sorcery
  1221. *
  1222. * \param object_type The object type to check against
  1223. * \param field_name The name of the field to check
  1224. *
  1225. * \retval 0 The field is not registered for this sorcery type
  1226. * \retval 1 The field is registered for this sorcery type
  1227. */
  1228. int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
  1229. const char *field_name);
  1230. /*!
  1231. * \brief Get the module that has opened the provided sorcery instance.
  1232. *
  1233. * \param sorcery The sorcery instance
  1234. *
  1235. * \return The module
  1236. */
  1237. const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery);
  1238. #if defined(__cplusplus) || defined(c_plusplus)
  1239. }
  1240. #endif
  1241. #endif /* _ASTERISK_SORCERY_H */