21
21
22
22
class Device
23
23
{
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;
24
32
private:
25
33
const device_handle_t *device_handle;
34
+ RMakerDevicePrivT private_data;
26
35
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
+ }
27
45
public:
28
46
Device ()
29
47
{
30
48
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
+
32
54
Device (const char *dev_name, const char *dev_type = NULL , void *priv_data = NULL )
33
55
{
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 );
35
60
if (device_handle == NULL ){
36
61
log_e (" Device create error" );
37
62
}
@@ -48,9 +73,6 @@ class Device
48
73
{
49
74
return device_handle;
50
75
}
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);
54
76
55
77
esp_err_t deleteDevice ();
56
78
void addCb (deviceWriteCb write_cb, deviceReadCb read_cb = NULL );
@@ -94,7 +116,8 @@ class Switch : public Device
94
116
}
95
117
void standardSwitchDevice (const char *dev_name, void *priv_data, bool power)
96
118
{
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);
98
121
setDeviceHandle (dev_handle);
99
122
if (dev_handle == NULL ){
100
123
log_e (" Switch device not created" );
@@ -115,7 +138,8 @@ class LightBulb : public Device
115
138
}
116
139
void standardLightBulbDevice (const char *dev_name, void *priv_data, bool power)
117
140
{
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);
119
143
setDeviceHandle (dev_handle);
120
144
if (dev_handle == NULL ){
121
145
log_e (" Light device not created" );
@@ -157,7 +181,8 @@ class TemperatureSensor : public Device
157
181
}
158
182
void standardTemperatureSensorDevice (const char *dev_name, void *priv_data, float temp)
159
183
{
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);
161
186
setDeviceHandle (dev_handle);
162
187
if (dev_handle == NULL ){
163
188
log_e (" Temperature Sensor device not created" );
0 commit comments