/***************************************************************************

  BionxControl
  Copyright © 2025 christoph holzheuer
  christoph.holzheuer@gmail.com

  Using:

  mhs_can_drv.c
  © 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
  Klaus Demlehner, klaus@mhs-elektronik.de
  @see www.mhs-elektronik.de

  Based on Bionx data type descriptions from:

  BigXionFlasher USB V 0.2.4 rev. 97
  © 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
  @see www.bigxionflasher.org

  Bionx Bike Info
  © 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
  @see www.ts-soft.de

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.
  @see https://github.com/bikemike/bionx-bikeinfo

 ***************************************************************************/


#ifndef BCDRIVER_H
#define BCDRIVER_H

#include <QObject>

#include <bc.h>

/*
int32_t   CanInitDriver(char *options);
void      CanDownDriver(void);
int32_t   CanSetOptions(char *options);
int32_t   CanDeviceOpen(uint32_t index, char *parameter);
int32_t   CanDeviceClose(uint32_t index);

int32_t   CanSetMode(uint32_t index, unsigned char can_op_mode, uint16_t can_command);

int32_t   CanTransmit(uint32_t index, struct TCanMsg *msg, int32_t count);
void      CanTransmitClear(uint32_t index);
uint32_t  CanTransmitGetCount(uint32_t index);
int32_t   CanTransmitSet(uint32_t index, uint16_t  cmd, uint32_t  time);
int32_t   CanReceive(uint32_t index, struct TCanMsg *msg, int32_t count);
void      CanReceiveClear(uint32_t index);
uint32_t  CanReceiveGetCount(uint32_t index);

int32_t   CanSetSpeed(uint32_t index, uint16_t speed);
int32_t   CanSetSpeedUser(uint32_t index, uint32_t value);
char*     CanDrvInfo(void);
char*     CanDrvHwInfo(uint32_t index);
int32_t   CanSetFilter(uint32_t index, struct TMsgFilter *msg_filter);
int32_t   CanGetDeviceStatus(uint32_t index, struct TDeviceStatus *status);
void      CanSetPnPEventCallback(void (DRV_CALLBACK_TYPE *event)(uint32_t index, int32_t status));
void      CanSetStatusEventCallback(void (DRV_CALLBACK_TYPE *event)  (uint32_t  index, struct TDeviceStatus *device_status) );
void      CanSetRxEventCallback(void (DRV_CALLBACK_TYPE *event)(uint32_t index,  struct TCanMsg *msg, int32_t count) );

void      CanSetEvents( uint16_t  events );
uint32_t  CanEventStatus(void);
*/

struct CBCItem;
class  BCDriverStatus;

/**
 * @Abstrakte Basisklasse für alle CAN-Bus Treiber.
 * Das Bionx CAN-Bus System kann auf verschiedenen Wegen
 * angesprochen werden, etwa den über BBI USB2CAN Controller,
 * über den TinyCAN Adapter oder ggf. über einen EL327 Stecker.
 *
 * Die hier relevante Implementierung über das TinyCan System
 * findet sich in der Unterklasse 'BCDriverTinyCan'.
 *
 * @see BCDriverTinyCan
 */


class BCDriver : public QObject
{
  Q_OBJECT

public:

  enum class DriverState
  {
    NotPresent,
    Error,
    Loaded,
    Initialized,
    Connected,
    Ready
  };
  Q_ENUM(DriverState)

  explicit BCDriver( QObject* parent = nullptr );
  virtual ~BCDriver() = default;

  DriverState getState() const;

  virtual DriverState loadDriver() = 0;
  virtual DriverState initDriver() = 0;

  virtual uint32_t readRawByte( uint32_t deviceID, uint8_t registerID ) const = 0;
  virtual void     writeRawByte( uint32_t deviceID, uint8_t registerID, uint8_t value ) const = 0;

signals:

  // __fix wird das gebraucht?
  void errorOccured( const QString& errMsg );
  void statusHint( const QString& msg ) const;

  void stateChanged( DriverState state );

public slots:

  void onStartDriver();

protected:

  void setState( DriverState newState );

  DriverState _driverState{DriverState::NotPresent};

  static constexpr int cRetries  =  5;
  static constexpr int cTimeOuts = 20;

};

#endif // BCDRIVER_H
