1111 products derived from this software without specific prior written permission.
1212 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
1313 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1515 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
17- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
17+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
1818 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1919************************************************************************************************************************/
2020
2626#include <unistd.h>
2727#include <letmecreate/letmecreate.h>
2828#include <getopt.h>
29- #include <stdio.h>
3029#include <string.h>
31- #include <stdlib.h>
3230#include <awa/common.h>
3331#include <awa/client.h>
3432#include <awa/types.h>
3937typedef float (* SensorReadFunc )(uint8_t );
4038
4139typedef enum {
42- ClickType_None = 0 ,
40+ ClickType_None ,
4341 ClickType_Thermo3 ,
4442 ClickType_Weather ,
4543 ClickType_Thunder ,
@@ -53,9 +51,14 @@ AwaClientSession* g_ClientSession;
5351int g_LogLevel = LOG_INFO ;
5452FILE * g_DebugStream ;
5553int g_SleepTime = 60 ; //default 1 minute
54+ static volatile bool _Running = true;
55+
56+ static void exitApp (int __attribute__((unused ))(signo )) {
57+ _Running = false;
58+ }
5659
5760ClickType configDecodeClickType (char * type ) {
58- static struct element {
61+ struct element {
5962 char * name ;
6063 ClickType mapsTo ;
6164 };
@@ -70,9 +73,9 @@ ClickType configDecodeClickType(char* type) {
7073
7174 struct element * iter = & types [0 ];
7275 while (iter -> name != NULL ) {
73- if (strcasecmp (iter -> name , type ) == 0 ) {
76+ if (strcasecmp (iter -> name , type ) == 0 )
7477 return iter -> mapsTo ;
75- }
78+
7679 iter ++ ;
7780 }
7881
@@ -94,7 +97,7 @@ static void printUsage(const char *program)
9497 program );
9598}
9699
97- bool loadConfiguration (int argc , char * * argv ) {
100+ static bool loadConfiguration (int argc , char * * argv ) {
98101 int c ;
99102 bool success = true;
100103
@@ -111,7 +114,8 @@ bool loadConfiguration(int argc, char **argv) {
111114 int option_index = 0 ;
112115 c = getopt_long (argc , argv , "s:1:2:c:b:hv:" , long_options , & option_index );
113116
114- if (c == -1 ) break ;
117+ if (c == -1 )
118+ break ;
115119
116120 switch (c ) {
117121 case '1' :
@@ -148,7 +152,7 @@ bool loadConfiguration(int argc, char **argv) {
148152 return success ;
149153}
150154
151- float readThermo3 (uint8_t busIndex ) {
155+ static float readThermo3 (uint8_t busIndex ) {
152156 LOG (LOG_DEBUG , "Reading thermo3 on bus#%d" , busIndex );
153157 float temperature = 0.f ;
154158
@@ -161,7 +165,7 @@ float readThermo3(uint8_t busIndex) {
161165 return temperature ;
162166}
163167
164- float readCO (uint8_t busIndex ) {
168+ static float readCO (uint8_t busIndex ) {
165169 LOG (LOG_DEBUG , "Reading CO on bus#%d" , busIndex );
166170 uint16_t value = 0 ;
167171
@@ -170,7 +174,7 @@ float readCO(uint8_t busIndex) {
170174 return value ;
171175}
172176
173- float readAirQuality (uint8_t busIndex ) {
177+ static float readAirQuality (uint8_t busIndex ) {
174178 LOG (LOG_DEBUG , "Reading air quality on bus#%d" , busIndex );
175179 uint16_t value = 0 ;
176180
@@ -179,19 +183,19 @@ float readAirQuality(uint8_t busIndex) {
179183 return value ;
180184}
181185
182- uint8_t readWeather (uint8_t busIndex , double * data ) {
183- LOG (LOG_DEBUG , "Reading weather on bus#%d" , busIndex );
186+ static uint8_t readWeather (uint8_t busIndex , double * data ) {
187+ LOG (LOG_DEBUG , "Reading weather on bus#%d" , busIndex );
184188
185- i2c_select_bus (busIndex );
186- if (weather_click_read_measurements (& data [0 ], & data [1 ], & data [2 ]) < 0 ) {
187- LOG (LOG_ERROR , "Reading weather measurements failed!" );
188- return -1 ;
189- }
189+ i2c_select_bus (busIndex );
190+ if (weather_click_read_measurements (& data [0 ], & data [1 ], & data [2 ]) < 0 ) {
191+ LOG (LOG_ERROR , "Reading weather measurements failed!" );
192+ return -1 ;
193+ }
190194
191- return 0 ;
195+ return 0 ;
192196}
193197
194- bool connectToAwa () {
198+ static bool connectToAwa (void ) {
195199 g_ClientSession = AwaClientSession_New ();
196200
197201 if (g_ClientSession != NULL ) {
@@ -214,15 +218,15 @@ bool connectToAwa() {
214218 return g_ClientSession != NULL ;
215219}
216220
217- void disconnectAwa () {
218- if (g_ClientSession == NULL ) {
221+ static void disconnectAwa (void ) {
222+ if (g_ClientSession == NULL )
219223 return ;
220- }
224+
221225 AwaClientSession_Disconnect (g_ClientSession );
222226 AwaClientSession_Free (& g_ClientSession );
223227}
224228
225- void createIPSO (int objectId , int instance , int resourceId ) {
229+ static void createIPSO (int objectId , int instance , int resourceId ) {
226230 AwaClientSetOperation * operation = AwaClientSetOperation_New (g_ClientSession );
227231
228232 char buf [40 ];
@@ -241,7 +245,7 @@ void createIPSO(int objectId, int instance, int resourceId) {
241245 AwaClientSetOperation_Free (& operation );
242246}
243247
244- void setIPSO (int objectId , int instance , int resourceId , float value , bool shouldRetry ) {
248+ static void setIPSO (int objectId , int instance , int resourceId , float value , bool shouldRetry ) {
245249 char buf [40 ];
246250 sprintf (& buf [0 ], "/%d/%d/%d" , objectId , instance , resourceId );
247251 LOG (LOG_INFO , "Storing value %0.3f into %s" , value , & buf [0 ]);
@@ -259,7 +263,7 @@ void setIPSO(int objectId, int instance, int resourceId, float value, bool shoul
259263 }
260264}
261265
262- float getIPSO (int objectId , int instance , int resourceId , float defaultValue ) {
266+ static float getIPSO (int objectId , int instance , int resourceId , float defaultValue ) {
263267 char buf [40 ];
264268 sprintf (& buf [0 ], "/%d/%d/%d" , objectId , instance , resourceId );
265269 LOG (LOG_DEBUG , "Getting value of %s" , & buf [0 ]);
@@ -284,86 +288,85 @@ float getIPSO(int objectId, int instance, int resourceId, float defaultValue) {
284288 return resultValue ;
285289}
286290
287- uint8_t setMeasurement (int objId , int instance , double value ) {
291+ static uint8_t setMeasurement (int objId , int instance , double value ) {
288292
289- float minValue = getIPSO (objId , instance , 5601 , 1000 );
290- float maxValue = getIPSO (objId , instance , 5602 , -1000 );
293+ float minValue = getIPSO (objId , instance , 5601 , 1000 );
294+ float maxValue = getIPSO (objId , instance , 5602 , -1000 );
291295
292- setIPSO (objId , instance , 5700 , value , true);
293- if (minValue > value ) {
294- setIPSO (objId , instance , 5601 , value , true);
295- }
296- if (maxValue < value ) {
297- setIPSO (objId , instance , 5602 , value , true);
298- }
299- return 0 ;
296+ setIPSO (objId , instance , 5700 , value , true);
297+ if (minValue > value ) {
298+ setIPSO (objId , instance , 5601 , value , true);
299+ }
300+ if (maxValue < value ) {
301+ setIPSO (objId , instance , 5602 , value , true);
302+ }
303+ return 0 ;
300304}
301305
302- void handleMeasurements (uint8_t bus , int objId , int instance , SensorReadFunc sensorFunc ) {
306+ static void handleMeasurements (uint8_t bus , int objId , int instance , SensorReadFunc sensorFunc ) {
303307 float value = sensorFunc (bus );
304308 setMeasurement (objId , instance , value );
305309}
306310
307311
308- void handleWeatherMeasurements (uint8_t busIndex ,
309- int temperatureInstance , int pressureInstance , int humidityInstance ) {
312+ static void handleWeatherMeasurements (uint8_t busIndex ,
313+ int temperatureInstance , int pressureInstance , int humidityInstance ) {
310314
311- double data [] = {0 ,0 ,0 };
315+ double data [] = {0 ,0 ,0 };
312316 if (readWeather (busIndex , data ) < 0 ) {
313- LOG (LOG_ERROR , "Reading weather on bus#%d failed!" , busIndex );
314- return ;
317+ LOG (LOG_ERROR , "Reading weather on bus#%d failed!" , busIndex );
318+ return ;
315319 }
316320 LOG (LOG_INFO , "Reading weather measurements: temp = %f, pressure = %f, humidity = %f" ,
317- data [0 ], data [1 ], data [2 ]);
321+ data [0 ], data [1 ], data [2 ]);
318322 setMeasurement (3303 , temperatureInstance , data [0 ]);
319323 setMeasurement (3315 , pressureInstance , data [1 ]);
320324 setMeasurement (3304 , humidityInstance , data [2 ]);
321325}
322326
323- void performMeasurements () {
324- if (connectToAwa () == false) {
327+ static void performMeasurements (void ) {
328+ if (connectToAwa () == false)
325329 return ;
326- }
327330
328331 int index ;
329- int instanceIndex [] = {0 , //3303 - temperature
330- 1 , //3304 - humidity
331- 2 , //3315 - barometer
332- 3 , //3325 - concentration
333- 4 , //3330 - distance
334- 5 }; //3328 - power
332+ int instanceIndex [] = {0 , //3303 - temperature
333+ 1 , //3304 - humidity
334+ 2 , //3315 - barometer
335+ 3 , //3325 - concentration
336+ 4 , //3330 - distance
337+ 5 }; //3328 - power
335338
336339 //contains last used instance ids for all registered sensors
337- int instances [] = {0 , //3303
338- 0 , //3304
339- 0 , //3315
340- 0 , //3325
341- 0 , //3330
342- 0 }; //3328
340+ int instances [] = {0 , //3303
341+ 0 , //3304
342+ 0 , //3315
343+ 0 , //3325
344+ 0 , //3330
345+ 0 }; //3328
343346
344347 for (index = 0 ; index < 2 ; index ++ ) {
345348 uint8_t bus = index == 0 ? MIKROBUS_1 : MIKROBUS_2 ;
346349
347350 switch (index == 0 ? g_Click1Type : g_Click2Type ) {
348351 case ClickType_Thermo3 :
349- handleMeasurements (bus , 3303 , instances [instanceIndex [0 ]]++ , & readThermo3 );
352+ handleMeasurements (bus , 3303 , instances [instanceIndex [0 ]]++ , & readThermo3 );
350353
351354 break ;
352355
353356 case ClickType_Weather :
354- handleWeatherMeasurements (bus ,
355- instances [instanceIndex [0 ]]++ ,
356- instances [instanceIndex [1 ]]++ ,
357- instances [instanceIndex [2 ]]++ );
357+ handleWeatherMeasurements (bus ,
358+ instances [instanceIndex [0 ]]++ ,
359+ instances [instanceIndex [1 ]]++ ,
360+ instances [instanceIndex [2 ]]++ );
358361
359- break ;
362+ break ;
360363 case ClickType_Thunder :
361- break ;
364+ break ;
362365 case ClickType_AirQuality :
363- handleMeasurements (bus , 3325 , instances [instanceIndex [3 ]]++ , & readAirQuality );
364- break ;
366+ handleMeasurements (bus , 3325 , instances [instanceIndex [3 ]]++ , & readAirQuality );
367+ break ;
365368 case ClickType_CODetector :
366- handleMeasurements (bus , 3325 , instances [instanceIndex [3 ]]++ , & readCO );
369+ handleMeasurements (bus , 3325 , instances [instanceIndex [3 ]]++ , & readCO );
367370 break ;
368371 default :
369372 break ;
@@ -373,48 +376,52 @@ void performMeasurements() {
373376 disconnectAwa ();
374377}
375378
376- void cleanupOnExit ( ) {
377- i2c_release () ;
378- disconnectAwa ();
379- }
379+ static void initialize ( void ) {
380+ int index ;
381+ for ( index = 0 ; index < 2 ; index ++ ) {
382+ uint8_t bus = index == 0 ? MIKROBUS_1 : MIKROBUS_2 ;
380383
381- void initialize () {
382- int index ;
383- for (index = 0 ; index < 2 ; index ++ ) {
384- uint8_t bus = index == 0 ? MIKROBUS_1 : MIKROBUS_2 ;
385-
386- switch (index == 0 ? g_Click1Type : g_Click2Type ) {
387- case ClickType_Thermo3 :
388- break ;
389- case ClickType_Weather :
390- i2c_select_bus (index );
391- if (weather_click_enable () < 0 ) {
392- LOG (LOG_ERROR , "Failed to enable weather click on bus#%d\n" , index );
393- }
394- break ;
395-
396- //TODO: add rest if needed
397- default :
398- break ;
399- }
400- }
384+ switch (index == 0 ? g_Click1Type : g_Click2Type ) {
385+ case ClickType_Thermo3 :
386+ break ;
387+ case ClickType_Weather :
388+ i2c_select_bus (index );
389+ if (weather_click_enable () < 0 )
390+ LOG (LOG_ERROR , "Failed to enable weather click on bus#%d\n" , index );
391+ break ;
392+
393+ //TODO: add rest if needed
394+ default :
395+ break ;
396+ }
397+ }
401398}
402399
403400int main (int argc , char * * argv ) {
404- if (loadConfiguration (argc , argv ) == false) {
401+ struct sigaction action = {
402+ .sa_handler = exitApp ,
403+ .sa_flags = 0
404+ };
405+
406+ if (loadConfiguration (argc , argv ) == false)
405407 return -1 ;
406- }
407408
408- signal (SIGINT , & cleanupOnExit );
409- atexit (& cleanupOnExit );
409+ if (sigemptyset (& action .sa_mask ) < 0
410+ || sigaction (SIGINT , & action , NULL ) < 0 ) {
411+ LOG (LOG_ERROR , "Failed to set Control+C handler\n" );
412+ return -1 ;
413+ }
410414
411415 i2c_init ();
412416
413417 initialize ();
414- while (true ) {
418+ while (_Running ) {
415419 performMeasurements ();
416420 sleep (g_SleepTime );
417421 }
418422
423+ i2c_release ();
424+ disconnectAwa ();
425+
419426 return 0 ;
420427}
0 commit comments