code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: LeLiang Zhang (zhangleliang@baidu.com) // // Description: Define the aes-cbc encrypted networks I/O. // more info: http://agroup.baidu.com/duersh/md/article/475516 #ifndef BAIDU_LIGHTDUER_NET_TRANS_AES_CBC_ENCRYPTED_H #define BAIDU_LIGHTDUER_NET_TRANS_AES_CBC_ENCRYPTED_H #include "lightduer_net_transport.h" #ifdef __cplusplus extern "C" { #endif /* * Connect to the host. * * @Param hdlr, in, the context for the transport * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_connect(duer_trans_ptr trans, const duer_addr_t* addr); /* * Send data. * * @Param hdlr, in, the context for the transport * @Param data, in, the data will be sent * @Param size, in, the data size * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_send(duer_trans_ptr trans, const void* data, duer_size_t size, const duer_addr_t* addr); /* * Set the timeout for receiving data. * * @Param hdlr, in, the context for the transport * @Param timeout, in, the recv data timeout interval * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_set_read_timeout(duer_trans_ptr trans, duer_u32_t timeout); /* * Receive data. * * @Param hdlr, in, the context for the transport * @Param data, out, the data will be read * @Param size, in, the data size * @Param addr, out, the target address infomations * @Return duer_status_t, the operation result * Note, the return size maybe different with the @size, because the BDCAEC used */ DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_recv(duer_trans_ptr trans, void* data, duer_size_t size, duer_addr_t* addr); DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_recv_timeout(duer_trans_ptr trans, void* data, duer_size_t size, duer_u32_t timeout, duer_addr_t* addr); /* * Disconnect from the remote host. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_close(duer_trans_ptr trans); #ifdef __cplusplus } #endif #endif // BAIDU_LIGHTDUER_NET_TRANS_AES_CBC_ENCRYPTED_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_net_trans_aes_cbc_encrypted.h
C
apache-2.0
3,522
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: Define the encrypted networks I/O. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_ENCRYPTED_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_ENCRYPTED_H #include "lightduer_net_transport.h" #ifdef __cplusplus extern "C" { #endif /* * Connect to the host. * * @Param hdlr, in, the context for the transport * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_encrypted_connect(duer_trans_ptr trans, const duer_addr_t* addr); /* * Send data. * * @Param hdlr, in, the context for the transport * @Param data, in, the data will be sent * @Param size, in, the data size * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_encrypted_send(duer_trans_ptr trans, const void* data, duer_size_t size, const duer_addr_t* addr); /* * Set the timeout for receiving data. * * @Param hdlr, in, the context for the transport * @Param timeout, in, the recv data timeout interval * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_encrypted_set_read_timeout(duer_trans_ptr trans, duer_u32_t timeout); /* * Receive data. * * @Param hdlr, in, the context for the transport * @Param data, out, the data will be read * @Param size, in, the data size * @Param addr, out, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_encrypted_recv(duer_trans_ptr trans, void* data, duer_size_t size, duer_addr_t* addr); DUER_INT duer_status_t duer_trans_encrypted_recv_timeout(duer_trans_ptr trans, void* data, duer_size_t size, duer_u32_t timeout, duer_addr_t* addr); /* * Disconnect from the remote host. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_encrypted_close(duer_trans_ptr trans); #ifdef __cplusplus } #endif #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_ENCRYPTED_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_net_trans_encrypted.h
C
apache-2.0
3,381
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: The wrapper for user's transport. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_WRAPPER_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_WRAPPER_H #include "lightduer_network_defs.h" #ifdef __cplusplus extern "C" { #endif #define DUER_READ_FOREVER ((duer_u32_t)-1) typedef void* duer_socket_t; /* * The handler operate transfer. */ typedef void* duer_trans_handler; #define DUER_SIZEOF_TCPHEADER 8 // more info _baidu_ca_coap_tcp_header_s lightduer_coap.c #ifdef NET_TRANS_ENCRYPTED_BY_AES_CBC #define DUER_MIN_BODY_SIZE (16 + 1) // see http://agroup.baidu.com/duersh/md/article/475516 #else #define DUER_MIN_BODY_SIZE (0) #endif #define DUER_MAX_BODY_SIZE 2048 // body longest value typedef struct _duer_trans_s { duer_socket_t ctx; duer_context secure; duer_transevt_func transevt_callback; void* cert; duer_size_t cert_len; duer_u32_t read_timeout; duer_addr_t addr; const void *key_info; duer_bool is_encrypted; #ifdef NET_TRANS_ENCRYPTED_BY_AES_CBC unsigned char received_header[DUER_SIZEOF_TCPHEADER]; // header info duer_size_t received_header_bytes; // received header bytes[0-DUER_SIZEOF_TCPHEADER] unsigned char received_body[DUER_MAX_BODY_SIZE]; // body [0-DUER_MAX_BODY_SIZE] duer_size_t received_body_bytes; // received body bytes #endif } duer_trans_t, *duer_trans_ptr; /* * The network transfer data callbacks. * See the detail in @{link baidu_ca_transport_init} */ typedef duer_socket_t (*duer_soc_create_f)(duer_transevt_func func); typedef duer_status_t (*duer_soc_connect_f)(duer_socket_t sock, const duer_addr_t* addr); typedef duer_status_t (*duer_soc_send_f)(duer_socket_t sock, const void* data, duer_size_t size, const duer_addr_t* addr); typedef duer_status_t (*duer_soc_recv_f)(duer_socket_t sock, void* data, duer_size_t size, duer_addr_t* addr); typedef duer_status_t (*duer_soc_recv_timeout_f)(duer_socket_t sock, void* data, duer_size_t size, duer_u32_t timeout, duer_addr_t* addr); typedef duer_status_t (*duer_soc_close_f)(duer_socket_t sock); typedef duer_status_t (*duer_soc_destroy_f)(duer_socket_t sock); /* * Set network input/ouput function * * @Param f_create, in, the function create socket context * @Param f_conn, in, the function connect socket to host * @Param f_send, in, the function send message * @Param f_recv, in, the function receive message * @Param f_recv_timeout, in, the function receive message with timeout * @Param f_close, in, the function close the socket * @Param f_destroy, in, the function destroy the socket context */ DUER_EXT void baidu_ca_transport_init(duer_soc_create_f f_create, duer_soc_connect_f f_conn, duer_soc_send_f f_send, duer_soc_recv_f f_recv, duer_soc_recv_timeout_f f_recv_timeout, duer_soc_close_f f_close, duer_soc_destroy_f f_destroy); /* * Create the socket context. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_create(duer_trans_ptr trans); /* * Connect to the host. * * @Param hdlr, in, the context for the transport * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_connect(duer_trans_ptr trans, const duer_addr_t* addr); /* * Send data. * * @Param hdlr, in, the context for the transport * @Param data, in, the data will be sent * @Param size, in, the data size * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_send(duer_trans_ptr trans, const void* data, duer_size_t size, const duer_addr_t* addr); /* * Receive data. * * @Param hdlr, in, the context for the transport * @Param data, out, the data will be read * @Param size, in, the data size * @Param addr, out, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_recv(duer_trans_ptr trans, void* data, duer_size_t size, duer_addr_t* addr); /* * Receive data. * * @Param hdlr, in, the context for the transport * @Param data, out, the data will be read * @Param size, in, the data size * @Param timeout, in, the receive timeout * @Param addr, out, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_recv_timeout(duer_trans_ptr trans, void* data, duer_size_t size, duer_u32_t timeout, duer_addr_t* addr); /* * Disconnect from the remote host. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_close(duer_trans_ptr trans); /* * Destroy the socket context. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_wrapper_destroy(duer_trans_ptr trans); #ifdef __cplusplus } #endif #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_WRAPPER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_net_transport.h
C
apache-2.0
7,160
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: Define the adapter for networks. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANSPORT_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANSPORT_H #include "lightduer_net_transport.h" #ifdef __cplusplus extern "C" { #endif /* * Acquire the handler. * * @Param func, in, the callback when transport event notify. * @Param key_info, something used in down layer but generated on up layer * @Return duer_trans_handler, the transport context */ DUER_INT duer_trans_handler duer_trans_acquire(duer_transevt_func func, const void *key_info); /* * Set the public key or certificate. * * @Param hdlr, in, the context for the transport * @Param data, in, the content of the public key or certificate * @Param size, in, the size of the data * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_set_pk(duer_trans_handler hdlr, const void* data, duer_size_t size); /* * clear the public key or certificate, release the buffer * * @Param hdlr, in, the context for the transport */ DUER_INT duer_status_t duer_trans_unset_pk(duer_trans_handler hdlr); /* * Set the timeout for receiving data. * * @Param hdlr, in, the context for the transport * @Param timeout, in, the recv data timeout interval * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_set_read_timeout(duer_trans_handler hdlr, duer_u32_t timeout); /* * Connect to the host. * * @Param hdlr, in, the context for the transport * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_connect(duer_trans_handler hdlr, const duer_addr_t* addr); /* * Send data. * * @Param hdlr, in, the context for the transport * @Param data, in, the data will be sent * @Param size, in, the data size * @Param addr, in, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_send(duer_trans_handler hdlr, const void* data, duer_size_t size, const duer_addr_t* addr); /* * Receive data. * * @Param hdlr, in, the context for the transport * @Param data, out, the data will be read * @Param size, in, the data size * @Param addr, out, the target address infomations * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_recv(duer_trans_handler hdlr, void* data, duer_size_t size, duer_addr_t* addr); DUER_INT duer_status_t duer_trans_recv_timeout(duer_trans_handler hdlr, void* data, duer_size_t size, duer_addr_t* addr); /* * Disconnect from the remote host. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_close(duer_trans_handler hdlr); /* * Release the handler. * * @Param hdlr, in, the context for the transport * @Return duer_status_t, the operation result */ DUER_INT duer_status_t duer_trans_release(duer_trans_handler hdlr); #ifdef __cplusplus } #endif #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANSPORT_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_net_transport_wrapper.h
C
apache-2.0
4,286
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: Network common definitions #ifndef LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_NETWORK_DEFS_H #define LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_NETWORK_DEFS_H #include "lightduer_types.h" /* * protocol type */ enum _duer_transport_protocol_enum { DUER_PROTO_TCP, DUER_PROTO_UDP }; /* * Transport Event */ typedef enum _duer_transport_event_enum { DUER_TEVT_CONNECT_RDY, DUER_TEVT_SEND_RDY, DUER_TEVT_RECV_RDY, DUER_TEVT_CLOSE_RDY, DUER_TEVT_SEND_TIMEOUT, DUER_TEVT_NONE = -1 } duer_transevt_e; /* * The network address structure */ typedef struct _duer_address_s { duer_u8_t type; // the socket type duer_u16_t port; // the host port void* host; // the host address duer_size_t host_size; // the host address length } duer_addr_t; typedef void (*duer_transevt_func)(duer_transevt_e event); #endif // LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_NETWORK_DEFS_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_network_defs.h
C
apache-2.0
1,621
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_notifier.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: Notifier Head file */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_NOTIFIER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_NOTIFIER_H #include <stdint.h> #include "lightduer_mutex.h" #define DUER_NOTIFY_STOP_MASK 0x80 /* Don't call further */ #define DUER_NOTIFY_DONE 0x00 /* Don't care */ #define DUER_NOTIFY_OK 0x01 /* Suits me */ #define DUER_NOTIFY_BAD (DUER_NOTIFY_STOP_MASK | 0x02) /* Bad/Veto action */ #ifdef __cplusplus extern "C" { #endif struct duer_notifier_block; typedef int (*notifier_func)(struct duer_notifier_block *nb, uint32_t val, void *data); typedef struct duer_notifier_block { notifier_func notifier_call; int priority; struct duer_notifier_block *next; } duer_notifier_block_t; typedef struct duer_notifier_block_head { duer_mutex_t lock; struct duer_notifier_block *head; } duer_notifier_block_head_t; /* * Create a notifier to receive the message * * @param void: * * @return pointer: Success: Pointer to duer_notifier_block_t * Failed: NULL */ extern duer_notifier_block_t *duer_notifier_create_notifier(void); /* * Destroy a notifier * * @param pointer to a notifier * * @return void: */ extern void duer_notifier_destroy_notifier(duer_notifier_block_t *notifier); /* * Create a notifier chain to send the message * * @param void: * * @return pointer: Success: Pointer to duer_notifier_block_head_t * Failed: NULL */ extern duer_notifier_block_head_t *duer_notifier_create_notifier_chain(void); /* * Destroy a notifier chain * * @param pointer to duer_notifier_block_head_t * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_notifier_destroy_notifier_chain(duer_notifier_block_head_t *head); /* * Register a notifier to a notifier chain * * @param pointer to duer_notifier_block_head_t * @param pointer to duer_notifier_block_t * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_notifier_register( duer_notifier_block_head_t *head, duer_notifier_block_t *notifier); /* * Unregister a notifier from a notifier chain * * @param pointer to duer_notifier_block_head_t * pointer to duer_notifier_block_t * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_notifier_unregister( duer_notifier_block_head_t *head, duer_notifier_block_t *notifier); /** * Send message to single notifier * * @param notifier: single notifier * val: the input data * data: the output data * * @return int: DUER_NOTIFY_ value * */ int duer_notifier_call(duer_notifier_block_t *notifier, uint32_t val, void *data); /* * Send message to the notifier * * @param pointer to duer_notifier_block_head_t * val: the message * data: which you can receive the message from the notifier * * @return int: Success: DUER_NOTIFY_OK || DUER_NOTIFY_DONE * Failed: DUER_NOTIFY_BAD */ extern int duer_notifier_call_chain( duer_notifier_block_head_t *head, uint32_t val, void *data); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_NOTIFIER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_notifier.h
C
apache-2.0
3,975
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: Adapter between nsdl and Baidu CA CoAP. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_NSDL_ADAPTER_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_NSDL_ADAPTER_H #include "ns_types.h" #include "sn_nsdl.h" #include "sn_coap_header.h" #include "lightduer_lib.h" #include "lightduer_types.h" #include "lightduer_coap.h" #ifndef MBED_CLIENT_C_VERSION #define MBED_CLIENT_C_VERSION (30001) // 3.0.1 #endif /* * Set the nsdl address from ca * * @Param target, in, the target will be evaluated * @Param source, the CA address */ DUER_INT void duer_nsdl_address_set(sn_nsdl_addr_s* target, const duer_addr_t* source); /* * Set the nsdl CoAP header from ca * * @Param target, in, the target will be evaluated * @Param opt, in, the CoAP options * @Param source, the CA message header */ DUER_INT void duer_nsdl_header_set(sn_coap_hdr_s* target, sn_coap_options_list_s* opt, const duer_msg_t* source); /* * Set the CoAP address from nsdl * * @Param target, in, the target will be evaluated * @Param source, the nsdl address */ DUER_INT void duer_coap_address_set(duer_addr_t* target, const sn_nsdl_addr_s* source); /* * Set the CoAP header from nsdl * * @Param target, in, the target will be evaluated * @Param source, the nsdl message header */ DUER_INT void duer_coap_header_set(duer_msg_t* target, const sn_coap_hdr_s* source); #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_NSDL_ADAPTER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_nsdl_adapter.h
C
apache-2.0
2,307
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_decompression.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA decompression abstraction layer header file */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DECOMPRESSION_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DECOMPRESSION_H #include <stdint.h> #include "lightduer_mutex.h" #ifdef __cplusplus extern "C" { #endif #define NAME_LENGTH (20) #define ERR_MSG_LEN (60) typedef enum _duer_ota_decompression_type_s { ZLIB = 0, DECOMPRESSION_MAX = 1, } duer_ota_decompress_type_t; typedef int (*DECOMPRESS_DATA_HANDLER_FUN)(void *custom_data, char const *buf, size_t len); typedef struct _duer_ota_unpacker_decom_s { char name[NAME_LENGTH + 1]; duer_mutex_t lock; void *pdata; char err_msg[ERR_MSG_LEN + 1]; int (*init)(struct _duer_ota_unpacker_decom_s *decompression, void **pdata); int (*unzip)( struct _duer_ota_unpacker_decom_s *decompression, void *pdata, char const *compressed_data, size_t data_size, void *custom_data, DECOMPRESS_DATA_HANDLER_FUN data_handler); int (*recovery)(struct _duer_ota_unpacker_decom_s *decompression, void *pdata); int (*sync)(struct _duer_ota_unpacker_decom_s *decompression, void *pdata); int (*unint)(struct _duer_ota_unpacker_decom_s *decompression, void *pdata); } duer_ota_decompression_t; /* * Initialize decompression module * * @param void: * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_init_decompression(void); /* * Create a decompression object * * @param name: the name of decompression * * @return: Success: duer_ota_decompression_t * * Failed: NULL */ extern duer_ota_decompression_t *duer_ota_unpacker_create_decompression(char const *name); /* * Get decompression object * * @param name: the name of decompression * * @return: Success: duer_ota_decompression_t * * Failed: NULL */ extern duer_ota_decompression_t *duer_ota_unpacker_get_decompression( duer_ota_decompress_type_t type); /* * Destroy decompression object * * @param void: * * @return: Success: duer_ota_decompression_t * * Failed: NULL */ extern int duer_ota_unpacker_destroy_decompression(duer_ota_decompression_t *decompression); /* * Get decompression name * * @param decompression: decompression object * * @return: Success: char const * * Failed: NULL */ extern const char *duer_ota_unpacker_get_decompression_name( duer_ota_decompression_t const *decompression); /* * Register a decompression library * * @param decompression: decompression object * type: decompression library type * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_register_decompression( duer_ota_decompression_t *decompression, duer_ota_decompress_type_t type); /* * Unregister a decompression library * * @param type: decompression library type * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_unregister_decompression(duer_ota_decompress_type_t type); /* * Initialize decompression * * @param decompression: decompression object * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_init_decompression(duer_ota_decompression_t *decompression); /* * Unzip data * * @param decompression: decompression object * compressed_data: compressed data * compressed_data_size: size of corpressed data * unpacker: Unpacker object * data_handler: A function which receive the decompressed data * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_unzip( duer_ota_decompression_t *decompression, char const *compressed_data, size_t compressed_data_size, void *custom_data, DECOMPRESS_DATA_HANDLER_FUN data_handler); /* * Uninitialize decompression * * @param decompression: decompression object * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_uninit_decompression(duer_ota_decompression_t *decompression); /* * Try to repair data * * @param decompression: decompression object * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_recovery_decompression(duer_ota_decompression_t *decompression); /* * Synchronous data * * @param decompression: decompression object * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_sync_decompression(duer_ota_decompression_t *decompression); /* * Report error message * * @param decompression: OTA Decompression * err_msg: error message * err_code: error code * * @return void: */ extern void duer_ota_decompression_report_err( duer_ota_decompression_t *decompression, char const *err_msg, int err_code); /* * Get decompression error message * * @param decompression: decompression object * * @return: Success: error message * Failed: NULL */ extern char const *duer_ota_decompression_get_err_msg( duer_ota_decompression_t const *decompression); /* * Check wheater the error message exists * * @param decompression: decompression object * * @return: Success: 1 * Failed: -1 */ extern int duer_ota_decompression_check_err_msg(duer_ota_decompression_t *decompression); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DECOMPRESSION_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_decompression.h
C
apache-2.0
6,213
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_ota_downloader.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA Downloader Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H #include "lightduer_mutex.h" #include "lightduer_http_client.h" #ifndef URL_LEN #define URL_LEN 301 #endif #define ERR_MSG_LEN (60) typedef enum _duer_downloader_Protocol { HTTP = 0, COAP = 1, LOCAL = 2, MAX_PROTOCOL_COUNT = 3, } duer_downloader_protocol; typedef struct _duer_ota_downloader_s { duer_downloader_protocol dp; void *private_data; // Set the private data you want to pass duer_mutex_t lock; char url[URL_LEN + 1]; struct _duer_ota_downloader_ops_s *ops; char err_msg[ERR_MSG_LEN]; } duer_ota_downloader_t ; typedef int (*data_handler)( void* private_data, const char *buf, size_t len); /* * We need to consider unblock & timeout situation (TBD) * connect_server callback function requires you that * connect to server and receive the data immediately * I think the abstract is not very good. Refactor (TBD) */ typedef struct _duer_ota_downloader_ops_s { int (*init)(duer_ota_downloader_t *downloader); int (*register_data_notify)(duer_ota_downloader_t *downloader, data_handler handler, void *private_data); int (*connect_server)(duer_ota_downloader_t *downloader, const char *url); int (*disconnect_server)(duer_ota_downloader_t *downloader); int (*destroy)(duer_ota_downloader_t *downloader); } duer_ota_downloader_ops_t; /* * Initialise OTA Downloader Module * * @param void: * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_init_ota_downloader(void); /* * Uninitialise OTA Downloader Module * * @param void: * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_uninit_ota_downloader(void); /* * Create OTA Downloader * * @param void: * * @return duer_ota_downloader_t *: Success: duer_ota_downloader_t * * Failed: NULL */ extern duer_ota_downloader_t *duer_ota_downloader_create_downloader(void); /* * Initialise OTA Downloader * * @param downloader: OTA Downloader object * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_init_downloader(duer_ota_downloader_t *downloader); /* * Register a OTA Downloader to OTA Downloader module * * @param downloader: OTA Downloader object * * @param dp: Download protocol which the downloader support * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_register_downloader( duer_ota_downloader_t *downloader, duer_downloader_protocol dp); /* * Unregister a OTA Downloader from OTA Downloader module * * @param dp: Download protocol * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_unregister_downloader(duer_downloader_protocol dp); /* * Get a OTA Downloader from OTA Downloader module * * @param dp: Download protocol which you want * * @return duer_ota_downloader_t *: Success: duer_ota_downloader_t * * Failed: NULL */ extern duer_ota_downloader_t *duer_ota_downloader_get_downloader(duer_downloader_protocol dp); /* * Destroy a OTA Downloader, free the memory and unregister it from OTA Downloader module * * @param downloader: OTA downloader object * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_destroy_downloader(duer_ota_downloader_t *downloader); /* * Register the downloader operations to a Downloader object * If you want to support a new downloader protocol, you need to implement duer_ota_downloader_ops * * @param downloader: OTA Downloader object * * @param downloader_ops: OTA Downloader operations * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_register_downloader_ops( duer_ota_downloader_t *downloader, duer_ota_downloader_ops_t *downloader_ops); /* * Connect to server to get the data * Note: Once you call it, the OTADownloader will download the data from server and pass it to you * * @param downloader: OTA Downloader object * * @param url: URL * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_connect_server(duer_ota_downloader_t *downloader, const char *url); /* * Disconnect server * Close the socket * * @param downloader: OTA Downloader object * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_disconnect_server(duer_ota_downloader_t *downloader); /* * Register a callback functions "hancler" to receive the data which get from URL * * @param downloader: OTA Downloader object * * @param handler: Callback function to receive the data which get form URL * * @param private: param which you want to pass to handler * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_register_data_notify( duer_ota_downloader_t *downloader, data_handler handler, void *private_data); /* * Set the private data you want pass to the OTA downloader OPS * * @param downloader: OTA Downloader object * * @param private_data: data which you want to pass * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_downloader_set_private_data( duer_ota_downloader_t *downloader, void *private_data); /* * Get the private data * * @param downloader: OTA Downloader object * * @return void *: Success: Other * Failed: NULL */ extern void *duer_ota_downloader_get_private_data(duer_ota_downloader_t *downloader); /* * Report error message * * @param downloader: OTA Downloader * err_msg: error message * err_code: error code * * @return void: */ extern void duer_ota_downloader_report_err( duer_ota_downloader_t *downloader, char const *err_msg, int err_code); /* * Get error message * * @param downloader: downloader object * * @return: Success: error message * Failed: NULL */ extern char const *duer_ota_downloader_get_err_msg(duer_ota_downloader_t const *downloader); /* * Check wheater the error message exists * * @param downloader: downloader object * * @return: Success: 1 * Failed: -1 */ extern int duer_ota_downloader_check_err_msg(duer_ota_downloader_t *downloader); #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_downloader.h
C
apache-2.0
7,326
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_http_downloader.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA HTTP Downloader Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H #include "lightduer_ota_downloader.h" /* * Initialise HTTP Downloader * * @param void: * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_init_http_downloader(void); #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_http_downloader.h
C
apache-2.0
1,165
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_installer.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA installer Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_INSTALLER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_INSTALLER_H #include "lightduer_mutex.h" #include "lightduer_ota_package_info.h" #ifdef __cplusplus extern "C" { #endif #define ERR_MSG_LEN (60) #define INSTALLER_NAME_LEN 20 typedef struct _duer_ota_installer_s { // The name of OTA installer char name[INSTALLER_NAME_LEN + 1]; // The customer data void *custom_data; // Lock the installer duer_mutex_t lock; // Last Error message char err_msg[ERR_MSG_LEN + 1]; /* * You can get module information */ int (*get_module_info)( struct _duer_ota_installer_s *installer, void **custom_data, duer_ota_module_info_t const *module_info); /* * Now we notify you, we are ready to start OTA * You can do someting before receive data * Such as open file, Initialise Flash, Initialise DMA, check chip ... */ int (*notify_ota_begin)(struct _duer_ota_installer_s *installer, void *custom_data); /* * You can free the resource which you use in notify_ota_begin * Set the boot partition ... */ int (*notify_ota_end)(struct _duer_ota_installer_s *installer, void *custom_data); /* * You can get the file which you upload to Duer Cloud */ int (*get_module_data)( struct _duer_ota_installer_s *installer, void *custom_data, unsigned int offset, unsigned char const *data, size_t size); /* * You can verify the data you received */ int (*verify_module_data)(struct _duer_ota_installer_s *installer, void *custom_data); /* * You can do someting before update the firmware * Such as Erase Flash ... */ int (*update_image_begin)(struct _duer_ota_installer_s *installer, void *custom_data); /* * Update the new firmware */ int (*update_image)(struct _duer_ota_installer_s *installer, void *custom_data); /* * You can check the new firmware, and select the boot partition */ int (*verify_image)(struct _duer_ota_installer_s *installer, void *custom_data); /* * Cancel the OTA update, free the resources and revert */ int (*cancel_ota_update)(struct _duer_ota_installer_s *installer, void *custom_data); struct _duer_ota_installer_s *next; } duer_ota_installer_t; /* * Initialise OTA installer module * * @param void * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_init_installer(void); /* * Get the customr data * * @param installer: OTA installer * * @return void *: Success: void * * Failed: NULL */ extern void *duer_ota_installer_get_custom_data(duer_ota_installer_t const *installer); /* * Create a OTA installer * * @param name: The name of installer * * @return int: Success: duer_ota_installer_t * * Failed: NULL */ extern duer_ota_installer_t *duer_ota_installer_create_installer(char const *name); /* * Destory a OTA installer * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_destroy_installer(duer_ota_installer_t *installer); /* * Register a OTA installer * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_register_installer(duer_ota_installer_t *installer); /* * Unregister a OTA installer * * @param name : The name of the OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_unregister_installer(char const *name); /* * Get the OTA installer by name * * @param name : The name of the OTA installer * * @return int: Success: duer_ota_installer_t * * Failed: NULL */ extern duer_ota_installer_t *duer_ota_installer_get_installer(char const *name); /* * Notify OTA installer data begin * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_notify_ota_begin(duer_ota_installer_t *installer); /* * Notify OTA installer the module information * * @param installer: OTA installer * @param module_info: module information * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_send_module_info( duer_ota_installer_t *installer, duer_ota_module_info_t const *module_info); /* * Send OTA installer the module data * * @param installer: OTA installer * @param offset: The offset of the image * @param data: OTA image data * @param size The size of OTA image data * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_send_module_data( duer_ota_installer_t *installer, unsigned int offset, unsigned char const *data, size_t size); /* * Verify the module data * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_verify_module_data(duer_ota_installer_t *installer); /* * Notify OTA installer bigin to update OTA image * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_update_image_begin(duer_ota_installer_t *installer); /* * Notify OTA installer to update OTA image * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_update_image(duer_ota_installer_t *installer); /* * Notify OTA installer to verify OTA image * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_verify_image(duer_ota_installer_t *installer); /* * Notify OTA installer to end the OTA update * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_notify_ota_end(duer_ota_installer_t *installer); /* * Cancel the OTA update * * @param installer: OTA installer * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_installer_cancel_ota_update(duer_ota_installer_t *installer); /* * Report error message * * @param installer: OTA installer * err_msg: error message * err_code: error code * * @return void: */ extern void duer_ota_installer_report_err( duer_ota_installer_t *installer, char const *err_msg, int err_code); /* * Get error message * * @param installer: OTA installer * * @return : Success: error message * Failed: NULL */ extern const char *duer_ota_installer_get_err_msg(duer_ota_installer_t const *installer); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_INSTALLER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_installer.h
C
apache-2.0
7,760
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_local_downloader.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA local Downloader Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_LOCAL_DOWNLOADER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_LOCAL_DOWNLOADER_H #include "lightduer_ota_downloader.h" /* * Initialise local Downloader * * @param void: * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_init_local_downloader(void); #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_local_downloader.h
C
apache-2.0
1,171
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_notifier.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA Notifier Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_NOTIFIER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_NOTIFIER_H #include "lightduer_dev_info.h" #include "lightduer_ota_updater.h" #ifdef __cplusplus extern "C" { #endif #define NAME_LEN 24 #define BATCH_LEN 4 #define OTA_PROTOCOL_VERSION "1.0" typedef enum _duer_ota_state { OTA_IDLE = 0, OTA_DOWNLOADING, OTA_DOWNLOADED, OTA_INSTALLING, OTA_INSTALLED } duer_ota_state; typedef enum _duer_ota_event { OTA_EVENT_BEGIN = 0, OTA_EVENT_CONNECT_FAIL = 1, OTA_EVENT_CONNECTION_LOST = 2, OTA_EVENT_TIMEOUT = 3, OTA_EVENT_DOWNLOADING = 4, OTA_EVENT_DOWNLOAD_COMPLETE = 5, OTA_EVENT_DOWNLOAD_FAIL = 6, OTA_EVENT_INSTALLING = 7, OTA_EVENT_IMAGE_INVALID = 8, OTA_EVENT_WRITE_ERROR = 9, OTA_EVENT_INSTALLED = 10, OTA_EVENT_DELETED = 11, OTA_EVENT_REJECT = 12, OTA_EVENT_UNPACK_FAIL = 30, OTA_EVENT_CHECKSUM_FAIL = 31, OTA_EVENT_ERASE_FLASH_FAIL = 32, OTA_EVENT_BOOTLOADER_WRITE_ERROR = 33, OTA_EVENT_BOOTLOADER_CHECKSUM_FAIL = 34, OTA_EVENT_BOOTLOADER_IMAGE_INVALID = 35, OTA_EVENT_BOOTLOADER_VERSION_INVALID = 36, OTA_EVENT_BOOTLOADER_UNPACK_FAIL = 37, OTA_EVENT_BOOTLOADER_ERASE_FLASH_FAIL = 38, OTA_EVENT_MAX, } duer_ota_event; typedef struct _duer_os_info_s { char os_name[NAME_LEN + 1]; char developer[NAME_LEN + 1]; char os_version[FIRMWARE_VERSION_LEN + 1]; char staged_version[NAME_LEN + 1]; } duer_os_info_t; typedef struct _duer_package_info_s { char product[NAME_LEN + 1]; char batch[BATCH_LEN + 1]; duer_os_info_t os_info; char duer_os_sdk_version[FIRMWARE_VERSION_LEN + 1]; char ota_package_version[FIRMWARE_VERSION_LEN + 1]; } duer_package_info_t; typedef struct _duer_package_info_ops_s { int (*get_package_info)(duer_package_info_t *info); } duer_package_info_ops_t; /* * Notify the package information to the Duer Cloud * * @param void: * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_notify_package_info(void); /* * Get the package information that about the current system * * @param info: Pointer to Package information * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_get_package_info(duer_package_info_t *info); /* * Register get package information OPS * * @param ops: duer_package_info_ops_t * which you need to implement * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_register_package_info_ops(duer_package_info_ops_t const *ops); /* * Notifier the OTA state to Duer Cloud * * @param updater: OTA updater object * * @param state: OTA state * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_notifier_state(duer_ota_updater_t const *updater, duer_ota_state state); /* * Notifier OTA event to Duer Cloud * * @param updater: OTA updater object * * @param event: OTA event * * @param progress: It can pass information about installing progress information * or downloading progress information * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_notifier_event( duer_ota_updater_t const *updater, duer_ota_event event, double progress, char const *desc); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_NOTIFIER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_notifier.h
C
apache-2.0
4,474
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_package_info.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA Package Information Head File */ #include <stddef.h> #include <stdint.h> #include <stdbool.h> #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_PACKAGE_INFO_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_PACKAGE_INFO_H #define KEY_LEN (128) #define PACKAGE_NAME_LENGTH (15) #define META_VERSION_LENGTH (3) #define PACKAGE_VERSION_LENGTH (15) #define INSTALL_PATH_LENGTH (255) #define MODULE_NAME_LENGTH (15) #define MODULE_VERSION_LENGTH (15) #define MODULE_SIGNATURE_LENGTH (33) #ifdef __cplusplus extern "C" { #endif typedef enum _duer_ota_package_type_s { PACKAGE_TYPE_OS, // Operation system PACKAGE_TYPE_APP, // Application PACKAGE_TYPE_FIRMWARE, // Chip firmware PACKAGE_TYPE_MIXED, // MIXED } duer_ota_package_type_t; typedef enum _duer_ota_module_type_s { MODULE_TYPE_BIN, MODULE_TYPE_IMG, MODULE_TYPE_JSON, MODULE_TYPE_SO, } duer_ota_module_type_t; // package header typedef struct _duer_ota_package_header_s { // 'mbed' verify tag uint8_t tag[4]; // package header size uint32_t package_header_size; // package signature size: 1024 bits uint32_t package_signature_size; // package signature: include meta.json and all modules unsigned char package_signature[KEY_LEN]; // meta.json signature size: 1024 bits uint32_t meta_signature_size; // meta.json signature unsigned char meta_signature[KEY_LEN]; // meta.json size: used for decompress meta.json from package body uint32_t meta_size; // size of meta.json + all modules (before compress) uint32_t ori_package_size; } duer_ota_package_header_t; typedef struct _duer_ota_package_basic_info_s { uint8_t package_name[PACKAGE_NAME_LENGTH + 1]; uint8_t meta_version[META_VERSION_LENGTH + 1]; duer_ota_package_type_t package_type; // Enforce the update bool update; } duer_ota_package_basic_info_t; typedef struct _duer_ota_module_info_s { uint8_t module_name[MODULE_NAME_LENGTH + 1]; uint32_t module_size; duer_ota_module_type_t module_type; // Enforce the update bool update; uint8_t module_signature[MODULE_SIGNATURE_LENGTH + 1]; uint8_t module_version[MODULE_VERSION_LENGTH + 1]; uint8_t module_support_hardware_version[MODULE_VERSION_LENGTH + 1]; uint8_t module_install_path[INSTALL_PATH_LENGTH + 1]; } duer_ota_module_info_t; typedef struct _duer_ota_package_install_info_s { uint8_t package_install_path[INSTALL_PATH_LENGTH + 1]; uint8_t module_count; duer_ota_module_info_t *module_list; } duer_ota_package_install_info_t; typedef struct _duer_ota_update_info_s { uint8_t package_version[PACKAGE_VERSION_LENGTH + 1]; } duer_ota_update_info_t; #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_PACKAGE_INFO_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_package_info.h
C
apache-2.0
3,650
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_unpacker.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA unpacker Head File */ #include <stddef.h> #include <stdint.h> #include <stdbool.h> #include "lightduer_ota_verifier.h" #include "lightduer_ota_installer.h" #include "lightduer_ota_package_info.h" #include "lightduer_ota_decompression.h" #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UNPACKER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UNPACKER_H #define ERR_MSG_LEN (60) #ifdef __cplusplus extern "C" { #endif typedef enum _duer_ota_unpack_mode_s { SAVE_PACKAGE_HEADER = 1, PARSE_PACKAGE_HEADER = 2, SAVE_META_DATA = 3, UNZIP_DATA = 4, UNZIP_DATA_DONE = 5, PARSE_META_DATA = 6, GET_OTA_INSTALLER = 7, UNZIP_MODLE_INFO = 8, UNZIP_MODLE_DATA = 9, NOTIFY_OTA_BEGIN = 10, NOTIFY_OTA_END = 11, DISTRIBUTE_MODULE_INFO = 12, DISTRIBUTE_MODULE_DATA = 13, VERIFY_MODULE_DATA = 14, PUBLIC_KEY_VERIFICATION = 15, UPDATE_IMAGE_BEGIN = 16, UPDATE_IMAGE = 17, VERIFY_IMAGE = 18, OTA_UPDATE_DONE = 19, CANCEL_OTA_UPDATE = 20, UNPACKER_MODE_MAX = 21, } duer_ota_unpacker_mode_t; typedef struct _duer_ota_unpack_state_s { duer_ota_unpacker_mode_t mode; size_t received_data_size; size_t processed_data_size; size_t decompress_data_size; size_t used_decompress_data_offset; size_t used_data_offset; size_t meta_data_offset; size_t module_data_offset; size_t package_header_data_offset; } duer_ota_unpack_state_t; typedef struct _duer_ota_unpacker_s { // Lock the unpacker duer_mutex_t lock; duer_ota_verifier_t *verifier; duer_ota_unpack_state_t state; duer_ota_package_header_t package_header; duer_ota_package_basic_info_t basic_info; duer_ota_package_install_info_t install_info; void *meta_data; duer_ota_decompression_t *decompression; duer_ota_installer_t *installer; char err_msg[ERR_MSG_LEN]; } duer_ota_unpacker_t; /* * Create a OTA unpacker * * @param void: * * @return: Success: duer_ota_unpacker_t * * Failed: NULL */ extern duer_ota_unpacker_t *duer_ota_unpacker_create_unpacker(void); /* * Destroy a OTA unpacker * * @param unpacker: OTA unpacker * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_destroy_unpacker(duer_ota_unpacker_t *unpacker); /* * Initialize a OTA unpacker * * @param unpacker: OTA unpacker * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_init_unpacker(duer_ota_unpacker_t *unpacker); /* * Get the basic information of the package * * @param unpacker: OTA unpacker * * @return : Success:duer_ota_package_basic_info_t * * Failed: NULL */ extern duer_ota_package_basic_info_t const *duer_ota_unpacker_get_package_basic_info( duer_ota_unpacker_t const *unpacker); /* * Get the header information of the package * * @param unpacker: OTA unpacker * * @return : Success: duer_ota_package_header_t * * Failed: NULL */ extern duer_ota_package_header_t const *duer_ota_unpacker_get_package_header_info( duer_ota_unpacker_t const *unpacker); /* * Unpacke a OTA upadate package * * @param unpacker: OTA unpacker * data: data of the package * size: data size * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_unpack_package( duer_ota_unpacker_t *unpacker, uint8_t const *data, size_t size); /* * Set the unpacker mode * * @param unpacker: OTA unpacker * mode: Unpacker mode * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_unpacker_set_unpacker_mode( duer_ota_unpacker_t *unpacker, duer_ota_unpacker_mode_t mode); /* * Get the unpacker mode * * @param unpacker: OTA unpacker * * @return mode: Unpacker mode */ extern duer_ota_unpacker_mode_t duer_ota_unpacker_get_unpacker_mode( duer_ota_unpacker_t const *unpacker); /* * Get the unpacker error message * * @param unpacker: OTA unpacker * * @return char const *: Success: error message * Failed: NULL */ extern char const *duer_ota_unpacker_get_err_msg(duer_ota_unpacker_t const *unpacker); /* * Check wheater the error message exists * * @param unpacker: unpacker object * * @return: Success: 1 * Failed: -1 */ extern int duer_ota_unpacker_check_err_msg(duer_ota_unpacker_t *unpacker); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UNPACKER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_unpacker.h
C
apache-2.0
5,388
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_updater.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA Updater Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UPDATER_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UPDATER_H #include "lightduer_ota_unpacker.h" #include "lightduer_ota_downloader.h" #include "mbedtls/md5.h" #define TRANSACTION_LEN (65) #define VERSION_LEN (16) #define SIGNATURE_LEN (129) #define MD5_LEN (16) #ifdef __cplusplus extern "C" { #endif typedef enum _duer_ota_switch { ENABLE_OTA = 1, DISABLE_OTA = -1, } duer_ota_switch; typedef enum _duer_ota_reboot { ENABLE_REBOOT = 1, DISABLE_REBOOT = -1, } duer_ota_reboot; typedef struct _duer_ota_update_command_s { char transaction[TRANSACTION_LEN + 1]; char version[VERSION_LEN + 1]; char old_version[VERSION_LEN + 1]; char url[URL_LEN + 1]; char signature[SIGNATURE_LEN + 1]; unsigned int size; } duer_ota_update_command_t; typedef struct _duer_ota_updater_s { int id; duer_ota_unpacker_t *unpacker; duer_ota_downloader_t *downloader; duer_ota_update_command_t *update_cmd; int received_data_size; int progress; mbedtls_md5_context md5_ctx; } duer_ota_updater_t; typedef struct _duer_ota_init_ops_s { /* * Call duer_ota_installer_register_installer() to register the * OTA installer */ int (*register_installer)(void); /* * Unregister OTA installer */ int (*unregister_installer)(void); /* * Implement reboot system function */ int (*reboot)(void *arg); } duer_ota_init_ops_t; typedef void (*OTA_Updater)(int arg, void *update_cmd); /* * Call it to initialize the OTA module * * @param ops: You need to implement the structure OTAInitOps * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_init_ota(duer_ota_init_ops_t const *ops); /* * User Call it to enable/disable OTA * * @param ops: You need to implement the structure OTAInitOps * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_set_switch(duer_ota_switch flag); /* * Get the OTA status * * @param void: * * @return int: Enable 1 * Disable -1 */ extern int duer_ota_get_switch(void); /* * Set the OTA reboot status * * @param void: * * @return int: Enable 1 * Disable -1 */ extern int duer_ota_set_reboot(duer_ota_reboot reboot); /* * get the OTA reboot status * * @param void: * * @return int: Enable 1 * Disable -1 */ extern int duer_ota_get_reboot(void); /* * get the OTA update command * * @param void: * * @return Success: duer_ota_update_command_t const * * Failed: NULL */ extern duer_ota_update_command_t const *duer_ota_get_update_cmd(void); /* * Create a OTA updater to update the firmware * * @param update_cmd: Update command * * @return int: Success: DUER_OK * Failed: Other */ extern int duer_ota_update_firmware(duer_ota_update_command_t const *update_cmd); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UPDATER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_updater.h
C
apache-2.0
3,738
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_verification.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA Verification Head File */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_VERIFICATION_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_VERIFICATION_H #include "mbedtls/md5.h" #include "mbedtls/rsa.h" #include "mbedtls/sha1.h" #include "lightduer_ota_package_info.h" #define ERR_MSG_LEN (60) #ifdef __cplusplus extern "C" { #endif typedef struct _duer_ota_verifier_s { mbedtls_rsa_context rsa; mbedtls_sha1_context sha1; char err_msg[ERR_MSG_LEN + 1]; } duer_ota_verifier_t; /* * Create a OTA verifier * * @param void: * * @return: Success: duer_ota_verifier_t * * Failed: NULL */ extern duer_ota_verifier_t *duer_ota_verification_create_verifier(void); /* * Initialize a OTA verifier * * @param void: * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_verification_init_verifier(duer_ota_verifier_t *verifier); /* * Destroy a OTA verifier * * @param verifier: duer_ota_verifier_t * * * @return void: */ extern void duer_ota_verification_destroy_verifier(duer_ota_verifier_t *verifier); /* * Verify data * * @param verifier: duer_ota_verifier_t * * data: uint8_t * * size: size_t * * @return void: */ extern void duer_ota_verification_update( duer_ota_verifier_t *verifier, uint8_t const *data, size_t size); /* * Verify the OTA package * * @param verify: duer_ota_verifier_t * * header: Heare information * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_verification_verify(duer_ota_verifier_t *verifier, duer_ota_package_header_t const *header); /* * Get verofoer error message * * @param verify: duer_ota_verifier_t * * * @return: Success: error message * Failed: NULL */ extern char const *duer_ota_verification_get_err_msg(duer_ota_verifier_t const *verifier); /* * Check wheater the error message exists * * @param verifier: verifier object * * @return: Success: 1 * Failed: -1 */ extern int duer_ota_verification_check_err_msg(duer_ota_verifier_t *verifier); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_VERIFICATION_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_verifier.h
C
apache-2.0
2,901
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_ota_zlib.h * Auth: Zhong Shuai (zhongshuai@baidu.com) * Desc: OTA Zlib header file */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_ZLIB_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_ZLIB_H #ifdef __cplusplus extern "C" { #endif /* * Initialize Zliblite * * @param void: * * @return: Success: DUER_OK * Failed: Other */ extern int duer_ota_init_zlib(void); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_ZLIB_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_ota_zlib.h
C
apache-2.0
1,120
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_pm_dev.h * Auth: Chang Li (changli@baidu.com) * Desc: PM Device Head file */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_PM_DEV_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_PM_DEV_H #include <stdint.h> #include "lightduer_mutex.h" #define DEV_NAME_LEN 32 #ifdef __cplusplus extern "C" { #endif typedef enum duer_pm_dev_state { PM_ST_NORMAL, PM_ST_IDLE, PM_ST_SUSPEND, PM_ST_SUSPENDING, PM_ST_RESUME, PM_ST_RESUMEING, PM_ST_POWER_OFF, MAX_PM_STATE, } duer_pm_dev_state_t; typedef enum duer_pm_dev_type { PM_DEV_CODEC, PM_DEV_LED, PM_DEV_PA, PM_DEV_WIFI, PM_DEV_DSP, MAX_PM_DEV_TYPE, } duer_pm_dev_type_t; typedef struct duer_pm_device { char name[DEV_NAME_LEN + 1]; duer_mutex_t lock; duer_pm_dev_type_t dev_type; duer_pm_dev_state_t pm_state; uint16_t user_cnt; uint32_t suspend_target_time; uint32_t power_off_target_time; uint32_t normal_timestamp; uint32_t prepare_suspend_timestamp; uint32_t suspend_timestamp; uint32_t prepare_poweroff_timestamp; uint32_t poweroff_timestamp; uint32_t pm_state_duration; int (*enter_pm_state)(duer_pm_dev_state_t state); struct duer_pm_device *next; } duer_pm_device_t; typedef struct duer_pm_device_list { duer_mutex_t list_lock; duer_pm_device_t *list_head; } duer_pm_device_list_t; typedef struct duer_init_pm_ops { int (*register_pm_dev_func)(void); int (*start_policy)(void); } duer_init_pm_ops_t; typedef int (*treatment_dev_func)(duer_pm_device_t *dev); /* * Initialize Power Managerment module * * @param pointer to duer_init_pm_ops_t * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_init_pm(const duer_init_pm_ops_t *init_ops); /* * Initialize devices for power managerment * * @param none * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ int duer_pm_init_devices(void); /** * Get device list * * @param none * * @return duer_pm_device_list_t* * */ const duer_pm_device_list_t *duer_pm_get_device_list(void); /* * Alloc a device * * @param void * * @return pointer: Success: duer_pm_device_t * * Failed: NULL */ extern duer_pm_device_t *duer_pm_alloc_dev(void); /* * Destroy a device object * * @param pointer to device * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_destroy_dev(duer_pm_device_t *dev); /* * Register a device to PM * * @param dev: duer_pm_device_t * * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_register_dev(duer_pm_device_t *dev); /* * Get device Object by the name of device * * @param name: const char * * * @return pointer: Success: duer_pm_device_t * * Failed: NULL */ extern duer_pm_device_t *duer_pm_get_dev_obj_by_name(const char *name); /* * Get device Object by the device type * * @param name: const char * * * @return pointer: Success: duer_pm_device_t * * Failed: NULL */ extern duer_pm_device_t *duer_pm_get_dev_obj_by_type(duer_pm_dev_type_t type); /* * Unregister a device from PM and destroy it * * @param dev: duer_pm_device_t * * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_unregister_dev(duer_pm_device_t *dev); /* * Increase the reference count of the device * * @param dev: duer_pm_device_t * * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_acquire_dev(duer_pm_device_t *dev); /* * Reduce the reference count of the device * * @param dev: duer_pm_device_t * * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_release_dev(duer_pm_device_t *dev); /* * Update the power state of the device * * @param dev: duer_pm_device_t * * state: duer_pm_dev_state * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_update_pm_state(duer_pm_device_t *dev, duer_pm_dev_state_t state); /* * Get the power state of the device * * @param dev: duer_pm_device_t * * * @return duer_pm_dev_state_t */ extern duer_pm_dev_state_t duer_pm_get_pm_state(duer_pm_device_t *dev); /* * Update the suspend target time of the device * * @param dev: duer_pm_device_t * * time: uint32_t * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_update_suspend_target_time(duer_pm_device_t *dev, uint32_t time_s); /* * Update the poweroff target time of the device * * @param dev: duer_pm_device_t * * time: uint32_t * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_update_poweroff_target_time(duer_pm_device_t *dev, uint32_t time_s); /* * Traverse the device list * * @param traverse : Processing the device function * * @return int: Success: DUER_OK * Failed: DUER_ERR_FAILED */ extern int duer_pm_traverses_dev_list(treatment_dev_func handler); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_PM_DEV_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_pm_dev.h
C
apache-2.0
5,833
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_pm_manager.c * Auth: Chang Li (changli@baidu.com) * Desc: PM manager implementation. */ #ifndef BAIDU_DUER_LIGHTDUER_PM_MANAGER_H #define BAIDU_DUER_LIGHTDUER_PM_MANAGER_H #include "lightduer_notifier.h" #include "lightduer_pm_policy.h" #ifdef __cplusplus extern "C" { #endif typedef enum { PM_SYSTEM_ENTER, PM_SYSTEM_EXIT } pm_system_handle_action_t; typedef int (*pm_system_handler_func)(pm_system_handle_action_t action, pm_policy_t policy, uint32_t timer_sec, int *data); /** * Register notifier for observer use * * @param notifier: duer_notifier_block_t object * * @return int : 0 for Success, others for failure * */ int duer_pm_register_notifier(duer_notifier_block_t *notifier); /** * Power Manager initialization * * @return int : 0 for Success, others for failure * */ int duer_pm_init(pm_system_handler_func hdlr); /** * Power Manager main logic * * @return int : 0 for Success, others for failure * */ int duer_pm_manager(); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_PM_MANAGER_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_pm_manager.h
C
apache-2.0
1,697
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * File: lightduer_pm_policy.h * Auth: Chang Li (changli@baidu.com) * Desc: PM Policy Head file */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_PM_POLICY_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_PM_POLICY_H #include "lightduer_pm_dev.h" #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif typedef enum pm_policy { PM_POLICY_NONE, PM_POLICY_CPU_FREQ, PM_POLICY_SLEEP, PM_POLICY_POWER_OFF, PM_POLICY_MAX } pm_policy_t; typedef struct user_policy { enum _baidu_ca_bool_e is_valid; duer_pm_dev_type_t type; duer_pm_dev_state_t state; uint32_t duration; } user_policy_t; typedef int (*pm_policy_func)(const duer_pm_device_list_t *dev_list, pm_policy_t *out_policy); /** * Make decision according to current situation * * @param *dev_list : list of registered device * @param *out_policy : output Policy * * @return int : 0 for Success, others for failure. * */ int duer_pm_policy(const duer_pm_device_list_t *dev_list, pm_policy_t *out_policy); /** * Check pm policy * * @param *dev_list : list of registered device * @param *out_policy : output Policy * @param handler: pm policy function * * @return int : 0 for Success, others for failure. * */ int duer_pm_check_policy(const duer_pm_device_list_t *dev_list, pm_policy_t *out_policy, pm_policy_func handler); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_PM_POLICY_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_pm_policy.h
C
apache-2.0
2,058
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: The random APIs. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_RANDOM_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_RANDOM_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif /* * Obtain the random number * * @Return duer_s32_t, the random number. */ DUER_INT duer_s32_t duer_random(void); /* * The random callbacks * Return the random number * 0, means random got fail */ typedef duer_s32_t (*duer_random_f)(); /* * Initial the random callback for Baidu CA * * @Param f_random, in, the function obtain the random */ DUER_EXT void duer_random_init(duer_random_f f_random); #ifdef __cplusplus } #endif #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_RANDOM_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_random.h
C
apache-2.0
1,433
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_random_impl.h * Auth: Zhang Leliang(zhangleliang@baidu.com) * Desc: Provide the random APIs. */ #ifndef BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_RANDOM_IMPL_H #define BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_RANDOM_IMPL_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif duer_s32_t duer_random_impl(void); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_RANDOM_IMPL_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_random_impl.h
C
apache-2.0
1,075
/** * Copyright (2018) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_security_bind_device.h * Auth: Chen Xihao (chenxihao@baidu.com) * Desc: Support App to bind device securely */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SECURITY_BIND_DEVICE_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SECURITY_BIND_DEVICE_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif /** * DESC: * Start the task to bind device securely. * * PARAM: * @param[in] uuid: the uuid of device * @param[in] bind_token: the bind token of device * @param[in] lifecycle: the lifecycle of task in seconds, at least 60s * * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED. */ duer_status_t duer_start_security_bind_device_task(const char *uuid, const char *bind_token, size_t lifecycle); /** * DESC: * Stop the task that bind device. * * PARAM: none * * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED. */ duer_status_t duer_stop_security_bind_device_task(void); #ifdef __cplusplus } #endif #endif /* BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SECURITY_BIND_DEVICE_H */
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_security_bind_device.h
C
apache-2.0
1,694
// Copyright (2017) Baidu Inc. All rights reserveed. // // File: lightduer_session.h // Auth: Su Hao (suhao@baidu.com) // Desc: Manage the talk session. #ifndef BAIDU_DUER_LIGHTDUER_MODULES_VOICE_ENGINE_LIGHTDUER_SESSION_H #define BAIDU_DUER_LIGHTDUER_MODULES_VOICE_ENGINE_LIGHTDUER_SESSION_H #ifdef __cplusplus extern "C" { #endif #include "lightduer_types.h" extern void duer_session_initialize(void); extern duer_u32_t duer_session_generate(void); extern duer_bool duer_session_consume(duer_u32_t id); extern duer_bool duer_session_is_matched(duer_u32_t id); extern void duer_session_finalize(void); #ifdef __cplusplus } #endif #endif/*BAIDU_DUER_LIGHTDUER_MODULES_VOICE_ENGINE_LIGHTDUER_SESSION_H*/
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_session.h
C
apache-2.0
713
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Chang Li(changli@baidu.com) // // Description: The sleep APIs. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_SLEEP_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_SLEEP_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif /* * sleep function * * @param ms, the sleep in ms. */ DUER_INT void duer_sleep(duer_u32_t ms); /* * The sleep callbacks */ typedef void (*duer_sleep_f_t)(duer_u32_t ms); /* * Initial the sleep callbacks for Baidu CA * * @Param _f_sleep, in, sleep function implementation from different platform */ DUER_EXT void baidu_ca_sleep_init(duer_sleep_f_t _f_sleep); #ifdef __cplusplus } #endif #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_SLEEP_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_sleep.h
C
apache-2.0
1,384
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_speex.h * Auth: Su Hao (suhao@baidu.com) * Desc: Light duer Speex encoder. */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SPEEX_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SPEEX_H #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif typedef void *duer_speex_handler; typedef void (*duer_encoded_func)(const void *, size_t); duer_speex_handler duer_speex_create(int rate, int quality); void duer_speex_encode(duer_speex_handler handler, const void *data, size_t size, duer_encoded_func func); void duer_speex_destroy(duer_speex_handler handler); #ifdef __cplusplus } #endif #endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SPEEX_H*/
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_speex.h
C
apache-2.0
1,296
/* Copyright (2017) Baidu Inc. All rights reserved. * * File: duerapp_system_info.h * Auth: Zhong Shuai(zhongshuai@baidu.com) * Desc: Provide the information about the system */ #ifndef BAIDU_DUER_LIBDUER_DEVICE_PLATFORM_INCLUDE_LIGHTDUER_STATISTICS_H #define BAIDU_DUER_LIBDUER_DEVICE_PLATFORM_INCLUDE_LIGHTDUER_STATISTICS_H #ifdef __cplusplus extern "C" { #endif /* * Initialise System Info Module * * @param void: * * @return int: Success: DUER_OK * Failed: Other */ int duer_statistics_initialize(void); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIBDUER_DEVICE_PLATFORM_INCLUDE_LIGHTDUER_STATISTICS_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_statistics.h
C
apache-2.0
644
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_store_voice.h * Auth: Zhang Leliang (zhangleliang@baidu.com) * Desc: the voice/speex store interface, * different platform with different implementations, make sure its implementation before use */ #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_STORE_VOICE_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_STORE_VOICE_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /* * Initialise store * * @param name_id: use this to identify the differernt voices * * @return int: Success: 0 * Failed: Other */ int duer_store_voice_start(int name_id); /* * store the voice data, e.g. PCM data * * @param data: voice data * @param size: data size * * @return int: Success: 0 * Failed: Other */ int duer_store_voice_write(const void *data, uint32_t size); /* * store the speex data * * @param data: speex data * @param size: data size * * @return int: Success: 0 * Failed: Other */ int duer_store_speex_write(const void *data, uint32_t size); /* * finilize store * * @return int: Success: 0 * Failed: Other */ int duer_store_voice_end(void); #ifdef __cplusplus } #endif #endif /*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_STORE_VOICE_H*/
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_store_voice.h
C
apache-2.0
1,882
/* * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * Author: Zhong Shuai (zhongshuai@baidu.com) * * Desc: Provide System Information */ #ifndef BAIDU_DUER_LIBDUER_DEVICE_LIGHTDUER_SYSTEM_INFO_H #define BAIDU_DUER_LIBDUER_DEVICE_LIGHTDUER_SYSTEM_INFO_H #include <stdint.h> #include <stddef.h> #define DUER_OS_VERSION "release-v3.1.1" #define OS_VERSION_LEN 20 #define SW_VERSION_LEN 20 #define FIRMWARE_VERSION_LEN 15 #define BRAND_LEN 20 #define EQUIPMENT_TYPE_LEN 20 #define HARDWARE_VERSION_LEN 20 #define DEVELOPER_NAME_LEN 20 #define TASK_NAME_LEN 30 #define PARTITION_NAME_LEN 10 #define MOUNT_INFO_LEN 20 #define UNILIZATION_LEN 5 #define NETWORK_CARD_NAME_LEN 10 #define HW_ADDRESS_LEN 17 #define SSID_LEN 20 #define BSSID_LEN 17 #define IPV4_ADDRESS_LEN 32 #define BCAST_LEN 32 #define MASK_LEN 32 #define IPV6_ADDRESS_LEN 128 #define LINK_ENACP_LEN 10 #ifdef __cplusplus extern "C" { #endif typedef struct _system_static_info_s { char os_version[OS_VERSION_LEN + 1]; char sw_version[SW_VERSION_LEN + 1]; char brand[BRAND_LEN + 1]; char equipment_type[EQUIPMENT_TYPE_LEN + 1]; char hardware_version[HARDWARE_VERSION_LEN + 1]; uint32_t ram_size; // Unit (KB) uint32_t rom_size; // Unit (KB) } duer_system_static_info_t; typedef struct _task_info_s { char task_name[TASK_NAME_LEN]; int8_t cpu_occupancy_rate; // If it is negative, it will not reported uint32_t stack_size; uint32_t stack_free_size; int32_t stack_max_used_size; // If it is negative, it will not reported struct _task_info_s *next; } duer_task_info_t; typedef struct _system_dynamic_info_s { uint64_t system_up_time; // Unit (Heartbeat of the system) uint32_t running_task; uint32_t total_task; duer_task_info_t *task_info; char *system_load; // If it is NULL, it will not reported size_t system_load_len; char *system_crash_info; // If it is NULL, it will not reported size_t system_crash_info_len; } duer_system_dynamic_info_t; typedef enum _memory_type { RAM = 1, PSRAM = 2, } duer_memory_type_t; typedef struct _memory_info { duer_memory_type_t memory_type; uint64_t total_memory_size; // Unit (KB) uint64_t available_memory_size; // Unit (KB) int64_t shared_memory_size; int64_t buffer_memory_size; int64_t swap_size; int64_t free_swap_size; uint64_t peak; // Within 5 minutes Unit (KB) uint64_t trough; // Within 5 minutes Unit (KB) uint64_t average; // Within 5 minutes Unit (KB) } duer_memory_info_t; typedef struct _disk_info_t { char partition_name[PARTITION_NAME_LEN + 1]; char mount_info[MOUNT_INFO_LEN + 1]; uint8_t unilization; // percentage (50%) uint64_t total_size; // Unit (KB) uint64_t used_size; // Unit (KB) uint64_t available_size; // Unit (KB) struct _disk_info_t *next; } duer_disk_info_t; typedef struct _wireless_info_s { char ssid[SSID_LEN + 1]; char bssid[BSSID_LEN + 1]; int8_t link; // General quality of the reception int32_t level; // Signal strength at the receiver int32_t noise; // Silence level (no packet) at the receiver } duer_wireless_info; typedef enum { ETHERNET_TYPE = 1, WIFI_TYPE, } duer_network_type; // Only retport one network info which the Duer OS used typedef struct _network_info_s { duer_network_type network_type; char hw_address[HW_ADDRESS_LEN + 1]; char ipv4_address[IPV4_ADDRESS_LEN + 1]; // If it is NULL, it will not reported char ipv6_address[IPV6_ADDRESS_LEN + 1]; // If it is NULL, it will not reported duer_wireless_info wireless; uint32_t mtu; uint64_t transmitted_packets; uint64_t received_packets; uint64_t forwarded_packets; uint64_t dropped_packets; uint64_t checksum_error; uint64_t invalid_length_error; uint64_t routing_error; uint64_t protocol_error; uint64_t tx_average_speed; // Within 5 minutes Unit (bytes/s) uint64_t rx_average_speed; // Within 5 minutes Unit (bytes/s) } duer_network_info_t; typedef struct _system_info_ops_t { int (*init)(void); int (*get_system_static_info)(duer_system_static_info_t *info); int (*get_system_dynamic_info)(duer_system_dynamic_info_t *info); int (*free_system_dynamic_info)(duer_system_dynamic_info_t *info); int (*get_memory_info)(duer_memory_info_t *mem_info); int (*get_disk_info)(duer_disk_info_t **disk_info); int (*free_disk_info)(duer_disk_info_t *disk_info); int (*get_network_info)(duer_network_info_t *network_info); int (*free_network_info)(duer_network_info_t *network_info); } duer_system_info_ops_t; extern int duer_init_system_info(void); extern int duer_system_info_register_system_info_ops(duer_system_info_ops_t *info_ops); extern int duer_system_info_get_static_system_info(duer_system_static_info_t *info); extern int duer_system_info_get_dynamic_system_info(duer_system_dynamic_info_t *info); extern int duer_system_info_free_dynamic_system_info(duer_system_dynamic_info_t *info); extern int duer_system_info_get_mem_info(duer_memory_info_t *info); extern int duer_system_info_get_disk_info(duer_disk_info_t **info); extern int duer_system_info_free_disk_info(duer_disk_info_t *info); extern int duer_system_info_get_network_info(duer_network_info_t *info); extern int duer_system_info_free_network_info(duer_network_info_t *info); extern void duer_system_info_show_system_static_info(duer_system_static_info_t const *info); extern void duer_system_info_show_system_dynamic_info(duer_system_dynamic_info_t const *info); extern void duer_system_info_show_memory_info(duer_memory_info_t const *memory_info); extern void duer_system_info_show_disk_info(duer_disk_info_t const *disk_info); extern void duer_system_info_show_networkcard_info(duer_network_info_t const *network_info); extern int duer_uninit_system_info(void); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIBDUER_DEVICE_LIGHTDUER_SYSTEM_INFO_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_system_info.h
C
apache-2.0
7,269
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_thread.h * Auth: Zhang Leliang(zhangleliang@baidu.com) * Desc: Provide the thread APIs. */ #ifndef LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_THREAD_H #define LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_THREAD_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif /* * get the thread id */ duer_u32_t duer_get_thread_id(void); /* * The thread callbacks */ typedef duer_u32_t (*duer_get_thread_id_f_t)(); /* * Initial the thread callbacks for lightduer * * @Param _f_get_thread_id, in, get threadid implementation from different platform */ DUER_EXT void duer_thread_init(duer_get_thread_id_f_t _f_get_thread_id); #ifdef __cplusplus } #endif #endif // LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_THREAD_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_thread.h
C
apache-2.0
1,368
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_thread_impl.h * Auth: Zhang Leliang(zhangleliang@baidu.com) * Desc: Provide the thread APIs. */ #ifndef BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_THREAD_IMPL_H #define BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_THREAD_IMPL_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif duer_u32_t duer_get_thread_id_impl(); #ifdef __cplusplus } #endif #endif // BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_THREAD_IMPL_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_thread_impl.h
C
apache-2.0
1,078
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_timers.h * Auth: Su Hao (suhao@baidu.com) * Desc: Provide the timer APIs. */ #ifndef BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_TIMERS_H #define BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_TIMERS_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif enum _duer_timer_type_enum { DUER_TIMER_ONCE, DUER_TIMER_PERIODIC, DUER_TIMER_TOTAL }; typedef void *duer_timer_handler; typedef void (*duer_timer_callback)(void *); duer_timer_handler duer_timer_acquire(duer_timer_callback callback, void *param, int type); int duer_timer_start(duer_timer_handler handle, size_t delay); int duer_timer_stop(duer_timer_handler handle); void duer_timer_release(duer_timer_handler handle); #ifdef __cplusplus } #endif #endif/*BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_TIMERS_H*/
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_timers.h
C
apache-2.0
1,445
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: The timestamp APIs. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TIMESTAMP_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TIMESTAMP_H #include "lightduer_types.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Macros used to compare time values * * These inlines deal with timer wrapping correctly. You are * strongly encouraged to use them * 1. Because people otherwise forget * 2. Because if the timer wrap changes in future you won't have to * alter your code. * * DUER_TIME_AFTER(a,b) returns true if the time a is after time b. */ #define DUER_TIME_AFTER(a, b) ((duer_s32_t)(b) - (duer_s32_t)(a) < 0) #define DUER_TIME_BEFORE(a, b) DUER_TIME_AFTER(b, a) #define DUER_TIME_AFTER_EQUAL(a, b) ((duer_s32_t)(a) - (duer_s32_t)(b) >= 0) #define DUER_TIME_BEFORE_EQUAL(a, b) DUER_TIME_AFTER_EQUAL(b, a) /* * Obtain the system timestamp by milliseconds * * @Return duer_u32_t, the system timestamp by milliseconds */ DUER_INT duer_u32_t duer_timestamp(void); /* * The timestamp callbacks * Return the timestamp by milliseconds */ typedef duer_u32_t (*duer_timestamp_f)(); /* * Initial the timestamp callbacks for Baidu CA * * @Param f_timestamp, in, the function obtain the timestamp */ DUER_EXT void baidu_ca_timestamp_init(duer_timestamp_f f_timestamp); #ifdef __cplusplus } #endif #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TIMESTAMP_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_timestamp.h
C
apache-2.0
2,153
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Author: Su Hao (suhao@baidu.com) // // Description: The common type definitions. #ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_TYPES_H #define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_TYPES_H #include <stdio.h> #include <stdlib.h> #include <string.h> // Declaration for local varaible & function. #define DUER_LOC static // Definition for local varaible & function. #define DUER_LOC_IMPL static // Declaration for internal varaible & function. #define DUER_INT extern // Definition for internal varaible & function. #define DUER_INT_IMPL // Declaration for external varaible & function. #define DUER_EXT extern // Definition for external varaible & function. #define DUER_EXT_IMPL /* * The ca handler */ typedef void* duer_handler; typedef void* duer_context; typedef int duer_status_t; #ifdef __GNUC__ typedef size_t duer_size_t; #else typedef unsigned int duer_size_t; #endif typedef unsigned int duer_u32_t; typedef signed int duer_s32_t; typedef unsigned short duer_u16_t; typedef signed int duer_s16_t; typedef unsigned char duer_u8_t; typedef signed char duer_s8_t; typedef char duer_bool; typedef duer_status_t (*duer_data_callback)(duer_context, const void *, duer_size_t); enum _baidu_ca_bool_e { DUER_FALSE, DUER_TRUE }; /* * The error codes. */ typedef enum _duer_errcode_enum { DUER_OK, DUER_ERR_FAILED = -0x0001, DUER_ERR_CONNECT_TIMEOUT = -0x0002, DUER_CANCEL = -0x0003, DUER_INF_TRANS_IP_BY_HTTP_DNS = -0x0004, // IP got through http dns /* Generic Errors */ DUER_ERR_INVALID_PARAMETER = -0x0010, DUER_ERR_MEMORY_OVERLOW = -0x0011, DUER_ERR_PROFILE_INVALID = -0x0012, DUER_ERR_HAS_STARTED = -0x0013, DUER_ERR_CA_NOT_CONNECTED = -0x0014, DUER_ERR_HAS_STOPPED = -0x0015, /* Network Errors */ DUER_ERR_TRANS_INTERNAL_ERROR = -0x0030, DUER_ERR_TRANS_WOULD_BLOCK = -0x0031, DUER_ERR_TRANS_TIMEOUT = -0x0032, // send timeout DUER_ERR_REG_FAIL = -0x0033, DUER_ERR_TRANS_DNS_FAIL = -0x0034, DUER_ERR_WIFI_SIGNAL_WEAK = -0x0035, DUER_ERR_WIFI_DISCONNECTED = -0x0036, DUER_ERR_MBEDTLS_NET_MAX = -0x0040, // -0x0040 - -0x0060 reserved for mbed net error code , see mbedtls/net.h for more info DUER_ERR_MBEDTLS_NET_MIN = -0x0060, DUER_ERR_MBEDTLS_SSL_MAX = -0x6000, // -0x6000 - -0x8000 reserved for ssl error code , see ssl.h for more info DUER_ERR_MBEDTLS_SSL_MIN = -0x8000, DUER_ERR_REPORT_FAILED = -0x070000, DUER_ERR_REPORT_FAILED_BEGIN = DUER_ERR_REPORT_FAILED + 0x10000 } duer_errcode_t; #endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_TYPES_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_types.h
C
apache-2.0
3,558
/** * Copyright (2017) Baidu Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * File: lightduer_voice.h * Auth: Su Hao (suhao@baidu.com) * Desc: Light duer voice APIS. */ #include "baidu_json.h" #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_VOICE_H #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_VOICE_H #ifdef __cplusplus extern "C" { #endif typedef enum _duer_voice_mode_enum { DUER_VOICE_MODE_DEFAULT, DUER_VOICE_MODE_CHINESE_TO_ENGLISH, DUER_VOICE_MODE_ENGLISH_TO_CHINESE, DUER_VOICE_MODE_WCHAT, DUER_VOICE_MODE_C2E_BOT, DUER_VOICE_MODE_INTERACTIVE_CLASS, } duer_voice_mode; typedef struct _duer_voice_cache_t { int head; int tail; int size; char *items[0]; } duer_voice_cache_t; #ifdef DUER_SECOND_WAKEUP_ENABLE typedef enum _duer_voice_send_state_enum { DUER_VOICE_SEND_STATE_DEFAULT = 1, DUER_VOICE_SEND_STATE_CACHE, DUER_VOICE_SEND_STATE_WAKEUP, DUER_VOICE_SEND_STATE_QUERY, } duer_voice_send_state; typedef enum _duer_wakeup_bit_enum { DUER_WAKEUP_BIT = 0x01, DUER_WAKEUP_EOF_BIT = 0x02, DUER_WAKEUP_2ND_WAKEUP_BIT = 0x04, } duer_wakeup_bit; #endif//DUER_SECOND_WAKEUP_ENABLE typedef void (*duer_voice_delay_func)(duer_u32_t); typedef const char* (*duer_get_dialog_id_func)(void); void duer_voice_set_delay_threshold(duer_u32_t delay, duer_voice_delay_func); void duer_voice_set_mode(duer_voice_mode mode); duer_voice_mode duer_voice_get_mode(void); int duer_voice_start(int samplerate); int duer_voice_send(const void *data, size_t size); int duer_voice_cache(duer_bool cached); int duer_voice_stop(void); int duer_voice_terminate(void); void duer_add_translate_flag(baidu_json *data); void duer_reg_dialog_id_cb(duer_get_dialog_id_func cb); #ifdef DUER_SECOND_WAKEUP_ENABLE void duer_voice_set_send_state(duer_voice_send_state send_state); /* * Restart voice session, but won't clear cached voice. * Used for second wakeup, when got new keyword recognition before previous session end. */ int duer_voice_restart(void); /* * Send voice with wakeup bit set, to trigger cloud second wakeup. * * @PARAM wakeup: the wakeup bit needs to be sent. */ int duer_voice_send_wakeup(int wakeup); /* * Check if voice started, according to topic id value. */ duer_bool duer_voice_is_started(); /* * Init second wakeup functionality. * * @PARAM cache_size: the size of cache of wakeup voice. */ void duer_voice_second_wakeup_init(int cache_size); /* * Uninit second wakeup functionality. */ void duer_voice_second_wakeup_uninit(); /* * Truncate wakeup voice cache. * * @PARAM count: the cache length after truncated. */ void duer_voice_truncate_wakeup_cache(int count); #endif//DUER_SECOND_WAKEUP_ENABLE /* * topic_id + 1 * * * Return: Other topic_id value * 0 Failed */ size_t duer_increase_topic_id(void); int duer_voice_set_speex_quality(int quality); #ifdef __cplusplus } #endif #endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_VOICE_H*/
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/clouds/dueros/lightduer/include/lightduer_voice.h
C
apache-2.0
3,515
set(COMPONENT_ADD_INCLUDEDIRS include) # Edit following two lines to set component requirements (see docs) set(COMPONENT_REQUIRES esp_http_client espcoredump spi_flash) set(COMPONENT_PRIV_REQUIRES audio_sal esp_dispatcher) set(COMPONENT_SRCS ./coredump_upload_service.c) register_component()
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/coredump_upload_service/CMakeLists.txt
CMake
apache-2.0
295
# # "main" pseudo-component makefile. # # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) COMPONENT_ADD_INCLUDEDIRS := include COMPONENT_SRCDIRS := .
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/coredump_upload_service/component.mk
Makefile
apache-2.0
206
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 <string.h> #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "freertos/queue.h" #include "freertos/task.h" #include "esp_core_dump.h" #include "esp_http_client.h" #include "esp_log.h" #include "esp_spi_flash.h" #include "esp_system.h" #include "audio_error.h" #include "audio_mem.h" #include "coredump_upload_service.h" typedef struct { xQueueHandle cmd_q; EventGroupHandle_t sync_evt; bool (*do_post)(char *url, uint8_t *data, size_t len); } coredump_upload_t; typedef struct { enum { MSG_UPLOAD, MSG_DESTROY, } msg_id; void *pdata; } coredump_msg_t; #define COREDUMP_UPLOAD_SUCCESS (BIT0) #define COREDUMP_UPLOAD_FAIL (BIT1) #define COREDUMP_UPLOAD_DESTROY (BIT2) static char *TAG = "COREDUMP_UPLOAD"; static bool coredump_read(uint8_t **des, size_t *len) { size_t addr = 0; if (esp_core_dump_image_get(&addr, len) != ESP_OK) { ESP_LOGW(TAG, "No dump info to upload"); return false; } if (*len == 0) { return false; } *des = audio_calloc(1, *len); AUDIO_MEM_CHECK(TAG, *des, return false); if (spi_flash_read(addr, *des, *len) != ESP_OK) { ESP_LOGE(TAG, "Core dump read ERROR"); free(*des); *des = NULL; *len = 0; return false; } return true; } static bool coredump_do_http_post(char *url, uint8_t *data, size_t len) { esp_http_client_config_t config = { .url = url, .method = HTTP_METHOD_POST, }; esp_http_client_handle_t http_client = esp_http_client_init(&config); esp_http_client_set_header(http_client, "Content-Type", "application/octet-stream"); esp_http_client_set_post_field(http_client, (const char *)data, (int)len); int response = 0; if (esp_http_client_perform(http_client) == ESP_OK) { response = esp_http_client_get_status_code(http_client); ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", response, esp_http_client_get_content_length(http_client)); } else { ESP_LOGE(TAG, "Post failed"); } esp_http_client_cleanup(http_client); return response == 200 ? true : false; } static bool coredump_upload_partition(coredump_upload_t *uploader, char *url) { uint8_t *buf = NULL; size_t len = 0; bool ret = false; if (coredump_read(&buf, &len) == true) { if (uploader->do_post(url, buf, len) == true) { ESP_LOGI(TAG, "core dump upload success"); ret = true; } else { ESP_LOGE(TAG, "core dump upload failed"); } } if (buf != NULL) { free(buf); } return ret; } static void coredump_upload_task(void *args) { coredump_upload_t *uploader = periph_service_get_data((periph_service_handle_t)args); bool task_run = true; coredump_msg_t msg = { 0 }; while (task_run) { if (xQueueReceive(uploader->cmd_q, &msg, portMAX_DELAY)) { switch (msg.msg_id) { case MSG_UPLOAD: { bool result = coredump_upload_partition(uploader, msg.pdata); xEventGroupSetBits(uploader->sync_evt, result == true ? COREDUMP_UPLOAD_SUCCESS : COREDUMP_UPLOAD_FAIL); break; } case MSG_DESTROY: task_run = false; break; default: break; } if (msg.pdata) { free(msg.pdata); } } } xEventGroupSetBits(uploader->sync_evt, COREDUMP_UPLOAD_DESTROY); vTaskDelete(NULL); } bool coredump_need_upload() { bool ret = false; esp_reset_reason_t reset_reason = esp_reset_reason(); ESP_LOGI(TAG, "reset reason is %d", reset_reason); switch (reset_reason) { case ESP_RST_UNKNOWN: case ESP_RST_PANIC: case ESP_RST_INT_WDT: case ESP_RST_TASK_WDT: // case ESP_RST_WDT: ret = true; break; default: ret = false; } return ret; } esp_err_t coredump_upload(periph_service_handle_t handle, char *url) { AUDIO_MEM_CHECK(TAG, handle, return ESP_FAIL); coredump_upload_t *uploader = periph_service_get_data(handle); coredump_msg_t msg = { .msg_id = MSG_UPLOAD, }; msg.pdata = audio_calloc(1, strlen(url) + 1); AUDIO_MEM_CHECK(TAG, msg.pdata, return ESP_FAIL); memcpy(msg.pdata, url, strlen(url)); xQueueSend(uploader->cmd_q, &msg, portMAX_DELAY); EventBits_t bits = xEventGroupWaitBits(uploader->sync_evt, COREDUMP_UPLOAD_FAIL | COREDUMP_UPLOAD_SUCCESS, true, false, portMAX_DELAY); return (bits & COREDUMP_UPLOAD_SUCCESS) ? ESP_OK : ESP_FAIL; } esp_err_t coredump_upload_deinit(periph_service_handle_t handle) { AUDIO_MEM_CHECK(TAG, handle, return ESP_FAIL); coredump_upload_t *uploader = periph_service_get_data(handle); coredump_msg_t msg = { .msg_id = MSG_DESTROY, }; xQueueSend(uploader->cmd_q, &msg, portMAX_DELAY); xEventGroupWaitBits(uploader->sync_evt, COREDUMP_UPLOAD_DESTROY, true, true, portMAX_DELAY); if (uploader) { if (uploader->cmd_q) { vQueueDelete(uploader->cmd_q); } if (uploader->sync_evt) { vEventGroupDelete(uploader->sync_evt); } free(uploader); } ESP_LOGW(TAG, "coredump uploader destroyed"); return ESP_OK; } periph_service_handle_t coredump_upload_service_create(coredump_upload_service_config_t *config) { coredump_upload_t *uploader = audio_calloc(1, sizeof(coredump_upload_t)); AUDIO_MEM_CHECK(TAG, uploader, return NULL); uploader->cmd_q = xQueueCreate(2, sizeof(coredump_msg_t)); AUDIO_MEM_CHECK(TAG, uploader->cmd_q, { free(uploader); return NULL; }); uploader->sync_evt = xEventGroupCreate(); AUDIO_MEM_CHECK(TAG, uploader->sync_evt, { vQueueDelete(uploader->cmd_q); free(uploader); return NULL; }); uploader->do_post = config->do_post != NULL ? config->do_post : coredump_do_http_post; periph_service_config_t cfg = { .task_stack = config->task_stack, .task_prio = config->task_prio, .task_core = config->task_core, .task_func = coredump_upload_task, .extern_stack = false, /* Need flash read operation */ .service_start = NULL, .service_stop = NULL, .service_ioctl = NULL, .service_destroy = coredump_upload_deinit, .service_name = "CoreDumpUpload", .user_data = (void *)uploader, }; periph_service_handle_t periph_coredump = periph_service_create(&cfg); AUDIO_MEM_CHECK(TAG, periph_coredump, { vQueueDelete(uploader->cmd_q); vEventGroupDelete(uploader->sync_evt); free(uploader); return NULL; }); periph_service_set_callback(periph_coredump, config->evt_cb, config->cb_ctx); return periph_coredump; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/coredump_upload_service/coredump_upload_service.c
C
apache-2.0
8,197
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef __COREDUMP_UPLOAD_H__ #define __COREDUMP_UPLOAD_H__ #include <stdbool.h> #include "esp_err.h" #include "periph_service.h" #ifdef __cplusplus extern "C" { #endif /** * @brief coredump service configuration parameters */ typedef struct { int task_stack; /*!< >0 Service task stack; =0 with out task created */ int task_prio; /*!< Service task priority (based on freeRTOS priority) */ int task_core; /*!< Service task running in core (0 or 1) */ periph_service_cb evt_cb; /*!< Service callback function */ void *cb_ctx; /*!< Callback context */ bool (*do_post)(char *url, uint8_t *data, size_t len); /*!< POST interface, users can override this to customize the http client. if left NULL, the service will use the default one */ } coredump_upload_service_config_t; #define COREDUMP_UPLOAD_SERVICE_DEFAULT_CONFIG() \ { \ .task_stack = 4096, \ .task_prio = 5, \ .task_core = 1, \ .evt_cb = NULL, \ .cb_ctx = NULL, \ .do_post = NULL, \ } /** * @brief This function will check the reset code and determine whether to upload the coredump * @return * - true: last reboot is a abnormal reset. * - false */ bool coredump_need_upload(); /** * @brief Upload the core dump image to the url. * This function will block the current task until the upload process finished. * * @param[in] handle the 'periph_service_handle_t' * @param[in] url server addr * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t coredump_upload(periph_service_handle_t handle, char *url); /** * @brief Create the core dump upload service instance * * @param config configuration of the OTA service * * @return * - NULL: Failed * - Others: Success */ periph_service_handle_t coredump_upload_service_create(coredump_upload_service_config_t *config); #ifdef __cplusplus } #endif #endif /* __COREDUMP_UPLOAD_H__ */
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/coredump_upload_service/include/coredump_upload_service.h
C
apache-2.0
3,401
set(COMPONENT_ADD_INCLUDEDIRS include led_indicator/include led_bar/include) # Edit following two lines to set component requirements (see docs) set(COMPONENT_REQUIRES ) set(COMPONENT_PRIV_REQUIRES audio_sal audio_board audio_hal esp_peripherals) set(COMPONENT_SRCS ./display_service.c led_indicator/led_indicator.c led_bar/led_bar_is31x.c led_bar/led_bar_aw2013.c led_bar/led_bar_ws2812.c) register_component()
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/CMakeLists.txt
CMake
apache-2.0
415
# # Main Makefile. This is basically the same as a component makefile. COMPONENT_ADD_INCLUDEDIRS := include led_indicator/include led_bar/include COMPONENT_SRCDIRS := . led_indicator led_bar
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/component.mk
Makefile
apache-2.0
192
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 <stdarg.h> #include <string.h> #include <stdlib.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" #include "freertos/event_groups.h" #include "board.h" #include "sdkconfig.h" #include "audio_mem.h" #include "display_service.h" #include "esp_log.h" #include "audio_error.h" #include "periph_service.h" #define DISPLAY_TASK_PRIORITY 5 #define DISPLAY_TASK_STACK_SIZE 6*1024 static const char *TAG = "DISPLAY"; typedef struct display_service_impl { periph_service_handle_t based; void *instance; char *service_name; void *user_data; } display_service_impl_t; display_service_handle_t display_service_create(display_service_config_t *config) { AUDIO_NULL_CHECK(TAG, config, return NULL); display_service_impl_t *disp = audio_calloc(1, sizeof(display_service_impl_t)); AUDIO_MEM_CHECK(TAG, disp, return NULL); config->based_cfg.user_data = (void *)disp; disp->based = periph_service_create(&config->based_cfg); disp->instance = config->instance; return disp; } esp_err_t display_service_set_pattern(void *handle, int display_pattern, int value) { AUDIO_NULL_CHECK(TAG, handle, return ESP_FAIL); display_service_impl_t *dis = (display_service_impl_t *)handle; esp_err_t ret = periph_service_ioctl(dis->based, (void *)dis->instance, display_pattern, value); return ret; } esp_err_t display_destroy(display_service_handle_t handle) { AUDIO_NULL_CHECK(TAG, handle, return ESP_FAIL); periph_service_destroy(handle->based); audio_free(handle); return ESP_OK; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/display_service.c
C
apache-2.0
3,053
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _DISPLAY_SERVICE_H_ #define _DISPLAY_SERVICE_H_ #include "periph_service.h" #ifdef __cplusplus extern "C" { #endif typedef enum { DISPLAY_PATTERN_UNKNOWN = 0, DISPLAY_PATTERN_WIFI_SETTING = 1, DISPLAY_PATTERN_WIFI_CONNECTTING = 2, DISPLAY_PATTERN_WIFI_CONNECTED = 3, DISPLAY_PATTERN_WIFI_DISCONNECTED = 4, DISPLAY_PATTERN_WIFI_SETTING_FINISHED = 5, DISPLAY_PATTERN_BT_CONNECTTING = 6, DISPLAY_PATTERN_BT_CONNECTED = 7, DISPLAY_PATTERN_BT_DISCONNECTED = 8, DISPLAY_PATTERN_RECORDING_START = 9, DISPLAY_PATTERN_RECORDING_STOP = 10, DISPLAY_PATTERN_RECOGNITION_START = 11, DISPLAY_PATTERN_RECOGNITION_STOP = 12, DISPLAY_PATTERN_WAKEUP_ON = 13, DISPLAY_PATTERN_WAKEUP_FINISHED = 14, DISPLAY_PATTERN_MUSIC_ON = 15, DISPLAY_PATTERN_MUSIC_FINISHED = 16, DISPLAY_PATTERN_VOLUME = 17, DISPLAY_PATTERN_MUTE_ON = 18, DISPLAY_PATTERN_MUTE_OFF = 19, DISPLAY_PATTERN_TURN_ON = 20, DISPLAY_PATTERN_TURN_OFF = 21, DISPLAY_PATTERN_BATTERY_LOW = 22, DISPLAY_PATTERN_BATTERY_CHARGING = 23, DISPLAY_PATTERN_BATTERY_FULL = 24, DISPLAY_PATTERN_POWERON_INIT = 25, DISPLAY_PATTERN_WIFI_NO_CFG = 26, DISPLAY_PATTERN_SPEECH_BEGIN = 27, DISPLAY_PATTERN_SPEECH_OVER = 28, DISPLAY_PATTERN_MAX, } display_pattern_t; typedef struct display_service_impl *display_service_handle_t; /** * @brief Display service configurations */ typedef struct { periph_service_config_t based_cfg; /*!< Peripheral service configuration */ void *instance; /*!< Sub-instance */ } display_service_config_t; /* * @brief Create the display service * * @param cfg A pointer to display_service_config_t * * @return * - NULL, Fail * - Others, Success */ display_service_handle_t display_service_create(display_service_config_t *cfg); /* * @brief Set service display pattern * * @param handle A pointer to display_service_handle_t * @param disp_pattern Enumeration of display_pattern_t * @param value Data of the disp_pattern * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t display_service_set_pattern(void *handle, int disp_pattern, int value); /* * @brief Destroy display service * * @param handle A pointer to display_service_handle_t * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t display_destroy(display_service_handle_t handle); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/include/display_service.h
C
apache-2.0
4,050
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _LED_BAR_AW2013_H_ #define _LED_BAR_AW2013_H_ #include "display_service.h" #include "esp_peripherals.h" #ifdef __cplusplus extern "C" { #endif void aw2013_led_bar_task(void *parameters); // Defination a pointer to the task entry function that will be referred by users code /** * @brief Initialize led bar instance * * @return * - NULL Error * - others Success */ esp_periph_handle_t led_bar_aw2013_init(void); /** * @brief Set led bar display pattern. * * @param handle led bar instance * @param pat display pattern * @param value value of pattern * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t led_bar_aw2013_pattern(void *handle, int pat, int value); /** * @brief Set blinking period and times * * @param handle led bar instance * @param time times of blink * @param period period of blink * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t led_bar_aw2013_set_blink_time(void *handle, uint8_t time, int period); /** * @brief Destroy esp_periph_handle_t instance * * @param handle led bar instance * */ void led_bar_aw2013_deinit(esp_periph_handle_t handle); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_bar/include/led_bar_aw2013.h
C
apache-2.0
2,472
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _LED_BAR_IS31X_H_ #define _LED_BAR_IS31X_H_ #include "display_service.h" #include "esp_peripherals.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Initialize esp_periph_handle_t instance * * @param num led gpio number * * @return * - NULL, Fail * - Others, Success */ esp_periph_handle_t led_bar_is31x_init(); /** * @brief Set led bar display pattern. * * @param handle led bar instance * @param pat display pattern * @param value value of pattern * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t led_bar_is31x_pattern(void *handle, int pat, int value); /** * @brief Destroy esp_periph_handle_t instance * * @param handle led bar instance * * @return * - ESP_OK * - ESP_FAIL */ void led_bar_is31x_deinit(esp_periph_handle_t handle); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_bar/include/led_bar_is31x.h
C
apache-2.0
2,128
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef __LED_BAR_WS2812_H__ #define __LED_BAR_WS2812_H__ #include "display_service.h" #ifdef __cplusplus extern "C" { #endif typedef struct led_bar_ws2812_impl *led_bar_ws2812_handle_t; /** * @brief Initialize led_bar_ws2812_handle_t instance * * @param gpio_num The GPIO number of ws2812 * @param led_num The number of all ws2812 * * @return * - led_bar_ws2812_handle_t */ led_bar_ws2812_handle_t led_bar_ws2812_init(gpio_num_t gpio_num, int led_num); /** * @brief Set ws2812 pattern * * @param handle ws2812 indicator instance * @param pat display pattern * @param value value of pattern * * @return * - ESP_OK, success * - Others, fail */ esp_err_t led_bar_ws2812_pattern(void *handle, int pat, int value); /** * @brief Destroy led_bar_ws2812_handle_t instance * * @param handle ws2812 indicator instance * * @return * @return * - ESP_OK, success * - Others, fail */ esp_err_t led_bar_ws2812_deinit(led_bar_ws2812_handle_t handle); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_bar/include/led_bar_ws2812.h
C
apache-2.0
2,302
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "esp_types.h" #include "esp_log.h" #include "periph_service.h" #include "periph_aw2013.h" #include "led_bar_aw2013.h" #define AW2013_QUE_LENGTH 4 #define AW2013_DEFAULT_BLINK_PERIOD 500 #define AW2013_DEFAULT_BLINK_TIME 3 static const char *TAG = "LED_BAR_AW2013"; static const int DESTROY_BIT = BIT0; static QueueHandle_t aw2013_que; static bool _task_run; static EventGroupHandle_t g_event_bit; typedef struct { esp_periph_handle_t instance; display_pattern_t pat; int blink_time_period; uint8_t blink_time; } led_bar_aw2013_cmd_t; typedef enum { AW2013_COLOR_RED = 0x000000ff, AW2013_COLOR_GREEN = 0x00ff0000, AW2013_COLOR_BLUE = 0x0000ff00, AW2013_COLOR_WHITE = 0x00ffffff, AW2013_COLOR_YELLOW = 0x00ff00ff, AW2013_COLOR_CYAN = 0x00ffff00, AW2013_COLOR_PURPLE = 0x0000ffff, AW2013_COLOR_BLACK = 0x0, } led_bar_aw2013_color_t; void aw2013_led_bar_task(void *parameters) { display_pattern_t pat = DISPLAY_PATTERN_UNKNOWN; led_bar_aw2013_cmd_t recv_cmd = {0}; _task_run = true; int blink_period = AW2013_DEFAULT_BLINK_PERIOD; int blink_time = AW2013_DEFAULT_BLINK_TIME; xEventGroupClearBits(g_event_bit, DESTROY_BIT); while (_task_run) { if (aw2013_que) { if (xQueueReceive(aw2013_que, &recv_cmd, 0) == pdPASS) { pat = recv_cmd.pat; if (recv_cmd.blink_time_period != 0) { blink_period = recv_cmd.blink_time_period; } if (recv_cmd.blink_time != 0) { blink_time = recv_cmd.blink_time; } } switch (pat) { case DISPLAY_PATTERN_UNKNOWN: { vTaskDelay(10 / portTICK_PERIOD_MS); break; } case DISPLAY_PATTERN_WIFI_SETTING: { // cyan led flash periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_AUTO); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_CYAN); break; } case DISPLAY_PATTERN_WIFI_CONNECTTING: { // green led flash periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_AUTO); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_GREEN); break; } case DISPLAY_PATTERN_WIFI_CONNECTED: { // green led blink periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); for (int i = 0; i < blink_time; i++) { periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_GREEN); vTaskDelay(blink_period / portTICK_PERIOD_MS); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLACK); vTaskDelay(blink_period / portTICK_PERIOD_MS); } break; } case DISPLAY_PATTERN_WIFI_DISCONNECTED: { // red led blink periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); for (int i = 0; i < blink_time; i++) { periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_RED); vTaskDelay(blink_period / portTICK_PERIOD_MS); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLACK); vTaskDelay(blink_period / portTICK_PERIOD_MS); } break; } case DISPLAY_PATTERN_WIFI_SETTING_FINISHED: { vTaskDelay(10 / portTICK_PERIOD_MS); break; } case DISPLAY_PATTERN_BT_CONNECTTING: { // blue led flash periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_AUTO); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLUE); break; } case DISPLAY_PATTERN_BT_CONNECTED: { // blue led blink periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); for (int i = 0; i < blink_time; i++) { periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLUE); vTaskDelay(blink_period / portTICK_PERIOD_MS); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLACK); vTaskDelay(blink_period / portTICK_PERIOD_MS); } break; } case DISPLAY_PATTERN_BT_DISCONNECTED: { // red led blink periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); for (int i = 0; i < blink_time; i++) { periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_RED); vTaskDelay(blink_period / portTICK_PERIOD_MS); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLACK); vTaskDelay(blink_period / portTICK_PERIOD_MS); } break; } case DISPLAY_PATTERN_WAKEUP_ON: { // white led on periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_WHITE); break; } case DISPLAY_PATTERN_WAKEUP_FINISHED: { periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLACK); break; } case DISPLAY_PATTERN_TURN_OFF: { periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_BLACK); break; } case DISPLAY_PATTERN_BATTERY_LOW: { // red led on periph_aw2013_set_mode(recv_cmd.instance, AW2013_MODE_LED); periph_aw2013_set_rgb_value(recv_cmd.instance, AW2013_COLOR_RED); break; } default: { ESP_LOGW(TAG, "Pat %d hasn't been defined, you can defined it by yourself", pat); break; } } pat = DISPLAY_PATTERN_UNKNOWN; } else { ESP_LOGE(TAG, "The msg queue is NULL"); break; } } if (g_event_bit) { xEventGroupSetBits(g_event_bit, DESTROY_BIT); } vTaskDelete(NULL); } esp_periph_handle_t led_bar_aw2013_init(void) { aw2013_que = xQueueCreate(AW2013_QUE_LENGTH, sizeof(led_bar_aw2013_cmd_t)); AUDIO_NULL_CHECK(TAG, aw2013_que, { ESP_LOGE(TAG, "Fail to create queue"); return NULL; }); g_event_bit = xEventGroupCreate(); AUDIO_NULL_CHECK(TAG, g_event_bit, { ESP_LOGE(TAG, "Fail to create a event group"); vQueueDelete(aw2013_que); aw2013_que = NULL; return NULL; }); periph_aw2013_cfg_t aw2013_cfg = { .mode = AW2013_MODE_LED, .bright = AW2013_BRIGHT_2, .rgb_value = AW2013_COLOR_BLACK }; esp_periph_handle_t aw2013_handle = periph_aw2013_init(&aw2013_cfg); AUDIO_NULL_CHECK(TAG, aw2013_handle, { ESP_LOGE(TAG, "Fail to initialize the aw2013 chip"); vQueueDelete(aw2013_que); aw2013_que = NULL; vEventGroupDelete(g_event_bit); g_event_bit = NULL; return NULL; }); esp_periph_init(aw2013_handle); return aw2013_handle; } esp_err_t led_bar_aw2013_pattern(void *handle, int pat, int value) { if (handle == NULL || aw2013_que == NULL) { ESP_LOGE(TAG, "The handle: %p or queue: %p of aw2013 is NULL", handle, aw2013_que); return ESP_FAIL; } led_bar_aw2013_cmd_t cmd = { .instance = handle, .pat = pat, }; if (xQueueSend(aw2013_que, &cmd, portMAX_DELAY) != pdPASS) { ESP_LOGE(TAG, "Fail to set aw2013's pattern"); return ESP_FAIL; } return ESP_OK; } esp_err_t led_bar_aw2013_set_blink_time(void *handle, uint8_t time, int period) { AUDIO_NULL_CHECK(TAG, handle, return ESP_ERR_INVALID_ARG); led_bar_aw2013_cmd_t cmd = { .instance = handle, .blink_time = time, .blink_time_period = period }; if (xQueueSend(aw2013_que, &cmd, portMAX_DELAY) != pdPASS) { ESP_LOGE(TAG, "Fail to set blink time"); return ESP_FAIL; } return ESP_OK; } void led_bar_aw2013_deinit(esp_periph_handle_t handle) { AUDIO_NULL_CHECK(TAG, handle, return); _task_run = false; if (g_event_bit) { xEventGroupWaitBits(g_event_bit, DESTROY_BIT, pdTRUE, pdFALSE, portMAX_DELAY); vEventGroupDelete(g_event_bit); g_event_bit = NULL; } if (aw2013_que) { vQueueDelete(aw2013_que); aw2013_que = NULL; } esp_periph_stop(handle); esp_periph_destroy(handle); }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_bar/led_bar_aw2013.c
C
apache-2.0
10,728
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 <string.h> #include "esp_types.h" #include "esp_log.h" #include "led_bar_is31x.h" #include "periph_is31fl3216.h" static char *TAG = "LED_BAR_IS31X"; esp_periph_handle_t led_bar_is31x_init(void) { periph_is31fl3216_cfg_t is31fl3216_cfg = { 0 }; is31fl3216_cfg.state = IS31FL3216_STATE_ON; esp_periph_handle_t handle = periph_is31fl3216_init(&is31fl3216_cfg); if (handle == NULL) { return NULL; } esp_periph_init(handle); for (int i = 0; i < BLUE_LED_MAX_NUM; i++) { periph_is31fl3216_set_duty(handle, i, 255); } return handle; } esp_err_t led_bar_is31x_pattern(void *handle, int pat, int value) { esp_err_t ret = ESP_OK; if (handle == NULL) { ESP_LOGE(TAG, "led_bar_is31x_pattern instance has not initialized"); return ESP_FAIL; } esp_periph_handle_t h = (esp_periph_handle_t)handle; switch (pat) { case DISPLAY_PATTERN_WIFI_SETTING: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_duty_step(h, 20); periph_is31fl3216_set_interval(h, 10); periph_is31fl3216_set_act_time(h, 0); periph_is31fl3216_set_state(h, IS31FL3216_STATE_FLASH); break; } case DISPLAY_PATTERN_WIFI_CONNECTTING: { periph_is31fl3216_set_light_on_num(h, 1, BLUE_LED_MAX_NUM); periph_is31fl3216_set_interval(h, 100); periph_is31fl3216_set_shift_mode(h, PERIPH_IS31_SHIFT_MODE_ACC); periph_is31fl3216_set_state(h, IS31FL3216_STATE_SHIFT); break; } case DISPLAY_PATTERN_WIFI_CONNECTED: { periph_is31fl3216_set_light_on_num(h, 2, BLUE_LED_MAX_NUM); periph_is31fl3216_set_interval(h, 150); periph_is31fl3216_set_shift_mode(h, PERIPH_IS31_SHIFT_MODE_SINGLE); periph_is31fl3216_set_act_time(h, 2500); periph_is31fl3216_set_state(h, IS31FL3216_STATE_SHIFT); break; } case DISPLAY_PATTERN_WIFI_DISCONNECTED: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_duty_step(h, 20); periph_is31fl3216_set_interval(h, 10); periph_is31fl3216_set_act_time(h, 0); periph_is31fl3216_set_state(h, IS31FL3216_STATE_FLASH); break; } break; case DISPLAY_PATTERN_WIFI_SETTING_FINISHED: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_OFF); break; } case DISPLAY_PATTERN_BT_CONNECTTING: break; case DISPLAY_PATTERN_BT_CONNECTED: break; case DISPLAY_PATTERN_BT_DISCONNECTED: break; case DISPLAY_PATTERN_RECORDING_START: { break; } case DISPLAY_PATTERN_RECORDING_STOP: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_OFF); break; } case DISPLAY_PATTERN_RECOGNITION_START: { periph_is31fl3216_set_light_on_num(h, 1, BLUE_LED_MAX_NUM); periph_is31fl3216_set_interval(h, 150); periph_is31fl3216_set_shift_mode(h, PERIPH_IS31_SHIFT_MODE_SINGLE); periph_is31fl3216_set_state(h, IS31FL3216_STATE_SHIFT); break; } case DISPLAY_PATTERN_RECOGNITION_STOP: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_OFF); break; } case DISPLAY_PATTERN_WAKEUP_ON: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_ON); break; } break; case DISPLAY_PATTERN_WAKEUP_FINISHED: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_OFF); break; } case DISPLAY_PATTERN_MUSIC_ON: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_BY_AUDIO); break; } case DISPLAY_PATTERN_MUSIC_FINISHED: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_OFF); break; } case DISPLAY_PATTERN_VOLUME: break; case DISPLAY_PATTERN_TURN_ON: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_ON); break; } break; case DISPLAY_PATTERN_TURN_OFF: { int bits_mask = ((1 << BLUE_LED_MAX_NUM) - 1); periph_is31fl3216_set_blink_pattern(h, bits_mask); periph_is31fl3216_set_state(h, IS31FL3216_STATE_OFF); break; } break; default: ESP_LOGW(TAG, "The mode is invalid"); break; } return ret; } void led_bar_is31x_deinit(esp_periph_handle_t handle) { AUDIO_NULL_CHECK(TAG, handle, return); esp_periph_destroy(handle); }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_bar/led_bar_is31x.c
C
apache-2.0
7,603
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "driver/gpio.h" #include "esp_log.h" #include "board.h" #include "led_bar_ws2812.h" #include "audio_mem.h" #include "periph_ws2812.h" #include "string.h" static const char *TAG = "LED_BAR_WS2812"; typedef struct led_bar_ws2812_impl { esp_periph_handle_t periph_handle; periph_ws2812_ctrl_cfg_t *control_cfg; uint32_t led_num; } led_bar_ws2812_impl_t; /** * This function is weak-linked, so you can implement your own version to custom display pattern. */ void __attribute__((weak)) ws2812_pattern_copy(struct periph_ws2812_ctrl_cfg *p) { ESP_LOGW(TAG, "Default %s has been called, %s", __func__, __FILE__); } led_bar_ws2812_handle_t led_bar_ws2812_init(gpio_num_t gpio_num, int led_num) { led_bar_ws2812_impl_t *impl = audio_calloc(1, sizeof(led_bar_ws2812_impl_t)); AUDIO_MEM_CHECK(TAG, impl, return NULL); impl->led_num = led_num; periph_ws2812_cfg_t ws2812_cfg = { .gpio_num = gpio_num, .led_num = led_num, }; impl->control_cfg = audio_malloc(sizeof(periph_ws2812_ctrl_cfg_t[impl->led_num]) * DISPLAY_PATTERN_MAX); AUDIO_MEM_CHECK(TAG, impl->control_cfg, return NULL); ws2812_pattern_copy(impl->control_cfg); impl->periph_handle = periph_ws2812_init(&ws2812_cfg); esp_periph_init(impl->periph_handle); return impl; } esp_err_t led_bar_ws2812_pattern(void *handle, int pat, int value) { AUDIO_NULL_CHECK(TAG, handle, return ESP_FAIL); led_bar_ws2812_handle_t h = (led_bar_ws2812_handle_t)handle; ESP_LOGD(TAG, "pat:%d", pat); if (pat == DISPLAY_PATTERN_VOLUME) { periph_ws2812_ctrl_cfg_t *cfg = (h->control_cfg + h->led_num * pat); int on_n = (value * h->led_num) / 100; // volume range is 0-100 for (int i = 0; i < on_n; ++i) { cfg->color = LED2812_COLOR_WHITE; cfg ++; } } periph_ws2812_control(h->periph_handle, (h->control_cfg + h->led_num * pat), NULL); return ESP_OK; } esp_err_t led_bar_ws2812_deinit(led_bar_ws2812_handle_t handle) { AUDIO_NULL_CHECK(TAG, handle, return ESP_FAIL); esp_periph_destroy(handle->periph_handle); if (handle->control_cfg) { audio_free(handle->control_cfg); handle->control_cfg = NULL; } audio_free(handle); return ESP_OK; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_bar/led_bar_ws2812.c
C
apache-2.0
3,561
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _LED_INDICATOR_H_ #define _LED_INDICATOR_H_ #include "display_service.h" #ifdef __cplusplus extern "C" { #endif typedef struct led_indicator_impl *led_indicator_handle_t; /** * @brief Initialize led_indicator_handle_t instance * * @param num led gpio number * * @return * - NULL, Fail * - Others, Success */ led_indicator_handle_t led_indicator_init(gpio_num_t num); /** * @brief Set led indicator display pattern. * * @param handle led indicator instance * @param pat display pattern * @param value value of pattern * * @return * - ESP_OK * - ESP_FAIL */ esp_err_t led_indicator_pattern(void *handle, int pat, int value); /** * @brief Destroy led_indicator_handle_t instance * * @param handle led indicator instance * * @return * - ESP_OK * - ESP_FAIL */ void led_indicator_deinit(led_indicator_handle_t handle); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_indicator/include/led_indicator.h
C
apache-2.0
2,203
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "periph_led.h" #include "esp_log.h" #include "led_indicator.h" #include "driver/gpio.h" #include "audio_mem.h" static char *TAG = "LED_INDI"; typedef struct led_indicator_impl { gpio_num_t gpio_num; esp_periph_handle_t periph_handle; } led_indicator_impl_t; led_indicator_handle_t led_indicator_init(gpio_num_t num) { led_indicator_impl_t *impl = audio_calloc(1, sizeof(led_indicator_impl_t)); AUDIO_MEM_CHECK(TAG, impl, return NULL); impl->gpio_num = num; periph_led_cfg_t led_cfg = { .led_speed_mode = LEDC_LOW_SPEED_MODE, .led_duty_resolution = LEDC_TIMER_10_BIT, .led_timer_num = LEDC_TIMER_0, .led_freq_hz = 10000, }; impl->periph_handle = periph_led_init(&led_cfg); esp_periph_init(impl->periph_handle); return impl; } esp_err_t led_indicator_pattern(void *handle, int pat, int value) { AUDIO_NULL_CHECK(TAG, handle, return ESP_FAIL); led_indicator_handle_t h = (led_indicator_handle_t)handle; ESP_LOGD(TAG, "pat:%d, gpio:%d", pat, h->gpio_num); switch (pat) { case DISPLAY_PATTERN_WIFI_SETTING: periph_led_blink(h->periph_handle, h->gpio_num, 500, 500, false, -1, value); break; case DISPLAY_PATTERN_WIFI_CONNECTED: periph_led_blink(h->periph_handle, h->gpio_num, 1000, 200, true, 10, value); break; case DISPLAY_PATTERN_WIFI_DISCONNECTED: periph_led_blink(h->periph_handle, h->gpio_num, 200, 500, false, -1, value); break; case DISPLAY_PATTERN_TURN_ON: case DISPLAY_PATTERN_WAKEUP_ON: periph_led_blink(h->periph_handle, h->gpio_num, 100, 0, false, -1, value); break; case DISPLAY_PATTERN_TURN_OFF: case DISPLAY_PATTERN_WAKEUP_FINISHED: case DISPLAY_PATTERN_SPEECH_OVER: periph_led_blink(h->periph_handle, h->gpio_num, 0, 100, false, -1, value); break; case DISPLAY_PATTERN_SPEECH_BEGIN: periph_led_blink(h->periph_handle, h->gpio_num, 500, 500, false, -1, value); break; default: ESP_LOGW(TAG, "The led mode is invalid"); break; } return ESP_OK; } void led_indicator_deinit(led_indicator_handle_t handle) { AUDIO_NULL_CHECK(TAG, handle, return); esp_periph_destroy (handle->periph_handle); }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/led_indicator/led_indicator.c
C
apache-2.0
3,620
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/test/component.mk
Makefile
apache-2.0
87
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "esp_log.h" #include "board.h" #include "audio_mem.h" #include "display_service.h" #include "periph_service.h" #include "led_bar_is31x.h" #include "led_indicator.h" #include "led_bar_ws2812.h" #include "led_bar_aw2013.h" #include "unity.h" static const char *TAG = "test_display_service"; /* * Usage of display service */ TEST_CASE("Create a display service and set different pattern", "[display_service]") { display_service_handle_t display_handle = audio_board_led_init(); TEST_ASSERT_NOT_NULL(display_handle); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi disconnected"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wakeup on"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WAKEUP_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wakeup finish"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WAKEUP_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "speech begin"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_SPEECH_BEGIN, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "speech finish"); TEST_ASSERT_FALSE(display_service_set_pattern(display_handle, DISPLAY_PATTERN_WAKEUP_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); TEST_ASSERT_FALSE(display_destroy(display_handle)); } /* * When there is no led display chip, such as Lyrat-4.3, Lyrat-mini, we use "led_indicator" to control leds on board. * To run this case, please choose Lyrat-4.3 or Lyrat-mini. */ TEST_CASE("Create a display service on board without is31flXXX chip", "[display_service]") { led_indicator_handle_t led_handle = led_indicator_init((gpio_num_t)get_green_led_gpio()); TEST_ASSERT_NOT_NULL(led_handle); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi disconnected"); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); led_indicator_deinit(led_handle); } /* * When there is a led display chip, such as MSC_V2.1, MSC_V2.2, we use "led_bar" to control the leds on board * To run this case, please choose MSC_V2.1 or MSC_V2.2 */ TEST_CASE("Create a display service on board with an is31flXXX chip", "[display_service]") { esp_periph_handle_t led_handle = led_bar_is31x_init();; TEST_ASSERT_NOT_NULL(led_handle); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi disconnected"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); led_bar_is31x_deinit(led_handle); } /* * When there are both led display chip and led controled by gpio on the board, run this case to check function. */ TEST_CASE("Use both led bar and led indicator", "[display_service]") { esp_periph_handle_t led_bar_handle = led_bar_is31x_init(); TEST_ASSERT_NOT_NULL(led_bar_handle); led_indicator_handle_t led_handle = led_indicator_init((gpio_num_t)get_green_led_gpio()); TEST_ASSERT_NOT_NULL(led_handle); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi disconnected"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_FALSE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); TEST_ASSERT_FALSE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); led_bar_is31x_deinit(led_bar_handle); led_indicator_deinit(led_handle); } TEST_CASE("Test when memory is not enough", "[display_service]") { void *pt = NULL; do { pt = audio_calloc(1, 1); } while (pt); ESP_LOGW(TAG, "The memory has been run out, enter test ..."); esp_periph_handle_t led_bar_handle = led_bar_is31x_init(); TEST_ASSERT_NULL(led_bar_handle); led_indicator_handle_t led_handle = led_indicator_init((gpio_num_t)get_green_led_gpio()); TEST_ASSERT_NULL(led_handle); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_TRUE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); TEST_ASSERT_TRUE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_TRUE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); TEST_ASSERT_TRUE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_TRUE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); TEST_ASSERT_TRUE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi disconnected"); TEST_ASSERT_TRUE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); TEST_ASSERT_TRUE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_TRUE(led_bar_is31x_pattern(led_bar_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); TEST_ASSERT_TRUE(led_indicator_pattern(led_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); led_bar_is31x_deinit(led_bar_handle); led_indicator_deinit(led_handle); } /* * When there is a ws2812 display chip * To run this case, please choose DUL1906_D v1.0 */ TEST_CASE("Create a display service on board with an ws2812 chip", "[display_service]") { led_bar_ws2812_handle_t ws2812_handle = led_bar_ws2812_init((gpio_num_t)get_ws2812_gpio_pin(), (int)2); TEST_ASSERT_NOT_NULL(ws2812_handle); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "bt connectting"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_BT_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "bt connected"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_BT_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "bt disconnected"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_BT_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recording start"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_RECORDING_START, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recording stop"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_RECORDING_STOP, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recotnition start"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_RECOGNITION_START, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recotnition stop"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_RECOGNITION_STOP, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wake up on"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WAKEUP_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wake up finished"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WAKEUP_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "music on"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_MUSIC_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "music finised"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_MUSIC_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "volume"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_VOLUME, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "mute off"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_MUTE_OFF , 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "mute on"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_MUTE_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "turn on"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_TURN_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "turn off"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_TURN_OFF, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "battery low"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_BATTERY_LOW, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "battery charging"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_BATTERY_CHARGING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "battery full"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_BATTERY_FULL, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "poweron init"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_POWERON_INIT, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi no cfg"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_WIFI_NO_CFG, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "speech begin"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_SPEECH_BEGIN, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "speech over"); TEST_ASSERT_FALSE(led_bar_ws2812_pattern(ws2812_handle, DISPLAY_PATTERN_SPEECH_OVER, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); led_bar_ws2812_deinit(ws2812_handle); } /* * When there is a aw2013 display chip * To run this case, please choose cpuid board */ TEST_CASE("Create a display service on board with an aw2013 chip", "[display_service]") { esp_periph_handle_t aw2013_handle = led_bar_aw2013_init(); TEST_ASSERT_NOT_NULL(aw2013_handle); TEST_ASSERT_FALSE(led_bar_aw2013_set_blink_time(aw2013_handle, 5, 300)); display_service_config_t display = { .based_cfg = { .task_stack = 3 * 1024, .task_prio = 5, .task_core = 0, .task_func = aw2013_led_bar_task, .service_start = NULL, .service_stop = NULL, .service_destroy = NULL, .service_ioctl = led_bar_aw2013_pattern, .service_name = "aw2013 display server", .user_data = NULL }, .instance = aw2013_handle, }; display_service_handle_t disp_serv = display_service_create(&display); TEST_ASSERT_NOT_NULL(disp_serv); ESP_LOGI(TAG, "wifi setting"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_SETTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connectting"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi connected"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wifi setting finished"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_SETTING_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "bt connectting"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_BT_CONNECTTING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "bt connected"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_BT_CONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "bt disconnected"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_BT_DISCONNECTED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recording start"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_RECORDING_START, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recording stop"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_RECORDING_STOP, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recotnition start"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_RECOGNITION_START, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "recotnition stop"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_RECOGNITION_STOP, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wake up on"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WAKEUP_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "wake up finished"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WAKEUP_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "music on"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_MUSIC_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "music finised"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_MUSIC_FINISHED, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "volume"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_VOLUME, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "mute off"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_MUTE_OFF , 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "mute on"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_MUTE_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "turn on"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_TURN_ON, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "turn off"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_TURN_OFF, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "battery low"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_BATTERY_LOW, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "battery charging"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_BATTERY_CHARGING, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "battery full"); TEST_ASSERT_FALSE(display_service_set_pattern(disp_serv, DISPLAY_PATTERN_BATTERY_FULL, 0)); vTaskDelay(2000 / portTICK_PERIOD_MS); ESP_LOGW(TAG, "Display service will be destroyed"); TEST_ASSERT_FALSE(display_destroy(disp_serv)); led_bar_aw2013_deinit(aw2013_handle); }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/display_service/test/test_display_service.c
C
apache-2.0
20,543
set(COMPONENT_ADD_INCLUDEDIRS include) # Edit following two lines to set component requirements (see docs) set(COMPONENT_REQUIRES ) set(COMPONENT_PRIV_REQUIRES clouds mbedtls audio_board esp-adf-libs audio_hal audio_sal esp_peripherals) set(COMPONENT_SRCS ./dueros_service.c ./esp_audio_device_info.c) set(COMPONENT_EMBED_FILES duer_profile) register_component()
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/CMakeLists.txt
CMake
apache-2.0
367
# # Main Makefile. This is basically the same as a component makefile. COMPONENT_ADD_INCLUDEDIRS := include COMPONENT_SRCDIRS := . COMPONENT_EMBED_TXTFILES := duer_profile
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/component.mk
Makefile
apache-2.0
172
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "dueros_esp_flash.h" #include "esp_partition.h" #include "audio_mem.h" #include "esp_log.h" #define MAX_FLASH_ALERT_COUNT 30 // 4k Byte erase size (12 bits), for erase. #define FLASH_SECTOR_ALIGN_BITS 12 // 4 Byte page size (2 bits), for write. #define FLASH_PAGE_ALIGN_BITS 8 // 4 byte (2 bits), for read flash data. #define FLASH_WORD_ALIGN_BITS 2 static const char *TAG = "DUER_FLASH"; static const duer_flash_config_t s_flash_config = { FLASH_SECTOR_ALIGN_BITS, FLASH_PAGE_ALIGN_BITS, FLASH_WORD_ALIGN_BITS, }; static const char *s_parti_label_list[] = { "alert", }; static duer_flash_string_context_t s_ctx_list[] = { {{NULL, 0, NULL}, MAX_FLASH_ALERT_COUNT, NULL}, }; duer_flash_string_context_t *duer_flash_string_init(duer_flash_module module) { int i = 0; const char *label = s_parti_label_list[module]; duer_flash_string_context_t *rt = &s_ctx_list[module]; duer_set_flash_config(&s_flash_config); if (module >= MAX_FLASH_MODULE) { ESP_LOGE(TAG, "Invalid flash module"); return NULL; } if (!label) { ESP_LOGE(TAG, "Invalid partition label."); return NULL; } rt->ctx.handle = (void *)esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, label); if (!rt->ctx.handle) { ESP_LOGE(TAG, "cannot find partition label %s.", label); return NULL; } rt->ctx.len = ((esp_partition_t *)(rt->ctx.handle))->size; if (!rt->ele_list) { rt->ele_list = (uint32_t *)audio_malloc(sizeof(uint32_t) * rt->max_ele_count); for (i = 0; i < rt->max_ele_count; ++i) { rt->ele_list[i] = FLASH_INVALID_ADDR; } } return rt; } duer_status_t duer_esp_flash_init(const char *label, duer_flash_context_t *ctx, duer_flash_config_t *config) { if (!label || !ctx || !config) { return DUER_ERR_INVALID_PARAMETER; } ctx->handle = (void *)esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, label); if (!ctx->handle) { ESP_LOGE(TAG, "cannot find partition label %s.", label); return DUER_ERR_FAILED; } ctx->len = ((esp_partition_t *)(ctx->handle))->size; memcpy(config, &s_flash_config, sizeof(*config)); return DUER_OK; } int duer_flash_read( duer_flash_context_t *ctx, unsigned int offset, void *buf, unsigned int len) { return esp_partition_read((esp_partition_t *)(ctx->handle), offset, buf, len); } int duer_flash_write( duer_flash_context_t *ctx, unsigned int offset, void *buf, unsigned int len) { return esp_partition_write((esp_partition_t *)(ctx->handle), offset, buf, len); } int duer_flash_erase( duer_flash_context_t *ctx, unsigned int offset, unsigned int len) { return esp_partition_erase_range((esp_partition_t *)(ctx->handle), offset, len); }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/dueros_esp_flash.c
C
apache-2.0
4,269
/* * ESPRESSIF MIT License * * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 <stdarg.h> #include <string.h> #include <stdlib.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" #include "freertos/event_groups.h" #include "esp_audio_device_info.h" #include "lightduer_ota_notifier.h" #include "lightduer_voice.h" #include "lightduer_connagent.h" #include "lightduer_dcs.h" #include "board.h" #include "sdkconfig.h" #include "audio_mem.h" #include "dueros_service.h" #include "recorder_engine.h" #include "esp_audio.h" #include "esp_log.h" #define DUEROS_TASK_PRIORITY 5 #define DUEROS_TASK_STACK_SIZE 6*1024 #define RECORD_SAMPLE_RATE (16000) #define RECORD_DEBUG 0 static const char *TAG = "DUEROS"; static bool duer_login_success; extern const uint8_t _duer_profile_start[] asm("_binary_duer_profile_start"); extern const uint8_t _duer_profile_end[] asm("_binary_duer_profile_end"); typedef enum { DUER_CMD_UNKNOWN, DUER_CMD_LOGIN, DUER_CMD_CONNECTED, DUER_CMD_START, DUER_CMD_STOP, DUER_CMD_QUIT, DUER_CMD_DESTROY, } duer_task_cmd_t; typedef struct { xQueueHandle duer_que; service_state_t duer_state; } dueros_service_t; typedef struct { duer_task_cmd_t type; uint32_t *pdata; int index; int len; } duer_task_msg_t; static audio_service_handle_t duer_serv_handle = NULL; static void duer_que_send(void *que, duer_task_cmd_t type, void *data, int index, int len, int dir) { duer_task_msg_t evt = {0}; evt.type = type; evt.pdata = data; evt.index = index; evt.len = len; if (dir) { xQueueSendToFront(que, &evt, 0) ; } else { xQueueSend(que, &evt, 0); } } static void report_info_task(void *pvParameters) { int ret; ret = duer_report_device_info(); if (ret != DUER_OK) { ESP_LOGE(TAG, "Report device info failed ret:%d", ret); } vTaskDelete(NULL); } static void duer_dcs_init(void) { static bool is_first_time = true; ESP_LOGI(TAG, "duer_dcs_init"); duer_dcs_framework_init(); duer_dcs_voice_input_init(); duer_dcs_voice_output_init(); duer_dcs_speaker_control_init(); duer_dcs_audio_player_init(); if (is_first_time) { is_first_time = false; duer_dcs_sync_state(); } } static void duer_event_hook(duer_event_t *event) { if (!event) { ESP_LOGE(TAG, "NULL event!!!"); return; } ESP_LOGE(TAG, "event: %d", event->_event); switch (event->_event) { case DUER_EVENT_STARTED: // Initialize the DCS API duer_dcs_init(); dueros_service_t *serv = audio_service_get_data(duer_serv_handle); duer_que_send(serv->duer_que, DUER_CMD_CONNECTED, NULL, 0, 0, 0); duer_login_success = true; ESP_LOGE(TAG, "event: DUER_EVENT_STARTED"); xTaskCreate(&report_info_task, "report_info_task", 1024 * 2, NULL, 5, NULL); break; case DUER_EVENT_STOPPED: ESP_LOGE(TAG, "event: DUER_EVENT_STOPPED"); duer_login_success = false; audio_service_disconnect(duer_serv_handle); break; } } static void duer_login(void) { int sz = _duer_profile_end - _duer_profile_start; char *data = audio_calloc_inner(1, sz); if (NULL == data) { ESP_LOGE(TAG, "audio_malloc failed"); return; } memcpy(data, _duer_profile_start, sz); ESP_LOGI(TAG, "duer_start, len:%d\n%s", sz, data); duer_start(data, sz); audio_free((void *)data); } static void dueros_task(void *pvParameters) { /** We get a delay here because of a known issue, or there will be a crash*/ vTaskDelay(1000 / portTICK_PERIOD_MS); audio_service_handle_t serv_handle = (audio_service_handle_t)pvParameters; dueros_service_t *serv = audio_service_get_data(serv_handle); duer_initialize(); duer_set_event_callback(duer_event_hook); duer_init_device_info(); uint8_t *voiceData = audio_calloc(1, REC_ONE_BLOCK_SIZE); if (NULL == voiceData) { ESP_LOGE(TAG, "Func:%s, Line:%d, Malloc failed", __func__, __LINE__); goto dueros_task_fail; } static duer_task_msg_t duer_msg; FILE *file = NULL; #if RECORD_DEBUG file = fopen("/sdcard/rec_adf_1.wav", "w+"); if (NULL == file) { ESP_LOGW(TAG, "open rec_adf_1.wav failed,[%d]", __LINE__); } #endif int task_run = 1; serv->duer_state = SERVICE_STATE_IDLE; service_event_t serv_evt = { .type = 0, .source = serv, .data = &serv->duer_state, .len = 0, }; while (task_run) { if (xQueueReceive(serv->duer_que, &duer_msg, portMAX_DELAY)) { if (duer_msg.type == DUER_CMD_LOGIN) { ESP_LOGE(TAG, "Recv Que DUER_CMD_LOGIN"); if (serv->duer_state == SERVICE_STATE_IDLE) { duer_login(); serv->duer_state = SERVICE_STATE_CONNECTING; audio_service_callback(serv_handle, &serv_evt); } else { ESP_LOGW(TAG, "DUER_CMD_LOGIN connecting,duer_state = %d", serv->duer_state); } } else if (duer_msg.type == DUER_CMD_CONNECTED) { ESP_LOGI(TAG, "Dueros DUER_CMD_CONNECTED, duer_state:%d", serv->duer_state); serv->duer_state = SERVICE_STATE_CONNECTED; audio_service_callback(serv_handle, &serv_evt); } else if (duer_msg.type == DUER_CMD_START) { if (serv->duer_state < SERVICE_STATE_CONNECTED) { ESP_LOGW(TAG, "Dueros has not connected, state:%d", serv->duer_state); continue; } ESP_LOGI(TAG, "Recv Que DUER_CMD_START"); duer_voice_start(RECORD_SAMPLE_RATE); duer_dcs_on_listen_started(); serv->duer_state = SERVICE_STATE_RUNNING; audio_service_callback(serv_handle, &serv_evt); while (1) { int ret = rec_engine_data_read(voiceData, REC_ONE_BLOCK_SIZE, 110 / portTICK_PERIOD_MS); ESP_LOGD(TAG, "index = %d", ret); if ((ret == 0) || (ret == -1)) { break; } if (file) { fwrite(voiceData, 1, REC_ONE_BLOCK_SIZE, file); } ret = duer_voice_send(voiceData, REC_ONE_BLOCK_SIZE); if (ret < 0) { ESP_LOGE(TAG, "duer_voice_send failed ret:%d", ret); break; } } } else if (duer_msg.type == DUER_CMD_STOP) { ESP_LOGI(TAG, "Dueros DUER_CMD_STOP"); if (file) { fclose(file); } duer_voice_stop(); serv->duer_state = SERVICE_STATE_STOPPED; audio_service_callback(serv_handle, &serv_evt); } else if (duer_msg.type == DUER_CMD_QUIT && (serv->duer_state != SERVICE_STATE_IDLE)) { if (duer_login_success) { duer_stop(); } serv->duer_state = SERVICE_STATE_IDLE; audio_service_callback(serv_handle, &serv_evt); } else if (duer_msg.type == DUER_CMD_DESTROY) { ESP_LOGI(TAG, "Dueros DUER_CMD_DESTROY"); duer_voice_stop(); serv->duer_state = SERVICE_STATE_IDLE; task_run = false; } } } dueros_task_fail: if (voiceData) { free(voiceData); } vQueueDelete(serv->duer_que); audio_free(serv); vTaskDelete(NULL); } esp_err_t dueros_start(audio_service_handle_t handle) { dueros_service_t *serv = audio_service_get_data(handle); duer_que_send(serv->duer_que, DUER_CMD_START, NULL, 0, 0, 0); return ESP_OK; } esp_err_t dueros_stop(audio_service_handle_t handle) { dueros_service_t *serv = audio_service_get_data(handle); duer_que_send(serv->duer_que, DUER_CMD_STOP, NULL, 0, 0, 0); return ESP_OK; } esp_err_t dueros_connect(audio_service_handle_t handle) { dueros_service_t *serv = audio_service_get_data(handle); duer_que_send(serv->duer_que, DUER_CMD_LOGIN, NULL, 0, 0, 0); return ESP_OK; } esp_err_t dueros_disconnect(audio_service_handle_t handle) { dueros_service_t *serv = audio_service_get_data(handle); duer_que_send(serv->duer_que, DUER_CMD_QUIT, NULL, 0, 0, 0); return ESP_OK; } esp_err_t dueros_destroy(audio_service_handle_t handle) { dueros_service_t *serv = audio_service_get_data(handle); duer_que_send(serv->duer_que, DUER_CMD_DESTROY, NULL, 0, 0, 0); return ESP_OK; } service_state_t dueros_service_state_get() { dueros_service_t *serv = audio_service_get_data(duer_serv_handle); return serv->duer_state; } audio_service_handle_t dueros_service_create(void) { dueros_service_t *serv = audio_calloc(1, sizeof(dueros_service_t)); serv->duer_que = xQueueCreate(3, sizeof(duer_task_msg_t)); serv->duer_state = SERVICE_STATE_UNKNOWN; audio_service_config_t duer_cfg = { .task_stack = DUEROS_TASK_STACK_SIZE, .task_prio = DUEROS_TASK_PRIORITY, .task_core = 1, .task_func = dueros_task, .service_start = dueros_start, .service_stop = dueros_stop, .service_connect = dueros_connect, .service_disconnect = dueros_disconnect, .service_destroy = dueros_destroy, .service_name = "duer_serv", .user_data = serv, }; audio_service_handle_t duer = audio_service_create(&duer_cfg); duer_serv_handle = duer; return duer; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/dueros_service.c
C
apache-2.0
11,443
/* * ESPRESSIF MIT License * * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "esp_log.h" #include "lightduer_types.h" #include "lightduer_dev_info.h" static const char* TAG="ESP_DEV_INFO"; #define FIRMWARE_VERSION "1.0.0.0" const duer_system_static_info_t g_system_static_info = { .os_version = "FreeRTOS V8.2.0", .sw_version = "ESP32_LYRAT_"FIRMWARE_VERSION, .brand = "ESPRESSIF", .hardware_version = "ESP32", .equipment_type = "ESP32_LYRAT", .ram_size = 5120, .rom_size = 4096, }; static int get_firmware_version(char *firmware_version) { strncpy(firmware_version, FIRMWARE_VERSION, FIRMWARE_VERSION_LEN); return 0; } static struct DevInfoOps dev_info_ops = { .get_firmware_version = get_firmware_version, }; int duer_init_device_info(void) { int ret = 0; ret = duer_register_device_info_ops(&dev_info_ops); if (ret != 0) { ESP_LOGE(TAG, "Dev Info: Register dev ops failed"); } return ret; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/esp_audio_device_info.c
C
apache-2.0
2,191
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _DUEROS_SERVICE_ESP_FLASH_H #define _DUEROS_SERVICE_ESP_FLASH_H #include "lightduer_types.h" #include "lightduer_flash_strings.h" #ifdef __cplusplus extern "C" { #endif /* * @brief Initialize flash partition for alert service * * @param[in] module The audio service instance * * @return * - NULL, Fail * - Others, Success */ duer_flash_string_context_t *duer_flash_string_init(duer_flash_module module); /* * @brief Initialize flash partition for dueros service * * @param[in] label A pointer to partition label * @param[in] ctx A pointer to `duer_flash_context_t` * @param[in] config A pointer to `duer_flash_config_t` * * @return * - ESP_OK, Success * - Others, Fail */ duer_status_t duer_esp_flash_init(const char *label, duer_flash_context_t *ctx, duer_flash_config_t *config); #ifdef __cplusplus } #endif #endif /* _DUEROS_SERVICE_ESP_FLASH_H */
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/include/dueros_esp_flash.h
C
apache-2.0
2,150
/* * ESPRESSIF MIT License * * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _DUEROS_SERVICE_H_ #define _DUEROS_SERVICE_H_ #include "audio_service.h" #ifdef __cplusplus extern "C" { #endif /* * @brief Create the dueros service * * @return * - NULL, Fail * - Others, Success */ audio_service_handle_t dueros_service_create(void); /* * @brief Get dueros service state * * @return The state of service * */ service_state_t dueros_service_state_get(); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/include/dueros_service.h
C
apache-2.0
1,674
/* * ESPRESSIF MIT License * * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _ESP_ADUIO_DEVICE_INFO_H #define _ESP_ADUIO_DEVICE_INFO_H #ifdef __cplusplus extern "C" { #endif /* * Initialise Device Information * * @param void: * * @return int: Success: 0 * Failed: Other */ extern int duer_init_device_info(void); #ifdef __cplusplus } #endif #endif // _ESP_ADUIO_DEVICE_INFO_H
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/dueros_service/include/esp_audio_device_info.h
C
apache-2.0
1,566
# Set the supported library variable for different chips IF (CONFIG_IDF_TARGET STREQUAL "esp32") set(recorder_engine_name "recorder_engine") ELSEIF (CONFIG_IDF_TARGET STREQUAL "esp32s2") set(recorder_engine_name "") ELSEIF (CONFIG_IDF_TARGET STREQUAL "esp32s3") set(recorder_engine_name "recorder_engine") ELSEIF (CONFIG_IDF_TARGET STREQUAL "esp32c3") set(recorder_engine_name "") ENDIF () # Add the source code and header file list(APPEND COMPONENT_ADD_INCLUDEDIRS ./esp_audio/include ./esp_codec/include/codec ./esp_codec/include/processing ./esp_upnp/include ./esp_ssdp/include ./esp_sip/include ./recorder_engine/include ./mrm_client/include) list(APPEND COMPONENT_SRCS ./esp_codec/audio_alc.c ./esp_codec/audio_sonic.c ./esp_codec/audio_forge.c ./esp_codec/downmix.c ./esp_codec/equalizer.c ./esp_codec/filter_resample.c ./esp_codec/wav_encoder.c) IF (NOT (recorder_engine_name STREQUAL "")) list(APPEND COMPONENT_ADD_INCLUDEDIRS ./audio_misc/include) list(APPEND COMPONENT_SRCS ./audio_misc/rec_eng_helper.c) if (CONFIG_REC_ENG_ENABLE_VAD_ONLY) set(target_recorder_engine "recorder_engine_vad") elseif (CONFIG_REC_ENG_ENABLE_VAD_WWE) set(target_recorder_engine "recorder_engine_vad_wwe") else() set(target_recorder_engine "recorder_engine") endif() ELSE() set(target_recorder_engine "") ENDIF () set(COMPONENT_REQUIRES audio_pipeline audio_sal esp-sr nghttp tcp_transport) register_component() message(STATUS "Chip is ${CONFIG_IDF_TARGET}, ${CMAKE_CURRENT_SOURCE_DIR}") target_link_libraries(${COMPONENT_TARGET} "-L ${CMAKE_CURRENT_SOURCE_DIR}/esp_audio/lib/${CONFIG_IDF_TARGET}" "-L ${CMAKE_CURRENT_SOURCE_DIR}/esp_codec/lib/${CONFIG_IDF_TARGET}" "-L ${CMAKE_CURRENT_SOURCE_DIR}/esp_ssdp/lib/${CONFIG_IDF_TARGET}" "-L ${CMAKE_CURRENT_SOURCE_DIR}/esp_upnp/lib/${CONFIG_IDF_TARGET}" "-L ${CMAKE_CURRENT_SOURCE_DIR}/esp_sip/lib/${CONFIG_IDF_TARGET}" "-L ${CMAKE_CURRENT_SOURCE_DIR}/mrm_client/lib/${CONFIG_IDF_TARGET}" "-L ${CMAKE_CURRENT_SOURCE_DIR}/recorder_engine/lib/${CONFIG_IDF_TARGET}") # GREATER_EQUAL is only available on CMake +3.7. # To be totally compatible with older versions, let's use GREATER instead IF (IDF_VERSION_MAJOR GREATER 3) set(name_of_recorder_engine_lib "lib${target_recorder_engine}.a") add_prebuilt_library(esp_audio "${CMAKE_CURRENT_SOURCE_DIR}/esp_audio/lib/${CONFIG_IDF_TARGET}/libesp_audio.a" PRIV_REQUIRES esp-adf-libs) add_prebuilt_library(esp_codec "${CMAKE_CURRENT_SOURCE_DIR}/esp_codec/lib/${CONFIG_IDF_TARGET}/libesp_codec.a" PRIV_REQUIRES esp-adf-libs) add_prebuilt_library(esp_processing "${CMAKE_CURRENT_SOURCE_DIR}/esp_codec/lib/${CONFIG_IDF_TARGET}/libesp_processing.a" PRIV_REQUIRES esp-adf-libs) add_prebuilt_library(esp_sip "${CMAKE_CURRENT_SOURCE_DIR}/esp_sip/lib/${CONFIG_IDF_TARGET}/libesp_sip.a" PRIV_REQUIRES tcp_transport) add_prebuilt_library(recorder_engine "${CMAKE_CURRENT_SOURCE_DIR}/recorder_engine/lib/${CONFIG_IDF_TARGET}/${name_of_recorder_engine_lib}" PRIV_REQUIRES esp-adf-libs) target_link_libraries(${COMPONENT_TARGET} "-Wl,--start-group" esp_processing esp_audio esp_codec esp_ssdp esp_sip esp_upnp mrm_client ${target_recorder_engine} "-Wl,--end-group") ELSE () target_link_libraries(${COMPONENT_TARGET} "-Wl,--start-group" esp_processing esp_audio esp_codec esp_ssdp-v33 esp_upnp esp_sip-v33 ${target_recorder_engine} "-Wl,--end-group") ENDIF (IDF_VERSION_MAJOR GREATER 3)
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/CMakeLists.txt
CMake
apache-2.0
4,163
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _RECORDER_ENGINE_HELPER_H_ #define _RECORDER_ENGINE_HELPER_H_ #ifdef __cplusplus extern "C" { #endif /** * @breif Get a wake net interface * * @param[out] wakenet_iface The handle of wakenet interface * * @return void */ void get_wakenet_iface(esp_wn_iface_t **wakenet_iface); /** * @brief Get coefficient of the model * * @param model_coeff The handle of model coefficient * * @return void */ void get_wakenet_coeff(model_coeff_getter_t **model_coeff); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/audio_misc/include/rec_eng_helper.h
C
apache-2.0
1,769
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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 "esp_log.h" #include "esp_wn_iface.h" #include "sdkconfig.h" extern const esp_wn_iface_t esp_sr_wakenet3_quantized; extern const esp_wn_iface_t esp_sr_wakenet4_quantized; extern const esp_wn_iface_t esp_sr_wakenet5_quantized; extern const esp_wn_iface_t esp_sr_wakenet5_float; extern const esp_wn_iface_t esp_sr_wakenet6_quantized; void get_wakenet_iface(esp_wn_iface_t **wakenet_iface) { #if CONFIG_SR_MODEL_WN3_QUANT *wakenet_iface = (esp_wn_iface_t *)&esp_sr_wakenet3_quantized; #elif CONFIG_SR_MODEL_WN4_QUANT *wakenet_iface = (esp_wn_iface_t *)&esp_sr_wakenet4_quantized; #elif CONFIG_SR_MODEL_WN5_FLOAT *wakenet_iface = (esp_wn_iface_t *)&esp_sr_wakenet5_float; #elif CONFIG_SR_MODEL_WN5_QUANT *wakenet_iface = (esp_wn_iface_t *)&esp_sr_wakenet5_quantized; #elif CONFIG_SR_MODEL_WN6_QUANT *wakenet_iface = (esp_wn_iface_t *)&esp_sr_wakenet6_quantized; #else ESP_LOGE("RECORDER_ENG_HELPER", "No valid neural network model selected"); #endif } void get_wakenet_coeff(model_coeff_getter_t **model_coeff) { #if CONFIG_SR_WN3_HILEXIN #include "hilexin_wn3.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hilexin_wn3; #elif CONFIG_SR_WN4_HILEXIN #include "hilexin_wn4.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hilexin_wn4; #elif CONFIG_SR_WN5_HILEXIN & CONFIG_SR_MODEL_WN5_FLOAT #include "hilexin_wn5_float.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hilexin_wn5_float; #elif CONFIG_SR_WN5_HILEXIN & CONFIG_SR_MODEL_WN5_QUANT #include "hilexin_wn5.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hilexin_wn5; #elif CONFIG_SR_WN5X2_HILEXIN & CONFIG_SR_MODEL_WN5_QUANT #include "hilexin_wn5X2.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hilexin_wn5X2; #elif CONFIG_SR_WN5X3_HILEXIN & CONFIG_SR_MODEL_WN5_QUANT #include "hilexin_wn5X3.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hilexin_wn5X3; #elif CONFIG_SR_WN5_NIHAOXIAOZHI & CONFIG_SR_MODEL_WN5_QUANT #include "nihaoxiaozhi_wn5.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_nihaoxiaozhi_wn5; #elif CONFIG_SR_WN5X2_NIHAOXIAOZHI & CONFIG_SR_MODEL_WN5_QUANT #include "nihaoxiaozhi_wn5X2.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_nihaoxiaozhi_wn5X2; #elif CONFIG_SR_WN5X3_NIHAOXIAOZHI & CONFIG_SR_MODEL_WN5_QUANT #include "nihaoxiaozhi_wn5X3.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_nihaoxiaozhi_wn5X3; #elif CONFIG_SR_WN5X3_NIHAOXIAOXIN & CONFIG_SR_MODEL_WN5_QUANT #include "nihaoxiaoxin_wn5X3.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_nihaoxiaoxin_wn5X3; #elif CONFIG_SR_WN5X3_HIJESON & CONFIG_SR_MODEL_WN5_QUANT #include "hijeson_wn5X3.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_hijeson_wn5X3; #elif CONFIG_SR_WN6_NIHAOXIAOXIN #include "nihaoxiaoxin_wn6.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_nihaoxiaoxin_wn6; #elif CONFIG_SR_WN5_CUSTOMIZED_WORD #include "customized_word_wn5.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_customized_word_wn5; #elif CONFIG_SR_WN6_CUSTOMIZED_WORD #include "customized_word_wn6.h" *model_coeff = (model_coeff_getter_t *)&get_coeff_customized_word_wn6; #else ESP_LOGE("RECORDER_ENG_HELPER", "No valid wake word selected"); #endif }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/audio_misc/rec_eng_helper.c
C
apache-2.0
4,480
# "main" pseudo-component makefile. # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) COMPONENT_ADD_INCLUDEDIRS := esp_audio/include \ esp_codec/include/codec \ esp_codec/include/processing \ recorder_engine/include \ esp_ssdp/include \ esp_upnp/include \ esp_sip/include \ audio_misc/include COMPONENT_SRCDIRS := . esp_codec audio_misc LIBS := esp_processing esp_audio esp_codec esp_upnp ifdef CONFIG_REC_ENG_ENABLE_VAD_ONLY LIBS += recorder_engine_vad endif ifdef CONFIG_REC_ENG_ENABLE_VAD_WWE LIBS += recorder_engine_vad_wwe endif ifdef CONFIG_REC_ENG_ENABLE_VAD_WWE_AMR LIBS += recorder_engine endif COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_audio/lib/esp32 \ -L$(COMPONENT_PATH)/esp_codec/lib/esp32 \ -L$(COMPONENT_PATH)/recorder_engine/lib/esp32 \ -L$(COMPONENT_PATH)/esp_ssdp/lib/esp32 \ -L$(COMPONENT_PATH)/esp_upnp/lib/esp32 \ $(addprefix -l,$(LIBS)) \ ifdef IDF_VERSION_MAJOR ifeq ($(IDF_VERSION_MAJOR),4) COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_sip/lib/esp32 -lesp_sip COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_ssdp/lib/esp32 -lesp_ssdp else COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_sip/lib/esp32 -lesp_sip-v33 COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_ssdp/lib/esp32 -lesp_ssdp-v33 endif else COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_sip/lib/esp32 -lesp_sip-v33 COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/esp_ssdp/lib/esp32 -lesp_ssdp-v33 endif ALL_LIB_FILES += $(patsubst %,$(COMPONENT_PATH)/%/lib/esp32/lib%.a,$(LIBS))
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/component.mk
Makefile
apache-2.0
1,911
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUDIO_DEF_H_ #define _AUDIO_DEF_H_ #include "esp_err.h" #ifdef __cplusplus extern "C" { #endif #define ESP_ERR_AUDIO_BASE (0x80000 + 0x1000) /*!< Starting number of ESP audio error codes */ typedef enum { ESP_ERR_AUDIO_NO_ERROR = ESP_OK, ESP_ERR_AUDIO_FAIL = ESP_FAIL, /* esp audio errors */ ESP_ERR_AUDIO_NO_INPUT_STREAM = ESP_ERR_AUDIO_BASE + 1, // 0x0x81001 ESP_ERR_AUDIO_NO_OUTPUT_STREAM = ESP_ERR_AUDIO_BASE + 2, ESP_ERR_AUDIO_NO_CODEC = ESP_ERR_AUDIO_BASE + 3, ESP_ERR_AUDIO_HAL_FAIL = ESP_ERR_AUDIO_BASE + 4, ESP_ERR_AUDIO_MEMORY_LACK = ESP_ERR_AUDIO_BASE + 5, ESP_ERR_AUDIO_INVALID_URI = ESP_ERR_AUDIO_BASE + 6, ESP_ERR_AUDIO_INVALID_PATH = ESP_ERR_AUDIO_BASE + 7, ESP_ERR_AUDIO_INVALID_PARAMETER = ESP_ERR_AUDIO_BASE + 8, ESP_ERR_AUDIO_NOT_READY = ESP_ERR_AUDIO_BASE + 9, ESP_ERR_AUDIO_NOT_SUPPORT = ESP_ERR_AUDIO_BASE + 10, ESP_ERR_AUDIO_TIMEOUT = ESP_ERR_AUDIO_BASE + 11, ESP_ERR_AUDIO_ALREADY_EXISTS = ESP_ERR_AUDIO_BASE + 12, ESP_ERR_AUDIO_LINK_FAIL = ESP_ERR_AUDIO_BASE + 13, ESP_ERR_AUDIO_UNKNOWN = ESP_ERR_AUDIO_BASE + 14, ESP_ERR_AUDIO_OUT_OF_RANGE = ESP_ERR_AUDIO_BASE + 15, ESP_ERR_AUDIO_STOP_BY_USER = ESP_ERR_AUDIO_BASE + 16, ESP_ERR_AUDIO_OPEN = ESP_ERR_AUDIO_BASE + 0x100,// 0x81100 ESP_ERR_AUDIO_INPUT = ESP_ERR_AUDIO_BASE + 0x101, ESP_ERR_AUDIO_PROCESS = ESP_ERR_AUDIO_BASE + 0x102, ESP_ERR_AUDIO_OUTPUT = ESP_ERR_AUDIO_BASE + 0x103, ESP_ERR_AUDIO_CLOSE = ESP_ERR_AUDIO_BASE + 0x104, } audio_err_t; typedef enum { AUDIO_STATUS_UNKNOWN = 0, AUDIO_STATUS_RUNNING = 1, AUDIO_STATUS_PAUSED = 2, AUDIO_STATUS_STOPPED = 3, AUDIO_STATUS_FINISHED = 4, AUDIO_STATUS_ERROR = 5, } esp_audio_status_t; typedef enum { TERMINATION_TYPE_NOW = 0, /*!< Audio operation will be terminated immediately */ TERMINATION_TYPE_DONE = 1, /*!< Audio operation will be stopped when finished */ TERMINATION_TYPE_MAX, } audio_termination_type_t; typedef enum { ESP_AUDIO_PREFER_MEM = 0, ESP_AUDIO_PREFER_SPEED = 1, } esp_audio_prefer_t; typedef enum { MEDIA_SRC_TYPE_NULL = 0, MEDIA_SRC_TYPE_MUSIC_BASE = 0x100, MEDIA_SRC_TYPE_MUSIC_SD = MEDIA_SRC_TYPE_MUSIC_BASE + 1, MEDIA_SRC_TYPE_MUSIC_HTTP = MEDIA_SRC_TYPE_MUSIC_BASE + 2, MEDIA_SRC_TYPE_MUSIC_FLASH = MEDIA_SRC_TYPE_MUSIC_BASE + 3, MEDIA_SRC_TYPE_MUSIC_A2DP = MEDIA_SRC_TYPE_MUSIC_BASE + 4, MEDIA_SRC_TYPE_MUSIC_DLNA = MEDIA_SRC_TYPE_MUSIC_BASE + 5, MEDIA_SRC_TYPE_MUSIC_RAW = MEDIA_SRC_TYPE_MUSIC_BASE + 6, MEDIA_SRC_TYPE_MUSIC_MAX = 0x1FF, MEDIA_SRC_TYPE_TONE_BASE = 0x200, MEDIA_SRC_TYPE_TONE_SD = MEDIA_SRC_TYPE_TONE_BASE + 1, MEDIA_SRC_TYPE_TONE_HTTP = MEDIA_SRC_TYPE_TONE_BASE + 2, MEDIA_SRC_TYPE_TONE_FLASH = MEDIA_SRC_TYPE_TONE_BASE + 3, MEDIA_SRC_TYPE_TONE_MAX = 0x2FF, MEDIA_SRC_TYPE_RESERVE_BASE = 0x800, MEDIA_SRC_TYPE_RESERVE_MAX = 0xFFF, } media_source_type_t; /** * @brief esp_audio status information parameters */ typedef struct { esp_audio_status_t status; /*!< Status of esp_audio */ audio_err_t err_msg; /*!< Status is `AUDIO_STATUS_ERROR`, err_msg will be setup */ media_source_type_t media_src; /*!< Media source type*/ } esp_audio_state_t; typedef void (*esp_audio_event_callback)(esp_audio_state_t *audio, void *ctx); typedef esp_err_t (*audio_volume_set)(void *hd, int vol); typedef esp_err_t (*audio_volume_get)(void *hd, int *vol); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_audio/include/audio_def.h
C
apache-2.0
4,187
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _ESP_AUDIO_H_ #define _ESP_AUDIO_H_ #include "audio_def.h" #include "audio_common.h" #include "audio_element.h" #ifdef __cplusplus extern "C" { #endif typedef void *esp_audio_handle_t; #define DEFAULT_ESP_AUDIO_CONFIG() {\ .in_stream_buf_size = 10*1024,\ .out_stream_buf_size = 4*1024,\ .resample_rate = 0,\ .evt_que = NULL,\ .cb_func = NULL,\ .cb_ctx = NULL,\ .prefer_type = ESP_AUDIO_PREFER_MEM,\ .vol_handle = NULL,\ .vol_set = NULL,\ .vol_get = NULL,\ .task_prio = 6,\ .task_stack = 4 * 1024,\ } /** * @brief esp_audio configuration parameters */ typedef struct { int in_stream_buf_size; /*!< Input buffer size */ int out_stream_buf_size; /*!< Output buffer size */ /** * Destination sample rate, 0: disable resample; others: 44.1K, 48K, 32K, 16K, 8K has supported * It should be make sure same with I2S stream `sample_rate` */ int resample_rate; QueueHandle_t evt_que; /*!< For received esp_audio events (optional)*/ esp_audio_event_callback cb_func; /*!< esp_audio events callback (optional)*/ void *cb_ctx; /*!< esp_audio callback context (optional)*/ /** * esp_audio works on sepcific type, default memory is preferred. * - `ESP_AUDIO_PREFER_MEM` mode stopped the previous linked elements before the new pipeline starting, except out stream element. * - `ESP_AUDIO_PREFER_SPEED` mode kept the previous linked elements before the new pipeline starting, except out stream element. */ esp_audio_prefer_t prefer_type; void *vol_handle; /*!< Volume change instance */ audio_volume_set vol_set; /*!< Set volume callback */ audio_volume_get vol_get; /*!< Get volume callback*/ int task_prio; /*!< esp_audio task priority*/ int task_stack; /*!< Size of esp_audio task stack */ } esp_audio_cfg_t; /** * @brief esp_audio setup parameters by manual */ typedef struct { audio_codec_type_t set_type; /*!< Set codec type */ int set_sample_rate; /*!< Set music sample rate */ int set_channel; /*!< Set music channels */ int set_pos; /*!< Set starting position */ int set_time; /*!< Set starting position of the microseconds time (optional)*/ char *set_uri; /*!< Set URI */ char *set_in_stream; /*!< Tag of in_stream */ char *set_codec; /*!< Tag of the codec */ char *set_out_stream; /*!< Tag of out_stream */ } esp_audio_setup_t; /** * @brief esp_audio information */ typedef struct { audio_element_info_t codec_info; /*!< Codec information */ audio_element_handle_t in_el; /*!< Handle of the in stream */ audio_element_handle_t out_el; /*!< Handle of the out stream */ audio_element_handle_t codec_el; /*!< Handle of the codec */ audio_element_handle_t filter_el; /*!< Handle of the filter */ esp_audio_state_t st; /*!< The state of esp_audio */ int time_pos; /*!< Position of the microseconds time */ float audio_speed; /*!< Play speed of audio */ int64_t in_stream_total_size; /*!< Total size of in stream */ } esp_audio_info_t; /** * @brief esp_audio play speed */ typedef enum { ESP_AUDIO_PLAY_SPEED_UNKNOW = -1, ESP_AUDIO_PLAY_SPEED_0_50 = 0, ESP_AUDIO_PLAY_SPEED_0_75 = 1, ESP_AUDIO_PLAY_SPEED_1_00 = 2, ESP_AUDIO_PLAY_SPEED_1_25 = 3, ESP_AUDIO_PLAY_SPEED_1_50 = 4, ESP_AUDIO_PLAY_SPEED_1_75 = 5, ESP_AUDIO_PLAY_SPEED_2_00 = 6, ESP_AUDIO_PLAY_SPEED_MAX = 7, } esp_audio_play_speed_t; /** * @brief Create esp_audio instance according to 'cfg' parameter * * This function create an esp_audio instance, at the specified configuration. * * @param[in] cfg Provide esp_audio initialization configuration * * @return * - NULL: Error * - Others: esp_audio instance fully certifying */ esp_audio_handle_t esp_audio_create(const esp_audio_cfg_t *cfg); /** * @brief Specific esp_audio instance will be destroyed * * @param[in] handle The esp_audio instance * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no instance to free, call esp_audio_init first */ audio_err_t esp_audio_destroy(esp_audio_handle_t handle); /** * @brief Add audio input stream to specific esp_audio instance * * @param[in] handle The esp_audio instance * @param[in] in_stream Audio stream instance * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_MEMORY_LACK: allocate memory fail */ audio_err_t esp_audio_input_stream_add(esp_audio_handle_t handle, audio_element_handle_t in_stream); /** * @brief Add audio output stream to specific esp_audio instance * * @param[in] handle The esp_audio instance * @param[in] out_stream The audio stream element instance * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_MEMORY_LACK: allocate memory fail */ audio_err_t esp_audio_output_stream_add(esp_audio_handle_t handle, audio_element_handle_t out_stream); /** * @brief Add a new codec lib that can decode or encode a music file * * @param[in] handle The esp_audio instance * @param[in] type The audio codec type(encoder or decoder) * @param[in] lib To provide audio stream element * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_MEMORY_LACK: allocate memory fail */ audio_err_t esp_audio_codec_lib_add(esp_audio_handle_t handle, audio_codec_type_t type, audio_element_handle_t lib); /** * @brief Check if this kind of music extension is supported or not * * @note This function just query the codec which has already add by esp_audio_codec_lib_add. * The max length of extension is 6. * * @param[in] handle The esp_audio instance * @param[in] type The CODEC_ENCODER or CODEC_DECODER * @param[in] extension Such as "mp3", "wav", "aac" * * @return * - ESP_ERR_AUDIO_NO_ERROR: supported * - ESP_ERR_AUDIO_NOT_SUPPORT: not support * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_codec_lib_query(esp_audio_handle_t handle, audio_codec_type_t type, const char *extension); /** * @brief Play the given uri * * The esp_audio_play have follow activity, setup inputstream, outputstream and codec by uri, start all of them. * There is a rule that esp_audio will select input stream, codec and output stream by URI field. * Rule of URI field are as follow. * - `UF_SCHEMA` field of URI for choose input stream from existing streams. e.g:"http","file" * - `UF_PATH` field of URI for choose codec from existing codecs. e.g:"/audio/mp3_music.mp3" * - `UF_FRAGMENT` field of URI for choose output stream from existing streams, output stream is I2S by default. * - `UF_USERINFO` field of URI for specific sample rate and channels at encode mode. * * The format "user:password" in the userinfo field, "user" is sample rate, "password" is channels. * * Now esp_audio_play support follow URIs. * - "https://dl.espressif.com/dl/audio/mp3_music.mp3" * - "http://media-ice.musicradio.com/ClassicFMMP3" * - "file://sdcard/test.mp3" * - "iis://16000:2@from.pcm/rec.wav#file" * - "iis://16000:1@record.pcm/record.wav#raw" * - "aadp://44100:2@bt/sink/stream.pcm" * - "hfp://8000:1@bt/hfp/stream.pcm" * * @note * - The URI parse by `http_parser_parse_url`,any illegal string will be return `ESP_ERR_AUDIO_INVALID_URI`. * - If the esp_decoder codec is added to `handle`, then the `handle` of esp_decoder will be set as the default decoder, * even if other decoders are added. * - Enabled `CONFIG_FATFS_API_ENCODING_UTF_8`, the URI can be support Chinese characters. * - Asynchronous interface * - The maximum of block time can be modify by `esp_audio_play_timeout_set`, default value is 25 seconds. * * @param handle The esp_audio_handle_t instance * @param uri Such as "file://sdcard/test.wav" or "http://iot.espressif.com/file/example.mp3". * If NULL to be set, the uri setup by`esp_audio_setup` will used. * @param type Specific handle type decoder or encoder * @param pos Specific starting position by bytes * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_TIMEOUT: timeout the play activity * - ESP_ERR_AUDIO_NOT_SUPPORT: Currently status is AUDIO_STATUS_RUNNING * - ESP_ERR_AUDIO_INVALID_URI: URI is illegal * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_STOP_BY_USER: Exit without play due to esp_audio_stop has been called. */ audio_err_t esp_audio_play(esp_audio_handle_t handle, audio_codec_type_t type, const char *uri, int pos); /** * @brief Play the given uri until music finished or error occured * * @note * - All features are same with `esp_audio_play` * - Synchronous interface * - Support decoder mode only * - No any events post during playing * * @param handle The esp_audio_handle_t instance * @param uri Such as "file://sdcard/test.wav" or "http://iot.espressif.com/file/example.mp3", * @param pos Specific starting position by bytes * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_TIMEOUT: timeout the play activity * - ESP_ERR_AUDIO_NOT_SUPPORT: Currently status is AUDIO_STATUS_RUNNING * - ESP_ERR_AUDIO_INVALID_URI: URI is illegal * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_sync_play(esp_audio_handle_t handle, const char *uri, int pos); /** * @brief A synchronous interface for stop the esp_audio. The maximum of block time is 8000ms. * * @note 1. If user queue has been registered by evt_que, AUDIO_STATUS_STOPPED event for success * or AUDIO_STATUS_ERROR event for error will be received. * 2. `TERMINATION_TYPE_DONE` only works with input stream which can't stopped by itself, * e.g. `raw read/write stream`, others streams are no effect. * 3. The synchronous interface is used to ensure that working pipeline is stopped. * * @param[in] handle The esp_audio instance * @param[in] type Stop immediately or done * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_NOT_READY: The status is not AUDIO_STATUS_RUNNING or AUDIO_STATUS_PAUSED or element has not created * - ESP_ERR_AUDIO_TIMEOUT: timeout(8000ms) the stop activity. */ audio_err_t esp_audio_stop(esp_audio_handle_t handle, audio_termination_type_t type); /** * @brief Pause the esp_audio * * @note 1. Only support music and without live stream. If user queue has been registered by evt_que, AUDIO_STATUS_PAUSED event for success * or AUDIO_STATUS_ERROR event for error will be received. * 2. The Paused music must be stoped by `esp_audio_stop` before new playing, otherwise got block on new play. * * @param[in] handle The esp_audio instance * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_NOT_READY: the status is not running * - ESP_ERR_AUDIO_TIMEOUT: timeout the pause activity. */ audio_err_t esp_audio_pause(esp_audio_handle_t handle); /** * @brief Resume the music paused * * @note Only support music and without live stream. If user queue has been registered by evt_que, AUDIO_STATUS_RUNNING event for success * or AUDIO_STATUS_ERROR event for error will be received. * * @param[in] handle The esp_audio instance * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_TIMEOUT: timeout the resume activity. */ audio_err_t esp_audio_resume(esp_audio_handle_t handle); /** * @brief Getting esp_audio play speed index, index value is from "esp_audio_speed_t" enum. * * @param[in] handle The esp_audio instance * @param[out] speed_index Current audio play speed index. * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_CTRL_HAL_FAIL: error with hardware. * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_speed_get(esp_audio_handle_t handle, esp_audio_play_speed_t *speed_index); /** * @brief Use speed_index which is from "esp_audio_speed_t" enum to set esp_audio play speed. * * @param[in] handle The esp_audio instance * @param[in] speed_index Value from "esp_audio_speed_t" enum. * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_CTRL_HAL_FAIL: error with hardware. * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_speed_set(esp_audio_handle_t handle, esp_audio_play_speed_t speed_index); /** * @brief Use speed_index which is from "esp_audio_speed_t" enum to get esp_audio play speed which is float type. * * @param[in] handle The esp_audio instance * @param[in] speed_index Current audio play speed index. * @param[out] speed Current audio play speed. * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_CTRL_HAL_FAIL: error with hardware. * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_speed_idx_to_float(esp_audio_handle_t handle, esp_audio_play_speed_t speed_index, float *speed); /** * @brief Setting esp_audio volume. * * @param[in] handle The esp_audio instance * @param[in] vol Specific volume will be set. 0-100 is legal. 0 will be mute. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_CTRL_HAL_FAIL: error with hardware. * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_vol_set(esp_audio_handle_t handle, int vol); /** * @brief Get esp_audio volume * * @param[in] handle The esp_audio instance * @param[out] vol A pointer to int that indicates esp_audio volume. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_CTRL_HAL_FAIL: error with hardware. * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_vol_get(esp_audio_handle_t handle, int *vol); /** * @brief Get esp_audio status * * @param[in] handle The esp_audio instance * @param[out] state A pointer to esp_audio_state_t that indicates esp_audio status. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance or esp_audio does not playing */ audio_err_t esp_audio_state_get(esp_audio_handle_t handle, esp_audio_state_t *state); /** * @brief Get the position in bytes of currently played music. * * @note This function works only with decoding music. * * @param[in] handle The esp_audio instance * @param[out] pos A pointer to int that indicates esp_audio decoding position. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance * - ESP_ERR_AUDIO_NOT_READY: no codec element */ audio_err_t esp_audio_pos_get(esp_audio_handle_t handle, int *pos); /** * @brief Get the position in microseconds of currently played music. * * @note This function works only with decoding music. * * @param[in] handle The esp_audio instance * @param[out] time A pointer to int that indicates esp_audio decoding position. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance * - ESP_ERR_AUDIO_NOT_READY: no out stream */ audio_err_t esp_audio_time_get(esp_audio_handle_t handle, int *time); /** * @brief Choose the `in_stream`, `codec` and `out_stream` definitely, and set `uri`. * * @note This function provide a manual way to select in/out stream and codec, should be called before the `esp_audio_play`, * then ignore the `esp_audio_play` URI parameter only one time. * * @param[in] handle The esp_audio instance * @param[in] sets A pointer to esp_audio_setup_t. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance * - ESP_ERR_AUDIO_MEMORY_LACK: allocate memory fail */ audio_err_t esp_audio_setup(esp_audio_handle_t handle, esp_audio_setup_t *sets); /* @brief Call this function before play to change the music source type. * * @note Music source type has been kept after music finished. * * @param[in] handle The esp_audio instance * @param[in] type Specific media source type will be set. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_media_type_set(esp_audio_handle_t handle, media_source_type_t type); /* @brief Duplicate the current audio information to `info`. * * @param[in] handle The esp_audio instance * @param[in] info A pointer to esp_audio_info_t * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_MEMORY_LACK: allocate memory fail * - ESP_ERR_AUDIO_NOT_READY: the status is not running. * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_info_get(esp_audio_handle_t handle, esp_audio_info_t *info); /* @brief Restore the backup audio information to prevoise state. * * @note If ESP_ERR_AUDIO_NO_ERROR returned, the info->codec_info.uri, info->in_el, info->codec_el, * info->out_el and info->filter_el are set NULL. * * @param[in] handle The esp_audio instance * @param[in] info A pointer to esp_audio_info_t * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_FAIL: elements relink fail * - ESP_ERR_AUDIO_MEMORY_LACK: allocate memory fail * - ESP_ERR_AUDIO_TIMEOUT: timeout for relink * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments * - ESP_ERR_AUDIO_NOT_READY: the status is AUDIO_STATUS_RUNNING or AUDIO_STATUS_PAUSED */ audio_err_t esp_audio_info_set(esp_audio_handle_t handle, esp_audio_info_t *info); /* @brief Set esp_audio event callback. * * @param[in] handle The esp_audio instance * @param[in] cb A pointer to esp_audio_event_callback * @param[in] cb_ctx The context of callback * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_callback_set(esp_audio_handle_t handle, esp_audio_event_callback cb, void *cb_ctx); /** * @brief Seek the position in second of currently played music. * * @note This function works only with decoding music. * * @param[in] handle The esp_audio instance * @param[out] seek_time_sec A pointer to int that indicates esp_audio decoding position. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_FAIL: codec or allocation fail * - ESP_ERR_AUDIO_TIMEOUT: timeout for sync the element status * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance * - ESP_ERR_AUDIO_NOT_SUPPORT: codec has finished * - ESP_ERR_AUDIO_OUT_OF_RANGE: the seek_time_ms is out of the range * - ESP_ERR_AUDIO_NOT_READY: the status is neither running nor paused */ audio_err_t esp_audio_seek(esp_audio_handle_t handle, int seek_time_sec); /** * @brief Get the duration in microseconds of playing music. * * @note This function works only with decoding music. * * @param[in] handle The esp_audio instance * @param[out] duration A pointer to int that indicates decoding total time. * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance * - ESP_ERR_AUDIO_NOT_READY: no codec element or no in element */ audio_err_t esp_audio_duration_get(esp_audio_handle_t handle, int *duration); /** * @brief Setting the maximum amount of time to waiting for `esp_audio_play` only. * * @param[in] handle The esp_audio instance * @param[in] time_ms The maximum amount of time * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: invalid arguments */ audio_err_t esp_audio_play_timeout_set(esp_audio_handle_t handle, int time_ms); /** * @brief Get the type of `esp_audio_prefer_t` * * @param[in] handle The esp_audio instance * @param[out] type A pointer to esp_audio_prefer_t * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance */ audio_err_t esp_audio_prefer_type_get(esp_audio_handle_t handle, esp_audio_prefer_t *type); /** * @brief Set event queue to notify the esp_audio status * * @param[in] handle The esp_audio instance * @param[out] que A pointer to QueueHandle_t * * @return * - ESP_ERR_AUDIO_NO_ERROR: on success * - ESP_ERR_AUDIO_INVALID_PARAMETER: no esp_audio instance */ audio_err_t esp_audio_event_que_set(esp_audio_handle_t handle, QueueHandle_t que); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_audio/include/esp_audio.h
C
apache-2.0
22,768
// Copyright 2019 Espressif Systems (Shanghai) CO., LTD. // All rights reserved. #include <math.h> #include <string.h> #include "esp_log.h" #include "audio_error.h" #include "audio_element.h" #include "audio_alc.h" #include "esp_alc.h" #include "audio_mem.h" #define ALC_INBUFFER_LENGTH (2048) static const char *TAG = "ALC"; static esp_err_t _alc_volume_setup_open(audio_element_handle_t self) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); vol_setup_info->in_buf = audio_calloc(1, ALC_INBUFFER_LENGTH); if (vol_setup_info->in_buf == NULL) { ESP_LOGE(TAG, "audio_calloc failed for in_buf. (line %d)", __LINE__); return ESP_ERR_NO_MEM; } vol_setup_info->handle = alc_volume_setup_open(); if (vol_setup_info->handle == NULL) { ESP_LOGE(TAG, "Failed to create ALC handle. (line %d)", __LINE__); return ESP_FAIL; } return ESP_OK; } static esp_err_t _alc_volume_setup_close(audio_element_handle_t self) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); alc_volume_setup_close(vol_setup_info->handle); if (vol_setup_info->in_buf) { audio_free(vol_setup_info->in_buf); vol_setup_info->in_buf = NULL; } return ESP_OK; } static esp_err_t _alc_volume_setup_process(audio_element_handle_t self, char *in_buffer, int in_len) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); int read_len = audio_element_input(self, (char *)vol_setup_info->in_buf, ALC_INBUFFER_LENGTH); if (read_len < 0) { return read_len; } int ret = alc_volume_setup_process((char *)vol_setup_info->in_buf, read_len, vol_setup_info->channel, vol_setup_info->handle, vol_setup_info->volume); read_len = audio_element_output(self, (char *)vol_setup_info->in_buf, read_len); return ret; } void alc_volume_setup_set_channel(audio_element_handle_t self, int channel) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); vol_setup_info->channel = channel; } void alc_volume_setup_set_volume(audio_element_handle_t self, int volume) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); vol_setup_info->volume = volume; } int alc_volume_setup_get_volume(audio_element_handle_t self) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); return vol_setup_info->volume; } static esp_err_t alc_volume_setup_destroy(audio_element_handle_t self) { volume_set_t *vol_setup_info = (volume_set_t *)audio_element_getdata(self); if (vol_setup_info) { audio_free(vol_setup_info); } return ESP_OK; } audio_element_handle_t alc_volume_setup_init(alc_volume_setup_cfg_t *config) { if (config == NULL) { ESP_LOGE(TAG, "config is NULL. (line %d)", __LINE__); return NULL; } volume_set_t *vol_setup_info = audio_calloc(1, sizeof(volume_set_t)); AUDIO_MEM_CHECK(TAG, vol_setup_info, return NULL); audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG(); audio_element_handle_t el; cfg.open = _alc_volume_setup_open; cfg.close = _alc_volume_setup_close; cfg.process = _alc_volume_setup_process; cfg.destroy = alc_volume_setup_destroy; cfg.task_stack = config->task_stack; cfg.task_prio = config->task_prio; cfg.task_core = config->task_core; cfg.out_rb_size = config->out_rb_size; cfg.stack_in_ext = config->stack_in_ext; cfg.tag = "alc_volume"; el = audio_element_init(&cfg); AUDIO_MEM_CHECK(TAG, el, {audio_free(vol_setup_info); return NULL;}); vol_setup_info->channel = config->channel; vol_setup_info->volume = config->volume; audio_element_setdata(el, vol_setup_info); return el; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/audio_alc.c
C
apache-2.0
3,932
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ /* * The module is multifunctional audio forge which contains resample, downmix, ALC, equalizer and sonic. Choose a combination of several function by `component_select`. * * resample: Change sample rate or number of channels for source stream. * downmix: Down-mix different source streams or stereo source stream.The downmix supports less and equal than `SOURCE_NUM_MAX` source files. * ALC: Change volume of source file. * equalizer: Modify a frequency response to compensate for distortion. * sonic: Change the speed and pitch of source file. * */ #include <string.h> #include "esp_log.h" #include "esp_err.h" #include "audio_mem.h" #include "audio_element.h" #include "audio_forge.h" #include "audio_mutex.h" // #define AUDIO_FORGE_SPEED_ANALYSIS // #define AUDIO_FORGE_MEMORY_ANALYSIS // #define DEBUG_AUDIO_FORGE_ISSUE #define RESAMPLE_COMPLEXITY (2) #define RESAMPLING_POINT_NUM (512) #define NUMBER_BAND (10) #define USE_XMMS_ORIGINAL_FREQENT (0) #define MAX_MEM_PARA (2) #ifdef AUDIO_FORGE_SPEED_ANALYSIS #include "codec_tick.h" int audio_forge_start_time; int audio_forge_stop_time; int audio_forge_whole_time; int audio_forge_decode_time; int audio_forge_write_time; long pcmcnt; #endif #ifdef AUDIO_FORGE_MEMORY_ANALYSIS int startmemory_inram; int stopmemory_inram; int startmemory_total; int stopmemory_total; long pcmcnt; #endif #ifdef DEBUG_AUDIO_FORGE_ISSUE FILE *in_file[SOURCE_NUM_MAX]; FILE *out_file; char in_name[SOURCE_NUM_MAX][100]; char out_name[100]; #endif static const char *TAG = "AUDIO_FORGE"; typedef enum { ADUIO_FORGE_ISSTART = 0x01, ADUIO_FORGE_RSP_RESTART = 0x02, ADUIO_FORGE_DM_RESTART = 0x04, ADUIO_FORGE_ALC_RESTART = 0x08, ADUIO_FORGE_EQ_RESTART = 0x10, ADUIO_FORGE_SONIC_RESTART = 0x20, } audio_forge_restart_flag_t; typedef struct { void *downmix_handle; esp_downmix_info_t downmix; unsigned char **inbuf; int inbuf_size; unsigned char *outbuf; int *tick_to_wait; void **rsp_handle; resample_info_t *rsp_info; unsigned char **rsp_in; unsigned char **rsp_out; int *in_offset; int rsp_cplx; audio_forge_select_t component_select; void *volume_handle; void *eq_handle; int *equalizer_gain; void *sonic_handle; float sonic_pitch; float sonic_speed; long long pcm_cnt; int sample_rate; int channel; int volume; audio_forge_restart_flag_t reflag; int max_sample; int sonic_num; audio_forge_stop_mode_t stop_mode; int status; xSemaphoreHandle lock; #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_handle_t *audio_forge_tick_handle; #endif } audio_forge_t; #ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif static esp_err_t dm_open(audio_forge_t *audio_forge) { for (int i = 0; i < audio_forge->downmix.source_num; i++) { if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { audio_forge->downmix.source_info[i].samplerate = audio_forge->sample_rate; } if ((audio_forge->sample_rate != audio_forge->downmix.source_info[i].samplerate) && (audio_forge->downmix.source_info[i].samplerate < SAMPLERATE_MIN || audio_forge->downmix.source_info[i].samplerate >= SAMPLERATE_MAX)) { ESP_LOGE(TAG, "The stream sample rate is out of range. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } if ((audio_forge->inbuf[i] == NULL)) { if (i == 0) { audio_forge->inbuf[i] = (unsigned char *)audio_realloc(audio_forge->inbuf[i], audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } else { audio_forge->inbuf[i] = (unsigned char *)audio_realloc(audio_forge->inbuf[i], audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } AUDIO_MEM_CHECK(TAG, audio_forge->inbuf[i], return ESP_ERR_NO_MEM); } if ((audio_forge->downmix.source_info[i].channel != 1) && (audio_forge->downmix.source_info[i].channel != 2)) { ESP_LOGE(TAG, "The number of channels should be either 1 or 2. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } if (audio_forge->downmix.source_info[i].gain[0] < GAIN_MIN || audio_forge->downmix.source_info[i].gain[0] > GAIN_MAX || audio_forge->downmix.source_info[i].gain[1] < GAIN_MIN || audio_forge->downmix.source_info[i].gain[1] > GAIN_MAX) { ESP_LOGE(TAG, "The gain is out (%d, %d) range", GAIN_MIN, GAIN_MAX); return ESP_ERR_INVALID_ARG; } if (audio_forge->downmix.source_info[i].transit_time < 0) { ESP_LOGE(TAG, "The transit_time (%d) must be equal or gather than zero, line %d", audio_forge->downmix.source_info[0].transit_time, __LINE__); return ESP_ERR_INVALID_ARG; } audio_forge->downmix.source_info[i].bits_num = ESP_AUDIO_BIT_16; #ifdef DEBUG_AUDIO_FORGE_ISSUE in_file[i] = fopen(in_name[i], "rb"); #endif } if (audio_forge->downmix.source_num > 1) { audio_forge->downmix.mode = ESP_DOWNMIX_WORK_MODE_SWITCH_ON; } audio_forge->downmix.output_type = audio_forge->channel; audio_forge->downmix.out_ctx = ESP_DOWNMIX_OUT_CTX_LEFT_RIGHT; audio_forge->downmix_handle = esp_downmix_open(&audio_forge->downmix); AUDIO_NULL_CHECK(TAG, audio_forge->downmix_handle, return ESP_FAIL); return ESP_OK; } static esp_err_t rsp_open(audio_forge_t *audio_forge, int index) { if (index > audio_forge->downmix.source_num) { ESP_LOGE(TAG, "index(%d) is out of range (0 %d), line (%d)", index, audio_forge->downmix.source_num, __LINE__); return ESP_FAIL; } audio_forge->rsp_info[index].dest_rate = audio_forge->sample_rate; audio_forge->rsp_info[index].dest_ch = audio_forge->rsp_info[index].src_ch; audio_forge->rsp_info[index].mode = RESAMPLE_ENCODE_MODE; int tmp = ((float)audio_forge->rsp_info[index].src_rate / (float)audio_forge->rsp_info[index].dest_rate + 0.5); audio_forge->rsp_info[index].max_indata_bytes = audio_forge->max_sample * sizeof(short) * tmp * audio_forge->rsp_info[index].src_ch + RESAMPLING_POINT_NUM * audio_forge->rsp_info[index].src_ch; //`RESAMPLING_POINT_NUM * resample_info->src_ch` has no meaning, just enought extra buffer for safety; audio_forge->rsp_info[index].out_len_bytes = audio_forge->max_sample * sizeof(short) * audio_forge->rsp_info[index].src_ch; audio_forge->in_offset[index] = 0; audio_forge->rsp_info[index].complexity = audio_forge->rsp_cplx; audio_forge->rsp_info[index].type = ESP_RESAMPLE_TYPE_AUTO; audio_forge->rsp_info[index].prefer_flag = ESP_RSP_PREFER_TYPE_SPEED; audio_forge->rsp_handle[index] = esp_resample_create((void *)&audio_forge->rsp_info[index], &audio_forge->rsp_in[index], &audio_forge->rsp_out[index]); AUDIO_NULL_CHECK(TAG, audio_forge->rsp_handle[index], return ESP_FAIL); if (audio_forge->inbuf[index]) { void *tmp = audio_forge->inbuf[index]; audio_forge->inbuf[index] = audio_forge->rsp_out[index]; audio_free(tmp); } else { audio_forge->inbuf[index] = audio_forge->rsp_out[index]; } if (audio_forge->rsp_info[index].src_ch != audio_forge->downmix.source_info[index].channel) { audio_forge->downmix.source_info[index].channel = audio_forge->rsp_info[index].src_ch; audio_forge->component_select |= AUDIO_FORGE_SELECT_DOWNMIX; audio_forge->reflag |= ADUIO_FORGE_DM_RESTART; } audio_forge->downmix.source_info[index].channel = audio_forge->rsp_info[index].src_ch; return ESP_OK; } static esp_err_t eq_open(audio_forge_t *audio_forge) { audio_forge->eq_handle = esp_equalizer_init(audio_forge->channel, audio_forge->sample_rate, NUMBER_BAND, USE_XMMS_ORIGINAL_FREQENT); AUDIO_NULL_CHECK(TAG, audio_forge->eq_handle, return ESP_FAIL); for (int i = 0; i < audio_forge->channel; i++) { for (int j = 0; j < NUMBER_BAND; j++) { esp_equalizer_set_band_value(audio_forge->eq_handle, audio_forge->equalizer_gain[NUMBER_BAND * i + j], j, i); } } return ESP_OK; } static esp_err_t sonic_open(audio_forge_t *audio_forge) { audio_forge->sonic_handle = esp_sonic_create_stream(audio_forge->sample_rate, audio_forge->channel); AUDIO_NULL_CHECK(TAG, audio_forge->sonic_handle, return ESP_FAIL); esp_sonic_set_resample_mode(audio_forge->sonic_handle, 0); esp_sonic_set_speed(audio_forge->sonic_handle, audio_forge->sonic_speed); esp_sonic_set_pitch(audio_forge->sonic_handle, audio_forge->sonic_pitch); audio_forge->sonic_num = (audio_forge->sample_rate << 1) / SONIC_MIN_PITCH; audio_forge->sonic_num = audio_forge->sonic_num / audio_forge->max_sample + 1; if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { if (audio_forge->inbuf[0] == audio_forge->rsp_out[0]) { audio_forge->inbuf[0] = (unsigned char *)audio_calloc(1, audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } } else { audio_forge->inbuf[0] = (unsigned char *)audio_realloc(audio_forge->inbuf[0], audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } AUDIO_MEM_CHECK(TAG, audio_forge->inbuf[0], return ESP_ERR_NO_MEM); audio_forge->outbuf = (unsigned char *)audio_realloc(audio_forge->outbuf, audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); AUDIO_MEM_CHECK(TAG, audio_forge->outbuf, return ESP_ERR_NO_MEM); return ESP_OK; } static esp_err_t audio_forge_open(audio_element_handle_t self) { ESP_LOGI(TAG, "audio_forge opened"); #ifdef AUDIO_FORGE_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); startmemory_inram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); startmemory_total = esp_get_free_heap_size(); #endif audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_config_t tick_config[6] = { { (const char *)"forge_whole_time", esp_tick_time_ms}, { (const char *)"forge_process_time", cpu_tick_time_ms}, { (const char *)"forge_write_time", cpu_tick_time_ms}, { (const char *)"forge_read_time", cpu_tick_time_ms}, { (const char *)"sonic_read_time", cpu_tick_time_ms}, { (const char *)"sonic_write_time", cpu_tick_time_ms}, }; audio_forge->audio_forge_tick_handle = codec_tick_init(tick_config, 6); #endif if (audio_forge->reflag & ADUIO_FORGE_ISSTART) { if (audio_forge->stop_mode == AUDIO_FORGE_STOP_MODE_MANUAL) { return ESP_OK; } } if (!audio_forge->lock) { audio_forge->lock = mutex_create(); } int ret = 0; audio_forge->reflag |= ADUIO_FORGE_ISSTART; audio_forge->sonic_num = 1; if ((audio_forge->sample_rate < SAMPLERATE_MIN) || (audio_forge->sample_rate >= SAMPLERATE_MAX)) { ESP_LOGE(TAG, "The samplerates of stream are error. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } if ((audio_forge->channel != 1) && (audio_forge->channel != 2)) { ESP_LOGE(TAG, "The number of channels should be either 1 or 2. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } mutex_lock(audio_forge->lock); if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { for(int i = 0; i < audio_forge->downmix.source_num; i++) { if (audio_forge->rsp_handle[i] == NULL){ ret |= rsp_open(audio_forge, i); } } } mutex_unlock(audio_forge->lock); if (audio_forge->component_select & AUDIO_FORGE_SELECT_DOWNMIX) { //downmix ret |= dm_open(audio_forge); } else { #ifdef DEBUG_AUDIO_FORGE_ISSUE in_file[0] = fopen(in_name[0], "rb"); #endif audio_forge->downmix.source_num = 1; } #ifdef DEBUG_AUDIO_FORGE_ISSUE out_file = fopen(out_name, "wb"); #endif if (audio_forge->component_select & AUDIO_FORGE_SELECT_ALC) { //alc audio_forge->volume_handle = alc_volume_setup_open(); AUDIO_NULL_CHECK(TAG, audio_forge->volume_handle, return ESP_FAIL); } if (audio_forge->component_select & AUDIO_FORGE_SELECT_EQUALIZER) { //equalizer ret |= eq_open(audio_forge); } if (audio_forge->component_select & AUDIO_FORGE_SELECT_SONIC) { //sonic ret |= sonic_open(audio_forge); } else { audio_forge->outbuf = (unsigned char *)audio_calloc(1, audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); AUDIO_MEM_CHECK(TAG, audio_forge->outbuf, return ESP_ERR_NO_MEM); } #ifdef AUDIO_FORGE_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_start(audio_forge->audio_forge_tick_handle, 0); #endif return ret; } esp_err_t audio_forge_destroy(audio_element_handle_t self) { ESP_LOGI(TAG, "audio_forge_destroy"); audio_forge_t* audio_forge = (audio_forge_t *)audio_element_getdata(self); if (audio_forge->inbuf) { audio_free(audio_forge->inbuf); audio_forge->inbuf = NULL; } if (audio_forge->rsp_handle) { audio_free(audio_forge->rsp_handle); audio_forge->rsp_handle = NULL; } if (audio_forge->rsp_info) { audio_free(audio_forge->rsp_info); audio_forge->rsp_info = NULL; } if (audio_forge->rsp_in) { audio_free(audio_forge->rsp_in); audio_forge->rsp_in = NULL; } if (audio_forge->rsp_out) { audio_free(audio_forge->rsp_out); audio_forge->rsp_out = NULL; } if (audio_forge->in_offset) { audio_free(audio_forge->in_offset); audio_forge->in_offset = NULL; } if (audio_forge->tick_to_wait) { audio_free(audio_forge->tick_to_wait); audio_forge->tick_to_wait = NULL; } if (audio_forge->downmix.source_info) { audio_free(audio_forge->downmix.source_info); audio_forge->downmix.source_info = NULL; } if (audio_forge->equalizer_gain) { audio_free(audio_forge->equalizer_gain); audio_forge->equalizer_gain = NULL; } audio_free(audio_forge); AUDIO_MEM_SHOW(TAG); audio_forge = NULL; return ESP_OK; } esp_err_t audio_forge_close(audio_element_handle_t self) { ESP_LOGI(TAG, "audio forge closed"); #ifdef AUDIO_FORGE_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); audio_forge->status = 0; if (audio_forge->stop_mode == AUDIO_FORGE_STOP_MODE_MANUAL) { return ESP_OK; } #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_stop(audio_forge->audio_forge_tick_handle, 0, false); int realtime = 1000 * audio_forge->pcm_cnt / (audio_forge->sample_rate * audio_forge->channel * sizeof(short)); audio_forge->pcm_cnt = 0; codec_tick_printf(audio_forge->audio_forge_tick_handle, realtime); codec_tick_deinit(audio_forge->audio_forge_tick_handle); audio_forge->audio_forge_tick_handle = NULL; #endif for (int i = 0; i < audio_forge->downmix.source_num; i++) { //resample if (audio_forge->rsp_handle[i] != NULL) { if (audio_forge->inbuf[i] != audio_forge->rsp_out[i]) { audio_free(audio_forge->inbuf[i]); audio_forge->inbuf[i] = NULL; } else { audio_forge->inbuf[i] = NULL; } esp_resample_destroy(audio_forge->rsp_handle[i]); audio_forge->rsp_out[i] = NULL; audio_forge->rsp_in[i] = NULL; audio_forge->rsp_handle[i] = NULL; } if (audio_forge->inbuf[i] != NULL) { audio_free(audio_forge->inbuf[i]); audio_forge->inbuf[i] = NULL; } #ifdef DEBUG_AUDIO_FORGE_ISSUE if (in_file[i] != NULL) { fclose(in_file[i]); } #endif } #ifdef DEBUG_AUDIO_FORGE_ISSUE if (out_file != NULL) { fclose(out_file); } #endif if (audio_forge->lock) { mutex_destroy(audio_forge->lock); audio_forge->lock = NULL; } if (audio_forge->outbuf != NULL) { audio_free(audio_forge->outbuf); audio_forge->outbuf = NULL; } //downmix if (audio_forge->downmix_handle != NULL) { esp_downmix_close(audio_forge->downmix_handle); audio_forge->downmix_handle = NULL; } //alc if (audio_forge->volume_handle != NULL) { alc_volume_setup_close(audio_forge->volume_handle); audio_forge->volume_handle = NULL; } //equalizer if (audio_forge->eq_handle != NULL) { esp_equalizer_uninit(audio_forge->eq_handle); audio_forge->eq_handle = NULL; } //sonic if (audio_forge->sonic_handle != NULL) { esp_sonic_destroy_stream(audio_forge->sonic_handle); audio_forge->sonic_handle = NULL; } #ifdef AUDIO_FORGE_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif return ESP_OK; } //resample static esp_err_t rsp_process(audio_element_handle_t self, audio_forge_t *audio_forge, int index) { int ret = 0; if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE)) { return ESP_ERR_NOT_SUPPORTED; } if ((audio_forge->in_offset[index] < audio_forge->rsp_info[index].max_indata_bytes) && (audio_forge->rsp_info[index].max_indata_bytes > 0)) { if (audio_forge->in_offset[index] > 0) { memmove(audio_forge->rsp_in[index], &audio_forge->rsp_in[index][audio_forge->rsp_info[index].max_indata_bytes - audio_forge->in_offset[index]], audio_forge->in_offset[index]); } #ifndef DEBUG_AUDIO_FORGE_ISSUE if (audio_forge->downmix.source_num != 1) { ret = audio_element_multi_input(self, (char *)&audio_forge->rsp_in[index][audio_forge->in_offset[index]], audio_forge->rsp_info[index].max_indata_bytes - audio_forge->in_offset[index], index, audio_forge->tick_to_wait[index]); } else { #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_start(audio_forge->audio_forge_tick_handle, 3); #endif ret = audio_element_input(self, (char *)&audio_forge->rsp_in[index][audio_forge->in_offset[index]], audio_forge->rsp_info[index].max_indata_bytes - audio_forge->in_offset[index]); #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_stop(audio_forge->audio_forge_tick_handle, 3, true); #endif } #else ret = fread(&audio_forge->rsp_in[index][audio_forge->in_offset[index]], 1, audio_forge->rsp_info[index].max_indata_bytes - audio_forge->in_offset[index], in_file[index]); #endif if (ret > 0) { int tmp = audio_forge->in_offset[index]; audio_forge->in_offset[index] += ret; if (audio_forge->in_offset[index] != audio_forge->rsp_info[index].max_indata_bytes) { memset(&audio_forge->rsp_in[index][tmp] + ret, 0, audio_forge->rsp_info[index].max_indata_bytes - audio_forge->in_offset[index]); } audio_forge->in_offset[index] = audio_forge->rsp_info[index].max_indata_bytes; } else { memset(audio_forge->rsp_out[index], 0, audio_forge->rsp_info[index].out_len_bytes); switch (ret) { case AEL_IO_TIMEOUT: return ESP_ERR_TIMEOUT; case AEL_IO_FAIL: return ESP_ERR_INVALID_ARG; case AEL_IO_ABORT: return ret; default: return ESP_ERR_NOT_FOUND; } } } ret = esp_resample_run(audio_forge->rsp_handle[index], (void *)&audio_forge->rsp_info[index], audio_forge->rsp_in[index], audio_forge->rsp_out[index], audio_forge->in_offset[index], &audio_forge->rsp_info[index].out_len_bytes); if (ret <= 0) { memset(audio_forge->rsp_out[index], 0, audio_forge->rsp_info[index].out_len_bytes); ESP_LOGE(TAG, "resample processing happened error, audio forge ways %d ,line %d", index, __LINE__); return ESP_FAIL; } else { audio_forge->in_offset[index] -= ret; } return ESP_OK; } static int audio_forge_process(audio_element_handle_t self, char *in_buffer, int in_len) { #ifdef AUDIO_FORGE_MEMORY_ANALYSIS if (pcmcnt == 0) { AUDIO_MEM_SHOW(TAG); } #endif audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_start(audio_forge->audio_forge_tick_handle, 1); #endif int ret = 0; if (!(audio_forge->reflag == ADUIO_FORGE_ISSTART)) { if (audio_forge->reflag & ADUIO_FORGE_DM_RESTART) { if (audio_forge->downmix_handle != NULL) { esp_downmix_close(audio_forge->downmix_handle); audio_forge->downmix_handle = NULL; } ret = dm_open(audio_forge); if (ret != ESP_OK) { return ESP_FAIL; } audio_forge->reflag &= (~ADUIO_FORGE_DM_RESTART); } if (audio_forge->reflag & ADUIO_FORGE_EQ_RESTART) { if (audio_forge->eq_handle != NULL) { esp_equalizer_uninit(audio_forge->eq_handle); audio_forge->eq_handle = NULL; } ret = eq_open(audio_forge); if (ret != ESP_OK) { return ESP_FAIL; } audio_forge->reflag &= (~ADUIO_FORGE_EQ_RESTART); } if (audio_forge->reflag & ADUIO_FORGE_SONIC_RESTART) { if (audio_forge->sonic_handle != NULL) { esp_sonic_destroy_stream(audio_forge->sonic_handle); audio_forge->sonic_handle = NULL; } ret = sonic_open(audio_forge); if (ret != ESP_OK) { return ESP_FAIL; } audio_forge->reflag &= (~ADUIO_FORGE_SONIC_RESTART); } ESP_LOGI(TAG, "audio_forge reopen"); } int w_size = 0; int j = 0; int rsp_ret = 0; for (j = 0; j < audio_forge->sonic_num; j++) { for (int i = 0; i < audio_forge->downmix.source_num; i++) { //resample if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { rsp_ret = 0; if (audio_forge->status & (1 << i)) { if (audio_forge->rsp_handle[i] == NULL) { memset(audio_forge->inbuf[i], 0, audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } continue; } if (audio_forge->rsp_handle[i] == NULL) { audio_forge->status |= 1 << i; continue; } mutex_lock(audio_forge->lock); ret = rsp_process(self, audio_forge, i); mutex_unlock(audio_forge->lock); if (ret == ESP_ERR_NOT_FOUND) { ret = audio_forge->rsp_info[i].out_len_bytes; esp_resample_destroy(audio_forge->rsp_handle[i]); audio_forge->rsp_handle[i] = NULL; if (i == 0) { if (audio_forge->inbuf[0] == audio_forge->rsp_out[0]) { audio_forge->inbuf[0] = (unsigned char *)audio_calloc(1, audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } else{ audio_forge->inbuf[0] = (unsigned char *)audio_realloc(audio_forge->inbuf[i], audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } } else { if (audio_forge->inbuf[i] == audio_forge->rsp_out[i]) { audio_forge->inbuf[i] = (unsigned char *)audio_calloc(1, audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } else { audio_forge->inbuf[i] = (unsigned char *)audio_realloc(audio_forge->inbuf[i], audio_forge->max_sample * sizeof(short) * MAX_MEM_PARA); } } AUDIO_MEM_CHECK(TAG, audio_forge->inbuf[i], return ESP_ERR_NO_MEM); audio_forge->rsp_out[i] = NULL; audio_forge->rsp_in[i] = NULL; audio_forge->status |= 1 << i; continue; } else if (ret == ESP_OK) { audio_forge->status &= ~(1 << i); ret = audio_forge->rsp_info[i].out_len_bytes; } else if (ret == ESP_ERR_INVALID_ARG) { audio_forge->status |= 1 << i; ret = audio_forge->rsp_info[i].out_len_bytes; continue; } else if (ret == AEL_IO_ABORT) { audio_forge->status |= 1 << i; ret = audio_forge->rsp_info[i].out_len_bytes; rsp_ret = AEL_IO_ABORT; continue; } else if (ret == ESP_ERR_TIMEOUT){ ret = audio_forge->rsp_info[i].out_len_bytes; continue; } else if (ret == ESP_FAIL) { return ESP_FAIL; } if (ret > 0) { memcpy(audio_forge->inbuf[i], audio_forge->rsp_out[i], audio_forge->rsp_info[i].out_len_bytes); } } else { #ifndef DEBUG_AUDIO_FORGE_ISSUE if (audio_forge->downmix.source_num != 1) { audio_forge->in_offset[i] = audio_element_multi_input(self, (char *)audio_forge->inbuf[i], audio_forge->max_sample * sizeof(short) * audio_forge->downmix.source_info[i].channel, i, audio_forge->tick_to_wait[i]); } else { audio_forge->in_offset[i] = audio_element_input(self, (char *)audio_forge->inbuf[i], audio_forge->max_sample * sizeof(short) * audio_forge->downmix.source_info[i].channel); } #else audio_forge->in_offset[i] = fread((void *)&audio_forge->inbuf[i][0], 1, audio_forge->max_sample * sizeof(short) * audio_forge->downmix.source_info[i].channel, in_file[i]); #endif if (audio_forge->in_offset[i] <= 0) { memset(audio_forge->inbuf[i], 0, audio_forge->max_sample * sizeof(short) * 2); switch (audio_forge->in_offset[i]) { case AEL_IO_TIMEOUT: break; case AEL_IO_FAIL: case AEL_IO_ABORT: default: audio_forge->status |= 1 << i; } continue; } else if (audio_forge->in_offset[i] != audio_forge->max_sample * sizeof( short) * audio_forge->downmix.source_info[i].channel) { memset(audio_forge->inbuf[i] + audio_forge->in_offset[i], 0, audio_forge->max_sample * sizeof(short) * 2 - audio_forge->in_offset[i]); } ret = audio_forge->in_offset[i]; } } if (audio_forge->component_select & AUDIO_FORGE_SELECT_DOWNMIX) { ret = esp_downmix_process(audio_forge->downmix_handle, audio_forge->inbuf, audio_forge->outbuf, audio_forge->max_sample, audio_forge->downmix.mode); } else { if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { if (audio_forge->rsp_out[0] != NULL) { memcpy(audio_forge->outbuf, audio_forge->rsp_out[0], ret); } } else { memcpy(audio_forge->outbuf, audio_forge->inbuf[0], ret); } } audio_forge->pcm_cnt += ret; if (audio_forge->component_select & AUDIO_FORGE_SELECT_ALC) { //alc alc_volume_setup_process(audio_forge->outbuf, ret, audio_forge->channel, audio_forge->volume_handle, audio_forge->volume); } if (audio_forge->component_select & AUDIO_FORGE_SELECT_EQUALIZER) { //equalizer esp_equalizer_process(audio_forge->eq_handle, (unsigned char *)audio_forge->outbuf, ret, audio_forge->sample_rate, audio_forge->channel); } //finished if ((audio_forge->status == (1 << audio_forge->downmix.source_num) - 1)) { if (rsp_ret == AEL_IO_ABORT) { return ESP_CODEC_ERR_ABORT; } break; } int write_ret = 0; if (audio_forge->component_select & AUDIO_FORGE_SELECT_SONIC) { //sonic #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_start(audio_forge->audio_forge_tick_handle, 5); #endif esp_sonic_write_to_stream(audio_forge->sonic_handle, (short *)(audio_forge->outbuf), ret / (audio_forge->channel * sizeof(short))); #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_stop(audio_forge->audio_forge_tick_handle, 5, true); #endif do { #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_start(audio_forge->audio_forge_tick_handle, 4); #endif write_ret = esp_sonic_read_from_stream(audio_forge->sonic_handle, (short *)audio_forge->inbuf[0], (int)audio_forge->max_sample); #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_stop(audio_forge->audio_forge_tick_handle, 4, true); #endif if (write_ret > 0) { #ifndef DEBUG_AUDIO_FORGE_ISSUE #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_start(audio_forge->audio_forge_tick_handle, 2); #endif w_size = audio_element_output(self, (char *)audio_forge->inbuf[0], write_ret * audio_forge->channel * sizeof(short)); #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_stop(audio_forge->audio_forge_tick_handle, 2, true); #endif #else w_size = fwrite(audio_forge->inbuf[0], 1, write_ret * audio_forge->channel * sizeof(short) * j / audio_forge->sonic_num, out_file); #endif } } while (write_ret > 0); } else { if (ret > 0) { #ifndef DEBUG_AUDIO_FORGE_ISSUE w_size = audio_element_output(self, (char *)audio_forge->outbuf, ret); #else w_size = fwrite(audio_forge->outbuf, 1, ret, out_file); #endif } } } if (j < audio_forge->sonic_num) { w_size = 0; } #ifdef AUDIO_FORGE_MEMORY_ANALYSIS pcmcnt += audio_forge->sonic_num; if (pcmcnt == audio_forge->sonic_num) { AUDIO_MEM_SHOW(TAG); stopmemory_inram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); stopmemory_total = esp_get_free_heap_size(); int inram_mem = startmemory_inram - stopmemory_inram; int total_mem = startmemory_total - stopmemory_total; int flag = (total_mem >= inram_mem) ? 1 : 0; printf("MEMORYINFO: samplerate = %dHz channel = %d\n", total_mem, inram_mem, flag); } #endif if (audio_forge->stop_mode == AUDIO_FORGE_STOP_MODE_MANUAL) { if (w_size <= 0) { w_size = 1; } } #ifdef AUDIO_FORGE_SPEED_ANALYSIS codec_tick_count_stop(audio_forge->audio_forge_tick_handle, 1, true); #endif return w_size; } esp_err_t audio_forge_alc_set_volume(audio_element_handle_t self, int volume) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (audio_forge->component_select & AUDIO_FORGE_SELECT_ALC) { audio_forge->volume = volume; return ESP_OK; } ESP_LOGE(TAG, "The ALC don't be used"); return ESP_FAIL; } esp_err_t audio_forge_alc_get_volume(audio_element_handle_t self, int *volume) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (audio_forge->component_select & AUDIO_FORGE_SELECT_ALC) { *volume = audio_forge->volume; return ESP_OK; } else { ESP_LOGE(TAG, " The ALC has not been used."); return ESP_FAIL; } } esp_err_t audio_forge_downmix_set_gain(audio_element_handle_t self, int *gain, int index) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_DOWNMIX)) { return ESP_OK; } if (index > audio_forge->downmix.source_num) { ESP_LOGE(TAG, "The index is out of source_num range"); return ESP_ERR_INVALID_ARG; } if (gain[0] < GAIN_MIN || gain[0] > GAIN_MAX || gain[1] < GAIN_MIN || gain[1] > GAIN_MAX) { ESP_LOGE(TAG, "The gain is out (%d, %d) range", GAIN_MIN, GAIN_MAX); return ESP_ERR_INVALID_ARG; } if ((int)(abs((gain[0] - audio_forge->downmix.source_info[index].gain[0]) * 100)) <= 5 //100 and 5 is to determine if two double numbers are equal. && (int)(abs((gain[1] - audio_forge->downmix.source_info[index].gain[0]) * 100)) <= 5) { return ESP_OK; } audio_forge->reflag |= ADUIO_FORGE_DM_RESTART; audio_forge->downmix.source_info[index].gain[0] = (float)gain[0]; audio_forge->downmix.source_info[index].gain[1] = (float)gain[1]; return ESP_OK; } esp_err_t audio_forge_downmix_set_transit_time(audio_element_handle_t self, int transit_time, int index) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_DOWNMIX)) { return ESP_OK; } if (index > audio_forge->downmix.source_num) { ESP_LOGE(TAG, "The index is out of source_num range"); return ESP_ERR_INVALID_ARG; } if (transit_time < 0) { ESP_LOGE(TAG, "The transit_time (%d) must be equal or gather than zero, line %d", transit_time,__LINE__); return ESP_ERR_INVALID_ARG; } if (audio_forge->downmix.source_info[index].transit_time == transit_time) { return ESP_OK; } audio_forge->reflag |= ADUIO_FORGE_DM_RESTART; audio_forge->downmix.source_info[index].transit_time = transit_time; return ESP_OK; } void audio_forge_downmix_set_input_rb_timeout(audio_element_handle_t self, int tick_to_wait, int index) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); audio_forge->tick_to_wait[index] = tick_to_wait; } void audio_forge_downmix_set_input_rb(audio_element_handle_t self, ringbuf_handle_t rb, int index) { audio_element_set_multi_input_ringbuf(self, rb, index); } esp_err_t audio_forge_eq_set_gain(audio_element_handle_t self, int eq_gain, int band_index) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_EQUALIZER)) { return ESP_OK; } if ((band_index < 0) || (band_index > NUMBER_BAND)) { ESP_LOGE(TAG,"The range of index for audio gain of equalizer should be [0 9]. Here is %d. (line %d)", band_index, __LINE__); return ESP_ERR_INVALID_ARG; } if (audio_forge->channel == 2) { if ((audio_forge->equalizer_gain[band_index] == eq_gain) && (audio_forge->equalizer_gain[NUMBER_BAND * 1 + band_index] == eq_gain)) { return ESP_OK; } audio_forge->reflag |= ADUIO_FORGE_EQ_RESTART; audio_forge->equalizer_gain[NUMBER_BAND * 0 + band_index] = eq_gain; audio_forge->equalizer_gain[NUMBER_BAND * 1 + band_index] = eq_gain; return ESP_OK; } if (audio_forge->equalizer_gain[band_index] == eq_gain) { return ESP_OK; } audio_forge->reflag |= ADUIO_FORGE_EQ_RESTART; audio_forge->equalizer_gain[band_index] = eq_gain; return ESP_OK; } esp_err_t audio_forge_sonic_get_speed(audio_element_handle_t self, float *sonic_speed) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_SONIC)) { *sonic_speed = 1.0; return ESP_OK; } *sonic_speed = audio_forge->sonic_speed; return ESP_OK; } esp_err_t audio_forge_sonic_set_speed(audio_element_handle_t self, float sonic_speed) { if (self == NULL) { ESP_LOGE(TAG, "audio forge handle is NULL. (line %d)", __LINE__); return ESP_FAIL; } audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_SONIC)) { return ESP_OK; } if ((int)(abs((sonic_speed - audio_forge->sonic_speed) * 100)) <= 5) { return ESP_OK; } audio_forge->reflag |= ADUIO_FORGE_SONIC_RESTART; audio_forge->sonic_speed = sonic_speed; return ESP_OK; } esp_err_t audio_forge_sonic_set_component_select(audio_element_handle_t self, int component_select) { if (self == NULL) { ESP_LOGE(TAG, "audio forge handle is NULL. (line %d)", __LINE__); return ESP_FAIL; } audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); audio_forge->component_select = component_select; return ESP_OK; } esp_err_t audio_forge_sonic_set_pitch(audio_element_handle_t self, float sonic_pitch) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (!(audio_forge->component_select & AUDIO_FORGE_SELECT_SONIC)) { return ESP_OK; } if ((int)(abs((sonic_pitch - audio_forge->sonic_pitch) * 100)) <= 5) { //100 and 5 is to determine if two double numbers are equal. return ESP_OK; } audio_forge->reflag |= ADUIO_FORGE_SONIC_RESTART; audio_forge->sonic_pitch = sonic_pitch; return ESP_OK; } esp_err_t audio_forge_set_src_info(audio_element_handle_t self, audio_forge_src_info_t source_info, int index) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); if (index > audio_forge->downmix.source_num) { ESP_LOGE(TAG, "The index is out of source_num range"); return ESP_ERR_INVALID_ARG; } if (source_info.samplerate < SAMPLERATE_MIN || source_info.samplerate > SAMPLERATE_MAX) { ESP_LOGE(TAG, "Samplerate is out of (%d %d) range.", SAMPLERATE_MIN, SAMPLERATE_MAX); return ESP_ERR_INVALID_ARG; } if (source_info.channel != 1 && source_info.channel != 2) { ESP_LOGE(TAG, "The number of channel must be 1 or 2 "); return ESP_ERR_INVALID_ARG; } if (source_info.bit_num != ESP_AUDIO_BIT_16) { ESP_LOGE(TAG, "Currently, the bit width only support 16. Bit width is set %d ", source_info.bit_num); return ESP_ERR_INVALID_ARG; } if ((audio_forge->component_select & AUDIO_FORGE_SELECT_EQUALIZER) && source_info.samplerate != 11025 && source_info.samplerate != 22050 && source_info.samplerate != 44100 && source_info.samplerate != 48000) { ESP_LOGE(TAG, "Unsupported sample rate %d. Currently supported rates are 11025 Hz, 22050 Hz, 44100 Hz and 48000 Hz", source_info.samplerate); return ESP_ERR_INVALID_ARG; } if (!audio_forge->lock){ audio_forge->lock = mutex_create(); } mutex_lock(audio_forge->lock); if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { if (audio_forge->rsp_handle[index]) { if (audio_forge->rsp_info[index].src_rate == source_info.samplerate && audio_forge->rsp_info[index].src_ch == source_info.channel && audio_forge->rsp_info[index].sample_bits == source_info.bit_num) { audio_forge->status &= ~(1 << index); mutex_unlock(audio_forge->lock); return ESP_OK; } else { esp_resample_destroy(audio_forge->rsp_handle[index]); if (audio_forge->inbuf[index] == audio_forge->rsp_out[index]) { audio_forge->inbuf[index] = NULL; } audio_forge->rsp_out[index] = NULL; audio_forge->rsp_in[index] = NULL; audio_forge->rsp_handle[index]= NULL; } } audio_forge->rsp_info[index].src_rate = source_info.samplerate; audio_forge->rsp_info[index].src_ch = source_info.channel; audio_forge->rsp_info[index].sample_bits = source_info.bit_num; if (rsp_open(audio_forge, index) == ESP_FAIL) { mutex_unlock(audio_forge->lock); return ESP_FAIL; } audio_forge->status &= ~(1 << index); mutex_unlock(audio_forge->lock); return ESP_OK; } mutex_unlock(audio_forge->lock); audio_forge->status &= ~(1 << index); if (audio_forge->sample_rate == source_info.samplerate && audio_forge->channel == source_info.channel) { return ESP_OK; } audio_forge->sample_rate = source_info.samplerate; audio_forge->channel = source_info.channel; if (audio_forge->component_select & AUDIO_FORGE_SELECT_DOWNMIX) { audio_forge->reflag |= ADUIO_FORGE_DM_RESTART; return ESP_OK; } if (audio_forge->component_select & AUDIO_FORGE_SELECT_EQUALIZER) { audio_forge->reflag |= ADUIO_FORGE_EQ_RESTART; } if (audio_forge->component_select & AUDIO_FORGE_SELECT_SONIC) { audio_forge->reflag |= ADUIO_FORGE_SONIC_RESTART; } return ESP_OK; } void audio_forge_set_stop_mode(audio_element_handle_t self, audio_forge_stop_mode_t stop_mode) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); audio_forge->stop_mode = stop_mode; } void audio_forge_downmix_start(audio_element_handle_t self) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); audio_forge->downmix.mode = ESP_DOWNMIX_WORK_MODE_SWITCH_ON; for (int i = 1; i < audio_forge->downmix.source_num; i++) { audio_forge->status &= ~(1 << i); } } void audio_forge_downmix_stop(audio_element_handle_t self) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); audio_forge->downmix.mode = ESP_DOWNMIX_WORK_MODE_BYPASS; for (int i = 1; i < audio_forge->downmix.source_num; i++) { audio_forge->status |= (1 << i); } } //if user only uses `downmix`, source streams has same sample rate. esp_err_t audio_forge_source_info_init(audio_element_handle_t self, audio_forge_src_info_t *source_num, audio_forge_downmix_t *downmix_info) { audio_forge_t *audio_forge = (audio_forge_t *)audio_element_getdata(self); for (int i = 0; i < audio_forge->downmix.source_num; i++) { audio_forge->downmix.source_info[i].samplerate = source_num[i].samplerate; audio_forge->downmix.source_info[i].channel = source_num[i].channel; audio_forge->downmix.source_info[i].bits_num = source_num[i].bit_num; audio_forge->downmix.source_info[i].gain[0] = downmix_info[i].gain[0]; audio_forge->downmix.source_info[i].gain[1] = downmix_info[i].gain[1]; audio_forge->downmix.source_info[i].transit_time = downmix_info[i].transit_time; if (audio_forge->component_select & AUDIO_FORGE_SELECT_RESAMPLE) { audio_forge->rsp_info[i].src_rate = audio_forge->downmix.source_info[i].samplerate; audio_forge->rsp_info[i].src_ch = audio_forge->downmix.source_info[i].channel; audio_forge->rsp_info[i].sample_bits = audio_forge->downmix.source_info[i].bits_num; } } return ESP_OK; } audio_element_handle_t audio_forge_init(audio_forge_cfg_t *config) { if (config == NULL) { ESP_LOGE(TAG, "config is NULL. (line %d)", __LINE__); return NULL; } audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG(); audio_element_handle_t el; cfg.open = audio_forge_open; cfg.close = audio_forge_close; cfg.process = audio_forge_process; cfg.destroy = audio_forge_destroy; cfg.task_stack = config->task_stack; cfg.task_prio = config->task_prio; cfg.task_core = config->task_core; cfg.out_rb_size = config->out_rb_size; cfg.stack_in_ext = config->stack_in_ext; config->audio_forge.source_num = config->audio_forge.source_num >= 1 ? config->audio_forge.source_num : 1; cfg.multi_in_rb_num = config->audio_forge.source_num; cfg.tag = "audio_forge"; el = audio_element_init(&cfg); AUDIO_MEM_CHECK(TAG, el, return NULL); audio_forge_t *audio_forge = audio_calloc(1, sizeof(audio_forge_t)); AUDIO_MEM_CHECK(TAG, audio_forge, return NULL); audio_element_setdata(el, audio_forge); audio_forge->sample_rate = config->audio_forge.dest_samplerate; audio_forge->channel = config->audio_forge.dest_channel; audio_forge->rsp_cplx = config->audio_forge.rsp_cplx; audio_forge->volume = config->audio_forge.alc_volume; audio_forge->sonic_pitch = config->audio_forge.sonic_pitch; audio_forge->sonic_speed = config->audio_forge.sonic_speed; audio_forge->component_select = config->audio_forge.component_select; audio_forge->max_sample = config->audio_forge.max_sample; audio_forge->downmix.source_num = config->audio_forge.source_num; audio_forge->stop_mode = config->audio_forge.stop_mode; if (audio_forge->equalizer_gain == NULL) { audio_forge->equalizer_gain = audio_calloc(1, NUMBER_BAND * 2 * sizeof(int)); AUDIO_MEM_CHECK(TAG, audio_forge->equalizer_gain, { goto exit; }); } audio_forge->downmix.source_info = audio_calloc(audio_forge->downmix.source_num, sizeof(esp_downmix_input_info_t)); AUDIO_MEM_CHECK(TAG, audio_forge->downmix.source_info, { goto exit; }); audio_forge->inbuf = audio_calloc(audio_forge->downmix.source_num, sizeof(unsigned char *)); AUDIO_MEM_CHECK(TAG, audio_forge->inbuf, { goto exit; }); audio_forge->rsp_handle = audio_calloc(audio_forge->downmix.source_num, sizeof(void *)); AUDIO_MEM_CHECK(TAG, audio_forge->rsp_handle, { goto exit; }); audio_forge->rsp_info = audio_calloc(audio_forge->downmix.source_num, sizeof(resample_info_t)); AUDIO_MEM_CHECK(TAG, audio_forge->rsp_info, { goto exit; }); audio_forge->rsp_in = audio_calloc(audio_forge->downmix.source_num, sizeof(unsigned char *)); AUDIO_MEM_CHECK(TAG, audio_forge->rsp_in, { goto exit; }); audio_forge->rsp_out = audio_calloc(audio_forge->downmix.source_num, sizeof(unsigned char *)); AUDIO_MEM_CHECK(TAG, audio_forge->rsp_out, { goto exit; }); audio_forge->in_offset = audio_calloc(audio_forge->downmix.source_num, sizeof(int)); AUDIO_MEM_CHECK(TAG, audio_forge->in_offset, { goto exit; }); audio_forge->tick_to_wait = audio_calloc(audio_forge->downmix.source_num, sizeof(int)); AUDIO_MEM_CHECK(TAG, audio_forge->tick_to_wait, { goto exit; }); return el; exit: audio_forge_destroy(el); return NULL; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/audio_forge.c
C
apache-2.0
48,863
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #include <string.h> #include "esp_log.h" #include "audio_error.h" #include "audio_mem.h" #include "audio_element.h" #include "esp_sonic.h" #include "audio_sonic.h" #include "audio_type_def.h" static const char *TAG = "SONIC"; #define BUF_SIZE (512) // #define DEBUG_SONIC_ENC_ISSUE // #define SONIC_MEMORY_ANALYSIS #ifdef AUDIO_SONIC_SPEED_ANALYSIS #include "codec_tick.h" #endif #ifdef DEBUG_SONIC_ENC_ISSUE FILE *inone; FILE *outone; #endif typedef struct { sonic_info_t sonic_info; int emulate_chord_pitch; int enable_nonlinear_speedup; int quality; void *sonic_handle; float volume; float rate; float pitch; float speed; int samplerate; int channel; short *inbuf; short *outbuf; #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_handle_t *sonic_tick_handle; #endif } sonic_t; static esp_err_t is_valid_sonic_samplerate(int samplerate) { if (samplerate < 8000 || samplerate > 48000) { ESP_LOGE(TAG, "The sample rate should be within range [8000,48000], here is %d Hz", samplerate); return ESP_ERR_INVALID_ARG; } return ESP_OK; } static esp_err_t is_valid_sonic_channel(int channel) { if (channel != 1 && channel != 2) { ESP_LOGE(TAG, "The number of channels should be either 1 or 2, here is %d", channel); return ESP_ERR_INVALID_ARG;; } return ESP_OK; } esp_err_t sonic_set_info(audio_element_handle_t self, int rate, int ch) { sonic_t *sonic = (sonic_t *)audio_element_getdata(self); if (sonic->samplerate == rate && sonic->channel == ch) { return ESP_OK; } if (is_valid_sonic_samplerate(rate) != ESP_OK || is_valid_sonic_channel(ch) != ESP_OK) { return ESP_ERR_INVALID_ARG;; } else { sonic->samplerate = rate; sonic->channel = ch; ESP_LOGI(TAG, "reset sample rate of stream data : %d, reset channel of stream data : %d", sonic->samplerate, sonic->channel); } return ESP_OK; } esp_err_t sonic_set_pitch_and_speed_info(audio_element_handle_t self, float pitch, float speed) { sonic_t *sonic = (sonic_t *)audio_element_getdata(self); if ((pitch - 0 < 0.0001) && (speed - 0 < 0.0001)) { return ESP_OK; } if ((pitch >= 0.2) && (pitch <= 4.0)) { sonic->pitch = pitch; } else if ((pitch - 0) > 0.0001) { ESP_LOGE(TAG, "The pitch must be in [0.2 4.0],reset pitch of stream data is %.2f .", pitch); return ESP_ERR_INVALID_ARG;; } if ((speed >= 0.1) && (speed <= 8.0)) { sonic->speed = speed; } else if ((speed - 0) > 0.0001) { ESP_LOGE(TAG, "The speed must be in [0.1 8.0],reset speed of stream data is %.2f . ", speed); return ESP_ERR_INVALID_ARG;; } ESP_LOGI(TAG, "The reset pitch and speed of stream data are %.2f and %.2f respectively ", sonic->pitch, sonic->speed); return ESP_OK; } static esp_err_t sonic_destroy(audio_element_handle_t self) { sonic_t *sonic = (sonic_t *)audio_element_getdata(self); audio_free(sonic); return ESP_OK; } static esp_err_t sonic_open(audio_element_handle_t self) { #ifdef SONIC_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif ESP_LOGD(TAG, "sonic_open"); sonic_t *sonic = (sonic_t *)audio_element_getdata(self); #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_config_t tick_config[4] = { { (const char *)"sonic_whole_time", esp_tick_time_ms}, { (const char *)"sonic_write_time", cpu_tick_time_ms}, { (const char *)"sonic_read_time", cpu_tick_time_ms}, { (const char *)"sonic_write_to_stream_time", cpu_tick_time_ms}, }; sonic->sonic_tick_handle = codec_tick_init(tick_config, 4); #endif sonic->inbuf = (short *)calloc(sizeof(short), BUF_SIZE); if (sonic->inbuf == NULL) { ESP_LOGE(TAG, "Failed to allocate input buffer"); return ESP_ERR_NO_MEM; } sonic->outbuf = (short *)calloc(sizeof(short), BUF_SIZE); if (sonic->outbuf == NULL) { ESP_LOGE(TAG, "Failed to allocate output buffer"); return ESP_ERR_NO_MEM; } sonic->emulate_chord_pitch = 0; sonic->enable_nonlinear_speedup = 0; sonic->quality = 0; sonic->volume = SONIC_SET_VALUE_FOR_INITIALIZATION; sonic->rate = SONIC_SET_VALUE_FOR_INITIALIZATION; sonic->sonic_info.pitch = sonic->pitch; sonic->sonic_info.speed = sonic->speed; sonic->sonic_info.samplerate = sonic->samplerate; sonic->sonic_info.channel = sonic->channel; sonic->sonic_handle = esp_sonic_create_stream(sonic->sonic_info.samplerate, sonic->sonic_info.channel); if (sonic->sonic_handle == NULL) { ESP_LOGE(TAG, "The handle of sonic is NULL"); return ESP_FAIL; } esp_sonic_set_resample_mode(sonic->sonic_handle, sonic->sonic_info.resample_linear_interpolate); esp_sonic_set_speed(sonic->sonic_handle, sonic->sonic_info.speed); esp_sonic_set_pitch(sonic->sonic_handle, sonic->sonic_info.pitch); esp_sonic_set_rate(sonic->sonic_handle, sonic->rate); esp_sonic_set_volume(sonic->sonic_handle, sonic->volume); esp_sonic_set_chord_pitch(sonic->sonic_handle, sonic->emulate_chord_pitch); esp_sonic_set_quality(sonic->sonic_handle, sonic->quality); #ifdef DEBUG_SONIC_ENC_ISSUE char fileName1[100] = {'//', 's', 'd', 'c', 'a', 'r', 'd', '//', 't', 'e', 's', 't', '1', '.', 'p', 'c', 'm', '\0'}; inone = fopen(fileName1, "rb"); if (!inone) { perror(fileName1); return ESP_FAIL; } char fileName[100] = {'//', 's', 'd', 'c', 'a', 'r', 'd', '//', 't', '.', 'p', 'c', 'm', '\0'}; outone = fopen(fileName, "w"); if (!outone) { perror(fileName); return ESP_FAIL; } #endif #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_start(sonic->sonic_tick_handle, 0); #endif return ESP_OK; } static esp_err_t sonic_close(audio_element_handle_t self) { ESP_LOGD(TAG, "sonic_close"); sonic_t *sonic = (sonic_t *)audio_element_getdata(self); #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_stop(sonic->sonic_tick_handle, 0, false); //int realtime = 1000 * audio_forge->pcm_cnt / (audio_forge->rsp_info[0].src_ch * audio_forge->rsp_info[0].src_rate * sizeof(short)); //int realtime = 1000 * audio_forge->pcm_cnt / (audio_forge->sample_rate * audio_forge->channel * sizeof(short)); //audio_forge->pcm_cnt = 0; codec_tick_printf(sonic->sonic_tick_handle, 1); codec_tick_deinit(sonic->sonic_tick_handle); sonic->sonic_tick_handle = NULL; #endif if (sonic->sonic_handle != NULL) { esp_sonic_destroy_stream(sonic->sonic_handle); } if (sonic->inbuf != NULL) { audio_free(sonic->inbuf); sonic->inbuf = NULL; } if (sonic->outbuf != NULL) { audio_free(sonic->outbuf); sonic->outbuf = NULL; } #ifdef SONIC_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif #ifdef DEBUG_SONIC_ENC_ISSUE fclose(inone); fclose(outone); #endif return ESP_OK; } static int sonic_process(audio_element_handle_t self, char *in_buffer, int in_len) { sonic_t *sonic = (sonic_t *)audio_element_getdata(self); int ret = 1; if ((sonic->samplerate != sonic->sonic_info.samplerate) || (sonic->channel != sonic->sonic_info.channel) || (sonic->pitch != sonic->sonic_info.pitch) || (sonic->speed != sonic->sonic_info.speed)) { ret = sonic_close(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } sonic_open(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } return ESP_CODEC_ERR_CONTINUE; } int samplesRead = 0; int samplesWritten = 0; int out_buffer_len = 0; #ifdef DEBUG_SONIC_ENC_ISSUE samplesRead = fread(sonic->inbuf, sizeof(short), BUF_SIZE / sonic->sonic_info.channel, inone); #else samplesRead = audio_element_input(self, (char *)sonic->inbuf, BUF_SIZE * sizeof(short)); #endif if (samplesRead > 0) { samplesRead = samplesRead / (sonic->sonic_info.channel * sizeof(short)); #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_start(sonic->sonic_tick_handle, 3); #endif esp_sonic_write_to_stream(sonic->sonic_handle, sonic->inbuf, samplesRead); #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_stop(sonic->sonic_tick_handle, 3, true); #endif do { #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_start(sonic->sonic_tick_handle, 2); #endif samplesWritten = esp_sonic_read_from_stream(sonic->sonic_handle, sonic->outbuf, BUF_SIZE / sonic->sonic_info.channel); #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_stop(sonic->sonic_tick_handle, 2, true); #endif if (samplesWritten > 0) { #ifdef DEBUG_SONIC_ENC_ISSUE ret = fwrite(sonic->outbuf, sizeof(short), samplesWritten, outone); #else out_buffer_len = samplesWritten * sonic->sonic_info.channel > BUF_SIZE ? BUF_SIZE : samplesWritten * sonic->sonic_info.channel; #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_start(sonic->sonic_tick_handle, 1); #endif ret = audio_element_output(self, (char *)sonic->outbuf, out_buffer_len * sizeof(short)); #ifdef AUDIO_SONIC_SPEED_ANALYSIS codec_tick_count_stop(sonic->sonic_tick_handle, 1, true); #endif #endif } } while (samplesWritten > 0); } else { ret = samplesRead; } return ret; } audio_element_handle_t sonic_init(sonic_cfg_t *config) { if (config == NULL) { ESP_LOGE(TAG, "config is NULL. (line %d)", __LINE__); return NULL; } sonic_t *sonic = audio_calloc(1, sizeof(sonic_t)); AUDIO_MEM_CHECK(TAG, sonic, return NULL); audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG(); cfg.destroy = sonic_destroy; cfg.process = sonic_process; cfg.open = sonic_open; cfg.close = sonic_close; cfg.buffer_len = 0; cfg.tag = "sonic"; cfg.task_stack = config->task_stack; cfg.task_prio = config->task_prio; cfg.task_core = config->task_core; cfg.out_rb_size = config->out_rb_size; cfg.stack_in_ext = config->stack_in_ext; audio_element_handle_t el = audio_element_init(&cfg); AUDIO_MEM_CHECK(TAG, el, {audio_free(sonic); return NULL;}); sonic->sonic_info = config->sonic_info; sonic->pitch = sonic->sonic_info.pitch; sonic->speed = sonic->sonic_info.speed; sonic->samplerate = sonic->sonic_info.samplerate; sonic->channel = sonic->sonic_info.channel; audio_element_setdata(el, sonic); audio_element_info_t info = {0}; audio_element_setinfo(el, &info); ESP_LOGD(TAG, "sonic_init"); return el; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/audio_sonic.c
C
apache-2.0
10,805
// Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> // All rights reserved. #include <string.h> #include "esp_log.h" #include "audio_error.h" #include "audio_mem.h" #include "audio_element.h" #include "esp_downmix.h" #include "downmix.h" #include "audio_type_def.h" static const char *TAG = "DOWNMIX"; // #define DEBUG_DOWNMIX_ISSUE // #define DOWNMIX_MEMORY_ANALYSIS typedef struct { esp_downmix_info_t downmix_info; int max_sample; unsigned char **inbuf; unsigned char *outbuf; void *downmix_handle; int* ticks_to_wait; int reset_flag; } downmix_t; #ifdef DEBUG_DOWNMIX_ISSUE static FILE *inputtwo[SOURCE_NUM_MAX] = {NULL}; static FILE *output = NULL; char filein1[10][100] = {0}; char fileOut[100] = {0}; int loocpcm = 0; #endif static esp_err_t downmix_destroy(audio_element_handle_t self) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix->downmix_info.source_info) { audio_free(downmix->downmix_info.source_info); } if (downmix->ticks_to_wait) { audio_free(downmix->ticks_to_wait); downmix->ticks_to_wait = NULL; } audio_free(downmix); downmix = NULL; return ESP_OK; } static esp_err_t downmix_open(audio_element_handle_t self) { #ifdef DOWNMIX_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif ESP_LOGD(TAG, "downmix_open"); downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix->downmix_info.source_num <= 0) { ESP_LOGE(TAG, "the number of source stream is less than 1"); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.mode >= ESP_DOWNMIX_WORK_MODE_MAX) { ESP_LOGE(TAG, "The downmix work mode is greater than ESP_DOWNMIX_WORK_MODE_SWITCH_OFF, %d", downmix->downmix_info.mode); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.output_type >= ESP_DOWNMIX_OUTPUT_TYPE_MAX) { ESP_LOGE(TAG, "The downmix work mode is greater than or equal to ESP_DOWNMIX_OUTPUT_TYPE_MAX, %d", downmix->downmix_info.output_type); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.out_ctx >= ESP_DOWNMIX_OUT_CTX_MAX) { ESP_LOGE(TAG, "The downmix out_ctx is greater than or equal to ESP_DOWNMIX_OUT_CTX_MAX, %d\n", downmix->downmix_info.out_ctx); return ESP_ERR_INVALID_ARG; } if (downmix->max_sample <= 0) { ESP_LOGE(TAG, "The number of sample per downmix processing must greater than zero. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } downmix->inbuf = (unsigned char **)audio_calloc(1, downmix->downmix_info.source_num * sizeof(unsigned char *)); if (downmix->inbuf == NULL) { ESP_LOGE(TAG, "Failed to audio_calloc. (line %d)", __LINE__); return ESP_ERR_NO_MEM; } int samplerate = downmix->downmix_info.source_info[0].samplerate; for (int i = 0; i < downmix->downmix_info.source_num; i++) { if ((samplerate != downmix->downmix_info.source_info[i].samplerate) && (downmix->downmix_info.source_info[i].samplerate < SAMPLERATE_MIN || downmix->downmix_info.source_info[i].samplerate >= SAMPLERATE_MAX)) { ESP_LOGE(TAG, "Unsupported sample rate. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } if ((downmix->downmix_info.source_info[i].channel != 1) && (downmix->downmix_info.source_info[i].channel != 2)) { ESP_LOGE(TAG, "The number of channels should be either 1 or 2. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.source_info[i].gain[0] < GAIN_MIN || downmix->downmix_info.source_info[i].gain[0] > GAIN_MAX || downmix->downmix_info.source_info[i].gain[1] < GAIN_MIN || downmix->downmix_info.source_info[i].gain[1] > GAIN_MAX) { ESP_LOGE(TAG, "The gain is out of [%d, %d] range", GAIN_MIN, GAIN_MAX); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.source_info[i].transit_time < 0) { ESP_LOGE(TAG, "The transit time (%d) must be greater than or equal to zero", downmix->downmix_info.source_info[0].transit_time); return ESP_ERR_INVALID_ARG; } downmix->downmix_info.source_info[i].bits_num = 16; downmix->inbuf[i] = (unsigned char *)audio_calloc(1, downmix->max_sample * 2 * sizeof(short)); if (downmix->inbuf[i] == NULL) { ESP_LOGE(TAG, "Failed to audio_calloc of downmix->inbuf[%d]. (line %d)", i, __LINE__); return ESP_ERR_NO_MEM; } } downmix->outbuf = (unsigned char *)audio_calloc(1, downmix->max_sample * 2 * sizeof(short)); if (downmix->outbuf == NULL) { ESP_LOGE(TAG, "audio_calloc failed for downmix->outbuf. (line %d)", __LINE__); return ESP_ERR_NO_MEM; } downmix->reset_flag = 0; esp_downmix_info_t esp_downmix_info; memcpy(&esp_downmix_info, &downmix->downmix_info, sizeof(esp_downmix_info_t)); downmix->downmix_handle = esp_downmix_open(&esp_downmix_info); if (downmix->downmix_handle == NULL) { ESP_LOGE(TAG, "downmix initialization failed, (line %d)", __LINE__); return ESP_FAIL; } #ifdef DEBUG_DOWNMIX_ISSUE loocpcm = 0; for (int i = 0; i < downmix->downmix_info.source_num; i++) { inputtwo[i] = fopen(filein1[i], "rb"); if (!inputtwo[i]) { perror(filein1); inputtwo[i] = NULL; return ESP_FAIL; } } output = fopen(fileOut, "wb"); if (!output) { perror(fileOut); return ESP_FAIL; } #endif #ifdef DOWNMIX_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif return ESP_OK; } static esp_err_t downmix_close(audio_element_handle_t self) { ESP_LOGD(TAG, "downmix_close"); downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix->downmix_handle != NULL) { esp_downmix_close(downmix->downmix_handle); } #ifdef DEBUG_DOWNMDX_ISSUE for (int i = 0; i < downmix->downmix_info.source_num; i++) { if (inputtwo[i] != NULL) { fclose(inputtwo[i]); } } if (output != NULL) { fclose(output); } #endif for (int i = 0; i < downmix->downmix_info.source_num; i++) { if (downmix->inbuf[i] != NULL) { memset(downmix->inbuf[i], 0, downmix->max_sample * 2 * sizeof(short)); audio_free(downmix->inbuf[i]); downmix->inbuf[i] = NULL; } } if (downmix->outbuf != NULL) { audio_free(downmix->outbuf); downmix->outbuf = NULL; } #ifdef DOWNMIX_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif return ESP_OK; } static int downmix_process(audio_element_handle_t self, char *in_buffer, int in_len) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "downmix is NULL(line %d)", __LINE__); return ESP_FAIL; } int r_size = downmix->max_sample * sizeof(short); int ret = 0; int bytes[SOURCE_NUM_MAX] = {0}; int status_end = 0; #ifdef DEBUG_DOWNMIX_ISSUE loocpcm++; if (loocpcm == 0) { downmix->downmix_info.mode = ESP_DOWNMIX_WORK_MODE_SWITCH_ON; } if (loocpcm == 1) { downmix->downmix_info.mode = ESP_DOWNMIX_WORK_MODE_SWITCH_OFF; } if (loocpcm >= 300) { return ESP_OK; } #endif if (downmix->reset_flag == 1) { ret = downmix_close(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } ret = downmix_open(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } ESP_LOGI(TAG, "Reopen downmix"); return ESP_CODEC_ERR_CONTINUE; } esp_downmix_info_t downmix_info; memcpy(&downmix_info, &downmix->downmix_info, sizeof(esp_downmix_info_t)); #ifdef DEBUG_DOWNMIX_ISSUE for (int i = 0; i < downmix->downmix_info.source_num; i++) { bytes[i] = fread((char *)downmix->inbuf[i], 1, r_size * downmix->downmix_info.source_info[i].channel, inputtwo[i]); if (bytes[i] <= 0) { memset(downmix->inbuf[i], 0, downmix->max_sample * 2 * sizeof(short)); } else if (bytes[i] != r_size * downmix->downmix_info.source_info[i].channel) { memset(downmix->inbuf[i] + bytes[i], 0, downmix->max_sample * 2 * sizeof(short) - bytes[i]); } if (loocpcm == 3) { printf("inputpcm[%d]: %d byte ", i, r_size * downmix->downmix_info.source_info[0].channel); } } #else int len = downmix_info.source_num; if (downmix->downmix_info.mode == ESP_DOWNMIX_WORK_MODE_BYPASS) { len = 1; } for (int index = 0; index < len; index++) { memset(downmix->inbuf[index], 0, downmix->max_sample * 2 * sizeof(short)); bytes[index] = audio_element_multi_input(self, (char *)downmix->inbuf[index], r_size * downmix_info.source_info[index].channel, index, downmix->ticks_to_wait[index]); if (bytes[index] < 0) { memset(downmix->inbuf[index], 0, downmix->max_sample * 2 * sizeof(short)); if ((bytes[index] != AEL_IO_TIMEOUT)) { status_end++; } } else if (bytes[index] != r_size * downmix->downmix_info.source_info[index].channel) { memset(downmix->inbuf[index] + bytes[index], 0, downmix->max_sample * 2 * sizeof(short) - bytes[index]); } ESP_LOGD(TAG, "bytes[ %d ] = %d", index, bytes[index]); } #endif //down-mixer finished if (status_end == downmix->downmix_info.source_num || (status_end == 1 && downmix->downmix_info.mode == ESP_DOWNMIX_WORK_MODE_BYPASS)) { return ESP_OK; } ret = esp_downmix_process(downmix->downmix_handle, downmix->inbuf, downmix->outbuf, downmix->max_sample, downmix->downmix_info.mode); #ifdef DEBUG_DOWNMIX_ISSUE ret = fwrite((char *)downmix->outbuf, 1, ret, output); if (loocpcm == 3) { printf("outputpcm: %d byte\n", ret); } #else ret = audio_element_output(self, (char *)downmix->outbuf, ret); #endif return ret; } void downmix_set_input_rb_timeout(audio_element_handle_t self, int ticks_to_wait, int index) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return; } if (index >= downmix->downmix_info.source_num) { ESP_LOGE(TAG, "the index of source number is out of range. line %d", __LINE__); return; } downmix->ticks_to_wait[index] = ticks_to_wait; } void downmix_set_input_rb(audio_element_handle_t self, ringbuf_handle_t rb, int index) { audio_element_set_multi_input_ringbuf(self, rb, index); } esp_err_t downmix_set_work_mode(audio_element_handle_t self, esp_downmix_work_mode_t mode) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.mode == mode) { return ESP_OK; } if (mode >= ESP_DOWNMIX_WORK_MODE_MAX) { ESP_LOGE(TAG, "The set downmix work mode is greater than ESP_DOWNMIX_WORK_MODE_MAX, %d", mode); return ESP_ERR_INVALID_ARG; } downmix->downmix_info.mode = mode; return ESP_OK; } esp_err_t downmix_set_output_type(audio_element_handle_t self, esp_downmix_output_type_t output_type) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.output_type == output_type) { return ESP_OK; } if (output_type >= ESP_DOWNMIX_OUTPUT_TYPE_MAX) { ESP_LOGE(TAG, "The set downmix output type is greater than or equal to ESP_DOWNMIX_OUTPUT_TYPE_MAX, %d", output_type); return ESP_ERR_INVALID_ARG; } downmix->downmix_info.output_type = output_type; downmix->reset_flag = 1; return ESP_OK; } esp_err_t downmix_set_out_ctx_info(audio_element_handle_t self, esp_downmix_out_ctx_type_t out_ctx) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.out_ctx == out_ctx) { return ESP_OK; } if (out_ctx >= ESP_DOWNMIX_OUT_CTX_MAX) { ESP_LOGE(TAG, "The set downmix out_ctx is greater than or equal to ESP_DOWNMIX_OUT_CTX_MAX, %d", out_ctx); return ESP_ERR_INVALID_ARG; } downmix->downmix_info.out_ctx = out_ctx; downmix->reset_flag = 1; return ESP_OK; } esp_err_t downmix_set_source_stream_info(audio_element_handle_t self, int rate, int ch, int index) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (index >= downmix->downmix_info.source_num) { ESP_LOGE(TAG, "the index of source number is out of range. line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (rate != downmix->downmix_info.source_info[index].samplerate) { ESP_LOGE(TAG, "The samplerates must be same as origin. set sample rate: %d, origin sample rate: %d (line %d)", rate, downmix->downmix_info.source_info[index].samplerate, __LINE__); return ESP_ERR_INVALID_ARG; } if (ch == downmix->downmix_info.source_info[index].channel) { return ESP_OK; } else { if ((ch != 1) && (ch != 2)) { ESP_LOGE(TAG, "The number of channels for stream is error. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } } downmix->reset_flag = 1; downmix->downmix_info.source_info[index].samplerate = rate; downmix->downmix_info.source_info[index].channel = ch; return ESP_OK; } esp_err_t downmix_set_gain_info(audio_element_handle_t self, float *gain, int index) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (index >= downmix->downmix_info.source_num) { ESP_LOGE(TAG, "the index of source number is out of range. line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if ((downmix->downmix_info.source_info[index].gain[0] - gain[0] < 0.01) && (downmix->downmix_info.source_info[index].gain[0] - gain[0] > 0.01) && (downmix->downmix_info.source_info[index].gain[1] - gain[1] < 0.01) && (downmix->downmix_info.source_info[index].gain[1] - gain[1] > 0.01)) { return ESP_OK; } if (gain[0] < GAIN_MIN || gain[0] > GAIN_MAX || gain[1] < GAIN_MIN || gain[1] > GAIN_MAX) { ESP_LOGE(TAG, "The gain is out of range [%d, %d]", GAIN_MIN, GAIN_MAX); return ESP_ERR_INVALID_ARG; } downmix->reset_flag = 1; downmix->downmix_info.source_info[index].gain[0] = gain[0]; downmix->downmix_info.source_info[index].gain[1] = gain[1]; return ESP_OK; } esp_err_t downmix_set_transit_time_info(audio_element_handle_t self, int transit_time, int index) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); if (downmix == NULL) { ESP_LOGE(TAG, "the down-mix handle is NULL, line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (index >= downmix->downmix_info.source_num) { ESP_LOGE(TAG, "the index of source number is out of range. line %d", __LINE__); return ESP_ERR_INVALID_ARG; } if (downmix->downmix_info.source_info[index].transit_time == transit_time) { return ESP_OK; } if (transit_time < 0) { ESP_LOGE(TAG, "The set transit_time must be greater than or equal to zero (%d)", transit_time); return ESP_ERR_INVALID_ARG; } downmix->reset_flag = 1; downmix->downmix_info.source_info[index].transit_time = transit_time; return ESP_OK; } esp_err_t source_info_init(audio_element_handle_t self, esp_downmix_input_info_t *source_num) { downmix_t *downmix = (downmix_t *)audio_element_getdata(self); for (int i = 0; i < downmix->downmix_info.source_num; i++) { downmix->downmix_info.source_info[i].samplerate = source_num[i].samplerate; downmix->downmix_info.source_info[i].channel = source_num[i].channel; downmix->downmix_info.source_info[i].bits_num = source_num[i].bits_num; downmix->downmix_info.source_info[i].gain[0] = source_num[i].gain[0]; downmix->downmix_info.source_info[i].gain[1] = source_num[i].gain[1]; downmix->downmix_info.source_info[i].transit_time = source_num[i].transit_time; } return ESP_OK; } audio_element_handle_t downmix_init(downmix_cfg_t *config) { if (config == NULL) { ESP_LOGE(TAG, "config is NULL. (line %d)", __LINE__); return NULL; } downmix_t *downmix = audio_calloc(1, sizeof(downmix_t)); AUDIO_MEM_CHECK(TAG, downmix, return NULL); audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG(); cfg.destroy = downmix_destroy; cfg.process = downmix_process; cfg.open = downmix_open; cfg.close = downmix_close; cfg.buffer_len = 0; cfg.tag = "downmix"; cfg.task_stack = config->task_stack; cfg.task_prio = config->task_prio; cfg.task_core = config->task_core; cfg.out_rb_size = config->out_rb_size; cfg.stack_in_ext = config->stack_in_ext; cfg.multi_in_rb_num = config->downmix_info.source_num; audio_element_handle_t el = audio_element_init(&cfg); AUDIO_MEM_CHECK(TAG, el, {audio_free(downmix); return NULL;}); memcpy(downmix, config, sizeof(esp_downmix_info_t)); downmix->max_sample = config->max_sample; downmix->reset_flag = 0; downmix->downmix_info.source_info = audio_calloc(downmix->downmix_info.source_num, sizeof(esp_downmix_input_info_t)); if (downmix->downmix_info.source_info == NULL) { ESP_LOGE(TAG, "Failed to audio_calloc. (line %d)", __LINE__); audio_free(downmix); return NULL; } downmix->ticks_to_wait = (int*)audio_calloc(downmix->downmix_info.source_num, sizeof(int)); if (downmix->ticks_to_wait == NULL) { ESP_LOGE(TAG, "audio_calloc failed for downmix->ticks_to_wait. (line %d)", __LINE__); audio_free(downmix->downmix_info.source_info); audio_free(downmix); return NULL; } audio_element_setdata(el, downmix); ESP_LOGD(TAG, "downmix_init"); return el; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/downmix.c
C
apache-2.0
19,363
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #include <string.h> #include "esp_log.h" #include "audio_error.h" #include "audio_mem.h" #include "audio_element.h" #include "esp_equalizer.h" #include "equalizer.h" #include "audio_type_def.h" static const char *TAG = "EQUALIZER"; #define BUF_SIZE (100) #define NUMBER_BAND (10) #define USE_XMMS_ORIGINAL_FREQENT (0) // #define EQUALIZER_MEMORY_ANALYSIS // #define DEBUG_EQUALIZER_ENC_ISSUE typedef struct equalizer { int samplerate; int channel; int *set_gain; int num_band; int use_xmms_original_freqs; unsigned char *buf; void *eq_handle; int byte_num; int at_eof; int gain_flag; } equalizer_t; int set_value_gain[NUMBER_BAND * 2] = { -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13}; #ifdef DEBUG_EQUALIZER_ENC_ISSUE static FILE *infile; #endif static esp_err_t is_valid_equalizer_samplerate(int samplerate) { if ((samplerate != 11025) && (samplerate != 22050) && (samplerate != 44100) && (samplerate != 48000)) { ESP_LOGE(TAG, "The sample rate should be only 11025Hz, 22050Hz, 44100Hz, 48000Hz, here is %dHz. (line %d)", samplerate, __LINE__); return ESP_ERR_INVALID_ARG; } return ESP_OK; } static esp_err_t is_valid_equalizer_channel(int channel) { if ((channel != 1) && (channel != 2)) { ESP_LOGE(TAG, "The number of channels should be only 1 or 2, here is %d. (line %d)", channel, __LINE__); return ESP_ERR_INVALID_ARG; } return ESP_OK; } esp_err_t equalizer_set_info(audio_element_handle_t self, int rate, int ch) { equalizer_t *equalizer = (equalizer_t *)audio_element_getdata(self); if (equalizer->samplerate == rate && equalizer->channel == ch) { return ESP_OK; } if ((is_valid_equalizer_samplerate(rate) != ESP_OK) || (is_valid_equalizer_channel(ch) != ESP_OK)) { return ESP_ERR_INVALID_ARG; } else { ESP_LOGI(TAG, "The reset sample rate and channel of audio stream are %d %d.", rate, ch); equalizer->gain_flag = 1; equalizer->samplerate = rate; equalizer->channel = ch; } return ESP_OK; } esp_err_t equalizer_set_gain_info(audio_element_handle_t self, int index, int value_gain, bool is_channels_gain_equal) { equalizer_t *equalizer = (equalizer_t *)audio_element_getdata(self); if ((equalizer->channel == 2) && (is_channels_gain_equal == true)) { if ((index < 0) || (index > NUMBER_BAND)) { ESP_LOGE(TAG, "The range of index for audio gain of equalizer should be [0 9]. Here is %d. (line %d)", index, __LINE__); return ESP_ERR_INVALID_ARG; } int pos_index_channel = index + (equalizer->channel - 1) * NUMBER_BAND; if ((equalizer->set_gain[index] == value_gain) && (equalizer->set_gain[pos_index_channel] == value_gain)) { return ESP_OK; } ESP_LOGI(TAG, "The reset gain[%d] and gain[%d] of audio stream are %d.", index, pos_index_channel, value_gain); equalizer->gain_flag = 1; equalizer->set_gain[index] = value_gain; equalizer->set_gain[pos_index_channel] = value_gain; return ESP_OK; } else { if ((index < 0) || (index > NUMBER_BAND * 2)) { ESP_LOGE(TAG, "The number of channels is %d. The range of index for audio gain of equalizer should be [0 %d]. Here is %d. (line %d)", equalizer->channel, (equalizer->channel * NUMBER_BAND - 1), index, __LINE__); return ESP_ERR_INVALID_ARG; } if (equalizer->set_gain[index] == value_gain) { return ESP_OK; } ESP_LOGI(TAG, "The reset gain[%d] of audio stream is %d.", index, value_gain); equalizer->gain_flag = 1; equalizer->set_gain[index] = value_gain; return ESP_OK; } } static esp_err_t equalizer_destroy(audio_element_handle_t self) { equalizer_t *equalizer = (equalizer_t *)audio_element_getdata(self); audio_free(equalizer); return ESP_OK; } static esp_err_t equalizer_open(audio_element_handle_t self) { #ifdef EQUALIZER_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif ESP_LOGD(TAG, "equalizer_open"); equalizer_t *equalizer = (equalizer_t *)audio_element_getdata(self); if (equalizer->set_gain == NULL) { ESP_LOGE(TAG, "The gain array should be set. (line %d)", __LINE__); return ESP_ERR_INVALID_ARG; } audio_element_info_t info = {0}; audio_element_getinfo(self, &info); if (info.sample_rates && info.channels) { equalizer->samplerate = info.sample_rates; equalizer->channel = info.channels; } equalizer->num_band = NUMBER_BAND; equalizer->use_xmms_original_freqs = USE_XMMS_ORIGINAL_FREQENT; equalizer->eq_handle = NULL; equalizer->at_eof = 0; equalizer->gain_flag = 0; if (equalizer->num_band != 10 && equalizer->num_band != 15 && equalizer->num_band != 25 && equalizer->num_band != 31) { ESP_LOGE(TAG, "The number of bands should be one of 10, 15, 25, 31, here is %d. (line %d)", equalizer->num_band, __LINE__); return ESP_ERR_INVALID_ARG; } if (is_valid_equalizer_samplerate(equalizer->samplerate) != ESP_OK || is_valid_equalizer_channel(equalizer->channel) != ESP_OK) { return ESP_ERR_INVALID_ARG; } if (equalizer->use_xmms_original_freqs != 0 && equalizer->use_xmms_original_freqs != 1) { ESP_LOGE(TAG, "The use_xmms_original_freqs should be only 0 or 1, here is %d. (line %d)", equalizer->use_xmms_original_freqs, __LINE__); return ESP_ERR_INVALID_ARG; } equalizer->buf = (unsigned char *)calloc(1, BUF_SIZE); if (equalizer->buf == NULL) { ESP_LOGE(TAG, "calloc buffer failed. (line %d)", __LINE__); return ESP_ERR_NO_MEM; } memset(equalizer->buf, 0, BUF_SIZE); equalizer->eq_handle = esp_equalizer_init(equalizer->channel, equalizer->samplerate, equalizer->num_band, equalizer->use_xmms_original_freqs); if (equalizer->eq_handle == NULL) { ESP_LOGE(TAG, "failed to do equalizer initialization. (line %d)", __LINE__); return ESP_FAIL; } for (int i = 0; i < equalizer->channel; i++) { for (int j = 0; j < NUMBER_BAND; j++) { esp_equalizer_set_band_value(equalizer->eq_handle, equalizer->set_gain[NUMBER_BAND * i + j], j, i); } } #ifdef DEBUG_EQUALIZER_ENC_ISSUE char fileName[100] = {'//', 's', 'd', 'c', 'a', 'r', 'd', '//', 't', 'e', 's', 't', '.', 'p', 'c', 'm', '\0'}; infile = fopen(fileName, "rb"); if (!infile) { perror(fileName); return ESP_FAIL; } #endif return ESP_OK; } static esp_err_t equalizer_close(audio_element_handle_t self) { ESP_LOGD(TAG, "equalizer_close"); equalizer_t *equalizer = (equalizer_t *)audio_element_getdata(self); esp_equalizer_uninit(equalizer->eq_handle); if (equalizer->buf == NULL) { audio_free(equalizer->buf); equalizer->buf = NULL; } #ifdef EQUALIZER_MEMORY_ANALYSIS AUDIO_MEM_SHOW(TAG); #endif #ifdef DEBUG_EQUALIZER_ENC_ISSUE fclose(infile); #endif return ESP_OK; } static int equalizer_process(audio_element_handle_t self, char *in_buffer, int in_len) { equalizer_t *equalizer = (equalizer_t *)audio_element_getdata(self); int ret = 0; if (equalizer->gain_flag == 1) { equalizer_close(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } equalizer_open(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } ESP_LOGI(TAG, "Reopen equalizer"); return ESP_CODEC_ERR_CONTINUE; } int r_size = 0; if (equalizer->at_eof == 0) { #ifdef DEBUG_EQUALIZER_ENC_ISSUE r_size = fread((char *)equalizer->buf, 1, BUF_SIZE, infile); #else r_size = audio_element_input(self, (char *)equalizer->buf, BUF_SIZE); #endif } if (r_size > 0) { if (r_size != BUF_SIZE) { equalizer->at_eof = 1; } equalizer->byte_num += r_size; ret = esp_equalizer_process(equalizer->eq_handle, (unsigned char *)equalizer->buf, r_size, equalizer->samplerate, equalizer->channel); ret = audio_element_output(self, (char *)equalizer->buf, BUF_SIZE); } else { ret = r_size; } return ret; } audio_element_handle_t equalizer_init(equalizer_cfg_t *config) { if (config == NULL) { ESP_LOGE(TAG, "config is NULL. (line %d)", __LINE__); return NULL; } equalizer_t *equalizer = audio_calloc(1, sizeof(equalizer_t)); AUDIO_MEM_CHECK(TAG, equalizer, return NULL); if (equalizer == NULL) { ESP_LOGE(TAG, "audio_calloc failed for equalizer. (line %d)", __LINE__); return NULL; } audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG(); cfg.destroy = equalizer_destroy; cfg.process = equalizer_process; cfg.open = equalizer_open; cfg.close = equalizer_close; cfg.buffer_len = 0; cfg.tag = "equalizer"; cfg.task_stack = config->task_stack; cfg.task_prio = config->task_prio; cfg.task_core = config->task_core; cfg.out_rb_size = config->out_rb_size; cfg.stack_in_ext = config->stack_in_ext; audio_element_handle_t el = audio_element_init(&cfg); AUDIO_MEM_CHECK(TAG, el, {audio_free(equalizer); return NULL;}); equalizer->samplerate = config->samplerate; equalizer->channel = config->channel; equalizer->set_gain = config->set_gain; audio_element_setdata(el, equalizer); audio_element_info_t info = {0}; audio_element_setinfo(el, &info); ESP_LOGD(TAG, "equalizer_init"); return el; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/equalizer.c
C
apache-2.0
9,890
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #include <string.h> #include "esp_log.h" #include "audio_error.h" #include "audio_mem.h" #include "audio_element.h" #include "filter_resample.h" #include "audio_type_def.h" #ifndef MAX #define MAX(a,b) ((a) > (b) ? (a) : (b)) #endif static const char *TAG = "RSP_FILTER"; #define RESAMPLING_POINT_NUM (512) typedef struct rsp_filter { resample_info_t *resample_info; unsigned char *out_buf; unsigned char *in_buf; void *rsp_hd; int in_offset; int8_t flag; //the flag must be 0 or 1. 1 means the parameter in `resample_info_t` changed. 0 means the parameter in `resample_info_t` is original. } rsp_filter_t; static esp_err_t is_valid_rsp_filter_samplerate(int samplerate) { if (samplerate < 8000 || samplerate > 96000) { ESP_LOGE(TAG, "The sample rate should be within range [8000,96000], here is %d Hz", samplerate); return ESP_FAIL; } return ESP_OK; } static esp_err_t is_valid_rsp_filter_channel(int channel) { if (channel != 1 && channel != 2) { ESP_LOGE(TAG, "The number of channels should be either 1 or 2, here is %d", channel); return ESP_FAIL; } return ESP_OK; } esp_err_t rsp_filter_set_src_info(audio_element_handle_t self, int src_rate, int src_ch) { rsp_filter_t *filter = (rsp_filter_t *)audio_element_getdata(self); if (filter->resample_info->src_rate == src_rate && filter->resample_info->src_ch == src_ch) { return ESP_OK; } if (is_valid_rsp_filter_samplerate(src_rate) != ESP_OK || is_valid_rsp_filter_channel(src_ch) != ESP_OK) { return ESP_ERR_INVALID_ARG; } else { filter->flag = 1; filter->resample_info->src_rate = src_rate; filter->resample_info->src_ch = src_ch; ESP_LOGI(TAG, "reset sample rate of source data : %d, reset channel of source data : %d", filter->resample_info->src_rate, filter->resample_info->src_ch); } return ESP_OK; } static esp_err_t rsp_filter_destroy(audio_element_handle_t self) { rsp_filter_t *filter = (rsp_filter_t *)audio_element_getdata(self); if (filter != NULL && filter->resample_info != NULL) { audio_free(filter->resample_info); filter->resample_info = NULL; } if (filter != NULL) { audio_free(filter); } return ESP_OK; } static esp_err_t rsp_filter_open(audio_element_handle_t self) { rsp_filter_t *filter = (rsp_filter_t *)audio_element_getdata(self); resample_info_t *resample_info = filter->resample_info; if (resample_info->sample_bits != 16) { ESP_LOGE(TAG, "Currently, the only supported bit width is 16 bits."); return ESP_ERR_INVALID_ARG; } if (resample_info->complexity < 0) { ESP_LOGI(TAG, "Currently, the complexity is %d, that is less than 0, it has been set 0.", resample_info->complexity); resample_info->complexity = 0; } else if (resample_info->complexity > COMPLEXITY_MAX_NUM ) { ESP_LOGI(TAG, "Currently, the complexity is %d, that is more than the maximal of complexity, it has been set the maximal of complexity.", resample_info->complexity); resample_info->complexity = COMPLEXITY_MAX_NUM; } if (filter->resample_info->mode == RESAMPLE_DECODE_MODE) { resample_info->max_indata_bytes = MAX(resample_info->max_indata_bytes, RESAMPLING_POINT_NUM * resample_info->src_ch); //`RESAMPLING_POINT_NUM * resample_info->src_ch` is for mininum of `resample_info->max_indata_bytes`, enough extra buffer for safety filter->in_offset = resample_info->max_indata_bytes; } else if (filter->resample_info->mode == RESAMPLE_ENCODE_MODE) { int tmp = (int)((float)resample_info->src_rate * resample_info->src_ch) / ((float)resample_info->dest_rate * resample_info->dest_ch); tmp = tmp > 1.0 ? tmp : 1.0; resample_info->max_indata_bytes = (int)(resample_info->out_len_bytes * tmp) + RESAMPLING_POINT_NUM * resample_info->src_ch; //`RESAMPLING_POINT_NUM * resample_info->src_ch` has no meaning, just enought extra buffer for safety filter->in_offset = 0; } filter->flag = 0; filter->rsp_hd = esp_resample_create((void *)filter->resample_info, (unsigned char **)&filter->in_buf, (unsigned char **)&filter->out_buf); if (filter->rsp_hd == NULL) { ESP_LOGE(TAG, "Failed to create the resample handler"); return ESP_FAIL; } ESP_LOGI(TAG, "sample rate of source data : %d, channel of source data : %d, sample rate of destination data : %d, channel of destination data : %d", filter->resample_info->src_rate, filter->resample_info->src_ch, filter->resample_info->dest_rate, filter->resample_info->dest_ch); return ESP_OK; } static esp_err_t rsp_filter_close(audio_element_handle_t self) { rsp_filter_t *filter = (rsp_filter_t *)audio_element_getdata(self); if (filter->rsp_hd != NULL) { esp_resample_destroy(filter->rsp_hd); filter->rsp_hd = 0; } return ESP_OK; } static int rsp_filter_process(audio_element_handle_t self, char *in_buffer, int in_len) { rsp_filter_t *filter = (rsp_filter_t *)audio_element_getdata(self); int out_len = -1; int read_len = 0; int in_bytes_consumed = 0; int ret = 0; if (filter->flag == 1) { ret = rsp_filter_close(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } ret = rsp_filter_open(self); if (ret != ESP_OK) { return AEL_PROCESS_FAIL; } } if (filter->resample_info->mode == RESAMPLE_DECODE_MODE) { read_len = audio_element_input(self, (char *)filter->in_buf, filter->in_offset); if (read_len == filter->in_offset) { in_bytes_consumed = esp_resample_run(filter->rsp_hd, filter->resample_info, (unsigned char *)filter->in_buf, (unsigned char *)filter->out_buf, filter->in_offset, &filter->resample_info->out_len_bytes); if (in_bytes_consumed != filter->in_offset) { return ESP_FAIL; } } else if (read_len > 0) { memset(filter->in_buf + read_len, 0, filter->in_offset - read_len); in_bytes_consumed = esp_resample_run(filter->rsp_hd, filter->resample_info, (unsigned char *)filter->in_buf, (unsigned char *)filter->out_buf, filter->in_offset, &filter->resample_info->out_len_bytes); if (in_bytes_consumed != filter->in_offset) { return ESP_FAIL; } } else { return read_len; } } else { if (filter->in_offset < filter->resample_info->max_indata_bytes) { if (filter->in_offset > 0) { memmove(filter->in_buf, &filter->in_buf[filter->resample_info->max_indata_bytes - filter->in_offset], filter->in_offset); } read_len = audio_element_input(self, (char *)&filter->in_buf[filter->in_offset], filter->resample_info->max_indata_bytes - filter->in_offset); if (read_len > 0) { filter->in_offset += read_len; } } if (read_len > 0) { in_bytes_consumed = esp_resample_run(filter->rsp_hd, filter->resample_info, (unsigned char *)filter->in_buf, (unsigned char *)filter->out_buf, filter->in_offset, &filter->resample_info->out_len_bytes); if (in_bytes_consumed < 0) { return ESP_FAIL; } else { filter->in_offset -= in_bytes_consumed; } } else { return read_len; } } out_len = audio_element_output(self, (char *)filter->out_buf, filter->resample_info->out_len_bytes); return out_len; } audio_element_handle_t rsp_filter_init(rsp_filter_cfg_t *config) { if (config == NULL) { ESP_LOGE(TAG, "config is NULL. (line %d)", __LINE__); return NULL; } rsp_filter_t *filter = (rsp_filter_t *)audio_calloc(1, sizeof(rsp_filter_t)); AUDIO_MEM_CHECK(TAG, filter, return NULL); resample_info_t *resample_info = audio_calloc(1, sizeof(resample_info_t)); AUDIO_MEM_CHECK(TAG, resample_info, {audio_free(filter); return NULL;}); audio_element_cfg_t cfg = DEFAULT_AUDIO_ELEMENT_CONFIG(); cfg.destroy = rsp_filter_destroy; cfg.process = rsp_filter_process; cfg.open = rsp_filter_open; cfg.close = rsp_filter_close; cfg.buffer_len = 0; cfg.tag = "resample"; cfg.task_stack = config->task_stack; cfg.task_prio = config->task_prio; cfg.task_core = config->task_core; cfg.out_rb_size = config->out_rb_size; cfg.stack_in_ext = config->stack_in_ext; audio_element_handle_t el = audio_element_init(&cfg); AUDIO_MEM_CHECK(TAG, el, {audio_free(filter); audio_free(resample_info); return NULL;}); memcpy(resample_info, config, sizeof(resample_info_t)); filter->resample_info = resample_info; audio_element_setdata(el, filter); audio_element_info_t info = {0}; audio_element_setinfo(el, &info); ESP_LOGD(TAG, "rsp_filter_init"); return el; }
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/filter_resample.c
C
apache-2.0
9,493
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AAC_DECODER_H_ #define _AAC_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif #define AAC_DECODER_TASK_STACK_SIZE (5 * 1024) #define AAC_DECODER_TASK_CORE (0) #define AAC_DECODER_TASK_PRIO (5) #define AAC_DECODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_AAC_DECODER_CONFIG() { \ .out_rb_size = AAC_DECODER_RINGBUFFER_SIZE, \ .task_stack = AAC_DECODER_TASK_STACK_SIZE, \ .task_core = AAC_DECODER_TASK_CORE, \ .task_prio = AAC_DECODER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief AAC Decoder configuration */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< CPU core number (0 or 1) where decoder task in running */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } aac_decoder_cfg_t; /** * @brief Create an Audio Element handle to decode incoming AAC data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t aac_decoder_init(aac_decoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/aac_decoder.h
C
apache-2.0
1,589
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AMR_DECODER_H_ #define _AMR_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif #define AMR_DECODER_TASK_STACK_SIZE (5 * 1024) #define AMR_DECODER_TASK_CORE (0) #define AMR_DECODER_TASK_PRIO (5) #define AMR_DECODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_AMR_DECODER_CONFIG() { \ .out_rb_size = AMR_DECODER_RINGBUFFER_SIZE, \ .task_stack = AMR_DECODER_TASK_STACK_SIZE, \ .task_core = AMR_DECODER_TASK_CORE, \ .task_prio = AMR_DECODER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief AMR Decoder configuration */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< CPU core number (0 or 1) where decoder task in running */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } amr_decoder_cfg_t; /** * @brief Create an Audio Element handle to decode incoming AMR data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t amr_decoder_init(amr_decoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/amr_decoder.h
C
apache-2.0
1,617
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AMRNB_ENCODER_H_ #define _AMRNB_ENCODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Enum of AMRNB Encoder bitrate choose */ typedef enum { AMRNB_ENC_BITRATE_UNKNOW = -1, /*!< Invalid mode */ AMRNB_ENC_BITRATE_MR475 = 0, /* 4.75 kbps */ AMRNB_ENC_BITRATE_MR515 = 1, /* 5.15 kbps */ AMRNB_ENC_BITRATE_MR59 = 2, /* 5.90 kbps */ AMRNB_ENC_BITRATE_MR67 = 3, /* 6.70 kbps */ AMRNB_ENC_BITRATE_MR74 = 4, /* 7.40 kbps */ AMRNB_ENC_BITRATE_MR795 = 5, /* 7.95 kbps */ AMRNB_ENC_BITRATE_MR102 = 6, /* 10.2 kbps */ AMRNB_ENC_BITRATE_MR122 = 7, /* 12.2 kbps */ AMRNB_ENC_BITRATE_MRDTX = 8, /* DTX */ AMRNB_ENC_BITRATE_N_MODES = 9, /* Not Used */ } amrnb_encoder_bitrate_t; /** * @brief AMRNB Encoder configurations */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core (0 or 1) */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ amrnb_encoder_bitrate_t bitrate_mode; /*!< AMRNB Encoder bitrate choose */ bool contain_amrnb_header; /*!< Choose to contain amrnb header in amrnb encoder whether or not (true or false, true means choose to contain amrnb header) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } amrnb_encoder_cfg_t; #define AMRNB_ENCODER_TASK_STACK (15 * 1024) #define AMRNB_ENCODER_TASK_CORE (0) #define AMRNB_ENCODER_TASK_PRIO (5) #define AMRNB_ENCODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_AMRNB_ENCODER_CONFIG() { \ .out_rb_size = AMRNB_ENCODER_RINGBUFFER_SIZE, \ .task_stack = AMRNB_ENCODER_TASK_STACK, \ .task_core = AMRNB_ENCODER_TASK_CORE, \ .task_prio = AMRNB_ENCODER_TASK_PRIO, \ .bitrate_mode = AMRNB_ENC_BITRATE_MR122, \ .contain_amrnb_header = false, \ .stack_in_ext = true, \ } /** * @brief Set AMRNB encoder bitrate * * @param self Audio element handle * * @param bitrate_mode Bitrate choose, value from amrnb_encoder_bitrate_t * * @return * ESP_OK * ESP_FAIL */ esp_err_t amrnb_encoder_set_bitrate(audio_element_handle_t self, amrnb_encoder_bitrate_t bitrate_mode); /** * @brief Create an Audio Element handle to encode incoming AMRNB data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t amrnb_encoder_init(amrnb_encoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/amrnb_encoder.h
C
apache-2.0
3,141
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AMRWB_ENCODER_H_ #define _AMRWB_ENCODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Enum of AMRWB Encoder bitrate choose */ typedef enum { AMRWB_ENC_BITRATE_MDNONE = -1, /*!< Invalid mode */ AMRWB_ENC_BITRATE_MD66 = 0, /*!< 6.60kbps */ AMRWB_ENC_BITRATE_MD885 = 1, /*!< 8.85kbps */ AMRWB_ENC_BITRATE_MD1265 = 2, /*!< 12.65kbps */ AMRWB_ENC_BITRATE_MD1425 = 3, /*!< 14.25kbps */ AMRWB_ENC_BITRATE_MD1585 = 4, /*!< 15.85bps */ AMRWB_ENC_BITRATE_MD1825 = 5, /*!< 18.25bps */ AMRWB_ENC_BITRATE_MD1985 = 6, /*!< 19.85kbps */ AMRWB_ENC_BITRATE_MD2305 = 7, /*!< 23.05kbps */ AMRWB_ENC_BITRATE_MD2385 = 8, /*!< 23.85kbps> */ AMRWB_ENC_BITRATE_N_MODES = 9, /*!< Invalid mode */ } amrwb_encoder_bitrate_t; /** * @brief AMRWB Encoder configurations */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core (0 or 1) */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ amrwb_encoder_bitrate_t bitrate_mode; /*!< AMRWB Encoder bitrate choose */ bool contain_amrwb_header; /*!< Choose to contain amrwb header in amrwb encoder whether or not (true or false, true means choose to contain amrwb header) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } amrwb_encoder_cfg_t; #define AMRWB_ENCODER_TASK_STACK (15 * 1024) #define AMRWB_ENCODER_TASK_CORE (0) #define AMRWB_ENCODER_TASK_PRIO (5) #define AMRWB_ENCODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_AMRWB_ENCODER_CONFIG() { \ .out_rb_size = AMRWB_ENCODER_RINGBUFFER_SIZE, \ .task_stack = AMRWB_ENCODER_TASK_STACK, \ .task_core = AMRWB_ENCODER_TASK_CORE, \ .task_prio = AMRWB_ENCODER_TASK_PRIO, \ .bitrate_mode = AMRWB_ENC_BITRATE_MD885, \ .contain_amrwb_header = false, \ .stack_in_ext = true, \ } /** * @brief Set AMRWB encoder bitrate * * @param self Audio element handle * * @param bitrate_mode Bitrate choose, value from amrwb_encoder_bitrate_t * * @return * ESP_OK * ESP_FAIL */ esp_err_t amrwb_encoder_set_bitrate(audio_element_handle_t self, amrwb_encoder_bitrate_t bitrate_mode); /** * @brief Create an Audio Element handle to encode incoming amrwb data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t amrwb_encoder_init(amrwb_encoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/amrwb_encoder.h
C
apache-2.0
3,229
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _AUDIO_ALC_H_ #define _AUDIO_ALC_H_ #ifdef __cplusplus extern "C" { #endif /** * @brief ALC Configuration */ typedef struct { int volume; /*!< The volume to be set to; The default is 0. 0 means the original valume of input stream. If the value is -64, the volume of input stream is silent. */ int channel; /*!< The number of channels for input stream */ int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core (0 or 1) */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } alc_volume_setup_cfg_t; #define ALC_VOLUME_SET_TASK_STACK (4 * 1024) #define ALC_VOLUME_SET_TASK_CORE (0) #define ALC_VOLUME_SET_TASK_PRIO (5) #define ALC_VOLUME_SET_RINGBUFFER_SIZE (8 * 1024) #define DEFAULT_ALC_VOLUME_SETUP_CONFIG() { \ .volume = 0, \ .channel = 1, \ .out_rb_size = ALC_VOLUME_SET_RINGBUFFER_SIZE, \ .task_stack = ALC_VOLUME_SET_TASK_STACK, \ .task_core = ALC_VOLUME_SET_TASK_CORE, \ .task_prio = ALC_VOLUME_SET_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Set the number of channels of input audio stream * * @param self Audio element handle * @param channels the number of channels for input stream */ void alc_volume_setup_set_channel(audio_element_handle_t self, int channel); /** * @brief Set the volume of input audio stream with ALC * * @param self Audio element handle * @param volume the set volume of input audio stream */ void alc_volume_setup_set_volume(audio_element_handle_t self, int volume); /** * @brief Get the volume of input audio stream * * @param self Audio element handle * * @return the volume of input audio stream */ int alc_volume_setup_get_volume(audio_element_handle_t self); /** * @brief Create an Audio Element handle to set volume of input audio stream with ALC * * @param config the configuration * @return the audio element handler */ audio_element_handle_t alc_volume_setup_init(alc_volume_setup_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/audio_alc.h
C
apache-2.0
4,121
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _AUDI_FORGE_WRITER_H_ #define _AUDI_FORGE_WRITER_H_ #include "audio_error.h" #include "esp_alc.h" #include "esp_downmix.h" #include "esp_equalizer.h" #include "esp_resample.h" #include "esp_sonic.h" #ifdef __cplusplus extern "C" { #endif /** * * The module is multifunctional audio forge which contains resample, downmix, ALC, equalizer and sonic. Choose a combination of several function by `component_select`. * * resample: Change sample rate or number of channels for source stream. * downmix: Down-mix different source streams or stereo source stream. * ALC: Change volume of source file. * equalizer: Modify a frequency response to compensate for distortion. * sonic: Change the speed and pitch of source file. * */ extern int audio_forge_set_gain_value[]; /*!< the paramenter is for equalizer */ /** * @brief Information of source files */ typedef struct { int samplerate; /*!< Sample rate */ int channel; /*!< Number of channel(s)*/ int bit_num; /*!< Only 16-bit PCM audio is supported */ } audio_forge_src_info_t; /** * @brief Information of downmix processing */ typedef struct { float gain[2]; /*!< The gain processed by downmix. Refer to `downmix_input_info_t` */ int transit_time; /*!< The transit time processed by downmix. Refer to `downmix_input_info_t` */ } audio_forge_downmix_t; /** * @brief The select of audio forge component. * eg. To use resample and downmix together, please enter AUDIO_FORGE_SELECT_RESAMPLE | AUDIO_FORGE_SELECT_DOWNMIX. */ typedef enum { AUDIO_FORGE_SELECT_RESAMPLE = 0x01, /*!< Resample selected */ AUDIO_FORGE_SELECT_DOWNMIX = 0x02, /*!< Downmix selected */ AUDIO_FORGE_SELECT_ALC = 0x04, /*!< ALC selected */ AUDIO_FORGE_SELECT_EQUALIZER = 0x08, /*!< Equalizer selected */ AUDIO_FORGE_SELECT_SONIC = 0x10, /*!< Sonic selected */ } audio_forge_select_t; /** * @brief Closed mode of the audio forge */ typedef enum { AUDIO_FORGE_STOP_MODE_AUTO = 1, /*!< When all audios are closed, the element will free resource. */ AUDIO_FORGE_STOP_MODE_MANUAL = 2, /*!< First use `audio_set_stop_mode` for setting stop mode to not AUDIO_FORGE_STOP_MODE_MANUAL. Then call `audio_forge_close` to free resource */ AUDIO_FORGE_STOP_MODE_INVALID = 0, } audio_forge_stop_mode_t; /** * @brief Information of audio forge */ typedef struct { int dest_samplerate; /*!< The sample rate of destination files. If resample is selected, this value must be set */ int dest_channel; /*!< The channel of destination files, If resample and downmix are selected, the value must be set */ int rsp_cplx; /*!< complexity level of resample. refer to `esp_resample.h`*/ int alc_volume; /*!< The volume processed by ALC */ float sonic_pitch; /*!< The pitch processed by sonic */ float sonic_speed; /*!< The speed processed by sonic */ int *equalizer_gain; /*!< The gain processed by equalizer */ audio_forge_select_t component_select; /*!< The select of audio forge component */ int max_sample; /*!< Number of sample per audio forge processing */ int source_num; /*!< Number of source streams */ audio_forge_stop_mode_t stop_mode; } audio_forge_info_t; /** * @brief audio forge configurations */ typedef struct { audio_forge_info_t audio_forge; /*!< Information of audio forge */ int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core (0 or 1) */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } audio_forge_cfg_t; #define AUDIO_FORGE_TASK_STACK (4096) #define AUDIO_FORGE_BUF_SIZE (2048) #define AUDIO_FORGE_TASK_PRIO (5) #define AUDIO_FORGE_TASK_CORE (1) #define AUDIO_FORGE_RINGBUFFER_SIZE (8 * 1024) #define AUDIO_FORGE_SAMPLE_SIZE (256) #define AUDIO_FORGE_CFG_DEFAULT() { \ .audio_forge = { \ .dest_samplerate = 48000, \ .dest_channel = 2, \ .rsp_cplx = 0, \ .alc_volume = 0, \ .sonic_pitch = 1.0, \ .sonic_speed = 1.0, \ .equalizer_gain = NULL, \ .component_select = AUDIO_FORGE_SELECT_RESAMPLE, \ .max_sample = AUDIO_FORGE_SAMPLE_SIZE, \ .source_num = 2, \ .stop_mode = AUDIO_FORGE_STOP_MODE_AUTO, \ }, \ .task_prio = AUDIO_FORGE_TASK_PRIO, \ .task_core = AUDIO_FORGE_TASK_CORE, \ .task_stack = AUDIO_FORGE_TASK_STACK, \ .out_rb_size = AUDIO_FORGE_RINGBUFFER_SIZE, \ .stack_in_ext = true, \ } /** * @brief Switch on the multi-audio downmix. * * @param self audio element handle */ void audio_forge_downmix_start(audio_element_handle_t self); /** * @brief Switch off the multi-audio downmix. * * @param self audio element handle */ void audio_forge_downmix_stop(audio_element_handle_t self); /** * @brief Set audio forge stop mode. * * @param self audio element handle */ void audio_forge_set_stop_mode(audio_element_handle_t self, audio_forge_stop_mode_t stop_mode); /** * @brief Set the audio forge timeout. * * @param self audio element handle * @param tick_to_wait input ringbuffer timeout * @param index the index of multi input ringbuffer. Refer to `ringbuf.h` */ void audio_forge_downmix_set_input_rb_timeout(audio_element_handle_t self, int tick_to_wait, int index); /** * @brief Set the audio forge input ringbuffer. * * @param self audio element handle * @param rb handle of ringbuffer * @param index the index of multi input ringbuffer. Refer to `ringbuf.h` */ void audio_forge_downmix_set_input_rb(audio_element_handle_t self, ringbuf_handle_t rb, int index); /** * @brief Set the audio `transform_time` to be processed by the downmix. * * @param self audio element handle * @param transit_time reset value of transform_time defined in audio_forge_downmix_t. The `transit_time` is an array of two elements. * @param index the index of audio streams, start from `0`. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_downmix_set_transit_time(audio_element_handle_t self, int transit_time, int index); /** * @brief Set the audio gain to be processed by the downmix. * * @param self audio element handle * @param gain the reset value of `gain` which in `audio_forge_downmix_t`. The `gain` is an array of four elements. * @param index the index of audio streams, start from `0`. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_downmix_set_gain(audio_element_handle_t self, int *gain, int index); /** * @brief Setup volume of stream by the ALC * * @param self audio element handle * @param volume the volume of stream will be set. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_alc_set_volume(audio_element_handle_t self, int volume); /** * @brief Get volume of stream * * @param self audio element handle * @param volume the volume of stream * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_alc_get_volume(audio_element_handle_t self, int *volume); /** * @brief Set the audio gain to be processed by the equalizer. * * @param self audio element handle * @param eq_gain the value of audio gain which in `index` * @param band_index the position of center frequencies of equalizer * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_eq_set_gain(audio_element_handle_t self, int eq_gain, int band_index); /** * @brief Set the component select. * * @param self audio element handle * @param component_select The select of audio forge component. eg. To use resample and downmix together, please enter AUDIO_FORGE_SELECT_RESAMPLE | AUDIO_FORGE_SELECT_DOWNMIX. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_sonic_set_component_select(audio_element_handle_t self, int component_select); /** * @brief Set the audio pitch to be processed by the sonic. * * @param self audio element handle * @param sonic_pitch scale factor of pitch of audio stream. 0 means the original pitch. The range is [0.2 4.0]. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_sonic_set_pitch(audio_element_handle_t self, float sonic_pitch); /** * @brief Get the audio speed to be processed by the sonic. * * @param self audio element handle * @param sonic_speed scale factor of speed of audio stream. 1.0 means the original speed. The range is [0.25 2.0]. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_sonic_get_speed(audio_element_handle_t self, float *sonic_speed); /** * @brief Set the audio speed to be processed by the sonic. * * @param self audio element handle * @param sonic_speed scale factor of speed of audio stream. 0 means the original speed. The range is [0.25 2.0]. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_sonic_set_speed(audio_element_handle_t self, float sonic_speed); /** * @brief Set the sample rate and the number of channels of input file to be processed . * * @param self audio element handle * @param samplerate sample rate of the source audio stream * @param channel number of channel(s) of the audio stream * @param index the index of audio streams, start from `0`. * * @return * ESP_OK * ESP_FAIL */ esp_err_t audio_forge_set_src_info(audio_element_handle_t self, audio_forge_src_info_t source_num, int index); /** * @brief Initialize information of the source files for audio forge. * * @param self audio element handle * @param source_num the information array of source files * @param downmix_info it is used in downmix. Refer to `audio_forge_downmix_t` * * @return ESP_OK * ESP_FAIL */ esp_err_t audio_forge_source_info_init(audio_element_handle_t self, audio_forge_src_info_t *source_num, audio_forge_downmix_t *downmix_info); /** * @brief Create a handle to an audio forge to processing the audio stream * * @param config the configuration * * @return the Audio Element handle */ audio_element_handle_t audio_forge_init(audio_forge_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/audio_forge.h
C
apache-2.0
13,460
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUDIO_SONIC_H_ #define _AUDIO_SONIC_H_ #include "esp_err.h" #include "audio_element.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Information on audio file and configuration parameters required by sonic to process the file. */ typedef struct { int samplerate; /*!< Audio file sample rate (in Hz)*/ int channel; /*!< Number of audio file channels (Mono=1, Dual=2) */ int resample_linear_interpolate; /*!< Flag of using simple linear interpolation. 1 indicates using simple linear interpolation. 0 indicates not using simple linear interpolation.*/ float pitch; /*!< Scale factor of pitch of audio file. If the value of 'pitch' is 0.3, the pitch of audio file processed by sonic islower than the original. If the value of 'pitch' is 1.3, the pitch of audio file processed by sonic is 30% higher than the original.*/ float speed; /*!< Scale factor of speed of audio file. If the value of 'speed' is 0.3, the speed of audio file processed by sonic is 70% slower than the original. If the value of 'speed' is 1.3, the speed of audio file processed by sonic is 30% faster than the original.*/ } sonic_info_t; /** * @brief Sonic configuration */ typedef struct { sonic_info_t sonic_info; /*!< Information of sonic*/ int out_rb_size; /*!< Size of output ring buffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core */ int task_prio; /*!< Task priority */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } sonic_cfg_t; #define SONIC_SET_VALUE_FOR_INITIALIZATION (1.0f) #define SONIC_TASK_STACK (4 * 1024) #define SONIC_TASK_CORE (0) #define SONIC_TASK_PRIO (5) #define SONIC_RINGBUFFER_SIZE (8 * 1024) #define DEFAULT_SONIC_CONFIG() \ { \ .sonic_info = { \ .samplerate = 44100, \ .channel = 1, \ .resample_linear_interpolate = 0, \ .pitch = SONIC_SET_VALUE_FOR_INITIALIZATION, \ .speed = SONIC_SET_VALUE_FOR_INITIALIZATION, \ }, \ .out_rb_size = SONIC_RINGBUFFER_SIZE, \ .task_stack = SONIC_TASK_STACK, \ .task_core = SONIC_TASK_CORE, \ .task_prio = SONIC_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Sets the audio sample rate and the number of channels to be processed by the sonic. * * @param self Audio element handle * @param rate The sample rate of stream data * @param ch The number channels of stream data * * @return * ESP_OK * ESP_FAIL */ esp_err_t sonic_set_info(audio_element_handle_t self, int rate, int ch); /** * @brief Sets the audio pitch and speed to be processed by the sonic. * * @param self Audio element handle * @param pitch Scale factor of pitch of audio file. 0 means the original pitch. The range is [0.2 4.0]. * @param speed Scale factor of speed of audio file. 0 means the original speed. The range is [0.1 8.0]. * * @return * ESP_OK * ESP_FAIL */ esp_err_t sonic_set_pitch_and_speed_info(audio_element_handle_t self, float pitch, float speed); /** * @brief Creates an Audio Element handle for sonic * * @param config The sonic configuration * * @return The sonic audio element handle */ audio_element_handle_t sonic_init(sonic_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/audio_sonic.h
C
apache-2.0
4,022
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUDIO_TYPE_DEF_H_ #define _AUDIO_TYPE_DEF_H_ #ifdef __cplusplus extern "C" { #endif #define ESP_AUDIO_SAMPLERATE_8K (8000) #define ESP_AUDIO_SAMPLERATE_11K (11025) #define ESP_AUDIO_SAMPLERATE_16K (16000) #define ESP_AUDIO_SAMPLERATE_22K (22050) #define ESP_AUDIO_SAMPLERATE_32K (32000) #define ESP_AUDIO_SAMPLERATE_44K (44100) #define ESP_AUDIO_SAMPLERATE_48K (48000) #define ESP_AUDIO_BIT_8 (8) #define ESP_AUDIO_BIT_16 (16) #define ESP_AUDIO_BIT_24 (24) #define ESP_AUDIO_BIT_32 (32) #define ESP_AUDIO_CHANNEL_MONO (1) #define ESP_AUDIO_CHANNEL_DUAL (2) #define ESP_AUDIO_U32_AT(ptr) (((uint32_t)(ptr)[0] << 24) | ((uint32_t)(ptr)[1] << 16) | ((uint32_t)(ptr)[2] << 8) | ((uint32_t)(ptr)[3])) #define ESP_AUDIO_U64_AT(ptr) (((uint64_t)(ptr)[0] << 56) | ((uint64_t)(ptr)[1] << 48) | ((uint64_t)(ptr)[2] << 40) | ((uint64_t)(ptr)[3] << 32) | ((uint64_t)(ptr)[4] << 24) | ((uint64_t)(ptr)[5] << 16) | ((uint64_t)(ptr)[6] << 8) | ((uint64_t)(ptr)[7])) typedef enum { ESP_CODEC_TYPE_UNKNOW = 0, ESP_CODEC_TYPE_RAW = 1, ESP_CODEC_TYPE_WAV = 2, ESP_CODEC_TYPE_MP3 = 3, ESP_CODEC_TYPE_AAC = 4, ESP_CODEC_TYPE_OPUS = 5, ESP_CODEC_TYPE_M4A = 6, ESP_CODEC_TYPE_MP4 = 7, ESP_CODEC_TYPE_FLAC = 8, ESP_CODEC_TYPE_OGG = 9, ESP_CODEC_TYPE_TSAAC = 10, ESP_CODEC_TYPE_AMRNB = 11, ESP_CODEC_TYPE_AMRWB = 12, ESP_CODEC_TYPE_PCM = 13, ESP_AUDIO_TYPE_M3U8 = 14, ESP_AUDIO_TYPE_PLS = 15, ESP_CODEC_TYPE_UNSUPPORT = 16, } esp_codec_type_t; typedef enum { ESP_DECODER_WORK_MODE_MANUAL = 0, ESP_DECODER_WORK_MODE_AUTO = 1, } esp_decoder_work_mode_t; /** * @brief the enum value from `ESP_CODEC_ERR_OK` to `ESP_CODEC_ERR_TIMEOUT` rely on `audio_element_err_t` which in `audio_element.h`. */ typedef enum { ESP_CODEC_ERR_CONTINUE = 1, ESP_CODEC_ERR_OK = 0, ESP_CODEC_ERR_FAIL = -1, ESP_CODEC_ERR_DONE = -2, ESP_CODEC_ERR_ABORT = -3, ESP_CODEC_ERR_TIMEOUT = -4, ESP_CODEC_ERR_UNSYNC = -5, ESP_CODEC_ERR_UNSUPPORT = -6, ESP_CODEC_ERR_PARSE = -7, ESP_CODEC_ERR_INFO = -8, ESP_CODEC_ERR_INPUT = -9, ESP_CODEC_ERR_NO_MEM = -10, } esp_codec_err_t; /** * @brief Gain codec extension. need to sync by `esp_codec_type_t`. * eg. esp_codec_type_t codec_type = ESP_CODEC_TYPE_MP3; * char* ext = get_codec_ext(codec_type); * the value of ext is `mp3`. * * @param codec_type the value of `esp_codec_type_t` * * @return codec extension */ const char * get_codec_ext(esp_codec_type_t codec_type); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/audio_type_def.h
C
apache-2.0
3,046
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUTO_AAC_DEC_H_ #define _AUTO_AAC_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t aac_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t aac_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t aac_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t aac_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_aac_dec.h
C
apache-2.0
1,511
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUTO_AMR_DEC_H_ #define _AUTO_AMR_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t amr_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t amr_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t amr_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t amr_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_amr_dec.h
C
apache-2.0
1,511
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUTO_FLAC_DEC_H_ #define _AUTO_FLAC_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t flac_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t flac_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t flac_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t flac_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_flac_dec.h
C
apache-2.0
1,517
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUTO_MP3_DEC_H_ #define _AUTO_MP3_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t mp3_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t mp3_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t mp3_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t mp3_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_mp3_dec.h
C
apache-2.0
1,512
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUTO_OGG_DEC_H_ #define _AUTO_OGG_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t ogg_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t ogg_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t ogg_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t ogg_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_ogg_dec.h
C
apache-2.0
1,511
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _AUTO_OPUS_DEC_H_ #define _AUTO_OPUS_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t opus_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t opus_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t opus_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t opus_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_opus_dec.h
C
apache-2.0
1,517
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _AUTO_PCM_DEC_H_ #define _AUTO_PCM_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @note The sample rate and channle of PCM should be specific by user with `audio_element_setinfo`. */ /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t pcm_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t pcm_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t pcm_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t pcm_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_pcm_dec.h
C
apache-2.0
2,780
/* * ESPRESSIF MIT License * * Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _AUTO_WAV_DEC_H_ #define _AUTO_WAV_DEC_H_ #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Open an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t wav_decoder_open(audio_element_handle_t el); /** * @brief Close an Audio Element type data * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_err_t wav_decoder_close(audio_element_handle_t el); /** * @brief Do music data to decode * * @param el The audio element handle * * @return * ESP_OK * ESP_FAIL */ esp_codec_err_t wav_decoder_process(audio_element_handle_t el); /** * @brief Seek postion of music data to decode * * @param self The audio element handle * @param in_data The parameter required by callback function format. Not used by this function. * @param in_size The parameter required by callback function format. Not used by this function. * @param out_data The position of music data * @param out_size the size of `outdata` * * @return * ESP_OK * ESP_FAIL */ esp_err_t wav_decoder_get_pos(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/auto_wav_dec.h
C
apache-2.0
2,668
// Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD.> // All rights reserved. #ifndef _DOWNMIX_H_ #define _DOWNMIX_H_ #include "esp_err.h" #include "audio_element.h" #include "esp_downmix.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Downmix configuration */ typedef struct { esp_downmix_info_t downmix_info; /*!< Downmix information */ int max_sample; /*!< The number of samples per downmix processing */ int out_rb_size; /*!< Size of ring buffer */ int task_stack; /*!< Size of task stack */ int task_core; /*!< Task running in core... */ int task_prio; /*!< Task priority (based on the FreeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } downmix_cfg_t; #define DOWNMIX_TASK_STACK (8 * 1024) #define DOWNMIX_TASK_CORE (0) #define DOWNMIX_TASK_PRIO (5) #define DOWNMIX_RINGBUFFER_SIZE (8 * 1024) #define DM_BUF_SIZE (256) #define DEFAULT_DOWNMIX_CONFIG() \ { \ .downmix_info = { \ .out_ctx = ESP_DOWNMIX_OUT_CTX_LEFT_RIGHT, \ .mode = ESP_DOWNMIX_WORK_MODE_BYPASS, \ .output_type = ESP_DOWNMIX_OUTPUT_TYPE_ONE_CHANNEL, \ .source_num = SOURCE_NUM_MAX, \ }, \ .max_sample = DM_BUF_SIZE, \ .out_rb_size = DOWNMIX_RINGBUFFER_SIZE, \ .task_stack = DOWNMIX_TASK_STACK, \ .task_core = DOWNMIX_TASK_CORE, \ .task_prio = DOWNMIX_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Sets the downmix timeout. * * @param self audio element handle * @param ticks_to_wait input ringbuffer timeout * @param index The index of multi input ringbuffer. */ void downmix_set_input_rb_timeout(audio_element_handle_t self, int ticks_to_wait, int index); /** * @brief Sets the downmix input ringbuffer. refer to `ringbuf.h` * * @param self audio element handle * @param rb handle of ringbuffer * @param index The index of multi input ringbuffer. */ void downmix_set_input_rb(audio_element_handle_t self, ringbuf_handle_t rb, int index); /** * @brief Passes number of channels for output stream. Only supported mono and dual. * * @param self audio element handle * @param output_type down-mixer output type. * * @return * ESP_OK * ESP_FAIL */ esp_err_t downmix_set_output_type(audio_element_handle_t self, esp_downmix_output_type_t output_type); /** * @brief Sets BYPASS, ON or OFF status of down-mixer. * * @param self audio element handle * @param mode down-mixer work mode. * * @return * ESP_OK * ESP_FAIL */ esp_err_t downmix_set_work_mode(audio_element_handle_t self, esp_downmix_work_mode_t mode); /** * @brief Passes content of per channel output stream by down-mixer. * * @param self audio element handle * @param out_ctx content of output stream. * * @return * ESP_OK * ESP_FAIL */ esp_err_t downmix_set_out_ctx_info(audio_element_handle_t self, esp_downmix_out_ctx_type_t out_ctx); /** * @brief Sets the sample rate and the number of channels of input stream to be processed. * * @param self audio element handle * @param rate sample rate of the input stream * @param ch number of channel(s) of the input stream * @param index The index of input stream. The index must be in [0, SOURCE_NUM_MAX - 1] range. * * @return * ESP_OK * ESP_FAIL */ esp_err_t downmix_set_source_stream_info(audio_element_handle_t self, int rate, int ch, int index); /** * @brief Sets the audio gain to be processed. * * @param self audio element handle * @param gain the reset value of `gain`. The `gain` is an array of two elements. * @param index The index of input stream. The index must be in [0, SOURCE_NUM_MAX - 1] range. * @return * ESP_OK * ESP_FAIL */ esp_err_t downmix_set_gain_info(audio_element_handle_t self, float *gain, int index); /** * @brief Sets the audio `transit_time` to be processed. * * @param self audio element handle * @param transit_time the reset value of `transit_time` * @param index The index of input stream. The index must be in [0, SOURCE_NUM_MAX - 1] range * * @return * ESP_OK * ESP_FAIL */ esp_err_t downmix_set_transit_time_info(audio_element_handle_t self, int transit_time, int index); /** * @brief Initializes information of the source streams for downmixing. * * @param self audio element handle * @param source_num The information array of source streams * * @return * ESP_OK * ESP_FAIL */ esp_err_t source_info_init(audio_element_handle_t self, esp_downmix_input_info_t *source_num); /** * @brief Initializes the Audio Element handle for downmixing. * * @param config the configuration * * @return The initialized Audio Element handle */ audio_element_handle_t downmix_init(downmix_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/downmix.h
C
apache-2.0
5,937
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _EQUALIZER_H_ #define _EQUALIZER_H_ #include "esp_err.h" #include "audio_element.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Equalizer Configuration */ typedef struct equalizer_cfg { int samplerate; /*!< Audio sample rate (in Hz)*/ int channel; /*!< Number of audio channels (Mono=1, Dual=2) */ int *set_gain; /*!< Equalizer gain */ int out_rb_size; /*!< Size of output ring buffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core...*/ int task_prio; /*!< Task priority*/ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } equalizer_cfg_t; #define EQUALIZER_TASK_STACK (4 * 1024) #define EQUALIZER_TASK_CORE (0) #define EQUALIZER_TASK_PRIO (5) #define EQUALIZER_RINGBUFFER_SIZE (8 * 1024) /** * @note `set_value_gain` is defined in c file. * values is {-13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, -13}; */ extern int set_value_gain[]; #define DEFAULT_EQUALIZER_CONFIG() { \ .samplerate = 48000, \ .channel = 1, \ .set_gain = set_value_gain, \ .out_rb_size = EQUALIZER_RINGBUFFER_SIZE, \ .task_stack = EQUALIZER_TASK_STACK, \ .task_core = EQUALIZER_TASK_CORE, \ .task_prio = EQUALIZER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Set the audio sample rate and the number of channels to be processed by the equalizer. * * @param self Audio element handle * @param rate Audio sample rate * @param ch Audio channel * * @return * ESP_OK * ESP_FAIL */ esp_err_t equalizer_set_info(audio_element_handle_t self, int rate, int ch); /** * @brief Set the audio gain to be processed by the equalizer. * * @param self Audio element handle * @param index the position of center frequencies of equalizer * @param value_gain the value of audio gain which in `index` * @param is_channels_gain_equal if Number of audio channel is equal 2, the value of audio gains which two channels are equal by checking `is_channels_gain_equal`. if `is_channels_gain_equal` is `true`,it means equal,otherwise unequal. * * @return * ESP_OK * ESP_FAIL */ esp_err_t equalizer_set_gain_info(audio_element_handle_t self, int index, int value_gain, bool is_channels_gain_equal); /** * @brief Create an Audio Element handle that equalizes incoming data. * * @param config The configuration * * @return The created audio element handle */ audio_element_handle_t equalizer_init(equalizer_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/equalizer.h
C
apache-2.0
3,075
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. /** * @brief ESP_Decoder is a decoder library Espressif develops which could automatically * recognize audio formats based on the input audio streams. */ #ifndef _ESP_DECODER_H_ #define _ESP_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #include "auto_wav_dec.h" #include "auto_aac_dec.h" #include "auto_mp3_dec.h" #include "auto_amr_dec.h" #include "auto_flac_dec.h" #include "auto_ogg_dec.h" #include "auto_opus_dec.h" #include "auto_pcm_dec.h" #include "audio_type_def.h" #ifdef __cplusplus extern "C" { #endif #define ESP_DECODER_TASK_STACK_SIZE (3 * 1024) #define ESP_DECODER_TASK_CORE (0) #define ESP_DECODER_TASK_PRIO (5) #define ESP_DECODER_RINGBUFFER_SIZE (10 * 1024) /** * @brief To provide audio stream element */ typedef struct{ esp_err_t (*decoder_open)(audio_element_handle_t el); /*!< Open an Audio Element type data */ esp_codec_err_t (*decoder_process)(audio_element_handle_t el); /*!< Audio data to decoding */ esp_err_t (*decoder_close)(audio_element_handle_t el); /*!< Close an Audio Element type data */ esp_err_t (*decoder_seek)(audio_element_handle_t self, void *in_data, int in_size, void *out_data, int *out_size); /*!< Seek Audio data in `timePos` to decode */ esp_codec_type_t decoder_type; /*!< Type of Audio file */ } audio_decoder_t; /** * @brief The audio formats list */ typedef struct decoder_node{ audio_decoder_t audio_decoder; /*!< Audio stream element */ struct decoder_node *next; /*!< The audio formats list */ } audio_decoder_node_t; /** * @brief Auto audio decoder configuration */ typedef struct{ int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< CPU core number (0 or 1) where decoder task in running */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } esp_decoder_cfg_t; #define DEFAULT_ESP_WAV_DECODER_CONFIG() \ { \ .decoder_open = wav_decoder_open, \ .decoder_process = wav_decoder_process, \ .decoder_close = wav_decoder_close, \ .decoder_seek = wav_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_WAV, \ } #define DEFAULT_ESP_MP3_DECODER_CONFIG() \ { \ .decoder_open = mp3_decoder_open, \ .decoder_process = mp3_decoder_process, \ .decoder_close = mp3_decoder_close, \ .decoder_seek = mp3_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_MP3, \ } #define DEFAULT_ESP_AMRNB_DECODER_CONFIG() \ { \ .decoder_open = amr_decoder_open, \ .decoder_process = amr_decoder_process, \ .decoder_close = amr_decoder_close, \ .decoder_seek = amr_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_AMRNB, \ } #define DEFAULT_ESP_AMRWB_DECODER_CONFIG() \ { \ .decoder_open = amr_decoder_open, \ .decoder_process = amr_decoder_process, \ .decoder_close = amr_decoder_close, \ .decoder_seek = amr_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_AMRWB, \ } #define DEFAULT_ESP_AAC_DECODER_CONFIG() \ { \ .decoder_open = aac_decoder_open, \ .decoder_process = aac_decoder_process, \ .decoder_close = aac_decoder_close, \ .decoder_seek = aac_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_AAC, \ } #define DEFAULT_ESP_M4A_DECODER_CONFIG() \ { \ .decoder_open = aac_decoder_open, \ .decoder_process = aac_decoder_process, \ .decoder_close = aac_decoder_close, \ .decoder_seek = aac_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_M4A, \ } #define DEFAULT_ESP_TS_DECODER_CONFIG() \ { \ .decoder_open = aac_decoder_open, \ .decoder_process = aac_decoder_process, \ .decoder_close = aac_decoder_close, \ .decoder_seek = aac_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_TSAAC, \ } #define DEFAULT_ESP_OGG_DECODER_CONFIG() \ { \ .decoder_open = ogg_decoder_open, \ .decoder_process = ogg_decoder_process, \ .decoder_close = ogg_decoder_close, \ .decoder_seek = ogg_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_OGG, \ } #define DEFAULT_ESP_OPUS_DECODER_CONFIG() \ { \ .decoder_open = opus_decoder_open, \ .decoder_process = opus_decoder_process, \ .decoder_close = opus_decoder_close, \ .decoder_seek = opus_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_OPUS, \ } #define DEFAULT_ESP_FLAC_DECODER_CONFIG() \ { \ .decoder_open = flac_decoder_open, \ .decoder_process = flac_decoder_process, \ .decoder_close = flac_decoder_close, \ .decoder_seek = flac_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_FLAC, \ } #define DEFAULT_ESP_PCM_DECODER_CONFIG() \ { \ .decoder_open = pcm_decoder_open, \ .decoder_process = pcm_decoder_process, \ .decoder_close = pcm_decoder_close, \ .decoder_seek = pcm_decoder_get_pos, \ .decoder_type = ESP_CODEC_TYPE_PCM, \ } #define DEFAULT_ESP_DECODER_CONFIG() \ { \ .out_rb_size = ESP_DECODER_RINGBUFFER_SIZE, \ .task_stack = ESP_DECODER_TASK_STACK_SIZE, \ .task_core = ESP_DECODER_TASK_CORE, \ .task_prio = ESP_DECODER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Create an Audio Element handler to decode incoming audio data. * - The handler is created to automatically recognize audio formats. * - The creation of the handler is dependent on the array of user’s audio decoder list. * * @param config The configuration * @param audio_decoder_list The audio formats list * @param list_size The number of audio formats * * @return The audio element handle */ audio_element_handle_t esp_decoder_init(esp_decoder_cfg_t *config, audio_decoder_t *audio_decoder_list, int list_size); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/esp_decoder.h
C
apache-2.0
7,607
/* * ESPRESSIF MIT License * * Copyright (c) 2019 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> * * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case, * it is 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. * */ #ifndef _ESP_G711_H #define _ESP_G711_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /** * @brief This function accepts an 16-bit signed integer and encodes it as A-law data * stored in a unsigned char. * * @param[in] pcm_val 16-bits pcm * * @return a-law pcm */ unsigned char esp_g711a_encode(int16_t pcm_val); /** * @brief Convert an A-law value to 16-bit signed linear PCM * * @param[in] pcm_val a-law pcm * * @return 16-bits pcm */ int16_t esp_g711a_decode(unsigned char pcm_val); /** * @brief This function accepts a 16-bit signed integer and encodes it as u-law data * stored in a unsigned char. * * @param[in] pcm_val 16-bits pcm * * @return u-law pcm */ unsigned char esp_g711u_encode(int16_t pcm_val); /** * @brief Convert a u-law value to 16-bit linear PCM * * @param[in] pcm_val u-law pcm * * @return 16-bits pcm */ int16_t esp_g711u_decode(unsigned char pcm_val); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/esp_g711.h
C
apache-2.0
2,269
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _RSP_FILTER_H_ #define _RSP_FILTER_H_ #include "esp_err.h" #include "audio_element.h" #include "esp_resample.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Resample Filter Configuration */ typedef struct { int src_rate; /*!< The sampling rate of the source PCM file (in Hz)*/ int src_ch; /*!< The number of channel(s) of the source PCM file (Mono=1, Dual=2) */ int dest_rate; /*!< The sampling rate of the destination PCM file (in Hz) */ int dest_ch; /*!< The number of channel(s) of the destination PCM file (Mono=1, Dual=2) */ int sample_bits; /*!< The bit width of the PCM file. Currently, the only supported bit width is 16 bits. */ esp_resample_mode_t mode; /*!< The resampling mode (the encoding mode or the decoding mode). For decoding mode, input PCM length is constant; for encoding mode, output PCM length is constant. */ int max_indata_bytes; /*!< The maximum buffer size of the input PCM (in bytes) */ int out_len_bytes; /*!< The buffer length of the output stream data. This parameter must be configured in encoding mode. */ esp_resample_type_t type; /*!< The resampling type (Automatic, Upsampling and Downsampling) */ int complexity; /*!< Indicates the complexity of the resampling. This parameter is only valid when a FIR filter is used. Range: 0~5; O indicates the lowest complexity, which means the accuracy is the lowest and the speed is the fastest; Meanwhile, 5 indicates the highest complexity, which means the accuracy is the highest and the speed is the slowest.If user set `complexity` less than 0, `complexity` can be set 0. If user set `complexity` more than 5, `complexity` can be set 5. */ int down_ch_idx; /*!< Indicates the channel that is selected (the right channel or the left channel). This parameter is only valid when the complexity parameter is set to 0 and the number of channel(s) of the input file has changed from dual to mono. */ esp_rsp_prefer_type_t prefer_flag; /*!< The select flag about lesser CPU usage or lower INRAM usage, refer to esp_resample.h */ int out_rb_size; /*!< Output ringbuffer size*/ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running on core */ int task_prio; /*!< Task priority */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } rsp_filter_cfg_t; #define RSP_FILTER_BUFFER_BYTE (512) #define RSP_FILTER_TASK_STACK (4 * 1024) #define RSP_FILTER_TASK_CORE (0) #define RSP_FILTER_TASK_PRIO (5) #define RSP_FILTER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_RESAMPLE_FILTER_CONFIG() { \ .src_rate = 44100, \ .src_ch = 2, \ .dest_rate = 48000, \ .dest_ch = 2, \ .sample_bits = 16, \ .mode = RESAMPLE_DECODE_MODE, \ .max_indata_bytes = RSP_FILTER_BUFFER_BYTE, \ .out_len_bytes = RSP_FILTER_BUFFER_BYTE, \ .type = ESP_RESAMPLE_TYPE_AUTO, \ .complexity = 2, \ .down_ch_idx = 0, \ .prefer_flag = ESP_RSP_PREFER_TYPE_SPEED, \ .out_rb_size = RSP_FILTER_RINGBUFFER_SIZE, \ .task_stack = RSP_FILTER_TASK_STACK, \ .task_core = RSP_FILTER_TASK_CORE, \ .task_prio = RSP_FILTER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Set the source audio sample rate and the number of channels to be processed by the resample. * * @param self Audio element handle * @param src_rate The sample rate of stream data * @param src_ch The number channels of stream data * * @return * ESP_OK * ESP_FAIL */ esp_err_t rsp_filter_set_src_info(audio_element_handle_t self, int src_rate, int src_ch); /** * @brief Create an Audio Element handle to resample incoming data. * * Depending on configuration, there are upsampling, downsampling, as well as * converting data between mono and dual. * * - If the esp_resample_mode_t is `RESAMPLE_DECODE_MODE`, `src_rate` and `src_ch` will be fetched from `audio_element_getinfo`. * - If the esp_resample_mode_t is `RESAMPLE_ENCODE_MODE`, `src_rate`, `src_ch`, `dest_rate` and `dest_ch` must be configured. * * @param config The configuration * * @return The audio element handler */ audio_element_handle_t rsp_filter_init(rsp_filter_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/filter_resample.h
C
apache-2.0
4,919
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _FLAC_DECODER_H_ #define _FLAC_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif #define FLAC_DECODER_TASK_STACK_SIZE (10 * 1024) #define FLAC_DECODER_TASK_CORE (0) #define FLAC_DECODER_TASK_PRIO (5) #define FLAC_DECODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_FLAC_DECODER_CONFIG() { \ .out_rb_size = FLAC_DECODER_RINGBUFFER_SIZE, \ .task_stack = FLAC_DECODER_TASK_STACK_SIZE, \ .task_core = FLAC_DECODER_TASK_CORE, \ .task_prio = FLAC_DECODER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief FLAC Decoder configuration */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< CPU core number (0 or 1) where decoder task in running */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } flac_decoder_cfg_t; /** * @brief Create an Audio Element handle to decode incoming FLAC data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t flac_decoder_init(flac_decoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/flac_decoder.h
C
apache-2.0
1,601
// Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> // All rights reserved. #ifndef _G711_DECODER_H_ #define _G711_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #ifdef __cplusplus extern "C" { #endif /** * @brief G711 Decoder configurations */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core (0 or 1) */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ bool dec_mode; /*!< 0: a-law 1: u-law */ } g711_decoder_cfg_t; #define G711_DECODER_TASK_STACK (3 * 1024) #define G711_DECODER_TASK_CORE (0) #define G711_DECODER_TASK_PRIO (5) #define G711_DECODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_G711_DECODER_CONFIG() {\ .out_rb_size = G711_DECODER_RINGBUFFER_SIZE,\ .task_stack = G711_DECODER_TASK_STACK,\ .task_core = G711_DECODER_TASK_CORE,\ .task_prio = G711_DECODER_TASK_PRIO,\ .stack_in_ext = true,\ .dec_mode = 0,\ } /** * @brief Create a handle to an Audio Element to decode incoming data using G711 format * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t g711_decoder_init(g711_decoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/g711_decoder.h
C
apache-2.0
1,614
// Copyright (c) 2020 <ESPRESSIF SYSTEMS (SHANGHAI) CO., LTD> // All rights reserved. #ifndef _G711_ENCODER_H_ #define _G711_ENCODER_H_ #include "esp_err.h" #include "audio_element.h" #ifdef __cplusplus extern "C" { #endif /** * @brief Enum of G711 Encoder enc mode */ typedef enum { G711_ENC_MODE_UNKNOW = -1, /*!< Invalid mode */ G711_ENC_MODE_A_LOW = 0, /* a-law */ G711_ENC_MODE_U_LOW = 1, /* u-law */ G711_ENC_MODE_N_MODE = 2, /* Not Used */ } g711_encoder_mode_t; /** * @brief G711 Encoder configurations */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< Task running in core (0 or 1) */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ bool enc_mode; /*!< 0: a-law 1: u-law */ } g711_encoder_cfg_t; #define G711_ENCODER_TASK_STACK (3 * 1024) #define G711_ENCODER_TASK_CORE (0) #define G711_ENCODER_TASK_PRIO (5) #define G711_ENCODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_G711_ENCODER_CONFIG() { \ .out_rb_size = G711_ENCODER_RINGBUFFER_SIZE, \ .task_stack = G711_ENCODER_TASK_STACK, \ .task_core = G711_ENCODER_TASK_CORE, \ .task_prio = G711_ENCODER_TASK_PRIO, \ .stack_in_ext = true, \ .enc_mode = G711_ENC_MODE_A_LOW, \ } /** * @brief Set G711 encoder mode * * @param self Audio element handle * * @param enc_mode Encode mode choose, value from g711_encoder_mode_t * * @return * ESP_OK * ESP_FAIL */ esp_err_t g711_encoder_set_enc_mode(audio_element_handle_t self, g711_encoder_mode_t enc_mode); /** * @brief Create a handle to an Audio Element to encode incoming data using G711 format * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t g711_encoder_init(g711_encoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/g711_encoder.h
C
apache-2.0
2,334
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _MP3_DECODER_H_ #define _MP3_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif #define MP3_DECODER_TASK_STACK_SIZE (5 * 1024) #define MP3_DECODER_TASK_CORE (0) #define MP3_DECODER_TASK_PRIO (5) #define MP3_DECODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_MP3_DECODER_CONFIG() { \ .out_rb_size = MP3_DECODER_RINGBUFFER_SIZE, \ .task_stack = MP3_DECODER_TASK_STACK_SIZE, \ .task_core = MP3_DECODER_TASK_CORE, \ .task_prio = MP3_DECODER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief Mp3 Decoder configuration */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< CPU core number (0 or 1) where decoder task in running */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } mp3_decoder_cfg_t; /** * @brief Create an Audio Element handle to decode incoming MP3 data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t mp3_decoder_init(mp3_decoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/mp3_decoder.h
C
apache-2.0
1,589
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD // All rights reserved. #ifndef _OGG_DECODER_H_ #define _OGG_DECODER_H_ #include "esp_err.h" #include "audio_element.h" #include "audio_common.h" #ifdef __cplusplus extern "C" { #endif #define OGG_DECODER_TASK_STACK_SIZE (10 * 1024) #define OGG_DECODER_TASK_CORE (0) #define OGG_DECODER_TASK_PRIO (5) #define OGG_DECODER_RINGBUFFER_SIZE (2 * 1024) #define DEFAULT_OGG_DECODER_CONFIG() { \ .out_rb_size = OGG_DECODER_RINGBUFFER_SIZE, \ .task_stack = OGG_DECODER_TASK_STACK_SIZE, \ .task_core = OGG_DECODER_TASK_CORE, \ .task_prio = OGG_DECODER_TASK_PRIO, \ .stack_in_ext = true, \ } /** * @brief OGG Decoder configuration */ typedef struct { int out_rb_size; /*!< Size of output ringbuffer */ int task_stack; /*!< Task stack size */ int task_core; /*!< CPU core number (0 or 1) where decoder task in running */ int task_prio; /*!< Task priority (based on freeRTOS priority) */ bool stack_in_ext; /*!< Try to allocate stack in external memory */ } ogg_decoder_cfg_t; /** * @brief Create an Audio Element handle to decode incoming OGG data * * @param config The configuration * * @return The audio element handle */ audio_element_handle_t ogg_decoder_init(ogg_decoder_cfg_t *config); #ifdef __cplusplus } #endif #endif
YifuLiu/AliOS-Things
hardware/chip/espressif_adf/esp-adf/components/esp-adf-libs/esp_codec/include/codec/ogg_decoder.h
C
apache-2.0
1,590