rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <string.h> |
| 2 | #include <stdio.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <unistd.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <pthread.h> |
| 9 | |
| 10 | #include "modem_afe_ctrl.h" |
| 11 | static char *server_rcv = "/tmp/libmodem-afe-ctrl/server_rcv"; |
| 12 | static char *server_send = "/tmp/libmodem-afe-ctrl/server_send"; |
| 13 | |
| 14 | int rcv_handler; |
| 15 | int send_handler; |
| 16 | |
| 17 | static pthread_t g_pthread_rcv; |
| 18 | static pthread_t g_pthread_dummy; |
| 19 | |
| 20 | /* read message, exec func and return result*/ |
| 21 | static void *func_operator(void *arg); |
| 22 | |
| 23 | /* a dummy operator to hold server_rcv pipe*/ |
| 24 | static void *dummy_operator(void *arg); |
| 25 | |
| 26 | static void *func_operator(void *arg) |
| 27 | { |
| 28 | char buf[256]; |
| 29 | char string_out[256]; |
| 30 | int size = 0, function_type = 0, first_param = 0; |
| 31 | int str_output_size = 0; |
| 32 | int func_result; |
| 33 | char *first_param_str; |
| 34 | char buf_response[256]; |
| 35 | |
| 36 | rcv_handler = open(server_rcv, O_RDONLY); |
| 37 | send_handler = open(server_send, O_WRONLY); |
| 38 | |
| 39 | while(1){ |
| 40 | size = read(rcv_handler, buf, 256); |
| 41 | if(!size) { |
| 42 | printf("reset fifo! \n"); |
| 43 | system("echo reset_pipe"); |
| 44 | close(rcv_handler); |
| 45 | rcv_handler = open(server_rcv, O_RDONLY); |
| 46 | continue; |
| 47 | } |
| 48 | /* finish string */ |
| 49 | buf[size] = '\0'; |
| 50 | |
| 51 | /* parse string */ |
| 52 | strtok_r(buf, ",", &first_param_str); |
| 53 | function_type = atoi(buf); |
| 54 | switch(function_type) { |
| 55 | case FUNC_ENABLE_PHONE_CALL_AFE_PATH: |
| 56 | first_param = atoi(first_param_str); |
| 57 | func_result = enable_phone_call_AFE_path_for_service(first_param); |
| 58 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 59 | break; |
| 60 | case FUNC_DISABLE_PHONE_CALL_AFE_PATH: |
| 61 | func_result = disable_phone_call_AFE_path_for_service(); |
| 62 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 63 | break; |
| 64 | case FUNC_GET_DEFAULT_VALUE: |
| 65 | func_result = get_default_device_for_service(); |
| 66 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 67 | break; |
| 68 | case FUNC_SET_UL_ANALOG_GAIN: |
| 69 | first_param = atoi(first_param_str); |
| 70 | func_result = set_UL_analog_gain_for_service(first_param); |
| 71 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 72 | break; |
| 73 | case FUNC_SET_DL_ANALOG_GAIN: |
| 74 | first_param = atoi(first_param_str); |
| 75 | func_result = set_DL_analog_gain_for_service(first_param); |
| 76 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 77 | break; |
| 78 | case FUNC_DYNAMIC_SWITCH_PHONE_CALL_PATH: |
| 79 | first_param = atoi(first_param_str); |
| 80 | func_result = dynamic_switch_phone_call_path_for_service(first_param); |
| 81 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 82 | break; |
| 83 | case FUNC_SET_BT_WB: |
| 84 | first_param = atoi(first_param_str); |
| 85 | func_result = set_BT_wb(first_param); |
| 86 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 87 | break; |
| 88 | case FUNC_SET_PHONE_CALL_SAMPLERATE: |
| 89 | first_param = atoi(first_param_str); |
| 90 | func_result = set_phonecall_rate_for_service(first_param); |
| 91 | str_output_size = sprintf(buf_response, "%d", func_result); |
| 92 | break; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | /*send result*/ |
| 98 | write(send_handler, buf_response, str_output_size); |
| 99 | |
| 100 | } |
| 101 | close(send_handler); |
| 102 | close(rcv_handler); |
| 103 | } |
| 104 | |
| 105 | static void *dummy_operator(void *arg) |
| 106 | { |
| 107 | int dummy_handler = 0; |
| 108 | dummy_handler = open(server_rcv, O_WRONLY); |
| 109 | while(1) { |
| 110 | sleep(1000000); |
| 111 | } |
| 112 | close(dummy_handler); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | int main() |
| 117 | { |
| 118 | |
| 119 | struct stat st = {0}; |
| 120 | |
| 121 | if (stat("/tmp/libmodem-afe-ctrl", &st) == -1) { |
| 122 | mkdir("/tmp/libmodem-afe-ctrl", 0700); |
| 123 | } |
| 124 | |
| 125 | mkfifo(server_rcv, 0600); |
| 126 | mkfifo(server_send, 0600); |
| 127 | |
| 128 | pthread_create(&g_pthread_rcv, NULL, func_operator, NULL); |
| 129 | /* make a client to prevent rcv pipe exit*/ |
| 130 | pthread_create(&g_pthread_dummy, NULL, dummy_operator, NULL); |
| 131 | |
| 132 | while(1){ |
| 133 | sleep(1000000); |
| 134 | } |
| 135 | |
| 136 | unlink(server_rcv); |
| 137 | unlink(server_send); |
| 138 | |
| 139 | return 0; |
| 140 | } |