Embedded full stack web server shoehorning fun!

FIrst, apologies, this is just such an interesting experience I need to vent somewhere or go crazy :smiley: I hope someone will find this piece entertaining or even perhaps helpful.

O.k. so I work as a contractor within embedded systems and web. Latest job started about three months ago, when one of the higher uppers managed to have a brilliant idea; They want to implement a web server management interface on The Product™.

Now, The Product™ is a highly specialized embedded system, used in an industry setting. After some back and forth on the subject, here are the basic specifications:

  • The Product™ is developed in C exclusively
  • The webserver must be implemented as a single threaded task
  • There is a TCP/IP stack implemented
  • The hardware is tiny and is an integrated SoC with a few ports
  • Available storage is 2 MB
  • Available RAM is 256 kb, of which 100 kb is already used
  • NO Open Source Software allowed, due to conservative company ideas
  • Wants support for AJAX and responsive web design
  • The Product™ will not have Internet access of any kind
  • Should allow for web interface device control
  • Should be compliant with: HTML5, CSS3, ECMAScript 6, HTTP(S), SOAP
  • Needs to support at least three browsers (Firefox, Chrome, Edge)

… And the best part, I was originally supposed to get a front-end developer to help me, but he quit 1 week before I started and they have yet to find a new one. Of course! :grin:

So, I have to write a full stack http server from the ground up that does a lot of heavy string parsing in C, make all of that work, then switch to JavaScript, and make the entire interface fit in like 50-60 kb of usable memory, with the assistance of perhaps 1 MB of flash storage. And no Internet Access! Wow. Just… WOW.

So, immediately all forms of unoptimized imagery is out the window, > 8 kb is pretty much useless to my purposes. Same thing with template files, I could develop a template engine but that would take time. O.k. time to look at the various frameworks available. Oh, I can barely use any frameworks due to the stipulation of no FOSS/unlicensed products. Ok then!

So I sit down, start to get the networking stuff up. Brush off my knowledge on sockets, poll() and select() APIs. Quite easy, actually, and I get something you can talk to the next day.

Next, developing an HTTP parser from scratch, designed to be small, lightweight, tiny and only support certain headers. This works fairly well, but takes time. A lot of time. I’m stumbling through in the dark, and clock is slowly ticking away. I make improvements but after 6 weeks I get a call from the product manager who wonder why a prototype takes so long to develop. I explain to him that there are six orthogonal technologies involved here (TCP/IP, TLS, HTTP, HTML/CSS, JavaScript and SOAP) and that the frontend guy, who was supposed to do the three later parts, left. Week after, I am done with the first prototype. For something held together with duct tape, chewing gum and paperclips, it runs remarkably well.

Allright, now for the difficult part! Which, in my case, given I have done HTML/CSS/JavaScript for 10 years by now, actually is the easy part. The frontend. This only takes 3 weeks to get a good, decent prototype work going. Sure¸ not perfect, but the pages are displayed where they should be, MVC-ish design, and best of it all - the web resources only take up a total of 60 kb, images and all! Not easy to get it that lean and mean, but I did it.

But, after two weeks, the deadline is passed, which means corporate does not feel like renewing my contract. So, since I “can’t meet deadlines” (which were unreasonably set in the first place) apparently I now gotta find another assignment. Because I chose the hard but rewarding path of going the extra mile to make this portable to the related Dis Project™ and Dat Project™, so basically all they need is one week integration work to save 8 weeks per project. Too bad they don’t think far enough…

Maybe I am just too much of an architect and take too much pride in my work to be a good contractor? Ah well, not too sad about it, will definitely not miss parsing strings with C on this fun little project, but it was interesting for sure to finally build a full stack webpage from HTTP server and up. What I have is already delivered + integrated, and got another thing lined up in August too. Let’s see the next guy struggle with juggling all those tech together… Thinking about doing a from-scratch implementation and releasing that under a BSD-style license, but let’s see what happens!

