libsigrok: Coding style fixes.

This commit is contained in:
Uwe Hermann 2010-04-15 20:07:16 +02:00
parent d86dc674a2
commit 1b452b8510
5 changed files with 82 additions and 122 deletions

View File

@ -18,23 +18,14 @@
*/ */
#include <glib.h> #include <glib.h>
#include "sigrok.h" #include <sigrok.h>
int sigrok_init(void) int sigrok_init(void)
{ {
int ret; return load_hwplugins();
ret = load_hwplugins();
return ret;
} }
void sigrok_cleanup(void) void sigrok_cleanup(void)
{ {
device_close_all(); device_close_all();
} }

View File

@ -21,12 +21,10 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <glib.h> #include <glib.h>
#include "sigrok.h" #include <sigrok.h>
static gpointer new_chunk(struct datastore **ds); static gpointer new_chunk(struct datastore **ds);
struct datastore *datastore_new(int unitsize) struct datastore *datastore_new(int unitsize)
{ {
struct datastore *ds; struct datastore *ds;
@ -39,20 +37,18 @@ struct datastore *datastore_new(int unitsize)
return ds; return ds;
} }
void datastore_destroy(struct datastore *ds) void datastore_destroy(struct datastore *ds)
{ {
GSList *chunk; GSList *chunk;
for(chunk = ds->chunklist; chunk; chunk = chunk->next) for (chunk = ds->chunklist; chunk; chunk = chunk->next)
g_free(chunk->data); g_free(chunk->data);
g_slist_free(ds->chunklist); g_slist_free(ds->chunklist);
g_free(ds); g_free(ds);
} }
void datastore_put(struct datastore *ds, void *data, unsigned int length,
void datastore_put(struct datastore *ds, void *data, unsigned int length, int in_unitsize, int *probelist) int in_unitsize, int *probelist)
{ {
unsigned int stored; unsigned int stored;
int capacity, size, num_chunks, chunk_bytes_free, chunk_offset; int capacity, size, num_chunks, chunk_bytes_free, chunk_offset;
@ -62,36 +58,37 @@ void datastore_put(struct datastore *ds, void *data, unsigned int length, int in
in_unitsize = in_unitsize; in_unitsize = in_unitsize;
probelist = probelist; probelist = probelist;
if(ds->chunklist == NULL) if (ds->chunklist == NULL)
chunk = new_chunk(&ds); chunk = new_chunk(&ds);
else else
chunk = g_slist_last(ds->chunklist)->data; chunk = g_slist_last(ds->chunklist)->data;
num_chunks = g_slist_length(ds->chunklist); num_chunks = g_slist_length(ds->chunklist);
capacity = (num_chunks * DATASTORE_CHUNKSIZE); capacity = (num_chunks * DATASTORE_CHUNKSIZE);
chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units); chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units);
chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) - chunk_bytes_free; chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1))
- chunk_bytes_free;
stored = 0; stored = 0;
while(stored < length) { while (stored < length) {
if(chunk_bytes_free == 0) { if (chunk_bytes_free == 0) {
chunk = new_chunk(&ds); chunk = new_chunk(&ds);
chunk_bytes_free = DATASTORE_CHUNKSIZE; chunk_bytes_free = DATASTORE_CHUNKSIZE;
chunk_offset = 0; chunk_offset = 0;
} }
if(length - stored > (unsigned int)chunk_bytes_free) if (length - stored > (unsigned int)chunk_bytes_free)
size = chunk_bytes_free; size = chunk_bytes_free;
else else
/* last part, won't fill up this chunk */ /* Last part, won't fill up this chunk. */
size = length - stored; size = length - stored;
memcpy(chunk + chunk_offset, data + stored, size); memcpy(chunk + chunk_offset, data + stored, size);
chunk_bytes_free -= size; chunk_bytes_free -= size;
stored += size; stored += size;
} }
ds->num_units += stored / ds->ds_unitsize; ds->num_units += stored / ds->ds_unitsize;
} }
static gpointer new_chunk(struct datastore **ds) static gpointer new_chunk(struct datastore **ds)
{ {
gpointer chunk; gpointer chunk;
@ -101,5 +98,3 @@ static gpointer new_chunk(struct datastore **ds)
return chunk; return chunk;
} }

