Example #1
0
 public static void main(String[] args) {
   VM.println("Run.main()");
   Run autoBrake = new Run(args);
   autoBrake.init();
   autoBrake.doFunction();
   VM.println("Run-main done");
 }
Example #2
0
  public int getVirtualRPortId(int pportId) {
    // showtables();
    // VM.println("getVirtualRPortId " + pportId);

    Enumeration<Integer> keys = pportId2vrportId.keys();
    while (keys.hasMoreElements()) {
      Integer from = keys.nextElement();
      Integer to = pportId2vrportId.get(from);
      // VM.println(" link " + from + " -> " + to);
      if (from.intValue() == pportId) return to.intValue();
    }

    Integer p = pportId2vrportId.get(new Integer(pportId));
    VM.println("getVirtualRPortId -> " + p);
    VM.println("getVirtualRPortId -> val " + p.intValue());
    return p.intValue();
  }
Example #3
0
  public void doFunction() {
    String data;

    VM.println("[Run is running] 2");

    speed = new PluginPPort(this, "sp");
    steering = new PluginPPort(this, "st");

    for (int j = 0; j < 1; j++) {
      try {
        Thread.sleep(2000);
        speed.write(0);
        steering.write(0);
        Thread.sleep(2000);

        speed.write(20);
        steering.write(0);
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        VM.println("Interrupted.");
      }
    }

    while (true) {
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        VM.println("Interrupted.");
      }

      try {
        // Object obj = ab.receive();
        Object obj = getval(ab);
        if (obj != null) {
          VM.println("ab returned an object");
          VM.println("ab returned an object: " + obj);

          String s = (String) obj;
          int x = Integer.parseInt(s);
          if (x < 150) {
            speed.write(0);
            steering.write(0);
          }
          if (x > 150) {
            speed.write(20);
            steering.write(0);
          }

        } else {
          speed.write(30);
          steering.write(0);
          VM.println("ab returned null");
        }

      } catch (Exception e) {
        e.printStackTrace();
        VM.println("Run: exception");
      }
    }
  }
Example #4
0
  public int getPluginRPortId(int pportId) {
    showtables();
    VM.println("getPluginRPortId " + pportId);
    // TODO: What is this while-loop for???
    Enumeration<Integer> keys = pportId2rportId.keys();
    while (keys.hasMoreElements()) {
      Integer from = keys.nextElement();
      Integer to = pportId2rportId.get(from);

      VM.println(" link " + from + " -> " + to);
      if (from.intValue() == pportId) return to.intValue();
    }

    Integer p = pportId2rportId.get(pportId);
    VM.println("getPluginRPortId -> " + p);
    VM.println("getPluginRPortId -> val " + p.intValue());
    return p;
  }
Example #5
0
 /**
  * Get any error message provided by the platform (as in dlerror). If no error, then return null.
  *
  * <p>Note that calling this method clears the error state, so calling two times without calling
  * any other platform getFunction method (getInstance(), getFunction(),
  * getGlobalVariableAddress(), dispose()) will result in returning null.
  *
  * @return String (may be null)
  */
 public static String errorStr() {
   if (DEBUG) {
     VM.println("Calling DLERROR");
   }
   int result = VM.execSyncIO(ChannelConstants.DLERROR, 0, 0, 0, 0, 0, 0, null, null);
   Address r = Address.fromPrimitive(result);
   if (r.isZero()) {
     return null;
   } else {
     return Pointer.NativeUnsafeGetString(r);
   }
 }
Example #6
0
 /** Read errno, try to clean up fd, and create exception. */
 private IOException newError(int fd, String msg) {
   if (DEBUG) {
     VM.print(msg);
     VM.print(": errno: ");
   }
   int err_code = LibCUtil.errno();
   if (DEBUG) {
     VM.print(err_code);
     VM.println();
   }
   sockets.shutdown(fd, 2);
   libc.close(fd);
   return new IOException(" errno: " + err_code + " on fd: " + fd + " during " + msg);
 }
Example #7
0
 public void link(LinkContextEntry entry) {
   int fromPortId = entry.getFromPortId();
   int toPortId = entry.getToPortId();
   int remotePortId = entry.getRemotePortId();
   if (remotePortId == Configuration.PPORT2VPORT) {
     // from PluginPort to VirtualPort
     VM.println("Linking " + fromPortId + " -> " + toPortId + " as P->V");
     pportId2vrportId.put(fromPortId, toPortId);
   } else if (remotePortId == Configuration.PPORT2PPORT) {
     // from PluginPort to PluginPort and they are in the same ECU
     VM.println("Linking " + fromPortId + " -> " + toPortId + " as P->P on the same ECU");
     pportId2rportId.put(fromPortId, toPortId);
   } else if (remotePortId == Configuration.VPORT2PPORT) {
     // from VirtualPPort to PluginRPort
     VM.println("Linking " + toPortId + " -> " + fromPortId + " as V->P");
     rportId2vpportId.put(toPortId, fromPortId);
   } else if (remotePortId > 0) {
     // from PluginPort to PluginPort but they are in different ECUs
     // In this case, remotePortId is just remote plug-in port ID
     pportId2rportId.put(fromPortId, remotePortId);
     // PV entry is also needed to register
     pportId2vrportId.put(fromPortId, toPortId);
   }
 }
