Commit 451e3eb7 authored by Michael Tross's avatar Michael Tross

Merge branch 'fix-invalid-free-with-wind-module' into 'master'

fix invalid free with wind module

See merge request !16
parents e934776c 237071a5
...@@ -104,6 +104,8 @@ read_config() ...@@ -104,6 +104,8 @@ read_config()
sprintf(path, "module.m%d.id", n); sprintf(path, "module.m%d.id", n);
if (config_lookup_string(&config, path, (const char **) &sval)) if (config_lookup_string(&config, path, (const char **) &sval))
module->id = strdup(sval); module->id = strdup(sval);
sprintf(path, "module.m%d.zoneid", n);
config_lookup_int(&config, path, (int *) &module->zoneID);
sprintf(path, "module.m%d.last_message", n); sprintf(path, "module.m%d.last_message", n);
config_lookup_int(&config, path, (int *) &module->last_message); config_lookup_int(&config, path, (int *) &module->last_message);
sprintf(path, "module.m%d.last_seen", n); sprintf(path, "module.m%d.last_seen", n);
...@@ -305,6 +307,12 @@ write_config() ...@@ -305,6 +307,12 @@ write_config()
} }
config_setting_set_string(setting, module->id); config_setting_set_string(setting, module->id);
setting = config_setting_add(s, "zoneid", CONFIG_TYPE_INT);
if (setting == NULL) {
setting = config_setting_get_member(s, "zoneid");
}
config_setting_set_int(setting, module->zoneID);
setting = config_setting_add(s, "last_message", CONFIG_TYPE_INT); setting = config_setting_add(s, "last_message", CONFIG_TYPE_INT);
if (setting == NULL) { if (setting == NULL) {
setting = config_setting_get_member(s, "last_message"); setting = config_setting_get_member(s, "last_message");
......
...@@ -52,6 +52,7 @@ netatmo_vdcd_t* devlist = NULL; ...@@ -52,6 +52,7 @@ netatmo_vdcd_t* devlist = NULL;
/* VDC-API data */ /* VDC-API data */
char g_vdc_modeluid[33] = { 0, };
char g_vdc_dsuid[35] = { 0, }; char g_vdc_dsuid[35] = { 0, };
char g_lib_dsuid[35] = { "053f848b85bb382198025cea1fd087f100" }; char g_lib_dsuid[35] = { "053f848b85bb382198025cea1fd087f100" };
...@@ -91,11 +92,11 @@ void signal_handler(int signum) ...@@ -91,11 +92,11 @@ void signal_handler(int signum)
void* networkThread(void *arg __attribute__((unused))) void* networkThread(void *arg __attribute__((unused)))
{ {
int rc;
static time_t last = 0; static time_t last = 0;
while (!g_shutdown_flag) { while (!g_shutdown_flag) {
sleep(5); sleep(5);
time_t now = time(NULL); time_t now = time(NULL);
if (now >= last + 10) { if (now >= last + 10) {
...@@ -104,17 +105,27 @@ void* networkThread(void *arg __attribute__((unused))) ...@@ -104,17 +105,27 @@ void* networkThread(void *arg __attribute__((unused)))
now, last, g_access_token, netatmo.refresh_token, now, last, g_access_token, netatmo.refresh_token,
g_access_token_valid_until, g_query_devices_time, g_query_values_time); g_access_token_valid_until, g_query_devices_time, g_query_values_time);
last = now;
if (g_access_token_valid_until <= now) { if (g_access_token_valid_until <= now) {
netatmo_get_token(); netatmo_get_token();
} }
if (g_access_token && (g_query_devices_time <= now)) { if (g_access_token && (g_query_devices_time <= now)) {
if (netatmo_get_devices() >= 0) { if ((rc = netatmo_get_devices()) >= 0) {
g_query_devices_time = g_resync_devices + now; g_query_devices_time = g_resync_devices + now;
g_network_changes = true; g_network_changes = true;
} else { } else {
g_query_devices_time = 60 + now; now = time(NULL);
g_query_values_time = 61 + now; switch(rc) {
case NETATMO_USER_LIMIT_REACHED:
g_query_devices_time = 300 + now;
break;
case NETATMO_TOKEN_EXPIRED:
g_query_devices_time = 600 + now;
break;
default:
g_query_devices_time = 60 + now;
break;
}
g_query_values_time = g_query_devices_time + 1;
} }
} }
if (g_access_token && (netatmo.base.modules_num > 0) && (g_query_values_time <= now)) { if (g_access_token && (netatmo.base.modules_num > 0) && (g_query_values_time <= now)) {
...@@ -122,9 +133,11 @@ void* networkThread(void *arg __attribute__((unused))) ...@@ -122,9 +133,11 @@ void* networkThread(void *arg __attribute__((unused)))
g_query_values_time = g_reload_values + now; g_query_values_time = g_reload_values + now;
g_network_changes = true; g_network_changes = true;
} else { } else {
now = time(NULL);
g_query_values_time = 60 + now; g_query_values_time = 60 + now;
} }
} }
last = now;
} }
} }
...@@ -217,7 +230,7 @@ int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) ...@@ -217,7 +230,7 @@ int main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
char hostname[HOST_NAME_MAX]; char hostname[HOST_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX); gethostname(hostname, HOST_NAME_MAX);
char servicename[HOST_NAME_MAX + 32]; char servicename[HOST_NAME_MAX + 32];
strcpy(servicename, "NetAtmo @"); strcpy(servicename, "NetAtmo Weather Station Controller @");
strcat(servicename, hostname); strcat(servicename, hostname);
dsvdc_t *handle = NULL; dsvdc_t *handle = NULL;
......
...@@ -55,6 +55,7 @@ typedef struct netatmo_module ...@@ -55,6 +55,7 @@ typedef struct netatmo_module
int battery_percent; int battery_percent;
unsigned char bid_length; unsigned char bid_length;
unsigned char bid[16]; unsigned char bid[16];
uint16_t zoneID;
} netatmo_module_t; } netatmo_module_t;
typedef struct netatmo_base typedef struct netatmo_base
...@@ -92,13 +93,15 @@ typedef struct netatmo_vdcd ...@@ -92,13 +93,15 @@ typedef struct netatmo_vdcd
} netatmo_vdcd_t; } netatmo_vdcd_t;
#define NETATMO_OK 0
#define NETATMO_OUT_OF_MEMORY -1 #define NETATMO_OUT_OF_MEMORY -1
#define NETATMO_AUTH_FAILED -10 #define NETATMO_AUTH_FAILED -10
#define NETATMO_BAD_CONFIG -12 #define NETATMO_BAD_CONFIG -12
#define NETATMO_DEVLIST_FAILED -13 #define NETATMO_DEVLIST_FAILED -13
#define NETATMO_GETMEASURE_FAILED -13 #define NETATMO_GETMEASURE_FAILED -14
#define NETATMO_BAD_ACCESS_TOKEN -14 #define NETATMO_BAD_ACCESS_TOKEN -15
#define NETATMO_TOKEN_EXPIRED -15 #define NETATMO_TOKEN_EXPIRED -16
#define NETATMO_USER_LIMIT_REACHED -17
extern const char *g_cfgfile; extern const char *g_cfgfile;
...@@ -107,6 +110,7 @@ extern netatmo_data_t netatmo; ...@@ -107,6 +110,7 @@ extern netatmo_data_t netatmo;
extern netatmo_vdcd_t* devlist; extern netatmo_vdcd_t* devlist;
extern pthread_mutex_t g_network_mutex; extern pthread_mutex_t g_network_mutex;
extern char g_vdc_modeluid[33];
extern char g_vdc_dsuid[35]; extern char g_vdc_dsuid[35];
extern char g_lib_dsuid[35]; extern char g_lib_dsuid[35];
......
...@@ -335,6 +335,8 @@ netatmo_get_token() ...@@ -335,6 +335,8 @@ netatmo_get_token()
} }
} else if (!strcmp(key, "scope")) { } else if (!strcmp(key, "scope")) {
// ignore // ignore
} else if (!strcmp(key, "expire_in")) {
// ignore
} else if (!strcmp(key, "error")) { } else if (!strcmp(key, "error")) {
if (type == json_type_string) { if (type == json_type_string) {
vdc_report(LOG_ERR, "network: token reponse contains error: %s\n", json_object_get_string(val)); vdc_report(LOG_ERR, "network: token reponse contains error: %s\n", json_object_get_string(val));
...@@ -392,13 +394,13 @@ static void parseModule(json_object* val, enum json_type type) ...@@ -392,13 +394,13 @@ static void parseModule(json_object* val, enum json_type type)
if (nmodule->type && (strcmp(nmodule->type, "NAModule2") == 0)) { if (nmodule->type && (strcmp(nmodule->type, "NAModule2") == 0)) {
nmodule->values_num = 4; nmodule->values_num = 4;
vmodule[0].data_type = "WindAngle"; vmodule[0].data_type = strdup("WindAngle");
vmodule[1].data_type = "WindStrength"; vmodule[1].data_type = strdup("WindStrength");
vmodule[2].data_type = "GustAngle"; vmodule[2].data_type = strdup("GustAngle");
vmodule[3].data_type = "GustStrength"; vmodule[3].data_type = strdup("GustStrength");
} else if (nmodule->type && (strcmp(nmodule->type, "NAModule3") == 0)) { } else if (nmodule->type && (strcmp(nmodule->type, "NAModule3") == 0)) {
nmodule->values_num = 1; nmodule->values_num = 1;
vmodule[0].data_type = "Rain"; vmodule[0].data_type = strdup("Rain");
} else { } else {
nmodule->values_num = dtypes->length > MAX_VALUES ? MAX_VALUES : dtypes->length; nmodule->values_num = dtypes->length > MAX_VALUES ? MAX_VALUES : dtypes->length;
for (nd = 0; nd < nmodule->values_num; nd++) { for (nd = 0; nd < nmodule->values_num; nd++) {
...@@ -465,6 +467,7 @@ int netatmo_get_devices() ...@@ -465,6 +467,7 @@ int netatmo_get_devices()
return NETATMO_GETMEASURE_FAILED; return NETATMO_GETMEASURE_FAILED;
} }
rc = NETATMO_OK;
json_object_object_foreach(jobj, key, val) { json_object_object_foreach(jobj, key, val) {
enum json_type type = json_object_get_type(val); enum json_type type = json_object_get_type(val);
//printf("key %s, type %d\n", key, type); //printf("key %s, type %d\n", key, type);
...@@ -484,9 +487,15 @@ int netatmo_get_devices() ...@@ -484,9 +487,15 @@ int netatmo_get_devices()
g_access_token = NULL; g_access_token = NULL;
g_access_token_valid_until = 0; g_access_token_valid_until = 0;
} }
rc = NETATMO_TOKEN_EXPIRED;
break;
} }
} else if (!strcmp(key, "message")) { } else if (!strcmp(key, "message")) {
vdc_report(LOG_ERR, "network: devicelist response error: %s\n", json_object_get_string(val)); vdc_report(LOG_ERR, "network: devicelist response error: %s\n", json_object_get_string(val));
if (!strcmp(json_object_get_string(val), "User usage reached")) {
rc = NETATMO_USER_LIMIT_REACHED;
break;
}
} }
} }
} }
...@@ -615,8 +624,9 @@ int netatmo_get_devices() ...@@ -615,8 +624,9 @@ int netatmo_get_devices()
free(response->memory); free(response->memory);
free(response); free(response);
if (g_access_token == NULL) { /* either access token or api error like usage limit reached */
return NETATMO_TOKEN_EXPIRED; if (rc != NETATMO_OK) {
return rc;
} }
if ((NULL == netatmo.base.bssid) || (strlen(netatmo.base.bssid) == 0)) { if ((NULL == netatmo.base.bssid) || (strlen(netatmo.base.bssid) == 0)) {
...@@ -663,6 +673,7 @@ int netatmo_get_devices() ...@@ -663,6 +673,7 @@ int netatmo_get_devices()
dev->id = strdup(buffer); dev->id = strdup(buffer);
dev->mod = m; dev->mod = m;
dev->present = true; dev->present = true;
m->zoneID = 65534;
dsuid_generate_v3_from_namespace(DSUID_NS_IEEE_MAC, buffer, &dev->dsuid); dsuid_generate_v3_from_namespace(DSUID_NS_IEEE_MAC, buffer, &dev->dsuid);
dsuid_to_string(&dev->dsuid, dev->dsuidstring); dsuid_to_string(&dev->dsuid, dev->dsuidstring);
......
...@@ -302,6 +302,62 @@ void vdc_setprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -302,6 +302,62 @@ void vdc_setprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
if (code == DSVDC_OK) { if (code == DSVDC_OK) {
write_config(); write_config();
} }
dsvdc_send_set_property_response(handle, property, code);
return;
}
netatmo_vdcd_t* dev;
pthread_mutex_lock(&g_network_mutex);
LL_FOREACH(devlist, dev)
{
if (strcasecmp(dsuid, dev->dsuidstring) == 0) {
break;
}
}
pthread_mutex_unlock(&g_network_mutex);
if (dev == NULL) {
vdc_report(LOG_WARNING, "set property: unhandled dsuid %s\n", dsuid);
dsvdc_property_free(property);
return;
}
/*
* Properties for the VDSD's
*/
pthread_mutex_lock(&g_network_mutex);
for (i = 0; i < dsvdc_property_get_num_properties(properties); i++) {
char *name;
int ret = dsvdc_property_get_name(properties, i, &name);
if (ret != DSVDC_OK) {
vdc_report(LOG_ERR, "getprop_cb: error getting property name, abort\n");
dsvdc_send_get_property_response(handle, property);
pthread_mutex_unlock(&g_network_mutex);
return;
}
if (!name) {
vdc_report(LOG_ERR, "getprop_cb: not yet handling wildcard properties\n");
//dsvdc_send_property_response(handle, property);
continue;
}
vdc_report(LOG_NOTICE, "get request name=\"%s\"\n", name);
if (strcmp(name, "zoneID") == 0) {
uint64_t zoneID;
ret = dsvdc_property_get_uint(properties, i, &zoneID);
if (ret != DSVDC_OK) {
vdc_report(LOG_ERR, "setprop_cb: error getting property value from property %s\n", name);
code = DSVDC_ERR_INVALID_VALUE_TYPE;
break;
}
vdc_report(LOG_NOTICE, "setprop_cb: \"%s\" = %d\n", name, zoneID);
dev->mod->zoneID = zoneID;
code = DSVDC_OK;
}
free(name);
} }
dsvdc_send_set_property_response(handle, property, code); dsvdc_send_set_property_response(handle, property, code);
...@@ -336,28 +392,32 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -336,28 +392,32 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
} }
vdc_report(LOG_NOTICE, "get request name=\"%s\"\n", name); vdc_report(LOG_NOTICE, "get request name=\"%s\"\n", name);
if (strcmp(name, "hardwareGuid") == 0) { if ((strcmp(name, "hardwareGuid") == 0) ||
(strcmp(name, "displayId") == 0)) {
char info[256]; char info[256];
char buffer[32]; char buffer[32];
size_t n; size_t n;
strcpy(info, "macaddress:"); memset(info, 0, sizeof(info));
if (strcmp(name, "hardwareGuid") == 0) {
strcpy(info, "macaddress:");
}
sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", netatmo.base.bssid[5], netatmo.base.bssid[4], sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", netatmo.base.bssid[5], netatmo.base.bssid[4],
netatmo.base.bssid[3], netatmo.base.bssid[2], netatmo.base.bssid[1], netatmo.base.bssid[0]); netatmo.base.bssid[3], netatmo.base.bssid[2], netatmo.base.bssid[1], netatmo.base.bssid[0]);
strcat(info, buffer); strcat(info, buffer);
dsvdc_property_add_string(property, name, info); dsvdc_property_add_string(property, name, info);
} else if (strcmp(name, "modelUID") == 0) { } else if (strcmp(name, "vendorId") == 0) {
dsvdc_property_add_string(property, name, "NetAtmo Weather Station"); } else if (strcmp(name, "oemGuid") == 0) {
} else if (strcmp(name, "modelGuid") == 0) { } else if (strcmp(name, "implementationId") == 0) {
dsvdc_property_add_string(property, name, "0.0.1.0"); dsvdc_property_add_string(property, name, "NetAtmo Weather Station");
} else if (strcmp(name, "oemGuid") == 0) { } else if (strcmp(name, "modelUID") == 0) {
dsvdc_property_add_string(property, name, "0.0.1.1"); dsvdc_property_add_string(property, name, "Weather Station");
} else if (strcmp(name, "vendorId") == 0) { } else if (strcmp(name, "modelGuid") == 0) {
dsvdc_property_add_string(property, name, "michael@tross.org"); dsvdc_property_add_string(property, name, "NetAtmo Weather Station");
} else if (strcmp(name, "name") == 0) { } else if (strcmp(name, "name") == 0) {
char info[256]; char info[256];
...@@ -366,7 +426,12 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -366,7 +426,12 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
dsvdc_property_add_string(property, name, info); dsvdc_property_add_string(property, name, info);
} else if (strcmp(name, "model") == 0) { } else if (strcmp(name, "model") == 0) {
dsvdc_property_add_string(property, name, "NetAtmo Weather Station by mtr"); char hostname[HOST_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
char servicename[HOST_NAME_MAX + 32];
strcpy(servicename, "NetAtmo Weather Station Controller @");
strcat(servicename, hostname);
dsvdc_property_add_string(property, name, servicename);
} else if (strcmp(name, "capabilities") == 0) { } else if (strcmp(name, "capabilities") == 0) {
dsvdc_property_t *reply; dsvdc_property_t *reply;
...@@ -394,6 +459,9 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -394,6 +459,9 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
} else if (strcmp(name, "x-netatmo-client-id") == 0) { } else if (strcmp(name, "x-netatmo-client-id") == 0) {
dsvdc_property_add_string(property, name, g_client_id); dsvdc_property_add_string(property, name, g_client_id);
} else if (strcmp(name, "x-netatmo-debug-level") == 0) {
dsvdc_property_add_int(property, name, vdc_get_debugLevel());
} else if (strcmp(name, "x-netatmo-connection-status") == 0) { } else if (strcmp(name, "x-netatmo-connection-status") == 0) {
dsvdc_property_add_bool(property, name, g_access_token != NULL); dsvdc_property_add_bool(property, name, g_access_token != NULL);
...@@ -444,6 +512,9 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -444,6 +512,9 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
if (strcmp(name, "primaryGroup") == 0) { if (strcmp(name, "primaryGroup") == 0) {
dsvdc_property_add_uint(property, "primaryGroup", 8); dsvdc_property_add_uint(property, "primaryGroup", 8);
} else if (strcmp(name, "zoneID") == 0) {
dsvdc_property_add_uint(property, "zoneID", dev->mod->zoneID);
} else if (strcmp(name, "buttonInputDescriptions") == 0) { } else if (strcmp(name, "buttonInputDescriptions") == 0) {
} else if (strcmp(name, "buttonInputSettings") == 0) { } else if (strcmp(name, "buttonInputSettings") == 0) {
} else if (strcmp(name, "outputDescription") == 0) { } else if (strcmp(name, "outputDescription") == 0) {
...@@ -532,8 +603,8 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -532,8 +603,8 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
struct windTypeList wl[] = { struct windTypeList wl[] = {
{ "WindAngle", 19 }, { "WindAngle", 19 },
{ "WindStrength", 13 }, { "WindStrength", 13 },
{ "GustAngle", 23 }, { "GustAngle", 24 },
{ "GustStrength", 24 }, { "GustStrength", 23 },
{ NULL, 0 } { NULL, 0 }
}; };
for (n = 0; wl[n].typeName != NULL; n++) { for (n = 0; wl[n].typeName != NULL; n++) {
...@@ -793,12 +864,16 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper ...@@ -793,12 +864,16 @@ void vdc_getprop_cb(dsvdc_t *handle, const char *dsuid, dsvdc_property_t *proper
} else if (strcmp(name, "hardwareModelGuid") == 0) { } else if (strcmp(name, "hardwareModelGuid") == 0) {
dsvdc_property_add_string(property, name, "gs1:3700730500111"); dsvdc_property_add_string(property, name, "gs1:3700730500111");
} else if (strcmp(name, "hardwareGuid") == 0) { } else if ((strcmp(name, "hardwareGuid") == 0) ||
(strcmp(name, "displayId") == 0)) {
char info[256]; char info[256];
char buffer[32]; char buffer[32];
size_t n; size_t n;
strcpy(info, "macaddress:"); memset(info, 0, sizeof(info));
if (strcmp(name, "hardwareGuid") == 0) {
strcpy(info, "macaddress:");
}
sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
dev->mod->bid[0], dev->mod->bid[1], dev->mod->bid[2], dev->mod->bid[0], dev->mod->bid[1], dev->mod->bid[2],
dev->mod->bid[3], dev->mod->bid[4], dev->mod->bid[5]); dev->mod->bid[3], dev->mod->bid[4], dev->mod->bid[5]);
......
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