Skip to content

Commit

Permalink
Connection mode now selectable
Browse files Browse the repository at this point in the history
- test() renamed to info()
- added ev3 send
- ls default is now /
- Makefile now uses $PREFIX; libusb needs to be recompiled and an arm
port should work
  • Loading branch information
a3f committed Aug 17, 2015
1 parent be660ac commit 64946ee
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 152 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ doc/
html/
latex/
s/

tags
37 changes: 28 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FLAGS += -std=c99 -Wall -Wextra -DVERSION='"$(VERSION)"'
SRCDIR = src
OBJDIR = build

SRCS = src/main.c src/packets.c src/run.c src/test.c src/up.c src/ls.c src/rm.c src/mkdir.c src/mkrbf.c src/dl.c src/listen.c
SRCS = src/main.c src/packets.c src/run.c src/info.c src/up.c src/ls.c src/rm.c src/mkdir.c src/mkrbf.c src/dl.c src/listen.c src/send.c

INC += -Ihidapi/hidapi/

Expand All @@ -28,14 +28,17 @@ RM = del /Q
endif

## Win32
ifneq ($(MAKECMDGOALS),cross)
FLAGS += -DCONFIGURATION='"HIDAPI/hid.dll"' -DSYSTEM="Windows"
# TODO: remove all %zu prints altogether?
FLAGS += -Wno-unused-value -D__USE_MINGW_ANSI_STDIO=1
SRCS += src/bt-win.c
HIDSRC += hidapi/windows/hid.c
LDFLAGS += -mwindows -lsetupapi -municode
BIN_NAME := $(addsuffix .exe, $(BIN_NAME))

# CodeSourcery prefix
endif
CROSS_PREFIX = arm-none-linux-gnueabi-g
else
UNAME = $(shell uname -s)

Expand All @@ -46,55 +49,71 @@ HIDSRC += hidapi/libusb/hid.c
HIDFLAGS += `pkg-config libusb-1.0 --cflags`
LDFLAGS += `pkg-config libusb-1.0 --libs` -lrt -lpthread
INSTALL = $(shell sh udev.sh)
# Linaro prefix
CROSS_PREFIX = arm-linux-gnueabi-g
endif

## OS X
ifeq ($(UNAME),Darwin)
ifneq ($(MAKECMDGOALS),cross)
FLAGS += -DCONFIGURATION='"HIDAPI/IOHidManager"' -DSYSTEM='"OS X"'
HIDSRC += hidapi/mac/hid.c
LDFLAGS += -framework IOKit -framework CoreFoundation
# minot prefix
endif
CROSS_PREFIX = arm-none-linux-gnueabi-g
endif

## BSD
ifeq ($(findstring BSD, $(UNAME)), BSD)
ifneq ($(MAKECMDGOALS),cross)
FLAGS += -DCONFIGURATION='"HIDAPI/libusb-1.0"' -DSYSTEM='"BSD"'
HIDSRC += hidapi/libusb/hid.c
LDFLAGS += -L/usr/local/lib -lusb -liconv -pthread
INC += -I/usr/local/include
endif
endif

## ALL UNICES
SRCS += src/bt-unix.c
SRCS += src/tcp-unix.c
SRCS += src/tunnel.c
endif

CROSS_PREFIX ?= arm-linux-gnueabi-g

OBJS = $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

.DEFAULT: all
all: binary
all: $(BIN_NAME)

binary: $(OBJS) $(OBJDIR)/hid.o
$(CC) $(OBJS) $(OBJDIR)/hid.o $(LDFLAGS) $(LIBS) -o $(BIN_NAME)
$(BIN_NAME): $(OBJS) $(OBJDIR)/hid.o
$(PREFIX)$(CC) $(OBJS) $(OBJDIR)/hid.o $(LDFLAGS) $(LIBS) -o $(BIN_NAME)

