Beispiel #1
0
 public Object executeRemoteCommand(Command c, String command, Object argument, long wait)
     throws ZigBeeException, SerialException {
   // Don't need to be in API mode to execute remote commands.
   byte[] packet = buildRemoteCommand(c, remote_at_command, command, argument);
   ZigBee zb = getLocal();
   return zb.sendRemote(c, packet, wait);
 }
Beispiel #2
0
      void process(ZigBee zb, byte[] packet, int offset) {
        if (packet.length < offset + 11) {
          return;
        }
        short address16 = (short) bytesToNum(packet, offset, 2);
        long address64 = bytesToNum(packet, offset + 2, 8);
        zb = findOrCreateRemote(address64, address16);
        try {
          zb.updatePan(getLocal().getPan());
        } catch (Exception e) {
          // Ignore it, can't happen.
          e.printStackTrace();
        }

        // Byte 10 is the signal strength, at least in XBee's.
        if (packet.length < offset + 13) {
          return;
        }
        zb.updateNI(bytesToString(packet, offset + 11, 21));

        offset = offset + 11 + zb.getNI().length() + 1;
        // next is parent network address 2 bytes
        // zb.setDeviceType(bytesToNum(packet, offset+2, 1)); // router vs end node vs coordinator
        // next is status 1 byte (reserved)
        if (packet.length < offset + 6) {
          return;
        }
        zb.setProfileID((short) bytesToNum(packet, offset + 4, 2));

        if (packet.length < offset + 8) {
          return;
        }
        zb.setManufacturerID((short) bytesToNum(packet, offset + 6, 2));
      }
Beispiel #3
0
  public void processIncomingPacket(String s) throws ZigBeeException, SerialException {
    if (s.charAt(0) != 0x7e) {
      return; // bogus packet.
    }

    byte[] packet = new byte[s.length()];
    String o = "ProcessIncomingPacket[" + packet.length + "] =";
    for (int i = 0; i < s.length(); ++i) {
      packet[i] = (byte) s.charAt(i);
      o = o + " 0x" + Integer.toHexString(s.charAt(i));
    }
    byte cksum = packet[packet.length - 1];
    computeChecksum(packet);
    if (cksum != packet[packet.length - 1]) {
      return; // bogus packet
    }

    // Setup Defaults for the local packet.
    ZigBee zb = this;
    int offset = 5;

    switch (packet[3]) {
      case remote_command_response:
        zb =
            findOrCreateRemote(
                longAddressFromPacket(packet, 5), shortAddressFromPacket(packet, 13));
        offset = 15; // fall through
      case local_at_command_response:
        zb.processCommandResponse(packet, offset);
        return;
      default:
        // Ignore packets we don't know what to do with.  It's in the spec.
        return;
    }
  }
Beispiel #4
0
 void process(ZigBee zb, byte[] packet, int offset) {
   int ind = (int) bytesToNum(packet, offset, 8, packet.length - 1);
   if (ind < bpsValues.length) {
     zb.updateBps(bpsValues[ind]);
   } else {
     zb.updateBps(ind);
   }
 }
Beispiel #5
0
 public static void close() {
   ZigBee zb = getLocalNoCreate();
   if (zb != null) {
     try {
       zb.exitAPIMode();
       zb.closeSerial();
     } catch (Exception e) {
     }
     ;
   }
 }
Beispiel #6
0
  static ZigBee findOrCreateRemote(long address64, short address16) {
    ZigBee zb;
    synchronized (knownNodes) {
      zb = knownNodes.get(new Long(address64));

      if (zb == null) {
        zb = new ZigBee();
        zb.updateAddress64(address64);
      }
    }
    zb.updateAddress16(address16);
    return zb;
  }
Beispiel #7
0
  public Object executeCommand(Command c, long wait) throws ZigBeeException, SerialException {
    String command = c.commandString;
    Object argument = c.argument;

    ZigBee local = getLocalNoCreate();
    local.enterAPIMode();

    if (local == this) {
      return executeLocalCommand(c, command, argument, wait);
    } else {
      return executeRemoteCommand(c, command, argument, wait);
    }
  }
