Skip to content

Commit

Permalink
Implement sleep framework
Browse files Browse the repository at this point in the history
  • Loading branch information
eberseth committed Oct 27, 2020
1 parent 4c75cdc commit a357141
Show file tree
Hide file tree
Showing 29 changed files with 2,023 additions and 235 deletions.
40 changes: 20 additions & 20 deletions lib/bmi160/src/bmi160.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int Bmi160::setSpiMode() {
}

int Bmi160::initialize() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

uint8_t reg = 0;
Expand Down Expand Up @@ -101,7 +101,7 @@ int Bmi160::initialize() {
}

int Bmi160::cleanup() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

// Disable interrupts, set defaults
Expand All @@ -115,7 +115,7 @@ int Bmi160::cleanup() {
}

int Bmi160::begin(const TwoWire* interface, uint8_t address, pin_t interruptPin, size_t eventDepth) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_FALSE(initialized_, SYSTEM_ERROR_NONE);
CHECK_TRUE(interface, SYSTEM_ERROR_INVALID_ARGUMENT);

Expand Down Expand Up @@ -147,7 +147,7 @@ int Bmi160::begin(const TwoWire* interface, uint8_t address, pin_t interruptPin,
}

int Bmi160::begin(const SPIClass& interface, pin_t selectPin, pin_t interruptPin, size_t eventDepth) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_FALSE(initialized_, SYSTEM_ERROR_NONE);

if (os_queue_create(&motionSyncQueue_, sizeof(Bmi160EventType), eventDepth, nullptr)) {
Expand All @@ -173,7 +173,7 @@ int Bmi160::begin(const SPIClass& interface, pin_t selectPin, pin_t interruptPin
}

int Bmi160::end() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_NONE);

cleanup();
Expand All @@ -189,7 +189,7 @@ int Bmi160::end() {
}

int Bmi160::reset() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);
if (type_ == InterfaceType::BMI_SPI) {
CHECK(setSpiMode());
Expand All @@ -207,7 +207,7 @@ int Bmi160::reset() {
}

int Bmi160::sleep() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);
CHECK(writeRegister(Bmi160Register::CMD_ADDR, Bmi160Command::CMD_ACC_PMU_MODE_SUSPEND));
delay(BMI160_ACC_PMU_CMD_TIME);
Expand All @@ -216,7 +216,7 @@ int Bmi160::sleep() {
}

int Bmi160::wakeup() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);
CHECK(writeRegister(Bmi160Register::CMD_ADDR, Bmi160Command::CMD_ACC_PMU_MODE_LOW));
delay(BMI160_ACC_PMU_CMD_TIME);
Expand Down Expand Up @@ -489,7 +489,7 @@ int Bmi160::setAccelHighGHysteresis(float& hysteresis, bool feedback) {
}

int Bmi160::initAccelerometer(Bmi160AccelerometerConfig& config, bool feedback) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

// Setting maximum range
Expand All @@ -502,7 +502,7 @@ int Bmi160::initAccelerometer(Bmi160AccelerometerConfig& config, bool feedback)
}

int Bmi160::getAccelerometer(Bmi160Accelerometer& data) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

uint8_t buffer[6];
Expand All @@ -518,7 +518,7 @@ int Bmi160::getAccelerometer(Bmi160Accelerometer& data) {
}

int Bmi160::getAccelerometerPmu(Bmi160PowerState& pmu) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);
uint8_t val = 0;
CHECK(readRegister(Bmi160Register::PMU_STATUS_ADDR, &val));
Expand Down Expand Up @@ -548,7 +548,7 @@ int Bmi160::getAccelerometerPmu(Bmi160PowerState& pmu) {
}

int Bmi160::startMotionDetect() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

uint8_t reg = 0;
Expand All @@ -562,7 +562,7 @@ int Bmi160::startMotionDetect() {
}

int Bmi160::stopMotionDetect() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

uint8_t reg = 0;
Expand All @@ -576,7 +576,7 @@ int Bmi160::stopMotionDetect() {
}

int Bmi160::initMotion(Bmi160AccelMotionConfig& config, bool feedback) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

