Skip to content

Commit

Permalink
Namespace refactor and NimBLEUUID cleanup.
Browse files Browse the repository at this point in the history
Move most of the UUIDs into the per-camera namespace and reduce the
identifier names.

Change to the hex-parts NimBLEUUID constructor, no need to parse strings
here.
  • Loading branch information
gkoh committed Jan 7, 2025
1 parent 49b91d3 commit 0e90274
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 113 deletions.
39 changes: 20 additions & 19 deletions lib/furble/CanonEOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Furble {

constexpr uint8_t CanonEOS::MODE_SHOOT;

CanonEOS::CanonEOS(Type type, const void *data, size_t len) : Camera(type) {
if (len != sizeof(eos_t))
abort();
Expand Down Expand Up @@ -39,8 +41,8 @@ void CanonEOS::pairCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic,
}

bool CanonEOS::write_value(NimBLEClient *pClient,
const char *serviceUUID,
const char *characteristicUUID,
const NimBLEUUID &serviceUUID,
const NimBLEUUID &characteristicUUID,
const uint8_t *data,
size_t length) {
NimBLERemoteService *pSvc = pClient->getService(serviceUUID);
Expand All @@ -53,8 +55,8 @@ bool CanonEOS::write_value(NimBLEClient *pClient,
}

bool CanonEOS::write_prefix(NimBLEClient *pClient,
const char *serviceUUID,
const char *characteristicUUID,
const NimBLEUUID &serviceUUID,
const NimBLEUUID &characteristicUUID,
const uint8_t prefix,
const uint8_t *data,
size_t length) {
Expand All @@ -73,7 +75,7 @@ bool CanonEOS::write_prefix(NimBLEClient *pClient,
bool CanonEOS::_connect(void) {
if (NimBLEDevice::isBonded(m_Address)) {
// Already bonded? Assume pair acceptance!
m_PairResult = CANON_EOS_PAIR_ACCEPT;
m_PairResult = PAIR_ACCEPT;
} else {
m_PairResult = 0x00;
}
Expand All @@ -94,9 +96,9 @@ bool CanonEOS::_connect(void) {
ESP_LOGI(LOG_TAG, "Secured!");
m_Progress = 20;

NimBLERemoteService *pSvc = m_Client->getService(CANON_EOS_SVC_IDEN_UUID);
NimBLERemoteService *pSvc = m_Client->getService(SVC_IDEN_UUID);
if (pSvc) {
NimBLERemoteCharacteristic *pChr = pSvc->getCharacteristic(CANON_EOS_CHR_NAME_UUID);
NimBLERemoteCharacteristic *pChr = pSvc->getCharacteristic(CHR_NAME_UUID);
if ((pChr != nullptr) && pChr->canIndicate()) {
ESP_LOGI(LOG_TAG, "Subscribed for pairing indication");
pChr->subscribe(false,
Expand All @@ -107,30 +109,30 @@ bool CanonEOS::_connect(void) {

ESP_LOGI(LOG_TAG, "Identifying 1!");
const auto name = Device::getStringID();
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_NAME_UUID, 0x01,
(uint8_t *)name.c_str(), name.length()))
if (!write_prefix(m_Client, SVC_IDEN_UUID, CHR_NAME_UUID, 0x01, (uint8_t *)name.c_str(),
name.length()))
return false;

m_Progress = 30;

ESP_LOGI(LOG_TAG, "Identifying 2!");
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, 0x03, m_Uuid.uint8,
if (!write_prefix(m_Client, SVC_IDEN_UUID, CHR_IDEN_UUID, 0x03, m_Uuid.uint8,
Device::UUID128_LEN))
return false;

m_Progress = 40;

ESP_LOGI(LOG_TAG, "Identifying 3!");
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, 0x04,
(uint8_t *)name.c_str(), name.length()))
if (!write_prefix(m_Client, SVC_IDEN_UUID, CHR_IDEN_UUID, 0x04, (uint8_t *)name.c_str(),
name.length()))
return false;

m_Progress = 50;

ESP_LOGI(LOG_TAG, "Identifying 4!");

uint8_t x = 0x02;
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, 0x05, &x, 1))
if (!write_prefix(m_Client, SVC_IDEN_UUID, CHR_IDEN_UUID, 0x05, &x, 1))
return false;

m_Progress = 60;
Expand All @@ -147,7 +149,7 @@ bool CanonEOS::_connect(void) {
vTaskDelay(pdMS_TO_TICKS(1000));
}

if (m_PairResult != CANON_EOS_PAIR_ACCEPT) {
if (m_PairResult != PAIR_ACCEPT) {
bool deleted = NimBLEDevice::deleteBond(m_Address);
ESP_LOGW(LOG_TAG, "Rejected, delete pairing: %d", deleted);
return false;
Expand All @@ -157,16 +159,15 @@ bool CanonEOS::_connect(void) {

/* write to 0xf104 */
x = 0x01;
if (!write_value(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, &x, 1))
if (!write_value(m_Client, SVC_IDEN_UUID, CHR_IDEN_UUID, &x, 1))
return false;

m_Progress = 90;

ESP_LOGI(LOG_TAG, "Switching mode!");

/* write to 0xf307 */
if (!write_value(m_Client, CANON_EOS_SVC_MODE_UUID, CANON_EOS_CHR_MODE_UUID,
&CANON_EOS_MODE_SHOOT, sizeof(CANON_EOS_MODE_SHOOT)))
if (!write_value(m_Client, SVC_MODE_UUID, CHR_MODE_UUID, &MODE_SHOOT, sizeof(MODE_SHOOT)))
return false;

ESP_LOGI(LOG_TAG, "Done!");
Expand All @@ -177,12 +178,12 @@ bool CanonEOS::_connect(void) {

void CanonEOS::shutterPress(void) {
uint8_t x[2] = {0x00, 0x01};
write_value(m_Client, CANON_EOS_SVC_SHUTTER_UUID, CANON_EOS_CHR_SHUTTER_UUID, &x[0], 2);
write_value(m_Client, SVC_SHUTTER_UUID, CHR_SHUTTER_UUID, &x[0], 2);
}

void CanonEOS::shutterRelease(void) {
uint8_t x[2] = {0x00, 0x02};
write_value(m_Client, CANON_EOS_SVC_SHUTTER_UUID, CANON_EOS_CHR_SHUTTER_UUID, &x[0], 2);
write_value(m_Client, SVC_SHUTTER_UUID, CHR_SHUTTER_UUID, &x[0], 2);
}

void CanonEOS::focusPress(void) {
Expand Down
34 changes: 18 additions & 16 deletions lib/furble/CanonEOS.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CANONEOS_H
#define CANONEOS_H

#include <NimBLEUUID.h>

#include "Camera.h"
#include "Device.h"

Expand All @@ -21,35 +23,35 @@ class CanonEOS: public Camera {
CanonEOS(Type type, const void *data, size_t len);
CanonEOS(Type type, const NimBLEAdvertisedDevice *pDevice);

const char *CANON_EOS_SVC_IDEN_UUID = "00010000-0000-1000-0000-d8492fffa821";
const NimBLEUUID SVC_IDEN_UUID {0x00010000, 0x0000, 0x1000, 0x0000d8492fffa821};
/** 0xf108 */
const char *CANON_EOS_CHR_NAME_UUID = "00010006-0000-1000-0000-d8492fffa821";
const NimBLEUUID CHR_NAME_UUID {0x00010006, 0x0000, 0x1000, 0x0000d8492fffa821};
/** 0xf104 */
const char *CANON_EOS_CHR_IDEN_UUID = "0001000a-0000-1000-0000-d8492fffa821";
const NimBLEUUID CHR_IDEN_UUID {0x0001000a, 0x0000, 0x1000, 0x0000d8492fffa821};

const char *CANON_EOS_SVC_MODE_UUID = "00030000-0000-1000-0000-d8492fffa821";
const NimBLEUUID SVC_MODE_UUID {0x00030000, 0x0000, 0x1000, 0x0000d8492fffa821};
/** 0xf307 */
const char *CANON_EOS_CHR_MODE_UUID = "00030010-0000-1000-0000-d8492fffa821";
const NimBLEUUID CHR_MODE_UUID {0x00030010, 0x0000, 0x1000, 0x0000d8492fffa821};

const char *CANON_EOS_SVC_SHUTTER_UUID = "00030000-0000-1000-0000-d8492fffa821";
const NimBLEUUID SVC_SHUTTER_UUID {0x00030000, 0x0000, 0x1000, 0x0000d8492fffa821};
/** 0xf311 */
const char *CANON_EOS_CHR_SHUTTER_UUID = "00030030-0000-1000-0000-d8492fffa821";
const NimBLEUUID CHR_SHUTTER_UUID {0x00030030, 0x0000, 0x1000, 0x0000d8492fffa821};

const uint8_t CANON_EOS_PAIR_ACCEPT = 0x02;
const uint8_t CANON_EOS_PAIR_REJECT = 0x03;
uint8_t CANON_EOS_MODE_PLAYBACK = 0x01;
uint8_t CANON_EOS_MODE_SHOOT = 0x02;
uint8_t CANON_EOS_MODE_WAKE = 0x03;
static constexpr uint8_t PAIR_ACCEPT = 0x02;
static constexpr uint8_t PAIR_REJECT = 0x03;
static constexpr uint8_t MODE_PLAYBACK = 0x01;
static constexpr uint8_t MODE_SHOOT = 0x02;
static constexpr uint8_t MODE_WAKE = 0x03;

bool write_value(NimBLEClient *pClient,
const char *serviceUUID,
const char *characteristicUUID,
const NimBLEUUID &serviceUUID,
const NimBLEUUID &characteristicUUID,
const uint8_t *data,
size_t length);

bool write_prefix(NimBLEClient *pClient,
const char *serviceUUID,
const char *characteristicUUID,
const NimBLEUUID &serviceUUID,
const NimBLEUUID &characteristicUUID,
const uint8_t prefix,
const uint8_t *data,
size_t length);
Expand Down
Loading

0 comments on commit 0e90274

Please sign in to comment.