Beispiel #8
0
 void connectToRemote(ZigBee remote) throws ZigBeeException, SerialException {
   long remoteAddress = remote.getSerialNumber();
   int remoteLow = (int) remoteAddress;
   int remoteHigh = (int) (remoteAddress >>> 32);
   executeCommand("DL", new Integer(remoteLow), internalWait);
   executeCommand("DH", new Integer(remoteHigh), internalWait);
   connectedAddress = remoteAddress;
   sendStaticMessage("DL");
 }
Beispiel #9
0
  public static ZigBee getLocal() throws ZigBeeException, SerialException {
    synchronized (getLocalLock) {
      if (local != null) {
        if (serialPort == null) {
          serialPort = new Serial();
          if (local.apiMode) {
            serialPort.addListener(local);
          }
        }
        return local;
      }

      try {
        ZigBee l = new ZigBee();
        local = l; // must do this first, otherwise isLocal fails.
        // l isn't really complete yet, but it's good enough for
        // local communications.
        l.enterAPIMode();
        // Just queue up these commands and let the incoming
        // api message processor deal with the results.
        l.executeCommand("SL", null, internalWait);
        l.executeCommand("SH", null, internalWait);
        l.executeCommand("NI", null, internalWait);
        l.executeCommand("MY", null, internalWait);
        l.executeCommand("ID", null, internalWait);
        l.executeCommand("DL", null, internalWait);
        l.executeCommand("DH", null, internalWait);

        // Can't get profile id and manufacturer_id with out doing a loop back node id.
        l.bps = Long.parseLong(Preferences.get("serial.debug_rate"));
        l.parity = Preferences.get("serial.parity").charAt(0);
        return l;
      } catch (Exception e) {
        close();
        local = null;
        e.printStackTrace();
        if (e instanceof ZigBeeException) {
          throw (ZigBeeException) e;
        } else if (e instanceof SerialException) {
          throw (SerialException) e;
        }
        throw new ZigBeeException(e.getMessage());
      }
    }
  }
Beispiel #10
0
      void process(ZigBee zb, byte[] packet, int offset) {
        char b = (char) bytesToNum(packet, offset, 1, packet.length - 1);
        switch (b) {
          default:
          case 0:
            b = 'N';
            break;

          case 1:
            b = 'E';
            break;

          case 2:
            b = 'O';
            break;

          case 3:
            b = 'M';
            break;
        }
        zb.updateParity(b);
      }
Beispiel #11
0
 void process(ZigBee zb, byte[] packet, int offset) {
   zb.updateAddress16((short) bytesToNum(packet, offset, 2, packet.length - 1));
 }
Beispiel #12
0
 public void connect() throws ZigBeeException, SerialException {
   ZigBee zb = getLocal();
   zb.connectToRemote(this);
 }
Beispiel #13
0
 public String getPrefPrefix() {
   return ZigBee.getPrefPrefix(getSerialNumber());
 }
Beispiel #14
0
 public static void searchForNodesWait() throws ZigBeeException, SerialException {
   ZigBee l = getLocal();
   l.executeCommand("ND", null, 5000);
 }
Beispiel #15
0
 public static void searchForNodes() throws ZigBeeException, SerialException {
   ZigBee l = getLocal();
   l.executeCommand("ND", null, 0);
   // Nodes will automatically be populated as responses come in.
 }
Beispiel #16
0
 void process(ZigBee zb, byte[] packet, int offset) {
   zb.updatePan(new Long(bytesToNum(packet, offset, 4, packet.length - 1)));
 }
Beispiel #17
0
 void process(ZigBee zb, byte[] packet, int offset) {
   zb.updateNI(bytesToString(packet, offset, Math.min(20, packet.length - offset - 1)));
 }
Beispiel #18
0
 void process(ZigBee zb, byte[] packet, int offset) {
   if (zb != getLocalNoCreate()) {
     return;
   }
   zb.setDH((int) bytesToNum(packet, offset, 4, packet.length - 1));
 }
Beispiel #19
0
 void process(ZigBee zb, byte[] packet, int offset) {
   zb.setSH((int) bytesToNum(packet, offset, 4, packet.length - 1));
 }