Excuting RPC locally in sequence

I’m working on a device that is powered by a battery, and utilizing Esp32 sleep.

So once the device boot and connect to a network ( I’m using pppos) it will connect to MQTT server and subscribe to topics, then it will this function that actually calles more than one RPC locally and publish responses to the MQTT.

static void power_flow_timer_cb(void *arg)

{

struct mg_rpc_call_opts opts = {.dst = mg_mk_str(MGOS_RPC_LOOPBACK_ADDR)};

int sleep_time = mgos_sys_config_get_cct_power_conf_sleep_cycle();

mg_rpc_callf(mgos_rpc_get_global(), mg_mk_str("CCT.1"), NULL, NULL, &opts,"{}");

mg_rpc_callf(mgos_rpc_get_global(), mg_mk_str("CCT.2"), NULL, NULL, &opts,"{}");

mg_rpc_callf(mgos_rpc_get_global(), mg_mk_str("Config.Save"), NULL, NULL, &opts,"{reboot: false}");

mgos_gpio_write(mgos_sys_config_get_pins_sleep(),1);

mg_rpc_callf(mgos_rpc_get_global(), mg_mk_str("CCT.espSleep"), NULL, NULL, &opts,"{time: %d}", sleep_time);

    (void)arg;

}

in the MQTT client I get the response from the first RPC call (CCT.1) and the esp32 goes to sleep before sending a response from (CCT.2).

My question is there a way to force the call to be in sequence after the previous call is completed to call the next RPC.

Thanks
Leo

You can add a callback for each rpc call and call the next function in it.
Something like this:

static void cct1_cb(const char *result, int error_code, const char *error_msg,
                    void *cb_arg) {
  LOG(LL_INFO, ("%s", __FUNCTION__));
  mgos_rpc_call(MGOS_RPC_LOOPBACK_ADDR, "CCT.2", "", cct2_cb, NULL);
  (void) result;
  (void) error_code;
  (void) error_msg;
  (void) cb_arg;
}

static void power_flow_timer_cb(void *arg) {
  mgos_rpc_call(MGOS_RPC_LOOPBACK_ADDR, "CCT.1", "", cct1_cb, NULL);
  (void) arg;
}