MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx::MeshXGpioImpl Class Reference

Concrete GPIO Implementation Class. More...

Inheritance diagram for meshx::MeshXGpioImpl:
meshx::MeshXIoInterface

Public Member Functions

 MeshXGpioImpl (uint8_t logical_pin, const char *name, GpioMode mode)
 Constructor.
virtual ~MeshXGpioImpl ()
 Destructor.
virtual uint8_t getLogicalPin () const override
 Get logical pin number.
virtual const char * getName () const override
 Get pin name.
virtual bool isFunctionSupported (IoFunction function) const override
 Check if function is supported.
virtual int execute (IoFunction function, const std::vector< uint32_t > &args) override
 Execute IO function.
virtual int setLevel (uint8_t level) override
 Set pin level (convenience method).
virtual int getLevel (uint8_t *level) override
 Get pin level (convenience method).
virtual int toggle () override
 Toggle pin level (convenience method).
virtual int setPwmDutyCycle (uint8_t duty_cycle) override
 Set PWM duty cycle (convenience method).
virtual int setPwmFrequency (uint32_t frequency) override
 Set PWM frequency (convenience method).
virtual int registerInterrupt (void(*callback)(uint8_t, void *), void *user_data, uint8_t trigger_type) override
 Register interrupt (convenience method).
virtual int unregisterInterrupt () override
 Unregister interrupt (convenience method).
virtual int enableInterrupt (bool enable) override
 Enable/disable interrupt (convenience method).
Public Member Functions inherited from meshx::MeshXIoInterface
virtual ~MeshXIoInterface ()=default

Private Attributes

uint8_t m_logical_pin
char m_name [32]
GpioMode m_mode
bool m_initialized
void(* m_intr_callback )(uint8_t, void *)
void * m_intr_user_data

Friends

void meshx_gpio_intr_cb_wrapper (uint8_t logical_pin, void *user_data)
 Static interrupt callback wrapper for C API.

Detailed Description

Concrete GPIO Implementation Class.

Constructor & Destructor Documentation

◆ MeshXGpioImpl()

meshx::MeshXGpioImpl::MeshXGpioImpl ( uint8_t logical_pin,
const char * name,
GpioMode mode )
inline

Constructor.

Parameters
logical_pinLogical pin number
namePin name
modeGPIO mode
54 : m_logical_pin(logical_pin), m_mode(mode), m_initialized(false),
55 m_intr_callback(nullptr), m_intr_user_data(nullptr) {
56 strncpy(m_name, name, sizeof(m_name) - 1);
57 m_name[sizeof(m_name) - 1] = '\0';
58 }
GpioMode m_mode
Definition meshx_gpio_impl.cpp:40
uint8_t m_logical_pin
Definition meshx_gpio_impl.cpp:38
void(* m_intr_callback)(uint8_t, void *)
Definition meshx_gpio_impl.cpp:42
char m_name[32]
Definition meshx_gpio_impl.cpp:39
void * m_intr_user_data
Definition meshx_gpio_impl.cpp:43
bool m_initialized
Definition meshx_gpio_impl.cpp:41

◆ ~MeshXGpioImpl()

virtual meshx::MeshXGpioImpl::~MeshXGpioImpl ( )
inlinevirtual

Destructor.

63 {
64 // Cleanup if needed
65 }

Member Function Documentation

◆ enableInterrupt()

virtual int meshx::MeshXGpioImpl::enableInterrupt ( bool enable)
inlineoverridevirtual

Enable/disable interrupt (convenience method).

Parameters
enabletrue to enable, false to disable
Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

327 {
328 std::vector<uint32_t> args = {static_cast<uint32_t>(enable)};
330 }
virtual int execute(IoFunction function, const std::vector< uint32_t > &args) override
Execute IO function.
Definition meshx_gpio_impl.cpp:129
@ ENABLE_INTERRUPT
Definition meshx_io_interface.hpp:33

◆ execute()

virtual int meshx::MeshXGpioImpl::execute ( IoFunction function,
const std::vector< uint32_t > & args )
inlineoverridevirtual

Execute IO function.

Parameters
functionIO function to execute
argsFunction arguments vector
Returns
int Error code (0 = success, negative = error)

Implements meshx::MeshXIoInterface.