# static enables valgrind to act better -DDEBUG!
$(OBJS): $(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) -c $< -MMD $(FLAGS) $(INC) -o $@
$(PREFIX)$(CC) -c $< -MMD $(FLAGS) $(INC) -o $@
-include $(OBJDIR)/*.d

$(OBJDIR)/hid.o: $(HIDSRC)
$(CC) -c $< -o $@ $(INC) $(HIDFLAGS)
$(PREFIX)$(CC) -c $< -o $@ $(INC) $(HIDFLAGS)


debug: FLAGS += -g
debug: LIBS := $(LIBS)
debug: LIBS +=
debug: binary
debug: $(BIN_NAME)

cross: PREFIX ?= $(CROSS_PREFIX)
cross: FLAGS += -DCONFIGURATION='"HIDAPI/libusb-1.0"' -DSYSTEM='"Linux"'
cross: HIDSRC += hidapi/libusb/hid.c
cross: HIDFLAGS += `pkg-config libusb-1.0 --cflags`
cross: LDFLAGS += `pkg-config libusb-1.0 --libs` -lrt -lpthread
cross: $(BIN_NAME)

# linux only for now, installs udev rules, for rootless access to ev3
.PHONY: install
install: binary ev3-udev.rules udev.sh
install: $(BIN_NAME) ev3-udev.rules udev.sh
$(INSTALL)

.PHONY: clean
Expand Down
14 changes: 10 additions & 4 deletions moveEv3.pl
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@
{
$ch = getch();
addstr(2, 0, " ");
if ($ch == KEY_UP) { addstr(2, 0, "KEY_UP");
if ($ch == KEY_UP) { addstr(2, 0, "CHARGE!");
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 09 50 A6 00 09\n";
}
elsif ($ch == KEY_DOWN) { addstr(2, 0, "KEY_DOWN");
elsif ($ch == KEY_SPACE) { addstr(2, 0, "STOP!");
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
}
elsif ($ch == KEY_LEFT) { addstr(2, 0, "KEY_LEFT");
elsif ($ch == KEY_LEFT) { addstr(2, 0, "LEFT!");
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 08 50 A6 00 08\n";
}
elsif ($ch == KEY_RIGHT) { addstr(2, 0, "KEY_RIGHT");
elsif ($ch == KEY_RIGHT) { addstr(2, 0, "RIGHT!");
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 01 50 A6 00 01\n";
}
elsif ($ch == KEY_DOWN) { addstr(2, 0, "RETREAT!");
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A3 00 09 B0 A6 00 09\n";
}

refresh;
}
END {endwin}
Expand Down
14 changes: 4 additions & 10 deletions src/bt-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <stdlib.h>
#include <errno.h>

#include <signal.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/stat.h>

Expand All @@ -29,7 +27,6 @@
enum {READ=0, WRITE=1};
void *bt_open(const char *file, const char*file2)
{
// signal(SIGINT, handle_sigint);
int *fd = malloc(2*sizeof(int));
struct stat buf;
fd[0] = fd[1] = open(file ?: BT, O_RDWR);
Expand Down Expand Up @@ -68,11 +65,6 @@ int bt_write(void* fd_, const u8* buf, size_t count)
}
return sent;
}
static jmp_buf env;
static void handle_alarm(int sig)
{
(void)sig; longjmp(env, 1);
}

/**
* \param [in] device handle returned by bt_open()
Expand All @@ -85,13 +77,14 @@ static void handle_alarm(int sig)
*/
int bt_read(void* fd_, u8* buf, size_t count, int milliseconds)
{
(void) milliseconds;
int fd = ((int*)fd_)[READ];
size_t recvd =0;
size_t packet_len = 2;
ssize_t ret;
do {
ret = read(fd, buf+recvd, packet_len-recvd);
if (ret == -1)
if (ret <= 0)
{
perror("read failed"); return -1;
}
Expand All @@ -105,12 +98,13 @@ int bt_read(void* fd_, u8* buf, size_t count, int milliseconds)
for (ssize_t ret=recvd; recvd < packet_len; recvd += ret)
{
ret = read(fd, buf+recvd, packet_len-recvd);
if (ret == 0) break; // EOF; turning off bluetooth during transfer
//FIXME: would returning -1 for EOF make more sense?
if (ret == -1)
{
perror("read failed");
return -1;
}
// bug one should handle disconnects during transfer. if this happens read keeps returning zeros
}

return recvd;
Expand Down
4 changes: 2 additions & 2 deletions src/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern int up(FILE* loc, const char *rem);
//! download remote source \p rem to local file \p loc
extern int dl(const char *rem, FILE* loc);

//! print HID information, beep and exit
extern int test(void);
//! print connection information, beep and exit
extern int info(const char *arg);

//! run remote .rbf file \p rem via VM
extern int run(const char *rem);
Expand Down
21 changes: 17 additions & 4 deletions src/test.c → src/info.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file test.c
* @file info.c
* @author Ahmad Fatoum
* @copyright (c) 2015 Ahmad Fatoum. Code available under terms of the GNU General Public License 3.0
* @brief test function - beeps and prints info
* @brief info function - beeps and prints info
*/

#include <stdio.h>
Expand Down Expand Up @@ -32,8 +32,22 @@ static const u8 tone[] = "\x0\x0F\x00\0\0\x80\x00\x00\x94\x01\x81\x02\x82\xE8\x0
* \bug needs more detailed information for bluetooth/wlan
* should print COM port, device name
*/
int test()
int info(const char*arg)
{
wchar_t wstr[MAX_STR];
if (arg)
{
if ((void*)ev3_write != (void*)hid_write)
return ERR_ARG;
if (strcmp(arg, "serial") == 0)
{
hid_get_serial_number_string(handle, wstr, MAX_STR);
fputws(wstr, stdout);
fputwc(L'\n', stdout);
return ERR_UNK;
}
}

int res;
//TODO:
res = ev3_write(handle, tone, sizeof tone - 1);
Expand All @@ -52,7 +66,6 @@ int test()
//! Print udev rules
printf("SUBSYSTEM==\"%s\", ATTRS{idVendor}==\"0694\", ATTRS{idProduct}==\"0005\a, MODE=\"0666\"\n", isOld ? "usb_device" : "usb");
#endif
wchar_t wstr[MAX_STR];
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
printf("Manufacturer String: %ls\n", wstr);
res = hid_get_product_string(handle, wstr, MAX_STR);
Expand Down
2 changes: 1 addition & 1 deletion src/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int listen()
{
u8 buffer[1280] = {0};
int read;
while ( (read = hid_read(handle,buffer, sizeof buffer) > 0))
while ( (read = hid_read(handle,buffer, sizeof buffer)) > 0)
{
printf("%d bytes read.\n", read);
//print_bytes(buffer, read);
Expand Down
2 changes: 1 addition & 1 deletion src/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int ls(const char *path)
errmsg = "`LIST_FILES` was denied.";
return ERR_VM;
}
printf("listrep->packetLen=%hu, res=%d\n", listrep->packetLen, res);
//printf("listrep->packetLen=%hu, res=%d\n", listrep->packetLen, res);
//FIXME: buffer overrun
fwrite(listrep->list, 1, listrep->packetLen - 10, stdout); // No NUL Termination over Serial COM for whatever reason.
//
Expand Down
Loading

0 comments on commit 64946ee

Please sign in to comment.