Blame view

Project/applications/smartcities/json.c 5.64 KB
Imanol-Mikel Barba Sabariego authored
1
2
#include "json.h"
Imanol-Mikel Barba Sabariego authored
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
uint8_t register_sensor(sensor sens)
{
        uint8_t result;
        if(sens.ID == PRESSURE_ADDR)
        {
                sens.additional_info = callibration_pressure_data_csv(get_pressure_callibration_data());
        }
        char *statement = prepare_json_register_statement(mod,sens);
        chHeapFree(sens.additional_info);
        sens.additional_info = NULL;
	char sensor_ID[3];
	sprintf(sensor_ID,"%d",sens.ID);
        result = send_json(statement,strlen(statement),mod.ID,sensor_ID);
        chHeapFree(statement);
        return result;
}
Imanol-Mikel Barba Sabariego authored
20
char* prepare_json_observation_statement(char** data, uint32_t nObservations)
Imanol-Mikel Barba Sabariego authored
21
{
Imanol-Mikel Barba Sabariego authored
22
23
24
25
26
27
28
29
	unsigned int i, length, observation_length;
	char *json_statement, *str_aux, *str_aux2;
	length = 17;
	str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE);
	for(i = 0; i < nObservations; i++)
	{
		str_aux2 = prepare_observation(data[i],strlen(data[i]));
		observation_length = strlen(str_aux2);
Imanol-Mikel Barba Sabariego authored
30
		str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM);
Imanol-Mikel Barba Sabariego authored
31
32
33
		length += observation_length;
	}
	length--; //REMOVE LAST ','
Imanol-Mikel Barba Sabariego authored
34
	json_statement = join_strings(str_aux,"]}\0",length,3,JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
35
36
	chHeapFree(str_aux);
	return json_statement;
Imanol-Mikel Barba Sabariego authored
37
38
}
Imanol-Mikel Barba Sabariego authored
39
char* prepare_json_register_statement(module mod, sensor sens)
Imanol-Mikel Barba Sabariego authored
40
{
Imanol-Mikel Barba Sabariego authored
41
	unsigned int length;
Imanol-Mikel Barba Sabariego authored
42
	char *json_statement, *str_aux, *str_aux2;
Imanol-Mikel Barba Sabariego authored
43
	char ID[MODULE_ID_LENGTH+3];
Imanol-Mikel Barba Sabariego authored
44
	length = 23;
Imanol-Mikel Barba Sabariego authored
45
	strcpy(ID,mod.ID);
Imanol-Mikel Barba Sabariego authored
46
	sprintf(ID+MODULE_ID_LENGTH,"%X",sens.ID);
Imanol-Mikel Barba Sabariego authored
47
48
49
	str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE);
	length += MODULE_ID_LENGTH+2;
	str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
50
	chHeapFree(str_aux);
Imanol-Mikel Barba Sabariego authored
51
	length += 17;
Imanol-Mikel Barba Sabariego authored
52
	str_aux = join_strings(str_aux2,sens.description,length,strlen(sens.description),JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
53
	chHeapFree(str_aux2);
Imanol-Mikel Barba Sabariego authored
54
	length += strlen(sens.description);
Imanol-Mikel Barba Sabariego authored
55
	str_aux2 = join_strings(str_aux,"\",\"type\":\"",length,10,JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
56
	chHeapFree(str_aux);
Imanol-Mikel Barba Sabariego authored
57
	length += 10;
Imanol-Mikel Barba Sabariego authored
58
	str_aux = join_strings(str_aux2,sens.type,length,strlen(sens.type),JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
59
	chHeapFree(str_aux2);
Imanol-Mikel Barba Sabariego authored
60
	length += strlen(sens.type);
Imanol-Mikel Barba Sabariego authored
61
62
63
	str_aux2 = join_strings(str_aux,"\",\"unit\":\"",length,10,JOIN_NO_FREE);
	chHeapFree(str_aux);
	length += 10;
Imanol-Mikel Barba Sabariego authored
64
	str_aux = join_strings(str_aux2,sens.unit,length,strlen(sens.unit),JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
65
	chHeapFree(str_aux2);
Imanol-Mikel Barba Sabariego authored
66
	length += strlen(sens.unit);
Imanol-Mikel Barba Sabariego authored
67
68
69
	str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE);
	chHeapFree(str_aux);
	length += 14;
Imanol-Mikel Barba Sabariego authored
70
	str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
71
	chHeapFree(str_aux2);
Imanol-Mikel Barba Sabariego authored
72
	length += strlen(mod.geoloc);
Imanol-Mikel Barba Sabariego authored
73
74
75
76
77
78
	if(sens.additional_info != NULL)
	{
		str_aux2 = join_strings(str_aux,sens.additional_info,length,strlen(sens.additional_info),JOIN_NO_FREE);
		chHeapFree(str_aux2);
		length += strlen(sens.additional_info);
	}
Imanol-Mikel Barba Sabariego authored
79
80
81
	json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE);
	chHeapFree(str_aux);
	return json_statement;
Imanol-Mikel Barba Sabariego authored
82
83
}
Imanol-Mikel Barba Sabariego authored
84
85
char* prepare_observation(char* observation, uint32_t length)
{
Imanol-Mikel Barba Sabariego authored
86
	char *json_data, *str_aux, *str_aux2;
Imanol-Mikel Barba Sabariego authored
87
88
89
90
91
92
93
	/* TOPKEK
	 *
	 * ¿Gestión de errores?
	 * ¿PA QUÉ?
	 *
	 * --le trole
	 */
Imanol-Mikel Barba Sabariego authored
94
	int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!!
Imanol-Mikel Barba Sabariego authored
95
96
97
98
99
100
101
	int timestamp_length = length - (value_length + 1);
	char *value = (char*) chHeapAlloc(NULL,value_length+1);
	char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1);
	strncpy(value,observation,value_length);
	strncpy(timestamp,observation+(value_length+1),timestamp_length);
	value[value_length] = '\0';
	timestamp[timestamp_length] = '\0';
Imanol-Mikel Barba Sabariego authored
102
103
104
105
	str_aux = join_strings("{\"value\":\"",value,10,value_length,JOIN_NO_FREE);
	str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,JOIN_NO_FREE);
	str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,JOIN_FREE_MEM);
	json_data = join_strings(str_aux2,"\"},",25+value_length+timestamp_length,3,JOIN_NO_FREE);
