Add lynq audio API and test.

Change-Id: Icfb293c609b1fe71e03fa8a36c26c9c831b51be9
diff --git a/mbtk/lynq_lib/src/lynq_alarm.c b/mbtk/lynq_lib/src/lynq_alarm.c
index 2419ac0..2722854 100755
--- a/mbtk/lynq_lib/src/lynq_alarm.c
+++ b/mbtk/lynq_lib/src/lynq_alarm.c
@@ -1,9 +1,16 @@
+#include <math.h>
+#include <stdlib.h>
+
 #include "mbtk_alarm.h"
 #include "lynq_alarm.h"
+#include "mbtk_str.h"
 
 int lynq_set_wakealarm(unsigned long time_sec)
 {
     UNUSED(time_sec);
+    if(time_sec < 1 || time_sec > pow(2, 28)) {
+        return -1;
+    }
 
     return 0;
 }
@@ -11,22 +18,31 @@
 int lynq_set_poweralarm(unsigned long time_sec)
 {
     UNUSED(time_sec);
+    if(time_sec < 1 || time_sec > pow(2, 28)) {
+        return -1;
+    }
 
     return 0;
 }
 
+// min:1 max:2^28
 ssize_t wakealarm(char *buffer)
 {
     UNUSED(buffer);
-
-    return 0;
+    if(str_empty(buffer)) {
+        return -1;
+    }
+    return lynq_set_wakealarm(atol(buffer));
 }
 
+// min:1 max:2^28
 ssize_t poweralarm(char *buffer)
 {
     UNUSED(buffer);
-
-    return 0;
+    if(str_empty(buffer)) {
+        return -1;
+    }
+    return lynq_set_poweralarm(atol(buffer));
 }
 
 int cancel_wakealarm(void)
diff --git a/mbtk/lynq_lib/src/lynq_audio.c b/mbtk/lynq_lib/src/lynq_audio.c
index 7cdb9be..259661e 100755
--- a/mbtk/lynq_lib/src/lynq_audio.c
+++ b/mbtk/lynq_lib/src/lynq_audio.c
@@ -1,11 +1,33 @@
 #include "lynq-qser-audio.h"
 #include "mbtk_type.h"
 #include "mbtk_log.h"
+#include "mbtk_audio2.h"
+
+#define AUDIO_DEV_PLAY "device1"
+#define AUDIO_DEV_RECORDER "device2"
+#define AUDIO_HDL_DEFAULT 0
+
+static _cb_onPlayer play_cb = NULL;
+static int play_hdl = -1;
+
+static _cb_onPlayer recv_cb = NULL;
+static int recv_hdl = -1;
 
 int qser_AudPlayer_Open(char* device, _cb_onPlayer cb_fun)
 {
     UNUSED(device);
     UNUSED(cb_fun);
+    if(device == NULL || strcmp(device, AUDIO_DEV_PLAY)) {
+        LOGE("device must be %s for play.", AUDIO_DEV_PLAY);
+        return -1;
+    }
+
+    if(mbtk_audio_wav_init()) {
+        LOGE("mbtk_audio_wav_init() fail.");
+        return -1;
+    }
+
+    play_cb = cb_fun;
 
     return 0;
 }
@@ -15,13 +37,70 @@
     UNUSED(hdl);
     UNUSED(fd);
     UNUSED(offset);
+    if(play_hdl >= 0) {
+        LOGE("Play busy.");
+        if(play_cb) {
+            play_cb(-1);
+        }
+        return -1;
+    }
 
+    if(mbtk_audio_wav_play_start(fd)) {
+        LOGE("mbtk_audio_wav_play_start() fail.");
+        if(play_cb) {
+            play_cb(-2);
+        }
+        return -1;
+    }
+
+    play_hdl = hdl;
+
+    if(play_cb) {
+        play_cb(0);
+    }
+
+    return 0;
+}
+
+int qser_AudPlayer_PlayPcmBuf(const unsigned char *pcm_data, int data_size, int period_size,
+                                int period_count, int num_channels, int sample_rate, int ownerid)
+{
+
+    if(play_hdl >= 0) {
+        LOGE("Play busy.");
+        if(play_cb) {
+            play_cb(-1);
+        }
+        return -1;
+    }
+
+    if(mbtk_audio_wav_stream_play_start(pcm_data, data_size, sample_rate, num_channels)) {
+        LOGE("mbtk_audio_wav_stream_play_start() fail.");
+        if(play_cb) {
+            play_cb(-2);
+        }
+        return -1;
+    }
+
+    play_hdl = AUDIO_HDL_DEFAULT;
+    if(play_cb) {
+        play_cb(0);
+    }
     return 0;
 }
 
 int qser_AudPlayer_Pause(int hdl)
 {
     UNUSED(hdl);
+    if((play_hdl != AUDIO_HDL_DEFAULT && hdl != play_hdl) || play_hdl < 0) {
+        LOGE("Play busy or hdl error.");
+        return -1;
+    }
+
+    if(mbtk_audio_wav_play_pause()) {
+        LOGE("mbtk_audio_wav_play_pause() fail.");
+        return -1;
+    }
 
     return 0;
 }
