registerCallbacks static method

void registerCallbacks()

Register device callbacks synchronously (Phase 1). Matches Swift: Device.register() in CppBridge.initialize() This registers the C++ callbacks without initializing SharedPreferences or caching device ID (those happen in Phase 2).

Implementation

static void registerCallbacks() {
  if (_callbacksRegistered) {
    _logger.debug('Device callbacks already registered');
    return;
  }

  try {
    final lib = PlatformLoader.loadCommons();

    // Allocate callbacks struct
    _callbacksPtr = calloc<RacDeviceCallbacksStruct>();
    final callbacks = _callbacksPtr!;

    // Set callback function pointers
    callbacks.ref.getDeviceInfo =
        Pointer.fromFunction<RacDeviceGetInfoCallbackNative>(
          _getDeviceInfoCallback,
        );
    callbacks.ref.getDeviceId =
        Pointer.fromFunction<RacDeviceGetIdCallbackNative>(
          _getDeviceIdCallback,
        );
    callbacks.ref.isRegistered =
        Pointer.fromFunction<RacDeviceIsRegisteredCallbackNative>(
          _isRegisteredCallback,
          _exceptionalReturnInt32,
        );
    callbacks.ref.setRegistered =
        Pointer.fromFunction<RacDeviceSetRegisteredCallbackNative>(
          _setRegisteredCallback,
        );
    callbacks.ref.httpPost =
        Pointer.fromFunction<RacDeviceHttpPostCallbackNative>(
          _httpPostCallback,
          _exceptionalReturnInt32,
        );
    callbacks.ref.userData = nullptr;

    // Register with C++
    final setCallbacks = lib
        .lookupFunction<
          Int32 Function(Pointer<RacDeviceCallbacksStruct>),
          int Function(Pointer<RacDeviceCallbacksStruct>)
        >('rac_device_manager_set_callbacks');

    final result = setCallbacks(callbacks);
    if (result != RacResultCode.success) {
      _logger.warning(
        'Failed to set device callbacks',
        metadata: {'error_code': result},
      );
      calloc.free(callbacks);
      _callbacksPtr = null;
      return;
    }

    _callbacksRegistered = true;
    _logger.debug('Device callbacks registered (sync)');
  } catch (_) {
    // librac_commons.so may not export rac_device_manager_set_callbacks in some
    // configurations (B-FL-1-002). Log at warning so it's visible in
    // non-debug builds; SDK falls back to no device callbacks.
    _logger.warning('Device callbacks are unavailable');
  }
}