2014-11-01 09:09:52 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include "sensor.h"
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
/**
|
2014-11-01 09:09:52 +08:00
|
|
|
* Sensor
|
|
|
|
*/
|
|
|
|
Sensor::Sensor()
|
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
this->next = this->prev = NULL;
|
|
|
|
Subscribe(NULL, NULL);
|
2014-11-01 09:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sensor::~Sensor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int Sensor::GetType(void)
|
|
|
|
{
|
|
|
|
return this->type;
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
int Sensor::Subscribe(SensorEventHandler_t *handler, void *user_data)
|
2014-11-01 09:09:52 +08:00
|
|
|
{
|
|
|
|
this->evtHandler = handler;
|
|
|
|
this->userData = user_data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
int Sensor::Publish(sensors_event_t *event)
|
2014-11-01 09:09:52 +08:00
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
if (this->evtHandler != NULL)
|
|
|
|
{
|
|
|
|
/* invoke subscribed handler */
|
|
|
|
(*evtHandler)(this, event, this->userData);
|
|
|
|
}
|
2014-11-01 09:09:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sensor Manager
|
|
|
|
*/
|
|
|
|
/* sensor manager instance */
|
|
|
|
static SensorManager _sensor_manager;
|
|
|
|
|
|
|
|
SensorManager::SensorManager()
|
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
sensorList = NULL;
|
2014-11-01 09:09:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SensorManager::~SensorManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
int SensorManager::RegisterSensor(Sensor *sensor)
|
2014-11-01 09:09:52 +08:00
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
SensorManager *self = &_sensor_manager;
|
|
|
|
|
2014-11-01 09:09:52 +08:00
|
|
|
RT_ASSERT(sensor != RT_NULL);
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
/* add sensor into the list */
|
|
|
|
if (self->sensorList = NULL)
|
|
|
|
{
|
|
|
|
sensor->prev = sensor->next = sensor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sensor->prev = self->sensorList;
|
|
|
|
sensor->next = self->sensorList->next;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
self->sensorList->next->prev = sensor;
|
|
|
|
self->sensorList->next = sensor;
|
|
|
|
}
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
/* point the sensorList to this sensor */
|
|
|
|
self->sensorList = sensor;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
return 0;
|
2014-11-01 09:09:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
int SensorManager::DeregisterSensor(Sensor *sensor)
|
2014-11-01 09:09:52 +08:00
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
SensorManager *self = &_sensor_manager;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
/* disconnect sensor list */
|
|
|
|
sensor->next->prev = sensor->prev;
|
|
|
|
sensor->prev->next = sensor->next;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
/* check the sensorList */
|
|
|
|
if (sensor == self->sensorList)
|
|
|
|
{
|
|
|
|
if (sensor->next == sensor) self->sensorList = NULL; /* empty list */
|
|
|
|
else self->sensorList = sensor->next;
|
|
|
|
}
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
/* re-initialize sensor node */
|
|
|
|
sensor->next = sensor->prev = sensor;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sensor *SensorManager::GetDefaultSensor(int type)
|
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
SensorManager *self = &_sensor_manager;
|
|
|
|
Sensor *sensor = self->sensorList;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
if (sensor == NULL) return NULL;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* find the same type */
|
|
|
|
if (sensor->GetType() == type) return sensor;
|
2014-11-01 09:09:52 +08:00
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
sensor = sensor->next;
|
|
|
|
}
|
|
|
|
while (sensor != self->sensorList);
|
2014-11-01 09:09:52 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:52:25 +08:00
|
|
|
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void *user_data)
|
2014-11-01 09:09:52 +08:00
|
|
|
{
|
2014-11-01 15:52:25 +08:00
|
|
|
Sensor *sensor;
|
|
|
|
|
|
|
|
sensor = SensorManager::GetDefaultSensor(type);
|
|
|
|
if (sensor != NULL)
|
|
|
|
{
|
|
|
|
sensor->Subscribe(handler, user_data);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-01 09:09:52 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|