Passing arguments to mgos_pppos_run_cmds

Hi, I’m trying to pass arguments to the mgos_pppos_run_cmds callback. I’m not familiar with the mechanism.

The following is a snippet of the code I’m using:

void getGPS(int seconds, void (*callback)(int, void *), void *user_data) {
  if (nSomeNum) {
    (*nSomeNum)++;
  }
  
  struct mgos_pppos_cmd ATCmd[] = {
      {.cmd = "AT$GPSACP", .cb = ATGPS_cb, .cb_arg=(nSomeNum)},
      {.cmd = NULL},
  };  
  if (user_data) {
    // (*callback)(*nSomeNum, user_data);
    mgos_pppos_run_cmds(0, ATCmd);
  }
}

static bool ATGPS_cb(void *cb_arg, bool ok, struct mg_str data) {
  if (!ok) {
    return false;
  }

 // Want to see the value of nSomeNum here
  
  (void)cb_arg;    
  return true;
}

How do I pass the reference of nSomeNum to mgos_pppos_run_cmds so that the callback may get its reference?

static bool ATGPS_cb(void *cb_arg, bool ok, struct mg_str data) {
  if (!ok) {
    return false;
  }

 // Want to see the value of nSomeNum here
 // you passed nSomeNum to the callback as cb_arg
 // assuming nSomeNum is a pointer to int
  int *nSomeNum=(int*)cb_arg;
  printf("nSomeNum=%d\n", *nSomeNum);
  return true;
}

Hi I tried that but the value is always zero, no matter what the value is in getGPS.

It looks like the cb_arg set in {.cmd = "AT$GPSACP", .cb = ATGPS_cb, .cb_arg=(nSomeNum)} is used only if it is set in the last line of struct mgos_pppos_cmd ATCmd[].

Otherwise cb_arg is a pointer to an internal struct mgos_pppos_data structure and the first member is int if_instance which is 0.

You can add some logging in getGPS and ATGPS_cb and compare the displayed values.
In getGPS:

LOG(LL_INFO, ("nSomeNum: %p", nSomeNum));

In ATGPS_cb:

LOG(LL_INFO, ("cb_arg: %p", cb_arg));

My guess is that cb_arg was not intended to be used with mgos_pppos_run_cmds.

Refs:
free_cmds
mgos_pppos_run_cmds