#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");