11
debug.c
View File

@ -18,22 +18,19 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include "sigrok.h" #include <sigrok.h>
void hexdump(unsigned char *address, int length) void hexdump(unsigned char *address, int length)
{ {
int i; int i;
for(i = 0; i < length; i++) for (i = 0; i < length; i++) {
{ if ((i & 0x0f) == 0) {
if((i & 0x0f) == 0) if (i)
{
if(i)
printf("\n"); printf("\n");
printf("%.4x ", i); printf("%.4x ", i);
} }
printf("%.2x ", address[i]); printf("%.2x ", address[i]);
} }
printf("\n"); printf("\n");
} }

107
device.c
View File

@ -19,13 +19,12 @@
#include <stdio.h> #include <stdio.h>
#include <glib.h> #include <glib.h>
#include "sigrok.h" #include <sigrok.h>
extern struct sigrok_global *global; extern struct sigrok_global *global;
GSList *devices = NULL; GSList *devices = NULL;
void device_scan(void) void device_scan(void)
{ {
GSList *plugins, *l; GSList *plugins, *l;
@ -34,45 +33,36 @@ void device_scan(void)
plugins = list_hwplugins(); plugins = list_hwplugins();
/* initialize all plugins first. Since the init() call may involve /*
* Initialize all plugins first. Since the init() call may involve
* a firmware upload and associated delay, we may as well get all * a firmware upload and associated delay, we may as well get all
* of these out of the way first. * of these out of the way first.
*/ */
for(l = plugins; l; l = l->next) for (l = plugins; l; l = l->next) {
{
plugin = l->data; plugin = l->data;
g_message("initializing %s plugin", plugin->name); g_message("initializing %s plugin", plugin->name);
num_devices = plugin->init(NULL); num_devices = plugin->init(NULL);
for(i = 0; i < num_devices; i++) for (i = 0; i < num_devices; i++)
{
device_new(plugin, i); device_new(plugin, i);
} }
}
} }
void device_close_all(void) void device_close_all(void)
{ {
struct device *device; struct device *device;
while(devices) while (devices) {
{
device = devices->data; device = devices->data;
device->plugin->close(device->plugin_index); device->plugin->close(device->plugin_index);
device_destroy(device); device_destroy(device);
} }
} }
GSList *device_list(void) GSList *device_list(void)
{ {
return devices; return devices;
} }
struct device *device_new(struct device_plugin *plugin, int plugin_index) struct device *device_new(struct device_plugin *plugin, int plugin_index)
{ {
struct device *device; struct device *device;
@ -84,71 +74,66 @@ struct device *device_new(struct device_plugin *plugin, int plugin_index)
device->plugin_index = plugin_index; device->plugin_index = plugin_index;
devices = g_slist_append(devices, device); devices = g_slist_append(devices, device);
num_probes = (int) device->plugin->get_device_info(device->plugin_index, DI_NUM_PROBES); num_probes = (int)device->plugin->get_device_info(device->plugin_index,
for(i = 0; i < num_probes; i++) DI_NUM_PROBES);
{ for (i = 0; i < num_probes; i++) {
snprintf(probename, 16, "%d", i+1); snprintf(probename, 16, "%d", i + 1);
device_probe_add(device, probename); device_probe_add(device, probename);
} }
return device; return device;
} }
void device_clear(struct device *device) void device_clear(struct device *device)
{ {
unsigned int probenum; unsigned int pnum;
/* TODO: plugin-specific clear call? */ /* TODO: Plugin-specific clear call? */
if(device->probes) if (!device->probes)
for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++) return;
device_probe_clear(device, probenum);
for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
device_probe_clear(device, pnum);
} }
void device_destroy(struct device *device) void device_destroy(struct device *device)
{ {
unsigned int probenum; unsigned int pnum;
/* TODO: plugin-specific destroy call, need to decrease refcount in plugin */ /*
* TODO: Plugin-specific destroy call, need to decrease refcount
* in plugin.
*/
devices = g_slist_remove(devices, device); devices = g_slist_remove(devices, device);
if(device->probes) if (device->probes) {
{ for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++) device_probe_clear(device, pnum);
device_probe_clear(device, probenum);
g_slist_free(device->probes); g_slist_free(device->probes);
} }
g_free(device); g_free(device);
} }
void device_probe_clear(struct device *device, int probenum) void device_probe_clear(struct device *device, int probenum)
{ {
struct probe *p; struct probe *p;
p = probe_find(device, probenum); p = probe_find(device, probenum);
if(!p) if (!p)
return; return;
if(p->name) if (p->name) {
{
g_free(p->name); g_free(p->name);
p->name = NULL; p->name = NULL;
} }
if(p->trigger) if (p->trigger) {
{
g_free(p->trigger); g_free(p->trigger);
p->trigger = NULL; p->trigger = NULL;
} }
} }
void device_probe_add(struct device *device, char *name) void device_probe_add(struct device *device, char *name)
{ {
struct probe *p; struct probe *p;
@ -159,21 +144,17 @@ void device_probe_add(struct device *device, char *name)
p->name = g_strdup(name); p->name = g_strdup(name);
p->trigger = NULL; p->trigger = NULL;
device->probes = g_slist_append(device->probes, p); device->probes = g_slist_append(device->probes, p);
} }
struct probe *probe_find(struct device *device, int probenum) struct probe *probe_find(struct device *device, int probenum)
{ {
GSList *l; GSList *l;
struct probe *p, *found_probe; struct probe *p, *found_probe;
found_probe = NULL; found_probe = NULL;
for(l = device->probes; l; l = l->next) for (l = device->probes; l; l = l->next) {
{
p = l->data; p = l->data;
if(p->index == probenum) if (p->index == probenum) {
{
found_probe = p; found_probe = p;
break; break;
} }
@ -182,54 +163,46 @@ struct probe *probe_find(struct device *device, int probenum)
return found_probe; return found_probe;
} }
void device_probe_name(struct device *device, int probenum, char *name) void device_probe_name(struct device *device, int probenum, char *name)
{ {
struct probe *p; struct probe *p;
p = probe_find(device, probenum); p = probe_find(device, probenum);
if(!p) if (!p)
return; return;
if(p->name) if (p->name)
g_free(p->name); g_free(p->name);
p->name = g_strdup(name); p->name = g_strdup(name);
} }
void device_trigger_clear(struct device *device) void device_trigger_clear(struct device *device)
{ {
struct probe *p; struct probe *p;
unsigned int probenum; unsigned int pnum;
if(device->probes) if (!device->probes)
for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++) return;
{
p = probe_find(device, probenum); for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
if(p && p->trigger) p = probe_find(device, pnum);
{ if (p && p->trigger) {
g_free(p->trigger); g_free(p->trigger);
p->trigger = NULL; p->trigger = NULL;
} }
} }
} }
void device_trigger_set(struct device *device, int probenum, char *trigger) void device_trigger_set(struct device *device, int probenum, char *trigger)
{ {
struct probe *p; struct probe *p;
p = probe_find(device, probenum); p = probe_find(device, probenum);
if(!p) if (!p)
return; return;
if(p->trigger) if (p->trigger)
g_free(p->trigger); g_free(p->trigger);
p->trigger = g_strdup(trigger); p->trigger = g_strdup(trigger);
} }