Anyway, rant over. Carry on! Hope my little rant brightened up the day for someone!

9 Likes

Thanks for sharing! I love reading war-stories :smiley:

2 Likes

Next guy is going to rant how you haven’t used any of the standard stuff and wrote everything from scratch and how thing is full of bugs and hard to use.

2 Likes

True. I would have loved to use something like Mongoose, alas that was not possible. At all. GPLv3 on a hard realtime embedded system where everything needs to be linked into a single binary to fit, weeeeeeell… No. Just. No. At this level it is more firmware than software, and suits gets this funny look whenever you speak of sharing source code. Think FreeRTOS level of OS, here.

I did try to look around if there, per any chance, would be a BSD-licensed HTTP library that also wasn’t a web server, the only things I could find were too integrated to the TCP/IP stack to be of any use, or used too many resources.

On my wishlist for this, I would love a BSD-based HTTP library that:

  • Allows me to disable and enable optional features at compile time (via defines)
  • Properly supports all 9 methods, TLS and all optional headers
  • Provides hooks for REST APIs
  • Has a minimal footprint or otherwise allows for such to happen
  • Has clear separation between network layer, protocol layer and content layer
  • Allows hard realtime determinism (optional, because it will impact performance)

Suggestions welcome, else I know what to do come Devember… :slight_smile:

1 Like

At work we do a lot of high performance servers, or should I say we do lots of high performance serving, albeit mostly c++, and regular server machines not microcontrollers, and only soft-realtime not hard.

But, like in embedded, we have a similar set of concerns, basically every ram access, every data structure, every memory copy, everything has a cost in terms of CPU cycles, and code has to be measurably lean.

If I were doing embedded, I’d start by reducing requirements, and would be looking at an easy to understand and optimize code base that’s easy to recycle, as a kind of light weight framework perhaps, not necessarily anything that amounts to a full blown library.

What soc or uc are you working with? Is it actually FreeRTOS compatible?

1 Like

CivetWeb - I only know it exists because I was looking into Zephyr. Their README says they forked from Mongoose in 2013, when it was still MIT. Do report back what you find out about it!

By the way, what’s wrong with whatever comes with FreeRTOS? I’d think that since Amazon bought them they’d invest in a sane HTTP server?

I’m guessing the tight integration of everything in Mbed is a no go for you?

2 Likes

No set SoC - Since we have 20-ish different-yet-similar low volume products, instead of 20 different SoCs we produce one FPGA and burn a different SoC for all 20 products. That way we save a lot of money on hardware costs. Sure, it’s not as performant as burning an ASIC, but it’s good enough. If required, we can always get an ASIC design later. :slight_smile:

I think they use something or other from Intel, though never dug that far down.

I think it’s just that the old OS used in the organisation is so ingrained, switching it now would cost a lot of time and manpower. Maybe they’re ready in another 10 years? :slight_smile:

Oooh, nice! Will look into it when time permits, this might be close to what I want. Thanks!

Very impressive work and experience there. My two cents on this scenario is that at least from my personal experience and know-how, the ask was not at all a reasonable one. For you it seemed like you knew it was possible but it was going to be hard, and for one reason or another you took the job. Maybe deep down you just knew you would have fun figuring out how to do it. And you did! Just took longer than the ridiculous timeline (which it sounds like you didn’t have control over) allowed for.

Given the absurdity of the ask as well as how you managed to actually get it done, I would say that you certainly have enough know-how and expertise to just say no to unrealistic expectations like that. Something to think about next time. I’m fairly convinced with your skill set you’d have little trouble finding folks that are a bit less divorced from reality to work for.

1 Like

Oh, for sure. This particular case I was supposed to focus 100% on the back end (e.g. HTTP and network only) and let the front end guy do the rest. For 2 people working together, that is not too big of an ask. I never thought they would be so strict that two weeks of delay on what was supposedly a 2 man, 10 week job would break the deal. Oh well, their loss. :slight_smile: