| b.liu | 88fbcc6 | 2024-03-18 17:10:03 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | *    mbtk_audio_ubus.c | 
|  | 3 | * | 
|  | 4 | *    MBTK audio ubus client API. | 
|  | 5 | * | 
|  | 6 | */ | 
|  | 7 | /****************************************************************************** | 
|  | 8 |  | 
|  | 9 | EDIT HISTORY FOR FILE | 
|  | 10 |  | 
|  | 11 | WHEN        WHO       WHAT,WHERE,WHY | 
|  | 12 | --------    --------    ------------------------------------------------------- | 
|  | 13 | 2024/3/18     LiuBin      Initial version | 
|  | 14 |  | 
|  | 15 | ******************************************************************************/ | 
|  | 16 | #include <fcntl.h> | 
|  | 17 | #include <stdint.h> | 
|  | 18 | #include <limits.h> | 
|  | 19 | #include <termios.h> | 
|  | 20 | #include <stdarg.h> | 
|  | 21 | #include <dirent.h> | 
|  | 22 | #include <sys/stat.h> | 
|  | 23 | #include <sys/statfs.h> | 
|  | 24 | #include <sys/types.h> | 
|  | 25 | #include "mbtk_log.h" | 
|  | 26 | #include "mbtk_type.h" | 
|  | 27 | #include <libubox/blobmsg_json.h> | 
|  | 28 | #include "libubus.h" | 
|  | 29 | #include "mbtk_audio_ubus.h" | 
|  | 30 |  | 
|  | 31 | #define AUDIO_UBUS_TIMEOUT 5000     // 3s | 
|  | 32 |  | 
|  | 33 | #define AUDIO_UBUS_REQUEST_NAME         "audio_if" | 
|  | 34 | #define AUDIO_UBUS_VOLUME_SET           "volume_set" | 
|  | 35 | #define AUDIO_UBUS_VOLUME_GET           "volume_status" | 
|  | 36 | #define AUDIO_UBUS_MODE_SET             "audio_mode_set" | 
|  | 37 | #define AUDIO_UBUS_PATH_ENABLE          "audio_path_enable" | 
|  | 38 | #define AUDIO_UBUS_SWITCH_PCM           "switch_pcm" | 
|  | 39 | #define AUDIO_UBUS_DSP_SET              "config_dspgain" | 
|  | 40 | #define AUDIO_UBUS_LOOPBACK_EN          "loopback_enable" | 
|  | 41 | #define AUDIO_UBUS_LOOPBACK_DIS         "loopback_disable" | 
|  | 42 | #define AUDIO_UBUS_AUDIO_GAIN_SET       "audio_gain_set" | 
|  | 43 | #define AUDIO_UBUS_AUDIO_REG_SET        "audio_reg_set" | 
|  | 44 |  | 
|  | 45 | static struct ubus_context *audio_ctx = NULL; | 
|  | 46 | static uint32_t ubus_id_audio_if = 0; | 
|  | 47 |  | 
|  | 48 | static void ubus_complete_cb(struct ubus_request *req, int rc) | 
|  | 49 | { | 
|  | 50 | if(audio_ctx == NULL) { | 
|  | 51 | LOGE("MBTK audio ubus not inited."); | 
|  | 52 | return; | 
|  | 53 | } | 
|  | 54 | if (req) | 
|  | 55 | { | 
|  | 56 | free(req); | 
|  | 57 | req = NULL; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | LOGD("ubus_complete_cb() , rc = %d", rc); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | int mbtk_audio_ubus_init() | 
|  | 64 | { | 
|  | 65 | if(audio_ctx) { | 
|  | 66 | LOGE("MBTK audio ubus has inited."); | 
|  | 67 | return -1; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | audio_ctx = ubus_connect(NULL); | 
|  | 71 | if (!audio_ctx) { | 
|  | 72 | LOGE("Failed to connect to ubus."); | 
|  | 73 | return -1; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | int ret = ubus_lookup_id(audio_ctx, AUDIO_UBUS_REQUEST_NAME, &ubus_id_audio_if); | 
|  | 77 | if (ret) { | 
|  | 78 | LOGE("ubus_lookup_id() fail."); | 
|  | 79 | return ret; | 
|  | 80 | } | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | int mbtk_audio_ubus_deinit() | 
|  | 85 | { | 
|  | 86 | if(audio_ctx) { | 
|  | 87 | ubus_free(audio_ctx); | 
|  | 88 | audio_ctx = NULL; | 
|  | 89 | ubus_id_audio_if = 0; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | return 0; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | /** | 
|  | 96 | * @brief      mbtk_audio_mode_set | 
|  | 97 | * | 
|  | 98 | * @details    detailed description | 
|  | 99 | * | 
|  | 100 | * @param      param | 
|  | 101 | *             "param0": UINT32 | 
|  | 102 | *             typedef enum { | 
|  | 103 | *                 AUDIO_MODE_INVALID = -2, | 
|  | 104 | *                 AUDIO_MODE_CURRENT = -1, | 
|  | 105 | *                 AUDIO_MODE_NORMAL = 0, | 
|  | 106 | *                 AUDIO_MODE_RINGTONE = 1, | 
|  | 107 | *                 AUDIO_MODE_IN_CALL = 2, | 
|  | 108 | *                 AUDIO_MODE_IN_COMMUNICATION=3, | 
|  | 109 | *                 AUDIO_MODE_IN_VT_CALL= 4, | 
|  | 110 | *                 AUDIO_MODE_CNT, | 
|  | 111 | *                 AUDIO_MODE_MAX = AUDIO_MODE_CNT-1, | 
|  | 112 | *             } audio_mode_ | 
|  | 113 | * @return     return type | 
|  | 114 | */ | 
|  | 115 | int mbtk_audio_mode_set(int mode) | 
|  | 116 | { | 
|  | 117 | if(audio_ctx == NULL || ubus_id_audio_if == 0) { | 
|  | 118 | LOGE("MBTK audio ubus not inited."); | 
|  | 119 | return -1; | 
|  | 120 | } | 
|  | 121 | static struct blob_buf b; | 
|  | 122 | int ret; | 
|  | 123 | blob_buf_init(&b, 0); | 
|  | 124 | blobmsg_add_u32(&b, "param0", mode); | 
|  | 125 | if((ret = ubus_invoke(audio_ctx, ubus_id_audio_if, AUDIO_UBUS_MODE_SET, b.head, NULL, NULL, AUDIO_UBUS_TIMEOUT)) != UBUS_STATUS_OK) { | 
|  | 126 | LOGE("ubus_invoke fail:%d.\n", ret); | 
|  | 127 | return -1; | 
|  | 128 | } else { | 
|  | 129 | LOGD("ubus_invoke success.\n"); | 
|  | 130 | return 0; | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | /** | 
|  | 135 | * @brief      mbtk_audio_loopback_start | 
|  | 136 | * | 
|  | 137 | * @details    detailed description | 
|  | 138 | * | 
|  | 139 | * @param      param | 
|  | 140 | *             device: UINT32 | 
|  | 141 | *             0: earpiece | 
|  | 142 | *             1: speaker | 
|  | 143 | *             2: headset | 
|  | 144 | * @return     return type | 
|  | 145 | */ | 
|  | 146 | int mbtk_audio_loopback_start(int device) | 
|  | 147 | { | 
|  | 148 | int rc = 0; | 
|  | 149 | struct ubus_request *req = NULL; | 
|  | 150 |  | 
|  | 151 | if(audio_ctx == NULL || ubus_id_audio_if == 0) { | 
|  | 152 | LOGE("MBTK audio ubus not inited."); | 
|  | 153 | return -1; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | req = (struct ubus_request *)malloc(sizeof(struct ubus_request)); | 
|  | 157 | if (req == NULL) | 
|  | 158 | { | 
|  | 159 | LOGE("leave %s: lack of memory", __FUNCTION__); | 
|  | 160 | return -1; | 
|  | 161 | } | 
|  | 162 | static struct blob_buf b; | 
|  | 163 | memset(req, 0, sizeof(struct ubus_request)); | 
|  | 164 | blob_buf_init(&b, 0); | 
|  | 165 | blobmsg_add_u32(&b, "param0", device); | 
|  | 166 | if ((rc = ubus_invoke_async(audio_ctx, | 
|  | 167 | ubus_id_audio_if, | 
|  | 168 | AUDIO_UBUS_LOOPBACK_EN, | 
|  | 169 | b.head, req)) != UBUS_STATUS_OK) { | 
|  | 170 | free(req); | 
|  | 171 | LOGE("ubus_invoke_async %s failed %s", AUDIO_UBUS_LOOPBACK_EN, ubus_strerror(rc)); | 
|  | 172 | return -1; | 
|  | 173 | } | 
|  | 174 | else | 
|  | 175 | { | 
|  | 176 | LOGD("ubus_invoke_async %s success", AUDIO_UBUS_LOOPBACK_EN); | 
|  | 177 | req->complete_cb = ubus_complete_cb; | 
|  | 178 | ubus_complete_request_async(audio_ctx, req); | 
|  | 179 | return 0; | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | int mbtk_audio_loopback_stop() | 
|  | 184 | { | 
|  | 185 | int rc = 0; | 
|  | 186 |  | 
|  | 187 | if(audio_ctx == NULL || ubus_id_audio_if == 0) { | 
|  | 188 | LOGE("MBTK audio ubus not inited."); | 
|  | 189 | return -1; | 
|  | 190 | } | 
|  | 191 | static struct blob_buf b; | 
|  | 192 | blob_buf_init(&b, 0); | 
|  | 193 | if ((rc = ubus_invoke(audio_ctx, | 
|  | 194 | ubus_id_audio_if, | 
|  | 195 | AUDIO_UBUS_LOOPBACK_DIS, | 
|  | 196 | b.head, NULL, NULL, AUDIO_UBUS_TIMEOUT)) != UBUS_STATUS_OK) | 
|  | 197 | { | 
|  | 198 | LOGE("ubus_invoke %s failed %s", AUDIO_UBUS_LOOPBACK_DIS, ubus_strerror(rc)); | 
|  | 199 | return -1; | 
|  | 200 | } | 
|  | 201 | else | 
|  | 202 | { | 
|  | 203 | LOGD("ubus_invoke %s success", AUDIO_UBUS_LOOPBACK_DIS); | 
|  | 204 | return 0; | 
|  | 205 | } | 
|  | 206 | } | 
|  | 207 |  | 
| luojian | 9e11c22 | 2024-06-25 19:54:52 +0800 | [diff] [blame] | 208 | /** | 
|  | 209 | * @brief      mbtk_audio_dsp_gain_set | 
|  | 210 | * | 
|  | 211 | * @details    dsp gain set | 
|  | 212 | * | 
|  | 213 | * @param      param | 
|  | 214 | *             type: 0:tx | 
|  | 215 | *             0: tx | 
|  | 216 | *             1: rx | 
|  | 217 | *             gain: -36~12 db | 
|  | 218 |  | 
|  | 219 | * @return     return | 
|  | 220 | */ | 
|  | 221 | int mbtk_audio_dsp_gain_set(int type, int gain) | 
|  | 222 | { | 
|  | 223 | int rc = 0; | 
|  | 224 | struct ubus_request *req = NULL; | 
|  | 225 |  | 
|  | 226 | if(audio_ctx == NULL || ubus_id_audio_if == 0) { | 
|  | 227 | LOGE("MBTK audio ubus not inited."); | 
|  | 228 | return -1; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | req = (struct ubus_request *)malloc(sizeof(struct ubus_request)); | 
|  | 232 | if (req == NULL) | 
|  | 233 | { | 
|  | 234 | LOGE("leave %s: lack of memory", __FUNCTION__); | 
|  | 235 | return -1; | 
|  | 236 | } | 
|  | 237 | static struct blob_buf b; | 
|  | 238 | memset(req, 0, sizeof(struct ubus_request)); | 
|  | 239 | blob_buf_init(&b, 0); | 
|  | 240 | blobmsg_add_u32(&b, "type", type); | 
|  | 241 | blobmsg_add_u32(&b, "gain", gain); | 
|  | 242 | if ((rc = ubus_invoke_async(audio_ctx, | 
|  | 243 | ubus_id_audio_if, | 
|  | 244 | AUDIO_UBUS_DSP_SET, | 
|  | 245 | b.head, req)) != UBUS_STATUS_OK) { | 
|  | 246 | free(req); | 
|  | 247 | LOGE("ubus_invoke_async %s failed %s", AUDIO_UBUS_DSP_SET, ubus_strerror(rc)); | 
|  | 248 | return -1; | 
|  | 249 | } | 
|  | 250 | else | 
|  | 251 | { | 
|  | 252 | LOGD("ubus_invoke_async %s success", AUDIO_UBUS_DSP_SET); | 
|  | 253 | req->complete_cb = ubus_complete_cb; | 
|  | 254 | ubus_complete_request_async(audio_ctx, req); | 
|  | 255 | return 0; | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 |  |