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

Dependencies:   Socket lwip-sys lwip

Fork of AbitUSBModem by phs fan

Committer:
phsfan
Date:
Wed Jul 01 01:00:31 2015 +0000
Revision:
102:f5bcfb224067
Parent:
101:b330179423c8
fix flow control

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 100:dbd92e9515ef 33 #include "AbitModemInterface.h"
phsfan 96:b50f5f795684 34 #include "Socket.h"
phsfan 96:b50f5f795684 35
phsfan 100:dbd92e9515ef 36 AbitModemInterface::AbitModemInterface (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, int baud) :
phsfan 100:dbd92e9515ef 37 m_module(tx, rx), // Construct AbitModemInterface: Serial interface
phsfan 101:b330179423c8 38 #if !DEVICE_SERIAL_FC
phsfan 100:dbd92e9515ef 39 m_cts(cts),
phsfan 100:dbd92e9515ef 40 m_rts(rts),
phsfan 101:b330179423c8 41 #endif
phsfan 100:dbd92e9515ef 42 m_reset(reset),
phsfan 100:dbd92e9515ef 43 m_pppStream(m_module), // PPP connections are managed via another serial channel.
phsfan 96:b50f5f795684 44 m_at(&m_pppStream), // Construct ATCommandsInterface with the AT serial channel
phsfan 97:7d9cc95e2ea7 45 m_sms(&m_at), // Construct SMSInterface with the ATCommandsInterface
phsfan 96:b50f5f795684 46 m_ppp(&m_pppStream, &m_pppStream, &m_at, false), // Construct PPPIPInterface with the PPP serial channel
phsfan 100:dbd92e9515ef 47 m_moduleConnected(false), // Dongle is initially not ready for anything
phsfan 96:b50f5f795684 48 m_ipInit(false), // PPIPInterface connection is initially down
phsfan 97:7d9cc95e2ea7 49 m_smsInit(false), // SMSInterface starts un-initialised
phsfan 96:b50f5f795684 50 m_atOpen(false) // ATCommandsInterface starts in a closed state
phsfan 96:b50f5f795684 51 {
phsfan 101:b330179423c8 52 m_reset.output();
phsfan 100:dbd92e9515ef 53 m_reset = 0;
phsfan 100:dbd92e9515ef 54 m_module.baud(baud);
phsfan 101:b330179423c8 55 #if DEVICE_SERIAL_FC
phsfan 102:f5bcfb224067 56 if (rts != NC && cts != NC) {
phsfan 102:f5bcfb224067 57 m_module.set_flow_control(Serial::RTSCTS, rts, cts);
phsfan 102:f5bcfb224067 58 } else
phsfan 102:f5bcfb224067 59 if (rts != NC && cts == NC) {
phsfan 102:f5bcfb224067 60 m_module.set_flow_control(Serial::RTS, rts);
phsfan 102:f5bcfb224067 61 } else
phsfan 102:f5bcfb224067 62 if (rts == NC && cts != NC) {
phsfan 102:f5bcfb224067 63 m_module.set_flow_control(Serial::CTS, cts);
phsfan 102:f5bcfb224067 64 }
phsfan 101:b330179423c8 65 #else
phsfan 100:dbd92e9515ef 66 m_rts = 0;
phsfan 100:dbd92e9515ef 67 m_cts.mode(PullUp);
phsfan 101:b330179423c8 68 #endif
phsfan 100:dbd92e9515ef 69 Thread::wait(100);
phsfan 101:b330179423c8 70 m_reset.input();
phsfan 101:b330179423c8 71 m_reset.mode(PullUp);
phsfan 96:b50f5f795684 72 }
phsfan 96:b50f5f795684 73
phsfan 100:dbd92e9515ef 74 int AbitModemInterface::connect (const char* user, const char* password) {
phsfan 96:b50f5f795684 75
phsfan 96:b50f5f795684 76 if( !m_ipInit )
phsfan 96:b50f5f795684 77 {
phsfan 96:b50f5f795684 78 m_ipInit = true;
phsfan 96:b50f5f795684 79 m_ppp.init();
phsfan 96:b50f5f795684 80 }
phsfan 96:b50f5f795684 81 m_ppp.setup(user, password);
phsfan 96:b50f5f795684 82
phsfan 96:b50f5f795684 83 int ret = init();
phsfan 96:b50f5f795684 84 if(ret)
phsfan 96:b50f5f795684 85 {
phsfan 96:b50f5f795684 86 return ret;
phsfan 96:b50f5f795684 87 }
phsfan 96:b50f5f795684 88
phsfan 96:b50f5f795684 89 m_at.close(); // Closing AT parser
phsfan 96:b50f5f795684 90 m_atOpen = false; //Will need to be reinitialized afterwards
phsfan 96:b50f5f795684 91
phsfan 96:b50f5f795684 92 DBG("Connecting PPP");
phsfan 96:b50f5f795684 93
phsfan 96:b50f5f795684 94 ret = m_ppp.connect();
phsfan 96:b50f5f795684 95 DBG("Result of connect: Err code=%d", ret);
phsfan 96:b50f5f795684 96 return ret;
phsfan 96:b50f5f795684 97 }
phsfan 96:b50f5f795684 98
phsfan 100:dbd92e9515ef 99 int AbitModemInterface::disconnect () {
phsfan 96:b50f5f795684 100 DBG("Disconnecting from PPP");
phsfan 96:b50f5f795684 101 int ret = m_ppp.disconnect();
phsfan 96:b50f5f795684 102 if(ret)
phsfan 96:b50f5f795684 103 {
phsfan 96:b50f5f795684 104 ERR("Disconnect returned %d, still trying to disconnect", ret);
phsfan 96:b50f5f795684 105 }
phsfan 96:b50f5f795684 106
phsfan 96:b50f5f795684 107 return OK;
phsfan 96:b50f5f795684 108 }
phsfan 96:b50f5f795684 109
phsfan 96:b50f5f795684 110
phsfan 96:b50f5f795684 111
phsfan 100:dbd92e9515ef 112 int AbitModemInterface::init()
phsfan 96:b50f5f795684 113 {
phsfan 96:b50f5f795684 114 //DBG("Entering init method for the VodafoneUSBModem");
phsfan 100:dbd92e9515ef 115 if( !m_moduleConnected )
phsfan 96:b50f5f795684 116 {
phsfan 96:b50f5f795684 117 DBG("Dongle is not connected");
phsfan 96:b50f5f795684 118
phsfan 100:dbd92e9515ef 119 m_moduleConnected = true;
phsfan 100:dbd92e9515ef 120 Thread::wait(3000);
phsfan 96:b50f5f795684 121
phsfan 96:b50f5f795684 122 }
phsfan 96:b50f5f795684 123
phsfan 96:b50f5f795684 124 if(m_atOpen)
phsfan 96:b50f5f795684 125 {
phsfan 96:b50f5f795684 126 return OK;
phsfan 96:b50f5f795684 127 }
phsfan 96:b50f5f795684 128
phsfan 96:b50f5f795684 129 DBG("Starting AT thread if needed");
phsfan 96:b50f5f795684 130 int ret = m_at.open();
phsfan 96:b50f5f795684 131 if(ret)
phsfan 96:b50f5f795684 132 {
phsfan 96:b50f5f795684 133 return ret;
phsfan 96:b50f5f795684 134 }
phsfan 96:b50f5f795684 135
phsfan 96:b50f5f795684 136 DBG("Sending initialisation commands");
phsfan 96:b50f5f795684 137 ret = m_at.init();
phsfan 96:b50f5f795684 138 if(ret)
phsfan 96:b50f5f795684 139 {
phsfan 96:b50f5f795684 140 return ret;
phsfan 96:b50f5f795684 141 }
phsfan 100:dbd92e9515ef 142
phsfan 100:dbd92e9515ef 143 ret = m_at.executeSimple("ATE1", NULL);
phsfan 96:b50f5f795684 144 DBG("Result of command: Err code=%d", ret);
phsfan 96:b50f5f795684 145 if(ret != OK)
phsfan 96:b50f5f795684 146 {
phsfan 96:b50f5f795684 147 return NET_PROTOCOL;
phsfan 96:b50f5f795684 148 }
phsfan 100:dbd92e9515ef 149
phsfan 100:dbd92e9515ef 150 m_at.executeSimple("AT#S1", NULL);
phsfan 100:dbd92e9515ef 151 m_at.executeSimple("AT#B1", NULL);
phsfan 100:dbd92e9515ef 152 Thread::wait(5000);
phsfan 100:dbd92e9515ef 153
phsfan 96:b50f5f795684 154 m_atOpen = true;
phsfan 96:b50f5f795684 155
phsfan 96:b50f5f795684 156 return OK;
phsfan 96:b50f5f795684 157 }
phsfan 96:b50f5f795684 158
phsfan 97:7d9cc95e2ea7 159
phsfan 100:dbd92e9515ef 160 int AbitModemInterface::sendSM(const char* number, const char* message)
phsfan 97:7d9cc95e2ea7 161 {
phsfan 97:7d9cc95e2ea7 162 int ret = init();
phsfan 97:7d9cc95e2ea7 163 if(ret)
phsfan 97:7d9cc95e2ea7 164 {
phsfan 97:7d9cc95e2ea7 165 return ret;
phsfan 97:7d9cc95e2ea7 166 }
phsfan 97:7d9cc95e2ea7 167
phsfan 97:7d9cc95e2ea7 168 if(!m_smsInit)
phsfan 97:7d9cc95e2ea7 169 {
phsfan 97:7d9cc95e2ea7 170 ret = m_sms.init();
phsfan 97:7d9cc95e2ea7 171 if(ret)
phsfan 97:7d9cc95e2ea7 172 {
phsfan 97:7d9cc95e2ea7 173 return ret;
phsfan 97:7d9cc95e2ea7 174 }
phsfan 97:7d9cc95e2ea7 175 m_smsInit = true;
phsfan 97:7d9cc95e2ea7 176 }
phsfan 97:7d9cc95e2ea7 177
phsfan 97:7d9cc95e2ea7 178 ret = m_sms.send(number, message);
phsfan 97:7d9cc95e2ea7 179 if(ret)
phsfan 97:7d9cc95e2ea7 180 {
phsfan 97:7d9cc95e2ea7 181 return ret;
phsfan 97:7d9cc95e2ea7 182 }
phsfan 97:7d9cc95e2ea7 183
phsfan 97:7d9cc95e2ea7 184 return OK;
phsfan 97:7d9cc95e2ea7 185 }
phsfan 97:7d9cc95e2ea7 186
phsfan 100:dbd92e9515ef 187 int AbitModemInterface::getSM(char* number, char* message, size_t maxLength)
phsfan 97:7d9cc95e2ea7 188 {
phsfan 97:7d9cc95e2ea7 189 int ret = init();
phsfan 97:7d9cc95e2ea7 190 if(ret)
phsfan 97:7d9cc95e2ea7 191 {
phsfan 97:7d9cc95e2ea7 192 return ret;
phsfan 97:7d9cc95e2ea7 193 }
phsfan 97:7d9cc95e2ea7 194
phsfan 97:7d9cc95e2ea7 195 if(!m_smsInit)
phsfan 97:7d9cc95e2ea7 196 {
phsfan 97:7d9cc95e2ea7 197 ret = m_sms.init();
phsfan 97:7d9cc95e2ea7 198 if(ret)
phsfan 97:7d9cc95e2ea7 199 {
phsfan 97:7d9cc95e2ea7 200 return ret;
phsfan 97:7d9cc95e2ea7 201 }
phsfan 97:7d9cc95e2ea7 202 m_smsInit = true;
phsfan 97:7d9cc95e2ea7 203 }
phsfan 97:7d9cc95e2ea7 204
phsfan 97:7d9cc95e2ea7 205 ret = m_sms.get(number, message, maxLength);
phsfan 97:7d9cc95e2ea7 206 if(ret)
phsfan 97:7d9cc95e2ea7 207 {
phsfan 97:7d9cc95e2ea7 208 return ret;
phsfan 97:7d9cc95e2ea7 209 }
phsfan 97:7d9cc95e2ea7 210
phsfan 97:7d9cc95e2ea7 211 return OK;
phsfan 97:7d9cc95e2ea7 212 }
phsfan 97:7d9cc95e2ea7 213
phsfan 100:dbd92e9515ef 214 char* AbitModemInterface::getIPAddress()
phsfan 96:b50f5f795684 215 {
phsfan 96:b50f5f795684 216 return m_ppp.getIPAddress();
phsfan 96:b50f5f795684 217 }