v2.0.0 Add support for ESP32S2 and update ESP-IDF to 4.4 (#4996)

This is very much still work in progress and much more will change before the final 2.0.0

Some APIs have changed. New libraries have been added. LittleFS included.

Co-authored-by: Seon Rozenblum <seonr@3sprockets.com>
Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
Co-authored-by: geeksville <kevinh@geeksville.com>
Co-authored-by: Mike Dunston <m_dunston@comcast.net>
Co-authored-by: Unexpected Maker <seon@unexpectedmaker.com>
Co-authored-by: Seon Rozenblum <seonr@3sprockets.com>
Co-authored-by: microDev <70126934+microDev1@users.noreply.github.com>
Co-authored-by: tobozo <tobozo@users.noreply.github.com>
Co-authored-by: bobobo1618 <bobobo1618@users.noreply.github.com>
Co-authored-by: lorol <lorolouis@gmail.com>
Co-authored-by: geeksville <kevinh@geeksville.com>
Co-authored-by: Limor "Ladyada" Fried <limor@ladyada.net>
Co-authored-by: Sweety <switi.mhaiske@espressif.com>
Co-authored-by: Loick MAHIEUX <loick111@gmail.com>
Co-authored-by: Larry Bernstone <lbernstone@gmail.com>
Co-authored-by: Valerii Koval <valeros@users.noreply.github.com>
Co-authored-by: 快乐的我531 <2302004040@qq.com>
Co-authored-by: chegewara <imperiaonline4@gmail.com>
Co-authored-by: Clemens Kirchgatterer <clemens@1541.org>
Co-authored-by: Aron Rubin <aronrubin@gmail.com>
Co-authored-by: Pete Lewis <601236+lewispg228@users.noreply.github.com>
This commit is contained in:
Me No Dev
2021-04-05 14:23:58 +03:00
committed by GitHub
parent 46d5afb17f
commit 5502879a5b
5209 changed files with 826360 additions and 322816 deletions

View File

@ -0,0 +1,80 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef _ALARM_H_
#define _ALARM_H_
#include <stdint.h>
#include "esp_timer.h"
typedef struct alarm_t osi_alarm_t;
typedef uint64_t period_ms_t;
typedef esp_timer_cb_t osi_alarm_callback_t;
typedef enum {
OSI_ALARM_ERR_PASS = 0,
OSI_ALARM_ERR_FAIL = -1,
OSI_ALARM_ERR_INVALID_ARG = -2,
OSI_ALARM_ERR_INVALID_STATE = -3,
} osi_alarm_err_t;
#define ALARM_CBS_NUM 50
#define ALARM_ID_BASE 1000
int osi_alarm_create_mux(void);
int osi_alarm_delete_mux(void);
void osi_alarm_init(void);
void osi_alarm_deinit(void);
// Creates a new alarm object. The returned object must be freed by calling
// |alarm_free|. Returns NULL on failure.
osi_alarm_t *osi_alarm_new(const char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire);
// Frees an alarm object created by |alarm_new|. |alarm| may be NULL. If the
// alarm is pending, it will be cancelled. It is not safe to call |alarm_free|
// from inside the callback of |alarm|.
void osi_alarm_free(osi_alarm_t *alarm);
// Sets an alarm to fire |cb| after the given |deadline|. Note that |deadline| is the
// number of milliseconds relative to the current time. |data| is a context variable
// for the callback and may be NULL. |cb| will be called back in the context of an
// unspecified thread (i.e. it will not be called back in the same thread as the caller).
// |alarm| and |cb| may not be NULL.
osi_alarm_err_t osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout);
// Sets an periodic alarm to fire |cb| each given |period|.
osi_alarm_err_t osi_alarm_set_periodic(osi_alarm_t *alarm, period_ms_t period);
// This function cancels the |alarm| if it was previously set. When this call
// returns, the caller has a guarantee that the callback is not in progress and
// will not be called if it hasn't already been called. This function is idempotent.
// |alarm| may not be NULL.
osi_alarm_err_t osi_alarm_cancel(osi_alarm_t *alarm);
// Figure out how much time until next expiration.
// Returns 0 if not armed. |alarm| may not be NULL.
// only for oneshot alarm, not for periodic alarm
// TODO: Remove this function once PM timers can be re-factored
period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm);
// Alarm-related state cleanup
//void alarm_cleanup(void);
uint32_t osi_time_get_os_boottime_ms(void);
#endif /*_ALARM_H_*/

View File

