py.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef _PCILIB_PY_H
  2. #define _PCILIB_PY_H
  3. #include <pcilib.h>
  4. #include <pcilib/error.h>
  5. #define pcilib_python_error(...) pcilib_log_python_error(__FILE__, __LINE__, PCILIB_LOG_DEFAULT, PCILIB_LOG_ERROR, __VA_ARGS__)
  6. #define pcilib_python_warning(...) pcilib_log_python_error(__FILE__, __LINE__, PCILIB_LOG_DEFAULT, PCILIB_LOG_WARNING, __VA_ARGS__)
  7. typedef struct pcilib_py_s pcilib_py_t;
  8. typedef void pcilib_py_object;
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flags, pcilib_log_priority_t prio, const char *msg, ...);
  13. /** Initializes Python engine
  14. *
  15. * This function will return success if Python support is disabled. Only functions
  16. * executing python call, like pcilib_py_eval_string(), return errors. Either way,
  17. * no script directories are configured. The pcilib_add_script_dir() call is used
  18. * for this purpose.
  19. *
  20. * @param[in,out] ctx - pcilib context
  21. * @return - error or 0 on success
  22. */
  23. int pcilib_init_py(pcilib_t *ctx);
  24. /** Cleans up memory used by various python structures
  25. * and finalyzes python environment if pcilib is not started from python script
  26. *
  27. * @param[in] ctx - the pcilib_t context
  28. */
  29. void pcilib_free_py(pcilib_t *ctx);
  30. /** Add an additional path to look for python scripts
  31. *
  32. * The default location for python files is /usr/local/share/pcilib/models/@b{model}.
  33. * This can be altered using CMake PCILIB_MODEL_DIR variable while building or using
  34. * PCILIB_MODEL_DIR environmental variable dynamicly. The default location is added
  35. * with @b{location} = NULL. Additional directories can be added as well either
  36. * by specifying relative path from the default directory or absolute path in the
  37. * system.
  38. *
  39. * @param[in,out] ctx - pcilib context
  40. * @param[in] location - NULL or path to additional scripts
  41. * @return - error or 0 on success
  42. */
  43. int pcilib_py_add_script_dir(pcilib_t *ctx, const char *location);
  44. /** Loads the specified python script
  45. *
  46. * Once loaded the script is available until pcilib context is destryoed.
  47. *
  48. * @param[in,out] ctx - pcilib context
  49. * @param[in] name - script name, the passed variable is referenced and, hence, should have static duration
  50. * @return - error or 0 on success
  51. */
  52. int pcilib_py_load_script(pcilib_t *ctx, const char *name);
  53. /** Check if the specified script can be used as transform view and detects transform configuration
  54. *
  55. * @param[in,out] ctx - pcilib context
  56. * @param[in] name - script name
  57. * @param[out] mode - supported access mode (read/write/read-write)
  58. * @return - error or 0 on success
  59. */
  60. int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *name, pcilib_access_mode_t *mode);
  61. /**
  62. * Get the PyObject from the polymorphic type. The returned value should be cleaned with Py_XDECREF()
  63. * @param[in] ctx - pcilib context
  64. * @param[in] val - initialized polymorphic value of arbitrary type
  65. * @param[out] err - error code or 0 on sccuess
  66. * @return - valid PyObject or NULL in the case of error
  67. */
  68. pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val, int *err);
  69. /**
  70. * Initializes the polymorphic value from PyObject. If `val` already contains the value, cleans it first.
  71. * Therefore, before first usage the value should be always initialized to 0.
  72. * @param[in] ctx - pcilib context
  73. * @param[in,out] val - initialized polymorphic value
  74. * @param[in] pyval - valid PyObject* containing PyInt, PyFloat, or PyString
  75. * @return - 0 on success or memory error
  76. */
  77. int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py_object *pyval);
  78. /** Evaluates the specified python code and returns result in @b{val}
  79. *
  80. * The python code may include special variables which will be substituted by pcitool before executing Python interpreter
  81. * @b{$value} - will be replaced by the current value of the @b{val} parameter
  82. * @b{$reg} - will be replaced by the current value of the specified register @b{reg}
  83. * @b{${/prop/temp}} - will be replaced by the current value of the specified property @b{/prop/temp}
  84. * @b{${/prop/temp:C}} - will be replaced by the current value of the specified property @b{/prop/temp} in the given units
  85. * @param[in,out] ctx - pcilib context
  86. * @param[in] codestr - python code to evaluate
  87. * @param[in,out] val - Should contain the value which will be substituted in place of @b{$value} and on
  88. * successful execution will contain the computed value
  89. * @return - error or 0 on success
  90. */
  91. int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *val);
  92. /** Execute the specified function in the Python script which was loaded with pcilib_py_load_script() call
  93. *
  94. * The function is expected to accept two paramters. The first parameter is pcipywrap context and the @b{val}
  95. * is passed as the second parameter. The return value of the script will be returned in the @b{val} as well.
  96. * If function returns Py_None, the value of @b{val} will be unchanged.
  97. *
  98. * @param[in,out] ctx - pcilib context
  99. * @param[in] script - script name
  100. * @param[in] func - function name
  101. * @param[in,out] val - Should contain the value of second parameter of the function before call and on
  102. * successful return will contain the returned value
  103. * @return - error or 0 on success
  104. */
  105. int pcilib_py_eval_func(pcilib_t *ctx, const char *script, const char *func, pcilib_value_t *val);
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #endif /* _PCILIB_PY_H */