Skip to content

Commit

Permalink
Curate more C++ features. (#130)
Browse files Browse the repository at this point in the history
* Curate more C++ features.

Strengthen immutability by adding more const qualifiers.
Use some more iterators.
Reduce binary size further by disabling exceptions.

* Remove stale use of placeholders.

* More references, fewer pointers.
  • Loading branch information
gkoh authored Sep 22, 2024
1 parent f146821 commit 321d5a1
Show file tree
Hide file tree
Showing 23 changed files with 152 additions and 154 deletions.
6 changes: 3 additions & 3 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ esp_power_level_t settings_load_esp_tx_power(void);
bool settings_load_gps(void);
void settings_menu_gps(void);

void settings_load_interval(interval_t *interval);
void settings_save_interval(interval_t *interval);
void settings_load_interval(interval_t &interval);
void settings_save_interval(const interval_t &interval);

void settings_add_interval_items(ezMenu *submenu, interval_t *interval);
void settings_add_interval_items(ezMenu *submenu, interval_t &interval);
void settings_menu_interval(void);

bool settings_load_multiconnect(void);
Expand Down
6 changes: 3 additions & 3 deletions include/spinner.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ struct __attribute__((packed)) SpinValue {
spin_unit_t unit;
};

void spinner_modify_value(const char *title, bool preset, SpinValue *sv);
void spinner_modify_value(const char *title, bool preset, SpinValue &sv);

std::string sv2str(const SpinValue *sv);
unsigned long sv2ms(const SpinValue *sv);
std::string sv2str(const SpinValue &sv);
unsigned long sv2ms(const SpinValue &sv);
void ms2hms(unsigned long ms, unsigned int *h, unsigned int *m, unsigned int *s);

#endif
10 changes: 5 additions & 5 deletions lib/furble/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ bool Camera::connect(esp_power_level_t power, progressFunc pFunc, void *pCtx) {
return connected;
}

bool Camera::isActive(void) {
bool Camera::isActive(void) const {
return m_Active;
}

void Camera::setActive(bool active) {
m_Active = active;
}

const Camera::Type &Camera::getType(void) {
const Camera::Type &Camera::getType(void) const {
return m_Type;
}

const std::string &Camera::getName(void) {
const std::string &Camera::getName(void) const {
return m_Name;
}

const NimBLEAddress &Camera::getAddress(void) {
const NimBLEAddress &Camera::getAddress(void) const {
return m_Address;
}

Expand All @@ -50,7 +50,7 @@ void Camera::updateProgress(progressFunc pFunc, void *ctx, float value) {
}
}

bool Camera::isConnected(void) {
bool Camera::isConnected(void) const {
return m_Client->isConnected();
}

Expand Down
14 changes: 7 additions & 7 deletions lib/furble/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,29 @@ class Camera {
*/
virtual void updateGeoData(const gps_t &gps, const timesync_t &timesync) = 0;

virtual size_t getSerialisedBytes(void) = 0;
virtual bool serialise(void *buffer, size_t bytes) = 0;
virtual size_t getSerialisedBytes(void) const = 0;
virtual bool serialise(void *buffer, size_t bytes) const = 0;

/**
* Checks if the client is still connected.
*/
virtual bool isConnected(void);
virtual bool isConnected(void) const;

/**
* Camera is active (ie. connect() has succeeded previously).
*/
bool isActive(void);
bool isActive(void) const;

/**
* Set camera activity state.
*/
void setActive(bool active);

const Type &getType(void);
const Type &getType(void) const;

const std::string &getName(void);
const std::string &getName(void) const;

const NimBLEAddress &getAddress(void);
const NimBLEAddress &getAddress(void) const;

protected:
Camera(Type type);
Expand Down
25 changes: 12 additions & 13 deletions lib/furble/CameraList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct {
Camera::Type type;
} index_entry_t;

void CameraList::fillSaveEntry(CameraList::index_entry_t &entry, Camera *camera) {
void CameraList::fillSaveEntry(index_entry_t &entry, const Camera *camera) {
snprintf(entry.name, 16, "%08llX", (uint64_t)camera->getAddress());
entry.type = camera->getType();
}
Expand Down Expand Up @@ -61,11 +61,11 @@ std::vector<CameraList::index_entry_t> CameraList::load_index(void) {

void CameraList::add_index(std::vector<CameraList::index_entry_t> &index, index_entry_t &entry) {
bool exists = false;
for (size_t i = 0; i < index.size(); i++) {
ESP_LOGI(LOG_TAG, "[%d] %s : %s", i, index[i].name, entry.name);
if (strcmp(index[i].name, entry.name) == 0) {
for (auto &i : index) {
ESP_LOGI(LOG_TAG, "%s : %s", i.name, entry.name);
if (strcmp(i.name, entry.name) == 0) {
ESP_LOGI(LOG_TAG, "Overwriting existing entry: %s", entry.name);
index[i] = entry;
i = entry;
exists = true;
break;
}
Expand All @@ -77,7 +77,7 @@ void CameraList::add_index(std::vector<CameraList::index_entry_t> &index, index_
}
}

void CameraList::save(Furble::Camera *camera) {
void CameraList::save(const Furble::Camera *camera) {
m_Prefs.begin(FURBLE_STR, false);
std::vector<index_entry_t> index = load_index();

Expand Down Expand Up @@ -110,12 +110,11 @@ void CameraList::remove(Furble::Camera *camera) {
for (i = 0; i < index.size(); i++) {
if (strcmp(index[i].name, entry.name) == 0) {
ESP_LOGI(LOG_TAG, "Deleting: %s", entry.name);
index.erase(index.begin() + i);
break;
}
}

index.erase(index.begin() + i);

m_Prefs.remove(entry.name);
save_index(index);

Expand All @@ -138,15 +137,15 @@ void CameraList::load(void) {
m_Prefs.begin(FURBLE_STR, true);
m_ConnectList.clear();
std::vector<index_entry_t> index = load_index();
for (size_t i = 0; i < index.size(); i++) {
size_t dbytes = m_Prefs.getBytesLength(index[i].name);
for (const auto &i : index) {
size_t dbytes = m_Prefs.getBytesLength(i.name);
if (dbytes == 0) {
continue;
}
uint8_t dbuffer[dbytes] = {0};
m_Prefs.getBytes(index[i].name, dbuffer, dbytes);
m_Prefs.getBytes(i.name, dbuffer, dbytes);

switch (index[i].type) {
switch (i.type) {
case Camera::Type::FUJIFILM:
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Fujifilm(dbuffer, dbytes)));
break;
Expand Down Expand Up @@ -184,7 +183,7 @@ Furble::Camera *CameraList::get(size_t n) {
return m_ConnectList[n].get();
}

bool CameraList::match(NimBLEAdvertisedDevice *pDevice) {
bool CameraList::match(const NimBLEAdvertisedDevice *pDevice) {
if (Fujifilm::matches(pDevice)) {
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Furble::Fujifilm(pDevice)));
return true;
Expand Down
6 changes: 3 additions & 3 deletions lib/furble/CameraList.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CameraList {
/**
* Save camera to connection list.
*/
static void save(Furble::Camera *camera);
static void save(const Furble::Camera *camera);

/**
* Remove camera from connection list.
Expand All @@ -37,7 +37,7 @@ class CameraList {
*
* @return true if device matches
*/
static bool match(NimBLEAdvertisedDevice *pDevice);
static bool match(const NimBLEAdvertisedDevice *pDevice);

/**
* Add mobile device to the list.
Expand Down Expand Up @@ -65,7 +65,7 @@ class CameraList {
Camera::Type type;
} index_entry_t;

static void fillSaveEntry(index_entry_t &entry, Camera *camera);
static void fillSaveEntry(index_entry_t &entry, const Camera *camera);
static std::vector<index_entry_t> load_index(void);
static void save_index(std::vector<index_entry_t> &index);
static void add_index(std::vector<index_entry_t> &index, index_entry_t &entry);
Expand Down
10 changes: 4 additions & 6 deletions lib/furble/CanonEOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace Furble {

CanonEOS::CanonEOS(Type type, const void *data, size_t len) : Camera(type) {
if (len != sizeof(eos_t))
throw;
abort();

const eos_t *eos = static_cast<const eos_t *>(data);
m_Name = std::string(eos->name);
m_Address = NimBLEAddress(eos->address, eos->type);
memcpy(&m_Uuid, &eos->uuid, sizeof(Device::uuid128_t));
}

CanonEOS::CanonEOS(Type type, NimBLEAdvertisedDevice *pDevice) : Camera(type) {
CanonEOS::CanonEOS(Type type, const NimBLEAdvertisedDevice *pDevice) : Camera(type) {
m_Name = pDevice->getName();
m_Address = pDevice->getAddress();
ESP_LOGI(LOG_TAG, "Name = %s", m_Name.c_str());
Expand Down Expand Up @@ -76,8 +76,6 @@ bool CanonEOS::write_prefix(NimBLEClient *pClient,
* handled by the underlying NimBLE and ESP32 libraries.
*/
bool CanonEOS::connect(progressFunc pFunc, void *pCtx) {
using namespace std::placeholders;

if (NimBLEDevice::isBonded(m_Address)) {
// Already bonded? Assume pair acceptance!
m_PairResult = CANON_EOS_PAIR_ACCEPT;
Expand Down Expand Up @@ -215,11 +213,11 @@ void CanonEOS::disconnect(void) {
m_Client->disconnect();
}

size_t CanonEOS::getSerialisedBytes(void) {
size_t CanonEOS::getSerialisedBytes(void) const {
return sizeof(eos_t);
}

bool CanonEOS::serialise(void *buffer, size_t bytes) {
bool CanonEOS::serialise(void *buffer, size_t bytes) const {
if (bytes != sizeof(eos_t)) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/furble/CanonEOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CanonEOS: public Camera {
} eos_t;

CanonEOS(Type type, const void *data, size_t len);
CanonEOS(Type type, NimBLEAdvertisedDevice *pDevice);
CanonEOS(Type type, const NimBLEAdvertisedDevice *pDevice);
~CanonEOS(void);

const char *CANON_EOS_SVC_IDEN_UUID = "00010000-0000-1000-0000-d8492fffa821";
Expand Down Expand Up @@ -63,8 +63,8 @@ class CanonEOS: public Camera {
void focusRelease(void) override;
void updateGeoData(const gps_t &gps, const timesync_t &timesync) override;
void disconnect(void) override;
size_t getSerialisedBytes(void) override;
bool serialise(void *buffer, size_t bytes) override;
size_t getSerialisedBytes(void) const override;
bool serialise(void *buffer, size_t bytes) const override;

Device::uuid128_t m_Uuid;

Expand Down
2 changes: 1 addition & 1 deletion lib/furble/CanonEOSM6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const uint8_t CANON_EOS_M6_XX_2 = 0x01;
const uint8_t CANON_EOS_M6_XX_3 = 0xc5;
const uint8_t CANON_EOS_M6_XX_4 = 0x32;

bool CanonEOSM6::matches(NimBLEAdvertisedDevice *pDevice) {
bool CanonEOSM6::matches(const NimBLEAdvertisedDevice *pDevice) {
if (pDevice->haveManufacturerData()
&& pDevice->getManufacturerData().length() == CANON_EOS_M6_ADV_DATA_LEN) {
const char *data = pDevice->getManufacturerData().data();
Expand Down
4 changes: 2 additions & 2 deletions lib/furble/CanonEOSM6.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace Furble {
class CanonEOSM6: public CanonEOS {
public:
CanonEOSM6(const void *data, size_t len) : CanonEOS(Type::CANON_EOS_M6, data, len){};
CanonEOSM6(NimBLEAdvertisedDevice *pDevice) : CanonEOS(Type::CANON_EOS_M6, pDevice){};
CanonEOSM6(const NimBLEAdvertisedDevice *pDevice) : CanonEOS(Type::CANON_EOS_M6, pDevice){};

/**
* Determine if the advertised BLE device is a Canon EOS M6.
*/
static bool matches(NimBLEAdvertisedDevice *pDevice);
static bool matches(const NimBLEAdvertisedDevice *pDevice);

private:
};
Expand Down
2 changes: 1 addition & 1 deletion lib/furble/CanonEOSRP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const uint8_t CANON_EOS_RP_XX_5 = 0x00;
const uint8_t CANON_EOS_RP_XX_6 = 0x00;
const uint8_t CANON_EOS_RP_XX_7 = 0x02;

bool CanonEOSRP::matches(NimBLEAdvertisedDevice *pDevice) {
bool CanonEOSRP::matches(const NimBLEAdvertisedDevice *pDevice) {
if (pDevice->haveManufacturerData()
&& pDevice->getManufacturerData().length() == CANON_EOS_RP_ADV_DATA_LEN) {
const char *data = pDevice->getManufacturerData().data();
Expand Down
4 changes: 2 additions & 2 deletions lib/furble/CanonEOSRP.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace Furble {
class CanonEOSRP: public CanonEOS {
public:
CanonEOSRP(const void *data, size_t len) : CanonEOS(Type::CANON_EOS_RP, data, len){};
CanonEOSRP(NimBLEAdvertisedDevice *pDevice) : CanonEOS(Type::CANON_EOS_RP, pDevice){};
CanonEOSRP(const NimBLEAdvertisedDevice *pDevice) : CanonEOS(Type::CANON_EOS_RP, pDevice){};

/**
* Determine if the advertised BLE device is a Canon EOS RP.
*/
static bool matches(NimBLEAdvertisedDevice *pDevice);
static bool matches(const NimBLEAdvertisedDevice *pDevice);

private:
};
Expand Down
10 changes: 5 additions & 5 deletions lib/furble/Fujifilm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ void Fujifilm::notify(BLERemoteCharacteristic *pChr, uint8_t *pData, size_t leng

Fujifilm::Fujifilm(const void *data, size_t len) : Camera(Type::FUJIFILM) {
if (len != sizeof(fujifilm_t))
throw;
abort();

const fujifilm_t *fujifilm = static_cast<const fujifilm_t *>(data);
m_Name = std::string(fujifilm->name);
m_Address = NimBLEAddress(fujifilm->address, fujifilm->type);
memcpy(m_Token.data(), fujifilm->token, FUJIFILM_TOKEN_LEN);
}

Fujifilm::Fujifilm(NimBLEAdvertisedDevice *pDevice) : Camera(Type::FUJIFILM) {
Fujifilm::Fujifilm(const NimBLEAdvertisedDevice *pDevice) : Camera(Type::FUJIFILM) {
const char *data = pDevice->getManufacturerData().data();
m_Name = pDevice->getName();
m_Address = pDevice->getAddress();
Expand All @@ -115,7 +115,7 @@ constexpr uint8_t FUJIFILM_TYPE_TOKEN = 0x02;
/**
* Determine if the advertised BLE device is a Fujifilm.
*/
bool Fujifilm::matches(NimBLEAdvertisedDevice *pDevice) {
bool Fujifilm::matches(const NimBLEAdvertisedDevice *pDevice) {
if (pDevice->haveManufacturerData()
&& pDevice->getManufacturerData().length() == FUJIFILM_ADV_TOKEN_LEN) {
const char *data = pDevice->getManufacturerData().data();
Expand Down Expand Up @@ -315,11 +315,11 @@ void Fujifilm::disconnect(void) {
m_Client->disconnect();
}

size_t Fujifilm::getSerialisedBytes(void) {
size_t Fujifilm::getSerialisedBytes(void) const {
return sizeof(fujifilm_t);
}

bool Fujifilm::serialise(void *buffer, size_t bytes) {
bool Fujifilm::serialise(void *buffer, size_t bytes) const {
if (bytes != sizeof(fujifilm_t)) {
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/furble/Fujifilm.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace Furble {
class Fujifilm: public Camera {
public:
Fujifilm(const void *data, size_t len);
Fujifilm(NimBLEAdvertisedDevice *pDevice);
Fujifilm(const NimBLEAdvertisedDevice *pDevice);
~Fujifilm(void);

/**
* Determine if the advertised BLE device is a Fujifilm X-T30.
*/
static bool matches(NimBLEAdvertisedDevice *pDevice);
static bool matches(const NimBLEAdvertisedDevice *pDevice);

bool connect(progressFunc pFunc = nullptr, void *pCtx = nullptr) override;
void shutterPress(void) override;
Expand All @@ -29,8 +29,8 @@ class Fujifilm: public Camera {
void focusRelease(void) override;
void updateGeoData(const gps_t &gps, const timesync_t &timesync) override;
void disconnect(void) override;
size_t getSerialisedBytes(void) override;
bool serialise(void *buffer, size_t bytes) override;
size_t getSerialisedBytes(void) const override;
bool serialise(void *buffer, size_t bytes) const override;

private:
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/furble/HIDServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void HIDServer::disconnect(NimBLEAddress &address) {
m_Server->disconnect(info.getConnHandle());
}

bool HIDServer::isConnected(NimBLEAddress &address) {
bool HIDServer::isConnected(const NimBLEAddress &address) {
NimBLEConnInfo info = m_Server->getPeerInfo(address);

return (!info.getIdAddress().isNull());
Expand Down
Loading

0 comments on commit 321d5a1

Please sign in to comment.