@ -0,0 +1,145 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef _ALLOCATOR_H_
#define _ALLOCATOR_H_
#include <stddef.h>
#include <stdlib.h>
#include "esp_heap_caps.h"
char *osi_strdup(const char *str);
void *osi_malloc_func(size_t size);
void *osi_calloc_func(size_t size);
void osi_free_func(void *ptr);
#if HEAP_MEMORY_DEBUG
void osi_mem_dbg_init(void);
void osi_mem_dbg_record(void *p, int size, const char *func, int line);
void osi_mem_dbg_clean(void *p, const char *func, int line);
void osi_mem_dbg_show(void);
uint32_t osi_mem_dbg_get_max_size(void);
uint32_t osi_mem_dbg_get_current_size(void);
void osi_men_dbg_set_section_start(uint8_t index);
void osi_men_dbg_set_section_end(uint8_t index);
uint32_t osi_mem_dbg_get_max_size_section(uint8_t index);
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define osi_malloc(size) \
({ \
void *p; \
p = heap_caps_malloc_prefer(size, 2, \
MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, \
MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \
})
#define osi_calloc(size) \
({ \
void *p; \
p = heap_caps_calloc_prefer(1, size, 2, \
MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, \
MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \
})
#else
#define osi_malloc(size) \
({ \
void *p; \
p = malloc((size)); \
osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \
})
#define osi_calloc(size) \
({ \
void *p; \
p = calloc(1, (size)); \
osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \
})
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
#if 0
#define osi_malloc(size) \
do { \
void *p; \
\
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST \
p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
#else \
p = malloc((size)); \
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */ \
osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \
}while(0)
#define osi_calloc(size) \
do { \
void *p; \
\
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST \
p = heap_caps_calloc_prefer(1, size, 2, \
MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, \
MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
#else \
p = calloc(1, (size)); \
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */ \
osi_mem_dbg_record(p, size, __func__, __LINE__); \
(void *)p; \
} while(0)
#endif
#define osi_free(ptr) \
do { \
void *tmp_point = (void *)(ptr); \
osi_mem_dbg_clean(tmp_point, __func__, __LINE__); \
free(tmp_point); \
} while (0)
#else
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
#define osi_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define osi_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#else
#define osi_malloc(size) malloc((size))
#define osi_calloc(size) calloc(1, (size))
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
#define osi_free(p) free((p))
#endif /* HEAP_MEMORY_DEBUG */
#define FREE_AND_RESET(a) \
do { \
if (a) { \
osi_free(a); \
a = NULL; \
} \
}while (0)
#endif /* _ALLOCATOR_H_ */

View File

@ -0,0 +1,59 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef _BUFFER_H_
#define _BUFFER_H_
#include <stdbool.h>
#include <stddef.h>
typedef struct buffer_t buffer_t;
// Returns a new buffer of |size| bytes. Returns NULL if a buffer could not be
// allocated. |size| must be non-zero. The caller must release this buffer with
// |buffer_free|.
buffer_t *buffer_new(size_t size);
// Creates a new reference to the buffer |buf|. A reference is indistinguishable
// from the original: writes to the original will be reflected in the reference
// and vice versa. In other words, this function creates an alias to |buf|. The
// caller must release the returned buffer with |buffer_free|. Note that releasing
// the returned buffer does not release |buf|. |buf| must not be NULL.
buffer_t *buffer_new_ref(const buffer_t *buf);
// Creates a new reference to the last |slice_size| bytes of |buf|. See
// |buffer_new_ref| for a description of references. |slice_size| must be
// greater than 0 and may be at most |buffer_length|
// (0 < slice_size <= buffer_length). |buf| must not be NULL.
buffer_t *buffer_new_slice(const buffer_t *buf, size_t slice_size);
// Frees a buffer object. |buf| may be NULL.
void buffer_free(buffer_t *buf);
// Returns a pointer to a writeable memory region for |buf|. All references
// and slices that share overlapping bytes will also be written to when
// writing to the returned pointer. The caller may safely write up to
// |buffer_length| consecutive bytes starting at the address returned by
// this function. |buf| must not be NULL.
void *buffer_ptr(const buffer_t *buf);
// Returns the length of the writeable memory region referred to by |buf|.
// |buf| must not be NULL.
size_t buffer_length(const buffer_t *buf);
#endif /*_BUFFER_H_*/

View File

@ -0,0 +1,148 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __CONFIG_H__
#define __CONFIG_H__
// This module implements a configuration parser. Clients can query the
// contents of a configuration file through the interface provided here.
// The current implementation is read-only; mutations are only kept in
// memory. This parser supports the INI file format.
// Implementation notes:
// - Key/value pairs that are not within a section are assumed to be under
// the |CONFIG_DEFAULT_SECTION| section.
// - Multiple sections with the same name will be merged as if they were in
// a single section.
// - Empty sections with no key/value pairs will be treated as if they do
// not exist. In other words, |config_has_section| will return false for
// empty sections.
// - Duplicate keys in a section will overwrite previous values.
// - All strings are case sensitive.
#include <stdbool.h>
// The default section name to use if a key/value pair is not defined within
// a section.
#define CONFIG_DEFAULT_SECTION "Global"
typedef struct config_t config_t;
typedef struct config_section_node_t config_section_node_t;
// Creates a new config object with no entries (i.e. not backed by a file).
// This function returns a config object or NULL on error. Clients must call
// |config_free| on the returned handle when it is no longer required.
config_t *config_new_empty(void);
// Loads the specified file and returns a handle to the config file. If there
// was a problem loading the file or allocating memory, this function returns
// NULL. Clients must call |config_free| on the returned handle when it is no
// longer required. |filename| must not be NULL and must point to a readable
// file on the filesystem.
config_t *config_new(const char *filename);
// Frees resources associated with the config file. No further operations may
// be performed on the |config| object after calling this function. |config|
// may be NULL.
void config_free(config_t *config);
// Returns true if the config file contains a section named |section|. If
// the section has no key/value pairs in it, this function will return false.
// |config| and |section| must not be NULL.
bool config_has_section(const config_t *config, const char *section);
// Returns true if the config file has a key named |key| under |section|.
// Returns false otherwise. |config|, |section|, and |key| must not be NULL.
bool config_has_key(const config_t *config, const char *section, const char *key);
// Returns true if the config file has a key named |key| and the key_value.
// Returns false otherwise. |config|, |key|, and |key_value| must not be NULL.
bool config_has_key_in_section(config_t *config, const char *key, char *key_value);
// Returns the integral value for a given |key| in |section|. If |section|
// or |key| do not exist, or the value cannot be fully converted to an integer,
// this function returns |def_value|. |config|, |section|, and |key| must not
// be NULL.
int config_get_int(const config_t *config, const char *section, const char *key, int def_value);
// Returns the boolean value for a given |key| in |section|. If |section|
// or |key| do not exist, or the value cannot be converted to a boolean, this
// function returns |def_value|. |config|, |section|, and |key| must not be NULL.
bool config_get_bool(const config_t *config, const char *section, const char *key, bool def_value);
// Returns the string value for a given |key| in |section|. If |section| or
// |key| do not exist, this function returns |def_value|. The returned string
// is owned by the config module and must not be freed. |config|, |section|,
// and |key| must not be NULL. |def_value| may be NULL.
const char *config_get_string(const config_t *config, const char *section, const char *key, const char *def_value);
// Sets an integral value for the |key| in |section|. If |key| or |section| do
// not already exist, this function creates them. |config|, |section|, and |key|
// must not be NULL.
void config_set_int(config_t *config, const char *section, const char *key, int value);
// Sets a boolean value for the |key| in |section|. If |key| or |section| do
// not already exist, this function creates them. |config|, |section|, and |key|
// must not be NULL.
void config_set_bool(config_t *config, const char *section, const char *key, bool value);
// Sets a string value for the |key| in |section|. If |key| or |section| do
// not already exist, this function creates them. |config|, |section|, |key|, and
// |value| must not be NULL.
void config_set_string(config_t *config, const char *section, const char *key, const char *value, bool insert_back);
// Removes |section| from the |config| (and, as a result, all keys in the section).
// Returns true if |section| was found and removed from |config|, false otherwise.
// Neither |config| nor |section| may be NULL.
bool config_remove_section(config_t *config, const char *section);
// Removes one specific |key| residing in |section| of the |config|. Returns true
// if the section and key were found and the key was removed, false otherwise.
// None of |config|, |section|, or |key| may be NULL.
bool config_remove_key(config_t *config, const char *section, const char *key);
// Returns an iterator to the first section in the config file. If there are no
// sections, the iterator will equal the return value of |config_section_end|.
// The returned pointer must be treated as an opaque handle and must not be freed.
// The iterator is invalidated on any config mutating operation. |config| may not
// be NULL.
const config_section_node_t *config_section_begin(const config_t *config);
// Returns an iterator to one past the last section in the config file. It does not
// represent a valid section, but can be used to determine if all sections have been
// iterated over. The returned pointer must be treated as an opaque handle and must
// not be freed and must not be iterated on (must not call |config_section_next| on
// it). |config| may not be NULL.
const config_section_node_t *config_section_end(const config_t *config);
// Moves |iter| to the next section. If there are no more sections, |iter| will
// equal the value of |config_section_end|. |iter| may not be NULL and must be
// a pointer returned by either |config_section_begin| or |config_section_next|.
const config_section_node_t *config_section_next(const config_section_node_t *iter);
// Returns the name of the section referred to by |iter|. The returned pointer is
// owned by the config module and must not be freed by the caller. The pointer will
// remain valid until |config_free| is called. |iter| may not be NULL and must not
// equal the value returned by |config_section_end|.
const char *config_section_name(const config_section_node_t *iter);
// Saves |config| to a file given by |filename|. Note that this could be a destructive
// operation: if |filename| already exists, it will be overwritten. The config
// module does not preserve comments or formatting so if a config file was opened
// with |config_new| and subsequently overwritten with |config_save|, all comments
// and special formatting in the original file will be lost. Neither |config| nor
// |filename| may be NULL.
bool config_save(const config_t *config, const char *filename);
#endif /* #ifndef __CONFIG_H__ */

View File

@ -0,0 +1,125 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef _FIXED_QUEUE_H_
#define _FIXED_QUEUE_H_
#include <stdbool.h>
#include "osi/list.h"
#include "osi/semaphore.h"
#ifndef QUEUE_SIZE_MAX
#define QUEUE_SIZE_MAX 254
#endif
#define FIXED_QUEUE_MAX_TIMEOUT OSI_SEM_MAX_TIMEOUT
struct fixed_queue_t;
typedef struct fixed_queue_t fixed_queue_t;
//typedef struct reactor_t reactor_t;
typedef void (*fixed_queue_free_cb)(void *data);
typedef void (*fixed_queue_cb)(fixed_queue_t *queue);
// Creates a new fixed queue with the given |capacity|. If more elements than
// |capacity| are added to the queue, the caller is blocked until space is
// made available in the queue. Returns NULL on failure. The caller must free
// the returned queue with |fixed_queue_free|.
fixed_queue_t *fixed_queue_new(size_t capacity);
// Freeing a queue that is currently in use (i.e. has waiters
// blocked on it) results in undefined behaviour.
void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb);
// Returns a value indicating whether the given |queue| is empty. If |queue|
// is NULL, the return value is true.
bool fixed_queue_is_empty(fixed_queue_t *queue);
// Returns the length of the |queue|. If |queue| is NULL, the return value
// is 0.
size_t fixed_queue_length(fixed_queue_t *queue);
// Returns the maximum number of elements this queue may hold. |queue| may
// not be NULL.
size_t fixed_queue_capacity(fixed_queue_t *queue);
// Enqueues the given |data| into the |queue|. The caller will be blocked or immediately return or wait for timeout according to the parameter timeout.
// If enqueue failed, it will return false, otherwise return true
bool fixed_queue_enqueue(fixed_queue_t *queue, void *data, uint32_t timeout);
// Dequeues the next element from |queue|. If the queue is currently empty,
// this function will block the caller until an item is enqueued or immediately return or wait for timeout according to the parameter timeout.
// If dequeue failed, it will return NULL, otherwise return a point.
void *fixed_queue_dequeue(fixed_queue_t *queue, uint32_t timeout);
// Returns the first element from |queue|, if present, without dequeuing it.
// This function will never block the caller. Returns NULL if there are no
// elements in the queue or |queue| is NULL.
void *fixed_queue_try_peek_first(fixed_queue_t *queue);
// Returns the last element from |queue|, if present, without dequeuing it.
// This function will never block the caller. Returns NULL if there are no
// elements in the queue or |queue| is NULL.
void *fixed_queue_try_peek_last(fixed_queue_t *queue);
// Tries to remove a |data| element from the middle of the |queue|. This
// function will never block the caller. If the queue is empty or NULL, this
// function returns NULL immediately. |data| may not be NULL. If the |data|
// element is found in the queue, a pointer to the removed data is returned,
// otherwise NULL.
void *fixed_queue_try_remove_from_queue(fixed_queue_t *queue, void *data);
// Returns the iterateable list with all entries in the |queue|. This function
// will never block the caller. |queue| may not be NULL.
//
// NOTE: The return result of this function is not thread safe: the list could
// be modified by another thread, and the result would be unpredictable.
// TODO: The usage of this function should be refactored, and the function
// itself should be removed.
list_t *fixed_queue_get_list(fixed_queue_t *queue);
// This function returns a valid file descriptor. Callers may perform one
// operation on the fd: select(2). If |select| indicates that the file
// descriptor is readable, the caller may call |fixed_queue_enqueue| without
// blocking. The caller must not close the returned file descriptor. |queue|
// may not be NULL.
//int fixed_queue_get_enqueue_fd(const fixed_queue_t *queue);
// This function returns a valid file descriptor. Callers may perform one
// operation on the fd: select(2). If |select| indicates that the file
// descriptor is readable, the caller may call |fixed_queue_dequeue| without
// blocking. The caller must not close the returned file descriptor. |queue|
// may not be NULL.
//int fixed_queue_get_dequeue_fd(const fixed_queue_t *queue);
// Registers |queue| with |reactor| for dequeue operations. When there is an element
// in the queue, ready_cb will be called. The |context| parameter is passed, untouched,
// to the callback routine. Neither |queue|, nor |reactor|, nor |read_cb| may be NULL.
// |context| may be NULL.
void fixed_queue_register_dequeue(fixed_queue_t *queue, fixed_queue_cb ready_cb);
// Unregisters the dequeue ready callback for |queue| from whichever reactor
// it is registered with, if any. This function is idempotent.
void fixed_queue_unregister_dequeue(fixed_queue_t *queue);
void fixed_queue_process(fixed_queue_t *queue);
list_t *fixed_queue_get_list(fixed_queue_t *queue);
#endif

View File

@ -0,0 +1,53 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef __FUTURE_H__
#define __FUTURE_H__
#include "osi/semaphore.h"
struct future {
bool ready_can_be_called;
osi_sem_t semaphore; // NULL semaphore means immediate future
void *result;
};
typedef struct future future_t;
#define FUTURE_SUCCESS ((void *)1)
#define FUTURE_FAIL ((void *)0)
// Constructs a new future_t object. Returns NULL on failure.
future_t *future_new(void);
// Constructs a new future_t object with an immediate |value|. No waiting will
// occur in the call to |future_await| because the value is already present.
// Returns NULL on failure.
future_t *future_new_immediate(void *value);
// Signals that the |future| is ready, passing |value| back to the context
// waiting for the result. Must only be called once for every future.
// |future| may not be NULL.
void future_ready(future_t *future, void *value);
// Waits for the |future| to be ready. Returns the value set in |future_ready|.
// Frees the future before return. |future| may not be NULL.
void *future_await(future_t *async_result);
//Free the future if this "future" is not used
void future_free(future_t *future);
#endif /* __FUTURE_H__ */

View File

@ -0,0 +1,37 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef _HASH_FUNCTIONS_H_
#define _HASH_FUNCTIONS_H_
#include "osi/hash_map.h"
typedef unsigned char hash_key_t[4];
hash_index_t hash_function_naive(const void *key);
hash_index_t hash_function_integer(const void *key);
// Hashes a pointer based only on its address value
hash_index_t hash_function_pointer(const void *key);
hash_index_t hash_function_string(const void *key);
void hash_function_blob(const unsigned char *s, unsigned int len, hash_key_t h);
#endif /* _HASH_FUNCTIONS_H_ */

View File

@ -0,0 +1,110 @@
/******************************************************************************
*
* Copyright (C) 2014 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef _HASH_MAP_H_
#define _HASH_MAP_H_
#include <stdbool.h>
#include <stdint.h>
struct hash_map_t;
typedef struct hash_map_t hash_map_t;
typedef struct hash_map_entry_t {
const void *key;
void *data;
const hash_map_t *hash_map;
} hash_map_entry_t;
typedef size_t hash_index_t;
// Takes a key structure and returns a hash value.
typedef hash_index_t (*hash_index_fn)(const void *key);
typedef bool (*hash_map_iter_cb)(hash_map_entry_t *hash_entry, void *context);
typedef bool (*key_equality_fn)(const void *x, const void *y);
typedef void (*key_free_fn)(void *data);
typedef void (*data_free_fn)(void *data);
// Returns a new, empty hash_map. Returns NULL if not enough memory could be allocated
// for the hash_map structure. The returned hash_map must be freed with |hash_map_free|.
// The |num_bucket| specifies the number of hashable buckets for the map and must not
// be zero. The |hash_fn| specifies a hash function to be used and must not be NULL.
// The |key_fn| and |data_fn| are called whenever a hash_map element is removed from
// the hash_map. They can be used to release resources held by the hash_map element,
// e.g. memory or file descriptor. |key_fn| and |data_fn| may be NULL if no cleanup
// is necessary on element removal. |equality_fn| is used to check for key equality.
// If |equality_fn| is NULL, default pointer equality is used.
hash_map_t *hash_map_new(
size_t size,
hash_index_fn hash_fn,
key_free_fn key_fn,
data_free_fn data_fn,
key_equality_fn equality_fn);
// Frees the hash_map. This function accepts NULL as an argument, in which case it
// behaves like a no-op.
void hash_map_free(hash_map_t *hash_map);
// Returns true if the hash_map is empty (has no elements), false otherwise.
// Note that a NULL |hash_map| is not the same as an empty |hash_map|. This function
// does not accept a NULL |hash_map|.
//bool hash_map_is_empty(const hash_map_t *hash_map);
// Returns the number of elements in the hash map. This function does not accept a
// NULL |hash_map|.
//size_t hash_map_size(const hash_map_t *hash_map);
// Returns the number of buckets in the hash map. This function does not accept a
// NULL |hash_map|.
//size_t hash_map_num_buckets(const hash_map_t *hash_map);
// Returns true if the hash_map has a valid entry for the presented key.
// This function does not accept a NULL |hash_map|.
bool hash_map_has_key(const hash_map_t *hash_map, const void *key);
// Returns the element indexed by |key| in the hash_map without removing it. |hash_map|
// may not be NULL. Returns NULL if no entry indexed by |key|.
void *hash_map_get(const hash_map_t *hash_map, const void *key);
// Sets the value |data| indexed by |key| into the |hash_map|. Neither |data| nor
// |hash_map| may be NULL. This function does not make copies of |data| nor |key|
// so the pointers must remain valid at least until the element is removed from the
// hash_map or the hash_map is freed. Returns true if |data| could be set, false
// otherwise (e.g. out of memory).
bool hash_map_set(hash_map_t *hash_map, const void *key, void *data);
// Removes data indexed by |key| from the hash_map. |hash_map| may not be NULL.
// If |key_fn| or |data_fn| functions were specified in |hash_map_new|, they
// will be called back with |key| or |data| respectively. This function returns true
// if |key| was found in the hash_map and removed, false otherwise.
bool hash_map_erase(hash_map_t *hash_map, const void *key);
// Removes all elements in the hash_map. Calling this function will return the hash_map
// to the same state it was in after |hash_map_new|. |hash_map| may not be NULL.
void hash_map_clear(hash_map_t *hash_map);
// Iterates through the entire |hash_map| and calls |callback| for each data
// element and passes through the |context| argument. If the hash_map is
// empty, |callback| will never be called. It is not safe to mutate the
// hash_map inside the callback. Neither |hash_map| nor |callback| may be NULL.
// If |callback| returns false, the iteration loop will immediately exit.
void hash_map_foreach(hash_map_t *hash_map, hash_map_iter_cb callback, void *context);
#endif /* _HASH_MAP_H_ */

View File

@ -0,0 +1,121 @@
#ifndef _LIST_H_
#define _LIST_H_
#include <stdbool.h>
#include <stddef.h>
struct list_node_t;
typedef struct list_node_t list_node_t;
struct list_t;
typedef struct list_t list_t;
typedef void (*list_free_cb)(void *data);
typedef bool (*list_iter_cb)(void *data, void *context);
// Returns a new, empty list. Returns NULL if not enough memory could be allocated
// for the list structure. The returned list must be freed with |list_free|. The
// |callback| specifies a function to be called whenever a list element is removed
// from the list. It can be used to release resources held by the list element, e.g.
// memory or file descriptor. |callback| may be NULL if no cleanup is necessary on
// element removal.
list_t *list_new(list_free_cb callback);
list_node_t *list_free_node(list_t *list, list_node_t *node);
// similar with list_free_node, this function doesn't free the node data
list_node_t *list_delete_node(list_t *list, list_node_t *node);
// Frees the list. This function accepts NULL as an argument, in which case it
// behaves like a no-op.
void list_free(list_t *list);
// Returns true if |list| is empty (has no elements), false otherwise.
// |list| may not be NULL.
bool list_is_empty(const list_t *list);
// Returns true if the list contains |data|, false otherwise.
// |list| may not be NULL.
bool list_contains(const list_t *list, const void *data);
// Returns list_node which contains |data|, NULL otherwise.
// |list| may not be NULL.
list_node_t *list_get_node(const list_t *list, const void *data);
// Returns the length of the |list|. |list| may not be NULL.
size_t list_length(const list_t *list);
// Returns the first element in the list without removing it. |list| may not
// be NULL or empty.
void *list_front(const list_t *list);
// Returns the last element in the list without removing it. |list| may not
// be NULL or empty.
void *list_back(const list_t *list);
list_node_t *list_back_node(const list_t *list);
// Inserts |data| after |prev_node| in |list|. |data|, |list|, and |prev_node|
// may not be NULL. This function does not make a copy of |data| so the pointer
// must remain valid at least until the element is removed from the list or the
// list is freed. Returns true if |data| could be inserted, false otherwise
// (e.g. out of memory).
bool list_insert_after(list_t *list, list_node_t *prev_node, void *data);
// Inserts |data| at the beginning of |list|. Neither |data| nor |list| may be NULL.
// This function does not make a copy of |data| so the pointer must remain valid
// at least until the element is removed from the list or the list is freed.
// Returns true if |data| could be inserted, false otherwise (e.g. out of memory).
bool list_prepend(list_t *list, void *data);
// Inserts |data| at the end of |list|. Neither |data| nor |list| may be NULL.
// This function does not make a copy of |data| so the pointer must remain valid
// at least until the element is removed from the list or the list is freed.
// Returns true if |data| could be inserted, false otherwise (e.g. out of memory).
bool list_append(list_t *list, void *data);
// Removes |data| from the list. Neither |list| nor |data| may be NULL. If |data|
// is inserted multiple times in the list, this function will only remove the first
// instance. If a free function was specified in |list_new|, it will be called back
// with |data|. This function returns true if |data| was found in the list and removed,
// false otherwise.
//list_node_t list_remove_node(list_t *list, list_node_t *prev_node, list_node_t *node);
//list_node_t list_insert_node(list_t *list, list_node_t *prev_node, list_node_t *node);
bool list_remove(list_t *list, void *data);
// similar with list_remove, but do not free the node data
bool list_delete(list_t *list, void *data);
// Removes all elements in the list. Calling this function will return the list to the
// same state it was in after |list_new|. |list| may not be NULL.
void list_clear(list_t *list);
// Iterates through the entire |list| and calls |callback| for each data element.
// If the list is empty, |callback| will never be called. It is safe to mutate the
// list inside the callback. If an element is added before the node being visited,
// there will be no callback for the newly-inserted node. Neither |list| nor
// |callback| may be NULL.
list_node_t *list_foreach(const list_t *list, list_iter_cb callback, void *context);
// Returns an iterator to the first element in |list|. |list| may not be NULL.
// The returned iterator is valid as long as it does not equal the value returned
// by |list_end|.
list_node_t *list_begin(const list_t *list);
// Returns an iterator that points past the end of the list. In other words,
// this function returns the value of an invalid iterator for the given list.
// When an iterator has the same value as what's returned by this function, you
// may no longer call |list_next| with the iterator. |list| may not be NULL.
list_node_t *list_end(const list_t *list);
// Given a valid iterator |node|, this function returns the next value for the
// iterator. If the returned value equals the value returned by |list_end|, the
// iterator has reached the end of the list and may no longer be used for any
// purpose.
list_node_t *list_next(const list_node_t *node);
// Returns the value stored at the location pointed to by the iterator |node|.
// |node| must not equal the value returned by |list_end|.
void *list_node(const list_node_t *node);
#endif /* _LIST_H_ */

View File

@ -0,0 +1,52 @@
/******************************************************************************
*
* Copyright (C) 2015 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef __MUTEX_H__
#define __MUTEX_H__
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "osi/semaphore.h"
#define OSI_MUTEX_MAX_TIMEOUT OSI_SEM_MAX_TIMEOUT
#define osi_mutex_valid( x ) ( ( ( *x ) == NULL) ? pdFALSE : pdTRUE )
#define osi_mutex_set_invalid( x ) ( ( *x ) = NULL )
typedef xSemaphoreHandle osi_mutex_t;
int osi_mutex_new(osi_mutex_t *mutex);
int osi_mutex_lock(osi_mutex_t *mutex, uint32_t timeout);
void osi_mutex_unlock(osi_mutex_t *mutex);
void osi_mutex_free(osi_mutex_t *mutex);
/* Just for a global mutex */
int osi_mutex_global_init(void);
void osi_mutex_global_deinit(void);
void osi_mutex_global_lock(void);
void osi_mutex_global_unlock(void);
#endif /* __MUTEX_H__ */

View File

@ -0,0 +1,16 @@
#ifndef _OSI_H_
#define _OSI_H_
#include <stdbool.h>
#include <stdint.h>
#define UNUSED_ATTR __attribute__((unused))
#define CONCAT(a, b) a##b
#define COMPILE_ASSERT(x)
int osi_init(void);
void osi_deinit(void);
#endif /*_OSI_H_*/

View File

@ -0,0 +1,43 @@
/******************************************************************************
*
* Copyright (C) 2015 Google, Inc.
*
* 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.
*
******************************************************************************/
#ifndef __SEMAPHORE_H__
#define __SEMAPHORE_H__
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#define OSI_SEM_MAX_TIMEOUT 0xffffffffUL
typedef xSemaphoreHandle osi_sem_t;
#define osi_sem_valid( x ) ( ( ( *x ) == NULL) ? pdFALSE : pdTRUE )
#define osi_sem_set_invalid( x ) ( ( *x ) = NULL )
int osi_sem_new(osi_sem_t *sem, uint32_t max_count, uint32_t init_count);
void osi_sem_free(osi_sem_t *sem);
int osi_sem_take(osi_sem_t *sem, uint32_t timeout);
void osi_sem_give(osi_sem_t *sem);
#endif /* __SEMAPHORE_H__ */

View File

@ -0,0 +1,91 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __THREAD_H__
#define __THREAD_H__
#include "freertos/FreeRTOSConfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "osi/semaphore.h"
#include "esp_task.h"
#include "bt_common.h"
#define portBASE_TYPE int
#define OSI_THREAD_MAX_TIMEOUT OSI_SEM_MAX_TIMEOUT
struct osi_thread;
typedef struct osi_thread osi_thread_t;
typedef void (*osi_thread_func_t)(void *context);
typedef enum {
OSI_THREAD_CORE_0 = 0,
OSI_THREAD_CORE_1,
OSI_THREAD_CORE_AFFINITY,
} osi_thread_core_t;
/*
* brief: Create a thread or task
* param name: thread name
* param stack_size: thread stack size
* param priority: thread priority
* param core: the CPU core which this thread run, OSI_THREAD_CORE_AFFINITY means unspecific CPU core
* param work_queue_num: speicify queue number, the queue[0] has highest priority, and the priority is decrease by index
* return : if create successfully, return thread handler; otherwise return NULL.
*/
osi_thread_t *osi_thread_create(const char *name, size_t stack_size, int priority, osi_thread_core_t core, uint8_t work_queue_num);
/*
* brief: Destroy a thread or task
* param thread: point of thread handler
*/
void osi_thread_free(osi_thread_t *thread);
/*
* brief: Post an msg to a thread and told the thread call the function
* param thread: point of thread handler
* param func: callback function that called by target thread
* param context: argument of callback function
* param queue_idx: the queue which the msg send to
* param timeout: post timeout, OSI_THREAD_MAX_TIMEOUT means blocking forever, 0 means never blocking, others means block millisecond
* return : if post successfully, return true, otherwise return false
*/
bool osi_thread_post(osi_thread_t *thread, osi_thread_func_t func, void *context, int queue_idx, uint32_t timeout);
/*
* brief: Set the priority of thread
* param thread: point of thread handler
* param priority: priority
* return : if set successfully, return true, otherwise return false
*/
bool osi_thread_set_priority(osi_thread_t *thread, int priority);
/* brief: Get thread name
* param thread: point of thread handler
* return: constant point of thread name
*/
const char *osi_thread_name(osi_thread_t *thread);
/* brief: Get the size of the specified queue
* param thread: point of thread handler
* param wq_idx: the queue index of the thread
* return: queue size
*/
int osi_thread_queue_wait_size(osi_thread_t *thread, int wq_idx);
#endif /* __THREAD_H__ */

View File

@ -0,0 +1,373 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_A2DP_API_H__
#define __ESP_A2DP_API_H__
#include "esp_err.h"
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
/// Media codec types supported by A2DP
#define ESP_A2D_MCT_SBC (0) /*!< SBC */
#define ESP_A2D_MCT_M12 (0x01) /*!< MPEG-1, 2 Audio */
#define ESP_A2D_MCT_M24 (0x02) /*!< MPEG-2, 4 AAC */
#define ESP_A2D_MCT_ATRAC (0x04) /*!< ATRAC family */
#define ESP_A2D_MCT_NON_A2DP (0xff)
typedef uint8_t esp_a2d_mct_t;
/** A2DP media codec capabilities union
*/
typedef struct {
esp_a2d_mct_t type; /*!< A2DP media codec type */
#define ESP_A2D_CIE_LEN_SBC (4)
#define ESP_A2D_CIE_LEN_M12 (4)
#define ESP_A2D_CIE_LEN_M24 (6)
#define ESP_A2D_CIE_LEN_ATRAC (7)
union {
uint8_t sbc[ESP_A2D_CIE_LEN_SBC]; /*!< SBC codec capabilities */
uint8_t m12[ESP_A2D_CIE_LEN_M12]; /*!< MPEG-1,2 audio codec capabilities */
uint8_t m24[ESP_A2D_CIE_LEN_M24]; /*!< MPEG-2, 4 AAC audio codec capabilities */
uint8_t atrac[ESP_A2D_CIE_LEN_ATRAC]; /*!< ATRAC family codec capabilities */
} cie; /*!< A2DP codec information element */
} __attribute__((packed)) esp_a2d_mcc_t;
/// Bluetooth A2DP connection states
typedef enum {
ESP_A2D_CONNECTION_STATE_DISCONNECTED = 0, /*!< connection released */
ESP_A2D_CONNECTION_STATE_CONNECTING, /*!< connecting remote device */
ESP_A2D_CONNECTION_STATE_CONNECTED, /*!< connection established */
ESP_A2D_CONNECTION_STATE_DISCONNECTING /*!< disconnecting remote device */
} esp_a2d_connection_state_t;
/// Bluetooth A2DP disconnection reason
typedef enum {
ESP_A2D_DISC_RSN_NORMAL = 0, /*!< Finished disconnection that is initiated by local or remote device */
ESP_A2D_DISC_RSN_ABNORMAL /*!< Abnormal disconnection caused by signal loss */
} esp_a2d_disc_rsn_t;
/// Bluetooth A2DP datapath states
typedef enum {
ESP_A2D_AUDIO_STATE_REMOTE_SUSPEND = 0, /*!< audio stream datapath suspended by remote device */
ESP_A2D_AUDIO_STATE_STOPPED, /*!< audio stream datapath stopped */
ESP_A2D_AUDIO_STATE_STARTED, /*!< audio stream datapath started */
} esp_a2d_audio_state_t;
/// A2DP media control command acknowledgement code
typedef enum {
ESP_A2D_MEDIA_CTRL_ACK_SUCCESS = 0, /*!< media control command is acknowledged with success */
ESP_A2D_MEDIA_CTRL_ACK_FAILURE, /*!< media control command is acknowledged with failure */
ESP_A2D_MEDIA_CTRL_ACK_BUSY, /*!< media control command is rejected, as previous command is not yet acknowledged */
} esp_a2d_media_ctrl_ack_t;
/// A2DP media control commands
typedef enum {
ESP_A2D_MEDIA_CTRL_NONE = 0, /*!< Not for application use, use inside stack only. */
ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY, /*!< check whether AVDTP is connected, only used in A2DP source */
ESP_A2D_MEDIA_CTRL_START, /*!< command to set up media transmission channel */
ESP_A2D_MEDIA_CTRL_STOP, /*!< command to stop media transmission */
ESP_A2D_MEDIA_CTRL_SUSPEND, /*!< command to suspend media transmission */
} esp_a2d_media_ctrl_t;
/// Bluetooth A2DP Initiation states
typedef enum {
ESP_A2D_DEINIT_SUCCESS = 0, /*!< A2DP profile deinit successful event */
ESP_A2D_INIT_SUCCESS /*!< A2DP profile deinit successful event */
} esp_a2d_init_state_t;
/// A2DP callback events
typedef enum {
ESP_A2D_CONNECTION_STATE_EVT = 0, /*!< connection state changed event */
ESP_A2D_AUDIO_STATE_EVT, /*!< audio stream transmission state changed event */
ESP_A2D_AUDIO_CFG_EVT, /*!< audio codec is configured, only used for A2DP SINK */
ESP_A2D_MEDIA_CTRL_ACK_EVT, /*!< acknowledge event in response to media control commands */
ESP_A2D_PROF_STATE_EVT, /*!< indicate a2dp init&deinit complete */
} esp_a2d_cb_event_t;
/// A2DP state callback parameters
typedef union {
/**
* @brief ESP_A2D_CONNECTION_STATE_EVT
*/
struct a2d_conn_stat_param {
esp_a2d_connection_state_t state; /*!< one of values from esp_a2d_connection_state_t */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
esp_a2d_disc_rsn_t disc_rsn; /*!< reason of disconnection for "DISCONNECTED" */
} conn_stat; /*!< A2DP connection status */
/**
* @brief ESP_A2D_AUDIO_STATE_EVT
*/
struct a2d_audio_stat_param {
esp_a2d_audio_state_t state; /*!< one of the values from esp_a2d_audio_state_t */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} audio_stat; /*!< audio stream playing state */
/**
* @brief ESP_A2D_AUDIO_CFG_EVT
*/
struct a2d_audio_cfg_param {
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
esp_a2d_mcc_t mcc; /*!< A2DP media codec capability information */
} audio_cfg; /*!< media codec configuration information */
/**
* @brief ESP_A2D_MEDIA_CTRL_ACK_EVT
*/
struct media_ctrl_stat_param {
esp_a2d_media_ctrl_t cmd; /*!< media control commands to acknowledge */
esp_a2d_media_ctrl_ack_t status; /*!< acknowledgement to media control commands */
} media_ctrl_stat; /*!< status in acknowledgement to media control commands */
/**
* @brief ESP_A2D_PROF_STATE_EVT
*/
struct a2d_prof_stat_param {
esp_a2d_init_state_t init_state; /*!< a2dp profile state param */
} a2d_prof_stat; /*!< status to indicate a2d prof init or deinit */
} esp_a2d_cb_param_t;
/**
* @brief A2DP profile callback function type
*
* @param event : Event type
*
* @param param : Pointer to callback parameter
*/
typedef void (* esp_a2d_cb_t)(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
/**
* @brief A2DP sink data callback function
*
* @param[in] buf : pointer to the data received from A2DP source device and is PCM format decoded from SBC decoder;
* buf references to a static memory block and can be overwritten by upcoming data
*
* @param[in] len : size(in bytes) in buf
*/
typedef void (* esp_a2d_sink_data_cb_t)(const uint8_t *buf, uint32_t len);
/**
* @brief A2DP source data read callback function
*
* @param[in] buf : buffer to be filled with PCM data stream from higher layer
*
* @param[in] len : size(in bytes) of data block to be copied to buf. -1 is an indication to user
* that data buffer shall be flushed
*
* @return size of bytes read successfully, if the argument len is -1, this value is ignored.
*
*/
typedef int32_t (* esp_a2d_source_data_cb_t)(uint8_t *buf, int32_t len);
/**
* @brief Register application callback function to A2DP module. This function should be called
* only after esp_bluedroid_enable() completes successfully, used by both A2DP source
* and sink.
*
* @param[in] callback: A2DP event callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_a2d_register_callback(esp_a2d_cb_t callback);
/**
* @brief Register A2DP sink data output function; For now the output is PCM data stream decoded
* from SBC format. This function should be called only after esp_bluedroid_enable()
* completes successfully, used only by A2DP sink. The callback is invoked in the context
* of A2DP sink task whose stack size is configurable through menuconfig.
*
* @param[in] callback: A2DP sink data callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_a2d_sink_register_data_callback(esp_a2d_sink_data_cb_t callback);
/**
*
* @brief Initialize the bluetooth A2DP sink module. This function should be called
* after esp_bluedroid_enable() completes successfully, and ESP_A2D_PROF_STATE_EVT
* with ESP_A2D_INIT_SUCCESS will reported to the APP layer. Note: A2DP can work independently.
* If you want to use AVRC together, you should initiate AVRC first. This
* function should be called after esp_bluedroid_enable() completes successfully.
*
* @return
* - ESP_OK: if the initialization request is sent successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_sink_init(void);
/**
*
* @brief De-initialize for A2DP sink module. This function
* should be called only after esp_bluedroid_enable() completes successfully,
* and ESP_A2D_PROF_STATE_EVT with ESP_A2D_DEINIT_SUCCESS will reported to APP layer.
*
* @return
* - ESP_OK: if the deinitialization request is sent successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_sink_deinit(void);
/**
*
* @brief Connect to remote bluetooth A2DP source device. This API must be called after
* esp_a2d_sink_init() and before esp_a2d_sink_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: connect request is sent to lower layer successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_sink_connect(esp_bd_addr_t remote_bda);
/**
*
* @brief Disconnect from the remote A2DP source device. This API must be called after
* esp_a2d_sink_init() and before esp_a2d_sink_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: disconnect request is sent to lower layer successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_sink_disconnect(esp_bd_addr_t remote_bda);
/**
*
* @brief Media control commands. This API can be used for both A2DP sink and source
* and must be called after esp_a2d_sink_init() and before esp_a2d_sink_deinit().
*
* @param[in] ctrl: control commands for A2DP data channel
*
* @return
* - ESP_OK: control command is sent to lower layer successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_media_ctrl(esp_a2d_media_ctrl_t ctrl);
/**
*
* @brief Initialize the bluetooth A2DP source module. A2DP can work independently.
* If you want to use AVRC together, you should initiate AVRC first. This function should be called
* after esp_bluedroid_enable() completes successfully, and ESP_A2D_PROF_STATE_EVT
* with ESP_A2D_INIT_SUCCESS will reported to the APP layer. Note: A2DP can work independently.
* If you want to use AVRC together, you should initiate AVRC first.
*
* @return
* - ESP_OK: if the initialization request is sent to lower layer successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_source_init(void);
/**
*
* @brief De-initialize for A2DP source module. This function
* should be called only after esp_bluedroid_enable() completes successfully,
* and ESP_A2D_PROF_STATE_EVT with ESP_A2D_DEINIT_SUCCESS will reported to APP layer.
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_source_deinit(void);
/**
* @brief Register A2DP source data input function. For now, the input shoule be PCM data stream.
* This function should be called only after esp_bluedroid_enable() completes
* successfully. The callback is invoked in the context of A2DP source task whose
* stack size is configurable through menuconfig.
*
* @param[in] callback: A2DP source data callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_a2d_source_register_data_callback(esp_a2d_source_data_cb_t callback);
/**
*
* @brief Connect to remote A2DP sink device. This API must be called
* after esp_a2d_source_init() and before esp_a2d_source_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: connect request is sent to lower layer successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_source_connect(esp_bd_addr_t remote_bda);
/**
*
* @brief Disconnect from the remote A2DP sink device. This API must be called
* after esp_a2d_source_init() and before esp_a2d_source_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_a2d_source_disconnect(esp_bd_addr_t remote_bda);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_A2DP_API_H__ */

View File

@ -0,0 +1,743 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_AVRC_API_H__
#define __ESP_AVRC_API_H__
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_AVRC_TRANS_LABEL_MAX 15 /*!< max transaction label */
/// AVRC feature bit mask
typedef enum {
ESP_AVRC_FEAT_RCTG = 0x0001, /*!< remote control target */
ESP_AVRC_FEAT_RCCT = 0x0002, /*!< remote control controller */
ESP_AVRC_FEAT_VENDOR = 0x0008, /*!< remote control vendor dependent commands */
ESP_AVRC_FEAT_BROWSE = 0x0010, /*!< use browsing channel */
ESP_AVRC_FEAT_META_DATA = 0x0040, /*!< remote control metadata transfer command/response */
ESP_AVRC_FEAT_ADV_CTRL = 0x0200, /*!< remote control advanced control commmand/response */
} esp_avrc_features_t;
/// AVRC supported features flag retrieved in SDP record
typedef enum {
ESP_AVRC_FEAT_FLAG_CAT1 = 0x0001, /*!< category 1 */
ESP_AVRC_FEAT_FLAG_CAT2 = 0x0002, /*!< category 2 */
ESP_AVRC_FEAT_FLAG_CAT3 = 0x0004, /*!< category 3 */
ESP_AVRC_FEAT_FLAG_CAT4 = 0x0008, /*!< category 4 */
ESP_AVRC_FEAT_FLAG_BROWSING = 0x0040, /*!< browsing */
ESP_AVRC_FEAT_FLAG_COVER_ART_GET_IMAGE_PROP = 0x0080, /*!< Cover Art GetImageProperties */
ESP_AVRC_FEAT_FLAG_COVER_ART_GET_IMAGE = 0x0100, /*!< Cover Art GetImage */
ESP_AVRC_FEAT_FLAG_COVER_ART_GET_LINKED_THUMBNAIL = 0x0200, /*!< Cover Art GetLinkedThumbnail */
} esp_avrc_feature_flag_t;
/// AVRC passthrough command code
typedef enum {
ESP_AVRC_PT_CMD_SELECT = 0x00, /*!< select */
ESP_AVRC_PT_CMD_UP = 0x01, /*!< up */
ESP_AVRC_PT_CMD_DOWN = 0x02, /*!< down */
ESP_AVRC_PT_CMD_LEFT = 0x03, /*!< left */
ESP_AVRC_PT_CMD_RIGHT = 0x04, /*!< right */
ESP_AVRC_PT_CMD_RIGHT_UP = 0x05, /*!< right-up */
ESP_AVRC_PT_CMD_RIGHT_DOWN = 0x06, /*!< right-down */
ESP_AVRC_PT_CMD_LEFT_UP = 0x07, /*!< left-up */
ESP_AVRC_PT_CMD_LEFT_DOWN = 0x08, /*!< left-down */
ESP_AVRC_PT_CMD_ROOT_MENU = 0x09, /*!< root menu */
ESP_AVRC_PT_CMD_SETUP_MENU = 0x0A, /*!< setup menu */
ESP_AVRC_PT_CMD_CONT_MENU = 0x0B, /*!< contents menu */
ESP_AVRC_PT_CMD_FAV_MENU = 0x0C, /*!< favorite menu */
ESP_AVRC_PT_CMD_EXIT = 0x0D, /*!< exit */
ESP_AVRC_PT_CMD_0 = 0x20, /*!< 0 */
ESP_AVRC_PT_CMD_1 = 0x21, /*!< 1 */
ESP_AVRC_PT_CMD_2 = 0x22, /*!< 2 */
ESP_AVRC_PT_CMD_3 = 0x23, /*!< 3 */
ESP_AVRC_PT_CMD_4 = 0x24, /*!< 4 */
ESP_AVRC_PT_CMD_5 = 0x25, /*!< 5 */
ESP_AVRC_PT_CMD_6 = 0x26, /*!< 6 */
ESP_AVRC_PT_CMD_7 = 0x27, /*!< 7 */
ESP_AVRC_PT_CMD_8 = 0x28, /*!< 8 */
ESP_AVRC_PT_CMD_9 = 0x29, /*!< 9 */
ESP_AVRC_PT_CMD_DOT = 0x2A, /*!< dot */
ESP_AVRC_PT_CMD_ENTER = 0x2B, /*!< enter */
ESP_AVRC_PT_CMD_CLEAR = 0x2C, /*!< clear */
ESP_AVRC_PT_CMD_CHAN_UP = 0x30, /*!< channel up */
ESP_AVRC_PT_CMD_CHAN_DOWN = 0x31, /*!< channel down */
ESP_AVRC_PT_CMD_PREV_CHAN = 0x32, /*!< previous channel */
ESP_AVRC_PT_CMD_SOUND_SEL = 0x33, /*!< sound select */
ESP_AVRC_PT_CMD_INPUT_SEL = 0x34, /*!< input select */
ESP_AVRC_PT_CMD_DISP_INFO = 0x35, /*!< display information */
ESP_AVRC_PT_CMD_HELP = 0x36, /*!< help */
ESP_AVRC_PT_CMD_PAGE_UP = 0x37, /*!< page up */
ESP_AVRC_PT_CMD_PAGE_DOWN = 0x38, /*!< page down */
ESP_AVRC_PT_CMD_POWER = 0x40, /*!< power */
ESP_AVRC_PT_CMD_VOL_UP = 0x41, /*!< volume up */
ESP_AVRC_PT_CMD_VOL_DOWN = 0x42, /*!< volume down */
ESP_AVRC_PT_CMD_MUTE = 0x43, /*!< mute */
ESP_AVRC_PT_CMD_PLAY = 0x44, /*!< play */
ESP_AVRC_PT_CMD_STOP = 0x45, /*!< stop */
ESP_AVRC_PT_CMD_PAUSE = 0x46, /*!< pause */
ESP_AVRC_PT_CMD_RECORD = 0x47, /*!< record */
ESP_AVRC_PT_CMD_REWIND = 0x48, /*!< rewind */
ESP_AVRC_PT_CMD_FAST_FORWARD = 0x49, /*!< fast forward */
ESP_AVRC_PT_CMD_EJECT = 0x4A, /*!< eject */
ESP_AVRC_PT_CMD_FORWARD = 0x4B, /*!< forward */
ESP_AVRC_PT_CMD_BACKWARD = 0x4C, /*!< backward */
ESP_AVRC_PT_CMD_ANGLE = 0x50, /*!< angle */
ESP_AVRC_PT_CMD_SUBPICT = 0x51, /*!< subpicture */
ESP_AVRC_PT_CMD_F1 = 0x71, /*!< F1 */
ESP_AVRC_PT_CMD_F2 = 0x72, /*!< F2 */
ESP_AVRC_PT_CMD_F3 = 0x73, /*!< F3 */
ESP_AVRC_PT_CMD_F4 = 0x74, /*!< F4 */
ESP_AVRC_PT_CMD_F5 = 0x75, /*!< F5 */
ESP_AVRC_PT_CMD_VENDOR = 0x7E, /*!< vendor unique */
} esp_avrc_pt_cmd_t;
/// AVRC passthrough command filter
typedef enum {
ESP_AVRC_PSTH_FILTER_ALLOWED_CMD = 0, /*!< all of the PASSTHROUGH commands that can possibly be used, immuateble */
ESP_AVRC_PSTH_FILTER_SUPPORTED_CMD = 1, /*!< PASSTHROUGH commands selectively supported according to the current configuration */
ESP_AVRC_PSTH_FILTER_SUPPORT_MAX,
} esp_avrc_psth_filter_t;
/// AVRC passthrough command bit mask
typedef struct {
uint16_t bits[8]; /*!< bit mask representation of PASSTHROUGH commands */
} esp_avrc_psth_bit_mask_t;
typedef enum {
ESP_AVRC_BIT_MASK_OP_TEST = 0, /*!< operation code to test a specific bit */
ESP_AVRC_BIT_MASK_OP_SET = 1, /*!< operation code to set a specific bit */
ESP_AVRC_BIT_MASK_OP_CLEAR = 2, /*!< operation code to clear a specific bit */
} esp_avrc_bit_mask_op_t;
/// AVRC passthrough command state
typedef enum {
ESP_AVRC_PT_CMD_STATE_PRESSED = 0, /*!< key pressed */
ESP_AVRC_PT_CMD_STATE_RELEASED = 1 /*!< key released */
} esp_avrc_pt_cmd_state_t;
/// AVRC Controller callback events
typedef enum {
ESP_AVRC_CT_CONNECTION_STATE_EVT = 0, /*!< connection state changed event */
ESP_AVRC_CT_PASSTHROUGH_RSP_EVT = 1, /*!< passthrough response event */
ESP_AVRC_CT_METADATA_RSP_EVT = 2, /*!< metadata response event */
ESP_AVRC_CT_PLAY_STATUS_RSP_EVT = 3, /*!< play status response event */
ESP_AVRC_CT_CHANGE_NOTIFY_EVT = 4, /*!< notification event */
ESP_AVRC_CT_REMOTE_FEATURES_EVT = 5, /*!< feature of remote device indication event */
ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT = 6, /*!< supported notification events capability of peer device */
ESP_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT = 7, /*!< set absolute volume response event */
} esp_avrc_ct_cb_event_t;
/// AVRC Target callback events
typedef enum {
ESP_AVRC_TG_CONNECTION_STATE_EVT = 0, /*!< connection state changed event */
ESP_AVRC_TG_REMOTE_FEATURES_EVT = 1, /*!< feature of remote device indication event */
ESP_AVRC_TG_PASSTHROUGH_CMD_EVT = 2, /*!< passthrough command event */
ESP_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT = 3, /*!< set absolute volume command from remote device */
ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT = 4, /*!< register notification event */
ESP_AVRC_TG_SET_PLAYER_APP_VALUE_EVT = 5, /*!< set applicaton attribute value, attribute refer to esp_avrc_ps_attr_ids_t */
} esp_avrc_tg_cb_event_t;
/// AVRC metadata attribute mask
typedef enum {
ESP_AVRC_MD_ATTR_TITLE = 0x1, /*!< title of the playing track */
ESP_AVRC_MD_ATTR_ARTIST = 0x2, /*!< track artist */
ESP_AVRC_MD_ATTR_ALBUM = 0x4, /*!< album name */
ESP_AVRC_MD_ATTR_TRACK_NUM = 0x8, /*!< track position on the album */
ESP_AVRC_MD_ATTR_NUM_TRACKS = 0x10, /*!< number of tracks on the album */
ESP_AVRC_MD_ATTR_GENRE = 0x20, /*!< track genre */
ESP_AVRC_MD_ATTR_PLAYING_TIME = 0x40 /*!< total album playing time in miliseconds */
} esp_avrc_md_attr_mask_t;
/// AVRC event notification ids
typedef enum {
ESP_AVRC_RN_PLAY_STATUS_CHANGE = 0x01, /*!< track status change, eg. from playing to paused */
ESP_AVRC_RN_TRACK_CHANGE = 0x02, /*!< new track is loaded */
ESP_AVRC_RN_TRACK_REACHED_END = 0x03, /*!< current track reached end */
ESP_AVRC_RN_TRACK_REACHED_START = 0x04, /*!< current track reached start position */
ESP_AVRC_RN_PLAY_POS_CHANGED = 0x05, /*!< track playing position changed */
ESP_AVRC_RN_BATTERY_STATUS_CHANGE = 0x06, /*!< battery status changed */
ESP_AVRC_RN_SYSTEM_STATUS_CHANGE = 0x07, /*!< system status changed */
ESP_AVRC_RN_APP_SETTING_CHANGE = 0x08, /*!< application settings changed */
ESP_AVRC_RN_NOW_PLAYING_CHANGE = 0x09, /*!< now playing content changed */
ESP_AVRC_RN_AVAILABLE_PLAYERS_CHANGE = 0x0a, /*!< available players changed */
ESP_AVRC_RN_ADDRESSED_PLAYER_CHANGE = 0x0b, /*!< the addressed player changed */
ESP_AVRC_RN_UIDS_CHANGE = 0x0c, /*!< UIDs changed */
ESP_AVRC_RN_VOLUME_CHANGE = 0x0d, /*!< volume changed locally on TG */
ESP_AVRC_RN_MAX_EVT
} esp_avrc_rn_event_ids_t;
/// AVRC target notification event notification capability
typedef enum {
ESP_AVRC_RN_CAP_ALLOWED_EVT = 0, /*!< all of the notification events that can possibly be supported, immutable */
ESP_AVRC_RN_CAP_SUPPORTED_EVT = 1, /*!< notification events selectively supported according to the current configuration */
ESP_AVRC_RN_CAP_MAX,
} esp_avrc_rn_evt_cap_t;
/// AVRC target notification event capability bit mask
typedef struct {
uint16_t bits; /*!< bit mask representation of PASSTHROUGH commands */
} esp_avrc_rn_evt_cap_mask_t;
/// AVRC notification response type
typedef enum {
ESP_AVRC_RN_RSP_INTERIM = 13, /*!< initial response to RegisterNotification, should be sent T_mtp(1000ms) from receiving the command */
ESP_AVRC_RN_RSP_CHANGED = 15, /*!< final response to RegisterNotification command */
} esp_avrc_rn_rsp_t;
/// AVRC player setting ids
typedef enum {
ESP_AVRC_PS_EQUALIZER = 0x01, /*!< equalizer, on or off */
ESP_AVRC_PS_REPEAT_MODE = 0x02, /*!< repeat mode */
ESP_AVRC_PS_SHUFFLE_MODE = 0x03, /*!< shuffle mode */
ESP_AVRC_PS_SCAN_MODE = 0x04, /*!< scan mode on or off */
ESP_AVRC_PS_MAX_ATTR
} esp_avrc_ps_attr_ids_t;
/// AVRC equalizer modes
typedef enum {
ESP_AVRC_PS_EQUALIZER_OFF = 0x1, /*!< equalizer OFF */
ESP_AVRC_PS_EQUALIZER_ON = 0x2 /*!< equalizer ON */
} esp_avrc_ps_eq_value_ids_t;
/// AVRC repeat modes
typedef enum {
ESP_AVRC_PS_REPEAT_OFF = 0x1, /*!< repeat mode off */
ESP_AVRC_PS_REPEAT_SINGLE = 0x2, /*!< single track repeat */
ESP_AVRC_PS_REPEAT_GROUP = 0x3 /*!< group repeat */
} esp_avrc_ps_rpt_value_ids_t;
/// AVRC shuffle modes
typedef enum {
ESP_AVRC_PS_SHUFFLE_OFF = 0x1, /*<! shuffle off */
ESP_AVRC_PS_SHUFFLE_ALL = 0x2, /*<! shuffle all tracks */
ESP_AVRC_PS_SHUFFLE_GROUP = 0x3 /*<! group shuffle */
} esp_avrc_ps_shf_value_ids_t;
/// AVRC scan modes
typedef enum {
ESP_AVRC_PS_SCAN_OFF = 0x1, /*!< scan off */
ESP_AVRC_PS_SCAN_ALL = 0x2, /*!< all tracks scan */
ESP_AVRC_PS_SCAN_GROUP = 0x3 /*!< group scan */
} esp_avrc_ps_scn_value_ids_t;
/// AVCTP response codes
typedef enum {
ESP_AVRC_RSP_NOT_IMPL = 8, /*!< not implemented */
ESP_AVRC_RSP_ACCEPT = 9, /*!< accept */
ESP_AVRC_RSP_REJECT = 10, /*!< reject */
ESP_AVRC_RSP_IN_TRANS = 11, /*!< in transition */
ESP_AVRC_RSP_IMPL_STBL = 12, /*!< implemented/stable */
ESP_AVRC_RSP_CHANGED = 13, /*!< changed */
ESP_AVRC_RSP_INTERIM = 15, /*!< interim */
} esp_avrc_rsp_t;
/// AVRCP battery status
typedef enum {
ESP_AVRC_BATT_NORMAL = 0, /*!< normal state */
ESP_AVRC_BATT_WARNING = 1, /*!< unable to operate soon */
ESP_AVRC_BATT_CRITICAL = 2, /*!< cannot operate any more */
ESP_AVRC_BATT_EXTERNAL = 3, /*!< plugged to external power supply */
ESP_AVRC_BATT_FULL_CHARGE = 4, /*!< when completely charged from external power supply */
} esp_avrc_batt_stat_t;
/// AVRCP current status of playback
typedef enum {
ESP_AVRC_PLAYBACK_STOPPED = 0, /*!< stopped */
ESP_AVRC_PLAYBACK_PLAYING = 1, /*!< playing */
ESP_AVRC_PLAYBACK_PAUSED = 2, /*!< paused */
ESP_AVRC_PLAYBACK_FWD_SEEK = 3, /*!< forward seek */
ESP_AVRC_PLAYBACK_REV_SEEK = 4, /*!< reverse seek */
ESP_AVRC_PLAYBACK_ERROR = 0xFF, /*!< error */
} esp_avrc_playback_stat_t;
/// AVRCP notification parameters
typedef union
{
uint8_t volume; /*!< response data for ESP_AVRC_RN_VOLUME_CHANGE, ranges 0..127 */
esp_avrc_playback_stat_t playback; /*!< response data for ESP_AVRC_RN_PLAY_STATUS_CHANGE */
uint8_t elm_id[8]; /*!< response data for ESP_AVRC_RN_TRACK_CHANGE */
uint32_t play_pos; /*!< response data for ESP_AVRC_RN_PLAY_POS_CHANGED, in millisecond */
esp_avrc_batt_stat_t batt; /*!< response data for ESP_AVRC_RN_BATTERY_STATUS_CHANGE */
} esp_avrc_rn_param_t;
/// AVRCP set app value parameters
typedef struct {
uint8_t attr_id; /*!< player application attribute id */
uint8_t attr_val; /*!< player application attribute value */
} esp_avrc_set_app_value_param_t;
/// AVRC controller callback parameters
typedef union {
/**
* @brief ESP_AVRC_CT_CONNECTION_STATE_EVT
*/
struct avrc_ct_conn_stat_param {
bool connected; /*!< whether AVRC connection is set up */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} conn_stat; /*!< AVRC connection status */
/**
* @brief ESP_AVRC_CT_PASSTHROUGH_RSP_EVT
*/
struct avrc_ct_psth_rsp_param {
uint8_t tl; /*!< transaction label, 0 to 15 */
uint8_t key_code; /*!< passthrough command code */
uint8_t key_state; /*!< 0 for PRESSED, 1 for RELEASED */
} psth_rsp; /*!< passthrough command response */
/**
* @brief ESP_AVRC_CT_METADATA_RSP_EVT
*/
struct avrc_ct_meta_rsp_param {
uint8_t attr_id; /*!< id of metadata attribute */
uint8_t *attr_text; /*!< attribute itself */
int attr_length; /*!< attribute character length */
} meta_rsp; /*!< metadata attributes response */
/**
* @brief ESP_AVRC_CT_CHANGE_NOTIFY_EVT
*/
struct avrc_ct_change_notify_param {
uint8_t event_id; /*!< id of AVRC event notification */
esp_avrc_rn_param_t event_parameter; /*!< event notification parameter */
} change_ntf; /*!< notifications */
/**
* @brief ESP_AVRC_CT_REMOTE_FEATURES_EVT
*/
struct avrc_ct_rmt_feats_param {
uint32_t feat_mask; /*!< AVRC feature mask of remote device */
uint16_t tg_feat_flag; /*!< feature flag of remote device as TG */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} rmt_feats; /*!< AVRC features discovered from remote SDP server */
/**
* @brief ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT
*/
struct avrc_ct_get_rn_caps_rsp_param {
uint8_t cap_count; /*!< number of items provided in event or company_id according to cap_id used */
esp_avrc_rn_evt_cap_mask_t evt_set; /*!< supported event_ids represented in bit-mask */
} get_rn_caps_rsp; /*!< get supported event capabilities response from AVRCP target */
/**
* @brief ESP_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT
*/
struct avrc_ct_set_volume_rsp_param {
uint8_t volume; /*!< the volume which has actually been set, range is 0 to 0x7f, means 0% to 100% */
} set_volume_rsp; /*!< set absolute volume response event */
} esp_avrc_ct_cb_param_t;
/// AVRC target callback parameters
typedef union {
/**
* @brief ESP_AVRC_TG_CONNECTION_STATE_EVT
*/
struct avrc_tg_conn_stat_param {
bool connected; /*!< whether AVRC connection is set up */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} conn_stat; /*!< AVRC connection status */
/**
* @brief ESP_AVRC_TG_REMOTE_FEATURES_EVT
*/
struct avrc_tg_rmt_feats_param {
uint32_t feat_mask; /*!< AVRC feature mask of remote device */
uint16_t ct_feat_flag; /*!< feature flag of remote device as CT */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} rmt_feats; /*!< AVRC features discovered through SDP */
/**
* @brief ESP_AVRC_TG_PASSTHROUGH_CMD_EVT
*/
struct avrc_tg_psth_cmd_param {
uint8_t key_code; /*!< passthrough command code */
uint8_t key_state; /*!< 0 for PRESSED, 1 for RELEASED */
} psth_cmd; /*!< passthrough command */
/**
* @brief ESP_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT
*/
struct avrc_tg_set_abs_vol_param {
uint8_t volume; /*!< volume ranges from 0 to 127 */
} set_abs_vol; /*!< set absolute volume command targeted on audio sink */
/**
* @brief ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT
*/
struct avrc_tg_reg_ntf_param {
uint8_t event_id; /*!< event id of AVRC RegisterNotification */
uint32_t event_parameter; /*!< event notification parameter */
} reg_ntf; /*!< register notification */
/**
* @brief ESP_AVRC_TG_SET_PLAYER_APP_VALUE_EVT
*/
struct avrc_tg_set_app_value_param {
uint8_t num_val; /*!< attribute num */
esp_avrc_set_app_value_param_t *p_vals; /*!< point to the id and value of player application attribute */
} set_app_value; /*!< set player application value */
} esp_avrc_tg_cb_param_t;
/**
* @brief AVRCP controller callback function type
*
* @param event : Event type
*
* @param param : Pointer to callback parameter union
*/
typedef void (* esp_avrc_ct_cb_t)(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
/**
* @brief AVRCP target callback function type
*
* @param event : Event type
*
* @param param : Pointer to callback parameter union
*/
typedef void (* esp_avrc_tg_cb_t)(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
/**
* @brief Register application callbacks to AVRCP module. This function should be
* called after esp_bluedroid_enable() completes successfully.
*
* @param[in] callback: AVRCP controller callback function
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_avrc_ct_register_callback(esp_avrc_ct_cb_t callback);
/**
*
* @brief Initialize the bluetooth AVRCP controller module, This function should be called
* after esp_bluedroid_enable() completes successfully. Note: AVRC cannot work independently,
* AVRC should be used along with A2DP and AVRC should be initialized before A2DP.
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_avrc_ct_init(void);
/**
*
* @brief De-initialize AVRCP controller module. This function should be called after
* after esp_bluedroid_enable() completes successfully. Note: AVRC cannot work independently,
* AVRC should be used along with A2DP and AVRC should be deinitialized before A2DP.
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_deinit(void);
/**
*
* @brief Send player application settings command to AVRCP target. This function should be called
* after ESP_AVRC_CT_CONNECTION_STATE_EVT is received and AVRCP connection is established.
*
* @param[in] tl : transaction label, 0 to 15, consecutive commands should use different values
*
* @param[in] attr_id : player application setting attribute IDs from one of esp_avrc_ps_attr_ids_t
*
* @param[in] value_id : attribute value defined for the specific player application setting attribute
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_send_set_player_value_cmd(uint8_t tl, uint8_t attr_id, uint8_t value_id);
/**
* @brief Send GetCapabilities PDU to AVRCP target to retrieve remote device's supported
* notification event_ids. This function should be called after
* ESP_AVRC_CT_CONNECTION_STATE_EVT is received and AVRCP connection is established.
*
* @param[in] tl : transaction label, 0 to 15, consecutive commands should use different values
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_send_get_rn_capabilities_cmd(uint8_t tl);
/**
* @brief Send register notification command to AVRCP target. This function should be called after
* ESP_AVRC_CT_CONNECTION_STATE_EVT is received and AVRCP connection is established.
*
* @param[in] tl : transaction label, 0 to 15, consecutive commands should use different values.
*
* @param[in] event_id : id of events, e.g. ESP_AVRC_RN_PLAY_STATUS_CHANGE, ESP_AVRC_RN_TRACK_CHANGE, etc.
*
* @param[in] event_parameter : playback interval for ESP_AVRC_RN_PLAY_POS_CHANGED;
* For other events , value of this parameter is ignored.
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_NOT_SUPPORTED: if the event_id is not supported in current implementation
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_send_register_notification_cmd(uint8_t tl, uint8_t event_id, uint32_t event_parameter);
/**
* @brief Send set absolute volume command to AVRCP target. This function should be called after
* ESP_AVRC_CT_CONNECTION_STATE_EVT is received and AVRCP connection is established.
*
* @param[in] tl : transaction label, 0 to 15, consecutive commands should use different values
*
* @param[in] volume : volume, 0 to 0x7f, means 0% to 100%
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_NOT_SUPPORTED: if the event_id is not supported in current implementation
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_send_set_absolute_volume_cmd(uint8_t tl, uint8_t volume);
/**
* @brief Send metadata command to AVRCP target. This function should be called after
* ESP_AVRC_CT_CONNECTION_STATE_EVT is received and AVRCP connection is established.
*
* @param[in] tl : transaction label, 0 to 15, consecutive commands should use different values.
*
* @param[in] attr_mask : mask of attributes, e.g. ESP_AVRC_MD_ATTR_ID_TITLE | ESP_AVRC_MD_ATTR_ID_ARTIST.
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_send_metadata_cmd(uint8_t tl, uint8_t attr_mask);
/**
* @brief Send passthrough command to AVRCP target. This function should be called after
* ESP_AVRC_CT_CONNECTION_STATE_EVT is received and AVRCP connection is established.
*
* @param[in] tl : transaction label, 0 to 15, consecutive commands should use different values.
*
* @param[in] key_code : passthrough command code, e.g. ESP_AVRC_PT_CMD_PLAY, ESP_AVRC_PT_CMD_STOP, etc.
*
* @param[in] key_state : passthrough command key state, ESP_AVRC_PT_CMD_STATE_PRESSED or
* ESP_AVRC_PT_CMD_STATE_RELEASED
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_ct_send_passthrough_cmd(uint8_t tl, uint8_t key_code, uint8_t key_state);
/**
* @brief Register application callbacks to AVRCP target module. This function should be
* called after esp_bluedroid_enable() completes successfully.
*
* @param[in] callback: AVRCP target callback function
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_avrc_tg_register_callback(esp_avrc_tg_cb_t callback);
/**
*
* @brief Initialize the bluetooth AVRCP target module, This function should be called
* after esp_bluedroid_enable() completes successfully. Note: AVRC cannot work independently,
* AVRC should be used along with A2DP and AVRC should be initialized before A2DP.
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_avrc_tg_init(void);
/**
*
* @brief De-initialize AVRCP target module. This function should be called after
* after esp_bluedroid_enable() completes successfully. Note: AVRC cannot work independently,
* AVRC should be used along with A2DP and AVRC should be deinitialized before A2DP.
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_avrc_tg_deinit(void);
/**
*
* @brief Get the current filter of remote passthrough commands on AVRC target. Filter is given by
* filter type and bit mask for the passthrough commands. This function should be called
* after esp_avrc_tg_init().
* For filter type ESP_AVRC_PSTH_FILTER_ALLOWED_CMD, the retrieved command set is constant and
* it covers all of the passthrough commands that can possibly be supported.
* For filter type ESP_AVRC_PSTH_FILTER_SUPPORT_COMMANDS, the retrieved command set covers the
* passthrough commands selected to be supported according to current configuration. The
* configuration can be changed using esp_avrc_tg_set_psth_cmd_filter().
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not enabled or AVRC TG is not initialized
* - ESP_ERR_INVALID_ARG: if filter type is invalid or cmd_set is NULL
* - ESP_FAIL: otherwise
*/
esp_err_t esp_avrc_tg_get_psth_cmd_filter(esp_avrc_psth_filter_t filter, esp_avrc_psth_bit_mask_t *cmd_set);
/**
*
* @brief Set the filter of remote passthrough commands on AVRC target. Filter is given by
* filter type and bit mask for the passthrough commands. This function should be called
* after esp_avrc_tg_init().
*
* If filter type is ESP_AVRC_PSTH_FILTER_SUPPORT_CMD, the passthrough commands which
* are set "1" as given in cmd_set will generate ESP_AVRC_CT_PASSTHROUGH_RSP_EVT callback
* event and are auto-accepted in the protocol stack, other commands are replied with response
* type "NOT IMPLEMENTED" (8). The set of supported commands should be a subset of allowed
* command set. The allowed command set can be retrieved using esp_avrc_tg_get_psth_cmd_filter()
* with filter type "ESP_AVRC_PSTH_FILTER_ALLOWED_CMD".
*
* Filter type "ESP_AVRC_PSTH_FILTER_ALLOWED_CMD" does not apply to this function.
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not enabled
* - ESP_ERR_INVALID_ARG: if filter type is invalid or cmd_set is NULL
* - ESP_ERR_NOT_SUPPORTED:: if filter type is ESP_AVRC_PSTH_FILTER_ALLOWED_CMD, or cmd_set
* includes unallowed commands
*
*/
esp_err_t esp_avrc_tg_set_psth_cmd_filter(esp_avrc_psth_filter_t filter, const esp_avrc_psth_bit_mask_t *cmd_set);
/**
* @brief Operate on the type esp_avrc_psth_bit_mask_t with regard to a specific PASSTHROUGH command.
*
* @param[in] op: operation requested on the bit mask field
*
* @param[in] psth: pointer to passthrough command bit mask structure
*
* @param[in] cmd: passthrough command code
*
* @return For operation ESP_AVRC_BIT_MASK_OP_SET or ESP_AVRC_BIT_MASK_OP_CLEAR, return
* true for a successful operation, otherwise return false.
* For operation ESP_AVRC_BIT_MASK_OP_TEST, return true if the corresponding bit
* is set, otherwise false.
*
*/
bool esp_avrc_psth_bit_mask_operation(esp_avrc_bit_mask_op_t op, esp_avrc_psth_bit_mask_t *psth, esp_avrc_pt_cmd_t cmd);
/**
*
* @brief Get the requested event notification capabilies on local AVRC target. The capability is returned
* in a bit mask representation in evt_set. This function should be called after esp_avrc_tg_init().
*
* For capability type "ESP_AVRC_RN_CAP_ALLOWED_EVT, the retrieved event set is constant and
* it covers all of the notifcation events that can possibly be supported with current
* implementation.
*
* For capability type ESP_AVRC_RN_CAP_SUPPORTED_EVT, the event set covers the notification
* events selected to be supported under current configuration, The configuration can be
* changed using esp_avrc_tg_set_rn_evt_cap().
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not enabled or AVRC TG is not initialized
* - ESP_ERR_INVALID_ARG: if cap is invalid or evt_set is NULL
* - ESP_FAIL: otherwise
*/
esp_err_t esp_avrc_tg_get_rn_evt_cap(esp_avrc_rn_evt_cap_t cap, esp_avrc_rn_evt_cap_mask_t *evt_set);
/**
*
* @brief Set the event notification capabilities on local AVRCP target. The capability is given in a
* bit mask representation in evt_set and must be a subset of allowed event IDs with current
* implementation. This function should be called after esp_avrc_tg_init().
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not enabled
* - ESP_ERR_INVALID_ARG: if evt_set is NULL
*
*/
esp_err_t esp_avrc_tg_set_rn_evt_cap(const esp_avrc_rn_evt_cap_mask_t *evt_set);
/**
* @brief Operate on the type esp_avrc_rn_evt_cap_mask_t with regard to a specific event.
*
* @param[in] op: operation requested on the bit mask field
*
* @param[in] events: pointer to event notification capability bit mask structure
*
* @param[in] event_id: notification event code
*
* @return For operation ESP_AVRC_BIT_MASK_OP_SET or ESP_AVRC_BIT_MASK_OP_CLEAR, return
* true for a successful operation, otherwise return false.
*
* For operation ESP_AVRC_BIT_MASK_OP_TEST, return true if the corresponding bit
* is set, otherwise false.
*
*/
bool esp_avrc_rn_evt_bit_mask_operation(esp_avrc_bit_mask_op_t op, esp_avrc_rn_evt_cap_mask_t *events,
esp_avrc_rn_event_ids_t event_id);
/**
*
* @brief Send RegisterNotification Response to remote AVRCP controller. Local event notification
* capability can be set using esp_avrc_tg_set_rn_evt_cap(), in a bit mask representation
* in evt_set. This function should be called after esp_avrc_tg_init().
*
* @param[in] event_id: notification event ID that remote AVRCP CT registers
*
* @param[in] rsp: notification response code
*
* @param[in] param: parameters included in the specific notification
*
* @return
* - ESP_OK: success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not enabled or AVRC TG is not initialized
* - ESP_ERR_INVALID_ARG: if evt_set is NULL
*
*/
esp_err_t esp_avrc_tg_send_rn_rsp(esp_avrc_rn_event_ids_t event_id, esp_avrc_rn_rsp_t rsp,
esp_avrc_rn_param_t *param);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_AVRC_API_H__ */

View File

@ -0,0 +1,442 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_BLUFI_API_H__
#define __ESP_BLUFI_API_H__
#include "esp_bt_defs.h"
#include "esp_gatt_defs.h"
#include "esp_err.h"
#include "esp_wifi_types.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
ESP_BLUFI_EVENT_INIT_FINISH = 0, /*<! When BLUFI init complete, this event happen */
ESP_BLUFI_EVENT_DEINIT_FINISH, /*<! When BLUFI deinit complete, this event happen */
ESP_BLUFI_EVENT_SET_WIFI_OPMODE, /*<! When Phone set ESP32 wifi operation mode(AP/STA/AP_STA), this event happen */
ESP_BLUFI_EVENT_BLE_CONNECT, /*<! When Phone connect to ESP32 with BLE, this event happen */
ESP_BLUFI_EVENT_BLE_DISCONNECT, /*<! When Phone disconnect with BLE, this event happen */
ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP, /*<! When Phone request ESP32's STA connect to AP, this event happen */
ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP, /*<! When Phone request ESP32's STA disconnect from AP, this event happen */
ESP_BLUFI_EVENT_GET_WIFI_STATUS, /*<! When Phone get ESP32 wifi status, this event happen */
ESP_BLUFI_EVENT_DEAUTHENTICATE_STA, /*<! When Phone deauthenticate sta from SOFTAP, this event happen */
/* recv data */
ESP_BLUFI_EVENT_RECV_STA_BSSID, /*<! When Phone send STA BSSID to ESP32 to connect, this event happen */
ESP_BLUFI_EVENT_RECV_STA_SSID, /*<! When Phone send STA SSID to ESP32 to connect, this event happen */
ESP_BLUFI_EVENT_RECV_STA_PASSWD, /*<! When Phone send STA PASSWORD to ESP32 to connect, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_SSID, /*<! When Phone send SOFTAP SSID to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD, /*<! When Phone send SOFTAP PASSWORD to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM, /*<! When Phone send SOFTAP max connection number to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE, /*<! When Phone send SOFTAP authentication mode to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL, /*<! When Phone send SOFTAP channel to ESP32 to start SOFTAP, this event happen */
ESP_BLUFI_EVENT_RECV_USERNAME, /*<! When Phone send username to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_CA_CERT, /*<! When Phone send CA certificate to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_CLIENT_CERT, /*<! When Phone send Client certificate to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_SERVER_CERT, /*<! When Phone send Server certificate to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY, /*<! When Phone send Client Private key to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY, /*<! When Phone send Server Private key to ESP32, this event happen */
ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE, /*<! When Phone send Disconnect key to ESP32, this event happen */
ESP_BLUFI_EVENT_GET_WIFI_LIST, /*<! When Phone send get wifi list command to ESP32, this event happen */
ESP_BLUFI_EVENT_REPORT_ERROR, /*<! When Blufi report error, this event happen */
ESP_BLUFI_EVENT_RECV_CUSTOM_DATA, /*<! When Phone send custom data to ESP32, this event happen */
} esp_blufi_cb_event_t;
/// BLUFI config status
typedef enum {
ESP_BLUFI_STA_CONN_SUCCESS = 0x00,
ESP_BLUFI_STA_CONN_FAIL = 0x01,
} esp_blufi_sta_conn_state_t;
/// BLUFI init status
typedef enum {
ESP_BLUFI_INIT_OK = 0,
ESP_BLUFI_INIT_FAILED,
} esp_blufi_init_state_t;
/// BLUFI deinit status
typedef enum {
ESP_BLUFI_DEINIT_OK = 0,
ESP_BLUFI_DEINIT_FAILED,
} esp_blufi_deinit_state_t;
typedef enum {
ESP_BLUFI_SEQUENCE_ERROR = 0,
ESP_BLUFI_CHECKSUM_ERROR,
ESP_BLUFI_DECRYPT_ERROR,
ESP_BLUFI_ENCRYPT_ERROR,
ESP_BLUFI_INIT_SECURITY_ERROR,
ESP_BLUFI_DH_MALLOC_ERROR,
ESP_BLUFI_DH_PARAM_ERROR,
ESP_BLUFI_READ_PARAM_ERROR,
ESP_BLUFI_MAKE_PUBLIC_ERROR,
ESP_BLUFI_DATA_FORMAT_ERROR,
} esp_blufi_error_state_t;
/**
* @brief BLUFI extra information structure
*/
typedef struct {
//station
uint8_t sta_bssid[6]; /*!< BSSID of station interface */
bool sta_bssid_set; /*!< is BSSID of station interface set */
uint8_t *sta_ssid; /*!< SSID of station interface */
int sta_ssid_len; /*!< length of SSID of station interface */
uint8_t *sta_passwd; /*!< password of station interface */
int sta_passwd_len; /*!< length of password of station interface */
uint8_t *softap_ssid; /*!< SSID of softap interface */
int softap_ssid_len; /*!< length of SSID of softap interface */
uint8_t *softap_passwd; /*!< password of station interface */
int softap_passwd_len; /*!< length of password of station interface */
uint8_t softap_authmode; /*!< authentication mode of softap interface */
bool softap_authmode_set; /*!< is authentication mode of softap interface set */
uint8_t softap_max_conn_num; /*!< max connection number of softap interface */
bool softap_max_conn_num_set; /*!< is max connection number of softap interface set */
uint8_t softap_channel; /*!< channel of softap interface */
bool softap_channel_set; /*!< is channel of softap interface set */
} esp_blufi_extra_info_t;
/** @brief Description of an WiFi AP */
typedef struct {
uint8_t ssid[33]; /**< SSID of AP */
int8_t rssi; /**< signal strength of AP */
} esp_blufi_ap_record_t;
/**
* @brief BLUFI callback parameters union
*/
typedef union {
/**
* @brief ESP_BLUFI_EVENT_INIT_FINISH
*/
struct blufi_init_finish_evt_param {
esp_blufi_init_state_t state; /*!< Initial status */
} init_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
/**
* @brief ESP_BLUFI_EVENT_DEINIT_FINISH
*/
struct blufi_deinit_finish_evt_param {
esp_blufi_deinit_state_t state; /*!< De-initial status */
} deinit_finish; /*!< Blufi callback param of ESP_BLUFI_EVENT_DEINIT_FINISH */
/**
* @brief ESP_BLUFI_EVENT_SET_WIFI_MODE
*/
struct blufi_set_wifi_mode_evt_param {
wifi_mode_t op_mode; /*!< Wifi operation mode */
} wifi_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_INIT_FINISH */
/**
* @brief ESP_BLUFI_EVENT_CONNECT
*/
struct blufi_connect_evt_param {
esp_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
uint8_t server_if; /*!< server interface */
uint16_t conn_id; /*!< Connection id */
} connect; /*!< Blufi callback param of ESP_BLUFI_EVENT_CONNECT */
/**
* @brief ESP_BLUFI_EVENT_DISCONNECT
*/
struct blufi_disconnect_evt_param {
esp_bd_addr_t remote_bda; /*!< Blufi Remote bluetooth device address */
} disconnect; /*!< Blufi callback param of ESP_BLUFI_EVENT_DISCONNECT */
/* ESP_BLUFI_EVENT_REQ_WIFI_CONNECT */ /* No callback param */
/* ESP_BLUFI_EVENT_REQ_WIFI_DISCONNECT */ /* No callback param */
/**
* @brief ESP_BLUFI_EVENT_RECV_STA_BSSID
*/
struct blufi_recv_sta_bssid_evt_param {
uint8_t bssid[6]; /*!< BSSID */
} sta_bssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_BSSID */
/**
* @brief ESP_BLUFI_EVENT_RECV_STA_SSID
*/
struct blufi_recv_sta_ssid_evt_param {
uint8_t *ssid; /*!< SSID */
int ssid_len; /*!< SSID length */
} sta_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_SSID */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_STA_PASSWD
*/
struct blufi_recv_sta_passwd_evt_param {
uint8_t *passwd; /*!< Password */
int passwd_len; /*!< Password Length */
} sta_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_STA_PASSWD */
/**
* @brief ESP_BLUFI_EVENT_RECV_SOFTAP_SSID
*/
struct blufi_recv_softap_ssid_evt_param {
uint8_t *ssid; /*!< SSID */
int ssid_len; /*!< SSID length */
} softap_ssid; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_SSID */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD
*/
struct blufi_recv_softap_passwd_evt_param {
uint8_t *passwd; /*!< Password */
int passwd_len; /*!< Password Length */
} softap_passwd; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD */
/**
* @brief ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM
*/
struct blufi_recv_softap_max_conn_num_evt_param {
int max_conn_num; /*!< SSID */
} softap_max_conn_num; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE
*/
struct blufi_recv_softap_auth_mode_evt_param {
wifi_auth_mode_t auth_mode; /*!< Authentication mode */
} softap_auth_mode; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL
*/
struct blufi_recv_softap_channel_evt_param {
uint8_t channel; /*!< Authentication mode */
} softap_channel; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL */
/**
* @brief ESP_BLUFI_EVENT_RECV_USERNAME
*/
struct blufi_recv_username_evt_param {
uint8_t *name; /*!< Username point */
int name_len; /*!< Username length */
} username; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_USERNAME*/
/**
* @brief ESP_BLUFI_EVENT_RECV_CA_CERT
*/
struct blufi_recv_ca_evt_param {
uint8_t *cert; /*!< CA certificate point */
int cert_len; /*!< CA certificate length */
} ca; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CA_CERT */
/**
* ESP_BLUFI_EVENT_RECV_CLIENT_CERT
*/
struct blufi_recv_client_cert_evt_param {
uint8_t *cert; /*!< Client certificate point */
int cert_len; /*!< Client certificate length */
} client_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_CERT */
/**
* ESP_BLUFI_EVENT_RECV_SERVER_CERT
*/
struct blufi_recv_server_cert_evt_param {
uint8_t *cert; /*!< Client certificate point */
int cert_len; /*!< Client certificate length */
} server_cert; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_CERT */
/**
* ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY
*/
struct blufi_recv_client_pkey_evt_param {
uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
int pkey_len; /*!< Client Private key length */
} client_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY */
/**
* ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY
*/
struct blufi_recv_server_pkey_evt_param {
uint8_t *pkey; /*!< Client Private Key point, if Client certificate not contain Key */
int pkey_len; /*!< Client Private key length */
} server_pkey; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY */
/**
* @brief
* ESP_BLUFI_EVENT_REPORT_ERROR
*/
struct blufi_get_error_evt_param {
esp_blufi_error_state_t state; /*!< Blufi error state */
} report_error; /*!< Blufi callback param of ESP_BLUFI_EVENT_REPORT_ERROR */
/**
* @brief
* ESP_BLUFI_EVENT_RECV_CUSTOM_DATA
*/
struct blufi_recv_custom_data_evt_param {
uint8_t *data; /*!< Custom data */
uint32_t data_len; /*!< Custom data Length */
} custom_data; /*!< Blufi callback param of ESP_BLUFI_EVENT_RECV_CUSTOM_DATA */
} esp_blufi_cb_param_t;
/**
* @brief BLUFI event callback function type
* @param event : Event type
* @param param : Point to callback parameter, currently is union type
*/
typedef void (* esp_blufi_event_cb_t)(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
/* security function declare */
/**
* @brief BLUFI negotiate data handler
* @param data : data from phone
* @param len : length of data from phone
* @param output_data : data want to send to phone
* @param output_len : length of data want to send to phone
*/
typedef void (*esp_blufi_negotiate_data_handler_t)(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
/**
* @brief BLUFI encrypt the data after negotiate a share key
* @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
* @param crypt_data : plain text and encrypted data, the encrypt function must support autochthonous encrypt
* @param crypt_len : length of plain text
* @return Nonnegative number is encrypted length, if error, return negative number;
*/
typedef int (* esp_blufi_encrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int cyprt_len);
/**
* @brief BLUFI decrypt the data after negotiate a share key
* @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
* @param crypt_data : encrypted data and plain text, the encrypt function must support autochthonous decrypt
* @param crypt_len : length of encrypted text
* @return Nonnegative number is decrypted length, if error, return negative number;
*/
typedef int (* esp_blufi_decrypt_func_t)(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
/**
* @brief BLUFI checksum
* @param iv8 : initial vector(8bit), normally, blufi core will input packet sequence number
* @param data : data need to checksum
* @param len : length of data
*/
typedef uint16_t (*esp_blufi_checksum_func_t)(uint8_t iv8, uint8_t *data, int len);
/**
* @brief BLUFI callback functions type
*/
typedef struct {
esp_blufi_event_cb_t event_cb; /*!< BLUFI event callback */
esp_blufi_negotiate_data_handler_t negotiate_data_handler; /*!< BLUFI negotiate data function for negotiate share key */
esp_blufi_encrypt_func_t encrypt_func; /*!< BLUFI encrypt data function with share key generated by negotiate_data_handler */
esp_blufi_decrypt_func_t decrypt_func; /*!< BLUFI decrypt data function with share key generated by negotiate_data_handler */
esp_blufi_checksum_func_t checksum_func; /*!< BLUFI check sum function (FCS) */
} esp_blufi_callbacks_t;
/**
*
* @brief This function is called to receive blufi callback event
*
* @param[in] callbacks: callback functions
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_register_callbacks(esp_blufi_callbacks_t *callbacks);
/**
*
* @brief This function is called to initialize blufi_profile
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_profile_init(void);
/**
*
* @brief This function is called to de-initialize blufi_profile
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_profile_deinit(void);
/**
*
* @brief This function is called to send wifi connection report
* @param opmode : wifi opmode
* @param sta_conn_state : station is already in connection or not
* @param softap_conn_num : softap connection number
* @param extra_info : extra information, such as sta_ssid, softap_ssid and etc.
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_send_wifi_conn_report(wifi_mode_t opmode, esp_blufi_sta_conn_state_t sta_conn_state, uint8_t softap_conn_num, esp_blufi_extra_info_t *extra_info);
/**
*
* @brief This function is called to send wifi list
* @param apCount : wifi list count
* @param list : wifi list
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_send_wifi_list(uint16_t apCount, esp_blufi_ap_record_t *list);
/**
*
* @brief Get BLUFI profile version
*
* @return Most 8bit significant is Great version, Least 8bit is Sub version
*
*/
uint16_t esp_blufi_get_version(void);
/**
* @brief Close a connection a remote device.
*
* @param[in] gatts_if: GATT server access interface
* @param[in] conn_id: connection ID to be closed.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_blufi_close(esp_gatt_if_t gatts_if, uint16_t conn_id);
/**
*
* @brief This function is called to send blufi error information
* @param state : error state
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_send_error_info(esp_blufi_error_state_t state);
/**
*
* @brief This function is called to custom data
* @param data : custom data value
* @param data_len : the length of custom data
*
* @return ESP_OK - success, other - failed
*
*/
esp_err_t esp_blufi_send_custom_data(uint8_t *data, uint32_t data_len);
#ifdef __cplusplus
}
#endif
#endif /* _ESP_BLUFI_API_ */

View File

@ -0,0 +1,144 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_BT_DEFS_H__
#define __ESP_BT_DEFS_H__
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_BLUEDROID_STATUS_CHECK(status) \
if (esp_bluedroid_get_status() != (status)) { \
return ESP_ERR_INVALID_STATE; \
}
/* relate to BT_STATUS_xxx in bt_def.h */
/// Status Return Value
typedef enum {
ESP_BT_STATUS_SUCCESS = 0, /* relate to BT_STATUS_SUCCESS in bt_def.h */
ESP_BT_STATUS_FAIL, /* relate to BT_STATUS_FAIL in bt_def.h */
ESP_BT_STATUS_NOT_READY, /* relate to BT_STATUS_NOT_READY in bt_def.h */
ESP_BT_STATUS_NOMEM, /* relate to BT_STATUS_NOMEM in bt_def.h */
ESP_BT_STATUS_BUSY, /* relate to BT_STATUS_BUSY in bt_def.h */
ESP_BT_STATUS_DONE = 5, /* relate to BT_STATUS_DONE in bt_def.h */
ESP_BT_STATUS_UNSUPPORTED, /* relate to BT_STATUS_UNSUPPORTED in bt_def.h */
ESP_BT_STATUS_PARM_INVALID, /* relate to BT_STATUS_PARM_INVALID in bt_def.h */
ESP_BT_STATUS_UNHANDLED, /* relate to BT_STATUS_UNHANDLED in bt_def.h */
ESP_BT_STATUS_AUTH_FAILURE, /* relate to BT_STATUS_AUTH_FAILURE in bt_def.h */
ESP_BT_STATUS_RMT_DEV_DOWN = 10, /* relate to BT_STATUS_RMT_DEV_DOWN in bt_def.h */
ESP_BT_STATUS_AUTH_REJECTED, /* relate to BT_STATUS_AUTH_REJECTED in bt_def.h */
ESP_BT_STATUS_INVALID_STATIC_RAND_ADDR, /* relate to BT_STATUS_INVALID_STATIC_RAND_ADDR in bt_def.h */
ESP_BT_STATUS_PENDING, /* relate to BT_STATUS_PENDING in bt_def.h */
ESP_BT_STATUS_UNACCEPT_CONN_INTERVAL, /* relate to BT_UNACCEPT_CONN_INTERVAL in bt_def.h */
ESP_BT_STATUS_PARAM_OUT_OF_RANGE, /* relate to BT_PARAM_OUT_OF_RANGE in bt_def.h */
ESP_BT_STATUS_TIMEOUT, /* relate to BT_STATUS_TIMEOUT in bt_def.h */
ESP_BT_STATUS_PEER_LE_DATA_LEN_UNSUPPORTED, /* relate to BTM_PEER_LE_DATA_LEN_UNSUPPORTED in stack/btm_api.h */
ESP_BT_STATUS_CONTROL_LE_DATA_LEN_UNSUPPORTED,/* relate to BTM_CONTROL_LE_DATA_LEN_UNSUPPORTED in stack/btm_api.h */
ESP_BT_STATUS_ERR_ILLEGAL_PARAMETER_FMT, /* relate to HCI_ERR_ILLEGAL_PARAMETER_FMT in stack/hcidefs.h */
ESP_BT_STATUS_MEMORY_FULL = 20, /* relate to BT_STATUS_MEMORY_FULL in bt_def.h */
ESP_BT_STATUS_EIR_TOO_LARGE, /* relate to BT_STATUS_EIR_TOO_LARGE in bt_def.h */
} esp_bt_status_t;
/*Define the bt octet 16 bit size*/
#define ESP_BT_OCTET16_LEN 16
typedef uint8_t esp_bt_octet16_t[ESP_BT_OCTET16_LEN]; /* octet array: size 16 */
#define ESP_BT_OCTET8_LEN 8
typedef uint8_t esp_bt_octet8_t[ESP_BT_OCTET8_LEN]; /* octet array: size 8 */
typedef uint8_t esp_link_key[ESP_BT_OCTET16_LEN]; /* Link Key */
/// Default GATT interface id
#define ESP_DEFAULT_GATT_IF 0xff
#define ESP_BLE_CONN_INT_MIN 0x0006 /*!< relate to BTM_BLE_CONN_INT_MIN in stack/btm_ble_api.h */
#define ESP_BLE_CONN_INT_MAX 0x0C80 /*!< relate to BTM_BLE_CONN_INT_MAX in stack/btm_ble_api.h */
#define ESP_BLE_CONN_LATENCY_MAX 499 /*!< relate to ESP_BLE_CONN_LATENCY_MAX in stack/btm_ble_api.h */
#define ESP_BLE_CONN_SUP_TOUT_MIN 0x000A /*!< relate to BTM_BLE_CONN_SUP_TOUT_MIN in stack/btm_ble_api.h */
#define ESP_BLE_CONN_SUP_TOUT_MAX 0x0C80 /*!< relate to ESP_BLE_CONN_SUP_TOUT_MAX in stack/btm_ble_api.h */
#define ESP_BLE_CONN_PARAM_UNDEF 0xffff /* use this value when a specific value not to be overwritten */ /* relate to ESP_BLE_CONN_PARAM_UNDEF in stack/btm_ble_api.h */
#define ESP_BLE_SCAN_PARAM_UNDEF 0xffffffff /* relate to ESP_BLE_SCAN_PARAM_UNDEF in stack/btm_ble_api.h */
/// Check the param is valid or not
#define ESP_BLE_IS_VALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) || ((x) == ESP_BLE_CONN_PARAM_UNDEF))
/// UUID type
typedef struct {
#define ESP_UUID_LEN_16 2
#define ESP_UUID_LEN_32 4
#define ESP_UUID_LEN_128 16
uint16_t len; /*!< UUID length, 16bit, 32bit or 128bit */
union {
uint16_t uuid16; /*!< 16bit UUID */
uint32_t uuid32; /*!< 32bit UUID */
uint8_t uuid128[ESP_UUID_LEN_128]; /*!< 128bit UUID */
} uuid; /*!< UUID */
} __attribute__((packed)) esp_bt_uuid_t;
/// Bluetooth device type
typedef enum {
ESP_BT_DEVICE_TYPE_BREDR = 0x01,
ESP_BT_DEVICE_TYPE_BLE = 0x02,
ESP_BT_DEVICE_TYPE_DUMO = 0x03,
} esp_bt_dev_type_t;
/// Bluetooth address length
#define ESP_BD_ADDR_LEN 6
/// Bluetooth device address
typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN];
/// BLE device address type
typedef enum {
BLE_ADDR_TYPE_PUBLIC = 0x00,
BLE_ADDR_TYPE_RANDOM = 0x01,
BLE_ADDR_TYPE_RPA_PUBLIC = 0x02,
BLE_ADDR_TYPE_RPA_RANDOM = 0x03,
} esp_ble_addr_type_t;
/// white list address type
typedef enum {
BLE_WL_ADDR_TYPE_PUBLIC = 0x00,
BLE_WL_ADDR_TYPE_RANDOM = 0x01,
} esp_ble_wl_addr_type_t;
/// Used to exchange the encryption key in the init key & response key
#define ESP_BLE_ENC_KEY_MASK (1 << 0) /* relate to BTM_BLE_ENC_KEY_MASK in stack/btm_api.h */
/// Used to exchange the IRK key in the init key & response key
#define ESP_BLE_ID_KEY_MASK (1 << 1) /* relate to BTM_BLE_ID_KEY_MASK in stack/btm_api.h */
/// Used to exchange the CSRK key in the init key & response key
#define ESP_BLE_CSR_KEY_MASK (1 << 2) /* relate to BTM_BLE_CSR_KEY_MASK in stack/btm_api.h */
/// Used to exchange the link key(this key just used in the BLE & BR/EDR coexist mode) in the init key & response key
#define ESP_BLE_LINK_KEY_MASK (1 << 3) /* relate to BTM_BLE_LINK_KEY_MASK in stack/btm_api.h */
typedef uint8_t esp_ble_key_mask_t; /* the key mask type */
/// Minimum of the application id
#define ESP_APP_ID_MIN 0x0000
/// Maximum of the application id
#define ESP_APP_ID_MAX 0x7fff
#define ESP_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define ESP_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
#ifdef __cplusplus
}
#endif
#endif /* __ESP_BT_DEFS_H__ */

View File

@ -0,0 +1,58 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_BT_DEVICE_H__
#define __ESP_BT_DEVICE_H__
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
*
* @brief Get bluetooth device address. Must use after "esp_bluedroid_enable".
*
* @return bluetooth device address (six bytes), or NULL if bluetooth stack is not enabled
*/
const uint8_t *esp_bt_dev_get_address(void);
/**
* @brief Set bluetooth device name. This function should be called after esp_bluedroid_enable()
* completes successfully.
*
* A BR/EDR/LE device type shall have a single Bluetooth device name which shall be
* identical irrespective of the physical channel used to perform the name discovery procedure.
*
* @param[in] name : device name to be set
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_ARG : if name is NULL pointer or empty, or string length out of limit
* - ESP_ERR_INVALID_STATE : if bluetooth stack is not yet enabled
* - ESP_FAIL : others
*/
esp_err_t esp_bt_dev_set_device_name(const char *name);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_BT_DEVICE_H__ */

View File

@ -0,0 +1,81 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_BT_MAIN_H__
#define __ESP_BT_MAIN_H__
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Bluetooth stack status type, to indicate whether the bluetooth stack is ready.
*/
typedef enum {
ESP_BLUEDROID_STATUS_UNINITIALIZED = 0, /*!< Bluetooth not initialized */
ESP_BLUEDROID_STATUS_INITIALIZED, /*!< Bluetooth initialized but not enabled */
ESP_BLUEDROID_STATUS_ENABLED /*!< Bluetooth initialized and enabled */
} esp_bluedroid_status_t;
/**
* @brief Get bluetooth stack status
*
* @return Bluetooth stack status
*
*/
esp_bluedroid_status_t esp_bluedroid_get_status(void);
/**
* @brief Enable bluetooth, must after esp_bluedroid_init().
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_bluedroid_enable(void);
/**
* @brief Disable bluetooth, must prior to esp_bluedroid_deinit().
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_bluedroid_disable(void);
/**
* @brief Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff.
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_bluedroid_init(void);
/**
* @brief Deinit and free the resource for bluetooth, must be after every bluetooth stuff.
*
* @return
* - ESP_OK : Succeed
* - Other : Failed
*/
esp_err_t esp_bluedroid_deinit(void);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_BT_MAIN_H__ */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,777 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_GAP_BT_API_H__
#define __ESP_GAP_BT_API_H__
#include <stdint.h>
#include "esp_err.h"
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
/// RSSI threshold
#define ESP_BT_GAP_RSSI_HIGH_THRLD -20 /*!< High RSSI threshold */
#define ESP_BT_GAP_RSSI_LOW_THRLD -45 /*!< Low RSSI threshold */
/// Class of device
typedef struct {
uint32_t reserved_2: 2; /*!< undefined */
uint32_t minor: 6; /*!< minor class */
uint32_t major: 5; /*!< major class */
uint32_t service: 11; /*!< service class */
uint32_t reserved_8: 8; /*!< undefined */
} esp_bt_cod_t;
/// class of device settings
typedef enum {
ESP_BT_SET_COD_MAJOR_MINOR = 0x01, /*!< overwrite major, minor class */
ESP_BT_SET_COD_SERVICE_CLASS = 0x02, /*!< set the bits in the input, the current bit will remain */
ESP_BT_CLR_COD_SERVICE_CLASS = 0x04, /*!< clear the bits in the input, others will remain */
ESP_BT_SET_COD_ALL = 0x08, /*!< overwrite major, minor, set the bits in service class */
ESP_BT_INIT_COD = 0x0a, /*!< overwrite major, minor, and service class */
} esp_bt_cod_mode_t;
#define ESP_BT_GAP_AFH_CHANNELS_LEN 10
typedef uint8_t esp_bt_gap_afh_channels[ESP_BT_GAP_AFH_CHANNELS_LEN];
/// Discoverability and Connectability mode
typedef enum {
ESP_BT_NON_CONNECTABLE, /*!< Non-connectable */
ESP_BT_CONNECTABLE, /*!< Connectable */
} esp_bt_connection_mode_t;
typedef enum {
ESP_BT_NON_DISCOVERABLE, /*!< Non-discoverable */
ESP_BT_LIMITED_DISCOVERABLE, /*!< Limited Discoverable */
ESP_BT_GENERAL_DISCOVERABLE, /*!< General Discoverable */
} esp_bt_discovery_mode_t;
/// Bluetooth Device Property type
typedef enum {
ESP_BT_GAP_DEV_PROP_BDNAME = 1, /*!< Bluetooth device name, value type is int8_t [] */
ESP_BT_GAP_DEV_PROP_COD, /*!< Class of Device, value type is uint32_t */
ESP_BT_GAP_DEV_PROP_RSSI, /*!< Received Signal strength Indication, value type is int8_t, ranging from -128 to 127 */
ESP_BT_GAP_DEV_PROP_EIR, /*!< Extended Inquiry Response, value type is uint8_t [] */
} esp_bt_gap_dev_prop_type_t;
/// Maximum bytes of Bluetooth device name
#define ESP_BT_GAP_MAX_BDNAME_LEN (248)
/// Maximum size of EIR Significant part
#define ESP_BT_GAP_EIR_DATA_LEN (240)
/// Bluetooth Device Property Descriptor
typedef struct {
esp_bt_gap_dev_prop_type_t type; /*!< Device property type */
int len; /*!< Device property value length */
void *val; /*!< Device property value */
} esp_bt_gap_dev_prop_t;
/// Extended Inquiry Response data type
#define ESP_BT_EIR_TYPE_FLAGS 0x01 /*!< Flag with information such as BR/EDR and LE support */
#define ESP_BT_EIR_TYPE_INCMPL_16BITS_UUID 0x02 /*!< Incomplete list of 16-bit service UUIDs */
#define ESP_BT_EIR_TYPE_CMPL_16BITS_UUID 0x03 /*!< Complete list of 16-bit service UUIDs */
#define ESP_BT_EIR_TYPE_INCMPL_32BITS_UUID 0x04 /*!< Incomplete list of 32-bit service UUIDs */
#define ESP_BT_EIR_TYPE_CMPL_32BITS_UUID 0x05 /*!< Complete list of 32-bit service UUIDs */
#define ESP_BT_EIR_TYPE_INCMPL_128BITS_UUID 0x06 /*!< Incomplete list of 128-bit service UUIDs */
#define ESP_BT_EIR_TYPE_CMPL_128BITS_UUID 0x07 /*!< Complete list of 128-bit service UUIDs */
#define ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME 0x08 /*!< Shortened Local Name */
#define ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME 0x09 /*!< Complete Local Name */
#define ESP_BT_EIR_TYPE_TX_POWER_LEVEL 0x0a /*!< Tx power level, value is 1 octet ranging from -127 to 127, unit is dBm*/
#define ESP_BT_EIR_TYPE_URL 0x24 /*!< Uniform resource identifier */
#define ESP_BT_EIR_TYPE_MANU_SPECIFIC 0xff /*!< Manufacturer specific data */
#define ESP_BT_EIR_TYPE_MAX_NUM 12 /*!< MAX number of EIR type */
typedef uint8_t esp_bt_eir_type_t;
/* ESP_BT_EIR_FLAG bit definition */
#define ESP_BT_EIR_FLAG_LIMIT_DISC (0x01 << 0)
#define ESP_BT_EIR_FLAG_GEN_DISC (0x01 << 1)
#define ESP_BT_EIR_FLAG_BREDR_NOT_SPT (0x01 << 2)
#define ESP_BT_EIR_FLAG_DMT_CONTROLLER_SPT (0x01 << 3)
#define ESP_BT_EIR_FLAG_DMT_HOST_SPT (0x01 << 4)
#define ESP_BT_EIR_MAX_LEN 240
/// EIR data content, according to "Supplement to the Bluetooth Core Specification"
typedef struct {
bool fec_required; /*!< FEC is required or not, true by default */
bool include_txpower; /*!< EIR data include TX power, false by default */
bool include_uuid; /*!< EIR data include UUID, false by default */
uint8_t flag; /*!< EIR flags, see ESP_BT_EIR_FLAG for details, EIR will not include flag if it is 0, 0 by default */
uint16_t manufacturer_len; /*!< Manufacturer data length, 0 by default */
uint8_t *p_manufacturer_data; /*!< Manufacturer data point */
uint16_t url_len; /*!< URL length, 0 by default */
uint8_t *p_url; /*!< URL point */
} esp_bt_eir_data_t;
/// Major service class field of Class of Device, mutiple bits can be set
typedef enum {
ESP_BT_COD_SRVC_NONE = 0, /*!< None indicates an invalid value */
ESP_BT_COD_SRVC_LMTD_DISCOVER = 0x1, /*!< Limited Discoverable Mode */
ESP_BT_COD_SRVC_POSITIONING = 0x8, /*!< Positioning (Location identification) */
ESP_BT_COD_SRVC_NETWORKING = 0x10, /*!< Networking, e.g. LAN, Ad hoc */
ESP_BT_COD_SRVC_RENDERING = 0x20, /*!< Rendering, e.g. Printing, Speakers */
ESP_BT_COD_SRVC_CAPTURING = 0x40, /*!< Capturing, e.g. Scanner, Microphone */
ESP_BT_COD_SRVC_OBJ_TRANSFER = 0x80, /*!< Object Transfer, e.g. v-Inbox, v-Folder */
ESP_BT_COD_SRVC_AUDIO = 0x100, /*!< Audio, e.g. Speaker, Microphone, Headset service */
ESP_BT_COD_SRVC_TELEPHONY = 0x200, /*!< Telephony, e.g. Cordless telephony, Modem, Headset service */
ESP_BT_COD_SRVC_INFORMATION = 0x400, /*!< Information, e.g., WEB-server, WAP-server */
} esp_bt_cod_srvc_t;
typedef enum{
ESP_BT_PIN_TYPE_VARIABLE = 0, /*!< Refer to BTM_PIN_TYPE_VARIABLE */
ESP_BT_PIN_TYPE_FIXED = 1, /*!< Refer to BTM_PIN_TYPE_FIXED */
} esp_bt_pin_type_t;
#define ESP_BT_PIN_CODE_LEN 16 /*!< Max pin code length */
typedef uint8_t esp_bt_pin_code_t[ESP_BT_PIN_CODE_LEN]; /*!< Pin Code (upto 128 bits) MSB is 0 */
typedef enum {
ESP_BT_SP_IOCAP_MODE = 0, /*!< Set IO mode */
//ESP_BT_SP_OOB_DATA, //TODO /*!< Set OOB data */
} esp_bt_sp_param_t;
/* relate to BTM_IO_CAP_xxx in stack/btm_api.h */
#define ESP_BT_IO_CAP_OUT 0 /*!< DisplayOnly */ /* relate to BTM_IO_CAP_OUT in stack/btm_api.h */
#define ESP_BT_IO_CAP_IO 1 /*!< DisplayYesNo */ /* relate to BTM_IO_CAP_IO in stack/btm_api.h */
#define ESP_BT_IO_CAP_IN 2 /*!< KeyboardOnly */ /* relate to BTM_IO_CAP_IN in stack/btm_api.h */
#define ESP_BT_IO_CAP_NONE 3 /*!< NoInputNoOutput */ /* relate to BTM_IO_CAP_NONE in stack/btm_api.h */
typedef uint8_t esp_bt_io_cap_t; /*!< Combination of the IO Capability */
/* BTM Power manager modes */
#define ESP_BT_PM_MD_ACTIVE 0x00 /*!< Active mode */
#define ESP_BT_PM_MD_HOLD 0x01 /*!< Hold mode */
#define ESP_BT_PM_MD_SNIFF 0x02 /*!< Sniff mode */
#define ESP_BT_PM_MD_PARK 0x03 /*!< Park state */
typedef uint8_t esp_bt_pm_mode_t;
/// Bits of major service class field
#define ESP_BT_COD_SRVC_BIT_MASK (0xffe000) /*!< Major service bit mask */
#define ESP_BT_COD_SRVC_BIT_OFFSET (13) /*!< Major service bit offset */
/// Major device class field of Class of Device
typedef enum {
ESP_BT_COD_MAJOR_DEV_MISC = 0, /*!< Miscellaneous */
ESP_BT_COD_MAJOR_DEV_COMPUTER = 1, /*!< Computer */
ESP_BT_COD_MAJOR_DEV_PHONE = 2, /*!< Phone(cellular, cordless, pay phone, modem */
ESP_BT_COD_MAJOR_DEV_LAN_NAP = 3, /*!< LAN, Network Access Point */
ESP_BT_COD_MAJOR_DEV_AV = 4, /*!< Audio/Video(headset, speaker, stereo, video display, VCR */
ESP_BT_COD_MAJOR_DEV_PERIPHERAL = 5, /*!< Peripheral(mouse, joystick, keyboard) */
ESP_BT_COD_MAJOR_DEV_IMAGING = 6, /*!< Imaging(printer, scanner, camera, display */
ESP_BT_COD_MAJOR_DEV_WEARABLE = 7, /*!< Wearable */
ESP_BT_COD_MAJOR_DEV_TOY = 8, /*!< Toy */
ESP_BT_COD_MAJOR_DEV_HEALTH = 9, /*!< Health */
ESP_BT_COD_MAJOR_DEV_UNCATEGORIZED = 31, /*!< Uncategorized: device not specified */
} esp_bt_cod_major_dev_t;
/// Bits of major device class field
#define ESP_BT_COD_MAJOR_DEV_BIT_MASK (0x1f00) /*!< Major device bit mask */
#define ESP_BT_COD_MAJOR_DEV_BIT_OFFSET (8) /*!< Major device bit offset */
/// Bits of minor device class field
#define ESP_BT_COD_MINOR_DEV_BIT_MASK (0xfc) /*!< Minor device bit mask */
#define ESP_BT_COD_MINOR_DEV_BIT_OFFSET (2) /*!< Minor device bit offset */
/// Bits of format type
#define ESP_BT_COD_FORMAT_TYPE_BIT_MASK (0x03) /*!< Format type bit mask */
#define ESP_BT_COD_FORMAT_TYPE_BIT_OFFSET (0) /*!< Format type bit offset */
/// Class of device format type 1
#define ESP_BT_COD_FORMAT_TYPE_1 (0x00)
/** Bluetooth Device Discovery state */
typedef enum {
ESP_BT_GAP_DISCOVERY_STOPPED, /*!< Device discovery stopped */
ESP_BT_GAP_DISCOVERY_STARTED, /*!< Device discovery started */
} esp_bt_gap_discovery_state_t;
/// BT GAP callback events
typedef enum {
ESP_BT_GAP_DISC_RES_EVT = 0, /*!< Device discovery result event */
ESP_BT_GAP_DISC_STATE_CHANGED_EVT, /*!< Discovery state changed event */
ESP_BT_GAP_RMT_SRVCS_EVT, /*!< Get remote services event */
ESP_BT_GAP_RMT_SRVC_REC_EVT, /*!< Get remote service record event */
ESP_BT_GAP_AUTH_CMPL_EVT, /*!< Authentication complete event */
ESP_BT_GAP_PIN_REQ_EVT, /*!< Legacy Pairing Pin code request */
ESP_BT_GAP_CFM_REQ_EVT, /*!< Security Simple Pairing User Confirmation request. */
ESP_BT_GAP_KEY_NOTIF_EVT, /*!< Security Simple Pairing Passkey Notification */
ESP_BT_GAP_KEY_REQ_EVT, /*!< Security Simple Pairing Passkey request */
ESP_BT_GAP_READ_RSSI_DELTA_EVT, /*!< Read rssi event */
ESP_BT_GAP_CONFIG_EIR_DATA_EVT, /*!< Config EIR data event */
ESP_BT_GAP_SET_AFH_CHANNELS_EVT, /*!< Set AFH channels event */
ESP_BT_GAP_READ_REMOTE_NAME_EVT, /*!< Read Remote Name event */
ESP_BT_GAP_MODE_CHG_EVT,
ESP_BT_GAP_REMOVE_BOND_DEV_COMPLETE_EVT, /*!< remove bond device complete event */
ESP_BT_GAP_QOS_CMPL_EVT, /*!< QOS complete event */
ESP_BT_GAP_EVT_MAX,
} esp_bt_gap_cb_event_t;
/** Inquiry Mode */
typedef enum {
ESP_BT_INQ_MODE_GENERAL_INQUIRY, /*!< General inquiry mode */
ESP_BT_INQ_MODE_LIMITED_INQUIRY, /*!< Limited inquiry mode */
} esp_bt_inq_mode_t;
/** Minimum and Maximum inquiry length*/
#define ESP_BT_GAP_MIN_INQ_LEN (0x01) /*!< Minimum inquiry duration, unit is 1.28s */
#define ESP_BT_GAP_MAX_INQ_LEN (0x30) /*!< Maximum inquiry duration, unit is 1.28s */
/// A2DP state callback parameters
typedef union {
/**
* @brief ESP_BT_GAP_DISC_RES_EVT
*/
struct disc_res_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
int num_prop; /*!< number of properties got */
esp_bt_gap_dev_prop_t *prop; /*!< properties discovered from the new device */
} disc_res; /*!< discovery result parameter struct */
/**
* @brief ESP_BT_GAP_DISC_STATE_CHANGED_EVT
*/
struct disc_state_changed_param {
esp_bt_gap_discovery_state_t state; /*!< discovery state */
} disc_st_chg; /*!< discovery state changed parameter struct */
/**
* @brief ESP_BT_GAP_RMT_SRVCS_EVT
*/
struct rmt_srvcs_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_status_t stat; /*!< service search status */
int num_uuids; /*!< number of UUID in uuid_list */
esp_bt_uuid_t *uuid_list; /*!< list of service UUIDs of remote device */
} rmt_srvcs; /*!< services of remote device parameter struct */
/**
* @brief ESP_BT_GAP_RMT_SRVC_REC_EVT
*/
struct rmt_srvc_rec_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_status_t stat; /*!< service search status */
} rmt_srvc_rec; /*!< specific service record from remote device parameter struct */
/**
* @brief ESP_BT_GAP_READ_RSSI_DELTA_EVT *
*/
struct read_rssi_delta_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_status_t stat; /*!< read rssi status */
int8_t rssi_delta; /*!< rssi delta value range -128 ~127, The value zero indicates that the RSSI is inside the Golden Receive Power Range, the Golden Receive Power Range is from ESP_BT_GAP_RSSI_LOW_THRLD to ESP_BT_GAP_RSSI_HIGH_THRLD */
} read_rssi_delta; /*!< read rssi parameter struct */
/**
* @brief ESP_BT_GAP_CONFIG_EIR_DATA_EVT *
*/
struct config_eir_data_param {
esp_bt_status_t stat; /*!< config EIR status:
ESP_BT_STATUS_SUCCESS: config success
ESP_BT_STATUS_EIR_TOO_LARGE: the EIR data is more than 240B. The EIR may not contain the whole data.
others: failed
*/
uint8_t eir_type_num; /*!< the number of EIR types in EIR type */
esp_bt_eir_type_t eir_type[ESP_BT_EIR_TYPE_MAX_NUM]; /*!< EIR types in EIR type */
} config_eir_data; /*!< config EIR data */
/**
* @brief ESP_BT_GAP_AUTH_CMPL_EVT
*/
struct auth_cmpl_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_status_t stat; /*!< authentication complete status */
uint8_t device_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1]; /*!< device name */
} auth_cmpl; /*!< authentication complete parameter struct */
/**
* @brief ESP_BT_GAP_PIN_REQ_EVT
*/
struct pin_req_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
bool min_16_digit; /*!< TRUE if the pin returned must be at least 16 digits */
} pin_req; /*!< pin request parameter struct */
/**
* @brief ESP_BT_GAP_CFM_REQ_EVT
*/
struct cfm_req_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
uint32_t num_val; /*!< the numeric value for comparison. */
} cfm_req; /*!< confirm request parameter struct */
/**
* @brief ESP_BT_GAP_KEY_NOTIF_EVT
*/
struct key_notif_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
uint32_t passkey; /*!< the numeric value for passkey entry. */
} key_notif; /*!< passkey notif parameter struct */
/**
* @brief ESP_BT_GAP_KEY_REQ_EVT
*/
struct key_req_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
} key_req; /*!< passkey request parameter struct */
/**
* @brief ESP_BT_GAP_SET_AFH_CHANNELS_EVT
*/
struct set_afh_channels_param {
esp_bt_status_t stat; /*!< set AFH channel status */
} set_afh_channels; /*!< set AFH channel parameter struct */
/**
* @brief ESP_BT_GAP_READ_REMOTE_NAME_EVT
*/
struct read_rmt_name_param {
esp_bt_status_t stat; /*!< read Remote Name status */
uint8_t rmt_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1]; /*!< Remote device name */
} read_rmt_name; /*!< read Remote Name parameter struct */
/**
* @brief ESP_BT_GAP_MODE_CHG_EVT
*/
struct mode_chg_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_pm_mode_t mode; /*!< PM mode*/
} mode_chg; /*!< mode change event parameter struct */
/**
* @brief ESP_BT_GAP_REMOVE_BOND_DEV_COMPLETE_EVT
*/
struct bt_remove_bond_dev_cmpl_evt_param {
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
esp_bt_status_t status; /*!< Indicate the remove bond device operation success status */
}remove_bond_dev_cmpl; /*!< Event parameter of ESP_BT_GAP_REMOVE_BOND_DEV_COMPLETE_EVT */
/**
* @brief ESP_BT_GAP_QOS_CMPL_EVT
*/
struct qos_cmpl_param {
esp_bt_status_t stat; /*!< QoS status */
esp_bd_addr_t bda; /*!< remote bluetooth device address*/
uint32_t t_poll; /*!< poll interval, the maximum time between transmissions
which from the master to a particular slave on the ACL
logical transport. unit is 0.625ms. */
} qos_cmpl; /*!< QoS complete parameter struct */
} esp_bt_gap_cb_param_t;
/**
* @brief bluetooth GAP callback function type
*
* @param event : Event type
*
* @param param : Pointer to callback parameter
*/
typedef void (* esp_bt_gap_cb_t)(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
/**
* @brief get major service field of COD
*
* @param[in] cod: Class of Device
*
* @return major service bits
*/
static inline uint32_t esp_bt_gap_get_cod_srvc(uint32_t cod)
{
return (cod & ESP_BT_COD_SRVC_BIT_MASK) >> ESP_BT_COD_SRVC_BIT_OFFSET;
}
/**
* @brief get major device field of COD
*
* @param[in] cod: Class of Device
*
* @return major device bits
*/
static inline uint32_t esp_bt_gap_get_cod_major_dev(uint32_t cod)
{
return (cod & ESP_BT_COD_MAJOR_DEV_BIT_MASK) >> ESP_BT_COD_MAJOR_DEV_BIT_OFFSET;
}
/**
* @brief get minor service field of COD
*
* @param[in] cod: Class of Device
*
* @return minor service bits
*/
static inline uint32_t esp_bt_gap_get_cod_minor_dev(uint32_t cod)
{
return (cod & ESP_BT_COD_MINOR_DEV_BIT_MASK) >> ESP_BT_COD_MINOR_DEV_BIT_OFFSET;
}
/**
* @brief get format type of COD
*
* @param[in] cod: Class of Device
*
* @return format type
*/
static inline uint32_t esp_bt_gap_get_cod_format_type(uint32_t cod)
{
return (cod & ESP_BT_COD_FORMAT_TYPE_BIT_MASK);
}
/**
* @brief decide the integrity of COD
*
* @param[in] cod: Class of Device
*
* @return
* - true if cod is valid
* - false otherise
*/
static inline bool esp_bt_gap_is_valid_cod(uint32_t cod)
{
if (esp_bt_gap_get_cod_format_type(cod) == ESP_BT_COD_FORMAT_TYPE_1 &&
esp_bt_gap_get_cod_srvc(cod) != ESP_BT_COD_SRVC_NONE) {
return true;
}
return false;
}
/**
* @brief register callback function. This function should be called after esp_bluedroid_enable() completes successfully
*
* @return
* - ESP_OK : Succeed
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback);
/**
* @brief Set discoverability and connectability mode for legacy bluetooth. This function should
* be called after esp_bluedroid_enable() completes successfully
*
* @param[in] c_mode : one of the enums of esp_bt_connection_mode_t
*
* @param[in] d_mode : one of the enums of esp_bt_discovery_mode_t
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_ARG: if argument invalid
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_set_scan_mode(esp_bt_connection_mode_t c_mode, esp_bt_discovery_mode_t d_mode);
/**
* @brief This function starts Inquiry and Name Discovery. This function should be called after esp_bluedroid_enable() completes successfully.
* When Inquiry is halted and cached results do not contain device name, then Name Discovery will connect to the peer target to get the device name.
* esp_bt_gap_cb_t will be called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT when Inquriry is started or Name Discovery is completed.
* esp_bt_gap_cb_t will be called with ESP_BT_GAP_DISC_RES_EVT each time the two types of discovery results are got.
*
* @param[in] mode - Inquiry mode
*
* @param[in] inq_len - Inquiry duration in 1.28 sec units, ranging from 0x01 to 0x30. This parameter only specifies the total duration of the Inquiry process,
* - when this time expires, Inquiry will be halted.
*
* @param[in] num_rsps - Number of responses that can be received before the Inquiry is halted, value 0 indicates an unlimited number of responses.
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_ARG: if invalid parameters are provided
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_start_discovery(esp_bt_inq_mode_t mode, uint8_t inq_len, uint8_t num_rsps);
/**
* @brief Cancel Inquiry and Name Discovery. This function should be called after esp_bluedroid_enable() completes successfully.
* esp_bt_gap_cb_t will be called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if Inquiry or Name Discovery is cancelled by
* calling this function.
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_cancel_discovery(void);
/**
* @brief Start SDP to get remote services. This function should be called after esp_bluedroid_enable() completes successfully.
* esp_bt_gap_cb_t will be called with ESP_BT_GAP_RMT_SRVCS_EVT after service discovery ends.
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda);
/**
* @brief Start SDP to look up the service matching uuid on the remote device. This function should be called after
* esp_bluedroid_enable() completes successfully.
*
* esp_bt_gap_cb_t will be called with ESP_BT_GAP_RMT_SRVC_REC_EVT after service discovery ends
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_uuid_t *uuid);
/**
* @brief This function is called to get EIR data for a specific type.
*
* @param[in] eir - pointer of raw eir data to be resolved
* @param[in] type - specific EIR data type
* @param[out] length - return the length of EIR data excluding fields of length and data type
*
* @return pointer of starting position of eir data excluding eir data type, NULL if not found
*
*/
uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8_t *length);
/**
* @brief This function is called to config EIR data.
*
* esp_bt_gap_cb_t will be called with ESP_BT_GAP_CONFIG_EIR_DATA_EVT after config EIR ends.
*
* @param[in] eir_data - pointer of EIR data content
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_ARG: if param is invalid
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_config_eir_data(esp_bt_eir_data_t *eir_data);
/**
* @brief This function is called to set class of device.
* The structure esp_bt_gap_cb_t will be called with ESP_BT_GAP_SET_COD_EVT after set COD ends.
* Some profile have special restrictions on class of device, changes may cause these profile do not work.
*
* @param[in] cod - class of device
* @param[in] mode - setting mode
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_ARG: if param is invalid
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_set_cod(esp_bt_cod_t cod, esp_bt_cod_mode_t mode);
/**
* @brief This function is called to get class of device.
*
* @param[out] cod - class of device
*
* @return
* - ESP_OK : Succeed
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_cod(esp_bt_cod_t *cod);
/**
* @brief This function is called to read RSSI delta by address after connected. The RSSI value returned by ESP_BT_GAP_READ_RSSI_DELTA_EVT.
*
*
* @param[in] remote_addr - remote device address, corresponding to a certain connection handle
* @return
* - ESP_OK : Succeed
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_gap_read_rssi_delta(esp_bd_addr_t remote_addr);
/**
* @brief Removes a device from the security database list of
* peer device.
*
* @param[in] bd_addr : BD address of the peer device
*
* @return - ESP_OK : success
* - ESP_FAIL : failed
*
*/
esp_err_t esp_bt_gap_remove_bond_device(esp_bd_addr_t bd_addr);
/**
* @brief Get the device number from the security database list of peer device.
* It will return the device bonded number immediately.
*
* @return - >= 0 : bonded devices number
* - ESP_FAIL : failed
*
*/
int esp_bt_gap_get_bond_device_num(void);
/**
* @brief Get the device from the security database list of peer device.
* It will return the device bonded information immediately.
*
* @param[inout] dev_num: Indicate the dev_list array(buffer) size as input.
* If dev_num is large enough, it means the actual number as output.
* Suggest that dev_num value equal to esp_ble_get_bond_device_num().
*
* @param[out] dev_list: an array(buffer) of `esp_bd_addr_t` type. Use for storing the bonded devices address.
* The dev_list should be allocated by who call this API.
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t esp_bt_gap_get_bond_device_list(int *dev_num, esp_bd_addr_t *dev_list);
/**
* @brief Set pin type and default pin code for legacy pairing.
*
* @param[in] pin_type: Use variable or fixed pin.
* If pin_type is ESP_BT_PIN_TYPE_VARIABLE, pin_code and pin_code_len
* will be ignored, and ESP_BT_GAP_PIN_REQ_EVT will come when control
* requests for pin code.
* Else, will use fixed pin code and not callback to users.
*
* @param[in] pin_code_len: Length of pin_code
*
* @param[in] pin_code: Pin_code
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*/
esp_err_t esp_bt_gap_set_pin(esp_bt_pin_type_t pin_type, uint8_t pin_code_len, esp_bt_pin_code_t pin_code);
/**
* @brief Reply the pin_code to the peer device for legacy pairing
* when ESP_BT_GAP_PIN_REQ_EVT is coming.
*
* @param[in] bd_addr: BD address of the peer
*
* @param[in] accept: Pin_code reply successful or declined.
*
* @param[in] pin_code_len: Length of pin_code
*
* @param[in] pin_code: Pin_code
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*/
esp_err_t esp_bt_gap_pin_reply(esp_bd_addr_t bd_addr, bool accept, uint8_t pin_code_len, esp_bt_pin_code_t pin_code);
#if (BT_SSP_INCLUDED == TRUE)
/**
* @brief Set a GAP security parameter value. Overrides the default value.
*
* @param[in] param_type : the type of the param which is to be set
*
* @param[in] value : the param value
*
* @param[in] len : the length of the param value
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*
*/
esp_err_t esp_bt_gap_set_security_param(esp_bt_sp_param_t param_type,
void *value, uint8_t len);
/**
* @brief Reply the key value to the peer device in the legacy connection stage.
*
* @param[in] bd_addr : BD address of the peer
*
* @param[in] accept : passkey entry successful or declined.
*
* @param[in] passkey : passkey value, must be a 6 digit number, can be lead by 0.
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*
*/
esp_err_t esp_bt_gap_ssp_passkey_reply(esp_bd_addr_t bd_addr, bool accept, uint32_t passkey);
/**
* @brief Reply the confirm value to the peer device in the legacy connection stage.
*
* @param[in] bd_addr : BD address of the peer device
*
* @param[in] accept : numbers to compare are the same or different
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*
*/
esp_err_t esp_bt_gap_ssp_confirm_reply(esp_bd_addr_t bd_addr, bool accept);
#endif /*(BT_SSP_INCLUDED == TRUE)*/
/**
* @brief Set the AFH channels
*
* @param[in] channels : The n th such field (in the range 0 to 78) contains the value for channel n :
* 0 means channel n is bad.
* 1 means channel n is unknown.
* The most significant bit is reserved and shall be set to 0.
* At least 20 channels shall be marked as unknown.
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*
*/
esp_err_t esp_bt_gap_set_afh_channels(esp_bt_gap_afh_channels channels);
/**
* @brief Read the remote device name
*
* @param[in] remote_bda: The remote device's address
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*
*/
esp_err_t esp_bt_gap_read_remote_name(esp_bd_addr_t remote_bda);
/**
* @brief Config Quality of service
*
* @param[in] remote_bda: The remote device's address
* @param[in] t_poll: Poll interval, the maximum time between transmissions
which from the master to a particular slave on the ACL
logical transport. unit is 0.625ms
*
* @return - ESP_OK : success
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - other : failed
*
*/
esp_err_t esp_bt_gap_set_qos(esp_bd_addr_t remote_bda, uint32_t t_poll);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_GAP_BT_API_H__ */

View File

@ -0,0 +1,56 @@
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_GATT_COMMON_API_H__
#define __ESP_GATT_COMMON_API_H__
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
// Maximum Transmission Unit used in GATT
#define ESP_GATT_DEF_BLE_MTU_SIZE 23 /* relate to GATT_DEF_BLE_MTU_SIZE in stack/gatt_api.h */
// Maximum Transmission Unit allowed in GATT
#define ESP_GATT_MAX_MTU_SIZE 517 /* relate to GATT_MAX_MTU_SIZE in stack/gatt_api.h */
/**
* @brief This function is called to set local MTU,
* the function is called before BLE connection.
*
* @param[in] mtu: the size of MTU.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu);
#if (BLE_INCLUDED == TRUE)
extern uint16_t esp_ble_get_sendable_packets_num (void);
extern uint16_t esp_ble_get_cur_sendable_packets_num (uint16_t connid);
#endif
#ifdef __cplusplus
}
#endif
#endif /* __ESP_GATT_COMMON_API_H__ */

View File

@ -0,0 +1,492 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_GATT_DEFS_H__
#define __ESP_GATT_DEFS_H__
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
/// GATT INVALID UUID
#define ESP_GATT_ILLEGAL_UUID 0
/// GATT INVALID HANDLE
#define ESP_GATT_ILLEGAL_HANDLE 0
/// GATT attribute max handle
#define ESP_GATT_ATTR_HANDLE_MAX 100
#define ESP_GATT_MAX_READ_MULTI_HANDLES 10 /* Max attributes to read in one request */
/**@{
* All "ESP_GATT_UUID_xxx" is attribute types
*/
#define ESP_GATT_UUID_IMMEDIATE_ALERT_SVC 0x1802 /* Immediate alert Service*/
#define ESP_GATT_UUID_LINK_LOSS_SVC 0x1803 /* Link Loss Service*/
#define ESP_GATT_UUID_TX_POWER_SVC 0x1804 /* TX Power Service*/
#define ESP_GATT_UUID_CURRENT_TIME_SVC 0x1805 /* Current Time Service Service*/
#define ESP_GATT_UUID_REF_TIME_UPDATE_SVC 0x1806 /* Reference Time Update Service*/
#define ESP_GATT_UUID_NEXT_DST_CHANGE_SVC 0x1807 /* Next DST Change Service*/
#define ESP_GATT_UUID_GLUCOSE_SVC 0x1808 /* Glucose Service*/
#define ESP_GATT_UUID_HEALTH_THERMOM_SVC 0x1809 /* Health Thermometer Service*/
#define ESP_GATT_UUID_DEVICE_INFO_SVC 0x180A /* Device Information Service*/
#define ESP_GATT_UUID_HEART_RATE_SVC 0x180D /* Heart Rate Service*/
#define ESP_GATT_UUID_PHONE_ALERT_STATUS_SVC 0x180E /* Phone Alert Status Service*/
#define ESP_GATT_UUID_BATTERY_SERVICE_SVC 0x180F /* Battery Service*/
#define ESP_GATT_UUID_BLOOD_PRESSURE_SVC 0x1810 /* Blood Pressure Service*/
#define ESP_GATT_UUID_ALERT_NTF_SVC 0x1811 /* Alert Notification Service*/
#define ESP_GATT_UUID_HID_SVC 0x1812 /* HID Service*/
#define ESP_GATT_UUID_SCAN_PARAMETERS_SVC 0x1813 /* Scan Parameters Service*/
#define ESP_GATT_UUID_RUNNING_SPEED_CADENCE_SVC 0x1814 /* Running Speed and Cadence Service*/
#define ESP_GATT_UUID_Automation_IO_SVC 0x1815 /* Automation IO Service*/
#define ESP_GATT_UUID_CYCLING_SPEED_CADENCE_SVC 0x1816 /* Cycling Speed and Cadence Service*/
#define ESP_GATT_UUID_CYCLING_POWER_SVC 0x1818 /* Cycling Power Service*/
#define ESP_GATT_UUID_LOCATION_AND_NAVIGATION_SVC 0x1819 /* Location and Navigation Service*/
#define ESP_GATT_UUID_ENVIRONMENTAL_SENSING_SVC 0x181A /* Environmental Sensing Service*/
#define ESP_GATT_UUID_BODY_COMPOSITION 0x181B /* Body Composition Service*/
#define ESP_GATT_UUID_USER_DATA_SVC 0x181C /* User Data Service*/
#define ESP_GATT_UUID_WEIGHT_SCALE_SVC 0x181D /* Weight Scale Service*/
#define ESP_GATT_UUID_BOND_MANAGEMENT_SVC 0x181E /* Bond Management Service*/
#define ESP_GATT_UUID_CONT_GLUCOSE_MONITOR_SVC 0x181F /* Continuous Glucose Monitoring Service*/
#define ESP_GATT_UUID_PRI_SERVICE 0x2800
#define ESP_GATT_UUID_SEC_SERVICE 0x2801
#define ESP_GATT_UUID_INCLUDE_SERVICE 0x2802
#define ESP_GATT_UUID_CHAR_DECLARE 0x2803 /* Characteristic Declaration*/
#define ESP_GATT_UUID_CHAR_EXT_PROP 0x2900 /* Characteristic Extended Properties */
#define ESP_GATT_UUID_CHAR_DESCRIPTION 0x2901 /* Characteristic User Description*/
#define ESP_GATT_UUID_CHAR_CLIENT_CONFIG 0x2902 /* Client Characteristic Configuration */
#define ESP_GATT_UUID_CHAR_SRVR_CONFIG 0x2903 /* Server Characteristic Configuration */
#define ESP_GATT_UUID_CHAR_PRESENT_FORMAT 0x2904 /* Characteristic Presentation Format*/
#define ESP_GATT_UUID_CHAR_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/
#define ESP_GATT_UUID_CHAR_VALID_RANGE 0x2906 /* Characteristic Valid Range */
#define ESP_GATT_UUID_EXT_RPT_REF_DESCR 0x2907 /* External Report Reference */
#define ESP_GATT_UUID_RPT_REF_DESCR 0x2908 /* Report Reference */
#define ESP_GATT_UUID_NUM_DIGITALS_DESCR 0x2909 /* Number of Digitals */
#define ESP_GATT_UUID_VALUE_TRIGGER_DESCR 0x290A /* Value Trigger Setting */
#define ESP_GATT_UUID_ENV_SENSING_CONFIG_DESCR 0x290B /* Environmental Sensing Configuration */
#define ESP_GATT_UUID_ENV_SENSING_MEASUREMENT_DESCR 0x290C /* Environmental Sensing Measurement */
#define ESP_GATT_UUID_ENV_SENSING_TRIGGER_DESCR 0x290D /* Environmental Sensing Trigger Setting */
#define ESP_GATT_UUID_TIME_TRIGGER_DESCR 0x290E /* Time Trigger Setting */
/* GAP Profile Attributes */
#define ESP_GATT_UUID_GAP_DEVICE_NAME 0x2A00
#define ESP_GATT_UUID_GAP_ICON 0x2A01
#define ESP_GATT_UUID_GAP_PREF_CONN_PARAM 0x2A04
#define ESP_GATT_UUID_GAP_CENTRAL_ADDR_RESOL 0x2AA6
/* Attribute Profile Attribute UUID */
#define ESP_GATT_UUID_GATT_SRV_CHGD 0x2A05
/* Link ESP_Loss Service */
#define ESP_GATT_UUID_ALERT_LEVEL 0x2A06 /* Alert Level */
#define ESP_GATT_UUID_TX_POWER_LEVEL 0x2A07 /* TX power level */
/* Current Time Service */
#define ESP_GATT_UUID_CURRENT_TIME 0x2A2B /* Current Time */
#define ESP_GATT_UUID_LOCAL_TIME_INFO 0x2A0F /* Local time info */
#define ESP_GATT_UUID_REF_TIME_INFO 0x2A14 /* reference time information */
/* Network availability Profile */
#define ESP_GATT_UUID_NW_STATUS 0x2A18 /* network availability status */
#define ESP_GATT_UUID_NW_TRIGGER 0x2A1A /* Network availability trigger */
/* Phone alert */
#define ESP_GATT_UUID_ALERT_STATUS 0x2A3F /* alert status */
#define ESP_GATT_UUID_RINGER_CP 0x2A40 /* ringer control point */
#define ESP_GATT_UUID_RINGER_SETTING 0x2A41 /* ringer setting */
/* Glucose Service */
#define ESP_GATT_UUID_GM_MEASUREMENT 0x2A18
#define ESP_GATT_UUID_GM_CONTEXT 0x2A34
#define ESP_GATT_UUID_GM_CONTROL_POINT 0x2A52
#define ESP_GATT_UUID_GM_FEATURE 0x2A51
/* device information characteristic */
#define ESP_GATT_UUID_SYSTEM_ID 0x2A23
#define ESP_GATT_UUID_MODEL_NUMBER_STR 0x2A24
#define ESP_GATT_UUID_SERIAL_NUMBER_STR 0x2A25
#define ESP_GATT_UUID_FW_VERSION_STR 0x2A26
#define ESP_GATT_UUID_HW_VERSION_STR 0x2A27
#define ESP_GATT_UUID_SW_VERSION_STR 0x2A28
#define ESP_GATT_UUID_MANU_NAME 0x2A29
#define ESP_GATT_UUID_IEEE_DATA 0x2A2A
#define ESP_GATT_UUID_PNP_ID 0x2A50
/* HID characteristics */
#define ESP_GATT_UUID_HID_INFORMATION 0x2A4A
#define ESP_GATT_UUID_HID_REPORT_MAP 0x2A4B
#define ESP_GATT_UUID_HID_CONTROL_POINT 0x2A4C
#define ESP_GATT_UUID_HID_REPORT 0x2A4D
#define ESP_GATT_UUID_HID_PROTO_MODE 0x2A4E
#define ESP_GATT_UUID_HID_BT_KB_INPUT 0x2A22
#define ESP_GATT_UUID_HID_BT_KB_OUTPUT 0x2A32
#define ESP_GATT_UUID_HID_BT_MOUSE_INPUT 0x2A33
/// Heart Rate Measurement
#define ESP_GATT_HEART_RATE_MEAS 0x2A37
/// Body Sensor Location
#define ESP_GATT_BODY_SENSOR_LOCATION 0x2A38
/// Heart Rate Control Point
#define ESP_GATT_HEART_RATE_CNTL_POINT 0x2A39
/* Battery Service characteristics */
#define ESP_GATT_UUID_BATTERY_LEVEL 0x2A19
/* Sensor Service */
#define ESP_GATT_UUID_SC_CONTROL_POINT 0x2A55
#define ESP_GATT_UUID_SENSOR_LOCATION 0x2A5D
/* Runners speed and cadence service */
#define ESP_GATT_UUID_RSC_MEASUREMENT 0x2A53
#define ESP_GATT_UUID_RSC_FEATURE 0x2A54
/* Cycling speed and cadence service */
#define ESP_GATT_UUID_CSC_MEASUREMENT 0x2A5B
#define ESP_GATT_UUID_CSC_FEATURE 0x2A5C
/* Scan ESP_Parameter characteristics */
#define ESP_GATT_UUID_SCAN_INT_WINDOW 0x2A4F
#define ESP_GATT_UUID_SCAN_REFRESH 0x2A31
/**
* @}
*/
/* relate to BTA_GATT_PREP_WRITE_xxx in bta/bta_gatt_api.h */
/// Attribute write data type from the client
typedef enum {
ESP_GATT_PREP_WRITE_CANCEL = 0x00, /*!< Prepare write cancel */ /* relate to BTA_GATT_PREP_WRITE_CANCEL in bta/bta_gatt_api.h */
ESP_GATT_PREP_WRITE_EXEC = 0x01, /*!< Prepare write execute */ /* relate to BTA_GATT_PREP_WRITE_EXEC in bta/bta_gatt_api.h */
} esp_gatt_prep_write_type;
/* relate to BTA_GATT_xxx in bta/bta_gatt_api.h */
/**
* @brief GATT success code and error codes
*/
typedef enum {
ESP_GATT_OK = 0x0, /* relate to BTA_GATT_OK in bta/bta_gatt_api.h */
ESP_GATT_INVALID_HANDLE = 0x01, /* 0x0001 */ /* relate to BTA_GATT_INVALID_HANDLE in bta/bta_gatt_api.h */
ESP_GATT_READ_NOT_PERMIT = 0x02, /* 0x0002 */ /* relate to BTA_GATT_READ_NOT_PERMIT in bta/bta_gatt_api.h */
ESP_GATT_WRITE_NOT_PERMIT = 0x03, /* 0x0003 */ /* relate to BTA_GATT_WRITE_NOT_PERMIT in bta/bta_gatt_api.h */
ESP_GATT_INVALID_PDU = 0x04, /* 0x0004 */ /* relate to BTA_GATT_INVALID_PDU in bta/bta_gatt_api.h */
ESP_GATT_INSUF_AUTHENTICATION = 0x05, /* 0x0005 */ /* relate to BTA_GATT_INSUF_AUTHENTICATION in bta/bta_gatt_api.h */
ESP_GATT_REQ_NOT_SUPPORTED = 0x06, /* 0x0006 */ /* relate to BTA_GATT_REQ_NOT_SUPPORTED in bta/bta_gatt_api.h */
ESP_GATT_INVALID_OFFSET = 0x07, /* 0x0007 */ /* relate to BTA_GATT_INVALID_OFFSET in bta/bta_gatt_api.h */
ESP_GATT_INSUF_AUTHORIZATION = 0x08, /* 0x0008 */ /* relate to BTA_GATT_INSUF_AUTHORIZATION in bta/bta_gatt_api.h */
ESP_GATT_PREPARE_Q_FULL = 0x09, /* 0x0009 */ /* relate to BTA_GATT_PREPARE_Q_FULL in bta/bta_gatt_api.h */
ESP_GATT_NOT_FOUND = 0x0a, /* 0x000a */ /* relate to BTA_GATT_NOT_FOUND in bta/bta_gatt_api.h */
ESP_GATT_NOT_LONG = 0x0b, /* 0x000b */ /* relate to BTA_GATT_NOT_LONG in bta/bta_gatt_api.h */
ESP_GATT_INSUF_KEY_SIZE = 0x0c, /* 0x000c */ /* relate to BTA_GATT_INSUF_KEY_SIZE in bta/bta_gatt_api.h */
ESP_GATT_INVALID_ATTR_LEN = 0x0d, /* 0x000d */ /* relate to BTA_GATT_INVALID_ATTR_LEN in bta/bta_gatt_api.h */
ESP_GATT_ERR_UNLIKELY = 0x0e, /* 0x000e */ /* relate to BTA_GATT_ERR_UNLIKELY in bta/bta_gatt_api.h */
ESP_GATT_INSUF_ENCRYPTION = 0x0f, /* 0x000f */ /* relate to BTA_GATT_INSUF_ENCRYPTION in bta/bta_gatt_api.h */
ESP_GATT_UNSUPPORT_GRP_TYPE = 0x10, /* 0x0010 */ /* relate to BTA_GATT_UNSUPPORT_GRP_TYPE in bta/bta_gatt_api.h */
ESP_GATT_INSUF_RESOURCE = 0x11, /* 0x0011 */ /* relate to BTA_GATT_INSUF_RESOURCE in bta/bta_gatt_api.h */
ESP_GATT_NO_RESOURCES = 0x80, /* 0x80 */ /* relate to BTA_GATT_NO_RESOURCES in bta/bta_gatt_api.h */
ESP_GATT_INTERNAL_ERROR = 0x81, /* 0x81 */ /* relate to BTA_GATT_INTERNAL_ERROR in bta/bta_gatt_api.h */
ESP_GATT_WRONG_STATE = 0x82, /* 0x82 */ /* relate to BTA_GATT_WRONG_STATE in bta/bta_gatt_api.h */
ESP_GATT_DB_FULL = 0x83, /* 0x83 */ /* relate to BTA_GATT_DB_FULL in bta/bta_gatt_api.h */
ESP_GATT_BUSY = 0x84, /* 0x84 */ /* relate to BTA_GATT_BUSY in bta/bta_gatt_api.h */
ESP_GATT_ERROR = 0x85, /* 0x85 */ /* relate to BTA_GATT_ERROR in bta/bta_gatt_api.h */
ESP_GATT_CMD_STARTED = 0x86, /* 0x86 */ /* relate to BTA_GATT_CMD_STARTED in bta/bta_gatt_api.h */
ESP_GATT_ILLEGAL_PARAMETER = 0x87, /* 0x87 */ /* relate to BTA_GATT_ILLEGAL_PARAMETER in bta/bta_gatt_api.h */
ESP_GATT_PENDING = 0x88, /* 0x88 */ /* relate to BTA_GATT_PENDING in bta/bta_gatt_api.h */
ESP_GATT_AUTH_FAIL = 0x89, /* 0x89 */ /* relate to BTA_GATT_AUTH_FAIL in bta/bta_gatt_api.h */
ESP_GATT_MORE = 0x8a, /* 0x8a */ /* relate to BTA_GATT_MORE in bta/bta_gatt_api.h */
ESP_GATT_INVALID_CFG = 0x8b, /* 0x8b */ /* relate to BTA_GATT_INVALID_CFG in bta/bta_gatt_api.h */
ESP_GATT_SERVICE_STARTED = 0x8c, /* 0x8c */ /* relate to BTA_GATT_SERVICE_STARTED in bta/bta_gatt_api.h */
ESP_GATT_ENCRYPED_MITM = ESP_GATT_OK, /* relate to BTA_GATT_ENCRYPED_MITM in bta/bta_gatt_api.h */
ESP_GATT_ENCRYPED_NO_MITM = 0x8d, /* 0x8d */ /* relate to BTA_GATT_ENCRYPED_NO_MITM in bta/bta_gatt_api.h */
ESP_GATT_NOT_ENCRYPTED = 0x8e, /* 0x8e */ /* relate to BTA_GATT_NOT_ENCRYPTED in bta/bta_gatt_api.h */
ESP_GATT_CONGESTED = 0x8f, /* 0x8f */ /* relate to BTA_GATT_CONGESTED in bta/bta_gatt_api.h */
ESP_GATT_DUP_REG = 0x90, /* 0x90 */ /* relate to BTA_GATT_DUP_REG in bta/bta_gatt_api.h */
ESP_GATT_ALREADY_OPEN = 0x91, /* 0x91 */ /* relate to BTA_GATT_ALREADY_OPEN in bta/bta_gatt_api.h */
ESP_GATT_CANCEL = 0x92, /* 0x92 */ /* relate to BTA_GATT_CANCEL in bta/bta_gatt_api.h */
/* 0xE0 ~ 0xFC reserved for future use */
ESP_GATT_STACK_RSP = 0xe0, /* 0xe0 */ /* relate to BTA_GATT_STACK_RSP in bta/bta_gatt_api.h */
ESP_GATT_APP_RSP = 0xe1, /* 0xe1 */ /* relate to BTA_GATT_APP_RSP in bta/bta_gatt_api.h */
//Error caused by customer application or stack bug
ESP_GATT_UNKNOWN_ERROR = 0xef, /* 0xef */ /* relate to BTA_GATT_UNKNOWN_ERROR in bta/bta_gatt_api.h */
ESP_GATT_CCC_CFG_ERR = 0xfd, /* 0xFD Client Characteristic Configuration Descriptor Improperly Configured */ /* relate to BTA_GATT_CCC_CFG_ERR in bta/bta_gatt_api.h */
ESP_GATT_PRC_IN_PROGRESS = 0xfe, /* 0xFE Procedure Already in progress */ /* relate to BTA_GATT_PRC_IN_PROGRESS in bta/bta_gatt_api.h */
ESP_GATT_OUT_OF_RANGE = 0xff, /* 0xFFAttribute value out of range */ /* relate to BTA_GATT_OUT_OF_RANGE in bta/bta_gatt_api.h */
} esp_gatt_status_t;
/* relate to BTA_GATT_CONN_xxx in bta/bta_gatt_api.h */
/**
* @brief Gatt Connection reason enum
*/
typedef enum {
ESP_GATT_CONN_UNKNOWN = 0, /*!< Gatt connection unknown */ /* relate to BTA_GATT_CONN_UNKNOWN in bta/bta_gatt_api.h */
ESP_GATT_CONN_L2C_FAILURE = 1, /*!< General L2cap failure */ /* relate to BTA_GATT_CONN_L2C_FAILURE in bta/bta_gatt_api.h */
ESP_GATT_CONN_TIMEOUT = 0x08, /*!< Connection timeout */ /* relate to BTA_GATT_CONN_TIMEOUT in bta/bta_gatt_api.h */
ESP_GATT_CONN_TERMINATE_PEER_USER = 0x13, /*!< Connection terminate by peer user */ /* relate to BTA_GATT_CONN_TERMINATE_PEER_USER in bta/bta_gatt_api.h */
ESP_GATT_CONN_TERMINATE_LOCAL_HOST = 0x16, /*!< Connection terminated by local host */ /* relate to BTA_GATT_CONN_TERMINATE_LOCAL_HOST in bta/bta_gatt_api.h */
ESP_GATT_CONN_FAIL_ESTABLISH = 0x3e, /*!< Connection fail to establish */ /* relate to BTA_GATT_CONN_FAIL_ESTABLISH in bta/bta_gatt_api.h */
ESP_GATT_CONN_LMP_TIMEOUT = 0x22, /*!< Connection fail for LMP response tout */ /* relate to BTA_GATT_CONN_LMP_TIMEOUT in bta/bta_gatt_api.h */
ESP_GATT_CONN_CONN_CANCEL = 0x0100, /*!< L2CAP connection cancelled */ /* relate to BTA_GATT_CONN_CONN_CANCEL in bta/bta_gatt_api.h */
ESP_GATT_CONN_NONE = 0x0101 /*!< No connection to cancel */ /* relate to BTA_GATT_CONN_NONE in bta/bta_gatt_api.h */
} esp_gatt_conn_reason_t;
/**
* @brief Gatt id, include uuid and instance id
*/
typedef struct {
esp_bt_uuid_t uuid; /*!< UUID */
uint8_t inst_id; /*!< Instance id */
} __attribute__((packed)) esp_gatt_id_t;
/**
* @brief Gatt service id, include id
* (uuid and instance id) and primary flag
*/
typedef struct {
esp_gatt_id_t id; /*!< Gatt id, include uuid and instance */
bool is_primary; /*!< This service is primary or not */
} __attribute__((packed)) esp_gatt_srvc_id_t;
/* relate to BTA_GATT_AUTH_REQ_xxx in bta/bta_gatt_api.h */
/**
* @brief Gatt authentication request type
*/
typedef enum {
ESP_GATT_AUTH_REQ_NONE = 0, /* relate to BTA_GATT_AUTH_REQ_NONE in bta/bta_gatt_api.h */
ESP_GATT_AUTH_REQ_NO_MITM = 1, /* unauthenticated encryption */ /* relate to BTA_GATT_AUTH_REQ_NO_MITM in bta/bta_gatt_api.h */
ESP_GATT_AUTH_REQ_MITM = 2, /* authenticated encryption */ /* relate to BTA_GATT_AUTH_REQ_MITM in bta/bta_gatt_api.h */
ESP_GATT_AUTH_REQ_SIGNED_NO_MITM = 3, /* relate to BTA_GATT_AUTH_REQ_SIGNED_NO_MITM in bta/bta_gatt_api.h */
ESP_GATT_AUTH_REQ_SIGNED_MITM = 4, /* relate to BTA_GATT_AUTH_REQ_SIGNED_MITM in bta/bta_gatt_api.h */
} esp_gatt_auth_req_t;
/* relate to BTA_GATT_PERM_xxx in bta/bta_gatt_api.h */
/**
* @brief Attribute permissions
*/
#define ESP_GATT_PERM_READ (1 << 0) /* bit 0 - 0x0001 */ /* relate to BTA_GATT_PERM_READ in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_ENCRYPTED (1 << 1) /* bit 1 - 0x0002 */ /* relate to BTA_GATT_PERM_READ_ENCRYPTED in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_ENC_MITM (1 << 2) /* bit 2 - 0x0004 */ /* relate to BTA_GATT_PERM_READ_ENC_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE (1 << 4) /* bit 4 - 0x0010 */ /* relate to BTA_GATT_PERM_WRITE in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_ENCRYPTED (1 << 5) /* bit 5 - 0x0020 */ /* relate to BTA_GATT_PERM_WRITE_ENCRYPTED in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_ENC_MITM (1 << 6) /* bit 6 - 0x0040 */ /* relate to BTA_GATT_PERM_WRITE_ENC_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_SIGNED (1 << 7) /* bit 7 - 0x0080 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_SIGNED_MITM (1 << 8) /* bit 8 - 0x0100 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_AUTHORIZATION (1 << 9) /* bit 9 - 0x0200 */
#define ESP_GATT_PERM_WRITE_AUTHORIZATION (1 << 10) /* bit 10 - 0x0400 */
typedef uint16_t esp_gatt_perm_t;
/* relate to BTA_GATT_CHAR_PROP_BIT_xxx in bta/bta_gatt_api.h */
/* definition of characteristic properties */
#define ESP_GATT_CHAR_PROP_BIT_BROADCAST (1 << 0) /* 0x01 */ /* relate to BTA_GATT_CHAR_PROP_BIT_BROADCAST in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_READ (1 << 1) /* 0x02 */ /* relate to BTA_GATT_CHAR_PROP_BIT_READ in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_WRITE_NR (1 << 2) /* 0x04 */ /* relate to BTA_GATT_CHAR_PROP_BIT_WRITE_NR in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_WRITE (1 << 3) /* 0x08 */ /* relate to BTA_GATT_CHAR_PROP_BIT_WRITE in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_NOTIFY (1 << 4) /* 0x10 */ /* relate to BTA_GATT_CHAR_PROP_BIT_NOTIFY in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_INDICATE (1 << 5) /* 0x20 */ /* relate to BTA_GATT_CHAR_PROP_BIT_INDICATE in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_AUTH (1 << 6) /* 0x40 */ /* relate to BTA_GATT_CHAR_PROP_BIT_AUTH in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_EXT_PROP (1 << 7) /* 0x80 */ /* relate to BTA_GATT_CHAR_PROP_BIT_EXT_PROP in bta/bta_gatt_api.h */
typedef uint8_t esp_gatt_char_prop_t;
/// GATT maximum attribute length
#define ESP_GATT_MAX_ATTR_LEN 600 //as same as GATT_MAX_ATTR_LEN
typedef enum {
ESP_GATT_SERVICE_FROM_REMOTE_DEVICE = 0, /* relate to BTA_GATTC_SERVICE_INFO_FROM_REMOTE_DEVICE in bta_gattc_int.h */
ESP_GATT_SERVICE_FROM_NVS_FLASH = 1, /* relate to BTA_GATTC_SERVICE_INFO_FROM_NVS_FLASH in bta_gattc_int.h */
ESP_GATT_SERVICE_FROM_UNKNOWN = 2, /* relate to BTA_GATTC_SERVICE_INFO_FROM_UNKNOWN in bta_gattc_int.h */
} esp_service_source_t;
/**
* @brief Attribute description (used to create database)
*/
typedef struct
{
uint16_t uuid_length; /*!< UUID length */
uint8_t *uuid_p; /*!< UUID value */
uint16_t perm; /*!< Attribute permission */
uint16_t max_length; /*!< Maximum length of the element*/
uint16_t length; /*!< Current length of the element*/
uint8_t *value; /*!< Element value array*/
} esp_attr_desc_t;
/**
* @brief attribute auto response flag
*/
typedef struct
{
#define ESP_GATT_RSP_BY_APP 0
#define ESP_GATT_AUTO_RSP 1
/**
* @brief if auto_rsp set to ESP_GATT_RSP_BY_APP, means the response of Write/Read operation will by replied by application.
if auto_rsp set to ESP_GATT_AUTO_RSP, means the response of Write/Read operation will be replied by GATT stack automatically.
*/
uint8_t auto_rsp;
} esp_attr_control_t;
/**
* @brief attribute type added to the gatt server database
*/
typedef struct
{
esp_attr_control_t attr_control; /*!< The attribute control type */
esp_attr_desc_t att_desc; /*!< The attribute type */
} esp_gatts_attr_db_t;
/**
* @brief set the attribute value type
*/
typedef struct
{
uint16_t attr_max_len; /*!< attribute max value length */
uint16_t attr_len; /*!< attribute current value length */
uint8_t *attr_value; /*!< the pointer to attribute value */
} esp_attr_value_t;
/**
* @brief Gatt include service entry element
*/
typedef struct
{
uint16_t start_hdl; /*!< Gatt start handle value of included service */
uint16_t end_hdl; /*!< Gatt end handle value of included service */
uint16_t uuid; /*!< Gatt attribute value UUID of included service */
} esp_gatts_incl_svc_desc_t; /*!< Gatt include service entry element */
/**
* @brief Gatt include 128 bit service entry element
*/
typedef struct
{
uint16_t start_hdl; /*!< Gatt start handle value of included 128 bit service */
uint16_t end_hdl; /*!< Gatt end handle value of included 128 bit service */
} esp_gatts_incl128_svc_desc_t; /*!< Gatt include 128 bit service entry element */
/// Gatt attribute value
typedef struct {
uint8_t value[ESP_GATT_MAX_ATTR_LEN]; /*!< Gatt attribute value */
uint16_t handle; /*!< Gatt attribute handle */
uint16_t offset; /*!< Gatt attribute value offset */
uint16_t len; /*!< Gatt attribute value length */
uint8_t auth_req; /*!< Gatt authentication request */
} esp_gatt_value_t;
/// GATT remote read request response type
typedef union {
esp_gatt_value_t attr_value; /*!< Gatt attribute structure */
uint16_t handle; /*!< Gatt attribute handle */
} esp_gatt_rsp_t;
/**
* @brief Gatt write type
*/
typedef enum {
ESP_GATT_WRITE_TYPE_NO_RSP = 1, /*!< Gatt write attribute need no response */
ESP_GATT_WRITE_TYPE_RSP, /*!< Gatt write attribute need remote response */
} esp_gatt_write_type_t;
/**
* @brief Connection parameters information
*/
typedef struct {
uint16_t interval; /*!< connection interval */
uint16_t latency; /*!< Slave latency for the connection in number of connection events. Range: 0x0000 to 0x01F3 */
uint16_t timeout; /*!< Supervision timeout for the LE Link. Range: 0x000A to 0x0C80.
Mandatory Range: 0x000A to 0x0C80 Time = N * 10 msec
Time Range: 100 msec to 32 seconds */
} esp_gatt_conn_params_t;
#define ESP_GATT_IF_NONE 0xff /*!< If callback report gattc_if/gatts_if as this macro, means this event is not correspond to any app */
typedef uint8_t esp_gatt_if_t; /*!< Gatt interface type, different application on GATT client use different gatt_if */
/**
* @brief the type of attribute element
*/
typedef enum {
ESP_GATT_DB_PRIMARY_SERVICE, /*!< Gattc primary service attribute type in the cache */
ESP_GATT_DB_SECONDARY_SERVICE, /*!< Gattc secondary service attribute type in the cache */
ESP_GATT_DB_CHARACTERISTIC, /*!< Gattc characteristic attribute type in the cache */
ESP_GATT_DB_DESCRIPTOR, /*!< Gattc characteristic descriptor attribute type in the cache */
ESP_GATT_DB_INCLUDED_SERVICE, /*!< Gattc include service attribute type in the cache */
ESP_GATT_DB_ALL, /*!< Gattc all the attribute (primary service & secondary service & include service & char & descriptor) type in the cache */
} esp_gatt_db_attr_type_t; /*!< Gattc attribute type element */
/**
* @brief read multiple attribute
*/
typedef struct {
uint8_t num_attr; /*!< The number of the attribute */
uint16_t handles[ESP_GATT_MAX_READ_MULTI_HANDLES]; /*!< The handles list */
} esp_gattc_multi_t; /*!< The gattc multiple read element */
/**
* @brief data base attribute element
*/
typedef struct {
esp_gatt_db_attr_type_t type; /*!< The attribute type */
uint16_t attribute_handle; /*!< The attribute handle, it's valid for all of the type */
uint16_t start_handle; /*!< The service start handle, it's valid only when the type = ESP_GATT_DB_PRIMARY_SERVICE or ESP_GATT_DB_SECONDARY_SERVICE */
uint16_t end_handle; /*!< The service end handle, it's valid only when the type = ESP_GATT_DB_PRIMARY_SERVICE or ESP_GATT_DB_SECONDARY_SERVICE */
esp_gatt_char_prop_t properties; /*!< The characteristic properties, it's valid only when the type = ESP_GATT_DB_CHARACTERISTIC */
esp_bt_uuid_t uuid; /*!< The attribute uuid, it's valid for all of the type */
} esp_gattc_db_elem_t; /*!< The gattc service data base element in the cache */
/**
* @brief service element
*/
typedef struct {
bool is_primary; /*!< The service flag, true if the service is primary service, else is secondary service */
uint16_t start_handle; /*!< The start handle of the service */
uint16_t end_handle; /*!< The end handle of the service */
esp_bt_uuid_t uuid; /*!< The uuid of the service */
} esp_gattc_service_elem_t; /*!< The gattc service element */
/**
* @brief characteristic element
*/
typedef struct {
uint16_t char_handle; /*!< The characteristic handle */
esp_gatt_char_prop_t properties; /*!< The characteristic properties */
esp_bt_uuid_t uuid; /*!< The characteristic uuid */
} esp_gattc_char_elem_t; /*!< The gattc characteristic element */
/**
* @brief descriptor element
*/
typedef struct {
uint16_t handle; /*!< The characteristic descriptor handle */
esp_bt_uuid_t uuid; /*!< The characteristic descriptor uuid */
} esp_gattc_descr_elem_t; /*!< The gattc descriptor type element */
/**
* @brief include service element
*/
typedef struct {
uint16_t handle; /*!< The include service current attribute handle */
uint16_t incl_srvc_s_handle; /*!< The start handle of the service which has been included */
uint16_t incl_srvc_e_handle; /*!< The end handle of the service which has been included */
esp_bt_uuid_t uuid; /*!< The include service uuid */
} esp_gattc_incl_svc_elem_t; /*!< The gattc include service element */
#ifdef __cplusplus
}
#endif
#endif /* __ESP_GATT_DEFS_H__ */

View File

@ -0,0 +1,891 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_GATTC_API_H__
#define __ESP_GATTC_API_H__
#include "esp_bt_defs.h"
#include "esp_gatt_defs.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/// GATT Client callback function events
typedef enum {
ESP_GATTC_REG_EVT = 0, /*!< When GATT client is registered, the event comes */
ESP_GATTC_UNREG_EVT = 1, /*!< When GATT client is unregistered, the event comes */
ESP_GATTC_OPEN_EVT = 2, /*!< When GATT virtual connection is set up, the event comes */
ESP_GATTC_READ_CHAR_EVT = 3, /*!< When GATT characteristic is read, the event comes */
ESP_GATTC_WRITE_CHAR_EVT = 4, /*!< When GATT characteristic write operation completes, the event comes */
ESP_GATTC_CLOSE_EVT = 5, /*!< When GATT virtual connection is closed, the event comes */
ESP_GATTC_SEARCH_CMPL_EVT = 6, /*!< When GATT service discovery is completed, the event comes */
ESP_GATTC_SEARCH_RES_EVT = 7, /*!< When GATT service discovery result is got, the event comes */
ESP_GATTC_READ_DESCR_EVT = 8, /*!< When GATT characteristic descriptor read completes, the event comes */
ESP_GATTC_WRITE_DESCR_EVT = 9, /*!< When GATT characteristic descriptor write completes, the event comes */
ESP_GATTC_NOTIFY_EVT = 10, /*!< When GATT notification or indication arrives, the event comes */
ESP_GATTC_PREP_WRITE_EVT = 11, /*!< When GATT prepare-write operation completes, the event comes */
ESP_GATTC_EXEC_EVT = 12, /*!< When write execution completes, the event comes */
ESP_GATTC_ACL_EVT = 13, /*!< When ACL connection is up, the event comes */
ESP_GATTC_CANCEL_OPEN_EVT = 14, /*!< When GATT client ongoing connection is cancelled, the event comes */
ESP_GATTC_SRVC_CHG_EVT = 15, /*!< When "service changed" occurs, the event comes */
ESP_GATTC_ENC_CMPL_CB_EVT = 17, /*!< When encryption procedure completes, the event comes */
ESP_GATTC_CFG_MTU_EVT = 18, /*!< When configuration of MTU completes, the event comes */
ESP_GATTC_ADV_DATA_EVT = 19, /*!< When advertising of data, the event comes */
ESP_GATTC_MULT_ADV_ENB_EVT = 20, /*!< When multi-advertising is enabled, the event comes */
ESP_GATTC_MULT_ADV_UPD_EVT = 21, /*!< When multi-advertising parameters are updated, the event comes */
ESP_GATTC_MULT_ADV_DATA_EVT = 22, /*!< When multi-advertising data arrives, the event comes */
ESP_GATTC_MULT_ADV_DIS_EVT = 23, /*!< When multi-advertising is disabled, the event comes */
ESP_GATTC_CONGEST_EVT = 24, /*!< When GATT connection congestion comes, the event comes */
ESP_GATTC_BTH_SCAN_ENB_EVT = 25, /*!< When batch scan is enabled, the event comes */
ESP_GATTC_BTH_SCAN_CFG_EVT = 26, /*!< When batch scan storage is configured, the event comes */
ESP_GATTC_BTH_SCAN_RD_EVT = 27, /*!< When Batch scan read event is reported, the event comes */
ESP_GATTC_BTH_SCAN_THR_EVT = 28, /*!< When Batch scan threshold is set, the event comes */
ESP_GATTC_BTH_SCAN_PARAM_EVT = 29, /*!< When Batch scan parameters are set, the event comes */
ESP_GATTC_BTH_SCAN_DIS_EVT = 30, /*!< When Batch scan is disabled, the event comes */
ESP_GATTC_SCAN_FLT_CFG_EVT = 31, /*!< When Scan filter configuration completes, the event comes */
ESP_GATTC_SCAN_FLT_PARAM_EVT = 32, /*!< When Scan filter parameters are set, the event comes */
ESP_GATTC_SCAN_FLT_STATUS_EVT = 33, /*!< When Scan filter status is reported, the event comes */
ESP_GATTC_ADV_VSC_EVT = 34, /*!< When advertising vendor spec content event is reported, the event comes */
ESP_GATTC_REG_FOR_NOTIFY_EVT = 38, /*!< When register for notification of a service completes, the event comes */
ESP_GATTC_UNREG_FOR_NOTIFY_EVT = 39, /*!< When unregister for notification of a service completes, the event comes */
ESP_GATTC_CONNECT_EVT = 40, /*!< When the ble physical connection is set up, the event comes */
ESP_GATTC_DISCONNECT_EVT = 41, /*!< When the ble physical connection disconnected, the event comes */
ESP_GATTC_READ_MULTIPLE_EVT = 42, /*!< When the ble characteristic or descriptor multiple complete, the event comes */
ESP_GATTC_QUEUE_FULL_EVT = 43, /*!< When the gattc command queue full, the event comes */
ESP_GATTC_SET_ASSOC_EVT = 44, /*!< When the ble gattc set the associated address complete, the event comes */
ESP_GATTC_GET_ADDR_LIST_EVT = 45, /*!< When the ble get gattc address list in cache finish, the event comes */
ESP_GATTC_DIS_SRVC_CMPL_EVT = 46, /*!< When the ble discover service complete, the event comes */
} esp_gattc_cb_event_t;
/**
* @brief Gatt client callback parameters union
*/
typedef union {
/**
* @brief ESP_GATTC_REG_EVT
*/
struct gattc_reg_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t app_id; /*!< Application id which input in register API */
} reg; /*!< Gatt client callback param of ESP_GATTC_REG_EVT */
/**
* @brief ESP_GATTC_OPEN_EVT
*/
struct gattc_open_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
uint16_t mtu; /*!< MTU size */
} open; /*!< Gatt client callback param of ESP_GATTC_OPEN_EVT */
/**
* @brief ESP_GATTC_CLOSE_EVT
*/
struct gattc_close_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_gatt_conn_reason_t reason; /*!< The reason of gatt connection close */
} close; /*!< Gatt client callback param of ESP_GATTC_CLOSE_EVT */
/**
* @brief ESP_GATTC_CFG_MTU_EVT
*/
struct gattc_cfg_mtu_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
uint16_t mtu; /*!< MTU size */
} cfg_mtu; /*!< Gatt client callback param of ESP_GATTC_CFG_MTU_EVT */
/**
* @brief ESP_GATTC_SEARCH_CMPL_EVT
*/
struct gattc_search_cmpl_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
esp_service_source_t searched_service_source; /*!< The source of the service information */
} search_cmpl; /*!< Gatt client callback param of ESP_GATTC_SEARCH_CMPL_EVT */
/**
* @brief ESP_GATTC_SEARCH_RES_EVT
*/
struct gattc_search_res_evt_param {
uint16_t conn_id; /*!< Connection id */
uint16_t start_handle; /*!< Service start handle */
uint16_t end_handle; /*!< Service end handle */
esp_gatt_id_t srvc_id; /*!< Service id, include service uuid and other information */
bool is_primary; /*!< True if this is the primary service */
} search_res; /*!< Gatt client callback param of ESP_GATTC_SEARCH_RES_EVT */
/**
* @brief ESP_GATTC_READ_CHAR_EVT, ESP_GATTC_READ_DESCR_EVT
*/
struct gattc_read_char_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
uint16_t handle; /*!< Characteristic handle */
uint8_t *value; /*!< Characteristic value */
uint16_t value_len; /*!< Characteristic value length */
} read; /*!< Gatt client callback param of ESP_GATTC_READ_CHAR_EVT */
/**
* @brief ESP_GATTC_WRITE_CHAR_EVT, ESP_GATTC_PREP_WRITE_EVT, ESP_GATTC_WRITE_DESCR_EVT
*/
struct gattc_write_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
uint16_t handle; /*!< The Characteristic or descriptor handle */
uint16_t offset; /*!< The prepare write offset, this value is valid only when prepare write */
} write; /*!< Gatt client callback param of ESP_GATTC_WRITE_DESCR_EVT */
/**
* @brief ESP_GATTC_EXEC_EVT
*/
struct gattc_exec_cmpl_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
} exec_cmpl; /*!< Gatt client callback param of ESP_GATTC_EXEC_EVT */
/**
* @brief ESP_GATTC_NOTIFY_EVT
*/
struct gattc_notify_evt_param {
uint16_t conn_id; /*!< Connection id */
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
uint16_t handle; /*!< The Characteristic or descriptor handle */
uint16_t value_len; /*!< Notify attribute value */
uint8_t *value; /*!< Notify attribute value */
bool is_notify; /*!< True means notify, false means indicate */
} notify; /*!< Gatt client callback param of ESP_GATTC_NOTIFY_EVT */
/**
* @brief ESP_GATTC_SRVC_CHG_EVT
*/
struct gattc_srvc_chg_evt_param {
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
} srvc_chg; /*!< Gatt client callback param of ESP_GATTC_SRVC_CHG_EVT */
/**
* @brief ESP_GATTC_CONGEST_EVT
*/
struct gattc_congest_evt_param {
uint16_t conn_id; /*!< Connection id */
bool congested; /*!< Congested or not */
} congest; /*!< Gatt client callback param of ESP_GATTC_CONGEST_EVT */
/**
* @brief ESP_GATTC_REG_FOR_NOTIFY_EVT
*/
struct gattc_reg_for_notify_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t handle; /*!< The characteristic or descriptor handle */
} reg_for_notify; /*!< Gatt client callback param of ESP_GATTC_REG_FOR_NOTIFY_EVT */
/**
* @brief ESP_GATTC_UNREG_FOR_NOTIFY_EVT
*/
struct gattc_unreg_for_notify_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t handle; /*!< The characteristic or descriptor handle */
} unreg_for_notify; /*!< Gatt client callback param of ESP_GATTC_UNREG_FOR_NOTIFY_EVT */
/**
* @brief ESP_GATTC_CONNECT_EVT
*/
struct gattc_connect_evt_param {
uint16_t conn_id; /*!< Connection id */
uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_gatt_conn_params_t conn_params; /*!< current connection parameters */
} connect; /*!< Gatt client callback param of ESP_GATTC_CONNECT_EVT */
/**
* @brief ESP_GATTC_DISCONNECT_EVT
*/
struct gattc_disconnect_evt_param {
esp_gatt_conn_reason_t reason; /*!< disconnection reason */
uint16_t conn_id; /*!< Connection id */
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
} disconnect; /*!< Gatt client callback param of ESP_GATTC_DISCONNECT_EVT */
/**
* @brief ESP_GATTC_SET_ASSOC_EVT
*/
struct gattc_set_assoc_addr_cmp_evt_param {
esp_gatt_status_t status; /*!< Operation status */
} set_assoc_cmp; /*!< Gatt client callback param of ESP_GATTC_SET_ASSOC_EVT */
/**
* @brief ESP_GATTC_GET_ADDR_LIST_EVT
*/
struct gattc_get_addr_list_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint8_t num_addr; /*!< The number of address in the gattc cache address list */
esp_bd_addr_t *addr_list; /*!< The pointer to the address list which has been get from the gattc cache */
} get_addr_list; /*!< Gatt client callback param of ESP_GATTC_GET_ADDR_LIST_EVT */
/**
* @brief ESP_GATTC_QUEUE_FULL_EVT
*/
struct gattc_queue_full_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
bool is_full; /*!< The gattc command queue is full or not */
} queue_full; /*!< Gatt client callback param of ESP_GATTC_QUEUE_FULL_EVT */
/**
* @brief ESP_GATTC_DIS_SRVC_CMPL_EVT
*/
struct gattc_dis_srvc_cmpl_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
} dis_srvc_cmpl; /*!< Gatt client callback param of ESP_GATTC_DIS_SRVC_CMPL_EVT */
} esp_ble_gattc_cb_param_t; /*!< GATT client callback parameter union type */
/**
* @brief GATT Client callback function type
* @param event : Event type
* @param gatts_if : GATT client access interface, normally
* different gattc_if correspond to different profile
* @param param : Point to callback parameter, currently is union type
*/
typedef void (* esp_gattc_cb_t)(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
/**
* @brief This function is called to register application callbacks
* with GATTC module.
*
* @param[in] callback : pointer to the application callback function.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_register_callback(esp_gattc_cb_t callback);
/**
* @brief This function is called to register application callbacks
* with GATTC module.
*
* @param[in] app_id : Application Identify (UUID), for different application
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_app_register(uint16_t app_id);
/**
* @brief This function is called to unregister an application
* from GATTC module.
*
* @param[in] gattc_if: Gatt client access interface.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_app_unregister(esp_gatt_if_t gattc_if);
#if (BLE_42_FEATURE_SUPPORT == TRUE)
/**
* @brief Open a direct connection or add a background auto connection
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] remote_bda: remote device bluetooth device address.
* @param[in] remote_addr_type: remote device bluetooth device the address type.
* @param[in] is_direct: direct connection or background auto connection(by now, background auto connection is not supported).
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct);
#endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
#if (BLE_50_FEATURE_SUPPORT == TRUE)
esp_err_t esp_ble_gattc_aux_open(esp_gatt_if_t gattc_if, esp_bd_addr_t remote_bda, esp_ble_addr_type_t remote_addr_type, bool is_direct);
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
/**
* @brief Close the virtual connection to the GATT server. gattc may have multiple virtual GATT server connections when multiple app_id registered,
* this API only close one virtual GATT server connection. if there exist other virtual GATT server connections,
* it does not disconnect the physical connection.
* if you want to disconnect the physical connection directly, you can use esp_ble_gap_disconnect(esp_bd_addr_t remote_device).
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID to be closed.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_close (esp_gatt_if_t gattc_if, uint16_t conn_id);
/**
* @brief Configure the MTU size in the GATT channel. This can be done
* only once per connection. Before using, use esp_ble_gatt_set_local_mtu()
* to configure the local MTU size.
*
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_send_mtu_req (esp_gatt_if_t gattc_if, uint16_t conn_id);
/**
* @brief This function is called to get service from local cache.
* This function report service search result by a callback
* event, and followed by a service search complete event.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID.
* @param[in] filter_uuid: a UUID of the service application is interested in.
* If Null, discover for all services.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *filter_uuid);
/**
* @brief Find all the service with the given service uuid in the gattc cache, if the svc_uuid is NULL, find all the service.
* Note: It just get service from local cache, won't get from remote devices. If want to get it from remote device, need
* to used the esp_ble_gattc_search_service.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] svc_uuid: the pointer to the service uuid.
* @param[out] result: The pointer to the service which has been found in the gattc cache.
* @param[inout] count: input the number of service want to find,
* it will output the number of service has been found in the gattc cache with the given service uuid.
* @param[in] offset: Offset of the service position to get.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_service(esp_gatt_if_t gattc_if, uint16_t conn_id, esp_bt_uuid_t *svc_uuid,
esp_gattc_service_elem_t *result, uint16_t *count, uint16_t offset);
/**
* @brief Find all the characteristic with the given service in the gattc cache
* Note: It just get characteristic from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] start_handle: the attribute start handle.
* @param[in] end_handle: the attribute end handle
* @param[out] result: The pointer to the characteristic in the service.
* @param[inout] count: input the number of characteristic want to find,
* it will output the number of characteristic has been found in the gattc cache with the given service.
* @param[in] offset: Offset of the characteristic position to get.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_all_char(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t start_handle,
uint16_t end_handle,
esp_gattc_char_elem_t *result,
uint16_t *count, uint16_t offset);
/**
* @brief Find all the descriptor with the given characteristic in the gattc cache
* Note: It just get descriptor from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] char_handle: the given characteristic handle
* @param[out] result: The pointer to the descriptor in the characteristic.
* @param[inout] count: input the number of descriptor want to find,
* it will output the number of descriptor has been found in the gattc cache with the given characteristic.
* @param[in] offset: Offset of the descriptor position to get.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_all_descr(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t char_handle,
esp_gattc_descr_elem_t *result,
uint16_t *count, uint16_t offset);
/**
* @brief Find the characteristic with the given characteristic uuid in the gattc cache
* Note: It just get characteristic from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] start_handle: the attribute start handle
* @param[in] end_handle: the attribute end handle
* @param[in] char_uuid: the characteristic uuid
* @param[out] result: The pointer to the characteristic in the service.
* @param[inout] count: input the number of characteristic want to find,
* it will output the number of characteristic has been found in the gattc cache with the given service.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_char_by_uuid(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t start_handle,
uint16_t end_handle,
esp_bt_uuid_t char_uuid,
esp_gattc_char_elem_t *result,
uint16_t *count);
/**
* @brief Find the descriptor with the given characteristic uuid in the gattc cache
* Note: It just get descriptor from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] start_handle: the attribute start handle
* @param[in] end_handle: the attribute end handle
* @param[in] char_uuid: the characteristic uuid.
* @param[in] descr_uuid: the descriptor uuid.
* @param[out] result: The pointer to the descriptor in the given characteristic.
* @param[inout] count: input the number of descriptor want to find,
* it will output the number of descriptor has been found in the gattc cache with the given characteristic.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_descr_by_uuid(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t start_handle,
uint16_t end_handle,
esp_bt_uuid_t char_uuid,
esp_bt_uuid_t descr_uuid,
esp_gattc_descr_elem_t *result,
uint16_t *count);
/**
* @brief Find the descriptor with the given characteristic handle in the gattc cache
* Note: It just get descriptor from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] char_handle: the characteristic handle.
* @param[in] descr_uuid: the descriptor uuid.
* @param[out] result: The pointer to the descriptor in the given characteristic.
* @param[inout] count: input the number of descriptor want to find,
* it will output the number of descriptor has been found in the gattc cache with the given characteristic.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_descr_by_char_handle(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t char_handle,
esp_bt_uuid_t descr_uuid,
esp_gattc_descr_elem_t *result,
uint16_t *count);
/**
* @brief Find the include service with the given service handle in the gattc cache
* Note: It just get include service from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] start_handle: the attribute start handle
* @param[in] end_handle: the attribute end handle
* @param[in] incl_uuid: the include service uuid
* @param[out] result: The pointer to the include service in the given service.
* @param[inout] count: input the number of include service want to find,
* it will output the number of include service has been found in the gattc cache with the given service.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_include_service(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t start_handle,
uint16_t end_handle,
esp_bt_uuid_t *incl_uuid,
esp_gattc_incl_svc_elem_t *result,
uint16_t *count);
/**
* @brief Find the attribute count with the given service or characteristic in the gattc cache
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.
* @param[in] type: the attribute type.
* @param[in] start_handle: the attribute start handle, if the type is ESP_GATT_DB_DESCRIPTOR, this parameter should be ignore
* @param[in] end_handle: the attribute end handle, if the type is ESP_GATT_DB_DESCRIPTOR, this parameter should be ignore
* @param[in] char_handle: the characteristic handle, this parameter valid when the type is ESP_GATT_DB_DESCRIPTOR. If the type
* isn't ESP_GATT_DB_DESCRIPTOR, this parameter should be ignore.
* @param[out] count: output the number of attribute has been found in the gattc cache with the given attribute type.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_attr_count(esp_gatt_if_t gattc_if,
uint16_t conn_id,
esp_gatt_db_attr_type_t type,
uint16_t start_handle,
uint16_t end_handle,
uint16_t char_handle,
uint16_t *count);
/**
* @brief This function is called to get the GATT database.
* Note: It just get attribute data base from local cache, won't get from remote devices.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] start_handle: the attribute start handle
* @param[in] end_handle: the attribute end handle
* @param[in] conn_id: connection ID which identify the server.
* @param[in] db: output parameter which will contain the GATT database copy.
* Caller is responsible for freeing it.
* @param[in] count: number of elements in database.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_gatt_status_t esp_ble_gattc_get_db(esp_gatt_if_t gattc_if, uint16_t conn_id, uint16_t start_handle, uint16_t end_handle,
esp_gattc_db_elem_t *db, uint16_t *count);
/**
* @brief This function is called to read a service's characteristics of
* the given characteristic handle
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] handle : characteritic handle to read.
* @param[in] auth_req : authenticate request type
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_read_char (esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t handle,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to read a service's characteristics of
* the given characteristic UUID
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] start_handle : the attribute start handle.
* @param[in] end_handle : the attribute end handle
* @param[in] uuid : The UUID of attribute which will be read.
* @param[in] auth_req : authenticate request type
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_read_by_type (esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t start_handle,
uint16_t end_handle,
esp_bt_uuid_t *uuid,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to read multiple characteristic or
* characteristic descriptors.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] read_multi : pointer to the read multiple parameter.
* @param[in] auth_req : authenticate request type
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_read_multiple(esp_gatt_if_t gattc_if,
uint16_t conn_id, esp_gattc_multi_t *read_multi,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to read a characteristics descriptor.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] handle : descriptor handle to read.
* @param[in] auth_req : authenticate request type
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_read_char_descr (esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t handle,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to write characteristic value.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] handle : characteristic handle to write.
* @param[in] value_len: length of the value to be written.
* @param[in] value : the value to be written.
* @param[in] write_type : the type of attribute write operation.
* @param[in] auth_req : authentication request.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_write_char( esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t handle,
uint16_t value_len,
uint8_t *value,
esp_gatt_write_type_t write_type,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to write characteristic descriptor value.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID
* @param[in] handle : descriptor hadle to write.
* @param[in] value_len: length of the value to be written.
* @param[in] value : the value to be written.
* @param[in] write_type : the type of attribute write operation.
* @param[in] auth_req : authentication request.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_write_char_descr (esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t handle,
uint16_t value_len,
uint8_t *value,
esp_gatt_write_type_t write_type,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to prepare write a characteristic value.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] handle : characteristic handle to prepare write.
* @param[in] offset : offset of the write value.
* @param[in] value_len: length of the value to be written.
* @param[in] value : the value to be written.
* @param[in] auth_req : authentication request.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_prepare_write(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t handle,
uint16_t offset,
uint16_t value_len,
uint8_t *value,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to prepare write a characteristic descriptor value.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] handle : characteristic descriptor handle to prepare write.
* @param[in] offset : offset of the write value.
* @param[in] value_len: length of the value to be written.
* @param[in] value : the value to be written.
* @param[in] auth_req : authentication request.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_prepare_write_char_descr(esp_gatt_if_t gattc_if,
uint16_t conn_id,
uint16_t handle,
uint16_t offset,
uint16_t value_len,
uint8_t *value,
esp_gatt_auth_req_t auth_req);
/**
* @brief This function is called to execute write a prepare write sequence.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id : connection ID.
* @param[in] is_execute : execute or cancel.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_execute_write (esp_gatt_if_t gattc_if, uint16_t conn_id, bool is_execute);
/**
* @brief This function is called to register for notification of a service.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] server_bda : target GATT server.
* @param[in] handle : GATT characteristic handle.
*
* @return
* - ESP_OK: registration succeeds
* - other: failed
*
*/
esp_err_t esp_ble_gattc_register_for_notify (esp_gatt_if_t gattc_if,
esp_bd_addr_t server_bda,
uint16_t handle);
/**
* @brief This function is called to de-register for notification of a service.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] server_bda : target GATT server.
* @param[in] handle : GATT characteristic handle.
*
* @return
* - ESP_OK: unregister succeeds
* - other: failed
*
*/
esp_err_t esp_ble_gattc_unregister_for_notify (esp_gatt_if_t gattc_if,
esp_bd_addr_t server_bda,
uint16_t handle);
/**
* @brief Refresh the server cache store in the gattc stack of the remote device. If
* the device is connected, this API will restart the discovery of service information of the remote device
*
* @param[in] remote_bda: remote device BD address.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_cache_refresh(esp_bd_addr_t remote_bda);
/**
* @brief Add or delete the associated address with the source address.
* Note: The role of this API is mainly when the client side has stored a server-side database,
* when it needs to connect another device, but the device's attribute database is the same
* as the server database stored on the client-side, calling this API can use the database
* that the device has stored used as the peer server database to reduce the attribute
* database search and discovery process and speed up the connection time.
* The associated address mains that device want to used the database has stored in the local cache.
* The source address mains that device want to share the database to the associated address device.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] src_addr: the source address which provide the attribute table.
* @param[in] assoc_addr: the associated device address which went to share the attribute table with the source address.
* @param[in] is_assoc: true add the associated device address, false remove the associated device address.
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_cache_assoc(esp_gatt_if_t gattc_if, esp_bd_addr_t src_addr,
esp_bd_addr_t assoc_addr, bool is_assoc);
/**
* @brief Get the address list which has store the attribute table in the gattc cache. There will
* callback ESP_GATTC_GET_ADDR_LIST_EVT event when get address list complete.
*
* @param[in] gattc_if: Gatt client access interface.
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_cache_get_addr_list(esp_gatt_if_t gattc_if);
/**
* @brief Clean the service cache of this device in the gattc stack,
*
* @param[in] remote_bda: remote device BD address.
*
* @return
* - ESP_OK: success
* - other: failed
*
*/
esp_err_t esp_ble_gattc_cache_clean(esp_bd_addr_t remote_bda);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_GATTC_API_H__ */

View File

@ -0,0 +1,585 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_GATTS_API_H__
#define __ESP_GATTS_API_H__
#include "esp_bt_defs.h"
#include "esp_gatt_defs.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/// GATT Server callback function events
typedef enum {
ESP_GATTS_REG_EVT = 0, /*!< When register application id, the event comes */
ESP_GATTS_READ_EVT = 1, /*!< When gatt client request read operation, the event comes */
ESP_GATTS_WRITE_EVT = 2, /*!< When gatt client request write operation, the event comes */
ESP_GATTS_EXEC_WRITE_EVT = 3, /*!< When gatt client request execute write, the event comes */
ESP_GATTS_MTU_EVT = 4, /*!< When set mtu complete, the event comes */
ESP_GATTS_CONF_EVT = 5, /*!< When receive confirm, the event comes */
ESP_GATTS_UNREG_EVT = 6, /*!< When unregister application id, the event comes */
ESP_GATTS_CREATE_EVT = 7, /*!< When create service complete, the event comes */
ESP_GATTS_ADD_INCL_SRVC_EVT = 8, /*!< When add included service complete, the event comes */
ESP_GATTS_ADD_CHAR_EVT = 9, /*!< When add characteristic complete, the event comes */
ESP_GATTS_ADD_CHAR_DESCR_EVT = 10, /*!< When add descriptor complete, the event comes */
ESP_GATTS_DELETE_EVT = 11, /*!< When delete service complete, the event comes */
ESP_GATTS_START_EVT = 12, /*!< When start service complete, the event comes */
ESP_GATTS_STOP_EVT = 13, /*!< When stop service complete, the event comes */
ESP_GATTS_CONNECT_EVT = 14, /*!< When gatt client connect, the event comes */
ESP_GATTS_DISCONNECT_EVT = 15, /*!< When gatt client disconnect, the event comes */
ESP_GATTS_OPEN_EVT = 16, /*!< When connect to peer, the event comes */
ESP_GATTS_CANCEL_OPEN_EVT = 17, /*!< When disconnect from peer, the event comes */
ESP_GATTS_CLOSE_EVT = 18, /*!< When gatt server close, the event comes */
ESP_GATTS_LISTEN_EVT = 19, /*!< When gatt listen to be connected the event comes */
ESP_GATTS_CONGEST_EVT = 20, /*!< When congest happen, the event comes */
/* following is extra event */
ESP_GATTS_RESPONSE_EVT = 21, /*!< When gatt send response complete, the event comes */
ESP_GATTS_CREAT_ATTR_TAB_EVT = 22, /*!< When gatt create table complete, the event comes */
ESP_GATTS_SET_ATTR_VAL_EVT = 23, /*!< When gatt set attr value complete, the event comes */
ESP_GATTS_SEND_SERVICE_CHANGE_EVT = 24, /*!< When gatt send service change indication complete, the event comes */
} esp_gatts_cb_event_t;
/**
* @brief Gatt server callback parameters union
*/
typedef union {
/**
* @brief ESP_GATTS_REG_EVT
*/
struct gatts_reg_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t app_id; /*!< Application id which input in register API */
} reg; /*!< Gatt server callback param of ESP_GATTS_REG_EVT */
/**
* @brief ESP_GATTS_READ_EVT
*/
struct gatts_read_evt_param {
uint16_t conn_id; /*!< Connection id */
uint32_t trans_id; /*!< Transfer id */
esp_bd_addr_t bda; /*!< The bluetooth device address which been read */
uint16_t handle; /*!< The attribute handle */
uint16_t offset; /*!< Offset of the value, if the value is too long */
bool is_long; /*!< The value is too long or not */
bool need_rsp; /*!< The read operation need to do response */
} read; /*!< Gatt server callback param of ESP_GATTS_READ_EVT */
/**
* @brief ESP_GATTS_WRITE_EVT
*/
struct gatts_write_evt_param {
uint16_t conn_id; /*!< Connection id */
uint32_t trans_id; /*!< Transfer id */
esp_bd_addr_t bda; /*!< The bluetooth device address which been written */
uint16_t handle; /*!< The attribute handle */
uint16_t offset; /*!< Offset of the value, if the value is too long */
bool need_rsp; /*!< The write operation need to do response */
bool is_prep; /*!< This write operation is prepare write */
uint16_t len; /*!< The write attribute value length */
uint8_t *value; /*!< The write attribute value */
} write; /*!< Gatt server callback param of ESP_GATTS_WRITE_EVT */
/**
* @brief ESP_GATTS_EXEC_WRITE_EVT
*/
struct gatts_exec_write_evt_param {
uint16_t conn_id; /*!< Connection id */
uint32_t trans_id; /*!< Transfer id */
esp_bd_addr_t bda; /*!< The bluetooth device address which been written */
#define ESP_GATT_PREP_WRITE_CANCEL 0x00 /*!< Prepare write flag to indicate cancel prepare write */
#define ESP_GATT_PREP_WRITE_EXEC 0x01 /*!< Prepare write flag to indicate execute prepare write */
uint8_t exec_write_flag; /*!< Execute write flag */
} exec_write; /*!< Gatt server callback param of ESP_GATTS_EXEC_WRITE_EVT */
/**
* @brief ESP_GATTS_MTU_EVT
*/
struct gatts_mtu_evt_param {
uint16_t conn_id; /*!< Connection id */
uint16_t mtu; /*!< MTU size */
} mtu; /*!< Gatt server callback param of ESP_GATTS_MTU_EVT */
/**
* @brief ESP_GATTS_CONF_EVT
*/
struct gatts_conf_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
uint16_t handle; /*!< attribute handle */
uint16_t len; /*!< The indication or notification value length, len is valid when send notification or indication failed */
uint8_t *value; /*!< The indication or notification value , value is valid when send notification or indication failed */
} conf; /*!< Gatt server callback param of ESP_GATTS_CONF_EVT (confirm) */
/**
* @brief ESP_GATTS_UNREG_EVT
*/
/**
* @brief ESP_GATTS_CREATE_EVT
*/
struct gatts_create_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t service_handle; /*!< Service attribute handle */
esp_gatt_srvc_id_t service_id; /*!< Service id, include service uuid and other information */
} create; /*!< Gatt server callback param of ESP_GATTS_CREATE_EVT */
/**
* @brief ESP_GATTS_ADD_INCL_SRVC_EVT
*/
struct gatts_add_incl_srvc_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t attr_handle; /*!< Included service attribute handle */
uint16_t service_handle; /*!< Service attribute handle */
} add_incl_srvc; /*!< Gatt server callback param of ESP_GATTS_ADD_INCL_SRVC_EVT */
/**
* @brief ESP_GATTS_ADD_CHAR_EVT
*/
struct gatts_add_char_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t attr_handle; /*!< Characteristic attribute handle */
uint16_t service_handle; /*!< Service attribute handle */
esp_bt_uuid_t char_uuid; /*!< Characteristic uuid */
} add_char; /*!< Gatt server callback param of ESP_GATTS_ADD_CHAR_EVT */
/**
* @brief ESP_GATTS_ADD_CHAR_DESCR_EVT
*/
struct gatts_add_char_descr_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t attr_handle; /*!< Descriptor attribute handle */
uint16_t service_handle; /*!< Service attribute handle */
esp_bt_uuid_t descr_uuid; /*!< Characteristic descriptor uuid */
} add_char_descr; /*!< Gatt server callback param of ESP_GATTS_ADD_CHAR_DESCR_EVT */
/**
* @brief ESP_GATTS_DELETE_EVT
*/
struct gatts_delete_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t service_handle; /*!< Service attribute handle */
} del; /*!< Gatt server callback param of ESP_GATTS_DELETE_EVT */
/**
* @brief ESP_GATTS_START_EVT
*/
struct gatts_start_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t service_handle; /*!< Service attribute handle */
} start; /*!< Gatt server callback param of ESP_GATTS_START_EVT */
/**
* @brief ESP_GATTS_STOP_EVT
*/
struct gatts_stop_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t service_handle; /*!< Service attribute handle */
} stop; /*!< Gatt server callback param of ESP_GATTS_STOP_EVT */
/**
* @brief ESP_GATTS_CONNECT_EVT
*/
struct gatts_connect_evt_param {
uint16_t conn_id; /*!< Connection id */
uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_gatt_conn_params_t conn_params; /*!< current Connection parameters */
} connect; /*!< Gatt server callback param of ESP_GATTS_CONNECT_EVT */
/**
* @brief ESP_GATTS_DISCONNECT_EVT
*/
struct gatts_disconnect_evt_param {
uint16_t conn_id; /*!< Connection id */
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_gatt_conn_reason_t reason; /*!< Indicate the reason of disconnection */
} disconnect; /*!< Gatt server callback param of ESP_GATTS_DISCONNECT_EVT */
/**
* @brief ESP_GATTS_OPEN_EVT
*/
struct gatts_open_evt_param {
esp_gatt_status_t status; /*!< Operation status */
} open; /*!< Gatt server callback param of ESP_GATTS_OPEN_EVT */
/**
* @brief ESP_GATTS_CANCEL_OPEN_EVT
*/
struct gatts_cancel_open_evt_param {
esp_gatt_status_t status; /*!< Operation status */
} cancel_open; /*!< Gatt server callback param of ESP_GATTS_CANCEL_OPEN_EVT */
/**
* @brief ESP_GATTS_CLOSE_EVT
*/
struct gatts_close_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t conn_id; /*!< Connection id */
} close; /*!< Gatt server callback param of ESP_GATTS_CLOSE_EVT */
/**
* @brief ESP_GATTS_LISTEN_EVT
*/
/**
* @brief ESP_GATTS_CONGEST_EVT
*/
struct gatts_congest_evt_param {
uint16_t conn_id; /*!< Connection id */
bool congested; /*!< Congested or not */
} congest; /*!< Gatt server callback param of ESP_GATTS_CONGEST_EVT */
/**
* @brief ESP_GATTS_RESPONSE_EVT
*/
struct gatts_rsp_evt_param {
esp_gatt_status_t status; /*!< Operation status */
uint16_t handle; /*!< Attribute handle which send response */
} rsp; /*!< Gatt server callback param of ESP_GATTS_RESPONSE_EVT */
/**
* @brief ESP_GATTS_CREAT_ATTR_TAB_EVT
*/
struct gatts_add_attr_tab_evt_param{
esp_gatt_status_t status; /*!< Operation status */
esp_bt_uuid_t svc_uuid; /*!< Service uuid type */
uint8_t svc_inst_id; /*!< Service id */
uint16_t num_handle; /*!< The number of the attribute handle to be added to the gatts database */
uint16_t *handles; /*!< The number to the handles */
} add_attr_tab; /*!< Gatt server callback param of ESP_GATTS_CREAT_ATTR_TAB_EVT */
/**
* @brief ESP_GATTS_SET_ATTR_VAL_EVT
*/
struct gatts_set_attr_val_evt_param{
uint16_t srvc_handle; /*!< The service handle */
uint16_t attr_handle; /*!< The attribute handle */
esp_gatt_status_t status; /*!< Operation status*/
} set_attr_val; /*!< Gatt server callback param of ESP_GATTS_SET_ATTR_VAL_EVT */
/**
* @brief ESP_GATTS_SEND_SERVICE_CHANGE_EVT
*/
struct gatts_send_service_change_evt_param{
esp_gatt_status_t status; /*!< Operation status*/
} service_change; /*!< Gatt server callback param of ESP_GATTS_SEND_SERVICE_CHANGE_EVT */
} esp_ble_gatts_cb_param_t;
/**
* @brief GATT Server callback function type
* @param event : Event type
* @param gatts_if : GATT server access interface, normally
* different gatts_if correspond to different profile
* @param param : Point to callback parameter, currently is union type
*/
typedef void (* esp_gatts_cb_t)(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
/**
* @brief This function is called to register application callbacks
* with BTA GATTS module.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_register_callback(esp_gatts_cb_t callback);
/**
* @brief This function is called to register application identifier
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_app_register(uint16_t app_id);
/**
* @brief unregister with GATT Server.
*
* @param[in] gatts_if: GATT server access interface
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatts_if);
/**
* @brief Create a service. When service creation is done, a callback
* event ESP_GATTS_CREATE_EVT is called to report status
* and service ID to the profile. The service ID obtained in
* the callback function needs to be used when adding included
* service and characteristics/descriptors into the service.
*
* @param[in] gatts_if: GATT server access interface
* @param[in] service_id: service ID.
* @param[in] num_handle: number of handle requested for this service.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatts_if,
esp_gatt_srvc_id_t *service_id, uint16_t num_handle);
/**
* @brief Create a service attribute tab.
* @param[in] gatts_attr_db: the pointer to the service attr tab
* @param[in] gatts_if: GATT server access interface
* @param[in] max_nb_attr: the number of attribute to be added to the service database.
* @param[in] srvc_inst_id: the instance id of the service
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t *gatts_attr_db,
esp_gatt_if_t gatts_if,
uint8_t max_nb_attr,
uint8_t srvc_inst_id);
/**
* @brief This function is called to add an included service. This function have to be called between
* 'esp_ble_gatts_create_service' and 'esp_ble_gatts_add_char'. After included
* service is included, a callback event ESP_GATTS_ADD_INCL_SRVC_EVT
* is reported the included service ID.
*
* @param[in] service_handle: service handle to which this included service is to
* be added.
* @param[in] included_service_handle: the service ID to be included.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_add_included_service(uint16_t service_handle, uint16_t included_service_handle);
/**
* @brief This function is called to add a characteristic into a service.
*
* @param[in] service_handle: service handle to which this included service is to
* be added.
* @param[in] char_uuid : Characteristic UUID.
* @param[in] perm : Characteristic value declaration attribute permission.
* @param[in] property : Characteristic Properties
* @param[in] char_val : Characteristic value
* @param[in] control : attribute response control byte
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_add_char(uint16_t service_handle, esp_bt_uuid_t *char_uuid,
esp_gatt_perm_t perm, esp_gatt_char_prop_t property, esp_attr_value_t *char_val,
esp_attr_control_t *control);
/**
* @brief This function is called to add characteristic descriptor. When
* it's done, a callback event ESP_GATTS_ADD_DESCR_EVT is called
* to report the status and an ID number for this descriptor.
*
* @param[in] service_handle: service handle to which this characteristic descriptor is to
* be added.
* @param[in] perm: descriptor access permission.
* @param[in] descr_uuid: descriptor UUID.
* @param[in] char_descr_val : Characteristic descriptor value
* @param[in] control : attribute response control byte
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle,
esp_bt_uuid_t *descr_uuid,
esp_gatt_perm_t perm, esp_attr_value_t *char_descr_val,
esp_attr_control_t *control);
/**
* @brief This function is called to delete a service. When this is done,
* a callback event ESP_GATTS_DELETE_EVT is report with the status.
*
* @param[in] service_handle: service_handle to be deleted.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle);
/**
* @brief This function is called to start a service.
*
* @param[in] service_handle: the service handle to be started.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_start_service(uint16_t service_handle);
/**
* @brief This function is called to stop a service.
*
* @param[in] service_handle - service to be topped.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle);
/**
* @brief Send indicate or notify to GATT client.
* Set param need_confirm as false will send notification, otherwise indication.
*
* @param[in] gatts_if: GATT server access interface
* @param[in] conn_id - connection id to indicate.
* @param[in] attr_handle - attribute handle to indicate.
* @param[in] value_len - indicate value length.
* @param[in] value: value to indicate.
* @param[in] need_confirm - Whether a confirmation is required.
* false sends a GATT notification, true sends a GATT indication.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id, uint16_t attr_handle,
uint16_t value_len, uint8_t *value, bool need_confirm);
/**
* @brief This function is called to send a response to a request.
*
* @param[in] gatts_if: GATT server access interface
* @param[in] conn_id - connection identifier.
* @param[in] trans_id - transfer id
* @param[in] status - response status
* @param[in] rsp - response data.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_send_response(esp_gatt_if_t gatts_if, uint16_t conn_id, uint32_t trans_id,
esp_gatt_status_t status, esp_gatt_rsp_t *rsp);
/**
* @brief This function is called to set the attribute value by the application
*
* @param[in] attr_handle: the attribute handle which to be set
* @param[in] length: the value length
* @param[in] value: the pointer to the attribute value
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_set_attr_value(uint16_t attr_handle, uint16_t length, const uint8_t *value);
/**
* @brief Retrieve attribute value
*
* @param[in] attr_handle: Attribute handle.
* @param[out] length: pointer to the attribute value length
* @param[out] value: Pointer to attribute value payload, the value cannot be modified by user
*
* @return
* - ESP_GATT_OK : success
* - other : failed
*
*/
esp_gatt_status_t esp_ble_gatts_get_attr_value(uint16_t attr_handle, uint16_t *length, const uint8_t **value);
/**
* @brief Open a direct open connection or add a background auto connection
*
* @param[in] gatts_if: GATT server access interface
* @param[in] remote_bda: remote device bluetooth device address.
* @param[in] is_direct: direct connection or background auto connection
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatts_if, esp_bd_addr_t remote_bda, bool is_direct);
/**
* @brief Close a connection a remote device.
*
* @param[in] gatts_if: GATT server access interface
* @param[in] conn_id: connection ID to be closed.
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_close(esp_gatt_if_t gatts_if, uint16_t conn_id);
/**
* @brief Send service change indication
*
* @param[in] gatts_if: GATT server access interface
* @param[in] remote_bda: remote device bluetooth device address.
* If remote_bda is NULL then it will send service change
* indication to all the connected devices and if not then
* to a specific device
*
* @return
* - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_gatts_send_service_change_indication(esp_gatt_if_t gatts_if, esp_bd_addr_t remote_bda);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_GATTS_API_H__ */

View File

@ -0,0 +1,612 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_HF_AG_API_H__
#define __ESP_HF_AG_API_H__
#include "esp_err.h"
#include "esp_bt_defs.h"
#include "esp_hf_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
/* features masks of HF AG */
#define ESP_HF_PEER_FEAT_3WAY 0x01 /* Three-way calling */
#define ESP_HF_PEER_FEAT_ECNR 0x02 /* Echo cancellation and/or noise reduction */
#define ESP_HF_PEER_FEAT_VREC 0x04 /* Voice recognition */
#define ESP_HF_PEER_FEAT_INBAND 0x08 /* In-band ring tone */
#define ESP_HF_PEER_FEAT_VTAG 0x10 /* Attach a phone number to a voice tag */
#define ESP_HF_PEER_FEAT_REJECT 0x20 /* Ability to reject incoming call */
#define ESP_HF_PEER_FEAT_ECS 0x40 /* Enhanced Call Status */
#define ESP_HF_PEER_FEAT_ECC 0x80 /* Enhanced Call Control */
#define ESP_HF_PEER_FEAT_EXTERR 0x100 /* Extended error codes */
#define ESP_HF_PEER_FEAT_CODEC 0x200 /* Codec Negotiation */
/* CHLD feature masks of HF AG */
#define ESP_HF_CHLD_FEAT_REL 0x01 /* 0 Release waiting call or held calls */
#define ESP_HF_CHLD_FEAT_REL_ACC 0x02 /* 1 Release active calls and accept other waiting or held call */
#define ESP_HF_CHLD_FEAT_REL_X 0x04 /* 1x Release specified active call only */
#define ESP_HF_CHLD_FEAT_HOLD_ACC 0x08 /* 2 Active calls on hold and accept other waiting or held call */
#define ESP_HF_CHLD_FEAT_PRIV_X 0x10 /* 2x Request private mode with specified call(put the rest on hold) */
#define ESP_HF_CHLD_FEAT_MERGE 0x20 /* 3 Add held call to multiparty */
#define ESP_HF_CHLD_FEAT_MERGE_DETACH 0x40 /* 4 Connect two calls and leave(disconnect from multiparty) */
/// HF callback events
typedef enum
{
ESP_HF_CONNECTION_STATE_EVT = 0, /*!< Connection state changed event */
ESP_HF_AUDIO_STATE_EVT, /*!< Audio connection state change event */
ESP_HF_BVRA_RESPONSE_EVT, /*!< Voice recognition state change event */
ESP_HF_VOLUME_CONTROL_EVT, /*!< Audio volume control command from HF Client, provided by +VGM or +VGS message */
ESP_HF_UNAT_RESPONSE_EVT, /*!< Unknown AT cmd Response*/
ESP_HF_IND_UPDATE_EVT, /*!< Indicator Update Event*/
ESP_HF_CIND_RESPONSE_EVT, /*!< Call And Device Indicator Response*/
ESP_HF_COPS_RESPONSE_EVT, /*!< Current operator information */
ESP_HF_CLCC_RESPONSE_EVT, /*!< List of current calls notification */
ESP_HF_CNUM_RESPONSE_EVT, /*!< Subscriber information response from HF Client */
ESP_HF_VTS_RESPONSE_EVT, /*!< Enable or not DTMF */
ESP_HF_NREC_RESPONSE_EVT, /*!< Enable or not NREC */
ESP_HF_ATA_RESPONSE_EVT, /*!< Answer an Incoming Call */
ESP_HF_CHUP_RESPONSE_EVT, /*!< Reject an Incoming Call */
ESP_HF_DIAL_EVT, /*!< Origin an outgoing call with specific number or the dial the last number */
ESP_HF_WBS_RESPONSE_EVT, /*!< Codec Status */
ESP_HF_BCS_RESPONSE_EVT, /*!< Final Codec Choice */
} esp_hf_cb_event_t;
/// HFP AG callback parameters
typedef union
{
/**
* @brief ESP_HS_CONNECTION_STATE_EVT
*/
struct hf_conn_stat_param {
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_hf_connection_state_t state; /*!< Connection state */
uint32_t peer_feat; /*!< HF supported features */
uint32_t chld_feat; /*!< AG supported features on call hold and multiparty services */
} conn_stat; /*!< AG callback param of ESP_HF_CONNECTION_STATE_EVT */
/**
* @brief ESP_HF_AUDIO_STATE_EVT
*/
struct hf_audio_stat_param {
esp_bd_addr_t remote_addr; /*!< Remote bluetooth device address */
esp_hf_audio_state_t state; /*!< Audio connection state */
} audio_stat; /*!< AG callback param of ESP_HF_AUDIO_STATE_EVT */
/**
* @brief ESP_HF_BVRA_RESPONSE_EVT
*/
struct hf_vra_rep_param {
esp_bd_addr_t remote_addr; /*!< Remote bluetooth device address */
esp_hf_vr_state_t value; /*!< Voice recognition state */
} vra_rep; /*!< AG callback param of ESP_HF_BVRA_RESPONSE_EVT */
/**
* @brief ESP_HF_VOLUME_CONTROL_EVT
*/
struct hf_volume_control_param {
esp_hf_volume_type_t type; /*!< Volume control target, speaker or microphone */
int volume; /*!< Gain, ranges from 0 to 15 */
} volume_control; /*!< AG callback param of ESP_HF_VOLUME_CONTROL_EVT */
/**
* @brief ESP_HF_UNAT_RESPOSNE_EVT
*/
struct hf_unat_rep_param {
char *unat; /*!< Unknown AT command string */
}unat_rep; /*!< AG callback param of ESP_HF_UNAT_RESPONSE_EVT */
/**
* @brief ESP_HF_CIND_RESPONSE_EVT
*/
struct hf_cind_param {
esp_hf_call_status_t call_status; /*!< call status indicator */
esp_hf_call_setup_status_t call_setup_status; /*!< call setup status indicator */
esp_hf_network_state_t svc; /*!< bluetooth proprietary call hold status indicator */
int signal_strength; /*!< bluetooth proprietary call hold status indicator */
esp_hf_roaming_status_t roam; /*!< bluetooth proprietary call hold status indicator */
int battery_level; /*!< battery charge value, ranges from 0 to 5 */
esp_hf_call_held_status_t call_held_status; /*!< bluetooth proprietary call hold status indicator */
} cind; /*!< AG callback param of ESP_HF_CIND_RESPONSE_EVT */
/**
* @brief ESP_HF_DIAL_EVT
*/
struct hf_out_call_param {
esp_bd_addr_t remote_addr; /*!< remote bluetooth device address */
char *num_or_loc; /*!< location in phone memory */
} out_call; /*!< AG callback param of ESP_HF_DIAL_EVT */
/**
* @brief ESP_HF_VTS_RESPOSNE_EVT
*/
struct hf_vts_rep_param {
char *code; /*!< MTF code from HF Client */
}vts_rep; /*!< AG callback param of ESP_HF_VTS_RESPONSE_EVT */
/**
* @brief ESP_HF_NREC_RESPOSNE_EVT
*/
struct hf_nrec_param {
esp_hf_nrec_t state; /*!< NREC enabled or disabled */
} nrec; /*!< AG callback param of ESP_HF_NREC_RESPONSE_EVT */
/**
* @brief ESP_HF_WBS_RESPONSE_EVT
*/
struct hf_wbs_rep_param {
esp_hf_wbs_config_t codec; /*!< codec mode CVSD or mSBC */
} wbs_rep; /*!< AG callback param of ESP_HF_WBS_RESPONSE_EVT */
/**
* @brief ESP_HF_BCS_RESPONSE_EVT
*/
struct hf_bcs_rep_param {
esp_hf_wbs_config_t mode; /*!< codec mode CVSD or mSBC */
} bcs_rep; /*!< AG callback param of ESP_HF_BCS_RESPONSE_EVT */
} esp_hf_cb_param_t; /*!< HFP AG callback param compound*/
/**
* @brief AG incoming data callback function, the callback is useful in case of
* Voice Over HCI.
*
* @param[in] buf : pointer to incoming data(payload of HCI synchronous data packet), the
* buffer is allocated inside bluetooth protocol stack and will be released after
* invoke of the callback is finished.
*
* @param[in] len : size(in bytes) in buf
*/
typedef void (* esp_hf_incoming_data_cb_t)(const uint8_t *buf, uint32_t len);
/**
* @brief AG outgoing data callback function, the callback is useful in case of
* Voice Over HCI. Once audio connection is set up and the application layer has
* prepared data to send, the lower layer will call this function to read data
* and then send. This callback is supposed to be implemented as non-blocking,
* and if data is not enough, return value 0 is supposed.
*
* @param[in] buf : pointer to incoming data(payload of HCI synchronous data packet), the
* buffer is allocated inside bluetooth protocol stack and will be released after
* invoke of the callback is finished.
*
* @param[in] len : size(in bytes) in buf
*
* @param[out] length of data successfully read
*/
typedef uint32_t (* esp_hf_outgoing_data_cb_t) (uint8_t *buf, uint32_t len);
/**
* @brief HF AG callback function type
*
* @param event : Event type
*
* @param param : Pointer to callback parameter
*/
typedef void (* esp_hf_cb_t) (esp_hf_cb_event_t event, esp_hf_cb_param_t *param);
/************************************************************************************
** ESP HF API
************************************************************************************/
/**
* @brief Register application callback function to HFP AG module.
* This function should be called only after esp_bluedroid_enable() completes successfully.
*
* @param[in] callback: HFP AG event callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_bt_hf_register_callback(esp_hf_cb_t callback);
/**
*
* @brief Initialize the bluetooth HF AG module.
* This function should be called after esp_bluedroid_enable() completes successfully.
*
* @param[in] remote_addr: remote bluetooth device address
*
* @return
* - ESP_OK: if the initialization request is sent successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_init(esp_bd_addr_t remote_addr);
/**
*
* @brief De-initialize for HF AG module.
* This function should be called only after esp_bluedroid_enable() completes successfully.
*
* @param[in] remote_addr: remote bluetooth device address
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_deinit(esp_bd_addr_t remote_addr);
/**
*
* @brief To establish a Service Level Connection to remote bluetooth HFP client device.
* This function must be called after esp_bt_hf_init() and before esp_bt_hf_deinit().
*
* @param[in] remote_bda: remote bluetooth HFP client device address
*
* @return
* - ESP_OK: connect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_connect(esp_bd_addr_t remote_bda);
/**
*
* @brief Disconnect from the remote HFP client. This function must be called
* after esp_bt_hf_init() and before esp_bt_hf_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_disconnect(esp_bd_addr_t remote_bda);
/**
*
* @brief Create audio connection with remote HFP client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_connect_audio(esp_bd_addr_t remote_bda);
/**
*
* @brief Release the established audio connection with remote HFP client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_disconnect_audio(esp_bd_addr_t remote_bda);
/**
*
* @brief Response of Volume Recognition Command(AT+VRA) from HFP client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_bda: the device address of voice recognization initiator
*
* @param[in] value: 0 - voice recognition disabled, 1- voice recognition enabled
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_vra(esp_bd_addr_t remote_bda, esp_hf_vr_state_t value);
/**
*
* @brief Volume synchronization with HFP client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_bda: remote bluetooth device address
*
* @param[in] type: volume control target, speaker or microphone
*
* @param[in] volume: gain of the speaker of microphone, ranges 0 to 15
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_volume_control(esp_bd_addr_t remote_bda, esp_hf_volume_control_target_t type, int volume);
/**
*
* @brief Handle Unknown AT command from HFP Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
*
* @param[in] unat: User AT command response to HF Client.
* It will response "ERROR" by default if unat is NULL.
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_unat_response(esp_bd_addr_t remote_addr, char *unat);
/**
*
* @brief Unsolicited send extend AT error code to HFP Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_bda: remote bluetooth device address
* @param[in] response_code: AT command response code
* @param[in] error_code: CME error code
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_cmee_response(esp_bd_addr_t remote_bda, esp_hf_at_response_code_t response_code, esp_hf_cme_err_t error_code);
/**
*
* @brief Usolicited send device status notificationto HFP Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] call_state: call state
* @param[in] call_setup_state: call setup state
* @param[in] ntk_state: network service state
* @param[in] signal: signal strength from 0 to 5
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_indchange_notification(esp_bd_addr_t remote_addr, esp_hf_call_status_t call_state,
esp_hf_call_setup_status_t call_setup_state,
esp_hf_network_state_t ntk_state, int signal);
/**
*
* @brief Response to device individual indicatiors to HFP Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] call_state: call state
* @param[in] call_setup_state: call setup state
* @param[in] ntk_state: network service state
* @param[in] signal: signal strength from 0 to 5
* @param[in] roam: roam state
* @param[in] batt_lev: batery level from 0 to 5
* @param[in] call_held_status: call held status
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_cind_response(esp_bd_addr_t remote_addr,
esp_hf_call_status_t call_state,
esp_hf_call_setup_status_t call_setup_state,
esp_hf_network_state_t ntk_state, int signal, esp_hf_roaming_status_t roam, int batt_lev,
esp_hf_call_held_status_t call_held_status);
/**
*
* @brief Reponse for AT+COPS command from HF Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] name: current operator name
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_cops_response(esp_bd_addr_t remote_addr, char *name);
/**
*
* @brief Response to AT+CLCC command from HFP Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] index: the index of current call
* @param[in] dir: call direction (incoming/outgoing)
* @param[in] current_call_state: current call state
* @param[in] mode: current call mode (voice/data/fax)
* @param[in] mpty: single or multi type
* @param[in] number: current call number
* @param[in] type: international type or unknow
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_clcc_response(esp_bd_addr_t remote_addr, int index, esp_hf_current_call_direction_t dir,
esp_hf_current_call_status_t current_call_state, esp_hf_current_call_mode_t mode,
esp_hf_current_call_mpty_type_t mpty, char *number, esp_hf_call_addr_type_t type);
/**
*
* @brief Response for AT+CNUM command from HF Client.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] number: registration number
* @param[in] type: service type (unknown/voice/fax)
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_cnum_response(esp_bd_addr_t remote_addr, char *number, esp_hf_subscriber_service_type_t type);
/**
*
* @brief Inform HF Client that AG Provided in-band ring tone or not.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] state: in-band ring tone state
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_bsir(esp_bd_addr_t remote_addr, esp_hf_in_band_ring_state_t state);
/**
*
* @brief Answer Incoming Call from AG.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] num_active: the number of active call
* @param[in] num_held: the number of held call
* @param[in] call_state: call state
* @param[in] call_setup_state: call setup state
* @param[in] number: number of the incoming call
* @param[in] call_addr_type: call address type
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_answer_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
esp_hf_call_status_t call_state, esp_hf_call_setup_status_t call_setup_state,
char *number, esp_hf_call_addr_type_t call_addr_type);
/**
*
* @brief Reject Incoming Call from AG.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] num_active: the number of active call
* @param[in] num_held: the number of held call
* @param[in] call_state: call state
* @param[in] call_setup_state: call setup state
* @param[in] number: number of the incoming call
* @param[in] call_addr_type: call address type
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_reject_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
esp_hf_call_status_t call_state, esp_hf_call_setup_status_t call_setup_state,
char *number, esp_hf_call_addr_type_t call_addr_type);
/**
*
* @brief Initiate a call from AG.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] num_active: the number of active call
* @param[in] num_held: the number of held call
* @param[in] call_state: call state
* @param[in] call_setup_state: call setup state
* @param[in] number: number of the outgoing call
* @param[in] call_addr_type: call address type
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_out_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
esp_hf_call_status_t call_state, esp_hf_call_setup_status_t call_setup_state,
char *number, esp_hf_call_addr_type_t call_addr_type);
/**
*
* @brief End an ongoing call.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
*
* @param[in] remote_addr: remote bluetooth device address
* @param[in] num_active: the number of active call
* @param[in] num_held: the number of held call
* @param[in] call_state: call state
* @param[in] call_setup_state: call setup state
* @param[in] number: number of the call
* @param[in] call_addr_type: call address type
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_bt_hf_end_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
esp_hf_call_status_t call_state, esp_hf_call_setup_status_t call_setup_state,
char *number, esp_hf_call_addr_type_t call_addr_type);
/**
* @brief Register AG data output function.
* The callback is only used in the case that Voice Over HCI is enabled.
*
* @param[in] recv: HFP client incoming data callback function
* @param[in] send: HFP client outgoing data callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_bt_hf_register_data_callback(esp_hf_incoming_data_cb_t recv, esp_hf_outgoing_data_cb_t send);
/**
* @brief Trigger the lower-layer to fetch and send audio data.
*
* This function is only used in the case that Voice Over HCI is enabled.
* As a precondition to use this API, Service Level Connection shall exist with HFP client.
* After this function is called, lower layer will invoke esp_hf_client_outgoing_data_cb_t to fetch data
*
*/
void esp_hf_outgoing_data_ready(void);
#ifdef __cplusplus
}
#endif
#endif //__ESP_HF_AG_API_H__