// Setting motion threshold
Expand All @@ -602,7 +602,7 @@ int Bmi160::initMotion(Bmi160AccelMotionConfig& config, bool feedback) {
}

int Bmi160::initHighG(Bmi160AccelHighGConfig& config, bool feedback) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

// Setting high G threshold
Expand All @@ -618,7 +618,7 @@ int Bmi160::initHighG(Bmi160AccelHighGConfig& config, bool feedback) {
}

int Bmi160::startHighGDetect() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

uint8_t reg = 0;
Expand All @@ -632,7 +632,7 @@ int Bmi160::startHighGDetect() {
}

int Bmi160::stopHighGDetect() {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);

uint8_t reg = 0;
Expand All @@ -646,7 +646,7 @@ int Bmi160::stopHighGDetect() {
}

int Bmi160::getStatus(uint32_t& val, bool clear) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);
CHECK(readRegister(Bmi160Register::INT_STATUS_0_ADDR, reinterpret_cast<uint8_t*>(&val), sizeof(val)));

Expand All @@ -669,7 +669,7 @@ bool Bmi160::isHighGDetect(uint32_t val) {
}

int Bmi160::getChipId(uint8_t& val) {
const std::lock_guard<std::recursive_mutex> lock(mutex_);
const std::lock_guard<RecursiveMutex> lock(mutex_);
CHECK_TRUE(initialized_, SYSTEM_ERROR_INVALID_STATE);
return readRegister(Bmi160Register::CHIPID_ADDR, &val);
}
Expand Down Expand Up @@ -758,4 +758,4 @@ int Bmi160::readRegister(uint8_t reg, uint8_t* val, int length) {
return SYSTEM_ERROR_INVALID_STATE;
}

std::recursive_mutex Bmi160::mutex_;
RecursiveMutex Bmi160::mutex_;
2 changes: 1 addition & 1 deletion lib/bmi160/src/bmi160.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class Bmi160 {
float rateAccel_;
uint8_t latchShadow_;
os_queue_t motionSyncQueue_;
static std::recursive_mutex mutex_;
static RecursiveMutex mutex_;
}; // class Bmi160