Imanol-Mikel Barba Sabariego authored
106
107
108
109
	chHeapFree(str_aux);
	chHeapFree(str_aux2);
	chHeapFree(value);
	return json_data;
Imanol-Mikel Barba Sabariego authored
110
111
}
Imanol-Mikel Barba Sabariego authored
112
uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID)
Imanol-Mikel Barba Sabariego authored
113
{
Imanol-Mikel Barba Sabariego authored
114
	int connectivity, response_code;
Imanol-Mikel Barba Sabariego authored
115
	char *URL, *PATH;
Imanol-Mikel Barba Sabariego authored
116
	connectivity = libwismart_IsConnected();
Imanol-Mikel Barba Sabariego authored
117
	if(connectivity != WISMART_CONNECTED)
Imanol-Mikel Barba Sabariego authored
118
119
120
	{
		return JSON_COMM_ERROR;
	}
Imanol-Mikel Barba Sabariego authored
121
	if(sensor_ID == NULL) //Register sensor
Imanol-Mikel Barba Sabariego authored
122
	{
Imanol-Mikel Barba Sabariego authored
123
		URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME));
Imanol-Mikel Barba Sabariego authored
124
		strcpy(URL,SERVER_HOSTNAME);
Imanol-Mikel Barba Sabariego authored
125
126
127
		PATH = (char*) chHeapAlloc(NULL,10+strlen(provider_ID));
		strcpy(PATH,"/catalog/");
		strcpy(PATH+9,provider_ID);
Imanol-Mikel Barba Sabariego authored
128
	}
Imanol-Mikel Barba Sabariego authored
129
	else //Post data
Imanol-Mikel Barba Sabariego authored
130
	{
Imanol-Mikel Barba Sabariego authored
131
132
133
134
135
136
137
138
		URL = (char*) chHeapAlloc(NULL,1+strlen(SERVER_HOSTNAME));
		strcpy(URL,SERVER_HOSTNAME);
		URL[strlen(SERVER_HOSTNAME)] = '\0';
		PATH = (char*) chHeapAlloc(NULL,22+strlen(provider_ID)+strlen(sensor_ID));
		strcpy(PATH,"/data/modularupload/");
		strcpy(PATH+20,provider_ID);
		strcpy(PATH+20+strlen(provider_ID),sensor_ID);
		PATH[21+strlen(provider_ID)+strlen(sensor_ID)] = '\0';
Imanol-Mikel Barba Sabariego authored
139
	}
Imanol-Mikel Barba Sabariego authored
140
141
142
143
	struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)};
	response_code = httpRequest(server, statement, length);
	chHeapFree(PATH);
	chHeapFree(URL);
Imanol-Mikel Barba Sabariego authored
144
	if(response_code == 200)
Imanol-Mikel Barba Sabariego authored
145
146
147
148
149
150
151
152
153
154
155
	{
		return JSON_POST_OK;
	}
	else if((response_code >= 400) && (response_code < 500))
	{
		return JSON_COMM_ERROR;
	}
	else
	{
		return JSON_OTHER_ERROR;
	}
Imanol-Mikel Barba Sabariego authored
156
157
158
159
160
161
162
}

uint32_t find_next_index(char* string, uint32_t length, char delimiter)
{
	unsigned int i;
	for(i = 0; i < length; i++)
	{
Imanol-Mikel Barba Sabariego authored
163
		if(string[i] == delimiter)
Imanol-Mikel Barba Sabariego authored
164
165
166
167
168
169
170
		{
			return i;
		}
	}
	return -1;
}
Imanol-Mikel Barba Sabariego authored
171
char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem)
Imanol-Mikel Barba Sabariego authored
172
173
{
	char* str = (char*) chHeapAlloc(NULL,len1+len2+1);
Imanol-Mikel Barba Sabariego authored
174
175
176
177
178
	strncpy(str,str1,len1);
	str[len1] = '\0';
	strncat(str,str2,len2);
	if(free_mem)
	{
Imanol-Mikel Barba Sabariego authored
179
180
		chHeapFree(str1);
		chHeapFree(str2);
Imanol-Mikel Barba Sabariego authored
181
182
	}
	return str; 
Imanol-Mikel Barba Sabariego authored
183
}