/** * Initialize libsigrokdecode. * * This initializes the Python interpreter, and creates and initializes * a "sigrok" Python module with a single put() method. * * Then, it searches for sigrok protocol decoder files (*.py) in the * "decoders" subdirectory of the the sigrok 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_list_decoders(). * * 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() inbetween, * are not allowed. * * @return SRD_OK upon success, a (negative) error code otherwise. * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders * directory cannot be accessed, return SRD_ERR_DECODERS_DIR. * If not enough memory could be allocated, return SRD_ERR_MALLOC. */ int srd_init(void) { DIR *dir; struct dirent *dp; char *decodername; struct srd_decoder *dec; int ret; /* Py_Initialize() returns void and usually cannot fail. */ Py_Initialize(); /* TODO: Use Py_InitModule3() to add a docstring? */ if (!Py_InitModule("sigrok", EmbMethods)) { Py_Finalize(); /* Returns void. */ return SRD_ERR_PYTHON; } /* Add search directory for the protocol decoders. */ /* FIXME: What happens if this function is called multiple times? */ ret = PyRun_SimpleString("import sys;" "sys.path.append(r'" DECODERS_DIR "');"); if (ret != 0) { Py_Finalize(); /* Returns void. */ return SRD_ERR_PYTHON; } if (!(dir = opendir(DECODERS_DIR))) { Py_Finalize(); /* Returns void. */ return SRD_ERR_DECODERS_DIR; } while ((dp = readdir(dir)) != NULL) { /* Ignore filenames which don't end with ".py". */ if (!g_str_has_suffix(dp->d_name, ".py")) continue; /* Decoder name == filename (without .py suffix). */ decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); /* TODO: Error handling. Use g_try_malloc(). */ if (!(dec = malloc(sizeof(struct srd_decoder)))) { Py_Finalize(); /* Returns void. */ return SRD_ERR_MALLOC; } /* Load the decoder. */ /* TODO: Warning if loading fails for a decoder. */ ret = srd_load_decoder(decodername, &dec); if (!ret) { /* Append it to the list of supported/loaded decoders. */ list_pds = g_slist_append(list_pds, dec); } } closedir(dir); return SRD_OK; }
int srd_load_all_decoders(void) { DIR *dir; struct dirent *dp; int ret; char *decodername; struct srd_decoder *dec; if (!(dir = opendir(DECODERS_DIR))) { Py_Finalize(); /* Returns void. */ return SRD_ERR_DECODERS_DIR; } while ((dp = readdir(dir)) != NULL) { /* Ignore filenames which don't end with ".py". */ if (!g_str_has_suffix(dp->d_name, ".py")) continue; /* Decoder name == filename (without .py suffix). */ decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3); /* TODO: Error handling. Use g_try_malloc(). */ if (!(dec = malloc(sizeof(struct srd_decoder)))) { Py_Finalize(); /* Returns void. */ return SRD_ERR_MALLOC; } /* Load the decoder. */ /* TODO: Warning if loading fails for a decoder. */ if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) { /* Append it to the list of supported/loaded decoders. */ pd_list = g_slist_append(pd_list, dec); } } closedir(dir); return SRD_OK; }