+/* This is a neutered version of libsigrokdecode's py_str_as_str(). It
+ * does no error checking, but then the only strings it processes are
+ * generated by Python's repr(), so are known good. */
+static char *py_str_as_str(const PyObject *py_str)
+{
+ PyObject *py_encstr;
+ char *str, *outstr;
+
+ py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL);
+ str = PyBytes_AS_STRING(py_encstr);
+ outstr = g_strdup(str);
+ Py_DecRef(py_encstr);
+
+ return outstr;
+}
+
+static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data)
+{
+ struct output *op;
+ PyObject *pydata, *pyrepr;
+ GString *out;
+ char *s;
+
+ DBG("Python output from %s", pdata->pdo->di->inst_id);
+ op = cb_data;
+ pydata = pdata->data;
+ DBG("ptr %p", pydata);
+
+ if (strcmp(pdata->pdo->di->inst_id, op->pd))
+ /* This is not the PD selected for output. */
+ return;
+
+ if (!(pyrepr = PyObject_Repr(pydata))) {
+ ERR("Invalid Python object.");
+ return;
+ }
+ s = py_str_as_str(pyrepr);
+ Py_DecRef(pyrepr);
+
+ /* Output format for testing is '<ss>-<es> <inst-id>: <repr>\n' */
+ out = g_string_sized_new(128);
+ g_string_printf(out, "%"PRIu64"-%"PRIu64" %s: %s\n",
+ pdata->start_sample, pdata->end_sample,
+ pdata->pdo->di->inst_id, s);
+ g_free(s);
+ if (write(op->outfd, out->str, out->len) == -1)
+ ERR("SRD_OUTPUT_PYTHON callback write failure!");
+ DBG("wrote '%s'", out->str);
+ g_string_free(out, TRUE);
+
+}
+
+static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data)
+{
+ struct srd_proto_data_binary *pdb;
+ struct output *op;
+ GString *out;
+ unsigned int i;
+
+ DBG("Binary output from %s", pdata->pdo->di->inst_id);
+ op = cb_data;
+ pdb = pdata->data;
+
+ if (strcmp(pdata->pdo->di->inst_id, op->pd))
+ /* This is not the PD selected for output. */
+ return;
+
+ if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
+ /*
+ * This output takes a specific binary class,
+ * but not the one that just came in.
+ */
+ return;
+
+ out = g_string_sized_new(128);
+ g_string_printf(out, "%"PRIu64"-%"PRIu64" %s:",
+ pdata->start_sample, pdata->end_sample,
+ pdata->pdo->di->inst_id);
+ for (i = 0; i < pdb->size; i++) {
+ g_string_append_printf(out, " %.2x", pdb->data[i]);
+ }
+ g_string_append(out, "\n");
+ if (write(op->outfd, out->str, out->len) == -1)
+ ERR("SRD_OUTPUT_BINARY callback write failure!");
+
+}
+