129 {
130 if (!m_initialized) {
131 // Initialize GPIO if not already initialized
133 if (err != MESHX_SUCCESS) {
134 return static_cast<int>(IoError::NOT_INITIALIZED);
135 }
136 m_initialized = true;
137 }
138
139 switch (function) {
141 if (args.size() < 1) {
142 return static_cast<int>(IoError::INVALID_LEVEL);
143 }
144 uint8_t level = static_cast<uint8_t>(args[0]);
145 if (level > 1) {
146 return static_cast<int>(IoError::INVALID_LEVEL);
147 }
149 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INVALID_MODE);
150 }
151
153 if (args.size() < 1) {
154 return static_cast<int>(IoError::INVALID_ARG);
155 }
156 uint8_t* level_ptr = reinterpret_cast<uint8_t*>(static_cast<uintptr_t>(args[0]));
157 if (level_ptr == nullptr) {
158 return static_cast<int>(IoError::INVALID_ARG);
159 }
161 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INVALID_MODE);
162 }
163
164 case IoFunction::TOGGLE: {
166 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INVALID_MODE);
167 }
168
170 if (args.size() < 1) {
171 return static_cast<int>(IoError::PWM_INVALID_PARAM);
172 }
173 // PWM functions should use meshx_pwm API or meshx_gpio_execute_function
175 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::PWM_NOT_SUPPORTED);
176 }
177
179 if (args.size() < 1) {
180 return static_cast<int>(IoError::PWM_INVALID_PARAM);
181 }
183 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::PWM_NOT_SUPPORTED);
184 }
185
187 if (args.size() < 3) {
188 return static_cast<int>(IoError::INVALID_ARG);
189 }
190 void (*callback)(uint8_t, void*) = reinterpret_cast<void (*)(uint8_t, void*)>(static_cast<uintptr_t>(args[0]));
191 void* user_data = reinterpret_cast<void*>(static_cast<uintptr_t>(args[1]));
192 uint8_t trigger_type = static_cast<uint8_t>(args[2]);
193 return registerInterrupt(callback, user_data, trigger_type);
194 }
195
198 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INTR_NOT_SUPPORTED);
199 }
200
202 if (args.size() < 1) {
203 return static_cast<int>(IoError::INVALID_ARG);
204 }
205 bool enable = static_cast<bool>(args[0]);
207 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INTR_NOT_SUPPORTED);
208 }
209
211 // Custom functions not implemented in this basic version
212 return static_cast<int>(IoError::NOT_SUPPORTED);
213 }
214
215 default:
216 return static_cast<int>(IoError::INVALID_MODE);
217 }
218 }
virtual int registerInterrupt(void(*callback)(uint8_t, void *), void *user_data, uint8_t trigger_type) override
Register interrupt (convenience method).
Definition meshx_gpio_impl.cpp:287
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
meshx_err_t meshx_gpio_set_level(uint8_t logical_pin, uint8_t level)
Set GPIO pin level.
Definition meshx_gpio.c:322
meshx_err_t meshx_gpio_execute_function(uint8_t logical_pin, meshx_io_function_t function, const uint32_t *args, uint8_t arg_count)
Execute IO function on GPIO pin.
Definition meshx_gpio.c:616
meshx_err_t meshx_gpio_get_level(uint8_t logical_pin, uint8_t *level)
Get GPIO pin level.
Definition meshx_gpio.c:392
meshx_err_t meshx_gpio_intr_enable(uint8_t logical_pin, bool enable)
Enable/disable GPIO interrupt.
Definition meshx_gpio.c:591
meshx_err_t meshx_gpio_unregister_intr(uint8_t logical_pin)
Unregister GPIO interrupt handler.
Definition meshx_gpio.c:557
meshx_err_t meshx_gpio_init(void)
Initialize GPIO subsystem.
Definition meshx_gpio.c:95
meshx_err_t meshx_gpio_toggle(uint8_t logical_pin)
Toggle GPIO pin level.
Definition meshx_gpio.c:453
@ MESHX_IO_FUNCTION_SET_PWM_FREQUENCY
Definition meshx_gpio.h:68
@ MESHX_IO_FUNCTION_SET_PWM_DUTY
Definition meshx_gpio.h:67
**This function is called by the parent element when a state change request *is received It validates the request and returns a result to the element not the model layer **return * MESHX_SUCCESS
Definition meshx_model_level.cpp:385
@ NOT_INITIALIZED
Definition meshx_io_types.hpp:90
@ PWM_NOT_SUPPORTED
Definition meshx_io_types.hpp:87
@ INVALID_MODE
Definition meshx_io_types.hpp:82
@ NOT_SUPPORTED
Definition meshx_io_types.hpp:89
@ INTR_NOT_SUPPORTED
Definition meshx_io_types.hpp:85
@ INVALID_ARG
Definition meshx_io_types.hpp:84
@ PWM_INVALID_PARAM
Definition meshx_io_types.hpp:88
@ INVALID_LEVEL
Definition meshx_io_types.hpp:83
@ REGISTER_INTERRUPT
Definition meshx_io_interface.hpp:31
@ TOGGLE
Definition meshx_io_interface.hpp:28
@ UNREGISTER_INTERRUPT
Definition meshx_io_interface.hpp:32
@ SET_PWM_DUTY
Definition meshx_io_interface.hpp:29
@ SET_PWM_FREQUENCY
Definition meshx_io_interface.hpp:30
@ CUSTOM_FUNCTION
Definition meshx_io_interface.hpp:34
@ SET_LEVEL
Definition meshx_io_interface.hpp:26
@ GET_LEVEL
Definition meshx_io_interface.hpp:27

