Home | History | Annotate | Download | only in dhimpl
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.nfc.dhimpl;
     18 
     19 import com.android.nfc.DeviceHost;
     20 import com.android.nfc.LlcpPacket;
     21 
     22 import java.io.IOException;
     23 
     24 /**
     25  * LlcpConnectionlessSocket represents a LLCP Connectionless object to be used
     26  * in a connectionless communication
     27  */
     28 public class NativeLlcpConnectionlessSocket implements DeviceHost.LlcpConnectionlessSocket {
     29 
     30     private int mHandle;
     31     private int mSap;
     32     private int mLinkMiu;
     33 
     34     public NativeLlcpConnectionlessSocket() { }
     35 
     36     public native boolean doSendTo(int sap, byte[] data);
     37 
     38     public native LlcpPacket doReceiveFrom(int linkMiu);
     39 
     40     public native boolean doClose();
     41 
     42     @Override
     43     public int getLinkMiu(){
     44         return mLinkMiu;
     45     }
     46 
     47     @Override
     48     public int getSap(){
     49         return mSap;
     50     }
     51 
     52     @Override
     53     public void send(int sap, byte[] data) throws IOException {
     54         if (!doSendTo(sap, data)) {
     55             throw new IOException();
     56         }
     57     }
     58 
     59     @Override
     60     public LlcpPacket receive() throws IOException {
     61         LlcpPacket packet = doReceiveFrom(mLinkMiu);
     62         if (packet == null) {
     63             throw new IOException();
     64         }
     65         return packet;
     66     }
     67 
     68     public int getHandle(){
     69         return mHandle;
     70     }
     71 
     72     @Override
     73     public void close() throws IOException {
     74         if (!doClose()) {
     75             throw new IOException();
     76         }
     77     }
     78 }
     79