try to fix segfaults

This commit is contained in:
Neale Pickett 2013-09-08 17:45:44 -06:00
parent 98c870b09b
commit d95064cf87
6 changed files with 74 additions and 33 deletions

View File

@ -1,5 +1,6 @@
CFLAGS += -Wall
CFLAGS += -Werror
CFLAGS += -g
all: hdjd aac123
@ -17,3 +18,6 @@ aac123: LDLIBS += -lfaad -lmp4v2
aactest: CFLAGS += $(shell pkg-config --cflags alsa)
aactest: LDLIBS += $(shell pkg-config --libs alsa)
aactest: LDLIBS += -lfaad -lmp4v2
clean:
rm -f hdjd *.o

63
alsa.c
View File

@ -3,11 +3,11 @@
#include <sys/select.h>
#include <alsa/asoundlib.h>
#include "alsa.h"
#include "usb.h"
#include "dump.h"
static snd_seq_t *snd_handle;
static int seq_port;
static snd_midi_event_t *midi_event_parser;
static snd_seq_event_t *ev;
int
@ -24,11 +24,6 @@ alsa_setup(const char *name)
SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_READ |
SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
if (snd_midi_event_new(256, &midi_event_parser) < 0) {
perror("snd_midi_event_new");
return -1;
}
return 0;
}
@ -56,31 +51,36 @@ alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds)
FD_SET(pfd[i].fd, rfds);
}
}
}
#define ALSA_BUFSIZE 4096
void
alsa_read_ready()
{
static snd_midi_event_t *midi_event_parser;
if (snd_midi_event_new(ALSA_BUFSIZE, &midi_event_parser) < 0) {
fprintf(stderr, "ALSA cannot allocate MIDI event parser\n");
abort();
}
for (;;) {
unsigned char buf[256];
unsigned char buf[ALSA_BUFSIZE];
long converted;
int i;
if (snd_seq_event_input(snd_handle, &ev) < 0) {
break;
}
if (!midi_event_parser) {
if (snd_midi_event_new(256, &midi_event_parser) < 0) {
continue;
}
converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev);
if (converted < 0) {
DUMP_l(converted);
} else {
usb_write(buf, converted);
}
converted = snd_midi_event_decode(midi_event_parser, buf, 256, ev);
printf(" << ");
for (i = 0; i < converted; i += 1) {
printf("%02x ", buf[i]);
}
printf(":\n");
}
}
@ -98,3 +98,30 @@ alsa_check_fds(fd_set *rfds, fd_set *wfds)
}
}
}
void
alsa_write(uint8_t *data, size_t datalen)
{
static snd_midi_event_t *midi_event_parser;
snd_seq_event_t ev;
long r;
if (snd_midi_event_new(ALSA_BUFSIZE, &midi_event_parser) < 0) {
fprintf(stderr, "ALSA cannot allocate MIDI event parser\n");
abort();
}
r = snd_midi_event_encode(midi_event_parser, data, datalen, &ev);
if (r < datalen) {
fprintf(stderr, "ALSA didn't parse that message\n");
abort();
}
if ((r = snd_seq_event_output(snd_handle, &ev)) < 0) {
fprintf(stderr, "ALSA couldn't write an event: %ld\n", r);
abort();
}
snd_seq_drain_output(snd_handle);
DUMP();
}

2
alsa.h
View File

@ -1,10 +1,12 @@
#ifndef _ALSA_H_
#define _ALSA_H_
#include <stdint.h>
#include <sys/select.h>
int alsa_setup(const char *name);
void alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds);
void alsa_read_ready();
void alsa_check_fds(fd_set *rfds, fd_set *wfds);
void alsa_write(uint8_t *data, size_t datalen);
#endif

1
dump.h
View File

@ -19,6 +19,7 @@
#endif
#define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v)
#define DUMP_l(v) DUMPf("%s = %ld", #v, v)
#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
#define DUMP_s(v) DUMPf("%s = %s", #v, v)
#define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v)

33
usb.c
View File

@ -5,6 +5,7 @@
#include <string.h>
#include <sys/select.h>
#include "usb.h"
#include "alsa.h"
#include "dump.h"
static struct libusb_device_handle *usb_dev;
@ -30,6 +31,7 @@ static void
usb_initiate_transfer()
{
// Tell libusb we want to know about bulk transfers
xfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(xfer, usb_dev, d->ep_in, data, sizeof(data), usb_xfer_done, NULL, 0);
libusb_submit_transfer(xfer);
}
@ -39,18 +41,14 @@ usb_xfer_done(struct libusb_transfer *transfer)
{
uint8_t *data = transfer->buffer;
int datalen = transfer->actual_length;
int i;
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
}
printf("\n");
alsa_write(data, datalen);
usb_initiate_transfer();
}
int
usb_setup(const char *name, size_t namelen)
usb_setup(char *name, size_t namelen)
{
if (libusb_init(NULL) < 0) {
return -1;
@ -76,16 +74,12 @@ usb_setup(const char *name, size_t namelen)
{
int ret;
struct libusb_device_descriptor ddesc;
char name_[200];
char *p = name_;
strncpy(name_, name, sizeof(name_) - 1);
name_[sizeof(name_) - 1] = 0;
libusb_get_device_descriptor(libusb_get_device(usb_dev), &ddesc);
ret = libusb_get_string_descriptor_ascii(usb_dev, ddesc.iManufacturer, (unsigned char *)name, namelen);
if (ret > 0) {
p = name_ + ret;
char *p = name + ret;
*p = ' ';
p += 1;
ret = libusb_get_string_descriptor_ascii(usb_dev, ddesc.iProduct, (unsigned char *)p, namelen - ret - 1);
@ -93,10 +87,9 @@ usb_setup(const char *name, size_t namelen)
if (ret < 0) {
printf("Warning: I can't figure out what to call this thing.\n");
}
printf("Opened a %s\n", name);
printf("Opened [%s]\n", name);
}
xfer = libusb_alloc_transfer(0);
return 0;
}
@ -139,3 +132,15 @@ usb_check_fds(fd_set *rfds, fd_set *wfds)
}
}
}
void
usb_write(uint8_t *data, size_t datalen)
{
int i;
printf("U> ");
for (i = 0; i < datalen; i += 1) {
printf("%02x ", data[i]);
}
printf("\n");
}

4
usb.h
View File

@ -1,10 +1,12 @@
#ifndef _USB_H_
#define _USB_H_
#include <stdint.h>
#include <sys/select.h>
int usb_setup(const char *name, size_t namelen);
int usb_setup(char *name, size_t namelen);
void usb_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds);
void usb_check_fds(fd_set *rfds, fd_set *wfds);
void usb_write(uint8_t *data, size_t datalen);
#endif