A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Diff: cellular/Cellular.cpp
- Revision:
- 57:88b07490d7e8
- Parent:
- 56:e5e5351f14b3
- Parent:
- 55:56d9a9d98079
- Child:
- 59:7777b3ed5a4c
--- a/cellular/Cellular.cpp Fri Dec 20 20:26:46 2013 +0000 +++ b/cellular/Cellular.cpp Fri Dec 20 20:29:10 2013 +0000 @@ -136,7 +136,16 @@ } bool Cellular::bind(unsigned int port) { - return false; + if(socketOpened) { + printf("[ERROR] socket is open. Can not set local port\r\n"); + return false; + } + if(port > 65535) { + printf("[ERROR] port out of range (0-65535)\r\n"); + return false; + } + local_port = port; + return true; } bool Cellular::open(const std::string& address, unsigned int port, Mode mode) { @@ -160,7 +169,14 @@ return true; } - //2) Check PPP connection + //2) Check Parameters + if(port > 65535) { + printf("[ERROR] port out of range (0-65535)\r\n"); + return false; + } + + + //3) Check PPP connection if(!isConnected()) { printf("[ERROR] PPP not established. Attempting to connect\r\n"); if(!connect()) { @@ -171,10 +187,20 @@ } } - ////Setup IP Connection + //Set Local Port + if(local_port != 0) { + //Attempt to set local port + sprintf(buffer, "AT#OUTPORT=%d", local_port); + Code code = sendBasicCommand(buffer, 1000); + if(code != CELL_OK) { + printf("[WARNING] Unable to set local port (%d) [%d]\r\n", local_port, (int) code); + } + } + + //Set TCP/UDP parameters if(mode == TCP) { if(socketCloseable) { - Code code = sendBasicCommand("AT#DLEMODE=1,0", 1000); + Code code = sendBasicCommand("AT#DLEMODE=1,1", 1000); if(code != CELL_OK) { printf("[WARNING] Unable to set socket closeable [%d]\r\n", (int) code); } @@ -189,9 +215,9 @@ printf("[WARNING] Unable to set socket closeable [%d]\r\n", (int) code); } } - sprintf(buffer, "AT#UDPPORT=1,%d", port); + sprintf(buffer, "AT#UDPPORT=%d", port); portCode = sendBasicCommand(buffer, 1000); - addressCode = sendBasicCommand("AT#UDPSERV=1,\"" + address + "\"", 1000); + addressCode = sendBasicCommand("AT#UDPSERV=\"" + address + "\"", 1000); } if(portCode == CELL_OK) { @@ -206,13 +232,24 @@ printf("[ERROR] Host address could not be set\r\n"); } + + // Try and Connect - string response = sendCommand("AT#OTCP=1", 30000); + std::string sMode; + std::string sOpenSocketCmd; + if(mode == TCP) { + sOpenSocketCmd = "AT#OTCP=1"; + sMode = "TCP"; + } else { + sOpenSocketCmd = "AT#OUDP"; + sMode = "UDP"; + } + string response = sendCommand(sOpenSocketCmd, 30000); if (response.find("Ok_Info_WaitingForData") != string::npos) { - printf("[INFO] Opened TCP Socket [%s:%d]\r\n", address.c_str(), port); + printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port); socketOpened = true; } else { - printf("[WARNING] Unable to open TCP Socket [%s:%d]\r\n", address.c_str(), port); + printf("[WARNING] Unable to open %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port); socketOpened = false; }