#define BMI160 Bmi160::getInstance()
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud-service/src/background_publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void BackgroundPublish::thread_f()
bool BackgroundPublish::publish(const char *name, const char *data, PublishFlags flags, publish_completed_cb_t cb, const void *context)
{
// protect against separate threads trying to publish at the same time
std::lock_guard<std::recursive_mutex> lg(mutex);
std::lock_guard<RecursiveMutex> lg(mutex);

// check currently in idle state and ready to accept publish request
if(!thread || state != BACKGROUND_PUBLISH_IDLE)
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud-service/src/background_publish.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class BackgroundPublish
private:
Thread *thread = NULL;
void thread_f();
std::recursive_mutex mutex;
RecursiveMutex mutex;
volatile publish_thread_state_t state = BACKGROUND_PUBLISH_IDLE;

// arguments for Particle.publish
Expand Down
10 changes: 5 additions & 5 deletions lib/cloud-service/src/cloud_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void CloudService::init()
void CloudService::tick()
{
auto sec = System.uptime();
std::lock_guard<std::recursive_mutex> lg(mutex);
std::lock_guard<RecursiveMutex> lg(mutex);

if(sec != last_tick_sec)
{
Expand Down Expand Up @@ -90,7 +90,7 @@ void CloudService::tick_sec()

int CloudService::regCommandCallback(const char *cmd, cloud_service_cb_t cb, uint32_t req_id, uint32_t timeout_ms, const void *context)
{
std::lock_guard<std::recursive_mutex> lg(mutex);
std::lock_guard<RecursiveMutex> lg(mutex);
cloud_service_handler_t handler = {CloudServicePublishFlags::NONE, cb, "", req_id, timeout_ms, context, millis()};

if(!cb)
Expand Down Expand Up @@ -210,7 +210,7 @@ int CloudService::dispatchCommand(String data)
return -EINVAL;
}

std::lock_guard<std::recursive_mutex> lg(mutex);
std::lock_guard<RecursiveMutex> lg(mutex);
auto it = handlers.begin();
while(it != handlers.end())
{
Expand Down Expand Up @@ -302,7 +302,7 @@ void CloudService::publish_cb(
const char *event_data,
const void *event_context)
{
std::lock_guard<std::recursive_mutex> lg(mutex);
std::lock_guard<RecursiveMutex> lg(mutex);

if(!event_context)
{
Expand Down Expand Up @@ -342,7 +342,7 @@ int CloudService::send(const char *event,
{
int rval = 0;
size_t event_len = strlen(event);
std::lock_guard<std::recursive_mutex> lg(mutex);
std::lock_guard<RecursiveMutex> lg(mutex);

if(!event_name ||
(!req_id && cb && (cloud_flags & CloudServicePublishFlags::FULL_ACK)))
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud-service/src/cloud_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class CloudService
std::list<cloud_service_handler_t> handlers;
std::list<cloud_service_handler_t> deferred_handlers;

std::recursive_mutex mutex;
RecursiveMutex mutex;
};

template <typename T>
Expand Down
30 changes: 21 additions & 9 deletions lib/config-service/src/config_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ int ConfigObject::exit(bool write, int status)
bool ConfigFloat::check(double value)
{
return (
(isnan(range_min) || value >= range_min) &&
(isnan(range_min) || value >= range_min) &&
(isnan(range_max) || value <= range_max)
);
};
Expand Down Expand Up @@ -168,7 +168,7 @@ void ConfigService::init()
CloudService::instance().regCommandCallback("set_cfg", &ConfigService::set_cfg_cb, this);
CloudService::instance().regCommandCallback("get_cfg", &ConfigService::get_cfg_cb, this);
CloudService::instance().regCommandCallback("reset_to_factory", &ConfigService::reset_to_factory_cb, this);

struct stat st;

if (!stat(CONFIG_SERVICE_FS_PATH, &st))
Expand Down Expand Up @@ -198,6 +198,18 @@ void ConfigService::tick()
}
}

void ConfigService::flush()
{
if(fs_ok)
{
for(auto &it : configs)
{
config_hash(it.root, it.hash);
}
save_all();
}
}

void ConfigService::tick_sec()
{
// iterate through all configs once a second to publish config updates
Expand Down Expand Up @@ -375,12 +387,12 @@ int ConfigService::_save(config_service_desc_t &config_desc, bool force)

close(fd);

// then rename over the existing config
// then rename over the existing config
if(!error)
{
if(_rename(temp_filename, filename))
{
error = -errno;
error = -errno;
}
else
{
Expand Down Expand Up @@ -472,7 +484,7 @@ int ConfigService::_load(config_service_desc_t &config_desc)
}

int fd = open(filename, O_RDONLY);

if(fd < 0)
{
return errno;
Expand Down Expand Up @@ -583,7 +595,7 @@ int ConfigService::get_cfg_cb(CloudServiceStatus status, JSONValue *root, const
}

CloudService::instance().sendAck(*root, rval);

return rval;
}

Expand Down Expand Up @@ -625,7 +637,7 @@ int ConfigService::set_cfg_cb(CloudServiceStatus status, JSONValue *root, const
auto _rval = _config_process_json(_config, it->root->name(), it->root);

// want to continue processing all modules and not abort if
// one module fails but still report overall failure if
// one module fails but still report overall failure if
// an earlier module fails so only update the overall rval
// on an actual failure code
if(_rval)
Expand Down Expand Up @@ -841,7 +853,7 @@ int config_write_json(ConfigNode *root, JSONWriter &writer)
{
writer.beginObject();
}

for(int i=0; i < object_node->child_count(); i++)
{
auto child = object_node->child(i);
Expand Down Expand Up @@ -931,7 +943,7 @@ void _config_hash(ConfigNode *root, murmur3_hash_t &hash)
{
murmur3_hash_update(hash, root->name(), strlen(root->name()));
}

for(int i=0; i < object_node->child_count(); i++)
{
auto child = object_node->child(i);
Expand Down
2 changes: 2 additions & 0 deletions lib/config-service/src/config_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class ConfigService

void resetToFactory();

void flush();

private:
ConfigService();
static ConfigService *_instance;
Expand Down
Loading

0 comments on commit a357141

Please sign in to comment.