#ifndef __BSS_HB_H__
#define __BSS_HB_H__

#include <ctype.h>
#include <errno.h>
#include <arpa/inet.h>

#include "mysql.h"
#include "iwtypes.h"
#include "includes.h"

#define BSC_BASE	9*1024 
#define BTS_BASE	10*1024 

#define BSC_OBJID	0x01	
#define BTS_OBJID	0x02	
#define OBJID_LEN	0x0C
#define VAR		0xff

#define SNMP_HEADER_LEN		44

#define LENGTH1		1 
#define LENGTH2		14 
#define LENGTH3		25 
#define LENGTH4		27 
#define SNMP_OBJID	39 
#define VALUE_LEN	43 
/*
 * System Type in heartbeat information
 */
#define BSC	9
#define BTS	10

#define NO_OF_BSS		12
#define NO_OF_BTSMgr	13
/*
 * Heartbeat message field length
 */
#define HB_SYSTEM_ID_LEN	2
#define HB_TIMESTAMP_LEN	4
#define HB_LED_CONTROL_LEN	8
#define HB_COMPONENT_ID_LEN	1
#define HB_ALARMLEVEL_LEN	1
#define HB_ALARMCODE_LEN	1
#define HB_RESERVED_LEN		1
#define HB_EXTENDED_LEN		48

#define HB_BODY_LEN		( HB_SYSTEM_ID_LEN + HB_TIMESTAMP_LEN		\
		                + HB_LED_CONTROL_LEN + HB_COMPONENT_ID_LEN	\
				+ HB_ALARMLEVEL_LEN + HB_ALARMCODE_LEN		\
				+ HB_RESERVED_LEN + HB_EXTENDED_LEN )

#define SNMP_MSG_LEN		(SNMP_HEADER_LEN + HB_BODY_LEN)
#define HB_NO_OF_LED		HB_LED_CONTROL_LEN * 2

#define def_sql_length   16384

/****************************************
 * BSS LED struct
 * Max 12 BSC and 12x12 BTS = 156 units
 ****************************************/
struct tBssLed
{
	bool presence;
	int led[HB_NO_OF_LED];
	int component_id;
	int alarmcode;
	int alarmlevel;
};
//define the structure that store status info
typedef struct _tHeartbeat
{
	int system_id;				/* 0-512			*/
	time_t timestamp;			/* 32-bit timestamp		*/
	BYTE led_control[HB_LED_CONTROL_LEN];	/* 16 LED, 4-bit per LED	*/
						/* 0 - off			*/
						/* 1 - green			*/
						/* 2 - red			*/
						/* 3 - blue			*/
						/* 4 - yellow			*/
						/* 5 - ambra			*/
						/* 6 - white			*/
						/* 7 - not in use		*/
	int component_id;
	int alarmlevel;
	int alarmcode;
	int reserved;
	BYTE extended[HB_EXTENDED_LEN];		/* application defined		*/
} tHeartbeat;

/*
 * index = BSSID * NO_OF_BTSMgr + BTSMGR_ID
 * e.g. BSC01 -> system_id = 13
 *      BTS3 of BSC01 -> system_id = 1*13+3 = 16
 *
 * Heartbeat format:
 *
 * byte 0-1:   system id
 *             0  :000-003 OMC
 *             4  :004-007 HLR
 *             8  :008-00B PPS
 *             12 :00C-00F CDR
 *             16 :010-013 SMS
 *             ... ... ...
 *             100:064-077 VPS
 *             120:078-0B7 CSU
 *             184:0B8-0CF BSC
 *             208:0D0-1EF BTS
 *
 * byte 2-5:   timestamp (4 bytes)
 * byte 6-13:  16 led code
 *             bit 3: 1=continuous, 0=blinking
 *             bit 2-0: 0=off,1=green,2=yellow,3=red,4=blue,5=umbra,6=white,7=not in use
 * byte 14:    Component ID (need further defined)
 * byte 15:    alarm level (00 normal, 01 critical, 02 major)
 * byte 16:    alarm code
 *             0 normal, 1-255 alarmcode,
 *             254(0FEH) reserved for device timeout
 *             255(0FFH) group alarm clear
 * byte 17:    reserved
 * byte 18-65: extended status (not used by BSS OMC)
 */
#define LED_OFF		(0x8) 
#define LED_GREEN	(0x8 | 1)
#define LED_YELLOW 	(0x8 | 2)
#define LED_RED 	(0x8 | 3)
#define LED_BLUE 	(0x8 | 4)
#define LED_UMBRA 	(0x8 | 5)
#define LED_WHITE 	(0x8 | 6)
#define LED_NOT_IN_USE 	(0x8 | 7)

extern void initBssStatusTransmitter(void);
extern void updateBssStatus(MYSQL *mysql_sock, int led_num, int color_ctrl);
extern void send_heartbeat(int index, struct tBssLed *pbsc_led,struct tBssLed *pbts_led, int component_id, int alarmcode, int alarmlevel,int bsType);
extern void clearBssStatus(void);
extern int object2LedId(int btsid,int signal_element);
extern struct tBssLed bts_led[NO_OF_BSS*NO_OF_BTSMgr];
extern struct tBssLed bsc_led[NO_OF_BSS];

#endif /* __BSS_HB_H__ */
