Add Audio gain set.
Change-Id: Id42678038875e9b0c1f89efbb609d7046e5527fb
diff --git a/mbtk/include/mbtk/mbtk_audio_gain.h b/mbtk/include/mbtk/mbtk_audio_gain.h
new file mode 100755
index 0000000..aa161d3
--- /dev/null
+++ b/mbtk/include/mbtk/mbtk_audio_gain.h
@@ -0,0 +1,89 @@
+/*
+* mbtk_audio_gain.h
+*
+* MBTK audio gain for nvm : /NVM/audio_gain.nvm.
+*
+* Author : lb
+* Date : 2024/3/1 16:04:46
+*/
+#ifndef _MBTK_AUDIO_GAIN_H
+#define _MBTK_AUDIO_GAIN_H
+#include "mbtk_type.h"
+
+#define MBTK_AUDIO_GAIN_ADD_START 0x0108
+#define MBTK_AUDIO_GAIN_MIN -36
+#define MBTK_AUDIO_GAIN_MAX 12
+#define CONFIG_DSPGAIN_MUTE_ON (-100)
+#define CONFIG_DSPGAIN_MUTE_OFF (100)
+
+#define MBTK_AUDIO_GAIN_PATH "/NVM/audio_gain.nvm"
+
+typedef enum
+{
+ CONFIG_DSPGAIN_TX = 0, /* TX: near to far end */
+ CONFIG_DSPGAIN_RX, /* RX: far to near end */
+// CONFIG_DSPGAIN_SIDETONE,
+// CONFIG_DSPGAIN_NUMBER = CONFIG_DSPGAIN_SIDETONE
+} CONFIG_DSPGAIN_Direction;
+
+typedef enum
+{
+ VC_HANDSET = 0,
+ VC_HANDSFREE,
+ VC_HEADSET,
+ VC_HEADPHONE,
+
+ VC_HEAD_NUM
+} AUDIO_PROFILE_ID;
+
+typedef enum
+{
+ AUDIOHAL_SPK_MUTE = 0,
+ AUDIOHAL_SPK_VOL_1,
+ AUDIOHAL_SPK_VOL_2,
+ AUDIOHAL_SPK_VOL_3,
+ AUDIOHAL_SPK_VOL_4,
+ AUDIOHAL_SPK_VOL_5,
+ AUDIOHAL_SPK_VOL_6,
+ AUDIOHAL_SPK_VOL_7,
+ AUDIOHAL_SPK_VOL_8,
+ AUDIOHAL_SPK_VOL_9,
+ AUDIOHAL_SPK_VOL_10,
+ AUDIOHAL_SPK_VOL_QTY //11
+} AUDIOHAL_SPK_LEVEL_T;
+
+typedef struct
+{
+ uint16 PGA_Stage1;
+ uint16 PGA_Stage2;
+ uint16 ADC_DigGain;
+ uint16 SideTone_Gain;
+ uint16 Reserved1;
+ uint16 Reserved2;
+} ACMCodec_GainInT;
+
+typedef struct
+{
+ uint16 DAC_DigGain;
+ uint16 DAC_Gain;
+ uint16 RCV_Gain;
+ uint16 ClassG_Mode;
+ uint16 ClassD_Gain;
+ uint16 Reserved;
+} ACMCodec_GainOutT;
+
+typedef struct{
+ AUDIO_PROFILE_ID Profile_id;
+ uint16 CodecControl;
+ ACMCodec_GainInT Tx_CodecGain; //TX has the same gain for every volume
+ int16 Tx_DSPGain; //TX has the same gain for every volume, High 8bits: WB gain, Low 8bits: NB gain
+ ACMCodec_GainOutT Rx_CodecGain[AUDIOHAL_SPK_VOL_QTY + 1];
+ int16 Rx_DSPGain[AUDIOHAL_SPK_VOL_QTY + 1]; //High 8bits: WB gain, Low 8bits: NB gain
+ int16 Rx_DSPSideToneGain; //High 8bits: WB gain, Low 8bits: NB gain
+} ACMCodec_GainT;
+
+int mbtk_dsp_gain_set(CONFIG_DSPGAIN_Direction dir, int gain);
+
+int mbtk_dsp_gain_get(int *rx_gain, int *tx_gain);
+
+#endif /* _MBTK_AUDIO_GAIN_H */
diff --git a/mbtk/mbtk_lib/Makefile b/mbtk/mbtk_lib/Makefile
index e709749..64caae9 100755
--- a/mbtk/mbtk_lib/Makefile
+++ b/mbtk/mbtk_lib/Makefile
@@ -85,7 +85,8 @@
src/mbtk_fota.c \
src/ringbuffer.c \
src/mbtk_mtd.c \
- src/mbtk_device_info.c
+ src/mbtk_device_info.c \
+ src/mbtk_audio_gain.c
ifeq ($(BUILD_PLATFORM), asr1803)
LOCAL_SRC_FILES += \
diff --git a/mbtk/mbtk_lib/src/mbtk_audio_alsa.c b/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
index 3e54453..45ce873 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio_alsa.c
@@ -81,6 +81,8 @@
static struct mopen_audio_t *internal_hdl = NULL;
+static int dsp_rx_gain = 0xFF;
+static int dsp_tx_gain = 0xFF;
int mbtk_wav_pcm16Le_check(int fd)
{
@@ -257,6 +259,14 @@
printf("%s: error reading!\n", __FUNCTION__);
break;
}
+
+ if(dsp_tx_gain == 0xFF) {
+ if(!mbtk_dsp_gain_get(&dsp_rx_gain, &dsp_tx_gain)) {
+ vcm_config_dspgain(CONFIG_DSPGAIN_TX, dsp_tx_gain);
+ dsp_rx_gain = 0xFF;
+ }
+ }
+
if ((bufsize > 0) && (NULL != _usrData->_cb))
{
_usrData->_cb(2, data, bufsize);
@@ -586,7 +596,7 @@
// {
// bufsize = len;
// }
-
+
while (pcxt->state != AUDIO_STOP)
{
@@ -595,7 +605,7 @@
if(read_size > len)
{
- // printf(">[%d]\n", read_size);
+ // printf(">[%d]\n", read_size);
break;
}
@@ -621,12 +631,12 @@
if(read_size == len)
{
- // printf("=[%d]", read_size);
+ // printf("=[%d]", read_size);
break;
}
}
-
+
if(pcxt->state != AUDIO_STOP)
{
mbtk_audio_set_status(dev_hdl, AUDIO_OPEN);
@@ -669,14 +679,14 @@
mbtk_audio_set_status(dev_hdl, AUDIO_RUNNING);
int all_size = 0;
-
+
while (pcxt->state != AUDIO_STOP)
{
res = read(file_fd, data, bufsize);
// printf("%s:read : %d bytes\n", __FUNCTION__, res);
if(res == 0 || res < 0)
{
- printf("read:[%d]", res);
+ printf("read:[%d]", res);
break;
}
@@ -709,6 +719,13 @@
audio_play_cb(pcxt, AUD_PLAYER_LESSDATA);
break;
}
+
+ if(dsp_rx_gain == 0xFF) {
+ if(!mbtk_dsp_gain_get(&dsp_rx_gain, &dsp_tx_gain)) {
+ vcm_config_dspgain(CONFIG_DSPGAIN_RX, dsp_rx_gain);
+ dsp_tx_gain = 0xFF;
+ }
+ }
}
if (audio_play_cb)
audio_play_cb(pcxt, AUD_PLAYER_FINISHED);
diff --git a/mbtk/mbtk_lib/src/mbtk_audio_gain.c b/mbtk/mbtk_lib/src/mbtk_audio_gain.c
new file mode 100755
index 0000000..f0a961b
--- /dev/null
+++ b/mbtk/mbtk_lib/src/mbtk_audio_gain.c
@@ -0,0 +1,156 @@
+/*
+* mbtk_audio_gain.c
+*
+* MBTK audio gain for nvm : /NVM/audio_gain.nvm.
+*
+*/
+/******************************************************************************
+
+ EDIT HISTORY FOR FILE
+
+ WHEN WHO WHAT,WHERE,WHY
+-------- -------- -------------------------------------------------------
+2024/3/1 LiuBin Initial version
+
+******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <cutils/properties.h>
+
+#include "mbtk_log.h"
+#include "mbtk_str.h"
+#include "mbtk_audio_gain.h"
+
+void vcm_config_dspgain(unsigned int type, unsigned int gain);
+
+int mbtk_dsp_gain_set(CONFIG_DSPGAIN_Direction dir, int gain)
+{
+ if((gain < MBTK_AUDIO_GAIN_MIN || gain > MBTK_AUDIO_GAIN_MAX)
+ && gain != CONFIG_DSPGAIN_MUTE_ON && gain != CONFIG_DSPGAIN_MUTE_OFF) {
+ LOGE("DSP gain range: %d - %d or %d or %d", MBTK_AUDIO_GAIN_MIN, MBTK_AUDIO_GAIN_MAX,
+ CONFIG_DSPGAIN_MUTE_ON, CONFIG_DSPGAIN_MUTE_OFF);
+ return -1;
+ }
+
+ int fd = open(MBTK_AUDIO_GAIN_PATH, O_RDWR);
+ if(fd < 0) {
+ LOGE("Open file(%s) fail:%d", MBTK_AUDIO_GAIN_PATH, errno);
+ return -1;
+ }
+
+ if( -1 == lseek(fd, MBTK_AUDIO_GAIN_ADD_START, SEEK_SET)) {
+ LOGE("lseek(%d) fail:%d", MBTK_AUDIO_GAIN_ADD_START, errno);
+ goto fail;
+ }
+
+ ACMCodec_GainT gains[VC_HEAD_NUM];
+ int len;
+ memset(gains, 0x0, sizeof(gains));
+ if((len = read(fd, gains, sizeof(gains))) != sizeof(gains)) {
+ LOGE("read(%d/%d) fail:%d", len, sizeof(gains), errno);
+ goto fail;
+ }
+
+ int i = 0;
+ while(i < VC_HEAD_NUM) {
+ if(dir == CONFIG_DSPGAIN_RX) {
+ gains[i].Rx_DSPGain[AUDIOHAL_SPK_VOL_QTY] = (int16)(gain & 0xFFFF);
+ } else if(dir == CONFIG_DSPGAIN_TX) {
+ gains[i].Tx_DSPGain = (int16)(gain & 0xFFFF);
+ }/* else if(dir == CONFIG_DSPGAIN_SIDETONE) {
+ gains[i].Rx_DSPSideToneGain = gain;
+ }*/ else {
+ LOGE("Unknown direction : %d", dir);
+ goto fail;
+ }
+ i++;
+ }
+
+ if( -1 == lseek(fd, MBTK_AUDIO_GAIN_ADD_START, SEEK_SET)) {
+ LOGE("lseek(%d) fail:%d", MBTK_AUDIO_GAIN_ADD_START, errno);
+ goto fail;
+ }
+
+ if(write(fd, gains, sizeof(gains)) != sizeof(gains)) {
+ LOGE("write(%d) fail:%d", sizeof(gains), errno);
+ goto fail;
+ }
+
+ if(fsync(fd)) {
+ LOGE("fsync(%d) fail:%d", fd, errno);
+ goto fail;
+ }
+
+ close(fd);
+
+ LOGD("DSP gain NVM update success.");
+
+ char buff[10];
+ memset(buff, 0, 10);
+ snprintf(buff, 10, "%d", gain);
+ if(dir == CONFIG_DSPGAIN_RX) {
+ if(property_set("persist.mbtk.dsp_rx_gain", buff) == 0) {
+ // Set gain to DSP.
+ vcm_config_dspgain(dir, gain);
+ }
+ } else if(dir == CONFIG_DSPGAIN_TX) {
+ if(property_set("persist.mbtk.dsp_tx_gain", buff) == 0) {
+ // Set gain to DSP.
+ vcm_config_dspgain(dir, gain);
+ }
+ }else {
+ LOGE("Unknown direction : %d", dir);
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ close(fd);
+ return -1;
+}
+
+int mbtk_dsp_gain_get(int *rx_gain, int *tx_gain)
+{
+ if(rx_gain == NULL || tx_gain == NULL) {
+ LOGE("gain is NULL.");
+ return -1;
+ }
+
+ int fd = open(MBTK_AUDIO_GAIN_PATH, O_RDONLY);
+ if(fd < 0) {
+ LOGE("Open file(%s) fail:%d", MBTK_AUDIO_GAIN_PATH, errno);
+ return -1;
+ }
+
+ if( -1 == lseek(fd, MBTK_AUDIO_GAIN_ADD_START, SEEK_SET)) {
+ LOGE("lseek(%d) fail:%d", MBTK_AUDIO_GAIN_ADD_START, errno);
+ goto fail;
+ }
+
+ ACMCodec_GainT gains[VC_HEAD_NUM];
+ int len;
+ memset(gains, 0x0, sizeof(gains));
+ if((len = read(fd, gains, sizeof(gains))) != sizeof(gains)) {
+ LOGE("read(%d/%d) fail:%d", len, sizeof(gains), errno);
+ goto fail;
+ }
+
+ close(fd);
+
+ *rx_gain = gains[VC_HANDSFREE].Rx_DSPGain[AUDIOHAL_SPK_VOL_QTY];
+ *tx_gain = gains[VC_HANDSFREE].Tx_DSPGain;
+ return 0;
+fail:
+ close(fd);
+ return -1;
+}
+
+
+
+
diff --git a/mbtk/test/asr1803/yx_audio_test.c b/mbtk/test/asr1803/yx_audio_test.c
index e01e9d9..740f152 100755
--- a/mbtk/test/asr1803/yx_audio_test.c
+++ b/mbtk/test/asr1803/yx_audio_test.c
@@ -4,13 +4,19 @@
#include <limits.h>
#include <termios.h>
#include <stdarg.h>
+#include <errno.h>
+#include <signal.h>
// #include "ql_at.h"
#include "ql/ql_audio.h"
// #include "mopen_tts.h"
-#define MBTK_AUD_DEMO_WAV "/data/demo.wav"
+#define MBTK_PLAY_PA_CONTROL 1
+#define MBTK_RECV_PA_CONTROL 1
-static int record_fd = 0;
+static int record_fd = -1;
+static int play_hdl = 0;
+static int gpio33_fd = -1;
+static int gpio34_fd = -1;
int Ql_cb_playback(int hdl, int result)
{
@@ -38,11 +44,6 @@
}
}
-void dtmf_cb1(char dtmf)
-{
- printf("%s:%c\n", __FUNCTION__, dtmf);
-}
-
int MBTK_wav_pcm16Le_check(int fd)
{
struct wav_header hdr;
@@ -77,6 +78,28 @@
return 0;
}
+static void audio_pa_open()
+{
+ if(gpio34_fd > 0) {
+ write(gpio34_fd, "1", 1);
+ }
+
+ if(gpio33_fd > 0) {
+ write(gpio33_fd, "0", 1);
+ }
+}
+
+static void audio_pa_close()
+{
+ if(gpio34_fd > 0) {
+ write(gpio34_fd, "0", 1);
+ }
+
+ if(gpio33_fd > 0) {
+ write(gpio33_fd, "1", 1);
+ }
+}
+
int MBTK_wav_pcm16Le_set(int fd)
{
struct wav_header hdr;
@@ -106,260 +129,157 @@
return 0;
}
-void aplay(void)
+
+static int play(const char *path)
{
- char operator[10];
- char databuf[1024];
- int opt = 0;
- int fd = 0;
- int size = 0;
- int state;
- int play_hdl = 0;
- int handler = 0;
-
- while(1)
- {
- printf("=========aplay========\n"
- "\t 0 Open PCM\n"
- "\t 1 Play Stream\n"
- "\t 2 Play file\n"
- "\t 3 Close\n"
- "\t others exit\n\n"
- "operator >> ");
-
- fflush(stdin);
- fgets(operator, sizeof(operator), stdin);
- opt = atoi(operator);
- switch (opt)
- {
- case 0:
- mbtk_audio_ubus_client_init(&handler, dtmf_cb1);
- mbtk_audio_switch_pcm(1);
- mbtk_audio_mode_set(2);
- play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
- if(0 == play_hdl)
- printf("Ql_AudPlayer_Open fail\n");
- break;
- case 1:
- if(0 == play_hdl)
- continue;
-
- fd = open(MBTK_AUD_DEMO_WAV, O_RDWR);
- if (fd <= 0)
- continue;
- mbtk_audio_path_enable(1);
-
- if(0 == MBTK_wav_pcm16Le_check(fd))
- {
- memset(databuf, 0, sizeof(databuf));
- while(0 < (size = read(fd, databuf, sizeof(databuf))))
- {
- if(-1 == Ql_AudPlayer_Play(play_hdl, databuf, size))
- break;
- }
- printf("aplay Stream end \n");
- }
- mbtk_audio_path_enable(0);
-
- close(fd);
- break;
- case 2:
- if(0 == play_hdl)
- continue;
- fd = open(MBTK_AUD_DEMO_WAV, O_RDWR);
- if (fd <= 0)
- continue;
-
- mbtk_audio_path_enable(1);
- if(0 == MBTK_wav_pcm16Le_check(fd))
- {
- Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0);
- }
- else
- {
- printf("aplay file type error\n");
- }
- close(fd);
- mbtk_audio_path_enable(0);
- break;
- case 3:
- if(0 == play_hdl)
- continue;
- Ql_AudPlayer_Close(play_hdl);
- mbtk_audio_mode_set(0);
- mbtk_audio_switch_pcm(0);
- break;
- case 4:
- // aplay_thread(MBTK_AUD_DEMO_WAV);
- Ql_Mp3_To_Play("/data/mp3demo.mp3", play_hdl, 0);
- break;
- default:
- return;
- }
-
- sleep(1);
+ int fd = open(path, O_RDONLY);
+ if(fd < 0) {
+ printf("open(%s) fail:%d\n", path, errno);
+ return -1;
}
- printf("aplay exit\n");
- return ;
-}
-void arec(void)
-{
- int ret;
- char operator[10];
- int opt;
- int hdl = 0;
- int handler = 0;
+#if MBTK_PLAY_PA_CONTROL
+ audio_pa_close();
+#endif
- while(1)
- {
- printf("=======arec======\n"
- "\t 0 Open PCM\n"
- "\t 1 Start Record\n"
- "\t 2 Get state\n"
- "\t 3 Pause\n"
- "\t 4 Resume\n"
- "\t 5 Stop\n"
- "\t 6 Close\n"
- "\t others exit\n\n"
- "operator >> ");
-
- fflush(stdin);
- fgets(operator, sizeof(operator), stdin);
- opt = atoi(operator);
- switch (opt)
- {
- case 0:
- mbtk_audio_ubus_client_init(&handler, dtmf_cb1);
- mbtk_audio_switch_pcm(1);
- mbtk_audio_mode_set(2);
- hdl = Ql_AudRecorder_Open(NULL, record_cb_func);
- if (hdl == 0)
- return ;
- break;
- case 1:
- if(0 == hdl)
- {
- printf("audio is not initialized yet.\n");
- continue;
- }
-
- if(0 != record_fd)
- {
- printf("audio It's already being recorded.\n");
- continue;
- }
-
- record_fd = open(MBTK_AUD_DEMO_WAV, O_RDWR|O_CREAT|O_TRUNC, 0644);
- if (record_fd <= 0)
- {
- printf("file open error\n");
- continue;
- }
-
- mbtk_audio_path_enable(1);
-
- if(0 == MBTK_wav_pcm16Le_set(record_fd))
- {
- ret = Ql_AudRecorder_StartRecord();
- if(0 != ret)
- {
- printf("audio record error: %d\n", ret);
- close(record_fd);
- record_fd = 0;
- }
- }
- else
- {
- printf("arec set file header error\n");
- close(record_fd);
- record_fd = 0;
- }
- mbtk_audio_path_enable(0);
- break;
- case 2:
- // printf("arec state : %d\n", state);
- break;
- case 3:
- break;
- case 4:
- break;
- case 5:
- break;
- case 6:
- Ql_AudRecorder_Close();
-// mbtk_audio_mode_set(2);
- if(record_fd > 0)
- {
- close(record_fd);
- record_fd = 0;
- }
- mbtk_audio_mode_set(0);
- mbtk_audio_switch_pcm(0);
- break;
- default:
- return;
- }
-
- sleep(1);
+ play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
+ if(0 == play_hdl) {
+ printf("Ql_AudPlayer_Open fail\n");
+ close(fd);
+ return -1;
}
- printf("arec exit\n");
- return ;
-}
-int main(void)
-{
- char operator[10];
- int opt;
-
- while(1)
- {
- printf("=========audio main=========\n"
- "\t0 exit\n"
- "\t1 aplay\n"
- "\t2 arec\n"
- "\t3 set speaker Volume\n"
- "\t4 get speaker Volume\n"
- "\t5 set mic Volume\n"
- "\t6 get mic Volume\n"
- "\t7 tts\n"
- "\t8 tone\n"
- "operator: >> ");
-
- fgets(operator, sizeof(operator), stdin);
- fflush(stdin);
- opt = atoi(operator);
- switch (opt)
- {
- case 0:
- printf("main exit\n");
- return 0;
- case 1:
- aplay();
- break;
- case 2:
- arec();
- break;
- case 3:
- mbtk_at_rec(NULL);
- break;
- case 4:
- mbtk_at_play(NULL);
- break;
- case 5:
- break;
- case 6:
- break;
- case 7:
- break;
- case 8:
- break;
- default:
- break;
- }
-
- sleep(1);
+ if(MBTK_wav_pcm16Le_check(fd)) {
+ printf("MBTK_wav_pcm16Le_check() fail.\n");
+ goto fail;
}
+#if MBTK_PLAY_PA_CONTROL
+ audio_pa_open();
+#endif
+ if(Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0)) {
+ printf("Ql_AudPlayer_PlayFrmFile() fail.\n");
+ goto fail;
+ }
+
+ if(play_hdl) {
+#if MBTK_PLAY_PA_CONTROL
+ audio_pa_close();
+#endif
+ printf("Play complete.\n");
+ Ql_AudPlayer_Close(play_hdl);
+#if MBTK_PLAY_PA_CONTROL
+ audio_pa_open();
+#endif
+ play_hdl = 0;
+ }
+ close(fd);
return 0;
+fail:
+ Ql_AudPlayer_Close(play_hdl);
+ close(fd);
+ return -1;
+}
+
+static int recv(const char *path)
+{
+ record_fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0644);
+ if(record_fd < 0) {
+ printf("open(%s) fail:%d\n", path, errno);
+ return -1;
+ }
+#if MBTK_RECV_PA_CONTROL
+ audio_pa_close();
+#endif
+
+ int hdl = Ql_AudRecorder_Open(NULL, record_cb_func);
+ if(0 == hdl) {
+ printf("Ql_AudRecorder_Open fail\n");
+ close(record_fd);
+ record_fd = -1;
+ return -1;
+ }
+
+ if(MBTK_wav_pcm16Le_set(record_fd)) {
+ printf("MBTK_wav_pcm16Le_set() fail.\n");
+ goto fail;
+ }
+#if MBTK_RECV_PA_CONTROL
+ audio_pa_open();
+#endif
+ if(Ql_AudRecorder_StartRecord()) {
+ printf("Ql_AudRecorder_StartRecord() fail.\n");
+ goto fail;
+ }
+ sleep(10);
+#if MBTK_RECV_PA_CONTROL
+ audio_pa_close();
+#endif
+ Ql_AudRecorder_Close();
+#if MBTK_RECV_PA_CONTROL
+ audio_pa_open();
+#endif
+ printf("Recorder complete.\n");
+ close(record_fd);
+ record_fd = -1;
+ return 0;
+fail:
+ Ql_AudRecorder_Close();
+ close(record_fd);
+ record_fd = -1;
+ return -1;
+}
+
+static void sig_handler(int sig)
+{
+ printf("output signal number: %d.\n", sig);
+ if(play_hdl) {
+ Ql_AudPlayer_Stop(play_hdl);
+ }
+}
+
+static void help()
+{
+ printf("yx_audio_test play/recv file_name\n");
+}
+
+int main(int argc, char *argv[])
+{
+ if(argc != 3) {
+ help();
+ return -1;
+ }
+
+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+ signal(SIGTSTP, sig_handler);
+ if(access("/sys/class/gpio/gpio33/value", F_OK)) {
+ system("echo 33 > /sys/class/gpio/export");
+ system("echo out > /sys/class/gpio/gpio33/direction");
+ }
+ gpio33_fd = open("/sys/class/gpio/gpio33/value", O_RDWR);
+
+ if(access("/sys/class/gpio/gpio34/value", F_OK)) {
+ system("echo 34 > /sys/class/gpio/export");
+ system("echo out > /sys/class/gpio/gpio34/direction");
+ }
+ gpio34_fd = open("/sys/class/gpio/gpio34/value", O_RDWR);
+
+ int ret = -1;
+ if(!strcmp(argv[1], "play")) {
+ ret = play(argv[2]);
+ } else if(!strcmp(argv[1], "recv")) {
+ ret = recv(argv[2]);
+ } else {
+ help();
+ }
+
+ if(gpio34_fd > 0) {
+ close(gpio34_fd);
+ }
+
+ if(gpio33_fd > 0) {
+ close(gpio33_fd);
+ }
+ return ret;
}
diff --git a/mbtk/test/mbtk_dsp_gain_set.c b/mbtk/test/mbtk_dsp_gain_set.c
new file mode 100755
index 0000000..bae9485
--- /dev/null
+++ b/mbtk/test/mbtk_dsp_gain_set.c
@@ -0,0 +1,36 @@
+#include "mbtk_audio_gain.h"
+#include "mbtk_log.h"
+
+static void help()
+{
+ printf("./mbtk_dsp_gain_set spk/mic <gain>\n");
+}
+
+int main(int argc, char *argv[])
+{
+ if(argc != 3) {
+ help();
+ return -1;
+ }
+
+ if(strcmp(argv[1], "spk") && strcmp(argv[1], "mic")) {
+ help();
+ return -1;
+ }
+
+ mbtk_log_init("radio", "MBTK_GAIN");
+
+ CONFIG_DSPGAIN_Direction dir = CONFIG_DSPGAIN_RX;
+ if(!strcmp(argv[1], "mic")) {
+ dir = CONFIG_DSPGAIN_TX;
+ }
+ if(mbtk_dsp_gain_set(dir, atoi(argv[2]))) {
+ printf("\nFail.\n");
+ } else {
+ printf("\nSuccess.\n");
+ }
+
+ return 0;
+}
+
+
diff --git a/mbtk/test/polarssl_demo.c b/mbtk/test/polarssl_demo.c
new file mode 100755
index 0000000..1405fdb
--- /dev/null
+++ b/mbtk/test/polarssl_demo.c
@@ -0,0 +1,488 @@
+#include <stdio.h>
+
+#ifdef MBTK_POLARSSL_SUPPORT
+#include "mbtk_log.h"
+#include "ql/ql_mcm_sim.h"
+#include <sys/socket.h>
+#include <polarssl/net.h>
+#include <polarssl/ssl.h>
+#include <polarssl/entropy.h>
+#include <polarssl/ctr_drbg.h>
+#include <polarssl/certs.h>
+#include <polarssl/x509.h>
+#include <polarssl/error.h>
+#include <polarssl/debug.h>
+#include <polarssl/config.h>
+
+#define DFL_SERVER_NAME "asr"
+#define DFL_SERVER_ADDR NULL
+#define DFL_SERVER_PORT 4433
+#define DFL_REQUEST_PAGE "/"
+#define DFL_REQUEST_SIZE -1
+#define DFL_DEBUG_LEVEL 0
+#define DFL_NBIO 0
+#define DFL_CA_FILE "/ca.crt"
+#define DFL_CA_PATH "/ca.crt"
+#define DFL_CRT_FILE "/client.crt"
+#define DFL_KEY_FILE "/client.key"
+#define DFL_PSK ""
+#define DFL_PSK_IDENTITY "Client_identity"
+#define DFL_FORCE_CIPHER 0
+#define DFL_RENEGOTIATION SSL_RENEGOTIATION_DISABLED
+#define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION
+#define DFL_RENEGOTIATE 0
+#define DFL_EXCHANGES 1
+#define DFL_MIN_VERSION SSL_MINOR_VERSION_3
+#define DFL_MAX_VERSION SSL_MINOR_VERSION_3
+#define DFL_AUTH_MODE SSL_VERIFY_REQUIRED
+#define DFL_MFL_CODE SSL_MAX_FRAG_LEN_NONE
+#define DFL_TRUNC_HMAC 0
+#define DFL_RECONNECT 0
+#define DFL_RECO_DELAY 0
+#define DFL_TICKETS SSL_SESSION_TICKETS_ENABLED
+#define DFL_ALPN_STRING NULL
+
+#define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
+#define GET_REQUEST_END "\r\n\r\n"
+
+#define CA_CERT \
+"-----BEGIN CERTIFICATE-----\r\n" \
+"MIIDKjCCAhICCQCOewfZiRCiNjANBgkqhkiG9w0BAQUFADBXMQswCQYDVQQGEwJD\r\n" \
+"TjEQMA4GA1UECBMHU2lDaHVhbjEVMBMGA1UEChMMTU9CSUxFVEVLLkNBMQswCQYD\r\n" \
+"VQQLEwJJVDESMBAGA1UEAxMJTU9CSUxFVEVLMB4XDTE4MDkxODA4MDUzMloXDTMz\r\n" \
+"MDkxOTA4MDUzMlowVzELMAkGA1UEBhMCQ04xEDAOBgNVBAgTB1NpQ2h1YW4xFTAT\r\n" \
+"BgNVBAoTDE1PQklMRVRFSy5DQTELMAkGA1UECxMCSVQxEjAQBgNVBAMTCU1PQklM\r\n" \
+"RVRFSzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOkdYJF1h1xjKbY0\r\n" \
+"ipbl88G653PiCh8ZMjmIUYeiDKC8+0wtXZtHvQIl6AncOzBy9XHVOctbKn34exC8\r\n" \
+"SEotMuo2T49vs9VtE8GYu2pOrf3m42NpLRnYAxfm9qw53CMHx+Jn7Oa9fnxa8haA\r\n" \
+"pRc2BTVadWGoS8EEwoZFk0eNb7Z2Gc7U0c+GhISI4oVTTocGvGgMzkvduu5JJbbc\r\n" \
+"BOcNFrii9sRO9vtOYQtqOEg01Uum2Dwp/o2bDLXNJEqAIh4WACiM4iPmmlRHWT2y\r\n" \
+"NjQ3vcbEdrFwbHRtO46+Vw54HnSyCoFb3uCHMNMvXObZ/8AU9E3Cgat4j0sgEeB0\r\n" \
+"hqA4MiMCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAtEAjf0CjsLgG9ROdmp1qXYft\r\n" \
+"+ndIT5l82KRK57ZQsfdFbnJOvALeF/ICKU0M2TXgJNiGOA5RxDi00YYdMbOIPwVZ\r\n" \
+"JH4b87J/LYdLAGf+Q+kVI6gWH3hPm4Jzfzq/40KVrf3mpa54yWz6ZYtwfxBjrMgr\r\n" \
+"IVe0O5SIJ99lsddgzgUkqYN2vWJW2zZ50xuXOAyo+pOnjzX0wuOcaBT3JCHWJRAb\r\n" \
+"VhJCf9JbswDgnddJerqFtB8pnpAOdGokLCOoM06q3s3P9mhGX+72HXdX7G8CSAuG\r\n" \
+"PVCGf6RaF0/G4B9R1c3du3lZRlQWfx2pxyU0LS86iFQFWqzqcWEXIcULVdcErQ==\r\n" \
+"-----END CERTIFICATE-----\r\n"
+
+const char ca1_cert[]= CA_CERT;
+
+
+struct options
+{
+ const char *server_name; /* hostname of the server (client only) */
+ const char *server_addr; /* address of the server (client only) */
+ int server_port; /* port on which the ssl service runs */
+ int debug_level; /* level of debugging */
+ int nbio; /* should I/O be blocking? */
+ const char *request_page; /* page on server to request */
+ int request_size; /* pad request with header to requested size */
+ const char *ca_file; /* the file with the CA certificate(s) */
+ const char *ca_path; /* the path with the CA certificate(s) reside */
+ const char *crt_file; /* the file with the client certificate */
+ const char *key_file; /* the file with the client key */
+ const char *psk; /* the pre-shared key */
+ const char *psk_identity; /* the pre-shared key identity */
+ int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
+ int renegotiation; /* enable / disable renegotiation */
+ int allow_legacy; /* allow legacy renegotiation */
+ int renegotiate; /* attempt renegotiation? */
+ int renego_delay; /* delay before enforcing renegotiation */
+ int exchanges; /* number of data exchanges */
+ int min_version; /* minimum protocol version accepted */
+ int max_version; /* maximum protocol version accepted */
+ int auth_mode; /* verify mode for connection */
+ unsigned char mfl_code; /* code for maximum fragment length */
+ int trunc_hmac; /* negotiate truncated hmac or not */
+ int reconnect; /* attempt to resume session */
+ int reco_delay; /* delay in seconds before resuming session */
+ int tickets; /* enable / disable session tickets */
+ const char *alpn_string; /* ALPN supported protocols */
+} opt;
+
+
+static sim_client_handle_type cli_handle;
+int server_fd = -1;
+
+static void my_debug( void *ctx, int level, const char *str )
+{
+ ((void) level);
+
+ fprintf( (FILE *) ctx, "%s", str );
+ fflush( (FILE *) ctx );
+}
+
+
+static int ssl_client_init()
+{
+ int ret = 0, len, tail_len, i, written, frags;
+ unsigned char buf[SSL_MAX_CONTENT_LEN + 1];
+ const char *pers = "ssl_client";
+
+ entropy_context entropy;
+ ctr_drbg_context ctr_drbg;
+ ssl_context ssl;
+ ssl_session saved_session;
+ x509_crt cacert;
+ x509_crt clicert;
+ pk_context pkey;
+
+ memset( &ssl, 0, sizeof( ssl_context ) );
+ memset( &saved_session, 0, sizeof( ssl_session ) );
+ x509_crt_init( &cacert );
+ x509_crt_init( &clicert );
+ pk_init( &pkey );
+
+ fflush( stdout );
+
+ /*
+ * 0. Initialize the RNG and the session data
+ */
+
+ entropy_init( &entropy );
+ if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
+ (const unsigned char *) pers,
+ strlen( pers ) ) ) != 0 )
+ {
+ printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret );
+ goto exit;
+ }
+
+ printf( " ok\n" );
+
+ /*
+ * 1.1. Load the trusted CA
+ */
+ //ret = x509_crt_parse(&cacert,ca1_cert,strlen(ca1_cert));
+ ret = x509_crt_parse_file( &cacert, opt.ca_path );
+ if( ret < 0 )
+ {
+ printf( " failed\n ! ca x509_crt_parse returned -0x%x\n\n", -ret );
+ goto exit;
+ }
+ printf( " ok\n" );
+
+ /*
+ * 1.2. Load own certificate and private key
+ *
+ * (can be skipped if client authentication is not required)
+ */
+
+ ret = x509_crt_parse_file( &clicert, opt.crt_file );
+ if( ret != 0 )
+ {
+ printf( " failed\n ! crt x509_crt_parse returned -0x%x\n\n", -ret );
+ goto exit;
+ }
+
+ ret = pk_parse_keyfile( &pkey, opt.key_file, NULL);
+ if( ret != 0 )
+ {
+ printf( " failed\n ! key x509_crt_parse returned -0x%x\n\n", -ret );
+ goto exit;
+ }
+
+ printf( " ok\n" );
+
+ /*
+ * 2. Setup stuff
+ */
+ printf( " . Setting up the SSL/TLS structure..." );
+ fflush( stdout );
+
+ if( ( ret = ssl_init( &ssl ) ) != 0 )
+ {
+ printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret );
+ goto exit;
+ }
+
+ ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
+ ssl_set_authmode( &ssl, opt.auth_mode );
+
+ ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
+ ssl_set_dbg( &ssl, my_debug, stdout );
+
+ ssl_set_bio( &ssl, net_recv, &server_fd, net_send, &server_fd );
+
+ ssl_set_renegotiation( &ssl, opt.renegotiation );
+ ssl_legacy_renegotiation( &ssl, opt.allow_legacy );
+
+ ssl_set_ca_chain( &ssl, &cacert, NULL, NULL );
+
+ if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
+ {
+ printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
+ goto exit;
+ }
+ if( opt.min_version != -1 )
+ ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
+ if( opt.max_version != -1 )
+ ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
+ printf( " ok\n" );
+ /*
+ * 3. Handshake
+ */
+ printf( " . Performing the SSL/TLS handshake..." );
+ fflush( stdout );
+
+ while( ( ret = ssl_handshake( &ssl ) ) != 0 )
+ {
+ if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
+ {
+ printf( " failed\n ! ssl_handshake returned -0x%x\n", -ret );
+ if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED )
+ printf(
+ " Unable to verify the server's certificate. "
+ "Either it is invalid,\n"
+ " or you didn't set ca_file or ca_path "
+ "to an appropriate value.\n"
+ " Alternatively, you may want to use "
+ "auth_mode=optional for testing purposes.\n" );
+ printf( "\n" );
+ goto exit;
+ }
+ }
+
+ printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
+ ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
+
+ /*
+ * 4. Verify the server certificate
+ */
+ printf( " . Verifying peer X.509 certificate..." );
+
+ if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
+ {
+ printf( " failed\n" );
+
+ if( ( ret & BADCERT_EXPIRED ) != 0 )
+ printf( " ! server certificate has expired\n" );
+
+ if( ( ret & BADCERT_REVOKED ) != 0 )
+ printf( " ! server certificate has been revoked\n" );
+
+ if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
+ printf( " ! CN mismatch (expected CN=%s)\n", opt.server_name );
+
+ if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
+ printf( " ! self-signed or not signed by a trusted CA\n" );
+
+ printf( "\n" );
+ }
+ else
+ printf( " ok\n" );
+
+ if( ssl_get_peer_cert( &ssl ) != NULL )
+ {
+ printf( " . Peer certificate information ...\n" );
+ x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
+ ssl_get_peer_cert( &ssl ) );
+ printf( "%s\n", buf );
+ }
+ /*
+ * 5. Write the GET request
+ */
+ printf( " > Write to server:" );
+ fflush( stdout );
+
+ len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST,
+ opt.request_page );
+ tail_len = strlen( GET_REQUEST_END );
+
+ /* Add padding to GET request to reach opt.request_size in length */
+ if( opt.request_size != DFL_REQUEST_SIZE &&
+ len + tail_len < opt.request_size )
+ {
+ memset( buf + len, 'A', opt.request_size - len - tail_len );
+ len += opt.request_size - len - tail_len;
+ }
+
+ strncpy( (char *) buf + len, GET_REQUEST_END, sizeof(buf) - len - 1 );
+ len += tail_len;
+
+ /* Truncate if request size is smaller than the "natural" size */
+ if( opt.request_size != DFL_REQUEST_SIZE &&
+ len > opt.request_size )
+ {
+ len = opt.request_size;
+
+ /* Still end with \r\n unless that's really not possible */
+ if( len >= 2 ) buf[len - 2] = '\r';
+ if( len >= 1 ) buf[len - 1] = '\n';
+ }
+
+ for( written = 0, frags = 0; written < len; written += ret, frags++ )
+ {
+ while( ( ret = ssl_write( &ssl, buf + written, len - written ) ) <= 0 )
+ {
+ if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
+ {
+ printf( " failed\n ! ssl_write returned -0x%x\n\n", -ret );
+ goto exit;
+ }
+ }
+ }
+
+ buf[written] = '\0';
+ printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
+
+ /*
+ * 6. Read the HTTP response
+ */
+ printf( " < Read from server:" );
+ fflush( stdout );
+
+ do
+ {
+ len = sizeof( buf ) - 1;
+ memset( buf, 0, sizeof( buf ) );
+ ret = ssl_read( &ssl, buf, len );
+
+ if( ret == POLARSSL_ERR_NET_WANT_READ ||
+ ret == POLARSSL_ERR_NET_WANT_WRITE )
+ continue;
+
+ if( ret <= 0 )
+ {
+ switch( ret )
+ {
+ case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
+ printf( " connection was closed gracefully\n" );
+ ret = 0;
+ goto close_notify;
+
+ case 0:
+ case POLARSSL_ERR_NET_CONN_RESET:
+ printf( " connection was reset by peer\n" );
+ ret = 0;
+ goto exit;
+
+ default:
+ printf( " ssl_read returned -0x%x\n", -ret );
+ goto exit;
+ }
+ }
+
+ len = ret;
+ buf[len] = '\0';
+ printf( " %d bytes read\n\n%s", len, (char *) buf );
+
+ /* End of message should be detected according to the syntax of the
+ * application protocol (eg HTTP), just use a dummy test here. */
+ if( ret > 0 && buf[len-1] == '\n' )
+ {
+ ret = 0;
+ break;
+ }
+ }
+ while( 1 );
+
+ /*
+ * 7. Done, cleanly close the connection
+ */
+close_notify:
+ printf( " . Closing the connection..." );
+
+ while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
+ {
+ if( ret == POLARSSL_ERR_NET_CONN_RESET )
+ {
+ printf( " ok (already closed by peer)\n" );
+ ret = 0;
+ goto exit;
+ }
+
+ if( ret != POLARSSL_ERR_NET_WANT_READ &&
+ ret != POLARSSL_ERR_NET_WANT_WRITE )
+ {
+ printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
+ goto exit;
+ }
+ }
+
+ printf( " ok\n" );
+exit:
+ if( server_fd )
+ net_close( server_fd );
+
+ x509_crt_free( &clicert );
+ x509_crt_free( &cacert );
+ pk_free( &pkey );
+ ssl_session_free( &saved_session );
+ ssl_free( &ssl );
+ ctr_drbg_free( &ctr_drbg );
+ entropy_free( &entropy );
+
+ printf( " ok end\n" );
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ printf("Start!\n");
+
+ opt.server_name = DFL_SERVER_NAME;
+ opt.server_addr = DFL_SERVER_ADDR;
+ opt.server_port = DFL_SERVER_PORT;
+ opt.debug_level = DFL_DEBUG_LEVEL;
+ opt.nbio = DFL_NBIO;
+ opt.request_page = DFL_REQUEST_PAGE;
+ opt.request_size = DFL_REQUEST_SIZE;
+ opt.ca_file = DFL_CA_FILE;
+ opt.ca_path = DFL_CA_PATH;
+ opt.crt_file = DFL_CRT_FILE;
+ opt.key_file = DFL_KEY_FILE;
+ opt.psk = DFL_PSK;
+ opt.psk_identity = DFL_PSK_IDENTITY;
+ opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
+ opt.renegotiation = DFL_RENEGOTIATION;
+ opt.allow_legacy = DFL_ALLOW_LEGACY;
+ opt.renegotiate = DFL_RENEGOTIATE;
+ opt.exchanges = DFL_EXCHANGES;
+ opt.min_version = DFL_MIN_VERSION;
+ opt.max_version = DFL_MAX_VERSION;
+ opt.auth_mode = DFL_AUTH_MODE;
+ opt.mfl_code = DFL_MFL_CODE;
+ opt.trunc_hmac = DFL_TRUNC_HMAC;
+ opt.reconnect = DFL_RECONNECT;
+ opt.reco_delay = DFL_RECO_DELAY;
+ opt.tickets = DFL_TICKETS;
+ opt.alpn_string = DFL_ALPN_STRING;
+
+
+ if(argc < 3)
+ {
+ printf("input error \n example: mbtk_test ip prot\n");
+ return -1;
+ }
+ opt.server_addr = argv[1];
+ opt.server_port = atoi(argv[2]);
+
+ int ret = -1;
+ if( ( ret = net_connect( &server_fd, opt.server_addr,
+ opt.server_port ) ) != 0 )
+ {
+ printf( " failed\n ! net_connect returned -0x%x\n\n", -ret );
+ return -1;
+ }
+
+ ret = net_set_nonblock( server_fd );
+ if( ret != 0 )
+ {
+ printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", -ret );
+ return -1;
+ }
+ printf( " ok\n" );
+ ret = ssl_client_init();
+ printf("ret is %d\n",ret);
+ printf("End!\n");
+ return 0;
+}
+#else
+int main(int argc, char *argv[])
+{
+ printf("No support polarssl.\n");
+ return 0;
+}
+#endif
\ No newline at end of file