mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-21 10:25:54 +08:00
更新硬件SDK
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
### 阿里云千里传音演示
|
||||
## 注意事项!!!
|
||||
1 此demo仅供演示阿里云千里传音功能,没有太好的异常处理,请勿用作其他用途
|
||||
2 需要更换为自己的设备三元组
|
||||
3 此demo的金额播报演示音频格式为amr
|
||||
4 此demo未启用tts功能,只有音频文件播报
|
||||
|
||||
## 演示项目
|
||||
1 语料推送和金额播报
|
||||
2 动态音频播报
|
||||
|
||||
## 演示准备
|
||||
# 语料推送和金额播报
|
||||
首先下发金额播报需要的语料,阿里云自带的常用语料,文件需为amr格式
|
||||
1 数字语料包
|
||||
2 量词语料包
|
||||
3 货币单位语料包
|
||||
|
||||
其次通过阿里云千里传音SpeechByCombination API 下发格式为"{$number}",不包括双引号
|
||||
其中number是一个不大于99999999.99的字符串
|
||||
如"{$10000.11}",设备会播报一万点一一元
|
||||
https://help.aliyun.com/document_detail/223764.html
|
||||
|
||||
# 动态音频播报
|
||||
通过里云千里传音SpeechBySynthesis API 动态下发音频文件,设备播放,
|
||||
https://help.aliyun.com/document_detail/369398.html
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,350 @@
|
||||
#ifndef __RTE_DEVICE_H
|
||||
#define __RTE_DEVICE_H
|
||||
|
||||
#include "ec618.h"
|
||||
|
||||
/* Peripheral IO Mode Select, Must Configure First !!!
|
||||
Note, when receiver works in DMA_MODE, interrupt is also enabled to transfer tailing bytes.
|
||||
*/
|
||||
|
||||
#define POLLING_MODE 0x1
|
||||
#define DMA_MODE 0x2
|
||||
#define IRQ_MODE 0x3
|
||||
#define UNILOG_MODE 0x4
|
||||
|
||||
#define RTE_UART0_TX_IO_MODE UNILOG_MODE
|
||||
#define RTE_UART0_RX_IO_MODE IRQ_MODE
|
||||
#define USART0_RX_TRIG_LVL (30)
|
||||
|
||||
#define RTE_UART1_TX_IO_MODE DMA_MODE
|
||||
#define RTE_UART1_RX_IO_MODE DMA_MODE
|
||||
|
||||
#define RTE_UART2_TX_IO_MODE POLLING_MODE
|
||||
#define RTE_UART2_RX_IO_MODE DMA_MODE
|
||||
|
||||
#define RTE_SPI0_IO_MODE POLLING_MODE
|
||||
|
||||
#define RTE_SPI1_IO_MODE POLLING_MODE
|
||||
|
||||
#define RTE_I2C0_IO_MODE POLLING_MODE
|
||||
#define RTE_I2C1_IO_MODE POLLING_MODE
|
||||
|
||||
|
||||
// I2C0 (Inter-integrated Circuit Interface) [Driver_I2C0]
|
||||
// Configuration settings for Driver_I2C0 in component ::Drivers:I2C
|
||||
#define RTE_I2C0 1
|
||||
|
||||
// { PAD_PIN28}, // 0 : gpio13 / 2 : I2C0 SCL
|
||||
// { PAD_PIN27}, // 0 : gpio12 / 2 : I2C0 SDA
|
||||
#define RTE_I2C0_SCL_BIT 28 // AUDIO use 28
|
||||
#define RTE_I2C0_SCL_FUNC PAD_MUX_ALT2
|
||||
|
||||
#define RTE_I2C0_SDA_BIT 27 // AUDIO use 27
|
||||
#define RTE_I2C0_SDA_FUNC PAD_MUX_ALT2
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_I2C0_DMA_TX_EN 0
|
||||
#define RTE_I2C0_DMA_TX_REQID DMA_REQUEST_I2C0_TX
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_I2C0_DMA_RX_EN 0
|
||||
#define RTE_I2C0_DMA_RX_REQID DMA_REQUEST_I2C0_RX
|
||||
|
||||
// I2C1 (Inter-integrated Circuit Interface) [Driver_I2C1]
|
||||
// Configuration settings for Driver_I2C1 in component ::Drivers:I2C
|
||||
#define RTE_I2C1 1
|
||||
|
||||
// { PAD_PIN20}, // 0 : gpio5 / 2 : I2C1 SCL
|
||||
// { PAD_PIN19}, // 0 : gpio4 / 2 : I2C1 SDA
|
||||
#define RTE_I2C1_SCL_BIT 20
|
||||
#define RTE_I2C1_SCL_FUNC PAD_MUX_ALT2
|
||||
|
||||
#define RTE_I2C1_SDA_BIT 19
|
||||
#define RTE_I2C1_SDA_FUNC PAD_MUX_ALT2
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_I2C1_DMA_TX_EN 1
|
||||
#define RTE_I2C1_DMA_TX_REQID DMA_REQUEST_I2C1_TX
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_I2C1_DMA_RX_EN 1
|
||||
#define RTE_I2C1_DMA_RX_REQID DMA_REQUEST_I2C1_RX
|
||||
|
||||
|
||||
// UART0 (Universal asynchronous receiver transmitter) [Driver_USART0]
|
||||
// Configuration settings for Driver_USART0 in component ::Drivers:USART
|
||||
#define RTE_UART0_CTS_PIN_EN 0
|
||||
#define RTE_UART0_RTS_PIN_EN 0
|
||||
|
||||
// { PAD_PIN27}, // 0 : gpio12 / 3 : UART0 RTSn
|
||||
// { PAD_PIN28}, // 0 : gpio13 / 3 : UART0 CTSn
|
||||
// { PAD_PIN29}, // 0 : gpio14 / 3 : UART0 RXD
|
||||
// { PAD_PIN30}, // 0 : gpio15 / 3 : UART0 TXD
|
||||
#define RTE_UART0_RTS_BIT 27
|
||||
#define RTE_UART0_RTS_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_UART0_CTS_BIT 28
|
||||
#define RTE_UART0_CTS_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_UART0_RX_BIT 29
|
||||
#define RTE_UART0_RX_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_UART0_TX_BIT 30
|
||||
#define RTE_UART0_TX_FUNC PAD_MUX_ALT3
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_UART0_DMA_TX_REQID DMA_REQUEST_USART0_TX
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_UART0_DMA_RX_REQID DMA_REQUEST_USART0_RX
|
||||
|
||||
// UART1 (Universal asynchronous receiver transmitter) [Driver_USART1]
|
||||
// Configuration settings for Driver_USART1 in component ::Drivers:USART
|
||||
#define RTE_UART1_CTS_PIN_EN 1
|
||||
#define RTE_UART1_RTS_PIN_EN 1
|
||||
|
||||
// { PAD_PIN31}, // 0 : gpio16 / 1 : UART1 RTS
|
||||
// { PAD_PIN32}, // 0 : gpio17 / 1 : UART1 CTS
|
||||
// { PAD_PIN33}, // 0 : gpio18 / 1 : UART1 RXD
|
||||
// { PAD_PIN34}, // 0 : gpio19 / 1 : UART1 TXD
|
||||
#define RTE_UART1_RTS_BIT 31
|
||||
#define RTE_UART1_RTS_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_UART1_CTS_BIT 32
|
||||
#define RTE_UART1_CTS_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_UART1_RX_BIT 33
|
||||
#define RTE_UART1_RX_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_UART1_TX_BIT 34
|
||||
#define RTE_UART1_TX_FUNC PAD_MUX_ALT1
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_UART1_DMA_TX_REQID DMA_REQUEST_USART1_TX
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_UART1_DMA_RX_REQID DMA_REQUEST_USART1_RX
|
||||
|
||||
// UART2 (Universal asynchronous receiver transmitter) [Driver_USART2]
|
||||
// Configuration settings for Driver_USART2 in component ::Drivers:USART
|
||||
#define RTE_UART2_CTS_PIN_EN 0
|
||||
#define RTE_UART2_RTS_PIN_EN 0
|
||||
|
||||
// { PAD_PIN25}, // 0 : gpio10 / 3 : UART2 RXD
|
||||
// { PAD_PIN26}, // 0 : gpio11 / 3 : UART2 TXD
|
||||
#define RTE_UART2_RX_BIT 25
|
||||
#define RTE_UART2_RX_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_UART2_TX_BIT 26
|
||||
#define RTE_UART2_TX_FUNC PAD_MUX_ALT3
|
||||
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_UART2_DMA_TX_REQID DMA_REQUEST_USART2_TX
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_UART2_DMA_RX_REQID DMA_REQUEST_USART2_RX
|
||||
|
||||
// SPI0 (Serial Peripheral Interface) [Driver_SPI0]
|
||||
// Configuration settings for Driver_SPI0 in component ::Drivers:SPI
|
||||
#define RTE_SPI0 1
|
||||
|
||||
// { PAD_PIN21}, // 0 : gpio16 / 1 : UART1 RTS / 2 : SPI0 SSn
|
||||
// { PAD_PIN22}, // 0 : gpio11 / 1 : UART1 CTS / 2 : SPI0 MOSI
|
||||
// { PAD_PIN23}, // 0 : gpio14 / 1 : UART1 RXD / 2 : SPI0 MISO
|
||||
// { PAD_PIN24}, // 0 : gpio15 / 1 : UART1 TXD / 2 : SPI0 SCLK
|
||||
#define RTE_SPI0_SSN_BIT 21
|
||||
#define RTE_SPI0_SSN_FUNC PAD_MUX_ALT2
|
||||
|
||||
#define RTE_SPI0_MOSI_BIT 22
|
||||
#define RTE_SPI0_MOSI_FUNC PAD_MUX_ALT2
|
||||
|
||||
#define RTE_SPI0_MISO_BIT 23
|
||||
#define RTE_SPI0_MISO_FUNC PAD_MUX_ALT2
|
||||
|
||||
#define RTE_SPI0_SCLK_BIT 24
|
||||
#define RTE_SPI0_SCLK_FUNC PAD_MUX_ALT2
|
||||
|
||||
#define RTE_SPI0_SSN_GPIO_INSTANCE 1
|
||||
#define RTE_SPI0_SSN_GPIO_INDEX 0
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_SPI0_DMA_TX_REQID DMA_REQUEST_SPI0_TX
|
||||
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_SPI0_DMA_RX_REQID DMA_REQUEST_SPI0_RX
|
||||
|
||||
// SPI1 (Serial Peripheral Interface) [Driver_SPI1]
|
||||
// Configuration settings for Driver_SPI1 in component ::Drivers:SPI
|
||||
#define RTE_SPI1 1
|
||||
|
||||
// { PAD_PIN13}, // 0 : gpio2 / 1 : UART0 RTSn / 3 : SPI1 SSn
|
||||
// { PAD_PIN14}, // 0 : gpio3 / 1 : UART0 CTSn / 3 : SPI1 MOSI
|
||||
// { PAD_PIN15}, // 0 : gpio4 / 1 : UART0 RXD / 3 : SPI1 MISO
|
||||
// { PAD_PIN16}, // 0 : gpio5 / 1 : UART0 TXD / 3 : SPI1 SCLK
|
||||
#define RTE_SPI1_SSN_BIT 13
|
||||
#define RTE_SPI1_SSN_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_SPI1_MOSI_BIT 14
|
||||
#define RTE_SPI1_MOSI_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_SPI1_MISO_BIT 15
|
||||
#define RTE_SPI1_MISO_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_SPI1_SCLK_BIT 16
|
||||
#define RTE_SPI1_SCLK_FUNC PAD_MUX_ALT3
|
||||
|
||||
#define RTE_SPI1_SSN_GPIO_INSTANCE 0
|
||||
#define RTE_SPI1_SSN_GPIO_INDEX 2
|
||||
|
||||
// DMA
|
||||
// Tx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_SPI1_DMA_TX_REQID DMA_REQUEST_SPI1_TX
|
||||
|
||||
// Rx
|
||||
// Channel <0=>0 <1=>1 <2=>2 <3=>3 <4=>4 <5=>5 <6=>6 <7=>7
|
||||
#define RTE_SPI1_DMA_RX_REQID DMA_REQUEST_SPI1_RX
|
||||
|
||||
|
||||
// PWM0 Controller [Driver_PWM0]
|
||||
// Configuration settings for Driver_PWM0 in component ::Drivers:PWM
|
||||
#define RTE_PWM 1
|
||||
|
||||
#define EFUSE_INIT_MODE POLLING_MODE
|
||||
#define L2CTLS_INIT_MODE POLLING_MODE
|
||||
|
||||
#define FLASH_BARE_RW_MODE 1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define RTE_UART0 1
|
||||
#define RTE_UART1 1
|
||||
#define RTE_UART2 1
|
||||
/* to enable external thermal */
|
||||
#define EXTERNAL_NTC_EXIST 0
|
||||
|
||||
|
||||
#if (RTE_UART1 == 1)
|
||||
#define UART1_DTR_PAD_INDEX 26 // GPIO11
|
||||
#define UART1_DTR_GPIO_INSTANCE 0
|
||||
#define UART1_DTR_GPIO_PIN 11
|
||||
|
||||
#define UART1_RI_PAD_INDEX 44 // AONIO 4 = GPIO24
|
||||
#define UART1_RI_GPIO_INSTANCE 1
|
||||
#define UART1_RI_GPIO_PIN 8
|
||||
#define UART1_RI_PWM_INSTANCE 1
|
||||
#define UART1_RI_PWM_CLK_ID FCLK_TIMER1
|
||||
#define UART1_RI_PWM_CLK_SEL FCLK_TIMER1_SEL_26M
|
||||
|
||||
#define UART1_DCD_PAD_INDEX 45 // AONIO 5 = GPIO25
|
||||
#define UART1_DCD_GPIO_INSTANCE 1
|
||||
#define UART1_DCD_GPIO_PIN 9
|
||||
#endif
|
||||
|
||||
#if (RTE_UART2 == 1)
|
||||
#define UART2_DTR_PAD_INDEX 25 // GPIO10
|
||||
#define UART2_DTR_GPIO_INSTANCE 0
|
||||
#define UART2_DTR_GPIO_PIN 10
|
||||
|
||||
#define UART2_RI_PAD_INDEX 43 // AONIO 3 = GPIO23
|
||||
#define UART2_RI_GPIO_INSTANCE 1
|
||||
#define UART2_RI_GPIO_PIN 7
|
||||
#define UART2_RI_PWM_INSTANCE 0
|
||||
#define UART2_RI_PWM_CLK_ID FCLK_TIMER0
|
||||
#define UART2_RI_PWM_CLK_SEL FCLK_TIMER0_SEL_26M
|
||||
|
||||
#define UART2_DCD_PAD_INDEX 47 // AONIO 7 = GPIO27
|
||||
#define UART2_DCD_GPIO_INSTANCE 1
|
||||
#define UART2_DCD_GPIO_PIN 11
|
||||
#endif
|
||||
|
||||
|
||||
#define NETLIGHT_PAD_INDEX 46 // AONIO 6 = GPIO26
|
||||
#define NETLIGHT_PAD_ALT_FUNC PAD_MUX_ALT5
|
||||
#define NETLIGHT_PWM_INSTANCE 3
|
||||
|
||||
//USIM1 OPTION1
|
||||
#define USIM1_URST_OP1_PAD_INDEX 19 // GPIO4
|
||||
#define USIM1_URST_OP1_GPIO_INSTANCE 0
|
||||
#define USIM1_URST_OP1_GPIO_PIN 4
|
||||
#define USIM1_UCLK_OP1_PAD_INDEX 20 // GPIO5
|
||||
#define USIM1_UCLK_OP1_GPIO_INSTANCE 0
|
||||
#define USIM1_UCLK_OP1_GPIO_PIN 5
|
||||
#define USIM1_UIO_OP1_PAD_INDEX 21 // GPIO6
|
||||
#define USIM1_UIO_OP1_GPIO_INSTANCE 0
|
||||
#define USIM1_UIO_OP1_GPIO_PIN 6
|
||||
|
||||
//USIM1 OPTION2
|
||||
#define USIM1_UIO_OP2_PAD_INDEX 27 // GPIO12
|
||||
#define USIM1_UIO_OP2_GPIO_INSTANCE 0
|
||||
#define USIM1_UIO_OP2_GPIO_PIN 12
|
||||
#define USIM1_URST_OP2_PAD_INDEX 28 // GPIO13
|
||||
#define USIM1_URST_OP2_GPIO_INSTANCE 0
|
||||
#define USIM1_URST_OP2_GPIO_PIN 13
|
||||
#define USIM1_UCLK_OP2_PAD_INDEX 29 // GPIO14
|
||||
#define USIM1_UCLK_OP2_GPIO_INSTANCE 0
|
||||
#define USIM1_UCLK_OP2_GPIO_PIN 14
|
||||
|
||||
//USIM1 clock latched by AONIO, for example, use AONIO-6 test on EVB
|
||||
#define AONIO_6_PAD_INDEX 46 // AONIO 6 = GPIO26
|
||||
#define AONIO_6_GPIO_INSTANCE 1
|
||||
#define AONIO_6_GPIO_PIN 10
|
||||
|
||||
#define RTE_CSPI0 0
|
||||
|
||||
#define RTE_CSPI0_MCLK_PAD_ADDR 39
|
||||
#define RTE_CSPI0_MCLK_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI0_PCLK_PAD_ADDR 35
|
||||
#define RTE_CSPI0_PCLK_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI0_CS_PAD_ADDR 36
|
||||
#define RTE_CSPI0_CS_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI0_SDO0_PAD_ADDR 37
|
||||
#define RTE_CSPI0_SDO0_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI0_SDO1_PAD_ADDR 38
|
||||
#define RTE_CSPI0_SDO1_FUNC PAD_MUX_ALT1
|
||||
|
||||
// DMA CSPI0 Request ID
|
||||
#define RTE_CSPI0_DMA_RX_REQID DMA_REQUEST_I2S0_RX
|
||||
|
||||
// CSPI1 Configuration
|
||||
#define RTE_CSPI1 1
|
||||
|
||||
#define RTE_CSPI1_MCLK_PAD_ADDR 18
|
||||
#define RTE_CSPI1_MCLK_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI1_PCLK_PAD_ADDR 19
|
||||
#define RTE_CSPI1_PCLK_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI1_CS_PAD_ADDR 20
|
||||
#define RTE_CSPI1_CS_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI1_SDO0_PAD_ADDR 21
|
||||
#define RTE_CSPI1_SDO0_FUNC PAD_MUX_ALT1
|
||||
|
||||
#define RTE_CSPI1_SDO1_PAD_ADDR 22
|
||||
#define RTE_CSPI1_SDO1_FUNC PAD_MUX_ALT1
|
||||
|
||||
// DMA CSPI1 Request ID
|
||||
#define RTE_CSPI1_DMA_RX_REQID DMA_REQUEST_I2S1_RX
|
||||
|
||||
#endif /* __RTE_DEVICE_H */
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef __AUDIO_TASK__
|
||||
#define __AUDIO_TASK__
|
||||
#include "luat_audio_play_ec618.h"
|
||||
typedef struct
|
||||
{
|
||||
uint8_t playType;
|
||||
struct
|
||||
{
|
||||
luat_audio_play_info_t *info;
|
||||
uint8_t count;
|
||||
} file;
|
||||
void *userParam;
|
||||
} audioQueueData;
|
||||
typedef enum
|
||||
{
|
||||
SPEECH_BY_SYNTHESIS_PLAY,
|
||||
} AUDIO_PLAY_TYPE;
|
||||
void audio_task_init(void);
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef __HTTP_TASK_QUEUE_H__
|
||||
#define __HTTP_TASK_QUEUE_H__
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SPEECH_POST,
|
||||
PUSH_SPEECH,
|
||||
SPEECH_BY_SYNTHESIS
|
||||
}SPEECH_TYPE_E;
|
||||
|
||||
typedef struct http_queue
|
||||
{
|
||||
char *url;
|
||||
char *filename;
|
||||
SPEECH_TYPE_E type;
|
||||
}http_queue_t;
|
||||
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,138 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "common_api.h"
|
||||
#include "audio_task.h"
|
||||
#include "bsp_custom.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_audio_play_ec618.h"
|
||||
#include "luat_i2s_ec618.h"
|
||||
#include "luat_gpio.h"
|
||||
#include "luat_debug.h"
|
||||
#include "audio_task.h"
|
||||
#include "power_audio.h"
|
||||
// AIR780E+TM8211开发板配置
|
||||
#define CODEC_PWR_PIN HAL_GPIO_12
|
||||
#define CODEC_PWR_PIN_ALT_FUN 4
|
||||
#define PA_PWR_PIN HAL_GPIO_25
|
||||
#define PA_PWR_PIN_ALT_FUN 0
|
||||
#define CHARGE_EN_PIN HAL_GPIO_2
|
||||
#define CHARGE_EN_PIN_ALT_FUN 0
|
||||
|
||||
#define AUDIO_QUEUE_SIZE 100
|
||||
extern bool http_get_status; // 这只是一个示例,没有对于多次下发动态报文的处理,简单使用一个变量来控制一下
|
||||
|
||||
extern const unsigned char audiopoweron[];
|
||||
static luat_rtos_semaphore_t audio_semaphore_handle;
|
||||
static luat_rtos_task_handle audio_task_handle;
|
||||
|
||||
luat_rtos_queue_t audio_queue_handle;
|
||||
static uint8_t audio_sleep_handler = 0xff;
|
||||
static HANDLE g_s_delay_timer;
|
||||
|
||||
void audio_data_cb(uint8_t *data, uint32_t len, uint8_t bits, uint8_t channels)
|
||||
{
|
||||
HAL_I2sSrcAdjustVolumn(data, len, 10);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task %x,%d,%d,%d,%d", data, len, bits, channels);
|
||||
}
|
||||
void app_pa_on(uint32_t arg)
|
||||
{
|
||||
luat_gpio_set(PA_PWR_PIN, 1); // 如果是780E+音频扩展小板,可以注释掉此行代码,因为PA长开
|
||||
}
|
||||
void audio_event_cb(uint32_t event, void *param)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task event_cb %d", event);
|
||||
switch (event)
|
||||
{
|
||||
case MULTIMEDIA_CB_AUDIO_DECODE_START:
|
||||
luat_gpio_set(CODEC_PWR_PIN, 1);
|
||||
luat_audio_play_write_blank_raw(0, 6, 1);
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_OUTPUT_START:
|
||||
luat_rtos_timer_start(g_s_delay_timer, 200, 0, app_pa_on, NULL); // 如果是780E+音频扩展小板,可以注释掉此行代码,因为PA长开
|
||||
break;
|
||||
case MULTIMEDIA_CB_TTS_INIT:
|
||||
break;
|
||||
case LUAT_MULTIMEDIA_CB_TTS_DONE:
|
||||
if (!luat_audio_play_get_last_error(0))
|
||||
{
|
||||
luat_audio_play_write_blank_raw(0, 1, 0);
|
||||
}
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_DONE:
|
||||
luat_rtos_timer_stop(g_s_delay_timer);
|
||||
LUAT_DEBUG_PRINT("audio play done, result=%d!", luat_audio_play_get_last_error(0));
|
||||
luat_gpio_set(PA_PWR_PIN, 0); // 如果是780E+音频扩展小板,可以注释掉此行代码,因为PA长开
|
||||
luat_gpio_set(CODEC_PWR_PIN, 0);
|
||||
luat_rtos_semaphore_release(audio_semaphore_handle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void audio_task(void *param)
|
||||
{
|
||||
audioQueueData audioQueueRecv = {0};
|
||||
uint32_t result = 0;
|
||||
while (1)
|
||||
{
|
||||
if (luat_rtos_queue_recv(audio_queue_handle, &audioQueueRecv, NULL, portMAX_DELAY) == 0)
|
||||
{
|
||||
|
||||
luat_audio_play_multi_files(0, audioQueueRecv.file.info, audioQueueRecv.file.count);
|
||||
luat_rtos_semaphore_take(audio_semaphore_handle, LUAT_WAIT_FOREVER);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task this is play wait result %d", result);
|
||||
if(audioQueueRecv.playType == SPEECH_BY_SYNTHESIS_PLAY)
|
||||
{
|
||||
http_get_status = false; // 这只是一个示例,没有对于多次下发动态报文的处理,简单使用一个变量来控制一下
|
||||
}
|
||||
if (audioQueueRecv.file.info != NULL)
|
||||
{
|
||||
free(audioQueueRecv.file.info);
|
||||
}
|
||||
|
||||
if (audioQueueRecv.userParam != NULL)
|
||||
{
|
||||
luat_fs_remove(audioQueueRecv.userParam);
|
||||
free(audioQueueRecv.userParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
luat_rtos_task_delete(audio_task_handle);
|
||||
}
|
||||
|
||||
void audio_task_init(void)
|
||||
{
|
||||
luat_rtos_timer_create(&g_s_delay_timer);
|
||||
|
||||
luat_gpio_cfg_t gpio_cfg;
|
||||
luat_gpio_set_default_cfg(&gpio_cfg);
|
||||
|
||||
gpio_cfg.pull = LUAT_GPIO_DEFAULT;
|
||||
|
||||
// 如果是780E+音频扩展小板,可以注释掉下面两行代码,因为PA长开
|
||||
gpio_cfg.pin = PA_PWR_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
|
||||
gpio_cfg.pin = CODEC_PWR_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
gpio_cfg.alt_fun = CODEC_PWR_PIN_ALT_FUN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
|
||||
luat_audio_play_global_init(audio_event_cb, audio_data_cb, luat_audio_play_file_default_fun, NULL, NULL);
|
||||
// luat_i2s_base_setup(0, I2S_MODE_I2S, I2S_FRAME_SIZE_16_16); //如果是780E+音频扩展小板,打开这行注释代码,这个配置对应ES7148/ES7149
|
||||
luat_i2s_base_setup(0, I2S_MODE_MSB, I2S_FRAME_SIZE_16_16); // 此处配置对应TM8211
|
||||
luat_rtos_semaphore_create(&audio_semaphore_handle, 1);
|
||||
|
||||
luat_rtos_queue_create(&audio_queue_handle, AUDIO_QUEUE_SIZE, sizeof(audioQueueData));
|
||||
audioQueueData powerOn = {0};
|
||||
powerOn.file.info = (audio_play_info_t *)calloc(1, sizeof(audio_play_info_t));
|
||||
powerOn.file.info->path = NULL;
|
||||
powerOn.file.info->address = Fqdqwer;
|
||||
powerOn.file.info->rom_data_len = sizeof(Fqdqwer);
|
||||
powerOn.file.count = 1;
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &powerOn, NULL, 0))
|
||||
{
|
||||
free(powerOn.file.info);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task start send audio fail");
|
||||
}
|
||||
int result = luat_rtos_task_create(&audio_task_handle, 2048, 20, "mqtt", audio_task, NULL, NULL);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task create task result %d", result);
|
||||
}
|
||||
@@ -0,0 +1,738 @@
|
||||
/*
|
||||
* 这个例程适用于`Linux`这类支持task的POSIX设备, 它演示了用SDK配置MQTT参数并建立连接, 之后创建2个线程
|
||||
*
|
||||
* + 一个线程用于保活长连接
|
||||
* + 一个线程用于接收消息, 并在有消息到达时进入默认的数据回调, 在连接状态变化时进入事件回调
|
||||
*
|
||||
* 需要用户关注或修改的部分, 已经用 TODO 在注释中标明
|
||||
*
|
||||
*/
|
||||
#include "common_api.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "aiot_state_api.h"
|
||||
#include "aiot_sysdep_api.h"
|
||||
#include "aiot_mqtt_api.h"
|
||||
#include "luat_mobile.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_debug.h"
|
||||
#include "cJSON.h"
|
||||
#include "http_queue.h"
|
||||
#include "audio_task.h"
|
||||
|
||||
// 阿里云系统自带数字语料
|
||||
const char *tone_0 = "SYS_TONE_0.amr";
|
||||
const char *tone_1 = "SYS_TONE_1.amr";
|
||||
const char *tone_2 = "SYS_TONE_2.amr";
|
||||
const char *tone_3 = "SYS_TONE_3.amr";
|
||||
const char *tone_4 = "SYS_TONE_4.amr";
|
||||
const char *tone_5 = "SYS_TONE_5.amr";
|
||||
const char *tone_6 = "SYS_TONE_6.amr";
|
||||
const char *tone_7 = "SYS_TONE_7.amr";
|
||||
const char *tone_8 = "SYS_TONE_8.amr";
|
||||
const char *tone_9 = "SYS_TONE_9.amr";
|
||||
|
||||
/* TODO: 替换为自己设备的三元组 */
|
||||
char *product_key = "${YourProductKey}";
|
||||
char *device_name = "${YourDeviceName}";
|
||||
char *device_secret = "${YourDeviceSecret}";
|
||||
extern luat_rtos_queue_t http_queue_handle;
|
||||
extern luat_rtos_queue_t audio_queue_handle;
|
||||
extern bool http_get_status; // 这只是一个示例示例,没有对于多次下发动态报文的处理,简单使用一个变量来控制一下
|
||||
/*
|
||||
TODO: 替换为自己实例的接入点
|
||||
|
||||
对于企业实例, 或者2021年07月30日之后(含当日)开通的物联网平台服务下公共实例
|
||||
mqtt_host的格式为"${YourInstanceId}.mqtt.iothub.aliyuncs.com"
|
||||
其中${YourInstanceId}: 请替换为您企业/公共实例的Id
|
||||
|
||||
对于2021年07月30日之前(不含当日)开通的物联网平台服务下公共实例
|
||||
需要将mqtt_host修改为: mqtt_host = "${YourProductKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com"
|
||||
其中, ${YourProductKey}:请替换为设备所属产品的ProductKey。可登录物联网平台控制台,在对应实例的设备详情页获取。
|
||||
${YourRegionId}:请替换为您的物联网平台设备所在地域代码, 比如cn-shanghai等
|
||||
该情况下完整mqtt_host举例: a1TTmBPIChA.iot-as-mqtt.cn-shanghai.aliyuncs.com
|
||||
|
||||
详情请见: https://help.aliyun.com/document_detail/147356.html
|
||||
*/
|
||||
char *mqtt_host = "${YourInstanceId}.mqtt.iothub.aliyuncs.com";
|
||||
|
||||
/* 位于portfiles/aiot_port文件夹下的系统适配函数集合 */
|
||||
extern aiot_sysdep_portfile_t g_aiot_sysdep_portfile;
|
||||
|
||||
/* 位于external/ali_ca_cert.c中的服务器证书 */
|
||||
extern const char *ali_ca_cert;
|
||||
|
||||
static luat_rtos_task_handle g_mqtt_process_thread = NULL;
|
||||
static luat_rtos_task_handle g_mqtt_recv_thread = NULL;
|
||||
static uint8_t g_mqtt_process_thread_running = 0;
|
||||
static uint8_t g_mqtt_recv_thread_running = 0;
|
||||
|
||||
luat_rtos_semaphore_t net_semaphore_handle;
|
||||
|
||||
static luat_rtos_task_handle linksdk_task_handle;
|
||||
|
||||
// 将金额字符串格式化为单个文件形式
|
||||
int fomatMoney(int num, audioQueueData *data, int *index, bool flag)
|
||||
{
|
||||
uint32_t audioArray[10] =
|
||||
{
|
||||
tone_0,
|
||||
tone_1,
|
||||
tone_2,
|
||||
tone_3,
|
||||
tone_4,
|
||||
tone_5,
|
||||
tone_6,
|
||||
tone_7,
|
||||
tone_8,
|
||||
tone_9,
|
||||
};
|
||||
int thousand = (num - num % 1000) / 1000;
|
||||
int hundred = ((num % 1000) - ((num % 1000) % 100)) / 100;
|
||||
int ten = ((num % 100) - ((num % 100) % 10)) / 10;
|
||||
int unit = num % 10;
|
||||
if (thousand == 0)
|
||||
{
|
||||
thousand = -1;
|
||||
if (hundred == 0)
|
||||
{
|
||||
hundred = -1;
|
||||
if (ten == 0)
|
||||
{
|
||||
ten = -1;
|
||||
if (unit == 0)
|
||||
{
|
||||
unit = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (unit == 0)
|
||||
{
|
||||
unit = -1;
|
||||
if (ten == 0)
|
||||
{
|
||||
ten = -1;
|
||||
if (hundred == 0)
|
||||
{
|
||||
hundred = -1;
|
||||
if (thousand == 0)
|
||||
{
|
||||
thousand = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ten == 0 && hundred == 0)
|
||||
{
|
||||
ten = -1;
|
||||
}
|
||||
if (thousand != -1)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[thousand];
|
||||
}
|
||||
*index += 1;
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = "SYS_TONE_MEASURE_WORD_qian.amr";
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (hundred != -1)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[hundred];
|
||||
}
|
||||
*index += 1;
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = "SYS_TONE_MEASURE_WORD_bai.amr";
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (ten != -1)
|
||||
{
|
||||
if (!(ten == 1 && hundred == -1 && thousand == -1))
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[ten];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (ten != 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = "SYS_TONE_MEASURE_WORD_shi.amr";
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
if (unit != -1)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[unit];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// 将金额字符串格式化为单个文件形式
|
||||
static int strToFile(char *money, audioQueueData *data, int *index, bool flag)
|
||||
{
|
||||
uint32_t audioArray[10] =
|
||||
{
|
||||
tone_0,
|
||||
tone_1,
|
||||
tone_2,
|
||||
tone_3,
|
||||
tone_4,
|
||||
tone_5,
|
||||
tone_6,
|
||||
tone_7,
|
||||
tone_8,
|
||||
tone_9,
|
||||
};
|
||||
int count = 0;
|
||||
int integer = 0;
|
||||
char *str = NULL;
|
||||
char intStr[8] = {0};
|
||||
char decStr[3] = {0};
|
||||
str = strstr(money, ".");
|
||||
if (str != NULL)
|
||||
{
|
||||
memcpy(intStr, money, str - money);
|
||||
str = str + 1;
|
||||
memcpy(decStr, str, 2);
|
||||
integer = atoi(intStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
integer = atoi(money);
|
||||
}
|
||||
if (integer >= 10000)
|
||||
{
|
||||
int filecount = fomatMoney(integer / 10000, data, index, flag);
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = "SYS_TONE_MEASURE_WORD_wan.amr";
|
||||
}
|
||||
*index += 1;
|
||||
if (((integer % 10000) < 1000) && ((integer % 10000) != 0))
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[0];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
if ((integer % 10000) > 0)
|
||||
{
|
||||
int filecount = fomatMoney(integer % 10000, data, index, flag);
|
||||
}
|
||||
if (*index == 1)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[0];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
int decial = atoi(decStr);
|
||||
if (decial > 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = "SYS_TONE_dian.amr";
|
||||
}
|
||||
*index += 1;
|
||||
if (decial > 10)
|
||||
{
|
||||
|
||||
int ten = decial / 10;
|
||||
int unit = decial % 10;
|
||||
LUAT_DEBUG_PRINT("this is decial %d, %d, %d", decial, ten, unit);
|
||||
if (ten != 0 && unit != 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[ten];
|
||||
}
|
||||
*index += 1;
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[unit];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
else if (ten == 0 && unit != 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[0];
|
||||
}
|
||||
*index += 1;
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[unit];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
else if (ten != 0 && unit == 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[0];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = audioArray[decial];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
data->file.info[*index].path = "SYS_TONE_MONETARY_yuan.amr";
|
||||
}
|
||||
*index += 1;
|
||||
return count;
|
||||
}
|
||||
|
||||
void mobile_event_callback(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status)
|
||||
{
|
||||
if (event == LUAT_MOBILE_EVENT_NETIF && status == LUAT_MOBILE_NETIF_LINK_ON)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("netif acivated");
|
||||
luat_rtos_semaphore_release(net_semaphore_handle);
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: 如果要关闭日志, 就把这个函数实现为空, 如果要减少日志, 可根据code选择不打印
|
||||
*
|
||||
* 例如: [1577589489.033][LK-0317] mqtt_basic_demo&gb80sFmX7yX
|
||||
*
|
||||
* 上面这条日志的code就是0317(十六进制), code值的定义见core/aiot_state_api.h
|
||||
*
|
||||
*/
|
||||
|
||||
/* 日志回调函数, SDK的日志会从这里输出 */
|
||||
int32_t demo_state_logcb(int32_t code, char *message)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("linksdk message: %s", message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* MQTT事件回调函数, 当网络连接/重连/断开时被触发, 事件定义见core/aiot_mqtt_api.h */
|
||||
void demo_mqtt_event_handler(void *handle, const aiot_mqtt_event_t *event, void *userdata)
|
||||
{
|
||||
switch (event->type)
|
||||
{
|
||||
/* SDK因为用户调用了aiot_mqtt_connect()接口, 与mqtt服务器建立连接已成功 */
|
||||
case AIOT_MQTTEVT_CONNECT:
|
||||
{
|
||||
LUAT_DEBUG_PRINT("AIOT_MQTTEVT_CONNECT\n");
|
||||
/* TODO: 处理SDK建连成功, 不可以在这里调用耗时较长的阻塞函数 */
|
||||
}
|
||||
break;
|
||||
|
||||
/* SDK因为网络状况被动断连后, 自动发起重连已成功 */
|
||||
case AIOT_MQTTEVT_RECONNECT:
|
||||
{
|
||||
LUAT_DEBUG_PRINT("AIOT_MQTTEVT_RECONNECT\n");
|
||||
/* TODO: 处理SDK重连成功, 不可以在这里调用耗时较长的阻塞函数 */
|
||||
}
|
||||
break;
|
||||
|
||||
/* SDK因为网络的状况而被动断开了连接, network是底层读写失败, heartbeat是没有按预期得到服务端心跳应答 */
|
||||
case AIOT_MQTTEVT_DISCONNECT:
|
||||
{
|
||||
char *cause = (event->data.disconnect == AIOT_MQTTDISCONNEVT_NETWORK_DISCONNECT) ? ("network disconnect") : ("heartbeat disconnect");
|
||||
LUAT_DEBUG_PRINT("AIOT_MQTTEVT_DISCONNECT: %s\n", cause);
|
||||
/* TODO: 处理SDK被动断连, 不可以在这里调用耗时较长的阻塞函数 */
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* MQTT默认消息处理回调, 当SDK从服务器收到MQTT消息时, 且无对应用户回调处理时被调用 */
|
||||
void demo_mqtt_default_recv_handler(void *handle, const aiot_mqtt_recv_t *packet, void *userdata)
|
||||
{
|
||||
switch (packet->type)
|
||||
{
|
||||
case AIOT_MQTTRECV_HEARTBEAT_RESPONSE:
|
||||
{
|
||||
LUAT_DEBUG_PRINT("heartbeat response\n");
|
||||
/* TODO: 处理服务器对心跳的回应, 一般不处理 */
|
||||
}
|
||||
break;
|
||||
|
||||
case AIOT_MQTTRECV_SUB_ACK:
|
||||
{
|
||||
LUAT_DEBUG_PRINT("suback, res: -0x%04X, packet id: %d, max qos: %d\n",
|
||||
-packet->data.sub_ack.res, packet->data.sub_ack.packet_id, packet->data.sub_ack.max_qos);
|
||||
/* TODO: 处理服务器对订阅请求的回应, 一般不处理 */
|
||||
}
|
||||
break;
|
||||
|
||||
case AIOT_MQTTRECV_PUB:
|
||||
{
|
||||
LUAT_DEBUG_PRINT("pub, qos: %d, topic: %.*s\n", packet->data.pub.qos, packet->data.pub.topic_len, packet->data.pub.topic);
|
||||
LUAT_DEBUG_PRINT("pub, payload: %.*s\n", packet->data.pub.payload_len, packet->data.pub.payload);
|
||||
/* 处理服务器下发的业务报文 */
|
||||
// TODO: 换成自己的项目key和设备名
|
||||
// 此主题为语料下载主题,阿里云推送语料会下发一个下载json文件的url,此json中包含语料下载地址
|
||||
if (strcmp("/sys/${YourProductKey}/${YourDeviceName}/thing/service/SpeechPost", packet->data.pub.topic) == 0)
|
||||
{
|
||||
cJSON *boss = NULL;
|
||||
boss = cJSON_Parse((const char *)packet->data.pub.payload);
|
||||
if (boss == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("PARES FAIL");
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("this is boss type %d", boss->type);
|
||||
cJSON *method = cJSON_GetObjectItem(boss, "method");
|
||||
cJSON *id = cJSON_GetObjectItem(boss, "id");
|
||||
cJSON *version = cJSON_GetObjectItem(boss, "version");
|
||||
cJSON *params = cJSON_GetObjectItem(boss, "params");
|
||||
|
||||
cJSON *url = cJSON_GetObjectItem(params, "url");
|
||||
http_queue_t send_http_url = {0};
|
||||
send_http_url.url = malloc(strlen(url->valuestring) + 1);
|
||||
send_http_url.type = SPEECH_POST;
|
||||
memset(send_http_url.url, 0x00, strlen(url->valuestring) + 1);
|
||||
memcpy(send_http_url.url, url->valuestring, strlen(url->valuestring));
|
||||
if (-1 == luat_rtos_queue_send(http_queue_handle, &send_http_url, NULL, 0))
|
||||
{
|
||||
free(send_http_url.url);
|
||||
LUAT_DEBUG_PRINT("http send requet fail");
|
||||
}
|
||||
LUAT_DEBUG_PRINT("this is url %s", url->valuestring);
|
||||
}
|
||||
cJSON_Delete(boss);
|
||||
}
|
||||
// TODO: 换成自己的项目key和设备名
|
||||
/*
|
||||
此主题为音频播报主题,阿里云推送音频播报会往此主题下发一个报文,这里只对金额做了解析
|
||||
下发的内容需为"{$number}",其中number是一个不大于99999999.99的字符串
|
||||
如"{$10000.11}",设备会播报一万点一一元
|
||||
*/
|
||||
else if (strcmp("/sys/${YourProductKey}/${YourDeviceName}/thing/service/SpeechBroadcast", packet->data.pub.topic) == 0)
|
||||
{
|
||||
cJSON *boss = NULL;
|
||||
boss = cJSON_Parse((const char *)packet->data.pub.payload);
|
||||
if (boss == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("PARES FAIL");
|
||||
}
|
||||
else
|
||||
{
|
||||
cJSON *method = cJSON_GetObjectItem(boss, "method");
|
||||
cJSON *params = cJSON_GetObjectItem(boss, "params");
|
||||
cJSON *speech = cJSON_GetObjectItem(params, "speechs");
|
||||
int array_cnt = cJSON_GetArraySize(speech);
|
||||
char *head = NULL;
|
||||
char *tail = NULL;
|
||||
for (int i = 0; i < array_cnt; i++)
|
||||
{
|
||||
cJSON *money = cJSON_GetArrayItem(speech, i);
|
||||
head = strstr(money->valuestring, "{$");
|
||||
if (head != NULL)
|
||||
{
|
||||
head++;
|
||||
head++;
|
||||
tail = strstr(head, "}");
|
||||
}
|
||||
}
|
||||
if (tail != NULL)
|
||||
{
|
||||
audioQueueData moneyPlay = {0};
|
||||
int index = 0;
|
||||
char moneyValue[20] = {0};
|
||||
memcpy(moneyValue, head, tail - head);
|
||||
strToFile(moneyValue, &moneyPlay, &index, false);
|
||||
moneyPlay.file.info = (audio_play_info_t *)calloc(index, sizeof(audio_play_info_t));
|
||||
index = 0;
|
||||
strToFile(moneyValue, &moneyPlay, &index, true);
|
||||
moneyPlay.file.count = index;
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &moneyPlay, NULL, 0))
|
||||
{
|
||||
free(moneyPlay.file.info);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt sub queue send error");
|
||||
}
|
||||
}
|
||||
}
|
||||
cJSON_Delete(boss);
|
||||
}
|
||||
// TODO: 换成自己的项目key和设备名
|
||||
/*
|
||||
此主题为动态音频播报主题,阿里云推送音频播报会往此主题下发一个音频下载url
|
||||
这里解析url然后发送到httptask去下载音频
|
||||
此示例播放完毕后会删掉音频
|
||||
*/
|
||||
else if (strcmp("/sys/${YourProductKey}/${YourDeviceName}/thing/service/AudioPlayback", packet->data.pub.topic) == 0)
|
||||
{
|
||||
if (!http_get_status)
|
||||
{
|
||||
cJSON *boss = NULL;
|
||||
boss = cJSON_Parse((const char *)packet->data.pub.payload);
|
||||
if (boss == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("PARES FAIL");
|
||||
}
|
||||
else
|
||||
{
|
||||
cJSON *params = cJSON_GetObjectItem(boss, "params");
|
||||
cJSON *format = cJSON_GetObjectItem(params, "format");
|
||||
cJSON *id = cJSON_GetObjectItem(params, "id");
|
||||
cJSON *url = cJSON_GetObjectItem(params, "url");
|
||||
http_queue_t send_http_url = {0};
|
||||
send_http_url.type = SPEECH_BY_SYNTHESIS;
|
||||
send_http_url.filename = malloc(strlen(id->valuestring) + strlen(format->valuestring) + 3);
|
||||
memset(send_http_url.filename, 0x00, strlen(id->valuestring) + strlen(format->valuestring) + 3);
|
||||
snprintf(send_http_url.filename, strlen(id->valuestring) + strlen(format->valuestring) + 3, "%s%s%s", id->valuestring, ".", format->valuestring);
|
||||
|
||||
send_http_url.url = malloc(strlen(url->valuestring) + 1);
|
||||
|
||||
memset(send_http_url.url, 0x00, strlen(url->valuestring) + 1);
|
||||
memcpy(send_http_url.url, url->valuestring, strlen(url->valuestring) + 1);
|
||||
|
||||
if (-1 == luat_rtos_queue_send(http_queue_handle, &send_http_url, NULL, 0))
|
||||
{
|
||||
free(send_http_url.filename);
|
||||
free(send_http_url.url);
|
||||
LUAT_DEBUG_PRINT("http send requet fail");
|
||||
}
|
||||
|
||||
LUAT_DEBUG_PRINT("this is url %s", url->valuestring);
|
||||
http_get_status = true;
|
||||
}
|
||||
cJSON_Delete(boss);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case AIOT_MQTTRECV_PUB_ACK:
|
||||
{
|
||||
LUAT_DEBUG_PRINT("puback, packet id: %d\n", packet->data.pub_ack.packet_id);
|
||||
/* TODO: 处理服务器对QoS1上报消息的回应, 一般不处理 */
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 执行aiot_mqtt_process的线程, 包含心跳发送和QoS1消息重发 */
|
||||
void *demo_mqtt_process_thread(void *args)
|
||||
{
|
||||
int32_t res = STATE_SUCCESS;
|
||||
|
||||
while (g_mqtt_process_thread_running)
|
||||
{
|
||||
res = aiot_mqtt_process(args);
|
||||
if (res == STATE_USER_INPUT_EXEC_DISABLED)
|
||||
{
|
||||
break;
|
||||
}
|
||||
luat_rtos_task_sleep(1000);
|
||||
}
|
||||
luat_rtos_task_delete(g_mqtt_process_thread);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 执行aiot_mqtt_recv的线程, 包含网络自动重连和从服务器收取MQTT消息 */
|
||||
void *demo_mqtt_recv_thread(void *args)
|
||||
{
|
||||
int32_t res = STATE_SUCCESS;
|
||||
|
||||
while (g_mqtt_recv_thread_running)
|
||||
{
|
||||
res = aiot_mqtt_recv(args);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
if (res == STATE_USER_INPUT_EXEC_DISABLED)
|
||||
{
|
||||
break;
|
||||
}
|
||||
luat_rtos_task_sleep(1);
|
||||
}
|
||||
}
|
||||
luat_rtos_task_delete(g_mqtt_recv_thread);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int linksdk_mqtt_task(void *param)
|
||||
{
|
||||
int32_t res = STATE_SUCCESS;
|
||||
void *mqtt_handle = NULL;
|
||||
uint16_t port = 443; /* 无论设备是否使用TLS连接阿里云平台, 目的端口都是443 */
|
||||
aiot_sysdep_network_cred_t cred; /* 安全凭据结构体, 如果要用TLS, 这个结构体中配置CA证书等参数 */
|
||||
|
||||
luat_rtos_semaphore_create(&net_semaphore_handle, 1);
|
||||
|
||||
luat_mobile_event_register_handler(mobile_event_callback);
|
||||
|
||||
luat_rtos_semaphore_take(net_semaphore_handle, LUAT_WAIT_FOREVER);
|
||||
/* 配置SDK的底层依赖 */
|
||||
aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
|
||||
/* 配置SDK的日志输出 */
|
||||
aiot_state_set_logcb(demo_state_logcb);
|
||||
|
||||
/* 创建SDK的安全凭据, 用于建立TLS连接 */
|
||||
memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
|
||||
cred.option = AIOT_SYSDEP_NETWORK_CRED_SVRCERT_CA; /* 使用RSA证书校验MQTT服务端 */
|
||||
cred.max_tls_fragment = 16384; /* 最大的分片长度为16K, 其它可选值还有4K, 2K, 1K, 0.5K */
|
||||
cred.sni_enabled = 1; /* TLS建连时, 支持Server Name Indicator */
|
||||
cred.x509_server_cert = ali_ca_cert; /* 用来验证MQTT服务端的RSA根证书 */
|
||||
cred.x509_server_cert_len = strlen(ali_ca_cert); /* 用来验证MQTT服务端的RSA根证书长度 */
|
||||
|
||||
/* 创建1个MQTT客户端实例并内部初始化默认参数 */
|
||||
mqtt_handle = aiot_mqtt_init();
|
||||
if (mqtt_handle == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("aiot_mqtt_init failed\n");
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TODO: 如果以下代码不被注释, 则例程会用TCP而不是TLS连接云平台 */
|
||||
|
||||
{
|
||||
memset(&cred, 0, sizeof(aiot_sysdep_network_cred_t));
|
||||
cred.option = AIOT_SYSDEP_NETWORK_CRED_NONE;
|
||||
}
|
||||
|
||||
/* 配置MQTT服务器地址 */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_HOST, (void *)mqtt_host);
|
||||
/* 配置MQTT服务器端口 */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PORT, (void *)&port);
|
||||
/* 配置设备productKey */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_PRODUCT_KEY, (void *)product_key);
|
||||
/* 配置设备deviceName */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_NAME, (void *)device_name);
|
||||
/* 配置设备deviceSecret */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_DEVICE_SECRET, (void *)device_secret);
|
||||
/* 配置网络连接的安全凭据, 上面已经创建好了 */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_NETWORK_CRED, (void *)&cred);
|
||||
/* 配置MQTT默认消息接收回调函数 */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_RECV_HANDLER, (void *)demo_mqtt_default_recv_handler);
|
||||
/* 配置MQTT事件回调函数 */
|
||||
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_EVENT_HANDLER, (void *)demo_mqtt_event_handler);
|
||||
|
||||
/* 与服务器建立MQTT连接 */
|
||||
res = aiot_mqtt_connect(mqtt_handle);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
/* 尝试建立连接失败, 销毁MQTT实例, 回收资源 */
|
||||
aiot_mqtt_deinit(&mqtt_handle);
|
||||
LUAT_DEBUG_PRINT("aiot_mqtt_connect failed: -0x%04X\n\r\n", -res);
|
||||
LUAT_DEBUG_PRINT("please check variables like mqtt_host, produt_key, device_name, device_secret in demo\r\n");
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* MQTT 订阅topic功能示例, 请根据自己的业务需求进行使用 */
|
||||
/* {
|
||||
char *sub_topic = "/sys/${YourProductKey}/${YourDeviceName}/thing/event/+/post_reply";
|
||||
res = aiot_mqtt_sub(mqtt_handle, sub_topic, NULL, 1, NULL);
|
||||
if (res < 0) {
|
||||
DBG("aiot_mqtt_sub failed, res: -0x%04X\n", -res);
|
||||
vTaskDelete(NULL);
|
||||
return -1;
|
||||
}
|
||||
} */
|
||||
|
||||
/* MQTT 发布消息功能示例, 请根据自己的业务需求进行使用 */
|
||||
/* {
|
||||
char *pub_topic = "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post";
|
||||
char *pub_payload = "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}}";
|
||||
res = aiot_mqtt_pub(mqtt_handle, pub_topic, (uint8_t *)pub_payload, (uint32_t)strlen(pub_payload), 0);
|
||||
if (res < 0) {
|
||||
LUAT_DEBUG_PRINT("aiot_mqtt_sub failed, res: -0x%04X\n", -res);
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
} */
|
||||
|
||||
/* 创建一个单独的线程, 专用于执行aiot_mqtt_process, 它会自动发送心跳保活, 以及重发QoS1的未应答报文 */
|
||||
g_mqtt_process_thread_running = 1;
|
||||
luat_rtos_task_create(&g_mqtt_process_thread, 4096, 20, "", demo_mqtt_process_thread, mqtt_handle, NULL);
|
||||
if (g_mqtt_process_thread == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("task_create demo_mqtt_process_thread failed: %d\n", res);
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 创建一个单独的线程用于执行aiot_mqtt_recv, 它会循环收取服务器下发的MQTT消息, 并在断线时自动重连 */
|
||||
g_mqtt_recv_thread_running = 1;
|
||||
luat_rtos_task_create(&g_mqtt_recv_thread, 4096, 20, "", demo_mqtt_recv_thread, mqtt_handle, NULL);
|
||||
if (g_mqtt_recv_thread == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("task_create demo_mqtt_recv_thread failed: %d\n", res);
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 主循环进入休眠 */
|
||||
while (1)
|
||||
{
|
||||
luat_rtos_task_sleep(1000);
|
||||
}
|
||||
|
||||
/* 断开MQTT连接, 一般不会运行到这里 */
|
||||
g_mqtt_process_thread_running = 0;
|
||||
g_mqtt_recv_thread_running = 0;
|
||||
luat_rtos_task_sleep(1);
|
||||
|
||||
res = aiot_mqtt_disconnect(mqtt_handle);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
aiot_mqtt_deinit(&mqtt_handle);
|
||||
LUAT_DEBUG_PRINT("aiot_mqtt_disconnect failed: -0x%04X\n", -res);
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 销毁MQTT实例, 一般不会运行到这里 */
|
||||
res = aiot_mqtt_deinit(&mqtt_handle);
|
||||
if (res < STATE_SUCCESS)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("aiot_mqtt_deinit failed: -0x%04X\n", -res);
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return -1;
|
||||
}
|
||||
luat_rtos_task_delete(linksdk_task_handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void task_demo_init(void)
|
||||
{
|
||||
luat_rtos_task_create(&linksdk_task_handle, 4096, 20, "", linksdk_mqtt_task, NULL, NULL);
|
||||
}
|
||||
extern void task_demo_https(void);
|
||||
extern void audio_task_init(void);
|
||||
INIT_TASK_EXPORT(audio_task_init, "1");
|
||||
INIT_TASK_EXPORT(task_demo_init, "1");
|
||||
INIT_TASK_EXPORT(task_demo_https, "1");
|
||||
@@ -0,0 +1,319 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "common_api.h"
|
||||
#include "luat_mobile.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_debug.h"
|
||||
#include "http_queue.h"
|
||||
#include "luat_fs.h"
|
||||
#include "HTTPClient.h"
|
||||
#include "cJSON.h"
|
||||
#include "audio_task.h"
|
||||
luat_rtos_queue_t http_queue_handle;
|
||||
extern luat_rtos_queue_t audio_queue_handle;
|
||||
|
||||
bool http_get_status = false; // 这只是一个示例,没有对于多次下发动态报文的处理,简单使用一个变量来控制一下
|
||||
// ciphersuite: TLS-RSA-WITH-AES-128-CBC-SHA msglen = 2609
|
||||
// support session ticket/fragment
|
||||
#define HTTP_RECV_BUF_SIZE (5000)
|
||||
#define HTTP_HEAD_BUF_SIZE (800)
|
||||
|
||||
static HttpClientContext gHttpClient = {0};
|
||||
|
||||
/**
|
||||
\fn INT32 httpGetData(CHAR *getUrl, CHAR *buf, UINT32 len)
|
||||
\brief
|
||||
\return
|
||||
*/
|
||||
static INT32 httpGetData(CHAR *getUrl, CHAR *buf, UINT32 len)
|
||||
{
|
||||
HTTPResult result = HTTP_INTERNAL;
|
||||
HttpClientData clientData = {0};
|
||||
UINT32 count = 0;
|
||||
uint16_t headerLen = 0;
|
||||
|
||||
LUAT_DEBUG_ASSERT(buf != NULL, 0, 0, 0);
|
||||
|
||||
clientData.headerBuf = malloc(HTTP_HEAD_BUF_SIZE);
|
||||
clientData.headerBufLen = HTTP_HEAD_BUF_SIZE;
|
||||
clientData.respBuf = buf;
|
||||
clientData.respBufLen = len;
|
||||
|
||||
result = httpSendRequest(&gHttpClient, getUrl, HTTP_GET, &clientData);
|
||||
LUAT_DEBUG_PRINT("send request result=%d", result);
|
||||
if (result != HTTP_OK)
|
||||
goto exit;
|
||||
do
|
||||
{
|
||||
LUAT_DEBUG_PRINT("recvResponse loop.");
|
||||
memset(clientData.headerBuf, 0, clientData.headerBufLen);
|
||||
memset(clientData.respBuf, 0, clientData.respBufLen);
|
||||
result = httpRecvResponse(&gHttpClient, &clientData);
|
||||
if (result == HTTP_OK || result == HTTP_MOREDATA)
|
||||
{
|
||||
headerLen = strlen(clientData.headerBuf);
|
||||
if (headerLen > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("total content length=%d", clientData.recvContentLength);
|
||||
}
|
||||
|
||||
if (clientData.blockContentLen > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("response content:{%s}", (uint8_t *)clientData.respBuf);
|
||||
}
|
||||
count += clientData.blockContentLen;
|
||||
LUAT_DEBUG_PRINT("has recv=%d", count);
|
||||
}
|
||||
} while (result == HTTP_MOREDATA || result == HTTP_CONN);
|
||||
|
||||
LUAT_DEBUG_PRINT("result=%d", result);
|
||||
if (gHttpClient.httpResponseCode < 200 || gHttpClient.httpResponseCode > 404)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("invalid http response code=%d", gHttpClient.httpResponseCode);
|
||||
}
|
||||
else if (count == 0 || count != clientData.recvContentLength)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("data not receive complete");
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("receive success");
|
||||
}
|
||||
exit:
|
||||
free(clientData.headerBuf);
|
||||
return result;
|
||||
}
|
||||
|
||||
static INT32 httpGetDataToFile(CHAR *getUrl, CHAR *buf, UINT32 len, char *file) //http下载数据并保存在文件系统
|
||||
{
|
||||
LUAT_DEBUG_PRINT("this wait write filename %s", file);
|
||||
HTTPResult result = HTTP_INTERNAL;
|
||||
HttpClientData clientData = {0};
|
||||
UINT32 count = 0;
|
||||
int status;
|
||||
uint16_t headerLen = 0;
|
||||
|
||||
LUAT_DEBUG_ASSERT(buf != NULL, 0, 0, 0);
|
||||
|
||||
clientData.headerBuf = malloc(HTTP_HEAD_BUF_SIZE);
|
||||
clientData.headerBufLen = HTTP_HEAD_BUF_SIZE;
|
||||
clientData.respBuf = buf;
|
||||
clientData.respBufLen = len;
|
||||
FILE *file_handle = luat_fs_fopen(file, "wb+");
|
||||
|
||||
result = httpSendRequest(&gHttpClient, getUrl, HTTP_GET, &clientData);
|
||||
LUAT_DEBUG_PRINT("send request result=%d", result);
|
||||
if (result != HTTP_OK)
|
||||
goto exit;
|
||||
do
|
||||
{
|
||||
LUAT_DEBUG_PRINT("recvResponse loop.");
|
||||
memset(clientData.headerBuf, 0, clientData.headerBufLen);
|
||||
memset(clientData.respBuf, 0, clientData.respBufLen);
|
||||
result = httpRecvResponse(&gHttpClient, &clientData);
|
||||
if (result == HTTP_OK || result == HTTP_MOREDATA)
|
||||
{
|
||||
headerLen = strlen(clientData.headerBuf);
|
||||
if (headerLen > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("total content length=%d", clientData.recvContentLength);
|
||||
}
|
||||
|
||||
if (clientData.blockContentLen > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("response content:{%s}", (uint8_t *)clientData.respBuf);
|
||||
}
|
||||
count += clientData.blockContentLen;
|
||||
status = luat_fs_fwrite((uint8_t *)clientData.respBuf, clientData.blockContentLen, 1, file_handle);
|
||||
if (status == 0)
|
||||
{
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
LUAT_DEBUG_PRINT("has recv=%d", count);
|
||||
}
|
||||
} while (result == HTTP_MOREDATA || result == HTTP_CONN);
|
||||
|
||||
LUAT_DEBUG_PRINT("result=%d", result);
|
||||
luat_fs_fclose(file_handle);
|
||||
if (gHttpClient.httpResponseCode < 200 || gHttpClient.httpResponseCode > 404)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("invalid http response code=%d", gHttpClient.httpResponseCode);
|
||||
luat_fs_remove(file);
|
||||
}
|
||||
else if (count == 0 || count != clientData.recvContentLength)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("data not receive complete");
|
||||
luat_fs_remove(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("receive success");
|
||||
}
|
||||
exit:
|
||||
free(clientData.headerBuf);
|
||||
return result;
|
||||
}
|
||||
|
||||
luat_rtos_task_handle https_task_handle;
|
||||
|
||||
static void task_test_https(void *param)
|
||||
{
|
||||
char *recvBuf = malloc(HTTP_RECV_BUF_SIZE);
|
||||
http_queue_t wait_http_request = {0};
|
||||
while (1)
|
||||
{
|
||||
HTTPResult result = HTTP_INTERNAL;
|
||||
if (0 == luat_rtos_queue_recv(http_queue_handle, &wait_http_request, NULL, LUAT_WAIT_FOREVER))
|
||||
{
|
||||
memset(recvBuf, 0x00, HTTP_RECV_BUF_SIZE);
|
||||
memset(&gHttpClient, 0, sizeof(HttpClientContext));
|
||||
gHttpClient.timeout_s = 2;
|
||||
gHttpClient.timeout_r = 20;
|
||||
result = httpConnect(&gHttpClient, wait_http_request.url);
|
||||
if (wait_http_request.type == SPEECH_POST)
|
||||
{
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
result = httpGetData(wait_http_request.url, recvBuf, HTTP_RECV_BUF_SIZE);
|
||||
httpClose(&gHttpClient);
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
cJSON *boss = cJSON_Parse(recvBuf);
|
||||
if (boss != NULL)
|
||||
{
|
||||
cJSON *audio = cJSON_GetObjectItem(boss, "audios");
|
||||
|
||||
int arraysize = cJSON_GetArraySize(audio);
|
||||
http_queue_t http_get_file = {0};
|
||||
for (size_t i = 0; i < arraysize; i++)
|
||||
{
|
||||
cJSON *row = cJSON_GetArrayItem(audio, i);
|
||||
if (row == NULL)
|
||||
break;
|
||||
cJSON *format = cJSON_GetObjectItem(row, "format");
|
||||
cJSON *id = cJSON_GetObjectItem(row, "id");
|
||||
cJSON *url = cJSON_GetObjectItem(row, "url");
|
||||
cJSON *size = cJSON_GetObjectItem(row, "size");
|
||||
http_get_file.filename = malloc(strlen(id->valuestring) + strlen(format->valuestring) + 2);
|
||||
memset(http_get_file.filename, 0x00, strlen(id->valuestring) + strlen(format->valuestring) + 2);
|
||||
snprintf(http_get_file.filename, strlen(id->valuestring) + strlen(format->valuestring) + 2, "%s%s%s", id->valuestring, ".", format->valuestring);
|
||||
if (luat_fs_fexist(http_get_file.filename) == 0) //文件不存在的时候才去下载
|
||||
{
|
||||
http_get_file.type = PUSH_SPEECH;
|
||||
http_get_file.url = malloc(strlen(url->valuestring) + 1);
|
||||
memset(http_get_file.url, 0x00, strlen(url->valuestring) + 1);
|
||||
memcpy(http_get_file.url, url->valuestring, strlen(url->valuestring) + 1);
|
||||
|
||||
if (-1 == luat_rtos_queue_send(http_queue_handle, &http_get_file, 0, 0))
|
||||
{
|
||||
free(http_get_file.filename);
|
||||
LUAT_DEBUG_PRINT("queue send fail");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
free(http_get_file.filename);
|
||||
LUAT_DEBUG_PRINT("file exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
cJSON_Delete(boss);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client connect error");
|
||||
}
|
||||
}
|
||||
else if (wait_http_request.type == PUSH_SPEECH) // 下载下发的语料文本,并保存在文件系统
|
||||
{
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
result = httpGetDataToFile(wait_http_request.url, recvBuf, HTTP_RECV_BUF_SIZE, wait_http_request.filename);
|
||||
httpClose(&gHttpClient);
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client connect error");
|
||||
}
|
||||
LUAT_DEBUG_PRINT("this is http get name and size: %s, %d", wait_http_request.filename, luat_fs_fsize(wait_http_request.filename));
|
||||
}
|
||||
else if (wait_http_request.type == SPEECH_BY_SYNTHESIS) // 下载动态下发的音频文件,并且去播放
|
||||
{
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
result = httpGetDataToFile(wait_http_request.url, recvBuf, HTTP_RECV_BUF_SIZE, wait_http_request.filename);
|
||||
httpClose(&gHttpClient);
|
||||
if (result == HTTP_OK)
|
||||
{
|
||||
audioQueueData data = {0};
|
||||
char *file_name = malloc(strlen(wait_http_request.filename) + 1);
|
||||
memset(file_name, 0x00, strlen(wait_http_request.filename) + 1);
|
||||
memcpy(file_name, wait_http_request.filename, strlen(wait_http_request.filename));
|
||||
data.file.info = (audio_play_info_t *)calloc(1, sizeof(audio_play_info_t));
|
||||
data.file.info[0].path = file_name;
|
||||
data.file.count = 1;
|
||||
data.userParam = (void *)file_name;
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &data, 0, 0))
|
||||
{
|
||||
free(file_name);
|
||||
free(data.file.info);
|
||||
http_get_status = false; // 这只是一个示例,没有对于多次下发动态报文的处理,简单使用一个变量来控制一下
|
||||
LUAT_DEBUG_PRINT("money send fail");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
http_get_status = false; // 这只是一个示例,没有对于多次下发动态报文的处理,简单使用一个变量来控制一下
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("http client connect error");
|
||||
}
|
||||
LUAT_DEBUG_PRINT("this is http get name and size: %s, %d", wait_http_request.filename, luat_fs_fsize(wait_http_request.filename));
|
||||
}
|
||||
|
||||
LUAT_DEBUG_PRINT("this is test result %d", result);
|
||||
|
||||
if (wait_http_request.url != NULL)
|
||||
{
|
||||
free(wait_http_request.url);
|
||||
}
|
||||
if (wait_http_request.filename != NULL)
|
||||
{
|
||||
free(wait_http_request.filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
luat_rtos_task_delete(https_task_handle);
|
||||
}
|
||||
|
||||
void task_demo_https(void)
|
||||
{
|
||||
luat_fs_init();
|
||||
if (-1 == luat_rtos_queue_create(&http_queue_handle, 100, sizeof(http_queue_t)))
|
||||
LUAT_DEBUG_PRINT("http queue create fail");
|
||||
luat_rtos_task_create(&https_task_handle, 32 * 1024, 20, "https", task_test_https, NULL, NULL);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
local TARGET_NAME = "example_link_speech1"
|
||||
local LIB_DIR = "$(buildir)/".. TARGET_NAME .. "/"
|
||||
local LIB_NAME = "lib" .. TARGET_NAME .. ".a "
|
||||
includes(SDK_TOP .. "/thirdparty/audio_decoder")
|
||||
target(TARGET_NAME)
|
||||
set_kind("static")
|
||||
set_targetdir(LIB_DIR)
|
||||
|
||||
--使用第三方的linksdk
|
||||
includes(SDK_TOP .. "/thirdparty/linksdk")
|
||||
add_deps("linksdk")
|
||||
add_deps("audio_decoder")
|
||||
includes(SDK_TOP .. "/thirdparty/httpclient")
|
||||
add_deps("httpclient")
|
||||
|
||||
--加入代码和头文件
|
||||
add_includedirs(SDK_TOP .. "/thirdparty/cJSON",{public = true})
|
||||
add_files(SDK_TOP .. "/thirdparty/cJSON/**.c")
|
||||
add_includedirs("./inc", SDK_TOP .. "/thirdparty/linksdk/core",{public = true})
|
||||
|
||||
add_files("/src/example_mqtt_basic.c",{public = true})
|
||||
add_files("/src/http_task.c",{public = true})
|
||||
add_files("/src/audio_task.c",{public = true})
|
||||
--路径可以随便写,可以加任意路径的代码,下面代码等效上方代码
|
||||
-- add_includedirs(SDK_TOP .. "project/" .. TARGET_NAME .. "/inc",{public = true})
|
||||
-- add_files(SDK_TOP .. "project/" .. TARGET_NAME .. "/src/*.c",{public = true})
|
||||
|
||||
--可以继续增加add_includedirs和add_files
|
||||
--自动链接c
|
||||
|
||||
LIB_USER = LIB_USER .. SDK_TOP .. LIB_DIR .. LIB_NAME .. " "
|
||||
--甚至可以加入自己的库
|
||||
target_end()
|
||||
Reference in New Issue
Block a user