g_message("ols: sending cmd 0x%.2x", command);
buf[0] = command;
if(write(fd, buf, 1) != 1)
- return SIGROK_NOK;
+ return SIGROK_ERR;
return SIGROK_OK;
}
buf[3] = data & 0xff0000 >> 16;
buf[4] = data & 0xff000000 >> 24;
if(write(fd, buf, 5) != 5)
- return SIGROK_NOK;
+ return SIGROK_ERR;
return SIGROK_OK;
}
if(stage > 3)
{
/* only supporting parallel mode, with up to 4 stages */
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
}
}
struct sigrok_device_instance *sdi;
if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
- return SIGROK_NOK;
+ return SIGROK_ERR;
sdi->serial->fd = open(sdi->serial->port, O_RDWR);
if(sdi->serial->fd == -1)
- return SIGROK_NOK;
+ return SIGROK_ERR;
sdi->status = ST_ACTIVE;
uint32_t divider;
if(samplerate < samplerates.low || samplerate > samplerates.high)
- return SIGROK_ERR_BADVALUE;
+ return SIGROK_ERR_SAMPLERATE;
if(samplerate > CLOCK_RATE) {
flag_reg |= FLAG_DEMUX;
g_message("setting samplerate to %"PRIu64" Hz (divider %u, demux %s)", samplerate, divider,
flag_reg & FLAG_DEMUX ? "on" : "off");
if(send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, divider) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
cur_samplerate = samplerate;
return SIGROK_OK;
uint64_t *tmp_u64;
if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
- return SIGROK_NOK;
+ return SIGROK_ERR;
if(sdi->status != ST_ACTIVE)
- return SIGROK_NOK;
+ return SIGROK_ERR;
if(capability == HWCAP_SAMPLERATE) {
tmp_u64 = value;
capture_ratio = strtol(value, NULL, 10);
if(capture_ratio < 0 || capture_ratio > 100) {
capture_ratio = 0;
- ret = SIGROK_NOK;
+ ret = SIGROK_ERR;
}
else
ret = SIGROK_OK;
}
else
- ret = SIGROK_NOK;
+ ret = SIGROK_ERR;
return ret;
}
uint32_t data;
if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
- return SIGROK_NOK;
+ return SIGROK_ERR;
if(sdi->status != ST_ACTIVE)
- return SIGROK_NOK;
+ return SIGROK_ERR;
/* reset again */
if(send_longcommand(sdi->serial->fd, CMD_RESET, 0) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
/* send flag register */
data = flag_reg << 24;
if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
/* send sample limit and pre/post-trigger capture ratio */
data = limit_samples / 4 << 16;
data |= (limit_samples - (limit_samples / 100 * capture_ratio)) / 4;
data = 0x00190019;
if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
/* trigger masks */
if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
/* trigger configuration */
/* TODO: the start flag should only be on the last used stage I think... */
if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK)
-// return SIGROK_NOK;
+// return SIGROK_ERR;
set_configuration_samplerate(sdi, cur_samplerate);
/* start acquisition on the device */
if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
- return SIGROK_NOK;
+ return SIGROK_ERR;
source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id);
packet = g_malloc(sizeof(struct datafeed_packet));
header = g_malloc(sizeof(struct datafeed_header));
if(!packet || !header)
- return SIGROK_NOK;
+ return SIGROK_ERR;
packet->type = DF_HEADER;
packet->length = sizeof(struct datafeed_header);
packet->payload = (unsigned char *) header;
trigger_value[stage] |= probe_bit;
stage++;
if(stage > NUM_TRIGGER_STAGES)
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
}
}
if( !(sdi = sl_open_device(device_index)) ) {
g_warning("unable to open device");
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
if(err != 0) {
g_warning("Unable to claim interface: %d", err);
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
if(cur_samplerate == 0) {
/* sample rate hasn't been set; default to the slowest it has */
- if(hw_set_configuration(device_index, HWCAP_SAMPLERATE, &supported_samplerates[0]) == SIGROK_NOK)
- return SIGROK_NOK;
+ if(hw_set_configuration(device_index, HWCAP_SAMPLERATE, &supported_samplerates[0]) == SIGROK_ERR)
+ return SIGROK_ERR;
}
return SIGROK_OK;
break;
}
if(supported_samplerates[i] == 0)
- return SIGROK_ERR_BADVALUE;
+ return SIGROK_ERR_SAMPLERATE;
divider = (uint8_t) (48 / (float) (samplerate/1000000)) - 1;
ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT, buf, 2, &result, 500);
if(ret != 0) {
g_warning("failed to set samplerate: %d", ret);
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
cur_samplerate = samplerate;
uint64_t *tmp_u64;
if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
- return SIGROK_NOK;
+ return SIGROK_ERR;
if(capability == HWCAP_SAMPLERATE) {
tmp_u64 = value;
ret = SIGROK_OK;
}
else
- ret = SIGROK_NOK;
+ ret = SIGROK_ERR;
return ret;
}
unsigned char *buf;
if( !(sdi = get_sigrok_device_instance(device_instances, device_index)))
- return SIGROK_NOK;
+ return SIGROK_ERR;
packet = g_malloc(sizeof(struct datafeed_packet));
header = g_malloc(sizeof(struct datafeed_header));
if(!packet || !header)
- return SIGROK_NOK;
+ return SIGROK_ERR;
/* start with 2K transfer, subsequently increased to 4K */
size = 2048;
/* TODO: free them all */
libusb_free_transfer(transfer);
g_free(buf);
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
size = 4096;
}
trigger_value[stage] |= probe_bit;
stage++;
if(stage > NUM_TRIGGER_STAGES)
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
}
}
if( !(sdi = zp_open_device(device_index)) ) {
g_warning("unable to open device");
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
if(err != 0) {
g_warning("Unable to claim interface: %d", err);
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
analyzer_reset(sdi->usb->devhdl);
analyzer_initialize(sdi->usb->devhdl);
if(cur_samplerate == 0) {
/* sample rate hasn't been set; default to the slowest it has */
- if(hw_set_configuration(device_index, HWCAP_SAMPLERATE, &samplerates.low) == SIGROK_NOK)
- return SIGROK_NOK;
+ if(hw_set_configuration(device_index, HWCAP_SAMPLERATE, &samplerates.low) == SIGROK_ERR)
+ return SIGROK_ERR;
}
return SIGROK_OK;
uint64_t *tmp_u64;
if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
- return SIGROK_NOK;
+ return SIGROK_ERR;
switch (capability) {
case HWCAP_SAMPLERATE:
return SIGROK_OK;
default:
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
}
unsigned char *buf;
if( !(sdi = get_sigrok_device_instance(device_instances, device_index)))
- return SIGROK_NOK;
+ return SIGROK_ERR;
analyzer_start(sdi->usb->devhdl);
g_message("Waiting for data");
buf = g_malloc(PACKET_SIZE);
if (!buf)
- return SIGROK_NOK;
+ return SIGROK_ERR;
analyzer_read_start(sdi->usb->devhdl);
/* send the incoming transfer to the session bus */
for(packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE); packet_num++) {
char sbuf[10], wbuf[1000];
ctx = malloc(sizeof(struct context));
+ if (ctx == NULL)
+ return SIGROK_ERR_MALLOC;
o->internal = ctx;
ctx->num_enabled_probes = 0;
for (l = o->device->probes; l; l = l->next) {
/* TODO: Allow for configuration via o->param. */
ctx->header = calloc(1, MAX_HEADER_LEN + 1);
+ if (ctx->header == NULL)
+ return SIGROK_ERR_MALLOC;
num_probes = g_slist_length(o->device->probes);
+ /* TODO: Handle num_probes == 0, too many probes, etc. */
samplerate = *((uint64_t *) o->device->plugin->get_device_info(
o->device->plugin_index, DI_CUR_SAMPLERATE));
b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
PACKAGE_STRING, ctx->num_enabled_probes, num_probes,
(char *)&sbuf, 1, "ns", PACKAGE, (char *)&wbuf);
+ /* TODO: Handle snprintf errors. */
ctx->prevbits = calloc(sizeof(int), num_probes);
+ if (ctx->prevbits == NULL)
+ return SIGROK_ERR_MALLOC;
return 0;
}
case DF_END:
outlen = strlen("$dumpoff\n$end\n");
outbuf = malloc(outlen + 1);
+ if (outbuf == NULL)
+ return SIGROK_ERR_MALLOC;
snprintf(outbuf, outlen, "$dumpoff\n$end\n");
*data_out = outbuf;
*length_out = outlen;
ctx = o->internal;
outsize = strlen(ctx->header);
outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
+ if (outbuf == NULL)
+ return SIGROK_ERR_MALLOC;
if (ctx->header) {
/* The header is still here, this must be the first packet. */
strncpy(outbuf, ctx->header, outsize);
unlink(filename);
if( !(zipfile = zip_open(filename, ZIP_CREATE, &error)) )
- return SIGROK_NOK;
+ return SIGROK_ERR;
/* version */
version[0] = '1';
if( !(src = zip_source_buffer(zipfile, version, 1, 0)) )
- return SIGROK_NOK;
+ return SIGROK_ERR;
if(zip_add(zipfile, "version", src) == -1) {
g_message("error saving version into zipfile: %s", zip_strerror(zipfile));
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
/* metadata */
strcpy(metafile, "sigrok-meta-XXXXXX");
if( (tmpfile = g_mkstemp(metafile)) == -1)
- return SIGROK_NOK;
+ return SIGROK_ERR;
close(tmpfile);
make_metadata(metafile);
if( !(src = zip_source_file(zipfile, metafile, 0, -1)) )
- return SIGROK_NOK;
+ return SIGROK_ERR;
if(zip_add(zipfile, "metadata", src) == -1)
- return SIGROK_NOK;
+ return SIGROK_ERR;
unlink(metafile);
/* raw */
bufcnt += DATASTORE_CHUNKSIZE;
}
if( !(src = zip_source_buffer(zipfile, buf, ds->num_units * ds->ds_unitsize, TRUE)) )
- return SIGROK_NOK;
+ return SIGROK_ERR;
snprintf(rawname, 15, "raw-%d", devcnt);
if(zip_add(zipfile, rawname, src) == -1)
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
devcnt++;
}
if( (ret = zip_close(zipfile)) == -1) {
g_message("error saving zipfile: %s", zip_strerror(zipfile));
- return SIGROK_NOK;
+ return SIGROK_ERR;
}
return SIGROK_OK;
}
-
-
-
-
-
-
-
-
#include <glib.h>
#include <libusb.h>
-/* Returned status/error codes */
-#define SIGROK_STATUS_DISABLED 0
-#define SIGROK_OK 1
-#define SIGROK_NOK 2
-#define SIGROK_ERR_BADVALUE 20
+/*
+ * Status/error codes returned by libsigrok functions.
+ *
+ * All possible return codes of libsigrok functions must be listed here.
+ * Functions should never return hardcoded numbers as status, but rather
+ * use these #defines instead. All error codes are negative numbers.
+ *
+ * The error codes are globally unique in libsigrok, i.e. if one of the
+ * libsigrok function returns a "malloc error" it must be exactly the same
+ * return value as used for all other functions to indicate "malloc error".
+ * There must be no functions which indicate two different errors via the
+ * same return code.
+ *
+ * Also, for compatibility reasons, no defined return codes are ever removed
+ * or reused for different #defines later. You can only add new #defines and
+ * return codes, but never remove or redefine existing ones.
+ */
+#define SIGROK_OK 0 /* No error */
+#define SIGROK_ERR -1 /* Generic/unspecified error */
+#define SIGROK_ERR_MALLOC -2 /* Malloc/calloc/realloc error */
+#define SIGROK_ERR_SAMPLERATE -3 /* Incorrect samplerate */
/* Handy little macros */
#define KHZ(n) (n * 1000)