Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Characteristic.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: Aug 30, 2022
5 * License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
6 */
7
8#pragma once
9
11#include <ble/cs_Nordic.h>
12#include <ble/cs_Service.h>
13#include <ble/cs_UUID.h>
14#include <cfg/cs_Config.h>
15#include <cfg/cs_Strings.h>
16#include <common/cs_Types.h>
19#include <logging/cs_Logger.h>
21#include <third/std/function.h>
22#include <util/cs_BleError.h>
23#include <util/cs_Utils.h>
24
28template <class T>
30public:
31 Characteristic() { setValueBuffer(reinterpret_cast<buffer_ptr_t>(&_value), sizeof(_value)); };
32
33 virtual ~Characteristic(){};
34
41 if (retCode != ERR_SUCCESS) {
42 return retCode;
43 }
44 _value = value;
45 return ERR_SUCCESS;
46 }
47
51 T& getValue() { return _value; }
52
56 cs_ret_code_t setValue(const T& value) {
57 _value = value;
58 return updateValue(sizeof(_value));
59 }
60
61protected:
64};
65
69template <>
71
75template <>
76class Characteristic<const char*> : public CharacteristicBase {
77private:
80
81public:
83
88 cs_ret_code_t setInitialValue(const char* value) {
89 uint16_t size = strlen(value);
91 if (retCode != ERR_SUCCESS) {
92 return retCode;
93 }
94 memcpy(_value, value, size);
95 return ERR_SUCCESS;
96 }
97
101 cs_ret_code_t setValue(const char* value) {
102 uint16_t size = strlen(value);
103 if (size > sizeof(_value)) {
105 }
106 memcpy(_value, value, size);
107 return updateValue(size);
108 }
109
113 const char* getValue() { return _value; }
114};
Base class for a BLE characteristic.
Definition: cs_CharacteristicBase.h:42
cs_ret_code_t setInitialValueLength(cs_buffer_size_t size)
Set the initial value length.
cs_ret_code_t updateValue(uint16_t length)
Call this function when you changed the characteristic value.
cs_ret_code_t setValueBuffer(buffer_ptr_t buffer, cs_buffer_size_t size)
Set the buffer to be used for the (plain text) characteristic value.
cs_ret_code_t setValue(const char *value)
Set the new (plain text) value.
Definition: cs_Characteristic.h:101
Characteristic()
Definition: cs_Characteristic.h:82
cs_ret_code_t setInitialValue(const char *value)
Set the initial value.
Definition: cs_Characteristic.h:88
const char * getValue()
Return the (plain text) value.
Definition: cs_Characteristic.h:113
Characteristic of generic type T.
Definition: cs_Characteristic.h:29
cs_ret_code_t setValue(const T &value)
Set the new (plain text) value.
Definition: cs_Characteristic.h:56
cs_ret_code_t setInitialValue(const T &value)
Set the initial value.
Definition: cs_Characteristic.h:39
T & getValue()
Return the (plain text) value.
Definition: cs_Characteristic.h:51
Characteristic()
Definition: cs_Characteristic.h:31
T _value
The (plain text) value.
Definition: cs_Characteristic.h:63
virtual ~Characteristic()
Definition: cs_Characteristic.h:33
Bluetooth Low Energy characteristics.
#define DEFAULT_CHAR_VALUE_STRING_LENGTH
maximum length of strings used for characteristic values
Definition: cs_Config.h:29
@ ERR_SUCCESS
Definition: cs_ErrorCodes.h:10
@ ERR_BUFFER_TOO_SMALL
Definition: cs_ErrorCodes.h:16
BLE service.
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
uint8_t * buffer_ptr_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 10 May....
Definition: cs_Typedefs.h:19