◆ getLevel()

virtual int meshx::MeshXGpioImpl::getLevel ( uint8_t * level)
inlineoverridevirtual

Get pin level (convenience method).

Parameters
[out]levelPointer to store pin level
Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

237 {
238 if (level == nullptr) {
239 return static_cast<int>(IoError::INVALID_ARG);
240 }
241 // Note: This would need to call meshx_gpio_get_level directly
242 // since execute() can't return the level value
244 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INVALID_MODE);
245 }

◆ getLogicalPin()

virtual uint8_t meshx::MeshXGpioImpl::getLogicalPin ( ) const
inlineoverridevirtual

Get logical pin number.

Returns
uint8_t Logical pin number

Implements meshx::MeshXIoInterface.

72 {
73 return m_logical_pin;
74 }

◆ getName()

virtual const char * meshx::MeshXGpioImpl::getName ( ) const
inlineoverridevirtual

Get pin name.

Returns
const char* Pin name

Implements meshx::MeshXIoInterface.

81 {
82 return m_name;
83 }

◆ isFunctionSupported()

virtual bool meshx::MeshXGpioImpl::isFunctionSupported ( IoFunction function) const
inlineoverridevirtual

Check if function is supported.

Parameters
functionIO function to check
Returns
true if function is supported
false if function is not supported

Implements meshx::MeshXIoInterface.

