Skip to content

Commit 2c4dead

Browse files
fix: rainmaker device callback not getting invoked (#8249)
This PR fixes an issue of multiple device callbacks (one callback per device) were not getting registered and invoked. Consider an example where I create two devices, a switch and a fan; each of them having their own write callbacks. On controlling either switch/fan through Rainmaker app, the callback that got registered at last gets invoked. This is also seen in the issue reported in #8231
1 parent 82227e6 commit 2c4dead

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

Diff for: libraries/RainMaker/src/RMakerDevice.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
55
static esp_err_t err;
66
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
77
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
8-
9-
void (*write_cb)(Device*, Param*, param_val_t, void*, write_ctx_t*);
10-
void (*read_cb)(Device*, Param*, void*, read_ctx_t*);
11-
Device device;
12-
Param param;
8+
typedef struct {
9+
void *priv_data;
10+
deviceWriteCb write_cb;
11+
deviceReadCb read_cb;
12+
} RMakerDevicePrivT;
1313

1414
static esp_err_t write_callback(const device_handle_t *dev_handle, const param_handle_t *par_handle, const param_val_t val, void *priv_data, write_ctx_t *ctx)
1515
{
16+
Device device;
17+
Param param;
1618
device.setDeviceHandle(dev_handle);
1719
param.setParamHandle(par_handle);
18-
19-
write_cb(&device, &param, val, priv_data, ctx);
20+
deviceWriteCb cb = ((RMakerDevicePrivT *)priv_data)->write_cb;
21+
cb(&device, &param, val, ((RMakerDevicePrivT *)priv_data)->priv_data, ctx);
2022
return ESP_OK;
2123
}
2224

2325
static esp_err_t read_callback(const device_handle_t *dev_handle, const param_handle_t *par_handle, void *priv_data, read_ctx_t *ctx)
2426
{
27+
Device device;
28+
Param param;
2529
device.setDeviceHandle(dev_handle);
2630
param.setParamHandle(par_handle);
27-
28-
read_cb(&device, &param, priv_data, ctx);
31+
deviceReadCb cb = ((RMakerDevicePrivT *)priv_data)->read_cb;
32+
cb(&device, &param, ((RMakerDevicePrivT *)priv_data)->priv_data, ctx);
2933
return ESP_OK;
3034
}
3135

@@ -41,8 +45,8 @@ esp_err_t Device::deleteDevice()
4145

4246
void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb)
4347
{
44-
write_cb = writeCb;
45-
read_cb = readCb;
48+
this->private_data.write_cb = writeCb;
49+
this->private_data.read_cb = readCb;
4650
err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback);
4751
if(err != ESP_OK) {
4852
log_e("Failed to register callback");

Diff for: libraries/RainMaker/src/RMakerDevice.h

+33-8
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,42 @@
2121

2222
class Device
2323
{
24+
public:
25+
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
26+
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
27+
typedef struct {
28+
void *priv_data;
29+
deviceWriteCb write_cb;
30+
deviceReadCb read_cb;
31+
} RMakerDevicePrivT;
2432
private:
2533
const device_handle_t *device_handle;
34+
RMakerDevicePrivT private_data;
2635

36+
protected:
37+
void setPrivateData(void *priv_data) {
38+
this->private_data.priv_data = priv_data;
39+
}
40+
41+
const RMakerDevicePrivT* getDevicePrivateData()
42+
{
43+
return &this->private_data;
44+
}
2745
public:
2846
Device()
2947
{
3048
device_handle = NULL;
31-
}
49+
this->private_data.priv_data = NULL;
50+
this->private_data.write_cb = NULL;
51+
this->private_data.read_cb = NULL;
52+
}
53+
3254
Device(const char *dev_name, const char *dev_type = NULL, void *priv_data = NULL)
3355
{
34-
device_handle = esp_rmaker_device_create(dev_name, dev_type, priv_data);
56+
this->private_data.priv_data = priv_data;
57+
this->private_data.write_cb = NULL;
58+
this->private_data.read_cb = NULL;
59+
device_handle = esp_rmaker_device_create(dev_name, dev_type, &this->private_data);
3560
if(device_handle == NULL){
3661
log_e("Device create error");
3762
}
@@ -48,9 +73,6 @@ class Device
4873
{
4974
return device_handle;
5075
}
51-
52-
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
53-
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
5476

5577
esp_err_t deleteDevice();
5678
void addCb(deviceWriteCb write_cb, deviceReadCb read_cb = NULL);
@@ -94,7 +116,8 @@ class Switch : public Device
94116
}
95117
void standardSwitchDevice(const char *dev_name, void *priv_data, bool power)
96118
{
97-
esp_rmaker_device_t *dev_handle = esp_rmaker_switch_device_create(dev_name, priv_data, power);
119+
this->setPrivateData(priv_data);
120+
esp_rmaker_device_t *dev_handle = esp_rmaker_switch_device_create(dev_name, (void *)this->getDevicePrivateData(), power);
98121
setDeviceHandle(dev_handle);
99122
if(dev_handle == NULL){
100123
log_e("Switch device not created");
@@ -115,7 +138,8 @@ class LightBulb : public Device
115138
}
116139
void standardLightBulbDevice(const char *dev_name, void *priv_data, bool power)
117140
{
118-
esp_rmaker_device_t *dev_handle = esp_rmaker_lightbulb_device_create(dev_name, priv_data, power);
141+
this->setPrivateData(priv_data);
142+
esp_rmaker_device_t *dev_handle = esp_rmaker_lightbulb_device_create(dev_name, (void *)this->getDevicePrivateData(), power);
119143
setDeviceHandle(dev_handle);
120144
if(dev_handle == NULL){
121145
log_e("Light device not created");
@@ -157,7 +181,8 @@ class TemperatureSensor : public Device
157181
}
158182
void standardTemperatureSensorDevice(const char *dev_name, void *priv_data, float temp)
159183
{
160-
esp_rmaker_device_t *dev_handle = esp_rmaker_temp_sensor_device_create(dev_name, priv_data, temp);
184+
this->setPrivateData(priv_data);
185+
esp_rmaker_device_t *dev_handle = esp_rmaker_temp_sensor_device_create(dev_name, (void *)this->getDevicePrivateData(), temp);
161186
setDeviceHandle(dev_handle);
162187
if(dev_handle == NULL){
163188
log_e("Temperature Sensor device not created");

0 commit comments

Comments
 (0)