Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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