From 2e1b03c431759d2169a05384cdca43547abfb5be Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 19 Apr 2018 15:52:20 +0900 Subject: [PATCH 01/16] Add .gitignore Change-Id: Ic21080f08ae17cc5983c3b79708d72067d7ad136 Signed-off-by: Hyotaek Shim --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9306ae6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +cscope.files +cscope.out +tags -- 2.7.4 From 482bb923f664faa69fa784f0cfddd4cfe8e97042 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 19 Apr 2018 14:34:09 +0200 Subject: [PATCH 02/16] Change sdb+acm configuration's idProduct MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I8b8381c910da9d7ce4641515389ca2b1c0b8fb3b Signed-off-by: Paweł Szewczyk --- hw/usb_gadget/usb_gadget.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/usb_gadget/usb_gadget.c b/hw/usb_gadget/usb_gadget.c index f7d9473..ad4513d 100755 --- a/hw/usb_gadget/usb_gadget.c +++ b/hw/usb_gadget/usb_gadget.c @@ -231,7 +231,7 @@ static int simple_id_to_gadget(struct usb_gadget_id *gadget_id, functions[0][0] = USB_FUNCTION_ACM; functions[0][1] = USB_FUNCTION_SDB; functions[0][2] = 0; - gadget->attrs.idProduct = 0x6866; + gadget->attrs.idProduct = 0x6860; break; case USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB: n_configs = 1; -- 2.7.4 From d1bfb925ae2e93fb11c989563a1fd3442bd56844 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Tue, 15 May 2018 17:10:05 +0200 Subject: [PATCH 03/16] usb_gadget: Retrieve device serial number MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Serial number is retrieved from /sys/firmware/devicetree/base/serial-number file. Change-Id: I3e8251bd1ce916b72d45102017b3533420ef9f54 Signed-off-by: Paweł Szewczyk --- hw/usb_gadget/usb_gadget.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/hw/usb_gadget/usb_gadget.c b/hw/usb_gadget/usb_gadget.c index ad4513d..d1e6704 100755 --- a/hw/usb_gadget/usb_gadget.c +++ b/hw/usb_gadget/usb_gadget.c @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -122,11 +123,36 @@ out: return -ENOMEM; } +#define SERIAL_FILE_PATH "/sys/firmware/devicetree/base/serial-number" +#define LINE_LEN 64 + +static int get_device_serial(char **out) +{ + FILE *fp; + char *line, *p; + + fp = fopen(SERIAL_FILE_PATH, "r"); + if (!fp) + return -1; + + line = malloc(LINE_LEN); + p = fgets(line, LINE_LEN, fp); + fclose(fp); + if (p == NULL) { + free(line); + return -1; + } + + *out = p; + return 0; +} + static int alloc_default_gadget(struct usb_gadget **_gadget) { struct usb_gadget *gadget; struct usb_gadget_strings *strs; struct usb_configuration **configs; + int ret; gadget = zalloc(sizeof(*gadget)); if (!gadget) @@ -143,7 +169,9 @@ static int alloc_default_gadget(struct usb_gadget **_gadget) strs[0].lang_code = 0x409; strs[0].manufacturer = strdup(DEFAULT_MANUFACTURER); strs[0].product = strdup(DEFAULT_PRODUCT); - strs[0].serial = strdup(DEFAULT_SERIAL); + ret = get_device_serial(&strs[0].serial); + if (ret < 0) + strs[0].serial = strdup(DEFAULT_SERIAL); if (!strs[0].manufacturer || !strs[0].product || !strs[0].serial) goto free_strs; -- 2.7.4 From 95a0642c57ee597753fc2327b94dfbbe362154b8 Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Tue, 19 Jun 2018 17:17:10 +0900 Subject: [PATCH 04/16] Add display_get_state to remove dependency with enlightenment Change-Id: I826e14589707f1a2e58c6105b7658ddbb4d8a108 Signed-off-by: lokilee73 --- hw/display/display.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/hw/display/display.c b/hw/display/display.c index 0eb19cb..a38ebd0 100755 --- a/hw/display/display.c +++ b/hw/display/display.c @@ -31,6 +31,10 @@ #define BACKLIGHT_PATH "/sys/class/backlight/s6e8fa0" #endif +#ifndef LCD_PATH +#define LCD_PATH "/sys/class/drm/card0" +#endif + #define MAX_BRIGHTNESS_TEMP 100 static int brightness_temp; @@ -100,6 +104,56 @@ static int display_set_brightness(int brightness) return 0; } +static int display_get_state(enum display_state *state) +{ + int r; + char status[64]; + + // PANEL + r = sys_get_str(LCD_PATH"/card0-DSI-1/enabled", status, sizeof(status)); + if (r < 0) { + _E("fail to get panel (errno:%d)", r); + return r; + } + + if (!strncmp(status, "enabled", 7)) { + r = sys_get_str(LCD_PATH"/card0-DSI-1/dpms", status, sizeof(status)); + if (r < 0) { + _E("fail to get state (errno:%d)", r); + return r; + } + goto out; + } + + //HDMI + r = sys_get_str(LCD_PATH"/card0-HDMI-A-1/enabled", status, sizeof(status)); + if (r < 0) { + _E("fail to get hdmi (errno:%d)", r); + return r; + } + + if (!strncmp(status, "enabled", 7)) { + r = sys_get_str(LCD_PATH"/card0-HDMI-A-1/dpms", status, sizeof(status)); + if (r < 0) { + _E("fail to get state (errno:%d)", r); + return r; + } + } + + //Add here for more LCD device + +out: + //remap LCD state + if (!strncmp(status, "On", 2)) { + *state = DISPLAY_ON; + } else if (!strncmp(status, "Off", 3)) { + *state = DISPLAY_OFF; + } else + *state = -EINVAL; + + return 0; +} + static int display_open(struct hw_info *info, const char *id, struct hw_common **common) { @@ -116,6 +170,7 @@ static int display_open(struct hw_info *info, display_dev->get_max_brightness = display_get_max_brightness; display_dev->get_brightness = display_get_brightness; display_dev->set_brightness = display_set_brightness; + display_dev->get_state = display_get_state; *common = (struct hw_common *)display_dev; return 0; -- 2.7.4 From a767db5c43da65675887deaaabcf89a1db338b31 Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Wed, 27 Jun 2018 11:34:22 +0900 Subject: [PATCH 05/16] Modify blinkm_led_stop_script peripherial_i2c_byte_write() was removed, So replaced it with peripherial_i2c_write(). Change-Id: I4fa3388489e23fdedc7e2c95c23272e65c6a949a Signed-off-by: lokilee73 --- hw/led/led.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) mode change 100644 => 100755 hw/led/led.c diff --git a/hw/led/led.c b/hw/led/led.c old mode 100644 new mode 100755 index 547c894..130a8e7 --- a/hw/led/led.c +++ b/hw/led/led.c @@ -73,7 +73,10 @@ static uint8_t off_cmd[4] = { SET_CMD_CODE, 0x00, 0x00, 0x00 }; static void blinkm_led_stop_script(peripheral_i2c_h handle) { - peripheral_i2c_write_byte(handle, STOP_SCRIPT_CMD); + uint8_t data[1] = {STOP_SCRIPT_CMD}; + uint32_t length = 1; + + peripheral_i2c_write(handle, data, length); } /** -- 2.7.4 From d6898ed6ad5006718c119e103a1a0e967fee7231 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 28 Jun 2018 12:27:53 +0200 Subject: [PATCH 06/16] usb_gadget: Move common code to hwcommon MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Icc1e965e889d9d73667198120667bd55fbea0df7 Signed-off-by: Paweł Szewczyk --- hw/usb_gadget/usb_gadget.c | 394 --------------------------------------------- 1 file changed, 394 deletions(-) diff --git a/hw/usb_gadget/usb_gadget.c b/hw/usb_gadget/usb_gadget.c index d1e6704..470a984 100755 --- a/hw/usb_gadget/usb_gadget.c +++ b/hw/usb_gadget/usb_gadget.c @@ -20,400 +20,6 @@ #include -#include -#include -#include -#include - -#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) -#define zalloc(amount) calloc(1, amount) - -/* Based on slp-gadget and initial version of USB HAL by Taeyoung Kim */ -#define DEFAULT_VID 0x04e8 -#define DEFAULT_PID 0x6860 -#define DEFAULT_BCD_DEVICE 0x0100 - -#define DEFAULT_LANG 0x409 /* US_en */ -#define DEFAULT_MANUFACTURER "Samsung" -#define DEFAULT_PRODUCT "TIZEN" -#define DEFAULT_SERIAL "01234TEST" - -#define DEFAULT_BMATTRIBUTES ((1 << 7) | (1 << 6)) -#define DEFAULT_MAX_POWER 500 - -static void simple_cleanup_config(struct usb_configuration *config) -{ - int i; - - if (!config) - return; - - if (config->strs) { - for (i = 0; config->strs[i].lang_code; ++i) - free(config->strs[i].config_str); - - free(config->strs); - } - - /* - * Each function will be free later, - * for now we cleanup only pointers. - */ - if (config->funcs) - free(config->funcs); - - free(config); -} - -static void simple_cleanup_gadget(struct usb_gadget *gadget) -{ - int i; - - if (!gadget) - return; - - if (gadget->strs) { - for (i = 0; gadget->strs[i].lang_code; ++i) { - free(gadget->strs[i].manufacturer); - free(gadget->strs[i].product); - free(gadget->strs[i].serial); - } - free(gadget->strs); - } - - if (gadget->configs) { - for (i = 0; gadget->configs[i]; ++i) - simple_cleanup_config(gadget->configs[i]); - - free(gadget->configs); - } - - if (gadget->funcs) { - for (i = 0; gadget->funcs[i]; ++i) - gadget->funcs[i]->free_func(gadget->funcs[i]); - - free(gadget->funcs); - } - - free(gadget); -} - -static int alloc_default_config(struct usb_configuration **_config) -{ - struct usb_configuration *config; - - config = zalloc(sizeof(*config)); - if (!config) - goto out; - - config->strs = calloc(1, sizeof(*config->strs)); - if (!config->strs) - goto free_config; - - config->attrs.bmAttributs = DEFAULT_BMATTRIBUTES; - config->attrs.MaxPower = DEFAULT_MAX_POWER; - - *_config = config; - - return 0; - -free_config: - free(config); -out: - return -ENOMEM; -} - -#define SERIAL_FILE_PATH "/sys/firmware/devicetree/base/serial-number" -#define LINE_LEN 64 - -static int get_device_serial(char **out) -{ - FILE *fp; - char *line, *p; - - fp = fopen(SERIAL_FILE_PATH, "r"); - if (!fp) - return -1; - - line = malloc(LINE_LEN); - p = fgets(line, LINE_LEN, fp); - fclose(fp); - if (p == NULL) { - free(line); - return -1; - } - - *out = p; - return 0; -} - -static int alloc_default_gadget(struct usb_gadget **_gadget) -{ - struct usb_gadget *gadget; - struct usb_gadget_strings *strs; - struct usb_configuration **configs; - int ret; - - gadget = zalloc(sizeof(*gadget)); - if (!gadget) - goto out; - - gadget->attrs.idVendor = DEFAULT_VID; - gadget->attrs.idProduct = DEFAULT_PID; - gadget->attrs.bcdDevice = DEFAULT_BCD_DEVICE; - - strs = calloc(2, sizeof(*strs)); - if (!strs) - goto free_gadget; - - strs[0].lang_code = 0x409; - strs[0].manufacturer = strdup(DEFAULT_MANUFACTURER); - strs[0].product = strdup(DEFAULT_PRODUCT); - ret = get_device_serial(&strs[0].serial); - if (ret < 0) - strs[0].serial = strdup(DEFAULT_SERIAL); - - if (!strs[0].manufacturer || !strs[0].product || !strs[0].serial) - goto free_strs; - - gadget->strs = strs; - - /* slp-gadget use max 2 confiuration and NULL termination */ - configs = calloc(3, sizeof(*configs)); - if (!configs) - goto free_strs; - - gadget->configs = configs; - *_gadget = gadget; - - return 0; - -free_strs: - free(strs[0].manufacturer); - free(strs[0].product); - free(strs[0].serial); - free(strs); -free_gadget: - free(gadget); -out: - return -ENOMEM; -} - -static inline struct usb_function *find_func(struct usb_gadget *gadget, - int func_id) -{ - int i; - - for (i = 0; gadget->funcs[i] && gadget->funcs[i]->id != func_id; ++i); - - return gadget->funcs[i]; -} - -static int simple_id_to_gadget(struct usb_gadget_id *gadget_id, - struct usb_gadget **_gadget) -{ - struct usb_gadget *gadget; - unsigned int n_configs = 0; - /* zero terminates */ - int functions[2][sizeof(gadget_id->function_mask)*8]; - int n_functions; - struct usb_function **funcs; - int idx, i, j; - int ret; - - if (!gadget_id || !_gadget) - return -EINVAL; - - ret = alloc_default_gadget(&gadget); - if (ret) - goto out; - - /* - * Currently all gadgets use inly single configuration but - * slp-gadget is capable to handle two of them - * - * Order of interfaces in configuration is significant - * so in this switch we sort our functions in a correct order - */ - switch (gadget_id->function_mask) { - case USB_FUNCTION_SDB: - n_configs = 1; - functions[0][0] = USB_FUNCTION_SDB; - functions[0][1] = 0; - gadget->attrs.idProduct = 0x685d; - break; - case USB_FUNCTION_MTP: - n_configs = 1; - functions[0][0] = USB_FUNCTION_MTP; - functions[0][1] = 0; - gadget->attrs.idProduct = 0x6860; - break; - case USB_FUNCTION_RNDIS: - n_configs = 1; - functions[0][0] = USB_FUNCTION_RNDIS; - functions[0][1] = 0; - gadget->attrs.idProduct = 0x6863; - break; - case USB_FUNCTION_SDB | USB_FUNCTION_ACM: - n_configs = 1; - functions[0][0] = USB_FUNCTION_ACM; - functions[0][1] = USB_FUNCTION_SDB; - functions[0][2] = 0; - gadget->attrs.idProduct = 0x6860; - break; - case USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB: - n_configs = 1; - functions[0][0] = USB_FUNCTION_MTP; - functions[0][1] = USB_FUNCTION_ACM; - functions[0][2] = USB_FUNCTION_SDB; - functions[0][3] = 0; - gadget->attrs.idProduct = 0x6860; - break; - case USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB - | USB_FUNCTION_DIAG: - n_configs = 1; - functions[0][0] = USB_FUNCTION_MTP; - functions[0][1] = USB_FUNCTION_ACM; - functions[0][2] = USB_FUNCTION_SDB; - functions[0][3] = USB_FUNCTION_DIAG; - functions[0][4] = 0; - gadget->attrs.idProduct = 0x6860; - break; - case USB_FUNCTION_RNDIS | USB_FUNCTION_SDB: - n_configs = 1; - functions[0][0] = USB_FUNCTION_RNDIS; - functions[0][1] = USB_FUNCTION_SDB; - functions[0][2] = 0; - gadget->attrs.idProduct = 0x6864; - break; - case USB_FUNCTION_RNDIS | USB_FUNCTION_SDB | USB_FUNCTION_ACM | USB_FUNCTION_DIAG: - n_configs = 1; - functions[0][0] = USB_FUNCTION_RNDIS; - functions[0][1] = USB_FUNCTION_SDB; - functions[0][2] = USB_FUNCTION_ACM; - functions[0][3] = USB_FUNCTION_DIAG; - functions[0][4] = 0; - gadget->attrs.idProduct = 0x6864; - break; - case USB_FUNCTION_RNDIS | USB_FUNCTION_DIAG: - n_configs = 1; - functions[0][0] = USB_FUNCTION_RNDIS; - functions[0][1] = USB_FUNCTION_DIAG; - functions[0][2] = 0; - gadget->attrs.idProduct = 0x6864; - break; - case USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_DM: - n_configs = 1; - functions[0][0] = USB_FUNCTION_ACM; - functions[0][1] = USB_FUNCTION_SDB; - functions[0][2] = USB_FUNCTION_DM; - functions[0][3] = 0; - gadget->attrs.idProduct = 0x6860; - break; - case USB_FUNCTION_DIAG | USB_FUNCTION_ACM | USB_FUNCTION_RMNET: - n_configs = 1; - functions[0][0] = USB_FUNCTION_DIAG; - functions[0][1] = USB_FUNCTION_ACM; - functions[0][2] = USB_FUNCTION_RMNET; - functions[0][3] = 0; - gadget->attrs.idProduct = 0x685d; - break; - }; - - if (n_configs > 2 || n_configs == 0) { - ret = -EINVAL; - goto free_gadget; - } - - n_functions = __builtin_popcount(gadget_id->function_mask); - - funcs = calloc(n_functions + 1, sizeof(*funcs)); - if (!funcs) { - ret = -ENOMEM; - goto free_gadget; - } - - gadget->funcs = funcs; - - idx = 0; - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) { - int func_id = 1 << i; - - if (!(gadget_id->function_mask & func_id)) - continue; - - ret = _available_funcs[i]->clone(_available_funcs[i], - gadget->funcs + idx); - if (ret) - goto free_functions; - ++idx; - } - - for (j = 0; j < n_configs; ++j) { - struct usb_configuration *config; - int n_funcs_in_config; - - for (i = 0; functions[j][i]; ++i); - n_funcs_in_config = i; - - ret = alloc_default_config(&config); - if (ret) - goto free_configs; - - gadget->configs[j] = config; - config->funcs = calloc(n_funcs_in_config + 1, - sizeof(void *)); - if (!config->funcs) - goto free_configs; - - for (i = 0; functions[j][i]; ++i) - config->funcs[i] = find_func(gadget, functions[j][i]); - } - - *_gadget = gadget; - return 0; -free_configs: -free_functions: -free_gadget: - simple_cleanup_gadget(gadget); -out: - return ret; -} - -static int simple_translator_open(struct hw_info *info, - const char *id, struct hw_common **common) -{ - struct usb_gadget_translator *simple_translator; - - if (!info || !common) - return -EINVAL; - - simple_translator = zalloc(sizeof(*simple_translator)); - if (!simple_translator) - return -ENOMEM; - - simple_translator->common.info = info; - simple_translator->id_to_gadget = simple_id_to_gadget; - simple_translator->cleanup_gadget = simple_cleanup_gadget; - - *common = &simple_translator->common; - return 0; -} - -static int simple_translator_close(struct hw_common *common) -{ - struct usb_gadget_translator *simple_translator; - - if (!common) - return -EINVAL; - - simple_translator = container_of(common, struct usb_gadget_translator, - common); - - free(simple_translator); - return 0; -} - HARDWARE_MODULE_STRUCTURE = { .magic = HARDWARE_INFO_TAG, .hal_version = HARDWARE_INFO_VERSION, -- 2.7.4 From 6f13e9d8a1587ce155f57094ff282d0c8bfe0c54 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 28 Jun 2018 12:58:30 +0200 Subject: [PATCH 07/16] Add board HAL MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ifcc9256bb722b32f8c3db3bdb873a4e6c88c8ef8 Signed-off-by: Paweł Szewczyk --- CMakeLists.txt | 1 + hw/board/CMakeLists.txt | 19 +++++++++++ hw/board/board.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 hw/board/CMakeLists.txt create mode 100755 hw/board/board.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fab672..3653bc0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ PROJECT(device-manager-artik C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) #ADD_SUBDIRECTORY(hw/battery) +ADD_SUBDIRECTORY(hw/board) ADD_SUBDIRECTORY(hw/display) #ADD_SUBDIRECTORY(hw/external_connection) ADD_SUBDIRECTORY(hw/led) diff --git a/hw/board/CMakeLists.txt b/hw/board/CMakeLists.txt new file mode 100644 index 0000000..5b8b5b4 --- /dev/null +++ b/hw/board/CMakeLists.txt @@ -0,0 +1,19 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(board C) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +INCLUDE(FindPkgConfig) +pkg_check_modules(usb_gadget_pkgs REQUIRED hwcommon) + +FOREACH(flag ${usb_gadget_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +ADD_LIBRARY(${PROJECT_NAME} MODULE board.c) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${usb_gadget_pkgs_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/board/board.c b/hw/board/board.c new file mode 100755 index 0000000..3c6b42d --- /dev/null +++ b/hw/board/board.c @@ -0,0 +1,91 @@ +/* + * libdevice-node + * + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include + +#include +#include +#include +#include + +#define SERIAL_FILE_PATH "/sys/firmware/devicetree/base/serial-number" +#define LINE_LEN 64 + +static int get_device_serial(char **out) +{ + FILE *fp; + char *line, *p; + + fp = fopen(SERIAL_FILE_PATH, "r"); + if (!fp) + return -1; + + line = malloc(LINE_LEN); + p = fgets(line, LINE_LEN, fp); + fclose(fp); + if (p == NULL) { + free(line); + return -1; + } + + *out = p; + return 0; +} + +static int board_open(struct hw_info *info, + const char *id, struct hw_common **common) +{ + struct hw_board *b; + + if (!info || !common) + return -EINVAL; + + b = calloc(1, sizeof(*b)); + if (!b) + return -ENOMEM; + + b->common.info = info; + b->get_device_serial = get_device_serial; + + *common = &b->common; + return 0; +} + +static int board_close(struct hw_common *common) +{ + struct hw_board *b; + + if (!common) + return -EINVAL; + + b = container_of(common, struct hw_board, common); + free(b); + + return 0; +} + +HARDWARE_MODULE_STRUCTURE = { + .magic = HARDWARE_INFO_TAG, + .hal_version = HARDWARE_INFO_VERSION, + .device_version = BOARD_HARDWARE_DEVICE_VERSION, + .id = BOARD_HARDWARE_DEVICE_ID, + .name = "device", + .open = board_open, + .close = board_close, +}; -- 2.7.4 From 963911e53cfc6883e0087b24703e0e858754cc6a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 28 Jun 2018 12:20:28 +0200 Subject: [PATCH 08/16] Move systemd-related code to common library MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I3de92bdc9ec5308cff875f30c22d70d307e00002 Signed-off-by: Paweł Szewczyk --- hw/usb_cfs_client/CMakeLists.txt | 2 +- hw/usb_cfs_client/usb_cfs_client.c | 121 +---------------------------- hw/usb_client/CMakeLists.txt | 2 +- hw/usb_client/usb_client.c | 59 +------------- packaging/device-manager-plugin-artik.spec | 2 - 5 files changed, 4 insertions(+), 182 deletions(-) diff --git a/hw/usb_cfs_client/CMakeLists.txt b/hw/usb_cfs_client/CMakeLists.txt index 15e7cbc..fefe1dc 100644 --- a/hw/usb_cfs_client/CMakeLists.txt +++ b/hw/usb_cfs_client/CMakeLists.txt @@ -4,7 +4,7 @@ PROJECT(usb_cfs_client C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) INCLUDE(FindPkgConfig) -pkg_check_modules(usb_cfs_client_pkgs REQUIRED hwcommon dlog glib-2.0 libsystemd libusbgx) +pkg_check_modules(usb_cfs_client_pkgs REQUIRED hwcommon dlog glib-2.0 libusbgx) FOREACH(flag ${usb_cfs_client_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/hw/usb_cfs_client/usb_cfs_client.c b/hw/usb_cfs_client/usb_cfs_client.c index c7875a4..28b10a0 100644 --- a/hw/usb_cfs_client/usb_cfs_client.c +++ b/hw/usb_cfs_client/usb_cfs_client.c @@ -17,6 +17,7 @@ */ #include +#include #include "../shared.h" @@ -28,7 +29,6 @@ #include #include #include -#include #include @@ -587,125 +587,6 @@ static int cfs_set_gadget_strs(struct cfs_client *cfs_client, return ret; } -#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1" -#define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1" -#define SYSTEMD_DBUS_MANAGER_IFACE "org.freedesktop.systemd1.Manager" - -#define SYSTEMD_SOCKET_SUFFIX ".socket" -#define MAX_SOCKET_NAME 1024 - -struct bus_ctx { - const char *unit; - sd_event *loop; -}; - -static int socket_started(sd_bus_message *m, void *userdata, - sd_bus_error *ret_error) -{ - struct bus_ctx *ctx = userdata; - char *signal_unit; - int ret; - - ret = sd_bus_message_read(m, "uoss", NULL, NULL, &signal_unit, NULL); - if (ret < 0) { - sd_event_exit(ctx->loop, ret); - return 0; - } - - if (!strcmp(signal_unit, ctx->unit)) - sd_event_exit(ctx->loop, 0); - - return 0; -} - -static int systemd_unit_interface_sync(const char *method, const char *unit, - bool wait) -{ - sd_bus *bus = NULL; - sd_event *loop = NULL; - struct bus_ctx ctx; - int ret; - - ret = sd_bus_open_system(&bus); - if (ret < 0) - return ret; - - if (wait) { - ret = sd_event_new(&loop); - if (ret < 0) - goto unref_bus; - - ctx.loop = loop; - ctx.unit = unit; - - ret = sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL); - if (ret < 0) - goto unref_loop; - - ret = sd_bus_add_match(bus, NULL, - "type='signal'," - "sender='" SYSTEMD_DBUS_SERVICE "'," - "interface='" SYSTEMD_DBUS_MANAGER_IFACE "'," - "member='JobRemoved'," - "path_namespace='" SYSTEMD_DBUS_PATH "'", - socket_started, - &ctx); - if (ret < 0) - goto unref_loop; - } - - - ret = sd_bus_call_method(bus, - SYSTEMD_DBUS_SERVICE, - SYSTEMD_DBUS_PATH, - SYSTEMD_DBUS_MANAGER_IFACE, - method, - NULL, - NULL, - "ss", - unit, - "replace"); - if (ret < 0) - goto unref_loop; - - if (wait) - ret = sd_event_loop(loop); - -unref_loop: - if (wait) - sd_event_unref(loop); -unref_bus: - sd_bus_unref(bus); - return ret; -} - -static int systemd_start_socket(const char *socket_name) -{ - char unit[MAX_SOCKET_NAME]; - int ret; - - ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SOCKET_SUFFIX, - socket_name); - if (ret < 0 || ret >= sizeof(unit)) - return -ENAMETOOLONG; - - - return systemd_unit_interface_sync("StartUnit", unit, true); -} - -static int systemd_stop_socket(const char *socket_name) -{ - char unit[MAX_SOCKET_NAME]; - int ret; - - ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SOCKET_SUFFIX, - socket_name); - if (ret < 0 || ret >= sizeof(unit)) - return -ENAMETOOLONG; - - return systemd_unit_interface_sync("StopUnit", unit, false); -} - static int cfs_ensure_dir(char *path) { int ret; diff --git a/hw/usb_client/CMakeLists.txt b/hw/usb_client/CMakeLists.txt index 4a10107..8494082 100644 --- a/hw/usb_client/CMakeLists.txt +++ b/hw/usb_client/CMakeLists.txt @@ -4,7 +4,7 @@ PROJECT(usb_client C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) INCLUDE(FindPkgConfig) -pkg_check_modules(usb_client_pkgs REQUIRED hwcommon dlog glib-2.0 libusbgx libsystemd) +pkg_check_modules(usb_client_pkgs REQUIRED hwcommon dlog glib-2.0 libusbgx) FOREACH(flag ${usb_client_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/hw/usb_client/usb_client.c b/hw/usb_client/usb_client.c index 0f1effe..2857c33 100644 --- a/hw/usb_client/usb_client.c +++ b/hw/usb_client/usb_client.c @@ -17,6 +17,7 @@ */ #include +#include #include "../shared.h" @@ -24,7 +25,6 @@ #include #include #include -#include #define zalloc(amount) calloc(1, amount) @@ -63,63 +63,6 @@ /* +5 to be always big enough */ #define INT_BUF_SIZE (sizeof(int)*8 + 5) -#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1" -#define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1" -#define SYSTEMD_DBUS_MANAGER_IFACE "org.freedesktop.systemd1.Manager" - -#define SYSTEMD_SERVICE_SUFFIX ".service" -#define MAX_SERVICE_NAME 1024 - -static int systemd_unit_interface(const char *method, const char *unit) -{ - sd_bus *bus = NULL; - int r; - - r = sd_bus_open_system(&bus); - if (r < 0) - return r; - - r = sd_bus_call_method(bus, - SYSTEMD_DBUS_SERVICE, - SYSTEMD_DBUS_PATH, - SYSTEMD_DBUS_MANAGER_IFACE, - method, - NULL, - NULL, - "ss", - unit, - "replace"); - - sd_bus_unref(bus); - return r; -} - -static int systemd_start_service(const char *service_name) -{ - char unit[MAX_SERVICE_NAME]; - int ret; - - ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SERVICE_SUFFIX, - service_name); - if (ret < 0 || ret >= sizeof(unit)) - return -ENAMETOOLONG; - - return systemd_unit_interface("StartUnit", unit); -} - -static int systemd_stop_service(const char *service_name) -{ - char unit[MAX_SERVICE_NAME]; - int ret; - - ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SERVICE_SUFFIX, - service_name); - if (ret < 0 || ret >= sizeof(unit)) - return -ENAMETOOLONG; - - return systemd_unit_interface("StopUnit", unit); -} - static int get_int_from_file(char *path, int *_val, int base) { char buf[INT_BUF_SIZE]; diff --git a/packaging/device-manager-plugin-artik.spec b/packaging/device-manager-plugin-artik.spec index 7268a6a..ea99faa 100755 --- a/packaging/device-manager-plugin-artik.spec +++ b/packaging/device-manager-plugin-artik.spec @@ -15,8 +15,6 @@ BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(libudev) BuildRequires: pkgconfig(capi-system-peripheral-io) BuildRequires: pkgconfig(libusbgx) -BuildRequires: pkgconfig(libsystemd) -BuildRequires: pkgconfig(libusbgx) %description Device manager plugin artik -- 2.7.4 From 1f4cfca92c372350baea186ad3a753f674597ee6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 28 Jun 2018 12:58:23 +0200 Subject: [PATCH 09/16] Move shared files to hwcommon package MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ieafe89155a1a4b54bb5368fae9b8aef24f96a248 Signed-off-by: Paweł Szewczyk --- hw/battery/CMakeLists.txt | 2 +- hw/battery/battery.c | 2 +- hw/display/CMakeLists.txt | 2 +- hw/display/display.c | 2 +- hw/external_connection/CMakeLists.txt | 2 +- hw/external_connection/external_connection.c | 2 +- hw/led/CMakeLists.txt | 2 +- hw/led/led.c | 2 +- hw/shared.c | 129 --------------------------- hw/shared.h | 43 --------- hw/touchscreen/CMakeLists.txt | 2 +- hw/touchscreen/touchscreen.c | 2 +- hw/udev.c | 2 +- hw/usb_cfs_client/CMakeLists.txt | 2 +- hw/usb_cfs_client/usb_cfs_client.c | 3 +- hw/usb_client/CMakeLists.txt | 2 +- hw/usb_client/usb_client.c | 3 +- hw/usb_gadget/CMakeLists.txt | 2 +- 18 files changed, 16 insertions(+), 190 deletions(-) delete mode 100755 hw/shared.c delete mode 100755 hw/shared.h diff --git a/hw/battery/CMakeLists.txt b/hw/battery/CMakeLists.txt index 48ef1aa..820c168 100755 --- a/hw/battery/CMakeLists.txt +++ b/hw/battery/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE battery.c ../shared.c ../udev.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE battery.c ../udev.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${battery_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/battery/battery.c b/hw/battery/battery.c index 076a542..f16da48 100755 --- a/hw/battery/battery.c +++ b/hw/battery/battery.c @@ -25,7 +25,7 @@ #include #include -#include "../shared.h" +#include #include "../udev.h" #define BATTERY_ROOT_PATH "/sys/class/power_supply" diff --git a/hw/display/CMakeLists.txt b/hw/display/CMakeLists.txt index ebccfbe..08c293d 100755 --- a/hw/display/CMakeLists.txt +++ b/hw/display/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE display.c ../shared.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE display.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${display_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/display/display.c b/hw/display/display.c index a38ebd0..2d60b4b 100755 --- a/hw/display/display.c +++ b/hw/display/display.c @@ -24,7 +24,7 @@ #include #include -#include "../shared.h" +#include #ifndef BACKLIGHT_PATH //#define BACKLIGHT_PATH "/sys/class/backlight/s6e36w1x01-bl" diff --git a/hw/external_connection/CMakeLists.txt b/hw/external_connection/CMakeLists.txt index e700a2f..b1f2f83 100755 --- a/hw/external_connection/CMakeLists.txt +++ b/hw/external_connection/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE external_connection.c ../shared.c ../udev.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE external_connection.c ../udev.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${external_connection_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/external_connection/external_connection.c b/hw/external_connection/external_connection.c index 0a94629..f49083b 100755 --- a/hw/external_connection/external_connection.c +++ b/hw/external_connection/external_connection.c @@ -25,7 +25,7 @@ #include #include -#include "../shared.h" +#include #include "../udev.h" #define SWITCH_ROOT_PATH "/sys/devices/virtual/switch" diff --git a/hw/led/CMakeLists.txt b/hw/led/CMakeLists.txt index 6acfd7b..dfe38d5 100644 --- a/hw/led/CMakeLists.txt +++ b/hw/led/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE led.c ../shared.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE led.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${led_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") diff --git a/hw/led/led.c b/hw/led/led.c index 130a8e7..3ff670f 100755 --- a/hw/led/led.c +++ b/hw/led/led.c @@ -21,7 +21,7 @@ #include #include -#include "../shared.h" +#include #include diff --git a/hw/shared.c b/hw/shared.c deleted file mode 100755 index b6401c1..0000000 --- a/hw/shared.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * device-node - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#include -#include -#include -#include -#include -#include -#include -#include - -#define BUF_MAX 255 - -static int sys_read_buf(char *file, char *buf, int len) -{ - int fd, r; - - if (!file || !buf || len < 0) - return -EINVAL; - - fd = open(file, O_RDONLY); - if (fd == -1) - return -ENOENT; - - r = read(fd, buf, len); - close(fd); - if ((r >= 0) && (r < len)) - buf[r] = '\0'; - else - return -EIO; - - return 0; -} - -static int sys_write_buf(char *file, char *buf) -{ - int fd, r; - - if (!file || !buf) - return -EINVAL; - - fd = open(file, O_WRONLY); - if (fd == -1) - return -EPERM; - - r = write(fd, buf, strlen(buf)); - close(fd); - if (r < 0) - return -EIO; - - return 0; -} - -int sys_get_int(char *fname, int *val) -{ - char buf[BUF_MAX]; - int r; - - if (!fname || !val) - return -EINVAL; - - r = sys_read_buf(fname, buf, sizeof(buf)); - if (r < 0) - return r; - - *val = atoi(buf); - return 0; -} - -int sys_get_str(char *fname, char *str, int len) -{ - int r; - - if (!fname || !str || len < 0) - return -EINVAL; - - r = sys_read_buf(fname, str, len); - if (r < 0) - return r; - - return 0; -} - -int sys_set_int(char *fname, int val) -{ - char buf[BUF_MAX]; - int r; - - if (!fname) - return -EINVAL; - - snprintf(buf, sizeof(buf), "%d", val); - r = sys_write_buf(fname, buf); - if (r < 0) - return r; - - return 0; -} - -int sys_set_str(char *fname, char *val) -{ - int r; - - if (!fname || !val) - return -EINVAL; - - r = sys_write_buf(fname, val); - if (r < 0) - return r; - - return 0; -} diff --git a/hw/shared.h b/hw/shared.h deleted file mode 100755 index da51ca4..0000000 --- a/hw/shared.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * libdevice-node - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef __HW_DEFAULT_SHARED_H__ -#define __HW_DEFAULT_SHARED_H__ - -#define FEATURE_HARDWARE_DLOG -#ifdef FEATURE_HARDWARE_DLOG -#define LOG_TAG "HARDWARE" -#include -#define _I(fmt, args...) SLOGI(fmt, ##args) -#define _D(fmt, args...) SLOGD(fmt, ##args) -#define _E(fmt, args...) SLOGE(fmt, ##args) -#else -#define _I(x, ...) do { } while (0) -#define _D(x, ...) do { } while (0) -#define _E(x, ...) do { } while (0) -#endif - -#define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0])) - -int sys_get_int(char *fname, int *val); -int sys_get_str(char *fname, char *str, int len); -int sys_set_int(char *fname, int val); -int sys_set_str(char *fname, char *val); - -#endif diff --git a/hw/touchscreen/CMakeLists.txt b/hw/touchscreen/CMakeLists.txt index b097615..f364805 100755 --- a/hw/touchscreen/CMakeLists.txt +++ b/hw/touchscreen/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE touchscreen.c ../shared.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE touchscreen.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${touchscreen_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/touchscreen/touchscreen.c b/hw/touchscreen/touchscreen.c index fe3c97e..5fe1cc6 100755 --- a/hw/touchscreen/touchscreen.c +++ b/hw/touchscreen/touchscreen.c @@ -25,7 +25,7 @@ #include #include -#include "../shared.h" +#include #define INPUT_PATH "/sys/class/input/" #define KEY_CAPABILITIES_PATH "/device/capabilities/key" diff --git a/hw/udev.c b/hw/udev.c index 1f64b08..e0c0d13 100755 --- a/hw/udev.c +++ b/hw/udev.c @@ -23,7 +23,7 @@ #include #include #include -#include "shared.h" +#include #include "udev.h" #define EVENT_KERNEL "kernel" diff --git a/hw/usb_cfs_client/CMakeLists.txt b/hw/usb_cfs_client/CMakeLists.txt index fefe1dc..70b054d 100644 --- a/hw/usb_cfs_client/CMakeLists.txt +++ b/hw/usb_cfs_client/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE usb_cfs_client.c ../shared.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE usb_cfs_client.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${usb_cfs_client_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/usb_cfs_client/usb_cfs_client.c b/hw/usb_cfs_client/usb_cfs_client.c index 28b10a0..f77f8be 100644 --- a/hw/usb_cfs_client/usb_cfs_client.c +++ b/hw/usb_cfs_client/usb_cfs_client.c @@ -18,8 +18,7 @@ #include #include - -#include "../shared.h" +#include #include #include diff --git a/hw/usb_client/CMakeLists.txt b/hw/usb_client/CMakeLists.txt index 8494082..28bab08 100644 --- a/hw/usb_client/CMakeLists.txt +++ b/hw/usb_client/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE usb_client.c ../shared.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE usb_client.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${usb_client_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/usb_client/usb_client.c b/hw/usb_client/usb_client.c index 2857c33..6f4b6d1 100644 --- a/hw/usb_client/usb_client.c +++ b/hw/usb_client/usb_client.c @@ -18,8 +18,7 @@ #include #include - -#include "../shared.h" +#include #include #include diff --git a/hw/usb_gadget/CMakeLists.txt b/hw/usb_gadget/CMakeLists.txt index 2e28b15..039baa4 100644 --- a/hw/usb_gadget/CMakeLists.txt +++ b/hw/usb_gadget/CMakeLists.txt @@ -13,7 +13,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE usb_gadget.c ../shared.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE usb_gadget.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${usb_gadget_pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) -- 2.7.4 From 5a1ea5e57e78dd3fae315afb4aae33af620bb1c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 28 Jun 2018 13:09:48 +0200 Subject: [PATCH 10/16] usb_client: Move common code to hwcommon library MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I42d5f0f48f1fc0a2600c3532464e8b1f792b3c26 Signed-off-by: Paweł Szewczyk --- hw/usb_cfs_client/usb_cfs_client.c | 941 +------------------------------------ hw/usb_client/usb_client.c | 684 +-------------------------- 2 files changed, 4 insertions(+), 1621 deletions(-) diff --git a/hw/usb_cfs_client/usb_cfs_client.c b/hw/usb_cfs_client/usb_cfs_client.c index f77f8be..36da551 100644 --- a/hw/usb_cfs_client/usb_cfs_client.c +++ b/hw/usb_cfs_client/usb_cfs_client.c @@ -17,943 +17,6 @@ */ #include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#define zalloc(amount) calloc(1, amount) - -#define MAX_GADGET_STR_LEN 256 -#define MAX_FUNCS 32 - -#define CONFIGFS_PATH "/sys/kernel/config" - -#define CONFIGFS_GADGET_NAME "hal-gadget" -#define CONFIGFS_CONFIG_LABEL "hal-config" - -#define NAME_INSTANCE_SEP '.' -#define MAX_INSTANCE_LEN 512 - -#define USB_FUNCS_PATH "/dev/usb-funcs/" - -struct cfs_client { - struct usb_client client; - usbg_state *ctx; - usbg_gadget *gadget; - usbg_udc *udc; -}; - -/* Based on values in slp-gadget kernel module */ -struct usbg_gadget_attrs default_g_attrs = { - .bcdUSB = 0x0200, - .idVendor = 0x04e8, - .idProduct = 0x6860, - .bcdDevice = 0x0100, -}; - -struct usbg_gadget_strs default_g_strs = { - .manufacturer = "Samsung", - .product = "TIZEN", - .serial = "01234TEST", -}; - -static void cfs_free_config(struct usb_configuration *config) -{ - int i; - - if (!config) - return; - - if (config->strs) { - for (i = 0; config->strs[i].lang_code; ++i) - free(config->strs[i].config_str); - - free(config->strs); - } - - /* - * Each function will be free later, - * for now we cleanup only pointers. - */ - if (config->funcs) - free(config->funcs); - - free(config); -} - -static void cfs_free_gadget(struct usb_gadget *gadget) -{ - int i; - - if (!gadget) - return; - - if (gadget->strs) { - for (i = 0; gadget->strs[i].lang_code; ++i) { - free(gadget->strs[i].manufacturer); - free(gadget->strs[i].product); - free(gadget->strs[i].serial); - } - free(gadget->strs); - } - - if (gadget->configs) { - for (i = 0; gadget->configs[i]; ++i) - cfs_free_config(gadget->configs[i]); - - free(gadget->configs); - } - - if (gadget->funcs) { - for (i = 0; gadget->funcs[i]; ++i) - gadget->funcs[i]->free_func(gadget->funcs[i]); - - free(gadget->funcs); - } -} - -static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget, - struct usb_gadget *usb_gadget) -{ - struct usbg_gadget_attrs attrs; - struct usbg_gadget_strs strs; - int ret; - - ret = usbg_get_gadget_attrs(gadget, &attrs); - if (ret) - goto out; - - usb_gadget->attrs.bDeviceClass = attrs.bDeviceClass; - usb_gadget->attrs.bDeviceSubClass = attrs.bDeviceSubClass; - usb_gadget->attrs.bDeviceProtocol = attrs.bDeviceProtocol; - usb_gadget->attrs.idVendor = attrs.idVendor; - usb_gadget->attrs.idProduct = attrs.idProduct; - usb_gadget->attrs.bcdDevice = attrs.bcdDevice; - - - ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs); - if (ret) - goto out; - - usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer); - usb_gadget->strs[0].product = strdup(strs.product); - usb_gadget->strs[0].serial = strdup(strs.serial); - - if (!usb_gadget->strs[0].manufacturer || - !usb_gadget->strs[0].product || - !usb_gadget->strs[0].serial) { - ret = -ENOMEM; - goto err_strs; - } - - return 0; -err_strs: - free(usb_gadget->strs[0].manufacturer); - free(usb_gadget->strs[0].product); - free(usb_gadget->strs[0].serial); -out: - return ret; -} - -static bool cfs_match_func(struct usb_function *f, - const char *name, const char *instance) { - if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS))) { - /* Standard functions */ - if (!strcmp(name, f->name) && !strcmp(instance, f->instance)) - return true; - } else { - /* Function with service */ - const char *sep, *fname, *finst; - int len; - - sep = strchr(instance, NAME_INSTANCE_SEP); - if (!sep || strlen(sep + 1) < 1) - return false; - - fname = instance; - len = sep - instance; - finst = sep + 1; - - if (strlen(f->name) == len - && !strncmp(f->name, fname, len) - && !strcmp(f->instance, finst)) - return true; - } - - return false; -} - - -static int cfs_find_func(const char *name, const char *instance) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) - if (cfs_match_func(_available_funcs[i], name, instance)) - return i; - - return -ENOENT; -} - -static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname, - const char *instance, struct usb_function **_func) -{ - struct usb_function *func; - int ret; - - ret = cfs_find_func(fname, instance); - if (ret < 0) - return -ENOTSUP; - - ret = _available_funcs[ret]->clone(_available_funcs[ret], &func); - if (ret) - return ret; - - *_func = func; - return 0; -} - -static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget) -{ - usbg_function *func; - int i; - int ret; - - i = 0; - usbg_for_each_function(func, gadget) { - char *func_name = (char *)usbg_get_function_type_str( - usbg_get_function_type(func)); - char *instance = (char *)usbg_get_function_instance(func); - - ret = cfs_alloc_new_func(usb_gadget, func_name, instance, - usb_gadget->funcs + i); - if (ret < 0) - goto clean_prev; - ++i; - } - - return 0; -clean_prev: - while (i >= 0) { - usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]); - --i; - } - - return ret; -} - -static struct usb_function *cfs_find_func_in_gadget( - struct usb_gadget *gadget, const char *name, const char *instance) -{ - int i; - - for (i = 0; gadget->funcs[i]; ++i) - if (cfs_match_func(gadget->funcs[i], name, instance)) - return gadget->funcs[i]; - - return NULL; -} - -static int cfs_alloc_config(int n_funcs, struct usb_configuration **_config) -{ - struct usb_configuration *config; - - config = zalloc(sizeof(*config)); - if (!config) - goto out; - - config->strs = calloc(2, sizeof(*config->strs)); - if (!config->strs) - goto free_config; - - config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs)); - if (!config->funcs) - goto free_strs; - - *_config = config; - - return 0; -free_strs: - free(config->strs); -free_config: - free(config); -out: - return -ENOMEM; -} - -static int cfs_read_config(usbg_config *config, struct usb_gadget *gadget, - struct usb_configuration *usb_config) -{ - usbg_binding *b; - usbg_function *func; - char *name, *instance; - struct usbg_config_attrs c_attrs; - struct usbg_config_strs c_strs; - int i = 0; - int ret; - - usbg_for_each_binding(b, config) { - func = usbg_get_binding_target(b); - - name = (char *)usbg_get_function_type_str( - usbg_get_function_type(func)); - instance = (char *)usbg_get_function_instance(func); - - usb_config->funcs[i] = cfs_find_func_in_gadget(gadget, - name, instance); - if (!usb_config->funcs[i]) { - return -ENOTSUP; - } - ++i; - } - - ret = usbg_get_config_attrs(config, &c_attrs); - if (ret) - return ret; - - usb_config->attrs.MaxPower = c_attrs.bMaxPower*2; - usb_config->attrs.bmAttributs = c_attrs.bmAttributes; - - ret = usbg_get_config_strs(config, LANG_US_ENG, &c_strs); - if (ret) { - usb_config->strs[0].lang_code = 0; - } else { - usb_config->strs[0].lang_code = LANG_US_ENG; - usb_config->strs[0].config_str = strdup(c_strs.configuration); - if (!usb_config->strs[0].config_str) - return -ENOMEM; - } - - return 0; -} - -static int cfs_count_bindings(usbg_config *config) -{ - usbg_binding *b; - int i = 0; - - usbg_for_each_binding(b, config) ++i; - - return i; -} - -static int cfs_read_configs(usbg_gadget *gadget, struct usb_gadget *usb_gadget) -{ - usbg_config *config; - int i = 0; - int n_funcs; - int ret; - - usbg_for_each_config(config, gadget) { - n_funcs = cfs_count_bindings(config); - - ret = cfs_alloc_config(n_funcs, usb_gadget->configs + i); - if (ret) - goto clean_prev; - ret = cfs_read_config(config, usb_gadget, - usb_gadget->configs[i]); - if (ret) - goto free_current; - - ++i; - } - - return 0; -free_current: - free(usb_gadget->configs[i]->strs); - free(usb_gadget->configs[i]->funcs); - free(usb_gadget->configs[i]); -clean_prev: - while (i >= 0) - cfs_free_config(usb_gadget->configs[i--]); - return ret; -} - -static int cfs_count_configs(usbg_gadget *gadget) -{ - usbg_config *config; - int i = 0; - - usbg_for_each_config(config, gadget) ++i; - - return i; -} - -static int cfs_count_functions(usbg_gadget *gadget) -{ - usbg_function *func; - int i = 0; - - usbg_for_each_function(func, gadget) ++i; - - return i; -} - -static int cfs_get_current_gadget(struct usb_client *usb, - struct usb_gadget **_usb_gadget) -{ - struct cfs_client *cfs_client; - struct usb_gadget *usb_gadget; - struct usb_gadget_strings *strs; - struct usb_configuration **usb_configs; - struct usb_function **usb_funcs; - int n_funcs, n_configs; - int i; - int ret = -ENOMEM; - - if (!usb) - return -EINVAL; - - cfs_client = container_of(usb, struct cfs_client, - client); - - usb_gadget = zalloc(sizeof(*usb_gadget)); - if (!usb_gadget) - goto out; - - /* - * Currently there is no interface in libusbg which - * allows to list all string languages. - * That's why we do this only for USA english - */ - strs = calloc(2, sizeof(*strs)); - if (!strs) - goto free_gadget; - - strs[0].lang_code = LANG_US_ENG; - - usb_gadget->strs = strs; - - ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget); - if (ret) - goto free_strs; - - - n_funcs = cfs_count_functions(cfs_client->gadget); - usb_funcs = calloc(n_funcs + 1, sizeof(*usb_funcs)); - if (!usb_funcs) - goto free_strs_with_content; - - usb_gadget->funcs = usb_funcs; - - ret = cfs_read_funcs(cfs_client->gadget, usb_gadget); - if (ret) - goto free_funcs; - - n_configs = cfs_count_configs(cfs_client->gadget); - usb_configs = calloc(n_configs + 1, sizeof(*usb_configs)); - if (!usb_configs) - goto free_funcs_with_content; - - usb_gadget->configs = usb_configs; - - ret = cfs_read_configs(cfs_client->gadget, usb_gadget); - if (ret) - goto free_configs; - - *_usb_gadget = usb_gadget; - return 0; - -free_configs: - free(usb_configs); -free_funcs_with_content: - for (i = 0; usb_gadget->funcs[i]; ++i) - usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]); -free_funcs: - free(usb_funcs); -free_strs_with_content: - for (i = 0; usb_gadget->strs[i].lang_code; ++i) { - free(usb_gadget->strs[i].manufacturer); - free(usb_gadget->strs[i].product); - free(usb_gadget->strs[i].serial); - } -free_strs: - free(usb_gadget->strs); -free_gadget: - free(usb_gadget); -out: - return ret; -} - -static bool cfs_is_function_supported(struct usb_client *usb, - struct usb_function *func) -{ - bool res; - int ret; - - switch (func->function_group) { - case USB_FUNCTION_GROUP_SIMPLE: - ret = usbg_lookup_function_type(func->name); - res = ret >= 0; - break; - case USB_FUNCTION_GROUP_WITH_SERVICE: - /* TODO: Check if socket is available */ - res = true; - break; - default: - res = false; - } - - return res; -} - -static bool cfs_is_gadget_supported(struct usb_client *usb, - struct usb_gadget *gadget) -{ - int i, j; - - if (!gadget || !gadget->configs || !gadget->funcs) - return false; - - /* - * TODO - * Here is a good place to ensure that serial is immutable - */ - - /* No real restrictions for strings */ - for (j = 0; gadget->configs && gadget->configs[j]; ++j) { - struct usb_configuration *config = gadget->configs[j]; - - if (!config->funcs) - return false; - - for (i = 0; config->funcs[i]; ++i) - if (!cfs_is_function_supported(usb, config->funcs[i])) - return false; - } - - if (j == 0) - return false; - - return true; -} - -static int cfs_set_gadget_attrs(struct cfs_client *cfs_client, - struct usb_gadget_attrs *attrs) -{ - int ret; - struct usbg_gadget_attrs gadget_attrs; - - ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs); - if (ret) - return ret; - - gadget_attrs.bDeviceClass = attrs->bDeviceClass; - gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass; - gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol; - gadget_attrs.idVendor = attrs->idVendor; - gadget_attrs.idProduct = attrs->idProduct; - gadget_attrs.bcdDevice = attrs->bcdDevice; - - ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs); - - return ret; -} - -static int cfs_set_gadget_strs(struct cfs_client *cfs_client, - struct usb_gadget_strings *strs) -{ - int ret = 0; - - /* - * TODO - * Here is a good place to ensure that serial is immutable - */ -#define SET_STR(FIELD, STR_ID) \ - if (strs->FIELD) { \ - ret = usbg_set_gadget_str(cfs_client->gadget, \ - STR_ID, \ - strs->lang_code, \ - strs->FIELD); \ - if (ret) \ - return ret; \ - } - - SET_STR(manufacturer, USBG_STR_MANUFACTURER); - SET_STR(product, USBG_STR_PRODUCT); - SET_STR(serial, USBG_STR_SERIAL_NUMBER); -#undef SET_STR - return ret; -} - -static int cfs_ensure_dir(char *path) -{ - int ret; - - ret = mkdir(path, 0770); - if (ret < 0) - ret = errno == EEXIST ? 0 : errno; - - return ret; -} - -static int cfs_prep_ffs_service(const char *name, const char *instance, - const char *dev_name, const char *socket_name) -{ - char buf[PATH_MAX]; - size_t left; - char *pos; - int ret; - - /* TODO: Add some good error handling */ - - left = sizeof(buf); - pos = buf; - ret = snprintf(pos, left, "%s", USB_FUNCS_PATH); - if (ret < 0 || ret >= left) { - _E("Function path too long"); - return -ENAMETOOLONG; - } else { - left -= ret; - pos += ret; - } - ret = cfs_ensure_dir(buf); - if (ret < 0) { - _E("Could not create directory %s", buf); - return ret; - } - - ret = snprintf(pos, left, "/%s", name); - if (ret < 0 || ret >= left) { - _E("Path too long"); - return -ENAMETOOLONG; - } else { - left -= ret; - pos += ret; - } - ret = cfs_ensure_dir(buf); - if (ret < 0) { - _E("Could not create directory %s", buf); - return ret; - } - - ret = snprintf(pos, left, "/%s", instance); - if (ret < 0 || ret >= left) { - _E("Path too long"); - return -ENAMETOOLONG; - } else { - left -= ret; - pos += ret; - } - ret = cfs_ensure_dir(buf); - if (ret < 0) { - _E("Could not create directory %s", buf); - return ret; - } - - ret = mount(dev_name, buf, "functionfs", 0, NULL); - if (ret < 0) { - _E("Could not mount %s: %m", dev_name); - return ret; - } - - ret = systemd_start_socket(socket_name); - if (ret < 0) { - _E("Could not start socket: %d", ret); - goto umount_ffs; - } - - return 0; -umount_ffs: - umount(buf); - return ret; -} - -static int cfs_set_gadget_config(struct cfs_client *cfs_client, - int config_id, - struct usb_configuration *usb_config) -{ - struct usbg_config_attrs cattrs = { - .bmAttributes = usb_config->attrs.bmAttributs, - .bMaxPower = usb_config->attrs.MaxPower/2, - }; - usbg_config *config; - int i; - int ret; - - if (!usb_config->funcs || !usb_config->funcs[0]) - return -EINVAL; - - config = usbg_get_config(cfs_client->gadget, config_id, NULL); - if (config) { - ret = usbg_rm_config(config, USBG_RM_RECURSE); - if (ret) { - _E("Could not remove config %d", config_id); - return ret; - } - } - - ret = usbg_create_config(cfs_client->gadget, config_id, - CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config); - if (ret) { - _E("Could not create config %d", config_id); - return ret; - } - - for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) { - ret = usbg_set_config_string(config, usb_config->strs[i].lang_code, - usb_config->strs[i].config_str); - if (ret) { - _E("Could not set config string"); - return ret; - } - } - - for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) { - struct usb_function *usb_func = usb_config->funcs[i]; - char instance[MAX_INSTANCE_LEN]; - int type; - usbg_function *func; - - switch (usb_func->function_group) { - case USB_FUNCTION_GROUP_SIMPLE: - _I("Adding simple function %s.%s", usb_func->name, usb_func->instance); - type = usbg_lookup_function_type(usb_func->name); - if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN) - return -ENAMETOOLONG; - strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN); - instance[MAX_INSTANCE_LEN - 1] = '\0'; - break; - case USB_FUNCTION_GROUP_WITH_SERVICE: - _I("Adding function %s.%s with service", usb_func->name, usb_func->instance); - type = USBG_F_FFS; - ret = snprintf(instance, sizeof(instance), "%s%c%s", - usb_func->name, NAME_INSTANCE_SEP, - usb_func->instance); - if (ret < 0 || ret >= sizeof(instance)) - return -ENAMETOOLONG; - break; - default: - return -EINVAL; - } - - - func = usbg_get_function(cfs_client->gadget, type, instance); - if (!func) { - ret = usbg_create_function(cfs_client->gadget, - type, - instance, - NULL, &func); - if (ret) { - _E("Could not create function %d %s: %d", type, instance, ret); - return ret; - } - - if (usb_func->function_group == - USB_FUNCTION_GROUP_WITH_SERVICE) { - struct usb_function_with_service *fws; - - fws = container_of(usb_func, - struct usb_function_with_service, - func); - ret = cfs_prep_ffs_service(usb_func->name, - usb_func->instance, - instance, - fws->service); - if (ret) { - _E("Could not prepare ffs servicef for %s.%s", type, instance); - return ret; - } - } - - } - - ret = usbg_add_config_function(config, NULL, func); - if (ret) { - _E("Could not add function to config"); - return ret; - } - } - - return ret; -} - -static int cfs_cleanup_left_configs(struct cfs_client *cfs_client, - int last_config) -{ - usbg_config *lconfig, *config; - int ret; - - lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL); - for (config = usbg_get_next_config(lconfig); - config; - config = usbg_get_next_config(lconfig)) { - ret = usbg_rm_config(config, USBG_RM_RECURSE); - if (ret) - return ret; - } - - return 0; -} - -static int cfs_reconfigure_gadget(struct usb_client *usb, - struct usb_gadget *gadget) -{ - struct cfs_client *cfs_client; - int i; - int ret; - - if (!usb) - return -EINVAL; - - cfs_client = container_of(usb, struct cfs_client, - client); - - if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget)) - return -EINVAL; - - ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs); - if (ret) - goto out; - - for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) { - ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i); - if (ret) - goto out; - } - - for (i = 0; gadget->configs && gadget->configs[i]; ++i) { - ret = cfs_set_gadget_config(cfs_client, i + 1, - gadget->configs[i]); - if (ret) - goto out; - } - - /* Workaround for enabling extcon notification on artik */ - ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc); - if (ret) { - _E("Could not enable gadget"); - goto out; - } - - ret = cfs_cleanup_left_configs(cfs_client, i); - - /* TODO - * Cleanup things which are left after previous gadget - */ -out: - return ret; -} - -static int cfs_enable(struct usb_client *usb) -{ - struct cfs_client *cfs_client; - - if (!usb) - return -EINVAL; - - cfs_client = container_of(usb, struct cfs_client, - client); - - return usbg_enable_gadget(cfs_client->gadget, cfs_client->udc); -} - -static int cfs_disable(struct usb_client *usb) -{ - struct cfs_client *cfs_client; - - if (!usb) - return -EINVAL; - - cfs_client = container_of(usb, struct cfs_client, - client); - - return usbg_disable_gadget(cfs_client->gadget); -} - -static int cfs_gadget_open(struct hw_info *info, - const char *id, struct hw_common **common) -{ - struct cfs_client *cfs_client; - int ret; - - _I("Opening configfs gadget"); - - if (!info || !common) - return -EINVAL; - - /* used exclusively with slp usb_client*/ - if (!access("/sys/class/usb_mode/usb0/enable", F_OK)) - return -ENOENT; - - cfs_client = zalloc(sizeof(*cfs_client)); - if (!cfs_client) - return -ENOMEM; - - ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx); - if (ret) { - _E("Could not init usbg"); - goto err_usbg_init; - } - - cfs_client->udc = usbg_get_first_udc(cfs_client->ctx); - if (!cfs_client->udc) { - _E("No UDC found by usbg"); - ret = -ENODEV; - goto err_no_udc; - } - - ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME, - &default_g_attrs, &default_g_strs, - &cfs_client->gadget); - if (ret) { - _E("Could not create gadget"); - goto err_create_gadget; - } - - _I("Gadget created"); - - cfs_client->client.common.info = info; - cfs_client->client.get_current_gadget = cfs_get_current_gadget; - cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget; - cfs_client->client.is_gadget_supported = cfs_is_gadget_supported; - cfs_client->client.is_function_supported = cfs_is_function_supported; - cfs_client->client.enable = cfs_enable; - cfs_client->client.disable = cfs_disable; - cfs_client->client.free_gadget = cfs_free_gadget; - - *common = &cfs_client->client.common; - return 0; - -err_create_gadget: -err_no_udc: - usbg_cleanup(cfs_client->ctx); -err_usbg_init: - free(cfs_client); - - return ret; -} - -static int cfs_gadget_close(struct hw_common *common) -{ - struct cfs_client *cfs_client; - - if (!common) - return -EINVAL; - - cfs_client = container_of(common, struct cfs_client, - client.common); - - /* - * For now we don't check for errors - * but we should somehow handle them - */ - usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE); - usbg_cleanup(cfs_client->ctx); - free(cfs_client); - - return 0; -} HARDWARE_MODULE_STRUCTURE = { .magic = HARDWARE_INFO_TAG, @@ -961,6 +24,6 @@ HARDWARE_MODULE_STRUCTURE = { .device_version = USB_CLIENT_HARDWARE_DEVICE_VERSION, .id = USB_CFS_CLIENT_HARDWARE_DEVICE_ID, .name = "cfs-gadget", - .open = cfs_gadget_open, - .close = cfs_gadget_close, + .open = hw_cfs_gadget_open, + .close = hw_cfs_gadget_close, }; diff --git a/hw/usb_client/usb_client.c b/hw/usb_client/usb_client.c index 6f4b6d1..5b4b61e 100644 --- a/hw/usb_client/usb_client.c +++ b/hw/usb_client/usb_client.c @@ -17,686 +17,6 @@ */ #include -#include -#include - -#include -#include -#include -#include - -#define zalloc(amount) calloc(1, amount) - -#define MAX_GADGET_STR_LEN 256 -#define MAX_FUNCS 32 - -#define LEGACY_ROOTPATH "/sys/class/usb_mode/usb0" - -/* Device descriptor values */ -#define LEGACY_ID_VENDOR_PATH LEGACY_ROOTPATH"/idVendor" -#define LEGACY_ID_PRODUCT_PATH LEGACY_ROOTPATH"/idProduct" -#define LEGACY_BCD_DEVICE_PATH LEGACY_ROOTPATH"/bcdDevice" -#define LEGACY_CLASS_PATH LEGACY_ROOTPATH"/bDeviceClass" -#define LEGACY_SUBCLASS_PATH LEGACY_ROOTPATH"/bDeviceSubClass" -#define LEGACY_PROTOCOL_PATH LEGACY_ROOTPATH"/bDeviceProtocol" - -/* Strings */ -#define LEGACY_IMANUFACTURER_PATH LEGACY_ROOTPATH"/iManufacturer" -#define LEGACY_IPRODUCT_PATH LEGACY_ROOTPATH"/iProduct" -#define LEGACY_ISERIAL_PATH LEGACY_ROOTPATH"/iSerial" - -/* Functions in each config */ -#define LEGACY_CONFIG_1_PATH LEGACY_ROOTPATH"/funcs_fconf" -#define LEGACY_CONFIG_2_PATH LEGACY_ROOTPATH"/funcs_sconf" -/* should be single char */ -#define LEGACY_FUNC_SEP "," - -/* ON/OFF switch */ -#define LEGACY_ENABLE_PATH LEGACY_ROOTPATH"/enable" -#define LEGACY_ENABLE "1" -#define LEGACY_DISABLE "0" - -#define LEGACY_BMATTRIBUTES ((1 << 7) | (1 << 6)) -#define LEGACY_MAX_POWER 500 - -/* +5 to be always big enough */ -#define INT_BUF_SIZE (sizeof(int)*8 + 5) - -static int get_int_from_file(char *path, int *_val, int base) -{ - char buf[INT_BUF_SIZE]; - char *endptr; - long int val; - int ret; - - ret = sys_get_str(path, buf, sizeof(buf)); - if (ret) - return ret; - - val = strtol(buf, &endptr, base); - if (val == LONG_MIN || val == LONG_MAX || - buf[0] == '\0' || (*endptr != '\0' && *endptr != '\n') - || val > INT_MAX) - return -EINVAL; - - *_val = (int)val; - return 0; -} - -static int legacy_read_gadget_attrs_strs(struct usb_gadget *gadget) -{ - int val; - int ret; - /* We assume that values received from kernel will be valid */ -#define GET_VALUE_FROM_SYSFS(path, field, type, base) \ - do { \ - ret = get_int_from_file(path, &val, base); \ - if (ret) \ - return ret; \ - \ - gadget->attrs.field = (type)val; \ - } while (0) - - GET_VALUE_FROM_SYSFS(LEGACY_CLASS_PATH, bDeviceClass, uint8_t, 0); - GET_VALUE_FROM_SYSFS(LEGACY_SUBCLASS_PATH, bDeviceSubClass, uint8_t, 0); - GET_VALUE_FROM_SYSFS(LEGACY_PROTOCOL_PATH, bDeviceProtocol, uint8_t, 0); - GET_VALUE_FROM_SYSFS(LEGACY_ID_VENDOR_PATH, idVendor, uint16_t, 16); - GET_VALUE_FROM_SYSFS(LEGACY_ID_PRODUCT_PATH, idProduct, uint16_t, 16); - GET_VALUE_FROM_SYSFS(LEGACY_BCD_DEVICE_PATH, bcdDevice, uint16_t, 0); -#undef GET_VALUE_FROM_SYSFS - -#define GET_STRING_FROM_SYSFS(path, field) \ - do { \ - char buf[MAX_GADGET_STR_LEN]; \ - \ - ret = sys_get_str(path, buf, sizeof(buf)); \ - if (ret) \ - goto err_##field; \ - \ - gadget->strs[0].field = strdup(buf); \ - if (!gadget->strs[0].field) { \ - ret = -ENOMEM; \ - goto err_##field; \ - } \ - } while (0) - - GET_STRING_FROM_SYSFS(LEGACY_IMANUFACTURER_PATH, manufacturer); - GET_STRING_FROM_SYSFS(LEGACY_IPRODUCT_PATH, product); - GET_STRING_FROM_SYSFS(LEGACY_ISERIAL_PATH, serial); -#undef GET_STRING_FROM_SYSFS - - return 0; - -err_serial: - free(gadget->strs[0].product); -err_product: - free(gadget->strs[0].manufacturer); -err_manufacturer: - return ret; -} - -static int legacy_find_func(const char *name) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) - if (!strcmp(name, _available_funcs[i]->name)) - return i; - - return -1; -} - -static struct usb_function *legacy_find_func_in_gadget( - struct usb_gadget *gadget, const char *name) -{ - int i; - - for (i = 0; gadget->funcs[i]; ++i) - if (!strcmp(name, gadget->funcs[i]->name)) - return gadget->funcs[i]; - return NULL; -} - -static int legacy_alloc_config(int n_funcs, struct usb_configuration **_config) -{ - struct usb_configuration *config; - - config = zalloc(sizeof(*config)); - if (!config) - goto out; - - config->strs = calloc(1, sizeof(*config->strs)); - if (!config->strs) - goto free_config; - - config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs)); - if (!config->funcs) - goto free_strs; - - /* - * We cannot read correct values - * so assume that they are always default - */ - config->attrs.bmAttributs = LEGACY_BMATTRIBUTES; - config->attrs.MaxPower = LEGACY_MAX_POWER; - - *_config = config; - - return 0; -free_strs: - free(config->strs); -free_config: - free(config); -out: - return -ENOMEM; -} - -static int legacy_alloc_new_func(struct usb_gadget *gadget, const char *fname, - struct usb_function **_func) -{ - struct usb_function *func; - int ret; - - ret = legacy_find_func(fname); - if (ret < 0) - return -ENOTSUP; - - ret = _available_funcs[ret]->clone(_available_funcs[ret], &func); - if (ret) - return ret; - - *_func = func; - return 0; -} - -static int legacy_read_config(struct usb_gadget *gadget, - char *cpath, - struct usb_configuration **_config) -{ - struct usb_configuration *config; - char buf[MAX_GADGET_STR_LEN]; - char *begin = buf; - char *fname; - char *sep = LEGACY_FUNC_SEP; - int i, f_cnt; - int f_idx; - int g_f_idx; - int ret; - - ret = sys_get_str(cpath, buf, sizeof(buf)); - if (ret) - return ret; - - /* Empty */ - if (buf[0] == '\0' || buf[0] == '\n') - return 0; - - /* count number of functions in this config */ - f_cnt = 1; - for (i = 0; buf[i] != '\0'; ++i) { - if (buf[i] == sep[0]) - ++f_cnt; - if (buf[i] == '\n') /* buf ends with it */ - buf[i] = 0; - } - - ret = legacy_alloc_config(f_cnt, &config); - if (ret) - return ret; - - for (g_f_idx = 0; gadget->funcs[g_f_idx]; ++g_f_idx); - - f_idx = 0; - for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) { - struct usb_function *func; - - func = legacy_find_func_in_gadget(gadget, fname); - if (!func) { - /* new function not added yet to gadget */ - ret = legacy_alloc_new_func(gadget, fname, &func); - if (ret) - goto free_config; - - gadget->funcs[g_f_idx++] = func; - } - - config->funcs[f_idx++] = func; - } - - *_config = config; - return 0; -free_config: - free(config->strs); - free(config->funcs); - free(config); - return ret; -} - -static int legacy_get_current_gadget(struct usb_client *usb, - struct usb_gadget **_gadget) -{ - struct usb_gadget *gadget; - struct usb_gadget_strings *strs; - struct usb_configuration **configs; - struct usb_function **funcs; - int i; - int ret = -ENOMEM; - - gadget = zalloc(sizeof(*gadget)); - if (!gadget) - goto out; - - strs = calloc(2, sizeof(*strs)); - if (!strs) - goto free_gadget; - - strs[0].lang_code = 0x409; - - gadget->strs = strs; - - ret = legacy_read_gadget_attrs_strs(gadget); - if (ret) - goto free_strs; - - /* There will be no more functions than bits in int */ - funcs = calloc(MAX_FUNCS, sizeof(*funcs)); - if (!funcs) - goto free_strs_with_content; - - gadget->funcs = funcs; - - /* slp-gadget use max 2 confiuration and NULL termination */ - configs = calloc(3, sizeof(*configs)); - if (!configs) - goto free_funcs; - - gadget->configs = configs; - - ret = legacy_read_config(gadget, LEGACY_CONFIG_1_PATH, configs + 0); - if (ret) - goto free_configs; - - ret = legacy_read_config(gadget, LEGACY_CONFIG_2_PATH, configs + 1); - if (ret) - goto free_config_1; - - *_gadget = gadget; - return 0; - -free_config_1: - free(configs[0]->funcs); - free(configs[0]->strs); - free(configs[0]); -free_configs: - free(configs); - for (i = 0; gadget->funcs[i]; ++i) - gadget->funcs[i]->free_func(gadget->funcs[i]); -free_funcs: - free(funcs); -free_strs_with_content: - free(gadget->strs[0].manufacturer); - free(gadget->strs[0].product); - free(gadget->strs[0].serial); -free_strs: - free(gadget->strs); -free_gadget: - free(gadget); -out: - return ret; -} - -static bool legacy_is_function_supported(struct usb_client *usb, - struct usb_function *func) -{ - int ret; - - /* - * TODO - * Instead of only checking whether we know this function - * we should also parse sysfs to check if it is build into - * slp-gadget. - */ - ret = legacy_find_func(func->name); - - return ret >= 0; -} - -static bool legacy_is_gadget_supported(struct usb_client *usb, - struct usb_gadget *gadget) -{ - int i, j; - - if (!gadget || !gadget->configs || !gadget->funcs) - return false; - - /* - * TODO - * Here is a good place to ensure that serial is immutable - */ - if (gadget->strs) { - /* only strings in US_en are allowed */ - if (gadget->strs[0].lang_code != 0x409 || - gadget->strs[1].lang_code) - return false; - } - - for (j = 0; gadget->configs[j]; ++j) { - struct usb_configuration *config = gadget->configs[j]; - - if (config->strs && config->strs[0].lang_code) - return false; - - if (!config->funcs) - return false; - - for (i = 0; config->funcs[i]; ++i) - if (!legacy_is_function_supported(usb, config->funcs[i])) - return false; - } - - if (j == 0 || j > 2) - return false; - - return true; -} - -/* TODO. Maybe move this to sys ? */ -static int legacy_set_int_hex(char *path, int val) -{ - char buf[MAX_GADGET_STR_LEN]; - int r; - - if (!path) - return -EINVAL; - - snprintf(buf, sizeof(buf), "%x", val); - r = sys_set_str(path, buf); - if (r < 0) - return r; - - return 0; -} - -static int legacy_set_gadget_attrs(struct usb_gadget_attrs *attrs) -{ - int ret; - - ret = sys_set_int(LEGACY_CLASS_PATH, attrs->bDeviceClass); - if (ret) - return ret; - - ret = sys_set_int(LEGACY_SUBCLASS_PATH, attrs->bDeviceSubClass); - if (ret) - return ret; - - ret = sys_set_int(LEGACY_PROTOCOL_PATH, attrs->bDeviceProtocol); - if (ret) - return ret; - - ret = legacy_set_int_hex(LEGACY_ID_VENDOR_PATH, attrs->idVendor); - if (ret) - return ret; - - ret = legacy_set_int_hex(LEGACY_ID_PRODUCT_PATH, attrs->idProduct); - if (ret) - return ret; - - ret = legacy_set_int_hex(LEGACY_BCD_DEVICE_PATH, attrs->bcdDevice); - - return ret; -} - -static int legacy_set_gadget_strs(struct usb_gadget_strings *strs) -{ - int ret = 0; - - /* - * TODO - * Here is a good place to ensure that serial is immutable - */ - - if (strs->manufacturer) { - ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, - strs->manufacturer); - if (ret) - return ret; - } - - if (strs->product) { - ret = sys_set_str(LEGACY_IPRODUCT_PATH, - strs->product); - if (ret) - return ret; - } - - return ret; -} - -static int legacy_set_gadget_config(char *cpath, - struct usb_configuration *config) -{ - char buf[MAX_GADGET_STR_LEN]; - int left = sizeof(buf); - char *pos = buf; - int ret; - int i; - - if (!config) { - buf[0] = '\n'; - buf[1] = '\0'; - goto empty_config; - } - - for (i = 0; config->funcs[i]; ++i) { - ret = snprintf(pos, left, "%s" LEGACY_FUNC_SEP, - config->funcs[i]->name); - if (ret >= left) - return -EOVERFLOW; - - pos += ret; - left -= ret; - } - - /* eliminate last separator */ - *(pos - 1) = '\0'; - -empty_config: - return sys_set_str(cpath, buf); -} - -static int legacy_reconfigure_gadget(struct usb_client *usb, - struct usb_gadget *gadget) -{ - int ret; - - if (!usb || !gadget || !legacy_is_gadget_supported(usb, gadget)) - return -EINVAL; - - ret = legacy_set_gadget_attrs(&gadget->attrs); - if (ret) - return ret; - - if (gadget->strs) { - ret = legacy_set_gadget_strs(gadget->strs + 0); - if (ret) - return ret; - } - - ret = legacy_set_gadget_config(LEGACY_CONFIG_1_PATH, gadget->configs[0]); - if (ret) - return ret; - - ret = legacy_set_gadget_config(LEGACY_CONFIG_2_PATH, gadget->configs[1]); - - return ret; -} - -static int legacy_enable(struct usb_client *usb) -{ - int ret; - int i; - struct usb_gadget *gadget; - struct usb_function_with_service *fws; - - ret = sys_set_str(LEGACY_ENABLE_PATH, - LEGACY_ENABLE); - if (ret < 0) - return ret; - - ret = legacy_get_current_gadget(usb, &gadget); - if (ret < 0) - goto disable_gadget; - - for (i = 0; gadget->funcs[i]; ++i) { - if (gadget->funcs[i]->function_group != - USB_FUNCTION_GROUP_WITH_SERVICE) - continue; - - fws = container_of(gadget->funcs[i], - struct usb_function_with_service, func); - ret = systemd_start_service(fws->service); - if (ret < 0) - goto stop_services; - } - - return 0; -stop_services: - while (--i >= 0) { - if (gadget->funcs[i]->function_group != - USB_FUNCTION_GROUP_WITH_SERVICE) - continue; - - fws = container_of(gadget->funcs[i], - struct usb_function_with_service, func); - systemd_stop_service(fws->service); - } - -disable_gadget: - sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE); - return ret; -} - -static int legacy_disable(struct usb_client *usb) -{ - int ret; - int i; - struct usb_gadget *gadget; - struct usb_function_with_service *fws; - - ret = legacy_get_current_gadget(usb, &gadget); - if (ret < 0) - return ret; - - for (i = 0; gadget->funcs[i]; ++i) { - if (gadget->funcs[i]->function_group != USB_FUNCTION_GROUP_WITH_SERVICE) - continue; - - fws = container_of(gadget->funcs[i], struct usb_function_with_service, func); - ret = systemd_stop_service(fws->service); - if (ret < 0) - return ret; - } - - return sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE); -} - -static void legacy_free_config(struct usb_configuration *config) -{ - int i; - - if (!config) - return; - - if (config->strs) { - for (i = 0; config->strs[i].lang_code; ++i) - free(config->strs[i].config_str); - - free(config->strs); - } - - /* - * Each function will be free later, - * for now we cleanup only pointers. - */ - if (config->funcs) - free(config->funcs); - - free(config); -} - -static void legacy_free_gadget(struct usb_gadget *gadget) -{ - int i; - - if (!gadget) - return; - - if (gadget->strs) { - for (i = 0; gadget->strs[i].lang_code; ++i) { - free(gadget->strs[i].manufacturer); - free(gadget->strs[i].product); - free(gadget->strs[i].serial); - } - free(gadget->strs); - } - - if (gadget->configs) { - for (i = 0; gadget->configs[i]; ++i) - legacy_free_config(gadget->configs[i]); - - free(gadget->configs); - } - - if (gadget->funcs) { - for (i = 0; gadget->funcs[i]; ++i) - gadget->funcs[i]->free_func(gadget->funcs[i]); - - free(gadget->funcs); - } -} - -static int legacy_gadget_open(struct hw_info *info, - const char *id, struct hw_common **common) -{ - struct usb_client *legacy; - - if (!info || !common) - return -EINVAL; - - /* check if slp usb gadget exists */ - if (access("/sys/class/usb_mode/usb0/enable", F_OK)) - return -ENOENT; - - legacy = zalloc(sizeof(*legacy)); - if (!legacy) - return -ENOMEM; - - legacy->common.info = info; - legacy->get_current_gadget = legacy_get_current_gadget; - legacy->reconfigure_gadget = legacy_reconfigure_gadget; - legacy->is_gadget_supported = legacy_is_gadget_supported; - legacy->is_function_supported = legacy_is_function_supported; - legacy->enable = legacy_enable; - legacy->disable = legacy_disable; - legacy->free_gadget = legacy_free_gadget; - - *common = &legacy->common; - return 0; -} - -static int legacy_gadget_close(struct hw_common *common) -{ - struct usb_client *legacy; - - if (!common) - return -EINVAL; - - legacy = container_of(common, struct usb_client, - common); - - free(legacy); - return 0; -} HARDWARE_MODULE_STRUCTURE = { .magic = HARDWARE_INFO_TAG, @@ -704,6 +24,6 @@ HARDWARE_MODULE_STRUCTURE = { .device_version = USB_CLIENT_HARDWARE_DEVICE_VERSION, .id = USB_CLIENT_HARDWARE_DEVICE_ID, .name = "legacy-gadget", - .open = legacy_gadget_open, - .close = legacy_gadget_close, + .open = hw_legacy_gadget_open, + .close = hw_legacy_gadget_close, }; -- 2.7.4 From e84fffeae963ae7672427c4e7d202f4c8c11a11e Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Thu, 27 Dec 2018 11:06:44 +0900 Subject: [PATCH 11/16] Remove executable flag from non-executable files Change-Id: Idee810863ae5750b64617b091dcb9d8e2964ddfa Signed-off-by: lokilee73 --- CMakeLists.txt | 0 LICENSE | 0 hw/battery/CMakeLists.txt | 0 hw/battery/battery.c | 0 hw/board/board.c | 0 hw/display/CMakeLists.txt | 0 hw/display/display.c | 0 hw/external_connection/CMakeLists.txt | 0 hw/external_connection/external_connection.c | 0 hw/led/led.c | 0 hw/touchscreen/CMakeLists.txt | 0 hw/touchscreen/touchscreen.c | 0 hw/udev.c | 0 hw/udev.h | 0 hw/usb_gadget/usb_gadget.c | 0 15 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 CMakeLists.txt mode change 100755 => 100644 LICENSE mode change 100755 => 100644 hw/battery/CMakeLists.txt mode change 100755 => 100644 hw/battery/battery.c mode change 100755 => 100644 hw/board/board.c mode change 100755 => 100644 hw/display/CMakeLists.txt mode change 100755 => 100644 hw/display/display.c mode change 100755 => 100644 hw/external_connection/CMakeLists.txt mode change 100755 => 100644 hw/external_connection/external_connection.c mode change 100755 => 100644 hw/led/led.c mode change 100755 => 100644 hw/touchscreen/CMakeLists.txt mode change 100755 => 100644 hw/touchscreen/touchscreen.c mode change 100755 => 100644 hw/udev.c mode change 100755 => 100644 hw/udev.h mode change 100755 => 100644 hw/usb_gadget/usb_gadget.c diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/hw/battery/CMakeLists.txt b/hw/battery/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/hw/battery/battery.c b/hw/battery/battery.c old mode 100755 new mode 100644 diff --git a/hw/board/board.c b/hw/board/board.c old mode 100755 new mode 100644 diff --git a/hw/display/CMakeLists.txt b/hw/display/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/hw/display/display.c b/hw/display/display.c old mode 100755 new mode 100644 diff --git a/hw/external_connection/CMakeLists.txt b/hw/external_connection/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/hw/external_connection/external_connection.c b/hw/external_connection/external_connection.c old mode 100755 new mode 100644 diff --git a/hw/led/led.c b/hw/led/led.c old mode 100755 new mode 100644 diff --git a/hw/touchscreen/CMakeLists.txt b/hw/touchscreen/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/hw/touchscreen/touchscreen.c b/hw/touchscreen/touchscreen.c old mode 100755 new mode 100644 diff --git a/hw/udev.c b/hw/udev.c old mode 100755 new mode 100644 diff --git a/hw/udev.h b/hw/udev.h old mode 100755 new mode 100644 diff --git a/hw/usb_gadget/usb_gadget.c b/hw/usb_gadget/usb_gadget.c old mode 100755 new mode 100644 -- 2.7.4 From f58900de1101d1bc80f6e90fc660013be71cf9a1 Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Tue, 8 Jan 2019 17:26:00 +0900 Subject: [PATCH 12/16] Add thermal module for device_thermal_get_temperature Change-Id: Idaec06e25703d32f773a6f0f7a984ccea8731d61 Signed-off-by: lokilee73 --- CMakeLists.txt | 1 + hw/thermal/CMakeLists.txt | 19 ++++++ hw/thermal/thermal.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 hw/thermal/CMakeLists.txt create mode 100644 hw/thermal/thermal.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3653bc0..42671b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,3 +12,4 @@ ADD_SUBDIRECTORY(hw/touchscreen) ADD_SUBDIRECTORY(hw/usb_gadget) ADD_SUBDIRECTORY(hw/usb_client) ADD_SUBDIRECTORY(hw/usb_cfs_client) +ADD_SUBDIRECTORY(hw/thermal) \ No newline at end of file diff --git a/hw/thermal/CMakeLists.txt b/hw/thermal/CMakeLists.txt new file mode 100644 index 0000000..42bcc20 --- /dev/null +++ b/hw/thermal/CMakeLists.txt @@ -0,0 +1,19 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(thermal C) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +INCLUDE(FindPkgConfig) +pkg_check_modules(thermal_pkgs REQUIRED hwcommon dlog glib-2.0 libudev) + +FOREACH(flag ${thermal_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +ADD_LIBRARY(${PROJECT_NAME} MODULE thermal.c ../udev.c) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${thermal_pkgs_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/thermal/thermal.c b/hw/thermal/thermal.c new file mode 100644 index 0000000..86f18f8 --- /dev/null +++ b/hw/thermal/thermal.c @@ -0,0 +1,156 @@ +/* + * device-node + * + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include +#include + +#include +#include + +#define AP_PATH "/sys/class/thermal/thermal_zone0/temp" + +static struct event_data { + ThermalUpdated updated_cb; + void *data; +} edata = { 0, }; + +static guint timer; + +static int thermal_get_info(device_thermal_e type, struct thermal_info *info) +{ + FILE *fp; + char buf[32]; + size_t len; + + if (!info) + return -EINVAL; + + fp = fopen(AP_PATH, "r"); + if (!fp) { + _E("Failed to open thermal path(%d)", errno); + return -errno; + } + + len = fread(buf, 1, sizeof(buf) - 1, fp); + fclose(fp); + if (len == 0) { + _E("Failed to read thermal value(%d)", errno); + return -errno; + } + buf[len] = '\0'; + info->temp = atoi(buf); + info->temp /= 1000; + info->adc = 0; + + _I("temp(%d) adc(%d)", info->temp, info->adc); + return 0; +} + +static gboolean thermal_timeout(gpointer data) +{ + struct thermal_info info; + int ret; + + ret = thermal_get_info(DEVICE_THERMAL_AP, &info); + if (ret < 0) { + _E("Failed to read thermal info(%d)", ret); + return G_SOURCE_CONTINUE; + } + + if (edata.updated_cb) + edata.updated_cb(&info, edata.data); + + return G_SOURCE_CONTINUE; +} + +static int thermal_register_changed_event(ThermalUpdated updated_cb, void *data) +{ + if (timer) + g_source_remove(timer); + + timer = g_timeout_add(10000, thermal_timeout, NULL); + if (timer == 0) { + _E("Failed to add timer for thermal"); + return -ENOENT; + } + + edata.updated_cb = updated_cb; + edata.data = data; + + return 0; +} + +static int thermal_unregister_changed_event(ThermalUpdated updated_cb) +{ + if (timer) { + g_source_remove(timer); + timer = 0; + } + + edata.updated_cb = NULL; + edata.data = NULL; + + return 0; +} + +static int thermal_open(struct hw_info *info, + const char *id, struct hw_common **common) +{ + struct thermal_device *thermal_dev; + + if (!info || !common) + return -EINVAL; + + thermal_dev = calloc(1, sizeof(struct thermal_device)); + if (!thermal_dev) + return -ENOMEM; + + thermal_dev->common.info = info; + thermal_dev->register_changed_event + = thermal_register_changed_event; + thermal_dev->unregister_changed_event + = thermal_unregister_changed_event; + thermal_dev->get_info + = thermal_get_info; + + *common = (struct hw_common *)thermal_dev; + return 0; +} + +static int thermal_close(struct hw_common *common) +{ + if (!common) + return -EINVAL; + + free(common); + return 0; +} + +HARDWARE_MODULE_STRUCTURE = { + .magic = HARDWARE_INFO_TAG, + .hal_version = HARDWARE_INFO_VERSION, + .device_version = THERMAL_HARDWARE_DEVICE_VERSION, + .id = THERMAL_HARDWARE_DEVICE_ID, + .name = "thermal", + .open = thermal_open, + .close = thermal_close, +}; -- 2.7.4 From 42006ef812e99880a0020cd124a9378fcf9396c4 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 5 Feb 2020 17:01:23 +0900 Subject: [PATCH 13/16] Add build option -Wall -Werror Change-Id: I40035cde88b5549f0300ed61661982718cf9c047 Signed-off-by: Youngjae Cho --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 42671b7..8c5de95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,8 @@ PROJECT(device-manager-artik C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") + #ADD_SUBDIRECTORY(hw/battery) ADD_SUBDIRECTORY(hw/board) ADD_SUBDIRECTORY(hw/display) @@ -12,4 +14,4 @@ ADD_SUBDIRECTORY(hw/touchscreen) ADD_SUBDIRECTORY(hw/usb_gadget) ADD_SUBDIRECTORY(hw/usb_client) ADD_SUBDIRECTORY(hw/usb_cfs_client) -ADD_SUBDIRECTORY(hw/thermal) \ No newline at end of file +ADD_SUBDIRECTORY(hw/thermal) -- 2.7.4 From 54c09942d300d08b34f5ea1142ce11dfcd8cc938 Mon Sep 17 00:00:00 2001 From: INSUN PYO Date: Fri, 7 Feb 2020 16:17:19 +0900 Subject: [PATCH 14/16] Remove unused module Change-Id: Ibdb713172b157e8063de24484a8b276522e40a7d --- CMakeLists.txt | 2 - hw/battery/CMakeLists.txt | 19 -- hw/battery/battery.c | 304 --------------------------- hw/external_connection/CMakeLists.txt | 19 -- hw/external_connection/external_connection.c | 216 ------------------- 5 files changed, 560 deletions(-) delete mode 100644 hw/battery/CMakeLists.txt delete mode 100644 hw/battery/battery.c delete mode 100644 hw/external_connection/CMakeLists.txt delete mode 100644 hw/external_connection/external_connection.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c5de95..01bda9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,10 +5,8 @@ SET(PREFIX ${CMAKE_INSTALL_PREFIX}) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") -#ADD_SUBDIRECTORY(hw/battery) ADD_SUBDIRECTORY(hw/board) ADD_SUBDIRECTORY(hw/display) -#ADD_SUBDIRECTORY(hw/external_connection) ADD_SUBDIRECTORY(hw/led) ADD_SUBDIRECTORY(hw/touchscreen) ADD_SUBDIRECTORY(hw/usb_gadget) diff --git a/hw/battery/CMakeLists.txt b/hw/battery/CMakeLists.txt deleted file mode 100644 index 820c168..0000000 --- a/hw/battery/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(battery C) - -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) - -INCLUDE(FindPkgConfig) -pkg_check_modules(battery_pkgs REQUIRED hwcommon dlog glib-2.0 libudev) - -FOREACH(flag ${battery_pkgs_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") - -ADD_LIBRARY(${PROJECT_NAME} MODULE battery.c ../udev.c) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${battery_pkgs_LDFLAGS}) -SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/battery/battery.c b/hw/battery/battery.c deleted file mode 100644 index f16da48..0000000 --- a/hw/battery/battery.c +++ /dev/null @@ -1,304 +0,0 @@ -/* - * device-node - * - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#include -#include -#include -#include -#include -#include - -#include -#include -#include "../udev.h" - -#define BATTERY_ROOT_PATH "/sys/class/power_supply" - -static struct uevent_data { - BatteryUpdated updated_cb; - void *data; -} udata = { 0, }; - -static int get_power_source(char **src) -{ - int ret, val; - - if (!src) - return -EINVAL; - - ret = sys_get_int(BATTERY_ROOT_PATH"/rk-ac/online", &val); - if (ret >= 0 && val > 0) { - *src = POWER_SOURCE_AC; - return 0; - } - - *src = POWER_SOURCE_NONE; - - return 0; -} - -static void remove_not_string(char *str) -{ - char *t = str; - - while (*t != '\0') { - if (*t == '\r' || - *t == '\n' || - *t == '\x0a') - *t = '\0'; - else - t++; - } -} - -static void uevent_delivered(struct udev_device *dev) -{ - struct battery_info info; - char *val; - int ret; - - _I("POWER_SUPPLY uevent is delivered"); - - if (!udata.updated_cb) { - _E("POWER_SUPPLY callback is NULL"); - return; - } - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_NAME"); - if (!val) - return; - info.name = val; - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_STATUS"); - if (!val) - return; - info.status = val; - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_HEALTH"); - if (!val) - return; - info.health = val; - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT"); - if (!val) - return; - info.present = atoi(val); - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE"); - if (val) - info.online = atoi(val); - else - info.online = info.present; - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CAPACITY"); - if (!val) - return; - info.capacity = atoi(val); - - ret = get_power_source(&val); - if (ret < 0) - return; - info.power_source = val; - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CURRENT_NOW"); - if (val) - info.current_now = atoi(val); /* uA */ - else { - if (strncmp(info.power_source, POWER_SOURCE_NONE, sizeof(POWER_SOURCE_NONE))) - info.current_now = 1000; /* current entering the battery from charge source */ - else - info.current_now = -1000; /* current discharging from the battery */ - } - - val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CURRENT_AVG"); - if (val) - info.current_average = atoi(val); /* uA */ - else - info.current_average = info.current_now; - - udata.updated_cb(&info, udata.data); -} - -static struct uevent_handler uh = { - .subsystem = "power_supply", - .uevent_func = uevent_delivered, -}; - -static int battery_register_changed_event( - BatteryUpdated updated_cb, void *data) -{ - int ret; - - ret = uevent_control_kernel_start(); - if (ret < 0) { - _E("Failed to register uevent handler (%d)", ret); - return ret; - } - - ret = register_kernel_event_control(&uh); - if (ret < 0) - _E("Failed to register kernel event control (%d)", ret); - - if (udata.updated_cb == NULL) { - udata.updated_cb = updated_cb; - udata.data = data; - } else - _E("update callback is already registered"); - - return ret; -} - -static void battery_unregister_changed_event( - BatteryUpdated updated_cb) -{ - unregister_kernel_event_control(&uh); - uevent_control_kernel_stop(); - udata.updated_cb = NULL; - udata.data = NULL; -} - -static int battery_get_current_state( - BatteryUpdated updated_cb, void *data) -{ - int ret, val; - struct battery_info info; - char *path; - char status[32]; - char health[32]; - char *power_source; - - if (!updated_cb) - return -EINVAL; - - info.name = BATTERY_HARDWARE_DEVICE_ID; - - path = BATTERY_ROOT_PATH"/rk-bat/status"; - ret = sys_get_str(path, status, sizeof(status)); - if (ret < 0) { - _E("Failed to get value of (%s, %d)", path, ret); - return ret; - } - remove_not_string(status); - info.status = status; - - path = BATTERY_ROOT_PATH"/rk-bat/health"; - ret = sys_get_str(path, health, sizeof(health)); - if (ret < 0) { - _E("Failed to get value of (%s, %d)", path, ret); - return ret; - } - remove_not_string(health); - info.health = health; - - ret = get_power_source(&power_source); - if (ret < 0) { - _E("Failed to get power source (%d)", ret); - return ret; - } - remove_not_string(power_source); - info.power_source = power_source; - - path = BATTERY_ROOT_PATH"/rk-bat/present"; - ret = sys_get_int(path, &val); - if (ret < 0) { - _E("Failed to get value of (%s, %d)", path, ret); - return ret; - } - info.present = val; - - path = BATTERY_ROOT_PATH"/rk-bat/online"; - ret = sys_get_int(path, &val); - if (ret == 0) - info.online = val; - else - info.online = info.present; - - path = BATTERY_ROOT_PATH"/rk-bat/capacity"; - ret = sys_get_int(path, &val); - if (ret < 0) { - _E("Failed to get value of (%s, %d)", path, ret); - return ret; - } - info.capacity = val; - - path = BATTERY_ROOT_PATH"/rk-bat/current_now"; - ret = sys_get_int(path, &val); - if (ret == 0) - info.current_now = val; - else { - if (strncmp(power_source, POWER_SOURCE_NONE, sizeof(POWER_SOURCE_NONE))) - info.current_now = 1000; /* current entering the battery from charge source */ - else - info.current_now = -1000; /* current discharging from the battery */ - } - - path = BATTERY_ROOT_PATH"/rk-bat/current_avg"; - ret = sys_get_int(path, &val); - if (ret == 0) - info.current_average = val; - else - info.current_average = info.current_now; - - updated_cb(&info, data); - - return 0; -} - -static int battery_open(struct hw_info *info, - const char *id, struct hw_common **common) -{ - struct battery_device *battery_dev; - - if (!info || !common) - return -EINVAL; - - battery_dev = calloc(1, sizeof(struct battery_device)); - if (!battery_dev) - return -ENOMEM; - - battery_dev->common.info = info; - battery_dev->register_changed_event - = battery_register_changed_event; - battery_dev->unregister_changed_event - = battery_unregister_changed_event; - battery_dev->get_current_state - = battery_get_current_state; - - *common = (struct hw_common *)battery_dev; - return 0; -} - -static int battery_close(struct hw_common *common) -{ - if (!common) - return -EINVAL; - - free(common); - return 0; -} - -HARDWARE_MODULE_STRUCTURE = { - .magic = HARDWARE_INFO_TAG, - .hal_version = HARDWARE_INFO_VERSION, - .device_version = BATTERY_HARDWARE_DEVICE_VERSION, - .id = BATTERY_HARDWARE_DEVICE_ID, - .name = "battery", - .open = battery_open, - .close = battery_close, -}; diff --git a/hw/external_connection/CMakeLists.txt b/hw/external_connection/CMakeLists.txt deleted file mode 100644 index b1f2f83..0000000 --- a/hw/external_connection/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(external_connection C) - -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) - -INCLUDE(FindPkgConfig) -pkg_check_modules(external_connection_pkgs REQUIRED hwcommon dlog glib-2.0 libudev) - -FOREACH(flag ${external_connection_pkgs_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") - -ADD_LIBRARY(${PROJECT_NAME} MODULE external_connection.c ../udev.c) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${external_connection_pkgs_LDFLAGS}) -SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/external_connection/external_connection.c b/hw/external_connection/external_connection.c deleted file mode 100644 index f49083b..0000000 --- a/hw/external_connection/external_connection.c +++ /dev/null @@ -1,216 +0,0 @@ -/* - * device-node - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#include -#include -#include -#include -#include -#include - -#include -#include -#include "../udev.h" - -#define SWITCH_ROOT_PATH "/sys/devices/virtual/switch" - -static struct switch_device { - char *type; - char *name; - int state; -} switch_devices[] = { - { EXTERNAL_CONNECTION_USB, "usb_cable", 0 }, - { EXTERNAL_CONNECTION_DOCK, "dock" , 0 }, - { EXTERNAL_CONNECTION_HEADPHONE, "earjack" , 0 }, -}; - -static struct uevent_data { - ConnectionUpdated updated_cb; - void *data; -} udata = { 0, }; - -static void uevent_delivered(struct udev_device *dev) -{ - struct connection_info info; - char *name, *state; - int i; - - _I("Switch uevent is delivered"); - - name = (char *)udev_device_get_property_value(dev, "SWITCH_NAME"); - if (!name) - return; - - state = (char *)udev_device_get_property_value(dev, "SWITCH_STATE"); - if (!state) - return; - - for (i = 0 ; i < ARRAY_SIZE(switch_devices) ; i++) { - if (strncmp(name, switch_devices[i].name, strlen(name) + 1)) - continue; - - switch_devices[i].state = atoi(state); - - info.name = switch_devices[i].type; - info.state = state; - info.flags = 0; - - if (udata.updated_cb) - udata.updated_cb(&info, udata.data); - else - _E("callback is NULL"); - } -} - -static struct uevent_handler uh = { - .subsystem = "switch", - .uevent_func = uevent_delivered, -}; - -static int external_connection_register_changed_event( - ConnectionUpdated updated_cb, void *data) -{ - int ret; - - ret = uevent_control_kernel_start(); - if (ret < 0) { - _E("Failed to register uevent handler (%d)", ret); - return ret; - } - - ret = register_kernel_event_control(&uh); - if (ret < 0) - _E("Failed to register kernel event control (%d)", ret); - - if (udata.updated_cb == NULL) { - udata.updated_cb = updated_cb; - udata.data = data; - } else - _E("update callback is already registered"); - - return ret; -} - -static void external_connection_unregister_changed_event( - ConnectionUpdated updated_cb) -{ - unregister_kernel_event_control(&uh); - uevent_control_kernel_stop(); - udata.updated_cb = NULL; - udata.data = NULL; -} - -static int read_switch_state(char *path) -{ - char node[128], val[8]; - FILE *fp; - - if (!path) - return -EINVAL; - - snprintf(node, sizeof(node), "%s/%s/state", - SWITCH_ROOT_PATH, path); - - fp = fopen(node, "r"); - if (!fp) { - _E("Failed to open (%s)", path); - return -ENOMEM; - } - - if (!fgets(val, sizeof(val), fp)) { - _E("Failed to read (%s)", path); - fclose(fp); - return -ENOENT; - } - - fclose(fp); - - return atoi(val); -} - -static int external_connection_get_current_state( - ConnectionUpdated updated_cb, void *data) -{ - int ret, i; - struct connection_info info; - char buf[8]; - - if (!updated_cb) - return -EINVAL; - - for (i = 0 ; i < ARRAY_SIZE(switch_devices) ; i++) { - ret = read_switch_state(switch_devices[i].name); - if (ret < 0) { - _E("Failed to get value of (%s, ret:%d)", - switch_devices[i].name, ret); - continue; - } - - info.name = switch_devices[i].type; - snprintf(buf, sizeof(buf), "%d", ret); - info.state = buf; - - updated_cb(&info, data); - } - - return 0; -} - -static int external_connection_open(struct hw_info *info, - const char *id, struct hw_common **common) -{ - struct external_connection_device *external_connection_dev; - - if (!info || !common) - return -EINVAL; - - external_connection_dev = calloc(1, sizeof(struct external_connection_device)); - if (!external_connection_dev) - return -ENOMEM; - - external_connection_dev->common.info = info; - external_connection_dev->register_changed_event - = external_connection_register_changed_event; - external_connection_dev->unregister_changed_event - = external_connection_unregister_changed_event; - external_connection_dev->get_current_state - = external_connection_get_current_state; - - *common = (struct hw_common *)external_connection_dev; - return 0; -} - -static int external_connection_close(struct hw_common *common) -{ - if (!common) - return -EINVAL; - - free(common); - return 0; -} - -HARDWARE_MODULE_STRUCTURE = { - .magic = HARDWARE_INFO_TAG, - .hal_version = HARDWARE_INFO_VERSION, - .device_version = EXTERNAL_CONNECTION_HARDWARE_DEVICE_VERSION, - .id = EXTERNAL_CONNECTION_HARDWARE_DEVICE_ID, - .name = "external_connection", - .open = external_connection_open, - .close = external_connection_close, -}; -- 2.7.4 From 74fcdd2aa28ea28cd6c91a26673d859fcbad7ebf Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 13 Feb 2020 17:34:57 +0900 Subject: [PATCH 15/16] Fix coverity issue CID 1095159 (#1 of 1): Resource leak (RESOURCE_LEAK) Change-Id: I185433f9079dbffa69f8b2673fdbe380f1a60804 Signed-off-by: Youngjae Cho --- hw/board/board.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/board/board.c b/hw/board/board.c index 3c6b42d..4f73ea7 100644 --- a/hw/board/board.c +++ b/hw/board/board.c @@ -45,6 +45,7 @@ static int get_device_serial(char **out) } *out = p; + free(line); return 0; } -- 2.7.4 From 221fec3ef48977b85f0f7746b675ae9ecc35a5bd Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Mon, 17 Feb 2020 06:28:39 +0000 Subject: [PATCH 16/16] Revert "Fix coverity issue" This reverts commit 74fcdd2aa28ea28cd6c91a26673d859fcbad7ebf. Change-Id: I1f0d89cedddb2f67be79b63117a972bb04fd9c77 --- hw/board/board.c | 1 - 1 file changed, 1 deletion(-) diff --git a/hw/board/board.c b/hw/board/board.c index 4f73ea7..3c6b42d 100644 --- a/hw/board/board.c +++ b/hw/board/board.c @@ -45,7 +45,6 @@ static int get_device_serial(char **out) } *out = p; - free(line); return 0; } -- 2.7.4