Calling the LOG macro from an RPC handler

#1

I’m trying to write an RPC callback method that will allow users to refresh their configuration settings. I started using the sum_cb() example, and added both my config read method and a LOG, but it seems that neither of them are called, even though the callback executes and returns the result as expected.

Is there some reason I can’t call other methods from a callback?

static void sum_cb(struct mg_rpc_request_info *ri, void *cb_arg,
               struct mg_rpc_frame_info *fi, struct mg_str args) 
{

  LOG(LL_ERROR, ("===>>>Calling: sum_cb()"));

  refreshConfig(&gUserSettings);

  double a = 0, b = 0;
  if (json_scanf(args.p, args.len, ri->args_fmt, &a, &b) == 2) {
mg_rpc_send_responsef(ri, "%.2lf", a + b);
  } else {
mg_rpc_send_errorf(ri, -1, "Bad request. Expected: {\"a\":N1,\"b\":N2}");
  }
  (void) cb_arg;
  (void) fi;
}
#2

Dumb question: did you save the file, build, flash or OTA update the device after adding those lines?

#3

Yes, I did. A whole bunch of times.

#4

Can you share a minimal example to reproduce the issue?

#5

Sure, it’s easy to reproduce. I took the example C language application, added the mgos_rpc.h include, the sample RPC sum_cb() and was able to reproduce it. Calling RPC.List shows that Sum is there, and calling Sum displays the result. The logged text never appears. Code below.

I called it from mos with this:

mos call Sum '{"a": 7, "b": 8}'

#include "mgos.h"
#include "mgos_app.h"
#include "mgos_rpc.h"

static void sum_cb(struct mg_rpc_request_info *ri, void *cb_arg,
                   struct mg_rpc_frame_info *fi, struct mg_str args)
{
  LOG(LL_ERROR, ("===>>>Calling: sum_cb()"));

  double a = 0, b = 0;
  if (json_scanf(args.p, args.len, ri->args_fmt, &a, &b) == 2) {
    mg_rpc_send_responsef(ri, "%.2lf", a + b);
  } else {
    mg_rpc_send_errorf(ri, -1, "Bad request. Expected: {\"a\":N1,\"b\":N2}");
  }
  (void) cb_arg;
  (void) fi;
}

// Entry point to app
enum mgos_app_init_result mgos_app_init(void)
{
  mg_rpc_add_handler(mgos_rpc_get_global(), "Sum", "{a: %lf, b: %lf}", sum_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}
#6

Ahhh, I see.
You can’t see the log message when you are not running mos console.
You can’t use the serial console and mos call over UART in the same time.

You will need to add, e.g. the wifi and rpc-ws libraries, configure wifi and run mos console over UART and mos --port ws://IP/rpc call Sum '{"a": 7, "b": 8}'

#7

Hm, okay I see. I already have WiFi enabled, I’ll try calling this endpoint using Postman and see if that works. Thanks very much!