[136] | 1 | /*
|
---|
| 2 | This file is part of the GSM3 communications library for Arduino
|
---|
| 3 | -- Multi-transport communications platform
|
---|
| 4 | -- Fully asynchronous
|
---|
| 5 | -- Includes code for the Arduino-Telefonica GSM/GPRS Shield V1
|
---|
| 6 | -- Voice calls
|
---|
| 7 | -- SMS
|
---|
| 8 | -- TCP/IP connections
|
---|
| 9 | -- HTTP basic clients
|
---|
| 10 |
|
---|
| 11 | This library has been developed by Telefónica Digital - PDI -
|
---|
| 12 | - Physical Internet Lab, as part as its collaboration with
|
---|
| 13 | Arduino and the Open Hardware Community.
|
---|
| 14 |
|
---|
| 15 | September-December 2012
|
---|
| 16 |
|
---|
| 17 | This library is free software; you can redistribute it and/or
|
---|
| 18 | modify it under the terms of the GNU Lesser General Public
|
---|
| 19 | License as published by the Free Software Foundation; either
|
---|
| 20 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 21 |
|
---|
| 22 | This library is distributed in the hope that it will be useful,
|
---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | Lesser General Public License for more details.
|
---|
| 26 |
|
---|
| 27 | You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | License along with this library; if not, write to the Free Software
|
---|
| 29 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 30 |
|
---|
| 31 | The latest version of this library can always be found at
|
---|
| 32 | https://github.com/BlueVia/Official-Arduino
|
---|
| 33 | */
|
---|
| 34 | #include <GSM3MobileServerService.h>
|
---|
| 35 | #include <GSM3MobileServerProvider.h>
|
---|
| 36 | #include <GSM3MobileClientProvider.h>
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | #define __TOUTSERVER__ 10000
|
---|
| 40 | #define BUFFERSIZETWEET 100
|
---|
| 41 |
|
---|
| 42 | #define GSM3MOBILESERVERSERVICE_SYNCH 0x01 // 1: TRUE, compatible with other clients 0: FALSE
|
---|
| 43 |
|
---|
| 44 | // While there is only a shield (ShieldV1) we will include it by default
|
---|
| 45 | #include <GSM3ShieldV1ServerProvider.h>
|
---|
| 46 | GSM3ShieldV1ServerProvider theShieldV1ServerProvider;
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | GSM3MobileServerService::GSM3MobileServerService(uint8_t port, bool synch)
|
---|
| 50 | {
|
---|
| 51 | mySocket=0;
|
---|
| 52 | _port=port;
|
---|
| 53 | flags = 0;
|
---|
| 54 |
|
---|
| 55 | // If synchronous
|
---|
| 56 | if(synch)
|
---|
| 57 | flags |= GSM3MOBILESERVERSERVICE_SYNCH;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | // Returns 0 if last command is still executing
|
---|
| 61 | // 1 if success
|
---|
| 62 | // >1 if error
|
---|
| 63 | int GSM3MobileServerService::ready()
|
---|
| 64 | {
|
---|
| 65 | return theGSM3MobileServerProvider->ready();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void GSM3MobileServerService::begin()
|
---|
| 69 | {
|
---|
| 70 | if(theGSM3MobileServerProvider==0)
|
---|
| 71 | return;
|
---|
| 72 | theGSM3MobileServerProvider->connectTCPServer(_port);
|
---|
| 73 |
|
---|
| 74 | if(flags & GSM3MOBILESERVERSERVICE_SYNCH)
|
---|
| 75 | waitForAnswer();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | GSM3MobileClientService GSM3MobileServerService::available(bool synch)
|
---|
| 79 | {
|
---|
| 80 | int newSocket;
|
---|
| 81 | // In case we are debugging, we'll need to force a look at the buffer
|
---|
| 82 | ready();
|
---|
| 83 |
|
---|
| 84 | newSocket=theGSM3MobileServerProvider->getNewOccupiedSocketAsServer();
|
---|
| 85 |
|
---|
| 86 | // Instatiate new client. If we are synch, the client is synchronous/blocking
|
---|
| 87 | GSM3MobileClientService client((uint8_t)(newSocket), (flags & GSM3MOBILESERVERSERVICE_SYNCH));
|
---|
| 88 |
|
---|
| 89 | return client;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | size_t GSM3MobileServerService::write(uint8_t c)
|
---|
| 93 | {
|
---|
| 94 | // Adapt to the new, lean implementation
|
---|
| 95 | // theGSM3MobileServerProvider->writeSocket(c);
|
---|
| 96 | return 1;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | void GSM3MobileServerService::beginWrite()
|
---|
| 100 | {
|
---|
| 101 | // Adapt to the new, lean implementation
|
---|
| 102 | // theGSM3MobileServerProvider->beginWriteSocket(local1Remote0, mySocket);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | size_t GSM3MobileServerService::write(const uint8_t* buf)
|
---|
| 106 | {
|
---|
| 107 | // Adapt to the new, lean implementation
|
---|
| 108 | // theGSM3MobileServerProvider->writeSocket((const char*)(buf));
|
---|
| 109 | return strlen((const char*)buf);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | size_t GSM3MobileServerService::write(const uint8_t* buf, size_t sz)
|
---|
| 113 | {
|
---|
| 114 | // Adapt to the new, lean implementation
|
---|
| 115 | // theGSM3MobileServerProvider->writeSocket((const char*)(buf));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void GSM3MobileServerService::endWrite()
|
---|
| 119 | {
|
---|
| 120 | // Adapt to the new, lean implementation
|
---|
| 121 | // theGSM3MobileServerProvider->endWriteSocket();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | void GSM3MobileServerService::stop()
|
---|
| 125 | {
|
---|
| 126 |
|
---|
| 127 | // Review, should be the server?
|
---|
| 128 | theGSM3MobileClientProvider->disconnectTCP(local1Remote0, mySocket);
|
---|
| 129 | if(flags & GSM3MOBILESERVERSERVICE_SYNCH)
|
---|
| 130 | waitForAnswer();
|
---|
| 131 | theGSM3MobileClientProvider->releaseSocket(mySocket);
|
---|
| 132 | mySocket = -1;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /*int GSM3MobileServerService::getIP(char* LocalIP, int LocalIPlength)
|
---|
| 137 | {
|
---|
| 138 | return theGSM3MobileServerProvider->getIP(LocalIP, LocalIPlength);
|
---|
| 139 | }*/
|
---|
| 140 |
|
---|
| 141 | int GSM3MobileServerService::waitForAnswer()
|
---|
| 142 | {
|
---|
| 143 | unsigned long m;
|
---|
| 144 | m=millis();
|
---|
| 145 | int res;
|
---|
| 146 |
|
---|
| 147 | while(((millis()-m)< __TOUTSERVER__ )&&(ready()==0))
|
---|
| 148 | delay(10);
|
---|
| 149 |
|
---|
| 150 | res=ready();
|
---|
| 151 |
|
---|
| 152 | // If we get something different from a 1, we are having a problem
|
---|
| 153 | if(res!=1)
|
---|
| 154 | res=0;
|
---|
| 155 |
|
---|
| 156 | return res;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|