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 "sigrok.h"
#include <sigrok.h>
int sigrok_init(void)
{
int ret;
ret = load_hwplugins();
return ret;
return load_hwplugins();
}
void sigrok_cleanup(void)
{
device_close_all();
}

View File

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

11
debug.c
View File

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

113
device.c
View File

@ -19,13 +19,12 @@
#include <stdio.h>
#include <glib.h>
#include "sigrok.h"
#include <sigrok.h>
extern struct sigrok_global *global;
GSList *devices = NULL;
void device_scan(void)
{
GSList *plugins, *l;
@ -34,45 +33,36 @@ void device_scan(void)
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
* of these out of the way first.
*/
for(l = plugins; l; l = l->next)
{
for (l = plugins; l; l = l->next) {
plugin = l->data;
g_message("initializing %s plugin", plugin->name);
num_devices = plugin->init(NULL);
for(i = 0; i < num_devices; i++)
{
for (i = 0; i < num_devices; i++)
device_new(plugin, i);
}
}
}
void device_close_all(void)
{
struct device *device;
while(devices)
{
while (devices) {
device = devices->data;
device->plugin->close(device->plugin_index);
device_destroy(device);
}
}
GSList *device_list(void)
{
return devices;
}
struct device *device_new(struct device_plugin *plugin, int plugin_index)
{
struct device *device;
@ -84,71 +74,66 @@ struct device *device_new(struct device_plugin *plugin, int plugin_index)
device->plugin_index = plugin_index;
devices = g_slist_append(devices, device);
num_probes = (int) device->plugin->get_device_info(device->plugin_index, DI_NUM_PROBES);
for(i = 0; i < num_probes; i++)
{
snprintf(probename, 16, "%d", i+1);
num_probes = (int)device->plugin->get_device_info(device->plugin_index,
DI_NUM_PROBES);
for (i = 0; i < num_probes; i++) {
snprintf(probename, 16, "%d", i + 1);
device_probe_add(device, probename);
}
return 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)
for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++)
device_probe_clear(device, probenum);
if (!device->probes)
return;
for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
device_probe_clear(device, pnum);
}
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);
if(device->probes)
{
for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++)
device_probe_clear(device, probenum);
if (device->probes) {
for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
device_probe_clear(device, pnum);
g_slist_free(device->probes);
}
g_free(device);
}
void device_probe_clear(struct device *device, int probenum)
{
struct probe *p;
p = probe_find(device, probenum);
if(!p)
if (!p)
return;
if(p->name)
{
if (p->name) {
g_free(p->name);
p->name = NULL;
}
if(p->trigger)
{
if (p->trigger) {
g_free(p->trigger);
p->trigger = NULL;
}
}
void device_probe_add(struct device *device, char *name)
{
struct probe *p;
@ -159,21 +144,17 @@ void device_probe_add(struct device *device, char *name)
p->name = g_strdup(name);
p->trigger = NULL;
device->probes = g_slist_append(device->probes, p);
}
struct probe *probe_find(struct device *device, int probenum)
{
GSList *l;
struct probe *p, *found_probe;
found_probe = NULL;
for(l = device->probes; l; l = l->next)
{
for (l = device->probes; l; l = l->next) {
p = l->data;
if(p->index == probenum)
{
if (p->index == probenum) {
found_probe = p;
break;
}
@ -182,54 +163,46 @@ struct probe *probe_find(struct device *device, int probenum)
return found_probe;
}
void device_probe_name(struct device *device, int probenum, char *name)
{
struct probe *p;
p = probe_find(device, probenum);
if(!p)
if (!p)
return;
if(p->name)
if (p->name)
g_free(p->name);
p->name = g_strdup(name);
}
void device_trigger_clear(struct device *device)
{
struct probe *p;
unsigned int probenum;
unsigned int pnum;
if(device->probes)
for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++)
{
p = probe_find(device, probenum);
if(p && p->trigger)
{
g_free(p->trigger);
p->trigger = NULL;
}
if (!device->probes)
return;
for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
p = probe_find(device, pnum);
if (p && p->trigger) {
g_free(p->trigger);
p->trigger = NULL;
}
}
}
void device_trigger_set(struct device *device, int probenum, char *trigger)
{
struct probe *p;
p = probe_find(device, probenum);
if(!p)
if (!p)
return;
if(p->trigger)
if (p->trigger)
g_free(p->trigger);
p->trigger = g_strdup(trigger);
}

View File

@ -76,7 +76,7 @@ struct protocol {
};
/*
* datafeed
* Datafeed
*/
/* datafeed_packet.type values */
@ -107,7 +107,7 @@ struct datafeed_header {
};
/*
* output
* Output
*/
struct output {
struct output_format *format;
@ -217,13 +217,13 @@ void device_trigger_set(struct device *device, int probenum, char *trigger);
/* Hardware plugin capabilities */
enum {
HWCAP_DUMMY, // used to terminate lists
HWCAP_DUMMY, /* Used to terminate lists */
HWCAP_LOGIC_ANALYZER,
HWCAP_SAMPLERATE, // change samplerate
HWCAP_PROBECONFIG, // configure probe mask
HWCAP_CAPTURE_RATIO, // set pre-trigger / post-trigger ratio
HWCAP_LIMIT_MSEC, // set a time limit for sample acquisition
HWCAP_LIMIT_SAMPLES, // set a limit on number of samples
HWCAP_SAMPLERATE, /* Change samplerate */
HWCAP_PROBECONFIG, /* Configure probe mask */
HWCAP_CAPTURE_RATIO, /* Set pre-trigger / post-trigger ratio */
HWCAP_LIMIT_MSEC, /* Set a time limit for sample acquisition */
HWCAP_LIMIT_SAMPLES, /* Set a limit on number of samples */
};
struct hwcap_option {
@ -293,8 +293,9 @@ enum {
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.
*/
struct samplerates {
@ -305,13 +306,13 @@ struct samplerates {
};
struct device_plugin {
/* plugin-specific */
/* Plugin-specific */
char *name;
int api_version;
int (*init) (char *deviceinfo);
void (*cleanup) (void);
/* device-specific */
/* Device-specific */
int (*open) (int device_index);
void (*close) (int device_index);
void *(*get_device_info) (int device_index, int device_info_id);
@ -337,7 +338,8 @@ GSList *list_hwplugins(void);
/* Generic device instances */
struct sigrok_device_instance *sigrok_device_instance_new(int index,
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);
/* USB-specific instances */
@ -352,7 +354,8 @@ void serial_device_instance_free(struct serial_device_instance *serial);
int find_hwcap(int *capabilities, int hwcap);
struct hwcap_option *find_hwcap_option(int hwcap);
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 -------------------------------------------------------------*/
@ -367,7 +370,7 @@ struct session {
GSList *devices;
/* List of struct analyzer* */
GSList *analyzers;
/* datafeed callbacks */
/* Datafeed callbacks */
GSList *datafeed_callbacks;
GTimeVal starttime;
};
@ -406,6 +409,7 @@ int serial_open(const char *pathname, int flags);
int serial_close(int fd);
void *serial_backup_params(int fd);
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