@@ -29,6 +108,15 @@
 int qser_AudPlayer_Resume(int hdl)
 {
     UNUSED(hdl);
+    if((play_hdl != AUDIO_HDL_DEFAULT && hdl != play_hdl) || play_hdl < 0) {
+        LOGE("Play busy or hdl error.");
+        return -1;
+    }
+
+    if(mbtk_audio_wav_play_resume()) {
+        LOGE("mbtk_audio_wav_play_resume() fail.");
+        return -1;
+    }
 
     return 0;
 }
@@ -36,8 +124,17 @@
 void qser_AudPlayer_Stop(int hdl)
 {
     UNUSED(hdl);
+    if((play_hdl != AUDIO_HDL_DEFAULT && hdl != play_hdl) || play_hdl < 0) {
+        LOGE("Play busy or hdl error.");
+        return;
+    }
 
-    return 0;
+    if(mbtk_audio_wav_play_stop()) {
+        LOGE("mbtk_audio_wav_play_stop() fail.");
+        return;
+    }
+
+    play_hdl = -1;
 }
 
 
@@ -45,15 +142,33 @@
 {
     UNUSED(hdl);
 
-    return 0;
-}
+    if(play_hdl >= 0) {
+        qser_AudPlayer_Stop(hdl);
+    }
 
+    if(mbtk_audio_wav_deinit()) {
+        LOGE("mbtk_audio_wav_deinit() fail.");
+        return;
+    }
+
+    play_cb = NULL;
+}
 
 int qser_AudRecorder_Open(char* device, _cb_onPlayer cb_fun)
 {
     UNUSED(device);
     UNUSED(cb_fun);
+    if(device == NULL || strcmp(device, AUDIO_DEV_RECORDER)) {
+        LOGE("device must be %s for recv.", AUDIO_DEV_RECORDER);
+        return -1;
+    }
 
+    if(mbtk_audio_wav_init()) {
+        LOGE("mbtk_audio_wav_init() fail.");
+        return -1;
+    }
+
+    recv_cb = cb_fun;
 
     return 0;
 }
@@ -63,35 +178,94 @@
     UNUSED(hdl);
     UNUSED(fd);
     UNUSED(offset);
+    if(recv_hdl >= 0) {
+        LOGE("Recv busy.");
+        if(recv_cb) {
+            recv_cb(-1);
+        }
+        return -1;
+    }
 
+    if(mbtk_audio_wav_recorder_start(fd, MBTK_AUDIO_SAMPLE_RATE_8000)) {
+        LOGE("mbtk_audio_wav_recorder_start() fail.");
+        if(recv_cb) {
+            recv_cb(-2);
+        }
+        return -1;
+    }
 
+    recv_hdl = hdl;
+    if(recv_cb) {
+        recv_cb(0);
+    }
     return 0;
 }
 
+int qser_AudRecorder_StartRecord_Custom(char *file, int period_size, int period_count,
+            int num_channels, int sample_rate)
+{
+    return qser_AudRecorder_StartRecord(0, file, 0);
+}
+
 int qser_AudRecorder_Pause(void)
 {
+    if(recv_hdl < 0) {
+        LOGE("Recv busy or hdl error.");
+        return -1;
+    }
 
+    if(mbtk_audio_wav_recorder_pause()) {
+        LOGE("mbtk_audio_wav_recorder_pause() fail.");
+        return -1;
+    }
     return 0;
 }
 
 int qser_AudRecorder_Resume(void)
 {
+    if(recv_hdl < 0) {
+        LOGE("Recv busy or hdl error.");
+        return -1;
+    }
 
+    if(mbtk_audio_wav_recorder_resume()) {
+        LOGE("mbtk_audio_wav_recorder_resume() fail.");
+        return -1;
+    }
     return 0;
 }
 
 void qser_AudRecorder_Stop(void)
 {
+    if(recv_hdl < 0) {
+        LOGE("Recv busy or hdl error.");
+        return;
+    }
 
+    if(mbtk_audio_wav_recorder_stop()) {
+        LOGE("mbtk_audio_wav_recorder_stop() fail.");
+        return;
+    }
+
+    recv_hdl = -1;
 }
 
 void qser_AudRecorder_Close(void)
 {
+    if(recv_hdl >= 0) {
+        qser_AudRecorder_Stop();
+    }
 
+    if(mbtk_audio_wav_deinit()) {
+        LOGE("mbtk_audio_wav_deinit() fail.");
+        return;
+    }
+
+    recv_cb = NULL;
 }
 
 void qser_Audio_Deinit(void)
 {
-
+    // Do nothing...
 }