static int bt_register_timer(void) { struct sigevent sev; struct sigaction sa; int sig_bt = SIGRTMIN; /* user defined signal area min */ LOG_TRC(); sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = sig_bt; sev.sigev_value.sival_ptr = &timerid; if (timer_create(CLOCK_REALTIME, &sev, &timerid) < 0){ LOG_ERR("Create timer fails errno %d\n", errno); return -1; } sa.sa_sigaction = bt_host_sleep; sa.sa_flags = SA_SIGINFO; if (sigaction(sig_bt, &sa, NULL) < 0){ LOG_ERR("Register sigaction fails errno %d\n", errno); return -1; } return 0; }
int mtk_bt_disable(int bt_fd) { LOG_TRC(); if (!glib_handle){ LOG_ERR("mtk bt library is unloaded!\n"); return -1; } #ifndef MTK_COMBO_SUPPORT bt_unregister_timer(); bt_hold_wake_lock(1); maskEint(); /* wait until thread exist */ pthread_join(eint_thr, NULL); #endif bt_restore(bt_fd); dlclose(glib_handle); glib_handle = NULL; #ifndef MTK_COMBO_SUPPORT bt_set_power(0); bt_hold_wake_lock(0); sleep_mode = 0; #endif return 0; }
static int bt_set_power(int on) { int sz; int fd = -1; int ret = -1; const char buf = (on ? '1' : '0'); LOG_TRC(); if (bt_rfkill_id == -1) { if (bt_init_rfkill()) goto out; } fd = open(bt_rfkill_state_path, O_WRONLY); if (fd < 0) { LOG_ERR("Open %s to set BT power fails: %s(%d)\n", bt_rfkill_state_path, strerror(errno), errno); goto out; } sz = write(fd, &buf, 1); if (sz < 0) { LOG_ERR("Write %s fails: %s(%d)\n", bt_rfkill_state_path, strerror(errno), errno); goto out; } ret = 0; out: if (fd >= 0) close(fd); return ret; }
static int bt_init_rfkill(void) { char path[128]; char buf[32]; int fd, id; ssize_t sz; LOG_TRC(); for (id = 0; id < 10 ; id++) { snprintf(path, sizeof(path), "/sys/class/rfkill/rfkill%d/type", id); fd = open(path, O_RDONLY); if (fd < 0) { LOG_ERR("Open %s fails: %s(%d)\n", path, strerror(errno), errno); return -1; } sz = read(fd, &buf, sizeof(buf)); close(fd); if (sz >= (ssize_t)strlen(BT_DRV_MOD_NAME) && memcmp(buf, BT_DRV_MOD_NAME, strlen(BT_DRV_MOD_NAME)) == 0) { bt_rfkill_id = id; break; } } if (id == 10) return -1; asprintf(&bt_rfkill_state_path, "/sys/class/rfkill/rfkill%d/state", bt_rfkill_id); return 0; }
static void *bt_eint_monitor(void *ptr) { int ret = 0; struct pollfd fds[1]; int bt_fd; LOG_TRC(); bt_fd = (int)ptr; eint_fd = open(BTWLANEM_DEVNAME, O_RDWR | O_NOCTTY); if (eint_fd < 0){ LOG_ERR("Can't get %s fd to handle EINT\n", BTWLANEM_DEVNAME); return 0; } unmaskEint(); fds[0].fd = eint_fd; fds[0].events = POLLIN; while(1) { ret = poll(fds, 1, -1); if(ret > 0){ if(fds[0].revents & POLLIN){ LOG_DBG("EINT arrives! Notify host wake up\n"); sleep_mode = 0; if(bt_host_wake_up(bt_fd) < 0){ LOG_ERR("Send host wake up command fails\n"); } unmaskEint(); } else if(fds[0].revents & POLLERR){ LOG_DBG("EINT monitor needs to exit\n"); goto exit; } } else if ((ret == -1) && (errno == EINTR)){ LOG_ERR("poll error EINTR\n"); } else{ LOG_ERR("poll error %s(%d)!\n", strerror(errno), errno); goto exit; } } exit: close(eint_fd); eint_fd = -1; return 0; }
int mtk_bt_disable(int bt_fd) { LOG_TRC(); if (!glib_handle){ LOG_ERR("mtk bt library is unloaded!\n"); return -1; } bt_restore(bt_fd); dlclose(glib_handle); glib_handle = NULL; return 0; }
int mtk_bt_enable(int flag, void *func_cb) { const char *errstr; int bt_fd = -1; LOG_TRC(); glib_handle = dlopen("libbluetooth_mtk.so", RTLD_LAZY); if (!glib_handle){ LOG_ERR("%s\n", dlerror()); goto error; } dlerror(); /* Clear any existing error */ mtk = dlsym(glib_handle, "mtk"); bt_restore = dlsym(glib_handle, "bt_restore"); write_com_port = dlsym(glib_handle, "write_com_port"); read_com_port = dlsym(glib_handle, "read_com_port"); if ((errstr = dlerror()) != NULL){ LOG_ERR("Can't find function symbols %s\n", errstr); goto error; } bt_fd = mtk(); if (bt_fd < 0) goto error; LOG_DBG("BT is enabled success\n"); return bt_fd; error: if (glib_handle){ dlclose(glib_handle); glib_handle = NULL; } return -1; }
void WebInterface::worker() { /* Backup the stdio streambufs */ std::streambuf * cin_streambuf = std::cin.rdbuf(); std::streambuf * cout_streambuf = std::cout.rdbuf(); std::streambuf * cerr_streambuf = std::cerr.rdbuf(); const std::string kw_title(KW_TITLE); const std::string kw_head(KW_HEAD); const std::string kw_menu(KW_MENU); const std::string kw_content(KW_CONTENT); FCGX_Request request; /* Initialize FastCGI library and request */ FCGX_Init(); FCGX_InitRequest(&request, 0, FCGI_FAIL_ACCEPT_ON_INTR); LOG_DBG("FastCGI initialization success!"); while (!stop_flag_) { if(FCGX_Accept_r(&request) >= 0) { fcgi_streambuf cin_fcgi_streambuf(request.in); fcgi_streambuf cout_fcgi_streambuf(request.out); fcgi_streambuf cerr_fcgi_streambuf(request.err); std::cin.rdbuf(&cin_fcgi_streambuf); std::cout.rdbuf(&cout_fcgi_streambuf); std::cerr.rdbuf(&cerr_fcgi_streambuf); /* getting the uri from the request */ std::string uri; const char *uri_param = FCGX_GetParam("REQUEST_URI", request.envp); if(!uri_param) { LOG_ERR("Failed to retrieve the request URI environment value!"); uri = URI_PAGE_ERROR; } else { uri = uri_param; } LOG_DBG("Request received: %s", uri.c_str()); /* Check if URI is a file in the home folder and get the mime of * that file (by extension) */ std::string path; std::string mime = if_file_get_mime(uri, &path); if (!mime.empty()) { /* This is a file we need to serve */ StringPtr file_data = Utils::read_file(path); std::cout << "Content-type: " << mime << "\r\n\r\n"; std::cout << *(file_data); file_data.reset(); } else { /* Parse the URI */ std::map<std::string, std::string> uri_data = parseURI(uri); LOG_DBG("URI Parsed, page requested: %s", uri_data[URI_PAGE].c_str()); /* Generate and serve the page depending on the URI */ StringPtr page; std::string content_type = "text/html"; /* Main page requested */ if (uri_data[URI_PAGE].compare(URI_PAGE_MAIN) == 0) { bool success = false; /* Check if a command was sent from the client. */ if (uri_data.find(URI_PAGE_COMMAND) != uri_data.end()) { success = add_command(uri_data[URI_PAGE_COMMAND]); } std::string s; /* Check if the request was sent from javascript or pure HTML */ if(uri_data.find(URI_PAGE_SOURCE) != uri_data.end()) { LOG_DBG("This query's source IS javascript: %s, Source: %s", uri.c_str(), (uri_data[URI_PAGE_SOURCE]).c_str()); content_type = "application/json"; page = generate_command_json(success); } else { LOG_DBG("This query's source IS NOT javascript: %s", uri.c_str()); /* Just generate a standard main page */ page = generate_main_page(); } /* Log page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_LOG) == 0) { page = generate_log_page(); /* Status page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_STATUS) == 0) { page = generate_status_page(); /* Console lines JSON page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_CL) == 0) { if (uri_data.find(URI_PAGE_BEFORE) != uri_data.end()) { content_type = "application/json"; page = generate_cljson_before( uri_data[URI_PAGE_BEFORE]); } else if (uri_data.find(URI_PAGE_AFTER) != uri_data.end()) { content_type = "application/json"; page = generate_cljson_after(uri_data[URI_PAGE_AFTER]); } else { page = generate_error_page(); } /* Log lines JSON page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_LL) == 0) { if (uri_data.find(URI_PAGE_BEFORE) != uri_data.end()) { content_type = "application/json"; page = generate_lljson_before( uri_data[URI_PAGE_BEFORE]); } else if (uri_data.find(URI_PAGE_AFTER) != uri_data.end()) { content_type = "application/json"; page = generate_lljson_after(uri_data[URI_PAGE_AFTER]); } else { page = generate_error_page(); } } else { page = generate_error_page(); } /* Output the generated page with the correct content type */ std::cout << "Content-type: " << content_type << "\r\n\r\n"; std::cout << *(page.get()); } } else { LOG_TRC("FCGX_Aceept_r returned less than 0!"); } } LOG_TRC("Out of accept request loop!"); // Free request strucure FCGX_Finish_r(&request); // Flag the thread as not running anymore. running_ = false; // restore stdio streambufs std::cin.rdbuf(cin_streambuf); std::cout.rdbuf(cout_streambuf); std::cerr.rdbuf(cerr_streambuf); }
int mtk_bt_enable(int flag, void *func_cb) { const char *errstr; struct uart_t u; int bt_fd = -1; LOG_TRC(); #ifndef MTK_COMBO_SUPPORT bt_hold_wake_lock(1); /* In case BT is powered on before test */ bt_set_power(0); if(bt_set_power(1) < 0) { LOG_ERR("BT power on fails\n"); return -1; } #endif glib_handle = dlopen("libbluetooth_mtk.so", RTLD_LAZY); if (!glib_handle){ LOG_ERR("%s\n", dlerror()); goto error; } dlerror(); /* Clear any existing error */ mtk = dlsym(glib_handle, "mtk"); bt_restore = dlsym(glib_handle, "bt_restore"); write_comm_port = dlsym(glib_handle, "write_comm_port"); read_comm_port = dlsym(glib_handle, "read_comm_port"); bt_send_data = dlsym(glib_handle, "bt_send_data"); bt_receive_data = dlsym(glib_handle, "bt_receive_data"); if ((errstr = dlerror()) != NULL){ LOG_ERR("Can't find function symbols %s\n", errstr); goto error; } #ifndef MTK_COMBO_SUPPORT u.flags &= ~FLOW_CTL_MASK; u.flags |= FLOW_CTL_SW; u.speed = CUST_BT_BAUD_RATE; #endif bt_fd = mtk(-1, &u, NULL); if (bt_fd < 0) goto error; LOG_DBG("BT is enabled success\n"); #ifndef MTK_COMBO_SUPPORT bt_register_timer(); /* Create thread to poll EINT event */ pthread_create(&eint_thr, NULL, bt_eint_monitor, (void*)bt_fd); sched_yield(); #endif return bt_fd; error: if (glib_handle){ dlclose(glib_handle); glib_handle = NULL; } #ifndef MTK_COMBO_SUPPORT bt_set_power(0); bt_hold_wake_lock(0); #endif return -1; }
static void bt_unregister_timer(void) { LOG_TRC(); signal(SIGRTMIN, SIG_DFL); timer_delete(timerid); }