View File

@ -0,0 +1,664 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_HF_CLIENT_API_H__
#define __ESP_HF_CLIENT_API_H__
#include "esp_err.h"
#include "esp_bt_defs.h"
#include "esp_hf_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_BT_HF_CLIENT_NUMBER_LEN (32)
#define ESP_BT_HF_CLIENT_OPERATOR_NAME_LEN (16)
/// Bluetooth HFP RFCOMM connection and service level connection status
typedef enum {
ESP_HF_CLIENT_CONNECTION_STATE_DISCONNECTED = 0, /*!< RFCOMM data link channel released */
ESP_HF_CLIENT_CONNECTION_STATE_CONNECTING, /*!< connecting remote device on the RFCOMM data link*/
ESP_HF_CLIENT_CONNECTION_STATE_CONNECTED, /*!< RFCOMM connection established */
ESP_HF_CLIENT_CONNECTION_STATE_SLC_CONNECTED, /*!< service level connection established */
ESP_HF_CLIENT_CONNECTION_STATE_DISCONNECTING, /*!< disconnecting with remote device on the RFCOMM dat link*/
} esp_hf_client_connection_state_t;
/// Bluetooth HFP audio connection status
typedef enum {
ESP_HF_CLIENT_AUDIO_STATE_DISCONNECTED = 0, /*!< audio connection released */
ESP_HF_CLIENT_AUDIO_STATE_CONNECTING, /*!< audio connection has been initiated */
ESP_HF_CLIENT_AUDIO_STATE_CONNECTED, /*!< audio connection is established */
ESP_HF_CLIENT_AUDIO_STATE_CONNECTED_MSBC, /*!< mSBC audio connection is established */
} esp_hf_client_audio_state_t;
/// in-band ring tone state
typedef enum {
ESP_HF_CLIENT_IN_BAND_RINGTONE_NOT_PROVIDED = 0,
ESP_HF_CLIENT_IN_BAND_RINGTONE_PROVIDED,
} esp_hf_client_in_band_ring_state_t;
/* features masks of AG */
#define ESP_HF_CLIENT_PEER_FEAT_3WAY 0x01 /* Three-way calling */
#define ESP_HF_CLIENT_PEER_FEAT_ECNR 0x02 /* Echo cancellation and/or noise reduction */
#define ESP_HF_CLIENT_PEER_FEAT_VREC 0x04 /* Voice recognition */
#define ESP_HF_CLIENT_PEER_FEAT_INBAND 0x08 /* In-band ring tone */
#define ESP_HF_CLIENT_PEER_FEAT_VTAG 0x10 /* Attach a phone number to a voice tag */
#define ESP_HF_CLIENT_PEER_FEAT_REJECT 0x20 /* Ability to reject incoming call */
#define ESP_HF_CLIENT_PEER_FEAT_ECS 0x40 /* Enhanced Call Status */
#define ESP_HF_CLIENT_PEER_FEAT_ECC 0x80 /* Enhanced Call Control */
#define ESP_HF_CLIENT_PEER_FEAT_EXTERR 0x100 /* Extended error codes */
#define ESP_HF_CLIENT_PEER_FEAT_CODEC 0x200 /* Codec Negotiation */
/* CHLD feature masks of AG */
#define ESP_HF_CLIENT_CHLD_FEAT_REL 0x01 /* 0 Release waiting call or held calls */
#define ESP_HF_CLIENT_CHLD_FEAT_REL_ACC 0x02 /* 1 Release active calls and accept other waiting or held call */
#define ESP_HF_CLIENT_CHLD_FEAT_REL_X 0x04 /* 1x Release specified active call only */
#define ESP_HF_CLIENT_CHLD_FEAT_HOLD_ACC 0x08 /* 2 Active calls on hold and accept other waiting or held call */
#define ESP_HF_CLIENT_CHLD_FEAT_PRIV_X 0x10 /* 2x Request private mode with specified call(put the rest on hold) */
#define ESP_HF_CLIENT_CHLD_FEAT_MERGE 0x20 /* 3 Add held call to multiparty */
#define ESP_HF_CLIENT_CHLD_FEAT_MERGE_DETACH 0x40 /* 4 Connect two calls and leave(disconnect from multiparty) */
/// HF CLIENT callback events
typedef enum {
ESP_HF_CLIENT_CONNECTION_STATE_EVT = 0, /*!< connection state changed event */
ESP_HF_CLIENT_AUDIO_STATE_EVT, /*!< audio connection state change event */
ESP_HF_CLIENT_BVRA_EVT, /*!< voice recognition state change event */
ESP_HF_CLIENT_CIND_CALL_EVT, /*!< call indication */
ESP_HF_CLIENT_CIND_CALL_SETUP_EVT, /*!< call setup indication */
ESP_HF_CLIENT_CIND_CALL_HELD_EVT, /*!< call held indication */
ESP_HF_CLIENT_CIND_SERVICE_AVAILABILITY_EVT, /*!< network service availability indication */
ESP_HF_CLIENT_CIND_SIGNAL_STRENGTH_EVT, /*!< signal strength indication */
ESP_HF_CLIENT_CIND_ROAMING_STATUS_EVT, /*!< roaming status indication */
ESP_HF_CLIENT_CIND_BATTERY_LEVEL_EVT, /*!< battery level indication */
ESP_HF_CLIENT_COPS_CURRENT_OPERATOR_EVT, /*!< current operator information */
ESP_HF_CLIENT_BTRH_EVT, /*!< call response and hold event */
ESP_HF_CLIENT_CLIP_EVT, /*!< Calling Line Identification notification */
ESP_HF_CLIENT_CCWA_EVT, /*!< call waiting notification */
ESP_HF_CLIENT_CLCC_EVT, /*!< list of current calls notification */
ESP_HF_CLIENT_VOLUME_CONTROL_EVT, /*!< audio volume control command from AG, provided by +VGM or +VGS message */
ESP_HF_CLIENT_AT_RESPONSE_EVT, /*!< AT command response event */
ESP_HF_CLIENT_CNUM_EVT, /*!< subscriber information response from AG */
ESP_HF_CLIENT_BSIR_EVT, /*!< setting of in-band ring tone */
ESP_HF_CLIENT_BINP_EVT, /*!< requested number of last voice tag from AG */
ESP_HF_CLIENT_RING_IND_EVT, /*!< ring indication event */
} esp_hf_client_cb_event_t;
/// HFP client callback parameters
typedef union {
/**
* @brief ESP_HF_CLIENT_CONNECTION_STATE_EVT
*/
struct hf_client_conn_stat_param {
esp_hf_client_connection_state_t state; /*!< HF connection state */
uint32_t peer_feat; /*!< AG supported features */
uint32_t chld_feat; /*!< AG supported features on call hold and multiparty services */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} conn_stat; /*!< HF callback param of ESP_HF_CLIENT_CONNECTION_STATE_EVT */
/**
* @brief ESP_HF_CLIENT_AUDIO_STATE_EVT
*/
struct hf_client_audio_stat_param {
esp_hf_client_audio_state_t state; /*!< audio connection state */
esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
} audio_stat; /*!< HF callback param of ESP_HF_CLIENT_AUDIO_STATE_EVT */
/**
* @brief ESP_HF_CLIENT_BVRA_EVT
*/
struct hf_client_bvra_param {
esp_hf_vr_state_t value; /*!< voice recognition state */
} bvra; /*!< HF callback param of ESP_HF_CLIENT_BVRA_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_SERVICE_AVAILABILITY_EVT
*/
struct hf_client_service_availability_param {
esp_hf_network_state_t status; /*!< service availability status */
} service_availability; /*!< HF callback param of ESP_HF_CLIENT_CIND_SERVICE_AVAILABILITY_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_ROAMING_STATUS_EVT
*/
struct hf_client_network_roaming_param {
esp_hf_roaming_status_t status; /*!< roaming status */
} roaming; /*!< HF callback param of ESP_HF_CLIENT_CIND_ROAMING_STATUS_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_SIGNAL_STRENGTH_EVT
*/
struct hf_client_signal_strength_ind_param {
int value; /*!< signal strength value, ranges from 0 to 5 */
} signal_strength; /*!< HF callback param of ESP_HF_CLIENT_CIND_SIGNAL_STRENGTH_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_BATTERY_LEVEL_EVT
*/
struct hf_client_battery_level_ind_param {
int value; /*!< battery charge value, ranges from 0 to 5 */
} battery_level; /*!< HF callback param of ESP_HF_CLIENT_CIND_BATTERY_LEVEL_EVT */
/**
* @brief ESP_HF_CLIENT_COPS_CURRENT_OPERATOR_EVT
*/
struct hf_client_current_operator_param {
const char *name; /*!< name of the network operator */
} cops; /*!< HF callback param of ESP_HF_CLIENT_COPS_CURRENT_OPERATOR_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_CALL_EVT
*/
struct hf_client_call_ind_param {
esp_hf_call_status_t status; /*!< call status indicator */
} call; /*!< HF callback param of ESP_HF_CLIENT_CIND_CALL_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_CALL_SETUP_EVT
*/
struct hf_client_call_setup_ind_param {
esp_hf_call_setup_status_t status; /*!< call setup status indicator */
} call_setup; /*!< HF callback param of ESP_HF_CLIENT_BVRA_EVT */
/**
* @brief ESP_HF_CLIENT_CIND_CALL_HELD_EVT
*/
struct hf_client_call_held_ind_param {
esp_hf_call_held_status_t status; /*!< bluetooth proprietary call hold status indicator */
} call_held; /*!< HF callback param of ESP_HF_CLIENT_CIND_CALL_HELD_EVT */
/**
* @brief ESP_HF_CLIENT_BTRH_EVT
*/
struct hf_client_btrh_param {
esp_hf_btrh_status_t status; /*!< call hold and response status result code */
} btrh; /*!< HF callback param of ESP_HF_CLIENT_BRTH_EVT */
/**
* @brief ESP_HF_CLIENT_CLIP_EVT
*/
struct hf_client_clip_param {
const char *number; /*!< phone number string of call */
} clip; /*!< HF callback param of ESP_HF_CLIENT_CLIP_EVT */
/**
* @brief ESP_HF_CLIENT_CCWA_EVT
*/
struct hf_client_ccwa_param {
const char *number; /*!< phone number string of waiting call */
} ccwa; /*!< HF callback param of ESP_HF_CLIENT_BVRA_EVT */
/**
* @brief ESP_HF_CLIENT_CLCC_EVT
*/
struct hf_client_clcc_param {
int idx; /*!< numbering(starting with 1) of the call */
esp_hf_current_call_direction_t dir; /*!< direction of the call */
esp_hf_current_call_status_t status; /*!< status of the call */
esp_hf_current_call_mpty_type_t mpty; /*!< multi-party flag */
char *number; /*!< phone number(optional) */
} clcc; /*!< HF callback param of ESP_HF_CLIENT_CLCC_EVT */
/**
* @brief ESP_HF_CLIENT_VOLUME_CONTROL_EVT
*/
struct hf_client_volume_control_param {
esp_hf_volume_control_target_t type; /*!< volume control target, speaker or microphone */
int volume; /*!< gain, ranges from 0 to 15 */
} volume_control; /*!< HF callback param of ESP_HF_CLIENT_VOLUME_CONTROL_EVT */
/**
* @brief ESP_HF_CLIENT_AT_RESPONSE_EVT
*/
struct hf_client_at_response_param {
esp_hf_at_response_code_t code; /*!< AT response code */
esp_hf_cme_err_t cme; /*!< Extended Audio Gateway Error Result Code */
} at_response; /*!< HF callback param of ESP_HF_CLIENT_AT_RESPONSE_EVT */
/**
* @brief ESP_HF_CLIENT_CNUM_EVT
*/
struct hf_client_cnum_param {
const char *number; /*!< phone number string */
esp_hf_subscriber_service_type_t type; /*!< service type that the phone number relates to */
} cnum; /*!< HF callback param of ESP_HF_CLIENT_CNUM_EVT */
/**
* @brief ESP_HF_CLIENT_BSIR_EVT
*/
struct hf_client_bsirparam {
esp_hf_client_in_band_ring_state_t state; /*!< setting state of in-band ring tone */
} bsir; /*!< HF callback param of ESP_HF_CLIENT_BSIR_EVT */
/**
* @brief ESP_HF_CLIENT_BINP_EVT
*/
struct hf_client_binp_param {
const char *number; /*!< phone number corresponding to the last voice tag in the HF */
} binp; /*!< HF callback param of ESP_HF_CLIENT_BINP_EVT */
} esp_hf_client_cb_param_t; /*!< HFP client callback parameters */
/**
* @brief HFP client incoming data callback function, the callback is useful in case of
* Voice Over HCI.
* @param[in] buf : pointer to incoming data(payload of HCI synchronous data packet), the
* buffer is allocated inside bluetooth protocol stack and will be released after
* invoke of the callback is finished.
* @param[in] len : size(in bytes) in buf
*/
typedef void (* esp_hf_client_incoming_data_cb_t)(const uint8_t *buf, uint32_t len);
/**
* @brief HFP client outgoing data callback function, the callback is useful in case of
* Voice Over HCI. Once audio connection is set up and the application layer has
* prepared data to send, the lower layer will call this function to read data
* and then send. This callback is supposed to be implemented as non-blocking,
* and if data is not enough, return value 0 is supposed.
*
* @param[in] buf : pointer to incoming data(payload of HCI synchronous data packet), the
* buffer is allocated inside bluetooth protocol stack and will be released after
* invoke of the callback is finished.
*
* @param[in] len : size(in bytes) in buf
*
* @param[out] length of data successfully read
*
*/
typedef uint32_t (* esp_hf_client_outgoing_data_cb_t)(uint8_t *buf, uint32_t len);
/**
* @brief HFP client callback function type
*
* @param event : Event type
*
* @param param : Pointer to callback parameter
*/
typedef void (* esp_hf_client_cb_t)(esp_hf_client_cb_event_t event, esp_hf_client_cb_param_t *param);
/**
* @brief Register application callback function to HFP client module.
* This function should be called only after esp_bluedroid_enable() completes successfully.
*
* @param[in] callback: HFP client event callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_hf_client_register_callback(esp_hf_client_cb_t callback);
/**
*
* @brief Initialize the bluetooth HFP client module.
* This function should be called after esp_bluedroid_enable() completes successfully.
*
* @return
* - ESP_OK: if the initialization request is sent successfully
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_init(void);
/**
*
* @brief De-initialize for HFP client module.
* This function should be called only after esp_bluedroid_enable() completes successfully.
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_deinit(void);
/**
*
* @brief Establish a Service Level Connection to remote bluetooth HFP audio gateway(AG) device.
* This function must be called after esp_hf_client_init() and before esp_hf_client_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: connect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_connect(esp_bd_addr_t remote_bda);
/**
*
* @brief Disconnect from the remote HFP audio gateway.
* This function must be called after esp_hf_client_init() and before esp_hf_client_deinit().
*
* @param[in] remote_bda: remote bluetooth device address
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_disconnect(esp_bd_addr_t remote_bda);
/**
*
* @brief Create audio connection with remote HFP AG.
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] remote_bda: remote bluetooth device address
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_connect_audio(esp_bd_addr_t remote_bda);
/**
*
* @brief Release the established audio connection with remote HFP AG.
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] remote_bda: remote bluetooth device address
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_disconnect_audio(esp_bd_addr_t remote_bda);
/**
*
* @brief Enable voice recognition in the AG.
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_start_voice_recognition(void);
/**
*
* @brief Disable voice recognition in the AG.
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_stop_voice_recognition(void);
/**
*
* @brief Volume synchronization with AG.
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] type: volume control target, speaker or microphone
* @param[in] volume: gain of the speaker of microphone, ranges 0 to 15
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_volume_update(esp_hf_volume_control_target_t type, int volume);
/**
*
* @brief Place a call with a specified number, if number is NULL, last called number is called.
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] number: number string of the call. If NULL, the last number is called(aka re-dial)
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_dial(const char *number);
/**
*
* @brief Place a call with number specified by location(speed dial).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] location: location of the number in the memory
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_dial_memory(int location);
/**
*
* @brief Send call hold and multiparty commands, or enhanced call control commands(Use AT+CHLD).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] chld: AT+CHLD call hold and multiparty handling AT command.
* @param[in] idx: used in Enhanced Call Control Mechanisms, used if chld is
* ESP_HF_CHLD_TYPE_REL_X or ESP_HF_CHLD_TYPE_PRIV_X
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_send_chld_cmd(esp_hf_chld_type_t chld, int idx);
/**
*
* @brief Send response and hold action command(Send AT+BTRH command)
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] btrh: response and hold action to send
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_send_btrh_cmd(esp_hf_btrh_cmd_t btrh);
/**
*
* @brief Answer an incoming call(send ATA command).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_answer_call(void);
/**
*
* @brief Reject an incoming call(send AT+CHUP command).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_reject_call(void);
/**
*
* @brief Query list of current calls in AG(send AT+CLCC command).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_query_current_calls(void);
/**
*
* @brief Query the name of currently selected network operator in AG(use AT+COPS commands).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_query_current_operator_name(void);
/**
*
* @brief Get subscriber information number from AG(send AT+CNUM command)
* As a precondition to use this API, Service Level Connection shall exist with AG
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_retrieve_subscriber_info(void);
/**
*
* @brief Transmit DTMF codes during an ongoing call(use AT+VTS commands)
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @param[in] code: dtmf code, single ascii character in the set 0-9, #, *, A-D
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_send_dtmf(char code);
/**
*
* @brief Request a phone number from AG corresponding to last voice tag recorded (send AT+BINP command).
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
* @return
* - ESP_OK: disconnect request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_request_last_voice_tag_number(void);
/**
*
* @brief Disable echo cancellation and noise reduction in the AG (use AT+NREC=0 command).
* As a precondition to use this API, Service Level Connection shall exist with AG
*
* @return
* - ESP_OK: NREC=0 request is sent to lower layer
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*
*/
esp_err_t esp_hf_client_send_nrec(void);
/**
* @brief Register HFP client data output function; the callback is only used in
* the case that Voice Over HCI is enabled.
*
* @param[in] recv: HFP client incoming data callback function
*
* @param[in] send: HFP client outgoing data callback function
*
* @return
* - ESP_OK: success
* - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: if callback is a NULL function pointer
*
*/
esp_err_t esp_hf_client_register_data_callback(esp_hf_client_incoming_data_cb_t recv,
esp_hf_client_outgoing_data_cb_t send);
/**
* @brief Trigger the lower-layer to fetch and send audio data.
* This function is only only used in the case that Voice Over HCI is enabled. After this
* function is called, lower layer will invoke esp_hf_client_outgoing_data_cb_t to fetch data.
*
* As a precondition to use this API, Service Level Connection shall exist with AG.
*
*/
void esp_hf_client_outgoing_data_ready(void);
/**
* @brief Initialize the down sampling converter. This is a utility function that can
* only be used in the case that Voice Over HCI is enabled.
*
* @param[in] src_sps: original samples per second(source audio data, i.e. 48000, 32000,
* 16000, 44100, 22050, 11025)
* @param[in] bits: number of bits per pcm sample (16)
*
* @param[in] channels: number of channels (i.e. mono(1), stereo(2)...)
*/
void esp_hf_client_pcm_resample_init(uint32_t src_sps, uint32_t bits, uint32_t channels);
/**
* @brief Deinitialize the down sampling converter.
*/
void esp_hf_client_pcm_resample_deinit(void);
/**
* @brief Down sampling utility to convert high sampling rate into 8K/16bits 1-channel mode PCM
* samples. This can only be used in the case that Voice Over HCI is enabled.
*
* @param[in] src: pointer to the buffer where the original sampling PCM are stored
*
* @param[in] in_bytes: length of the input PCM sample buffer in byte
*
* @param[in] dst: pointer to the buffer which is to be used to store the converted PCM samples
*
* @return number of samples converted
*/
int32_t esp_hf_client_pcm_resample(void *src, uint32_t in_bytes, void *dst);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_HF_CLIENT_API_H__ */

