/* * Copyright (c) 2025 Amlogic, Inc. All rights reserved. * * This source code is subject to the terms and conditions defined in the * file 'LICENSE' which is part of this source code package. * * */ #include "utils.h" #include #include #include #include #include #include #include "aml_log.h" /** * Check if the specified ALSA device is playing audio using /proc/asound. * @param device_name ALSA device name, e.g., "hw:0,1". * @return true if the device is playing audio (RUNNING state), false otherwise or on error. */ bool alsa_is_audio_playing(const char *device_name) { // Validate input if (!device_name || strncmp(device_name, "hw:", 3) != 0) { AML_LOGE( "Invalid device name format: %s\n", device_name ? device_name : "null"); return false; } // Extract card and device numbers from "hw:card,device" int card_num, device_num; if (sscanf(device_name, "hw:%d,%d", &card_num, &device_num) != 2) { AML_LOGE( "Failed to parse device name: %s\n", device_name); return false; } // Construct path to status file, e.g., /proc/asound/card0/pcm1p/sub0/status char proc_path[64]; snprintf(proc_path, sizeof(proc_path), "/proc/asound/card%d/pcm%dp/sub0/status", card_num, device_num); // Open status file FILE *fp = fopen(proc_path, "r"); if (!fp) { AML_LOGE( "Failed to open %s: %s\n", proc_path, strerror(errno)); return false; } // Read status char status[32]; if (!fgets(status, sizeof(status), fp)) { AML_LOGE( "Failed to read status from %s\n", proc_path); fclose(fp); return false; } fclose(fp); // Check if status contains "RUNNING" bool is_playing = (strstr(status, "RUNNING") != NULL); return is_playing; } /** * Checks if a specified process is running using the `pidof` command. * * @param process_name The name of the process to check (e.g., "bluetoothd"). * @return true if the process is running, false otherwise or if an error occurs. */ bool is_process_running(const char *process_name) { // Validate input if (process_name == NULL || *process_name == '\0') { fprintf(stderr, "Error: Invalid process name\n"); return false; } // Use stack memory for command buffer (fixed size to avoid overflow) char cmd[128]; if (snprintf(cmd, sizeof(cmd), "pidof %s > /dev/null", process_name) >= (int)sizeof(cmd)) { fprintf(stderr, "Error: Process name too long: %s\n", process_name); return false; } // Execute command and check status int status = system(cmd); if (status == -1) { fprintf(stderr, "Error: Failed to execute pidof for %s\n", process_name); return false; } // Return true if pidof found the process (exit status 0) return WIFEXITED(status) && WEXITSTATUS(status) == 0; } int lea_util_file_create(char *filename, const char *text) { FILE *fp; fp = fopen(filename, "w"); if (fp == NULL) { perror("Error creating file.\n"); return -1; } if (text) { fprintf(fp, "%s", text); } fflush(fp); fsync(fileno(fp)); fclose(fp); return 0; } int lea_util_file_read(char *filename, char *buffer, int size) { FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Can not open file %s\n", filename); return -1; } int read_size = fread(buffer, sizeof(char), size, fp); buffer[read_size] = '\0'; fclose(fp); return 0; } int lea_util_file_get_size(char *filename) { struct stat file_state; if (!filename) return -1; if (stat(filename, &file_state) != 0) { AML_LOGE("Failed to get file information:%s\n",filename); return -1; } return file_state.st_size; }