nRF Connect SDK API 0.1.0
Loading...
Searching...
No Matches
nsms.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef BT_NSMS_H_
8#define BT_NSMS_H_
9
17#include <zephyr/kernel.h>
18#include <zephyr/types.h>
19#include <zephyr/bluetooth/conn.h>
20#include <zephyr/bluetooth/uuid.h>
21#include <zephyr/bluetooth/gatt.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
28#define BT_UUID_NSMS_VAL \
29 BT_UUID_128_ENCODE(0x57a70000, 0x9350, 0x11ed, 0xa1eb, 0x0242ac120002)
30
32#define BT_UUID_NSMS_STATUS_VAL \
33 BT_UUID_128_ENCODE(0x57a70001, 0x9350, 0x11ed, 0xa1eb, 0x0242ac120002)
34
35#define BT_UUID_NSMS_SERVICE BT_UUID_DECLARE_128(BT_UUID_NSMS_VAL)
36#define BT_UUID_NSMS_STATUS BT_UUID_DECLARE_128(BT_UUID_NSMS_STATUS_VAL)
37
38
42 struct k_mutex *mtx;
44 size_t size;
46 char *buf;
47};
48
53struct bt_nsms {
55 const struct bt_gatt_attr *status_attr;
56};
57
65ssize_t bt_nsms_status_read(struct bt_conn *conn,
66 struct bt_gatt_attr const *attr,
67 void *buf, uint16_t len, uint16_t offset);
68
69#define BT_NSMS_SECURITY_LEVEL_NONE 0
70#define BT_NSMS_SECURITY_LEVEL_ENCRYPT 1
71#define BT_NSMS_SECURITY_LEVEL_AUTHEN 2
72
73#define _BT_NSMS_CH_READ_PERM(_slevel) ((_slevel == BT_NSMS_SECURITY_LEVEL_AUTHEN) ? \
74 (BT_GATT_PERM_READ_AUTHEN) : \
75 (_slevel == BT_NSMS_SECURITY_LEVEL_ENCRYPT) ? \
76 (BT_GATT_PERM_READ_ENCRYPT) : \
77 (BT_GATT_PERM_READ) \
78 )
79
80#define _BT_NSMS_CH_WRITE_PERM(_slevel) ((_slevel == BT_NSMS_SECURITY_LEVEL_AUTHEN) ? \
81 (BT_GATT_PERM_WRITE_AUTHEN) : \
82 (_slevel == BT_NSMS_SECURITY_LEVEL_ENCRYPT) ? \
83 (BT_GATT_PERM_WRITE_ENCRYPT) : \
84 (BT_GATT_PERM_WRITE) \
85 )
86
87/* @note:
88 * The macro is required to provide proper arguments expansion as we are using name concatenation.
89 * This allow proper double argument expansion.
90 */
91#define BT_NSMS_SERVICE_DEF(_name, ...) \
92 BT_GATT_SERVICE_DEFINE(_name, __VA_ARGS__)
93
109#define BT_NSMS_DEF(_nsms, _name, _slevel, _status_init, _len_max) \
110 static K_MUTEX_DEFINE(_CONCAT(_nsms, _status_mtx)); \
111 static char _CONCAT(_nsms, _status_buf)[_len_max] = _status_init; \
112 static const struct bt_nsms_status_str _CONCAT(_nsms, _status_str) = { \
113 .mtx = &_CONCAT(_nsms, _status_mtx), \
114 .size = (_len_max), \
115 .buf = _CONCAT(_nsms, _status_buf) \
116 }; \
117 BT_NSMS_SERVICE_DEF(_CONCAT(_nsms, _svc), \
118 BT_GATT_PRIMARY_SERVICE(BT_UUID_NSMS_SERVICE), \
119 BT_GATT_CHARACTERISTIC(BT_UUID_NSMS_STATUS, \
120 BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, \
121 _BT_NSMS_CH_READ_PERM(_slevel), \
122 bt_nsms_status_read, \
123 NULL, \
124 (void *)&_CONCAT(_nsms, _status_str)), \
125 BT_GATT_CCC(NULL, _BT_NSMS_CH_READ_PERM(_slevel) | \
126 _BT_NSMS_CH_WRITE_PERM(_slevel)), \
127 BT_GATT_CUD(_name, _BT_NSMS_CH_READ_PERM(_slevel)), \
128 ); \
129 static const struct bt_nsms _nsms = { \
130 .status_attr = &_CONCAT(_nsms, _svc).attrs[2], \
131 }
132
144int bt_nsms_set_status(const struct bt_nsms *nsms_obj, const char *status);
145
146
147#ifdef __cplusplus
148}
149#endif
150
155#endif /* BT_NSMS_H_ */
int bt_nsms_set_status(const struct bt_nsms *nsms_obj, const char *status)
Set status.
ssize_t bt_nsms_status_read(struct bt_conn *conn, struct bt_gatt_attr const *attr, void *buf, uint16_t len, uint16_t offset)
Nordic Status Message Service reading command.
struct k_mutex * mtx
Definition: nsms.h:42
size_t size
Definition: nsms.h:44
char * buf
Definition: nsms.h:46
The status.
Definition: nsms.h:40
const struct bt_gatt_attr * status_attr
Definition: nsms.h:55
Nordic Status Message Service structure.
Definition: nsms.h:53