/* 
 * File:   cKlinkBus.h
 * Author: john
 *
 * Created on 04 April 2011, 10:29 AM
 */

#ifndef CKLINKBUS_H
#define	CKLINKBUS_H

#include <klog.h>

#include <cKlinkBusEvent.h>
#include <cKlinkBusInterface.h>
#include <cKlinkTimer.h>
#include <cKlinkFrame.h>
#include <cKlinkDiscoveryFrame.h>
#include <cKlinkEnumeratedList.h>
#include <cKlinkDeviceFactory.h>
#include <cKlinkPduFrame.h>
#include <cKlinkDebug.h>
#include <cKlinkDeviceListener.h>
#include <cKlinkDevice.h>


class cKlinkBus : public cKlog
{
public:
    cKlinkBus(cKlinkBusInterface &,bool,cKlinkDeviceFactory * f,cKlinkDeviceListener *dl,int ndisc_cycles);
    void setDeviceListener(cKlinkDeviceListener *);
    void run(bool block);
    virtual ~cKlinkBus();
    int getFileDescriptor();
    int nextTimer();
    void registerDevice(cKlinkDevice *);
    void removeDevice(cKlinkDevice *);
    
private:
    
    cKlinkBusInterface & mBusInterface;
    bool mMaster;
    cKlinkTimer mPollTimer;
    uint32_t mNumDiscCycles;
    uint32_t mCycleCtr;

    
    void sendNextFrame();
    void processFrame(cKlinkDataBuffer &);
    bool isFrameForThisBus(cKlinkFrame * f);
    void processDiscoveryFrame(cKlinkDiscoveryFrame *f);
    void processPduFrame(cKlinkPduFrame *f);
    cKlinkEnumeratedList<cKlinkDevice> mDeviceList;
    cKlinkEnumeratedList<cKlinkDevice> mPollList;
    cKlinkEnumeratedList<cKlinkFrame> mTransmitList;
    
    void sendFrame(cKlinkFrame *f);
    uint8_t getNextAddress();
    uint8_t mSlaveAddressCtr;
    cKlinkDeviceFactory * mDeviceFactory;
    cKlinkDeviceListener * mDeviceListener;
    

};

#endif	/* CKLINKBUS_H */

