Interaction between CGI-Webserver and C++ code - c++

I have a c++ code on a robot infrastructure. I would like to make a web-UI that is accessible via the robots WiFi shield. I have never done this before so i have some questions.
Is there a way to have my c++ code directly interact with the cgi of the
website or do i need to use intermediary files?
Would i have to start both the cgi and the c++ code or can i start the cgi code from my c++ program?
Is there an alternative to compiling as cgi? Like maybe having the interface open directly in the c++ code?
I am sure this is a duplicate, but i didnt find anything, so any hint is appreciated!
Currently i have the c++ code send out html commands as string to the localhost,which works and i can see them in my browser. But it does not render as website, and just displays as html, probably because it is not compiled as cgi.
pushCommand("HTTP / 1.1 200 OK\r\n");
pushCommand("Content-Type: text/html\r\n");
pushCommand("Connection: close\r\n");
// the connection will be closed after completion of the response
pushCommand("Refresh: 5\r\n"); // refresh the page automatically every 5 sec
pushCommand("\r\n");
pushCommand("<!DOCTYPE HTML>\r\n");
pushCommand("<html lang=\"en\">\r\n");
pushCommand("\r\n");
pushCommand("<html><head><title>CGI demo stage one</title></head>\r\n");
pushCommand("<body><h1>CGI demo stage one</h1>\r\n");
pushCommand("This program will calculate the average of some numbers<br>\r\n");
pushCommand("<form action=cgidemo.cgi?step2 method=post>\r\n");
pushCommand(" How many numbers would you like to enter? <input type=text name=number size=10><br>\r\n");
pushCommand(" What is your favourite colour? <input type=text name=favcol size=20><br>\r\n");
pushCommand(" <input type=submit value=\" DO IT \">\r\n");
pushCommand("</form>\r\n");
pushCommand("</body></html>\r\n");
where pushCommand(cmd) is basically a fifo implementation doing the following for every entry:
//doing this only once when the client is accepted
cout << "WEBUI Accepting client...\n" << endl;
int fd = accept(m_sockfd, (struct sockaddr*)&clientaddr, &len);
//doing this for every entry of the fifo cue
cmd.copy(sendBuff, cmd.length());
sendBuff[cmd.length()] = 10;
n = send(fd, sendBuff, cmd.length() + 1, 0);

Related

Custom C++ App returns "Syntax Error" when sending specific AT Commands

I'm trying to write a C++ app, that is able to make some phone calls. For this, I ordered a USB GSM Module, and a SIM Card with a prepaid contract.
I tested the module in PuTTY and everything works as expected. I choose a COM Port and can send/receive messages. See example:
AT
OK
AT+CMEE=2
OK
But when I try sending these commands via my own application, SOME of them fail (e.g. AT+CMEE=2).
AT
OK
AT+CMEE=2
+CMEE ERROR: Syntax Error
Other commands, such as unlocking the SIM Card work fine as well. But e.g "ATD" doesn't work. Here is the code for writing to the serial port. Keep in mind this is just for testing purposes:
DWORD dummy;
// "buffer" is just an std::string
char* string = new char[buffer.length() + 3]; // "+3" because I append '\0' later when printing
strcpy(string, buffer.c_str());
string[buffer.length()] = ' ';
string[buffer.length() + 1] = '\r';
WriteFile(m_hCom,
string,
(buffer.length() + 2) * sizeof(char),
&dummy,
0
);
I tried several variations ('\0' at the end, no space in between, etc) but all of those led to no commands working at all. So this is the solution where at least some commands are working and responding.
Any more ideas what could be wrong here?
It depends on the operating mode, but sometimes you should terminate the AT command with a \r\n when working with GSM modems. See this post. It looks like you are only adding a \r

Debian program ends early (systemd-logind: Removed session)

I have a remote debian-jessie server and I sometimes run my programs on it using sftp-client WinSCP through its command line tool.
My program sends a bulk of http requests to get api responses from another site. It takes about 2-3 hours to do the whole process. It was alright earlier, but recently it started to quit unexpectedly at random times without any traces in log files in /var/log/, except strange "session removed" messages like this:
Feb 12 16:34:00 Debian-82-jessie-64-LAMP systemd-logind[570]: Removed session 11360.
This happens on the exact same second the program stops writing into its own log file about the progress and quits.
Also it seems to stop working at jsoncpp related parsing code, which looks like this:
Json::Value root;
Json::Reader reader;
bool parsedSuccess = reader.parse(data, root, false);
if (parsedSuccess)
{
for (int j = 0; j < root.size(); j++)
{
// looks like the program quits before this loop ends.
LogLine(root[j].get("user_id", "0").asString());
}
}
Can this happen if there's no "user_id" field in received json?
What walkaround can I use, if that's the case of my problem?
I also updated jsoncpp to the latest version, but it didn't help.
Basically I don't know what the cause is: removal of the session or program crash? And I don't understand why I don't see any crash messages in debian logs too :(
Please, help me out