View File

@ -0,0 +1,348 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_HF_DEFS_H__
#define __ESP_HF_DEFS_H__
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_BT_HF_NUMBER_LEN (32)
#define ESP_BT_HF_OPERATOR_NAME_LEN (16)
#ifndef BTC_HSAG_SERVICE_NAME
#define BTC_HSAG_SERVICE_NAME ("Headset Gateway")
#endif
#ifndef BTC_HFAG_SERVICE_NAME
#define BTC_HFAG_SERVICE_NAME ("Handsfree Gateway")
#endif
#ifndef BTC_HF_SERVICES
#define BTC_HF_SERVICES (BTA_HSP_SERVICE_MASK | BTA_HFP_SERVICE_MASK )
#endif
#ifndef BTC_HF_SERVICE_NAMES
#define BTC_HF_SERVICE_NAMES {BTC_HSAG_SERVICE_NAME , BTC_HFAG_SERVICE_NAME}
#endif
#ifndef BTC_HF_SECURITY
#define BTC_HF_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)
#endif
#define BTC_HF_CALL_END_TIMEOUT 6
#define BTC_HF_INVALID_IDX -1
/// in-band ring tone state
typedef enum {
ESP_HF_IN_BAND_RINGTONE_NOT_PROVIDED = 0,
ESP_HF_IN_BAND_RINGTONE_PROVIDED,
} esp_hf_in_band_ring_state_t;
/// voice recognition state
typedef enum {
ESP_HF_VR_STATE_DISABLED = 0, /*!< voice recognition disabled */
ESP_HF_VR_STATE_ENABLED, /*!< voice recognition enabled */
} esp_hf_vr_state_t;
/// Bluetooth HFP audio volume control target
typedef enum {
ESP_HF_VOLUME_CONTROL_TARGET_SPK = 0, /*!< speaker */
ESP_HF_VOLUME_CONTROL_TARGET_MIC, /*!< microphone */
} esp_hf_volume_control_target_t;
/// Bluetooth HFP audio connection status
typedef enum {
ESP_HF_AUDIO_STATE_DISCONNECTED = 0, /*!< audio connection released */
ESP_HF_AUDIO_STATE_CONNECTING, /*!< audio connection has been initiated */
ESP_HF_AUDIO_STATE_CONNECTED, /*!< audio connection is established */
ESP_HF_AUDIO_STATE_CONNECTED_MSBC, /*!< mSBC audio connection is established */
} esp_hf_audio_state_t;
typedef enum {
ESP_HF_VOLUME_TYPE_SPK = 0,
ESP_HF_VOLUME_TYPE_MIC
} esp_hf_volume_type_t;
/// +CIND network service availability status
typedef enum
{
ESP_HF_NETWORK_STATE_NOT_AVAILABLE = 0,
ESP_HF_NETWORK_STATE_AVAILABLE
} esp_hf_network_state_t;
/** +CIEV Service type */
typedef enum
{
ESP_HF_SERVICE_TYPE_HOME = 0,
ESP_HF_SERVICE_TYPE_ROAMING
} esp_hf_service_type_t;
/// +CIND call status indicator values
typedef enum {
ESP_HF_CALL_STATUS_NO_CALLS = 0, /*!< no call in progress */
ESP_HF_CALL_STATUS_CALL_IN_PROGRESS = 1, /*!< call is present(active or held) */
} esp_hf_call_status_t;
/// +CIND call setup status indicator values
typedef enum {
ESP_HF_CALL_SETUP_STATUS_IDLE = 0, /*!< no call setup in progress */
ESP_HF_CALL_SETUP_STATUS_INCOMING = 1, /*!< incoming call setup in progress */
ESP_HF_CALL_SETUP_STATUS_OUTGOING_DIALING = 2, /*!< outgoing call setup in dialing state */
ESP_HF_CALL_SETUP_STATUS_OUTGOING_ALERTING = 3, /*!< outgoing call setup in alerting state */
} esp_hf_call_setup_status_t;
/// +CIND roaming status indicator values
typedef enum {
ESP_HF_ROAMING_STATUS_INACTIVE = 0, /*!< roaming is not active */
ESP_HF_ROAMING_STATUS_ACTIVE, /*!< a roaming is active */
} esp_hf_roaming_status_t;
/// +CIND call held indicator values
typedef enum {
ESP_HF_CALL_HELD_STATUS_NONE = 0, /*!< no calls held */
ESP_HF_CALL_HELD_STATUS_HELD_AND_ACTIVE = 1, /*!< both active and held call */
ESP_HF_CALL_HELD_STATUS_HELD = 2, /*!< call on hold, no active call*/
} esp_hf_call_held_status_t;
/// +CLCC status of the call
typedef enum {
ESP_HF_CURRENT_CALL_STATUS_ACTIVE = 0, /*!< active */
ESP_HF_CURRENT_CALL_STATUS_HELD = 1, /*!< held */
ESP_HF_CURRENT_CALL_STATUS_DIALING = 2, /*!< dialing (outgoing calls only) */
ESP_HF_CURRENT_CALL_STATUS_ALERTING = 3, /*!< alerting (outgoing calls only) */
ESP_HF_CURRENT_CALL_STATUS_INCOMING = 4, /*!< incoming (incoming calls only) */
ESP_HF_CURRENT_CALL_STATUS_WAITING = 5, /*!< waiting (incoming calls only) */
ESP_HF_CURRENT_CALL_STATUS_HELD_BY_RESP_HOLD = 6, /*!< call held by response and hold */
} esp_hf_current_call_status_t;
/// +CLCC direction of the call
typedef enum {
ESP_HF_CURRENT_CALL_DIRECTION_OUTGOING = 0, /*!< outgoing */
ESP_HF_CURRENT_CALL_DIRECTION_INCOMING = 1, /*!< incoming */
} esp_hf_current_call_direction_t;
/// +CLCC multi-party call flag
typedef enum {
ESP_HF_CURRENT_CALL_MPTY_TYPE_SINGLE = 0, /*!< not a member of a multi-party call */
ESP_HF_CURRENT_CALL_MPTY_TYPE_MULTI = 1, /*!< member of a multi-party call */
} esp_hf_current_call_mpty_type_t;
/// +CLCC call mode
typedef enum {
ESP_HF_CURRENT_CALL_MODE_VOICE = 0,
ESP_HF_CURRENT_CALL_MODE_DATA = 1,
ESP_HF_CURRENT_CALL_MODE_FAX = 2,
} esp_hf_current_call_mode_t;
/// +CLCC address type
typedef enum {
ESP_HF_CALL_ADDR_TYPE_UNKNOWN = 0x81, /*!< unkown address type */
ESP_HF_CALL_ADDR_TYPE_INTERNATIONAL = 0x91, /*!< international address */
} esp_hf_call_addr_type_t;
/// +CNUM service type of the phone number
typedef enum {
ESP_HF_SUBSCRIBER_SERVICE_TYPE_UNKNOWN = 0, /*!< unknown */
ESP_HF_SUBSCRIBER_SERVICE_TYPE_VOICE, /*!< voice service */
ESP_HF_SUBSCRIBER_SERVICE_TYPE_FAX, /*!< fax service */
} esp_hf_subscriber_service_type_t;
/// +BTRH response and hold result code
typedef enum {
ESP_HF_BTRH_STATUS_HELD = 0, /*!< incoming call is put on held in AG */
ESP_HF_BTRH_STATUS_ACCEPTED, /*!< held incoming call is accepted in AG */
ESP_HF_BTRH_STATUS_REJECTED, /*!< held incoming call is rejected in AG */
} esp_hf_btrh_status_t;
/// AT+BTRH response and hold action code
typedef enum {
ESP_HF_BTRH_CMD_HOLD = 0, /*!< put the incoming call on hold */
ESP_HF_BTRH_CMD_ACCEPT = 1, /*!< accept a held incoming call */
ESP_HF_BTRH_CMD_REJECT = 2, /*!< reject a held incoming call */
} esp_hf_btrh_cmd_t;
/* +NREC */
typedef enum
{
ESP_HF_NREC_STOP = 0,
ESP_HF_NREC_START
} esp_hf_nrec_t;
///+CCWA resposne status
typedef enum {
ESP_HF_CALL_WAITING_INACTIVE,
ESP_HF_CALL_WAITING_ACTIVE,
} esp_hf_call_waiting_status_t;
/* WBS codec setting */
typedef enum
{
ESP_HF_WBS_NONE,
ESP_HF_WBS_NO,
ESP_HF_WBS_YES
}esp_hf_wbs_config_t;
/// Bluetooth HFP RFCOMM connection and service level connection status
typedef enum {
ESP_HF_CONNECTION_STATE_DISCONNECTED = 0, /*!< RFCOMM data link channel released */
ESP_HF_CONNECTION_STATE_CONNECTING, /*!< connecting remote device on the RFCOMM data link*/
ESP_HF_CONNECTION_STATE_CONNECTED, /*!< RFCOMM connection established */
ESP_HF_CONNECTION_STATE_SLC_CONNECTED, /*!< service level connection established */
ESP_HF_CONNECTION_STATE_DISCONNECTING, /*!< disconnecting with remote device on the RFCOMM data link*/
} esp_hf_connection_state_t;
/// AT+CHLD command values
typedef enum {
ESP_HF_CHLD_TYPE_REL = 0, /*!< <0>, Terminate all held or set UDUB("busy") to a waiting call */
ESP_HF_CHLD_TYPE_REL_ACC, /*!< <1>, Terminate all active calls and accepts a waiting/held call */
ESP_HF_CHLD_TYPE_HOLD_ACC, /*!< <2>, Hold all active calls and accepts a waiting/held call */
ESP_HF_CHLD_TYPE_MERGE, /*!< <3>, Add all held calls to a conference */
ESP_HF_CHLD_TYPE_MERGE_DETACH, /*!< <4>, connect the two calls and disconnects the subscriber from both calls */
ESP_HF_CHLD_TYPE_REL_X, /*!< <1x>, releases specified calls only */
ESP_HF_CHLD_TYPE_PRIV_X, /*!< <2x>, request private consultation mode with specified call */
} esp_hf_chld_type_t;
/* AT response code - OK/Error */
typedef enum {
ESP_HF_AT_RESPONSE_CODE_OK = 0, /*!< acknowledges execution of a command line */
ESP_HF_AT_RESPONSE_CODE_ERR, /*!< command not accepted */
ESP_HF_AT_RESPONSE_CODE_NO_CARRIER, /*!< connection terminated */
ESP_HF_AT_RESPONSE_CODE_BUSY, /*!< busy signal detected */
ESP_HF_AT_RESPONSE_CODE_NO_ANSWER, /*!< connection completion timeout */
ESP_HF_AT_RESPONSE_CODE_DELAYED, /*!< delayed */
ESP_HF_AT_RESPONSE_CODE_BLACKLISTED, /*!< blacklisted */
ESP_HF_AT_RESPONSE_CODE_CME, /*!< CME error */
} esp_hf_at_response_code_t;
/* AT response code - OK/Error */
typedef enum {
ESP_HF_AT_RESPONSE_ERROR = 0,
ESP_HF_AT_RESPONSE_OK
} esp_hf_at_response_t;
/// Extended Audio Gateway Error Result Code Response
typedef enum {
ESP_HF_CME_AG_FAILURE = 0, /*!< ag failure */
ESP_HF_CME_NO_CONNECTION_TO_PHONE = 1, /*!< no connection to phone */
ESP_HF_CME_OPERATION_NOT_ALLOWED = 3, /*!< operation not allowed */
ESP_HF_CME_OPERATION_NOT_SUPPORTED = 4, /*!< operation not supported */
ESP_HF_CME_PH_SIM_PIN_REQUIRED = 5, /*!< PH-SIM PIN Required */
ESP_HF_CME_SIM_NOT_INSERTED = 10, /*!< SIM not inserted */
ESP_HF_CME_SIM_PIN_REQUIRED = 11, /*!< SIM PIN required */
ESP_HF_CME_SIM_PUK_REQUIRED = 12, /*!< SIM PUK required */
ESP_HF_CME_SIM_FAILURE = 13, /*!< SIM failure */
ESP_HF_CME_SIM_BUSY = 14, /*!< SIM busy */
ESP_HF_CME_INCORRECT_PASSWORD = 16, /*!< incorrect password */
ESP_HF_CME_SIM_PIN2_REQUIRED = 17, /*!< SIM PIN2 required */
ESP_HF_CME_SIM_PUK2_REQUIRED = 18, /*!< SIM PUK2 required */
ESP_HF_CME_MEMEORY_FULL = 20, /*!< memory full */
ESP_HF_CME_INVALID_INDEX = 21, /*!< invalid index */
ESP_HF_CME_MEMEORY_FAILURE = 23, /*!< memory failure */
ESP_HF_CME_TEXT_STRING_TOO_LONG = 24, /*!< test string too long */
ESP_HF_CME_INVALID_CHARACTERS_IN_TEXT_STRING = 25, /*!< invalid characters in text string */
ESP_HF_CME_DIAL_STRING_TOO_LONG = 26, /*!< dial string too long*/
ESP_HF_CME_INVALID_CHARACTERS_IN_DIAL_STRING = 27, /*!< invalid characters in dial string */
ESP_HF_CME_NO_NETWORK_SERVICE = 30, /*!< no network service */
ESP_HF_CME_NETWORK_TIMEOUT = 31, /*!< network timeout */
ESP_HF_CME_NETWORK_NOT_ALLOWED = 32, /*!< network not allowed --emergency calls only */
} esp_hf_cme_err_t;
/** Callback for connection state change.
* state will have one of the values from BtHfConnectionState
*/
typedef void (* esp_hf_connection_state_callback)(esp_hf_connection_state_t state, esp_bd_addr_t *bd_addr);
/** Callback for audio connection state change.
* state will have one of the values from BtHfAudioState
*/
typedef void (* esp_hf_audio_state_callback)(esp_hf_audio_state_t state, esp_bd_addr_t *bd_addr);
/** Callback for VR connection state change.
* state will have one of the values from BtHfVRState
*/
typedef void (* esp_hf_vr_cmd_callback)(esp_hf_vr_state_t state, esp_bd_addr_t *bd_addr);
/** Callback for answer incoming call (ATA)
*/
typedef void (* esp_hf_answer_call_cmd_callback)(esp_bd_addr_t *bd_addr);
/** Callback for disconnect call (AT+CHUP)
*/
typedef void (* esp_hf_hangup_call_cmd_callback)(esp_bd_addr_t *bd_addr);
/** Callback for disconnect call (AT+CHUP)
* type will denote Speaker/Mic gain (BtHfVolumeControl).
*/
typedef void (* esp_hf_volume_cmd_callback)(esp_hf_volume_control_target_t type, int volume, esp_bd_addr_t *bd_addr);
/** Callback for dialing an outgoing call
* If number is NULL, redial
*/
typedef void (* esp_hf_dial_call_cmd_callback)(char *number, esp_bd_addr_t *bd_addr);
/** Callback for sending DTMF tones
* tone contains the dtmf character to be sent
*/
typedef void (* esp_hf_dtmf_cmd_callback)(char tone, esp_bd_addr_t *bd_addr);
/** Callback for enabling/disabling noise reduction/echo cancellation
* value will be 1 to enable, 0 to disable
*/
typedef void (* esp_hf_nrec_cmd_callback)(esp_hf_nrec_t nrec, esp_bd_addr_t *bd_addr);
/** Callback for AT+BCS and event from BAC
* WBS enable, WBS disable
*/
typedef void (* esp_hf_wbs_callback)(esp_hf_wbs_config_t wbs, esp_bd_addr_t *bd_addr);
/** Callback for call hold handling (AT+CHLD)
* value will contain the call hold command (0, 1, 2, 3)
*/
typedef void (* esp_hf_chld_cmd_callback)(esp_hf_chld_type_t chld, esp_bd_addr_t *bd_addr);
/** Callback for CNUM (subscriber number)
*/
typedef void (* esp_hf_cnum_cmd_callback)(esp_bd_addr_t *bd_addr);
/** Callback for indicators (CIND)
*/
typedef void (* esp_hf_cind_cmd_callback)(esp_bd_addr_t *bd_addr);
/** Callback for operator selection (COPS)
*/
typedef void (* esp_hf_cops_cmd_callback)(esp_bd_addr_t *bd_addr);
/** Callback for call list (AT+CLCC)
*/
typedef void (* esp_hf_clcc_cmd_callback) (esp_bd_addr_t *bd_addr);
/** Callback for unknown AT command recd from AG
* at_string will contain the unparsed AT string
*/
typedef void (* esp_hf_unknown_at_cmd_callback)(char *at_string, esp_bd_addr_t *bd_addr);
/** Callback for keypressed (HSP) event.
*/
typedef void (* esp_hf_key_pressed_cmd_callback)(esp_bd_addr_t *bd_addr);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_HF_DEFS_H__ */

