00001
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #include <string.h>
00059
00060 #include "compiler.h"
00061 #include "FreeRTOS.h"
00062
00063 #include "tracedump.h"
00064
00065 #include "supervisor.h"
00066
00067 #include "shell.h"
00068
00069 #include "sensor.h"
00070 #include "temperature.h"
00071 #include "potentiometer.h"
00072 #include "light.h"
00073 #include "pushb.h"
00074 #include "joystick.h"
00075 #include "adc.h"
00076
00077
00078
00080 #define SENSOR_MAXNB_SENSORS (DATALOG_ID_JS - DATALOG_ID_TEMP +1)
00081
00082 #define SENSOR_LIST "{temp,pot,light,pb1,pb2,pb3,js,all}"
00083
00084
00085
00087 typedef struct SensorReg {
00088
00089 Bool (*pfConfigureSensor)( void );
00090
00091 Bool (*pfGetSensorValue)( xLogDef *pxLog );
00092
00093 eExecStatus (*pfGetSensorConfig)( signed portCHAR **ppcStringReply );
00094
00095 eExecStatus (*pfSetSensorConfig)( signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[] );
00096
00097 void (*pfStopSensor)( void );
00098 }xSensorReg;
00099
00100 xSensorReg axSensorsRegistry[SENSOR_MAXNB_SENSORS] = {
00101 { b_temperature_init, b_temperature_get_value, e_temperature_get_config, e_temperature_set_config, NULL } ,
00102 { b_potentiometer_init, b_potentiometer_get_value, e_potentiometer_get_config, e_potentiometer_set_config, NULL } ,
00103 { b_light_init, b_light_get_value, e_light_get_config, e_light_set_config, NULL } ,
00104 { b_pushb1_init, b_pushb1_get_value, e_pushb1_get_config, e_pushb1_set_config, v_pushb1_stop } ,
00105 { b_pushb2_init, b_pushb2_get_value, e_pushb2_get_config, e_pushb2_set_config, v_pushb2_stop } ,
00106 { b_pushb3_init, b_pushb3_get_value, e_pushb3_get_config, e_pushb3_set_config, v_pushb3_stop } ,
00107 { b_joystick_init, b_joystick_get_value, e_joystick_get_config, e_joystick_set_config, v_joystick_stop }
00108 };
00109
00111 const signed portCHAR *const SENSOR_ERRMSG_GETVAL_SYNTAXERROR = (signed portCHAR *)"Error"CRLF"Usage: get_sensor_value sensor=sensorname"CRLF;
00112
00114 const signed portCHAR *const SENSOR_ERRMSG_GETCFG_SYNTAXERROR = (signed portCHAR *)"Error"CRLF"Usage: get_sensor_config sensor=sensorname"CRLF;
00115
00117 const signed portCHAR *const SENSOR_ERRMSG_SETCFG_SYNTAXERROR = (signed portCHAR *)"Error"CRLF"Usage: set_sensor_config sensor=sensorname param=value"CRLF;
00118
00120 const signed portCHAR *const SENSOR_ERRMSG_GETVAL_FAIL = (signed portCHAR *)"Error"CRLF"Sensor failed to deliver a value."CRLF;
00121
00123 const signed portCHAR *const SENSOR_ERRMSG_UNREFSENSOR = (signed portCHAR *)"Error"CRLF"Unreferenced sensor name"CRLF;
00124
00125 const signed portCHAR *const SENSOR_MSG_HELP = (signed portCHAR *)"\
00126 "CRLF"get_sensor_config sensor="SENSOR_LIST" : display the config of a sensor"CRLF"\
00127 set_sensor_config sensor=temp min=val : set the min temp that triggers an alarm"CRLF"\
00128 set_sensor_config sensor=temp max=val : set the max temp that triggers an alarm"CRLF"\
00129 set_sensor_config sensor=pot min=val : set the min value that triggers an alarm"CRLF"\
00130 set_sensor_config sensor=pot max=val : set the max value that triggers an alarm"CRLF"\
00131 set_sensor_config sensor=light min=val : set the min value that triggers an alarm"CRLF"\
00132 set_sensor_config sensor=light max=val : set the max value that triggers an alarm"CRLF"\
00133 set_sensor_config sensor=pb{1,2,3} alarm={on,off} : en/disable an alarm upon event"CRLF"\
00134 set_sensor_config sensor=js alarm={on,off} : en/disable an alarm upon event"CRLF"\
00135 get_sensor_value sensor="SENSOR_LIST" : display the current value of one sensor or of all sensors"CRLF;
00136
00138 extern xSemaphoreHandle xCFGMutex;
00139
00140
00141
00147 Bool bsensor_start( void )
00148 {
00149 volatile avr32_adc_t * adc= (volatile avr32_adc_t *) &AVR32_ADC;
00150 int i;
00151
00152
00153 adc_configure( adc );
00154
00155 for( i = 0; i < SENSOR_MAXNB_SENSORS ; i++)
00156 {
00157 if ( axSensorsRegistry[i].pfConfigureSensor() != TRUE )
00158 {
00159 NAKED_TRACE_COM2( "Init sensor %d failed", i );
00160 return FALSE;
00161 }
00162 }
00163 return TRUE;
00164 }
00165
00169 void v_sensor_stop( void )
00170 {
00171 int i;
00172 for( i = 0; i < SENSOR_MAXNB_SENSORS ; i++)
00173 {
00174 if( NULL != axSensorsRegistry[i].pfStopSensor )
00175 {
00176 axSensorsRegistry[i].pfStopSensor();
00177 }
00178 }
00179 }
00180
00181
00189 Bool b_sensor_get_value( xLogDef *pxLog )
00190 {
00191 switch( pxLog->id )
00192 {
00193 case DATALOG_ID_TEMP:
00194 return( b_temperature_get_value( pxLog ) );
00195 case DATALOG_ID_POT:
00196 return( b_potentiometer_get_value( pxLog ) );
00197 case DATALOG_ID_LIGHT:
00198 return( b_light_get_value( pxLog ) );
00199 case DATALOG_ID_PB1:
00200 return( b_pushb1_get_value( pxLog ) );
00201 case DATALOG_ID_PB2:
00202 return( b_pushb2_get_value( pxLog ) );
00203 case DATALOG_ID_PB3:
00204 return( b_pushb3_get_value( pxLog ) );
00205 case DATALOG_ID_JS:
00206 return( b_joystick_get_value( pxLog ) );
00207 default:
00208 TRACE_COM2( "Unknown sensor id %d", pxLog->id );
00209 return( FALSE );
00210 }
00211 }
00212
00230 eExecStatus e_sensor_cmd_get_value( eModId xModId, signed short FsNavId,
00231 int ac, signed portCHAR *av[],
00232 signed portCHAR **ppcStringReply )
00233 {
00234 int i;
00235 xLogDef *pxLog;
00236 int end=0;
00237
00238
00239
00240 if( ppcStringReply == NULL )
00241 return( SHELL_EXECSTATUS_KO );
00242
00243
00244
00245 if( 2 != ac )
00246 {
00247 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_GETVAL_SYNTAXERROR;
00248 return( SHELL_EXECSTATUS_KO );
00249 }
00250
00251
00252
00253
00254 for( i=DATALOG_ID_TEMP;
00255 i<=DATALOG_ID_JS && strcmp( (char *)av[1], acLogSourceName[i] );
00256 i++ );
00257
00258 if( ( SENSOR_MAXNB_SENSORS == i ) && strcmp( (char *)av[1], "all" ) )
00259 {
00260 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_UNREFSENSOR;
00261 return( SHELL_EXECSTATUS_KO );
00262 }
00263
00264
00265 pxLog = pxdatalog_log_alloc_init( DATALOG_ALLOC_DYNAMIC );
00266 if( NULL == pxLog )
00267 {
00268 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00269 return( SHELL_EXECSTATUS_KO );
00270 }
00271
00272
00273 if( SENSOR_MAXNB_SENSORS != i )
00274 {
00275
00276 pxLog->id = i;
00277
00278
00279 if( FALSE == axSensorsRegistry[i].pfGetSensorValue( pxLog ) )
00280 {
00281 vdatalog_log_free( pxLog );
00282 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_GETVAL_FAIL;
00283 return( SHELL_EXECSTATUS_KO );
00284 }
00285
00286
00287
00288 *ppcStringReply = (signed portCHAR *)pvPortMalloc( DATALOG_LOG_MAXSIZE );
00289 if( NULL == *ppcStringReply )
00290 {
00291 vdatalog_log_free( pxLog );
00292 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00293 return( SHELL_EXECSTATUS_KO );
00294 }
00295
00296
00297 vdatalog_make_logstring( pxLog, *ppcStringReply );
00298
00299
00300 vdatalog_log_free( pxLog );
00301 }
00302 else
00303 {
00304
00305 *ppcStringReply = (signed portCHAR *)pvPortMalloc( SENSOR_MAXNB_SENSORS*DATALOG_LOG_MAXSIZE );
00306 if( NULL == *ppcStringReply )
00307 {
00308 vdatalog_log_free( pxLog );
00309 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00310 return( SHELL_EXECSTATUS_KO );
00311 }
00312
00313 for( i=DATALOG_ID_TEMP; i<=DATALOG_ID_JS; i++ )
00314 {
00315
00316 pxLog->id = i;
00317
00318
00319 if( FALSE == axSensorsRegistry[i].pfGetSensorValue( pxLog ) )
00320 {
00321 vdatalog_log_free( pxLog );
00322 vPortFree( *ppcStringReply );
00323 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_GETVAL_FAIL;
00324 return( SHELL_EXECSTATUS_KO );
00325 }
00326
00327
00328
00329
00330 vdatalog_make_logstring( pxLog, *ppcStringReply + end );
00331 end = strlen( (char *)*ppcStringReply );
00332
00333
00334 if( NULL != pxLog->pfFreeStringLog)
00335 {
00336 pxLog->pfFreeStringLog( pxLog->pcStringLog );
00337 }
00338 }
00339
00340 pxLog->pfFreeStringLog = NULL;
00341 vdatalog_log_free( pxLog );
00342 }
00343
00344 return( SHELL_EXECSTATUS_OK );
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 eExecStatus e_sensor_cmd_get_config( eModId xModId, signed short FsNavId,
00366 int ac, signed portCHAR *av[],
00367 signed portCHAR **ppcStringReply )
00368 {
00369 int i;
00370
00371
00372
00373 if( ppcStringReply == NULL )
00374 return( SHELL_EXECSTATUS_KO );
00375
00376
00377
00378 if( 2 != ac )
00379 {
00380 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_GETCFG_SYNTAXERROR;
00381 return( SHELL_EXECSTATUS_KO );
00382 }
00383
00384
00385
00386
00387 for( i=DATALOG_ID_TEMP;
00388 i<=DATALOG_ID_JS && strcmp( (char *)av[1], acLogSourceName[i] );
00389 i++ );
00390
00391 if( SENSOR_MAXNB_SENSORS == i )
00392 {
00393 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_UNREFSENSOR;
00394 return( SHELL_EXECSTATUS_KO );
00395 }
00396
00397
00398 return( axSensorsRegistry[i].pfGetSensorConfig( ppcStringReply ) );
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 eExecStatus e_sensor_cmd_set_config( eModId xModId, signed short FsNavId,
00422 int ac, signed portCHAR *av[],
00423 signed portCHAR **ppcStringReply )
00424 {
00425 eExecStatus xRet = SHELL_EXECSTATUS_KO;
00426 int i;
00427
00428
00429 if( pdFALSE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00430 {
00431 if( NULL != ppcStringReply )
00432 {
00433 *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MAINTENANCEMODE;
00434 }
00435 return( SHELL_EXECSTATUS_KO );
00436 }
00437 else if(ppcStringReply != NULL)
00438 {
00439
00440
00441 if( 4 != ac )
00442 {
00443 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_SETCFG_SYNTAXERROR;
00444 x_supervisor_SemaphoreGive( xCFGMutex );
00445 return( SHELL_EXECSTATUS_KO );
00446 }
00447
00448
00449
00450
00451 for( i=DATALOG_ID_TEMP;
00452 i<=DATALOG_ID_JS && strcmp( (char *)av[1], acLogSourceName[i] );
00453 i++ );
00454
00455 if( SENSOR_MAXNB_SENSORS == i )
00456 {
00457 *ppcStringReply = (signed portCHAR *)SENSOR_ERRMSG_UNREFSENSOR;
00458 x_supervisor_SemaphoreGive( xCFGMutex );
00459 return( SHELL_EXECSTATUS_KO );
00460 }
00461
00462
00463 xRet = axSensorsRegistry[i].pfSetSensorConfig( ppcStringReply, (ac - 2), &av[2] );
00464 }
00465
00466 x_supervisor_SemaphoreGive( xCFGMutex );
00467 return( xRet );
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 eExecStatus e_sensor_help( eModId xModId, signed short FsNavId,
00488 int ac, signed portCHAR *av[],
00489 signed portCHAR **ppcStringReply )
00490 {
00491
00492 if( ppcStringReply == NULL )
00493 return( SHELL_EXECSTATUS_KO );
00494
00495
00496 *ppcStringReply = (signed portCHAR *)SENSOR_MSG_HELP;
00497
00498 return( SHELL_EXECSTATUS_OK_NO_FREE );
00499 }