MFRC522 PICC responded with NAK (Porting MFRC522 arduino library[C++] to [C])

First some introducing.
I am trying to make the MFRC522 library for Arduino work on an ATmega328 programmed in C(I am using a 'normal' controller first, to make it work on a raspberry pi in a later state).
I copied the .h and .cpp from the library to my own project and renamed the .c to .cpp. After removing the classes in the .h file, it was time for the .c file. I replaced all the 'byte' statements to 'uint8_t', replaced the 'Serial.print' with printf and did the changes for GPIO and SPI.
The problem.
After some small mistakes I finally got data from a keycard. However it looked like to work, I get an error with reading line 58 from the card. The error is:
MIFARE_READ() failed: A MIFARE PICC responded with NAK.
I added a print statement to the SPI write and read and found out the following difference(on the left the [C] version and on the right the Arduino version): (because of my reputation, the picture can be found in the BitBucket I mentioned at the code part)
Code
The code is pretty long, but i made it available on BitBucket
I hope someone can point me where to look(some [C++] >> [C] different interpretations), because I don't know anymore where to look.
Sander
You need to run the PCD_Authenticate function before reads and writes. There are a few pre programmed keys in the linked github library that will authenticate the cards. I was getting this when trying to write to the card because I was using KEY_B and not KEY_A. You can see this Authenticate used in the samples provided on that GitHub page. It should looks something like this.
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
From what I can tell the NAK simply means that the wrong key was used or maybe no key.

WritePrinter not asking for PIN Code

We have something like Secure Print.
I go to my control panel >> Printers and Devices >> Printer >> Printer Preference
Here i assign a 4 digit pin code ..
I fire a print from Word or Excel ... go to the printer >> retrieve jobs >> select my print job and enter my pin code.
Only then my job gets printed.
Now Our application has two source codes for printing
While firing print from our Application the print is instantaneously done .... I.e. it doesn't store my job in the queue and wait for pin ... but just prints immediately
Some source code
bResult = OpenPrinter(szDevice, &hPrinter, NULL);
// szDevice contains printer name
startDocPrtInfo.pDocName = msg;
startDocPrtInfo.pOutputFile = NULL;
startDocPrtInfo.pDatatype = "RAW";
bPrntErr = (StartDocPrinter(hPrinter,1,(LPBYTE)&startDocPrtInfo) == 0);
WritePrinter(hPrinter, &buffer[2], i, &cnt);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
~~~~~
The Code is printing the given document however its not waiting for user to enter his pin code
QUESTION
How to make the above code wait just like its doing for other applications ? is there some setting that I am missing here ?
Thanks in Anticipation
CP
I resolved this.
There are NO CODE CHANGES.
In our Printer Settings, we UNCHECKED the "Allow Escape Sequence" and it worked.
Thanks to the forum tho for entertaining the question.
Will visit often with more questions and resolutions to Questions that others raise
Good Luck
A Clueless Programmer :)

Win32 API Print not asking user input

We have something like Secure Print.
I go to my control panel >> Printers and Devices >> Printer >> Printer Preference Here i assign a 4 digit pin code ..
I fire a print from Word or Excel ... go to the printer >> retrieve jobs >> select my print job and enter my pin code.
Only then my job gets printed.
Now Our application has two source codes for printing
While firing print from our Application the print is instantaneously done .... I.e. it doesn't store my job in the queue and wait for pin ... but just prints immediately
Some source code
bResult = OpenPrinter(szDevice, &hPrinter, NULL);
// szDevice contains printer name
startDocPrtInfo.pDocName = msg;
startDocPrtInfo.pOutputFile = NULL;
startDocPrtInfo.pDatatype = "RAW";
bPrntErr = (StartDocPrinter(hPrinter,1,(LPBYTE)&startDocPrtInfo) == 0);
WritePrinter(hPrinter, &buffer[2], i, &cnt);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
The Code is printing the given document however its not waiting for user to enter his pin code
QUESTION How to make the above code wait just like its doing for other applications ? is there some setting that I am missing here ?
To give you more details .
We have another Print subroutine in the same application that works as desierd .. I.e. waits for User to enter his PIN
However the subroutine uses TextOut method instead of WritePrinter
Any idea why print is not waiting when i use the WritePrinter methods of printing?
Thanks in Anticipation CP
I resolved this.
There is NO CODE CHANGES.
In our Printer Settings, we need to UNCHECK the "Allow Escape Sequence"
Thanks to the forum tho for entertaining the question.
Will visit often with more questions and resolutions to Questions that others raise
Good Luck
A Clueless Programmer :)