Add cp2-downloader execution logic 44/256144/1 accepted/tizen/unified/20210331.054028 submit/tizen/20210330.223417
authorWootak Jung <wootak.jung@samsung.com>
Tue, 30 Mar 2021 05:41:23 +0000 (14:41 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Tue, 30 Mar 2021 05:41:23 +0000 (14:41 +0900)
Change-Id: Id1bd55fddea2f71a065af33b81c8d5725cd7e22c
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
src/hal-backend-bluetooth.c

index 744b353..68ec7af 100644 (file)
@@ -3,7 +3,8 @@
 #include <stdint.h>
 #include <errno.h>
 #include <dlog.h>
-
+#include <fcntl.h>
+#include <unistd.h>
 #include <hal/hal-bluetooth-interface.h>
 
 #undef LOG_TAG
 
 #define EXPORT __attribute__ ((visibility("default")))
 
+#define WIFI_CP2_FIRMWARE_STATE_PATH "/tmp/.wifi-firmware-loaded"
+#define BT_CP2_FIRMWARE_STATE_PATH "/tmp/.bt-firmware-loaded"
+#define BT_CP2_FIRMWARE_PATH "/hal/bin/cp2-downloader"
+#define MAX_SIZE_ERROR_BUFFER 256
+
+int __bt_execute_file(const char *file_path,
+               char *const args[], char *const envs[])
+{
+       pid_t pid = 0;
+       int status = 0;
+       int rv = 0;
+       errno = 0;
+       register unsigned int index = 0;
+       char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
+
+       while (args[index] != NULL) {
+               LOGD("%s", args[index]);
+               index++;
+       }
+
+       if (!(pid = fork())) {
+               LOGD("pid(%d), ppid (%d)", getpid(), getppid());
+               LOGD("Inside child, exec (%s) command", file_path);
+
+               errno = 0;
+               if (execve(file_path, args, envs) == -1) {
+                       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+                       LOGE("Fail to execute command (%s)", error_buf);
+                       exit(1);
+               }
+       } else if (pid > 0) {
+               if (waitpid(pid, &status, 0) == -1)
+                       LOGD("wait pid (%u) status (%d)", pid, status);
+
+               if (WIFEXITED(status)) {
+                       rv = WEXITSTATUS(status);
+                       LOGD("exited, status=%d", rv);
+               } else if (WIFSIGNALED(status)) {
+                       LOGD("killed by signal %d", WTERMSIG(status));
+               } else if (WIFSTOPPED(status)) {
+                       LOGD("stopped by signal %d", WSTOPSIG(status));
+               } else if (WIFCONTINUED(status)) {
+                       LOGD("continued");
+               }
+
+               return rv;
+       }
+
+       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+       LOGD("failed to fork(%s)", error_buf);
+       return -EIO;
+}
+
+static void __bt_firmware_update_state(void)
+{
+    int fd;
+    mode_t mode = S_IRGRP | S_IWUSR | S_IXGRP;
+
+    fd = creat(BT_CP2_FIRMWARE_STATE_PATH, mode);
+    if (fd >= 0)
+        close(fd);
+    else
+        LOGE("Failed to create bt firmware state file");
+}
+
 static int bluetooth_tm1_start(void)
 {
        int ret;
+       char *const args[] = {BT_CP2_FIRMWARE_PATH, NULL};
+       char *const envs[] = {NULL};
+
+       if (access(WIFI_CP2_FIRMWARE_STATE_PATH, F_OK) != 0
+                       && access(BT_CP2_FIRMWARE_STATE_PATH, F_OK) != 0) {
+               ret = __bt_execute_file(BT_CP2_FIRMWARE_PATH, args, envs);
+               if (ret < 0) {
+                       LOGE("bt firmware download failed");
+                       return HAL_BACKEND_ERROR_INTERNAL;
+               }
+               __bt_firmware_update_state();
+               LOGD("bt firmware download succeeded");
+       }
+
        ret = system("/hal/etc/bluetooth/bt-dev-start.sh");
        if (ret == 0x100) {
                LOGE("script internal failed");