how do I connect a socket to a modem connection? - c++

All,
I am working on an embedded linux application that is to use a cellular connection to communicate with a database.
I know that I can use AT commands to create socket to the server, but how do I get access to the socket from a C++ program? That is, after I issue the AT commands, how do I use it from an application?
Sorry if this is a stupid question, but I can't find an answer...
Thanks for all your help!
:bp:

Check the documentation for your modem. Multitech has one online here for their modems which may or may not be helpful (if yours is compatible).
Generally, after doing the WIPCREATE, you need to wait for a WIPREADY or WIPACCEPT from the modem; you can then do WIPDATA to put your connection to the modem into data mode, at which point everything you write will be sent to the socket, and anything received on the socket will be sent back to you (so you can treat the fd connected to the modem as if it was a socket, you just need to be careful not to send escape sequences accidentally -- or at least escape the escape sequences properly).

That depends on your OS. On most normal Unix OS's you can simply use /dev/tty*, open it, set the baudrate etc and issue AT commands.

Related

How can I ignore a TCP PSH bit in Windows 7?

I have a C++ program that uses Boost ASIO to communicate with a network device over a TCP socket. The program is working fine on Linux, but with Windows 7 I'm finding that the communication is not working very well. After some experimentation, I found that there's a 0.5-second delay between command and response when communicating with the device using the ASIO example telnet program, even though the response shows up in Wireshark much more quickly.
I gather that the problem is that the network device is not setting the PSH flag after it completes a chunk of data. See: http://smallvoid.com/article/winnt-tcp-push-flag.html.
I need to somehow set up my app so that it receives data from the TCP socket regardless of whether a packet has arrived with the PSH bit set. I know this must be possible because PuTTY can communicate with my device normally. I'd rather not use a registry key to get the effect, because I want to change the behavior only for this one socket, not the entire system.
What do I need to do to get Windows to ignore the PSH flag for this connection?
You could try specifying the MSG_PUSH_IMMEDIATE flag on the receiving side (https://msdn.microsoft.com/en-us/library/windows/desktop/ms741688(v=vs.85).aspx).

Create virtual serial port in Qt/C++

I would like to create a linux app which appears as a serial port (eg /dev/ttyTEST). This app will listen for commands sent to the port, and respond back.
Is this possible using Qt/C++ ? I haven't done kernel programming so I'm hoping this is possible in user space.
Everything depends on what the application using such device expects.
If /dev/ttyTEST is to behave like a real serial device and respond properly to all ioctl's that set its speed etc., then this can't be done from userspace. It wouldn't be too hard to implement in the kernel space, though.
If /dev/ttyTEST only needs to be a tty, then provide a pseudo tty.
If /dev/ttyTEST is merely to be something another application can write to and read from then socketpair() does it.
If you have control over the application's code, then you can have it check whether the device is a socket pair or a real character device, and ignore the failures of the serial-port-specific APIs on a socket.

How to retrieve a data from a directory by sending a request through the serial port?

I have a controller which has a serial port and ethernet. I want to retrieve an event and the data associated with this event from the event directory through the serial or ethernet port of the controller. I do have a packet format (request packet data) for the specified event to be retrieved. Can anyone tell me how to retrieve the data by sending a request through the serial port? I am beginner and not that much well-versed in programming.
You will need to have some kind of a program running on your embedded platform, listening to the serial port and answering requests. This kind of program is usually called a "daemon" (pronounced the same as "demon"; just like "Caesar" rhymes with "sea star").
If you already have a daemon, you will need to figure out what format it uses. Since I have no idea what you might have I cannot even guess.
If you will be writing your own daemon, you will need to choose some sort of protocol. Personally I like the JSON format for a serial protocol; it is simple enough that you can extract data just using sscanf() from the C library if there is not a better library available, and of course it's easy to build JSON strings just using sprintf().
http://json.org/
What you want is the Serial Programming Guide for POSIX Operating Systems. If you are bound to Windows for some reason, you get POSIX through installing Cygwin. Expect to become familiar with man pages like termios and fcntl since you'll first have to set the serial port parameters to work with your device, though they're likely to be the standard 8-N-1 at some rate. Then it's a matter of reading and writing bytes to the port's file descriptor. You're more likely to be using the low level open(), close(), read(), and write(), which are a level below the stdio (printf, fopen, stdout) you're more likely to be used to as a newer programmer.
Computers these days often lack the RS232 serial port, so if you need one you can find a cheap USB adapter. Be aware that USB adapters don't necessarily implement some of the ancillary signals (RTS,CTS,etc.) in my experience.
Also look into libraries for your specific needs and situation.
You should specify the controller for more useful answers.
Your controller should support any data exchange protocol. You can find this info in documenttion. May be, it supports MODBUS or MODBUS TCP. In this case you can use any modbus compatible software.

TCP Connection Hijacking

I have a small project that I've been working on in C++, and due to the nature of what it does, I need to insert packets in to a live TCP stream. (The purpose is innocent enough, http://ee.forumify.com/viewtopic.php?id=3299 if you MUST know)
I'm creating a level editor for a game, and due to the nature of the handshakes, I can't simply establish a new connection with a high level library such as WinSock. Until now, it has relied on Winsock Packet Editor to do the dirty work, but if I were to let the application handle it all, it would make everyone happy.
So my question is this: Is there an API somewhere that will allow me to take control of a live TCP stream, and preferably one that keeps it valid after it finishes? And I would prefer to not have to inject any DLLs. Also, Detours is a no-no as I'm using GCC/Mingw.
I've toyed around with WinPCap and I have some working code (I can collect a packet, and from that generate a proper packet to send) but since it operates at such a low level, I cannot anticipate all of the potential protocols that the end user might use. Yes, chances are that they'll be using IPv4 over Ethernet, but what about those people who still use PPP, or some other obscure protocol? Also, the connection gets dropped by the client application after mine is done with it, as the latest ID values in the packets have changed and the client assumes that it has disconnected.
So, if anyone could provide a high-level TCP stream manipulator, I would be very happy. If not, I'll just continue tinkering with WinPCap and tell all the dial-up users to go get better internet.
Target platform: Microsoft Windows XP through Windows 7
Create a separate process to bind to a local port. When the initial tcp stream is created, proxy it through that process, which can then forward it on to the network. When you need to 'inject' into the stream you can have this proxy process do it. Just a thought.
you should look at the source code of ettercap http://ettercap.sourceforge.net/
or hunt, tcp hijacker http://packetstormsecurity.org/files/view/21967/hunt-1.5.tgz
Those 2 softs do what you're after.
I don't think there's any sensible API that will allow you to hijack a TCP stream. Such a thing would, inherently, be a security problem.
Can you insert your program as a proxy for the connection in question? That is, get the program that opens the connection to open it to your program, then have your program open the connection to the real target.
The idea is that if all the packets pass through your program anyway, then modifying the TCP stream becomes relatively trivial.

regarding writing with TCP/IP in symbian

void CSocket::WriteSocket()
{
TBuf8<2> KData (_L8("60"));
//RBuf8 KData;
RBuf8 aQuery;
aQuery.CleanupClosePushL();
aQuery.CreateL(100);
// <?xml version="1.0"?><AM><P/><A><CE/></A></AM>
_LIT8(KData1,"61");
//_LIT8(KData2,"20");
_LIT8(KData3,"A");
_LIT8(KData4,"<?xml version=\"1.0\"?><AM><P/><A><CE/></A></AM>");
TBuf8<100> buff,buff1;
buff.Copy(_L("<?xml version=\"1.0\"?><AM><P/><A><CE/></A></AM>"));
TInt len=buff.Length();
buff1.AppendNum(len);
aQuery.Append(KData1);
aQuery.Append(buff1);
// aQuery.Append(KData2);
aQuery.Append(KData3);
aQuery.Append(buff);
//iSocket.Send(KData,KExpeditedDataOpt,iStatus);
iSocket.Write((TDesC8)aQuery,iStatus);
User::WaitForRequest(iStatus);
}
I am using this code on Symbian for communication with server which is in Java.
But the issue is: the data is not reaching the server. It is showing device has succesfully connected. What i am doing wrong in this code? Is TDes8 compatible with plain text in Java?
TDes8 is just bytes, which is fine if the Java end is trying to read ASCII, not so fine if the Java end is expecting 16bit unicode (which is what Java's char is). But if you're reading the socket at the other end, then even if it wasn't compatible you'd still see some data, just not what you were expecting. And the protocol by which you're communicating with the server should specify the charset, regardless of what language the server is implemented in.
Otherwise:
Does it work on the emulator?
Have you checked iStatus for errors? Normally if a socket connects you can write to it, but you never know.
Is the server reporting that the socket is connected? It's possible you've connected to the wrong host or the wrong port.
If the server did read some data, but then failed for some reason, would you know? I guess I'm asking whether you're debugging the other end too. If not then it's possible your data isn't in the right format and is being ignored at the other end. I think you're sending 6146A<?xml version="1.0"?><AM><P/><A><CE/></A></AM>. Some tracing and/or packet sniffing will tell you whether that's true.
A variable starting with "a" is usually a parameter: is this your real code? If not, then the thing about the malformed data applies, and your caller might be giving you the wrong thing.
You might want to PopAndDestroy aQuery before return, although that doesn't affect this issue.
Your function should be called WriteSocketL, since it can leave.
cross-posting reply from duplicated question:
Your mobile network operator could be blocking any non-HTTP traffic.
Your server could need to receive more data before returning it all.
I'm also particularly concerned about your use of java character/string considering I would expect a low-level java socket on the server to put incoming network data in a byte[], not a String. If your server is using something like a call to a readLine() method, you may need to add a carriage return character in the data your client sends.