mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-11 05:57:05 +03:00
button json parser improvements
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -28,35 +28,41 @@
|
|||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
#include "audio_controls.h"
|
#include "audio_controls.h"
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
ACTRLS_MAP_INT, ACTRLS_MAP_BOOL, ACTRLS_MAP_ACTION,ACTRLS_MAP_TYPE,ACTRLS_MAP_END
|
|
||||||
} actrls_action_map_element_type_e;
|
|
||||||
|
|
||||||
|
typedef esp_err_t (actrls_config_map_handler) (const cJSON * member, actrls_config_t *cur_config,uint32_t offset);
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char * member;
|
char * member;
|
||||||
uint32_t offset;
|
uint32_t offset;
|
||||||
actrls_action_map_element_type_e type;
|
actrls_config_map_handler * handler;
|
||||||
} actrls_config_map_t;
|
} actrls_config_map_t;
|
||||||
|
|
||||||
|
esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config);
|
||||||
|
esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config);
|
||||||
|
esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
|
||||||
|
esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
|
||||||
|
esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
|
||||||
|
esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset);
|
||||||
|
|
||||||
static const actrls_config_map_t actrls_config_map[] =
|
static const actrls_config_map_t actrls_config_map[] =
|
||||||
{
|
{
|
||||||
{"gpio", offsetof(actrls_config_t,gpio), ACTRLS_MAP_INT},
|
{"gpio", offsetof(actrls_config_t,gpio), actrls_process_int},
|
||||||
{"debounce", offsetof(actrls_config_t,debounce), ACTRLS_MAP_INT},
|
{"debounce", offsetof(actrls_config_t,debounce), actrls_process_int},
|
||||||
{"type", offsetof(actrls_config_t,type),ACTRLS_MAP_TYPE},
|
{"type", offsetof(actrls_config_t,type), actrls_process_type},
|
||||||
{"pull", offsetof(actrls_config_t,pull),ACTRLS_MAP_BOOL},
|
{"pull", offsetof(actrls_config_t,pull), actrls_process_bool},
|
||||||
{"long_press", offsetof(actrls_config_t,long_press),ACTRLS_MAP_INT},
|
{"long_press", offsetof(actrls_config_t,long_press),actrls_process_int},
|
||||||
{"shifter_gpio", offsetof(actrls_config_t,shifter_gpio),ACTRLS_MAP_INT},
|
{"shifter_gpio", offsetof(actrls_config_t,shifter_gpio), actrls_process_int},
|
||||||
{"normal", offsetof(actrls_config_t,normal), ACTRLS_MAP_ACTION},
|
{"normal", offsetof(actrls_config_t,normal), actrls_process_action},
|
||||||
{"shifted", offsetof(actrls_config_t,shifted), ACTRLS_MAP_ACTION},
|
{"shifted", offsetof(actrls_config_t,shifted), actrls_process_action},
|
||||||
{"longpress", offsetof(actrls_config_t,longpress), ACTRLS_MAP_ACTION},
|
{"longpress", offsetof(actrls_config_t,longpress), actrls_process_action},
|
||||||
{"longshifted", offsetof(actrls_config_t,longshifted), ACTRLS_MAP_ACTION},
|
{"longshifted", offsetof(actrls_config_t,longshifted), actrls_process_action},
|
||||||
{"", 0,ACTRLS_MAP_END}
|
{"", 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
// BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
|
// BEWARE: the actions below need to stay aligned with the corresponding enum to properly support json parsing
|
||||||
static const char * actrls_action_s[ ] = { "ACTRLS_VOLUP","ACTRLS_VOLDOWN","ACTRLS_TOGGLE","ACTRLS_PLAY",
|
#define EP(x) [x] = #x /* ENUM PRINT */
|
||||||
"ACTRLS_PAUSE","ACTRLS_STOP","ACTRLS_REW","ACTRLS_FWD","ACTRLS_PREV","ACTRLS_NEXT",
|
static const char * actrls_action_s[ ] = { EP(ACTRLS_VOLUP),EP(ACTRLS_VOLDOWN),EP(ACTRLS_TOGGLE),EP(ACTRLS_PLAY),
|
||||||
"BCTRLS_PUSH", "BCTRLS_UP","BCTRLS_DOWN","BCTRLS_LEFT","BCTRLS_RIGHT", ""} ;
|
EP(ACTRLS_PAUSE),EP(ACTRLS_STOP),EP(ACTRLS_REW),EP(ACTRLS_FWD),EP(ACTRLS_PREV),EP(ACTRLS_NEXT),
|
||||||
|
EP(BCTRLS_PUSH), EP(BCTRLS_UP),EP(BCTRLS_DOWN),EP(BCTRLS_LEFT),EP(BCTRLS_RIGHT), ""} ;
|
||||||
|
|
||||||
static const char * TAG = "audio controls";
|
static const char * TAG = "audio controls";
|
||||||
static actrls_config_t *json_config;
|
static actrls_config_t *json_config;
|
||||||
@@ -125,131 +131,186 @@ esp_err_t actrls_init(int n, const actrls_config_t *config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actrls_action_e actrls_parse_action_json(const char * name){
|
actrls_action_e actrls_parse_action_json(const char * name){
|
||||||
for(int i=0;actrls_action_s[i][0]!='\0' && i<ACTRLS_MAX;i++){
|
|
||||||
|
for(int i=0;i<ACTRLS_MAX && actrls_action_s[i][0]!='\0' ;i++){
|
||||||
if(!strcmp(actrls_action_s[i], name)){
|
if(!strcmp(actrls_action_s[i], name)){
|
||||||
return (actrls_action_e) i;
|
return (actrls_action_e) i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ACTRLS_NONE;
|
return ACTRLS_NONE;
|
||||||
}
|
}
|
||||||
esp_err_t actrls_parse_config_map(const cJSON * member, actrls_config_t *cur_config){
|
|
||||||
const actrls_config_map_t * map=NULL;
|
esp_err_t actrls_process_int (const cJSON * member, actrls_config_t *cur_config,uint32_t offset){
|
||||||
esp_err_t err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
char *config_pointer = (char*) cur_config;
|
ESP_LOGD(TAG,"Processing int member");
|
||||||
cJSON *button_action;
|
int *value = (int*)((char*) cur_config + offset);
|
||||||
|
*value = member->valueint;
|
||||||
char * string = cJSON_Print(member);
|
return err;
|
||||||
ESP_LOGD(TAG, "Processing structure member json : %s", string);
|
|
||||||
free(string);
|
|
||||||
|
|
||||||
for(int i = 0;!map && actrls_config_map[i].type!=ACTRLS_MAP_END ;i++){
|
|
||||||
if(!strcmp(member->string, actrls_config_map[i].member)){
|
|
||||||
map = &actrls_config_map[i];
|
|
||||||
}
|
}
|
||||||
}
|
esp_err_t actrls_process_type (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
|
||||||
|
esp_err_t err = ESP_OK;
|
||||||
if(!map){
|
ESP_LOGD(TAG,"Processing type member");
|
||||||
ESP_LOGE(TAG,"Unknown structure member [%s]", member->string?member->string:"");
|
int *value = (int *)((char*) cur_config + offset);
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGD(TAG,
|
|
||||||
"Found map type %d at structure offset %u",
|
|
||||||
map->type, map->offset);
|
|
||||||
void *value = (config_pointer + map->offset);
|
|
||||||
switch (map->type) {
|
|
||||||
case ACTRLS_MAP_TYPE:
|
|
||||||
if (member->type == cJSON_String) {
|
if (member->type == cJSON_String) {
|
||||||
*(int*) value =
|
*value =
|
||||||
!strcmp(member->valuestring,
|
!strcmp(member->valuestring,
|
||||||
"BUTTON_LOW") ?
|
"BUTTON_LOW") ?
|
||||||
BUTTON_LOW : BUTTON_HIGH;
|
BUTTON_LOW : BUTTON_HIGH;
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG,
|
ESP_LOGE(TAG,
|
||||||
"Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
|
"Button type value expected string value of BUTTON_LOW or BUTTON_HIGH, none found");
|
||||||
|
err=ESP_FAIL;
|
||||||
}
|
}
|
||||||
break;
|
return err;
|
||||||
case ACTRLS_MAP_INT:
|
|
||||||
*(int*) value = member->valueint;
|
|
||||||
break;
|
|
||||||
case ACTRLS_MAP_BOOL:
|
|
||||||
*(bool*) value = cJSON_IsTrue(member);
|
|
||||||
break;
|
|
||||||
case ACTRLS_MAP_ACTION:
|
|
||||||
button_action= cJSON_GetObjectItemCaseSensitive(
|
|
||||||
member, "pressed");
|
|
||||||
if (button_action != NULL) {
|
|
||||||
((actrls_action_e*) value)[0] =
|
|
||||||
actrls_parse_action_json(
|
|
||||||
button_action->valuestring);
|
|
||||||
}
|
}
|
||||||
button_action = cJSON_GetObjectItemCaseSensitive(
|
|
||||||
member, "released");
|
|
||||||
if (button_action != NULL) {
|
|
||||||
((actrls_action_e*) value)[1] =
|
|
||||||
actrls_parse_action_json(
|
|
||||||
button_action->valuestring);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
esp_err_t actrls_process_bool (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
|
||||||
break;
|
esp_err_t err = ESP_OK;
|
||||||
|
if(!member) {
|
||||||
|
ESP_LOGE(TAG,"Null json member pointer!");
|
||||||
|
err = ESP_FAIL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ESP_LOGD(TAG,"Processing bool member ");
|
||||||
|
if(cJSON_IsBool(member)){
|
||||||
|
bool*value = (bool*)((char*) cur_config + offset);
|
||||||
|
*value = cJSON_IsTrue(member);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ESP_LOGE(TAG,"Member %s is not a boolean", member->string?member->string:"unknown");
|
||||||
|
err = ESP_FAIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
esp_err_t actrls_process_action (const cJSON * member, actrls_config_t *cur_config, uint32_t offset){
|
||||||
|
esp_err_t err = ESP_OK;
|
||||||
|
cJSON * button_action= cJSON_GetObjectItemCaseSensitive(member, "pressed");
|
||||||
|
actrls_action_e*value = (actrls_action_e*)((char *)cur_config + offset);
|
||||||
|
if (button_action != NULL) {
|
||||||
|
value[0] = actrls_parse_action_json( button_action->valuestring);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
ESP_LOGW(TAG,"Action pressed not found in json structure");
|
||||||
|
err = ESP_FAIL;
|
||||||
|
}
|
||||||
|
button_action = cJSON_GetObjectItemCaseSensitive(member, "released");
|
||||||
|
if (button_action != NULL) {
|
||||||
|
value[1] = actrls_parse_action_json( button_action->valuestring);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
ESP_LOGW(TAG,"Action released not found in json structure");
|
||||||
|
err = ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
esp_err_t actrls_process_member(const cJSON * member, actrls_config_t *cur_config) {
|
||||||
|
esp_err_t err = ESP_FAIL;
|
||||||
|
const actrls_config_map_t * h=actrls_config_map;
|
||||||
|
|
||||||
|
char * str = cJSON_Print(member);
|
||||||
|
while (h->handler && strcmp(member->string, h->member)) { h++; }
|
||||||
|
|
||||||
|
if (h->handler) {
|
||||||
|
ESP_LOGD(TAG,"found handler for member %s, json value %s", h->member,str?str:"");
|
||||||
|
err = h->handler(member, cur_config, h->offset);
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(TAG, "Unknown json structure member : %s", str?str:"");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(str) free(str);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t actrls_process_button(const cJSON * button, actrls_config_t *cur_config) {
|
||||||
|
esp_err_t err= ESP_FAIL;
|
||||||
|
const cJSON *member;
|
||||||
|
|
||||||
|
cJSON_ArrayForEach(member, button)
|
||||||
|
{
|
||||||
|
ESP_LOGD(TAG,"Processing member %s. ", member->string);
|
||||||
|
esp_err_t loc_err = actrls_process_member(member, cur_config);
|
||||||
|
err = (err == ESP_OK) ? loc_err : err;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
actrls_config_t * actrls_init_alloc_structure(const cJSON *buttons){
|
||||||
|
int member_count = 0;
|
||||||
|
const cJSON *button;
|
||||||
|
actrls_config_t * json_config=NULL;
|
||||||
|
ESP_LOGD(TAG,"Counting the number of buttons definition");
|
||||||
|
cJSON_ArrayForEach(button, buttons) {
|
||||||
|
member_count++;
|
||||||
|
}
|
||||||
|
ESP_LOGD(TAG, "config contains %u button definitions",
|
||||||
|
member_count);
|
||||||
|
if (member_count != 0) {
|
||||||
|
json_config = malloc(sizeof(actrls_config_t) * member_count);
|
||||||
|
if(json_config){
|
||||||
|
memset(json_config, 0x00, sizeof(actrls_config_t) * member_count);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ESP_LOGE(TAG,"Unable to allocate memory to hold configuration for %u buttons ",member_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ESP_LOGE(TAG,"No button found in configuration structure");
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_config;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************************
|
/****************************************************************************************
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
esp_err_t actrls_init_json(const char *config) {
|
esp_err_t actrls_init_json(const char *config) {
|
||||||
esp_err_t err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
|
|
||||||
actrls_config_t *cur_config;
|
actrls_config_t *cur_config;
|
||||||
|
|
||||||
cJSON *buttons = cJSON_Parse(config);
|
|
||||||
if (buttons != NULL) {
|
|
||||||
if (cJSON_IsArray(buttons)) {
|
|
||||||
int member_count = 0;
|
|
||||||
const cJSON *button;
|
const cJSON *button;
|
||||||
const cJSON *member;
|
ESP_LOGD(TAG,"Parsing JSON structure ");
|
||||||
cJSON_ArrayForEach(button, buttons)
|
cJSON *buttons = cJSON_Parse(config);
|
||||||
{
|
if (buttons == NULL) {
|
||||||
member_count++;
|
ESP_LOGE(TAG,"JSON Parsing failed for %s", config);
|
||||||
|
err = ESP_FAIL;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "config contains %u button definitions",
|
else {
|
||||||
member_count);
|
ESP_LOGD(TAG,"Json parsing completed");
|
||||||
if (member_count == 0) {
|
if (cJSON_IsArray(buttons)) {
|
||||||
|
ESP_LOGD(TAG,"configuration is an array as expected");
|
||||||
|
cur_config = json_config = actrls_init_alloc_structure(buttons);
|
||||||
|
if(!cur_config) {
|
||||||
|
ESP_LOGE(TAG,"Config buffer was empty. ");
|
||||||
|
cJSON_Delete(buttons);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
json_config = malloc(sizeof(actrls_config_t) * member_count);
|
ESP_LOGD(TAG,"Processing button definitions. ");
|
||||||
memset(json_config, 0x00, sizeof(actrls_config_t) * member_count);
|
cJSON_ArrayForEach(button, buttons){
|
||||||
cur_config = json_config;
|
char * str = cJSON_Print(button);
|
||||||
|
ESP_LOGD(TAG,"Processing %s. ", str?str:"");
|
||||||
cJSON_ArrayForEach(button, buttons)
|
if(str){
|
||||||
{
|
free(str);
|
||||||
esp_err_t loc_err = ESP_OK;
|
|
||||||
cJSON_ArrayForEach(member, button)
|
|
||||||
{
|
|
||||||
actrls_parse_config_map(member, cur_config);
|
|
||||||
err = err == ESP_OK ? loc_err : err;
|
|
||||||
}
|
}
|
||||||
|
esp_err_t loc_err = actrls_process_button(button, cur_config);
|
||||||
|
err = (err == ESP_OK) ? loc_err : err;
|
||||||
if (loc_err == ESP_OK) {
|
if (loc_err == ESP_OK) {
|
||||||
button_create((void*) cur_config, cur_config->gpio,
|
button_create((void*) cur_config, cur_config->gpio,cur_config->type, cur_config->pull,cur_config->debounce,
|
||||||
cur_config->type, cur_config->pull,cur_config->debounce,
|
control_handler, cur_config->long_press, cur_config->shifter_gpio);
|
||||||
control_handler, cur_config->long_press,
|
|
||||||
cur_config->shifter_gpio);
|
|
||||||
}
|
}
|
||||||
cur_config++;
|
cur_config++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
cJSON_Delete(buttons);
|
ESP_LOGE(TAG,"Invalid configuration; array is expected and none received in %s ", config);
|
||||||
} else {
|
}
|
||||||
err = ESP_FAIL;
|
cJSON_Delete(buttons);
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -358,12 +358,15 @@ void app_main()
|
|||||||
bypass_wifi_manager=(strcmp(bypass_wm,"1")==0 ||strcasecmp(bypass_wm,"y")==0);
|
bypass_wifi_manager=(strcmp(bypass_wm,"1")==0 ||strcasecmp(bypass_wm,"y")==0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ESP_LOGD(TAG,"Getting audio control mapping ");
|
||||||
char *actrls_brd = config_alloc_get_default(NVS_TYPE_STR, "actrls_brd", NULL, 0);
|
char *actrls_brd = config_alloc_get_default(NVS_TYPE_STR, "actrls_brd", NULL, 0);
|
||||||
if (actrls_brd) {
|
if (actrls_brd) {
|
||||||
if(actrls_brd[0] !='\0'){
|
if(actrls_brd[0] !='\0'){
|
||||||
ESP_LOGD(TAG,"Initializing audio control buttons board type %s", actrls_brd);
|
ESP_LOGD(TAG,"Initializing audio control buttons type %s", actrls_brd);
|
||||||
char *actrls_brd_json = config_alloc_get_default(NVS_TYPE_STR, actrls_brd, NULL, 0);
|
char *actrls_brd_json = config_alloc_get_default(NVS_TYPE_STR, actrls_brd, NULL, 0);
|
||||||
|
|
||||||
if(actrls_brd_json){
|
if(actrls_brd_json){
|
||||||
|
ESP_LOGD(TAG,"with config JSON as follow: %s", actrls_brd_json );
|
||||||
actrls_init_json(actrls_brd_json);
|
actrls_init_json(actrls_brd_json);
|
||||||
free(actrls_brd_json);
|
free(actrls_brd_json);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user