Unverified Commit 2c4dead3 authored by Sanket Wadekar's avatar Sanket Wadekar Committed by GitHub

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
parent 82227e6d
...@@ -5,27 +5,31 @@ ...@@ -5,27 +5,31 @@
static esp_err_t err; static esp_err_t err;
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx); typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx); typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
typedef struct {
void (*write_cb)(Device*, Param*, param_val_t, void*, write_ctx_t*); void *priv_data;
void (*read_cb)(Device*, Param*, void*, read_ctx_t*); deviceWriteCb write_cb;
Device device; deviceReadCb read_cb;
Param param; } RMakerDevicePrivT;
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) 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)
{ {
Device device;
Param param;
device.setDeviceHandle(dev_handle); device.setDeviceHandle(dev_handle);
param.setParamHandle(par_handle); param.setParamHandle(par_handle);
deviceWriteCb cb = ((RMakerDevicePrivT *)priv_data)->write_cb;
write_cb(&device, &param, val, priv_data, ctx); cb(&device, &param, val, ((RMakerDevicePrivT *)priv_data)->priv_data, ctx);
return ESP_OK; return ESP_OK;
} }
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) 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)
{ {
Device device;
Param param;
device.setDeviceHandle(dev_handle); device.setDeviceHandle(dev_handle);
param.setParamHandle(par_handle); param.setParamHandle(par_handle);
deviceReadCb cb = ((RMakerDevicePrivT *)priv_data)->read_cb;
read_cb(&device, &param, priv_data, ctx); cb(&device, &param, ((RMakerDevicePrivT *)priv_data)->priv_data, ctx);
return ESP_OK; return ESP_OK;
} }
...@@ -41,8 +45,8 @@ esp_err_t Device::deleteDevice() ...@@ -41,8 +45,8 @@ esp_err_t Device::deleteDevice()
void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb) void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb)
{ {
write_cb = writeCb; this->private_data.write_cb = writeCb;
read_cb = readCb; this->private_data.read_cb = readCb;
err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback); err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback);
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Failed to register callback"); log_e("Failed to register callback");
......
...@@ -21,17 +21,42 @@ ...@@ -21,17 +21,42 @@
class Device class Device
{ {
public:
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
typedef struct {
void *priv_data;
deviceWriteCb write_cb;
deviceReadCb read_cb;
} RMakerDevicePrivT;
private: private:
const device_handle_t *device_handle; const device_handle_t *device_handle;
RMakerDevicePrivT private_data;
protected:
void setPrivateData(void *priv_data) {
this->private_data.priv_data = priv_data;
}
const RMakerDevicePrivT* getDevicePrivateData()
{
return &this->private_data;
}
public: public:
Device() Device()
{ {
device_handle = NULL; device_handle = NULL;
} this->private_data.priv_data = NULL;
this->private_data.write_cb = NULL;
this->private_data.read_cb = NULL;
}
Device(const char *dev_name, const char *dev_type = NULL, void *priv_data = NULL) Device(const char *dev_name, const char *dev_type = NULL, void *priv_data = NULL)
{ {
device_handle = esp_rmaker_device_create(dev_name, dev_type, priv_data); this->private_data.priv_data = priv_data;
this->private_data.write_cb = NULL;
this->private_data.read_cb = NULL;
device_handle = esp_rmaker_device_create(dev_name, dev_type, &this->private_data);
if(device_handle == NULL){ if(device_handle == NULL){
log_e("Device create error"); log_e("Device create error");
} }
...@@ -48,9 +73,6 @@ class Device ...@@ -48,9 +73,6 @@ class Device
{ {
return device_handle; return device_handle;
} }
typedef void (*deviceWriteCb)(Device*, Param*, const param_val_t val, void *priv_data, write_ctx_t *ctx);
typedef void (*deviceReadCb)(Device*, Param*, void *priv_data, read_ctx_t *ctx);
esp_err_t deleteDevice(); esp_err_t deleteDevice();
void addCb(deviceWriteCb write_cb, deviceReadCb read_cb = NULL); void addCb(deviceWriteCb write_cb, deviceReadCb read_cb = NULL);
...@@ -94,7 +116,8 @@ class Switch : public Device ...@@ -94,7 +116,8 @@ class Switch : public Device
} }
void standardSwitchDevice(const char *dev_name, void *priv_data, bool power) void standardSwitchDevice(const char *dev_name, void *priv_data, bool power)
{ {
esp_rmaker_device_t *dev_handle = esp_rmaker_switch_device_create(dev_name, priv_data, power); this->setPrivateData(priv_data);
esp_rmaker_device_t *dev_handle = esp_rmaker_switch_device_create(dev_name, (void *)this->getDevicePrivateData(), power);
setDeviceHandle(dev_handle); setDeviceHandle(dev_handle);
if(dev_handle == NULL){ if(dev_handle == NULL){
log_e("Switch device not created"); log_e("Switch device not created");
...@@ -115,7 +138,8 @@ class LightBulb : public Device ...@@ -115,7 +138,8 @@ class LightBulb : public Device
} }
void standardLightBulbDevice(const char *dev_name, void *priv_data, bool power) void standardLightBulbDevice(const char *dev_name, void *priv_data, bool power)
{ {
esp_rmaker_device_t *dev_handle = esp_rmaker_lightbulb_device_create(dev_name, priv_data, power); this->setPrivateData(priv_data);
esp_rmaker_device_t *dev_handle = esp_rmaker_lightbulb_device_create(dev_name, (void *)this->getDevicePrivateData(), power);
setDeviceHandle(dev_handle); setDeviceHandle(dev_handle);
if(dev_handle == NULL){ if(dev_handle == NULL){
log_e("Light device not created"); log_e("Light device not created");
...@@ -157,7 +181,8 @@ class TemperatureSensor : public Device ...@@ -157,7 +181,8 @@ class TemperatureSensor : public Device
} }
void standardTemperatureSensorDevice(const char *dev_name, void *priv_data, float temp) void standardTemperatureSensorDevice(const char *dev_name, void *priv_data, float temp)
{ {
esp_rmaker_device_t *dev_handle = esp_rmaker_temp_sensor_device_create(dev_name, priv_data, temp); this->setPrivateData(priv_data);
esp_rmaker_device_t *dev_handle = esp_rmaker_temp_sensor_device_create(dev_name, (void *)this->getDevicePrivateData(), temp);
setDeviceHandle(dev_handle); setDeviceHandle(dev_handle);
if(dev_handle == NULL){ if(dev_handle == NULL){
log_e("Temperature Sensor device not created"); log_e("Temperature Sensor device not created");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment