/*
 * btmsg.h
 *
 *  Created on: 20 Apr 2015
 *      Author: john
 */

#ifndef BTMSG_H_
#define BTMSG_H_

#include <klog/klog.h>
#include <kutils/properties.h>
#include <stdint.h>
#include <kmsg/kmsg.h>

#include "btmodem.h"


class cBTMsg : public cKlog
{
public:
	static void start(cProperties * props);
	static void writeModem(uint8_t * buff, int len);
	static void enablePairing();
	static void paired();
	static void connected();
	static void disConnected();
	static void handleData(uint8_t * msg_buff,int msg_len);
	static void sendMsg(KMsg * msg);

private:
	enum eLinkStatus
	{
		BT_LINK_STANDBY,
		BT_LINK_DISCOVERY,
		BT_LINK_PAIRED,
		BT_LINK_CONNECTED
	};
	cBTMsg(cProperties *props);
	static void *state_thread(void *);
	void run();

	void request_state(eLinkStatus);

	void execute_standby();
	void execute_discovery();
	void execute_paired();
	void execute_connected();

	void check_standby();
	void check_discovery();
	void check_paired();
	void check_connected();

	void receive_data(uint8_t * msg_buff,int msg_len);
	void transmit_msg(KMsg * msg);

	static cBTMsg * mInstance;
	cBTModem * mBTModem;
	cProperties * mProps;
	eLinkStatus mLinkStatus;
	eLinkStatus mNextStatus;
	uint32_t mDiscoveryStartTime;
	KMsg * mRxMsg;
	uint32_t mSegmentSize;

	pthread_mutex_t mStateMutex;
	pthread_cond_t mStateCond;

};



#endif /* BTMSG_H_ */