View File

@ -76,7 +76,7 @@ struct protocol {
}; };
/* /*
* datafeed * Datafeed
*/ */
/* datafeed_packet.type values */ /* datafeed_packet.type values */
@ -107,7 +107,7 @@ struct datafeed_header {
}; };
/* /*
* output * Output
*/ */
struct output { struct output {
struct output_format *format; struct output_format *format;
@ -217,13 +217,13 @@ void device_trigger_set(struct device *device, int probenum, char *trigger);
/* Hardware plugin capabilities */ /* Hardware plugin capabilities */
enum { enum {
HWCAP_DUMMY, // used to terminate lists HWCAP_DUMMY, /* Used to terminate lists */
HWCAP_LOGIC_ANALYZER, HWCAP_LOGIC_ANALYZER,
HWCAP_SAMPLERATE, // change samplerate HWCAP_SAMPLERATE, /* Change samplerate */
HWCAP_PROBECONFIG, // configure probe mask HWCAP_PROBECONFIG, /* Configure probe mask */
HWCAP_CAPTURE_RATIO, // set pre-trigger / post-trigger ratio HWCAP_CAPTURE_RATIO, /* Set pre-trigger / post-trigger ratio */
HWCAP_LIMIT_MSEC, // set a time limit for sample acquisition HWCAP_LIMIT_MSEC, /* Set a time limit for sample acquisition */
HWCAP_LIMIT_SAMPLES, // set a limit on number of samples HWCAP_LIMIT_SAMPLES, /* Set a limit on number of samples */
}; };
struct hwcap_option { struct hwcap_option {
@ -293,8 +293,9 @@ enum {
DI_CUR_SAMPLERATE, DI_CUR_SAMPLERATE,
}; };
/* a device supports either a range of samplerates with steps of a given /*
* granularity, or is limited to a set of defined samplerates. use either * A device supports either a range of samplerates with steps of a given
* granularity, or is limited to a set of defined samplerates. Use either
* step or list, but not both. * step or list, but not both.
*/ */
struct samplerates { struct samplerates {
@ -305,13 +306,13 @@ struct samplerates {
}; };
struct device_plugin { struct device_plugin {
/* plugin-specific */ /* Plugin-specific */
char *name; char *name;
int api_version; int api_version;
int (*init) (char *deviceinfo); int (*init) (char *deviceinfo);
void (*cleanup) (void); void (*cleanup) (void);
/* device-specific */ /* Device-specific */
int (*open) (int device_index); int (*open) (int device_index);
void (*close) (int device_index); void (*close) (int device_index);
void *(*get_device_info) (int device_index, int device_info_id); void *(*get_device_info) (int device_index, int device_info_id);
@ -337,7 +338,8 @@ GSList *list_hwplugins(void);
/* Generic device instances */ /* Generic device instances */
struct sigrok_device_instance *sigrok_device_instance_new(int index, struct sigrok_device_instance *sigrok_device_instance_new(int index,
int status, char *vendor, char *model, char *version); int status, char *vendor, char *model, char *version);
struct sigrok_device_instance *get_sigrok_device_instance(GSList *device_instances, int device_index); struct sigrok_device_instance *get_sigrok_device_instance(
GSList *device_instances, int device_index);
void sigrok_device_instance_free(struct sigrok_device_instance *sdi); void sigrok_device_instance_free(struct sigrok_device_instance *sdi);
/* USB-specific instances */ /* USB-specific instances */
@ -352,7 +354,8 @@ void serial_device_instance_free(struct serial_device_instance *serial);
int find_hwcap(int *capabilities, int hwcap); int find_hwcap(int *capabilities, int hwcap);
struct hwcap_option *find_hwcap_option(int hwcap); struct hwcap_option *find_hwcap_option(int hwcap);
void source_remove(int fd); void source_remove(int fd);
void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, void *user_data); void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
void *user_data);
/*--- session.c -------------------------------------------------------------*/ /*--- session.c -------------------------------------------------------------*/
@ -367,7 +370,7 @@ struct session {
GSList *devices; GSList *devices;
/* List of struct analyzer* */ /* List of struct analyzer* */
GSList *analyzers; GSList *analyzers;
/* datafeed callbacks */ /* Datafeed callbacks */
GSList *datafeed_callbacks; GSList *datafeed_callbacks;
GTimeVal starttime; GTimeVal starttime;
}; };
@ -406,6 +409,7 @@ int serial_open(const char *pathname, int flags);
int serial_close(int fd); int serial_close(int fd);
void *serial_backup_params(int fd); void *serial_backup_params(int fd);
void serial_restore_params(int fd, void *backup); void serial_restore_params(int fd, void *backup);
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, int flowcontrol); int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
int flowcontrol);
#endif #endif