SMS Scheduler that will automatically send and receive text messages using the Enfora 1308 GSM Modem. Please note that it uses a modified NetServices library and to set the baud rate for the GSM Modem to 19.2K.
NetServices/core/net.cpp@0:d9266031f832, 2011-10-13 (annotated)
- Committer:
- mafischl
- Date:
- Thu Oct 13 17:02:29 2011 +0000
- Revision:
- 0:d9266031f832
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mafischl | 0:d9266031f832 | 1 | |
mafischl | 0:d9266031f832 | 2 | /* |
mafischl | 0:d9266031f832 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
mafischl | 0:d9266031f832 | 4 | |
mafischl | 0:d9266031f832 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mafischl | 0:d9266031f832 | 6 | of this software and associated documentation files (the "Software"), to deal |
mafischl | 0:d9266031f832 | 7 | in the Software without restriction, including without limitation the rights |
mafischl | 0:d9266031f832 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mafischl | 0:d9266031f832 | 9 | copies of the Software, and to permit persons to whom the Software is |
mafischl | 0:d9266031f832 | 10 | furnished to do so, subject to the following conditions: |
mafischl | 0:d9266031f832 | 11 | |
mafischl | 0:d9266031f832 | 12 | The above copyright notice and this permission notice shall be included in |
mafischl | 0:d9266031f832 | 13 | all copies or substantial portions of the Software. |
mafischl | 0:d9266031f832 | 14 | |
mafischl | 0:d9266031f832 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mafischl | 0:d9266031f832 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mafischl | 0:d9266031f832 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mafischl | 0:d9266031f832 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mafischl | 0:d9266031f832 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mafischl | 0:d9266031f832 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mafischl | 0:d9266031f832 | 21 | THE SOFTWARE. |
mafischl | 0:d9266031f832 | 22 | */ |
mafischl | 0:d9266031f832 | 23 | |
mafischl | 0:d9266031f832 | 24 | #include "net.h" |
mafischl | 0:d9266031f832 | 25 | |
mafischl | 0:d9266031f832 | 26 | #include "host.h" |
mafischl | 0:d9266031f832 | 27 | #include "ipaddr.h" |
mafischl | 0:d9266031f832 | 28 | #include "netservice.h" |
mafischl | 0:d9266031f832 | 29 | #include "if/net/netif.h" |
mafischl | 0:d9266031f832 | 30 | #include "if/net/nettcpsocket.h" |
mafischl | 0:d9266031f832 | 31 | #include "if/net/netudpsocket.h" |
mafischl | 0:d9266031f832 | 32 | #include "if/net/netdnsrequest.h" |
mafischl | 0:d9266031f832 | 33 | |
mafischl | 0:d9266031f832 | 34 | //#define __DEBUG |
mafischl | 0:d9266031f832 | 35 | #include "dbg/dbg.h" |
mafischl | 0:d9266031f832 | 36 | |
mafischl | 0:d9266031f832 | 37 | Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket() |
mafischl | 0:d9266031f832 | 38 | { |
mafischl | 0:d9266031f832 | 39 | |
mafischl | 0:d9266031f832 | 40 | } |
mafischl | 0:d9266031f832 | 41 | |
mafischl | 0:d9266031f832 | 42 | Net::~Net() |
mafischl | 0:d9266031f832 | 43 | { |
mafischl | 0:d9266031f832 | 44 | |
mafischl | 0:d9266031f832 | 45 | } |
mafischl | 0:d9266031f832 | 46 | |
mafischl | 0:d9266031f832 | 47 | |
mafischl | 0:d9266031f832 | 48 | void Net::poll() |
mafischl | 0:d9266031f832 | 49 | { |
mafischl | 0:d9266031f832 | 50 | //DBG("\r\nNet : Services polling\r\n"); |
mafischl | 0:d9266031f832 | 51 | |
mafischl | 0:d9266031f832 | 52 | //Poll Services |
mafischl | 0:d9266031f832 | 53 | NetService::servicesPoll(); |
mafischl | 0:d9266031f832 | 54 | |
mafischl | 0:d9266031f832 | 55 | //DBG("\r\nNet : Interfaces polling\r\n"); |
mafischl | 0:d9266031f832 | 56 | |
mafischl | 0:d9266031f832 | 57 | //Poll Interfaces |
mafischl | 0:d9266031f832 | 58 | list<NetIf*>::iterator pIfIt; |
mafischl | 0:d9266031f832 | 59 | |
mafischl | 0:d9266031f832 | 60 | for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ ) |
mafischl | 0:d9266031f832 | 61 | { |
mafischl | 0:d9266031f832 | 62 | (*pIfIt)->poll(); |
mafischl | 0:d9266031f832 | 63 | } |
mafischl | 0:d9266031f832 | 64 | |
mafischl | 0:d9266031f832 | 65 | //DBG("\r\nNet : Sockets polling\r\n"); |
mafischl | 0:d9266031f832 | 66 | |
mafischl | 0:d9266031f832 | 67 | //Poll Tcp Sockets |
mafischl | 0:d9266031f832 | 68 | list<NetTcpSocket*>::iterator pNetTcpSocketIt; |
mafischl | 0:d9266031f832 | 69 | |
mafischl | 0:d9266031f832 | 70 | for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); ) |
mafischl | 0:d9266031f832 | 71 | { |
mafischl | 0:d9266031f832 | 72 | (*pNetTcpSocketIt)->poll(); |
mafischl | 0:d9266031f832 | 73 | |
mafischl | 0:d9266031f832 | 74 | if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) ) |
mafischl | 0:d9266031f832 | 75 | { |
mafischl | 0:d9266031f832 | 76 | (*pNetTcpSocketIt)->m_removed = true; |
mafischl | 0:d9266031f832 | 77 | delete (*pNetTcpSocketIt); |
mafischl | 0:d9266031f832 | 78 | (*pNetTcpSocketIt) = NULL; |
mafischl | 0:d9266031f832 | 79 | pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt); |
mafischl | 0:d9266031f832 | 80 | } |
mafischl | 0:d9266031f832 | 81 | else |
mafischl | 0:d9266031f832 | 82 | { |
mafischl | 0:d9266031f832 | 83 | pNetTcpSocketIt++; |
mafischl | 0:d9266031f832 | 84 | } |
mafischl | 0:d9266031f832 | 85 | } |
mafischl | 0:d9266031f832 | 86 | |
mafischl | 0:d9266031f832 | 87 | //Poll Udp Sockets |
mafischl | 0:d9266031f832 | 88 | list<NetUdpSocket*>::iterator pNetUdpSocketIt; |
mafischl | 0:d9266031f832 | 89 | |
mafischl | 0:d9266031f832 | 90 | for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); ) |
mafischl | 0:d9266031f832 | 91 | { |
mafischl | 0:d9266031f832 | 92 | (*pNetUdpSocketIt)->poll(); |
mafischl | 0:d9266031f832 | 93 | |
mafischl | 0:d9266031f832 | 94 | if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) ) |
mafischl | 0:d9266031f832 | 95 | { |
mafischl | 0:d9266031f832 | 96 | (*pNetUdpSocketIt)->m_removed = true; |
mafischl | 0:d9266031f832 | 97 | delete (*pNetUdpSocketIt); |
mafischl | 0:d9266031f832 | 98 | (*pNetUdpSocketIt) = NULL; |
mafischl | 0:d9266031f832 | 99 | pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt); |
mafischl | 0:d9266031f832 | 100 | } |
mafischl | 0:d9266031f832 | 101 | else |
mafischl | 0:d9266031f832 | 102 | { |
mafischl | 0:d9266031f832 | 103 | pNetUdpSocketIt++; |
mafischl | 0:d9266031f832 | 104 | } |
mafischl | 0:d9266031f832 | 105 | } |
mafischl | 0:d9266031f832 | 106 | |
mafischl | 0:d9266031f832 | 107 | |
mafischl | 0:d9266031f832 | 108 | } |
mafischl | 0:d9266031f832 | 109 | |
mafischl | 0:d9266031f832 | 110 | NetTcpSocket* Net::tcpSocket(NetIf& netif) { |
mafischl | 0:d9266031f832 | 111 | NetTcpSocket* pNetTcpSocket = netif.tcpSocket(); |
mafischl | 0:d9266031f832 | 112 | pNetTcpSocket->m_refs++; |
mafischl | 0:d9266031f832 | 113 | return pNetTcpSocket; |
mafischl | 0:d9266031f832 | 114 | } |
mafischl | 0:d9266031f832 | 115 | |
mafischl | 0:d9266031f832 | 116 | NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if |
mafischl | 0:d9266031f832 | 117 | if ( net().m_defaultIf == NULL ) |
mafischl | 0:d9266031f832 | 118 | return NULL; |
mafischl | 0:d9266031f832 | 119 | NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket(); |
mafischl | 0:d9266031f832 | 120 | pNetTcpSocket->m_refs++; |
mafischl | 0:d9266031f832 | 121 | return pNetTcpSocket; |
mafischl | 0:d9266031f832 | 122 | } |
mafischl | 0:d9266031f832 | 123 | |
mafischl | 0:d9266031f832 | 124 | void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket) |
mafischl | 0:d9266031f832 | 125 | { |
mafischl | 0:d9266031f832 | 126 | pNetTcpSocket->m_refs--; |
mafischl | 0:d9266031f832 | 127 | if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs) |
mafischl | 0:d9266031f832 | 128 | pNetTcpSocket->close(); |
mafischl | 0:d9266031f832 | 129 | } |
mafischl | 0:d9266031f832 | 130 | |
mafischl | 0:d9266031f832 | 131 | NetUdpSocket* Net::udpSocket(NetIf& netif) { |
mafischl | 0:d9266031f832 | 132 | NetUdpSocket* pNetUdpSocket = netif.udpSocket(); |
mafischl | 0:d9266031f832 | 133 | pNetUdpSocket->m_refs++; |
mafischl | 0:d9266031f832 | 134 | return pNetUdpSocket; |
mafischl | 0:d9266031f832 | 135 | } |
mafischl | 0:d9266031f832 | 136 | |
mafischl | 0:d9266031f832 | 137 | NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if |
mafischl | 0:d9266031f832 | 138 | if ( net().m_defaultIf == NULL ) |
mafischl | 0:d9266031f832 | 139 | return NULL; |
mafischl | 0:d9266031f832 | 140 | NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket(); |
mafischl | 0:d9266031f832 | 141 | pNetUdpSocket->m_refs++; |
mafischl | 0:d9266031f832 | 142 | return pNetUdpSocket; |
mafischl | 0:d9266031f832 | 143 | } |
mafischl | 0:d9266031f832 | 144 | |
mafischl | 0:d9266031f832 | 145 | void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket) |
mafischl | 0:d9266031f832 | 146 | { |
mafischl | 0:d9266031f832 | 147 | pNetUdpSocket->m_refs--; |
mafischl | 0:d9266031f832 | 148 | if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs) |
mafischl | 0:d9266031f832 | 149 | pNetUdpSocket->close(); |
mafischl | 0:d9266031f832 | 150 | } |
mafischl | 0:d9266031f832 | 151 | |
mafischl | 0:d9266031f832 | 152 | NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) { |
mafischl | 0:d9266031f832 | 153 | return netif.dnsRequest(hostname); |
mafischl | 0:d9266031f832 | 154 | } |
mafischl | 0:d9266031f832 | 155 | |
mafischl | 0:d9266031f832 | 156 | NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if |
mafischl | 0:d9266031f832 | 157 | if ( net().m_defaultIf == NULL ) |
mafischl | 0:d9266031f832 | 158 | return NULL; |
mafischl | 0:d9266031f832 | 159 | return net().m_defaultIf->dnsRequest(hostname); |
mafischl | 0:d9266031f832 | 160 | } |
mafischl | 0:d9266031f832 | 161 | |
mafischl | 0:d9266031f832 | 162 | NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif) |
mafischl | 0:d9266031f832 | 163 | { |
mafischl | 0:d9266031f832 | 164 | return netif.dnsRequest(pHost); |
mafischl | 0:d9266031f832 | 165 | } |
mafischl | 0:d9266031f832 | 166 | |
mafischl | 0:d9266031f832 | 167 | NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if |
mafischl | 0:d9266031f832 | 168 | { |
mafischl | 0:d9266031f832 | 169 | if ( net().m_defaultIf == NULL ) |
mafischl | 0:d9266031f832 | 170 | return NULL; |
mafischl | 0:d9266031f832 | 171 | return net().m_defaultIf->dnsRequest(pHost); |
mafischl | 0:d9266031f832 | 172 | } |
mafischl | 0:d9266031f832 | 173 | |
mafischl | 0:d9266031f832 | 174 | void Net::setDefaultIf(NetIf& netif) { |
mafischl | 0:d9266031f832 | 175 | net().m_defaultIf = &netif; |
mafischl | 0:d9266031f832 | 176 | } |
mafischl | 0:d9266031f832 | 177 | |
mafischl | 0:d9266031f832 | 178 | void Net::setDefaultIf(NetIf* pIf) |
mafischl | 0:d9266031f832 | 179 | { |
mafischl | 0:d9266031f832 | 180 | net().m_defaultIf = pIf; |
mafischl | 0:d9266031f832 | 181 | } |
mafischl | 0:d9266031f832 | 182 | |
mafischl | 0:d9266031f832 | 183 | NetIf* Net::getDefaultIf() { |
mafischl | 0:d9266031f832 | 184 | return net().m_defaultIf; |
mafischl | 0:d9266031f832 | 185 | } |
mafischl | 0:d9266031f832 | 186 | |
mafischl | 0:d9266031f832 | 187 | void Net::registerIf(NetIf* pIf) |
mafischl | 0:d9266031f832 | 188 | { |
mafischl | 0:d9266031f832 | 189 | net().m_lpIf.push_back(pIf); |
mafischl | 0:d9266031f832 | 190 | } |
mafischl | 0:d9266031f832 | 191 | |
mafischl | 0:d9266031f832 | 192 | void Net::unregisterIf(NetIf* pIf) |
mafischl | 0:d9266031f832 | 193 | { |
mafischl | 0:d9266031f832 | 194 | if( net().m_defaultIf == pIf ) |
mafischl | 0:d9266031f832 | 195 | net().m_defaultIf = NULL; |
mafischl | 0:d9266031f832 | 196 | net().m_lpIf.remove(pIf); |
mafischl | 0:d9266031f832 | 197 | } |
mafischl | 0:d9266031f832 | 198 | |
mafischl | 0:d9266031f832 | 199 | void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket) |
mafischl | 0:d9266031f832 | 200 | { |
mafischl | 0:d9266031f832 | 201 | net().m_lpNetTcpSocket.push_back(pNetTcpSocket); |
mafischl | 0:d9266031f832 | 202 | DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size()); |
mafischl | 0:d9266031f832 | 203 | } |
mafischl | 0:d9266031f832 | 204 | |
mafischl | 0:d9266031f832 | 205 | void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket) |
mafischl | 0:d9266031f832 | 206 | { |
mafischl | 0:d9266031f832 | 207 | DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1); |
mafischl | 0:d9266031f832 | 208 | if(!pNetTcpSocket->m_removed) |
mafischl | 0:d9266031f832 | 209 | net().m_lpNetTcpSocket.remove(pNetTcpSocket); |
mafischl | 0:d9266031f832 | 210 | } |
mafischl | 0:d9266031f832 | 211 | |
mafischl | 0:d9266031f832 | 212 | void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket) |
mafischl | 0:d9266031f832 | 213 | { |
mafischl | 0:d9266031f832 | 214 | net().m_lpNetUdpSocket.push_back(pNetUdpSocket); |
mafischl | 0:d9266031f832 | 215 | DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size()); |
mafischl | 0:d9266031f832 | 216 | } |
mafischl | 0:d9266031f832 | 217 | |
mafischl | 0:d9266031f832 | 218 | void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket) |
mafischl | 0:d9266031f832 | 219 | { |
mafischl | 0:d9266031f832 | 220 | DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1); |
mafischl | 0:d9266031f832 | 221 | if(!pNetUdpSocket->m_removed) |
mafischl | 0:d9266031f832 | 222 | net().m_lpNetUdpSocket.remove(pNetUdpSocket); |
mafischl | 0:d9266031f832 | 223 | } |
mafischl | 0:d9266031f832 | 224 | |
mafischl | 0:d9266031f832 | 225 | Net& Net::net() |
mafischl | 0:d9266031f832 | 226 | { |
mafischl | 0:d9266031f832 | 227 | static Net* pInst = new Net(); //Called only once |
mafischl | 0:d9266031f832 | 228 | return *pInst; |
mafischl | 0:d9266031f832 | 229 | } |