From 1aa5a65601669dc33c1ce8729ca607d847985a44 Mon Sep 17 00:00:00 2001 From: Yordan Dimitrov Date: Sat, 7 Jul 2018 11:07:58 +0300 Subject: Setting up a JSON configuration file to change LED paths without recompiling v1 - the change allows the paths to the LED folders to be changed without having to recompile the widget - the JSON file itself is included in this commit with the name "hvac.json" - the location which the parser expects the configuration file to be in is /etc v2 - optimised code in terms of opening and closing paths for writing - put afb header definitions together v3 - removed all caps warning notifications Change-Id: If075c6650d8802849cbd706be4adbca9f1996c9e Signed-off-by: Yordan Dimitrov --- binding/hvac-demo-binding.c | 118 ++++++++++++++++++++++++++++++-------------- hvac.json | 7 +++ 2 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 hvac.json diff --git a/binding/hvac-demo-binding.c b/binding/hvac-demo-binding.c index b4f922a..a8993a1 100644 --- a/binding/hvac-demo-binding.c +++ b/binding/hvac-demo-binding.c @@ -29,14 +29,14 @@ #include #include #include - #include #define AFB_BINDING_VERSION 2 +#include + #define RED "/sys/class/leds/blinkm-3-9-red/brightness" -#define BLUE "/sys/class/leds/blinkm-3-9-blue/brightness" #define GREEN "/sys/class/leds/blinkm-3-9-green/brightness" -#include +#define BLUE "/sys/class/leds/blinkm-3-9-blue/brightness" #define CAN_DEV "vcan0" @@ -134,6 +134,16 @@ struct can_handler { struct sockaddr_can txAddress; }; +struct led_paths { + char *red; + char *green; + char *blue; +}led_paths_values = { + .red = RED, + .green = GREEN, + .blue = BLUE +}; + static struct can_handler can_handler = { .socket = -1, .simulation = false, .send_msg = "SENDING CAN FRAME"}; static int open_can_dev_helper() @@ -233,40 +243,52 @@ static uint8_t read_fanspeed() } /* - * @brief Writing to LED for both temperature sliders + * @param: None + * + * @brief: Parse JSON configuration file for blinkm path */ - -static int temp_write_led() +static int parse_config() { - int rc = 0, red_flag, blue_flag, green_flag, red_value, green_value, blue_value; - int right_temp; - int left_temp; + struct json_object *ledtemp = NULL; + struct json_object *jobj = json_object_from_file("/etc/hvac.json"); - // /sys/class/leds/blinkm-3-9-red/brightness - FILE* r = fopen(RED, "w"); - if (r == NULL) { - AFB_ERROR("Unable to open RED path for writing"); - red_flag = 1; + // Check if .json file has been parsed as a json_object + if (!jobj) { + AFB_ERROR("JSON file could not be opened!\n"); + return 1; } - // /sys/class/leds/blinkm-3-9-green/brightness - FILE* g = fopen(GREEN, "w"); - if (g == NULL) { - AFB_ERROR("Unable to open GREEN path for writing"); - green_flag = 1; + // Check if json_object with key "ledtemp" has been found in .json file + if (!json_object_object_get_ex(jobj, "ledtemp", &ledtemp)){ + AFB_ERROR("Key not found!\n"); + return 1; } - // /sys/class/leds/blinkm-3-9-blue/brightness - FILE* b = fopen(BLUE, "w"); - if (b == NULL) { - AFB_ERROR("Unable to open BLUE path for writing"); - blue_flag = 1; + // Extract directory paths for each LED colour + json_object_object_foreach(ledtemp, key, value) { + if (strcmp(key, "red") == 0) { + led_paths_values.red = json_object_get_string(value); + } else if (strcmp(key, "green") == 0) { + led_paths_values.green = json_object_get_string(value); + } else if (strcmp(key, "blue") == 0) { + led_paths_values.blue = json_object_get_string(value); + } } - if (red_flag || green_flag || blue_flag) - { - rc = 1; - } + // return 0 if all succeeded + return 0; + +} + +/* + * @brief Writing to LED for both temperature sliders + */ +static int temp_write_led() +{ + + int rc = -1, red_value, green_value, blue_value; + int right_temp; + int left_temp; left_temp = read_temp_left_led() - 15; right_temp = read_temp_right_led() - 15; @@ -275,16 +297,38 @@ static int temp_write_led() red_value = (degree_colours[left_temp].rgb[0] + degree_colours[right_temp].rgb[0]) / 2; green_value = (degree_colours[left_temp].rgb[1] + degree_colours[right_temp].rgb[1]) / 2; blue_value = (degree_colours[left_temp].rgb[2] + degree_colours[right_temp].rgb[2]) / 2; + + // default path: /sys/class/leds/blinkm-3-9-red/brightness + FILE* r = fopen(led_paths_values.red, "w"); + if(r){ + fprintf(r, "%d", red_value); + fclose(r); + } else { + AFB_ERROR("Unable to open red LED path!\n"); + return -1; + } - // Writes to LED file the specific colour - fprintf(r, "%d", red_value); - fprintf(g, "%d", green_value); - fprintf(b, "%d", blue_value); - fclose(r); - fclose(g); - fclose(b); - return rc; + // default path: /sys/class/leds/blinkm-3-9-green/brightness + FILE* g = fopen(led_paths_values.green, "w"); + if(g){ + fprintf(g, "%d", green_value); + fclose(g); + } else { + AFB_ERROR("Unable to open green LED path!\n"); + return -1; + } + + // default path: /sys/class/leds/blinkm-3-9-blue/brightness + FILE* b = fopen(led_paths_values.blue, "w"); + if(b){ + fprintf(b, "%d", blue_value); + fclose(b); + } else { + AFB_ERROR("Unable to open blue LED path!\n"); + return -1; + } + return 0; } /* @@ -655,6 +699,8 @@ int bindingServicePreInit(struct afb_service service) int bindingServiceInit(struct afb_service service) { event = afb_daemon_make_event("language"); + if(parse_config() != 0) + AFB_WARNING("Default values are being used!\n"); if(afb_daemon_require_api("identity", 1)) return -1; return afb_service_call_sync("identity", "subscribe", NULL, NULL); diff --git a/hvac.json b/hvac.json new file mode 100644 index 0000000..6e7d0db --- /dev/null +++ b/hvac.json @@ -0,0 +1,7 @@ +{ + "ledtemp": { + "red": "/sys/class/leds/blinkm-3-9-red/brightness", + "green": "/sys/class/leds/blinkm-3-9-green/brightness", + "blue": "/sys/class/leds/blinkm-3-9-blue/brightness" + } +} \ No newline at end of file -- cgit 1.2.3-korg