10-23-2020 11:13 AM - edited 10-23-2020 12:22 PM
I am going to apologize in advance for this question. I have been trying to figure out how to even get started with respect to creating a web server in Linux.
I found this example which is great, however, it's for bare-metal application:
I found links that relate to using busybox/http but I could not find how my c application would interact with the webpage.
10-23-2020 05:32 PM
You have two basic approaches: use a stand-alone webserver (such as busybox httpd), which can then use CGI to launch a program you write (can be C code), which outputs data to the client. Alternatively, you can embed a web server within your C code, usually via a library such as libmicrohttpd (or many similar ones). Both methods have pros and cons, and both take some time to learn enough to really make a decision.
11-02-2020 11:43 AM
Still on the fence on this topic. Would running a node js server on my petalinux/Linux image and have a c application communicate through BSD sockets with the node js server along with the web browser?
11-02-2020 12:16 PM
I don't have much experience with nodejs. But I suspect it would support communicating with your C code via a socket, in much the same way as other web servers would support this too. In fact there are probably multiple ways to do this, all with their own pros/cons.
Perhaps think a bit more about the data you want to exchange between your C code and the web browser. How are you going to represent it, as binary data (not ideal for web browser consumption), as XML/JSON/other? Maybe you are serving up images? Also consider the direction of flow, and the frequency: is it purely from C code to browser, or does data also have to go the other way? Is it one-shot (user reloads the page, or it refreshes periodically) or is it a continuous stream that should appear "live" to the user?
11-02-2020 12:18 PM
The busybox/httpd server is a basic server that can be used for a generic application, with support for CGI scripting. If you need a more complex server, you can go with thttpd that is not included in the collection of packages in Petalinux but can be added as a yocto recipe. Thttpd is lightweight but more powerful and feature rich than busybox/httpd version.
If you do need a full fledged web server, the apache server is included in Petalinux and can be added easily with a petalinux-config -c rootfs. Search for apache2.
Give kudos if helpful. Accept as solution if it solves your problem.
11-02-2020 02:05 PM
The end product will be the web browser be the UI for an embedded application. Will have user input button, text input, system configuration, ADC readings, setting DAC values, uploading files, etc. So, data needs to be continuously updating. Defiantly data will be bidirectional should be live to the user. The main application has to be interfacing with custom IP through the UIO driver.
11-02-2020 06:06 PM
Of the elements you mentioned, I would say that only the "ADC readings" qualify as "continuous". Everything else changes only when the user triggers it, by clicking on a button, or entering some text, etc. These can be handled by basic CGI scripts (where "script" can mean C code which talks to your application via a BSD socket if you wish). Or equivalently you can invert this and embed a web server in your C code, which can be more convenient.
The ADC reading is slightly more complicated, depending on how fast you want it to update, and how many clients (web browsers) may access it simultaneously. In the old days, the client would poll repeatedly, making a new request each time. This can happen in the background using "AJAX" so the user won't really notice, they just see the value change on the page. If you take care to keep code reasonably efficient, you should be able to handle 100 requests per second, which is probably more than you need. This can be done using CGI, or an embedded web server.
It starts to get more complicated when the number of clients (web browsers) grows, or the refresh rate needs to be higher. There is a relatively high overhead in starting a CGI scripts, or even just establishing a new TCP connections, just to transfer a few bytes of readings to a client. Using more modern web technology, such as "websocket", can help here, by essentially allowing each client to open a pipe that remains open. Then both sides can exchange packets, very much like BSD socket, however encapsulated and transported over HTTP. If you need this level of data flow, then your web server now needs support for websocket (meaning a larger more complex server).
If you are doing this work yourself, and are not very familiar, I would suggest to start small, learn the fundamentals. Avoid the latest framework-du-jour, don't believe any of the "our magic product does everything you need" marketing fluff. You don't want to learn the innards of some framework that will be obsolete next month (that really is the velocity of web development these days, sadly). The basics change much less frequently, you would want to focus on understanding HTTP requests, how they are structured (headers followed by body), the difference between GET and POST, etc. Check out an earlier post I made on this topic: https://forums.xilinx.com/t5/Embedded-Linux/webserver-data-user-interface-with-busybox/td-p/1054117 (and countless more from other people too)
11-04-2020 10:29 AM
Thank you for your guidance; always the best explanations. But I think I will be giving node js a shot as I see its mechanics. I have successfully installed it into my build.