Skip to content

FS doesnt seem to work from BLE service - FileOpen, DirOpen etc #2262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
satellitecam opened this issue Feb 23, 2025 · 1 comment
Open

Comments

@satellitecam
Copy link

Im trying to implement a data file in my BLE service loaded by NimbleController, using FS, but have encountered a brick wall.

When using "F:/events.dat", I am constantly getting errors back - either -2 (no entry), or -84 (corrupt).
When using "/events.dat", the device just crashes.

The same things are happening for DirOpen, using "F:/" and "/" paths respectively.

Im trying to follow whats being done in built in services and controllers, like Settings, which seem to work without issue. Also, my code works fine on Infinisim.

This issue does not occur if I transplant my code into an app like a watch face.

Am I doing something wrong here?

debug_log is a function ive defined for logging to the screen, defined much like printf.

void TestService::SaveEventsLog() {
  debug_log("app", "SaveEventsLog");

  lfs_file_t saveFile;

  int file_operation_result = fs.FileOpen(&saveFile, "F:/events.dat", LFS_O_WRONLY | LFS_O_CREAT);
  if (file_operation_result < LFS_ERR_OK) {
    debug_log("app", "saveFile open failed: %d", file_operation_result);

    if (file_operation_result == LFS_ERR_CORRUPT) {
      fs.FileDelete("F:/events.dat");
      debug_log("app", "saveFile corrupt - deleting");
    }
    return;
  }

  debug_log("app", "saveFile opened");

  struct SaveData {
    uint8_t version = 1;
    std::vector<EventLogEntry> data;
  } saveData;

  saveData.data = eventLog;

  file_operation_result = fs.FileWrite(&saveFile, reinterpret_cast<uint8_t*>(&saveData), sizeof(saveData));
  if (file_operation_result < LFS_ERR_OK) {
    debug_log("app", "saveFile write failed: %d", file_operation_result);
  } else {
    debug_log("app", "saveFile saved");
  }

  file_operation_result = fs.FileClose(&saveFile);
  if (file_operation_result < LFS_ERR_OK) {
    debug_log("app", "saveFile close failed: %d", file_operation_result);
  } else {
    debug_log("app", "saveFile closed");
  }
}
@satellitecam
Copy link
Author

I have worked around this by making FS accessible from NimbleController with a public getter, rather than use the object passed through my service constructor. This works perfectly, but its odd that the passed parameter does not work. Im probably missing something obvious?

NimbleController.cpp:

// ...
    testService {*this, motionSensor, batteryController, dateTimeController, fs},
// ...

TestService.cpp:

TestService::TestService(
  NimbleController& nimble,
  Pinetime::Drivers::Bma421& motionSensor,
  Battery& batteryController,
  DateTime& dateTimeController,
  FS& fs
)
  : nimble {nimble},
    motionSensor(motionSensor),
    batteryController {batteryController},
    dateTimeController {dateTimeController},
    fs {fs},
    characteristicDefinition {
// ...
    },
    serviceDefinition {
// ...
    } {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant