This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.
Dependencies: mbed DebugLibrary
NetServices/drv/gprs/GPRSModem.cpp@0:f30e2135b0db, 2011-02-09 (annotated)
- Committer:
- Yann
- Date:
- Wed Feb 09 13:57:49 2011 +0000
- Revision:
- 0:f30e2135b0db
V0.0.0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 0:f30e2135b0db | 1 | |
Yann | 0:f30e2135b0db | 2 | /* |
Yann | 0:f30e2135b0db | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
Yann | 0:f30e2135b0db | 4 | |
Yann | 0:f30e2135b0db | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
Yann | 0:f30e2135b0db | 6 | of this software and associated documentation files (the "Software"), to deal |
Yann | 0:f30e2135b0db | 7 | in the Software without restriction, including without limitation the rights |
Yann | 0:f30e2135b0db | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Yann | 0:f30e2135b0db | 9 | copies of the Software, and to permit persons to whom the Software is |
Yann | 0:f30e2135b0db | 10 | furnished to do so, subject to the following conditions: |
Yann | 0:f30e2135b0db | 11 | |
Yann | 0:f30e2135b0db | 12 | The above copyright notice and this permission notice shall be included in |
Yann | 0:f30e2135b0db | 13 | all copies or substantial portions of the Software. |
Yann | 0:f30e2135b0db | 14 | |
Yann | 0:f30e2135b0db | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Yann | 0:f30e2135b0db | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Yann | 0:f30e2135b0db | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Yann | 0:f30e2135b0db | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Yann | 0:f30e2135b0db | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Yann | 0:f30e2135b0db | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Yann | 0:f30e2135b0db | 21 | THE SOFTWARE. |
Yann | 0:f30e2135b0db | 22 | */ |
Yann | 0:f30e2135b0db | 23 | |
Yann | 0:f30e2135b0db | 24 | #include "GPRSModem.h" |
Yann | 0:f30e2135b0db | 25 | #include "mbed.h" |
Yann | 0:f30e2135b0db | 26 | |
Yann | 0:f30e2135b0db | 27 | //#define __DEBUG |
Yann | 0:f30e2135b0db | 28 | #include "dbg/dbg.h" |
Yann | 0:f30e2135b0db | 29 | |
Yann | 0:f30e2135b0db | 30 | #define WAIT_BTW_NETW_POLLS 3. |
Yann | 0:f30e2135b0db | 31 | |
Yann | 0:f30e2135b0db | 32 | #include "netCfg.h" |
Yann | 0:f30e2135b0db | 33 | #if NET_GPRS |
Yann | 0:f30e2135b0db | 34 | |
Yann | 0:f30e2135b0db | 35 | GPRSModem::GPRSModem() : ATIf() |
Yann | 0:f30e2135b0db | 36 | { |
Yann | 0:f30e2135b0db | 37 | DBG("New GPRSModem@%p\n", this); |
Yann | 0:f30e2135b0db | 38 | } |
Yann | 0:f30e2135b0db | 39 | |
Yann | 0:f30e2135b0db | 40 | GPRSModem::~GPRSModem() |
Yann | 0:f30e2135b0db | 41 | { |
Yann | 0:f30e2135b0db | 42 | |
Yann | 0:f30e2135b0db | 43 | } |
Yann | 0:f30e2135b0db | 44 | |
Yann | 0:f30e2135b0db | 45 | GPRSErr GPRSModem::getNetworkState() |
Yann | 0:f30e2135b0db | 46 | { |
Yann | 0:f30e2135b0db | 47 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 48 | /* |
Yann | 0:f30e2135b0db | 49 | netState can be : (Telit_AT_Reference_Guide.pdf p.98) |
Yann | 0:f30e2135b0db | 50 | 0 - not registered, ME is not currently searching a new operator to register to |
Yann | 0:f30e2135b0db | 51 | 1 - registered, home network |
Yann | 0:f30e2135b0db | 52 | 2 - not registered, but ME is currently searching a new operator to register to |
Yann | 0:f30e2135b0db | 53 | 3 - registration denied |
Yann | 0:f30e2135b0db | 54 | 4 - unknown |
Yann | 0:f30e2135b0db | 55 | 5 - registered, roaming |
Yann | 0:f30e2135b0db | 56 | */ |
Yann | 0:f30e2135b0db | 57 | // DBG("Network?...\r\n"); |
Yann | 0:f30e2135b0db | 58 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 59 | ATIf::setTimeout(10000); |
Yann | 0:f30e2135b0db | 60 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 61 | int netState = 0; |
Yann | 0:f30e2135b0db | 62 | int len; |
Yann | 0:f30e2135b0db | 63 | len = ATIf::printf("AT+CREG?"); //Registered ? |
Yann | 0:f30e2135b0db | 64 | if(!len) DBG("\r\nprintf - len=%d\r\n",len); |
Yann | 0:f30e2135b0db | 65 | if(!len) |
Yann | 0:f30e2135b0db | 66 | return GPRS_MODEM; //Nothing was actually sent |
Yann | 0:f30e2135b0db | 67 | |
Yann | 0:f30e2135b0db | 68 | len = ATIf::scanf("+CREG: 0,%d", &netState); //Get status |
Yann | 0:f30e2135b0db | 69 | if(len != 1) DBG("\r\nscanf - len=%d\r\n",len); |
Yann | 0:f30e2135b0db | 70 | if(len != 1) //Likely +CMS ERROR was returned |
Yann | 0:f30e2135b0db | 71 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 72 | |
Yann | 0:f30e2135b0db | 73 | if( !!ATIf::checkOK() ) //Should not be a problem |
Yann | 0:f30e2135b0db | 74 | {DBG("\r\nNOK\r\n"); return GPRS_MODEM; } |
Yann | 0:f30e2135b0db | 75 | |
Yann | 0:f30e2135b0db | 76 | switch(netState) |
Yann | 0:f30e2135b0db | 77 | { |
Yann | 0:f30e2135b0db | 78 | case 1: |
Yann | 0:f30e2135b0db | 79 | case 5: //TODO: Option allow roaming |
Yann | 0:f30e2135b0db | 80 | DBG("\r\nNetwork is up!\r\n"); |
Yann | 0:f30e2135b0db | 81 | return GPRS_OK; |
Yann | 0:f30e2135b0db | 82 | case 3: |
Yann | 0:f30e2135b0db | 83 | DBG("\r\nAccess to network denied.\r\n"); |
Yann | 0:f30e2135b0db | 84 | return GPRS_DENIED; |
Yann | 0:f30e2135b0db | 85 | case 0: |
Yann | 0:f30e2135b0db | 86 | DBG("\r\nNo network.\r\n"); |
Yann | 0:f30e2135b0db | 87 | return GPRS_NONETWORK; |
Yann | 0:f30e2135b0db | 88 | case 4: |
Yann | 0:f30e2135b0db | 89 | case 2: |
Yann | 0:f30e2135b0db | 90 | //DBG("\r\nRegistering...\r\n"); |
Yann | 0:f30e2135b0db | 91 | return GPRS_REGISTERING; |
Yann | 0:f30e2135b0db | 92 | } |
Yann | 0:f30e2135b0db | 93 | |
Yann | 0:f30e2135b0db | 94 | return GPRS_MODEM; // Should not reach this |
Yann | 0:f30e2135b0db | 95 | |
Yann | 0:f30e2135b0db | 96 | } |
Yann | 0:f30e2135b0db | 97 | |
Yann | 0:f30e2135b0db | 98 | GPRSErr GPRSModem::setNetworkUp() |
Yann | 0:f30e2135b0db | 99 | { |
Yann | 0:f30e2135b0db | 100 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 101 | GPRSErr err = GPRS_REGISTERING; |
Yann | 0:f30e2135b0db | 102 | while(true) |
Yann | 0:f30e2135b0db | 103 | { |
Yann | 0:f30e2135b0db | 104 | err = getNetworkState(); |
Yann | 0:f30e2135b0db | 105 | if(err != GPRS_REGISTERING) |
Yann | 0:f30e2135b0db | 106 | break; |
Yann | 0:f30e2135b0db | 107 | wait(WAIT_BTW_NETW_POLLS); |
Yann | 0:f30e2135b0db | 108 | } |
Yann | 0:f30e2135b0db | 109 | return err; |
Yann | 0:f30e2135b0db | 110 | } |
Yann | 0:f30e2135b0db | 111 | |
Yann | 0:f30e2135b0db | 112 | //Same, but for GPRS |
Yann | 0:f30e2135b0db | 113 | GPRSErr GPRSModem::getGPRSState() |
Yann | 0:f30e2135b0db | 114 | { |
Yann | 0:f30e2135b0db | 115 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 116 | /* |
Yann | 0:f30e2135b0db | 117 | netState can be : (Telit_AT_Reference_Guide.pdf p.192) |
Yann | 0:f30e2135b0db | 118 | 0 - not registered, terminal is not currently searching a new operator to register to |
Yann | 0:f30e2135b0db | 119 | 1 - registered, home network |
Yann | 0:f30e2135b0db | 120 | 2 - not registered, but terminal is currently searching a new operator to register to |
Yann | 0:f30e2135b0db | 121 | 3 - registration denied |
Yann | 0:f30e2135b0db | 122 | 4 - unknown |
Yann | 0:f30e2135b0db | 123 | 5 - registered, roaming |
Yann | 0:f30e2135b0db | 124 | */ |
Yann | 0:f30e2135b0db | 125 | |
Yann | 0:f30e2135b0db | 126 | DBG("GPRS?...\r\n"); |
Yann | 0:f30e2135b0db | 127 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 128 | ATIf::setTimeout(10000); |
Yann | 0:f30e2135b0db | 129 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 130 | int netState = 0; |
Yann | 0:f30e2135b0db | 131 | int len; |
Yann | 0:f30e2135b0db | 132 | len = ATIf::printf("AT+CGREG?"); //Registered ? |
Yann | 0:f30e2135b0db | 133 | if(!len) |
Yann | 0:f30e2135b0db | 134 | return GPRS_MODEM; //Nothing was actually sent |
Yann | 0:f30e2135b0db | 135 | |
Yann | 0:f30e2135b0db | 136 | len = ATIf::scanf("+CGREG: %*d,%d", &netState); //Get GPRS status, see GSM 07.07 spec as Telit AT ref is wrong |
Yann | 0:f30e2135b0db | 137 | if(len != 1) DBG("\r\nscanf - len=%d\r\n",len); |
Yann | 0:f30e2135b0db | 138 | if(len != 1) //Likely +CMS ERROR was returned |
Yann | 0:f30e2135b0db | 139 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 140 | |
Yann | 0:f30e2135b0db | 141 | if( !!ATIf::checkOK() ) //Should not be a problem |
Yann | 0:f30e2135b0db | 142 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 143 | |
Yann | 0:f30e2135b0db | 144 | switch(netState) |
Yann | 0:f30e2135b0db | 145 | { |
Yann | 0:f30e2135b0db | 146 | case 1: |
Yann | 0:f30e2135b0db | 147 | case 5: //TODO: Option allow roaming |
Yann | 0:f30e2135b0db | 148 | DBG("\r\nNetwork is up!\r\n"); |
Yann | 0:f30e2135b0db | 149 | return GPRS_OK; |
Yann | 0:f30e2135b0db | 150 | case 3: |
Yann | 0:f30e2135b0db | 151 | DBG("\r\nAccess to network denied.\r\n"); |
Yann | 0:f30e2135b0db | 152 | return GPRS_DENIED; |
Yann | 0:f30e2135b0db | 153 | case 0: |
Yann | 0:f30e2135b0db | 154 | DBG("\r\nNo network.\r\n"); |
Yann | 0:f30e2135b0db | 155 | return GPRS_NONETWORK; |
Yann | 0:f30e2135b0db | 156 | case 4: |
Yann | 0:f30e2135b0db | 157 | case 2: |
Yann | 0:f30e2135b0db | 158 | DBG("\r\nRegistering...\r\n"); |
Yann | 0:f30e2135b0db | 159 | return GPRS_REGISTERING; |
Yann | 0:f30e2135b0db | 160 | } |
Yann | 0:f30e2135b0db | 161 | |
Yann | 0:f30e2135b0db | 162 | return GPRS_MODEM; // Should not reach this |
Yann | 0:f30e2135b0db | 163 | |
Yann | 0:f30e2135b0db | 164 | } |
Yann | 0:f30e2135b0db | 165 | |
Yann | 0:f30e2135b0db | 166 | GPRSErr GPRSModem::setGPRSUp() |
Yann | 0:f30e2135b0db | 167 | { |
Yann | 0:f30e2135b0db | 168 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 169 | GPRSErr err; |
Yann | 0:f30e2135b0db | 170 | |
Yann | 0:f30e2135b0db | 171 | err = setNetworkUp(); |
Yann | 0:f30e2135b0db | 172 | if(err) |
Yann | 0:f30e2135b0db | 173 | return err; |
Yann | 0:f30e2135b0db | 174 | |
Yann | 0:f30e2135b0db | 175 | DBG("\r\nAttaching GPRS...\r\n"); |
Yann | 0:f30e2135b0db | 176 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 177 | ATIf::setTimeout(10000); |
Yann | 0:f30e2135b0db | 178 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 179 | int len; |
Yann | 0:f30e2135b0db | 180 | |
Yann | 0:f30e2135b0db | 181 | err = getGPRSState(); |
Yann | 0:f30e2135b0db | 182 | if(err == GPRS_NONETWORK) |
Yann | 0:f30e2135b0db | 183 | { |
Yann | 0:f30e2135b0db | 184 | len = ATIf::printf("AT+CGATT=1"); //Attach |
Yann | 0:f30e2135b0db | 185 | if(!len) |
Yann | 0:f30e2135b0db | 186 | return GPRS_MODEM; //Nothing was actually sent |
Yann | 0:f30e2135b0db | 187 | |
Yann | 0:f30e2135b0db | 188 | if( !!ATIf::checkOK() ) //Should not be a problem |
Yann | 0:f30e2135b0db | 189 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 190 | } |
Yann | 0:f30e2135b0db | 191 | |
Yann | 0:f30e2135b0db | 192 | while(true) |
Yann | 0:f30e2135b0db | 193 | { |
Yann | 0:f30e2135b0db | 194 | err = getGPRSState(); |
Yann | 0:f30e2135b0db | 195 | if(err != GPRS_REGISTERING) |
Yann | 0:f30e2135b0db | 196 | break; |
Yann | 0:f30e2135b0db | 197 | wait(WAIT_BTW_NETW_POLLS); |
Yann | 0:f30e2135b0db | 198 | } |
Yann | 0:f30e2135b0db | 199 | return err; |
Yann | 0:f30e2135b0db | 200 | } |
Yann | 0:f30e2135b0db | 201 | |
Yann | 0:f30e2135b0db | 202 | GPRSErr GPRSModem::setGPRSDown() |
Yann | 0:f30e2135b0db | 203 | { |
Yann | 0:f30e2135b0db | 204 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 205 | DBG("\r\nDetaching GPRS...\r\n"); |
Yann | 0:f30e2135b0db | 206 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 207 | ATIf::setTimeout(10000); |
Yann | 0:f30e2135b0db | 208 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 209 | int len; |
Yann | 0:f30e2135b0db | 210 | |
Yann | 0:f30e2135b0db | 211 | len = ATIf::printf("AT+CGATT=0"); //Detach |
Yann | 0:f30e2135b0db | 212 | if(!len) |
Yann | 0:f30e2135b0db | 213 | return GPRS_MODEM; //Nothing was actually sent |
Yann | 0:f30e2135b0db | 214 | |
Yann | 0:f30e2135b0db | 215 | if( !!ATIf::checkOK() ) //Should not be a problem |
Yann | 0:f30e2135b0db | 216 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 217 | |
Yann | 0:f30e2135b0db | 218 | return GPRS_OK; |
Yann | 0:f30e2135b0db | 219 | } |
Yann | 0:f30e2135b0db | 220 | |
Yann | 0:f30e2135b0db | 221 | |
Yann | 0:f30e2135b0db | 222 | GPRSErr GPRSModem::connect(const char* apn /*=NULL*/) |
Yann | 0:f30e2135b0db | 223 | { |
Yann | 0:f30e2135b0db | 224 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 225 | GPRSErr err; |
Yann | 0:f30e2135b0db | 226 | |
Yann | 0:f30e2135b0db | 227 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 228 | ATIf::setTimeout(5000); |
Yann | 0:f30e2135b0db | 229 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 230 | |
Yann | 0:f30e2135b0db | 231 | DBG("\r\nConnecting...\r\n"); |
Yann | 0:f30e2135b0db | 232 | |
Yann | 0:f30e2135b0db | 233 | int len; |
Yann | 0:f30e2135b0db | 234 | |
Yann | 0:f30e2135b0db | 235 | if( apn != NULL ) //Config APN |
Yann | 0:f30e2135b0db | 236 | { |
Yann | 0:f30e2135b0db | 237 | len = ATIf::printf("AT+CGDCONT=1,\"IP\",\"%s\"",apn); //Define APN |
Yann | 0:f30e2135b0db | 238 | if(!len) |
Yann | 0:f30e2135b0db | 239 | return GPRS_MODEM; //Nothing was actually sent |
Yann | 0:f30e2135b0db | 240 | |
Yann | 0:f30e2135b0db | 241 | if( !!ATIf::checkOK() ) //Should not be a problem |
Yann | 0:f30e2135b0db | 242 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 243 | } |
Yann | 0:f30e2135b0db | 244 | |
Yann | 0:f30e2135b0db | 245 | err = setGPRSUp(); |
Yann | 0:f30e2135b0db | 246 | if(err) |
Yann | 0:f30e2135b0db | 247 | return err; |
Yann | 0:f30e2135b0db | 248 | |
Yann | 0:f30e2135b0db | 249 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 250 | ATIf::setTimeout(60000); |
Yann | 0:f30e2135b0db | 251 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 252 | // |
Yann | 0:f30e2135b0db | 253 | //len = ATIf::printf("AT+CGDATA=\"PPP\",1"); //Connect using PDP context #1 |
Yann | 0:f30e2135b0db | 254 | // len = ATIf::printf("ATDT *99***1#"); |
Yann | 0:f30e2135b0db | 255 | len = ATIf::printf("ATDT *99#"); |
Yann | 0:f30e2135b0db | 256 | if(!len) |
Yann | 0:f30e2135b0db | 257 | return GPRS_MODEM; //Nothing was actually sent |
Yann | 0:f30e2135b0db | 258 | |
Yann | 0:f30e2135b0db | 259 | len = ATIf::scanf("CONNECT"); //Beginning of session |
Yann | 0:f30e2135b0db | 260 | if(len != 0) //Likely +CME ERROR was returned or NO CARRIER |
Yann | 0:f30e2135b0db | 261 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 262 | |
Yann | 0:f30e2135b0db | 263 | //ATIf::setSignals(false); |
Yann | 0:f30e2135b0db | 264 | |
Yann | 0:f30e2135b0db | 265 | DBG("\r\nConnected.\r\n"); |
Yann | 0:f30e2135b0db | 266 | |
Yann | 0:f30e2135b0db | 267 | return GPRS_OK; //Time to enter a PPP Session ! |
Yann | 0:f30e2135b0db | 268 | |
Yann | 0:f30e2135b0db | 269 | } |
Yann | 0:f30e2135b0db | 270 | |
Yann | 0:f30e2135b0db | 271 | GPRSErr GPRSModem::disconnect() |
Yann | 0:f30e2135b0db | 272 | { |
Yann | 0:f30e2135b0db | 273 | ATIf::flushBuffer(); |
Yann | 0:f30e2135b0db | 274 | ATIf::setReadMode(false); //Discard chars |
Yann | 0:f30e2135b0db | 275 | ATIf::setTimeout(5000); |
Yann | 0:f30e2135b0db | 276 | ATIf::setLineMode(true); //Line mode |
Yann | 0:f30e2135b0db | 277 | |
Yann | 0:f30e2135b0db | 278 | if( !!ATIf::checkOK() ) //Should be present at the end of connection |
Yann | 0:f30e2135b0db | 279 | return GPRS_MODEM; |
Yann | 0:f30e2135b0db | 280 | |
Yann | 0:f30e2135b0db | 281 | GPRSErr err; |
Yann | 0:f30e2135b0db | 282 | err = setGPRSDown(); |
Yann | 0:f30e2135b0db | 283 | if(err) |
Yann | 0:f30e2135b0db | 284 | return err; |
Yann | 0:f30e2135b0db | 285 | |
Yann | 0:f30e2135b0db | 286 | DBG("\r\nDisconnected.\r\n"); |
Yann | 0:f30e2135b0db | 287 | |
Yann | 0:f30e2135b0db | 288 | return GPRS_OK; |
Yann | 0:f30e2135b0db | 289 | } |
Yann | 0:f30e2135b0db | 290 | |
Yann | 0:f30e2135b0db | 291 | #endif |
Yann | 0:f30e2135b0db | 292 |