py.h 5.2 KB

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