12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679 |
- #ifndef ZEPHYR_INCLUDE_BLUETOOTH_GATT_H_
- #define ZEPHYR_INCLUDE_BLUETOOTH_GATT_H_
- #include <stddef.h>
- #include <sys/slist.h>
- #include <sys/types.h>
- #include <sys/util.h>
- #include <bluetooth/conn.h>
- #include <bluetooth/uuid.h>
- #include <bluetooth/att.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- enum {
-
- BT_GATT_PERM_NONE = 0,
-
- BT_GATT_PERM_READ = BIT(0),
-
- BT_GATT_PERM_WRITE = BIT(1),
-
- BT_GATT_PERM_READ_ENCRYPT = BIT(2),
-
- BT_GATT_PERM_WRITE_ENCRYPT = BIT(3),
-
- BT_GATT_PERM_READ_AUTHEN = BIT(4),
-
- BT_GATT_PERM_WRITE_AUTHEN = BIT(5),
-
- BT_GATT_PERM_PREPARE_WRITE = BIT(6),
- };
- #define BT_GATT_ERR(_att_err) (-(_att_err))
- enum {
-
- BT_GATT_WRITE_FLAG_PREPARE = BIT(0),
-
- BT_GATT_WRITE_FLAG_CMD = BIT(1),
- };
- struct bt_gatt_attr {
-
- const struct bt_uuid *uuid;
-
- ssize_t (*read)(struct bt_conn *conn, const struct bt_gatt_attr *attr,
- void *buf, uint16_t len, uint16_t offset);
-
- ssize_t (*write)(struct bt_conn *conn, const struct bt_gatt_attr *attr,
- const void *buf, uint16_t len, uint16_t offset,
- uint8_t flags);
-
- void *user_data;
-
- uint16_t handle;
-
- uint8_t perm;
- };
- struct bt_gatt_service_static {
-
- const struct bt_gatt_attr *attrs;
-
- size_t attr_count;
- };
- struct bt_gatt_service {
-
- struct bt_gatt_attr *attrs;
-
- size_t attr_count;
- sys_snode_t node;
- };
- struct bt_gatt_service_val {
-
- const struct bt_uuid *uuid;
-
- uint16_t end_handle;
- };
- struct bt_gatt_include {
-
- const struct bt_uuid *uuid;
-
- uint16_t start_handle;
-
- uint16_t end_handle;
- };
- struct bt_gatt_cb {
-
- void (*att_mtu_updated)(struct bt_conn *conn, uint16_t tx, uint16_t rx);
- sys_snode_t node;
- };
- #define BT_GATT_CHRC_BROADCAST 0x01
- #define BT_GATT_CHRC_READ 0x02
- #define BT_GATT_CHRC_WRITE_WITHOUT_RESP 0x04
- #define BT_GATT_CHRC_WRITE 0x08
- #define BT_GATT_CHRC_NOTIFY 0x10
- #define BT_GATT_CHRC_INDICATE 0x20
- #define BT_GATT_CHRC_AUTH 0x40
- #define BT_GATT_CHRC_EXT_PROP 0x80
- struct bt_gatt_chrc {
-
- const struct bt_uuid *uuid;
-
- uint16_t value_handle;
-
- uint8_t properties;
- };
- #define BT_GATT_CEP_RELIABLE_WRITE 0x0001
- #define BT_GATT_CEP_WRITABLE_AUX 0x0002
- struct bt_gatt_cep {
-
- uint16_t properties;
- };
- #define BT_GATT_CCC_NOTIFY 0x0001
- #define BT_GATT_CCC_INDICATE 0x0002
- struct bt_gatt_ccc {
-
- uint16_t flags;
- };
- #define BT_GATT_SCC_BROADCAST 0x0001
- struct bt_gatt_scc {
-
- uint16_t flags;
- };
- struct bt_gatt_cpf {
-
- uint8_t format;
-
- int8_t exponent;
-
- uint16_t unit;
-
- uint8_t name_space;
-
- uint16_t description;
- };
- void bt_gatt_cb_register(struct bt_gatt_cb *cb);
- int bt_gatt_service_register(struct bt_gatt_service *svc);
- int bt_gatt_service_unregister(struct bt_gatt_service *svc);
- enum {
- BT_GATT_ITER_STOP = 0,
- BT_GATT_ITER_CONTINUE,
- };
- typedef uint8_t (*bt_gatt_attr_func_t)(const struct bt_gatt_attr *attr,
- uint16_t handle,
- void *user_data);
- void bt_gatt_foreach_attr_type(uint16_t start_handle, uint16_t end_handle,
- const struct bt_uuid *uuid,
- const void *attr_data, uint16_t num_matches,
- bt_gatt_attr_func_t func,
- void *user_data);
- static inline void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle,
- bt_gatt_attr_func_t func,
- void *user_data)
- {
- bt_gatt_foreach_attr_type(start_handle, end_handle, NULL, NULL, 0, func,
- user_data);
- }
- struct bt_gatt_attr *bt_gatt_attr_next(const struct bt_gatt_attr *attr);
- struct bt_gatt_attr *bt_gatt_find_by_uuid(const struct bt_gatt_attr *attr,
- uint16_t attr_count,
- const struct bt_uuid *uuid);
- uint16_t bt_gatt_attr_get_handle(const struct bt_gatt_attr *attr);
- uint16_t bt_gatt_attr_value_handle(const struct bt_gatt_attr *attr);
- ssize_t bt_gatt_attr_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
- void *buf, uint16_t buf_len, uint16_t offset,
- const void *value, uint16_t value_len);
- ssize_t bt_gatt_attr_read_service(struct bt_conn *conn,
- const struct bt_gatt_attr *attr,
- void *buf, uint16_t len, uint16_t offset);
- #define BT_GATT_SERVICE_DEFINE(_name, ...) \
- const struct bt_gatt_attr attr_##_name[] = { __VA_ARGS__ }; \
- const STRUCT_SECTION_ITERABLE(bt_gatt_service_static, _name) = \
- BT_GATT_SERVICE(attr_##_name)
- #define _BT_GATT_ATTRS_ARRAY_DEFINE(n, _instances, _attrs_def) \
- static struct bt_gatt_attr attrs_##n[] = _attrs_def(_instances[n]);
- #define _BT_GATT_SERVICE_ARRAY_ITEM(_n, _) BT_GATT_SERVICE(attrs_##_n),
- #define BT_GATT_SERVICE_INSTANCE_DEFINE( \
- _name, _instances, _instance_num, _attrs_def) \
- BUILD_ASSERT(ARRAY_SIZE(_instances) == _instance_num, \
- "The number of array elements does not match its size"); \
- UTIL_EVAL(UTIL_REPEAT( \
- _instance_num, _BT_GATT_ATTRS_ARRAY_DEFINE, _instances, \
- _attrs_def)) \
- static struct bt_gatt_service _name[] = { \
- UTIL_LISTIFY(_instance_num, _BT_GATT_SERVICE_ARRAY_ITEM) \
- }
- #define BT_GATT_SERVICE(_attrs) \
- { \
- .attrs = _attrs, \
- .attr_count = ARRAY_SIZE(_attrs), \
- }
- #define BT_GATT_PRIMARY_SERVICE(_service) \
- BT_GATT_ATTRIBUTE(BT_UUID_GATT_PRIMARY, BT_GATT_PERM_READ, \
- bt_gatt_attr_read_service, NULL, _service)
- #define BT_GATT_SECONDARY_SERVICE(_service) \
- BT_GATT_ATTRIBUTE(BT_UUID_GATT_SECONDARY, BT_GATT_PERM_READ, \
- bt_gatt_attr_read_service, NULL, _service)
- ssize_t bt_gatt_attr_read_included(struct bt_conn *conn,
- const struct bt_gatt_attr *attr,
- void *buf, uint16_t len, uint16_t offset);
- #define BT_GATT_INCLUDE_SERVICE(_service_incl) \
- BT_GATT_ATTRIBUTE(BT_UUID_GATT_INCLUDE, BT_GATT_PERM_READ, \
- bt_gatt_attr_read_included, NULL, _service_incl)
- ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, void *buf,
- uint16_t len, uint16_t offset);
- #define BT_GATT_CHRC_INIT(_uuid, _handle, _props) \
- { \
- .uuid = _uuid, \
- .value_handle = _handle, \
- .properties = _props, \
- }
- #define BT_GATT_CHARACTERISTIC(_uuid, _props, _perm, _read, _write, _user_data) \
- BT_GATT_ATTRIBUTE(BT_UUID_GATT_CHRC, BT_GATT_PERM_READ, \
- bt_gatt_attr_read_chrc, NULL, \
- ((struct bt_gatt_chrc[]) { \
- BT_GATT_CHRC_INIT(_uuid, 0U, _props), \
- })), \
- BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data)
- #if defined(CONFIG_BT_SETTINGS_CCC_LAZY_LOADING)
- #define BT_GATT_CCC_MAX (CONFIG_BT_MAX_CONN)
- #elif defined(CONFIG_BT_CONN)
- #define BT_GATT_CCC_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
- #else
- #define BT_GATT_CCC_MAX 0
- #endif
- struct bt_gatt_ccc_cfg {
-
- uint8_t id;
-
- bt_addr_le_t peer;
-
- uint16_t value;
- };
- struct _bt_gatt_ccc {
-
- struct bt_gatt_ccc_cfg cfg[BT_GATT_CCC_MAX];
-
- uint16_t value;
-
- void (*cfg_changed)(const struct bt_gatt_attr *attr, uint16_t value);
-
- ssize_t (*cfg_write)(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, uint16_t value);
-
- bool (*cfg_match)(struct bt_conn *conn,
- const struct bt_gatt_attr *attr);
- };
- ssize_t bt_gatt_attr_read_ccc(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, void *buf,
- uint16_t len, uint16_t offset);
- ssize_t bt_gatt_attr_write_ccc(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, const void *buf,
- uint16_t len, uint16_t offset, uint8_t flags);
- #define BT_GATT_CCC_INITIALIZER(_changed, _write, _match) \
- { \
- .cfg = {}, \
- .cfg_changed = _changed, \
- .cfg_write = _write, \
- .cfg_match = _match, \
- }
- #define BT_GATT_CCC_MANAGED(_ccc, _perm) \
- BT_GATT_ATTRIBUTE(BT_UUID_GATT_CCC, _perm, \
- bt_gatt_attr_read_ccc, bt_gatt_attr_write_ccc, \
- _ccc)
- #define BT_GATT_CCC(_changed, _perm) \
- BT_GATT_CCC_MANAGED(((struct _bt_gatt_ccc[]) \
- {BT_GATT_CCC_INITIALIZER(_changed, NULL, NULL)}), _perm)
- ssize_t bt_gatt_attr_read_cep(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, void *buf,
- uint16_t len, uint16_t offset);
- #define BT_GATT_CEP(_value) \
- BT_GATT_DESCRIPTOR(BT_UUID_GATT_CEP, BT_GATT_PERM_READ, \
- bt_gatt_attr_read_cep, NULL, (void *)_value)
- ssize_t bt_gatt_attr_read_cud(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, void *buf,
- uint16_t len, uint16_t offset);
- #define BT_GATT_CUD(_value, _perm) \
- BT_GATT_DESCRIPTOR(BT_UUID_GATT_CUD, _perm, bt_gatt_attr_read_cud, \
- NULL, (void *)_value)
- ssize_t bt_gatt_attr_read_cpf(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, void *buf,
- uint16_t len, uint16_t offset);
- #define BT_GATT_CPF(_value) \
- BT_GATT_DESCRIPTOR(BT_UUID_GATT_CPF, BT_GATT_PERM_READ, \
- bt_gatt_attr_read_cpf, NULL, (void *)_value)
- #define BT_GATT_DESCRIPTOR(_uuid, _perm, _read, _write, _user_data) \
- BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data)
- #define BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data) \
- { \
- .uuid = _uuid, \
- .read = _read, \
- .write = _write, \
- .user_data = _user_data, \
- .handle = 0, \
- .perm = _perm, \
- }
- typedef void (*bt_gatt_complete_func_t) (struct bt_conn *conn, void *user_data);
- struct bt_gatt_notify_params {
-
- const struct bt_uuid *uuid;
-
- const struct bt_gatt_attr *attr;
-
- const void *data;
-
- uint16_t len;
-
- bt_gatt_complete_func_t func;
-
- void *user_data;
- };
- int bt_gatt_notify_cb(struct bt_conn *conn,
- struct bt_gatt_notify_params *params);
- int bt_gatt_notify_multiple(struct bt_conn *conn, uint16_t num_params,
- struct bt_gatt_notify_params *params);
- static inline int bt_gatt_notify(struct bt_conn *conn,
- const struct bt_gatt_attr *attr,
- const void *data, uint16_t len)
- {
- struct bt_gatt_notify_params params;
- memset(¶ms, 0, sizeof(params));
- params.attr = attr;
- params.data = data;
- params.len = len;
- return bt_gatt_notify_cb(conn, ¶ms);
- }
- static inline int bt_gatt_notify_uuid(struct bt_conn *conn,
- const struct bt_uuid *uuid,
- const struct bt_gatt_attr *attr,
- const void *data, uint16_t len)
- {
- struct bt_gatt_notify_params params;
- memset(¶ms, 0, sizeof(params));
- params.uuid = uuid;
- params.attr = attr;
- params.data = data;
- params.len = len;
- return bt_gatt_notify_cb(conn, ¶ms);
- }
- struct bt_gatt_indicate_params;
- typedef void (*bt_gatt_indicate_func_t)(struct bt_conn *conn,
- struct bt_gatt_indicate_params *params,
- uint8_t err);
- typedef void (*bt_gatt_indicate_params_destroy_t)(
- struct bt_gatt_indicate_params *params);
- struct bt_gatt_indicate_params {
-
- const struct bt_uuid *uuid;
-
- const struct bt_gatt_attr *attr;
-
- bt_gatt_indicate_func_t func;
-
- bt_gatt_indicate_params_destroy_t destroy;
-
- const void *data;
-
- uint16_t len;
-
- uint8_t _ref;
- };
- int bt_gatt_indicate(struct bt_conn *conn,
- struct bt_gatt_indicate_params *params);
- bool bt_gatt_is_subscribed(struct bt_conn *conn,
- const struct bt_gatt_attr *attr, uint16_t ccc_value);
- uint16_t bt_gatt_get_mtu(struct bt_conn *conn);
- struct bt_gatt_exchange_params {
-
- void (*func)(struct bt_conn *conn, uint8_t err,
- struct bt_gatt_exchange_params *params);
- };
- int bt_gatt_exchange_mtu(struct bt_conn *conn,
- struct bt_gatt_exchange_params *params);
- struct bt_gatt_discover_params;
- typedef uint8_t (*bt_gatt_discover_func_t)(struct bt_conn *conn,
- const struct bt_gatt_attr *attr,
- struct bt_gatt_discover_params *params);
- enum {
-
- BT_GATT_DISCOVER_PRIMARY,
-
- BT_GATT_DISCOVER_SECONDARY,
-
- BT_GATT_DISCOVER_INCLUDE,
-
- BT_GATT_DISCOVER_CHARACTERISTIC,
-
- BT_GATT_DISCOVER_DESCRIPTOR,
-
- BT_GATT_DISCOVER_ATTRIBUTE,
-
- BT_GATT_DISCOVER_STD_CHAR_DESC,
- };
- struct bt_gatt_discover_params {
-
- const struct bt_uuid *uuid;
-
- bt_gatt_discover_func_t func;
- union {
- struct {
-
- uint16_t attr_handle;
-
- uint16_t start_handle;
-
- uint16_t end_handle;
- } _included;
-
- uint16_t start_handle;
- };
-
- uint16_t end_handle;
-
- uint8_t type;
- #if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC)
-
- struct bt_gatt_subscribe_params *sub_params;
- #endif
- };
- int bt_gatt_discover(struct bt_conn *conn,
- struct bt_gatt_discover_params *params);
- struct bt_gatt_read_params;
- typedef uint8_t (*bt_gatt_read_func_t)(struct bt_conn *conn, uint8_t err,
- struct bt_gatt_read_params *params,
- const void *data, uint16_t length);
- struct bt_gatt_read_params {
-
- bt_gatt_read_func_t func;
-
- size_t handle_count;
- union {
- struct {
-
- uint16_t handle;
-
- uint16_t offset;
- } single;
- struct {
-
- uint16_t *handles;
-
- bool variable;
- } multiple;
- struct {
-
- uint16_t start_handle;
-
- uint16_t end_handle;
-
- const struct bt_uuid *uuid;
- } by_uuid;
- };
- };
- int bt_gatt_read(struct bt_conn *conn, struct bt_gatt_read_params *params);
- struct bt_gatt_write_params;
- typedef void (*bt_gatt_write_func_t)(struct bt_conn *conn, uint8_t err,
- struct bt_gatt_write_params *params);
- struct bt_gatt_write_params {
-
- bt_gatt_write_func_t func;
-
- uint16_t handle;
-
- uint16_t offset;
-
- const void *data;
-
- uint16_t length;
- };
- int bt_gatt_write(struct bt_conn *conn, struct bt_gatt_write_params *params);
- int bt_gatt_write_without_response_cb(struct bt_conn *conn, uint16_t handle,
- const void *data, uint16_t length,
- bool sign, bt_gatt_complete_func_t func,
- void *user_data);
- static inline int bt_gatt_write_without_response(struct bt_conn *conn,
- uint16_t handle, const void *data,
- uint16_t length, bool sign)
- {
- return bt_gatt_write_without_response_cb(conn, handle, data, length,
- sign, NULL, NULL);
- }
- struct bt_gatt_subscribe_params;
- typedef uint8_t (*bt_gatt_notify_func_t)(struct bt_conn *conn,
- struct bt_gatt_subscribe_params *params,
- const void *data, uint16_t length);
- enum {
-
- BT_GATT_SUBSCRIBE_FLAG_VOLATILE,
-
- BT_GATT_SUBSCRIBE_FLAG_NO_RESUB,
-
- BT_GATT_SUBSCRIBE_FLAG_WRITE_PENDING,
- BT_GATT_SUBSCRIBE_NUM_FLAGS
- };
- struct bt_gatt_subscribe_params {
-
- bt_gatt_notify_func_t notify;
-
- bt_gatt_write_func_t write;
-
- uint16_t value_handle;
-
- uint16_t ccc_handle;
- #if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC)
-
- uint16_t end_handle;
-
- struct bt_gatt_discover_params *disc_params;
- #endif
-
- uint16_t value;
-
- ATOMIC_DEFINE(flags, BT_GATT_SUBSCRIBE_NUM_FLAGS);
- sys_snode_t node;
- };
- int bt_gatt_subscribe(struct bt_conn *conn,
- struct bt_gatt_subscribe_params *params);
- int bt_gatt_resubscribe(uint8_t id, const bt_addr_le_t *peer,
- struct bt_gatt_subscribe_params *params);
- int bt_gatt_unsubscribe(struct bt_conn *conn,
- struct bt_gatt_subscribe_params *params);
- void bt_gatt_cancel(struct bt_conn *conn, void *params);
- #ifdef __cplusplus
- }
- #endif
- #endif
|