void CExamplesvc::svc_mult_cb(json_rpc_t *json_rpc) { int a, b; int ret; ret = json_rpc_get_int(json_rpc, "a", &a); if (ret == 0) ret = json_rpc_get_int(json_rpc, "b", &b); printf("## %s: arg=%s, ret=%d, a=%d, b=%d, => result=%d\n", __func__, m_foo, ret, a, b, a*b); if (ret) json_rpc_set_error(json_rpc, ret, NULL); else json_rpc_append_int(json_rpc, "res_value", a*b); }
static void svc_ap_scan_cb(json_rpc_t *json_rpc, void *arg) { int a; struct wpa_supplicant *wpa_s=arg; json_rpc_get_int(json_rpc, "a", &a); if(a==0 || a==1 || a==2) wpa_supplicant_ap_scan(wpa_s,a); else json_rpc_append_int(json_rpc, "a must be equal to 0 or 1 or 2. you put : ", a); }
static void svc_array_sqr_cb(json_rpc_t *json_rpc, void *arg) { int k; int ret, count, i; int *ary = NULL; ret = json_rpc_get_int(json_rpc, "k", &k); if (ret != 0) { json_rpc_set_error(json_rpc, ret, NULL); return; } count = json_rpc_get_array_count(json_rpc, "my_array"); if (count >= 0) { /* first put all the values in an array, in order to not mix json_rpc_get's and json_rpc_append's for readability sake */ ary = malloc(count*sizeof(int)); for (i = 0; i<count; i++) { /* Aim at i-th element within array "my_array" */ ret = json_rpc_get_point_at(json_rpc, "my_array", i); if (ret != 0) break; /* from that dictionary, get parameter "a" * Rem: expects all array elements to contain at least a param "a" */ ret = json_rpc_get_int(json_rpc, "a", &ary[i]); if (ret != 0) break; } } printf("## %s: arg=%s, ret=%d, k=%d, array count=%d\n", __func__, (const char*)arg, ret, k, count); if (ret) { json_rpc_set_error(json_rpc, ret, NULL); } else { json_rpc_append_int(json_rpc, "res_k", k); /* begin the array */ json_rpc_append_args(json_rpc, JSON_KEY, "res_array", -1, JSON_ARRAY_BEGIN, -1); for (i = 0; i<count; i++) { /* each array element *must* be an "OBJECT", i.e. a dictonary */ json_rpc_append_args(json_rpc, JSON_OBJECT_BEGIN, -1); json_rpc_append_int(json_rpc, "res_a", ary[i]*ary[i]); /* more stuff may be appended in there */ json_rpc_append_args(json_rpc, JSON_OBJECT_END, -1); } /* end the array */ json_rpc_append_args(json_rpc, JSON_ARRAY_END, -1); } if (ary) free(ary); }