View File

@ -0,0 +1,372 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_SPP_API_H__
#define __ESP_SPP_API_H__
#include "esp_err.h"
#include "esp_bt_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
ESP_SPP_SUCCESS = 0, /*!< Successful operation. */
ESP_SPP_FAILURE, /*!< Generic failure. */
ESP_SPP_BUSY, /*!< Temporarily can not handle this request. */
ESP_SPP_NO_DATA, /*!< No data */
ESP_SPP_NO_RESOURCE, /*!< No more resource */
ESP_SPP_NEED_INIT, /*!< SPP module shall init first */
ESP_SPP_NEED_DEINIT, /*!< SPP module shall deinit first */
ESP_SPP_NO_CONNECTION, /*!< Connection may have been closed */
ESP_SPP_NO_SERVER, /*!< No SPP server */
} esp_spp_status_t;
/* Security Setting Mask
Use these three mask mode:
1. ESP_SPP_SEC_NONE
2. ESP_SPP_SEC_AUTHENTICATE
3. (ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE)
*/
#define ESP_SPP_SEC_NONE 0x0000 /*!< No security. relate to BTA_SEC_NONE in bta/bta_api.h */
#define ESP_SPP_SEC_AUTHORIZE 0x0001 /*!< Authorization required (only needed for out going connection ) relate to BTA_SEC_AUTHORIZE in bta/bta_api.h*/
#define ESP_SPP_SEC_AUTHENTICATE 0x0012 /*!< Authentication required. relate to BTA_SEC_AUTHENTICATE in bta/bta_api.h*/
#define ESP_SPP_SEC_ENCRYPT 0x0024 /*!< Encryption required. relate to BTA_SEC_ENCRYPT in bta/bta_api.h*/
#define ESP_SPP_SEC_MODE4_LEVEL4 0x0040 /*!< Mode 4 level 4 service, i.e. incoming/outgoing MITM and P-256 encryption relate to BTA_SEC_MODE4_LEVEL4 in bta/bta_api.h*/
#define ESP_SPP_SEC_MITM 0x3000 /*!< Man-In-The_Middle protection relate to BTA_SEC_MITM in bta/bta_api.h*/
#define ESP_SPP_SEC_IN_16_DIGITS 0x4000 /*!< Min 16 digit for pin code relate to BTA_SEC_IN_16_DIGITS in bta/bta_api.h*/
typedef uint16_t esp_spp_sec_t;
typedef enum {
ESP_SPP_ROLE_MASTER = 0, /*!< Role: master */
ESP_SPP_ROLE_SLAVE = 1, /*!< Role: slave */
} esp_spp_role_t;
typedef enum {
ESP_SPP_MODE_CB = 0, /*!< When data is coming, a callback will come with data */
ESP_SPP_MODE_VFS = 1, /*!< Use VFS to write/read data */
} esp_spp_mode_t;
#define ESP_SPP_MAX_MTU (3*330) /*!< SPP max MTU */
#define ESP_SPP_MAX_SCN 31 /*!< SPP max SCN */
/**
* @brief SPP callback function events
*/
typedef enum {
ESP_SPP_INIT_EVT = 0, /*!< When SPP is inited, the event comes */
ESP_SPP_UNINIT_EVT = 1, /*!< When SPP is uninited, the event comes */
ESP_SPP_DISCOVERY_COMP_EVT = 8, /*!< When SDP discovery complete, the event comes */
ESP_SPP_OPEN_EVT = 26, /*!< When SPP Client connection open, the event comes */
ESP_SPP_CLOSE_EVT = 27, /*!< When SPP connection closed, the event comes */
ESP_SPP_START_EVT = 28, /*!< When SPP server started, the event comes */
ESP_SPP_CL_INIT_EVT = 29, /*!< When SPP client initiated a connection, the event comes */
ESP_SPP_DATA_IND_EVT = 30, /*!< When SPP connection received data, the event comes, only for ESP_SPP_MODE_CB */
ESP_SPP_CONG_EVT = 31, /*!< When SPP connection congestion status changed, the event comes, only for ESP_SPP_MODE_CB */
ESP_SPP_WRITE_EVT = 33, /*!< When SPP write operation completes, the event comes, only for ESP_SPP_MODE_CB */
ESP_SPP_SRV_OPEN_EVT = 34, /*!< When SPP Server connection open, the event comes */
ESP_SPP_SRV_STOP_EVT = 35, /*!< When SPP server stopped, the event comes */
} esp_spp_cb_event_t;
/**
* @brief SPP callback parameters union
*/
typedef union {
/**
* @brief SPP_INIT_EVT
*/
struct spp_init_evt_param {
esp_spp_status_t status; /*!< status */
} init; /*!< SPP callback param of SPP_INIT_EVT */
/**
* @brief SPP_UNINIT_EVT
*/
struct spp_uninit_evt_param {
esp_spp_status_t status; /*!< status */
} uninit; /*!< SPP callback param of SPP_UNINIT_EVT */
/**
* @brief SPP_DISCOVERY_COMP_EVT
*/
struct spp_discovery_comp_evt_param {
esp_spp_status_t status; /*!< status */
uint8_t scn_num; /*!< The num of scn_num */
uint8_t scn[ESP_SPP_MAX_SCN]; /*!< channel # */
const char *service_name[ESP_SPP_MAX_SCN]; /*!< service_name */
} disc_comp; /*!< SPP callback param of SPP_DISCOVERY_COMP_EVT */
/**
* @brief ESP_SPP_OPEN_EVT
*/
struct spp_open_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
int fd; /*!< The file descriptor only for ESP_SPP_MODE_VFS */
esp_bd_addr_t rem_bda; /*!< The peer address */
} open; /*!< SPP callback param of ESP_SPP_OPEN_EVT */
/**
* @brief ESP_SPP_SRV_OPEN_EVT
*/
struct spp_srv_open_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
uint32_t new_listen_handle; /*!< The new listen handle */
int fd; /*!< The file descriptor only for ESP_SPP_MODE_VFS */
esp_bd_addr_t rem_bda; /*!< The peer address */
} srv_open; /*!< SPP callback param of ESP_SPP_SRV_OPEN_EVT */
/**
* @brief ESP_SPP_CLOSE_EVT
*/
struct spp_close_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t port_status; /*!< PORT status */
uint32_t handle; /*!< The connection handle */
bool async; /*!< FALSE, if local initiates disconnect */
} close; /*!< SPP callback param of ESP_SPP_CLOSE_EVT */
/**
* @brief ESP_SPP_START_EVT
*/
struct spp_start_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
uint8_t sec_id; /*!< security ID used by this server */
uint8_t scn; /*!< Server channel number */
bool use_co; /*!< TRUE to use co_rfc_data */
} start; /*!< SPP callback param of ESP_SPP_START_EVT */
/**
* @brief ESP_SPP_SRV_STOP_EVT
*/
struct spp_srv_stop_evt_param {
esp_spp_status_t status; /*!< status */
uint8_t scn; /*!< Server channel number */
} srv_stop; /*!< SPP callback param of ESP_SPP_SRV_STOP_EVT */
/**
* @brief ESP_SPP_CL_INIT_EVT
*/
struct spp_cl_init_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
uint8_t sec_id; /*!< security ID used by this server */
bool use_co; /*!< TRUE to use co_rfc_data */
} cl_init; /*!< SPP callback param of ESP_SPP_CL_INIT_EVT */
/**
* @brief ESP_SPP_WRITE_EVT
*/
struct spp_write_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
int len; /*!< The length of the data written. */
bool cong; /*!< congestion status */
} write; /*!< SPP callback param of ESP_SPP_WRITE_EVT */
/**
* @brief ESP_SPP_DATA_IND_EVT
*/
struct spp_data_ind_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
uint16_t len; /*!< The length of data */
uint8_t *data; /*!< The data received */
} data_ind; /*!< SPP callback param of ESP_SPP_DATA_IND_EVT */
/**
* @brief ESP_SPP_CONG_EVT
*/
struct spp_cong_evt_param {
esp_spp_status_t status; /*!< status */
uint32_t handle; /*!< The connection handle */
bool cong; /*!< TRUE, congested. FALSE, uncongested */
} cong; /*!< SPP callback param of ESP_SPP_CONG_EVT */
} esp_spp_cb_param_t; /*!< SPP callback parameter union type */
/**
* @brief SPP callback function type.
* When handle ESP_SPP_DATA_IND_EVT, it is strongly recommended to cache incoming data, and process them in
* other lower priority application task rather than in this callback directly.
*
* @param event: Event type
* @param param: Point to callback parameter, currently is union type
*/
typedef void (esp_spp_cb_t)(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
/**
* @brief This function is called to init callbacks with SPP module.
*
* @param[in] callback: pointer to the init callback function.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_register_callback(esp_spp_cb_t callback);
/**
* @brief This function is called to init SPP module.
* When the operation is completed, the callback function will be called with ESP_SPP_INIT_EVT.
* This function should be called after esp_bluedroid_enable() completes successfully.
*
* @param[in] mode: Choose the mode of SPP, ESP_SPP_MODE_CB or ESP_SPP_MODE_VFS.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_init(esp_spp_mode_t mode);
/**
* @brief This function is called to uninit SPP module.
* The operation will close all active SPP connection first, then the callback function will be called
* with ESP_SPP_CLOSE_EVT, and the number of ESP_SPP_CLOSE_EVT is equal to the number of connection.
* When the operation is completed, the callback function will be called with ESP_SPP_UNINIT_EVT.
* This function should be called after esp_spp_init() completes successfully.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_deinit(void);
/**
* @brief This function is called to performs service discovery for the services provided by the given peer device.
* When the operation is completed, the callback function will be called with ESP_SPP_DISCOVERY_COMP_EVT.
* This funciton must be called after esp_spp_init() successful and before esp_spp_deinit().
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_start_discovery(esp_bd_addr_t bd_addr);
/**
* @brief This function makes an SPP connection to a remote BD Address.
* When the connection is initiated or failed to initiate, the callback is called with ESP_SPP_CL_INIT_EVT.
* When the connection is established or failed, the callback is called with ESP_SPP_OPEN_EVT.
* This funciton must be called after esp_spp_init() successful and before esp_spp_deinit().
*
* @param[in] sec_mask: Security Setting Mask. Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only.
* @param[in] role: Master or slave.
* @param[in] remote_scn: Remote device bluetooth device SCN.
* @param[in] peer_bd_addr: Remote device bluetooth device address.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_connect(esp_spp_sec_t sec_mask, esp_spp_role_t role, uint8_t remote_scn, esp_bd_addr_t peer_bd_addr);
/**
* @brief This function closes an SPP connection.
* When the operation is completed, the callback function will be called with ESP_SPP_CLOSE_EVT.
* This funciton must be called after esp_spp_init() successful and before esp_spp_deinit().
*
* @param[in] handle: The connection handle.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_disconnect(uint32_t handle);
/**
* @brief This function create a SPP server and starts listening for an
* SPP connection request from a remote Bluetooth device.
* When the server is started successfully, the callback is called with ESP_SPP_START_EVT.
* When the connection is established, the callback is called with ESP_SPP_SRV_OPEN_EVT.
* This funciton must be called after esp_spp_init() successful and before esp_spp_deinit().
*
* @param[in] sec_mask: Security Setting Mask. Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only.
* @param[in] role: Master or slave.
* @param[in] local_scn: The specific channel you want to get.
* If channel is 0, means get any channel.
* @param[in] name: Server's name.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_start_srv(esp_spp_sec_t sec_mask, esp_spp_role_t role, uint8_t local_scn, const char *name);
/**
* @brief This function stops all SPP servers.
* The operation will close all active SPP connection first, then the callback function will be called
* with ESP_SPP_CLOSE_EVT, and the number of ESP_SPP_CLOSE_EVT is equal to the number of connection.
* When the operation is completed, the callback is called with ESP_SPP_SRV_STOP_EVT.
* This funciton must be called after esp_spp_init() successful and before esp_spp_deinit().
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_stop_srv(void);
/**
* @brief This function stops a specific SPP server.
* The operation will close all active SPP connection first on the specific SPP server, then the callback function will be called
* with ESP_SPP_CLOSE_EVT, and the number of ESP_SPP_CLOSE_EVT is equal to the number of connection.
* When the operation is completed, the callback is called with ESP_SPP_SRV_STOP_EVT.
* This funciton must be called after esp_spp_init() successful and before esp_spp_deinit().
*
* @param[in] scn: Server channel number.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_stop_srv_scn(uint8_t scn);
/**
* @brief This function is used to write data, only for ESP_SPP_MODE_CB.
* When this function need to be called repeatedly, it is strongly recommended to call this function again after
* the previous event ESP_SPP_WRITE_EVT is received and the parameter 'cong' is equal to false. If the previous event
* ESP_SPP_WRITE_EVT with parameter 'cong' is equal to true, the function can only be called again when the event
* ESP_SPP_CONG_EVT with parameter 'cong' equal to false is received.
* This funciton must be called after an connection between initiator and acceptor has been established.
*
* @param[in] handle: The connection handle.
* @param[in] len: The length of the data written.
* @param[in] p_data: The data written.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data);
/**
* @brief This function is used to register VFS.
* For now, SPP only supports write, read and close.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_spp_vfs_register(void);
#ifdef __cplusplus
}
#endif
#endif ///__ESP_SPP_API_H__

View File

@ -0,0 +1,503 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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.
#ifndef __ESP_BT_H__
#define __ESP_BT_H__
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "sdkconfig.h"
#include "esp_task.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL 0x20200622
/**
* @brief Bluetooth mode for controller enable/disable
*/
typedef enum {
ESP_BT_MODE_IDLE = 0x00, /*!< Bluetooth is not running */
ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */
ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */
ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */
} esp_bt_mode_t;
/**
* @brief BLE sleep clock accuracy(SCA), values for ble_sca field in esp_bt_controller_config_t,
* currently only ESP_BLE_SCA_500PPM and ESP_BLE_SCA_250PPM are supported
*/
enum {
ESP_BLE_SCA_500PPM = 0, /*!< BLE SCA at 500ppm */
ESP_BLE_SCA_250PPM, /*!< BLE SCA at 250ppm */
ESP_BLE_SCA_150PPM, /*!< BLE SCA at 150ppm */
ESP_BLE_SCA_100PPM, /*!< BLE SCA at 100ppm */
ESP_BLE_SCA_75PPM, /*!< BLE SCA at 75ppm */
ESP_BLE_SCA_50PPM, /*!< BLE SCA at 50ppm */
ESP_BLE_SCA_30PPM, /*!< BLE SCA at 30ppm */
ESP_BLE_SCA_20PPM, /*!< BLE SCA at 20ppm */
};
#ifdef CONFIG_BT_ENABLED
/* While scanning, if the free memory value in controller is less than SCAN_SEND_ADV_RESERVED_SIZE,
the adv packet will be discarded until the memory is restored. */
#define SCAN_SEND_ADV_RESERVED_SIZE 1000
/* enable controller log debug when adv lost */
#define CONTROLLER_ADV_LOST_DEBUG_BIT (0<<0)
#ifdef CONFIG_BTDM_CTRL_HCI_UART_NO
#define BT_HCI_UART_NO_DEFAULT CONFIG_BTDM_CTRL_HCI_UART_NO
#else
#define BT_HCI_UART_NO_DEFAULT 1
#endif /* BT_HCI_UART_NO_DEFAULT */
#ifdef CONFIG_BTDM_CTRL_HCI_UART_BAUDRATE
#define BT_HCI_UART_BAUDRATE_DEFAULT CONFIG_BTDM_CTRL_HCI_UART_BAUDRATE
#else
#define BT_HCI_UART_BAUDRATE_DEFAULT 921600
#endif /* BT_HCI_UART_BAUDRATE_DEFAULT */
#ifdef CONFIG_BTDM_SCAN_DUPL_TYPE
#define SCAN_DUPLICATE_TYPE_VALUE CONFIG_BTDM_SCAN_DUPL_TYPE
#else
#define SCAN_DUPLICATE_TYPE_VALUE 0
#endif
/* normal adv cache size */
#ifdef CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE
#define NORMAL_SCAN_DUPLICATE_CACHE_SIZE CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE
#else
#define NORMAL_SCAN_DUPLICATE_CACHE_SIZE 20
#endif
#ifndef CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN
#define CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN FALSE
#endif
#define SCAN_DUPLICATE_MODE_NORMAL_ADV_ONLY 0
#define SCAN_DUPLICATE_MODE_NORMAL_ADV_MESH_ADV 1
#if CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN
#define SCAN_DUPLICATE_MODE SCAN_DUPLICATE_MODE_NORMAL_ADV_MESH_ADV
#ifdef CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE
#define MESH_DUPLICATE_SCAN_CACHE_SIZE CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE
#else
#define MESH_DUPLICATE_SCAN_CACHE_SIZE 50
#endif
#else
#define SCAN_DUPLICATE_MODE SCAN_DUPLICATE_MODE_NORMAL_ADV_ONLY
#define MESH_DUPLICATE_SCAN_CACHE_SIZE 0
#endif
#if defined(CONFIG_BTDM_CTRL_MODE_BLE_ONLY)
#define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_BLE
#elif defined(CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY)
#define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_CLASSIC_BT
#else
#define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_BTDM
#endif
#ifdef CONFIG_BTDM_CTRL_AUTO_LATENCY_EFF
#define BTDM_CTRL_AUTO_LATENCY_EFF CONFIG_BTDM_CTRL_AUTO_LATENCY_EFF
#else
#define BTDM_CTRL_AUTO_LATENCY_EFF false
#endif
#ifdef CONFIG_BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF
#define BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF CONFIG_BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF
#else
#define BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF false
#endif
#define BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT 9 //Maximum BLE connection limitation
#define BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT 7 //Maximum ACL connection limitation
#define BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT 3 //Maximum SCO/eSCO connection limitation
#define BTDM_CONTROLLER_SCO_DATA_PATH_HCI 0 // SCO data is routed to HCI
#define BTDM_CONTROLLER_SCO_DATA_PATH_PCM 1 // SCO data path is PCM
#define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \
.controller_task_stack_size = ESP_TASK_BT_CONTROLLER_STACK, \
.controller_task_prio = ESP_TASK_BT_CONTROLLER_PRIO, \
.hci_uart_no = BT_HCI_UART_NO_DEFAULT, \
.hci_uart_baudrate = BT_HCI_UART_BAUDRATE_DEFAULT, \
.scan_duplicate_mode = SCAN_DUPLICATE_MODE, \
.scan_duplicate_type = SCAN_DUPLICATE_TYPE_VALUE, \
.normal_adv_size = NORMAL_SCAN_DUPLICATE_CACHE_SIZE, \
.mesh_adv_size = MESH_DUPLICATE_SCAN_CACHE_SIZE, \
.send_adv_reserved_size = SCAN_SEND_ADV_RESERVED_SIZE, \
.controller_debug_flag = CONTROLLER_ADV_LOST_DEBUG_BIT, \
.mode = BTDM_CONTROLLER_MODE_EFF, \
.ble_max_conn = CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF, \
.bt_max_acl_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF, \
.bt_sco_datapath = CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF, \
.auto_latency = BTDM_CTRL_AUTO_LATENCY_EFF, \
.bt_legacy_auth_vs_evt = BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF, \
.bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF, \
.ble_sca = CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF, \
.pcm_role = CONFIG_BTDM_CTRL_PCM_ROLE_EFF, \
.pcm_polar = CONFIG_BTDM_CTRL_PCM_POLAR_EFF, \
.magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL, \
};
#else
#define BT_CONTROLLER_INIT_CONFIG_DEFAULT() {0}; _Static_assert(0, "please enable bluetooth in menuconfig to use bt.h");
#endif
/**
* @brief Controller config options, depend on config mask.
* Config mask indicate which functions enabled, this means
* some options or parameters of some functions enabled by config mask.
*/
typedef struct {
/*
* Following parameters can be configured runtime, when call esp_bt_controller_init()
*/
uint16_t controller_task_stack_size; /*!< Bluetooth controller task stack size */
uint8_t controller_task_prio; /*!< Bluetooth controller task priority */
uint8_t hci_uart_no; /*!< If use UART1/2 as HCI IO interface, indicate UART number */
uint32_t hci_uart_baudrate; /*!< If use UART1/2 as HCI IO interface, indicate UART baudrate */
uint8_t scan_duplicate_mode; /*!< scan duplicate mode */
uint8_t scan_duplicate_type; /*!< scan duplicate type */
uint16_t normal_adv_size; /*!< Normal adv size for scan duplicate */
uint16_t mesh_adv_size; /*!< Mesh adv size for scan duplicate */
uint16_t send_adv_reserved_size; /*!< Controller minimum memory value */
uint32_t controller_debug_flag; /*!< Controller debug log flag */
uint8_t mode; /*!< Controller mode: BR/EDR, BLE or Dual Mode */
uint8_t ble_max_conn; /*!< BLE maximum connection numbers */
uint8_t bt_max_acl_conn; /*!< BR/EDR maximum ACL connection numbers */
uint8_t bt_sco_datapath; /*!< SCO data path, i.e. HCI or PCM module */
bool auto_latency; /*!< BLE auto latency, used to enhance classic BT performance */
bool bt_legacy_auth_vs_evt; /*!< BR/EDR Legacy auth complete event required to protect from BIAS attack */
/*
* Following parameters can not be configured runtime when call esp_bt_controller_init()
* It will be overwrite with a constant value which in menuconfig or from a macro.
* So, do not modify the value when esp_bt_controller_init()
*/
uint8_t bt_max_sync_conn; /*!< BR/EDR maximum ACL connection numbers. Effective in menuconfig */
uint8_t ble_sca; /*!< BLE low power crystal accuracy index */
uint8_t pcm_role; /*!< PCM role (master & slave)*/
uint8_t pcm_polar; /*!< PCM polar trig (falling clk edge & rising clk edge) */
uint32_t magic; /*!< Magic number */
} esp_bt_controller_config_t;
/**
* @brief Bluetooth controller enable/disable/initialised/de-initialised status
*/
typedef enum {
ESP_BT_CONTROLLER_STATUS_IDLE = 0,
ESP_BT_CONTROLLER_STATUS_INITED,
ESP_BT_CONTROLLER_STATUS_ENABLED,
ESP_BT_CONTROLLER_STATUS_NUM,
} esp_bt_controller_status_t;
/**
* @brief BLE tx power type
* ESP_BLE_PWR_TYPE_CONN_HDL0-8: for each connection, and only be set after connection completed.
* when disconnect, the correspond TX power is not effected.
* ESP_BLE_PWR_TYPE_ADV : for advertising/scan response.
* ESP_BLE_PWR_TYPE_SCAN : for scan.
* ESP_BLE_PWR_TYPE_DEFAULT : if each connection's TX power is not set, it will use this default value.
* if neither in scan mode nor in adv mode, it will use this default value.
* If none of power type is set, system will use ESP_PWR_LVL_P3 as default for ADV/SCAN/CONN0-9.
*/
typedef enum {
ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, /*!< For connection handle 0 */
ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, /*!< For connection handle 1 */
ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, /*!< For connection handle 2 */
ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, /*!< For connection handle 3 */
ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, /*!< For connection handle 4 */
ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, /*!< For connection handle 5 */
ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, /*!< For connection handle 6 */
ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, /*!< For connection handle 7 */
ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, /*!< For connection handle 8 */
ESP_BLE_PWR_TYPE_ADV = 9, /*!< For advertising */
ESP_BLE_PWR_TYPE_SCAN = 10, /*!< For scan */
ESP_BLE_PWR_TYPE_DEFAULT = 11, /*!< For default, if not set other, it will use default value */
ESP_BLE_PWR_TYPE_NUM = 12, /*!< TYPE numbers */
} esp_ble_power_type_t;
/**
* @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm).
*/
typedef enum {
ESP_PWR_LVL_N12 = 0, /*!< Corresponding to -12dbm */
ESP_PWR_LVL_N9 = 1, /*!< Corresponding to -9dbm */
ESP_PWR_LVL_N6 = 2, /*!< Corresponding to -6dbm */
ESP_PWR_LVL_N3 = 3, /*!< Corresponding to -3dbm */
ESP_PWR_LVL_N0 = 4, /*!< Corresponding to 0dbm */
ESP_PWR_LVL_P3 = 5, /*!< Corresponding to +3dbm */
ESP_PWR_LVL_P6 = 6, /*!< Corresponding to +6dbm */
ESP_PWR_LVL_P9 = 7, /*!< Corresponding to +9dbm */
ESP_PWR_LVL_N14 = ESP_PWR_LVL_N12, /*!< Backward compatibility! Setting to -14dbm will actually result to -12dbm */
ESP_PWR_LVL_N11 = ESP_PWR_LVL_N9, /*!< Backward compatibility! Setting to -11dbm will actually result to -9dbm */
ESP_PWR_LVL_N8 = ESP_PWR_LVL_N6, /*!< Backward compatibility! Setting to -8dbm will actually result to -6dbm */
ESP_PWR_LVL_N5 = ESP_PWR_LVL_N3, /*!< Backward compatibility! Setting to -5dbm will actually result to -3dbm */
ESP_PWR_LVL_N2 = ESP_PWR_LVL_N0, /*!< Backward compatibility! Setting to -2dbm will actually result to 0dbm */
ESP_PWR_LVL_P1 = ESP_PWR_LVL_P3, /*!< Backward compatibility! Setting to +1dbm will actually result to +3dbm */
ESP_PWR_LVL_P4 = ESP_PWR_LVL_P6, /*!< Backward compatibility! Setting to +4dbm will actually result to +6dbm */
ESP_PWR_LVL_P7 = ESP_PWR_LVL_P9, /*!< Backward compatibility! Setting to +7dbm will actually result to +9dbm */
} esp_power_level_t;
/**
* @brief Bluetooth audio data transport path
*/
typedef enum {
ESP_SCO_DATA_PATH_HCI = 0, /*!< data over HCI transport */
ESP_SCO_DATA_PATH_PCM = 1, /*!< data over PCM interface */
} esp_sco_data_path_t;
/**
* @brief Set BLE TX power
* Connection Tx power should only be set after connection created.
* @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
* @param power_level: Power level(index) corresponding to absolute value(dbm)
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level);
/**
* @brief Get BLE TX power
* Connection Tx power should only be get after connection created.
* @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
* @return >= 0 - Power level, < 0 - Invalid
*/
esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type);
/**
* @brief Set BR/EDR TX power
* BR/EDR power control will use the power in range of minimum value and maximum value.
* The power level will effect the global BR/EDR TX power, such inquire, page, connection and so on.
* Please call the function after esp_bt_controller_enable and before any function which cause RF do TX.
* So you can call the function before doing discovery, profile init and so on.
* For example, if you want BR/EDR use the new TX power to do inquire, you should call
* this function before inquire. Another word, If call this function when BR/EDR is in inquire(ING),
* please do inquire again after call this function.
* Default minimum power level is ESP_PWR_LVL_N0, and maximum power level is ESP_PWR_LVL_P3.
* @param min_power_level: The minimum power level
* @param max_power_level: The maximum power level
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bredr_tx_power_set(esp_power_level_t min_power_level, esp_power_level_t max_power_level);
/**
* @brief Get BR/EDR TX power
* If the argument is not NULL, then store the corresponding value.
* @param min_power_level: The minimum power level
* @param max_power_level: The maximum power level
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bredr_tx_power_get(esp_power_level_t *min_power_level, esp_power_level_t *max_power_level);
/**
* @brief Set default SCO data path
* Should be called after controller is enabled, and before (e)SCO link is established
* @param data_path: SCO data path
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path);
/**
* @brief Initialize BT controller to allocate task and other resource.
* This function should be called only once, before any other BT functions are called.
* @param cfg: Initial configuration of BT controller. Different from previous version, there's a mode and some
* connection configuration in "cfg" to configure controller work mode and allocate the resource which is needed.
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg);
/**
* @brief De-initialize BT controller to free resource and delete task.
*
* This function should be called only once, after any other BT functions are called.
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bt_controller_deinit(void);
/**
* @brief Enable BT controller.
* Due to a known issue, you cannot call esp_bt_controller_enable() a second time
* to change the controller mode dynamically. To change controller mode, call
* esp_bt_controller_disable() and then call esp_bt_controller_enable() with the new mode.
* @param mode : the mode(BLE/BT/BTDM) to enable. For compatible of API, retain this argument. This mode must be
* equal as the mode in "cfg" of esp_bt_controller_init().
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
/**
* @brief Disable BT controller
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bt_controller_disable(void);
/**
* @brief Get BT controller is initialised/de-initialised/enabled/disabled
* @return status value
*/
esp_bt_controller_status_t esp_bt_controller_get_status(void);
/**
* @brief Get BT MAC address.
* @return Array pointer of length 6 storing MAC address value.
*/
uint8_t* esp_bt_get_mac(void);
/** @brief esp_vhci_host_callback
* used for vhci call host function to notify what host need to do
*/
typedef struct esp_vhci_host_callback {
void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
} esp_vhci_host_callback_t;
/** @brief esp_vhci_host_check_send_available
* used for check actively if the host can send packet to controller or not.
* @return true for ready to send, false means cannot send packet
*/
bool esp_vhci_host_check_send_available(void);
/** @brief esp_vhci_host_send_packet
* host send packet to controller
*
* Should not call this function from within a critical section
* or when the scheduler is suspended.
*
* @param data the packet point
* @param len the packet length
*/
void esp_vhci_host_send_packet(uint8_t *data, uint16_t len);
/** @brief esp_vhci_host_register_callback
* register the vhci reference callback
* struct defined by vhci_host_callback structure.
* @param callback esp_vhci_host_callback type variable
* @return ESP_OK - success, ESP_FAIL - failed
*/
esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback);
/** @brief esp_bt_controller_mem_release
* release the controller memory as per the mode
*
* This function releases the BSS, data and other sections of the controller to heap. The total size is about 70k bytes.
*
* esp_bt_controller_mem_release(mode) should be called only before esp_bt_controller_init()
* or after esp_bt_controller_deinit().
*
* Note that once BT controller memory is released, the process cannot be reversed. It means you cannot use the bluetooth
* mode which you have released by this function.
*
* If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled)
* then do not call this function.
*
* If the app calls esp_bt_controller_enable(ESP_BT_MODE_BLE) to use BLE only then it is safe to call
* esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT) at initialization time to free unused BT Classic memory.
*
* If the mode is ESP_BT_MODE_BTDM, then it may be useful to call API esp_bt_mem_release(ESP_BT_MODE_BTDM) instead,
* which internally calls esp_bt_controller_mem_release(ESP_BT_MODE_BTDM) and additionally releases the BSS and data
* consumed by the BT/BLE host stack to heap. For more details about usage please refer to the documentation of
* esp_bt_mem_release() function
*
* @param mode : the mode want to release memory
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode);
/** @brief esp_bt_mem_release
* release controller memory and BSS and data section of the BT/BLE host stack as per the mode
*
* This function first releases controller memory by internally calling esp_bt_controller_mem_release().
* Additionally, if the mode is set to ESP_BT_MODE_BTDM, it also releases the BSS and data consumed by the BT/BLE host stack to heap
*
* Note that once BT memory is released, the process cannot be reversed. It means you cannot use the bluetooth
* mode which you have released by this function.
*
* If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled)
* then do not call this function.
*
* If you never intend to use bluetooth in a current boot-up cycle, you can call esp_bt_mem_release(ESP_BT_MODE_BTDM)
* before esp_bt_controller_init or after esp_bt_controller_deinit.
*
* For example, if a user only uses bluetooth for setting the WiFi configuration, and does not use bluetooth in the rest of the product operation".
* In such cases, after receiving the WiFi configuration, you can disable/deinit bluetooth and release its memory.
* Below is the sequence of APIs to be called for such scenarios:
*
* esp_bluedroid_disable();
* esp_bluedroid_deinit();
* esp_bt_controller_disable();
* esp_bt_controller_deinit();
* esp_bt_mem_release(ESP_BT_MODE_BTDM);
*
* @note In case of NimBLE host, to release BSS and data memory to heap, the mode needs to be
* set to ESP_BT_MODE_BTDM as controller is dual mode.
* @param mode : the mode whose memory is to be released
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_bt_mem_release(esp_bt_mode_t mode);
/**
* @brief enable bluetooth to enter modem sleep
*
* Note that this function shall not be invoked before esp_bt_controller_enable()
*
* There are currently two options for bluetooth modem sleep, one is ORIG mode, and another is EVED Mode. EVED Mode is intended for BLE only.
*
* For ORIG mode:
* Bluetooth modem sleep is enabled in controller start up by default if CONFIG_CTRL_BTDM_MODEM_SLEEP is set and "ORIG mode" is selected. In ORIG modem sleep mode, bluetooth controller will switch off some components and pause to work every now and then, if there is no event to process; and wakeup according to the scheduled interval and resume the work. It can also wakeup earlier upon external request using function "esp_bt_controller_wakeup_request".
*
* @return
* - ESP_OK : success
* - other : failed
*/
esp_err_t esp_bt_sleep_enable(void);
/**
* @brief disable bluetooth modem sleep
*
* Note that this function shall not be invoked before esp_bt_controller_enable()
*
* If esp_bt_sleep_disable() is called, bluetooth controller will not be allowed to enter modem sleep;
*
* If ORIG modem sleep mode is in use, if this function is called, bluetooth controller may not immediately wake up if it is dormant then.
* In this case, esp_bt_controller_wakeup_request() can be used to shorten the time for wakeup.
*
* @return
* - ESP_OK : success
* - other : failed
*/
esp_err_t esp_bt_sleep_disable(void);
/**
* @brief Manually clear scan duplicate list
*
* Note that scan duplicate list will be automatically cleared when the maximum amount of device in the filter is reached
* the amount of device in the filter can be configured in menuconfig.
*
* @note This function name is incorrectly spelled, it will be fixed in release 5.x version.
*
* @return
* - ESP_OK : success
* - other : failed
*/
esp_err_t esp_ble_scan_dupilcate_list_flush(void);
#ifdef __cplusplus
}
#endif
#endif /* __ESP_BT_H__ */