]> sigrok.org Git - libsigrokdecode.git/blobdiff - controller.c
Documentation and coding style fixes
[libsigrokdecode.git] / controller.c
index 672ac93258b67d8c8317c9508ae581c337f302a4..a91defac97c16e8dacbe26e9c59f5c81bba63142 100644 (file)
@@ -305,6 +305,16 @@ SRD_API int srd_inst_option_set(struct srd_decoder_inst *di,
        const char *val_str;
        char *dbg, *key;
 
+       if (!di) {
+               srd_err("Invalid decoder instance.");
+               return SRD_ERR_ARG;
+       }
+
+       if (!options) {
+               srd_err("Invalid options GHashTable.");
+               return SRD_ERR_ARG;
+       }
+
        if (!PyObject_HasAttrString(di->decoder->py_dec, "options")) {
                /* Decoder has no options. */
                if (g_hash_table_size(options) == 0) {
@@ -326,8 +336,18 @@ SRD_API int srd_inst_option_set(struct srd_decoder_inst *di,
        /* All of these are synthesized objects, so they're good. */
        py_dec_optkeys = PyDict_Keys(py_dec_options);
        num_optkeys = PyList_Size(py_dec_optkeys);
+
+       /*
+        * The 'options' dictionary is a class variable, but we need to
+        * change it. Changing it directly will affect the entire class,
+        * so we need to create a new object for it, and populate that
+        * instead.
+        */
        if (!(py_di_options = PyObject_GetAttrString(di->py_inst, "options")))
                goto err_out;
+       Py_DECREF(py_di_options);
+       py_di_options = PyDict_New();
+       PyObject_SetAttrString(di->py_inst, "options", py_di_options);
        for (i = 0; i < num_optkeys; i++) {
                /* Get the default class value for this option. */
                py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key);
@@ -407,7 +427,6 @@ SRD_API int srd_inst_option_set(struct srd_decoder_inst *di,
        ret = SRD_OK;
 
 err_out:
-       Py_XDECREF(py_optlist);
        Py_XDECREF(py_di_options);
        Py_XDECREF(py_dec_optkeys);
        Py_XDECREF(py_dec_options);
@@ -482,7 +501,7 @@ SRD_API int srd_inst_probe_set_all(struct srd_decoder_inst *di,
 
        for (l = g_hash_table_get_keys(new_probes); l; l = l->next) {
                probe_id = l->data;
-               probe_val= g_hash_table_lookup(new_probes, probe_id);
+               probe_val = g_hash_table_lookup(new_probes, probe_id);
                if (!g_variant_is_of_type(probe_val, G_VARIANT_TYPE_INT32)) {
                        /* Probe name was specified without a value. */
                        srd_err("No probe number was specified for %s.",
@@ -532,7 +551,7 @@ SRD_API int srd_inst_probe_set_all(struct srd_decoder_inst *di,
  * @return Pointer to a newly allocated struct srd_decoder_inst, or
  *         NULL in case of failure.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
                const char *decoder_id, GHashTable *options)
@@ -573,10 +592,10 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
         * order in which the decoder class defined them.
         */
        di->dec_num_probes = g_slist_length(di->decoder->probes) +
-                            g_slist_length(di->decoder->opt_probes);
+                       g_slist_length(di->decoder->opt_probes);
        if (di->dec_num_probes) {
                if (!(di->dec_probemap =
-                    g_try_malloc(sizeof(int) * di->dec_num_probes))) {
+                               g_try_malloc(sizeof(int) * di->dec_num_probes))) {
                        srd_err("Failed to g_malloc() probe map.");
                        g_free(di);
                        return NULL;
@@ -589,7 +608,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
        if (!(di->py_inst = PyObject_CallObject(dec->py_dec, NULL))) {
                if (PyErr_Occurred())
                        srd_exception_catch("failed to create %s instance: ",
-                                           decoder_id);
+                                       decoder_id);
                g_free(di->dec_probemap);
                g_free(di);
                return NULL;
@@ -616,7 +635,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
  *
  * @return SRD_OK upon success, a (negative) error code otherwise.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API int srd_inst_stack(struct srd_session *sess,
                struct srd_decoder_inst *di_from, struct srd_decoder_inst *di_to)
@@ -654,7 +673,7 @@ SRD_API int srd_inst_stack(struct srd_session *sess,
  *
  * @return Pointer to struct srd_decoder_inst, or NULL if not found.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API struct srd_decoder_inst *srd_inst_find_by_id(struct srd_session *sess,
                const char *inst_id)
@@ -800,8 +819,9 @@ SRD_PRIV int srd_inst_decode(uint64_t start_samplenum,
        srd_logic *logic;
        uint64_t end_samplenum;
 
-       srd_dbg("Calling decode() on instance %s with %d bytes starting "
-               "at sample %d.", di->inst_id, inbuflen, start_samplenum);
+       srd_dbg("Calling decode() on instance %s with %" PRIu64 " bytes "
+               "starting at sample %" PRIu64 ".", di->inst_id, inbuflen,
+               start_samplenum);
 
        /* Return an error upon unusable input. */
        if (!di) {
@@ -915,8 +935,8 @@ static int session_is_valid(struct srd_session *sess)
  * A session holds all decoder instances, their stack relationships and
  * output callbacks.
  *
- * @param sess. A pointer which will hold a pointer to a newly
- *              initialized session on return.
+ * @param sess A pointer which will hold a pointer to a newly
+ *             initialized session on return.
  *
  * @return SRD_OK upon success, a (negative) error code otherwise.
  *
@@ -927,7 +947,7 @@ SRD_API int srd_session_new(struct srd_session **sess)
 
        if (!sess) {
                srd_err("Invalid session pointer.");
-               return SRD_ERR;
+               return SRD_ERR_ARG;
        }
 
        if (!(*sess = g_try_malloc(sizeof(struct srd_session))))
@@ -954,7 +974,7 @@ SRD_API int srd_session_new(struct srd_session **sess)
  *
  * @return SRD_OK upon success, a (negative) error code otherwise.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API int srd_session_start(struct srd_session *sess)
 {
@@ -983,7 +1003,8 @@ SRD_API int srd_session_start(struct srd_session *sess)
        ret = SRD_OK;
 
        srd_dbg("Calling start() on all instances in session %d with "
-                       "%d probes, unitsize %d samplerate %d.", sess->session_id,
+                       "%" PRIu64 " probes, unitsize %" PRIu64
+                       ", samplerate %" PRIu64 ".", sess->session_id,
                        sess->num_probes, sess->unitsize, sess->samplerate);
 
        /*
@@ -1032,8 +1053,13 @@ SRD_API int srd_session_config_set(struct srd_session *sess, int key,
                return SRD_ERR_ARG;
        }
 
+       if (!data) {
+               srd_err("Invalid config data.");
+               return SRD_ERR_ARG;
+       }
+
        if (!g_variant_is_of_type(data, G_VARIANT_TYPE_UINT64)) {
-               srd_err("Value for key %d should be of type uint64.");
+               srd_err("Value for key %d should be of type uint64.", key);
                return SRD_ERR_ARG;
        }
 
@@ -1047,6 +1073,9 @@ SRD_API int srd_session_config_set(struct srd_session *sess, int key,
        case SRD_CONF_SAMPLERATE:
                sess->samplerate = g_variant_get_uint64(data);
                break;
+       default:
+               srd_err("Cannot set config for unknown key %d.", key);
+               return SRD_ERR_ARG;
        }
 
        g_variant_unref(data);
@@ -1064,7 +1093,7 @@ SRD_API int srd_session_config_set(struct srd_session *sess, int key,
  *
  * @return SRD_OK upon success, a (negative) error code otherwise.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API int srd_session_send(struct srd_session *sess, uint64_t start_samplenum,
                const uint8_t *inbuf, uint64_t inbuflen)
@@ -1078,8 +1107,8 @@ SRD_API int srd_session_send(struct srd_session *sess, uint64_t start_samplenum,
        }
 
        srd_dbg("Calling decode() on all instances with starting sample "
-               "number %" PRIu64 ", %" PRIu64 " bytes at 0x%p",
-               start_samplenum, inbuflen, inbuf);
+                       "number %" PRIu64 ", %" PRIu64 " bytes at 0x%p",
+                       start_samplenum, inbuflen, inbuf);
 
        for (d = sess->di_list; d; d = d->next) {
                if ((ret = srd_inst_decode(start_samplenum, d->data, inbuf,
@@ -1095,16 +1124,21 @@ SRD_API int srd_session_send(struct srd_session *sess, uint64_t start_samplenum,
  *
  * All decoder instances and output callbacks are properly released.
  *
- * @param sess. The session to be destroyed.
+ * @param sess The session to be destroyed.
  *
  * @return SRD_OK upon success, a (negative) error code otherwise.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API int srd_session_destroy(struct srd_session *sess)
 {
        int session_id;
 
+       if (!sess) {
+               srd_err("Invalid session.");
+               return SRD_ERR_ARG;
+       }
+
        session_id = sess->session_id;
        if (sess->di_list)
                srd_inst_free_all(sess, NULL);
@@ -1131,7 +1165,7 @@ SRD_API int srd_session_destroy(struct srd_session *sess)
  * @param cb The function to call. Must not be NULL.
  * @param cb_data Private data for the callback function. Can be NULL.
  *
- * @since 0.1.0
+ * @since 0.3.0
  */
 SRD_API int srd_pd_output_callback_add(struct srd_session *sess,
                int output_type, srd_pd_output_callback_t cb, void *cb_data)