PHS module SMA-01 library. see: https://developer.mbed.org/users/phsfan/notebook/abitusbmodem/

Dependencies:   Socket lwip-sys lwip

Dependents:   AbitUSBModem_HTTPTest AbitUSBModem_MQTTTest AbitUSBModem_WebsocketTest AbitUSBModem_SMSTest

Fork of VodafoneUSBModem by mbed official

/media/uploads/phsfan/sma01_003.png

Committer:
phsfan
Date:
Wed Feb 18 09:40:07 2015 +0000
Revision:
96:b50f5f795684
Child:
97:7d9cc95e2ea7
1st build.; ABIT SMA-01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phsfan 96:b50f5f795684 1 /* AbitUSBModem.cpp
phsfan 96:b50f5f795684 2 * modifyed by Suga
phsfan 96:b50f5f795684 3 */
phsfan 96:b50f5f795684 4 /* VodafoneUSBModem.cpp */
phsfan 96:b50f5f795684 5 /* Copyright (C) 2012 mbed.org, MIT License
phsfan 96:b50f5f795684 6 *
phsfan 96:b50f5f795684 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
phsfan 96:b50f5f795684 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
phsfan 96:b50f5f795684 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
phsfan 96:b50f5f795684 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
phsfan 96:b50f5f795684 11 * furnished to do so, subject to the following conditions:
phsfan 96:b50f5f795684 12 *
phsfan 96:b50f5f795684 13 * The above copyright notice and this permission notice shall be included in all copies or
phsfan 96:b50f5f795684 14 * substantial portions of the Software.
phsfan 96:b50f5f795684 15 *
phsfan 96:b50f5f795684 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
phsfan 96:b50f5f795684 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
phsfan 96:b50f5f795684 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
phsfan 96:b50f5f795684 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
phsfan 96:b50f5f795684 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
phsfan 96:b50f5f795684 21 */
phsfan 96:b50f5f795684 22
phsfan 96:b50f5f795684 23
phsfan 96:b50f5f795684 24 #define __DEBUG__ 0
phsfan 96:b50f5f795684 25
phsfan 96:b50f5f795684 26 #ifndef __MODULE__
phsfan 96:b50f5f795684 27 #define __MODULE__ "AbitUSBModem.cpp"
phsfan 96:b50f5f795684 28 #endif
phsfan 96:b50f5f795684 29
phsfan 96:b50f5f795684 30 #include "core/fwk.h"
phsfan 96:b50f5f795684 31
phsfan 96:b50f5f795684 32 #include "AbitUSBModem.h"
phsfan 96:b50f5f795684 33 #include "Socket.h"
phsfan 96:b50f5f795684 34
phsfan 96:b50f5f795684 35 AbitUSBModem::AbitUSBModem () :
phsfan 96:b50f5f795684 36 m_dongle(), // Construct WANDongle: USB interface with two serial channels to the modem (USBSerialStream objects)
phsfan 96:b50f5f795684 37 m_pppStream((IUSBHostSerial&)m_dongle), // PPP connections are managed via another serial channel.
phsfan 96:b50f5f795684 38 m_at(&m_pppStream), // Construct ATCommandsInterface with the AT serial channel
phsfan 96:b50f5f795684 39 m_ppp(&m_pppStream, &m_pppStream, &m_at, false), // Construct PPPIPInterface with the PPP serial channel
phsfan 96:b50f5f795684 40 m_dongleConnected(false), // Dongle is initially not ready for anything
phsfan 96:b50f5f795684 41 m_ipInit(false), // PPIPInterface connection is initially down
phsfan 96:b50f5f795684 42 m_atOpen(false) // ATCommandsInterface starts in a closed state
phsfan 96:b50f5f795684 43 {
phsfan 96:b50f5f795684 44 }
phsfan 96:b50f5f795684 45
phsfan 96:b50f5f795684 46 int AbitUSBModem::connect (const char* user, const char* password) {
phsfan 96:b50f5f795684 47
phsfan 96:b50f5f795684 48 if( !m_ipInit )
phsfan 96:b50f5f795684 49 {
phsfan 96:b50f5f795684 50 m_ipInit = true;
phsfan 96:b50f5f795684 51 m_ppp.init();
phsfan 96:b50f5f795684 52 }
phsfan 96:b50f5f795684 53 m_ppp.setup(user, password);
phsfan 96:b50f5f795684 54
phsfan 96:b50f5f795684 55 int ret = init();
phsfan 96:b50f5f795684 56 if(ret)
phsfan 96:b50f5f795684 57 {
phsfan 96:b50f5f795684 58 return ret;
phsfan 96:b50f5f795684 59 }
phsfan 96:b50f5f795684 60
phsfan 96:b50f5f795684 61 m_at.close(); // Closing AT parser
phsfan 96:b50f5f795684 62 m_atOpen = false; //Will need to be reinitialized afterwards
phsfan 96:b50f5f795684 63
phsfan 96:b50f5f795684 64 DBG("Connecting PPP");
phsfan 96:b50f5f795684 65
phsfan 96:b50f5f795684 66 ret = m_ppp.connect();
phsfan 96:b50f5f795684 67 DBG("Result of connect: Err code=%d", ret);
phsfan 96:b50f5f795684 68 return ret;
phsfan 96:b50f5f795684 69 }
phsfan 96:b50f5f795684 70
phsfan 96:b50f5f795684 71 int AbitUSBModem::disconnect () {
phsfan 96:b50f5f795684 72 DBG("Disconnecting from PPP");
phsfan 96:b50f5f795684 73 int ret = m_ppp.disconnect();
phsfan 96:b50f5f795684 74 if(ret)
phsfan 96:b50f5f795684 75 {
phsfan 96:b50f5f795684 76 ERR("Disconnect returned %d, still trying to disconnect", ret);
phsfan 96:b50f5f795684 77 }
phsfan 96:b50f5f795684 78
phsfan 96:b50f5f795684 79 return OK;
phsfan 96:b50f5f795684 80 }
phsfan 96:b50f5f795684 81
phsfan 96:b50f5f795684 82
phsfan 96:b50f5f795684 83
phsfan 96:b50f5f795684 84 int AbitUSBModem::init()
phsfan 96:b50f5f795684 85 {
phsfan 96:b50f5f795684 86 //DBG("Entering init method for the VodafoneUSBModem");
phsfan 96:b50f5f795684 87 if( !m_dongleConnected )
phsfan 96:b50f5f795684 88 {
phsfan 96:b50f5f795684 89 DBG("Dongle is not connected");
phsfan 96:b50f5f795684 90
phsfan 96:b50f5f795684 91 m_dongle.connect();
phsfan 96:b50f5f795684 92
phsfan 96:b50f5f795684 93 m_dongleConnected = true;
phsfan 96:b50f5f795684 94 bool detectConnectedModem = false; // local variable to use to create a while loop that we can break out of - this is used to detect if we can see a modem or not
phsfan 96:b50f5f795684 95
phsfan 96:b50f5f795684 96 while(!detectConnectedModem)
phsfan 96:b50f5f795684 97 {
phsfan 96:b50f5f795684 98 for (int x=0; x<100;x++)
phsfan 96:b50f5f795684 99 {
phsfan 96:b50f5f795684 100 DBG("Trying to connect the dongle");
phsfan 96:b50f5f795684 101 m_dongle.connect();
phsfan 96:b50f5f795684 102 if (m_dongle.connected())
phsfan 96:b50f5f795684 103 {
phsfan 96:b50f5f795684 104 DBG("Great the dongle is connected - I've tried %d times to connect", x);
phsfan 96:b50f5f795684 105 detectConnectedModem = true; // OK we can break out this while loop now - the dongle has been connected
phsfan 96:b50f5f795684 106 break; // Break out of the for loop once the dongle is connected - otherwise try for a while more
phsfan 96:b50f5f795684 107 }
phsfan 96:b50f5f795684 108 Thread::wait(1000);
phsfan 96:b50f5f795684 109 }
phsfan 96:b50f5f795684 110 if (!detectConnectedModem)
phsfan 96:b50f5f795684 111 {
phsfan 96:b50f5f795684 112 // OK we got this far - so give up trying and let someone know you can't see the modem
phsfan 96:b50f5f795684 113 m_dongleConnected = false; // set the member variable of this object to false - so if we get called again we know we have to try to detect again
phsfan 96:b50f5f795684 114 ERR("There is no dongle pluged into the board, or the module does not respond. Is the module/modem switched on?");
phsfan 96:b50f5f795684 115 Thread:wait(1000);
phsfan 96:b50f5f795684 116 //DBG("Last ditch attempt to re-initialise the USB Subsystem");
phsfan 96:b50f5f795684 117 //m_dongle.init();
phsfan 96:b50f5f795684 118 return HARDWARE_NO_RESPONSE;
phsfan 96:b50f5f795684 119 }
phsfan 96:b50f5f795684 120 }
phsfan 96:b50f5f795684 121 }
phsfan 96:b50f5f795684 122
phsfan 96:b50f5f795684 123 if(m_atOpen)
phsfan 96:b50f5f795684 124 {
phsfan 96:b50f5f795684 125 return OK;
phsfan 96:b50f5f795684 126 }
phsfan 96:b50f5f795684 127
phsfan 96:b50f5f795684 128 DBG("Starting AT thread if needed");
phsfan 96:b50f5f795684 129 int ret = m_at.open();
phsfan 96:b50f5f795684 130 if(ret)
phsfan 96:b50f5f795684 131 {
phsfan 96:b50f5f795684 132 return ret;
phsfan 96:b50f5f795684 133 }
phsfan 96:b50f5f795684 134
phsfan 96:b50f5f795684 135 DBG("Sending initialisation commands");
phsfan 96:b50f5f795684 136 ret = m_at.init();
phsfan 96:b50f5f795684 137 if(ret)
phsfan 96:b50f5f795684 138 {
phsfan 96:b50f5f795684 139 return ret;
phsfan 96:b50f5f795684 140 }
phsfan 96:b50f5f795684 141 /*
phsfan 96:b50f5f795684 142 ret = m_at.executeSimple("ATE0", NULL);
phsfan 96:b50f5f795684 143 DBG("Result of command: Err code=%d", ret);
phsfan 96:b50f5f795684 144 if(ret != OK)
phsfan 96:b50f5f795684 145 {
phsfan 96:b50f5f795684 146 return NET_PROTOCOL;
phsfan 96:b50f5f795684 147 }
phsfan 96:b50f5f795684 148 */
phsfan 96:b50f5f795684 149 m_atOpen = true;
phsfan 96:b50f5f795684 150
phsfan 96:b50f5f795684 151 return OK;
phsfan 96:b50f5f795684 152 }
phsfan 96:b50f5f795684 153
phsfan 96:b50f5f795684 154 char* AbitUSBModem::getIPAddress()
phsfan 96:b50f5f795684 155 {
phsfan 96:b50f5f795684 156 return m_ppp.getIPAddress();
phsfan 96:b50f5f795684 157 }