// GattServerDlg.h : header file
//

#pragma once
#include "afxwin.h"

#include "..\..\..\..\Source\Bluetooth\wclBluetooth.h"

using namespace wclCommon;
using namespace wclBluetooth;

#define SHORT_DATA_LEN	5
#define LONG_DATA_LEN	1024

// CGattServerDlg dialog
class CGattServerDlg : public CDialog
{
// Construction
public:
	CGattServerDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_GATTSERVER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

private:
	CListBox lbLog;
	
	CwclBluetoothManager FManager;
	CwclGattServer FServer;
	bool FStarted;
	DWORD FCounter;

	BYTE FLongData[LONG_DATA_LEN];
	BYTE FShortData[SHORT_DATA_LEN];

	void FManager_AfterOpen(void* Sender);
	void FManager_BeforeClose(void* sender);

	void FServer_OnStarted(void* Sender);
	void FServer_OnStopped(void* Sender);
	void FServer_OnWrite(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic,
		CwclGattLocalCharacteristicWriteRequest* const Request);
	void FServer_OnRead(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic,
		CwclGattLocalCharacteristicReadRequest* const Value);
	void FServer_OnSubscribed(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic);
	void FServer_OnUnsubscribed(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic);
	void FServer_OnClientConnected(void* Sender, CwclGattServerClient* const Client);
	void FServer_OnClientDisconnected(void* Sender, CwclGattServerClient* const Client);
	void FServer_OnNotificationSizeChanged(void* Sender, CwclGattServerClient* const Client);
	void FServer_OnMaxPduSizeChanged(void* Sender, CwclGattServerClient* const Client);
	void FServer_OnConnectionParamsChanged(void* Sender, CwclGattServerClient* const Client);
	void FServer_OnConnectionPhyChanged(void* Sender, CwclGattServerClient* const Client);

	bool InitBluetooth(CwclBluetoothRadio*& Radio);
	void UninitBluetooth();

	bool InitServer(CwclBluetoothRadio* const Radio);
	void UninitServer();

	bool AddCharacteristics(CwclGattLocalService* const Service);
	bool AddServices();

	void Start();
	void Stop();

	void GetConParams(CwclGattServerClient* const Client);
	void GetConPhy(CwclGattServerClient* const Client);

public:
	afx_msg void OnBnClickedButtonStart();
	afx_msg void OnBnClickedButtonStop();
	afx_msg void OnDestroy();
};
