From b829ccf57c83d0cad0b285fdd9a25e823ce2bbcd Mon Sep 17 00:00:00 2001 From: Jonathan Aillet Date: Thu, 19 Sep 2019 11:54:50 +0200 Subject: Rework verb request json parsing Rework verb request json parsing to allow having request without 'devid' key in json. BUG-AGL: SPEC-2832 Change-Id: Id937d98c29d30b9298be3a1972f1a2cf5d0b722c Signed-off-by: Jonathan Aillet --- alsa-binding/Alsa-SetGet.c | 85 ++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 37 deletions(-) (limited to 'alsa-binding/Alsa-SetGet.c') diff --git a/alsa-binding/Alsa-SetGet.c b/alsa-binding/Alsa-SetGet.c index 19b2164..78ac7e9 100644 --- a/alsa-binding/Alsa-SetGet.c +++ b/alsa-binding/Alsa-SetGet.c @@ -38,18 +38,18 @@ #define SNDRV_CTL_IOCTL_CARD_INFO(size) _IOR_HACKED('U', 0x01, size) -PUBLIC void NumidsListParse(ActionSetGetT action, queryValuesT *queryValues, ctlRequestT *ctlRequest) { +PUBLIC void NumidsListParse(ActionSetGetT action, controlQueryValuesT *queryControlValues, ctlRequestT *ctlRequest) { int length; - for (int idx = 0; idx < queryValues->count; idx++) { + for (int idx = 0; idx < queryControlValues->count; idx++) { json_object *jId, *valuesJ; ctlRequest[idx].used = 0; ctlRequest[idx].tag = NULL; ctlRequest[idx].valuesJ = NULL; // when only one NUMID is provided it might not be encapsulated in a JSON array - if (json_type_array == json_object_get_type(queryValues->numidsJ)) ctlRequest[idx].jToken = json_object_array_get_idx(queryValues->numidsJ, idx); - else ctlRequest[idx].jToken = queryValues->numidsJ; + if (json_type_array == json_object_get_type(queryControlValues->numidsJ)) ctlRequest[idx].jToken = json_object_array_get_idx(queryControlValues->numidsJ, idx); + else ctlRequest[idx].jToken = queryControlValues->numidsJ; enum json_type jtype = json_object_get_type(ctlRequest[idx].jToken); switch (jtype) { @@ -59,9 +59,9 @@ PUBLIC void NumidsListParse(ActionSetGetT action, queryValuesT *queryValues, ctl ctlRequest[idx].numId = json_object_get_int(ctlRequest[idx].jToken); // Special SET simple short numid form [numid, [VAL1...VALX]] - if (action == ACTION_SET && queryValues->count == 2) { - ctlRequest[idx].valuesJ = json_object_array_get_idx(queryValues->numidsJ, 1); - queryValues->count = 1; //In this form count==2 , when only one numid is to set + if (action == ACTION_SET && queryControlValues->count == 2) { + ctlRequest[idx].valuesJ = json_object_array_get_idx(queryControlValues->numidsJ, 1); + queryControlValues->count = 1; //In this form count==2 , when only one numid is to set idx++; continue; } else @@ -1243,24 +1243,35 @@ OnErrorExit: // assign multiple control to the same value STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { - ctlRequestT *ctlRequest; int err = 0, done; unsigned int ctlCount; - snd_ctl_t *ctlDev=NULL; + + queryModeE mode; + + char *devId = NULL; + + snd_ctl_t *ctlDev = NULL; snd_ctl_elem_list_t *ctlList; - queryValuesT queryValues; + json_object *queryJ, *numidsJ, *sndctls; - queryJ = alsaCheckQuery(request, &queryValues); - if (!queryJ) { - afb_req_fail_f(request, "invalid-query", "Invalid Query=%s", json_object_get_string(queryJ)); + controlQueryValuesT queryControlValues; + ctlRequestT *ctlRequest; + + queryJ = afb_req_json(request); + + devId = alsaGetDevIdFromQuery(queryJ); + if (!devId) { + afb_req_fail_f(request, "devid-missing", "Invalid query='%s'", json_object_get_string(queryJ)); return; } + mode = alsaGetModeFromQuery(queryJ); + // Phrase Numids + optional values done = json_object_object_get_ex(queryJ, "ctl", &numidsJ); if (!done && action == ACTION_GET) { - queryValues.count = 0; + queryControlValues.count = 0; } else if (!done && action == ACTION_SET) { afb_req_fail_f(request, "invalid-set", "To set an ALSA control value(s), they must be specified in query=%s", json_object_get_string(queryJ)); @@ -1270,15 +1281,15 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { enum json_type jtype = json_object_get_type(numidsJ); switch (jtype) { case json_type_array: - queryValues.numidsJ = numidsJ; - queryValues.count = (int) json_object_array_length(numidsJ); + queryControlValues.numidsJ = numidsJ; + queryControlValues.count = (int) json_object_array_length(numidsJ); break; case json_type_int: case json_type_object: case json_type_string: - queryValues.count = 1; - queryValues.numidsJ = numidsJ; + queryControlValues.count = 1; + queryControlValues.numidsJ = numidsJ; break; default: @@ -1287,24 +1298,24 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { } } - if ((err = snd_ctl_open(&ctlDev, queryValues.devid, 0)) < 0) { - afb_req_fail_f(request, "sndcrl-notfound", "devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); + if ((err = snd_ctl_open(&ctlDev, devId, 0)) < 0) { + afb_req_fail_f(request, "sndcrl-notfound", "devid='%s' load fail error=%s\n", devId, snd_strerror(err)); goto close_ctl; } snd_ctl_elem_list_alloca(&ctlList); if ((err = snd_ctl_elem_list(ctlDev, ctlList)) < 0) { - afb_req_fail_f(request, "listInit-failed", "devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f(request, "listInit-failed", "devid='%s' load fail error=%s\n", devId, snd_strerror(err)); goto close_ctl; } if ((err = snd_ctl_elem_list_alloc_space(ctlList, snd_ctl_elem_list_get_count(ctlList))) < 0) { - afb_req_fail_f(request, "listAlloc-failed", "devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f(request, "listAlloc-failed", "devid='%s' load fail error=%s\n", devId, snd_strerror(err)); goto close_ctl; } if ((err = snd_ctl_elem_list(ctlDev, ctlList)) < 0) { - afb_req_fail_f(request, "listOpen-failed", "devid='%s' load fail error=%s\n", queryValues.devid, snd_strerror(err)); + afb_req_fail_f(request, "listOpen-failed", "devid='%s' load fail error=%s\n", devId, snd_strerror(err)); goto free_elem_list; } @@ -1312,10 +1323,10 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { ctlCount = snd_ctl_elem_list_get_used(ctlList); // if more than one crl requested prepare an array for response - if (queryValues.count != 1 && action == ACTION_GET) sndctls = json_object_new_array(); + if (queryControlValues.count != 1 && action == ACTION_GET) sndctls = json_object_new_array(); else sndctls = NULL; - if (queryValues.count == 0) { + if (queryControlValues.count == 0) { ctlRequest = alloca(sizeof (ctlRequestT)*(ctlCount)); ctlRequest->tag = NULL; @@ -1326,7 +1337,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { snd_ctl_elem_list_get_id(ctlList, ctlIndex, elemId); - err = alsaGetSingleCtl(ctlDev, elemId, &ctlRequest[ctlIndex], queryValues.mode); + err = alsaGetSingleCtl(ctlDev, elemId, &ctlRequest[ctlIndex], mode); if (err) { afb_req_fail_f(request, "control_get", @@ -1337,7 +1348,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { ctlCount); goto free_response_array; } - else if (queryValues.count == 1) { + else if (queryControlValues.count == 1) { sndctls = ctlRequest[ctlIndex].valuesJ; } else { @@ -1348,10 +1359,10 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { else { int found, ctlIndex; - ctlRequest = alloca(sizeof (ctlRequestT)*(queryValues.count)); - NumidsListParse(action, &queryValues, ctlRequest); + ctlRequest = alloca(sizeof (ctlRequestT)*(queryControlValues.count)); + NumidsListParse(action, &queryControlValues, ctlRequest); - for (int jdx = 0; jdx < queryValues.count; jdx++) { + for (int jdx = 0; jdx < queryControlValues.count; jdx++) { found = 0; ctlIndex = 0; while (ctlIndex < ctlCount) { @@ -1381,7 +1392,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { "Error while trying to find control using its name=%s (query %i out of %i)", ctlRequest[jdx].tag, (jdx + 1), - queryValues.count); + queryControlValues.count); goto free_response_array; } else { @@ -1390,7 +1401,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { "Error while trying to find control using its numid=%i (query %i out of %i)", ctlRequest[jdx].numId, (jdx + 1), - queryValues.count); + queryControlValues.count); goto free_response_array; } } @@ -1401,7 +1412,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { snd_ctl_elem_list_get_id(ctlList, ctlIndex, elemId); switch (action) { case ACTION_GET: - err = alsaGetSingleCtl(ctlDev, elemId, &ctlRequest[jdx], queryValues.mode); + err = alsaGetSingleCtl(ctlDev, elemId, &ctlRequest[jdx], mode); if (err) { afb_req_fail_f(request, "control_get", @@ -1409,7 +1420,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { snd_ctl_elem_list_get_numid(ctlList, ctlIndex), snd_ctl_elem_list_get_name(ctlList, ctlIndex), (jdx + 1), - queryValues.count); + queryControlValues.count); goto free_response_array; } break; @@ -1424,7 +1435,7 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { snd_ctl_elem_list_get_name(ctlList, ctlIndex), json_object_get_string(ctlRequest[jdx].valuesJ), (jdx + 1), - queryValues.count); + queryControlValues.count); goto free_response_array; } break; @@ -1436,12 +1447,12 @@ STATIC void alsaSetGetCtls(ActionSetGetT action, afb_req_t request) { snd_ctl_elem_list_get_numid(ctlList, ctlIndex), snd_ctl_elem_list_get_name(ctlList, ctlIndex), (jdx + 1), - queryValues.count); + queryControlValues.count); goto free_response_array; } if (action == ACTION_GET) { - if (queryValues.count == 1) + if (queryControlValues.count == 1) sndctls = ctlRequest[jdx].valuesJ; else json_object_array_add(sndctls, ctlRequest[jdx].valuesJ); -- cgit 1.2.3-korg