平成24年中国四国技術職員研修用
Dependencies: MSCUsbHost NetServices RPCInterface TextLCD mbed
NetServices/if/gprsmodule/GPRSModuleNetIf.cpp@0:fd83f3540b1a, 2012-08-18 (annotated)
- Committer:
- yueee_yt
- Date:
- Sat Aug 18 07:26:48 2012 +0000
- Revision:
- 0:fd83f3540b1a
NTP????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:fd83f3540b1a | 1 | |
yueee_yt | 0:fd83f3540b1a | 2 | /* |
yueee_yt | 0:fd83f3540b1a | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
yueee_yt | 0:fd83f3540b1a | 4 | |
yueee_yt | 0:fd83f3540b1a | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yueee_yt | 0:fd83f3540b1a | 6 | of this software and associated documentation files (the "Software"), to deal |
yueee_yt | 0:fd83f3540b1a | 7 | in the Software without restriction, including without limitation the rights |
yueee_yt | 0:fd83f3540b1a | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yueee_yt | 0:fd83f3540b1a | 9 | copies of the Software, and to permit persons to whom the Software is |
yueee_yt | 0:fd83f3540b1a | 10 | furnished to do so, subject to the following conditions: |
yueee_yt | 0:fd83f3540b1a | 11 | |
yueee_yt | 0:fd83f3540b1a | 12 | The above copyright notice and this permission notice shall be included in |
yueee_yt | 0:fd83f3540b1a | 13 | all copies or substantial portions of the Software. |
yueee_yt | 0:fd83f3540b1a | 14 | |
yueee_yt | 0:fd83f3540b1a | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yueee_yt | 0:fd83f3540b1a | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yueee_yt | 0:fd83f3540b1a | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yueee_yt | 0:fd83f3540b1a | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yueee_yt | 0:fd83f3540b1a | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yueee_yt | 0:fd83f3540b1a | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yueee_yt | 0:fd83f3540b1a | 21 | THE SOFTWARE. |
yueee_yt | 0:fd83f3540b1a | 22 | */ |
yueee_yt | 0:fd83f3540b1a | 23 | |
yueee_yt | 0:fd83f3540b1a | 24 | #include "netCfg.h" |
yueee_yt | 0:fd83f3540b1a | 25 | #if NET_GPRS_MODULE |
yueee_yt | 0:fd83f3540b1a | 26 | |
yueee_yt | 0:fd83f3540b1a | 27 | #include "GPRSModuleNetIf.h" |
yueee_yt | 0:fd83f3540b1a | 28 | |
yueee_yt | 0:fd83f3540b1a | 29 | #define __DEBUG |
yueee_yt | 0:fd83f3540b1a | 30 | #include "dbg/dbg.h" |
yueee_yt | 0:fd83f3540b1a | 31 | |
yueee_yt | 0:fd83f3540b1a | 32 | GPRSModuleNetIf::GPRSModuleNetIf(PinName tx, PinName rx, int baud /*= 115200*/) : PPPNetIf(NULL), m_serial(tx, rx) |
yueee_yt | 0:fd83f3540b1a | 33 | { |
yueee_yt | 0:fd83f3540b1a | 34 | PPPNetIf::m_pIf = new GPRSModem(); |
yueee_yt | 0:fd83f3540b1a | 35 | m_serial.baud(baud); |
yueee_yt | 0:fd83f3540b1a | 36 | } |
yueee_yt | 0:fd83f3540b1a | 37 | |
yueee_yt | 0:fd83f3540b1a | 38 | GPRSModuleNetIf::~GPRSModuleNetIf() |
yueee_yt | 0:fd83f3540b1a | 39 | { |
yueee_yt | 0:fd83f3540b1a | 40 | delete PPPNetIf::m_pIf; |
yueee_yt | 0:fd83f3540b1a | 41 | } |
yueee_yt | 0:fd83f3540b1a | 42 | |
yueee_yt | 0:fd83f3540b1a | 43 | PPPErr GPRSModuleNetIf::connect(const char* apn /*= NULL*/, const char* userId /*= NULL*/, const char* password /*= NULL*/) //Connect using GPRS |
yueee_yt | 0:fd83f3540b1a | 44 | { |
yueee_yt | 0:fd83f3540b1a | 45 | |
yueee_yt | 0:fd83f3540b1a | 46 | DBG("Powering on module.\n") |
yueee_yt | 0:fd83f3540b1a | 47 | if(!setOn()) //Could not power on module |
yueee_yt | 0:fd83f3540b1a | 48 | { |
yueee_yt | 0:fd83f3540b1a | 49 | DBG("Could not power on module.\n"); |
yueee_yt | 0:fd83f3540b1a | 50 | return PPP_MODEM; |
yueee_yt | 0:fd83f3540b1a | 51 | } |
yueee_yt | 0:fd83f3540b1a | 52 | |
yueee_yt | 0:fd83f3540b1a | 53 | //wait(10); //Wait for module to init. |
yueee_yt | 0:fd83f3540b1a | 54 | |
yueee_yt | 0:fd83f3540b1a | 55 | ATErr atErr; |
yueee_yt | 0:fd83f3540b1a | 56 | for(int i=0; i<3; i++) |
yueee_yt | 0:fd83f3540b1a | 57 | { |
yueee_yt | 0:fd83f3540b1a | 58 | atErr = m_pIf->open(&m_serial); //3 tries |
yueee_yt | 0:fd83f3540b1a | 59 | if(!atErr) |
yueee_yt | 0:fd83f3540b1a | 60 | break; |
yueee_yt | 0:fd83f3540b1a | 61 | DBG("Could not open AT If, trying again.\n"); |
yueee_yt | 0:fd83f3540b1a | 62 | wait(4); |
yueee_yt | 0:fd83f3540b1a | 63 | } |
yueee_yt | 0:fd83f3540b1a | 64 | |
yueee_yt | 0:fd83f3540b1a | 65 | if(atErr) |
yueee_yt | 0:fd83f3540b1a | 66 | { |
yueee_yt | 0:fd83f3540b1a | 67 | setOff(); |
yueee_yt | 0:fd83f3540b1a | 68 | return PPP_MODEM; |
yueee_yt | 0:fd83f3540b1a | 69 | } |
yueee_yt | 0:fd83f3540b1a | 70 | |
yueee_yt | 0:fd83f3540b1a | 71 | DBG("AT If opened.\n"); |
yueee_yt | 0:fd83f3540b1a | 72 | |
yueee_yt | 0:fd83f3540b1a | 73 | PPPErr pppErr; |
yueee_yt | 0:fd83f3540b1a | 74 | for(int i=0; i<3; i++) |
yueee_yt | 0:fd83f3540b1a | 75 | { |
yueee_yt | 0:fd83f3540b1a | 76 | DBG("Trying to connect.\n"); |
yueee_yt | 0:fd83f3540b1a | 77 | pppErr = PPPNetIf::GPRSConnect(apn, userId, password); |
yueee_yt | 0:fd83f3540b1a | 78 | if(!pppErr) |
yueee_yt | 0:fd83f3540b1a | 79 | break; |
yueee_yt | 0:fd83f3540b1a | 80 | DBG("Could not connect.\n"); |
yueee_yt | 0:fd83f3540b1a | 81 | wait(4); |
yueee_yt | 0:fd83f3540b1a | 82 | } |
yueee_yt | 0:fd83f3540b1a | 83 | if(pppErr) |
yueee_yt | 0:fd83f3540b1a | 84 | { |
yueee_yt | 0:fd83f3540b1a | 85 | setOff(); |
yueee_yt | 0:fd83f3540b1a | 86 | return pppErr; |
yueee_yt | 0:fd83f3540b1a | 87 | } |
yueee_yt | 0:fd83f3540b1a | 88 | |
yueee_yt | 0:fd83f3540b1a | 89 | DBG("Connected.\n"); |
yueee_yt | 0:fd83f3540b1a | 90 | |
yueee_yt | 0:fd83f3540b1a | 91 | return PPP_OK; |
yueee_yt | 0:fd83f3540b1a | 92 | } |
yueee_yt | 0:fd83f3540b1a | 93 | |
yueee_yt | 0:fd83f3540b1a | 94 | PPPErr GPRSModuleNetIf::disconnect() |
yueee_yt | 0:fd83f3540b1a | 95 | { |
yueee_yt | 0:fd83f3540b1a | 96 | DBG("Disconnecting...\n"); |
yueee_yt | 0:fd83f3540b1a | 97 | PPPErr pppErr = PPPNetIf::disconnect(); |
yueee_yt | 0:fd83f3540b1a | 98 | if(pppErr) |
yueee_yt | 0:fd83f3540b1a | 99 | return pppErr; |
yueee_yt | 0:fd83f3540b1a | 100 | |
yueee_yt | 0:fd83f3540b1a | 101 | m_pIf->close(); |
yueee_yt | 0:fd83f3540b1a | 102 | |
yueee_yt | 0:fd83f3540b1a | 103 | DBG("Powering off module.\n") |
yueee_yt | 0:fd83f3540b1a | 104 | setOff(); //Power off module |
yueee_yt | 0:fd83f3540b1a | 105 | |
yueee_yt | 0:fd83f3540b1a | 106 | DBG("Off.\n") |
yueee_yt | 0:fd83f3540b1a | 107 | |
yueee_yt | 0:fd83f3540b1a | 108 | return PPP_OK; |
yueee_yt | 0:fd83f3540b1a | 109 | } |
yueee_yt | 0:fd83f3540b1a | 110 | |
yueee_yt | 0:fd83f3540b1a | 111 | |
yueee_yt | 0:fd83f3540b1a | 112 | #endif |