View Javadoc
1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package net.sf.atmodem4j.core.gsm;
7   
8   /*
9    * #%L
10   * ATModem4J Core
11   * %%
12   * Copyright (C) 2009 - 2014 atmodem4j
13   * %%
14   * atmodem4j - Drivers for the AT modem - http://atmodem4j.sourceforge.net/
15   * Copyright (C) 2009-2014, atmodem4j.sf.net, and individual contributors as indicated
16   * by the @authors tag. See the copyright.txt in the distribution for a
17   * full listing of individual contributors.
18   * 
19   * This is free software; you can redistribute it and/or modify it
20   * under the terms of the GNU General Public License as
21   * published by the Free Software Foundation; either version 3 of
22   * the License, or (at your option) any later version.
23   * 
24   * This software is distributed in the hope that it will be useful,
25   * but WITHOUT ANY WARRANTY; without even the implied warranty of
26   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27   * Lesser General Public License for more details.
28   * 
29   * You should have received a copy of the GNU Lesser General Public
30   * License along with this software; if not, write to the Free
31   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
32   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
33   * #L%
34   */
35  
36  import java.io.IOException;
37  import net.sf.atmodem4j.core.Modem;
38  
39  /**
40   *
41   * @author aploese
42   */
43  public class DefaultGsmExtention implements GsmExtention {
44  
45      Modem modem;
46  
47      public void sendSms(String phonenumber, String text) {
48          throw new UnsupportedOperationException("Not supported yet.");
49      }
50  
51      public void sendPdu(int pduLength, String pdu) throws IOException, InterruptedException {
52          modem.sendCommandLineWithPrompt("AT+CMGS=" + pduLength, pdu);
53      }
54  
55      public Modem getModem() {
56          return modem;
57      }
58  
59      public void setModem(Modem modem) {
60          this.modem = modem;
61      }
62  
63      public String getSimPin() throws IOException, InterruptedException {
64          return getModem().extractData(getModem().sendAndEctractData("AT+CPIN?")[0]);
65      }
66  
67      public boolean isSimPin() throws IOException, InterruptedException {
68          return "READY".equals(getSimPin());
69      }
70  
71      public void setSimPin(String simPin) throws IOException,
72              InterruptedException {
73          getModem().sendAndEctractData("AT+CPIN="+ simPin, 1, getModem().getDefaultWaitTime() * 2);
74      }
75  
76      public String getNetworkProvider() throws IOException, InterruptedException {
77          return getModem().extractData(getModem().sendAndEctractData("AT+COPS?")[0]);
78      }
79  
80      public String getSignalQuality(boolean raw) throws IOException, InterruptedException {
81          String data = getModem().extractData(getModem().sendAndEctractData("AT+CSQ")[0]);
82          if (raw) {
83              return data;
84          } else {
85              int  sQ = Integer.parseInt(data.substring(0, data.indexOf(',')));
86              if (sQ == 0) {
87                  return "<-113dBm"; // -113dBm or worse
88              } else if (sQ < 31) {
89                  return Integer.toString(-111 + (sQ - 1) * 2) + "dBm";
90              } else if (sQ == 31) {
91                          return ">-51dBm"; // -51dBm or better
92              } else if (sQ == 99) {
93                              return "unknown";
94              } else {
95                  return "unknown value sQ == " + sQ;
96              }
97          }
98      }
99  
100 }