Example #8
0
 private Object getval(PluginRPort port) {
   WorkThread p = new WorkThread(ab);
   p.start();
   try {
     Thread.sleep(1000);
   } catch (InterruptedException e) {
     VM.println("Interrupted.");
   }
   Object o2 = p.obj;
   // VM.println("plupp " + o2);
   if (p.obj != null) {
     // p.stop();
   }
   return o2;
 }
Example #9
0
 /**
  * Look up a native libray named "name". Load if not loaded. Name can be a path or a libray name.
  * The Library will be looked up in a platform-dependant manor.
  *
  * @param name short library name, full file name, or path to the library file
  * @return NativeLibrary
  */
 public static NativeLibrary getInstance(String name) {
   String nativeName = nativeLibraryName(name);
   Pointer name0 = Pointer.createStringBuffer(nativeName);
   if (DEBUG) {
     VM.print("Calling DLOPEN on ");
     VM.println(name);
   }
   int result =
       VM.execSyncIO(
           ChannelConstants.DLOPEN, name0.address().toUWord().toInt(), 0, 0, 0, 0, 0, null, null);
   Address r = Address.fromPrimitive(result);
   name0.free();
   if (r.isZero()) {
     throw new RuntimeException("Can't open library " + name + ". OS Error: " + errorStr());
   }
   return new NativeLibrary(name, r);
 }
Example #10
0
 /**
  * Close the library, as in dlclose.
  *
  * @throws RuntimeException if dlcose fails
  */
 public void dispose() {
   if (closed || ptr.isZero()) {
     throw new RuntimeException("closed or RTLD_DEFAULT");
   }
   if (DEBUG) {
     VM.print("Calling DLCLOSE on ");
     VM.println(name);
   }
   Pointer name0 = Pointer.createStringBuffer(name);
   int result =
       VM.execSyncIO(
           ChannelConstants.DLCLOSE, ptr.toUWord().toInt(), 0, 0, 0, 0, 0, 0, null, null);
   name0.free();
   if (result != 0) {
     throw new RuntimeException("Error on dlclose: " + errorStr());
   }
   closed = true;
 }
Example #11
0
 /**
  * Dynamically look up a native variable by name.
  *
  * <p>Look up the symbol in the default list of loaded libraries.
  *
  * @param varName
  * @param size the size of the variable in bytes
  * @return a Pointer that can be used to get/set the variable
  * @throws RuntimeException if there is no function by that name.
  */
 public VarPointer getGlobalVariableAddress(String varName, int size) {
   Address result = getSymbolAddress(varName);
   if (DEBUG) {
     VM.print("Var Lookup for ");
     VM.print(varName);
     VM.print(", size: ");
     VM.print(size);
     VM.print(" returned ");
     VM.printAddress(result);
     VM.println();
   }
   if (result.isZero()) {
     if (varName.charAt(0) != '_') {
       return getGlobalVariableAddress("_" + varName, size);
     }
     throw new RuntimeException(
         "Can't find native symbol " + varName + ". OS Error: " + errorStr());
   }
   return new VarPointer(varName, result, size);
 }
Example #12
0
 /**
  * Dynamically look up a native function address by name. Look up the symbol in the specified
  * library
  *
  * @param funcName
  * @return address of the function
  * @throws RuntimeException if there is no function by that name.
  */
 private Address getFunction0(String funcName) {
   Address result = getSymbolAddress(funcName);
   if (DEBUG) {
     VM.print("Function Lookup for ");
     VM.print(funcName);
     VM.print(" = ");
     VM.printAddress(result);
     VM.println();
   }
   if (result.isZero()) {
     if (Platform.getPlatform().isWindows()) {
       if (funcName.charAt(funcName.length() - 1) != 'A') {
         return getFunction0(funcName + 'A');
       }
     } else if (funcName.charAt(0) != '_') {
       return getFunction0("_" + funcName);
     }
     throw new RuntimeException(
         "Can't find native symbol " + funcName + ". OS Error: " + errorStr());
   }
   return result;
 }
Example #13
0
 /**
  * getFunction a symbol's address by name (warpper around dlsym)
  *
  * @param name
  * @return Address of symbol
  */
 private Address getSymbolAddress(String name) {
   if (closed) {
     throw new IllegalStateException("closed");
   }
   if (DEBUG) {
     VM.print("Calling DLSYM on ");
     VM.println(name);
   }
   Pointer name0 = Pointer.createStringBuffer(name);
   int result =
       VM.execSyncIO(
           ChannelConstants.DLSYM,
           ptr.toUWord().toInt(),
           name0.address().toUWord().toInt(),
           0,
           0,
           0,
           0,
           null,
           null);
   name0.free();
   return Address.fromPrimitive(result);
 }
Example #14
0
 private void showtables() {
   VM.println("pport2pport: " + pport2pport);
   VM.println("pportId2vrportId: " + pportId2vrportId);
   VM.println("pportId2rportId: " + pportId2rportId);
   VM.println("rportId2vpportId: " + rportId2vpportId);
 }
Example #15
0
 public void run() {
   VM.println("Run.run()");
   init();
   doFunction();
   VM.println("Run-main done");
 }