- * Initialize libsigrokdecode.
- *
- * This initializes the Python interpreter, and creates and initializes
- * a "sigrokdecode" Python module.
- *
- * Then, it searches for sigrok protocol decoders in the "decoders"
- * subdirectory of the the libsigrokdecode installation directory.
- * All decoders that are found are loaded into memory and added to an
- * internal list of decoders, which can be queried via srd_decoder_list().
- *
- * The caller is responsible for calling the clean-up function srd_exit(),
- * which will properly shut down libsigrokdecode and free its allocated memory.
- *
- * Multiple calls to srd_init(), without calling srd_exit() in between,
- * are not allowed.
- *
- * @param path Path to an extra directory containing protocol decoders
- * which will be added to the Python sys.path. May be NULL.
- *
- * @return SRD_OK upon success, a (negative) error code otherwise.
- * Upon Python errors, SRD_ERR_PYTHON is returned. If the decoders
- * directory cannot be accessed, SRD_ERR_DECODERS_DIR is returned.
- * If not enough memory could be allocated, SRD_ERR_MALLOC is returned.
- *
- * @since 0.1.0
- */
-SRD_API int srd_init(const char *path)
-{
- int ret;
- char *env_path;
-
- if (max_session_id != -1) {
- srd_err("libsigrokdecode is already initialized.");
- return SRD_ERR;
- }
-
- srd_dbg("Initializing libsigrokdecode.");
-
- /* Add our own module to the list of built-in modules. */
- PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
-
- /* Initialize the Python interpreter. */
- Py_Initialize();
-
- /* Installed decoders. */
- if ((ret = srd_decoder_searchpath_add(DECODERS_DIR)) != SRD_OK) {
- Py_Finalize();
- return ret;
- }
-
- /* Path specified by the user. */
- if (path) {
- if ((ret = srd_decoder_searchpath_add(path)) != SRD_OK) {
- Py_Finalize();
- return ret;
- }
- }
-
- /* Environment variable overrides everything, for debugging. */
- if ((env_path = getenv("SIGROKDECODE_DIR"))) {
- if ((ret = srd_decoder_searchpath_add(env_path)) != SRD_OK) {
- Py_Finalize();
- return ret;
- }
- }
-
- max_session_id = 0;
-
- return SRD_OK;
-}
-
-/**
- * Shutdown libsigrokdecode.
- *
- * This frees all the memory allocated for protocol decoders and shuts down
- * the Python interpreter.
- *
- * This function should only be called if there was a (successful!) invocation
- * of srd_init() before. Calling this function multiple times in a row, without
- * any successful srd_init() calls in between, is not allowed.
- *
- * @return SRD_OK upon success, a (negative) error code otherwise.
- *
- * @since 0.1.0
- */
-SRD_API int srd_exit(void)
-{
- GSList *l;
-
- srd_dbg("Exiting libsigrokdecode.");
-
- for (l = sessions; l; l = l->next)
- srd_session_destroy((struct srd_session *)l->data);
-
- srd_decoder_unload_all();
- g_slist_free(pd_list);
- pd_list = NULL;
-
- /* Py_Finalize() returns void, any finalization errors are ignored. */
- Py_Finalize();
-
- max_session_id = -1;
-
- return SRD_OK;
-}
-
-/**
- * Add an additional search directory for the protocol decoders.
- *
- * The specified directory is prepended (not appended!) to Python's sys.path,
- * in order to search for sigrok protocol decoders in the specified
- * directories first, and in the generic Python module directories (and in
- * the current working directory) last. This avoids conflicts if there are
- * Python modules which have the same name as a sigrok protocol decoder in
- * sys.path or in the current working directory.
- *
- * @param path Path to the directory containing protocol decoders which shall
- * be added to the Python sys.path, or NULL.
- *
- * @return SRD_OK upon success, a (negative) error code otherwise.
- *
- * @private