92 {
93 switch (function) {
97 return (m_mode == GpioMode::OUTPUT ||
101
105 return (m_mode == GpioMode::INPUT ||
108
111 return (m_mode == GpioMode::PWM_OUTPUT);
112
114 // Custom functions are always supported for extensibility
115 return true;
116
117 default:
118 return false;
119 }
120 }
@ PWM_OUTPUT
Definition meshx_io_types.hpp:28
@ OUTPUT
Definition meshx_io_types.hpp:24
@ OPEN_DRAIN_INPUT_OUTPUT
Definition meshx_io_types.hpp:27
@ INPUT
Definition meshx_io_types.hpp:23
@ INPUT_OUTPUT
Definition meshx_io_types.hpp:25
@ OPEN_DRAIN
Definition meshx_io_types.hpp:26

◆ registerInterrupt()

virtual int meshx::MeshXGpioImpl::registerInterrupt ( void(* callback )(uint8_t, void *),
void * user_data,
uint8_t trigger_type )
inlineoverridevirtual

Register interrupt (convenience method).

Parameters
callbackFunction pointer to callback
user_dataUser data passed to callback
trigger_typeInterrupt trigger type
Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

289 {
290 // Convert trigger type to meshx_gpio_intr_type_t
291 meshx_gpio_intr_type_t intr_type;
292 switch (trigger_type) {
293 case 1: intr_type = MESHX_GPIO_INTR_POSITIVE_EDGE; break;
294 case 2: intr_type = MESHX_GPIO_INTR_NEGATIVE_EDGE; break;
295 case 3: intr_type = MESHX_GPIO_INTR_ANY_EDGE; break;
296 case 4: intr_type = MESHX_GPIO_INTR_LOW_LEVEL; break;
297 case 5: intr_type = MESHX_GPIO_INTR_HIGH_LEVEL; break;
298 default: return static_cast<int>(IoError::INVALID_ARG);
299 }
300
301 // Store the callback and user data for the wrapper
302 m_intr_callback = callback;
303 m_intr_user_data = user_data;
304
305 // Use a static wrapper function that calls back with the logical pin
308 return (err == MESHX_SUCCESS) ? 0 : static_cast<int>(IoError::INTR_NOT_SUPPORTED);
309 }
friend void meshx_gpio_intr_cb_wrapper(uint8_t, void *)
Static interrupt callback wrapper for C API.
Definition meshx_gpio_impl.cpp:339
meshx_err_t meshx_gpio_register_intr(uint8_t logical_pin, meshx_gpio_intr_type_t intr_type, meshx_gpio_intr_cb_t callback, void *user_data)
Register GPIO interrupt handler.
Definition meshx_gpio.c:488
meshx_gpio_intr_type_t
GPIO Interrupt Trigger Types.
Definition meshx_gpio_types.h:28
@ MESHX_GPIO_INTR_POSITIVE_EDGE
Definition meshx_gpio_types.h:30
@ MESHX_GPIO_INTR_NEGATIVE_EDGE
Definition meshx_gpio_types.h:31
@ MESHX_GPIO_INTR_ANY_EDGE
Definition meshx_gpio_types.h:32
@ MESHX_GPIO_INTR_LOW_LEVEL
Definition meshx_gpio_types.h:33
@ MESHX_GPIO_INTR_HIGH_LEVEL
Definition meshx_gpio_types.h:34

◆ setLevel()

virtual int meshx::MeshXGpioImpl::setLevel ( uint8_t level)
inlineoverridevirtual

Set pin level (convenience method).

Parameters
levelPin level (0 = low, 1 = high)
Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

226 {
227 std::vector<uint32_t> args = {static_cast<uint32_t>(level)};
228 return execute(IoFunction::SET_LEVEL, args);
229 }

◆ setPwmDutyCycle()

virtual int meshx::MeshXGpioImpl::setPwmDutyCycle ( uint8_t duty_cycle)
inlineoverridevirtual

Set PWM duty cycle (convenience method).

Parameters
duty_cycleDuty cycle (0-100%)
Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

263 {
264 std::vector<uint32_t> args = {static_cast<uint32_t>(duty_cycle)};
265 return execute(IoFunction::SET_PWM_DUTY, args);
266 }

◆ setPwmFrequency()

virtual int meshx::MeshXGpioImpl::setPwmFrequency ( uint32_t frequency)
inlineoverridevirtual

Set PWM frequency (convenience method).

Parameters
frequencyFrequency in Hz
Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

274 {
275 std::vector<uint32_t> args = {frequency};
277 }

◆ toggle()

virtual int meshx::MeshXGpioImpl::toggle ( )
inlineoverridevirtual

Toggle pin level (convenience method).

Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

252 {
253 std::vector<uint32_t> args; // No arguments needed for toggle
254 return execute(IoFunction::TOGGLE, args);
255 }

◆ unregisterInterrupt()

virtual int meshx::MeshXGpioImpl::unregisterInterrupt ( )
inlineoverridevirtual

Unregister interrupt (convenience method).

Returns
int Error code (0 = success, negative = error)

Reimplemented from meshx::MeshXIoInterface.

316 {
317 std::vector<uint32_t> args; // No arguments needed
319 }

◆ meshx_gpio_intr_cb_wrapper

void meshx_gpio_intr_cb_wrapper ( uint8_t logical_pin,
void * user_data )
friend

Static interrupt callback wrapper for C API.

Parameters
logical_pinLogical pin number
user_dataUser data (pointer to MeshXGpioImpl instance)
340{
341 MeshXGpioImpl* instance = static_cast<MeshXGpioImpl*>(user_data);
342 if (instance != nullptr && instance->m_intr_callback != nullptr) {
343 instance->m_intr_callback(logical_pin, instance->m_intr_user_data);
344 }
345}
MeshXGpioImpl(uint8_t logical_pin, const char *name, GpioMode mode)
Constructor.
Definition meshx_gpio_impl.cpp:53

Field Documentation

◆ m_initialized

bool meshx::MeshXGpioImpl::m_initialized
private

Initialization flag

◆ m_intr_callback

void(* meshx::MeshXGpioImpl::m_intr_callback) (uint8_t, void *)
private

Interrupt callback

◆ m_intr_user_data

void* meshx::MeshXGpioImpl::m_intr_user_data
private

Interrupt user data

◆ m_logical_pin

uint8_t meshx::MeshXGpioImpl::m_logical_pin
private

Logical pin number

◆ m_mode

GpioMode meshx::MeshXGpioImpl::m_mode
private

GPIO mode

◆ m_name

char meshx::MeshXGpioImpl::m_name[32]
private

Pin name


The documentation for this class was generated from the following file: