ESP8266 - HTTP RESTful server - mos build error

  1. My goal is:
    I would like to implement a Restful server and I’m folowing the example from https://github.com/cesanta/mongoose/blob/master/examples/http-restful-server

  2. My actions are:
    In the file app\src\main.c:
    #include “mgos.h”
    #include “mongoose.h”
    (…)
    if (ev == MG_EV_HTTP_MSG) {
    (…)

  3. The result I see is:
    After execute the command “mos build --local --verbose”:
    (…)
    error: ‘MG_EV_HTTP_MSG’ undeclared (first use in this function); did you mean ‘MG_EV_HTTP_CHUNK’?
    error: implicit declaration of function ‘mg_http_match_uri’ [-Werror=implicit-function-declaration]
    error: implicit declaration of function ‘mg_http_reply’ [-Werror=implicit-function-declaration]
    error: dereferencing pointer to incomplete type ‘struct mg_http_message’
    (…)

  4. My expectation & question is:
    What should I do to the header file “mongoose.h” be included in the project?

The version compatible with Mongoose OS is here.

From https://github.com/mongoose-os-apps I saw that the apps has the entry point ‘mgos_app_init’ and include header prefix ‘mgos’. The RESTful server example from the link you passed has the entry point ‘main(int argc, char *argv[])’ and the include header mongoose.h, like the example I’m using until now. My question is about how to call functions from mongoose.h in the, e.g. ‘mgos_app_init’ function? Include mongoose.h is resulting in build error

Mongoose-OS uses Mongoose as an HTTP server (and other stuff), The version of Mongoose inside Mongoose-OS (mOS) is not the latest one, it is provided as a precompiled binary. You need to follow mOS specific examples.
The example is just an example on how to use Mongoose functions, follow it as a guide for your code,
#include "mongoose.h" builds correctly, maybe you are not properly including other headers, this is what I’ve just tried:

#include "mgos.h"
#include "mgos_app.h"
#include "mongoose.h"

If you get an error, please indicate which error so someone can guide you.
If you don’t know what to do inside mgos_app_init()then check some C code examples and ask specific questions. One usually registers event handlers and starts timers there, this is an event-driven system, there is no main() here.

C:\mos\app\src\main.c:

#include "mgos.h"
#include "mgos_app.h"
#include "mongoose.h"


static const char *s_http_addr = "http://0.0.0.0:8000";
static const char *s_root_dir = ".";

static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
    if (ev == MG_EV_HTTP_MSG) {
        struct mg_http_message *hm = (struct mg_http_message *) ev_data;
        if (mg_http_match_uri(hm, "/api/f1")) {
            mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);  
        } else if (mg_http_match_uri(hm, "/api/f2/*")) {
            mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len, hm->uri.ptr);
        } else {
            struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
            mg_http_serve_dir(c, ev_data, &opts);
        }
    }
    (void) fn_data;
}


enum mgos_app_init_result mgos_app_init(void) {  
  struct mg_mgr mgr = mgos_get_mgr();
  mg_log_set("2");
  if(!mgr) return MGOS_APP_INIT_ERROR;
  mg_http_listen(&mgr, s_http_addr, fn, NULL);
  return MGOS_APP_INIT_SUCCESS;
}

mos.yml:


author: mongoose-os
description: A Mongoose OS app skeleton
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

# Optional. List of tags for online search.
tags:
  - c

# List of files / directories with C sources. No slashes at the end of dir names.
sources:
  - src

# List of dirs. Files from these dirs will be copied to the device filesystem
filesystem:
  - fs

libs:
  - origin: https://github.com/mongoose-os-libs/boards
  - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/http-server
  - origin: https://github.com/mongoose-os-libs/wifi
cdefs:
  MG_ENABLE_DNS: 1
  MG_ENABLE_DNS_SERVER: 1


# Used by the mos tool to catch mos binaries incompatible with this file format
manifest_version: 2017-09-29

mos tool, comand “mos build --local --verbose”:

(...)
make: Entering directory '/app'
  MKDIR /c/mos/app/build/objs/fw_temp
  MKDIR /c/mos/app/build/fw
  GEN   /c/mos/app/build/gen/mgos_config.c
  GEN   /c/mos/app/build/gen/mgos_ro_vars.c
  CC    esptool2.c
  CC    esptool2_elf.c
  LD    /c/mos/app/build/objs/esptool2
  GEN   /c/mos/app/build/gen/ffi_exports.c
  GEN   /mongoose-os/platforms/esp8266/ld/non_os.ld (0xffff0 @ 0x40200010) -> /c/mos/app/build/gen/0x100000.ld
  CP    /c/mos/app/build/gen/conf0.json -> /c/mos/app/build/objs/fs
  CC    /mongoose-os/platforms/esp8266/src/esp_cache.c
  CC    rboot-stage2a.c
  CC    /c/mos/app/src/main.c
  CP    /c/mos/app/deps/ca-bundle/fs/ca.pem -> /c/mos/app/build/objs/fs
  CC    /c/mos/app/deps/mbedtls/src/mgos_mbedtls.c
  CP    /c/mos/app/fs/index.html -> /c/mos/app/build/objs/fs
  CC    /c/mos/app/deps/mbedtls/src/esp8266/esp_aes.c
  MKFS  /usr/local/bin/mkspiffs8 262144 {bs:4096,ps:256,es:4096} /c/mos/app/build/objs/fs -> /c/mos/app/build/objs/fw_temp/fs.bin
  CC    /c/mos/app/deps/mbedtls/src/esp8266/esp_md5.c
     FS params: size=262144, bs=4096, ps=256, es=4096
       Adding ca.pem: 32265
       Adding conf0.json: 3
       Adding index.html: 75
     FS stats : space total=233681, used=33885, free=199796
  CC    /c/mos/app/deps/mbedtls/src/esp8266/esp_sha1.c
  LD    /c/mos/app/build/objs/rboot/rboot-stage2a.elf
  CC    /c/mos/app/deps/modules/mbedtls/library/aes.c
  E2    /c/mos/app/build/gen/rboot-hex2a.h
  CC    rboot.c
  LD    /c/mos/app/build/objs/rboot/rboot.elf
  E2    /c/mos/app/build/objs/rboot/rboot.bin
  CC    /c/mos/app/deps/modules/mbedtls/library/aes_atca.c
  CC    /c/mos/app/deps/modules/mbedtls/library/aesni.c
  CC    /c/mos/app/deps/modules/mbedtls/library/arc4.c
  CC    /c/mos/app/deps/modules/mbedtls/library/aria.c
  CC    /c/mos/app/deps/modules/mbedtls/library/asn1parse.c
  CC    /c/mos/app/deps/modules/mbedtls/library/asn1write.c
  CC    /c/mos/app/deps/modules/mbedtls/library/base64.c
  CC    /c/mos/app/deps/modules/mbedtls/library/bignum.c
  CC    /c/mos/app/deps/modules/mbedtls/library/blowfish.c
  CC    /c/mos/app/deps/modules/mbedtls/library/camellia.c
  CC    /c/mos/app/deps/modules/mbedtls/library/ccm.c
  CC    /c/mos/app/deps/modules/mbedtls/library/certs.c
  CC    /c/mos/app/deps/modules/mbedtls/library/chacha20.c
  CC    /c/mos/app/deps/modules/mbedtls/library/chachapoly.c
  CC    /c/mos/app/deps/modules/mbedtls/library/cipher.c
