Package org.tc65sh.util

Examples of org.tc65sh.util.ByteArray


 
  public String executeAtCommand(String atCommand) throws IOException {
    if ( inObexMode ) {
      closeObexMode();
    }
    ByteArray request = new ByteArray(atCommand,"ISO-8859-1");
    request.append(13);
    sendByteArray(request);
    ByteArray response = waitForATResponseWithOK(DEFAULT_AT_COMMAND_RESPONSE_TIMEOUT, true);
    return new String(response.getBuffer(),"ISO-8859-1");
  }
View Full Code Here


 
  // private stuff

  private void openObexMode() throws IOException {
    Log.debug(this.getClass(), "opening obex mode");
    sendByteArray(new ByteArray("AT\\Q3\r","ISO-8859-1"));
    waitForATResponseWithOK(DEFAULT_INTERNAL_AT_RESPONSE_TIMEOUT, true);
    sendByteArray(new ByteArray("AT^SQWE=0\r","ISO-8859-1"));
    waitForATResponseWithOK(DEFAULT_INTERNAL_AT_RESPONSE_TIMEOUT, true);
    sendByteArray(new ByteArray("AT^SQWE=3\r","ISO-8859-1"));
    waitForATResponseWithOK(DEFAULT_INTERNAL_AT_RESPONSE_TIMEOUT, true);
    Log.debug(this.getClass(), "connecting obex");
    ByteArray fsUid = new ByteArray(new byte[] { (byte) 0x6b, (byte) 0x01, (byte) 0xcb, (byte) 0x31, (byte) 0x41, (byte) 0x06, (byte) 0x11, (byte) 0xd4, (byte) 0x9a, (byte) 0x77, (byte) 0x00, (byte) 0x50, (byte) 0xda, (byte) 0x3f, (byte) 0x47, (byte) 0x1f });
    ByteArray targetHeader = new ByteArray();
    targetHeader.append(Obex.HEADER_TARGET);
    targetHeader.append(Obex.shortToBytes(3+fsUid.length()));
    targetHeader.append(fsUid);
    ByteArray req = new ByteArray();
    req.append(Obex.REQUEST_CONNECT);
    req.append(Obex.shortToBytes(7+targetHeader.length()));
    req.append(0x13); // obex version
    req.append(0x00); // flags
    req.append(Obex.shortToBytes(0xffff)); // max packet length
    req.append(targetHeader);
    sendByteArray(req);
    ByteArray response = receiveObexResponse(DEFAULT_OBEX_RESPONSE_TIMEOUT);
    validateResponseCode(response);
    inObexMode = true;
  }
View Full Code Here

  }

 
  private void closeObexMode() throws IOException {
    Log.debug(this.getClass(), "closing obex mode obex");
    ByteArray req = new ByteArray();
    req.append(Obex.REQUEST_DISCONNECT);
    req.append(Obex.shortToBytes(3));
    sendByteArray(req);
    ByteArray response = receiveObexResponse(DEFAULT_OBEX_RESPONSE_TIMEOUT);
    validateResponseCode(response);
    Log.debug(this.getClass(), "escaping data mode");
    boolean foundOK = false;
    int max = 5;
    while( ! foundOK && max >= 0 ) {
      max--;
      sendByteArray(new ByteArray("+++","ISO-8859-1"));
      ByteArray plusResponse = waitForATResponseWithOK(1000, false);       
      if ( plusResponse != null ) {
        foundOK = true;
      }
    }
    sendByteArray(new ByteArray("ATE1\r","ISO-8859-1"));
    waitForATResponseWithOK(DEFAULT_INTERNAL_AT_RESPONSE_TIMEOUT, true);
    inObexMode = false;
  }
View Full Code Here

  }
 
 
  private void putFilePart(FileHolder file, int contentPartOffset, int contentPartLength, boolean isFirstPart, boolean isLastPart) throws IOException {
    Log.debug(getClass(), "sending bytes "+contentPartOffset+".."+(contentPartOffset+contentPartLength)+" to "+file.fileInfo.name);
    ByteArray contentPart = file.content.subArray(contentPartOffset, contentPartLength);
    ByteArray nameHeader = new ByteArray();
    ByteArray lengthHeader = new ByteArray();
    ByteArray timeHeader = new ByteArray();
    if ( isFirstPart ) {
      ByteArray obexFilename = Obex.encodeUtf16String(file.fileInfo.name);
      // HEADER_NAME
      nameHeader.append(Obex.HEADER_NAME);   
      nameHeader.append(Obex.shortToBytes(3+obexFilename.length()));
      nameHeader.append(obexFilename);
      // HEADER_LENGTH
      lengthHeader.append(Obex.HEADER_LENGTH);   
      lengthHeader.append(Obex.intToBytes(file.fileInfo.filesize));
      // HEADER_TIME
      ByteArray obexTime = Obex.encodeDateTime(file.fileInfo.date);
      timeHeader.append(Obex.HEADER_TIME);   
      timeHeader.append(Obex.shortToBytes(3+obexTime.length()));
      timeHeader.append(obexTime);
    }
    // HEADER_BODY
    ByteArray bodyHeader = new ByteArray();
    if ( isLastPart ) {
      bodyHeader.append(Obex.HEADER_END_OF_BODY);   
    } else {
      bodyHeader.append(Obex.HEADER_BODY);   
    }
    bodyHeader.append(Obex.shortToBytes(3+contentPart.length()));
    bodyHeader.append(contentPart);
    // REQUEST_PUT
    ByteArray req = new ByteArray();
    if ( isLastPart ) {
      req.append(Obex.REQUEST_PUT | Obex.REQUEST_FINAL );
    } else {
      req.append(Obex.REQUEST_PUT);
    }
    req.append(Obex.shortToBytes(3+nameHeader.length()+lengthHeader.length()+timeHeader.length()+bodyHeader.length()));
    req.append(nameHeader);
    req.append(lengthHeader);
    req.append(timeHeader);
    req.append(bodyHeader);
    sendByteArray(req);
    ByteArray response = receiveObexResponse(DEFAULT_OBEX_RESPONSE_TIMEOUT);
    validateResponseCode(response);
  }
View Full Code Here

    validateResponseCode(response);
  }
 
 
  private ByteArray receiveBodyToEnd() throws IOException {
    ByteArray response = receiveObexResponse(DEFAULT_OBEX_RESPONSE_TIMEOUT);
    validateResponseCode(response);
    ByteArray fullBody = new ByteArray();
    ByteArray bodyPart = extractGetResponseBody(response);
    if ( bodyPart != null ) {
      fullBody.append(bodyPart);
    }
    while( isObexContinue(response) ) {
      ByteArray req = new ByteArray();
      req.append(Obex.REQUEST_GET);
      req.append(Obex.shortToBytes(3));
      sendByteArray(req);
      response = receiveObexResponse(DEFAULT_OBEX_RESPONSE_TIMEOUT);
      validateResponseCode(response);
      bodyPart = extractGetResponseBody(response);
      if ( bodyPart != null ) {
View Full Code Here

 
 

 
  private ByteArray waitForATResponseWithOK(long timeoutmillis, boolean responseRequired) throws IOException {
    ByteArray response = new ByteArray();
    long t1 = System.currentTimeMillis();
    boolean foundOK = false;
    while (!foundOK) {
      receiveIntoByteArray(response);
      if ( response.toPrintableString().contains("OK") ) {
        foundOK = true;
      } else {
        long runtime = System.currentTimeMillis() - t1;
        if ( runtime > timeoutmillis) {
          if ( responseRequired ) {
                                                throw new IOException("response timeout waiting for OK after "+runtime+" ms and "+response.length()+" bytes");
          } else {
            return null;
          }
        }
        sleep(DEFAULT_SLEEP_MILLIS);
View Full Code Here


 
 
  private void setPath(String pathname, boolean create) throws IOException {
    ByteArray header = new ByteArray();
    byte flags;
    if ( pathname == null || pathname.length() == 0 || pathname.equals("/") || pathname.equals("..") || pathname.toLowerCase().equals("a:") || pathname.toLowerCase().equals("a:/") ) {
      flags = Obex.FLAG_SETPATH_PARENT_FOLDER;
    } else {
      ByteArray obexPath = Obex.encodeUtf16String(pathname);
      Log.debug(this.getClass(), "obexPath = "+obexPath.toPrintableString()+" " +obexPath.toHexString());
      header.append(Obex.HEADER_NAME);
      header.append(Obex.shortToBytes(obexPath.length()+3));
      header.append(obexPath);
      if ( create ) {
        flags = Obex.FLAG_SETPATH_CREATE;
      } else {
        flags = Obex.FLAG_SETPATH_NOCREATE;
      }
    }
    ByteArray req = new ByteArray();
    req.append(Obex.REQUEST_SETPATH);
    req.append(Obex.shortToBytes(header.length()+5));
    req.append(flags);
    req.append(0x00);
    req.append(header);
    sendByteArray(req);
    ByteArray response = receiveObexResponse(DEFAULT_OBEX_RESPONSE_TIMEOUT);
    validateResponseCode(response);
  }
View Full Code Here

    validateResponseCode(response);
  }

 
  private ByteArray extractGetResponseBody(ByteArray response) {
    ByteArray body = null;
    int i = indexOfBodyHeader(response);
    if ( i >= 0 ) {
      Log.debug(getClass(), "indexOfBodyHeader = "+i);
      body = response.subArray(i+3, response.length()-i-3);
    } else {
View Full Code Here

    if ( (code & (int)Obex.RESPONSE_CREATED) == (int)Obex.RESPONSE_CREATED) return;
    throw new IOException("response validation error, code="+code);
  }

  private ByteArray receiveObexResponse(long timeoutMillis) throws IOException {
    ByteArray response = new ByteArray();
    long t1 = System.currentTimeMillis();
    boolean complete = false;
    while( ! complete ) {
      receiveIntoByteArray(response);
      if ( response.length() >= 3 ) {
        int expectedFrameLength = Obex.bytesToInt(response.subArray(1,2).getBuffer());
        if ( response.length() >= expectedFrameLength ) {
          complete = true;
        }
      }
      if ( ! complete ) {
        long runtime = System.currentTimeMillis()-t1;
        if ( runtime > timeoutMillis ) {
          throw new IOException("obex response timeout after "+runtime+" ms and "+response.length()+" bytes");
        }
      }
    }
    return response;
  }
View Full Code Here

    byte[] buf = new byte[512];
    int totalReadCount = 0;
    while( serialIn.available() > 0 ) {
      int readCount = serialIn.read(buf);
      totalReadCount += readCount;
      ByteArray temp = new ByteArray(buf, 0, readCount);
      if ( Log.isDebugEnabled() ) {
        Log.debug(this.getClass(), "received " + temp.length() + " bytes: " + temp.toHexString() + temp.toPrintableString());
      }
      byteArray.append(temp);
    }
    return totalReadCount;
  }
View Full Code Here

TOP

Related Classes of org.tc65sh.util.ByteArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.