Permalink
Browse files

added build for bugduino middleware code/classes

  • Loading branch information...
1 parent de59fa5 commit bafa759b6af9fb1f9937f758319c584204984b5c Far McKon committed Jul 8, 2011
Showing with 431 additions and 0 deletions.
  1. +87 −0 app/Main.java
  2. +17 −0 app/Makefile
  3. +25 −0 lib/Makefile
  4. +158 −0 lib/backend.c
  5. +123 −0 lib/cmdline_bugduino_push.c
  6. +21 −0 lib/duinoServerFrontend_Main.h
View
@@ -0,0 +1,87 @@
+//package duinoServerFrontend;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.PushbackReader;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+/**
+ * @author mhorowitz
+ *
+ */
+public class Main {
+
+ public static native int passInstruction( String instruction, int slot, byte[] arguments, int argumentsSize );
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ System.setProperty( "java.library.path", "/home/root" );
+ System.loadLibrary( "javabugduino" );
+
+ //System.out.println( "OUTPUT: " + passInstruction( "WRIT", 0, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 10 ));
+
+ ServerSocket serverSocket = null;
+ try {
+ serverSocket = new ServerSocket( 8806 );
+
+ while( true ){
+ Socket clientSocket = serverSocket.accept();
+
+ clientSocket.setSoTimeout( 5000 );
+
+ InputStream rawSocketInput = clientSocket.getInputStream();
+ OutputStream rawSocketOutput = clientSocket.getOutputStream();
+ DataInputStream socketInput = new DataInputStream( rawSocketInput );
+ DataOutputStream socketOutput = new DataOutputStream( rawSocketOutput );
+
+ String instruction = socketInput.readUTF();
+ /*
+ {
+ char[] instruction_chars = new char[4];
+ for( int i = 0; i < 4; ++i ){
+ instruction_chars[i] = socketInput.readChar();
+ }
+ instruction = new String( instruction_chars );
+ }
+ */
+
+ int slot = socketInput.readInt();
+ int argumentsSize = socketInput.readInt();
+
+ System.out.println( "Instruction: " + instruction + " Slot: " + slot + " ArgumentsSize: " + argumentsSize );
+
+ try {
+ Thread.sleep( 1000 );
+ } catch (InterruptedException e) {
+ System.out.println( "WARNING: Sleep interrupted - BUGduino might not have been programmed correctly." );
+ e.printStackTrace();
+ }
+
+ byte[] arguments = new byte[argumentsSize];
+ System.out.println( "Bytes read: " + socketInput.read( arguments, 0, argumentsSize ) + "(if this does not match ArgumentsSize, abort the operation and try again)" );
+
+ int returnCode = passInstruction( instruction, slot, arguments, argumentsSize );
+
+ //socketOutput.write( returnCode );
+ //socketOutput.flush();
+ }
+
+ } catch (IOException e) {
+ System.err.println( "ERROR: Problem getting data" );
+ e.printStackTrace();
+ }
+ }
+}
View
@@ -0,0 +1,17 @@
+
+JFLAGS = -g
+JC = javac
+.SUFFIXES: .java .class
+.java.class:
+ $(JC) $(JFLAGS) $*.java
+
+
+CLASSES = \
+ Main.java
+
+default: classes
+
+classes: $(CLASSES:.java=.class)
+
+clean:
+ rm *.class
View
@@ -0,0 +1,25 @@
+#KERN_INC_DIR=/home/mhorowitz/BUG_Stuff/2.0_kernel/bl-linux-omap-latest/bl-linux-omap/include/
+
+KERN_INC_DIR?=/home/farmckon/dev_bl/linaro_kern/include/
+JDK_INC_DIR?=/usr/lib/jvm/java-6-openjdk/include/
+CC=$(CROSS_COMPILE)gcc
+
+HAS_KERN_DIR=$(shell if [ -d $(KERN_INC_DIR) ]; then echo 'true' ; fi)
+HAS_JDK_DIR=$(shell if [ -d $(JDK_INC_DIR) ]; then echo 'true' ; fi)
+
+
+all:
+ifneq ('$(HAS_JDK_DIR)','true')
+ $(error "no jdk include dir')
+endif
+ifneq ('$(HAS_KERN_DIR)','true')
+ $(error "no kern include dir')
+endif
+
+ $(CC) backend.c -shared -o libjavabugduino.so \
+ -Wall -I$(JDK_INC_DIR) -I$(KERN_INC_DIR)
+ $(CC) cmdline_bugduino_push.c -o program_bugduino \
+ -Wall -I$(KERN_INC_DIR)
+
+clean:
+ rm program_bugduino libjavabugduino.so
View
@@ -0,0 +1,158 @@
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <jni.h>
+
+#define BMI_BUGDUINO_IOCTL ('d')
+
+#include <linux/bmi/bmi_bugduino.h>
+
+#define INSTRUCTION_WRITE "WRIT"
+
+static void bugduino_write_dl( int slot, uint8_t *data, size_t data_size );
+static int bugduino_write( int slot, char* filename);
+
+
+JNIEXPORT jint JNICALL Java_Main_passInstruction(
+ JNIEnv *env, jclass class,
+ jstring instruction_arg, jint slot_arg,
+ jbyteArray arguments_arg, jint arguments_size_arg )
+{
+ int slot;
+ uint8_t *arguments;
+ size_t arguments_size;
+ const char *instruction;
+ //fprintf( stderr, "NOTE: in function %s\n", __func__ );
+
+ instruction = (*env)->GetStringUTFChars( env, instruction_arg, NULL );
+ slot = (int)slot_arg;
+ //fprintf( stderr, "NOTE: in function 2 %s\n", __func__ );
+ arguments = (*env)->GetByteArrayElements( env, arguments_arg, NULL );
+ //fprintf( stderr, "NOTE: in function 3 %s\n", __func__ );
+ arguments_size = (int)arguments_size_arg;
+
+//printf( "Instruction: %s; Slot: %i; Size: %i\n", instruction,
+// slot_arg, arguments_size );
+// for( i = 0; i < arguments_size; ++i ){
+// printf( "!0x%X!\n", *arguments );
+// 0++arguments;
+// }
+// puts( "" );
+
+
+ // it's not that I don't trust Java - I just don't trust JNI.
+ if( strncmp( instruction, INSTRUCTION_WRITE,
+ strlen( INSTRUCTION_WRITE ) ) == 0 ){
+ //fprintf( stderr, "NOTE: in function 6 %s\n", __func__ );
+ bugduino_write_dl( slot, arguments, arguments_size );
+ //fprintf( stderr, "NOTE: in function 7 %s\n", __func__ );
+ } else {
+ char* buffer;
+ //fprintf( stderr, "NOTE: in function 5 %s\n", __func__ );
+
+ buffer = (char*)malloc( 32 );
+ strncpy( buffer, instruction, 32 );
+ fprintf( stderr, "ERROR: Invalid instruction: %s\n", buffer );
+ free( buffer );
+
+ exit( EXIT_FAILURE );
+ }
+ //fprintf( stderr, "NOTE: in function 4 %s\n", __func__ );
+
+ //if(ret == 0);
+ return (jint)0;
+ //exit(EXIT_FAILURE);
+}
+
+static void bugduino_write_dl( int slot, uint8_t *data, size_t data_size ){
+ int fd;
+ pid_t pid;
+
+ /* create a temp'data' file to write data to */
+ //fprintf( stderr, "NOTE: in function d7 %s\n", __func__ );
+ fd = open( "./.avr_data.hex", O_WRONLY | O_CREAT | O_TRUNC,
+ S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH );
+ write( fd, data, data_size );
+ close( fd );
+
+ /* spawn compiler thread, compile arduino code via avrdude */
+ pid = vfork();
+ if( pid == 0 ){
+ // spawed thread
+ int iRet = 0;
+ iRet = bugduino_write( slot, "./.avr_data.hex");
+ if(iRet != 0)
+ printf("error doing bugduino_write %d",iRet);
+ } else {
+ //main thread
+ printf("waiting on PID %d\n", pid);
+ waitpid( pid, NULL, 0 );
+ }
+
+ return;
+}
+
+
+/**
+ * write a chunk of hex data to an arduino for programming.
+ * @slot - target slot with the bugduino on it
+ * @data - data to program to the bugduino
+ * @dataSz - size of data to program in bytes
+*/
+static int bugduino_write( int slot, char* filename)
+{
+ char uart[254];
+ char fileCtl[256];
+ int fd = 0;
+ if(slot < 0 || slot > 3) {
+ printf("Slot %d out of range\n.",slot);
+ return -5;
+ }
+
+ // generate our ioctl file, and our uart file by slot #
+ snprintf(uart, 256,"/dev/ttyBMI%d",slot);
+ snprintf(fileCtl,256,"/dev/bmi_bugduino_slot%d",slot);
+ fd = open(fileCtl,'w');
+ if(fd > 0 ) {
+ ioctl(fd, BMI_BUGDUINO_RESET,1);
+ sleep(1);
+ ioctl(fd, BMI_BUGDUINO_RESET,0);
+ close(fd);
+ }
+ else {
+ printf("unable to open bmi slot %d ctrl file %s",slot, fileCtl);
+ return -6;
+ }
+
+ //control block for actual writing of data
+ //via the onboard uart.
+ {
+ int iRet =0;
+ char* uart_prefix = "-P";
+ char uart_option[256];
+ char file_option[256];
+ snprintf( uart_option, 256, "%s%s", uart_prefix, uart );
+ printf("uart option %s%s\n", uart_prefix, uart );
+ printf("trying to write via uart %s\n",uart_option);
+
+ snprintf(file_option, 256, "-Uflash:w:%s", filename);
+ iRet = execl( "/usr/bin/avrdude", "/usr/bin/avrdude",
+ "-pm328p",
+ "-cstk500v1",
+ uart_option,
+ "-b57600",
+ "-D",
+ file_option,
+ NULL );
+ return iRet;
+ }
+ return 0;
+}
Oops, something went wrong.

0 comments on commit bafa759

Please sign in to comment.