/c/mos/app/src/main.c: In function 'fn':
/c/mos/app/src/main.c:29:15: error: 'MG_EV_HTTP_MSG' undeclared (first use in this function); did you mean 'MG_EV_HTTP_CHUNK'?
   29 |     if (ev == MG_EV_HTTP_MSG) {
      |               ^~~~~~~~~~~~~~
      |               MG_EV_HTTP_CHUNK
/c/mos/app/src/main.c:29:15: note: each undeclared identifier is reported only once for each function it appears in
/c/mos/app/src/main.c:31:13: error: implicit declaration of function 'mg_http_match_uri' [-Werror=implicit-function-declaration]
   31 |         if (mg_http_match_uri(hm, "/api/f1")) {
      |             ^~~~~~~~~~~~~~~~~
/c/mos/app/src/main.c:32:13: error: implicit declaration of function 'mg_http_reply' [-Werror=implicit-function-declaration]
   32 |             mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
      |             ^~~~~~~~~~~~~
/c/mos/app/src/main.c:34:75: error: dereferencing pointer to incomplete type 'struct mg_http_message'
   34 |             mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len, hm->uri.ptr);
      |                                                                           ^~
  CC    /c/mos/app/deps/modules/mbedtls/library/cipher_wrap.c
/c/mos/app/src/main.c:36:20: error: variable 'opts' has initializer but incomplete type
   36 |             struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
      |                    ^~~~~~~~~~~~~~~~~~
/c/mos/app/src/main.c:36:48: error: 'struct mg_http_serve_opts' has no member named 'root_dir'
   36 |             struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
      |                                                ^~~~~~~~
/c/mos/app/src/main.c:36:59: error: excess elements in struct initializer [-Werror]
   36 |             struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
      |                                                           ^~~~~~~~~~
/c/mos/app/src/main.c:36:59: note: (near initialization for 'opts')
/c/mos/app/src/main.c:36:39: error: storage size of 'opts' isn't known
   36 |             struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
      |                                       ^~~~
/c/mos/app/src/main.c:37:13: error: implicit declaration of function 'mg_http_serve_dir'; did you mean 'mg_http_serve_file'? [-Werror=implicit-function-declaration]
   37 |             mg_http_serve_dir(c, ev_data, &opts);
      |             ^~~~~~~~~~~~~~~~~
      |             mg_http_serve_file
/c/mos/app/src/main.c:36:39: error: unused variable 'opts' [-Werror=unused-variable]
   36 |             struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
      |                                       ^~~~
/c/mos/app/src/main.c: In function 'mgos_app_init':
/c/mos/app/src/main.c:44:23: error: invalid initializer
   44 |   struct mg_mgr mgr = mgos_get_mgr();
      |                       ^~~~~~~~~~~~
/c/mos/app/src/main.c:45:3: error: implicit declaration of function 'mg_log_set' [-Werror=implicit-function-declaration]
   45 |   mg_log_set("2");
      |   ^~~~~~~~~~
/c/mos/app/src/main.c:46:6: error: wrong type argument to unary exclamation mark
   46 |   if(!mgr) return MGOS_APP_INIT_ERROR;
      |      ^
  CC    /c/mos/app/deps/modules/mbedtls/library/cmac.c
/c/mos/app/src/main.c:47:3: error: implicit declaration of function 'mg_http_listen' [-Werror=implicit-function-declaration]
   47 |   mg_http_listen(&mgr, s_http_addr, fn, NULL);
      |   ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make: *** [/c/mos/app/build/objs/main.c.o] Error 1
make: *** Waiting for unfinished jobs....
/c/mos/app/deps/modules/mongoose-os/platforms/esp8266/Makefile.build:379: recipe for target '/c/mos/app/build/objs/main.c.o' failed
make: Leaving directory '/app'
Error: exit status 2
/src/cli/build_local.go:692: 
/src/cli/build_local.go:679: 
/src/cli/build_local.go:444: 
/src/cli/build.go:278: 
/src/cli/build.go:221: 
/src/cli/main.go:194: build failed
exit status 1
Command completed.

I would like to know how to fix the errors “‘MG_EV_HTTP_MSG’ undeclared (first use in this function)” and “implicit declaration of function ‘mg_http_reply’”?

@nliviu and @scaprile, thank you for support. Now I did understand that I was calling functions and using enumerations from a different Mongoose version. I saw into the header file moogoose.h (app/deps/mongoose/include/mongoose.h) the version is 6.18 and I was following
the example from the version 7.6.