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 14:11:24 2015 +0000
Revision:
97:7d9cc95e2ea7
Parent:
96:b50f5f795684
Child:
99:514e67a69ad6
support sms

Who changed what in which revision?

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