<repo_name>isliulin/TimeFrequency<file_sep>/MCPU/ext_ctx.h
#ifndef	__EXT_CTX__
#define	__EXT_CTX__







#include "ext_type.h"







#define	EXT_DRV_NUM		2
#define	EXT_MGR_NUM		6
#define	EXT_OUT_NUM		30
#define	EXT_GRP_NUM		3


#define	EXT_1_OUT_LOWER		23
#define	EXT_1_OUT_UPPER		32

#define	EXT_1_PWR1			35
#define	EXT_1_PWR2			36

#define	EXT_1_MGR_PRI		37
#define	EXT_1_MGR_RSV		38

#define	EXT_1_OUT_OFFSET	(EXT_1_OUT_LOWER)

#define	EXT_2_OUT_LOWER		39
#define	EXT_2_OUT_UPPER		48

#define	EXT_2_PWR1			51
#define	EXT_2_PWR2			52

#define	EXT_2_MGR_PRI		53
#define	EXT_2_MGR_RSV		54

#define	EXT_2_OUT_OFFSET	(EXT_2_OUT_LOWER - 10)

#define	EXT_3_OUT_LOWER		55
#define	EXT_3_OUT_UPPER		66//58，59不使用

#define	EXT_3_PWR1			67
#define	EXT_3_PWR2			68

#define	EXT_3_MGR_PRI		69
#define	EXT_3_MGR_RSV		70

#define	EXT_3_OUT_OFFSET	(EXT_3_OUT_LOWER - 20)








#define	EXT_SAVE_PATH	"/var/p300file"
#define	EXT_SAVE_ADDR	0x1F0000








enum
{
    EXT_NONE	= 'O',
    EXT_PWR		= 'M',
    EXT_MGR		= 'f',
    EXT_OUT16	= 'J',
    EXT_OUT32	= 'a',
    EXT_DRV		= 'd',
    EXT_OUT32S	= 'h',
    EXT_OUT16S	= 'i'
};












struct extdrv
{
    /*
      1 主DRV
      2 备DRV
      3 通信故障
      4 未连接
    */
    u8_t drvPR[EXT_GRP_NUM];

    /*
      DRV和MGR之间的连接状态，1个字节
      1	未连接
      0	已连接
      0100Y3Y2Y1Y0
      Y0-Y2，分别表示DRV和MGR(1-3)之间的连接状态
    */
    u8_t drvAlm;

    /*版本信息*/
    u8_t drvVer[23];
};







struct extmgr
{
    /*
      1	主MGR
      2	备MGR
    */
    u8_t mgrPR;

    /*
      MGR和输出板卡之间的通讯告警，3个字节
      1	告警
      0	正常
      0100Y3Y2Y1Y0 0100Y7Y6Y5Y4 0100Y11Y10Y9Y8
      Y0-Y9，分别表示MGR和输出板卡(1-10)之间的通讯告警
      Y10	表示PWR1告警
      Y11	表示PWR2告警
      Y12	1槽位拔盘告警
      Y13	2槽位拔盘告警
      Y14	3槽位拔盘告警
      Y15	4槽位拔盘告警
      Y16	5槽位拔盘告警
      Y17	6槽位拔盘告警
      Y18	7槽位拔盘告警
      Y19	8槽位拔盘告警
      Y20	9槽位拔盘告警
      Y21	10槽位拔盘告警
      Y22	11槽位拔盘告警
      Y24	12槽位拔盘告警
      Y24	13槽位拔盘告警
      Y25	14槽位拔盘告警
    */
    u8_t mgrAlm[8];

    /*版本信息*/
    u8_t mgrVer[23];
};







struct extout
{
    /* 时钟等级 */
    u8_t outSsm[3];

    /*
      信号类型
      0：2.048Mhz
      1：2.048Mbit/s
      2：无输出
    */
    u8_t outSignal[17];

    /*
      第一个字节表示<mode>，第二个字节表示<out-sta>
      <mode> ::= 0|1，普通模式|主备模式
      <out-sta> ::= 0|1，主用|备用
    */
    u8_t outPR[4];

    /*
      告警，6个字节
      0100Y3Y2Y1Y0 0100Y7Y6Y5Y4 0100Y11Y10Y9Y8
      0100Y15Y14Y13Y12 0100Y19Y18Y17Y16 0100Y23Y22Y21Y20
      Y0		主钟10M输入告警
      Y1		备钟10M输入告警
      Y2~Y17	1~16（路/组）输出告警
    */
    u8_t outAlm[7];

    /*版本信息*/
    u8_t outVer[23];
};








struct extsave
{
    /*
      'E'	FLASH已保存配置
    */
    u8_t flashMark;

    /*
      1 主DRV
      2 备DRV
    */
    u8_t drvPR[EXT_DRV_NUM][EXT_GRP_NUM];

    /*
      1	主MGR
      2	备MGR
    */
    u8_t mgrPR[EXT_MGR_NUM];

    /* 时钟等级 */
    u8_t outSsm[EXT_OUT_NUM][3];

    /*
      信号类型
      0：2.048Mhz
      1：2.048Mbit/s
      2：无输出
    */
    u8_t outSignal[EXT_OUT_NUM][17];

    /*
      第一个字节表示<mode>，第二个字节表示<out-sta>
      <mode> ::= 0|1，普通模式|主备模式
      <out-sta> ::= 0|1，主用|备用
    */
    u8_t outPR[EXT_OUT_NUM][4];
    unsigned char onlineSta;
};








struct extctx
{
    //ext save
    struct extsave save;

    // MCP和DRV的通信告警
    // 1	告警
    // 0	正常
    u8_t extMcpDrvAlm[2];

    /* 2张DRV */
    struct extdrv drv[EXT_DRV_NUM];

    /* 板卡类型，O 无盘，M PWR，f MGR，J OUT16，a OUT32 */
    u8_t extBid[EXT_GRP_NUM][15];

    /* 6张MGR */
    struct extmgr mgr[EXT_MGR_NUM];

    /* 30张输出板卡 */
    struct extout out[EXT_OUT_NUM];

    u8_t old_drv_mgr_sta[32];
    u8_t new_drv_mgr_sta[32];
};








#endif//__EXT_CTX__


<file_sep>/MCPU/mcp_main.h

#ifndef _MCP_MAIN_H
#define  _MCP_MAIN_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
//#include <linux/if.h>
#include <net/route.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>

#include <fcntl.h>

#include <errno.h>
#include "version.h"




#define PORT 5000               //服务器端口  
#define BACKLOG 5               //listen队列中等待的连接数  
#define MAXRECVDATA 1024
#define MAXDATASIZE 256        //缓冲区大小 
#define SENDBUFSIZE 168
//#define _CMD_NODE_MAX 70    //命令条数//modified 2013-4-27
//#define _CMD_NODE_MAX ((int)(sizeof(g_cmd_fun)/sizeof(_CMD_NODE)))
#define PROJ_ID 32
#define PATH_NAME	"/var/"
#define MAX_CLIENT 11

//#define _NUM_RPT100S 45  //
#define _NUM_REVT  48      //

#define SAVE_ALM_NUM 50

#define CONFIG_FILE "/usr/config.txt"

#define CR 0x0D       //回车
#define LF 0x0A       //换行
#define SP 0x20	//空格

#define MCP_SLOT 0x75 //即u
#define GBTP1_SLOT 0x71 //q
#define GBTP2_SLOT 0x72 //r
#define RB1_SLOT 0x6f     //o
#define RB2_SLOT 0x70     //p

#ifndef INT_EXTINT0
#define INT_EXTINT0     0x00000000
#endif
#ifndef INT_EXTINT1
#define INT_EXTINT1     0x00000001
#endif

#define FPGA_DEV "/dev/p210fpga"
#define FILE_DEV "/var/p300file"

#define NO_INFO 0
#define DEBUG_NET_INFO 1
#define API_DEF 1
#define NET_INFO	"<NET-INFO>"
#define NET_SAVE	"<NET-SAVE>"

#define TYPE_MN "MN"
#define TYPE_MJ "MJ"
#define TYPE_CL "CL"
#define TYPE_CR "CR"
#define TYPE_WA "WA"

#define TYPE_MN_A "* "
#define TYPE_MJ_A "**"
#define TYPE_CL_A "A "
#define TYPE_CR_A "*C"
#define TYPE_WA_A "*W"

#define COMMA_1 ","
#define COMMA_3 ",,,"
#define COMMA_7 ",,,,,,,"
#define COMMA_8 ",,,,,,,,"
#define COMMA_9 ",,,,,,,,,"



#define GBTP_G "G,00"
#define GBTP_B "B,00"
#define GBTP_GPS "GPS,00"
#define GBTP_BD  "BD,00"
#define GBTP_GLO "GLO,00"
#define GBTP_GAL "GAL,00"

struct mymsgbuf
{
    long msgtype;
    unsigned char slot;
    int len;
    unsigned char ctag[7];
    unsigned char ctrlstring[SENDBUFSIZE];
};


//网管下发命令结构体
typedef struct
{
    unsigned char cmd[16];
    int cmd_len;
    unsigned char solt;
    unsigned char ctag[7];
    unsigned char data[MAXDATASIZE];
} _MSG_NODE, *P_MSG_NODE;

typedef struct
{
    unsigned char cmd[14];
    int cmd_len;
    int  (*cmd_fun)(_MSG_NODE *, int);
    int  (*rpt_fun)(int , _MSG_NODE * );
} _CMD_NODE, *P_CMD_NODE;

typedef struct
{
    //
    //!RPT100S cmd 增加到12
    //
    unsigned char cmd[12];
    int cmd_len;
    unsigned char cmd_num[4];
    int  (*rpt_fun)(char , unsigned char *);
} _RPT100S_NODE;

typedef struct
{
    unsigned char cmd_num[4];
    int  (*rpt_fun)(char , unsigned char *);
} _REVT_NODE;

extern _CMD_NODE g_cmd_fun[];
extern _RPT100S_NODE RPT100S_fun[];
extern _REVT_NODE REVT_fun[];
extern int print_switch;
extern char TOD_STA;
#endif

<file_sep>/MCPU/ext_cmn.c
#include "ext_cmn.h"
//#include "memwatch.h"




int strlen_r( char *str , char end)
{
    int len = 0;

    while( (*str++) != end )
    {
        len++;
    }

    return len;
}



<file_sep>/MCPU/mcp_process.h

#ifndef _MCP_PROCESS_H_
#define _MCP_PROCESS_H_


#include "ext_type.h"
#include "mcp_main.h"



extern int shake_hand_count;
extern int reset_network_card;


int send_tod(unsigned char *buf, unsigned char slot);
int get_tod_sta(unsigned char *buf, unsigned char slot);

extern int strncmp1(unsigned char *p1, unsigned char *p2, unsigned char n);
extern void strncpynew(unsigned char *str1, unsigned char *str2, int n);
extern void strncatnew(unsigned char *str1, unsigned char *str2, int n);





int current_client;
int g_lev;

int init_timer();
void init_pan(char slot);
int judge_length(unsigned char *buf);
void sendto_all_client(unsigned char *buf);
int judge_client_online(int *clt_num);
void judge_data(unsigned char *recvbuf, int len , int client);
int judge_buff(unsigned char *buf);

int IsLeapYear(int year);

void init_var(void);

int Open_FpgaDev(void);
void Close_FpgaDev(void);
int FpgaRead( const long paddr, unsigned short *DstAddr);
int FpgaWrite( const long paddr, unsigned short value);

int process_config();

int process_client(int client, char *recvbuf, int len);   //客户请求处理函数

int process_report(unsigned char *recv, int len);
int process_ralm(unsigned char *recv, int len);
int process_revt(unsigned char *recv, int len);
void rpt_alm_to_client(char slot, unsigned char *data);
void send_online_framing(int num, char type);
void get_alm(char slot, unsigned char *data);
int get_time(unsigned char *data);
int get_fpga_ver(unsigned char *data);

int send_pullout_alm(int num, int flag_cl);

void get_time_fromnet();

void get_data(char slot, unsigned char *data);

void rpt_rb(char slot, unsigned char *data);
void rpt_gbtp(char slot, unsigned char *data);
void rpt_out(char slot, unsigned char *data);
void rpt_mcp(unsigned char *data);
void rpt_test(char slot, unsigned char *data);
void rpt_drv(char slot, unsigned char *data);




void send_alm(char slot, int num, char type, int flag_cl);
void save_alm(char slot, unsigned char *data, int flag);
void rpt_alm_framing(char slot, unsigned char *ntfcncde,
                     unsigned char *almcde, unsigned char *alm);
void get_gbtp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_gtp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_btp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);

void get_rb_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_TP16_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_OUT16_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_PTP_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);

void get_PGEIN_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_PGE4V_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);//
void get_tod_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_ntp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_drv_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_mge_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);


void get_MCP_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);
void get_REF_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num);

int cmd_process(int client, _MSG_NODE *msg);
void respond_success(int client, unsigned char *ctag);
void respond_fail(int client, unsigned char *ctag, int type);
int get_format(char *format, unsigned char *ctag);
int if_a_string_is_a_valid_ipv4_address(const char *str);



void Init_filesystem();
void config_enet();

extern void save_config();

extern int API_Set_McpIp(unsigned char  *mcp_local_ip);
extern int API_Set_McpMask(unsigned char  *mcp_local_mask);
extern int API_Set_McpGateWay(unsigned char *mcp_local_gateway);
extern int API_Set_McpMacAddr(unsigned char *mcp_local_mac);
extern int API_Get_McpIp(unsigned char  *mcp_local_ip);
extern int API_Get_McpMask(unsigned char  *mcp_local_mask);
extern int API_Get_McpGateWay(unsigned char  *mcp_local_gateway);
extern int API_Get_McpMacAddr(unsigned char  *mcp_local_mac);



extern void sendtodown(unsigned char *buf, unsigned char slot);
void rpt_event_framing(unsigned char *event, char slot,
                       int flag_na, unsigned char *data, int flag_sa, unsigned char *reason);
void send_out_lev(int flag_lev, int num);
int get_freq_lev(int lev);
int get_out_now_lev(unsigned char *parg);
void get_out_now_lev_char(int lev );
void get_out_lev(int num, int flag);
int get_ref_tl(unsigned char c);
int send_ref_lev(int num);
int file_exists(char *filename);

int msgQ_create( const char *pathname, char proj_id );
void sendtodown_cli(unsigned char *buf, unsigned char slot, unsigned char *ctag);








int ext_issue_cmd(u8_t *buf, u16_t len, u8_t slot, u8_t *ctag);
void get_ext1_eqt(char slot, unsigned char *data);
void get_ext2_eqt(char slot, unsigned char *data);
void get_ext3_eqt(char slot, unsigned char *data);

void get_ext_alm(char slot, unsigned char *data);
void get_ext1_alm(char slot, unsigned char *data);
void get_ext2_alm(char slot, unsigned char *data);
void get_ext3_alm(char slot, unsigned char *data);

int ext_alm_cmp(u8_t ext_sid, u8_t *ext_new_data, struct extctx *ctx);
int ext_alm_evt(u8_t eid, u8_t sid, u8_t almid, u8_t *agcc1, u8_t *agcc2);
int ext_bpp_cfg(u8_t ext_sid, u8_t *ext_new_type, struct extctx *ctx);
void ext_bpp_evt(int aid, char bid);

int ext_drv_mgr_evt(struct extctx *ctx);

int ext_bid_cmp(u8_t ext_sid, u8_t *ext_new_data, struct extctx *ctx);

void ext_ssm_cfg(char *pSsm);


void mcpd_reset_network_card(void);

void timer_mcp(void * loop);
int Create_Thread(bool_t * loop);

void get_exta1_eqt(char slot, unsigned char *data);
void get_exta2_eqt(char slot, unsigned char *data);
void get_exta3_eqt(char slot, unsigned char *data);








#endif

<file_sep>/MCPU/mcp_set_struct.h

#ifndef _MCP_SET_STRUCT_H_
#define _MCP_SET_STRUCT_H_


typedef struct
{
    unsigned char ip[16];
    unsigned char mask[16];
    unsigned char gate[16];
    unsigned char dns1[16];
    unsigned char dns2[16];
    unsigned char mac[18];
    unsigned char leap_num[3];
    unsigned char tid[17];
    unsigned char time_source; //MCP盘时间源
    unsigned char msmode[5];   //MCP主备用状态

    unsigned char tl[3];
    unsigned char fb;
    /*'0' / '1' 默认为'1'(打开)*/
    unsigned char out_ssm_en;

    /*默认为0x04*/
    unsigned char out_ssm_oth;
} mcp_content;

typedef struct
{
    unsigned char mode[4];
    unsigned char mask[3];
    unsigned char pos[35];
    unsigned char bdzb[8];
	//unsigned char delay[8];
	//unsigned char out_pps_delay[8];
} gbtp_content;

typedef struct
{
    unsigned char msmode[5];
    unsigned char sys_ref;
    unsigned char dely[4][9];
    unsigned char priority[9];//the front of reference source priority.
    unsigned char mask[9];
    unsigned char leapmask[6];

    unsigned char tzo[4];
    unsigned char leaptag;
	unsigned char thresh[2][9];
} rb_content;

typedef struct
{
    unsigned char ptp_type;
    unsigned char out_lev[3];
    unsigned char ptp_linkmode[4];
    unsigned char ptp_enp[4];
    unsigned char ptp_step[4];
    unsigned char ptp_en[4];
    unsigned char ptp_delaytype[4];
    unsigned char ptp_multicast[4];
    unsigned char out_typ[17];
    unsigned char out_mod;
    unsigned char tp16_en[17];
    unsigned char ptp_sync[4][3];
    unsigned char ptp_announce[4][3];
    unsigned char ptp_delayreq[4][3];
    unsigned char ptp_pdelayreq[4][3];
    unsigned char ptp_delaycom[4][9];
    unsigned char ptp_ip[4][16];
    unsigned char ptp_gate[4][16];
    unsigned char ptp_mask[4][16];
    unsigned char ptp_dns1[4][16];
    unsigned char ptp_dns2[4][16];
    unsigned char ptp_mac[4][18];
    unsigned char ptp_out[4];
    unsigned char ref_prio1[9];
    unsigned char ref_prio2[9];
    unsigned char ref_mod1[4];
    unsigned char ref_mod2[4];

    unsigned char ref_in_ssm_en[9];
	unsigned char ref_2mb_lvlm;
    unsigned char rs_en[5];
    unsigned char rs_tzo[5];

    unsigned char ppx_mod[17];

    unsigned char tod_en[17];
    unsigned char tod_br;
    unsigned char tod_tzo;

    unsigned char igb_en[5];
    unsigned char igb_rat[5];
    unsigned char igb_max[5];

    unsigned char ntp_ip[9];
    unsigned char ntp_mac[13];
    unsigned char ntp_gate[9];
    unsigned char ntp_msk[9];

    unsigned char ntp_bcast_en;
    unsigned char ntp_tpv_en;
    unsigned char ntp_md5_en;
    unsigned char ntp_ois_en;
    unsigned char ntp_interval[5];

    unsigned char ntp_key_flag[4];
    unsigned char ntp_keyid[4][5];
    unsigned char ntp_key[4][17];

    /*unsigned char out_type;*/
} out_content;

#define alm_msk_total 50

struct ALM_MSK
{
    unsigned char data[24];
    unsigned char type; //type 类型，拼写错了
};


typedef struct
{
    //'M'表示保存过配置
    u8_t flashMask;

    //所有要保存设置的信息
    mcp_content slot_u;
    gbtp_content slot_q, slot_r;
    rb_content slot_o, slot_p;
    out_content slot[14];
    struct ALM_MSK alm_msk[alm_msk_total];
} file_content;
file_content conf_content;

typedef struct
{
    unsigned char ip[9];
    unsigned char gate[9];
    unsigned char mask[9];
    unsigned char dns1[9];
    unsigned char dns2[9];
    unsigned char mac[18];
} STR_STA1;

typedef struct
{
    u8_t flashMask;
    unsigned char ptp_prio1[14][4][5]; //14个槽位，4个端口，5个字节保存信息。
    unsigned char ptp_prio2[14][4][5];
    unsigned char LeapMod;
	unsigned char ptp_mtc_ip[14][4][9];//保存单播ip
	unsigned char ptp_dom[14][4][4];   //20170616新添时钟域号保存
	unsigned char ptp_esmcen[14][4];   //ptp esmcen .
	unsigned char ptp_sfp[14][4][3];
	unsigned char ptp_oplo[14][4][7];
	unsigned char ptp_ophi[14][4][7];
    unsigned char irigb_tzone[14]; 
    STR_STA1 sta1;
	unsigned char mcp_protocol;
	unsigned char gbtp_delay[2][8];
	unsigned char gbtp_pps_delay[2][16];
	unsigned char pps_sel[2];
	unsigned char tdev_en[2];
	unsigned char ptp_mtc1_ip[14][4][152];
	//unsigned char rbb_priority[9];    //#sb the back of reference source priority.
	
} file_content3; //第一部分配置信息由file_content保存，第二部分为扩展框，此为第三部分。
file_content3 conf_content3;

#define ALM_MASK_LEN   		6
#define MAX_PORT_NUM   		52
#define MAX_MAC_LEN    		12
#define MAX_IP_LEN     		8
#define FRAME_FRE_LEN  		2
#define DELAY_COM_LEN  		7
#define MAX_PRIO_LEN   		4
#define MAX_DOM_LEN    		3
#define MAX_OPTHR_LEN  		6
#define MAX_TIMES_LEN  		4
#define MAX_CLKCLS_LEN 		3
#define MAX_SYNCSSM_LEN 	13

#define PGE4S_ALM5_START    23
#define PGE4S_TALM5_START   22


#endif
<file_sep>/MCPU/version.h
#ifndef _VERSION_H_
#define _VERSION_H_


	//主版本号
	#define VER_MAJOR	"12"	
	//子版本号
	#define VER_MINOR	"11"	
	//修订版本号
	#define VER_REVIS	"0"
	//版本完成日期
	#define VER_DATE	"190218"
	//版本标识
	#define VER_STATE	'r'	//正式发布版本
	//#define VER_STATE 't'
	
	#define MCP_VER "v"VER_MAJOR"."VER_MINOR
	

#endif<file_sep>/MCPU/ext_alloc.h
#ifndef	__EXT_ALLOC__
#define	__EXT_ALLOC__







#include "ext_ctx.h"








int initializeContext(struct extctx *ctx);

int ext_read_flash(struct extsave *psave);
int ext_write_flash(struct extsave *psave);

void ext_drv2_mgr6_pr(struct extctx *ctx, u8_t bid13, u8_t bid14);












#endif//__EXT_ALLOC__


<file_sep>/MCPU/ext_alloc.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "ext_alloc.h"
#include "mcp_set_struct.h"
//#include "memwatch.h"

/*
  1	成功
  0	失败
*/
int initializeContext(struct extctx *ctx)
{
    int i, ret;

    memset(ctx, 0, sizeof(struct extctx));

    //drv主备状态初始化
    for(i = 0; i < EXT_GRP_NUM; i++)
    {
        ctx->drv[0].drvPR[i] = 1;
    }

    for(i = 0; i < EXT_GRP_NUM; i++)
    {
        ctx->drv[1].drvPR[i] = 2;
    }

    //mgr主备状态初始化
    for(i = 0; i < EXT_MGR_NUM; i += 2)
    {
        ctx->mgr[i].mgrPR = 1;
        ctx->mgr[i + 1].mgrPR = 2;
    }

    for(i = 0; i < EXT_GRP_NUM; i++)
    {
        memcpy(ctx->extBid[i], "OOOOOOOOOOOOOO", 14);
    }

    for(i = 0; i < EXT_DRV_NUM; i++)
    {
        ctx->drv[i].drvAlm = '@';
    }

    for(i = 0; i < EXT_MGR_NUM; i++)
    {
        memcpy(ctx->mgr[i].mgrAlm, "@@@@@@@", 7);
    }

    for(i = 0; i < EXT_OUT_NUM; i++)
    {
        memcpy(ctx->out[i].outAlm, "@@@@@@", 6);
    }

    ret = ext_read_flash(&(ctx->save));

    printf("return=%d\n", ret);
    printf("p300-1 flashmark=%c\n", ctx->save.flashMark);


    if( (1 == ret) && ('F' == ctx->save.flashMark) )
    {
        printf("%s\n", "read config from flash");

        for(i = 0; i < EXT_OUT_NUM; i++)
        {
            memcpy(ctx->out[i].outSsm, ctx->save.outSsm[i], 2);
            memcpy(ctx->out[i].outSignal, ctx->save.outSignal[i], 16);
            memcpy(ctx->out[i].outPR, ctx->save.outPR[i], 3);
        }
    }
    else
    {
        for(i = 0; i < EXT_OUT_NUM; i++)
        {
            memcpy(ctx->out[i].outSsm, "04", 2);
            memcpy(ctx->out[i].outSignal, "1111111111111111", 16);
            memcpy(ctx->out[i].outPR, "0,0", 3);
        }

        memset(&ctx->save, 0, sizeof(struct extsave));
        for(i = 0; i < EXT_OUT_NUM; i++)
        {
            memcpy(ctx->save.outSsm[i], ctx->out[i].outSsm, 2);
            memcpy(ctx->save.outSignal[i], ctx->out[i].outSignal, 16);
            memcpy(ctx->save.outPR[i], ctx->out[i].outPR, 3);
        }

        printf("%s\n", "write config to flash");
        ctx->save.flashMark = 'F';
        if(0 == ext_write_flash(&(ctx->save)))
        {
            return 0;
        }
    }

    if( (ctx->save.onlineSta & 0xf0) != 0x40)
    {
        ctx->save.onlineSta = 0x4f;
    }

    printf("sizeof(struct extsave) = %d\n", (int)sizeof(struct extsave));
    printf("sizeof(struct extctx) = %d\n", (int)sizeof(struct extctx));

    return 1;
}







/*
  1	成功
  0	失败
*/
int ext_read_flash(struct extsave *psave)
{
    FILE *pf;
    u8_t cmd[128];

    memset(cmd, 0, 128);
    sprintf((char *)cmd, "dd if=/dev/rom1 of=%s bs=65536 count=1 skip=31", EXT_SAVE_PATH);
    if(-1 == system(cmd))
    {
        return 0;
    }
    sync();
    printf("%s SUCCESS\n", cmd);

    pf = fopen(EXT_SAVE_PATH, "rb+");
    if(NULL == pf)
    {
        return 0;
    }

    if(0 != fseek(pf, sizeof(file_content), SEEK_SET))
    {
        fclose(pf);
        return 0;
    }

    memset(psave, 0, sizeof(struct extsave));
    if(1 != fread(psave, sizeof(struct extsave), 1, pf))
    {
        fclose(pf);
        return 0;
    }

    fclose(pf);
    return 1;
}


/*
  1	成功
  0	失败
*/
int ext_write_flash(struct extsave *psave)
{
    FILE *pf;
    u8_t cmd[128];

    pf = fopen(EXT_SAVE_PATH, "rb+");
    if(NULL == pf)
    {
        return 0;
    }

    if(0 != fseek(pf, sizeof(file_content), SEEK_SET))
    {
        fclose(pf);
        return 0;
    }

    if(1 != fwrite(psave, sizeof(struct extsave), 1, pf))
    {
        fclose(pf);
        return 0;
    }

    fclose(pf);
	sync();
    memset(cmd, 0, 128);
    sprintf((char *)cmd, "fw -ul -f %s -o 0x%x /dev/rom1", EXT_SAVE_PATH, EXT_SAVE_ADDR);
    if(-1 == system(cmd))
    {
        return 0;
    }
    sync();
    printf("%s SUCCESS\n", cmd);
    return 1;
}







void ext_drv2_mgr6_pr(struct extctx *ctx, u8_t bid13, u8_t bid14)
{
    //drv13
    if(EXT_DRV != bid13)
    {
        ctx->drv[0].drvPR[0] = 4;
        ctx->drv[0].drvPR[1] = 4;
        ctx->drv[0].drvPR[2] = 4;
    }
    else
    {
        //连接状态
        if( (ctx->drv[0].drvAlm)&BIT(0) )
        {
            ctx->drv[0].drvPR[0] = 4;
        }
        else
        {
            //通信告警
            if(1 == ctx->extMcpDrvAlm[0])
            {
                ctx->drv[0].drvPR[0] = 3;
            }
            else
            {
                ctx->drv[0].drvPR[0] = 1;
            }
        }

        //连接状态
        if( (ctx->drv[0].drvAlm)&BIT(1) )
        {
            ctx->drv[0].drvPR[1] = 4;
        }
        else
        {
            //通信告警
            if(1 == ctx->extMcpDrvAlm[0])
            {
                ctx->drv[0].drvPR[1] = 3;
            }
            else
            {
                ctx->drv[0].drvPR[1] = 1;
            }
        }

        //连接状态
        if( (ctx->drv[0].drvAlm)&BIT(2) )
        {
            ctx->drv[0].drvPR[2] = 4;
        }
        else
        {
            //通信告警
            if(1 == ctx->extMcpDrvAlm[0])
            {
                ctx->drv[0].drvPR[2] = 3;
            }
            else
            {
                ctx->drv[0].drvPR[2] = 1;
            }
        }
    }

    //drv14
    if(EXT_DRV != bid14)
    {
        ctx->drv[1].drvPR[0] = 4;
        ctx->drv[1].drvPR[1] = 4;
        ctx->drv[1].drvPR[2] = 4;
    }
    else
    {
        //连接状态
        if( (ctx->drv[1].drvAlm)&BIT(0) )
        {
            ctx->drv[1].drvPR[0] = 4;
        }
        else
        {
            //通信告警
            if(1 == ctx->extMcpDrvAlm[1])
            {
                ctx->drv[1].drvPR[0] = 3;
            }
            else
            {
                if(1 != ctx->drv[0].drvPR[0])
                {
                    ctx->drv[1].drvPR[0] = 1;
                }
                else
                {
                    ctx->drv[1].drvPR[0] = 2;
                }
            }
        }

        //连接状态
        if( (ctx->drv[1].drvAlm)&BIT(1) )
        {
            ctx->drv[1].drvPR[1] = 4;
        }
        else
        {
            //通信告警
            if(1 == ctx->extMcpDrvAlm[1])
            {
                ctx->drv[1].drvPR[1] = 3;
            }
            else
            {
                if(1 != ctx->drv[0].drvPR[1])
                {
                    ctx->drv[1].drvPR[1] = 1;
                }
                else
                {
                    ctx->drv[1].drvPR[1] = 2;
                }
            }
        }

        //连接状态
        if( (ctx->drv[1].drvAlm)&BIT(2) )
        {
            ctx->drv[1].drvPR[2] = 4;
        }
        else
        {
            //通信告警
            if(1 == ctx->extMcpDrvAlm[1])
            {
                ctx->drv[1].drvPR[2] = 3;
            }
            else
            {
                if(1 != ctx->drv[0].drvPR[2])
                {
                    ctx->drv[1].drvPR[2] = 1;
                }
                else
                {
                    ctx->drv[1].drvPR[2] = 2;
                }
            }
        }
    }

    if((4 == ctx->drv[0].drvPR[0]) && (4 == ctx->drv[1].drvPR[0]))
    {
        memcpy(ctx->extBid[0], "OOOOOOOOOOOOOO", 14);
    }
    if((4 == ctx->drv[0].drvPR[1]) && (4 == ctx->drv[1].drvPR[1]))
    {
        memcpy(ctx->extBid[1], "OOOOOOOOOOOOOO", 14);
    }
    if((4 == ctx->drv[0].drvPR[2]) && (4 == ctx->drv[1].drvPR[2]))
    {
        memcpy(ctx->extBid[2], "OOOOOOOOOOOOOO", 14);
    }
}







<file_sep>/MCPU/ext_global.c
#include "ext_global.h"
//#include "memwatch.h"











struct extctx gExtCtx;


















<file_sep>/MCPU/ext_crpt.c
#include <stdio.h>
#include <string.h>

#include "ext_ctx.h"
#include "ext_crpt.h"
//#include "memwatch.h"



#define MGR_RALM_NUM 4


/*
  1	成功
  0	失败
*/
u8_t ext_out_ssm_crpt(u8_t save_flag, u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int _sid;

    if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
        _sid = ext_sid - EXT_1_OUT_LOWER;
        if( EXT_OUT16 == ctx->extBid[0][_sid] ||
                EXT_OUT32 == ctx->extBid[0][_sid] ||
                EXT_OUT16S == ctx->extBid[0][_sid] ||
                EXT_OUT32S == ctx->extBid[0][_sid] )
        {
            memcpy(ctx->out[ext_sid - EXT_1_OUT_OFFSET].outSsm, ext_data, 2);

            if(save_flag)
            {
                memcpy(ctx->save.outSsm[ext_sid - EXT_1_OUT_OFFSET], ext_data, 2);
            }
        }
    }
    else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
    {
        _sid = ext_sid - EXT_2_OUT_LOWER;
        if( EXT_OUT16 == ctx->extBid[1][_sid] ||
                EXT_OUT32 == ctx->extBid[1][_sid] ||
                EXT_OUT16S == ctx->extBid[1][_sid] ||
                EXT_OUT32S == ctx->extBid[1][_sid] )
        {
            memcpy(ctx->out[ext_sid - EXT_2_OUT_OFFSET].outSsm, ext_data, 2);

            if(save_flag)
            {
                memcpy(ctx->save.outSsm[ext_sid - EXT_2_OUT_OFFSET], ext_data, 2);
            }
        }
    }
    else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
    {
        if(ext_sid < 58)//except ':;'
        {
            _sid = ext_sid - EXT_3_OUT_LOWER;
            if( EXT_OUT16 == ctx->extBid[2][_sid] ||
                    EXT_OUT32 == ctx->extBid[2][_sid] ||
                    EXT_OUT16S == ctx->extBid[2][_sid] ||
                    EXT_OUT32S == ctx->extBid[2][_sid])
            {
                memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET].outSsm, ext_data, 2);

                if(save_flag)
                {
                    memcpy(ctx->save.outSsm[ext_sid - EXT_3_OUT_OFFSET], ext_data, 2);
                }
            }
        }
        else
        {
            _sid = ext_sid - EXT_3_OUT_LOWER - 2;
            if( EXT_OUT16 == ctx->extBid[2][_sid] ||
                    EXT_OUT32 == ctx->extBid[2][_sid] ||
                    EXT_OUT16S == ctx->extBid[2][_sid] ||
                    EXT_OUT32S == ctx->extBid[2][_sid] )
            {
                memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET - 2].outSsm, ext_data, 2);

                if(save_flag)
                {
                    memcpy(ctx->save.outSsm[ext_sid - EXT_3_OUT_OFFSET - 2], ext_data, 2);
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}





/*
  1	成功
  0	失败
*/
u8_t ext_out_signal_crpt(u8_t save_flag, u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int _sid;

    if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
        _sid = ext_sid - EXT_1_OUT_LOWER;
        if( EXT_OUT16 == ctx->extBid[0][_sid] ||
                EXT_OUT32 == ctx->extBid[0][_sid] ||
                EXT_OUT16S == ctx->extBid[0][_sid] ||
                EXT_OUT32S == ctx->extBid[0][_sid])
        {
            memcpy(ctx->out[ext_sid - EXT_1_OUT_OFFSET].outSignal, ext_data, 16);

            if(save_flag)
            {
                memcpy(ctx->save.outSignal[ext_sid - EXT_1_OUT_OFFSET], ext_data, 16);
            }
        }
    }
    else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
    {
        _sid = ext_sid - EXT_2_OUT_LOWER;
        if( EXT_OUT16 == ctx->extBid[1][_sid] ||
                EXT_OUT32 == ctx->extBid[1][_sid] ||
                EXT_OUT16S == ctx->extBid[1][_sid] ||
                EXT_OUT32S == ctx->extBid[1][_sid] )
        {
            memcpy(ctx->out[ext_sid - EXT_2_OUT_OFFSET].outSignal, ext_data, 16);

            if(save_flag)
            {
                memcpy(ctx->save.outSignal[ext_sid - EXT_2_OUT_OFFSET], ext_data, 16);
            }
        }
    }
    else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
    {
        if(ext_sid < 58)//except ':;'
        {
            _sid = ext_sid - EXT_3_OUT_LOWER;
            if( EXT_OUT16 == ctx->extBid[2][_sid] ||
                    EXT_OUT32 == ctx->extBid[2][_sid] ||
                    EXT_OUT16S == ctx->extBid[2][_sid] ||
                    EXT_OUT32S == ctx->extBid[2][_sid] )
            {
                memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET].outSignal, ext_data, 16);

                if(save_flag)
                {
                    memcpy(ctx->save.outSignal[ext_sid - EXT_3_OUT_OFFSET], ext_data, 16);
                }
            }
        }
        else
        {
            _sid = ext_sid - EXT_3_OUT_LOWER - 2;
            if( EXT_OUT16 == ctx->extBid[2][_sid] ||
                    EXT_OUT32 == ctx->extBid[2][_sid] ||
                    EXT_OUT16S == ctx->extBid[2][_sid] ||
                    EXT_OUT32S == ctx->extBid[2][_sid] )
            {
                memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET - 2].outSignal, ext_data, 16);

                if(save_flag)
                {
                    memcpy(ctx->save.outSignal[ext_sid - EXT_3_OUT_OFFSET - 2], ext_data, 16);
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}







/*
  1	成功
  0	失败
*/
u8_t ext_out_mode_crpt(u8_t save_flag, u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int _sid;
    if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
        _sid = ext_sid - EXT_1_OUT_LOWER;
        if( EXT_OUT16 == ctx->extBid[0][_sid] ||
                EXT_OUT32 == ctx->extBid[0][_sid] ||
                EXT_OUT16S == ctx->extBid[0][_sid] ||
                EXT_OUT32S == ctx->extBid[0][_sid] )
        {
            memcpy(ctx->out[ext_sid - EXT_1_OUT_OFFSET].outPR, ext_data, 3);
            if(save_flag)
            {
                memcpy(ctx->save.outPR[ext_sid - EXT_1_OUT_OFFSET], ext_data, 3);
            }
        }
    }
    else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
    {
        _sid = ext_sid - EXT_2_OUT_LOWER;
        if( EXT_OUT16 == ctx->extBid[1][_sid] ||
                EXT_OUT32 == ctx->extBid[1][_sid] ||
                EXT_OUT16S == ctx->extBid[1][_sid] ||
                EXT_OUT32S == ctx->extBid[1][_sid] )
        {
            memcpy(ctx->out[ext_sid - EXT_2_OUT_OFFSET].outPR, ext_data, 3);

            if(save_flag)
            {
                memcpy(ctx->save.outPR[ext_sid - EXT_2_OUT_OFFSET], ext_data, 3);
            }
        }
    }
    else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
    {
        if(ext_sid < 58)//except ':;'
        {
            _sid = ext_sid - EXT_3_OUT_LOWER;
            if( EXT_OUT16 == ctx->extBid[2][_sid] ||
                    EXT_OUT32 == ctx->extBid[2][_sid] ||
                    EXT_OUT16S == ctx->extBid[2][_sid] ||
                    EXT_OUT32S == ctx->extBid[2][_sid] )
            {
                memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET].outPR, ext_data, 3);

                if(save_flag)
                {
                    memcpy(ctx->save.outPR[ext_sid - EXT_3_OUT_OFFSET], ext_data, 3);
                }
            }
        }
        else
        {
            _sid = ext_sid - EXT_3_OUT_LOWER - 2;
            if( EXT_OUT16 == ctx->extBid[2][_sid] ||
                    EXT_OUT32 == ctx->extBid[2][_sid] ||
                    EXT_OUT16S == ctx->extBid[2][_sid] ||
                    EXT_OUT32S == ctx->extBid[2][_sid] )
            {
                memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET - 2].outPR, ext_data, 3);

                if(save_flag)
                {
                    memcpy(ctx->save.outPR[ext_sid - EXT_3_OUT_OFFSET - 2], ext_data, 3);
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}







/*
  1	成功
  0	失败
*/
u8_t ext_out_prr_crpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    return 1;
}







/*
  1	成功
  0	失败
*/
u8_t ext_out_alm_crpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
        memcpy(ctx->out[ext_sid - EXT_1_OUT_OFFSET].outAlm, ext_data, 6);
    }
    else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
    {
        memcpy(ctx->out[ext_sid - EXT_2_OUT_OFFSET].outAlm, ext_data, 6);
    }
    else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
    {
        if(ext_sid < 58)//except ':;'
        {
            memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET].outAlm, ext_data, 6);
        }
        else
        {
            memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET - 2].outAlm, ext_data, 6);
        }
    }
    else if(EXT_1_MGR_PRI == ext_sid)
    {
        memcpy(ctx->mgr[0].mgrAlm, ext_data, MGR_RALM_NUM);
    }
    else if(EXT_1_MGR_RSV == ext_sid)
    {
        memcpy(ctx->mgr[1].mgrAlm, ext_data, MGR_RALM_NUM);
    }
    else if(EXT_2_MGR_PRI == ext_sid)
    {
        memcpy(ctx->mgr[2].mgrAlm, ext_data, MGR_RALM_NUM);
    }
    else if(EXT_2_MGR_RSV == ext_sid)
    {
        memcpy(ctx->mgr[3].mgrAlm, ext_data, MGR_RALM_NUM);
    }
    else if(EXT_3_MGR_PRI == ext_sid)
    {
        memcpy(ctx->mgr[4].mgrAlm, ext_data, MGR_RALM_NUM);
    }
    else if(EXT_3_MGR_RSV == ext_sid)
    {
        memcpy(ctx->mgr[5].mgrAlm, ext_data, MGR_RALM_NUM);
    }
    else
    {
        return 0;
    }

    return 1;
}






<file_sep>/MCPU/ext_trpt.h
#ifndef	__EXT_TRPT__
#define	__EXT_TRPT__






#include "ext_ctx.h"






u8_t ext_out_ssm_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_signal_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_mode_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_bid_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_alm_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);

u8_t ext_out_ver_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_mgr_pr_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);












#endif//__EXT_TRPT__


<file_sep>/MCPU/mcp_set.c
#include "ext_crpt.h"
#include "ext_global.h"
#include "ext_ctx.h"
#include "mcp_main.h"
#include "mcp_process.h"

#include "ext_alloc.h"
#include "ext_cmn.h"
#include "mcp_def.h"
#include "mcp_set_struct.h"
#include "mcp_save_struct.h"
//#include "memwatch.h"




//#include <sys/ipc.h>
//#include <sys/msg.h>

/* fpga device file descriptor */
static int fpga_fd = -1;


/**
 * @brief use linux3.0.x
 * @details [long description]
 *
 */
typedef struct jiffy_counts_t
{
    unsigned long long usr, nic, sys, idle;
    unsigned long long iowait, irq, softirq, steal;
    unsigned long long total;
    unsigned long long busy;
} jiffy_counts_t;

#define CPU_INFO_PATH 	"/proc/stat"
#define LINE_BUF_NO 	500

/**
 * @brief 	cpu_utilization
 * @details [long description]
 * @return 	CPU utilization percentage example:return 1 -> 1%
 */
int cpu_utilization(void)
{
    static const char fmt[] = "cpu %llu %llu %llu %llu %llu %llu %llu %llu";
    char line_buf[LINE_BUF_NO];
    jiffy_counts_t jiffy;
    jiffy_counts_t jiffyold;

    int cpu_util;
    int ret;
    unsigned long long total_old;
    unsigned long long total;
    unsigned long long busy_old;
    unsigned long long busy;

    FILE *fp = fopen(CPU_INFO_PATH, "r");

    if (!fgets(line_buf, LINE_BUF_NO, fp))
    {
        printf("cpu utilization fgets err \n");
    }
    ret = sscanf(line_buf, fmt,
                 &jiffyold.usr, &jiffyold.nic, &jiffyold.sys, &jiffyold.idle,
                 &jiffyold.iowait, &jiffyold.irq, &jiffyold.softirq,
                 &jiffyold.steal);
    busy_old = jiffyold.usr + jiffyold.nic + jiffyold.sys;
    total_old = jiffyold.usr + jiffyold.nic + jiffyold.sys + jiffyold.idle;

    fclose(fp);

    sleep(1);

    fp = fopen(CPU_INFO_PATH, "r");

    if (!fgets(line_buf, LINE_BUF_NO, fp))
    {
        printf("cpu utilization fgets err \n");
    }
    ret = sscanf(line_buf, fmt,
                 &jiffy.usr, &jiffy.nic, &jiffy.sys, &jiffy.idle,
                 &jiffy.iowait, &jiffy.irq, &jiffy.softirq,
                 &jiffy.steal);


    busy = jiffy.usr + jiffy.nic + jiffy.sys;
    total = jiffy.usr + jiffy.nic + jiffy.sys + jiffy.idle;
    cpu_util = (busy - busy_old) * 100.0 / (total - total_old);
#if CPU_UTILIZATION_DEBUG
    printf("busy is %llu  busy_old is %llu total is %llu  total_old is %llu\n", busy, busy_old, total, total_old);
    printf("usr is %llu usr_old is %llu \n", jiffyold.usr, jiffy.usr);
    printf("cpu is %d \n", cpu_util);
#endif
    fclose(fp);

    return cpu_util;
}

int Open_FpgaDev(void)
{
    if ( ( fpga_fd = open( FPGA_DEV, O_RDWR) ) < 0 )
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


/* FPGA 读写函数, 读写FPGA 某一地址的值 */
int FpgaRead( const long paddr, unsigned short *DstAddr)
{
    long addr;
    int retval;

    addr = paddr << 16;

    retval = ioctl( fpga_fd, FPGA_READ, &addr );
    if ( 0 == retval )
    {
        *DstAddr = (unsigned short)( addr & 0xFFFF );
        return 1;
    }
    else
    {
        return 0;
    }
}


int FpgaWrite( const long paddr, unsigned short value)
{
    long addr;
    int retval;

    addr = (paddr << 16) + value;

    retval = ioctl( fpga_fd, FPGA_WRITE, &addr );
    if ( 0 == retval )
    {
        return 0;
    }
    else
    {
        return 1;
    }
}


void Close_FpgaDev(void)
{
    close(fpga_fd);
}




int cmd_set_tid(_MSG_NODE *msg, int flag)
{
    int len = 0;
    len = strlen(msg->data);
    if(len != 16)
    {
        return 1;
    }
    if(strncasecmp(msg->data, "LT", 2) == 0)
    {
        memset(conf_content.slot_u.tid, '\0', 17);
        memcpy(conf_content.slot_u.tid, msg->data, 16);
        respond_success(current_client, msg->ctag);

        save_config();
    }
    else
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:set_tid:%s\n", NET_INFO, msg->data);
        printf("now,tid:%s\n", conf_content.slot_u.tid);
    }
#endif
    return 0;
}
int cmd_hand_shake(_MSG_NODE *msg, int flag )
{
    shake_hand_count++;
    //printf("<cmd_hand_shake>\n");
    respond_success(current_client, msg->ctag);

    timeout_cnt[client_num]++;
    if(print_switch == 0)
    {
        printf("<cmd_hand_shake>client<%d>++\n", client_num);
    }

    return 0;
}
int cmd_set_main(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    //int len_send;
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(msg->solt == MCP_SLOT)
    {
        memset(conf_content.slot_u.msmode, '\0', 5);
        memcpy(conf_content.slot_u.msmode, msg->data, 4);
        save_config();
    }
    else if(msg->solt == RB1_SLOT)
    {
        //if(strcmp(rpt_content.slot_o.rb_msmode,msg->data)!=0)
        {
            memset(conf_content.slot_o.msmode, '\0', 5);
            memcpy(conf_content.slot_o.msmode, msg->data, 4);
            /*下发设置*/
            /* SET-MAIN:<ctag>::<msmode>;*/
            sprintf((char *)sendbuf, "SET-MAIN:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        //if(strcmp(rpt_content.slot_p.rb_msmode,msg->data)!=0)
        {
            memset(conf_content.slot_p.msmode, '\0', 5);
            memcpy(conf_content.slot_p.msmode, msg->data, 4);

            sprintf((char *)sendbuf, "SET-MAIN:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:set_main:%s\n", NET_INFO, msg->data);
        printf("now,msmode:%s\n", conf_content.slot_u.msmode);
    }
#endif
    return 0;
}
int cmd_set_dat(_MSG_NODE *msg, int flag)
{
    /*计算时间差值*/
    unsigned char year_s[5], year_rb[5];
    unsigned char month_s[3], month_rb[3];
    unsigned char day_s[3], day_rb[3];
    unsigned char hour_s[3], hour_rb[3];
    unsigned char minute_s[3], minute_rb[3];
    unsigned char second_s[3], second_rb[3];
    int len;
    int year, month, day, hour, minute, second;
    int year_r, month_r, day_r, hour_r, minute_r, second_r;
    if(conf_content.slot_u.time_source == '1')
    {
        len = strlen(msg->data);
        //printf("TEST<1>!\n");
        if(len == 21)
            /*	YYYY-MM-DD HH:mm:SS:f*/
        {
            memcpy(year_s, msg->data, 4);
            memcpy(month_s, &msg->data[5], 2);
            memcpy(day_s, &msg->data[8], 2);
            memcpy(hour_s, &msg->data[11], 2);
            memcpy(minute_s, &msg->data[14], 2);
            memcpy(second_s, &msg->data[17], 2);

            year = atoi(year_s);
            month = atoi(month_s);
            day = atoi(day_s);
            hour = atoi(hour_s);
            minute = atoi(minute_s);
            second = atoi(second_s);

            if(((hour < 0) || (hour > 23)) || ((minute < 0) || (minute > 60)) || ((second < 0) || (second > 60)))
            {
                return 1;
            }

            memcpy(year_rb, mcp_date, 4);
            memcpy(month_rb, &mcp_date[5], 2);
            memcpy(day_rb, &mcp_date[8], 2);
            memcpy(hour_rb, mcp_time, 2);
            memcpy(minute_rb, &mcp_time[3], 2);
            memcpy(second_rb, &mcp_time[6], 2);

            year_r = atoi(year_rb);
            month_r = atoi(month_rb);
            day_r = atoi(day_rb);
            hour_r = atoi(hour_rb);
            minute_r = atoi(minute_rb);
            second_r = atoi(second_rb);
            //	printf("TEST<2>!\n");
            if((year == year_r) && (month == month_r) && (day == day_r))
            {
                time_temp = (hour - hour_r) * 3600;
                time_temp += (minute - minute_r) * 60;
                time_temp += (second - second_r);
                printf("time_temp=%d\n", time_temp);
                //
            }
        }
        else
        {
            //	printf("TEST<3>!\n");
            return 1;
        }
    }
    else
    {
        //	respond_fail(current_client,msg->ctag,0);
        return 1;
    }
    //	printf("TEST<4>\n");
    respond_success(current_client, msg->ctag);
    //save_config();
    return 0;
}
int cmd_set_dat_s(_MSG_NODE *msg, int flag)
{
    if((msg->data[0] != '1') && (msg->data[0] != '0'))
    {
        //printf("error dat\n");
        return 1;
    }
    conf_content.slot_u.time_source = msg->data[0];
    /*处理*/
    respond_success(current_client, msg->ctag);
    save_config();
    return 0;
}


//设置输出信号SSM 打开,闭塞
int cmd_set_out_ssm_en(_MSG_NODE *msg, int flag)
{
    int ret = 1;

    printf("SSM enable = %c\n", msg->data[0]);
    if(MCP_SLOT == msg->solt)
    {
        if(('0' == msg->data[0]) || ('1' == msg->data[0]))
        {
            conf_content.slot_u.out_ssm_en = msg->data[0];
            respond_success(current_client, msg->ctag);
            save_config();
            ret = 0;
        }
    }

    return ret;
}

//
//设置输出信号SSM 门限
//
int cmd_set_out_ssm_oth(_MSG_NODE *msg, int flag)
{
    int ret = 1;
    unsigned char sendbuf[SENDBUFSIZE];
    unsigned char temp = msg->data[0];

    printf("SSM threshold = %c\n", msg->data[0]);
    if(MCP_SLOT == msg->solt)
    {
        if((msg->data[0] >= 'a') && (msg->data[0] <= 'f'))
        {
            switch(msg->data[0])
            {
            case 'a':
                conf_content.slot_u.out_ssm_oth = 0x02;
                break;
            case 'b':
                conf_content.slot_u.out_ssm_oth = 0x04;
                break;
            case 'c':
                conf_content.slot_u.out_ssm_oth = 0x08;
                break;
            case 'd':
                conf_content.slot_u.out_ssm_oth = 0x0b;
                break;
            case 'e':
                conf_content.slot_u.out_ssm_oth = 0x0f;
                break;
            case 'f':
                conf_content.slot_u.out_ssm_oth = 0x00;
                break;
            }
            respond_success(current_client, msg->ctag);
            if('S' == slot_type[0])
            {
                memset(sendbuf, 0, sizeof(sendbuf));
                sprintf((char *)sendbuf, "SET-SSM-OTH:%s::%c;", "000000", temp);
                sendtodown(sendbuf, 'a');
            }
            if('S' == slot_type[1])
            {
                memset(sendbuf, 0, sizeof(sendbuf));
                sprintf((char *)sendbuf, "SET-SSM-OTH:%s::%c;", "000000", temp);
                sendtodown(sendbuf, 'b');
            }
            save_config();
            ret = 0;
        }
    }

    return ret;
}


int cmd_set_net(_MSG_NODE *msg, int flag)
{
    int len, i, j, i1, i2, i3, i4, j1, j2, j3, len_tmp;
    int ret;
    unsigned char  tmp[50];
    memset(tmp, '\0', 50);
	//
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    j1 = 0;
    j2 = 0;
    j3 = 0;

    if(msg->solt == MCP_SLOT)
    {
        len = strlen(msg->data);
        for(i = 0; i < len; i++)
        {
            if(msg->data[i] == ',')
            {
                j++;
                if(j == 1)
                {
                    i1 = i + 1;
                }
                if(j == 2)
                {
                    i2 = i + 1;
                }
                if(j == 3)
                {
                    i3 = i + 1;
                }
                if(j == 4)
                {
                    i4 = i + 1;
                }
            }
        }
        if(i4 == 0)
        {
            return 1;
        }
        //printf("#%s#,%d,%d,%d,%d\n",msg->data,i1,i2,i3,i4);
        if(msg->data[0] != ',')
        {
            memset(tmp, '\0', 50);
            len_tmp = i1 - 1;
            memcpy(tmp, msg->data, len_tmp);

            if(if_a_string_is_a_valid_ipv4_address(tmp) == 0)
            {
                //if(strcmp(tmp,conf_content.slot_u.ip )!=0)
                {
                    ret = API_Set_McpIp(tmp);
                    if(ret == 0)
                    {
                        memset(conf_content.slot_u.ip, '\0', 16);
                        memcpy(conf_content.slot_u.ip, tmp, len_tmp);
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
            else
            {
                return 1;
            }
        }
        if(msg->data[i2] != ',')
        {
            memset(tmp, '\0', 50);
            len_tmp = i3 - i2 - 1;
            memcpy(tmp, &msg->data[i2], len_tmp);

            if(if_a_string_is_a_valid_ipv4_address(tmp) == 0)
            {
                //if(strcmp(tmp,conf_content.slot_u.mask )!=0)
                {
                    ret = API_Set_McpMask(tmp);
                    if(ret == 0)
                    {
                        memset(conf_content.slot_u.mask, '\0', 16);
                        memcpy(conf_content.slot_u.mask, tmp, len_tmp);
                    }
                    else
                    {
                        return 1;
                    }
                }
            }
            else
            {
                return 1;
            }
        }
        if(msg->data[i1] != ',')
        {
            memset(tmp, '\0', 50);
            j = 0;
            len_tmp = i2 - i1 - 1;
            memcpy(tmp, &msg->data[i1], len_tmp);
            //printf("gggggggggate=%s\n",tmp);
            if(if_a_string_is_a_valid_ipv4_address(tmp) == 0)
            {

                for(i = 0; i < len_tmp; i++)
                {
                    if(tmp[i] == '.')
                    {
                        j++;
                        if(j == 1)
                        {
                            j1 = i;
                        }
                        if(j == 2)
                        {
                            j2 = i;
                        }
                        if(j == 3)
                        {
                            j3 = i;
                        }
                    }
                }


                ret = API_Set_McpGateWay(tmp);
                if(ret == 0)
                {
                    memset(conf_content.slot_u.gate, '\0', 16);
                    memcpy(conf_content.slot_u.gate, tmp, len_tmp);
                }
                else
                {
                    return 1;
                }


            }
            else
            {
                return 1;
            }
        }
        if(msg->data[i3] != ',')
        {
            memset(tmp, '\0', 50);
            len_tmp = i4 - i3 - 1;
            memcpy(tmp, &msg->data[i3], len_tmp);

            if(if_a_string_is_a_valid_ipv4_address(tmp) == 0)
            {
                memset(conf_content.slot_u.dns1, '\0', 16);
                memcpy(conf_content.slot_u.dns1, tmp, len_tmp);
            }
            else
            {
                return 1;
            }
        }
        if(msg->data[i4] != '\0')
        {
            memset(tmp, '\0', 50);
            len_tmp = len - i4;
            memcpy(tmp, &msg->data[i4], len_tmp);

            if(if_a_string_is_a_valid_ipv4_address(tmp) == 0)
            {
                memset(conf_content.slot_u.dns2, '\0', 16);
                memcpy(conf_content.slot_u.dns2, tmp, len_tmp);
            }
            else
            {
                return 1;
            }
        }
    }
    else
    {
        return 1;
    }

    respond_success(current_client, msg->ctag);

    save_config();
#if DEBUG_NET_INFO
    printf("%sip:%s\n", NET_INFO, conf_content.slot_u.ip);
    printf("%smask:%s\n", NET_INFO, conf_content.slot_u.mask);
    printf("%sgate:%s\n", NET_INFO, conf_content.slot_u.gate);
    printf("%sdns1:%s\n", NET_INFO, conf_content.slot_u.dns1);
    printf("%sdns2:%s\n", NET_INFO, conf_content.slot_u.dns2);
#endif

    return 0;
}
int cmd_set_mac(_MSG_NODE *msg, int flag)
{
    int ret;
    if(msg->solt == MCP_SLOT)
    {
        //	if(strcmp(msg->data,conf_content.slot_u.mac )!=0)
        {
            ret = API_Set_McpMacAddr(msg->data);
            if(ret == 0)
            {
                memset(conf_content.slot_u.mac, '\0', 16);
                memcpy(conf_content.slot_u.mac, msg->data, 17);
            }
            else
            {
                return 1;
            }

            respond_success(current_client, msg->ctag);
            save_config();
        }
    }
    else
    {
        return 1;
    }
    return 0;
}
int cmd_set_password(_MSG_NODE *msg, int flag)
{
    memset(password, '\0', 9);
    memcpy(password, msg->data, 8);

    respond_success(current_client, msg->ctag);
    save_config();
#if DEBUG_NET_INFO
    printf("%s:set_password:%s\n", NET_INFO, msg->data);
    printf("now,password:%s\n", password);
#endif
    return 0;
}
int cmd_set_mode(_MSG_NODE *msg, int flag)
{
    int len;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    len = strlen(msg->data);
    if(msg->solt == GBTP1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_q.gb_acmode,msg->data)!=0)
        //		{
        memset(conf_content.slot_q.mode, '\0', 4);
        memcpy(conf_content.slot_q.mode, msg->data, len);
        /*下发处理*/
        /*SET-MODE:<ctag>::<acmode>;*/
        sprintf((char *)sendbuf, "SET-MODE:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
        //		}
    }
    else if(msg->solt == GBTP2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_r.gb_acmode,msg->data)!=0)
        //		{
        memset(conf_content.slot_r.mode, '\0', 4);
        memcpy(conf_content.slot_r.mode, msg->data, len);

        sprintf((char *)sendbuf, "SET-MODE:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
        //		}
    }
    else
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_pos(_MSG_NODE *msg, int flag)
{
    int len;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    len = strlen(msg->data);
    if(msg->solt == GBTP1_SLOT)
    {
        //if(strcmp(rpt_content.slot_q.pos,msg->data)!=0)
        //	{
        memset(conf_content.slot_q.pos, '\0', 35);
        memcpy(conf_content.slot_q.pos, msg->data, len);
        /*下发*//* SET-POS:<ctag>::<postion>;*/
        sprintf((char *)sendbuf, "SET-POS:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
        //}
    }
    else if(msg->solt == GBTP2_SLOT)
    {
        //if(strcmp(conf_content.slot_r.pos,msg->data)!=0)
        //{
        memset(conf_content.slot_r.pos, '\0', 35);
        memcpy(conf_content.slot_r.pos, msg->data, len);

        sprintf((char *)sendbuf, "SET-POS:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
        //}
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_mask_gbtp(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    if((msg->data[0] != '0') && (msg->data[0] != '1'))
    {
        return 1;
    }
    if((msg->data[1] != '0') && (msg->data[1] != '1'))
    {
        return 1;
    }
    if(msg->solt == GBTP1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_q.gb_mask,msg->data)!=0)
        {
            memset(conf_content.slot_q.mask, '\0', 3);
            memcpy(conf_content.slot_q.mask, msg->data, 2);
            /*下发处理*//* SET-MASK:<ctag>::<mask>;*/
            sprintf((char *)sendbuf, "SET-MASK:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == GBTP2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_r.gb_mask,msg->data)!=0)
        {
            memset(conf_content.slot_r.mask, '\0', 3);
            memcpy(conf_content.slot_r.mask, msg->data, 2);

            sprintf((char *)sendbuf, "SET-MASK:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

int cmd_bdzb(_MSG_NODE *msg, int flag)
{
    int len = 0;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    if((strncmp1(msg->data, "BJ54", 4) != 0) && (strncmp1(msg->data, "CGS2000", 7) != 0))
    {
        return 1;
    }
    len = strlen(msg->data);
    if(msg->solt == GBTP1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_q.gb_mask,msg->data)!=0)
        {
            memset(conf_content.slot_q.bdzb, '\0', 8);
            memcpy(conf_content.slot_q.bdzb, msg->data, len);
            sprintf((char *)sendbuf, "SET-BDZB:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == GBTP2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_r.gb_mask,msg->data)!=0)
        {
            memset(conf_content.slot_r.bdzb, '\0', 8);
            memcpy(conf_content.slot_r.bdzb, msg->data, len);

            sprintf((char *)sendbuf, "SET-BDZB:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}


int cmd_sys_ref(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
	
	//
	//!Set sys ref: '0' Auto Select Sys Ref; '1' --- '8' Manual Select Sys Ref; 'F' -----NO Sys Ref;  
	//
    if((*msg->data < 0x30) || ((*msg->data > 0x38) && (*msg->data != 0x46)))
    {
        return 1;
    }
    if(msg->solt == RB1_SLOT)
    {
        //
        //!Main Slot Set Sys Ref.
        //
        conf_content.slot_o.sys_ref = *msg->data;
        //
        //!Send down Command。
        //
        sprintf((char *)sendbuf, "SET-SYS-REF:%s::INP-%c;", msg->ctag, *msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    
    }
    else if(msg->solt == RB2_SLOT)
    {
        //
        //!Slave Slot Set Sys Ref。
        //
        conf_content.slot_p.sys_ref = *msg->data;
        //
        //!Send down Command。
        //
        sprintf((char *)sendbuf, "SET-SYS-REF:%s::INP-%c;", msg->ctag, *msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }
	printf("%s:%s\n", "Set System Ref", sendbuf);
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_dely(_MSG_NODE *msg, int flag)
{
    int len, num;
    int temp;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    len = strlen(msg->data);
    if((msg->data[0] < 0x31) || (msg->data[0] > 0x38))
    {
        return 1;
    }
    temp = atoi(&msg->data[2]);
    if((temp < 0) || (temp > 99999999))
    {
        return 1;
    }
    if(msg->solt == RB1_SLOT)
    {
        num = (int)(msg->data[0] - 0x31);
        if((num >= 0x00) && (num < 4))
        {
            //if(strcmp(conf_content.slot_o.dely[num], &msg->data[2])!=0)
            //	{
            memset(conf_content.slot_o.dely[num], '\0', 9);
            memcpy(conf_content.slot_o.dely[num], &msg->data[2], len - 2);

            sprintf((char *)sendbuf, "SET-DELY:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //	}
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        num = (int)(msg->data[0] - 0x31);
        if((num >= 0x00) && (num < 4))
        {
            //if(strcmp(rpt_content.slot_p., &msg->data[2])!=0)
            //	{
            memset(conf_content.slot_p.dely[num], '\0', 9);
            memcpy(conf_content.slot_p.dely[num], &msg->data[2], len - 2);

            sprintf((char *)sendbuf, "SET-DELY:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //	}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_prio_inp(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int i;
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] > '0') && (msg->data[i] < '9'))
        {
        }
        else
        {
            return 1;
        }
    }
    if(msg->solt == RB1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_o.rb_prio, msg->data)!=0)
        {
            memset(conf_content.slot_o.priority, '\0', 9);
            memcpy(conf_content.slot_o.priority, msg->data, 8);
            /*下发*//*SET-PRIO-INP:<ctag>::<priority>;*/
            sprintf((char *)sendbuf, "SET-PRIO-INP:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_p.rb_prio, msg->data)!=0)
        {
            memset(conf_content.slot_p.priority, '\0', 9);
            memcpy(conf_content.slot_p.priority, msg->data, 8);
            /*下发*/
            sprintf((char *)sendbuf, "SET-PRIO-INP:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_mask_e1(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int i;
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] == '0') || (msg->data[i] == '1'))
        {}
        else
        {
            return 1;
        }
    }
    if(msg->solt == RB1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_o.rb_mask, msg->data)!=0)
        {
            memset(conf_content.slot_o.mask, '\0', 9);
            memcpy(conf_content.slot_o.mask, msg->data, 8);
            /*下发*//*SET-MASK-INP:<ctag>::<mask>;*/
            sprintf((char *)sendbuf, "SET-MASK-INP:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_p.rb_mask, msg->data)!=0)
        {
            memset(conf_content.slot_p.mask, '\0', 9);
            memcpy(conf_content.slot_p.mask, msg->data, 8);
            /*下发*/
            sprintf((char *)sendbuf, "SET-MASK-INP:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_leap_mask(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int i;
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 5; i++)
    {
        if((msg->data[i] == '0') || (msg->data[i] == '1'))
        {}
        else
        {
            return 1;
        }
    }
    if(msg->solt == RB1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_o.rb_leapmask, msg->data)!=0)
        {
            memset(conf_content.slot_o.leapmask, '\0', 6);
            memcpy(conf_content.slot_o.leapmask, msg->data, 5);
            /*下发*//*SET-LMAK-INP:<ctag>::<leapmask>;*/
            sprintf((char *)sendbuf, "SET-LMAK-INP:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_p.rb_leapmask, msg->data)!=0)
        {
            memset(conf_content.slot_p.leapmask, '\0', 6);
            memcpy(conf_content.slot_p.leapmask, msg->data, 5);
            /*下发*/
            sprintf((char *)sendbuf, "SET-LMAK-INP:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_tzo(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    if((msg->data[0] != '0') && (msg->data[0] != '1'))
    {
        return 1;
    }
    if((msg->data[2] < 0x40) || (msg->data[2] > 0x4c))
    {
        return 1;
    }
    if(msg->solt == RB1_SLOT)
    {
        //	if(strcmp(rpt_content.slot_o.rf_tzo,msg->data)!=0)
        {
            memset(conf_content.slot_o.tzo, '\0', 4);
            memcpy(conf_content.slot_o.tzo, msg->data, 3);
            sprintf((char *)sendbuf, "SET-TZO:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }

    }
    else if(msg->solt == RB2_SLOT)
    {
        //	if(strcmp(rpt_content.slot_p.rf_tzo,msg->data)!=0)
        {
            memset(conf_content.slot_p.tzo, '\0', 4);
            memcpy(conf_content.slot_p.tzo, msg->data, 3);
            sprintf((char *)sendbuf, "SET-TZO:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_ptp_net(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3, i4, i5, i6;
    int len = strlen(msg->data);
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[2] > '0') && (msg->data[2] < '5'))
        {
            //	memset(conf_content.slot[num].tp16_en,'\0',17);
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            //	if(strcmp(conf_content.slot[num].ptp_net[port],msg->data)!=0)
            //		{
            //memset(conf_content.slot[num].ptp_net[port],'\0',99);
            //memcpy(conf_content.slot[num].ptp_net[port],msg->data,len);
            /*SET-PTP-NET:<type>,<response message>;*/

            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                    if(j == 5)
                    {
                        i4 = i + 1;
                    }
                    if(j == 6)
                    {
                        i5 = i + 1;
                    }
                    if(j == 7)
                    {
                        i6 = i + 1;
                    }
                }
            }
            if(i6 == 0)
            {
                return 1;
            }
            conf_content.slot[num].ptp_type = msg->data[0];
            if(msg->data[i1] != ',')
            {
                memcpy(conf_content.slot[num].ptp_ip[port], &msg->data[i1], i2 - i1 - 1);
            }
            if(msg->data[i2] != ',')
            {
                memcpy(conf_content.slot[num].ptp_mac[port], &msg->data[i2], i3 - i2 - 1);
            }
            if(msg->data[i3] != ',')
            {
                memcpy(conf_content.slot[num].ptp_gate[port], &msg->data[i3], i4 - i3 - 1);
            }
            if(msg->data[i4] != ',')
            {
                memcpy(conf_content.slot[num].ptp_mask[port], &msg->data[i4], i5 - i4 - 1);
            }
            if(msg->data[i5] != ',')
            {
                memcpy(conf_content.slot[num].ptp_dns1[port], &msg->data[i5], i6 - i5 - 1);
            }
            if(msg->data[i6] != '\0')
            {
                memcpy(conf_content.slot[num].ptp_dns2[port], &msg->data[i6], len - i6);
            }

            sprintf((char *)sendbuf, "SET-PTP-NET:%s;", msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //		}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_ptp_mod(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13;
    int len = strlen(msg->data);
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
    i7 = 0;
    i8 = 0;
    i9 = 0;
    i10 = 0;
    i11 = 0;
    i12 = 0;
    i13 = 0;
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[2] > '0') && (msg->data[2] < '5'))
        {
            //memset(conf_content.slot[num].tp16_en,'\0',17);
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            //if(strcmp(conf_content.slot[num].ptp_mod[port],msg->data)!=0)
            //	{
            //		memset(conf_content.slot[num].ptp_mod[port],'\0',34);
            //		memcpy(conf_content.slot[num].ptp_mod[port],msg->data,len);
            /*SET-PTP-MOD:<type>,<response message>;*/
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                    if(j == 5)
                    {
                        i4 = i + 1;
                    }
                    if(j == 6)
                    {
                        i5 = i + 1;
                    }
                    if(j == 7)
                    {
                        i6 = i + 1;
                    }
                    if(j == 8)
                    {
                        i7 = i + 1;
                    }
                    if(j == 9)
                    {
                        i8 = i + 1;
                    }
                    if(j == 10)
                    {
                        i9 = i + 1;
                    }
                    if(j == 11)
                    {
                        i10 = i + 1;
                    }
                    if(j == 12)
                    {
                        i11 = i + 1;
                    }
                    if(j == 13)
                    {
                        i12 = i + 1;
                    }
                    if(j == 14)
                    {
                        i13 = i + 1;    //2014.12.2
                    }
                }
            }
            if(i12 == 0)
            {
                return 1;
            }
            conf_content.slot[num].ptp_type = msg->data[0];
            if((msg->data[i1] == '0') || (msg->data[i1] == '1'))
            {
                conf_content.slot[num].ptp_en[port] = msg->data[i1];
            }
            if((msg->data[i2] == '0') || (msg->data[i2] == '1'))
            {
                conf_content.slot[num].ptp_delaytype[port] = msg->data[i2];
            }
            if((msg->data[i3] == '0') || (msg->data[i3] == '1'))
            {
                //printf("ptp set %c\n",msg->data[i3]);
                conf_content.slot[num].ptp_multicast[port] = msg->data[i3];
            }
            if((msg->data[i4] == '0') || (msg->data[i4] == '1'))
            {
                conf_content.slot[num].ptp_enp[port] = msg->data[i4];
            }
            if((msg->data[i5] == '0') || (msg->data[i5] == '1'))
            {
                conf_content.slot[num].ptp_step[port] = msg->data[i5];
            }
            if(msg->data[i6] != ',')
            {
                memcpy(conf_content.slot[num].ptp_sync[port], &msg->data[i6], i7 - i6 - 1);
            }
            if(msg->data[i7] != ',')
            {
                memcpy(conf_content.slot[num].ptp_announce[port], &msg->data[i7], i8 - i7 - 1);
            }
            if(msg->data[i8] != ',')
            {
                memcpy(conf_content.slot[num].ptp_delayreq[port], &msg->data[i8], i9 - i8 - 1);
            }
            if(msg->data[i9] != ',')
            {
                memcpy(conf_content.slot[num].ptp_pdelayreq[port], &msg->data[i9], i10 - i9 - 1);
            }
            if(msg->data[i10] != ',')
            {
                memset(conf_content.slot[num].ptp_delaycom[port],'\0',9);//ADD PTP4
                memcpy(conf_content.slot[num].ptp_delaycom[port], &msg->data[i10], i11 - i10 - 1);
            }
            if((msg->data[i11] == '0') || (msg->data[i11] == '1'))
            {
                conf_content.slot[num].ptp_linkmode[port] = msg->data[i11];
            }
            if((msg->data[i12] == '0') || (msg->data[i12] == '1'))
            {
                conf_content.slot[num].ptp_out[port] = msg->data[i12];
            }
            //2014.12.2
            if(i13 != 0 && msg->data[i13] != '\0')
            {
                if(msg->data[i13] == 'A')
                {
                    memset(conf_content3.ptp_prio1[num][port], 0, 5);
                    memcpy(conf_content3.ptp_prio1[num][port], (msg->data) + i13, len - i13);
                    //prio = 0;
                }
                else if(msg->data[i13] == 'B')
                {
                    memset(conf_content3.ptp_prio2[num][port], 0, 5);
                    memcpy(conf_content3.ptp_prio2[num][port], (msg->data) + i13, len - i13);
                }
                else
                {
                    return 1;
                }
            }

            //printf("\n\nLUOHAO~~~~~~~~~2,conf_content.slot[%d].ptp_multicast[%d] = %d\n\n",num,port,conf_content.slot[num].ptp_multicast[port]);

            sprintf((char *)sendbuf, "SET-PTP-MOD:%s;", msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //	}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}


int cmd_set_ptp_mtc(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num,i;
    int port;
	int ip_len;
	int max_ip_len = sizeof( conf_content3.ptp_mtc1_ip[0][0]);
	
	ip_len = strlen(msg->data)-4;
	//
    //!if no ip set,return;one ip address 8 byte;
    //
	if(ip_len < 8 || ip_len > max_ip_len)
	return 1;

	//
	//!check ip array is valid.
	//
	for(i = 9; i < ip_len; )
	{
       if(msg->data[i+3] != '.')
	   	 return 1;
	   else
	   	 i += 9;
	}
	
    memset(sendbuf, '\0', SENDBUFSIZE);

    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[2] > '0') && (msg->data[2] < '5'))
        {
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            if(msg->data[4] != '\0')
            {
                memset(conf_content3.ptp_mtc1_ip[num][port], '\0', max_ip_len);
                memcpy(conf_content3.ptp_mtc1_ip[num][port], &msg->data[4], ip_len);
				sprintf((char *)sendbuf, "SET-PTP-MTC:%s;", msg->data);
				sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            return 0;

        }
		return 1;
    }
    else
    {
        return 1;
    }
}
int cmd_set_ptp_dom(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
	int i;
	
    memset(sendbuf, '\0', SENDBUFSIZE);

    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        
        if((msg->data[2] > '0') && (msg->data[2] < '5'))
        {
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            if(msg->data[4] != ';')
            {
                //printf("set ptp domA:%s \n",msg->data);
                for(i = 4; msg->data[i] != '\0' ; i++ )
                {
                    if(i >= 7)
						return 1;
                }
				//printf("set ptp domB:%s \n",msg->data);
                memset(conf_content3.ptp_dom[num][port], 0, 4);
                memcpy(conf_content3.ptp_dom[num][port], &msg->data[4], i - 4);
                sprintf((char *)sendbuf, "SET-PTP-DOM:%s;", msg->data);

                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            return 0;

        }
		return 1;
    }
    else
    {
        return 1;
    }
}

/*
int cmd_set_ptp_out(_MSG_NODE * msg,int flag)
{
	unsigned char sendbuf[SENDBUFSIZE];
	int num;
	int port;
	memset(sendbuf,'\0',SENDBUFSIZE);
	if(((msg->solt)>'`')&&((msg->solt)<'o'))
		{
			if((msg->data[2]>'0')&&(msg->data[2]<'5'))
				{
					num=(int)(msg->solt-0x61);
					port=(int)(msg->data[2]-0x31);

					if((msg->data[4]=='1' )||(msg->data[4]=='0'))
						{
							conf_content.slot[num].ptp_out[port]=msg->data[4];
						}
					else
						return 1;


					sprintf((char *)sendbuf,"SET-PTP-OUT:%s;",msg->data);
					if(flag)
						{
							sendtodown_cli(sendbuf,msg->solt,msg->ctag);
							save_config();
						}
					else
						sendtodown(sendbuf, msg->solt);

				}
			else
				return 1;
		}
	else
		return 1;

	return 0;
}
*/






int cmd_set_out_lev(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    unsigned char tmp_ctag[7];
    int num;

    memset(sendbuf, '\0', SENDBUFSIZE);
    memset(tmp_ctag, 0, 7);
    memcpy(tmp_ctag, "000000", 6);
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memset(conf_content.slot[num].out_lev, '\0', 3);
        memcpy(conf_content.slot[num].out_lev, msg->data, 2);
        //SET-OUT-LEV:<ctag>::<level>;
        sprintf((char *)sendbuf, "SET-OUT-LEV:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    //ext
    else if((msg->solt >= EXT_1_OUT_LOWER && msg->solt <= EXT_1_OUT_UPPER) ||
            (msg->solt >= EXT_2_OUT_LOWER && msg->solt <= EXT_2_OUT_UPPER) ||
            (msg->solt >= EXT_3_OUT_LOWER && msg->solt <= EXT_3_OUT_UPPER && msg->solt != ':' && msg->solt != ';') )
    {
        if(msg->solt >= EXT_1_OUT_LOWER && msg->solt <= EXT_1_OUT_UPPER)
        {
            memcpy(gExtCtx.out[msg->solt - EXT_1_OUT_OFFSET].outSsm, msg->data, 2);
            memcpy(gExtCtx.save.outSsm[msg->solt - EXT_1_OUT_OFFSET], msg->data, 2);
        }
        else if(msg->solt >= EXT_2_OUT_LOWER && msg->solt <= EXT_2_OUT_UPPER)
        {
            memcpy(gExtCtx.out[msg->solt - EXT_2_OUT_OFFSET].outSsm, msg->data, 2);
            memcpy(gExtCtx.save.outSsm[msg->solt - EXT_2_OUT_OFFSET], msg->data, 2);
        }
        else
        {
            if(msg->solt < ':')
            {
                memcpy(gExtCtx.out[msg->solt - EXT_3_OUT_OFFSET].outSsm, msg->data, 2);
                memcpy(gExtCtx.save.outSsm[msg->solt - EXT_3_OUT_OFFSET], msg->data, 2);
            }
            else
            {
                memcpy(gExtCtx.out[msg->solt - EXT_3_OUT_OFFSET - 2].outSsm, msg->data, 2);
                memcpy(gExtCtx.save.outSsm[msg->solt - EXT_3_OUT_OFFSET - 2], msg->data, 2);
            }
        }

        if(flag)
        {
            sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;", msg->solt, msg->ctag, msg->data);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, msg->ctag))
            {
                return 1;
            }
        }
        else
        {
            sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;", msg->solt, tmp_ctag, msg->data);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, tmp_ctag))
            {
                return 1;
            }
        }

        if(0 == ext_write_flash(&(gExtCtx.save)))
        {
            return 1;
        }
    }
    else
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif

    return 0;
}








int cmd_set_out_ppr(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num;
    unsigned char tmp_ctag[7];

    memset(sendbuf, '\0', SENDBUFSIZE);
    memset(tmp_ctag, 0, 7);
    memcpy(tmp_ctag, "000000", 6);
    if( ((msg->solt) > '`') && ((msg->solt) < 'o') )
    {
        num = (int)(msg->solt - 0x61);
        sprintf((char *)sendbuf, "SET-OUT-PRR:%s::;", msg->ctag);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    //ext
    else if((msg->solt >= EXT_1_OUT_LOWER && msg->solt <= EXT_1_OUT_UPPER) ||
            (msg->solt >= EXT_2_OUT_LOWER && msg->solt <= EXT_2_OUT_UPPER) ||
            (msg->solt >= EXT_3_OUT_LOWER && msg->solt <= EXT_3_OUT_UPPER && msg->solt != ':' && msg->solt != ';') )
    {
        if(flag)
        {
            sprintf((char *)sendbuf, "SET-OUT-PRR:%c:%s::;", msg->solt, msg->ctag);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, msg->ctag))
            {
                return 1;
            }
        }
        else
        {
            sprintf((char *)sendbuf, "SET-OUT-PRR:%c:%s::;", msg->solt, tmp_ctag);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, tmp_ctag))
            {
                return 1;
            }
        }
    }
    else
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif

    return 0;
}



int cmd_set_out_typ(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num, i;
    unsigned char tmp_ctag[7];

    memset(tmp_ctag, 0, 7);
    memcpy(tmp_ctag, "000000", 6);
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        for(i = 0; i < 16; i++)
        {
            if((msg->data[i] < '0') || (msg->data[i] > '2'))
            {
                return 1;
            }
        }

        memset(conf_content.slot[num].out_typ, '\0', 17);
        memcpy(conf_content.slot[num].out_typ, msg->data, 16);
        /*SET-OUT-TYP:<ctag>::<level>;*/
        sprintf((char *)sendbuf, "SET-OUT-TYP:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else if((msg->solt >= EXT_1_OUT_LOWER && msg->solt <= EXT_1_OUT_UPPER) ||
            (msg->solt >= EXT_2_OUT_LOWER && msg->solt <= EXT_2_OUT_UPPER) ||
            (msg->solt >= EXT_3_OUT_LOWER && msg->solt <= EXT_3_OUT_UPPER && msg->solt != ':' && msg->solt != ';') )
    {
        for(i = 0; i < 16; i++)
        {
            if( (msg->data[i] < '0') || (msg->data[i] > '2') )
            {
                return 1;
            }
        }

        if(0 == ext_out_signal_crpt(1, msg->solt, msg->data, &gExtCtx))
        {
            return 1;
        }

        if(flag)
        {
            sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;", msg->solt, msg->ctag, msg->data);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, msg->ctag))
            {
                return 1;
            }
        }
        else
        {
            sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;", msg->solt, tmp_ctag, msg->data);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, tmp_ctag))
            {
                return 1;
            }
        }

        if(0 == ext_write_flash(&(gExtCtx.save)))
        {
            return 1;
        }
    }
    else
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif

    return 0;
}





int cmd_set_out_mod(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num;
    unsigned char tmp_ctag[7];

    memset(sendbuf, '\0', SENDBUFSIZE);
    memset(tmp_ctag, 0, 7);
    memcpy(tmp_ctag, "000000", 6);

    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[0] == '1') || (msg->data[0] == '0'))
        {
            num = (msg->solt) - 'a';
            conf_content.slot[num].out_mod = msg->data[0];
            sprintf((char *)sendbuf, "SET-OUT-MOD:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
        else
        {
            return 1;
        }
    }
    //ext
    else if((msg->solt >= EXT_1_OUT_LOWER && msg->solt <= EXT_1_OUT_UPPER) ||
            (msg->solt >= EXT_2_OUT_LOWER && msg->solt <= EXT_2_OUT_UPPER) ||
            (msg->solt >= EXT_3_OUT_LOWER && msg->solt <= EXT_3_OUT_UPPER && msg->solt != ':' && msg->solt != ';') )
    {
        if(msg->solt >= EXT_1_OUT_LOWER && msg->solt <= EXT_1_OUT_UPPER)
        {
            gExtCtx.out[msg->solt - EXT_1_OUT_OFFSET].outPR[0] = msg->data[0];
            gExtCtx.save.outPR[msg->solt - EXT_1_OUT_OFFSET][0] = msg->data[0];
        }
        else if(msg->solt >= EXT_2_OUT_LOWER && msg->solt <= EXT_2_OUT_UPPER)
        {
            gExtCtx.out[msg->solt - EXT_2_OUT_OFFSET].outPR[0] = msg->data[0];
            gExtCtx.save.outPR[msg->solt - EXT_2_OUT_OFFSET][0] = msg->data[0];
        }
        else
        {
            if(msg->solt < ':')//except : ;
            {
                gExtCtx.out[msg->solt - EXT_3_OUT_OFFSET].outPR[0] = msg->data[0];
                gExtCtx.save.outPR[msg->solt - EXT_3_OUT_OFFSET][0] = msg->data[0];
            }
            else
            {
                gExtCtx.out[msg->solt - EXT_3_OUT_OFFSET - 2].outPR[0] = msg->data[0];
                gExtCtx.save.outPR[msg->solt - EXT_3_OUT_OFFSET - 2][0] = msg->data[0];
            }
        }

        if(flag)
        {
            sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%s;", msg->solt, msg->ctag, msg->data);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, msg->ctag))
            {
                return 1;
            }
        }
        else
        {
            sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%s;", msg->solt, tmp_ctag, msg->data);
            if(0 == ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, msg->solt, tmp_ctag))
            {
                return 1;
            }
        }

        if(0 == ext_write_flash(&(gExtCtx.save)))
        {
            return 1;
        }
    }
    else
    {
        return 1;
    }

    return 0;
}





int cmd_set_reset(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    if((msg->solt == ':') || (msg->solt == 'u'))
    {
        /****reboot*/
        memcpy(sendbuf, "RESET", 5);
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        respond_success(current_client, msg->ctag);

    }
    else if((msg->solt > '`') && (msg->solt < 's'))
    {
        sprintf((char *)sendbuf, "SET-RESET:%s::;", msg->ctag);
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
    }
    else if(msg->solt == 'v')
    {}
    else if((msg->solt == 's') || ((msg->solt == 't')))
    {}
    else
    {
        return 1;
    }
    /*SET-RESET:<ctag>::;*/
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_leap_tag(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(msg->solt == RB1_SLOT)
    {
        //	if(rpt_content.slot_o.rb_leaptag!=*msg->data)
        {
            conf_content.slot_o.leaptag = *msg->data;
            /*SET-LEAP:<ctag>::<leaptag>;*/
            sprintf((char *)sendbuf, "SET-LEAP:%s::%c;", msg->ctag, *msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        //	if(rpt_content.slot_p.rb_leaptag!=*msg->data)
        {
            conf_content.slot_p.leaptag = *msg->data;

            sprintf((char *)sendbuf, "SET-LEAP:%s::%c;", msg->ctag, *msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
int cmd_set_tp16_en(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num = 0, i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    //	memset(tmp,'\0',sizeof(unsigned char) * 17);
    for(i = 0; i < 16; i++)
    {
        if((msg->data[i] != '0') && (msg->data[i] != '1'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        //memset(&conf_content.slot[num],'\0',sizeof(out_content));
        num = (int)(msg->solt - 0x61);
        //	if(strcmp(rpt_content.slot[num].tp16_en, msg->data)!=0)
        //		{
        //memset(conf_content.slot[num].tp16_en, '\0', 17);
        //			for(i=0;i<16;i++)
        //				tmp[i]=msg->data[15-i];
        memcpy(conf_content.slot[num].tp16_en, msg->data, 16);
        /*SET-TP16-EN::<ctag>::<en>;*/
        sprintf((char *)sendbuf, "SET-TP16-EN:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
        //		}
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    //	free(tmp);
    return 0;
}
int cmd_set_leap_num(_MSG_NODE *msg, int flag)
{
    int num, i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    num = atoi(msg->data);
    if(strncmp1(msg->data, "00", 2) == 0)
    {}
    else if((num < 1) || (num > 99))
    {
        return 1;
    }
    if((msg->solt == ':') || (msg->solt == 'u'))
    {
        memset(conf_content.slot_u.leap_num, '\0', 3);
        memcpy(conf_content.slot_u.leap_num, msg->data, 2);

        sprintf((char *)sendbuf, "SET-LEAP-NUM:%s::%s;", msg->ctag, msg->data);
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();

        for(i = 0; i < 14; i++)
        {
            if(slot_type[i] == 'I')
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-TP16-LEAP:%s::%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
            }
            else if((slot_type[i] > '@') && (slot_type[i] < 'I'))
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PTP-LEAP:%s::%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
            }
			else if(slot_type[i] == 'j')
			{
			    memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PTP-LEAP:%s::%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
			}
            else if(slot_type[i] == 'T' || slot_type[i] == 'X')
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-RS-TZO:%s::,%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
            }
            else if(slot_type[i] == 'V')
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-TOD-PRO:%s::,,%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
            }
            else if(slot_type[i] == 'W')
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-IGB-LEAP:%s::%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
            }
            else if(slot_type[i] == 'Z')
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-NTP-LEAP:%s::%s;", msg->ctag, msg->data);
                sendtodown(sendbuf, i + 0x61);
            }
        }
		//if the online pan type at the 15 slot, it need to send the leap value to the 15 slot.the 16 slot as the up.
		//
		if((slot_type[14] == 'R') | (slot_type[14] == 'K'))
		{
		    memset(sendbuf, '\0', SENDBUFSIZE);
            sprintf((char *)sendbuf, "SET-RB-LEAP:%s::%s;", msg->ctag, msg->data);
            sendtodown(sendbuf, 14 + 0x61);
		}
		if((slot_type[15] == 'R') | (slot_type[15] == 'K'))
		{
		    memset(sendbuf, '\0', SENDBUFSIZE);
            sprintf((char *)sendbuf, "SET-RB-LEAP:%s::%s;", msg->ctag, msg->data);
            sendtodown(sendbuf, 15 + 0x61);
		}
    }
    else
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, conf_content.slot_u.leap_num);
    }
#endif
    return 0;
}



int cmd_rtrv_alrms(int client, _MSG_NODE *msg)
{
    int send_ok, num;
    unsigned char sendbuf[1024];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    num = (int)(msg->solt - 'a');
    get_format(sendbuf, msg->ctag);
    get_alm(msg->solt, sendbuf);
    //printf("<%s>:%s\n",__FUNCTION__,sendbuf);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }

    return 0;
}




/*
  0	成功
  1	失败
*/
int cmd_ext_alm(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);
    get_ext_alm(msg->solt, sendbuf);
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}




/*
  0	成功
  1	失败
*/
int cmd_ext1_alm(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);
    get_ext1_alm(msg->solt, sendbuf);
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}





/*
  0	成功
  1	失败
*/
int cmd_ext2_alm(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);
    get_ext2_alm(msg->solt, sendbuf);
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}





/*
  0	成功
  1	失败
*/
int cmd_ext3_alm(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);
    get_ext3_alm(msg->solt, sendbuf);
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}







/*
  0	成功
  1	失败
*/
int cmd_ext1_prs(int client, _MSG_NODE *msg)
{
    u8_t sendbuf[1024];
    int i, ret;

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);

    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);

    for(i = 0; i < 10; i++)
    {
        if( (EXT_OUT16 == gExtCtx.extBid[0][i]) ||
                (EXT_OUT32 == gExtCtx.extBid[0][i]) ||
                (EXT_OUT16S == gExtCtx.extBid[0][i]) ||
                (EXT_OUT32S == gExtCtx.extBid[0][i]) )
        {
            sprintf((char *)sendbuf, "%s%c,%s",
                    sendbuf,
                    gExtCtx.extBid[0][i],
                    ((0 == strlen(gExtCtx.out[i].outPR)) ? (char *)"," : (char *)(gExtCtx.out[i].outPR)));
        }
        else
        {
            sprintf((char *)sendbuf, "%s%c,%s",
                    sendbuf,
                    gExtCtx.extBid[0][i],
                    ",");
        }

        if(i < 9)
        {
            strcat(sendbuf, ":");
        }
    }

    strcat(sendbuf, "\"\r\n;");
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}







/*
  0	成功
  1	失败
*/
int cmd_ext2_prs(int client, _MSG_NODE *msg)
{
    u8_t sendbuf[1024];
    int i, ret;

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);

    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);

    for(i = 0; i < 10; i++)
    {
        if( (EXT_OUT16 == gExtCtx.extBid[1][i]) ||
                (EXT_OUT32 == gExtCtx.extBid[1][i]) ||
                (EXT_OUT16S == gExtCtx.extBid[1][i]) ||
                (EXT_OUT32S == gExtCtx.extBid[1][i]) )
        {
            sprintf((char *)sendbuf, "%s%c,%s",
                    sendbuf,
                    gExtCtx.extBid[1][i],
                    ((0 == strlen(gExtCtx.out[10 + i].outPR)) ? (char *)"," : (char *)(gExtCtx.out[10 + i].outPR)));
        }
        else
        {
            sprintf((char *)sendbuf, "%s%c,%s",
                    sendbuf,
                    gExtCtx.extBid[1][i],
                    ",");
        }

        if(i < 9)
        {
            strcat(sendbuf, ":");
        }
    }

    strcat(sendbuf, "\"\r\n;");
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}







/*
  0	成功
  1	失败
*/
int cmd_ext3_prs(int client, _MSG_NODE *msg)
{
    u8_t sendbuf[1024];
    int i, ret;

    memset(sendbuf, 0, 1024);
    get_format(sendbuf, msg->ctag);

    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);

    for(i = 0; i < 10; i++)
    {
        if( (EXT_OUT16 == gExtCtx.extBid[2][i]) ||
                (EXT_OUT32 == gExtCtx.extBid[2][i]) ||
                (EXT_OUT16S == gExtCtx.extBid[2][i]) ||
                (EXT_OUT32S == gExtCtx.extBid[2][i]) )
        {
            sprintf((char *)sendbuf, "%s%c,%s",
                    sendbuf,
                    gExtCtx.extBid[2][i],
                    ((0 == strlen(gExtCtx.out[20 + i].outPR)) ? (char *)"," : (char *)(gExtCtx.out[20 + i].outPR)));
        }
        else
        {
            sprintf((char *)sendbuf, "%s%c,%s",
                    sendbuf,
                    gExtCtx.extBid[2][i],
                    ",");
        }

        if(i < 9)
        {
            strcat(sendbuf, ":");
        }
    }

    strcat(sendbuf, "\"\r\n;");
    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}








/*
  0	成功
  1	失败
*/
int cmd_drv_mgr(int client, _MSG_NODE *msg)
{
    u8_t sendbuf[1024];
    int ret;
    unsigned char drvPr[2][3];
    unsigned char mgrPr[6];

    memset(drvPr, 0, sizeof(drvPr));
    memset(mgrPr, 0, sizeof(mgrPr));
    memset(sendbuf, 0, 1024);

    drvPr[0][0] = gExtCtx.drv[0].drvPR[0];
    drvPr[1][0] = gExtCtx.drv[1].drvPR[0];
    drvPr[0][1] = gExtCtx.drv[0].drvPR[1];
    drvPr[1][1] = gExtCtx.drv[1].drvPR[1];
    drvPr[0][2] = gExtCtx.drv[0].drvPR[2];
    drvPr[1][2] = gExtCtx.drv[1].drvPR[2];

    mgrPr[0] = gExtCtx.mgr[0].mgrPR;
    mgrPr[1] = gExtCtx.mgr[1].mgrPR;
    mgrPr[2] = gExtCtx.mgr[2].mgrPR;
    mgrPr[3] = gExtCtx.mgr[3].mgrPR;
    mgrPr[4] = gExtCtx.mgr[4].mgrPR;
    mgrPr[5] = gExtCtx.mgr[5].mgrPR;

    get_format(sendbuf, msg->ctag);

    if( (((alm_sta[12][5] & 0x01) == 0x01) && (slot_type[12] == 'd') )
            || ( ((alm_sta[13][5] & 0x01) == 0x01) && (slot_type[13] == 'd') )
            || ((gExtCtx.save.onlineSta & 0x01) == 0))
    {
        drvPr[0][0] = 4;
        drvPr[1][0] = 4;
        mgrPr[0] = 4;
        mgrPr[1] = 4;
    }


    if( (((alm_sta[12][5] & 0x02) == 0x02) && (slot_type[12] == 'd'))
            || ( ((alm_sta[13][5] & 0x02) == 0x02) && (slot_type[13] == 'd') )
            || ((gExtCtx.save.onlineSta & 0x02) == 0x00))
    {
        drvPr[0][1] = 4;
        drvPr[1][1] = 4;
        mgrPr[2] = 4;
        mgrPr[3] = 4;
    }


    if( (((alm_sta[12][5] & 0x04) == 0x04) && (slot_type[12] == 'd'))
            || ( ((alm_sta[13][5] & 0x04) == 0x04) && (slot_type[13] == 'd') )
            || ((gExtCtx.save.onlineSta & 0x04) == 0x00))
    {
        drvPr[0][2] = 4;
        drvPr[1][2] = 4;
        mgrPr[4] = 4;
        mgrPr[5] = 4;
    }

    sprintf((char *)sendbuf, "%s   \"%d%d%d%d%d%d,%d%d%d%d%d%d\"\r\n;",
            sendbuf,
            drvPr[0][0], drvPr[1][0],
            drvPr[0][1], drvPr[1][1],
            drvPr[0][2], drvPr[1][2],
            mgrPr[0], mgrPr[1],
            mgrPr[2], mgrPr[3],
            mgrPr[4], mgrPr[5] );


    /*
    	sprintf((char *)sendbuf, "%s   \"%d%d%d%d%d%d,%d%d%d%d%d%d\"\r\n;",
    					 sendbuf,
    					 gExtCtx.drv[0].drvPR[0], gExtCtx.drv[1].drvPR[0],
    					 gExtCtx.drv[0].drvPR[1], gExtCtx.drv[1].drvPR[1],
    					 gExtCtx.drv[0].drvPR[2], gExtCtx.drv[1].drvPR[2],
    					 gExtCtx.mgr[0].mgrPR, gExtCtx.mgr[1].mgrPR,
    					 gExtCtx.mgr[2].mgrPR, gExtCtx.mgr[3].mgrPR,
    					 gExtCtx.mgr[4].mgrPR, gExtCtx.mgr[5].mgrPR );
    */

    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}








int cmd_rtrv_out(int client , _MSG_NODE *msg  )
{
    unsigned char sendbuf[1024];
    int send_ok, i;
    unsigned char real_rpt;

    memset(sendbuf, '\0', 1024);
    get_format(sendbuf, msg->ctag);
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);
    for(i = 0; i < 14; i++)
    {
        if( (slot_type[i] == 'J') ||
                (slot_type[i] == 'a' ) ||
                (slot_type[i] == 'b') ||
                (slot_type[i] == 'c') ||
                (slot_type[i] == 'h') ||
                (slot_type[i] == 'i') )
        {
            if('0' == rpt_content.slot[i].out_mode)
            {
                real_rpt = '2';
            }
            else
            {
                real_rpt = rpt_content.slot[i].out_sta;
            }
            sprintf((char *)sendbuf, "%s%c%c", sendbuf, slot_type[i], real_rpt);
        }
        if(i < 13)
        {
            strncatnew(sendbuf, ",", 1);
        }
    }

    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        /*	close(client);
        FD_CLR(client, &allset);
        client= -1;*/
        printf("timeout_<4>!\n");
    }

    return 0;
	
}


int cmd_set_CONFIG(_MSG_NODE *msg, int flag)
{
    unsigned char buf[15] = "SET-CONFIG:::;";
    if(file_exists(CONFIG_FILE))
    {
        printf("CONFIG wait\n");
        sendtodown_cli(buf, msg->solt, msg->ctag);
    }
    else
    {
        return 1;
    }
    return 0;
}


int cmd_rtrv_eqpt(int client , _MSG_NODE *msg  )
{
    int send_ok;
    unsigned char sendbuf[1024];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    get_format(sendbuf, msg->ctag);
    get_data(msg->solt, sendbuf);

    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    //printf("\n\n%s SEND: %s\n\n", __func__, sendbuf);
    if(send_ok == -1 && errno == EAGAIN)
    {
        /*	close(client);
        	FD_CLR(client, &allset);
        	client= -1;*/
        printf("timeout_<4>!\n");
    }
    return 0;
}





/*
  0	成功
  1	失败
*/
int cmd_ext1_eqt(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);

    get_format(sendbuf, msg->ctag);
    get_ext1_eqt(msg->solt, sendbuf);

    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}





/*
  0	成功
  1	失败
*/
int cmd_ext2_eqt(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);

    get_format(sendbuf, msg->ctag);
    get_ext2_eqt(msg->solt, sendbuf);

    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}





/*
  0	成功
  1	失败
*/
int cmd_ext3_eqt(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);

    get_format(sendbuf, msg->ctag);
    get_ext3_eqt(msg->solt, sendbuf);

    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}

/*
  0	成功
  1	失败
*/
int cmd_ext_eqt(int client, _MSG_NODE *msg)
{
    int ret;
    u8_t sendbuf[1024];

    memset(sendbuf, 0, 1024);

    get_format(sendbuf, msg->ctag);
	get_exta1_eqt(msg->solt, sendbuf);
	get_exta2_eqt(msg->solt, sendbuf);
    get_exta3_eqt(msg->solt, sendbuf);

    ret = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(ret == -1 && errno != EAGAIN)
    {
        return 1;
    }

    return 0;
}



int cmd_rtrv_pm_inp(int client, _MSG_NODE *msg )
{
    //unsigned char  *sendbuf=(unsigned char *) malloc ( sizeof(unsigned char) * 1536);
    unsigned char sendbuf[1536];

    int i, send_ok;
    memset(sendbuf, '\0', sizeof(unsigned char) * 1536);

    get_format(sendbuf, msg->ctag);
    /*	<cr><lf><lf>      ^^^<sid>^<date>^<time><cr><lf>
    M^^<catg>^COMPLD<cr><lf>
    [^^^<response message><cr><lf>]*
        ;*/
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);
    if(msg->solt == RB1_SLOT)
    {
        for(i = 0; i < 10; i++)
        {
            strncatnew(sendbuf, rpt_content.slot_o.rb_inp[i], strlen(rpt_content.slot_o.rb_inp[i]));
        }
    }
    else if(msg->solt == RB2_SLOT)
    {
        for(i = 0; i < 10; i++)
        {
            strncatnew(sendbuf, rpt_content.slot_p.rb_inp[i], strlen(rpt_content.slot_p.rb_inp[i]));
        }
    }
    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        /*	close(client);
        	FD_CLR(client, &allset);
        	client= -1;*/
        printf("timeout_<5>!\n");
    }
    //	free(sendbuf);
    //	sendbuf=NULL;
    return 0;
}
int cmd_rtrv_pm_msg(int client , _MSG_NODE *msg  )
{
    /*unsigned char  *sendbuf=(unsigned char *) malloc ( sizeof(unsigned char) * SENDBUFSIZE);
    memset(sendbuf,'\0',SENDBUFSIZE);
    free(sendbuf);*/
    /*"[<aid>:<type>,<date>,<time>,<almcde>,<ctfcncde>,<condtype>,<serveff>;"<cr><lf>]*"*/
    //unsigned char  *sendbuf=(unsigned char *) malloc ( sizeof(unsigned char) * 3072);
    unsigned char  sendbuf[3072];
    int i, send_ok, len;
    memset(sendbuf, '\0', sizeof(unsigned char) * 3072);

    get_format(sendbuf, msg->ctag);
    /*	<cr><lf><lf>      ^^^<sid>^<date>^<time><cr><lf>
    M^^<catg>^COMPLD<cr><lf>
    [^^^<response message><cr><lf>]*
        ;*/
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);
    for(i = 0; i < SAVE_ALM_NUM; i++)
    {
        len = strlen(saved_alms[i]);
        if(len > 0)
        {
            strncatnew(sendbuf, saved_alms[i], len);
        }
    }

    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        /*	close(client);
        	FD_CLR(client, &allset);
        	client= -1;*/
        printf("timeout_<5>!\n");
    }
    //	free(sendbuf);
    //	sendbuf=NULL;
    return 0;
}

int cmd_set_ref_prio(_MSG_NODE *msg, int flag)
{
    int num, i, len;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    len = strlen(msg->data);
    num = (int)(msg->solt - 0x61);
    if((num < 0) || (num > 13))
    {
        return 1;
    }

    if(len == 9)
    {
        if(msg->data[0] == ',') //wu prio1
        {
            for(i = 0; i < 8; i++)
            {
                if((msg->data[i + 1] < 0x30) || (msg->data[i + 1] > 0x38))
                {
                    return 1;
                }
            }
            memcpy(conf_content.slot[num].ref_prio2, &msg->data[1], 8);

        }
        else
        {
            for(i = 0; i < 8; i++)
            {
                if((msg->data[i] < 0x30) || (msg->data[i] > 0x38))
                {
                    return 1;
                }
            }
            memcpy(conf_content.slot[num].ref_prio1, msg->data, 8);
        }

    }
    else if(len == 17)
    {
        for(i = 0; i < 8; i++)
        {
            if((msg->data[i + 9] < 0x30) || (msg->data[i + 9] > 0x38))
            {
                return 1;
            }
            if((msg->data[i] < 0x30) || (msg->data[i] > 0x38))
            {
                return 1;
            }
        }
        memcpy(conf_content.slot[num].ref_prio2, &msg->data[9], 8);
        memcpy(conf_content.slot[num].ref_prio1, msg->data, 8);
    }
    else
    {
        return 1;
    }

    sprintf((char *)sendbuf, "SET-REF-PRIO:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_ref_mod(_MSG_NODE *msg, int flag)
{
    int num, len;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    len = strlen(msg->data);
    num = (int)(msg->solt - 0x61);
    if((num < 0) || (num > 13))
    {
        return 1;
    }
    if(len == 5)
    {
        if(msg->data[0] == ',')
        {
            if(((msg->data[2] == '0') || (msg->data[2] == '1')) &&
                    ((msg->data[4] > 0x2f) && (msg->data[4] < 0x39)))
            {
                memcpy(conf_content.slot[num].ref_mod2, &msg->data[2], 3);
            }
            else
            {
                return 1;
            }
        }
        else
        {
            if(((msg->data[0] == '0') || (msg->data[0] == '1')) &&
                    ((msg->data[2] > 0x2f) && (msg->data[2] < 0x39)))
            {
                memcpy(conf_content.slot[num].ref_mod1, &msg->data[0], 3);
            }
            else
            {
                return 1;
            }
        }
    }
    else if(len == 7)
    {
        if(((msg->data[0] == '0') || (msg->data[0] == '1')) &&
                ((msg->data[2] > 0x2f) && (msg->data[2] < 0x39)))
        {}
        else
        {
            return 1;
        }
        if(((msg->data[4] == '0') || (msg->data[4] == '1')) &&
                ((msg->data[6] > 0x2f) && (msg->data[6] < 0x39)))
        {}
        else
        {
            return 1;
        }

        memcpy(conf_content.slot[num].ref_mod1, &msg->data[0], 3);
        memcpy(conf_content.slot[num].ref_mod2, &msg->data[4], 3);
    }
    else
    {
        return 1;
    }
    sprintf((char *)sendbuf, "SET-REF-MOD:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_ref_sa(_MSG_NODE *msg, int flag)
{
    int i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] < '@') && (msg->data[i] > 'E'))
        {
            return 1;
        }
    }

    sprintf((char *)sendbuf, "SET-REF-SA:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}
int cmd_set_ref_tlb(_MSG_NODE *msg, int flag)
{
    int i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] < 'a') || (msg->data[i] > 'g'))
        {
            return 1;
        }
    }
    sprintf((char *)sendbuf, "SET-REF-TLB:%s::%s;", msg->ctag, msg->data);
	//printf("%s\n","BBBB");
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}


int cmd_set_ref_tlm(_MSG_NODE *msg, int flag)
{
    int num;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
	num = (int)(msg->solt - 0x61);
    
    if((msg->data[0] != '0') && (msg->data[0] != '1'))
    {
            return 1;
    }
    

    sprintf((char *)sendbuf, "SET-REF-TLM:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
    
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
		conf_content.slot[num].ref_2mb_lvlm = msg->data[0];
        save_config();
    }
    else 
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_ref_tl(_MSG_NODE *msg, int flag)
{
    int i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] < 'a') && (msg->data[i] > 'g'))
        {
            return 1;
        }
    }

    sprintf((char *)sendbuf, "SET-REF-TL:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}


int cmd_set_ref_ssm_en(_MSG_NODE *msg, int flag)
{
    int num, i;
    unsigned char sendbuf[SENDBUFSIZE];

    memset(sendbuf, '\0', SENDBUFSIZE);
    num = (int)(msg->solt - 0x61);

    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] < '0') && (msg->data[i] > '2'))
        {
            return 1;
        }
    }

    sprintf((char *)sendbuf, "SET-REF-IEN:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        memcpy(conf_content.slot[num].ref_in_ssm_en, &msg->data[0], 8);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}


int cmd_set_ref_ph(_MSG_NODE *msg, int flag)
{
    int i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    for(i = 0; i < 8; i++)
    {
        if((msg->data[i] != '0') && (msg->data[i] != '1'))
        {
            return 1;
        }
    }

    sprintf((char *)sendbuf, "SET-REF-PH:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_rs_en(_MSG_NODE *msg, int flag)
{
    int num, i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    for(i = 0; i < 4; i++)
    {
        if((msg->data[i] != '0') && (msg->data[i] != '1'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);

        memcpy(conf_content.slot[num].rs_en, msg->data, 4);
        sprintf((char *)sendbuf, "SET-RS-EN:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }

    return 0;
}

int cmd_set_rs_tzo(_MSG_NODE *msg, int flag)
{
    int num, i;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    for(i = 0; i < 4; i++)
    {
        if((msg->data[i] < 0x40) || (msg->data[i] > 0x57))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memcpy(conf_content.slot[num].rs_tzo, msg->data, 4);
        sprintf((char *)sendbuf, "SET-RS-TZO:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }
    return 0;
}


int cmd_set_ppx_mod(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    for(i = 0; i < 16; i++)
    {
        if((msg->data[i] != '1') && (msg->data[i] != '2') && (msg->data[i] != '3'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memcpy(conf_content.slot[num].ppx_mod, msg->data, 16);
        sprintf((char *)sendbuf, "SET-PPX-MOD:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }
    return 0;
}

int cmd_set_tod_en(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    for(i = 0; i < 16; i++)
    {
        if((msg->data[i] != '1') && (msg->data[i] != '0'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memcpy(conf_content.slot[num].tod_en, msg->data, 16);
        sprintf((char *)sendbuf, "SET-TOD-EN:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }

    return 0;
}

int cmd_set_tod_pro(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        //	memcpy(conf_content.slot[num].tod_en, msg->data, 16);

        if(msg->data[0] == ',')
        {
            if((msg->data[1] < 0x40) || (msg->data[1] > 0x57))
            {
                return 1;
            }
            else
            {
                conf_content.slot[num].tod_tzo = msg->data[1];
            }
        }
        else
        {
            if((msg->data[0] < 0x30) || (msg->data[0] > 0x37))
            {
                return 1;
            }
            else
            {
                if(msg->data[1] == ',')
                {
                    if( (msg->data[2] < 0x40) || (msg->data[2] > 0x57) )
                    {
                        //return 1;
                    }
                    else
                    {
                        conf_content.slot[num].tod_tzo = msg->data[2];
                    }
                }
                else
                {
                    return 1;
                }
            }

            conf_content.slot[num].tod_br = msg->data[0];
        }

        sprintf((char *)sendbuf, "SET-TOD-PRO:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }

    }

    return 0;
}

int cmd_set_igb_en(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    for(i = 0; i < 4; i++)
    {
        if((msg->data[i] != '1') && (msg->data[i] != '0'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memcpy(conf_content.slot[num].igb_en, msg->data, 4);
        sprintf((char *)sendbuf, "SET-IGB-EN:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }
    return 0;
}

int cmd_set_igb_rat(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    for(i = 0; i < 4; i++)
    {
        if((msg->data[i] < 'a') && (msg->data[i] > 'i'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memcpy(conf_content.slot[num].igb_rat, msg->data, 4);
        sprintf((char *)sendbuf, "SET-IGB-RAT:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }
    return 0;
}

int cmd_set_igb_max(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    for(i = 0; i < 4; i++)
    {
        if((msg->data[i] < 'A') && (msg->data[i] > 'E'))
        {
            return 1;
        }
    }
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        num = (int)(msg->solt - 0x61);
        memcpy(conf_content.slot[num].igb_max, msg->data, 4);
        sprintf((char *)sendbuf, "SET-IGB-MAX:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }
    }
    else
    {
        return 1;
    }
    return 0;
}

int cmd_set_ntp_net(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i, len = 0;
    int j, i1, i2, i3;
    j = i1 = i2 = i3 = 0;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    num = (int)(msg->solt - 0x61);
    len = strlen(msg->data);

    for(i = 0; i < len; i++)
    {
        if(msg->data[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
        }
    }
    if(i3 == 0)
    {
        return 1;
    }
    if(msg->data[0] != ',')
    {
        memcpy(conf_content.slot[num].ntp_ip, &msg->data[0], i1 - 1);
    }
    if(msg->data[i1] != ',')
    {
        memcpy(conf_content.slot[num].ntp_mac, &msg->data[i1], i2 - i1 - 1);
    }
    if(msg->data[i2] != ',')
    {
        memcpy(conf_content.slot[num].ntp_gate, &msg->data[i2], i3 - i2 - 1);
    }
    if(msg->data[i3] != '\0')
    {
        memcpy(conf_content.slot[num].ntp_msk, &msg->data[i3], len - i3);
    }


    sprintf((char *)sendbuf, "SET-NTP-NET:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}
int cmd_set_ntp_pnet(_MSG_NODE *msg, int flag)
{
	unsigned char sendbuf[SENDBUFSIZE];
	int len = 0;
	int num;
	memset(sendbuf,0, sizeof(unsigned char) * SENDBUFSIZE);
	
	len = strlen(msg->data);
	num = (int)(msg->solt - 0x61);
	
	
	sprintf((char *)sendbuf, "SET-NTP-PNET:%s::%s;", msg->ctag, msg->data);
	if(flag)
	{
		sendtodown_cli(sendbuf, msg->solt, msg->ctag);
	}
	else
	{
		sendtodown(sendbuf, msg->solt);
	}

	return 0;
}

int cmd_set_ntp_pmac(_MSG_NODE *msg, int flag)
{
	unsigned char sendbuf[SENDBUFSIZE];
	int len = 0;
	memset(sendbuf,0, sizeof(unsigned char) * SENDBUFSIZE);
	
	len = strlen(msg->data);
	
	
	sprintf((char *)sendbuf, "SET-NTP-PMAC:%s::%s;", msg->ctag, msg->data);
	if(flag)
	{
		sendtodown_cli(sendbuf, msg->solt, msg->ctag);
	}
	else
	{
		sendtodown(sendbuf, msg->solt);
	}

	return 0;
}


int cmd_set_ntp_pen(_MSG_NODE *msg, int flag)
{
	unsigned char sendbuf[SENDBUFSIZE];
	int len = 0;
	memset(sendbuf,0, sizeof(unsigned char) * SENDBUFSIZE);
	
	len = strlen(msg->data);
	
	
	sprintf((char *)sendbuf, "SET-NTP-PEN:%s::%s;", msg->ctag, msg->data);
	if(flag)
	{
		sendtodown_cli(sendbuf, msg->solt, msg->ctag);
	}
	else
	{
		sendtodown(sendbuf, msg->solt);
	}

	return 0;
}


int cmd_set_ntp_en(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0, i, len;
    int j, i1, i2, i3, i4;
    j = i1 = i2 = i3 = i4 = 0;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    num = (int)(msg->solt - 0x61);
    len = strlen(msg->data);
    for(i = 0; i < len; i++)
    {
        if(msg->data[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
        }
    }
#if 1
    if((msg->data[0] == '0') || (msg->data[0] == '1'))
    {
        conf_content.slot[num].ntp_bcast_en = msg->data[0];
    }
    if(msg->data[i1] != ',')
    {
        memcpy(conf_content.slot[num].ntp_interval, &msg->data[i1], i2 - i1 - 1);
    }
    if((msg->data[i2] > 0x40) && (msg->data[i2] < 0x50))
    {
        conf_content.slot[num].ntp_tpv_en = msg->data[i2];
    }
    if((msg->data[i3] == '0') || (msg->data[i3] == '1'))
    {
        conf_content.slot[num].ntp_md5_en = msg->data[i3];
    }
    /*if((msg->data[i4] == '0') || (msg->data[i4] == '1'))
    {
        conf_content.slot[num].ntp_ois_en = msg->data[i4];
    }*/
#endif
    sprintf((char *)sendbuf, "SET-NTP-EN:%s::%s;", msg->ctag, msg->data);

    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}


int cmd_set_ntp_key(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    unsigned char keyid[5];
	unsigned char i =0;
    int num = 0;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    memset(keyid, '\0', 5);
    num = (int)(msg->solt - 0x61);

    if((msg->data[0] != '0') && (msg->data[0] != '1') && (msg->data[0] != '2'))
    {
        return 1;
    }
    else if(msg->data[1] != ',')
    {
        return 1;
    }
    else if(msg->data[6] != ',')
    {
        return 1;
    }
    #if 1
    	memcpy(keyid, &msg->data[2], 4);
    	if(msg->data[0]=='0')
    	{
    		for(i=0;i<4;i++)
    			{
    				if(conf_content.slot[num].ntp_key_flag[i]!=2)
    				{
    					conf_content.slot[num].ntp_key_flag[i]=2;
    					memcpy(conf_content.slot[num].ntp_keyid[i], &msg->data[2], 4);
    					memcpy(conf_content.slot[num].ntp_key[i], &msg->data[7], 16);
    					break;
    				}
    				//else if(i==3)
    				//	return 1;
    			}
    	}
    	else if(msg->data[0]=='1')
    	{
    		for(i=0;i<4;i++)
    			{
    				if(strncmp1(keyid, conf_content.slot[num].ntp_keyid[i] , 4)==0)
    					{
    						conf_content.slot[num].ntp_key_flag[i]=1;
    						memset(conf_content.slot[num].ntp_keyid[i],'\0',5);
    						memset(conf_content.slot[num].ntp_key[i],'\0',17);
    						break;
    					}
    				//else if(i==3)
    				//	return 1;
    			}
    	}
    	else if(msg->data[0]=='2')
    	{
    		for(i=0;i<4;i++)
    			{
    				if(strncmp1(keyid, conf_content.slot[num].ntp_keyid[i] , 4)==0)
    					{
    						memset(conf_content.slot[num].ntp_key[i],'\0',17);
    						memcpy(conf_content.slot[num].ntp_key[i], &msg->data[7], 16);
    						break;
    					}
    				//else if(i==3)
    				//	return 1;
    			}
    	}
	#endif
    
    sprintf((char *)sendbuf, "SET-NTP-KEY:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_ntp_ms(_MSG_NODE *msg, int flag)
{

    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-NTP-MS:%s::%s;", msg->ctag, msg->data);

    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        //save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_mcp_fb(_MSG_NODE *msg, int flag)
{

    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

    if((msg->data[0] != '0') && (msg->data[0] != '1'))
    {
        return 1;
    }
    if(conf_content.slot_u.fb == msg->data[0])
    {}
    else
    {
        conf_content.slot_u.fb = msg->data[0];
        sprintf((char *)sendbuf, "SET-RB-FB:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            if(slot_type[14] == 'R' || slot_type[14] == 'K')
            {
                sendtodown_cli(sendbuf, RB1_SLOT, msg->ctag);
            }
            if(slot_type[15] == 'R' || slot_type[15] == 'K')
            {
                sendtodown_cli(sendbuf, RB2_SLOT, msg->ctag);
            }
        }
        else
            //sendtodown(sendbuf, msg->solt);
        {
            if(slot_type[14] == 'R' || slot_type[14] == 'K')
            {
                sendtodown(sendbuf, RB1_SLOT);
            }
            if(slot_type[15] == 'R' || slot_type[15] == 'K')
            {
                sendtodown(sendbuf, RB2_SLOT);
            }
        }
        save_config();

    }
    respond_success(current_client, msg->ctag);
    return 0;
}

int cmd_set_rb_sa(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

    if((msg->data[0] < '@') || (msg->data[0] > 'E'))
    {
        return 1;
    }
    sprintf((char *)sendbuf, "SET-RB-SA:%s::%s;", msg->ctag, msg->data);
    if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        //save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }

    return 0;
}

int cmd_set_rb_tl(_MSG_NODE *msg, int flag)
{
    if((msg->data[0] < 'a') || (msg->data[0] > 'g'))
    {
        return 1;
    }
    if(msg->solt == RB1_SLOT)
    {
        rpt_content.slot_o.rb_tl_2mhz = msg->data[0];
    }
    else if(msg->solt == RB2_SLOT)
    {
        rpt_content.slot_p.rb_tl_2mhz = msg->data[0];
    }
    get_out_lev(0, 0);
    respond_success(current_client, msg->ctag);
    return 0;
}
//
//!设置钟盘1槽位REFTF输入IRIGB信号性能超限阀值
//!设置钟盘2槽位REFTF输入IRIGB信号性能超限阀值
//
int cmd_set_rb_thresh(_MSG_NODE *msg, int flag)
{
    int len, num;
    int temp;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    len = strlen(msg->data);
	
    if((msg->data[0] < 0x31)&&(msg->data[0] > 0x32))          //参考输入IRIGB信号的槽位号{1:2}
    {
        return 1;
    }
    temp = atoi(&msg->data[2]);
    if((temp < 1) || (temp > 999999))//6字节，最大为999，999，最小为1
    {
        return 1;
    }
    if(msg->solt == RB1_SLOT)            //钟盘槽位号15+0x60        
    {
        num = (int)(msg->data[0] - 0x31);//参考输入IRIGB信号的槽位号{1:2}
        if((num >= 0x00) && (num <= 0x01))
        {
            //if(strcmp(conf_content.slot_o.dely[num], &msg->data[2])!=0)
            //	{
            memset(conf_content.slot_o.thresh[num], '\0', 7);
            memcpy(conf_content.slot_o.thresh[num], &msg->data[2], len - 2);

            sprintf((char *)sendbuf, "SET-RB-TH:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //}
        }
    }
    else if(msg->solt == RB2_SLOT)        //钟盘槽位号16+0x60
    {
        num = (int)(msg->data[0] - 0x31); //参考输入IRIGB信号的槽位号{1:2}
        if((num >= 0x00) && (num <= 0x01))
        {
            //if(strcmp(rpt_content.slot_p., &msg->data[2])!=0)
            //	{
            memset(conf_content.slot_p.thresh[num], '\0', 7);
            memcpy(conf_content.slot_p.thresh[num], &msg->data[2], len - 2);
			printf("set rb thresh%s,%s \n",conf_content.slot_p.thresh[0],conf_content.slot_p.thresh[1]);

            sprintf((char *)sendbuf, "SET-RB-TH:%s::%s;", msg->ctag, msg->data);
            if(flag)
            {
                
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //	}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

int cmd_set_alm_msk(_MSG_NODE *msg, int inflag)
{
    //msg->data:[<type>,<xxxxxxxxxxx>]+
    int typeflag = 0; //1:表示为msg->data的type段；0:表示为msg->data的xxxxxxxxxxx段；
    int num;
    int i, j, len;
    int msgType[128], index = 0;
    unsigned char *data;
	unsigned char errflag;
	unsigned char maxlen;
	

	// downflag[3] [0]:REFTF [1]:RB 2MHZ/2MB [2]GBTP
	//unsigned char almmsknum[3];
	//unsigned char sendbuf[SENDBUFSIZE];
	//unsigned char maskdata[17];
	//unsigned char tmpdata[7];
	errflag = 0;
	maxlen  = 0;
	//memset(tmpdata,'\0',7);
	//memset(maskdata,'\0',17);
	
    //memset(sendbuf, '\0', SENDBUFSIZE);
	//memset(almmsknum,'\0',3);

	
    printf("-----\n%s:\n", __FUNCTION__);
    if(NULL == msg)
    {
        printf("cmd_set_alm_msk is error\n");
        return -1;
    }
    else
    {
        len = strlen(msg->data);
        data = (char *)malloc(len);
        if(NULL == data)
        {
            printf("cmd_set_alm_msk Malloc is failed\n");
            return -1;
        }
        else
        {
            memset(data, 0, len);
            memcpy(data, msg->data, len);
            printf("Msk data:%s\n", data);
        }
    }
	
    for(i = 0; i < len; i++)
    {
        if('=' == data[i])
        {
            msgType[index] = i - 1; //msg->data的type段所在位置
            index++;
        }
    }
	//
	//!the tail of msgtype array mark as -1. the -1 mean terminiter.
    msgType[index] = -1;
    index = 0;
    i = 0;
    num = -1;
	//
	//! Check the MsgType is or not out of bounds.
	//
    while(-1 != msgType[index])
    {
        //
        //if the i equal to the value of the msgType,find the table of mask .
        //
        if(i == msgType[index])
        {
            typeflag = 1;
			errflag =  1;
            
        }
        else 
        {
            typeflag = 0;
        }
		
        //
        // The Header Msgtype Should Be Corrected,or not the fuc don't deal.
        //
		if(1 == errflag)
		{
			
		        if(1 == typeflag)
		        {
		            printf("The CMD RALM----slot_type:%c\n", data[i]);
					
						
		            for(j = 0; j < alm_msk_total; j++)
		            {
		            //if equal S or K, record the num of mask.
						//almmsknum[0] = (data[i] == 'S')? j : 0;
						//almmsknum[1] = (data[i] == 'K')? j : 0;
		                if(data[i] == conf_content.alm_msk[j].type)
		                {
		                    num = j;
		                    break;
		                }
		            }
		            if(-1 == num)
		            {
		                for(j = 0; j < alm_msk_total; j++)
		                {
		                    if('\0' == conf_content.alm_msk[j].type)
		                    {
		                        conf_content.alm_msk[j].type = data[i];
		                        num = j;
		                        break;
		                    }
		                }
		            }
		            i += 2;
		        }
		        else 
		        {
		            printf("num= %d\n", num);
		            index++;
					 
		            if(-1 != msgType[index])
		            {
		                //
		                //limit the max len of data to  6。
		                //
		                maxlen =  msgType[index] - i - 1;
						
						if(maxlen <= ALM_MASK_LEN)
						{
			                if((data[i] >> 4) == 0x4)
			                {
			                    memcpy(conf_content.alm_msk[num].data, &data[i], msgType[index] - i - 1);
			                    i = msgType[index];
			                }
			                else
			                {
			                    memcpy(conf_content.alm_msk[num].data + 6, &data[i], msgType[index] - i - 1);
			                    i = msgType[index];
			                }
						}
		                printf("-1 != msgType[index]---%c:%s\n", conf_content.alm_msk[num].type, conf_content.alm_msk[num].data);
		            }
		            else
		            {
		            
		               //
		               //limit the max len of data to  6。
		               //
		               maxlen =  len - i;
						
					   if(maxlen <= ALM_MASK_LEN)
					   {
			                if((data[i] >> 4) == 0x4)
			                {
			                    memcpy(conf_content.alm_msk[num].data, &data[i], len - i);
			                }
			                else
			                {
			                    memcpy(conf_content.alm_msk[num].data + 6, &data[i], len - i);
			                }
					   }
			                //printf("-1 == msgType[index]---%c  %s\n",conf_content.alm_msk[num].type,conf_content.alm_msk[num].data);
		            }
		            typeflag = 1;
		            num = -1;
		        }
			}
		    else
		    {
		        msgType[index] = -1;
		    }

    }
    free(data);
	//
	//REFTF E1 OR TIME PORT LAMP ON/OFF control;
	/*if(almmsknum[0])
	{
	   num = almmsknum[0];

       tmpdata[2] =  (conf_content.alm_msk[num].data[3] & 0x0C) >> 2;
	   tmpdata[2] |= (conf_content.alm_msk[num].data[2] & 0x03) <<2 ;

	   tmpdata[3] =  (conf_content.alm_msk[num].data[2] & 0x0C) >>2;
       tmpdata[3] |= ( conf_content.alm_msk[num].data[3] & 0x03) <<2;

	   tmpdata[0] =  conf_content.alm_msk[num].data[4] &0x0f;
       
       tmpdata[1] =  conf_content.alm_msk[num].data[5] &0x0f;
       
	   for(i = 0; i < 4;i++)
	   for(j = 0; j < 4; j++)
	   {
		    //
		    if((tmpdata[i] & (1 << j)) == 0)
						maskdata[4*i+j] = '0';
			else
			            maskdata[4*i+j] = '1';
	   }

	   for(i = 0; i <14; i++)
	   {
	   		if(slot_type[i] == 'S')
				break;
	   }
	   sprintf((char *)sendbuf, "SET-REF-LAMP:%s::%s;", msg->ctag, maskdata);
	   sendtodown(sendbuf, i + 0x61);
	}*/
	save_config();
    return 0;
}

//cmd_rtrv_ralm_mask
int cmd_rtrv_ralm_mask(int client , _MSG_NODE *msg  )
{

    unsigned char  sendbuf[3072];
    int i, send_ok, len;
    memset(sendbuf, '\0', sizeof(unsigned char) * 3072);

    get_format(sendbuf, msg->ctag);
    /*	<cr><lf><lf>      ^^^<sid>^<date>^<time><cr><lf>
    M^^<catg>^COMPLD<cr><lf>
    [^^^<response message><cr><lf>]*
        ;*/
    /*[<type>:<data>:<cr><lf>]*/
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);
    for(i = 0; i < alm_msk_total; i++)
    {
        if(conf_content.alm_msk[i].type == '\0')
        {
            len = strlen(sendbuf);

            if(',' == sendbuf[len - 1])
            {
                sendbuf[len - 1] = '\0';
            }

            break;
        }
        //out盘等输出盘不上传
        else if(conf_content.alm_msk[i].type == 'B' ||
                conf_content.alm_msk[i].type == 'D' ||
                conf_content.alm_msk[i].type == 'E' ||
                conf_content.alm_msk[i].type == 'F' ||
                conf_content.alm_msk[i].type == 'G' ||
                conf_content.alm_msk[i].type == 'H' ||
                conf_content.alm_msk[i].type == 'J' ||
                conf_content.alm_msk[i].type == 'a' ||
                conf_content.alm_msk[i].type == 'c' ||
                conf_content.alm_msk[i].type == 'b' ||
                //conf_content.alm_msk[i].type == 'f' ||
                conf_content.alm_msk[i].type == 'h' ||
                conf_content.alm_msk[i].type == 'i'
               )
        {
            continue;
        }

        if( (conf_content.alm_msk[i].data[6] >> 4) == 0x5 )
        {
            sprintf((char *)sendbuf, "%s%c%c", sendbuf, conf_content.alm_msk[i].type, '=');
            strncatnew(sendbuf, conf_content.alm_msk[i].data, 6);
            strncatnew(sendbuf, ":", 1);
            strncatnew(sendbuf, &(conf_content.alm_msk[i].data[6]), 6);

        }
        else
        {
            sprintf((char *)sendbuf, "%s%c%c%s", sendbuf, conf_content.alm_msk[i].type, '=', conf_content.alm_msk[i].data);
        }

        if(conf_content.alm_msk[i + 1].type != '\0' && i + 1 < alm_msk_total)
        {
            strncatnew(sendbuf, ",", 1);
        }
        else
        {
            //sprintf((char *)sendbuf,"%s\"%c%c;",sendbuf,CR,LF);
        }
    }
    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<5>!\n");
    }
    return 0;
}




int cmd_set_ext_online(_MSG_NODE *msg, int flag)
{
    //
    //!
    //
    unsigned char onlineSta = '\0';
    int i;
    if(msg == NULL)
    {
        return 1;
    }

    onlineSta = msg->data[0];

    for(i = 0; i < 3; i++)
    {
        if( ( (onlineSta >> i) & 0x01 ) == 0 )
        {
            memset(gExtCtx.extBid[i], 'O', 15);
        }
        else
        {
            ;
        }
    }

    gExtCtx.save.onlineSta = onlineSta;
    if(0 == ext_write_flash(&(gExtCtx.save)))
    {
        return 1;
    }
    return 0;
}


int cmd_qext_online(int client, _MSG_NODE *msg)
{

    int send_ok, num;
    unsigned char sendbuf[1024];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    num = (int)(msg->solt - 'a');

    get_format(sendbuf, msg->ctag);
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);

    if( (gExtCtx.save.onlineSta & 0xF0) != 0x40 )
    {
        gExtCtx.save.onlineSta = 0x47;
        sprintf((char *)sendbuf, "%s%c", sendbuf, gExtCtx.save.onlineSta);
    }
    else
    {
        sprintf((char *)sendbuf, "%s%c", sendbuf, gExtCtx.save.onlineSta);
    }

    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;
}


int cmd_set_leap_mod(_MSG_NODE *msg, int flag)
{
    char leapMod;

    leapMod = msg->data[0];
    conf_content3.LeapMod = 0;
    conf_content3.LeapMod = leapMod;
    save_config();

    return 0;
}

/*
2014.12.10
返回闰秒模式
0---手动
1---自动
*/
int cmd_rtrv_leap_mod(int client, _MSG_NODE *msg)
{

    int send_ok, num;
    unsigned char sendbuf[1024];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    num = (int)(msg->solt - 'a');

    get_format(sendbuf, msg->ctag);
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);

    if( '0' != conf_content3.LeapMod && '1' != conf_content3.LeapMod)
    {
        conf_content3.LeapMod = '0';
        sprintf((char *)sendbuf, "%s%c", sendbuf, conf_content3.LeapMod);
    }
    else
    {
        sprintf((char *)sendbuf, "%s%c", sendbuf, conf_content3.LeapMod);
    }

    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;
}

int cmd_rtrv_igb_tzone(int client, _MSG_NODE *msg)
{

    int send_ok, num;
    unsigned char sendbuf[1024];

    num = (int)(msg->solt - 'a');
    if( ((msg->solt) <= '`') || ((msg->solt) >= 'o') )
    {
        return 1;
    }
    else if( slot_type[num] != 'W' )
    {
        return 1;
    }
    else
    {
        memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    }

    get_format(sendbuf, msg->ctag);
    sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);
    if( '0' == rpt_content.slot[num].igb_tzone || '8' == rpt_content.slot[num].igb_tzone)
    {
        sprintf((char *)sendbuf, "%s%c", sendbuf, rpt_content.slot[num].igb_tzone);
    }
    else
    {
        sprintf((char *)sendbuf, "%s%c", sendbuf, '0');
    }

    sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;
}



/*
2015.5.27
set irigb16 timezone
*/
int cmd_set_igb_tzone(_MSG_NODE *msg, int flag)
{
    unsigned char sendbuf[SENDBUFSIZE];
    int num = 0;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

    if( ((msg->solt) > '`') && ((msg->solt) < 'o') )
    {
        num = (int)(msg->solt - 0x61);
        if(slot_type[num] != 'W')
        {
            return 1;
        }

        conf_content3.irigb_tzone[num] = (msg->data)[0];
        sprintf((char *)sendbuf, "SET-IGB-TZONE:%s::%s;", msg->ctag, msg->data);
        if(flag)
        {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
        }
        else
        {
            sendtodown(sendbuf, msg->solt);
        }

        return 0;
    }
    else
    {
        return 1;
    }
}


int cmd_sys_cpu(int client, _MSG_NODE *msg)
{

    int send_ok;
    unsigned char sendbuf[1024];
    char *sys_cpu = "6";
    char *sys_mem = "10";

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);

    get_format(sendbuf, msg->ctag);
	sprintf((char *)sendbuf, "%s%c%c%c\"", sendbuf, SP, SP, SP);
    sprintf((char *)sendbuf, "%s%s,%s", sendbuf, sys_cpu, sys_mem);
	sprintf((char *)sendbuf, "%s\"%c%c;", sendbuf, CR, LF);

    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;

}


int read_ip(unsigned const char *ip, unsigned char *out_ip)
{
    //0100 Y7Y6Y5Y4     0100 Y3Y2Y1Y0
    int i;
    unsigned char temp = 0;
	char in_ip[9];
    if(in_ip == NULL || out_ip == NULL)
    {
        printf("read ip error\n");
        return -1;
    }
    else if(*in_ip == ',')
    {
        return 1;
    }
    else
    {
        memset(in_ip,0,9);
		memcpy(in_ip,ip,8);
    }

    for(i = 0; i < 8; i++)
    {
        if( ((*(in_ip + i)) & 0xf0) != 0x40 )
        {
            return 1;
        }
        else
        {
            if( (i & 0x01) == 0x00 )
            {
                temp = (*(in_ip + i)) << 4;
            }
            else
            {
                temp |= ((*(in_ip + i)) & 0x0f);
                sprintf((char *)out_ip, "%s%d", out_ip, temp);
                if(7 == i)
                {
                    ;
                }
                else
                {
                    sprintf((char *)out_ip, "%s.", out_ip);
                }
                temp = 0;
            }
        }
    }
    return 0;
}

int cmd_rtrv_mtc_ip(int client, _MSG_NODE *msg)
{

    int num, send_ok;
    unsigned char sendbuf[1024];
	unsigned char sendtmp[256];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    memset(sendtmp, '\0', sizeof(unsigned char) * 256);
    num = (int)(msg->solt - 'a');
    get_format(sendtmp, msg->ctag);
	
	sprintf((char *)sendbuf, "%s%c%c%c\"RPT-MTC-IP,%s,%s,%s,%s\"%c%c;",sendtmp,SP, SP, SP, &rpt_ptp_mtc.ptp_mtc_ip[num][0][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][1][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][2][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][3][0],
            CR,LF);
		
    /*sprintf((char *)sendbuf, "%sRPT-MTC-IP,%s,%s,%s,%s", sendbuf,
            &rpt_ptp_mtc.ptp_mtc_ip[num][0][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][1][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][2][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][3][0]);*/
	
	/*sprintf((char *)sendbuf, "%s\"%c%c", sendbuf, CR, LF);*/
	//printf("mtc send %s\n",&rpt_ptp_mtc.ptp_mtc_ip[num][0][0]);
    //printf("mtc send %d,%s\n",num,sendbuf);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;

}
int cmd_rtrv_uni_ip(int client, _MSG_NODE *msg)
{

    int num, send_ok;
    unsigned char sendbuf[1024];
	unsigned char sendtmp[256];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);
    memset(sendtmp, '\0', sizeof(unsigned char) * 256);
    num = (int)(msg->solt - 'a');
    get_format(sendtmp, msg->ctag);
	
	sprintf((char *)sendbuf, "%s%c%c%c\"RPT-MTC-IP,%s|%s|%s|%s\"%c%c;",sendtmp,SP, SP, SP, &rpt_ptp_mtc.ptp_mtc_ip[num][0][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][1][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][2][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][3][0],
            CR,LF);
		
    /*sprintf((char *)sendbuf, "%sRPT-MTC-IP,%s,%s,%s,%s", sendbuf,
            &rpt_ptp_mtc.ptp_mtc_ip[num][0][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][1][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][2][0],
            &rpt_ptp_mtc.ptp_mtc_ip[num][3][0]);*/
	
	/*sprintf((char *)sendbuf, "%s\"%c%c", sendbuf, CR, LF);*/
	//printf("mtc send %s\n",&rpt_ptp_mtc.ptp_mtc_ip[num][0][0]);
    //printf("mtc send %d,%s\n",num,sendbuf);
    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;

}

int cmd_rtrv_mtc_dom(int client, _MSG_NODE *msg)
{

    int num, send_ok;
    unsigned char sendbuf[1024];

    memset(sendbuf, '\0', sizeof(unsigned char) * 1024);

    num = (int)(msg->solt - 'a');
    get_format(sendbuf, msg->ctag);
    sprintf((char *)sendbuf, "%sRPT-MTC-DOM,%s,%s,%s,%s;", sendbuf,
            &rpt_ptp_mtc.ptp_dom[num][0][0],
            &rpt_ptp_mtc.ptp_dom[num][1][0],
            &rpt_ptp_mtc.ptp_dom[num][2][0],
            &rpt_ptp_mtc.ptp_dom[num][3][0]);

    send_ok = send(client, sendbuf, strlen(sendbuf), MSG_NOSIGNAL);
    if(send_ok == -1 && errno == EAGAIN)
    {
        printf("timeout_<3>!\n");
    }
    return 0;

}


void set_sta_ip(unsigned  const char *ip, unsigned char solt)
{
    unsigned char r_ip[50];
    unsigned char ip_temp[9];
    unsigned char sendbuf[50];

    if(NULL == ip)
    {
        return;
    }
    else
    {
        memset(sendbuf, 0, 50);
        memset(r_ip, 0, 50);
        memset(ip_temp, 0, 9);
		memcpy(ip_temp, ip, 8);

        sprintf((char *)sendbuf, "SET-STA1:000000::Board61850IP=");
    }
#if 1
    if(!read_ip(ip_temp, r_ip))
    {
        memset(conf_content3.sta1.ip, 0, 9);
        memcpy(conf_content3.sta1.ip, ip_temp, 8);
        sprintf((char *)sendbuf, "%s%s;", sendbuf, r_ip);
        sendtodown(sendbuf, solt);
    }
    else
    {
    }
#endif
}

void set_sta_gateway(unsigned char *ip, unsigned char solt)
{
    unsigned char r_ip[50];
    unsigned char sendbuf[SENDBUFSIZE];

    if(NULL == ip)
    {
        return;
    }
    else
    {
        memset(sendbuf, 0, SENDBUFSIZE);
        memset(r_ip, 0, 50);
        sprintf((char *)sendbuf, "SET-STA1:000000::Board61850GateWay=");
    }

    if(!read_ip(ip, r_ip))
    {
        memset(conf_content3.sta1.gate, 0, 9);
        memcpy(conf_content3.sta1.gate, ip, 8);
        sprintf((char *)sendbuf, "%s%s;", sendbuf, r_ip);
        sendtodown(sendbuf, solt);
    }
    else
    {
        //;
    }
}

void set_sta_netmask(unsigned char *ip, unsigned char solt)
{
    unsigned char r_ip[50];
    unsigned char sendbuf[SENDBUFSIZE];

    if(NULL == ip)
    {
        return;
    }
    else
    {
        memset(sendbuf, 0, SENDBUFSIZE);
        memset(r_ip, 0, 50);
        sprintf((char *)sendbuf, "SET-STA1:000000::Board61850NetMask=");
    }

    if(!read_ip(ip, r_ip))
    {
        memset(conf_content3.sta1.mask, 0, 9);
        memcpy(conf_content3.sta1.mask, ip, 8);

        sprintf((char *)sendbuf, "%s%s;", sendbuf, r_ip);
        sendtodown(sendbuf, solt);
    }
    else
    {
        //;
    }
}


/*
SET-STA1-NET:
[<tid>]:<aid>:<ctag>:<password>:
<type>,<num>,<ip>,<mac>,<gateway>,<netmask>,<dns1>,<dns2>;
*/
int cmd_set_sta_net(_MSG_NODE *msg, int flag)
{

   	unsigned char *p = NULL;
    unsigned char comma_id = 0;
	signed char len;

    printf("cmd_set_sta_net data:\n%s\n", msg->data);
    printf("slot:%c\n", msg->solt);

    if( ((msg->solt) < 'a') || ((msg->solt) > 'm') )
    {
        return 1;
    }
    else
    {
        p = msg->data;
		len = strlen(msg->data);
    }

    while( (len>=0) && ((*p) != ';'))
    {
    	len --;
        if(',' == (*p))
        {
            comma_id++;
            switch (comma_id)
            {
            case 0 :
                //tpye
                break;
            case 1 :
                //num
                break;
            case 2 ://ip
                if( *(p+1) != ',' && *(p+1) != ';' )
                {
                    set_sta_ip((p+1), msg->solt);
                }
                break;
            case 3 ://mac
                break;
            case 4 ://gateway
                if( *(p + 1) != ',' && *(p + 1) != ';' )
                {
                    set_sta_gateway((p + 1), msg->solt);
                }
                break;
            case 5 ://mask
                if( *(p + 1) != ',' && *(p + 1) != ';' )
                {
                    set_sta_netmask((p + 1), msg->solt);
                }
                break;
            default://others
                break;
            }

        }
        else
        {
            ;
        }
        p++;
    }

    if(flag)
    {
        //sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        //sendtodown(sendbuf, msg->solt);
    }
    respond_success(current_client, msg->ctag);
    return 0;
}
//
//!set pgein net parameters
//
int cmd_set_pgein_net(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3;
    int len = strlen(msg->data);
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
   
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[2] == '1') )
        {
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
       
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                }
            }
            if(i3 == 0)
            {
                return 1;
            }
            conf_content.slot[num].ptp_type = msg->data[0];
            if(msg->data[i1] != ',')
            {
                memcpy(conf_content.slot[num].ptp_ip[port], &msg->data[i1], i2 - i1 - 1);
				conf_content.slot[num].ptp_ip[port][8] = '\0';
            }
            
            if(msg->data[i2] != ',')
            {
                memcpy(conf_content.slot[num].ptp_gate[port], &msg->data[i2], i3 - i2 - 1);
				conf_content.slot[num].ptp_gate[port][8] = '\0';
				
            }
            if(msg->data[i3] != '\0')
            {
                memcpy(conf_content.slot[num].ptp_mask[port], &msg->data[i3], len - i3);
				conf_content.slot[num].ptp_mask[port][8] = '\0';
            }
            
            sprintf((char *)sendbuf, "SET-PGEIN-NET:%s;", msg->data);

			if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //		}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
//
//!set pgein mode parameters
//
int cmd_set_pgein_mod(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3, i4, i5, i6;
	
    int len = strlen(msg->data);
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
   
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[2] == '1') )
        {
            
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
          
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                    if(j == 5)
                    {
                        i4 = i + 1;
                    }
                    if(j == 6)
                    {
                        i5 = i + 1;
                    }
                    if(j == 7)
                    {
                        i6 = i + 1;
                    }
                    
                }
            }
            if(i6 == 0)
            {
                return 1;
            }
            conf_content.slot[num].ptp_type = msg->data[0];
            //
            //! set delaytype: 0-P2P, 1-E2E; 
            //
            if((msg->data[i1] == '0') || (msg->data[i1] == '1'))
            {
                conf_content.slot[num].ptp_delaytype[port] = msg->data[i1];
            }
			//
			//! 0-unicast,1-multicast;
			//
            if((msg->data[i2] == '0') || (msg->data[i2] == '1'))
            {
                conf_content.slot[num].ptp_multicast[port] = msg->data[i2];
            }
			//
			//! enq : 0-two,1-three
			//
            if((msg->data[i3] == '0') || (msg->data[i3] == '1'))
            {
                conf_content.slot[num].ptp_enp[port] = msg->data[i3];
            }
			//
			//!0:one step,1:two step;
			//
            if((msg->data[i4] == '0') || (msg->data[i4] == '1'))
            {
                conf_content.slot[num].ptp_step[port] = msg->data[i4];
            }
			//
			//!delay req fre
			//
            if(msg->data[i5] != ',')
            {
                memcpy(conf_content.slot[num].ptp_delayreq[port], &msg->data[i5], i6 - i5 - 1);
				conf_content.slot[num].ptp_delayreq[port][2] = '\0';
				
            }
			//
			//!pdelay req fre
			//
            if(msg->data[i6] != '\0')
            {
                memcpy(conf_content.slot[num].ptp_pdelayreq[port], &msg->data[i6], len - i6);
				conf_content.slot[num].ptp_pdelayreq[port][2] = '\0';
				
            }
            //
            //!SET PGEIN MOD
            //
            sprintf((char *)sendbuf, "SET-PGEIN-MOD:%s;", msg->data);
			
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //	}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("PI MOD %s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
//
//!set pge4s net parameters
//
int cmd_set_pge4v_net(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
	unsigned char  datalen;
	unsigned char  errflag;
    int num;
    int port;
    int i, j, i1, i2, i3, i4;
    int len = strlen(msg->data);
	datalen = 0;
	errflag = 0;
    j =  0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
	//
	//!Clear the snedbuf as null.
    memset(sendbuf, '\0', SENDBUFSIZE);
	
	//
	//!the slot num le 14 and ge 1.
    if(((msg->solt) > '`') && ((msg->solt) <= 'o'))
    {
        //
        //check pge port le MAX_PORT_NUM.
        //
        if((msg->data[2] > '0') && (msg->data[2] <= MAX_PORT_NUM))
        {
           //
           //Calculate the slot num and port num.
           //
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
           //
           //! find the conntect (mac、IP、netmask)
           //
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                    if(j == 5)
                    {
                        i4 = i + 1;
                    }
                }
            }
			//
			//if i4 equal to 0,the data frame is null。
			//
            if(i4 == 0)
            {
                return 1;
            }
			//
			//save the ptp type of slot.
			//
            conf_content.slot[num].ptp_type = msg->data[0];
			
			//
			//Judge the data len or not correct.
			//
			datalen = i2 - i1 -1; 
			if(datalen > MAX_MAC_LEN)
				errflag = 1;
			
			datalen = i3 - i2 -1; 
			if(datalen > MAX_IP_LEN)
				errflag = 1;
			
			datalen = i4 - i3 -1;
			if(datalen > MAX_IP_LEN)
				errflag = 1;

			datalen = len - i4;
			if(datalen > MAX_IP_LEN)
				errflag = 1;

			if(1 == errflag)
				return 1;
			
			//
			//save the mac parameters.
			//
            if(msg->data[i1] != ',')
            { 
				memset(conf_content.slot[num].ptp_mac[port],'\0',18);
                memcpy(conf_content.slot[num].ptp_mac[port], &msg->data[i1], i2 - i1 - 1);    
            }
			//
			//save the ip address.
			//
            if(msg->data[i2] != ',')
            {
                memset(conf_content.slot[num].ptp_ip[port],'\0',16);
                memcpy(conf_content.slot[num].ptp_ip[port], &msg->data[i2], i3 - i2 - 1);
            }
			//
			//save the mask address.
			//
            if(msg->data[i3] != ',')
            {
                memset(conf_content.slot[num].ptp_mask[port],'\0',16);
                memcpy(conf_content.slot[num].ptp_mask[port], &msg->data[i3], i4 - i3 - 1);
            }
			//
			//save the gate address.
			//
            if(msg->data[i4] != '\0')
            {
                memset(conf_content.slot[num].ptp_gate[port],'\0',16);
                memcpy(conf_content.slot[num].ptp_gate[port], &msg->data[i4], len - i4);
            }
     
            sprintf((char *)sendbuf, "SET-PGE4V-NET:%s;", msg->data);
			printf("SET-PGE4V-NET %s\n",msg->data);
            //
            // sned down to uart.
            //
			if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
           
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

//
//! set the pge4s work mode.
//
int cmd_set_pge4v_mod(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3, i4, i5, i6; /* i7, i8, i9, i10, i11, i12, i13;*/
    int len = strlen(msg->data);
	
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
     /*i7 = 0;
    i8 = 0;
    i9 = 0;
    i10 = 0;
    i11 = 0;
    i12 = 0;
    i13 = 0;*/
	//
	//clear sendbuf .
	//
    memset(sendbuf, '\0', SENDBUFSIZE);
	//
	//judge slot num is le 14 .
	//
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        //
        //judge the port num is le MAX_PORT_NUM。
        //
        if((msg->data[2] > '0') && (msg->data[2] <= MAX_PORT_NUM))
        {
            //
            //!Calculate the num or port.
            //
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
          
            /*SET-PGE4S-MOD:<type>,<response message>;*/
			//
			//!
			//
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                    if(j == 5)
                    {
                        i4 = i + 1;
                    }
                    if(j == 6)
                    {
                        i5 = i + 1;
                    }
                    if(j == 7)
                    {
                        i6 = i + 1;
                    }
                  
                }
            }
            if(i5 == 0)
            {
                return 1;
            }
            conf_content.slot[num].ptp_type = msg->data[0];
			//
			//!save the ptp enable. 
			//
            if((msg->data[i1] == '0') || (msg->data[i1] == '1'))
            {
                conf_content.slot[num].ptp_en[port] = msg->data[i1];
            }
			//
			//!save the esmc enable.
			//
            if((msg->data[i2] == '0') || (msg->data[i2] == '1'))
            {
                conf_content3.ptp_esmcen[num][port] = msg->data[i2];
				//conf_content.slot[num].ptp_delaytype[port] = msg->data[i2];
            }
			//
			//!save the delay type.
			//
			if((msg->data[i3] == '0') || (msg->data[i3] == '1'))
            {
                conf_content.slot[num].ptp_delaytype[port] = msg->data[i3];
            }
			//
			//!save the multicast/unicast.
			//
            if((msg->data[i4] == '0') || (msg->data[i4] == '1'))
            {
                conf_content.slot[num].ptp_multicast[port] = msg->data[i4];
            }
			//
			//!save the enq .
			//
            if((msg->data[i5] == '0') || (msg->data[i5] == '1'))
            {
                conf_content.slot[num].ptp_enp[port] = msg->data[i5];
            }
			//
			//!save the step.
			//
            if((msg->data[i6] == '0') || (msg->data[i6] == '1'))
            {
                conf_content.slot[num].ptp_step[port] = msg->data[i5];
            }
            
            //
            //!send the command  down to uart.
            //
            sprintf((char *)sendbuf, "SET-PGE4V-MOD:%s;", msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            //	}
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

//
//!set pge4s work paramters(Extern)。
//
int cmd_set_pge4v_par(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3, i4, i5;/*, i6, i7, i8, i9, i10, i11, i12, i13;*/
    int len = strlen(msg->data);
	unsigned char datalen = 0;
	unsigned errflag = 0;
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    /*i6 = 0;
    i7 = 0;
    i8 = 0;
    i9 = 0;
    i10 = 0;
    i11 = 0;
    i12 = 0;
    i13 = 0;*/
    //
    //!clear send buffer.
    //
    memset(sendbuf, '\0', SENDBUFSIZE);
	//
	//judge the slot le 14 。
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        //
        //limit the port range 1 ~ 4
        if((msg->data[2] > '0') && (msg->data[2] <= MAX_PORT_NUM))
        {
            //
            //Calculate slot number and Port number。
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            //
            //Calculate the paramters position。
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    }
                    if(j == 5)
                    {
                        i4 = i + 1;
                    }
                    if(j == 6)
                    {
                        i5 = i + 1;
                    }
                   
                }
            }
            if(i5 == 0)
            {
                return 1;
            }
            //
            //judge the domain len is or not gt 3.
            //
			datalen = i2 - i1 -1;
			if(datalen > MAX_DOM_LEN)
				errflag = 1;
			//
			//judge the sync frequency len or not eq 2/0。
			//
			datalen = i3 - i2 -1;
			if((datalen != FRAME_FRE_LEN)&&(datalen != 0))
				errflag = 1;
			//
			//judge the announce frequency len or not eq 2/0。
			//
			datalen = i4 - i3 -1;
			if((datalen != FRAME_FRE_LEN)&&(datalen != 0))
				errflag = 1;
			//
			//judge the delaycon gt 7。
			//
			datalen = i5 - i4 -1;
			if((datalen > DELAY_COM_LEN))
				errflag = 1;	
			//
			//judge the prio gt 4
			datalen = len -i5;
			if((datalen > MAX_PRIO_LEN))
				errflag = 1;	
			if(1 == errflag)
				return 1;
			//
			//slot type。
			//
            conf_content.slot[num].ptp_type = msg->data[0];
			//
			//save the domain number。
			//
            if(msg->data[i1] != ',')
            {
                memset(conf_content3.ptp_dom[num][port], 0, 4);
                memcpy(conf_content3.ptp_dom[num][port], &msg->data[i1], i2 - i1 -1);
            }
			//
			//save the sync frequency 。
			//
            if(msg->data[i2] != ',')
            {
                memcpy(conf_content.slot[num].ptp_sync[port], &msg->data[i2], i3 - i2 - 1);
            }
			//
			//save the annouce frequency。
			//
            if(msg->data[i3] != ',')
            {
                memcpy(conf_content.slot[num].ptp_announce[port], &msg->data[i3], i4 - i3 - 1);
            }
			//
			//save the delaycom。
			//
            if(msg->data[i4] != ',')
            {
                memset(conf_content.slot[num].ptp_delaycom[port],'\0',9);
                memcpy(conf_content.slot[num].ptp_delaycom[port], &msg->data[i4], i5 - i4 - 1);
            }
			//
			//save the pro。
			//
            if(msg->data[i5] != '\0')
            {
                if(msg->data[i5] == 'A')
                {
                    memset(conf_content3.ptp_prio1[num][port], 0, 5);
                    memcpy(conf_content3.ptp_prio1[num][port], &msg->data[i5], len - i5);
                    
                }
                else if(msg->data[i5] == 'B')
                {
                    memset(conf_content3.ptp_prio2[num][port], 0, 5);
                    memcpy(conf_content3.ptp_prio2[num][port], &msg->data[i5], len - i5);
                }
                else
                {
                    return 1;
                }
            }
            
            sprintf((char *)sendbuf, "SET-PGE4V-PAR:%s;", msg->data);
			printf("SET-PGE4V-PAR %s\n",msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
//
//! set pge4s mtc.
//
int cmd_set_pge4v_mtc(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
	
    memset(sendbuf, '\0', SENDBUFSIZE);
    //
    //slot num.
    //
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        if((msg->data[2] > '0') && (msg->data[2] <= MAX_PORT_NUM))
        {
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            if(msg->data[4] != '\0')
            {
                memset(conf_content3.ptp_mtc_ip[num][port], 0, 9);
                memcpy(conf_content3.ptp_mtc_ip[num][port], &msg->data[4], MAX_IP_LEN);


                sprintf((char *)sendbuf, "SET-PGE4V-MTC:%s;", msg->data);

                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            return 0;

        }
		return 1;
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
}


//
//!set pge4s work sfp modual(Extern)。
//
int cmd_set_pge4v_sfp(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    int num;
    int port;
    int i, j, i1, i2, i3;/*, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13;*/
    int len = strlen(msg->data);
	unsigned char datalen = 0;
	unsigned errflag = 0;
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    //
    //!clear send buffer.
    //
    memset(sendbuf, '\0', SENDBUFSIZE);
	//
	//judge the slot le 14 。
    if(((msg->solt) > '`') && ((msg->solt) < 'o'))
    {
        //
        //limit the port range 1 ~ 4
        if((msg->data[2] > '0') && (msg->data[2] <= MAX_PORT_NUM))
        {
            //
            //Calculate slot number and Port number。
            num = (int)(msg->solt - 0x61);
            port = (int)(msg->data[2] - 0x31);
            //
            //Calculate the paramters position。
            for(i = 0; i < len; i++)
            {
                if(msg->data[i] == ',')
                {
                    j++;
                    if(j == 2)
                    {
                        i1 = i + 1;
                    }
                    if(j == 3)
                    {
                        i2 = i + 1;
                    }
                    if(j == 4)
                    {
                        i3 = i + 1;
                    } 
                }
            }
            if(i3 == 0)
            {
                return 1;
            }
            //
            //judge the sfp modual setting len is or not gt 3.
            //
			datalen = i2 - i1 -1;
			if((datalen != 2)&&(datalen != 0))
				errflag = 1;
	
			if(1 == errflag)
				return 1;
			//
			//slot type。
			//
            conf_content.slot[num].ptp_type = msg->data[0];
			//
			//save the domain number。
			//
            if(msg->data[i1] != ',')
            {
                memset(conf_content3.ptp_sfp[num][port], 0, 3);
                memcpy(conf_content3.ptp_sfp[num][port], &msg->data[i1], i2 - i1 -1);
            }
			//
			//save the sfp modul op low thresh 。
			//
            if(msg->data[i2] != ',')
            {
                memset(conf_content3.ptp_oplo[num][port], 0, 7);
                memcpy(conf_content3.ptp_oplo[num][port], &msg->data[i2], i3 - i2 - 1);
            }
			//
			//save the sfp modul op hi thresh 。
			//
            if(msg->data[i3] != '\0')
            {
                memset(conf_content3.ptp_ophi[num][port], 0, 7);
                memcpy(conf_content3.ptp_ophi[num][port], &msg->data[i2], len - i3);
            }
			//
			//send down to uart 。
            sprintf((char *)sendbuf, "SET-PGE4V-SFP:%s;", msg->data); 
			printf("SET-PGE4V-SFP %s\n",msg->data);
            if(flag)
            {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
            }
            else
            {
                sendtodown(sendbuf, msg->solt);
            }
            
        }
    }
    else
    {
        return 1;
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int cmd_set_gb_mode(_MSG_NODE *msg, int flag)
*	Descript: gbtpv2 work mode setting.
*	Parameters:  - msg  :the point of input data.
*                - flag :1:NMG SETTING; 0:Init Setting.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/
int cmd_set_gb_mode(_MSG_NODE *msg, int flag)
{
    int len;
	
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    len = strlen(msg->data);
	
	if((strncmp1(msg->data,"GPS",3) != 0)&&(strncmp1(msg->data,"BD",3) != 0)&&(strncmp1(msg->data,"MIX",3) != 0))
	 return 1;
	
    if(msg->solt == GBTP1_SLOT)
    {
        memset(conf_content.slot_q.mode, '\0', 4);
        memcpy(conf_content.slot_q.mode, msg->data, len);  
    }
    else if(msg->solt == GBTP2_SLOT)
    {
        memset(conf_content.slot_r.mode, '\0', 4);
        memcpy(conf_content.slot_r.mode, msg->data, len);
    }
    else
    {
        return 1;
    }

	sprintf((char *)sendbuf, "SET-GBTP-MODE:%s::%s;", msg->ctag, msg->data);
	
    if(flag)
    {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
    }
    else
    {
            sendtodown(sendbuf, msg->solt);
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int cmd_set_gn_mode(_MSG_NODE *msg, int flag)
*	Descript: gbtpv2 work mode setting.
*	Parameters:  - msg  :the point of input data.
*                - flag :1:NMG SETTING; 0:Init Setting.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/
int cmd_set_gn_mode(_MSG_NODE *msg, int flag)
{
    int len;
	
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    len = strlen(msg->data);
	
	if((strncmp1(msg->data,"GPS",3) != 0)&&(strncmp1(msg->data,"BD",3) != 0)&&(strncmp1(msg->data,"MIX",3) != 0)
		&&(strncmp1(msg->data,"GLO",3)!= 0)&&(strncmp1(msg->data,"GAL",3)!= 0))
	 	return 1;
	
    if(msg->solt == GBTP1_SLOT)
    {
        memset(conf_content.slot_q.mode, '\0', 4);
        memcpy(conf_content.slot_q.mode, msg->data, len);  
    }
    else if(msg->solt == GBTP2_SLOT)
    {
        memset(conf_content.slot_r.mode, '\0', 4);
        memcpy(conf_content.slot_r.mode, msg->data, len);
    }
    else
    {
        return 1;
    }

	sprintf((char *)sendbuf, "SET-GBTP-MODE:%s::%s;", msg->ctag, msg->data);
	
    if(flag)
    {
            sendtodown_cli(sendbuf, msg->solt, msg->ctag);
            save_config();
    }
    else
    {
            sendtodown(sendbuf, msg->solt);
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int cmd_set_gb_en(_MSG_NODE *msg, int flag)
*	Descript: gbtpv2 work mode setting.
*	Parameters:  - msg  :the point of input data.
*                - flag :1:NMG SETTING; 0:Init Setting.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/
int cmd_set_gb_en(_MSG_NODE *msg, int flag)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);
    if((msg->data[0] != '0') && (msg->data[0] != '1'))
    {
        return 1;
    }
    if(msg->solt == GBTP1_SLOT)
    {
        
            memset(conf_content.slot_q.mask, '\0', 3);
            memcpy(conf_content.slot_q.mask, msg->data, 2);
    }
    else if(msg->solt == GBTP2_SLOT)
    {
            memset(conf_content.slot_r.mask, '\0', 3);
            memcpy(conf_content.slot_r.mask, msg->data, 2);
    }
    else
    {
        return 1;
    }
	
	sprintf((char *)sendbuf, "SET-GBTP-SATEN:%s::%s;", msg->ctag, msg->data);
	if(flag)
    {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
    }
    else
    {
                sendtodown(sendbuf, msg->solt);
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
/*
*********************************************************************************************************
*	Function: int cmd_set_gn_dly(_MSG_NODE *msg, int flag)
*	Descript: gbtpv2 work mode setting.
*	Parameters:  - msg  :the point of input data.
*                - flag :1:NMG SETTING; 0:Init Setting.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/
int is_digit1(unsigned char *parg,unsigned char len)
{
	int i;
	int usError;
	usError = 0;
	
	for(i = 0; i < len; i++)
	{
		if(i == 0)
		{
		   if(parg[0] != '-')
		   {
				if(parg[0] < 0x30 || parg[0] > 0x39)
				{
					usError = 1;
						break;
				}
		   }
		}
		else if(parg[i] < 0x30 || parg[i] > 0x39)
		{
				usError = 1;
				break;
		}
	}
	return usError;
	
}

int cmd_set_gn_dly(_MSG_NODE *msg, int flag)
{
    int len, tmp_value;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    len = strlen(msg->data);
    /*set delay rang -32768 ~ 32767*/
	if(len <= 0 || len > 6)
		return 1;
    /*judge the ascii (0 ~ 9)*/
	if(is_digit1(msg->data, len))
	    return 1;

	/*judge delay rang -32768 ~ 32767*/
    tmp_value = atoi(msg->data);
	if(tmp_value < -32768 || tmp_value > 32767)
		return 1;

    if(msg->solt == GBTP1_SLOT)
    {
            memcpy(conf_content3.gbtp_delay[0], msg->data, len);
	        conf_content3.gbtp_delay[0][len] = '\0';
    }
    else if(msg->solt == GBTP2_SLOT)
    {
            memcpy(conf_content3.gbtp_delay[1], msg->data, len);
	        conf_content3.gbtp_delay[1][len] = '\0';
    }
    else
    {
        return 1;
    }
	
	sprintf((char *)sendbuf, "SET-GBTP-DELAY:%s::%s;", msg->ctag, msg->data);
	if(flag)
    {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
    }
    else
    {
                sendtodown(sendbuf, msg->solt);
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}
/*GBTPIII 1PPS OUT DELAY,RANGE OF -400000~ 400000,UNIT 2.5ns*/
int cmd_set_pps_dly(_MSG_NODE *msg, int flag)
{
    int len, tmp_value;
    unsigned char  sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    len = strlen(msg->data);
    /*set delay rang -400000 ~ 400000*/
	if(len <= 0 || len > 10)
		return 1;
    /*judge the ascii (0 ~ 9)*/
	if(is_digit1(msg->data, len))
	    return 1;

	/*judge delay rang -200,000,000 ~ 200,000,000*/
    tmp_value = atoi(msg->data);
	if(tmp_value < -200000000 || tmp_value > 200000000)
		return 1;

    if(msg->solt == GBTP1_SLOT)
    {
            memcpy(conf_content3.gbtp_pps_delay[0], msg->data, len);
	        conf_content3.gbtp_pps_delay[0][len] = '\0';
    }
    else if(msg->solt == GBTP2_SLOT)
    {
            memcpy(conf_content3.gbtp_pps_delay[1], msg->data, len);
	        conf_content3.gbtp_pps_delay[1][len] = '\0';
    }
    else
    {
        return 1;
    }
	
	sprintf((char *)sendbuf, "SET-PPS-DELAY:%s::%s;", msg->ctag, msg->data);
	if(flag)
    {
                sendtodown_cli(sendbuf, msg->solt, msg->ctag);
                save_config();
    }
    else
    {
                sendtodown(sendbuf, msg->solt);
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s\n", NET_INFO, sendbuf);
    }
#endif
    return 0;
}

/*
2015-9-9
*/
int cmd_set_protocol(_MSG_NODE *msg, int flag)
{

    unsigned char sendbuf[SENDBUFSIZE];
	unsigned char i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

	
    if((msg->data[0] != '0') && (msg->data[0] != '1') && (msg->data[0] != '2'))
    {
        return 1;
    }
	else
	{
		conf_content3.mcp_protocol = msg->data[0];
	}
	printf("### cmd_set_protocol:%c\n",msg->data[0]);
	sprintf((char *)sendbuf, "SET-PROTOCOL:%s::%c;", msg->ctag, msg->data[0]);
	for(i=0;i<13;i++)
	{
		printf("### %d--type:%c\n",i,slot_type[i]);
		if( 'I' == slot_type[i] || 	'j' == slot_type[i]  ||						//TP16 PGE4V2
			(slot_type[i] >= 'A' && slot_type[i] <= 'H') || //PTP4\PFO4\PGE4\PGO4\PTP8\PFO8\PGE8\PGO8
			(slot_type[i] >= 'n' && slot_type[i] <= 'q')  )  //PTP\PGE\PTP2\PGE2
		{
			printf("### SENDTODOWN:%s\n",sendbuf);
			if(flag)
			{
				sendtodown_cli(sendbuf, (char)('a'+i), msg->ctag);
			}
			else
			{
				sendtodown(sendbuf, (char)('a'+i));
			}
			
		}
		else
		{
			continue;
		}
	}
	
	save_config();
    respond_success(current_client, msg->ctag);
    return 0;
}

int cmd_set_ppssel(_MSG_NODE *msg, int flag)
{

    unsigned char sendbuf[SENDBUFSIZE];
	//unsigned char i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

	
    if((msg->data[0] < '0') || (msg->data[0] > '6'))
    {
        return 1;
    }
	
	if(msg->solt == RB1_SLOT)
    {
            conf_content3.pps_sel[0] = msg->data[0];   
    }
    else if(msg->solt == RB2_SLOT)
    {
         
            conf_content3.pps_sel[1] = msg->data[0];
    }
    else
    {
        return 1;
    }

	sprintf((char *)sendbuf, "SET-PPS-SEL:%s::%s;", msg->ctag, msg->data);
	if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }
	
    respond_success(current_client, msg->ctag);
    return 0;
}
int cmd_set_tdeven(_MSG_NODE *msg, int flag)
{

    unsigned char sendbuf[SENDBUFSIZE];
	//unsigned char i;
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);

	
    if((msg->data[0] !='0') && (msg->data[0] != '1'))
    {
        return 1;
    }
	
	if(msg->solt == RB1_SLOT)
    {
            conf_content3.tdev_en[0] = msg->data[0];   
    }
    else if(msg->solt == RB2_SLOT)
    {
         
            conf_content3.tdev_en[1] = msg->data[0];
    }
    else
    {
        return 1;
    }

	sprintf((char *)sendbuf, "SET-TDEV-EN:%s::%s;", msg->ctag, msg->data);
	if(flag)
    {
        sendtodown_cli(sendbuf, msg->solt, msg->ctag);
        save_config();
    }
    else
    {
        sendtodown(sendbuf, msg->solt);
    }
	
    respond_success(current_client, msg->ctag);
    return 0;
}

_CMD_NODE g_cmd_fun[] =
{
    //扩展框
    {"QEXT-1-EQT", 10, NULL, cmd_ext1_eqt},
    {"QEXT-2-EQT", 10, NULL, cmd_ext2_eqt},
    {"QEXT-3-EQT", 10, NULL, cmd_ext3_eqt},
    {"QEXT-EQT",8,NULL,cmd_ext_eqt},

    {"QEXT-1-ALM", 10, NULL, cmd_ext1_alm},
    {"QEXT-2-ALM", 10, NULL, cmd_ext2_alm},
    {"QEXT-3-ALM", 10, NULL, cmd_ext3_alm},
    {"QEXT-ALM", 	8, NULL, cmd_ext_alm},

    {"QEXT-1-PRS", 10, NULL, cmd_ext1_prs},
    {"QEXT-2-PRS", 10, NULL, cmd_ext2_prs},
    {"QEXT-3-PRS", 10, NULL, cmd_ext3_prs},

    {"QEXT-DRV-MGR", 12, NULL, cmd_drv_mgr},

    {"QEXT-ONLINE", 11, NULL, cmd_qext_online},
    {"SET-EXT-ONLINE", 14, cmd_set_ext_online, NULL},

    {"RTRV-ALRMS", 10, NULL, cmd_rtrv_alrms},
    {"RTRV-EQPT", 9, NULL, cmd_rtrv_eqpt},
    {"RTRV-PM-INP", 11, NULL, cmd_rtrv_pm_inp},
    {"RTRV-PM-MSG", 11, NULL, cmd_rtrv_pm_msg},
    {"RTRV-OUT", 8, NULL, cmd_rtrv_out},
    {"RTRV-MTC-IP", 11, NULL, cmd_rtrv_mtc_ip},
    {"RTRV-UNI-IP", 11, NULL, cmd_rtrv_uni_ip},
    {"RTRV-MTC-DOM", 12, NULL, cmd_rtrv_mtc_dom},
    {"SET-TID", 7, cmd_set_tid, NULL},

    {"HAND-SHAKE", 10, cmd_hand_shake, NULL},
    {"SET-MAIN", 8, cmd_set_main, NULL},
    {"SET-DAT_S", 9, cmd_set_dat_s, NULL},
    {"SET-DAT", 7, cmd_set_dat, NULL},
    {"SET-NET", 7, cmd_set_net, NULL},
    {"SET-SSM-OEN", 11, cmd_set_out_ssm_en, NULL},
    {"SET-SSM-OTH", 11, cmd_set_out_ssm_oth, NULL},

    {"SET-MAC", 7, cmd_set_mac, NULL},
    {"ED-USER-SECU", 12, cmd_set_password, NULL},
    {"SET-MODE", 8, cmd_set_mode, NULL},
    {"SET-POS", 7, cmd_set_pos, NULL},
    {"SET-MASK-GBTP", 13, cmd_mask_gbtp, NULL},

    {"SET-BDZB", 8, cmd_bdzb, NULL},

    {"SET-SYS-REF", 11, cmd_sys_ref, NULL},
    {"SET-DELY", 8, cmd_set_dely, NULL},
    {"SET-PRIO-INP", 12, cmd_set_prio_inp, NULL},
    {"SET-MASK-E1", 11, cmd_set_mask_e1, NULL},
    {"SET-LEAP-MASK", 13, cmd_set_leap_mask, NULL},

    {"SET-TZO", 7, cmd_set_tzo, NULL},
    {"SET-PTP-NET", 11, cmd_set_ptp_net, NULL},
    {"SET-PTP-MOD", 11, cmd_set_ptp_mod, NULL},
    {"SET-PTP-MTC", 11, cmd_set_ptp_mtc, NULL},
    {"SET-PTP-DOM",11, cmd_set_ptp_dom,NULL},//添加PTP时钟域号设置。
   
    {"SET-OUT-LEV", 11, cmd_set_out_lev, NULL},
    {"SET-OUT-TYP", 11, cmd_set_out_typ, NULL},
    {"SET-OUT-PRR", 11, cmd_set_out_ppr, NULL},

    {"SET-RESET", 9, cmd_set_reset, NULL},
    {"SET-LEAP-TAG", 12, cmd_set_leap_tag, NULL},
    {"SET-TP16-EN", 11, cmd_set_tp16_en, NULL},
    {"SET-CONFIG", 10, cmd_set_CONFIG, NULL},
    {"SET-LEAP-NUM", 12, cmd_set_leap_num, NULL},

    {"SET-REF-PRIO", 12, cmd_set_ref_prio, NULL},
    {"SET-REF-MOD", 11, cmd_set_ref_mod, NULL},
    {"SET-REF-SA", 10, cmd_set_ref_sa, NULL},
    {"SET-REF-PH", 10, cmd_set_ref_ph, NULL},
    {"SET-REF-TLB", 11, cmd_set_ref_tlb, NULL},//增加2Mb信号等级设置，在2Mb设置为网管预设值时有效。
    {"SET-REF-TLM", 11, cmd_set_ref_tlm, NULL},//增加2Mbit信号获取信号等级来源。
    {"SET-REF-TL", 10, cmd_set_ref_tl, NULL},
    {"SET-REF-IEN", 11, cmd_set_ref_ssm_en, NULL},

    {"SET-RS-EN", 9, cmd_set_rs_en, NULL},
    {"SET-RS-TZO", 10, cmd_set_rs_tzo, NULL},
    {"SET-PPX-MOD", 11, cmd_set_ppx_mod, NULL},
    {"SET-TOD-EN", 10, cmd_set_tod_en, NULL},
    {"SET-TOD-PRO", 11, cmd_set_tod_pro, NULL},

    {"SET-IGB-EN", 10, cmd_set_igb_en, NULL},
    {"SET-IGB-RAT", 11, cmd_set_igb_rat, NULL},
    {"SET-IGB-MAX", 11, cmd_set_igb_max, NULL},
    //2015527 set IRIGR16 timezone
    {"SET-IGB-TZONE", 13, cmd_set_igb_tzone, NULL},
    {"SET-NTP-EN", 10, cmd_set_ntp_en, NULL},
    {"SET-NTP-NET", 11, cmd_set_ntp_net, NULL},
    {"SET-NTP-PNET", 12, cmd_set_ntp_pnet, NULL},//new add
    {"SET-NTP-PMAC", 12, cmd_set_ntp_pmac, NULL},//2017/6/9 (new add)
    {"SET-NTP-PEN", 11, cmd_set_ntp_pen, NULL},	//new add
    {"SET-NTP-KEY", 11, cmd_set_ntp_key, NULL},
    {"SET-NTP-MS", 10, cmd_set_ntp_ms, NULL},
    {"SET-MCP-FB", 10, cmd_set_mcp_fb, NULL},
    {"SET-RB-SA", 9, cmd_set_rb_sa, NULL},
    {"SET-RB-TL", 9, cmd_set_rb_tl, NULL},
     {"SET-RB-TH", 9, cmd_set_rb_thresh, NULL},//设置钟盘IRIGB设置门限

    {"SET-OUT-MOD", 11, cmd_set_out_mod, NULL},
    //设置告警屏蔽
    {"SET-ARLM-MASK", 13, cmd_set_alm_msk, NULL},
    {"RTRV-RALM-MASK", 14, NULL, cmd_rtrv_ralm_mask},
    //20141210
    {"SET-LEAP-MOD", 12, cmd_set_leap_mod, NULL},
    {"RTRV-LEAP-MOD", 13, NULL, cmd_rtrv_leap_mod},
    //20150527 查询irigb16时区
    {"RTRV-IGB-TZONE", 13, NULL, cmd_rtrv_igb_tzone},
    {"SYS-CPU", 7, NULL, cmd_sys_cpu},
    //STA-1
    {"SET-STA1-NET", 12, cmd_set_sta_net, NULL},
    //PGEIN
    {"SET-PGEIN-NET", 13, cmd_set_pgein_net, NULL},
    {"SET-PGEIN-MOD", 13, cmd_set_pgein_mod, NULL},
	//PGE4S SET-PGE4S-NET
	{"SET-PGE4V-NET", 13, cmd_set_pge4v_net, NULL},
	{"SET-PGE4V-MOD", 13, cmd_set_pge4v_mod, NULL},
	{"SET-PGE4V-PAR", 13, cmd_set_pge4v_par, NULL},
	{"SET-PGE4V-MTC", 13, cmd_set_pge4v_mtc, NULL},
	{"SET-PGE4V-SFP", 13, cmd_set_pge4v_sfp, NULL},
	//GBTPIIV2 SETTING
    {"SET-GB-MODE", 11, cmd_set_gb_mode, NULL},
    {"SET-GB-EN", 9,    cmd_set_gb_en, NULL},
    //GBTPIII SETTING
    {"SET-GN-MODE", 11, cmd_set_gn_mode, NULL},
    {"SET-GN-EN", 9,    cmd_set_gb_en, NULL},
    {"SET-GN-DLY", 10,  cmd_set_gn_dly, NULL},
    {"SET-PPS-DLY", 11, cmd_set_pps_dly, NULL},
    
     //2015-9-9  联通移动版本选择
    {"SET-PROTOCOL", 12, cmd_set_protocol, NULL},
    {"SET-PPS-SEL", 11, cmd_set_ppssel, NULL},
    {"SET-TDEV-EN", 11, cmd_set_tdeven, NULL},
    
};
int _CMD_NODE_MAX = ((int)(sizeof(g_cmd_fun) / sizeof(_CMD_NODE))); //2014.12.5

<file_sep>/MCPU/ext_trpt.c
#include <stdio.h>
#include <string.h>

#include "ext_ctx.h"
#include "ext_trpt.h"
#include "ext_cmn.h"
#include "mcp_process.h"
//#include "memwatch.h"





/*
  1	成功
  0	失败
*/
u8_t ext_out_ssm_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int i, j;
    int slotid;
    u8_t sendbuf[128];

    //printf("--%s--\n", ext_data);

    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; (i < 20) && (j < 10); i += 2, j++)
        {
            if( EXT_OUT16 == ctx->extBid[0][j] ||
                    EXT_OUT32 == ctx->extBid[0][j] ||
                    EXT_OUT16S == ctx->extBid[0][j] ||
                    EXT_OUT32S == ctx->extBid[0][j] )
            {
                if(' ' != ext_data[i] && ' ' != ext_data[i + 1])
                {
                    memcpy(ctx->out[j].outSsm, &ext_data[i], 2);
                    //memcpy(ctx->save.outSsm[j], &ext_data[i], 2);
                    if(0 != memcmp(ctx->out[j].outSsm, ctx->save.outSsm[j], 2))
                    {
                        //lev
                        if(2 == strlen(ctx->save.outSsm[j]))
                        {
                            memcpy(ctx->out[j].outSsm, ctx->save.outSsm[j], 2);
                            memset(sendbuf, 0, 128);
                            sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                                    j + EXT_1_OUT_LOWER,
                                    "000000",
                                    ctx->save.outSsm[j]);
                            ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, j + EXT_1_OUT_LOWER, "000000");
                        }
                    }
                }
            }
        }
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; (i < 20) && (j < 10); i += 2, j++)
        {
            if( EXT_OUT16 == ctx->extBid[1][j] ||
                    EXT_OUT32 == ctx->extBid[1][j] ||
                    EXT_OUT16S == ctx->extBid[1][j] ||
                    EXT_OUT32S == ctx->extBid[1][j] )
            {
                if(' ' != ext_data[i] && ' ' != ext_data[i + 1])
                {
                    memcpy(ctx->out[10 + j].outSsm, &ext_data[i], 2);
                    //memcpy(ctx->save.outSsm[10+j], &ext_data[i], 2);
                    if(0 != memcmp(ctx->out[10 + j].outSsm, ctx->save.outSsm[10 + j], 2))
                    {
                        //lev
                        if(2 == strlen(ctx->save.outSsm[10 + j]))
                        {
                            memcpy(ctx->out[10 + j].outSsm, ctx->save.outSsm[10 + j], 2);
                            memset(sendbuf, 0, 128);
                            sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                                    j + EXT_2_OUT_LOWER,
                                    "000000",
                                    ctx->save.outSsm[10 + j]);
                            ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, j + EXT_2_OUT_LOWER, "000000");
                        }
                    }
                }
            }
        }
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; (i < 20) && (j < 10); i += 2, j++)
        {
            if( EXT_OUT16 == ctx->extBid[2][j] ||
                    EXT_OUT32 == ctx->extBid[2][j] ||
                    EXT_OUT16S == ctx->extBid[2][j] ||
                    EXT_OUT32S == ctx->extBid[2][j] )
            {
                if(' ' != ext_data[i] && ' ' != ext_data[i + 1])
                {
                    memcpy(ctx->out[20 + j].outSsm, &ext_data[i], 2);
                    //memcpy(ctx->save.outSsm[20+j], &ext_data[i], 2);
                    if(0 != memcmp(ctx->out[20 + j].outSsm, ctx->save.outSsm[20 + j], 2))
                    {
                        if((j + EXT_3_OUT_LOWER) >= ':')//except ':;'
                        {
                            slotid = j + EXT_3_OUT_LOWER + 2;
                        }
                        else
                        {
                            slotid = j + EXT_3_OUT_LOWER;
                        }

                        //lev
                        if(2 == strlen(ctx->save.outSsm[20 + j]))
                        {
                            memcpy(ctx->out[20 + j].outSsm, ctx->save.outSsm[20 + j], 2);
                            memset(sendbuf, 0, 128);
                            sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                                    slotid,
                                    "000000",
                                    ctx->save.outSsm[20 + j]);
                            ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, "000000");
                        }
                    }
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}







/*
  1	成功
  0	失败
*/
u8_t ext_out_signal_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int i;
    u8_t bid;
    int slotid;
    u8_t sendbuf[128];

    /*
      对于OUT16，分为两组（1-8  9-16），
          bit0-bit3表示1-8路，bit4-bit7表示9-16路。
      对于OUT32，分为四组（1-4  5-8  9-12  13-16），
          bit0-bit1表示1-4路，bit2-bit3表示5-8路，bit4-bit5表示9-12路，bit6-bit7表示13-16路。
      0：2.048Mhz
      1：2.048Mbit/s
      2：无输出
    */
    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        for(i = 0; i < 10; i++)
        {
            bid = ctx->extBid[0][i];
            if(EXT_OUT16 == bid || EXT_OUT16S == bid)
            {
                if(0 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[i].outSignal, "00000000", 8);
                }
                else if(1 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[i].outSignal, "11111111", 8);
                }
                else if(2 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[i].outSignal, "22222222", 8);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[i].outSignal[8]), "00000000", 8);
                }
                else if(1 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[i].outSignal[8]), "11111111", 8);
                }
                else if(2 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[i].outSignal[8]), "22222222", 8);
                }
                else
                {
                    return 0;
                }

                //memcpy(ctx->save.outSignal[i], ctx->out[i].outSignal, 16);
                if(0 != memcmp(ctx->out[i].outSignal, ctx->save.outSignal[i], 16))
                {
                    //signal
                    if(16 == strlen(ctx->save.outSignal[i]))
                    {
                        memcpy(ctx->out[i].outSignal, ctx->save.outSignal[i], 16);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                                i + EXT_1_OUT_LOWER,
                                "000000",
                                ctx->save.outSignal[i]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_1_OUT_LOWER, "000000");
                    }
                }
            }
            else if(EXT_OUT32 == bid || EXT_OUT32S == bid)
            {
                if(0 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[i].outSignal, "0000", 4);
                }
                else if(1 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[i].outSignal, "1111", 4);
                }
                else if(2 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[i].outSignal, "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[4]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[4]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[4]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[8]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[8]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[8]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[12]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[12]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[i].outSignal[12]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                //memcpy(ctx->save.outSignal[i], ctx->out[i].outSignal, 16);
                if(0 != memcmp(ctx->out[i].outSignal, ctx->save.outSignal[i], 16))
                {
                    //signal
                    if(16 == strlen(ctx->save.outSignal[i]))
                    {
                        memcpy(ctx->out[i].outSignal, ctx->save.outSignal[i], 16);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                                i + EXT_1_OUT_LOWER,
                                "000000",
                                ctx->save.outSignal[i]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_1_OUT_LOWER, "000000");
                    }
                }
            }
            else
            {
                //
            }
        }
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        for(i = 0; i < 10; i++)
        {
            bid = ctx->extBid[1][i];
            if(EXT_OUT16 == bid || EXT_OUT16S == bid)
            {
                if(0 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[10 + i].outSignal, "00000000", 8);
                }
                else if(1 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[10 + i].outSignal, "11111111", 8);
                }
                else if(2 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[10 + i].outSignal, "22222222", 8);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[8]), "00000000", 8);
                }
                else if(1 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[8]), "11111111", 8);
                }
                else if(2 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[8]), "22222222", 8);
                }
                else
                {
                    return 0;
                }

                //memcpy(ctx->save.outSignal[10+i], ctx->out[10+i].outSignal, 16);
                if(0 != memcmp(ctx->out[10 + i].outSignal, ctx->save.outSignal[10 + i], 16))
                {
                    //signal
                    if(16 == strlen(ctx->save.outSignal[10 + i]))
                    {
                        memcpy(ctx->out[10 + i].outSignal, ctx->save.outSignal[10 + i], 16);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                                i + EXT_2_OUT_LOWER,
                                "000000",
                                ctx->save.outSignal[10 + i]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_2_OUT_LOWER, "000000");
                    }
                }
            }
            else if(EXT_OUT32 == bid || EXT_OUT32S == bid)
            {
                if(0 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[10 + i].outSignal, "0000", 4);
                }
                else if(1 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[10 + i].outSignal, "1111", 4);
                }
                else if(2 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[10 + i].outSignal, "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[4]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[4]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[4]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[8]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[8]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[8]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[12]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[12]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[10 + i].outSignal[12]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                //memcpy(ctx->save.outSignal[10+i], ctx->out[10+i].outSignal, 16);
                if(0 != memcmp(ctx->out[10 + i].outSignal, ctx->save.outSignal[10 + i], 16))
                {
                    //signal
                    printf("2+%d+%s\n", i + 1, ctx->save.outSignal[10 + i]);
                    if(16 == strlen(ctx->save.outSignal[10 + i]))
                    {
                        memcpy(ctx->out[10 + i].outSignal, ctx->save.outSignal[10 + i], 16);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                                i + EXT_2_OUT_LOWER,
                                "000000",
                                ctx->save.outSignal[10 + i]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_2_OUT_LOWER, "000000");
                    }
                }
            }
            else
            {
                //
            }
        }
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        for(i = 0; i < 10; i++)
        {
            bid = ctx->extBid[2][i];
            if(EXT_OUT16 == bid || EXT_OUT16S == bid)
            {
                if(0 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[20 + i].outSignal, "00000000", 8);
                }
                else if(1 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[20 + i].outSignal, "11111111", 8);
                }
                else if(2 == (ext_data[i] & 0x0F))
                {
                    memcpy(ctx->out[20 + i].outSignal, "22222222", 8);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[8]), "00000000", 8);
                }
                else if(1 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[8]), "11111111", 8);
                }
                else if(2 == ((ext_data[i] >> 4) & 0x0F))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[8]), "22222222", 8);
                }
                else
                {
                    return 0;
                }

                //memcpy(ctx->save.outSignal[20+i], ctx->out[20+i].outSignal, 16);
                if(0 != memcmp(ctx->out[20 + i].outSignal, ctx->save.outSignal[20 + i], 16))
                {
                    if((i + EXT_3_OUT_LOWER) >= ':')//except ':;'
                    {
                        slotid = i + EXT_3_OUT_LOWER + 2;
                    }
                    else
                    {
                        slotid = i + EXT_3_OUT_LOWER;
                    }

                    //signal
                    if(16 == strlen(ctx->save.outSignal[20 + i]))
                    {
                        memcpy(ctx->out[20 + i].outSignal, ctx->save.outSignal[20 + i], 16);
                        printf("3+%d+%s\n", i + 1, ctx->save.outSignal[20 + i]);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                                slotid,
                                "000000",
                                ctx->save.outSignal[20 + i]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, "000000");
                    }
                }
            }
            else if(EXT_OUT32 == bid || EXT_OUT32S == bid)
            {
                if(0 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[20 + i].outSignal, "0000", 4);
                }
                else if(1 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[20 + i].outSignal, "1111", 4);
                }
                else if(2 == (ext_data[i] & 0x03))
                {
                    memcpy(ctx->out[20 + i].outSignal, "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[4]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[4]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 2) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[4]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[8]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[8]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 4) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[8]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                if(0 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[12]), "0000", 4);
                }
                else if(1 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[12]), "1111", 4);
                }
                else if(2 == ((ext_data[i] >> 6) & 0x03))
                {
                    memcpy(&(ctx->out[20 + i].outSignal[12]), "2222", 4);
                }
                else
                {
                    return 0;
                }

                //memcpy(ctx->save.outSignal[20+i], ctx->out[20+i].outSignal, 16);
                if(0 != memcmp(ctx->out[20 + i].outSignal, ctx->save.outSignal[20 + i], 16))
                {
                    if((i + EXT_3_OUT_LOWER) >= ':')//except ':;'
                    {
                        slotid = i + EXT_3_OUT_LOWER + 2;
                    }
                    else
                    {
                        slotid = i + EXT_3_OUT_LOWER;
                    }

                    //signal
                    if(16 == strlen(ctx->save.outSignal[20 + i]))
                    {
                        memcpy(ctx->out[20 + i].outSignal, ctx->save.outSignal[20 + i], 16);
                        printf("3+%d+%s\n", i + 1, ctx->save.outSignal[20 + i]);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                                slotid,
                                "000000",
                                ctx->save.outSignal[20 + i]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, "000000");
                    }
                }
            }
            else
            {
                //
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}







/*
  1	成功
  0	失败
*/
u8_t ext_out_mode_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int i, j;
    int slotid;
    u8_t sendbuf[128];

    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; i < 20 && j < 10; i += 2, j++)
        {
            if( EXT_OUT16 == ctx->extBid[0][j] ||
                    EXT_OUT32 == ctx->extBid[0][j] ||
                    EXT_OUT16S == ctx->extBid[0][j] ||
                    EXT_OUT32S == ctx->extBid[0][j] )
            {
                if(' ' != ext_data[i] && ' ' != ext_data[i + 1])
                {
                    ctx->out[j].outPR[0] = ext_data[i];
                    ctx->out[j].outPR[1] = ',';
                    ctx->out[j].outPR[2] = ext_data[i + 1];
                }

                /*ctx->save.outPR[j][0] = ext_data[i];
                ctx->save.outPR[j][1] = ',';
                ctx->save.outPR[j][2] = ext_data[i+1];*/
                if(0 != memcmp(ctx->out[j].outPR, ctx->save.outPR[j], 3))
                {
                    //mod
                    printf("1+%d+%s\n", j + 1, ctx->save.outPR[j]);
                    if('0' == ctx->save.outPR[j][0] || '1' == ctx->save.outPR[j][0])
                    {
                        memcpy(ctx->out[j].outPR, ctx->save.outPR[j], 1);

                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%c;",
                                j + EXT_1_OUT_LOWER,
                                "000000",
                                ctx->save.outPR[j][0]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, j + EXT_1_OUT_LOWER, "000000");
                    }
                }
            }
        }
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; i < 20 && j < 10; i += 2, j++)
        {
            if( EXT_OUT16 == ctx->extBid[1][j] ||
                    EXT_OUT32 == ctx->extBid[1][j] ||
                    EXT_OUT16S == ctx->extBid[1][j] ||
                    EXT_OUT32S == ctx->extBid[1][j] )
            {
                if(' ' != ext_data[i] && ' ' != ext_data[i + 1])
                {
                    ctx->out[10 + j].outPR[0] = ext_data[i];
                    ctx->out[10 + j].outPR[1] = ',';
                    ctx->out[10 + j].outPR[2] = ext_data[i + 1];
                }

                /*ctx->save.outPR[10+j][0] = ext_data[i];
                ctx->save.outPR[10+j][1] = ',';
                ctx->save.outPR[10+j][2] = ext_data[i+1];*/
                if(0 != memcmp(ctx->out[10 + j].outPR, ctx->save.outPR[10 + j], 3))
                {
                    //mod
                    printf("2+%d+%s\n", j + 1, ctx->save.outPR[10 + j]);
                    if('0' == ctx->save.outPR[10 + j][0] || '1' == ctx->save.outPR[10 + j][0])
                    {
                        memcpy(ctx->out[10 + j].outPR, ctx->save.outPR[10 + j], 1);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%c;",
                                j + EXT_2_OUT_LOWER,
                                "000000",
                                ctx->save.outPR[10 + j][0]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, j + EXT_2_OUT_LOWER, "000000");
                    }
                }
            }
        }
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; i < 20 && j < 10; i += 2, j++)
        {
            if( EXT_OUT16 == ctx->extBid[2][j] ||
                    EXT_OUT32 == ctx->extBid[2][j] ||
                    EXT_OUT16S == ctx->extBid[2][j] ||
                    EXT_OUT32S == ctx->extBid[2][j] )
            {
                if(' ' != ext_data[i] && ' ' != ext_data[i + 1])
                {
                    ctx->out[20 + j].outPR[0] = ext_data[i];
                    ctx->out[20 + j].outPR[1] = ',';
                    ctx->out[20 + j].outPR[2] = ext_data[i + 1];
                }

                /*ctx->save.outPR[20+j][0] = ext_data[i];
                ctx->save.outPR[20+j][1] = ',';
                ctx->save.outPR[20+j][2] = ext_data[i+1];*/
                if(0 != memcmp(ctx->out[20 + j].outPR, ctx->save.outPR[20 + j], 3))
                {
                    if((j + EXT_3_OUT_LOWER) >= ':')//except ':;'
                    {
                        slotid = j + EXT_3_OUT_LOWER + 2;
                    }
                    else
                    {
                        slotid = j + EXT_3_OUT_LOWER;
                    }

                    //mod
                    printf("3+%d+%s\n", j + 1, ctx->save.outPR[20 + j]);
                    if('0' == ctx->save.outPR[20 + j][0] || '1' == ctx->save.outPR[20 + j][0])
                    {
                        memcpy(ctx->out[20 + j].outPR, ctx->save.outPR[20 + j], 1);
                        memset(sendbuf, 0, 128);
                        sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%c;",
                                slotid,
                                "000000",
                                ctx->save.outPR[20 + j][0]);
                        ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, "000000");
                    }
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}








/*
  1	成功
  0	失败
*/
u8_t ext_out_bid_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        memcpy(ctx->extBid[0], ext_data, 14);
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        memcpy(ctx->extBid[1], ext_data, 14);
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        //printf("<ext_out_bid_trpt>:%s\n",ext_data);
        memcpy(ctx->extBid[2], ext_data, 14);
    }
    else
    {
        return 0;
    }

    return 1;
}









/*
  1	成功
  0	失败
*/
u8_t ext_mgr_pr_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        ctx->mgr[0].mgrPR = ext_data[0] - '0';
        ctx->mgr[1].mgrPR = ext_data[1] - '0';
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        ctx->mgr[2].mgrPR = ext_data[0] - '0';
        ctx->mgr[3].mgrPR = ext_data[1] - '0';
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        ctx->mgr[4].mgrPR = ext_data[0] - '0';
        ctx->mgr[5].mgrPR = ext_data[1] - '0';
    }
    else
    {
        return 0;
    }

    return 1;
}








/*
  1	成功
  0	失败
*/
u8_t ext_out_alm_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    int i, j;
    u8_t tmp[7];

    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; i < 30 && j < 10; i += 4, j++)
        {
            memset(tmp, 0, 7);
            tmp[0] = 0x40 | (ext_data[i + 0] & 0x0F);
            tmp[1] = 0x40 | ((ext_data[i + 1] & 0x03) << 2) | ((ext_data[i + 0] >> 4) & 0x03);
			
            tmp[2] = 0x40 | ((ext_data[i + 1] >> 2) & 0x0F);
			
            tmp[3] = 0x40 | (ext_data[i + 2] & 0x0F);
			
            tmp[4] = 0x40 | ((ext_data[i + 3] & 0x03) << 2) | ((ext_data[i + 2] >> 4) & 0x03);
			
            tmp[5] = 0x40 |((ext_data[i + 3] >> 2) & 0x0F);
            memcpy(ctx->out[j].outAlm, tmp, 6);
        }
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; i < 30 && j < 10; i += 4, j++)
        {
            memset(tmp, 0, 7);
            tmp[0] = 0x40 | (ext_data[i + 0] & 0x0F);
            tmp[1] = 0x40 | ((ext_data[i + 1] & 0x03) << 2) | ((ext_data[i + 0] >> 4) & 0x03);
			
            tmp[2] = 0x40 | ((ext_data[i + 1] >> 2) & 0x0F);
			
            tmp[3] = 0x40 | (ext_data[i + 2] & 0x0F);
			
            tmp[4] = 0x40 | ((ext_data[i + 3] & 0x03) << 2) | ((ext_data[i + 2] >> 4) & 0x03);
			
            tmp[5] = 0x40 |((ext_data[i + 3] >> 2) & 0x0F);
            memcpy(ctx->out[10 + j].outAlm, tmp, 6);
        }
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        for(i = 0, j = 0; i < 30 && j < 10; i += 4, j++)
        {
            memset(tmp, 0, 7);
            tmp[0] = 0x40 | (ext_data[i + 0] & 0x0F);
            tmp[1] = 0x40 | ((ext_data[i + 1] & 0x03) << 2) | ((ext_data[i + 0] >> 4) & 0x03);
            tmp[2] = 0x40 | ((ext_data[i + 1] >> 2) & 0x0F);
            tmp[3] = 0x40 | (ext_data[i + 2] & 0x0F);
            tmp[4] = 0x40 | ((ext_data[i + 3] & 0x03) << 2) | ((ext_data[i + 2] >> 4) & 0x03);
            tmp[5] = 0x40 |((ext_data[i + 3] >> 2) & 0x0F);
            memcpy(ctx->out[20 + j].outAlm, tmp, 6);
        }
    }
    else
    {
        return 0;
    }

    return 1;
}









/*
  1	成功
  0	失败
*/
u8_t ext_out_ver_trpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx)
{
    //drv
    if('m' == ext_sid)
    {
        memcpy(ctx->drv[0].drvVer, ext_data, 22);
        printf("%s\n", ctx->drv[0].drvVer);
    }
    else if('n' == ext_sid)
    {
        memcpy(ctx->drv[1].drvVer, ext_data, 22);
    }

    //mgr
    else if(EXT_1_MGR_PRI == ext_sid)
    {
        memcpy(ctx->mgr[0].mgrVer, ext_data, 22);
    }
    else if(EXT_1_MGR_RSV == ext_sid)
    {
        memcpy(ctx->mgr[1].mgrVer, ext_data, 22);
    }

    else if(EXT_2_MGR_PRI == ext_sid)
    {
        memcpy(ctx->mgr[2].mgrVer, ext_data, 22);
    }
    else if(EXT_2_MGR_RSV == ext_sid)
    {
        memcpy(ctx->mgr[3].mgrVer, ext_data, 22);
    }

    else if(EXT_3_MGR_PRI == ext_sid)
    {
        memcpy(ctx->mgr[4].mgrVer, ext_data, 22);
    }
    else if(EXT_3_MGR_RSV == ext_sid)
    {
        memcpy(ctx->mgr[5].mgrVer, ext_data, 22);
    }

    //out
    else if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
        memcpy(ctx->out[ext_sid - EXT_1_OUT_OFFSET].outVer, ext_data, 22);
    }
    else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
    {
        if(',' == ext_sid)
        {
            memcpy(ctx->out[ext_sid - EXT_2_OUT_OFFSET].outVer, ext_data + 1, 22);
        }
        else
        {
            memcpy(ctx->out[ext_sid - EXT_2_OUT_OFFSET].outVer, ext_data, 22);
        }
    }
    else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
    {
        if(ext_sid < 58)//except ':;'
        {
            memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET].outVer, ext_data, 22);
        }
        else
        {
            memcpy(ctx->out[ext_sid - EXT_3_OUT_OFFSET - 2].outVer, ext_data, 22);
        }
    }

    else
    {
        return 0;
    }

    return 1;
}








<file_sep>/MCPU/mcp_save.c
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/msg.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <ctype.h>

#include "ext_global.h"
#include "ext_crpt.h"
#include "ext_trpt.h"

#include "mcp_main.h"
#include "mcp_process.h"

#include "mcp_def.h"
#include "mcp_save_struct.h"

#include "mcp_set_struct.h"
//#include "memwatch.h"


//#define ErrorOK  0


/*
*********************************************************************************************************
*	Function: int save_gb_rcv_mode(char slot, unsigned char *parg)
*	Descript: gbtpv2 alarm infomation.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/

int save_gb_rcv_mode(char slot, unsigned char *parg)
{
    /*<acmode>*/
    //int len;

    if(slot == GBTP1_SLOT)
    {
        memset(rpt_content.slot_q.gb_acmode, 0, 4);
        memcpy(rpt_content.slot_q.gb_acmode, parg, strlen(parg));
    }
    else if(slot == GBTP2_SLOT)
    {
        memset(rpt_content.slot_r.gb_acmode, 0, 4);
        memcpy(rpt_content.slot_r.gb_acmode, parg, strlen(parg));
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif

    return 0;
}

int save_gb_work_mode(char slot, unsigned char *parg)
{
    /*<pos_mode>*/
    //int len;
    if(slot == GBTP1_SLOT)
    {
        memcpy(rpt_content.slot_q.gb_pos_mode, parg, 4);

    }
    else if(slot == GBTP2_SLOT)
    {
        memcpy(rpt_content.slot_r.gb_pos_mode, parg, 4);
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int save_gb_mask(char slot, unsigned char *parg)
*	Descript: gbtpv2 enble setting event.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/

int save_gb_mask(char slot, unsigned char *parg)
{
    int num;//, i, len
    num = (int)(slot - 'a');
    
    if((parg[0]!='0') && (parg[0]!='1'))

	   return 1;
	
    if(slot == GBTP1_SLOT)
    {
        if(slot_type[num] != 'v')
        {
           memcpy(rpt_content.slot_q.gb_mask, parg, 2);
		   rpt_content.slot_q.gb_mask[2] = '\0';
		   
        }
		else 
		{
		   rpt_content.slot_q.staen = parg[0];
		   //rpt_content.slot_q.gb_mask[0] = parg[0];
		   //rpt_content.slot_q.gb_mask[1] = '\0';
		}
			
    }
    else if(slot == GBTP2_SLOT)
    {
        if(slot_type[num] != 'v')
        {
        	memcpy(rpt_content.slot_r.gb_mask, parg, 2);
			rpt_content.slot_r.gb_mask[2] = '\0';
        }
		else 
		{
		   rpt_content.slot_r.staen = parg[0];
		   //rpt_content.slot_r.gb_mask[0] = parg[0];
		   //rpt_content.slot_r.gb_mask[1] = '\0';
		}
		
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_gb_bdzb(char slot, unsigned char *parg)
{
    int len = 0;
    if((strncmp1(parg, "BJ54", 4) != 0) && (strncmp1(parg, "CGS2000", 7) != 0))
    {
        return 1;
    }
    len = strlen(parg);
    if(slot == GBTP1_SLOT)
    {
        memcpy(rpt_content.slot_q.bdzb, parg, len	);
    }
    else if(slot == GBTP2_SLOT)
    {
        memcpy(rpt_content.slot_r.bdzb, parg, len);
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_rb_msmode(char slot, unsigned char *parg)
{
    unsigned char buf[4];
    /*<msmode>::=MAIN|STAN*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rb_msmode, parg, 4);
        get_out_lev(0, 0);
        memcpy(buf, rpt_content.slot_o.tod_buf, 3);
        //printf("<save_rb_msmode>:%s\n",buf);
        send_tod(buf, 'o');
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rb_msmode, parg, 4);
        get_out_lev(0, 0);
        memcpy(buf, rpt_content.slot_p.tod_buf, 3);
        //printf("<save_rb_msmode>:%s\n",buf);
        send_tod(buf, 'p');
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_msmode);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_msmode);
        }
    }
#endif

    return 0;
}

int save_rb_src(char slot, unsigned char *parg)
{
    /*<ref>::= y */
    //int flag_lev=0;
    //int i;
    if(slot == RB1_SLOT)
    {
        rpt_content.slot_o.rb_ref = parg[0];
        get_out_lev(0, 0);
    }
    else if(slot == RB2_SLOT)
    {
        rpt_content.slot_p.rb_ref = parg[0];
        get_out_lev(0, 0);
    }
    /*#if DEBUG_NET_INFO
    	if(slot==RB1_SLOT)
    	{
    		printf("%s:<%c>%s#\n",NET_SAVE,slot,rpt_content.slot_o.rb_ref);
    	}
    	else if(slot==RB2_SLOT)
    	{
    		printf("%s:<%c>%s#\n",NET_SAVE,slot,rpt_content.slot_p.rb_ref);
    	}
    #endif*/
    return 0;
}

int save_rb_prio(char slot, unsigned char *parg)
{
    /*<priority>::=xxxxxxxx*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rb_prio, parg, 8);
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rb_prio, parg, 8);
    }

#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_prio);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_prio);
        }
    }
#endif

    return 0;
}

int save_rb_mask(char slot, unsigned char *parg)
{
    /*<mask>::=xxxxxxxx*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rb_mask, parg, 8);
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rb_mask, parg, 8);
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_mask);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_mask);
        }
    }
#endif

    return 0;
}

int save_rf_tzo(char slot, unsigned char *parg)
{
    /*<we>,<out>*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rf_tzo, parg, 3);
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rf_tzo, parg, 3);
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rf_tzo);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rf_tzo);
        }
    }
#endif

    return 0;
}

int save_rb_dey_chg(char slot, unsigned char *parg)
{
    /**/
    int num;
    num = (int)(parg[0] - 0x31);
    if(slot == RB1_SLOT)
    {
    	memset(rpt_content.slot_o.rf_dey[num], 0, 9);
        memcpy(rpt_content.slot_o.rf_dey[num], &parg[2], strlen(&parg[2]));
    }
    else if(slot == RB2_SLOT)
    {
    	memset(rpt_content.slot_p.rf_dey[num], 0, 9);
        memcpy(rpt_content.slot_p.rf_dey[num], &parg[2], strlen(&parg[2]));
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rf_dey[num]);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rf_dey[num]);
        }
    }
#endif

    return 0;
}

int save_rb_sta_chg(char slot, unsigned char *parg)
{
    /*<trmode>::=TRCK|HOLD|FATR|TEST|FREE*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rb_trmode, parg, 4);
        get_out_lev(0, 0);
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rb_trmode, parg, 4);
        get_out_lev(0, 0);
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_trmode);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_trmode);
        }
    }
#endif

    return 0;
}

int save_rb_leap_chg(char slot, unsigned char *parg)
{
    /*<leap>::=xx*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rb_leap, parg, 2);
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rb_leap, parg, 2);
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_leap);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_leap);
        }
    }
#endif
    return 0;
}

int save_rb_leap_flg(char slot, unsigned char *parg)
{

    /*<leapmask>::=xxxxx*/
    if(slot == RB1_SLOT)
    {
        memcpy(rpt_content.slot_o.rb_leapmask, parg, 5);
    }
    else if(slot == RB2_SLOT)
    {
        memcpy(rpt_content.slot_p.rb_leapmask, parg, 5);
    }
#if DEBUG_NET_INFO
    if(slot == RB1_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_leapmask);
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(print_switch == 0)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_leapmask);
        }
    }
#endif

    return 0;
}

int save_tod_sta(char slot, unsigned char *parg)
{
    int num;
    //	unsigned char tmp[17];
    //	memset(tmp,'\0',sizeof(unsigned char )*17);
    num = (int)(slot - 'a');
    if(slot_type[num] != 'I')
    {
        //
        send_online_framing(num, 'I');
        slot_type[num] = 'I';
    }

    memcpy(rpt_content.slot[num].tp16_tod, parg, 3);
    memcpy(rpt_content.slot[num].tp16_en, &parg[4], 16);

    //rpt_content.slot[num].tp16_en[16]='\0';
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    //printf("<save_tod_sta>:%s\n",parg);
    return 0;
}



int save_out_lev(char slot, unsigned char *parg)
{
    int num, len;

    if(slot >= 'a' && slot <= 'n')
    {
        num = (int)(slot - 'a');
        len = strlen(parg);
        if(2 == len)
        {
            memcpy(rpt_content.slot[num].out_lev, parg, 2);
        }
        else
        {
            return(1);
        }
    }
    else
    {
        if(2 == strlen(parg))
        {
            if(0 == ext_out_ssm_crpt(0, slot, parg, &gExtCtx))
            {
                return 1;
            }
        }
        else
        {
            if(0 == ext_out_ssm_trpt(slot, parg, &gExtCtx))
            {
                return 1;
            }
        }
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif

    return 0;
}


//
//
//
int save_out_typ(char slot, unsigned char *parg)
{
    int num, len;

    if(slot >= 'a' && slot <= 'n')
    {
        num = (int)(slot - 'a');
		
        len = strlen(parg);
        if(16 == len)
        {
            memcpy(rpt_content.slot[num].out_typ, parg, 16);
        }
        else
        {
            return(1);
        }
    }
    else
    {
        if(16 == strlen(parg))
        {
            if(0 == ext_out_signal_crpt(0, slot, parg, &gExtCtx))
            {
                return 1;
            }
        }
        else
        {
            if(0 == ext_out_signal_trpt(slot, parg, &gExtCtx))
            {
                return 1;
            }
        }
    }
	//
	//!OUT32S ADD.
	//
	//printf("out32s type change:%d,%s#\n", num,parg);

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif

    return 0;
}



int save_ptp_net(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1, i2, i3, i4, i5, i6, len;
    j = i1 = i2 = i3 = i4 = i5 = i6 = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
        }
    }
    if(i6 == 0)
    {
        return (1);
    }
    //memcpy(rpt_content.slot[num].ptp_net[port],parg,99);
    //	flag=i2-i1-1;
    //if(flag!=0)
    if(parg[i1] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_ip[port], &parg[i1], i2 - i1 - 1);
    }

    if(parg[i2] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_mac[port], &parg[i2], i3 - i2 - 1);
    }

    if(parg[i3] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_gate[port], &parg[i3], i4 - i3 - 1);
    }

    if(parg[i4] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_mask[port], &parg[i4], i5 - i4 - 1);
    }

    if(parg[i5] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_dns1[port], &parg[i5], i6 - i5 - 1);
    }

    if(parg[i6] != '\0')
    {
        memcpy(rpt_content.slot[num].ptp_dns2[port], &parg[i6], len - i6);
    }

    //printf("\n\n\nLUOHAO------22,Get: rpt_content.slot[%d].ptp_ip[%d]=%s\n\n\n",num,port,rpt_content.slot[num].ptp_ip[port]);

#if DEBUG_NET_INFO
    if(print_switch == 0)
        printf("%s:%s,%s,%s,%s,%s,%s#\n", NET_SAVE, rpt_content.slot[num].ptp_ip[port],
               rpt_content.slot[num].ptp_mac[port], rpt_content.slot[num].ptp_gate[port],
               rpt_content.slot[num].ptp_mask[port], rpt_content.slot[num].ptp_dns1[port],
               rpt_content.slot[num].ptp_dns2[port]);
#endif
    return 0;
}

int save_ptp_mod(char slot, unsigned char *parg)
{
    int i, j, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, len, i12;
    int num, port;
    j = i1 = i2 = i3 = i4 = i5 = i6 = i7 = i8 = i9 = i10 = i11 = i12 = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
	//parg[len] = '\0';
	//printf("ptp recv %s\n",parg);
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
            if(j == 7)
            {
                i7 = i + 1;
            }
            if(j == 8)
            {
                i8 = i + 1;
            }
            if(j == 9)
            {
                i9 = i + 1;
            }
            if(j == 10)
            {
                i10 = i + 1;
            }
            if(j == 11)
            {
                i11 = i + 1;
            }
            if(j == 12)
            {
                i12 = i + 1;
            }
        }
    }
    if(i11 == 0)
    {
        return (1);
    }
    if((parg[i1] == '1') || (parg[i1] == '0') )
    {
        rpt_content.slot[num].ptp_en[port] = parg[i1];
    }
    if((parg[i2] == '1') || (parg[i2] == '0'))
    {
        rpt_content.slot[num].ptp_delaytype[port] = parg[i2];
    }
    if((parg[i3] == '1') || (parg[i3] == '0'))
    {
        rpt_content.slot[num].ptp_multicast[port] = parg[i3];
    }
    if((parg[i4] == '1') || (parg[i4] == '0'))
    {
        rpt_content.slot[num].ptp_enp[port] = parg[i4];
    }
    if((parg[i5] == '1') || (parg[i5] == '0'))
    {
        rpt_content.slot[num].ptp_step[port] = parg[i5];
    }
    if(parg[i6] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_sync[port], &parg[i6], i7 - i6 - 1);
    }
    if(parg[i7] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_announce[port], &parg[i7], i8 - i7 - 1);
    }
    if(parg[i8] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_delayreq[port], &parg[i8], i9 - i8 - 1);
    }
    if(parg[i9] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_pdelayreq[port], &parg[i9], i10 - i9 - 1);
    }
    if(parg[i10] != ',')
    {
        memset(rpt_content.slot[num].ptp_delaycom[port],0,9);
        memcpy(rpt_content.slot[num].ptp_delaycom[port], &parg[i10], i11 - i10 - 1);
    }
    if((parg[i11] == '1') || (parg[i11] == '0'))
    {
        rpt_content.slot[num].ptp_linkmode[port] = parg[i11];
    }
    if((parg[i12] == '1') || (parg[i12] == '0'))
    {
        rpt_content.slot[num].ptp_out[port] = parg[i12];
    }

    //printf("\n\n\nLUOHAO------33,Get: rpt_content.slot[%d].ptp_multicast[%d]=%d\n\n\n",num,port,rpt_content.slot[num].ptp_multicast[port]);

    /*rpt_content.slot[num].ptp_out[port]='1';*/
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s,<%c>#\n", NET_SAVE, parg, rpt_content.slot[num].ptp_linkmode[port]);
    }
#endif
    return 0;
}

int save_ptp_mtc(char slot, unsigned char *parg)
{
    int i, j, i_ip, i_prio1, i_prio2;
    int len, num, port;
    i = j = i_ip = i_prio1 = i_prio2 = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    //
    //!
    //
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i_ip = i + 1;
            }
            if(j == 2)
            {
                i_prio1 = i + 1;
            }
            if(j == 3)
            {
                i_prio2 = i + 1;
            }
        }
    }
    if(i_ip == 0)
    {
        return (1);
    }
    if(parg[i_ip] != ',')
    {
        memset(&rpt_ptp_mtc.ptp_mtc_ip[num][port][0],'\0',9);
        memcpy(&rpt_ptp_mtc.ptp_mtc_ip[num][port][0], &parg[i_ip], 8);
		
    }

    if(parg[i_prio1] != ',')
    {
        memset(&rpt_ptp_mtc.ptp_prio1[num][port][0], 0, 5);
        for (i = i_prio1; parg[i] != ','; i++)
        {
            memcpy(&rpt_ptp_mtc.ptp_prio1[num][port][i - i_prio1], &parg[i], 1);
        }
    }

    if(parg[i_prio2] != ',')
    {
        memset(&rpt_ptp_mtc.ptp_prio2[num][port][0], 0, 5);
        for (i = i_prio2; parg[i] != '\0'; i++) //
        {
            memcpy(&rpt_ptp_mtc.ptp_prio2[num][port][i - i_prio2], &parg[i], 1);
        }
    }
	
    return 0;
}

int save_ptp_mtc16(char slot, unsigned char *parg)
{
    int i, j, i_ip, i_inx;
    int len, num, port,str_len;
    i = j = i_inx = i_ip = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);

	len = strlen(parg);
    str_len = 0;
	
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i_inx = i + 1;
            }
            if(j == 2)
            {
                i_ip = i + 1;
            }
        }
    }
    if(i_inx == 0)
    	return 1;

	if(port < 0 || port > 3)
		return 1;
   
    

	
	
    if(parg[i_inx] == '1')
    {
        //一帧上报8个IP地址，每个IP 8个字节，XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX
	   if((len - 4) < 8 || (len - 4) > 71)
		return 1;

	   memset(&rpt_ptp_mtc.ptp_mtc_ip[num][port][0],'\0',sizeof(rpt_ptp_mtc.ptp_mtc_ip[num][port]));
       memcpy(&rpt_ptp_mtc.ptp_mtc_ip[num][port][0], &parg[i_ip], len - 4);
		
    }
	else if(parg[i_inx] == '2')
	{
	    //一帧上报8个IP地址，每个IP 8个字节，,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX
	    if((len - 4) < 8 || (len - 4) > 72)
			return 1;
		
	    str_len = strlen(rpt_ptp_mtc.ptp_mtc_ip[num][port]);

		//一帧上报8个IP地址，每个IP 8个字节，XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX,XXXXXXXX
		if(str_len > 71)
			return 1;
		
	    memcpy(&rpt_ptp_mtc.ptp_mtc_ip[num][port][0] + str_len , &parg[i_ip], len - 4);
	}
	else
	{
	    return 1;
	}

    return 0;
}

//
//! 2017/6/16 添加PTP闰秒事件上报CC7
//
int save_ptp_sta(char slot, unsigned char *parg)
{
	int num, i;
	int  len;
	
	num = (int)(slot - 'a');
	len = strlen(parg);
	
	if((0 == len)||(2 < len))
	{
		return 1;
	}

	i = atoi(parg);
	if((i < 0)||(i > 99))
		return 1;
	
	if(slot_type[num] == 'v' || slot_type[num] == 'x' )
    {
		if(slot == GBTP1_SLOT)
		{
		    memset(rpt_content.slot_q.leapnum,0,sizeof(rpt_content.slot_q.leapnum));
			memcpy(rpt_content.slot_q.leapnum, &parg[0], len);
		}
		else
		{
			memset(rpt_content.slot_r.leapnum,0,sizeof(rpt_content.slot_r.leapnum));
			memcpy(rpt_content.slot_r.leapnum, &parg[0], len);
		}
    }
    else
    {
		memset(rpt_content.slot[num].ntp_leap,0,sizeof(rpt_content.slot[num].ntp_leap));
		memcpy(rpt_content.slot[num].ntp_leap, &parg[0], len);
    }
	
    //printf("ptp leap %d,%s\n",num,parg);
	return 0;
}

int save_Leap_sta(char slot, unsigned char *parg)
{
	int num, i;
	int  len;
	
	num = (int)(slot - 'a');
	len = strlen(parg);
	
	if((0 == len)||(2 < len))
	{
		return 1;
	}

	i = atoi(parg);
	
	if((i < 0)||(i > 99))
		return 1;
	
	if(slot_type[num] == 'v' || slot_type[num] == 'x')
    {
		if(slot == GBTP1_SLOT)
		{
		    memset(rpt_content.slot_q.leapnum,0,sizeof(rpt_content.slot_q.leapnum));
			memcpy(rpt_content.slot_q.leapnum, &parg[0], len);
		}
		else
		{
			memset(rpt_content.slot_r.leapnum,0,sizeof(rpt_content.slot_r.leapnum));
			memcpy(rpt_content.slot_r.leapnum, &parg[0], len);
		}
    }
    
    //printf("ptp leap %d,%s\n",num,parg);
	return 0;
}

int save_ptp_dom(char slot, unsigned char *parg)
{
	int num,port ;
	int len,i_dom,i,j ;
	
	num = port = 0;
	i = j = len = i_dom = 0;
	
	num = (int)(slot - 'a');
	port = (int)(parg[0] - 0x31);
	len =strlen(parg);
	
	for(i = 0; i < len; i++)
	{
	      if(parg[i] == ',')
	      {
	            j++;
	            if(j == 1)
	            {
	                i_dom = i+1;
	            }
	      }
	}
	if(i_dom != 2)
	    return (1);
	
	if((port < 0)||(port > 4))
		return 1;
	
	if((len == 0)||(len > 5))
		return 1;
	
	if(parg[i_dom] != ',')
	{
	    memset(&rpt_ptp_mtc.ptp_dom[num][port][0],0,4);
	    memcpy(&rpt_ptp_mtc.ptp_dom[num][port][0], &parg[i_dom],len - i_dom );
	}

	return 0;
}

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

int save_gb_sta(char slot, unsigned char *parg)
{
    int i, j, i1, i2, i3, i4, len, i5, i6;
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
    len = strlen(parg);
    /*<pos_mode>,<delay>,<elevation>,<format>,<acmode>
    POST,20,8888,IRIGA,GPS*/
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
        }
    }
    //if(i6==0)/***************/
    //	return (1);
    if(slot == GBTP1_SLOT)
    {
        if(parg[0] != ',')
        {
            memcpy(rpt_content.slot_q.gb_pos_mode, parg, 4);
        }
        if(parg[i2] != ',')
        {
            memcpy(rpt_content.slot_q.gb_elevation, &parg[i2], i3 - i2 - 1);
        }
        if(parg[i3] != ',')
        {
            memcpy(rpt_content.slot_q.gb_format, &parg[i3], i4 - i3 - 1);
        }
        if(parg[i4] != ',')
        {
            memset(rpt_content.slot_q.gb_acmode, 0, 4);
            memcpy(rpt_content.slot_q.gb_acmode, &parg[i4], i5 - i4 - 1);
        }

        if(parg[i5] == '0' || parg[i5] == '1' || parg[i5] == '2')
        {
            rpt_content.slot_q.gb_sta = parg[i5];
        }
        if(parg[i6] == '0')
        {
            memcpy(rpt_content.slot_q.bdzb, "BJ54", 4);
        }
        else if(parg[i6] == '1')
        {
            memcpy(rpt_content.slot_q.bdzb, "CGS2000", 7);
        }
    }
    else if(slot == GBTP2_SLOT)
    {
        if(parg[0] != ',')
        {
            memcpy(rpt_content.slot_r.gb_pos_mode, parg, 4);
        }
        if(parg[i2] != ',')
        {
            memcpy(rpt_content.slot_r.gb_elevation, &parg[i2], i3 - i2 - 1);
        }
        if(parg[i3] != ',')
        {
            memcpy(rpt_content.slot_r.gb_format, &parg[i3], i4 - i3 - 1);
        }
        if(parg[i4] != ',')
        {
            memset(rpt_content.slot_r.gb_acmode, 0, 4);
            memcpy(rpt_content.slot_r.gb_acmode, &parg[i4], i5 - i4 - 1);

        }
        if(parg[i5] == '0' || parg[i5] == '1' || parg[i5] == '2')
        {
            rpt_content.slot_r.gb_sta = parg[i5];
        }
        if(parg[i6] == '0')
        {
            memcpy(rpt_content.slot_r.bdzb, "BJ54", 4);
        }
        else if(parg[i6] == '1')
        {
            memcpy(rpt_content.slot_r.bdzb, "CGS2000", 7);
        }
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int save_gbtp_sta(char slot, unsigned char *parg)
*	Descript: gbtpv2 status infomation.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/
int save_gbtp_sta(char slot, unsigned char *parg)
{
    int i, j, i1, i2, i3, i4, len, i5;//, i6
	int usError;
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
	usError = 0;
    //i6 = 0;
    len = strlen(parg);
    /*<saten>,<acmode>,<linkstatus>,<pos_mode>,<rec_type>,<rec_ver>
    POST,20,8888,IRIGA,GPS*/
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
        }
    }

	if(i5 == 0)
		return 1;
    if(slot == GBTP1_SLOT)
    {
        if((parg[0] == '0')||(parg[0] == '1'))
        {
           rpt_content.slot_q.staen = parg[0];
        }
		if(parg[i1] != ',')
		{
		   //judge the len lt 4.
		   usError = ((i2 - i1 - 1) < sizeof(rpt_content.slot_q.gb_acmode)) ? 0:1;
		   if(usError)
		   	 return 1;
		   
		   //excete memset and memcpy,save data.
		   memset(rpt_content.slot_q.gb_acmode, 0, 4);
		   memcpy(rpt_content.slot_q.gb_acmode, &parg[i1], i2 - i1 - 1);
		   
		}
		if(parg[i2] == '0' || parg[i2] == '1' || parg[i2] == '2')
        {
            rpt_content.slot_q.gb_sta = parg[i2];
        }
        if(parg[i3] != ',')
		{
		    //judge the len et 4.
		    usError = ((i4 - i3 - 1) < sizeof(rpt_content.slot_q.gb_pos_mode)) ? 0:1;
			if(usError)
		   	 return 1;
			
			//excete memset and memcpy,save data.
			memcpy(rpt_content.slot_q.gb_pos_mode,&parg[i3], i4 - i3 - 1);
			rpt_content.slot_q.gb_pos_mode[i4 - i3 - 1] = '\0';
		}
        if(parg[i4] != ',')
        {
            //judge the len et 11.
		    usError = ((i5 - i4 - 1) < sizeof(rpt_content.slot_q.rec_type)) ? 0:1;
			if(usError)
		   	 return 1;
			
			//excete memset and memcpy,save data.
			memcpy(rpt_content.slot_q.rec_type, &parg[i4], i5 - i4 - 1);
			rpt_content.slot_q.rec_type[i5 - i4 - 1] = '\0';
        }
		if(parg[i5] != '\0')
        {
            //judge the len et 9.
		    usError = ((len - i5) < sizeof(rpt_content.slot_q.rec_ver)) ? 0:1;
			if(usError)
		   	 return 1;
			
			//excete memset and memcpy,save data.
			memcpy(rpt_content.slot_q.rec_ver, &parg[i5], len -i5);
			rpt_content.slot_q.rec_ver[len - i5] = '\0';
        }
    }
    else if(slot == GBTP2_SLOT)
    {
        if((parg[0] == '0')||(parg[0] == '1'))
        {
           rpt_content.slot_r.staen = parg[0];
        }
		if(parg[i1] != ',')
		{
		   //judge the len lt 4.
		   usError = ((i2 - i1 - 1) < sizeof(rpt_content.slot_r.gb_acmode)) ? 0:1;
		   if(usError)
		   	 return 1;
		   
		   //excete memset and memcpy,save data.
		   memset(rpt_content.slot_r.gb_acmode, 0, 4);
		   memcpy(rpt_content.slot_r.gb_acmode, &parg[i1], i2 - i1 - 1);
		   
		}
		if(parg[i2] == '0' || parg[i2] == '1' || parg[i2] == '2')
        {
            rpt_content.slot_r.gb_sta = parg[i2];
        }
        if(parg[i3] != ',')
		{
		    //judge the len et 4.
		    usError = ((i4 - i3 - 1) < sizeof(rpt_content.slot_r.gb_pos_mode)) ? 0:1;
			if(usError)
		   	 return 1;
			
			//excete memset and memcpy,save data.
			memcpy(rpt_content.slot_r.gb_pos_mode,&parg[i3], i4 - i3 - 1);
			rpt_content.slot_r.gb_pos_mode[i4 - i3 - 1] = '\0';
		}
        if(parg[i4] != ',')
        {
            //judge the len et 10.
		    usError = ((i5 - i4 - 1) < sizeof(rpt_content.slot_r.rec_type)) ? 0:1;
			if(usError)
		   	 return 1;
			
			//excete memset and memcpy,save data.
			memcpy(rpt_content.slot_r.rec_type, &parg[i4], i5 - i4 - 1);
			rpt_content.slot_r.rec_type[i5 - i4 - 1] = '\0';
        }
		if(parg[i5] != '\0')
        {
            //judge the len et 9.
		    usError = ((len - i5) < sizeof(rpt_content.slot_r.rec_ver)) ? 0:1;
			if(usError)
		   	 return 1;
			
			//excete memset and memcpy,save data.
			memcpy(rpt_content.slot_r.rec_ver, &parg[i5], len -i5);
			rpt_content.slot_r.rec_ver[len - i5] = '\0';
        }
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}


/*
*********************************************************************************************************
*	Function: int save_gbtp_delay(char slot, unsigned char *parg)
*	Descript: gbtpIII delay.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/

int is_digit(unsigned char *parg,unsigned char len)
{
	int i;
	int usError;
	usError = 0;
	
    for(i = 0; i < len; i++)
    {
        if(i == 0)
        {
           if(parg[0] != '-')
           {
           		if(parg[0] < 0x30 || parg[0] > 0x39)
           		{
           			usError = 1;
						break;
           		}
           }
        }
		else if(parg[i] < 0x30 || parg[i] > 0x39)
		{
		 		usError = 1;
				break;
		}
    }
	return usError;
	
}
int save_gbtp_delay(char slot, unsigned char *parg)
{
    int len;
	int usError;
	usError = 0;
    len = strlen(parg);

	/*<delay>; -32768 ~ 32767 */
	if(len <= 0 || len > 6)
		return 1;
	
	/*judge if the parg is or digit*/	
	if(is_digit(parg, len))
		return 1;
         
	     
    if(slot == GBTP1_SLOT)
    {
           //memset(rpt_content.slot_q.delay, '\0', sizeof(rpt_content.slot_q.delay));
           memcpy(rpt_content.slot_q.delay, &parg[0], len);	
		   rpt_content.slot_q.delay[len] = '\0';
    }
    else if(slot == GBTP2_SLOT)
    {
    	   //memset(rpt_content.slot_r.delay, '\0', sizeof(rpt_content.slot_r.delay));
           memcpy(rpt_content.slot_r.delay, &parg[0], len);
		   rpt_content.slot_r.delay[len] = '\0';
    }

	printf("%s:gn_dly,%s#\n", NET_SAVE, parg);
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}
/*GBTPIII 1PPS out delay,range of -400000 ~ 400000,unit 2.5ns*/
int save_pps_delay(char slot, unsigned char *parg)
{
    int len;
	int usError;
	usError = 0;
    len = strlen(parg);

	/*<delay>; -400000 ~ 400000 */
	if(len <= 0 || len > 10)
		return 1;
	
	/*judge if the parg is or digit*/	
	if(is_digit(parg, len))
		return 1;
         
	     
    if(slot == GBTP1_SLOT)
    {
           //memset(rpt_content.slot_q.delay, '\0', sizeof(rpt_content.slot_q.delay));
           memcpy(rpt_content.slot_q.pps_out_delay, &parg[0], len);	
		   rpt_content.slot_q.pps_out_delay[len] = '\0';
    }
    else if(slot == GBTP2_SLOT)
    {
    	   //memset(rpt_content.slot_r.delay, '\0', sizeof(rpt_content.slot_r.delay));
           memcpy(rpt_content.slot_r.pps_out_delay, &parg[0], len);
		   rpt_content.slot_r.pps_out_delay[len] = '\0';
    }
    printf("%s:pps_dly,%s#\n", NET_SAVE, parg);
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_gb_ori(char slot, unsigned char *parg)
{
    if(slot == GBTP1_SLOT)
    {
        memcpy(rpt_content.slot_q.gb_ori, parg, strlen(parg));
    }
    else if(slot == GBTP2_SLOT)
    {
        memcpy(rpt_content.slot_r.gb_ori, parg, strlen(parg));
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

/*********************************************************************************************************
*	Function: int save_gbtp_pst(char slot, unsigned char *parg)
*	Descript: gbtpv2 post time infomation.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************/

int save_gbtp_pst(char slot, unsigned char *parg)
{
   int sLen = 0;
   
   //string length .
   sLen = ((0 == strlen(parg))||(strlen(parg) > 70))? 0:strlen(parg);
   if(!sLen)
   	return 1;

   //copy the data.
    if(slot == GBTP1_SLOT)
    {
        memcpy(rpt_content.slot_q.gb_ori, parg, sLen);
		rpt_content.slot_q.gb_ori[sLen] = '\0';
		
    }
    else if(slot == GBTP2_SLOT)
    {
        memcpy(rpt_content.slot_r.gb_ori, parg, sLen);
		rpt_content.slot_r.gb_ori[sLen] = '\0';
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_gb_rev(char slot, unsigned char *parg)
{

    if(slot == GBTP1_SLOT)
    {
        if(parg[0] == 'G')
        {
            memset(rpt_content.slot_q.gb_rev_g, '\0', 256);
            memcpy(rpt_content.slot_q.gb_rev_g, parg, strlen(parg));
        }
        else if(parg[0] == 'B')
        {
            memset(rpt_content.slot_q.gb_rev_b, '\0', 256);
            memcpy(rpt_content.slot_q.gb_rev_b, parg, strlen(parg));
        }
    }
    else if(slot == GBTP2_SLOT)
    {
        if(parg[0] == 'G')
        {
            memset(rpt_content.slot_r.gb_rev_g, '\0', 256);
            memcpy(rpt_content.slot_r.gb_rev_g, parg, strlen(parg));
        }
        else if(parg[0] == 'B')
        {
            memset(rpt_content.slot_r.gb_rev_b, '\0', 256);
            memcpy(rpt_content.slot_r.gb_rev_b, parg, strlen(parg));
        }
    }
    
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int save_gbtp_info(char slot, unsigned char *parg)
*	Descript: gbtpv2 status infomation.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/
int save_gbtp_info(char slot, unsigned char *parg)
{

    if(slot == GBTP1_SLOT)
    {
        if(parg[0] == 'G' && parg[1] == 'P' && parg[2] == 'S')
        {
            memset(rpt_content.slot_q.gb_rev_g, '\0', 256);
            memcpy(rpt_content.slot_q.gb_rev_g, parg, strlen(parg));
        }
        else if(parg[0] == 'B' && parg[1] == 'D')
        {
            memset(rpt_content.slot_q.gb_rev_b, '\0', 256);
            memcpy(rpt_content.slot_q.gb_rev_b, parg, strlen(parg));
        }
		else if(parg[0] == 'G' && parg[1] == 'L'&& parg[2] == 'O')
        {
            memset(rpt_content.slot_q.gb_rev_glo, '\0', 256);
            memcpy(rpt_content.slot_q.gb_rev_glo, parg, strlen(parg));
        }
		else if(parg[0] == 'G' && parg[1] == 'A'&& parg[2] == 'L')
        {
            memset(rpt_content.slot_q.gb_rev_gal, '\0', 256);
            memcpy(rpt_content.slot_q.gb_rev_gal, parg, strlen(parg));
        }
    }
    else if(slot == GBTP2_SLOT)
    {
        if(parg[0] == 'G' && parg[1] == 'P' && parg[2] == 'S')
        {
            memset(rpt_content.slot_r.gb_rev_g, '\0', 256);
            memcpy(rpt_content.slot_r.gb_rev_g, parg, strlen(parg));
        }
        else if(parg[0] == 'B' && parg[1] == 'D')
        {
            memset(rpt_content.slot_r.gb_rev_b, '\0', 256);
            memcpy(rpt_content.slot_r.gb_rev_b, parg, strlen(parg));
        }
		else if(parg[0] == 'G' && parg[1] == 'L'&& parg[2] == 'O')
        {
            memset(rpt_content.slot_r.gb_rev_glo, '\0', 256);
            memcpy(rpt_content.slot_r.gb_rev_glo, parg, strlen(parg));
        }
		else if(parg[0] == 'G' && parg[1] == 'A'&& parg[2] == 'L')
        {
            memset(rpt_content.slot_r.gb_rev_gal, '\0', 256);
            memcpy(rpt_content.slot_r.gb_rev_gal, parg, strlen(parg));
        }
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_gb_tod(char slot, unsigned char *parg)
{
    unsigned char  sendbuf[SENDBUFSIZE];
    unsigned char  ctag[6] = "000000";

    memset(sendbuf, '\0', SENDBUFSIZE);
    if(slot == GBTP1_SLOT)
    {
        memcpy(rpt_content.slot_q.gb_tod, parg, 3);
    }
    else if(slot == GBTP2_SLOT)
    {
        memcpy(rpt_content.slot_r.gb_tod, parg, 3);
    }
    /**  SET-TOD-STA:<ctag>::<ms>,<ppssta>,<sourcetype>; ***/
    sprintf((char *)sendbuf, "SET-TOD-STA:%s::%d,%s;", ctag, (slot == GBTP1_SLOT) ? 0 : 1, parg);
    //printf("<save_gb_tod>:%s\n",sendbuf);

    if(slot_type[15] == 'K' || slot_type[15] == 'R')
    {
        sendtodown(sendbuf, (unsigned char)RB2_SLOT);
    }
    if(slot_type[14] == 'K' || slot_type[14] == 'R')
    {
        sendtodown(sendbuf, (unsigned char)RB1_SLOT);
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_gb_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[12];
    memset(tmp, '\0', 12);

    num = (int)(slot - 'a');
    rpt_alm_to_client(slot, parg);
    sprintf((char *)tmp, "\"%c,%c,%s\"", slot, slot_type[num], parg);
    if(slot == GBTP1_SLOT)
    { 
        memset(alm_sta[16],'\0',14);
        memcpy(alm_sta[16], tmp, strlen(tmp));
    }
    else if(slot == GBTP2_SLOT)
    {
        memset(alm_sta[17],'\0',14);
        memcpy(alm_sta[17], tmp, strlen(tmp));
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

/*
*********************************************************************************************************
*	Function: int save_gbtp_alm(char slot, unsigned char *parg)
*	Descript: gbtpv2 alarm infomation.
*	Parameters:  - slot 
*                - parg :the point of input data.

*	Return:      - 0 : ok
*				 - 1 : error
*				
*********************************************************************************************************
*/

int save_gbtp_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[12];
    memset(tmp, '\0', 12);

    num = (int)(slot - 'a');
    rpt_alm_to_client(slot, parg);
	
    sprintf((char *)tmp, "\"%c,%c,%s\"", slot, slot_type[num], parg);
    if(slot == GBTP1_SLOT)
    { 
        memset(alm_sta[16],'\0',14);
        memcpy(alm_sta[16], tmp, strlen(tmp));
    }
    else if(slot == GBTP2_SLOT)
    {
        memset(alm_sta[17],'\0',14);
        memcpy(alm_sta[17], tmp, strlen(tmp));
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}


int save_sy_ver(char slot, unsigned char *parg)
{
    int num, len;
    int i, j, i1, i2, i3,i4,i5;
    unsigned char mcu_ver[7];
    unsigned char fpga_ver[7];
	unsigned char pcb_ver[7];
    unsigned char temp[40];

    if( (('m' == slot) && (EXT_DRV == slot_type[12]) ) ||
            (('n' == slot) && (EXT_DRV == slot_type[13])) ||
            (EXT_1_MGR_PRI == slot) || (EXT_1_MGR_RSV == slot) ||
            (EXT_2_MGR_PRI == slot) || (EXT_2_MGR_RSV == slot) ||
            (EXT_3_MGR_PRI == slot) || (EXT_3_MGR_RSV == slot) ||
            (slot >= EXT_1_OUT_LOWER && slot <= EXT_1_OUT_UPPER) ||
            (slot >= EXT_2_OUT_LOWER && slot <= EXT_2_OUT_UPPER) ||
            (slot >= EXT_3_OUT_LOWER && slot <= EXT_3_OUT_UPPER) )
    {
        if(0 == ext_out_ver_trpt(slot, parg, &gExtCtx))
        {
            return 1;
        }
    }
    else
    {
        //printf("main-----ver :%s   slot :%d\n",parg,slot);
        j = 0;
        i1 = 0;
        i2 = 0;
        i3 = 0;
		i4 =0;
		i5 = 0;
        len = strlen(parg);
        if(strncmp1(parg, "MCU", 3) != 0) //
        {
            if(slot == RB1_SLOT)
            {
                memcpy(rpt_content.slot_o.sy_ver, parg, len);
            }
            else if(slot == RB2_SLOT)
            {
                memcpy(rpt_content.slot_p.sy_ver, parg, len);
            }
            else if(slot == GBTP1_SLOT)
            {
                memset(rpt_content.slot_q.sy_ver, '\0', sizeof(rpt_content.slot_q.sy_ver));
                memcpy(rpt_content.slot_q.sy_ver, parg, len);
                /*MCU,V01.00,FPGA,V01.00,CPLD,V01.00,PCB,V01.00,*/
                /*V01.00,V01.00,V01.00,<sta>*/
                if(rpt_content.slot_q.gb_sta == '0' || rpt_content.slot_q.gb_sta == '1' || rpt_content.slot_q.gb_sta == '2')
                {
                    rpt_content.slot_q.sy_ver[21] = rpt_content.slot_q.gb_sta;
                    rpt_content.slot_q.sy_ver[22] = '\0';
                }
                else
                {
                    rpt_content.slot_q.sy_ver[21] = '\0';
                }
            }
            else if(slot == GBTP2_SLOT)
            {
                memset(rpt_content.slot_r.sy_ver, '\0', sizeof(rpt_content.slot_r.sy_ver));
                memcpy(rpt_content.slot_r.sy_ver, parg, len);

                if(rpt_content.slot_r.gb_sta == '0' || rpt_content.slot_r.gb_sta == '1' || rpt_content.slot_r.gb_sta == '2')
                {
                    rpt_content.slot_r.sy_ver[21] = rpt_content.slot_r.gb_sta;
                    rpt_content.slot_r.sy_ver[22] = '\0';
                }
                else
                {
                    rpt_content.slot_r.sy_ver[21] = '\0';
                }
            }
            else
            {
                num = (int)(slot - 'a');
                if((num >= 0) && (num < 14))
                {
                    memcpy(rpt_content.slot[num].sy_ver, parg, len);
                    //printf("--%s--\n", rpt_content.slot[num].sy_ver);
                }
            }
        }
        else
        {
			
            for(i = 0; i < len; i++)
            {
                if(parg[i] == ',')
                {
                    j++;
                    if(j == 1)
                    {
                        i1 = i + 1;
                    }
                    if(j == 2)
                    {
                        i2 = i + 1;
                    }
                    if(j == 3)
                    {
                        i3 = i + 1;
                    }
					if(j == 4)
						i4 = i+1;
					if(j == 5)
						i5 = i+1;
                }
            }
            if(i3 == 0)
            {
                return 1;
            }
			/*MCU,V01.00,FPGA,V01.00,PCB,V01.00*/
			memset(pcb_ver,0,7);
            memset(mcu_ver, 0, 7);
            memcpy(mcu_ver, &parg[i1], i2 - i1 - 1);
            memset(fpga_ver, 0, 7);
			if(i4 == 0)
			{
            	memcpy(fpga_ver, &parg[i3], len - i3);
            	sprintf((char *)temp, "%s,%s,,", mcu_ver, fpga_ver);
			}
			else
			{
				memcpy(fpga_ver, &parg[i3], i4 - i3-1);
				memcpy(pcb_ver, &parg[i5], len - i5);
				sprintf((char *)temp, "%s,%s,,%s", mcu_ver, fpga_ver,pcb_ver);
			}
            len = strlen(temp);
            if(slot == RB1_SLOT)
            {
                memcpy(rpt_content.slot_o.sy_ver, temp, len);
            }
            else if(slot == RB2_SLOT)
            {
                memcpy(rpt_content.slot_p.sy_ver, temp, len);
            }
            else if(slot == GBTP1_SLOT)
            {
                memset(rpt_content.slot_q.sy_ver, '\0', sizeof(rpt_content.slot_q.sy_ver));
                memcpy(rpt_content.slot_q.sy_ver, temp, len);
            }
            else if(slot == GBTP2_SLOT)
            {
                memset(rpt_content.slot_r.sy_ver, '\0', sizeof(rpt_content.slot_r.sy_ver));
                memcpy(rpt_content.slot_r.sy_ver, temp, len);
            }
            else
            {
                
                num = (int)(slot - 'a');
                if((num >= 0) && (num < 14))
                {
                    memset(rpt_content.slot[num].sy_ver, '\0', sizeof(rpt_content.slot[num].sy_ver));
                    memcpy(rpt_content.slot[num].sy_ver, temp, len);
                }
            }
        }
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif

    return 0;
}


int save_ch1andch3dph(char slot, unsigned char *parg)
{
    int i, j, i1, i2, i3, i4, i5, i6, i7,i8, len;
	j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
    i7 = 0;
	i8 = 0;
	len = strlen(parg);
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
            if(j == 7)
            {
                i7 = i + 1;
            }
			if(j == 8)
			{
                i8 = i + 1;
			}
        }
    }
	 if(i8 == 0)
    {
        return 1;
    }
	if(slot == RB1_SLOT)
    {
        
        if(parg[i1] != ',')
        {
            memset(rpt_content.slot_o.rb_phase[0], 0, 9);
            memcpy(rpt_content.slot_o.rb_phase[0], &parg[i1], i2 - i1 - 1);
        }
		else
		{
		    memset(rpt_content.slot_o.rb_phase[0], 0, 9);
		}
        
        if(parg[i3] != ',')
        {
            memset(rpt_content.slot_o.rb_phase[1], 0, 9);
            memcpy(rpt_content.slot_o.rb_phase[1], &parg[i3], i4 - i3 - 1);
        }
		else
		{ 
		   memset(rpt_content.slot_o.rb_phase[1], 0, 9);
		}  
        
    }
    else if(slot == RB2_SLOT)
    {

        
        if(parg[i1] != ',')
        {
            memset(rpt_content.slot_p.rb_phase[0], 0, 9);
            memcpy(rpt_content.slot_p.rb_phase[0], &parg[i1], i2 - i1 - 1);
        }
		else
		{
		   memset(rpt_content.slot_p.rb_phase[0], 0, 9);
		}
        
        if(parg[i3] != ',')
        {
            memset(rpt_content.slot_p.rb_phase[1], 0, 9);
            memcpy(rpt_content.slot_p.rb_phase[1], &parg[i3], i4 - i3 - 1);
        }
		else
		{
		   memset(rpt_content.slot_p.rb_phase[1], 0, 9);
		}
    }

    return 0;
	
}


int save_rx_dph(char slot, unsigned char *parg)
{
	//
	//!new defined.
	int i;
	int j,i1,i2,i3,i4,i5,i6,i7,i8,len;

    unsigned char  tmp[SENDBUFSIZE];
    memset(tmp, '\0', SENDBUFSIZE);

	//
    //!new initial.
    len=i= 0;
    j=i1=i2=i3=i4=i5=i6=i7=i8=0;

	len = strlen(parg);
    for(i =0;i<len;i++)
    {
	   if(parg[i] == ',')
	   {
	      j++;
	      if(j == 1) i1 = i+1;
	      if(j == 2) i2 = i+1;
	      if(j == 3) i3 = i+1;
	      if(j == 4) i4 = i+1;
	      if(j == 5) i5 = i+1;
	      if(j == 6) i6 = i+1;
	      if(j == 7) i7 = i+1;
	      if(j == 8) i8 = i+1;
	   }
    }
	if(i8 == 0)
     return 1;
	
    get_time_fromnet();
    if(slot == RB1_SLOT)
    {
        sprintf((char *)tmp, "\"%c,%s,%s,%s\"%c%c%c%c%c", slot, parg, mcp_date, mcp_time, CR, LF, SP, SP, SP);
        memcpy(rpt_content.slot_o.rb_inp[flag_i_o], tmp, strlen(tmp));
        flag_i_o++;
        if(flag_i_o == 10)
        {
            flag_i_o = 0;
        }
		
		//
		//!new record #17 #18 #1 time 2# time 2# fre 1#  
		//            #17 #18                 2mhz   2mb
		if(parg[0] != ',')
			strncpynew(rpt_content.slot_o.rb_ph[0],&parg[0],i1-1);
		else
			memset(rpt_content.slot_o.rb_ph[0],'\0',10);
 		
		if(parg[i2] != ',')
			strncpynew(rpt_content.slot_o.rb_ph[1],&parg[i2],i3-i2-1);
		else
			memset(rpt_content.slot_o.rb_ph[1],'\0',10);

		if(parg[i1] != ',')
			strncpynew(rpt_content.slot_o.rb_ph[2],&parg[i1],i2-i1-1);
		else
			memset(rpt_content.slot_o.rb_ph[2],'\0',10);
		
        if(parg[i3] != ',')
			strncpynew(rpt_content.slot_o.rb_ph[3],&parg[i3],i4-i3-1);
		else
			memset(rpt_content.slot_o.rb_ph[3],'\0',10);
		
		if(parg[i5] != ',')    
        	strncpynew(rpt_content.slot_o.rb_ph[4],&parg[i5],i6-i5-1); 
		else
			memset(rpt_content.slot_o.rb_ph[4],'\0',10);
		
		if(parg[i7] != ',')    
        	strncpynew(rpt_content.slot_o.rb_ph[5],&parg[i7],i8-i7-1);
		else
			memset(rpt_content.slot_o.rb_ph[5],'\0',10);
		
		if(parg[i8] != '\0')    
        		strncpynew(rpt_content.slot_o.rb_ph[6],&parg[i8],len-i8);
		
    }
    if(slot == RB2_SLOT)
    {
        sprintf((char *)tmp, "\"%c,%s,%s,%s\"%c%c%c%c%c", slot, parg, mcp_date, mcp_time, CR, LF, SP, SP, SP);
        memcpy(rpt_content.slot_p.rb_inp[flag_i_p], tmp, strlen(tmp));
        flag_i_p++;
        if(flag_i_p == 10)
        {
            flag_i_p = 0;
        }
		//
		//!new record #17 #18 #1 time 2# time 2# fre 1#  
		//            #17 #18                 2mhz   2mb
		if(parg[0] != ',')
			strncpynew(rpt_content.slot_p.rb_ph[0],&parg[0],i1-1);
		else
			memset(rpt_content.slot_p.rb_ph[0],'\0',10);
		
		if(parg[i2] != ',')
			strncpynew(rpt_content.slot_p.rb_ph[1],&parg[i2],i3-i2-1); 
		else
			memset(rpt_content.slot_p.rb_ph[1],'\0',10);

		if(parg[i1] != ',')
			strncpynew(rpt_content.slot_p.rb_ph[2],&parg[i1],i2-i1-1);
		else
			memset(rpt_content.slot_p.rb_ph[2],'\0',10);
		
        if(parg[i3] != ',')
			strncpynew(rpt_content.slot_p.rb_ph[3],&parg[i3],i4-i3-1);
		else
			memset(rpt_content.slot_p.rb_ph[3],'\0',10);

		if(parg[i4] != ',')    
        	strncpynew(rpt_content.slot_p.rb_ph[4],&parg[i4],i5-i4-1);
		else
			memset(rpt_content.slot_p.rb_ph[4],'\0',10);

		if(parg[i6] != ',')    
        	strncpynew(rpt_content.slot_p.rb_ph[5],&parg[i6],i7-i6-1);
		else
			memset(rpt_content.slot_p.rb_ph[5],'\0',10);

		if(parg[i8] != '\0')    
        		strncpynew(rpt_content.slot_p.rb_ph[6],&parg[i8],len-i8);
    }
    save_ch1andch3dph(slot,parg);
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_rf_src(char slot, unsigned char *parg)
{
    int i, j, i1, i2, i3, i4, i5, len;
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    len = strlen(parg);
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
        }
    }
    if(i5 == 0)
    {
        return 1;
    }
    if(slot == RB1_SLOT)
    {
        if(parg[0] != ',')
        {
            rpt_content.slot_o.rb_ref = parg[0];
            get_out_lev(0, 0);
        }
        if(parg[i1] != ',')
        {
            memcpy(rpt_content.slot_o.rb_prio, &parg[i1], i2 - i1 - 1);
        }
        if(parg[i2] != ',')
        {
            memcpy(rpt_content.slot_o.rb_mask, &parg[i2], i3 - i2 - 1);
        }
        if(parg[i3] != ',')
        {
            memcpy(rpt_content.slot_o.rb_leapmask, &parg[i3], i4 - i3 - 1);
        }
        if(parg[i4] != ',')
        {
            memcpy(rpt_content.slot_o.rb_leap, &parg[i4], i5 - i4 - 1);
        }
        if(parg[i5] != '\0')
        {
            rpt_content.slot_o.rb_leaptag = parg[i5];
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(parg[0] != ',')
        {
            rpt_content.slot_p.rb_ref = parg[0];
            get_out_lev(0, 0);
        }
        if(parg[i1] != ',')
        {
            memcpy(rpt_content.slot_p.rb_prio, &parg[i1], i2 - i1 - 1);
        }
        if(parg[i2] != ',')
        {
            memcpy(rpt_content.slot_p.rb_mask, &parg[i2], i3 - i2 - 1);
        }
        if(parg[i3] != ',')
        {
            memcpy(rpt_content.slot_p.rb_leapmask, &parg[i3], i4 - i3 - 1);
        }
        if(parg[i4] != ',')
        {
            memcpy(rpt_content.slot_p.rb_leap, &parg[i4], i5 - i4 - 1);
        }
        if(parg[i5] != '\0')
        {
            rpt_content.slot_p.rb_leaptag = parg[i5];
        }
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        if(slot == RB1_SLOT)
            printf("%s:<%c>%s,%s,%s,%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_prio,
                   rpt_content.slot_o.rb_mask, rpt_content.slot_o.rb_leapmask,
                   rpt_content.slot_o.rb_leap);
        else if(slot == RB2_SLOT)
            printf("%s:<%c>%s,%s,%s,%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_prio,
                   rpt_content.slot_p.rb_mask, rpt_content.slot_p.rb_leapmask,
                   rpt_content.slot_p.rb_leap);
    }
#endif

    return 0;
}

int save_rb_tod(char slot, unsigned char *parg)
{
    /** SET-TOD-STA:<ctag>::<ms>,<ppssta>,<sourcetype>; **/
    unsigned char  sendbuf[SENDBUFSIZE];
    //unsigned char  *ctag="000000";
    //char send_slot;
    //int i;
    int value = 0;
    char tmp_buf[64];
    //printf("<save_rb_tod>:%s\n",parg);
    memset(sendbuf, '\0', SENDBUFSIZE);
    if(slot == RB1_SLOT)
    {
        if(strncmp1(rpt_content.slot_o.rb_msmode, "MAIN", 4) == 0)
        {
            value = 0;
        }
        //else if(strncmp1(rpt_content.slot_o.rb_msmode,"STBY",4)==0)
        //	value=1;
        else
        {
            return 0;
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(strncmp1(rpt_content.slot_p.rb_msmode, "MAIN", 4) == 0)
        {
            value = 0;
        }
        //else if(strncmp1(rpt_content.slot_p.rb_msmode,"STBY",4)==0)
        //	value=1;
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }

    //modified by zhanghui 2013-9-10 15:50:12
    memset(tmp_buf, 0, 64);
    memcpy(tmp_buf, parg, strlen(parg));
    //printf("<save_rb_tod>:%s\n",parg);
    send_tod(tmp_buf, slot);

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif

    return 0;
}

int save_rf_dey(char slot, unsigned char *parg)
{
    int i, j, i1, i2, i3, i4, i5, i6, i7, len;
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    i6 = 0;
    i7 = 0;
    len = strlen(parg);
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
            if(j == 7)
            {
                i7 = i + 1;
            }
        }
    }
    if(i7 == 0)
    {
        return 1;
    }
    if(slot == RB1_SLOT)
    {
        if(parg[0] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[0], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[0], &parg[0], i1 - 1);
        }
        if(parg[i1] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[1], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[1], &parg[i1], i2 - i1 - 1);
        }
        if(parg[i2] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[2], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[2], &parg[i2], i3 - i2 - 1);
        }
        if(parg[i3] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[3], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[3], &parg[i3], i4 - i3 - 1);
        }
        if(parg[i4] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[4], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[4], &parg[i4], i5 - i4 - 1);
        }
        if(parg[i5] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[5], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[5], &parg[i5], i6 - i5 - 1);
        }
        if(parg[i6] != ',')
        {
            memset(rpt_content.slot_o.rf_dey[6], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[6], &parg[i6], i7 - i6 - 1);
        }
        if(parg[i7] != '\0')
        {
            memset(rpt_content.slot_o.rf_dey[7], 0, 9);
            memcpy(rpt_content.slot_o.rf_dey[7], &parg[i7], len - i7);
        }
    }
    else if(slot == RB2_SLOT)
    {

        if(parg[0] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[0], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[0], &parg[0], i1 - 1);
        }
        if(parg[i1] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[1], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[1], &parg[i1], i2 - i1 - 1);
        }
        if(parg[i2] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[2], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[2], &parg[i2], i3 - i2 - 1);
        }
        if(parg[i3] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[3], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[3], &parg[i3], i4 - i3 - 1);
        }
        if(parg[i4] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[4], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[4], &parg[i4], i5 - i4 - 1);
        }
        if(parg[i5] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[5], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[5], &parg[i5], i6 - i5 - 1);
        }
        if(parg[i6] != ',')
        {
            memset(rpt_content.slot_p.rf_dey[6], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[6], &parg[i6], i7 - i6 - 1);
        }
        if(parg[i7] != '\0')
        {
            memset(rpt_content.slot_p.rf_dey[7], 0, 9);
            memcpy(rpt_content.slot_p.rf_dey[7], &parg[i7], len - i7);
        }
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_rx_sta(char slot, unsigned char *parg)
{
    int i, j, i1, i2, len, i3, i4, i5;
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', sizeof(unsigned char) * SENDBUFSIZE);
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    len = strlen(parg);

    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
        }
    }
    if(i5 == 0)
    {
        return 1;
    }
    if(slot == RB1_SLOT)
    {
        if(parg[0] != ',')
        {
            memcpy(rpt_content.slot_o.rb_typework, &parg[0], i1 - 1);
        }
        if(parg[i1] != ',')
        {
            memcpy(rpt_content.slot_o.rb_msmode, &parg[i1], i2 - i1 - 1);
            get_out_lev(0, 0);
        }
        if(parg[i2] != ',')
        {
            memcpy(rpt_content.slot_o.rb_trmode, &parg[i2], i3 - i2 - 1);
            get_out_lev(0, 0);
        }
        if(parg[i3] != ',')
        {
            if(parg[i3] != conf_content.slot_u.fb)
            {
                sprintf((char *)sendbuf, "SET-RB-FB:000000::%c;", conf_content.slot_u.fb);
                sendtodown(sendbuf, RB1_SLOT);
            }
        }
        if(parg[i4] != ',')
        {
            if((parg[i4] < '@') || (parg[i4] > 'E'))
            {}
            else
            {
                rpt_content.slot_o.rb_sa = parg[i4];
            }
        }
        if(parg[i5] != '\0')
        {
            if((parg[i5] < 'a') || (parg[i5] > 'g'))
            {}
            else
            {
                rpt_content.slot_o.rb_tl = parg[i5];
            }
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(parg[0] != ',')
        {
            memcpy(rpt_content.slot_p.rb_typework, &parg[0], i1 - 1);
        }
        if(parg[i1] != ',')
        {
            memcpy(rpt_content.slot_p.rb_msmode, &parg[i1], i2 - i1 - 1);
            get_out_lev(0, 0);
        }
        if(parg[i2] != ',')
        {
            memcpy(rpt_content.slot_p.rb_trmode, &parg[i2], i3 - i2 - 1);
            get_out_lev(0, 0);
        }
        if(parg[i3] != ',')
        {
            if(parg[i3] != conf_content.slot_u.fb)
            {
                sprintf((char *)sendbuf, "SET-RB-FB:000000::%c;", conf_content.slot_u.fb);
                sendtodown(sendbuf, RB2_SLOT);
            }
        }
        if(parg[i4] != ',')
        {
            if((parg[i4] < '@') || (parg[i4] > 'E'))
            {
            
            }
            else
            {
                rpt_content.slot_p.rb_sa = parg[i4];
            }
        }
        if(parg[i5] != '\0')
        {
            if((parg[i5] < 'a') || (parg[i5] > 'g'))
            {
            
            }
            else
            {
                rpt_content.slot_p.rb_tl = parg[i5];
            }
        }
    }
    get_out_lev(0, 0);
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        if(slot == RB1_SLOT)
            printf("%s:<%c>%s,%s,%s#\n", NET_SAVE, slot, rpt_content.slot_o.rb_typework,
                   rpt_content.slot_o.rb_msmode, rpt_content.slot_o.rb_trmode);
        else if(slot == RB2_SLOT)
            printf("%s:<%c>%s,%s,%s#\n", NET_SAVE, slot, rpt_content.slot_p.rb_typework,
                   rpt_content.slot_p.rb_msmode, rpt_content.slot_p.rb_trmode);
    }
#endif

    return 0;
}

int save_rx_alm(char slot, unsigned char *parg)
{
    unsigned char  tmp[14];
    int num;
    unsigned char  buf[4];


    memset(tmp, '\0', 14);
    num = (int)(slot - 'a');
    rpt_alm_to_client(slot, parg);
    get_out_lev(0, 0);

    sprintf((char *)tmp, "\"%c,%c,%s\"", slot, slot_type[num], parg);
    if(slot == RB1_SLOT)
    {
        memcpy(alm_sta[14], tmp, strlen(tmp));

        strncpynew(buf, rpt_content.slot_o.tod_buf, 3);
        //printf("<save_rx_alm>:%s\n",buf);

        send_tod(buf, slot);

    }
    else if(slot == RB2_SLOT)
    {
        memcpy(alm_sta[15], tmp, strlen(tmp));

        strncpynew(buf, rpt_content.slot_p.tod_buf, 3);

        //printf("<save_rx_alm>:%s\n",buf);
        send_tod(buf, slot);

    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        if(slot == RB1_SLOT)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, alm_sta[14]);
        }
        else if(slot == RB2_SLOT)
        {
            printf("%s:<%c>%s#\n", NET_SAVE, slot, alm_sta[15]);
        }
    }
#endif
    return 0;
}

int save_tp_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];
    memset(tmp, '\0', 14);
    rpt_alm_to_client(slot, parg);
    sprintf((char *)tmp, "\"%c,I,%s\"", slot, parg);
    num = (int)(slot - 'a');
    memcpy(alm_sta[num], tmp, strlen(tmp));
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}

int save_out_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];

    if (strlen(parg) > 14)
    {
        printf("\n\n\n! ! ! para  overlength : %s\n\n\n", __func__);
        return -1;
    }

    if(slot >= 'a' && slot <= 'w')
    {
        memset(tmp, '\0', 14);
        num = (int)(slot - 'a');
        rpt_alm_to_client(slot, parg);
        sprintf((char *)tmp, "\"%c,%c,%s\"", slot, slot_type[num], parg);
        memcpy(alm_sta[num], tmp, strlen(tmp));
    }
    else
    {
        if(0 == ext_out_alm_trpt(slot, parg, &gExtCtx))
        {
            return 1;
        }
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif

    return 0;
}

int save_ptp_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];
    memset(tmp, '\0', 14);
    rpt_alm_to_client(slot, parg);
    num = (int)(slot - 'a');
    sprintf((char *)tmp, "\"%c,%c,%s\"", slot, slot_type[num], parg);
    memcpy(alm_sta[num], tmp, strlen(tmp));
	//printf("PTP ALM %s \n",alm_sta[num]);
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
}



void JudgeAllowExtOnLine(const char slot, unsigned char *parg)
{
    u8_t online;
    u8_t flag;

    online = gExtCtx.save.onlineSta;

    if( (EXT_1_MGR_PRI == slot || EXT_1_MGR_RSV == slot)
            && ( (online & 0x01) == 0x00 ) )
    {
        printf("---1\n");
        flag = 0;
    }
    else if( (EXT_2_MGR_PRI == slot || EXT_2_MGR_RSV == slot)
             && ( (online & 0x02) == 0x00 ) )
    {
        printf("---2\n");
        flag = 0;
    }
    else if( (EXT_3_MGR_PRI == slot || EXT_3_MGR_RSV == slot)
             && ( (online & 0x04) == 0x00 ) )
    {
        printf("---3\n");
        flag = 0;
    }
    else
    {
        flag = 1;
    }

    if(0 == flag)
    {
        memset(parg, 0, 14);
        memset(parg, 'O', 14);
    }
    else
    {
    }

}


int save_ext_bid(char slot, unsigned char *parg)
{

    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }

    JudgeAllowExtOnLine(slot, parg);

    if(0 == ext_bpp_cfg(slot, parg, &gExtCtx))
    {
        return 1;
    }

    if(0 == ext_bid_cmp(slot, parg, &gExtCtx))
    {
        return 1;
    }

    if(0 == ext_out_bid_trpt(slot, parg, &gExtCtx))
    {
        return 1;
    }

    return 0;
}






int save_ext_mgr_pr(char slot, unsigned char *parg)
{
    if(print_switch == 0)
    {
        printf("%s:%s#\n", NET_SAVE, parg);
    }

    if(0 == ext_mgr_pr_trpt(slot, parg, &gExtCtx))
    {
        return 1;
    }

    return 0;
}





int save_out_sta(char slot, unsigned char *parg)
{
    int num;

    if(slot >= 'a' && slot <= 'n')
    {
        num = (int)(slot - 'a');
        if((parg[0] == '1') || (parg[0] == '0'))
        {
            rpt_content.slot[num].out_mode = parg[0];
        }
        else
        {
            return 1;
        }

        if((parg[2] == '1') || (parg[2] == '0'))
        {
            rpt_content.slot[num].out_sta = parg[2];
        }
        else
        {
            return 1;
        }
		//
		//OUT32S ADD
		//
		//printf("out32s mod change:%d,%s\n",num,parg);
    }
    else
    {
        if(3 == strlen(parg))
        {
            if(0 == ext_out_mode_crpt(0, slot, parg, &gExtCtx))
            {
                return 1;
            }
        }
        else
        {
            if(0 == ext_out_mode_trpt(slot, parg, &gExtCtx))
            {
                return 1;
            }
        }
    }

    return 0;
}





int save_out_prr(char slot, unsigned char *parg)
{
    if(slot >= 'a' && slot <= 'n')
    {
        return 0;
    }
    else
    {
        if(0 == ext_out_prr_crpt(slot, parg, &gExtCtx))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}


int save_ref_prio(char slot, unsigned char *parg)
{
    int num, i, len;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    len = strlen(parg);
    if(len == 9)
    {
        if(parg[0] == ',') //wu prio1
        {
            for(i = 0; i < 8; i++)
            {
                if((parg[i + 1] < 0x30) || (parg[i + 1] > 0x38))
                {
                    return 1;
                }
            }
            memcpy(rpt_content.slot[num].ref_prio2, &parg[1], 8);

        }
        else
        {
            for(i = 0; i < 8; i++)
            {
                if((parg[i] < 0x30) || (parg[i] > 0x38))
                {
                    return 1;
                }
            }
            memcpy(rpt_content.slot[num].ref_prio1, parg, 8);
        }

    }
    else if(len == 17)
    {
        for(i = 0; i < 8; i++)
        {
            if((parg[i + 9] < 0x30) || (parg[i + 9] > 0x38))
            {
                return 1;
            }
            if((parg[i] < 0x30) || (parg[i] > 0x38))
            {
                return 1;
            }
        }
        memcpy(rpt_content.slot[num].ref_prio2, &parg[9], 8);
        memcpy(rpt_content.slot[num].ref_prio1, parg, 8);
    }
    else
    {
        return 1;
    }

    return 0;
}

int save_ref_mod(char slot, unsigned char *parg)
{
    int num, len;
    num = (int)(slot - 'a');
    len = strlen(parg);
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    if(len == 5)
    {
        if(parg[0] == ',')
        {
            if(((parg[2] == '0') || (parg[2] == '1')) &&
                    ((parg[4] > 0x2f) && (parg[4] < 0x39)))
            {
                memcpy(rpt_content.slot[num].ref_mod2, &parg[2], 3);
                //	send_ref_lev(num);
                get_out_lev(0, 0);
            }
            else
            {
                return 1;
            }
        }
        else
        {
            if(((parg[0] == '0') || (parg[0] == '1')) &&
                    ((parg[2] > 0x2f) && (parg[2] < 0x39)))
            {
                memcpy(rpt_content.slot[num].ref_mod1, &parg[0], 3);
                //send_ref_lev(num);
                get_out_lev(0, 0);
            }
            else
            {
                return 1;
            }
        }
    }
    else if(len == 7)
    {
        if(((parg[0] == '0') || (parg[0] == '1')) &&
                ((parg[2] > 0x2f) && (parg[2] < 0x39)))
        {}
        else
        {
            return 1;
        }
        if(((parg[4] == '0') || (parg[4] == '1')) &&
                ((parg[6] > 0x2f) && (parg[6] < 0x39)))
        {}
        else
        {
            return 1;
        }

        memcpy(rpt_content.slot[num].ref_mod1, &parg[0], 3);
        memcpy(rpt_content.slot[num].ref_mod2, &parg[4], 3);
        //send_ref_lev(num);
        get_out_lev(0, 0);
    }
    else
    {
        return 1;
    }

    return 0;
}

int save_ref_ph(char slot, unsigned char *parg)
{
    int num, i, cnt, j1;
    num = (int)(slot - 'a');
    cnt = i = j1 = 0;
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    while(parg[i])
    {
        if(parg[i] == ',')
        {
            cnt++;
            if(cnt == 1)
            {
                j1 = i + 1;
            }
        }
        i++;
    }
    if(cnt != 8)
    {
        return 1;
    }
    memcpy(rpt_content.slot[num].ref_en, parg, 8);
    memset(rpt_content.slot[num].ref_ph, 0, 128);
    memcpy(rpt_content.slot[num].ref_ph, &parg[9], strlen(parg) - 9);
    return 0;
}

int save_ref_sta(char slot, unsigned char *parg)
{
    int num, i, len;

    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    len = strlen(parg);
    if(len != 17)
    {
        return 1;
    }
    for(i = 0; i < 8; i++)
    {
        if((parg[i] < '@') && (parg[i] > 'A'))
        {
            return 1;
        }
    }
    for(i = 9; i < 8; i++)
    {
        if((parg[i] < 'a') && (parg[i] > 'g'))
        {
            return 1;
        }
    }

    memcpy(rpt_content.slot[num].ref_sa, parg, 8);
    memcpy(rpt_content.slot[num].ref_tl, &parg[9], 8);
    //send_ref_lev(num);
    get_out_lev(0, 0);

    return 0;
}


int save_ref_tpy(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    for(i = 0; i < 8; i++)
    {
        if((parg[i] != '0') && (parg[i] != '1') && (parg[i] != '2'))
        {
            return 1;
        }
    }
    for(i = 0; i < 8; i++)
    {
        if((parg[i + 9] != '0') && (parg[i + 9] != '1'))
        {
            return 1;
        }
    }

    memcpy(rpt_content.slot[num].ref_typ1, parg, 8);
    memcpy(rpt_content.slot[num].ref_typ2, &parg[9], 8);
    return 0;
}

int save_ref_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];
    memset(tmp, '\0', 14);
    num = (int)(slot - 'a');

    rpt_alm_to_client(slot, parg);
    sprintf((char *)tmp, "\"%c,S,%s\"", slot, parg);
    //printf("<save_ref_alm>:%s\n",tmp);
    if((parg[0] >> 4) == 0x05) 
    {
        if(num == 0) //ref1
        {
            memcpy(alm_sta[18], tmp, strlen(tmp));
        }
        else if(num == 1) //ref2
        {
            memcpy(alm_sta[19], tmp, strlen(tmp));
        }
    }
    else
    {
        memcpy(alm_sta[num], tmp, strlen(tmp));
    }

    return 0;
}

int save_ref_en(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    for(i = 0; i < 8; i++)
    {
        if((parg[i] != '1') && (parg[i] != '0'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].ref_en, parg, 8);

    return 0;
}



int save_ref_ssm_en(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    for(i = 0; i < 8; i++)
    {
        if(parg[i] < '0' || parg[i] > '2')
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].ref_ssm_en, parg, 8);

    return 0;
}

int save_ref_sa(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    for(i = 0; i < 8; i++)
    {
        if((parg[i] < '@') && (parg[i] > 'E'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].ref_sa, parg, 8);

    return 0;
}

int save_ref_tl(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'S')
    {
        //
        send_online_framing(num, 'S');
        slot_type[num] = 'S';
    }
    for(i = 0; i < 8; i++)
    {
        if((parg[i] < 'a') && (parg[i] > 'g'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].ref_tl, parg, 8);
    //send_ref_lev(num);
    get_out_lev(0, 0);
    return 0;
}

int save_rs_en(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    /*if(slot_type[num]!='T')
    	{
    		//
    		send_online_framing(num,'T');
    		slot_type[num]='T';
    	}*/
    for(i = 0; i < 4; i++)
    {
        if((parg[i] != '1') && (parg[i] != '0'))
        {
            return 1;
        }
    }

    memcpy(rpt_content.slot[num].rs_en, parg, 4);

    return 0;
}

int save_rs_tzo(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    /*if(slot_type[num]!='T')
    	{
    		//
    		send_online_framing(num,'T');
    		slot_type[num]='T';
    	}*/
    for(i = 0; i < 4; i++)
    {
        if((parg[i] < 0x40) || (parg[i] > 0x57))
        {
            return 1;
        }
    }

    memcpy(rpt_content.slot[num].rs_tzo, parg, 4);

    return 0;
}

int save_ppx_mod(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'U')
    {
        //
        send_online_framing(num, 'U');
        slot_type[num] = 'U';
    }
    for(i = 0; i < 16; i++)
    {
        if((parg[i] != '1') && (parg[i] != '2') && (parg[i] != '3'))
        {
            return 1;
        }
    }

    memcpy(rpt_content.slot[num].ppx_mod, parg, 16);

    return 0;
}

int save_tod_en(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'V')
    {
        //
        send_online_framing(num, 'V');
        slot_type[num] = 'V';
    }
    for(i = 0; i < 16; i++)
    {
        if((parg[i] != '1') && (parg[i] != '0'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].tod_en, parg, 16);

    return 0;
}
int save_tod_pro(char slot, unsigned char *parg)
{
    int num;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'V')
    {
        //
        send_online_framing(num, 'V');
        slot_type[num] = 'V';
    }
    if((parg[0] < 0x30) || (parg[0] > 0x37))
    {
        return 1;
    }
    if((parg[2] < 0x40) || (parg[2] > 0x57))
    {
        return 1;
    }
    rpt_content.slot[num].tod_br = parg[0];
    rpt_content.slot[num].tod_tzo = parg[2];

    return 0;
}

int save_igb_en(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'W')
    {
        //
        send_online_framing(num, 'W');
        slot_type[num] = 'W';
    }
    for(i = 0; i < 4; i++)
    {
        if((parg[i] != '1') && (parg[i] != '0'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].igb_en, parg, 4);

    return 0;
}
int save_igb_rat(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'W')
    {
        //
        send_online_framing(num, 'W');
        slot_type[num] = 'W';
    }
    for(i = 0; i < 4; i++)
    {
        if((parg[i] < 'a') && (parg[i] > 'i'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].igb_rat, parg, 4);
    return 0;
}
int save_igb_tzone(char slot, unsigned char *parg)
{
    int num;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'W')
    {
        send_online_framing(num, 'W');
        slot_type[num] = 'W';
    }
    rpt_content.slot[num].igb_tzone = parg[0];
    return 0;
}

int save_igb_max(char slot, unsigned char *parg)
{
    int num, i;
    num = (int)(slot - 'a');
    if(slot_type[num] != 'W')
    {
        //
        send_online_framing(num, 'W');
        slot_type[num] = 'W';
    }
    for(i = 0; i < 4; i++)
    {
        if((parg[i] < 'A') && (parg[i] > 'E'))
        {
            return 1;
        }
    }
    memcpy(rpt_content.slot[num].igb_max, parg, 4);
    return 0;
}
int check_net(char *parg,int len)
{
	int i;
	for(i=0;i<len;i++)
	{
	    if( (parg[i]&0xf0) != 0x40)
	    {
	    	return i;
	    }
	    else
	    {
	    }
	}
	return i;
}

int net_2_str(char h_4bit,char l_4bit)
{
    if((h_4bit & 0xf0) != 0x40 || (l_4bit & 0xf0) != 0x40)
    {
    	return -1;	
    }
    else
    {
    	return ( ((h_4bit&0x0f)<<4)|(l_4bit&0x0f) );
    }
}

void __save_ntp_pnet(unsigned char *dit,char * parg,int len)
{
	int i,res;
	unsigned char str[6];
	for(i=0;i<len;i+=2)
	{
		res = net_2_str(parg[i],parg[i+1]);
		if(res >= 0)
		{
			str[i/2] = (unsigned char)res;
		}
		else
		{
		    return;
		}
	}
	memcpy(dit,str,len/2);
}

int save_ntp_pnet(char slot, unsigned char *parg)
{
	int num, i,port,res;
	int net_sta = 0;
	int len;
	unsigned char net_char[13];
	
	
	num = (int)(slot - 'a');
	len = strlen(parg);
	port = 0;
	for(i=0;i<len;i++)
	{
		memset(net_char,0,13);
		if(parg[i]>='0' && parg[i]<='9')
		{
			net_sta = 0;
		}
		switch (net_sta)
		{
			case 0://port
				
				//(parg[i+1] != ',')?(port = (parg[i++]-'0')*10+(parg[i++]-'0') ):(port = parg[i++]-'0' );
				if(parg[i+1] != ',')
				{
					port = parg[i++]-'0';
					port = port *10 + parg[i++]-'0';
				
				}
				else
				{
					port = parg[i++]-'0';
				}
				port -=1;
				if(port < 0)
				{
					printf("port error\n");
					net_sta = 5;
					break;
				}
				
				net_sta = 1;
				break;
			case 1://mac
				
				res = check_net(parg+i,len-i+1);
				
				if(res == 12)
				{

					__save_ntp_pnet(rpt_content.slot[num].ntp_pmac[port],parg+i,res);
				}
				i = i+res;
				net_sta = 2;
				break;
			case 2:
				
				res = check_net(parg+i,len-i+1);
				
				if(res == 8)
				{
					__save_ntp_pnet(rpt_content.slot[num].ntp_pip[port],parg+i,res);
				}
				i = i+res;
				net_sta = 3;
				
				break;
			case 3:
				res = check_net(parg+i,len-i+1);
				
				if(res == 8)
				{
					__save_ntp_pnet(rpt_content.slot[num].ntp_pgateway[port],parg+i,res);
				}
				i = i+res;
				net_sta = 4;
				
				break;
			case 4:
				res = check_net(parg+i,len-i+1);
				
				if(res == 8)
				{
					__save_ntp_pnet(rpt_content.slot[num].ntp_pnetmask[port],parg+i,res);
				}
				i = i+res;
				net_sta = 1;
				break;
			case 5:
				i = len+1;
				break;
			default:
				break;
		}
		
	}
	return 0;
}

int save_ntp_net(char slot, unsigned char *parg)
{
    int num, i;
    int j, i1, i2, i3, len;
    j = i1 = i2 = i3 = 0;
    num = (int)(slot - 'a');
    len = strlen(parg);
    if(slot_type[num] != 'Z')
    {
        //
        send_online_framing(num, 'Z');
        slot_type[num] = 'Z';
    }


    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
        }
    }
    if(i3 == 0)
    {
        return 1;
    }
    if(parg[0] != ',')
    {
        memcpy(rpt_content.slot[num].ntp_ip, &parg[0], i1 - 1);
    }
    if(parg[i1] != ',')
    {
        memcpy(rpt_content.slot[num].ntp_mac, &parg[i1], i2 - i1 - 1);
    }
    if(parg[i2] != ',')
    {
        memcpy(rpt_content.slot[num].ntp_gate, &parg[i2], i3 - i2 - 1);
    }
    if(parg[i3] != '\0')
    {
        memcpy(rpt_content.slot[num].ntp_msk, &parg[i3], len - i3);
    }



    return 0;
}

int save_ntp_en(char slot, unsigned char *parg)
{
    int num, i;
    int j, i1, i2, i3, i4, len;
    j = i1 = i2 = i3 = i4 = 0;
    num = (int)(slot - 'a');
    len = strlen(parg);
    if(slot_type[num] != 'Z')
    {
        //
        send_online_framing(num, 'Z');
        slot_type[num] = 'Z';
    }
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
        }
    }
    if(i3 == 0)
    {
        return 1;
    }
    if((parg[0] == '0') || (parg[0] == '1'))
    {
        rpt_content.slot[num].ntp_bcast_en = parg[0];
    }
    if(parg[i1] != ',')
    {
		memset(rpt_content.slot[num].ntp_interval, 0, sizeof(rpt_content.slot[num].ntp_interval));
        memcpy(rpt_content.slot[num].ntp_interval, &parg[i1], i2 - i1 - 1);
    }
    if((parg[i2] > 0x3f) && (parg[i2] < 0x48))
    {
        rpt_content.slot[num].ntp_tpv_en = parg[i2];
    }
    if((parg[i3] == '0') || (parg[i3] == '1'))
    {
        rpt_content.slot[num].ntp_md5_en = parg[i3];
    }
/*
    if((parg[i4] > 0x3f) && (parg[i4] < 0x48))
    {
        rpt_content.slot[num].ntp_ois_en = parg[i4];
    }
*/
    return 0;
}


int save_r_ntp_key(char slot, unsigned char *parg)
{
    int num, i;
    int len, i1, i2, i3, i4, i5, i6, i7, j;
    len = i1 = i2 = i3 = i4 = i5 = i6 = i7 = j = 0;
    num = (int)(slot - 'a');
    len = strlen(parg);
    if(slot_type[num] != 'Z')
    {
        //
        send_online_framing(num, 'Z');
        slot_type[num] = 'Z';
    }
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
            if(j == 7)
            {
                i7 = i + 1;
            }
        }
    }
    if(i7 == 0)
    {
        return 1;
    }
    memset(rpt_content.slot[num].ntp_keyid, '\0', 20);
    memset(rpt_content.slot[num].ntp_key, '\0', 68);
    if((parg[0] != ',') && (parg[i1] != ','))
    {
        memcpy(rpt_content.slot[num].ntp_keyid[0], &parg[0], i1 - 1);
        memcpy(rpt_content.slot[num].ntp_key[0], &parg[i1], i2 - i1 - 1);
    }
    if((parg[i2] != ',') && (parg[i3] != ','))
    {
        memcpy(rpt_content.slot[num].ntp_keyid[1], &parg[i2], i3 - i2 - 1);
        memcpy(rpt_content.slot[num].ntp_key[1], &parg[i3], i4 - i3 - 1);
    }
    if((parg[i4] != ',') && (parg[i5] != ','))
    {
        memcpy(rpt_content.slot[num].ntp_keyid[2], &parg[i4], i5 - i4 - 1);
        memcpy(rpt_content.slot[num].ntp_key[2], &parg[i5], i6 - i5 - 1);
    }
    if((parg[i6] != ',') && (parg[i7] != '\0'))
    {
        memcpy(rpt_content.slot[num].ntp_keyid[3], &parg[i6], i7 - i6 - 1);
        memcpy(rpt_content.slot[num].ntp_key[3], &parg[i7], len - i7);
    }

    return 0;
}

int save_ntp_key(char slot, unsigned char *parg)
{
    int num, i;
    unsigned char keyid[5];

    memset(keyid, '\0', 5);
    num = (int)(slot - 'a');
    if(slot_type[num] != 'Z')
    {
        //
        send_online_framing(num, 'Z');
        slot_type[num] = 'Z';
    }

    if((parg[0] != '0') && (parg[0] != '1') && (parg[0] != '2'))
    {
        return 1;
    }
    else if(parg[1] != ',')
    {
        return 1;
    }
    else if(parg[6] != ',')
    {
        return 1;
    }
    memcpy(keyid, &parg[2], 4);
    if(parg[0] == '0')
    {
        for(i = 0; i < 4; i++)
        {
            //if(rpt_content.slot[num].ntp_key_flag[i]!=2)
            if(strncmp1(rpt_content.slot[num].ntp_keyid[i], keyid, 4) == 0)
            {
                return 1;
            }
            else if(rpt_content.slot[num].ntp_keyid[i][0] == '\0')
            {
                //	rpt_content.slot[num].ntp_key_flag[i]=2;
                memcpy(rpt_content.slot[num].ntp_keyid[i], &parg[2], 4);
                memcpy(rpt_content.slot[num].ntp_key[i], &parg[7], 16);
                break;
            }

            else if(i == 3)
            {
                return 1;
            }
        }
    }
    else if(parg[0] == '1')
    {
        for(i = 0; i < 4; i++)
        {
            if(strncmp1(keyid, rpt_content.slot[num].ntp_keyid[i] , 4) == 0)
            {
                //rpt_content.slot[num].ntp_key_flag[i]=1;
                memset(rpt_content.slot[num].ntp_keyid[i], '\0', 5);
                memset(rpt_content.slot[num].ntp_key[i], '\0', 17);
                break;
            }
            else if(i == 3)
            {
                return 1;
            }
        }
    }
    else if(parg[0] == '2')
    {
        for(i = 0; i < 4; i++)
        {
            if(strncmp1(keyid, rpt_content.slot[num].ntp_keyid[i] , 4) == 0)
            {
                memset(rpt_content.slot[num].ntp_key[i], '\0', 17);
                memcpy(rpt_content.slot[num].ntp_key[i], &parg[7], 16);
                break;
            }
            else if(i == 3)
            {
                return 1;
            }
        }
    }

    return 0;
}

int save_ntp_ms(char slot, unsigned char *parg)
{
    int num, len;
    num = (int)(slot - 'a');
    len = strlen(parg);
    if(slot_type[num] != 'Z')
    {
        //
        send_online_framing(num, 'Z');
        slot_type[num] = 'Z';
    }
    if((parg[0] != '0') && (parg[0] != '1'))
    {
        return 1;
    }
    if(len < 4)
    {
        return 1;
    }
    memcpy(rpt_content.slot[num].ntp_ms, parg, len);

    return 0;
}
int save_ntp_sta(char slot, unsigned char *parg)
{
	int num, i;
	int j, i1, i2, i3, len;
	j = i1 = i2 = i3 = 0;
	num = (int)(slot - 'a');
	len = strlen(parg);
	if(0 == len)
	{
		return 1;
	}
	for(i = 0; i < len; i++)
	{
		if(parg[i] == ',')
		{
			j++;
			if(j == 1)
			{
				i1 = i + 1;
			}

		}
	}
	if(i1 == 0)
	{
		memset(rpt_content.slot[num].ntp_leap,0,sizeof(rpt_content.slot[num].ntp_leap));
		memcpy(rpt_content.slot[num].ntp_leap, &parg[0], len);

		
	}
	else
	{
		if(parg[0] != ',')
		{
			memset(rpt_content.slot[num].ntp_leap,0,sizeof(rpt_content.slot[num].ntp_leap));
			memcpy(rpt_content.slot[num].ntp_leap, &parg[0], i1 - 1);
		}
		if(parg[i1] != '\0')
		{
			memset(rpt_content.slot[num].ntp_mcu,0,sizeof(rpt_content.slot[num].ntp_mcu));
			memcpy(rpt_content.slot[num].ntp_mcu, &parg[i1], len - i1);
		}
	}
	return 0;
}

int save_ntp_psta(char slot, unsigned char *parg)
{
	int num;
	num = (int)(slot - 'a');
	if(slot_type[num] != 'Z')
	{
		send_online_framing(num, 'Z');
		slot_type[num] = 'Z';
	}
	memset(rpt_content.slot[num].ntp_portEn,0,sizeof(rpt_content.slot[num].ntp_portEn));
	memset(rpt_content.slot[num].ntp_portStatus,0,sizeof(rpt_content.slot[num].ntp_portStatus));
	
	rpt_content.slot[num].ntp_portEn[0] = parg[0];
	rpt_content.slot[num].ntp_portEn[1] = parg[1];
	rpt_content.slot[num].ntp_portEn[2] = parg[2];
	rpt_content.slot[num].ntp_portEn[3] = parg[3];
	
	rpt_content.slot[num].ntp_portStatus[0] = parg[5];
	rpt_content.slot[num].ntp_portStatus[1] = parg[6];
	rpt_content.slot[num].ntp_portStatus[2] = parg[7];
	rpt_content.slot[num].ntp_portStatus[3] = parg[8];

	return 0;
}



int save_ntp_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];
    memset(tmp, '\0', 14);
    rpt_alm_to_client(slot, parg);
    num = (int)(slot - 'a');
    sprintf((char *)tmp, "\"%c,Z,%s\"", slot, parg);
    memcpy(alm_sta[num], tmp, strlen(tmp));
    return 0;
}
int save_2mb_lvlm(char slot, unsigned char *parg)
{
    int num;
    num = (int)(slot - 'a');
    /*if(slot_type[num]!='T')
    	{
    		//
    		send_online_framing(num,'T');
    		slot_type[num]='T';
    	}*/
    //for(i = 0; i < 4; i++)
    //{
    //    if((parg[i] < 0x40) || (parg[i] > 0x57))
    //    {
      if((parg[0] != '1')&&(parg[0] != '0'))
            return 1;
    //   }
    //}

    rpt_content.slot[num].ref_2mb_lvlm = parg[0];

    return 0;
}

int save_rb_sa(char slot, unsigned char *parg)
{
    if((parg[0] < '@') || (parg[0] > 'E'))
    {
        return 1;
    }
    if(slot == RB1_SLOT)
    {
        rpt_content.slot_o.rb_sa = parg[0];
    }
    else if(slot == RB2_SLOT)
    {
        rpt_content.slot_p.rb_sa = parg[0];
    }

    return 0;
}
int save_ok(char slot, unsigned char *parg)
{
    
    return 0;
}

/*
璁剧疆闂扮锛?
鎴愬姛0锛?
澶辫触1锛?
*/
/*
struct StrTime
{
	unsigned int hour;
	unsigned int minute;
	unsigned int second;
};

struct StrDate
{
	unsigned int year;
	unsigned int month;
	unsigned int day;
};


struct StrTime msgTime;
struct StrDate msgDate;
struct StrTime mcpTime;
struct StrDate mcpDate;


int getLeapDate(char * msg)
{
	int i=0;
	int len=0;
	int cnt = 0;
	char tmp[11];

	get_time_fromnet();

	while(mcp_date[i]!='\0' && i<11)
	{
		if('-' == mcp_date[i])
		{
			memset(tmp,0,11);
			if(cnt == 0)
			{
				cnt ++;
				memcpy(tmp,&mcp_date[i-4],4);
				mcpDate.year = atoi(tmp);
			}
			else if(cnt == 1)
			{
				cnt ++;
				memcpy(tmp,&mcp_date[i-2],2);
				mcpDate.month= atoi(tmp);
			}
			else if(cnt == 2)
			{
				cnt ++;
				memcpy(tmp,&mcp_date[i-2],2);
				mcpDate.day= atoi(tmp);
				break;

			}
			else
			{
				printf("%s mcp date error\n",__FUNCTION__);
				return 0;
			}
		}
		i++;
	}

	i=0;
	cnt = 0;

	while(mcp_time[i]!='\0' && i<11)
	{
		if(':' == mcp_time[i])
		{
			memset(tmp,0,11);
			if(cnt == 0)
			{
				cnt ++;
				memcpy(tmp,&mcp_time[i-2],2);
				mcpTime.hour = atoi(tmp);
			}
			else if(cnt == 1)
			{
				cnt ++;
				memcpy(tmp,&mcp_time[i-2],2);
				mcpTime.minute= atoi(tmp);

			}
			else if( 2 == cnt )
			{
				cnt ++;
				memcpy(tmp,&mcp_time[i-2],2);
				mcpTime.second= atoi(tmp);
				break;
			}
			else
			{
				//printf("%s mcp date error\n",__FUNCTION__);
				//return 0;
			}
		}
		i++;
	}

//L,235960,31,12,2012,15,16,01;
	i = 0;
	cnt = 0;

	while(msg[i] != '\0' && i < len)
	{
		if(',' == msg[i])
		{
			memset(tmp,0,11);
			if( cnt == 0)
			{
				cnt ++;

			}
			else if( cnt == 1)
			{
				cnt++;
				memset(tmp,0,11);
				memcpy(tmp,&msg[i-6],2);
				msgTime.hour = atoi(tmp);

				memset(tmp,0,11);
				memcpy(tmp,&msg[i-4],2);
				msgTime.minute= atoi(tmp);

				memset(tmp,0,11);
				memcpy(tmp,&msg[i-2],2);
				msgTime.second= atoi(tmp);
			}
			else if( 2 == cnt)
			{
				cnt ++;
				memset(tmp,0,11);
				memcpy(tmp,&msg[i-2],2);
				msgDate.day= atoi(tmp);
			}
			else if( 3 == cnt )
			{
				cnt ++;
				memset(tmp,0,11);
				memcpy(tmp,&msg[i-2],2);
				msgDate.month= atoi(tmp);
			}
			else if( 4 == cnt)
			{
				cnt ++;
				memset(tmp,0,11);
				memcpy(tmp,&msg[i-4],4);
				msgDate.year= atoi(tmp);
			}
			else
			{
				printf("leap msg error \n");
				return 1;
			}
		}
	}
}
*/
int save_leap_alm(char slot, unsigned char *parg)
{

    int i;
    int cnt = 0;
    int len;

    char msg[64];
    char tmp[11];
    char leapTime[20];
    char leapDate[20];

    len = strlen(parg);
	if(len > 64)
		return 1;
	
    memset(msg, 0, 64);
    memcpy(msg, parg, len);
    printf("@#%s\n", __FUNCTION__);
    if(g_LeapFlag != 0)
    {
        return 1;
    }
    else
    {
        ;
    }

    for(i = 0; i < len; i++)
    {
        if(',' == msg[i])
        {
            cnt++;
        }
        else
        {
            continue;
        }
    }

    if(cnt != 6)
    {
        return 1;
    }
    else
    {
        memset(tmp, 0, 11);
        memset(leapDate, 0, 20);
        memset(leapTime, 0, 20);
        i = 0;
        cnt = 0;
    }

    while(msg[i] != '\0' && i < len)
    {
        if(',' == msg[i])
        {
            memset(tmp, 0, 11);

            if( cnt == 0)
            {
                cnt++;
                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 6], 2);
                sprintf((char *)leapTime, "%s", tmp);

                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 4], 2);
                sprintf((char *)leapTime, "%s:%s", leapTime, tmp);

                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 2], 2);
                sprintf((char *)leapTime, "%s:%s", leapTime, tmp);
            }
            else if( 1 == cnt)
            {
                cnt ++;
                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 2], 2);
                sprintf((char *)leapDate, "%s", tmp);
            }
            else if( 2 == cnt )
            {
                cnt ++;
                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 2], 2);
                sprintf((char *)tmp, "%s-%s", tmp, leapDate);
                memset(leapDate , 0, 20);
                memcpy(leapDate, tmp, strlen(tmp));
            }
            else if( 3 == cnt)
            {
                cnt ++;
                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 4], 4);
                sprintf((char *)tmp, "%s-%s", tmp, leapDate);
                memset(leapDate , 0, 20);
                memcpy(leapDate, tmp, strlen(tmp));
            }
            else if(5 == cnt)
            {
                cnt++;
				
                //R-LEAP-ALM :L,235960,31,12,2012,15,16,01;
                ;
            }
            else if(4 == cnt)
            {
                cnt++;
                memset(tmp, 0, 11);
                memcpy(tmp, &msg[i - 2], 2);
                if( (tmp[0] >= '0' && tmp[0] <= '9') &&
                        (tmp[1] >= '0' && tmp[1] <= '9') )
                {
                    memset(conf_content.slot_u.leap_num, '\0', 3);
                    memcpy(conf_content.slot_u.leap_num, tmp, 2);
                }
                else
                {
                    return 1;
                }
            }
        }
        i++;
    }

	if((msg[len-2] == '0')&&((msg[len-1] == '0')||(msg[len-1] == '1')||(msg[len-1] == '2')))
	{
	   rpt_content.slot_q.leapalm[0] = msg[len-2];
	   rpt_content.slot_q.leapalm[1] = msg[len-1];
	   rpt_content.slot_q.leapalm[2] = '\0';
	}
	else
	{
	   rpt_content.slot_q.leapalm[0] = '0';
	   rpt_content.slot_q.leapalm[1] = '0';
	   rpt_content.slot_q.leapalm[2] = '\0';
	}
#if 0
    get_time_fromnet();
    ret = strncmp1( mcp_date, leapDate, strlen(mcp_date));
    if( ret <= 0)
    {
        g_LeapFlag = 1;
    }
    else
    {
        g_LeapFlag = 0;
        return 1;
    }

    ret = strncmp1( mcp_time, leapTime, strlen(mcp_time));
    if( ret <= 0)
    {
        g_LeapFlag = 1;
    }
    else
    {
        g_LeapFlag = 0;
        return 1;
    }
#endif
    g_LeapFlag = 1;

    printf("###%s\n", conf_content.slot_u.leap_num);
    return 0;

}

void sta1_alm(unsigned char *sendbuf, unsigned char slot)
{
    if( 1 == (alm_sta[20][1] & 0x01) )
    {
        memset(sendbuf, '\0', SENDBUFSIZE);
        sprintf((char *)sendbuf, "SET-STA1:000000::MONT/GGIO1/Alm=1;");
        sendtodown(sendbuf, slot);
    }
    if( 0x02 == (alm_sta[20][1] & 0x02))
    {
        memset(sendbuf, '\0', SENDBUFSIZE);
        sprintf((char *)sendbuf, "SET-STA1:000000::MONT/GGIO1/Alm1=1;");
        sendtodown(sendbuf, slot);
    }
}

void sta1_portnum(unsigned char *sendbuf, unsigned char slot)
{
    unsigned char i;
    unsigned char portNum = 0;
    for(i = 0; i < 14; i++)
    {
        if( 'O' != slot_type[i] )
        {
            portNum++;
        }
    }

    //memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/PortNum=%d;", portNum);
    sendtodown(sendbuf, slot);
}

void sta1_net(unsigned char *sendbuf, unsigned char slot)
{
#if 1

    unsigned char  net_buf[50];

    unsigned int net_ip;

    int s;
    struct ifreq ifr;
    struct sockaddr_in *ptr;
    unsigned char *interface_name = "eth0";
    if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Socket");
        return;
    }

    strncpy(ifr.ifr_name, interface_name, IFNAMSIZ);

    if(ioctl(s, SIOCGIFADDR, &ifr) < 0)
    {
        perror("Get_McuLocalIp ioctl");
        close(s);
    }
    ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
    net_ip = (unsigned int)(ptr->sin_addr.s_addr);
    //strcpy(mcp_local_ip,(unsigned char *)inet_ntoa (ptr->sin_addr));
    close(s);


    //IPAddr
    //memset(net_buf,0,50);
    //API_Get_McpIp(net_buf);
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/IPAddr=%x;", net_ip);
    sendtodown(sendbuf, slot);
    //MACAddr
    memset(net_buf, 0, 50);
    memset(sendbuf, '\0', SENDBUFSIZE);
    API_Get_McpMacAddr(net_buf);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/MACAddr=%s;", net_buf);
    sendtodown(sendbuf, slot);
#endif

}


void sta1_cpu(unsigned char *sendbuf, unsigned char slot)
{
    //CpuUsage
    //memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/CpuUsage=67;");
    sendtodown(sendbuf, slot);
}

void sta1_in_out(unsigned char *sendbuf, unsigned char slot)
{
    unsigned char temp = 0;
    unsigned char i = 0;



    //00010001
    //GPS BD
    if('O' != slot_type[16])
    {
        if( (strncmp(rpt_content.slot_q.gb_acmode, "BD", 2) == 0) )
        {
            temp |= 0x01;
        }
        else
        {
            temp |= 0x10;
        }
    }
    if('O' != slot_type[17] )
    {
        if( (strncmp(rpt_content.slot_r.gb_acmode, "BD", 2) == 0) )
        {
            temp |= 0x01;
        }
        else
        {
            temp |= 0x10;
        }
    }
    //memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/InType=");
    if((temp & 0x10) == 0x10)
    {
        sprintf((char *)sendbuf, "%sGPS ", sendbuf);
    }
    if((temp & 0x01) == 0x01)
    {
        sprintf((char *)sendbuf, "%sBD ", sendbuf);
    }

    temp = strlen(sendbuf);
    if(sendbuf[temp - 1] == ' ')
    {
        sendbuf[temp - 1] = ';';
    }

    if(sendbuf[temp - 1] == ';')
    {
        sendtodown(sendbuf, slot);
    }

    temp = 0;
    //111000
    //b鐮?588 pps+tod
    for(i = 0; i < 14; i++)
    {
        if('W' == slot_type[i])
        {
            temp |= 0x80;
        }

        if( (slot_type[i] >= 'A' && slot_type[i] <= 'H')
                || (slot_type[i] >= 'n' && slot_type[i] <= 'q') )
        {
            temp |= 0x40;
        }

        if( 'I' == slot_type[i] )
        {
            temp |= 0x20;
        }
    }
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/OutType=");
    if( (temp & 0x80) == 0x80)
    {
        sprintf((char *)sendbuf, "%sB鐮?", sendbuf);
    }

    if( (temp & 0x40) == 0x40 )
    {
        sprintf((char *)sendbuf, "%s1588 ", sendbuf);
    }

    if( (temp & 0x20) == 0x20 )
    {
        sprintf((char *)sendbuf, "%s1PPS+TOD ", sendbuf);
    }

    temp = strlen(sendbuf);
    if(sendbuf[temp - 1] == ' ')
    {
        sendbuf[temp - 1] = ';';
    }

    if(sendbuf[temp - 1] == ';')
    {
        sendtodown(sendbuf, slot);
    }

}



void send_61850_sta(unsigned char slot)
{


    unsigned char sendbuf[125];

    sta1_alm(sendbuf, slot);


    sta1_portnum(sendbuf, slot);


    sta1_net(sendbuf, slot);


    sta1_cpu(sendbuf, slot);

    sta1_in_out(sendbuf, slot);


}

int save_sta1_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];

    if(slot < 'a' || slot > 'n')
    {
        return -1;
    }
    else
    {
        num = (int)(slot - 'a');
    }

    memset(tmp, '\0', 14);
    rpt_alm_to_client(slot, parg);
    sprintf((char *)tmp, "\"%c,m,%s\"", slot, parg);
    memcpy(alm_sta[num], tmp, strlen(tmp));

    send_61850_sta(slot);
    return 0;
}
//
//!PGEIN单盘输入100S上报保存网络参数/事件CD1
//
int save_pgein_net(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1, i2, i3,len;
    j = i1 = i2 = i3  = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    //
    //!PI NET PRINTF .
    //
	//printf("PI NET %s\n",parg);	
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
           
        }
    }
    if(i3 == 0)
    {
        return (1);
    }
	//
	//! ip address
	//
    if(parg[i1] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_ip[port], &parg[i1], i2 - i1 - 1);
    }
    //
    //! gate address
    //
    if(parg[i2] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_gate[port], &parg[i2], i3 - i2 - 1);
    }
    //
    //! mask address
    //
    if(parg[i3] != '\0')
    {
        memcpy(rpt_content.slot[num].ptp_mask[port], &parg[i3], len - i3);
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    printf("PI NET %s:%s,%s,%s#\n", NET_SAVE, rpt_content.slot[num].ptp_ip[port],
 											  rpt_content.slot[num].ptp_gate[port],
                							  rpt_content.slot[num].ptp_mask[port]);
#endif
    return 0; 
        

}

//
//! PGEIN 单盘输入 工作模式100S上报/事件CD2
//
int save_pgein_mod(char slot, unsigned char *parg)
{
   int i, j, i1, i2, i3, i4, i5, i6, len;
   int num, port;
   j = i1 = i2 = i3 = i4 = i5 = i6 = 0;
   num = (int)(slot - 'a');
   port = (int)(parg[0] - 0x31);
   len = strlen(parg);
   //
   //! PGEIN
   //
   //printf("PI MOD %s\n",parg);
   for(i = 0; i < len; i++)
   {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
            if(j == 6)
            {
                i6 = i + 1;
            }
            
        }
    }
    if(i6 == 0)
    {
        return (1);
    }
    //
    //! E2E/P2P
    //
    if((parg[i1] == '1') || (parg[i1] == '0'))
    {
        
        rpt_content.slot[num].ptp_delaytype[port] = parg[i1];
		
		rpt_content.slot[num].ptp_delaytype[1] = '\0';
		//
		//!PGEIN
		//
		//printf("PI Delaytype %d,%c\n",port,rpt_content.slot[num].ptp_delaytype[port]);
    }
	//
	//! multicast /uniticast
	//
    if((parg[i2] == '1') || (parg[i2] == '0'))
    {
        rpt_content.slot[num].ptp_multicast[port] = parg[i2];
		rpt_content.slot[num].ptp_multicast[1] = '\0';
    }
	//
	//!enq two /three
	//
    if((parg[i3] == '1') || (parg[i3] == '0'))
    {
        rpt_content.slot[num].ptp_enp[port] = parg[i3];
		rpt_content.slot[num].ptp_enp[1] = '\0';
    }
	//
	//! one step/two step
	//
    if((parg[i4] == '1') || (parg[i4] == '0'))
    {
        rpt_content.slot[num].ptp_step[port] = parg[i4];
		rpt_content.slot[num].ptp_step[1] = '\0';
		
    }
    //
    //!delay req fre
    //
    if(parg[i5] != ',')
    {
        memcpy(rpt_content.slot[num].ptp_delayreq[port], &parg[i5], i6 - i5 - 1);
		rpt_content.slot[num].ptp_delayreq[port][2] = '\0';
    }
	//
	//!pdelay req fre
	//
    if(parg[i6] != '\0')
    {
        memcpy(rpt_content.slot[num].ptp_pdelayreq[port], &parg[i6], len - i6);
		rpt_content.slot[num].ptp_pdelayreq[port][2] = '\0';
    }
    
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("PI MOD %s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
        
}

//
//!PGEIN单盘输入参考输入信号质量变化事件CD3
//
int save_pgein_clkclass(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1,len;
    j = i1 = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
	//
	//! clock class
	//
	//printf("PI clkcls %s\n",parg);
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            
           
        }
    }
    if(i1 == 0)
    {
        return (1);
    }
	//
	//! clkclass 
	//
    if(parg[i1] != '\0')
    {
        memcpy(rpt_content.slot[num].ptp_clkcls[port], &parg[i1], len - i1);
		rpt_content.slot[num].ptp_clkcls[port][3] = '\0';
    }
   
#if DEBUG_NET_INFO
    if(print_switch == 0)
    printf("PI CLK %s:%s#\n", NET_SAVE, rpt_content.slot[num].ptp_clkcls[port]);
#endif
    return 0; 
        

}


//
//! 100S上报报警
//
int save_pgein_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char tmp[14];
    memset(tmp, '\0', 14);
    rpt_alm_to_client(slot, parg);
    num = (int)(slot - 'a');
    sprintf((char *)tmp, "\"%c,%c,%s\"", slot, slot_type[num], parg);
    memcpy(alm_sta[num], tmp, strlen(tmp));
	
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("PGEIN %s:%s#\n", NET_SAVE, parg);
    }
#endif
    return 0;
        

}

//
//!PGE4V2单盘输出100S上报
//
int save_pge4v_net(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1, i2, i3, i4,len;
	unsigned char datalen;
	unsigned char errflag;
	datalen = errflag = 0;
    j = i1 = i2 = i3  =  i4 = 0;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    //
    //slot range 0 ~ 13。
    //
	if((num < 0) || (num > 13))
		return 1;
    //
    //port range 0 ~ 3。
    //
	if((port < 0) ||(port > 3))
		return 1;
		
   
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
			if(j == 4)
			{
			    i4 = i + 1;
			}
           
        }
    }
	//
	//error ,return 1。
    if(i4 == 0)
    {
        return (1);
    }
    //
	//Judge the data len or not correct.
	//
	datalen = i2 - i1 -1; 
	if(datalen >  MAX_MAC_LEN)
		errflag = 1;
			
	datalen = i3 - i2 -1; 
	if(datalen >  MAX_IP_LEN)
		errflag = 1;
			
	datalen = i4 - i3 -1;
	if(datalen >  MAX_IP_LEN)
		errflag = 1;

	datalen = len - i4;
	if(datalen >  MAX_IP_LEN)
		errflag = 1;

	if(1 == errflag)
		return 1;
	//
	//! mac address
	//
    if(parg[i1] != ',')
    {
        memset(rpt_content.slot[num].ptp_mac[port],'\0',18);
        memcpy(rpt_content.slot[num].ptp_mac[port], &parg[i1], i2 - i1 - 1);  
    }
    //
    //! ip address
    //
    if(parg[i2] != ',')
    {
        memset(rpt_content.slot[num].ptp_ip[port],'\0',MAX_IP_LEN + 1);
        memcpy(rpt_content.slot[num].ptp_ip[port],  &parg[i2], i3 - i2 - 1);
    }
    //
    //! mask address
    //
    if(parg[i3] != ',')
    {
        memset(rpt_content.slot[num].ptp_mask[port],'\0',MAX_IP_LEN + 1);
        memcpy(rpt_content.slot[num].ptp_mask[port], &parg[i3], i4 - i3 - 1);
    }
	//
	//! gate address
	//
	if(parg[i4] != '\0')
	{
		memset(rpt_content.slot[num].ptp_gate[port],'\0',MAX_IP_LEN + 1);
		memcpy(rpt_content.slot[num].ptp_gate[port], &parg[i4], len - i4);
	}

#if DEBUG_NET_INFO
    if(print_switch == 0)
    printf("PGE4S NET %s:%s,%s,%s,%s#\n", NET_SAVE, rpt_content.slot[num].ptp_mac[port],
    										  rpt_content.slot[num].ptp_ip[port],
 											  rpt_content.slot[num].ptp_gate[port],
                							  rpt_content.slot[num].ptp_mask[port]);
#endif
    return 0; 
        

}
//
//!PGE4S单盘输出工作模式100S上报
//
int save_pge4v_mod(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13,len;
    unsigned char datalen;
	datalen = 0;
    j = i1 = i2 = i3  =  i4 = i5 = i6 = 0;
	i7 = i8 = i9 = i10 = i11 = i12 = i13 = 0;
	//
	//num , port , len ;
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    //
    //slot range 0 ~ 13。
    //
	if((num < 0) || (num > 13))
		return 1;
    //
    //port range 0 ~ 3。
    //
	if((port < 0) ||(port > 3))
		return 1;
		
   
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
			if(j == 4)
			{
			    i4 = i + 1;
			}
			if(j == 5)
			{
			    i5 = i + 1;
			}
			if(j == 6)
			{
			    i6 = i + 1;
			}
			if(j == 7)
            {
                i7 = i + 1;
            }
			if(j == 8)
			{
			    i8 = i + 1;
			}
			if(j == 9)
			{
			    i9 = i + 1;
			}
			if(j == 10)
			{
			    i10 = i + 1;
			}
			if(j == 11)
			{
			    i11 = i + 1;
			}
			if(j == 12)
			{
			    i12 = i + 1;
			}
			if(j == 13)
			{  
			    i13 = i + 1;
			}
			
           
        }
    }
	//
	//error ,return 1。
    if(i12 == 0)
    {
        return (1);
    }
   
	//
	//! enable。
	//
    if((parg[i1] == '0') || (parg[i1] == '1'))
    {
        rpt_content.slot[num].ptp_en[port] = parg[i1];  
    }
    //
    //! esmc enable。
    //
    if((parg[i2] == '0') || (parg[i2] == '1'))
    {
        rpt_ptp_mtc.esmc_en[num][port] = parg[i2];
    }
    //
    //! delaytype 
    //
    if((parg[i3] == '0') || (parg[i3] == '1'))
    {
        rpt_content.slot[num].ptp_delaytype[port] = parg[i3];
    }
	//
	//! multicast
	//
	if((parg[i4] == '0') || (parg[i4] == '1'))
	{
		rpt_content.slot[num].ptp_multicast[port] = parg[i4];
	}
	
	//
	//! enp
	//
	if((parg[i5] == '0') || (parg[i5] == '1'))
	{
		rpt_content.slot[num].ptp_enp[port] = parg[i5];
	}
	//
	//! step
	//
	if((parg[i6] == '0') || (parg[i6] == '1'))
	{
		rpt_content.slot[num].ptp_step[port] = parg[i6];
	}
	//
	//! domain
	//
	if(parg[i7] != ',')
	{  
	    datalen  = i8 - i7 - 1;
	    if(datalen <= MAX_DOM_LEN)
		{ 
			 memset(rpt_ptp_mtc.ptp_dom[num][port],'\0', 4);
			 memcpy(rpt_ptp_mtc.ptp_dom[num][port], &parg[i7], datalen);
		}
	}
	//
	//!sync fre
	//
	if(parg[i8] != ',')
	{
	   datalen = i9 - i8 - 1;
	   if(datalen == FRAME_FRE_LEN)
	   		memcpy(rpt_content.slot[num].ptp_sync[port], &parg[i8], datalen);
	  
	}
	//
	//!announce。
	//
	if(parg[i9] != ',')
	{
	   datalen = i10 - i9 - 1;
	   if(datalen == FRAME_FRE_LEN)
	   		memcpy(rpt_content.slot[num].ptp_announce[port], &parg[i9], datalen);
	   	 
	}
	//
	//!delaycom
	//
	if(parg[i10] != ',')
	{
	   datalen = i11 - i10 - 1;
	   if(datalen  <= DELAY_COM_LEN)
	   {
	       memset(rpt_content.slot[num].ptp_delaycom[port],'\0', 9);
	       memcpy(rpt_content.slot[num].ptp_delaycom[port], &parg[i10], datalen);
	   }
	}
	//
	//!prio 1
	//
	if(parg[i11] != ',')
	{
	   datalen = i12 - i11 - 1;
	   if(datalen  <= MAX_PRIO_LEN)
	   {
	       memset(rpt_ptp_mtc.ptp_prio1[num][port],'\0', 5);
	       memcpy(rpt_ptp_mtc.ptp_prio1[num][port], &parg[i11], datalen);
	   }
	}
	//
	//! prio 2
	//
	if(parg[i12] != ',')
	{
	   datalen = i13 - i12 - 1;
	   if(datalen  <= MAX_PRIO_LEN)
	   {
	       memset(rpt_ptp_mtc.ptp_prio2[num][port],'\0', 5);
	       memcpy(rpt_ptp_mtc.ptp_prio2[num][port], &parg[i12], datalen);
	   }
	}
	//
	//! ptp versionptp_ver
	//
    if(parg[i13] != '\0')
	{
	   datalen = len - i13;
	   if(datalen  <= 9)
	   {
	       memset(rpt_ptp_mtc.ptp_ver[num][port],'\0', 10);
	       memcpy(rpt_ptp_mtc.ptp_ver[num][port], &parg[i13], datalen);
	   }
	}
#if DEBUG_NET_INFO
    if(print_switch == 0)
    printf("PGE4S MOD %s:%s#\n", NET_SAVE, rpt_ptp_mtc.ptp_ver[num][port]);
#endif
    return 0; 
        

}

//
//!PGE4S单盘输出单播对端100S上报
//
int save_pge4v_mtc(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1, i2, i3, i4,len;//, i5, i6, i7, i8, i9, i10, i11, i12, i13
    unsigned char datalen;
	datalen = 0;
    j = i1 = i2 = i3  =i4 = 0;
	
	//
	//num , port , len ;
	//
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    //
    //slot range 0 ~ 13。
    //
	if((num < 0) || (num > 13))
		return 1;
    //
    //port range 0 ~ 3。
    //
	if((port < 0) ||(port > 3))
		return 1;
		
    
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
			if(j == 4)
			{
			    i4 = i + 1;
			}  
        }
    }
	//
	//error ,return 1。
    if(i4 == 0)
    {
        return (1);
    }
	//
	//! mtc ip。
	//
    if(parg[i1] != ',')
    {
        datalen = i2 - i1 - 1;
		if(datalen <= MAX_IP_LEN)
		{
        	memset(rpt_ptp_mtc.ptp_mtc_ip[num][port], '\0', 9);
        	memcpy(rpt_ptp_mtc.ptp_mtc_ip[num][port], &parg[i1], datalen); 
		}
    }
    //
    //! sfp modul type。
    //
    if(parg[i2] != ',')
    {
        datalen = i3 - i2 - 1; 
		if(datalen == FRAME_FRE_LEN)
		{
		    memset(rpt_ptp_mtc.sfp_type[num][port], '\0', 3);
        	memcpy(rpt_ptp_mtc.sfp_type[num][port], &parg[i2], datalen);
		}
    }
    //
    //! op low. 
    //
    if(parg[i3] != ',')
    {
        datalen = i4 - i3 - 1;
		if(datalen <= MAX_OPTHR_LEN)
		{
		    memset(rpt_ptp_mtc.sfp_oplo[num][port], '\0', 7);
        	memcpy(rpt_ptp_mtc.sfp_oplo[num][port], &parg[i3], datalen);
		}
    }
	//
	//! op hi.
	//
	if(parg[i4] != '\0')
	{
	   datalen = len - i4;
	   if(datalen <= MAX_OPTHR_LEN)
	   {
		    memset(rpt_ptp_mtc.sfp_ophi[num][port], '\0', 7);
        	memcpy(rpt_ptp_mtc.sfp_ophi[num][port], &parg[i4], datalen);
	   }
	}
	
#if DEBUG_NET_INFO
    if(print_switch == 0)
    printf("PGE4S%s:%s#\n", NET_SAVE, rpt_ptp_mtc.ptp_mtc_ip[num][port]);
#endif
    return 0; 
        
}
//
//!PGE4S单盘输出PTP状态字节100S上报
//
int save_pge4v_sta(char slot, unsigned char *parg)
{
    int num, port;
    int i, j, i1, i2, i3, i4,len;//, i5, i6, i7, i8, i9, i10, i11, i12, i13
    unsigned char datalen;
	datalen = 0;
    j = i1 = i2 = i3  =i4 = 0;
	
	//
	//num , port , len ;
	//
    num = (int)(slot - 'a');
    port = (int)(parg[0] - 0x31);
    len = strlen(parg);
    //
    //slot range 0 ~ 13。
    //
	if((num < 0) || (num > 13))
		return 1;
    //
    //port range 0 ~ 3。
    //
	if((port < 0) ||(port > 3))
		return 1;
		
    
    for(i = 0; i < len; i++)
    {
        if(parg[i] == ',')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
			if(j == 4)
			{
			    i4 = i + 1;
			}  
        }
    }
	//
	//error ,return 1。
    if(i4 == 0)
    {
        return (1);
    }
	//
	//! time source。
	//
    if(parg[i1] != ',')
    {
        datalen = i2 - i1 - 1;
		if(datalen == MAX_TIMES_LEN)
		{
        	memset(rpt_ptp_mtc.time_source[num][port], '\0', 5);
        	memcpy(rpt_ptp_mtc.time_source[num][port], &parg[i1], datalen); 
		}
    }
    //
    //! clock class。
    //
    if(parg[i2] != ',')
    {
        datalen = i3 - i2 - 1; 
		if(datalen <= MAX_CLKCLS_LEN)
		{
		    memset(rpt_ptp_mtc.clock_class[num][port], '\0', 4);
        	memcpy(rpt_ptp_mtc.clock_class[num][port], &parg[i2], datalen);
		}
    }
    //
    //! time acc. 
    //
    if(parg[i3] != ',')
    {
        datalen = i4 - i3 - 1;
		if(datalen <= MAX_TIMES_LEN)
		{
		    memset(rpt_ptp_mtc.time_acc[num][port], '\0', 5);
        	memcpy(rpt_ptp_mtc.time_acc[num][port], &parg[i3], datalen);
		}
    }
	//
	//! synce ssm.
	//
	if(parg[i4] != '\0')
	{
	   datalen = len - i4;
	   if(datalen <= MAX_SYNCSSM_LEN)
	   {
		    memset(rpt_ptp_mtc.synce_ssm[num][port], '\0', 14);
        	memcpy(rpt_ptp_mtc.synce_ssm[num][port], &parg[i4], datalen);
	   }
	}
	
#if DEBUG_NET_INFO
    if(print_switch == 0)
    printf("PGE4S%s:%s#\n", NET_SAVE, rpt_ptp_mtc.synce_ssm[num][port]);
#endif
    return 0; 
        
}

//
//!PGE4S单盘输出ALM 100S上报
//
int save_pge4v_alm(char slot, unsigned char *parg)
{
    int num;
    unsigned char  tmp[14];
    memset(tmp, '\0', 14);
    num = (int)(slot - 'a');
    //
    //!屏蔽、告警产生、告警消除
    //
    rpt_alm_to_client(slot, parg);
    sprintf((char *)tmp, "\"%c,j,%s\"", slot, parg);
    
    if((parg[0] >> 4) == 0x05) 
   		memcpy(alm_sta[PGE4S_ALM5_START + num], tmp, strlen(tmp));
    else
        memcpy(alm_sta[num], tmp, strlen(tmp));
	//printf("pge4v2 alarm %s\n",parg);
    return 0;
        
}







int send_init_config(char slot, unsigned char *parg)
{
    int num;

	printf("###send_init_config\n");
    if(slot < 'a' || slot > 'r')
    {
        return -1;
    }
    else
    {
        num = (int)(slot - 'a');
    }
	printf("###parg:%s\n",parg);
    printf("###init_pan:%d\n",num);
	
	init_pan(slot);
    return 0;
}



/**********************/
_REVT_NODE REVT_fun[] =
{
    {"C23", save_gb_rcv_mode},
    {"C25", save_gb_work_mode},
    {"C26", save_gb_mask},
    {"C01", save_rb_msmode},
    {"C02", save_rb_src},

    {"CF1", save_gb_rcv_mode},
    {"CF2", save_gb_mask},
    {"CF3", save_gbtp_sta},
    {"CF4", save_gb_tod},
    {"CF5", save_gbtp_delay},
    {"CF6", save_pps_delay},
    {"C24", save_gb_bdzb},

    {"C05", save_rb_prio},
    {"C06", save_rb_mask},
    {"C07", save_rf_tzo},
    {"C08", save_rb_dey_chg},
    {"C09", save_rb_sta_chg},

    {"C03", save_rb_leap_chg},
    {"C04", save_rb_leap_flg},
    {"C31", save_tod_sta},
    {"C41", save_out_lev},
    {"C42", save_out_typ},

    {"C43", save_out_sta},
    {"C44", save_out_prr},
    {"C51", save_ptp_net},
    {"C52", save_ptp_mod},
    {"C14", save_ptp_mtc16},   //ptp mtc ip
    
    {"C53", save_ptp_mtc},
    {"CC7", save_ptp_sta},     /*20170616 add		  */
    {"CC9", save_Leap_sta},    /*Satellite leap change*/
	{"CC8", save_ptp_dom},     /*20170616 add		 */
    {"C61", save_ref_prio},
    {"C62", save_ref_mod},

    {"C63", save_ref_sa},
    {"C64", save_ref_tl},
    {"C65", save_ref_en},
    {"C67", save_ref_ssm_en},
    {"C71", save_rs_en},
    {"C72", save_rs_tzo},

    {"C81", save_ppx_mod},
    {"CA1", save_tod_en},
    {"CA2", save_tod_pro},
    {"CB1", save_igb_en},
    {"CB2", save_igb_rat},

    {"CB3", save_igb_max},
    {"CC1", save_ntp_pnet},
    {"CC2", save_ntp_en},
    {"CC3", save_ntp_key},
    {"CC4", save_ntp_ms},
    {"CC5", save_ntp_sta},
	{"CC6", save_ntp_psta},
	
	{"CD1", save_pgein_net},     
	{"CD2", save_pgein_mod},     
	{"CD3", save_pgein_clkclass},
	
	{"CE1", save_pge4v_net},     
	{"CE2", save_pge4v_mod},     
	{"CE3", save_pge4v_mtc},
	{"CE4", save_pge4v_sta},     
	
    {"C84", save_2mb_lvlm},   /*new add*/
    {"C10", save_rb_sa},
    {"C12", save_ok},
    {"C13", save_ok}
   
};

int NUM_REVT = ((int)(sizeof(REVT_fun) / sizeof(_REVT_NODE)));

_RPT100S_NODE RPT100S_fun[] =
{
    {"R-GB-STA", 8, "0", save_gb_sta}, 
    {"R-GB-ORI", 8, "C28", save_gb_ori},
    {"R-GB-REV", 8, "C27", save_gb_rev},
    {"R-GB-TOD", 8, "C29", save_gb_tod},
    {"R-GB-ALM", 8, "0", save_gb_alm}, 		//GB ALARM
    
    {"R-GBTP-STA",10,"0",save_gbtp_sta},	//GBTPV2 Status.R-GBTP-STA
    {"R-GBTP-PST",10,"0",save_gbtp_pst},	//GBTPV2 Postion.R-GBTP-PST
    {"R-GBTP-INFO",11,"0", save_gbtp_info},	//GBTPV2 Sat info.R-GBTP-INFO
    {"R-GBTP-ALM",10,"0", save_gbtp_alm},	//GBTPV2 ALM.R-GBTP-ALM
    {"R-GBTP-DLY",10,"0", save_gbtp_delay}, //GBTPIII DLY.R-GBTP-DLY 
    {"R-PPS-DLY",9,"0", save_pps_delay},    //GBTPIII DLY.R-PPS-DLY
    
    {"R-GB-MASK", 9, "0", save_gb_mask}, 
    {"R-SY-VER", 8, "0", save_sy_ver},
    {"R-RX-DPH", 8, "C00", save_rx_dph},
    {"R-RF-SRC", 8, "0", save_rf_src},
    {"R-RB-TOD", 8, "0", save_rb_tod},

    {"R-RF-DEY", 8, "0", save_rf_dey},
    {"R-RF-TZO", 8, "0", save_rf_tzo},
    {"R-RX-STA", 8, "0", save_rx_sta},
    {"R-RX-ALM", 8, "0", save_rx_alm},
    {"R-TOD-STA", 9, "0", save_tod_sta},

    {"R-TP-ALM", 8, "0", save_tp_alm},
    {"R-OUT-LEV", 9, "0", save_out_lev},
    {"R-OUT-TYP", 9, "0", save_out_typ},
    {"R-OUT-ALM", 9, "0", save_out_alm},//OUT32 ALARM
    {"R-OUT-STA", 9, "0", save_out_sta},

    {"R-PTP-NET", 9, "0", save_ptp_net},
    {"R-PTP-MOD", 9, "0", save_ptp_mod},
    {"R-PTP-ALM", 9, "0", save_ptp_alm},//R-PTP-ALM
    {"R-REF-PRIO", 10, "0", save_ref_prio},
    {"R-REF-MOD", 9, "0", save_ref_mod},

    {"R-REF-PH", 8, "C66", save_ref_ph},
    {"R-REF-STA", 9, "0", save_ref_sta},
    {"R-REF-ALM", 9, "0", save_ref_alm},//REF ALM
    {"R-REF-TPY", 9, "0", save_ref_tpy},
    {"R-REF-IEN", 9, "0", save_ref_ssm_en},
    {"R-RS-EN", 7, "0", save_rs_en},

    {"R-RS-TZO", 8, "0", save_rs_tzo},
    {"R-PPX-MOD", 9, "0", save_ppx_mod},
    {"R-TOD-EN", 8, "0", save_tod_en},
    {"R-TOD-PRO", 9, "0", save_tod_pro},
    {"R-IGB-EN", 8, "0", save_igb_en},

    {"R-IGB-RAT", 9, "0", save_igb_rat},
    {"R-IGB-MAX", 9, "0", save_igb_max},
    
    {"R-IGB-ZONE", 10, "0", save_igb_tzone},/*igb zone set******/
    
    {"R-NTP-STA", 9, "0", save_ntp_sta},
    {"R-NTP-NET", 9, "0", save_ntp_net},
    {"R-NTP-PNET", 10, "0", save_ntp_pnet},
    
    {"R-NTP-EN", 8, "0", save_ntp_en},
    {"R-NTP-PSTA",10, "0", save_ntp_psta},
    {"R-NTP-KEY", 9, "0", save_r_ntp_key},
    {"R-NTP-MS", 8, "0", save_ntp_ms},
    {"R-NTP-ALM", 9, "0", save_ntp_alm},//NTP ALM
    {"R-OUT-CFG", 9, "0", save_ext_bid},
    {"R-OUT-MGR", 9, "0", save_ext_mgr_pr},

    {"R-LEAP-ALM", 10, "0", save_leap_alm},
    {"R-STA1-ALM", 10, "0", save_sta1_alm},
    
    {"R-PGEIN-NET", 11, "0", save_pgein_net},
    {"R-PGEIN-MOD", 11, "0", save_pgein_mod},
    {"R-PGEIN-ALM", 11, "0", save_pgein_alm},
    
    {"R-PGE4V-NET", 11, "0", save_pge4v_net},
    {"R-PGE4V-MOD", 11, "0", save_pge4v_mod},
    {"R-PGE4V-MTC", 11, "0", save_pge4v_mtc},
    {"R-PGE4V-STA", 11, "0", save_pge4v_sta},
    {"R-PGE4V-ALM", 11, "0", save_pge4v_alm},
	
    
	{"R-CONFIG", 8, "0", send_init_config}   /*20150909 init pan*/
	

};

int _NUM_RPT100S = ((int)(sizeof(RPT100S_fun) / sizeof(_RPT100S_NODE))); //2014.12.5

<file_sep>/MCPU/ext_global.h
#ifndef	__EXT_GLOBAL__
#define	__EXT_GLOBAL__







#include "ext_ctx.h"









extern struct extctx gExtCtx;












#endif//__EXT_GLOBAL__


<file_sep>/MCPU/ext_type.h
#ifndef	__EXT_TYPE__
#define	__EXT_TYPE__






typedef unsigned char		u8_t;
typedef signed char			s8_t;

typedef unsigned short		u16_t;
typedef signed short		s16_t;

typedef unsigned int		u32_t;
typedef signed int			s32_t;

typedef unsigned long long	u64_t;
typedef signed long long	s64_t;

typedef enum {false = 0, true = 1} bool_t;



//0~31
#define BIT(i)	(1<<(i))









#endif//__EXT_TYPE__



<file_sep>/MCPU/ext_crpt.h
#ifndef	__EXT_CRPT__
#define	__EXT_CRPT__







#include "ext_ctx.h"






u8_t ext_out_ssm_crpt(u8_t save_flag, u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_signal_crpt(u8_t save_flag, u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_mode_crpt(u8_t save_flag, u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);
u8_t ext_out_prr_crpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);

u8_t ext_out_alm_crpt(u8_t ext_sid, u8_t *ext_data, struct extctx *ctx);










#endif//__EXT_CRPT__


<file_sep>/MCPU/Makefile
#arm-elf-gcc -DMEMWATCH -DMW_STDIO
export CC := arm-elf-gcc
export CFLAGS := -Wall -O3 -pipe


TARGET := mcpu


LOCAL_OBJS := $(patsubst %.c, %.o, $(wildcard *.c))


.PHONY : all
all : $(TARGET)

$(TARGET) : $(LOCAL_OBJS)
	$(CC) $(CFLAGS) -o $@ $+ -elf2flt -lpthread


%.o : %.c
	$(CC) -c $(CFLAGS) $< -o $@


.PHONY : clean
clean :
	rm -f $(TARGET) *.o *.gch *.gdb *.elf *.elf2flt *.bak
<file_sep>/MCPU/mcp_process.c
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/msg.h>
//#include <sys/select.h>
//#include <sys/time.h>

#include <signal.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>


#include "ext_cmn.h"
#include "ext_global.h"
#include "ext_crpt.h"
#include "ext_trpt.h"
#include "ext_alloc.h"

#include "mcp_process.h"

#include "mcp_def.h"
#include "mcp_set_struct.h"
#include "mcp_save_struct.h"
//#include "memwatch.h"


#define MGE_RALM_NUM  4

int shake_hand_count = 0;
int reset_network_card = 0;
//int _CMD_NODE_MAX = ((int)(sizeof(g_cmd_fun)/sizeof(_CMD_NODE)));


const char *g_cmd_fun_ret[] = { "ENEQ", "IIAC", "ICNV", "IDNV",
                                "IISP", "IITA", "INUP", "SCSN", "IPMS", "PSWD"
                              };

unsigned char ssm_alm_last = 0;//0表示无告警
unsigned char ssm_alm_curr = 0;//0表示无告警


void strncpynew(unsigned char *str1, unsigned char *str2, int n)
{
    strncpy(str1, str2, n);
    *(str1 + n) = '\0';
}
void strncatnew(unsigned char *str1, unsigned char *str2, int n)
{
    Uint16 strlength;
    strlength = strlen(str1);
    strncat(str1, str2, n);
    *(str1 + strlength + n) = '\0';
}

int strncmp1(unsigned char *p1, unsigned char *p2, unsigned char n)
{
    unsigned int  i = 0;
    signed char  j = 0;
    for (i = 0; i < n; i++)
    {
        if (p1[i] == p2[i])
        {
            ;
        }
        else if(p1[i] > p2[2])
        {
            j = 1;
            break;
        }
        else
        {
            j = -1;
            break;
        }
    }
    return j;
}

void timefunc(int sig)
{
    int count, i;
    int  client_num[MAX_CLIENT];

    count = judge_client_online(client_num);

    for(i = 0; i < count; i++)
    {
        if(timeout_cnt[client_num[i]] == 0)
        {
            FD_CLR(client_fd[client_num[i]], &allset);
			close(client_fd[client_num[i]]);
            client_fd[client_num[i]] = -1;
            printf("<timeout>close client<%d>\n", client_num[i]);
        }

        timeout_cnt[client_num[i]] = 0;
    }

    reset_network_card = 1;
}

int init_timer()
{
    struct itimerval value;
    memset(&value, 0, sizeof(struct itimerval));
    value.it_value.tv_sec = 120;
    value.it_interval.tv_sec = 120;
    signal(SIGALRM, timefunc);             /*捕获定时信号 */
    setitimer(ITIMER_REAL, &value, NULL);  /*定时开始     */
    //printf("250s timer startx\n");
    return 0;
}

int msgQ_create( const char *pathname, char proj_id )
{
    key_t key;
    int msgQ_id;
    /* Create unique key via call to ftok() */
    key = ftok( pathname, proj_id );
    if ( -1 == key )
    {
        return(-1);
    }
    /* Open the queue,create if necessary */
    msgQ_id = msgget( key, IPC_CREAT | 0660 );
    if( -1 == msgQ_id )
    {
        return (-1);
    }
#if DEBUG_NET_INFO
    printf("%s:qid=%d\n", NET_INFO, msgQ_id);
#endif

    return (msgQ_id);
}

int almMsk_id(int index, char type)
{
    if(type != conf_content.alm_msk[index].type)
    {
        conf_content.alm_msk[index].type = type;
        return 0;
    }
    else
    {
        return 1;
    }
}

void init_almMsk(void)
{
    if(almMsk_id(0, 'L') == 0 )//GBTP Y0 Y1, 
    {
        memset(conf_content.alm_msk[0].data, 0x4f, 2);
        memset(conf_content.alm_msk[0].data + 2, 0, 22);
        conf_content.alm_msk[0].data[0] = 0x4b;
    }
    else
    {
        memset(conf_content.alm_msk[0].data + 2, 0, 22);
        conf_content.alm_msk[0].data[0] &= ~(1 << 2);  //alrm mask bit 2 ,1pps+tod alarm.
    }

    if(almMsk_id(1, 'g') == 0 )//GBTP2 
    {
        memset(conf_content.alm_msk[1].data, 0x4f, 2);
        memset(conf_content.alm_msk[1].data + 2, 0, 22);
        conf_content.alm_msk[1].data[0] = 0x4b;
    }
    else
    {
        memset(conf_content.alm_msk[1].data + 2, 0, 22);
        conf_content.alm_msk[1].data[0] &= ~(1 << 2); // alrm mask bit 2 ,1pps+tod alarm.
    }

    if(almMsk_id(2, 'P') == 0 )//GTP
    {
        memset(conf_content.alm_msk[2].data, 0x4f, 2);
        memset(conf_content.alm_msk[2].data + 2, 0, 22);
        conf_content.alm_msk[2].data[0] = 0x49;
    }
    else
    {
        memset(conf_content.alm_msk[2].data + 2, 0, 22);
        conf_content.alm_msk[2].data[0] &= ~(3 << 1);
    }

    if(almMsk_id(3, 'e') == 0 )//GGTP
    {
        memset(conf_content.alm_msk[3].data, 0x4f, 2);
        memset(conf_content.alm_msk[3].data + 2, 0, 22);
        conf_content.alm_msk[3].data[0] = 0x4b;
    }
    else
    {
        memset(conf_content.alm_msk[3].data + 2, 0, 22);

        conf_content.alm_msk[3].data[0] &= ~(1 << 2);
    }

	if(almMsk_id(4, 'l') == 0 )//BTP
    {
        memset(conf_content.alm_msk[4].data, 0x4f, 2);
        memset(conf_content.alm_msk[4].data + 2, 0, 22);
        conf_content.alm_msk[4].data[0] = 0x4a;
    }
    else
    {
        memset(conf_content.alm_msk[4].data + 2, 0, 22);
		conf_content.alm_msk[4].data[0] &= ~(1 << 0);
        conf_content.alm_msk[4].data[0] &= ~(1 << 2);
    }

    if(almMsk_id(5, 'K') == 0 )//RB
    {
        memset(conf_content.alm_msk[5].data, 0x4f, 5);
        memset(conf_content.alm_msk[5].data + 5, 0, 19);
        conf_content.alm_msk[5].data[0] = 0x43;        //   Mask 17# 18# 1pp+tod 
        conf_content.alm_msk[5].data[2] = 0x4b;        //
        conf_content.alm_msk[5].data[4] = 0x4e;      
        conf_content.alm_msk[5].data[1] &= ~(0x3 << 2);// mask LOF、AIS
        conf_content.alm_msk[5].data[2] &= ~(0x7 << 0);// mask BPV、CRC
    }
    else
    {
        memset(conf_content.alm_msk[5].data + 5, 0, 19);
        conf_content.alm_msk[5].data[0] &= ~( (1 << 2) | (1 << 3) );//mask 17# 18# 1pps+tod
        //conf_content.alm_msk[4].data[2] &= ~(1 << 2);//modify:2017-07-19, delete the Null mask.
        conf_content.alm_msk[5].data[4] &= ~1;     
        conf_content.alm_msk[5].data[1] &= ~(0x3 << 2);
        conf_content.alm_msk[5].data[2] &= ~(0x7 << 0);

    }

    if(almMsk_id(6, 'R') == 0 )
    {
        memset(conf_content.alm_msk[6].data, 0x4f, 5);
        memset(conf_content.alm_msk[6].data + 5, 0, 19);
        conf_content.alm_msk[6].data[0] = 0x43;
        conf_content.alm_msk[6].data[2] = 0x4b;
        conf_content.alm_msk[6].data[4] = 0x4e;
    }
    else
    {
        memset(conf_content.alm_msk[6].data + 5, 0, 19);
        conf_content.alm_msk[6].data[0] &= ~( (1 << 2) | (1 << 3) );
        //conf_content.alm_msk[5].data[2] &= ~(1 << 2);//modify:2017-07-19, delete the Null mask.
        conf_content.alm_msk[6].data[4] &= ~1;
    }

    if(almMsk_id(7, 'I') == 0 )//TP16
    {
        memset(conf_content.alm_msk[7].data, 0x4f, 6);
        memset(conf_content.alm_msk[7].data + 6, 0, 18);
        conf_content.alm_msk[7].data[0] = 0x40;
    }
    else
    {
        memset(conf_content.alm_msk[7].data + 6, 0, 18);
        //conf_content.alm_msk[7].data[0] = 0x40;
    }

    if(almMsk_id(8, 'J') == 0 )//OUT16
    {
        memset(conf_content.alm_msk[8].data, 0x4f, 6);
        memset(conf_content.alm_msk[8].data + 6, 0, 18);
    }
    else
    {
        memset(conf_content.alm_msk[8].data + 6, 0, 18);
    }

    if(almMsk_id(9, 'a') == 0 )//OUT32
    {
        memset(conf_content.alm_msk[9].data, 0x4f, 6);
        memset(conf_content.alm_msk[9].data + 6, 0, 18);
    }
    else
    {
        memset(conf_content.alm_msk[9].data + 6, 0, 18);
    }


    if(almMsk_id(10, 'b') == 0 )//OUTH32
    {
        memset(conf_content.alm_msk[10].data, 0x4f, 6);
        memset(conf_content.alm_msk[10].data + 6, 0, 18);
    }
    else
    {
        memset(conf_content.alm_msk[10].data + 6, 0, 18);
    }


    if(almMsk_id(11, 'c') == 0 )//OUTE32
    {
        memset(conf_content.alm_msk[11].data, 0x4f, 6);
        memset(conf_content.alm_msk[11].data + 6, 0, 18);
    }
    else
    {
        memset(conf_content.alm_msk[11].data + 6, 0, 18);
    }


    /*if(almMsk_id(11, 'A') == 0 )//PTP4
    {
        memset(conf_content.alm_msk[11].data, 0x4f, 3);
        memset(conf_content.alm_msk[11].data + 3, 0, 21);
        conf_content.alm_msk[11].data[0] = 0x40;
    }
    else
    {
        memset(conf_content.alm_msk[11].data + 3, 0, 21);
        conf_content.alm_msk[11].data[0] = 0x40;
    }
    if(almMsk_id(12, 'B') == 0 )//PF04
    {
        memset(conf_content.alm_msk[12].data, 0x4f, 3);
        memset(conf_content.alm_msk[12].data + 3, 0, 21);
        conf_content.alm_msk[12].data[0] = 0x40;
    }
    else
    {
        memset(conf_content.alm_msk[12].data + 3, 0, 21);
        conf_content.alm_msk[12].data[0] = 0x40;
    }
    if(almMsk_id(13, 'C') == 0 )//PGE4
    {
        memset(conf_content.alm_msk[13].data, 0x4f, 3);
        memset(conf_content.alm_msk[13].data + 3, 0, 21);
        conf_content.alm_msk[13].data[0] = 0x40;
    }
    else
    {
        memset(conf_content.alm_msk[13].data + 3, 0, 21);
        conf_content.alm_msk[13].data[0] = 0x40;
    }
    if(almMsk_id(14, 'D') == 0 )//PGO4
    {
        memset(conf_content.alm_msk[14].data, 0x4f, 3);
        memset(conf_content.alm_msk[14].data + 3, 0, 21);
        conf_content.alm_msk[14].data[0] = 0x40;
    }
    else
    {
        memset(conf_content.alm_msk[14].data + 3, 0, 21);
        conf_content.alm_msk[14].data[0] = 0x40;
    }*/

    if(almMsk_id(12, 'S') == 0 )//REFTF
    {
        memset(conf_content.alm_msk[12].data, 0x4f, 6);
        memset(conf_content.alm_msk[12].data + 6, 0x5f, 6);
        memset(conf_content.alm_msk[12].data + 12, 0, 12);
    }
    else
    {
        memset(conf_content.alm_msk[12].data + 12, 0, 12);
    }

    if(almMsk_id(13, 'U') == 0 )//PPX
    {
        memset(conf_content.alm_msk[13].data, 0x40, 1);
        memset(conf_content.alm_msk[13].data + 1, 0, 23);
    }
    else
    {
        memset(conf_content.alm_msk[13].data + 1, 0, 23);
        //conf_content.alm_msk[13].data[0] = 0x40;
    }

    if(almMsk_id(14, 'V') == 0 )//TOD16
    {
        memset(conf_content.alm_msk[14].data, 0x40, 1);
        memset(conf_content.alm_msk[14].data + 1, 0, 23);
    }
    else
    {
        memset(conf_content.alm_msk[14].data + 1, 0, 23);
        //conf_content.alm_msk[14].data[0] = 0x40;
    }

    if(almMsk_id(15, 'Z') == 0 )//NTP12
    {
        memset(conf_content.alm_msk[15].data, 0x4f, 4);
        memset(conf_content.alm_msk[15].data + 4, 0, 20);//modify:2017-07-19 ,alarm mask,
    }
    else
    {
        memset(conf_content.alm_msk[15].data + 4, 0, 20);//modify:2017-07-19,alarm mask.
    }

    if(almMsk_id(16, 'N') == 0 )//MCP
    {
        memset(conf_content.alm_msk[16].data, 0x4f, 6);
        //memset(conf_content.alm_msk[16].data + 6, 0x5f, 6);
        memset(conf_content.alm_msk[16].data + 6, 0, 18);

		conf_content.alm_msk[16].data[0] &= ~( (1 << 2) | (1 << 3) );
		conf_content.alm_msk[16].data[1] &= ~( 1 << 3) ;
        //conf_content.alm_msk[19].data[2] = 0x48;
		//memset(conf_content.alm_msk[19].data + 3, 0x40, 3);
    }
    else
    {
        memset(conf_content.alm_msk[16].data + 6, 0, 18);
        conf_content.alm_msk[16].data[0] &= ~( (1 << 2) | (1 << 3) );
        conf_content.alm_msk[16].data[1] &= ~(1 << 3);
        //memset(conf_content.alm_msk[19].data + 2, 0x40, 4);
    }

    if(almMsk_id(17, 'Q') == 0 )//FAN
    {
        memset(conf_content.alm_msk[17].data, 0x4f, 1);
        memset(conf_content.alm_msk[17].data + 1, 0, 23);
    }
    else
    {
        memset(conf_content.alm_msk[17].data + 1, 0, 23);
    }
    if(almMsk_id(18, 'h') == 0 )//out32s
    {
        memset(conf_content.alm_msk[18].data, 0x4f, 6);
        memset(conf_content.alm_msk[18].data + 6, 0, 18);
    }
    else
    {
        memset(conf_content.alm_msk[18].data + 6, 0, 18);
    }
    if(almMsk_id(19, 'i') == 0 )//OUT16S
    {
        memset(conf_content.alm_msk[19].data, 0x4f, 6);
        memset(conf_content.alm_msk[19].data + 6, 0, 18);
    }
    else
    {
        memset(conf_content.alm_msk[19].data + 6, 0, 18);
    }

    if(almMsk_id(20, 's') == 0 )//BPGEIN
    {
        memset(conf_content.alm_msk[20].data, 0x4f, 6);
        memset(conf_content.alm_msk[20].data + 6, 0, 18);
        
    }
    else
    {
        memset(conf_content.alm_msk[20].data + 6, 0, 18);
    }

	if(almMsk_id(21, 'j') == 0 )//pge4v2
    {
        memset(conf_content.alm_msk[21].data, 0x4f, 6);
        memset(conf_content.alm_msk[21].data + 6, 0x5f, 6);
        memset(conf_content.alm_msk[21].data + 12, 0, 12);
    }
    else
    {
        memset(conf_content.alm_msk[21].data + 12, 0, 12);
    }

	if(almMsk_id(22, 'v') == 0 )//GBTPIIV2
    {
        memset(conf_content.alm_msk[22].data, 0x4f, 2);
        memset(conf_content.alm_msk[22].data + 2, 0, 22);
    }
    else
    {
        memset(conf_content.alm_msk[22].data + 2, 0, 22);
    }
	if(almMsk_id(23, 'f') == 0 )//MGE
    {
        memset(conf_content.alm_msk[23].data, 0x4f, 4);
        memset(conf_content.alm_msk[23].data + 4, 0, 20);
    }
    else
    {
        memset(conf_content.alm_msk[23].data + 4, 0, 20);
    }

	if(almMsk_id(24, 'd') == 0 )//DRV
    {
        memset(conf_content.alm_msk[24].data, 0x4f, 1);
        memset(conf_content.alm_msk[24].data + 1, 0, 23);
    }
    else
    {
        memset(conf_content.alm_msk[24].data + 1, 0, 23);
    }
	
    memset(&(conf_content.alm_msk[25]), 0, (alm_msk_total - 26)*sizeof(struct ALM_MSK));

}



void init_var(void)
{
    int i;

    memset(slot_type, 'O', sizeof(unsigned char) * 24);
    slot_type[20] = 'N';
	//
	//!clear temp alarm cache.
	//
    memset(&flag_alm, '\0', sizeof(int) * 36 * 14);
    memset(online_sta, 0, sizeof(int) * 24);
	//
	//!clear alarm array.
	//
    memset(&alm_sta, '\0', sizeof(unsigned char) * 37 * 14);
    memset(fpga_ver, '\0', 7);
    memset(timeout_cnt, 0, sizeof(int) * 4);
    //printf("%s\n",slot_type);
    memset(&saved_alms, 0, SAVE_ALM_NUM * 50);
    saved_alms_cnt = 0;

    memcpy(mcp_date, "2011-11-11", 10);
    memcpy(mcp_time, "11:11:11", 8);
    memcpy(password, "<PASSWORD>", 8);


    memcpy(alm_sta[20], "\"u,N,@@@\"", 9);

    g_DrvLost = 200;

    i = 0;
    i = atoi(conf_content.slot_u.leap_num);
    if(strncmp1(conf_content.slot_u.leap_num, "00", 2) == 0)
    {}
    else if((i < 1) || (i > 99))
    {
        memcpy(conf_content.slot_u.leap_num, "18", 2);
    }


    /****test**/
    for(i = 0; i < 14; i++)
    {
        rpt_content.slot[i].out_mode = '0';
        rpt_content.slot[i].out_sta = '0';
    }
    /*
    	printf("slot_type=<%s>\n",slot_type);
    	printf("online=<");
    	for(i=0;i<24;i++)
    		printf("%d",online_sta[i]);
    	printf(">;\n");
    */
    atag = 0;
    flag_i_o = 0;
    flag_i_p = 0;
    time_temp = 0;
    current_client = -1;
    g_lev = -1;
    qid = -1;


    rpt_content.slot_o.rb_sa = '@';
    rpt_content.slot_p.rb_sa = '@';
    rpt_content.slot_o.rb_tl = 'g';
    rpt_content.slot_p.rb_tl = 'g';
    rpt_content.slot_o.rb_tl_2mhz = 'g';
    rpt_content.slot_p.rb_tl_2mhz = 'g';
	rpt_content.slot[0].ref_2mb_lvlm = '0';
	rpt_content.slot[1].ref_2mb_lvlm = '0';
	
	//GBTPIIV2 VAR INIT
	rpt_content.slot_q.gb_sta = '0';
    rpt_content.slot_q.staen = '1';
	
    rpt_content.slot_q.gb_tod[0] = '3';
    rpt_content.slot_q.leapalm[0] = '0';
	rpt_content.slot_q.leapalm[1] = '0';
    rpt_content.slot_q.leapnum[0] = '0';
	rpt_content.slot_q.leapnum[1] = '0';

	rpt_content.slot_r.gb_sta = '0';
    rpt_content.slot_r.staen = '1';
	
    rpt_content.slot_r.gb_tod[0] = '3';
    rpt_content.slot_r.leapalm[0] = '0';
	rpt_content.slot_r.leapalm[1] = '0';
    rpt_content.slot_r.leapnum[0] = '0';
	rpt_content.slot_r.leapnum[1] = '0';

    //告警屏蔽初始化；
    init_almMsk();
    memset(event_old, 0, 17 * 128);

    g_LeapFlag = 0;
}

int Create_Thread(bool_t * loop)
{
    pthread_t thread1;
	pthread_attr_t attr;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if(pthread_create(&thread1,&attr,(void *)timer_mcp,(void *) loop) != 0 ) { 
		printf("pthread_create1 mcp gf_Usual error.\n");
		return(false);
	}
	pthread_attr_destroy(&attr);
	return (true);
 
}


void init_sta1_ip(unsigned char slot)
{
    unsigned char ip_temp[9];
    unsigned char gate_temp[9];
    unsigned char mask_temp[9];
    if( (( (conf_content3.sta1.ip[0] ) & 0xf0 ) != 0x40) ||
            (((conf_content3.sta1.ip[2] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.ip[4] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.ip[6] ) & 0xf0) != 0x40) )
    {

        memset(conf_content3.sta1.ip, 0, 9);
        memcpy(conf_content3.sta1.ip, "L@JH@AFC", 8);
    }


    if( (((conf_content3.sta1.gate[0] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.gate[2] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.gate[4] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.gate[6] ) & 0xf0) != 0x40) )
    {

        memset(conf_content3.sta1.gate, 0, 9);
        memcpy(conf_content3.sta1.gate, "L@JH@A@A", 8);
    }

    if( (((conf_content3.sta1.mask[0] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.mask[2] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.mask[4] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.mask[6] ) & 0xf0) != 0x40) )
    {


        memset(conf_content3.sta1.mask, 0, 9);
        memcpy(conf_content3.sta1.mask, "OOOOOO@@", 8);
    }

    memset(ip_temp, 0, 9);
    memcpy(ip_temp, conf_content3.sta1.ip, 8);
    set_sta_ip(ip_temp, slot);
    sleep(1);
    memset(gate_temp, 0, 9);
    memcpy(gate_temp, conf_content3.sta1.gate, 8);
    set_sta_gateway(gate_temp, slot);
    sleep(1);
    memset(mask_temp, 0, 9);
    memcpy(mask_temp, conf_content3.sta1.mask, 8);
    set_sta_netmask(mask_temp, slot);
}

void init_sta1(int num, unsigned char *sendbuf)
{

    unsigned char slot;
    //DevType
    slot = 'a' + num;

    init_sta1_ip(slot);

    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/DevType=GNSS-97-P300;");
    sendtodown(sendbuf, (char)('a' + num));
    sendtodown(sendbuf, (char)('a' + num));
    usleep(500);
    //DevDescr
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/DevDescr=P300;");
    sendtodown(sendbuf, (char)('a' + num));
    sendtodown(sendbuf, (char)('a' + num));

#if 1

    //Company
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/Company=DTT;");
    sendtodown(sendbuf, (char)('a' + num));
    sendtodown(sendbuf, (char)('a' + num));
    //PortNum 实际为板卡数量
    sta1_portnum(sendbuf, slot);
    //HWVersion
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/HWVersion=v01.00;");
    sendtodown(sendbuf, (char)('a' + num));
    sendtodown(sendbuf, (char)('a' + num));
    //FWVersion
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/FWVersion=v01.00;");
    sendtodown(sendbuf, (char)('a' + num));
    sendtodown(sendbuf, (char)('a' + num));
    //SWVersion
    memset(sendbuf, '\0', SENDBUFSIZE);
    sprintf((char *)sendbuf, "SET-STA1:000000::MONT/LLN0/SWVersion=v01.00;");
    sendtodown(sendbuf, (char)('a' + num));
    sendtodown(sendbuf, (char)('a' + num));
    //ip mac
    sta1_net(sendbuf, slot);
    //CpuUsage
    sta1_cpu(sendbuf, slot);
    //InType OutType
    sta1_in_out(sendbuf, slot);
    //Alm
    sta1_alm(sendbuf, slot);
#endif
}

void delay_s(int iSec,int iUsec)
{ 
  struct timeval tv;
  tv.tv_sec=iSec;
  tv.tv_usec=iUsec;
  select(0,NULL,NULL,NULL,&tv);
}



void init_pan(char slot)
{
    /* RB1*/
    unsigned char out_ssm_threshold = 0, out_port = 4;
    int j, len, num, flag, len1, len2;
    unsigned char sendbuf[SENDBUFSIZE];
    unsigned char *ctag = "000000";
    unsigned char tmp1[3], tmp2[3], tmp3[3], tmp4[3], tmp5[3], tmp6[3], tmp7[3];
    //int flag_lev=0;
    num = (int)(slot - 'a');
	//printf("###init_pan:%d\n",num);
    if(slot == 20) /*mcp*/
    {
        memset(sendbuf, '\0', SENDBUFSIZE);
        num = atoi(conf_content.slot_u.leap_num);
        if(strncmp1(conf_content.slot_u.leap_num, "00", 2) == 0)
        {
		}
        else if((num < 1) || (num > 99))
        {
            return ;
        }
        sprintf((char *)sendbuf, "SET-LEAP-NUM:%s::%s;", ctag, conf_content.slot_u.leap_num);
        sendtodown(sendbuf, 'u');
    }
    else if(slot == RB1_SLOT)
    {
        if(slot_type[num] == 'Y')
        {
            return;
        }
        if(conf_content.slot_o.msmode[0] != 0xff)
        {
            if(strlen(conf_content.slot_o.msmode) == 4)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-MAIN:%s::%s;", ctag, conf_content.slot_o.msmode);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.sys_ref != 0xff)
        {
            if(((conf_content.slot_o.sys_ref > 0x29) && (conf_content.slot_o.sys_ref < 0x39))||conf_content.slot_o.sys_ref == 0x46)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-SYS-REF:%s::INP-%c;", ctag, conf_content.slot_o.sys_ref);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.priority[0] != 0xff)
        {
            if(strlen(conf_content.slot_o.priority) == 8)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PRIO-INP:%s::%s;", ctag, conf_content.slot_o.priority);
                sendtodown(sendbuf, 'o');
            }

        }
        if(conf_content.slot_o.mask[0] != 0xff)
        {
            if(strlen(conf_content.slot_o.mask) == 8)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-MASK-INP:%s::%s;", ctag, conf_content.slot_o.mask);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.dely[0][0] != 0xff)
        {
            if(strlen(conf_content.slot_o.dely[0]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::1,%s;", ctag, conf_content.slot_o.dely[0]);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.dely[1][0] != 0xff)
        {
            if(strlen(conf_content.slot_o.dely[1]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::2,%s;", ctag, conf_content.slot_o.dely[1]);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.dely[2][0] != 0xff)
        {
            if(strlen(conf_content.slot_o.dely[2]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::3,%s;", ctag, conf_content.slot_o.dely[2]);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.dely[3][0] != 0xff)
        {
            if(strlen(conf_content.slot_o.dely[3]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::4,%s;", ctag, conf_content.slot_o.dely[3]);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.tzo[0] != 0xff)
        {
            if(strlen(conf_content.slot_o.tzo) == 3)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-TZO:%s::%s;", ctag, conf_content.slot_o.tzo);
                sendtodown(sendbuf, 'o');
				printf("15 set tzo %s\n",conf_content.slot_o.tzo);
            }
        }
        if(conf_content.slot_o.leaptag != 0xff)
        {
            if((conf_content.slot_o.leaptag > 0x29) && (conf_content.slot_o.leaptag < 0x34))
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-LEAP:%s::%c;", ctag, conf_content.slot_o.leaptag);
                sendtodown(sendbuf, 'o');
            }
        }
        if(conf_content.slot_o.leapmask[0] != 0xff)
        {
            if(strlen(conf_content.slot_o.leapmask) == 5)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-LMAK-INP:%s::%s;", ctag, conf_content.slot_o.leapmask);
                sendtodown(sendbuf, 'o');
            }
        }
    }

    /*RB2*/
    else if(slot == RB2_SLOT)
    {
        if(slot_type[num] == 'Y')
        {
            return;
        }
        if(conf_content.slot_p.msmode[0] != 0xff)
        {
            if(strlen(conf_content.slot_p.msmode) == 4)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-MAIN:%s::%s;", ctag, conf_content.slot_p.msmode);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.sys_ref != 0xff)
        {
            if(((conf_content.slot_p.sys_ref > 0x29) && (conf_content.slot_p.sys_ref < 0x39))||conf_content.slot_p.sys_ref == 0x46)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-SYS-REF:%s::INP-%c;", ctag, conf_content.slot_p.sys_ref);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.priority[0] != 0xff)
        {
            if(strlen(conf_content.slot_p.priority) == 8)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PRIO-INP:%s::%s;", ctag, conf_content.slot_p.priority);
                sendtodown(sendbuf, 'p');
            }

        }
        if(conf_content.slot_p.mask[0] != 0xff)
        {
            if(strlen(conf_content.slot_p.mask) == 8)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-MASK-INP:%s::%s;", ctag, conf_content.slot_p.mask);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.dely[0][0] != 0xff)
        {
            if(strlen(conf_content.slot_p.dely[0]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::1,%s;", ctag, conf_content.slot_p.dely[0]);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.dely[1][0] != 0xff)
        {
            if(strlen(conf_content.slot_p.dely[1]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::2,%s;", ctag, conf_content.slot_p.dely[1]);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.dely[2][0] != 0xff)
        {
            if(strlen(conf_content.slot_p.dely[2]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::3,%s;", ctag, conf_content.slot_p.dely[2]);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.dely[3][0] != 0xff)
        {
            if(strlen(conf_content.slot_p.dely[3]) < 9)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-DELY:%s::4,%s;", ctag, conf_content.slot_p.dely[3]);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.tzo[0] != 0xff)
        {
            if(strlen(conf_content.slot_p.tzo) == 3)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-TZO:%s::%s;", ctag, conf_content.slot_p.tzo);
                sendtodown(sendbuf, 'p');
				printf("16 set tzo %s\n",conf_content.slot_p.tzo);
            }
        }
        if(conf_content.slot_p.leaptag != 0xff)
        {
            if((conf_content.slot_p.leaptag > 0x29) && (conf_content.slot_p.leaptag < 0x34))
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-LEAP:%s::%c;", ctag, conf_content.slot_p.leaptag);
                sendtodown(sendbuf, 'p');
            }
        }
        if(conf_content.slot_p.leapmask[0] != 0xff)
        {
            if(strlen(conf_content.slot_p.leapmask) == 5)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-LMAK-INP:%s::%s;", ctag, conf_content.slot_p.leapmask);
                sendtodown(sendbuf, 'p');
            }
        }
    }
    /*gbtp1*/
    else if(slot == GBTP1_SLOT)
    {
        if(slot_type[num] == 'Y')
        {
            return;
        }
        if(conf_content.slot_q.mode[0] != 0xff)
        {
            if( ('L' == slot_type[16]) ||
                    ('e' == slot_type[16]) ||
                    'l' == slot_type[16] ||
                    'g' == slot_type[16]
              )
            {
                len = strlen(conf_content.slot_q.mode);
                if( (2 == len) || (3 == len) )
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-MODE:%s::%s;", ctag, conf_content.slot_q.mode);
                    sendtodown(sendbuf, 'q');
                }
            }
            else if( 'P' == slot_type[16] )
            {
                if( 0 == strncmp1(conf_content.slot_q.mode, "GPS", 3) )
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-MODE:%s::%s;", ctag, conf_content.slot_q.mode);
                    sendtodown(sendbuf, 'q');
                }
            }
			else if('v' == slot_type[16] || 'x' == slot_type[16] )
			{
			    len = strlen(conf_content.slot_q.mode);
				if((2 == len)||(3 == len))
				{
				  memset(sendbuf, '\0', SENDBUFSIZE);
                  sprintf((char *)sendbuf, "SET-GBTP-MODE:%s::%s;", ctag, conf_content.slot_q.mode);
                  sendtodown(sendbuf, 'q');
				}
			}
        }/*
			if(conf_content.slot_q.pos[0]!=0xff)
				{
					if(strlen(conf_content.slot_q.pos)>35)
						{
							memset(sendbuf,'\0',SENDBUFSIZE);
							sprintf((char *)sendbuf,"SET-POS:%s::%s;",ctag,conf_content.slot_q.pos);
							sendtodown(sendbuf,'q');
						}
				}*/
        if('v' == slot_type[16])//SET-GBTP-SATEN
		{
            if((conf_content.slot_q.mask[0] == '0') ||(conf_content.slot_q.mask[0] == '1')) 
            {
                    memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-GBTP-SATEN:%s::%s;", ctag, conf_content.slot_q.mask);
	                sendtodown(sendbuf, 'q');
               
            }
			
		}
		else if('x' == slot_type[16])
		{
		    if((conf_content.slot_q.mask[0] == '0') ||(conf_content.slot_q.mask[0] == '1')) 
            {
                    memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-GBTP-SATEN:%s::%s;", ctag, conf_content.slot_q.mask);
	                sendtodown(sendbuf, 'q');
               
            }

			if((strlen(conf_content3.gbtp_delay[0]) <=6) && 
				(atoi(conf_content3.gbtp_delay[0]) <= 32767) &&
				(atoi(conf_content3.gbtp_delay[0]) >= -32768))
			{
					memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-GBTP-DELAY:%s::%s;", ctag, conf_content3.gbtp_delay[0]);
	                sendtodown(sendbuf, 'q');
			}

			if((strlen(conf_content3.gbtp_pps_delay[0]) <=10) && 
				(atoi(conf_content3.gbtp_pps_delay[0]) <= 200000000) &&
				(atoi(conf_content3.gbtp_pps_delay[0]) >= -200000000))
			{
					memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-PPS-DELAY:%s::%s;", ctag, conf_content3.gbtp_pps_delay[0]);
	                sendtodown(sendbuf, 'q');
			}
		}
		else
		{
	        if(conf_content.slot_q.mask[0] != 0xff)
	        {
	            if(strlen(conf_content.slot_q.mask) == 2)
	            {
	                memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-MASK:%s::%s;", ctag, conf_content.slot_q.mask);
	                sendtodown(sendbuf, 'q');
	            }
	        }
		}
    }
    /*gbtp2*/
    else if(slot == GBTP2_SLOT)
    {
        if(slot_type[num] == 'Y')
        {
            return;
        }
        if(conf_content.slot_r.mode[0] != 0xff)
        {
            if( ('L' == slot_type[17]) ||
                    ('e' == slot_type[17]) ||
                    'l' == slot_type[17] ||
                    'g' == slot_type[17]
              )
            {
                len = strlen(conf_content.slot_r.mode);
                if( (2 == len) || (3 == len) )
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-MODE:%s::%s;", ctag, conf_content.slot_r.mode);
                    sendtodown(sendbuf, 'r');
                }
            }
            else if( 'P' == slot_type[17])
            {
                if( 0 == strncmp1(conf_content.slot_r.mode, "GPS", 3) )
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-MODE:%s::%s;", ctag, conf_content.slot_r.mode);
                    sendtodown(sendbuf, 'r');
                }
            }
			else if('v' == slot_type[17] || 'x' == slot_type[17])
			{
			    len = strlen(conf_content.slot_r.mode);
				printf("enter init pan 18\r\n");
				if((2 == len)||(3 == len))
				{
				  memset(sendbuf, '\0', SENDBUFSIZE);
                  sprintf((char *)sendbuf, "SET-GBTP-MODE:%s::%s;", ctag, conf_content.slot_r.mode);
                  sendtodown(sendbuf, 'r');
				  printf(sendbuf);
				  
				}
			}
        }/*
			if(conf_content.slot_r.pos[0]!=0xff)
				{
					len=strlen(conf_content.slot_r.pos);
					if((len<35)&&(len>25))
						{
							memset(sendbuf,'\0',SENDBUFSIZE);
							sprintf((char *)sendbuf,"SET-POS:%s::%s;",ctag,conf_content.slot_r.pos);
							sendtodown(sendbuf,'r');
						}
				}*/
		if('v' == slot_type[17] )//SET-GBTP-SATEN
		{
            if((conf_content.slot_r.mask[0] == '0') ||(conf_content.slot_r.mask[0] == '1')) 
            {
                    memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-GBTP-SATEN:%s::%s;", ctag, conf_content.slot_r.mask);
	                sendtodown(sendbuf, 'r');
               
            }
		}
		else if('x' == slot_type[17])
		{
		    if((conf_content.slot_r.mask[0] == '0') ||(conf_content.slot_r.mask[0] == '1')) 
            {
                    memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-GBTP-SATEN:%s::%s;", ctag, conf_content.slot_r.mask);
	                sendtodown(sendbuf, 'r');
               
            }

			if((strlen(conf_content3.gbtp_delay[1]) <=6) && 
				(atoi(conf_content3.gbtp_delay[1]) <= 32767) &&
				(atoi(conf_content3.gbtp_delay[1]) >= -32768))
			{
					memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-GBTP-DELAY:%s::%s;", ctag, conf_content3.gbtp_delay[1]);
	                sendtodown(sendbuf, 'r');
			}

			if((strlen(conf_content3.gbtp_pps_delay[1]) <=10) && 
				(atoi(conf_content3.gbtp_pps_delay[1]) <= 200000000) &&
				(atoi(conf_content3.gbtp_pps_delay[1]) >= -200000000))
			{
					memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-PPS-DELAY:%s::%s;", ctag, conf_content3.gbtp_pps_delay[1]);
	                sendtodown(sendbuf, 'r');
			}
		}
		else
		{
	        if(conf_content.slot_r.mask[0] != 0xff)
	        {
	            if(strlen(conf_content.slot_r.mask) == 2)
	            {
	                memset(sendbuf, '\0', SENDBUFSIZE);
	                sprintf((char *)sendbuf, "SET-MASK:%s::%s;", ctag, conf_content.slot_r.mask);
	                sendtodown(sendbuf, 'r');
	            }
	        }
		}
    }
    /*out*/
    else if((num >= 0) && (num < 14))
    {
        if( (slot_type[num] == 'J') ||
                (slot_type[num] == 'a') ||
                (slot_type[num] == 'b') ||
                (slot_type[num] == 'c') ||
                (slot_type[num] == 'h') ||
                (slot_type[num] == 'i'))
        {
        
            /*if(conf_content.slot[num].out_lev[0]!=0xff)
            	{
            		if(strlen(conf_content.slot[num].out_lev)==2)
            			{
            				memset(sendbuf,'\0',SENDBUFSIZE);
            				sprintf((char *)sendbuf,"SET-OUT-LEV:%s::%s;",ctag,conf_content.slot[num].out_lev);
            				sendtodown(sendbuf,(char)('a'+num));
            			}
            	}*/
            //
            //OUT32S ADD
            //
            //printf("###init_pan:%d\n",num);
            
            get_out_lev(num, 1);
            //printf("test\n");
             
            if(conf_content.slot[num].out_typ[0] != 0xff)
            {
                if(strlen(conf_content.slot[num].out_typ) == 16)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-OUT-TYP:%s::%s;", ctag, conf_content.slot[num].out_typ);
                    sendtodown(sendbuf, (char)('a' + num));
					//
					//OUT32S ADD.
					//
					//printf("out32s:%s\n",sendbuf);
                }
            }

            if(conf_content.slot[num].out_mod != 0xff)
            {
				if( (conf_content.slot[num].out_mod != '0') && (conf_content.slot[num].out_mod != '1'))
					conf_content.slot[num].out_mod = '0';
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-OUT-MOD:%s::%c;", ctag, conf_content.slot[num].out_mod);
                sendtodown(sendbuf, (char)('a' + num));
				//
				//OUT32S ADD
				//
				//printf("out32s:%s\n",sendbuf);
            }
        }
		//
		//! tp16 initialtion.
		//
        else if(slot_type[num] == 'I')
        {
            
            if(conf_content.slot[num].tp16_en[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].tp16_en) == 16)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-TP16-EN:%s::%s;", ctag, conf_content.slot[num].tp16_en);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }


            j = atoi(conf_content.slot_u.leap_num);
            if(strncmp1(conf_content.slot_u.leap_num, "00", 2) == 0)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-TP16-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }
            else if((j < 1) || (j > 99))
            {}
            else
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-TP16-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
				printf("set tp16 leap %d\n",j);
            }

			//2015.9.9 初始化TP16协议
			if( '0' == conf_content3.mcp_protocol ||
				'1' == conf_content3.mcp_protocol ||
				'2' == conf_content3.mcp_protocol   )
			{
				printf("###init protocol ptp --%d\n",num);
				memset(sendbuf, '\0', SENDBUFSIZE);
				sprintf((char *)sendbuf, "SET-PROTOCOL:%s::%c;", ctag, conf_content3.mcp_protocol);
                sendtodown(sendbuf, (char)('a' + num));
			}
			else
			{
				;
			}
			
        }
		//
		//! ppx
		//
        else if(slot_type[num] == 'U')
        {
            if(conf_content.slot[num].ppx_mod[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].ppx_mod) == 16)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PPX-MOD:%s::%s;", ctag, conf_content.slot[num].ppx_mod);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }
        }
		//
		//!TOD16 
		//
        else if(slot_type[num] == 'V')
        {
            if(conf_content.slot[num].tod_en[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].tod_en) == 16)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-TOD-EN:%s::%s;", ctag, conf_content.slot[num].tod_en);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }

            flag = 0;
            memset(sendbuf, '\0', SENDBUFSIZE);
            if(conf_content.slot[num].tod_br >= '0' && conf_content.slot[num].tod_br <= '7')
            {
                sprintf((char *)sendbuf, "SET-TOD-PRO:%s::%c", ctag, conf_content.slot[num].tod_br);
            }
            else
            {
                sprintf((char *)sendbuf, "SET-TOD-PRO:%s::", ctag);
            }
            if(conf_content.slot[num].tod_tzo >= 0x40 && conf_content.slot[num].tod_tzo <= 0x57)
            {
                sprintf((char *)sendbuf, "%s,%c", sendbuf, conf_content.slot[num].tod_tzo);
            }
            else
            {
                sprintf((char *)sendbuf, "%s,", sendbuf);
            }
            j = atoi(conf_content.slot_u.leap_num);
            if((j < 1) || (j > 99))
            {
                if(flag == 0)
                {
                    sprintf((char *)sendbuf, "%s,;", sendbuf);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }
            else
            {
                sprintf((char *)sendbuf, "%s,%s;", sendbuf, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }

        }
		//
		//!IRIGB 
		//
        else if(slot_type[num] == 'W')
        {
            if(conf_content.slot[num].igb_en[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].igb_en) == 4)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-IGB-EN:%s::%s;", ctag, conf_content.slot[num].igb_en);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }
            if(conf_content.slot[num].igb_rat[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].igb_rat) == 4)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-IGB-RAT:%s::%s;", ctag, conf_content.slot[num].igb_rat);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }
            if(conf_content.slot[num].igb_max[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].igb_max) == 4)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-IGB-MAX:%s::%s;", ctag, conf_content.slot[num].igb_max);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }

            j = atoi(conf_content.slot_u.leap_num);
            if((j < 1) || (j > 99))
            {
                ;
            }
            else
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-IGB-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }

            if(conf_content3.irigb_tzone[num] != 0)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-IGB-TZONE:%s::%c;", ctag, conf_content3.irigb_tzone[num]);
                sendtodown(sendbuf, (char)('a' + num));
            }

        }
		//
		//REFTF 
		//
        else if(slot_type[num] == 'S')
        {
            switch(conf_content.slot_u.out_ssm_oth)
            {
            case 0x02:
                out_ssm_threshold = 'a';
                break;
            case 0x04:
                out_ssm_threshold = 'b';
                break;
            case 0x08:
                out_ssm_threshold = 'c';
                break;
            case 0x0B:
                out_ssm_threshold = 'd';
                break;
            case 0x0F:
                out_ssm_threshold = 'e';
                break;
            case 0x00:
                out_ssm_threshold = 'f';
                break;
            }

            memset(sendbuf, '\0', SENDBUFSIZE);
            sprintf((char *)sendbuf, "SET-SSM-OTH:%s::%c;", ctag, out_ssm_threshold);
            sendtodown(sendbuf, (char)('a' + num));

            len1 = len2 = 0;
            if(conf_content.slot[num].ref_prio1[0] != 0xff)
            {
                len1 = strlen(conf_content.slot[num].ref_prio1);
            }
            if(conf_content.slot[num].ref_prio2[0] != 0xff)
            {
                len2 = strlen(conf_content.slot[num].ref_prio2);
            }
            if(len1 == 8)
            {
                if(len2 == 8)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-REF-PRIO:%s::%s,%s;", ctag, conf_content.slot[num].ref_prio1,
                            conf_content.slot[num].ref_prio2);
                    sendtodown(sendbuf, (char)('a' + num));
                }
                else
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-REF-PRIO:%s::%s,;", ctag, conf_content.slot[num].ref_prio1);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }
            else
            {
                if(len2 == 8)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-REF-PRIO:%s::,%s;", ctag, conf_content.slot[num].ref_prio2);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }

            len1 = len2 = 0;
            if(conf_content.slot[num].ref_mod1[0] != 0xff)
            {
                len1 = strlen(conf_content.slot[num].ref_mod1);
            }
            if(conf_content.slot[num].ref_mod2[0] != 0xff)
            {
                len2 = strlen(conf_content.slot[num].ref_mod2);
            }
            if(len1 == 3)
            {
                if(len2 == 3)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-REF-MOD:%s::%s,%s;", ctag, conf_content.slot[num].ref_mod1,
                            conf_content.slot[num].ref_mod1);
                    sendtodown(sendbuf, (char)('a' + num));
                }
                else
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-REF-MOD:%s::%s,,;", ctag, conf_content.slot[num].ref_mod1);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }
            else
            {
                if(len2 == 3)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-REF-MOD:%s::,,%s;", ctag, conf_content.slot[num].ref_mod2);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }

        }
		//
		//!RS422 SF16
		//
        else if(slot_type[num] == 'T' || slot_type[num] == 'X')
        {
            if(conf_content.slot[num].rs_en[0] != 0xff)
            {

                if(strlen(conf_content.slot[num].rs_en) == 4)
                {
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-RS-EN:%s::%s;", ctag, conf_content.slot[num].rs_en);
                    sendtodown(sendbuf, (char)('a' + num));
                }
            }


            j = atoi(conf_content.slot_u.leap_num);
            if(((j < 0) || (j > 99)) && (strncmp1(conf_content.slot_u.leap_num, "00", 2) != 0))
            {
                if(conf_content.slot[num].rs_tzo[0] != 0xff)
                {
                    if(strlen(conf_content.slot[num].rs_tzo) == 4)
                    {
                        memset(sendbuf, '\0', SENDBUFSIZE);
                        sprintf((char *)sendbuf, "SET-RS-TZO:%s::%s,;", ctag, conf_content.slot[num].rs_tzo);
                        sendtodown(sendbuf, (char)('a' + num));
                    }
                }
            }
            else
            {

                if(conf_content.slot[num].rs_tzo[0] != 0xff)
                {
                    if(strlen(conf_content.slot[num].rs_tzo) == 4)
                    {

                        memset(sendbuf, '\0', SENDBUFSIZE);
                        sprintf((char *)sendbuf, "SET-RS-TZO:%s::%s,%s;", ctag,
                                conf_content.slot[num].rs_tzo, conf_content.slot_u.leap_num);
                        sendtodown(sendbuf, (char)('a' + num));
                        return ;
                    }
                }
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-RS-TZO:%s::,%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }
        }
		//
		//!PTP OUT 
		//
        else if(((slot_type[num] > 0x40) && (slot_type[num] < 0x49)) || ((slot_type[num] > 109) && (slot_type[num] < 114)))
        {
            j = atoi(conf_content.slot_u.leap_num);
            if(strncmp1(conf_content.slot_u.leap_num, "00", 2) == 0)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PTP-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }
            else if((j < 1) || (j > 99))
            {}
            else
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PTP-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }

            //						if((slot_type[num] == 110) || (slot_type[num] == 111)) out_port = 1;
            //						else if((slot_type[num] == 112) || (slot_type[num] == 113)) out_port = 2;
            //						else out_port = 4;
            
			//2015.9.9  初始化ptp的协议
			if( '0' == conf_content3.mcp_protocol ||
				'1' == conf_content3.mcp_protocol ||
				'2' == conf_content3.mcp_protocol   )
			{
				printf("###init protocol ptp --%d\n",num);
				memset(sendbuf, '\0', SENDBUFSIZE);
				sprintf((char *)sendbuf, "SET-PROTOCOL:%s::%c;", ctag, conf_content3.mcp_protocol);
                sendtodown(sendbuf, (char)('a' + num));
				
			}
			else
			{
				;
			}
			
            for(j = 0; j < out_port; j++)
            {
                if(conf_content.slot[num].ptp_type == slot_type[num])
                {
                    
                   
                    /*memset(sendbuf, 0, SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PTP-MTC:%c,%d,%s;",
                            conf_content.slot[num].ptp_type, j + 1, conf_content3.ptp_mtc1_ip[num][j]);
					printf("###run sendbuf mtc --%s\n",sendbuf);
                    sendtodown(sendbuf, (char)('a' + num));
                    usleep(200000);*/
					//conf_content3.ptp_mtc1_ip[num][port]

                    /*MODE*/
                    flag = 0;
                    if((conf_content.slot[num].ptp_en[j] != '0') && (conf_content.slot[num].ptp_en[j] != '1'))
                    {
                        memcpy(tmp1, ",", 1);
						tmp1[1] = '\0';
                    }
                    else
                    {
                        sprintf((char *)tmp1, "%c,", conf_content.slot[num].ptp_en[j]);
                        tmp1[2] = '\0';
                        flag = 1;
                    }
                    if((conf_content.slot[num].ptp_delaytype[j] != '0') && (conf_content.slot[num].ptp_delaytype[j] != '1'))
                    {
                        memcpy(tmp2, ",", 1);
						tmp2[1] = '\0';
                    }
                    else
                    {
                        sprintf((char *)tmp2, "%c,", conf_content.slot[num].ptp_delaytype[j]);
                        tmp2[2] = '\0';
                        flag = 1;
                    }
                    if((conf_content.slot[num].ptp_multicast[j] != '0') && (conf_content.slot[num].ptp_multicast[j] != '1'))
                    {
                        memcpy(tmp3, ",", 1);
						tmp3[1] = '\0';
                    }
                    else
                    {
                        sprintf((char *)tmp3, "%c,", conf_content.slot[num].ptp_multicast[j]);
                        tmp3[2] = '\0';
                        flag = 1;
                    }
                    if((conf_content.slot[num].ptp_enp[j] != '0') && (conf_content.slot[num].ptp_enp[j] != '1'))
                    {
                        memcpy(tmp4, ",", 1);
						tmp4[1] = '\0';
                    }
                    else
                    {
                        sprintf((char *)tmp4, "%c,", conf_content.slot[num].ptp_enp[j]);
                        tmp4[2] = '\0';
                        flag = 1;
                    }
                    if((conf_content.slot[num].ptp_step[j] != '0') && (conf_content.slot[num].ptp_step[j] != '1'))
                    {
                        memcpy(tmp5, ",", 1);
						tmp5[1] = '\0';
                    }
                    else
                    {
                        sprintf((char *)tmp5, "%c,", conf_content.slot[num].ptp_step[j]);
                        tmp5[2] = '\0';
                        flag = 1;
                    }
                    if(conf_content.slot[num].ptp_sync[j][0] != 0xff)
                    {
                        if(strlen(conf_content.slot[num].ptp_sync[j]) == 2)
                        {
                            flag = 1;
                        }
                        else
                        {
                            memset(conf_content.slot[num].ptp_sync[j], 0, 3);
                        }
                    }
                    else
                    {
                        memset(conf_content.slot[num].ptp_sync[j], 0, 3);
                    }
                    if(conf_content.slot[num].ptp_announce[j][0] != 0xff)
                    {
                        if(strlen(conf_content.slot[num].ptp_announce[j]) == 2)
                        {
                            flag = 1;
                        }
                        else
                        {
                            memset(conf_content.slot[num].ptp_announce[j], 0, 3);
                        }
                    }
                    else
                    {
                        memset(conf_content.slot[num].ptp_announce[j], 0, 3);
                    }
                    if(conf_content.slot[num].ptp_delayreq[j][0] != 0xff)
                    {
                        if(strlen(conf_content.slot[num].ptp_delayreq[j]) == 2)
                        {
                            flag = 1;
                        }
                        else
                        {
                            memset(conf_content.slot[num].ptp_delayreq[j], 0, 3);
                        }
                    }
                    else
                    {
                        memset(conf_content.slot[num].ptp_delayreq[j], 0, 3);
                    }

                    if(conf_content.slot[num].ptp_pdelayreq[j][0] != 0xff)
                    {
                        if(strlen(conf_content.slot[num].ptp_pdelayreq[j]) == 2)
                        {
                            flag = 1;
                        }
                        else
                        {
                            memset(conf_content.slot[num].ptp_pdelayreq[j], 0, 3);
                        }
                    }
                    else
                    {
                        memset(conf_content.slot[num].ptp_pdelayreq[j], 0, 3);
                    }

                    if(conf_content.slot[num].ptp_delaycom[j][0] != 0xff)
                    {
                        len = strlen(conf_content.slot[num].ptp_delaycom[j]);
                        if((len > 0) && (len < 9))
                        {
                            flag = 1;
                        }
                        else
                        {
                            memset(conf_content.slot[num].ptp_delaycom[j], 0, 9);
                        }
                    }
                    else
                    {
                        memset(conf_content.slot[num].ptp_delaycom[j], 0, 9);
                    }
                    if((conf_content.slot[num].ptp_linkmode[j] != '0') && (conf_content.slot[num].ptp_linkmode[j] != '1'))
                    {
                        memcpy(tmp6, ",", 1);
						tmp6[1] = '\0';
						tmp6[2] = '\0';	
                    }
                    else
                    {
                        sprintf((char *)tmp6, "%c,", conf_content.slot[num].ptp_linkmode[j]);
                        tmp6[2] = '\0';
                        flag = 1;
                    }
                    if((conf_content.slot[num].ptp_out[j] != '0') && (conf_content.slot[num].ptp_out[j] != '1'))
                    {
                        //memcpy(tmp7, ";", 1);
                        memcpy(tmp7, ",", 1); //2014.12.3
                        tmp7[1] = '\0';
						tmp7[2] = '\0';	
                    }
                    else
                    {
                        sprintf((char *)tmp7, "%c,", conf_content.slot[num].ptp_out[j]); //2014.12.3
                        tmp7[2] = '\0';
                        flag = 1;
                    }
                    //2014.12.3
                    if(conf_content3.ptp_prio1[num][j][0] != 'A')
                    {
                        memset(&conf_content3.ptp_prio1[num][j], 0, 5);
                        memcpy(&conf_content3.ptp_prio1[num][j], "A128", 4);
                    }
                    else
                    {
                        ;
                    }

                    if(conf_content3.ptp_prio2[num][j][0] != 'B')
                    {
                        memset(&conf_content3.ptp_prio2[num][j], 0, 5);
                        memcpy(&conf_content3.ptp_prio2[num][j], "B128", 4);
                    }
                    else
                    {
                        ;
                    }
                    if(flag == 1)
                    {
                        //2014.12.3
                        //ptp配置一次低于8个选项最为安全
                        //为保险起见,将命令分两次发送
						memset(sendbuf, '\0', SENDBUFSIZE);
                        //SET-PTP-MOD:%c,%d, , , , , ,  ,  ,  ,,,,,%s;
                        //<num>,<eable>,<delaytype>,<multicast>,<enp>,<step>,<sync>,<announce>,<delayreq>,<pdelayreq>,<delaycom>,<linkmode>，<outtype>,[< prio >]
                        sprintf((char *)sendbuf, "SET-PTP-MOD:%c,%d,,,,,,,,,,,,,%s;",
                                conf_content.slot[num].ptp_type,
                                (j + 1),
                                conf_content3.ptp_prio2[num][j]
                               );     
                        sendtodown(sendbuf, (char)('a' + num));
                        usleep(200000);
						
                        memset(sendbuf, '\0', SENDBUFSIZE);
                        //SET-PTP-MOD:%c,%d, , , , , ,  ,  ,  ,,,,,%s;
                        //<num>,<eable>,<delaytype>,<multicast>,<enp>,<step>,<sync>,<announce>,<delayreq>,<pdelayreq>,<delaycom>,<linkmode>，<outtype>,[< prio >]
                        sprintf((char *)sendbuf, "SET-PTP-MOD:%c,%d,%s%s%s%s%s%s,%s,%s,%s,%s,%s%s%s;",
                                conf_content.slot[num].ptp_type,
                                (j + 1),
                                tmp1, tmp2, tmp3,tmp4, tmp5,
                                conf_content.slot[num].ptp_sync[j],
                                conf_content.slot[num].ptp_announce[j],
                                conf_content.slot[num].ptp_delayreq[j],
								conf_content.slot[num].ptp_pdelayreq[j],
								conf_content.slot[num].ptp_delaycom[j],
								tmp6, tmp7,
                                conf_content3.ptp_prio1[num][j]
                               );/*%s%s%s,%s  tmp4, tmp5,
                                conf_content.slot[num].ptp_sync[j],
                                conf_content.slot[num].ptp_announce[j],*/
                        //printf("ptp init send1 %s\n",sendbuf);      
                        sendtodown(sendbuf, (char)('a' + num));
						delay_s(7,0);

						

                        /*memset(sendbuf, 0, SENDBUFSIZE);
                        sprintf((char *)sendbuf, "SET-PTP-MOD:%c,%d,,,,,,,,%s,%s,%s,,%s;",
                                conf_content.slot[num].ptp_type,
                                (j + 1),
                                conf_content.slot[num].ptp_delayreq[j],
                                conf_content.slot[num].ptp_pdelayreq[j],
                                conf_content.slot[num].ptp_delaycom[j],
                                
                                conf_content3.ptp_prio2[num][j]
                               );
                        //printf("ptp init send3 %s\n",sendbuf);
                        sendtodown(sendbuf, (char)('a' + num));
                        usleep(200000);

						memset(sendbuf, 0, SENDBUFSIZE);
                        sprintf((char *)sendbuf, "SET-PTP-MOD:%c,%d,,,,,,,,,,,%s%s;",
                                conf_content.slot[num].ptp_type,
                                (j + 1),
                                tmp6, tmp7
                               );
                        //printf("ptp init send4 %s\n",sendbuf);
                        sendtodown(sendbuf, (char)('a' + num));
                        usleep(200000);

						memset(sendbuf, '\0', SENDBUFSIZE);
                        //SET-PTP-MOD:%c,%d, , , , , ,  ,  ,  ,,,,,%s;
                        sprintf((char *)sendbuf, "SET-PTP-MOD:%c,%d,,,,%s%s%s,%s,,,,,,;",
                                conf_content.slot[num].ptp_type,
                                (j + 1),
                                 tmp4, tmp5,
                                conf_content.slot[num].ptp_sync[j],
                                conf_content.slot[num].ptp_announce[j]
                                
                               );
						//printf("ptp init send2 %s\n",sendbuf);
                        sendtodown(sendbuf, (char)('a' + num));
                        delay_s(5,0);*/

                    }
					memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PTP-NET:%c,%d,%s,%s,%s,%s,%s,%s;",
                            conf_content.slot[num].ptp_type, (j + 1),
                            conf_content.slot[num].ptp_ip[j],
                            conf_content.slot[num].ptp_mac[j],
                            conf_content.slot[num].ptp_gate[j],
                            conf_content.slot[num].ptp_mask[j],
                            conf_content.slot[num].ptp_dns1[j],
                            conf_content.slot[num].ptp_dns2[j]);
					//printf("###run sendbuf net --%s\n",sendbuf);
                    sendtodown(sendbuf, (char)('a' + num));
                    delay_s(3,0);

                }
            }
        }
		else if('j' == slot_type[num])
		{
		    //
		    //!闰秒值下发
		    //
		    j = atoi(conf_content.slot_u.leap_num);
            if(strncmp1(conf_content.slot_u.leap_num, "00", 2) == 0)
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PTP-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }
            else if((j < 1) || (j > 99))
            {
               ;
            }
            else
            {
                memset(sendbuf, '\0', SENDBUFSIZE);
                sprintf((char *)sendbuf, "SET-PTP-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
                sendtodown(sendbuf, (char)('a' + num));
            }
			//
			//!协议下发(联通/移动/工信)
			//
			if( '0' == conf_content3.mcp_protocol ||
				'1' == conf_content3.mcp_protocol ||
				'2' == conf_content3.mcp_protocol   )
			{
				printf("###init protocol ptp --%d\n",num);
				memset(sendbuf, '\0', SENDBUFSIZE);
				sprintf((char *)sendbuf, "SET-PROTOCOL:%s::%c;", ctag, conf_content3.mcp_protocol);
                sendtodown(sendbuf, (char)('a' + num));
				printf("SET-PROTOCOL %c\n",conf_content3.mcp_protocol);
			}
			else
			{
				;
			}
			//
			//!设置网络参数，工作模式，单播对端IP地址，光模块配置；
			//
			for(j = 0; j < out_port; j++)
            {
			    if(conf_content.slot[num].ptp_type == slot_type[num]) 
			    {
			        //
			        //!Initial the net parameters。
			        //
			        memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PGE4V-NET:%c,%d,,%s,%s,%s;",
                            conf_content.slot[num].ptp_type, (j + 1),
                           // conf_content.slot[num].ptp_mac[j],
                            conf_content.slot[num].ptp_ip[j],
                            conf_content.slot[num].ptp_mask[j],
                            conf_content.slot[num].ptp_gate[j]
                            );
                    sendtodown(sendbuf, (char)('a' + num));
                    usleep(200000);
                    //
                    //!Set the unicast dest parameters。
                    //
					memset(sendbuf, 0, SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PGE4V-MTC:%c,%d,%s;",
                            conf_content.slot[num].ptp_type, j + 1, conf_content3.ptp_mtc_ip[num][j]);
                    sendtodown(sendbuf, (char)('a' + num));
                    usleep(200000);

					//
					//!Check the parameters corrected and Set Mode parameters。
					//
					flag = 0;
					if((conf_content.slot[num].ptp_en[j] != '0') && (conf_content.slot[num].ptp_en[j] != '1'))
                        conf_content.slot[num].ptp_en[j] = '\0';
					else
						flag = 1;
					
					if((conf_content3.ptp_esmcen[num][j] != '0') && (conf_content3.ptp_esmcen[num][j] != '1'))
					    conf_content3.ptp_esmcen[num][j] = '\0';
				    else
						flag = 1;
					
					if((conf_content.slot[num].ptp_delaytype[j] != '0') && (conf_content.slot[num].ptp_delaytype[j] != '1'))
						conf_content.slot[num].ptp_delaytype[j] = '\0';
					else 
						flag = 1;

					if((conf_content.slot[num].ptp_multicast[j] != '0') && (conf_content.slot[num].ptp_multicast[j] != '1'))
						conf_content.slot[num].ptp_multicast[j] = '\0';
					else
						flag = 1;

					if((conf_content.slot[num].ptp_enp[j] != '0') && (conf_content.slot[num].ptp_enp[j] != '1') )
						conf_content.slot[num].ptp_enp[j] = '\0';
					else
						flag = 1;

					if((conf_content.slot[num].ptp_step[j] != '0') && (conf_content.slot[num].ptp_step[j] != '1'))
						conf_content.slot[num].ptp_step[j] = '\0';
					else
						flag = 1; 

					//
                    //!Set the ptp mode。
                    //

					if(flag == 1)
					{
						memset(sendbuf, 0, SENDBUFSIZE);
	                    sprintf((char *)sendbuf, "SET-PGE4V-MOD:%c,%d,%c,%c,%c,%c,%c,%c;",
	                            conf_content.slot[num].ptp_type, j + 1, conf_content.slot[num].ptp_en[j],
	                                                                    conf_content3.ptp_esmcen[num][j],
	                                                                    conf_content.slot[num].ptp_delaytype[j],
	                                                                    conf_content.slot[num].ptp_multicast[j],
	                                                                    conf_content.slot[num].ptp_enp[j],
	                                                                    conf_content.slot[num].ptp_step[j]
	                                                                    );
	                    sendtodown(sendbuf, (char)('a' + num));
	                    usleep(200000);
					}
					//
					//!Set the ptp par。
					//
					flag = 0;
					if((strlen(conf_content3.ptp_dom[num][j]) > 0) && (strlen(conf_content3.ptp_dom[num][j]) <= MAX_DOM_LEN))
				    	flag = 1;
					else
						memset(conf_content3.ptp_dom[num][j],'\0', 4);

					if(strlen(conf_content.slot[num].ptp_sync[j]) == FRAME_FRE_LEN)
						flag = 1;
					else
						memset(conf_content.slot[num].ptp_sync[j],'\0',3);

					if(strlen(conf_content.slot[num].ptp_announce[j]) == FRAME_FRE_LEN)
						flag = 1;
					else
						memset(conf_content.slot[num].ptp_announce[j],'\0',3);

					if((strlen(conf_content.slot[num].ptp_delaycom[j]) > 0) &&(strlen(conf_content.slot[num].ptp_delaycom[j]) <= DELAY_COM_LEN))
						flag = 1;
					else
						memset(conf_content.slot[num].ptp_delaycom[j],'\0',9);
					

					if(conf_content3.ptp_prio1[num][j][0] != 'A')
                    {
                        memset(&conf_content3.ptp_prio1[num][j], 0, 5);
                        memcpy(&conf_content3.ptp_prio1[num][j], "A128", 4);
                    }
                    else
                    {
                        ;
                    }

                    if(conf_content3.ptp_prio2[num][j][0] != 'B')
                    {
                        memset(&conf_content3.ptp_prio2[num][j], 0, 5);
                        memcpy(&conf_content3.ptp_prio2[num][j], "B128", 4);
                    }
                    else
                    {
                        ;
                    }
					if(flag == 1)
					{
					    memset(sendbuf, 0, SENDBUFSIZE);
	                    sprintf((char *)sendbuf, "SET-PGE4V-PAR:%c,%d,%s,%s,%s,%s,%s;",
	                            conf_content.slot[num].ptp_type, j + 1, conf_content3.ptp_dom[num][j],
	                                                                    conf_content.slot[num].ptp_sync[j],
	                                                                    conf_content.slot[num].ptp_announce[j],
	                                                                    conf_content.slot[num].ptp_delaycom[j],
	                                                                    conf_content3.ptp_prio1[num][j]
	                                                                    );
	                    sendtodown(sendbuf, (char)('a' + num));
	                    usleep(200000);
						memset(sendbuf, 0, SENDBUFSIZE);
	                    sprintf((char *)sendbuf, "SET-PGE4V-PAR:%c,%d,,,,,%s;",
	                            conf_content.slot[num].ptp_type, j + 1, conf_content3.ptp_prio2[num][j]
	                                                                    );
	                    sendtodown(sendbuf, (char)('a' + num));
	                    usleep(200000);
					}
					//
					//!sfp set
					//
					flag = 0;
					if(strlen(conf_content3.ptp_sfp[num][j]) == 2)
						flag = 1;
					else
						memset(conf_content3.ptp_sfp[num][j],'\0',3);

					if((strlen(conf_content3.ptp_oplo[num][j]) > 0) && (strlen(conf_content3.ptp_oplo[num][j]) <= MAX_OPTHR_LEN))
						flag = 1;
					else
						memset(conf_content3.ptp_oplo[num][j],'\0',6);

					if((strlen(conf_content3.ptp_ophi[num][j]) > 0) && (strlen(conf_content3.ptp_ophi[num][j]) <= MAX_OPTHR_LEN))
						flag = 1;
					else
						memset(conf_content3.ptp_ophi[num][j],'\0',6);

					if(flag == 1)
					{
					     memset(sendbuf, 0, SENDBUFSIZE);
	                     sprintf((char *)sendbuf, "SET-PGE4V-SFP:%c,%d,%s,%s,%s;",
	                            conf_content.slot[num].ptp_type, j + 1, conf_content3.ptp_sfp[num][j],
	                                                                    conf_content3.ptp_oplo[num][j],
	                                                                    conf_content3.ptp_ophi[num][j]   
	                                                                    );
	                    sendtodown(sendbuf, (char)('a' + num));
	                    usleep(200000);
					}
						
			    }
			}
		}
		else if('s' == slot_type[num])
		{
		    if(conf_content.slot[num].ptp_type == slot_type[num])
			{
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PGEIN-NET:%c,%d,%s,%s,%s;",conf_content.slot[num].ptp_type, 1,
                                                                                                                conf_content.slot[num].ptp_ip[0],
                                                                                                                conf_content.slot[num].ptp_gate[0],
                                                                                                                conf_content.slot[num].ptp_mask[0]);
                    sendtodown(sendbuf, (char)('a' + num));
                    usleep(200000);
                    memset(sendbuf, '\0', SENDBUFSIZE);
                    sprintf((char *)sendbuf, "SET-PGEIN-MOD:%c,%d,%c,%c,%c,%c,%s,%s;",conf_content.slot[num].ptp_type,1,
                                                                                                                 conf_content.slot[num].ptp_delaytype[0],
                                                                                                                 conf_content.slot[num].ptp_multicast[0], 
                                                                                                                 conf_content.slot[num].ptp_enp[0],
                                                                                                                 conf_content.slot[num].ptp_step[0],
                                                                                                                 conf_content.slot[num].ptp_delayreq[0],
                                                                                                                 conf_content.slot[num].ptp_pdelayreq[0]);
					sendtodown(sendbuf, (char)('a' + num));
					
                               
                    
  			}  
		}
        else if( 'Z' == slot_type[num])
        {
            memset(sendbuf, '\0', SENDBUFSIZE);
            sprintf((char *)sendbuf, "SET-NTP-LEAP:%s::%s;", ctag, conf_content.slot_u.leap_num);
            sendtodown(sendbuf, (char)('a' + num));
        }
        else if( 'm' == slot_type[num])
        {
            init_sta1(num, sendbuf);
        }
    }
}




int read_config3(FILE *pf)
{
    if( pf == NULL )
    {
        return -1;
    }

    if(0 != fseek(pf, sizeof(file_content) + sizeof(struct extsave), SEEK_SET))
    {
        return 0;
    }

    memset(&conf_content3, 0, sizeof(file_content3));
    if(1 != fread((void *)&conf_content3, sizeof(file_content3), 1, pf))
    {
        return 0;
    }
    if('T' != conf_content3.flashMask)
    {
        memset(&conf_content3, 0, sizeof(file_content3));
		if(0 != fseek(pf, sizeof(file_content) + sizeof(struct extsave), SEEK_SET))
        {
           return 0;
        }
        fwrite((void *)&conf_content3, sizeof(file_content3), 1, pf);
		fflush(pf);
		sync();
        system("fw -ul -f /var/p300file -o 0x1f0000 /dev/rom1");
    }
    else
    {
    }
    return 1;

}

void Init_filesystem()
{
    FILE  *file_fd;
    int rc;
    //int i;
    //Uint16 value;
    rc = system("dd if=/dev/rom1 of=/var/p300file bs=65536 count=1 skip=31");
    if(rc != 0)
    {
        printf("dd failed\n");
        perror("system");
    }
	sync();
    if((file_fd = fopen(FILE_DEV, "rb+")) == NULL) /*打开文件失败*/
    {
        printf("only read Open FILE_DEV Error:\n");
        perror("fopen");

    }
    else
    {
        //printf("\nopen ok!\n");
        memset(&conf_content, '\0', sizeof(file_content));
        rc = fread((void *)&conf_content, sizeof(file_content), 1, file_fd);
        printf("p300-0 flashmask=%c\n", conf_content.flashMask);
        if('T' != conf_content.flashMask)
        {
            memset(&conf_content, '\0', sizeof(file_content));
			if(0 != fseek(file_fd, 0, SEEK_SET))
    		{
        		printf("only SEEK_SET FILE_DEV Error:\n");
                perror("SEEK_SET");
    		}
            fwrite((void *)&conf_content, sizeof(file_content), 1, file_fd);
			fflush(file_fd);
			sync();
            system("fw -ul -f /var/p300file -o 0x1f0000 /dev/rom1");
        }
        read_config3(file_fd);
        fclose(file_fd);
		sync();
#if 0
        if((file_fd = fopen(FILE_DEV, "wb+")) < 0)
        {
            printf("only write open  FILE_DEV Error:\n");
            perror("fopen");
        }


        printf("\nbefor fwrite\n");
        rc = fwrite((void *)&conf_content, sizeof(file_content), 1, file_fd);
        fclose(file_fd);
        printf("\nbefor fw\n");
        system("fw -ul -f /var/p210file -o 0x1f0000 /dev/rom1");

        //config_enet();
#endif
      if(conf_content3.mcp_protocol <'0' ||conf_content3.mcp_protocol >'2')

	    conf_content3.mcp_protocol = '1';
    }
}






void save_config()
{
    FILE  *file_fd;
    if((file_fd = fopen(FILE_DEV, "rb+")) == NULL)
    {
        printf("Open FILE_DEV Error:\n");

    }
    else
    {
        conf_content.flashMask = 'T';
        fwrite((void *)&conf_content, sizeof(file_content), 1, file_fd);


        conf_content3.flashMask = 'T';

        //2014.12.2
        if(0 != fseek(file_fd, sizeof(file_content) + sizeof(struct extsave), SEEK_SET))
        {
            //return 0;
        }
        else
        {
            fwrite((void *)&conf_content3, sizeof(file_content3), 1, file_fd);
        }

        fflush(file_fd);
        fclose(file_fd);
		sync();
        system("fw -ul -f /var/p300file -o 0x1f0000 /dev/rom1");
        sync();

    }
}


/***********************************************************************************
函数名:	send_arppacket
功能:		构建arp包，以garp包告知网段内ip更新
参数:		无
返回值:	无
************************************************************************************/
void send_arppacket()
{
    struct sockaddr sa;
    int sock;
    unsigned int ip_addr;

    struct arp_packet
    {
        unsigned char targ_hw_addr[6];
        unsigned char src_hw_addr[6];
        unsigned short frame_type;
        unsigned short hw_type;
        unsigned short prot_type;
        unsigned char hw_addr_size;
        unsigned char prot_addr_size;
        unsigned short op;
        unsigned char sndr_hw_addr[6];
        unsigned char sndr_ip_addr[4];
        unsigned char rcpt_hw_addr[6];
        unsigned char rcpt_ip_addr[4];
        unsigned char padding[18];
    } arphead;

    void get_hw_addr(char *buf, char *str);

    sock = socket(AF_INET, SOCK_PACKET, htons(0x0003));
    arphead.frame_type = htons(0x0806);
    arphead.hw_type = htons(1);
    arphead.prot_type = htons(0x0800);
    arphead.hw_addr_size = 6;
    arphead.prot_addr_size = 4;
    arphead.op = htons(1);
    bzero(arphead.padding, 18);

    get_hw_addr(arphead.targ_hw_addr, "ff:ff:ff:ff:ff:ff");
    get_hw_addr(arphead.rcpt_hw_addr, "ff:ff:ff:ff:ff:ff");

    get_hw_addr(arphead.src_hw_addr, conf_content.slot_u.mac);
    get_hw_addr(arphead.sndr_hw_addr, conf_content.slot_u.mac);

    ip_addr = inet_addr(conf_content.slot_u.ip);

    memcpy(arphead.sndr_ip_addr, &ip_addr, 4);
    memcpy(arphead.rcpt_ip_addr, &ip_addr, 4);
    strcpy(sa.sa_data, "eth0");

    if (sendto(sock, &arphead, sizeof(arphead), 0, &sa, sizeof(sa)) < 0)
    {
        perror("sendto");
        return ;
    }
    close(sock);
    printf("Garp send ok\n");
    return ;
}

/***********************************************************************************
函数名:	get_hw_addr
功能:		将mac地址转换为16进制
参数:		char类型的mac地址
返回值:	16进制数据，存放于字节中
************************************************************************************/

void get_hw_addr(char *buf, char *str)
{
    int i;
    char c, val;
    for (i = 0; i < 6; i++)
    {
        c = tolower(*str++);
        if (isdigit(c))
        {
            val = c - '0';
        }
        else
        {
            val = c - 'a' + 10;
        }
        *buf = val << 4;

        c = tolower(*str++);
        if (isdigit(c))
        {
            val = c - '0';
        }
        else
        {
            val = c - 'a' + 10;
        }
        *buf++ |= val;

        if (*str == ':')
        {
            str++;
        }
    }
}


void config_enet()
{
    int len;
    unsigned char  mcp_ip[16];
    unsigned char  mcp_gateway[16];
    unsigned char  mcp_mask[16];
    unsigned char  mcp_mac[18];

    memset( mcp_ip, '\0', sizeof(unsigned char) * 16);
    memset( mcp_gateway, '\0', sizeof(unsigned char) * 16);
    memset( mcp_mask, '\0', sizeof(unsigned char) * 16);
    memset( mcp_mac, '\0', sizeof(unsigned char) * 18);

    API_Get_McpIp(mcp_ip);
    API_Get_McpGateWay(mcp_gateway);
    len = strlen(mcp_gateway);
    if(len < 7)
    {
        memset( mcp_gateway, '\0', sizeof(unsigned char) * 16);
    }
    API_Get_McpMask(mcp_mask);
    API_Get_McpMacAddr(mcp_mac);
    if(strncmp1(conf_content.slot_u.tid, "LT", 2) != 0)
    {
        memcpy(conf_content.slot_u.tid, "LT00000000000005", 16);
        printf("tid=%s\n", conf_content.slot_u.tid);

        //mcp_init
        memcpy(conf_content.slot_u.ip, mcp_ip, strlen(mcp_ip));
        memcpy(conf_content.slot_u.mask, mcp_mask, strlen(mcp_mask));
        memcpy(conf_content.slot_u.gate, "192.168.1.1", 11);
        API_Set_McpGateWay("192.168.1.1");
        //memcpy(conf_content.slot_u.dns1,"172.16.31.10",11);
        //memcpy(conf_content.slot_u.dns2,"172.16.31.10",11);
        memcpy(conf_content.slot_u.mac, mcp_mac, strlen(mcp_mac));

        memcpy(conf_content.slot_u.msmode, "MAIN", 4);
        conf_content.slot_u.time_source = '0';
        conf_content.slot_u.fb = '0';

        conf_content.slot_u.out_ssm_en = '1';
        conf_content.slot_u.out_ssm_oth = 0x04;
        //memcpy(conf_content.slot_u.leap_num,"00",2);
    }
    else
    {
        printf("tid=%s\n", conf_content.slot_u.tid);
		
        if(strcmp(conf_content.slot_u.ip, mcp_ip) != 0)
        {
            API_Set_McpIp(conf_content.slot_u.ip);
        }
       // if(strcmp(conf_content.slot_u.mask, mcp_mask) != 0)
        {
            API_Set_McpMask(conf_content.slot_u.mask);
			printf("mask=%s\n", conf_content.slot_u.mask);
        }
        if(strcmp(conf_content.slot_u.gate, mcp_gateway) != 0)
            if(if_a_string_is_a_valid_ipv4_address(conf_content.slot_u.gate) == 0)
            {
                API_Set_McpGateWay(conf_content.slot_u.gate);
            }
        if(strcmp(conf_content.slot_u.mac, mcp_mac) != 0)
        {
            API_Set_McpMacAddr(conf_content.slot_u.mac);
        }

        if((conf_content.slot_u.fb == '0') || (conf_content.slot_u.fb == '1'))
        {}
        else
        {
            conf_content.slot_u.fb = '0';
        }
		if( '0'!=conf_content.slot_u.time_source && '1'!=conf_content.slot_u.time_source )
		{
        	conf_content.slot_u.time_source = '0';
		}
    }
#if DEBUG_NET_INFO
    printf("%s:<ip>:<%s>\n", NET_INFO, conf_content.slot_u.ip);
    printf("%s:<gate>:<%s>\n", NET_INFO, conf_content.slot_u.gate);
    printf("%s:<mask>:<%s>\n", NET_INFO, conf_content.slot_u.mask);
    printf("%s:<mac>:<%s>\n", NET_INFO, conf_content.slot_u.mac);
#endif
    send_arppacket();//2014-5-19发送arp包，告知所有pc端mcp的ip与mac地址
}

int API_Set_McpIp(unsigned char  *mcp_local_ip)
{
    struct sockaddr_in sin;
    struct ifreq ifr;


    int s;
    //int ret;
    //unsigned char  *ptr;
    unsigned char  *eth_name = "eth0";
    memset(&ifr, '\0', sizeof(struct ifreq));
    s = socket(AF_INET, SOCK_DGRAM, 0);
    if(s == -1)
    {
        //perror("Not create network socket connection\\n");
        return (-1);
    }
    strncpy(ifr.ifr_name, eth_name, IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ - 1] = 0;
    memset(&sin, '\0', sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = inet_addr(mcp_local_ip);

    memmove(&ifr.ifr_addr, &sin, sizeof(sin));
    if(ioctl(s, SIOCSIFADDR, &ifr) < 0)
    {
        return (-1);
    }
    ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
    if(ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
    {
        return (-1);
    }
    close(s);

    return 0;
}


int API_Set_McpMask(unsigned char  *mcp_local_mask)
{
    int s;
    unsigned char  *interface_name = "eth0";
    struct ifreq ifr;
    struct sockaddr_in netmask_addr;

    if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Socket");
        return (-1);
    }

    strcpy(ifr.ifr_name, interface_name);
    memset(&netmask_addr, '\0', sizeof(struct sockaddr_in));
    netmask_addr.sin_family = PF_INET;
    netmask_addr.sin_addr.s_addr = inet_addr(mcp_local_mask);

    memmove(&ifr.ifr_ifru.ifru_netmask, &netmask_addr, sizeof(struct sockaddr_in));

    if(ioctl(s, SIOCSIFNETMASK, &ifr) < 0)
    {
        perror("ioctl");
        return (-1);
    }
    close(s);

    return 0;
}

int API_Set_McpGateWay(unsigned char *mcp_local_gateway)
{
    unsigned char cmd[50] = "route add default gw ";
    struct sockaddr_in ip_gateway;

    ip_gateway.sin_addr.s_addr = inet_addr(mcp_local_gateway); //inet_addr("192.168.3.11");
    system("route del default");
    usleep(20);

    strcat(cmd, mcp_local_gateway);
    printf("%s\n", cmd);
    system(cmd);
    return 0;
}

int API_Set_McpMacAddr(unsigned char *mcp_local_mac)
{
    unsigned char  mac_add[60];
    memset(mac_add, '\0', 60);
    sprintf((char *)mac_add, "ifconfig eth0 hw ether %s", mcp_local_mac );
    system(mac_add);
    return 0;
}

int API_Get_McpIp(unsigned char  *mcp_local_ip)
{
    int s;
    struct ifreq ifr;
    struct sockaddr_in *ptr;
    unsigned char *interface_name = "eth0";
    if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Socket");
        return (-1);
    }

    strncpy(ifr.ifr_name, interface_name, IFNAMSIZ);

    if(ioctl(s, SIOCGIFADDR, &ifr) < 0)
    {
        perror("Get_McuLocalIp ioctl");
        return (-1);

    }
    ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
    strcpy(mcp_local_ip, (unsigned char *)inet_ntoa (ptr->sin_addr)); // 获取IP地址
    close(s);

    //printf("get ip =%s\n",mcp_local_ip);
    return 0;
}

int API_Get_McpMask(unsigned char  *mcp_local_mask)
{
    int s;
    unsigned char *interface_name = "eth0";
    struct ifreq ifr;
    struct sockaddr_in *ptr;
    if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Socket");
        return (-1);
    }

    strcpy(ifr.ifr_name, interface_name);

    if(ioctl(s, SIOCGIFNETMASK, &ifr) < 0)
    {
        perror("ioctl");
        return (-1);
    }


    ptr = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_netmask;
    strcpy(mcp_local_mask , (unsigned char *)inet_ntoa(ptr->sin_addr));
    close(s);

    //printf("get mask =%s\n",mcp_local_mask);
    return 0;
}

int API_Get_McpGateWay(unsigned char  *mcp_local_gateway)
{
    int pl = 0, ph = 0;
    int i;
    FILE *rtreadfp;
    unsigned char  buf[1024];
    memset(buf, '\0', 1024);
    if((rtreadfp = popen( "route -n", "r" )) == NULL)
    {
    }
    fread( buf, sizeof(char), sizeof(buf),  rtreadfp);
    pclose( rtreadfp );

    for (i = 0; i < 1024; i++)
    {
        if (buf[i] == 'U'  && buf[i + 1] == 'G' && (i + 1) < 1024)
        {
            pl = i;
            break;
        }
    }
    while (buf[pl] > '9' || buf[pl] < '0')
    {
        pl--;
    }
    while (('0' <= buf[pl] && buf[pl] <= '9') || buf[pl] == '.')
    {
        pl--;
    }
    while (buf[pl] > '9' || buf[pl] < '0')
    {
        pl--;
    }
    buf[pl + 1] = '\0';
    for (i = pl; ('0' <= buf[i] && buf[i] <= '9') || buf[i] == '.'; i--);
    ph = i + 1;
    strcpy(mcp_local_gateway, &buf[ph]);
    return 0;
}

int API_Get_McpMacAddr(unsigned char  *mcp_local_mac)
{
    int s;
    struct ifreq ifr;
    unsigned char *ptr;
    unsigned char *interface_name = "eth0";
    unsigned char  buf[6];
    unsigned char  tmp[20];
    memset(buf, '\0', 6);
    memset(tmp, '\0', 20);
    if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("Socket");
    }

    strcpy(ifr.ifr_name, interface_name);

    if(ioctl(s, SIOCGIFHWADDR, &ifr) != 0)
    {
        perror("ioctl");
    }
    ptr = (unsigned char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
    buf[0] = *ptr;
    buf[1] = *(ptr + 1);
    buf[2] = *(ptr + 2);
    buf[3] = *(ptr + 3);
    buf[4] = *(ptr + 4);
    buf[5] = *(ptr + 5);

    sprintf((char *)tmp, "%02x:%02x:%02x:%02x:%02x:%02x",
            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
    strcpy(mcp_local_mac, tmp);
    //printf("mac:%s\n",mcp_local_mac);

    close(s);
    return 0;
}

int if_a_string_is_a_valid_ipv4_address(const char *str)
{
    struct in_addr addr;
    int ret;
    ret = inet_pton(AF_INET, str, &addr);
    if (ret > 0)
    {
        return 0;
    }
    else
    {
        return (-1);
    }
}


int judge_length(unsigned char *buf)
{
    int len;

    len = strlen_r(buf, ';') + 1;
    if(len > 2)
    {
        if(buf[1] == (len - 2))
        {
            return 0;
        }
        else
        {
            return (-1);
        }
    }
    else
    {
        return (-1);
    }
}

int judge_client_online(int *clt_num)
{
    int i, count;
    count = 0;
    for(i = 1; i < MAX_CLIENT; i++)
    {
        if(client_fd[i] != (-1))
        {
            clt_num[count] = i;
            count++;
        }
    }
    return count;
}




void judge_data(unsigned char *recvbuf, int len , int client)
{
    unsigned char ctag[7];
    unsigned char temp[20];
    int ret = 0;
    int len_t = 0;

    memset(ctag, 0, 7);
    memset(temp, 0, 20);

    if(len == 10)
    {
        if(strncmp1("YE", recvbuf, 2) == 0)
        {
            memcpy(ctag, &recvbuf[3], 6);
            respond_success(current_client, ctag);
        }
        else if(strncmp1("NO", recvbuf, 2) == 0)
        {
            memcpy(ctag, &recvbuf[3], 6);
            respond_fail(current_client, ctag, 2);
        }
        else if(strncmp1("BEGINNING", recvbuf, 9) == 0)
        {
            process_config();
        }
    }
    else if(strncmp1("MCP", recvbuf, 3) == 0)
    {
        len_t = strlen(recvbuf);
        strncpy(temp, &recvbuf[8], len_t - 9);
        //printf("%s\n",temp);
        if(strncmp1("COM", &recvbuf[4], 3) == 0)
        {
            if(recvbuf[9] == '0')
            {
                print_switch = 0;
                sendtodown("open", 'u');
            }
            else if(recvbuf[9] == '1')
            {
                print_switch = 1;
                sendtodown("clse", 'u');
            }
            else
            {
                return;
            }
        }
        else if(strncmp1("IPP", &recvbuf[4], 3) == 0)
        {

            if(if_a_string_is_a_valid_ipv4_address(temp) == 0)
            {
                ret = API_Set_McpIp(temp);
                if(ret == 0)
                {
                    memset(conf_content.slot_u.ip, '\0', 16);
                    memcpy(conf_content.slot_u.ip, temp, strlen(temp));
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }
        else if(strncmp1("GAT", &recvbuf[4], 3) == 0)
        {
            if(if_a_string_is_a_valid_ipv4_address(temp) == 0)
            {
                ret = API_Set_McpGateWay(temp);
                if(ret == 0)
                {
                    memset(conf_content.slot_u.gate, '\0', 16);
                    memcpy(conf_content.slot_u.gate, temp, strlen(temp));
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }
        else if(strncmp1("MSK", &recvbuf[4], 3) == 0)
        {
            if(if_a_string_is_a_valid_ipv4_address(temp) == 0)
            {
                ret = API_Set_McpMask(temp);
				//printf("MSK=");
				//printf("MSK=%s\n", temp);
                if(ret == 0)
                {
                    memset(conf_content.slot_u.mask, '\0', 16);
                    memcpy(conf_content.slot_u.mask, temp, strlen(temp));
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }
        else if(strncmp1("MAC", &recvbuf[4], 3) == 0)
        {
            if(strlen(temp) == 17)
            {
                ret = API_Set_McpMacAddr(temp);
                if(ret == 0)
                {
                    memset(conf_content.slot_u.mac, '\0', 18);
                    memcpy(conf_content.slot_u.mac, temp, 17);
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }
        else if(strncmp1("TID", &recvbuf[4], 3) == 0)
        {
            if(strlen(temp) == 16)
            {
                memset(conf_content.slot_u.tid, '\0', 18);
                memcpy(conf_content.slot_u.tid, temp, 16);
            }
            else
            {
                return;
            }
        }
        else
        {
            return;
        }
        save_config();
    }
    else if(strncmp1("TIME", recvbuf, 4) == 0)
    {
        get_time(recvbuf);
    }
    else if(strncmp1("FPGA", recvbuf, 4) == 0)
    {
        get_fpga_ver(recvbuf);
    }
    else if(strncmp1("R-", &recvbuf[2], 2) == 0) //判断为100s上报信息
    {
        if((-1) == judge_length(recvbuf))
        {
            printf("rpt_len error1\n");
        }
        else
        {
            process_report(recvbuf, len);
        }
    }
    else if(strncmp1("RALM", &recvbuf[2], 4) == 0)
    {
        if((-1) == judge_length(recvbuf))
        {
            printf("rpt_len error2\n");
        }
        else
        {
            process_ralm(recvbuf, len);
        }
    }
    else if(strncmp1("REVT", &recvbuf[2], 4) == 0)
    {
        if((-1) == judge_length(recvbuf))
        {
            printf("rpt_len error3\n");
        }
        else
        {
            process_revt(recvbuf, len);
        }
    }
    else
    {
        //printf("<%s>:'recvbuf':%s\n",__FUNCTION__,recvbuf);
        process_client(client, recvbuf, len); //接收到客户数据，开始处理
    }
}

int judge_buff(unsigned char *buf)
{
    int i;
    unsigned char data[MAXDATASIZE];
    unsigned char temp[20];
    unsigned char passwd[9];
    _MSG_NODE msg_config;
    int len_data;
    char slot;
    memset(data, 0, MAXDATASIZE);
    memset(temp, 0, 20);
    memset(passwd, 0, 9);
    for(i = 0; i < _CMD_NODE_MAX; i++)
    {
        if(strncmp1(g_cmd_fun[i].cmd , buf, g_cmd_fun[i].cmd_len) == 0)
        {
            //sscanf(s,"%[^:]:%[^:]:%[^;];",buf1,buf2,buf3);
            sscanf(buf, "%[^:]::%c::%[^:]:%[^;];", temp, &slot, passwd, data);
            memset(&msg_config, '\0', sizeof(_MSG_NODE));
            //memcpy(msg_config.cmd,&recvbuf[0],i1);
            //memcpy(msg_config.ctag,&recvbuf[i3],6);
            len_data = strlen(data);
            if(len_data > 0)
            {
                memcpy(msg_config.data, data, len_data);
            }
            else
            {
                return 0;
            }
            //msg_net.cmd_len=i1;
            msg_config.solt = slot;
            memcpy(msg_config.ctag, "000000", 6);
            g_cmd_fun[i].cmd_fun(&msg_config, 0);
            break;
        }

    }
    return 0;
}


int process_config()
{
    FILE *fp;
    unsigned char buf[100];
    char slot;
    if((fp = fopen(CONFIG_FILE, "rt")) == NULL)
    {
        printf("cannot open config \n");
		return 1;
    }
    else
    {
        printf(" open CONFIG \n");
    }

    printf("CONFIG start\n");
    while(!feof(fp))
    {
        fgets(buf, sizeof(buf), fp);
        if((buf[0] == '\r') || (buf[0] == '\0') ||  (buf[0] == '\n') )
        {
            continue;
        }
        else if((buf[0] == '#') || (buf[2] == '#'))
        {
            slot = buf[1];
        }
        else
        {
            judge_buff(buf);
            /*		p = buf+strlen("trap2sink ");
            		for(i=0; *p!='\t'&&*p!=' '; i++,p++)
            			str2[i]=*p;
            		str2[i]='\0';
            */
        }
    }
    fclose(fp);
    return 0;
}


int process_client(int client, char *recvbuf, int len)
{
    unsigned char ctag[7];
    unsigned char *noctag = "noctag";
    int i, j, i1, i2, i3, i4, i5, len_buf;
    _MSG_NODE msg_net;

    memset(ctag, '\0', sizeof(unsigned char) * 7);
    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    i5 = 0;
    for(i = 2; i < len; i++)
    {
        if(recvbuf[i] == ':')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
            if(j == 5)
            {
                i5 = i + 1;
            }
        }
    }
    if(i5 == 0)
    {
        return 1;
    }
    if((j < 5) || (recvbuf[len - 1] != ';'))
    {
        //出错响应
        if((i4 - i3) == 6)
        {
            memcpy(ctag, &recvbuf[i3], i4 - i3);
        }
        else if((i4 - i3) == 0)
        {
            memcpy(ctag, noctag, 6);
        }

        respond_fail(client, ctag, 6);
        return 1;
    }
    else
    {
        //如果tid相同，赋值到结构体(密码功能暂无)
        if(strncmp1(&recvbuf[i1], conf_content.slot_u.tid, 16) == 0)
        {

#if DEBUG_NET_INFO
            if(print_switch == 0)
            {
                printf("%s:<from net>:%s\n", NET_INFO, recvbuf);
            }
#endif
            //printf("%s:<from net>:%s\n", NET_INFO, recvbuf);
            memset(&msg_net, '\0', sizeof(_MSG_NODE));
            memcpy(msg_net.cmd, &recvbuf[0], i1);
            memcpy(msg_net.ctag, &recvbuf[i3], 6);
            len_buf = len - i5 - 1;
            if(len_buf > 0)
            {
                memcpy(msg_net.data, &recvbuf[i5], len_buf);
            }
            msg_net.cmd_len = i1;
            msg_net.solt = recvbuf[i2];
            cmd_process(client, &msg_net);
        }
        else
        {
            //失败响应
            memcpy(ctag, &recvbuf[i3], 6);
            respond_fail(client, ctag, 5);

            return 1;
        }
    }

    return 0;
}




/***********************************************************************************
函数名:	event_filter
功能:		屏蔽掉多余事件
参数:		槽位号；
返回值:	无
************************************************************************************/

int event_filter(char *cmd_num)
{
    char *event_id = "C23,C26,C24,C27,C28,C29,C05,C06,C07,C08,C10,C31,C42,C43,C51,C52,C61,C62,C63,C64,C67,C71,C72,C81,CB1,CB2,CB3,CC1,CC2,CC3,CC4,";
    if( (NULL == cmd_num) || (cmd_num[0] != 'C') )
    {
        printf("event_filter para error\n");
        return 0;
    }
    else
    {
        if(strstr(event_id, cmd_num) != NULL)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}

/***********************************************************************************
函数名:	event_filter
功能:		屏蔽掉多余事件
参数:		槽位号；
返回值:	无
************************************************************************************/

int event_filter_another(char *cmd_num)
{
    char *event_id = "C03,C31,C51,C52,C53,CC7,CC5,CD3,";
    if( (NULL == cmd_num) || (cmd_num[0] != 'C') )
    {
        printf("event_filter para error\n");
        return 0;
    }
    else
    {
        if(strstr(event_id, cmd_num) != NULL)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}



/***********************************************************************************
函数名:	is_change
功能:		是否为重复上报
参数:		槽位号,上报参数
返回值:	无
************************************************************************************/
int is_change(int num, char *str)
{
    int ret = 1;
    int len = 0;
    if(num < 0)
    {
        printf("is_change error pare_num %d\n", num);
        return 0;
    }
    if(str == NULL)
    {
        printf("is_change error pare_str NULL\n");
        return 0;
    }

    len = strlen(str);
    ret = strncmp(event_old[num], str, len);
    if(ret == 0)
    {
        return 0;
    }
    else
    {
        memset(event_old[num], 0, 128);
        memcpy(event_old[num], str, 128);
        return 1;
    }
}



int process_report(unsigned char *recv, int len)
{
    int i, j, i1, i2, num;
    unsigned char  rpt_head[10];
    unsigned char  rpt_data[128];
    unsigned char  rpt_type;
    unsigned char  *buf_auto = "AUTO";

    //int i_temp,j_temp;
    memset(rpt_head, '\0', 10);
    memset(rpt_data, '\0', 128);
    j = 0;
    i1 = 0;
    i2 = 0;

    for(i = 2; i < len; i++)
    {
        if(recv[i] == ':')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
        }
        else if(recv[i] == ',')
        {
            j++;
            if(j == 2)
            {
                i2 = i + 1;
            }
            break;
        }
    }
    if((i1 == 0) || (i2 == 0))
    {
        return 1;
    }

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:<from pan>:>%s<\n", NET_INFO, &recv[2]);
    }
#endif

    memcpy(rpt_head, &recv[2], i1 - 3);
    memcpy(rpt_data, &recv[i2], len - i2 - 1);
    rpt_type = recv[i1];

    num = (int)recv[0] - 0x01;
    if((num < 0) || (num > 17))
    {
        return 1;
    }

    for(i = 0; i < _NUM_RPT100S; i++)
    {
        if(strncmp1(RPT100S_fun[i].cmd , rpt_head , RPT100S_fun[i].cmd_len) == 0)
        {
            if((0 == memcmp(rpt_head, "R-OUT-CFG", 9)) || (0 == memcmp(rpt_head, "R-OUT-MGR", 9)))
            {
                if( (EXT_DRV != slot_type[num]) &&
                        ((12 == num) || (13 == num)) )
                {
                    slot_type[num] = EXT_DRV;
                    send_online_framing(num, EXT_DRV);
                }
            }

            if(	(EXT_DRV == slot_type[num]) &&
                    ((12 == num) || (13 == num)) )
            {
                RPT100S_fun[i].rpt_fun(rpt_type, rpt_data);
            }
            else
            {
                //保存槽位类型
                if(slot_type[num] != rpt_type)
                {
                    slot_type[num] = rpt_type;
                    /*send 插盘事件(针对输出盘)*/
                    send_online_framing(num, rpt_type);
                    init_pan((char)(num + 'a'));
                }

                if(RPT100S_fun[i].cmd_num[0] == 'C')
                {
                    if( event_filter(RPT100S_fun[i].cmd_num) )
                    {
                        //过滤不上报事件.
                    }
                    else
                    {
                        //if( is_change(num,rpt_data) )
                        //{
                        //有需要及时上报，组针上报
                        rpt_event_framing(RPT100S_fun[i].cmd_num, (char)(num + 'a'),
                                          1, rpt_data, 0, buf_auto);
                        //}
                        //else
                        //{
                        //;
                        //}
                    }
                }

                RPT100S_fun[i].rpt_fun((char)(num + 'a'), rpt_data);
            }

            break;
        }
    }

    return 0;
}


int almMsk_ExtRALM(u8_t ext_sid, unsigned char *rpt_alm,struct extctx *ctx)
{
    int i,j,len;
	int index = -1;
    unsigned char type_flag = 0;
	unsigned char num = 0;
	len = strlen(rpt_alm);
    if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
       num = (ext_sid - EXT_1_OUT_OFFSET)%10;
       type_flag = ctx->extBid[0][num];
    }
	else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
	{
	   num = (ext_sid - EXT_2_OUT_OFFSET)%10;
	   type_flag = ctx->extBid[1][num];
	}
	else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
	{
	   num = (ext_sid < 58) ? (ext_sid - EXT_3_OUT_OFFSET):(ext_sid - EXT_3_OUT_OFFSET - 2);//except ':;'
	   type_flag = ctx->extBid[2][num];
	}

    else if(EXT_1_MGR_PRI == ext_sid)
    {
       type_flag = ctx->extBid[0][10];
    }
	else if(EXT_1_MGR_RSV == ext_sid)
    {
       type_flag = ctx->extBid[0][11];
    }	
	else if(EXT_2_MGR_PRI == ext_sid)
	{
	   type_flag = ctx->extBid[1][10];
	}
	else if(EXT_2_MGR_RSV == ext_sid)
	{
	   type_flag = ctx->extBid[1][11];
	}	
	else if(EXT_3_MGR_PRI == ext_sid)
	{
	   type_flag = ctx->extBid[2][10];
	}
	else if(EXT_3_MGR_RSV == ext_sid)	
	{
	   type_flag = ctx->extBid[2][11];
	}
	else
		return -1;
	for(j = 0; j < alm_msk_total; j++)
    {
        if(type_flag == conf_content.alm_msk[j].type)
        {
            index = j;
            //printf("slot_type[num] = %c\n",slot_type[num]);
            break;
        }
    }
	if(j == alm_msk_total)
    {
        //printf("slottype[%d]=%c  alm_msk no record\n",num,slot_type[num]);
        return -1;
    }
	if( (rpt_alm[0] >> 4) == 0x4 && -1 != index)
    {

        for(i = 0; i < len; i++)
        {

            rpt_alm[i] &= conf_content.alm_msk[index].data[i];

        }
    }
	
	return 0;
}



int almMsk_cmdRALM(int num, unsigned char *rpt_alm, int len)
{
    int i, j;
    int index = -1;
    if(num < 0)
    {
        printf("%s num is error\n", __FUNCTION__);
        return -1;
    }
    if('O' == slot_type[num])
    {
        return 0;
    }
    if(NULL == rpt_alm)
    {
        printf("rpt_alm is NULL\n");
        return -1;
    }
    //20140926 drv在线告警处理
    if( (12 == num || 13 == num)
            && ('d' == slot_type[num]) )
    {
        g_DrvLost = 200;//20141017
        rpt_alm[0] = rpt_alm[0] & gExtCtx.save.onlineSta;
        return 0;
    }
    else
    {
    }

    for(j = 0; j < alm_msk_total; j++)
    {
        if(slot_type[num] == conf_content.alm_msk[j].type)
        {
            index = j;
            //printf("slot_type[num] = %c\n",slot_type[num]);
            break;
        }
    }
    if(j == alm_msk_total)
    {
        //printf("slottype[%d]=%c  alm_msk no record\n",num,slot_type[num]);
        return -1;
    }
    if( (rpt_alm[0] >> 4) == 0x4 && -1 != index)
    {

        for(i = 0; i < len; i++)
        {

            rpt_alm[i] &= conf_content.alm_msk[index].data[i];

        }
    }
    else if((rpt_alm[0] >> 4) == 0x5 && -1 != index)
    {

        for(i = 0; i < len; i++)
        {

            rpt_alm[i] &= conf_content.alm_msk[index].data[i + 6];
        }
    }
    else
    {

    }
    return 0;
}

/*
RB是否锁定
未锁定，对MCP中的第0位第1位    置1
*/
void is_RB_unlocked(char *rpt_alm)
{
	unsigned char temp;
	
	if(rpt_alm == NULL)
	{
		return ;
	}

	temp = rpt_alm[0];
	if(slot_type[14] != 'O') //15槽位RB 盘
	{
		if(strncmp(rpt_content.slot_o.rb_trmode,"TRCK",4) == 0)
		{
			temp &= (~(1<<1));
		}
		else
		{
			temp |= (1<<1);
		}
	}

	if(slot_type[15] != 'O') //16槽位RB 盘
	{
		if(strncmp(rpt_content.slot_p.rb_trmode,"TRCK",4) == 0)
		{
			temp &= (~(1<<0));
		}
		else
		{
			temp |= (1<<0);
		}
	}
	
	rpt_alm[0] = temp;
}

//
// 改变上报的告警处理
//
int process_ralm(unsigned char *recv, int len)
{
    /*<slot><len>RALM:<slotid>:<alarm>;*/
    unsigned char  rpt_alm[7];
    unsigned char  savealm[14];
    unsigned char  ext_alm[32];
    int num;
    int flag_ref = 0;
    unsigned char  buf[4];

    memset(rpt_alm, '\0', 7);
    memset(savealm, '\0', 14);

#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:<from pan>:%s\n", NET_INFO, recv);
    }
#endif

    if(recv[7] >= 'a' && recv[7] <= 'w')
    {
        num = (int)(recv[7] - 'a');
        if((num <= 0) || (num > 23))
        {
            return 1;
        }
        if(slot_type[num] == 'O')
        {
            return 1;
        }
        memcpy(rpt_alm, &recv[9], len - 10);


        if(num == 14)
        {
            strncpynew(buf, rpt_content.slot_o.tod_buf, 3);
            send_tod(buf, 'o');
        }
        else if(num == 15)
        {
            strncpynew(buf, rpt_content.slot_p.tod_buf, 3);
            send_tod(buf, 'p');
        }
		else if(20 == num)
		{
			is_RB_unlocked(rpt_alm);
		}
        /*告警帧*/
        //printf("<%s>\n",rpt_alm);
        rpt_alm_to_client(recv[7], rpt_alm);

        /*保存告警*/
        /*"<aid>,<type>,<alarm>"*/
        if((rpt_alm[0] >> 4) == 0x05)
        {
            if(slot_type[num] == 'S')
            {
	            if((num != 1) || (num != 0))
	                return 1;
				else
					flag_ref = 1;
            }
			else if(slot_type[num] == 'j')
			{
			        flag_ref = 2;
			}
				
            
        }
        sprintf((char *)savealm, "\"%c,%c,%s\"", recv[7], slot_type[num], rpt_alm);
        save_alm(recv[7], savealm, flag_ref);

        //ext
        if( ('m' == recv[7]) && (EXT_DRV == slot_type[12]) )
        {
            gExtCtx.drv[0].drvAlm = recv[9];
        }
        if( ('n' == recv[7]) && (EXT_DRV == slot_type[13]) )
        {
            gExtCtx.drv[1].drvAlm = recv[9];
        }
        if('u' == recv[7])
        {
            if(recv[13]&BIT(2))
            {
                gExtCtx.extMcpDrvAlm[0] = 1;
            }
            else
            {
                gExtCtx.extMcpDrvAlm[0] = 0;
            }

            if(recv[13]&BIT(3))
            {
                gExtCtx.extMcpDrvAlm[1] = 1;
            }
            else
            {
                gExtCtx.extMcpDrvAlm[1] = 0;
            }
        }

        ext_drv2_mgr6_pr(&gExtCtx, slot_type[12], slot_type[13]);
        ext_drv_mgr_evt(&gExtCtx);
    }
    else
    {
        memset(ext_alm, 0, 32);
        memcpy(ext_alm, &recv[9], len - 10);
		almMsk_ExtRALM(recv[7], ext_alm,&gExtCtx);
        if(0 == ext_alm_cmp(recv[7], ext_alm, &gExtCtx))
        {
            return 1;
        }

        if(0 == ext_out_alm_crpt(recv[7], ext_alm, &gExtCtx))
        {
            return 1;
        }
    }

    return 0;
}



int process_revt(unsigned char *recv, int len)
{
    int i, j, i1, i2, i3, i4, ret;
    unsigned char  rpt_event[4];
    unsigned char  rpt_data[SENDBUFSIZE];
    unsigned char  rpt_reason[5];
    char  rpt_slot;
    char tmp_buf[64];
    char tp_1588_flag = 0;

    memset(rpt_event, '\0', 4);
    memset(rpt_data, '\0', SENDBUFSIZE);
    memset(rpt_reason, '\0', 5);

    j = 0;
    i1 = 0;
    i2 = 0;
    i3 = 0;
    i4 = 0;
    for(i = 2; i < len; i++)
    {
        if(recv[i] == ':')
        {
            j++;
            if(j == 1)
            {
                i1 = i + 1;
            }
            if(j == 2)
            {
                i2 = i + 1;
            }
            if(j == 3)
            {
                i3 = i + 1;
            }
            if(j == 4)
            {
                i4 = i + 1;
            }
        }
    }
    if(i4 == 0)
    {
        return 1;
    }
    rpt_slot = recv[i1];
    memcpy(rpt_event, &recv[i2], 3);
    memcpy(rpt_data, &recv[i3], i4 - i3 - 1);
    memcpy(rpt_reason, &recv[i4], len - i4 - 1);
    if((strncmp1(rpt_reason, "LMC", 3) != 0) && (strncmp1(rpt_reason, "AUTO", 4) != 0))
    {
        return 1;
    }
    //modified by zhanghui 2013-9-11 15:52:38
    if('I' == slot_type[recv[0] - 1])
    {
        //printf("revt recv :%s\n",recv);
        //printf("revt :%s\n",rpt_data);
        memset(tmp_buf, 0, 64);
        memcpy(tmp_buf, rpt_data, strlen(rpt_data));
        tp_1588_flag = 1;

        //pps state switch
        if('0' == tmp_buf[0])
        {
            tmp_buf[0] = '1';
        }
        else if('1' == tmp_buf[0])
        {
            tmp_buf[0] = '2';
        }
        else if('2' == tmp_buf[0])
        {
            tmp_buf[0] = '3';
        }
        else if('5' == tmp_buf[0])
        {
            tmp_buf[0] = '4';
        }
        else
        {
            tmp_buf[0] = '3';
        }

        //source type switch
        if('2' == tmp_buf[2])
        {
            tmp_buf[2] = '3';
        }
        else if('3' == tmp_buf[2])
        {
            tmp_buf[2] = '2';
        }
        else
        {
            //do nothing
        }
    }

    for(i = 0; i < NUM_REVT; i++)
    {
        if(strncmp1(REVT_fun[i].cmd_num, rpt_event, 3) == 0)
        {
            //保存信息
            ret = REVT_fun[i].rpt_fun(rpt_slot, rpt_data);
            if(( 0 == ret ) && ( !event_filter_another(REVT_fun[i].cmd_num )))
            {
                if(1 == tp_1588_flag)
                {

                    //printf("<process_revt>:%s\n",tmp_buf);
                    rpt_event_framing(rpt_event, (char)(recv[0] + 'a' - 1),
                                      1, tmp_buf, 0, rpt_reason);
                }
                else
                {
                    //组针上报
                    //rpt_event_framing(rpt_event, (char)(recv[0] + 'a' - 1),
                    //                  1, rpt_data, 0, rpt_reason);
                    rpt_event_framing(rpt_event, (char)rpt_slot,
                                      1, rpt_data, 0, rpt_reason);
                }
            }
            break;
        }
    }

    return 0;

}



int cmd_process(int client, _MSG_NODE *msg)
{
    int i, ret;

    for(i = 0; i < _CMD_NODE_MAX; i++)
    {
        if(strncmp1(g_cmd_fun[i].cmd, msg->cmd, g_cmd_fun[i].cmd_len) == 0)
        {
            current_client = client;
            if(g_cmd_fun[i].cmd_fun != NULL)
            {
                ret = g_cmd_fun[i].cmd_fun(msg, 1);
                if(ret == 0)
                {
                    //返回成功响应
                    respond_success(client, msg->ctag);
                    return 0;
                }
                else
                {
                    //返回错误响应
                    //printf("ret=%d\n",ret);
                    respond_fail(client, msg->ctag, 7);
                    return 0;
                }
            }
            else
            {
                ret = g_cmd_fun[i].rpt_fun(client, msg);
                //printf("RTRV\n");
                if(ret == 0)
                {
                    respond_success(client, msg->ctag);
                }
                else
                {
                    respond_fail(client, msg->ctag, 8);
                }
                return 0;
            }

        }
    }

    respond_fail(client, msg->ctag, 3);
    return 0;
}
int get_fpga_ver(unsigned char *data)
{
    memcpy(fpga_ver, &data[5], 6);
    return 0;
}

//12#drv在在没插入mge时可能不会上报告警信息，特此检查12#扩展框是否没插mge
void CheckExtLost()
{
    if(slot_type[12] != 'd')
    {
        g_DrvLost = 200;
        return;
    }
    else
    {
        if(g_DrvLost <= 0 )
        {
            g_DrvLost = 200;
            alm_sta[12][5] = 0x4f;
            alm_sta[12][5] = alm_sta[12][5] & gExtCtx.save.onlineSta;
        }
        else
        {
            g_DrvLost--;
        }
    }
}


void autoLeap(void)
{
    static int count = 0;
    _MSG_NODE *msg = NULL;

    extern int cmd_set_leap_num(_MSG_NODE * msg, int flag);
    if(g_LeapFlag != 1)
    {
        return ;
    }
    else
    {
        count++;
    }

    if(count >= 60)
    {
        msg = (_MSG_NODE *)malloc(sizeof(_MSG_NODE));
		if(NULL == msg)
        {
            printf("autoLeap Malloc is failed\n");
            return;
        }
        memset(msg, 0, sizeof(_MSG_NODE));
        sprintf((char *)msg->cmd, "SET-LEAP-NUM:");
        msg->cmd_len = 13;
        msg->solt = 'u';
        memcpy(msg->ctag, "R00000", 6);
        memcpy(msg->data, conf_content.slot_u.leap_num, 2);
        cmd_set_leap_num(msg, 1);
        cmd_set_leap_num(msg, 1);
        cmd_set_leap_num(msg, 1);

        count = 0;
        g_LeapFlag = 0;
        free(msg);

    }
    else
    {
        ;
    }
}

int get_time(unsigned char *data) /*来自RB的时间*/
{
    /*<TIME YYYY-MM-DD HH:MM:DD;>*/
    int len;
    unsigned char  year_rb[5];
    int year;
    len = strlen(data);

    //20141017 判断mge是否全丢失
    CheckExtLost();

	//20141210自动设置闰秒
    if(1 == g_LeapFlag)
    {
        autoLeap();
    }
    else
    {
        ;
    }

    if(len == 25)
    {
        if((data[9] == '-') && (data[12] == '-') && (data[18] == ':') && (data[21] == ':'))
        {

            memcpy(year_rb, &data[5], 4);
            year = atoi(year_rb);
            if(year < 2011)
            {
                return 1;    /*如果从FPGA时间未获得，使用默认时间*/
            }

            memcpy(mcp_date, &data[5], 10);
            memcpy(mcp_time, &data[16], 8);

        }
    }
    else
    {
#if DEBUG_NET_INFO
        if(print_switch == 0)
        {
            printf("%s:error_time!\n", NET_INFO);
            printf("%s:<%s>\n", NET_INFO, data);
        }
#endif
    }
    return 0;
}

void get_time_fromnet()/*根据网管的配置确定时间*/
{
    unsigned char year_rb[5];
    unsigned char month_rb[3];
    unsigned char day_rb[3];
    unsigned char hour_rb[3];
    unsigned char minute_rb[3];
    unsigned char second_rb[3];
    int year, month, day, hour, minute, second;
    int tmp_hour, tmp_minute, tmp_second;

    if((conf_content.slot_u.time_source == '1') && (time_temp != 0))
    {
        memcpy(year_rb, mcp_date, 4);
        memcpy(month_rb, &mcp_date[5], 2);
        memcpy(day_rb, &mcp_date[8], 2);

        memcpy(hour_rb, mcp_time, 2);
        memcpy(minute_rb, &mcp_time[3], 2);
        memcpy(second_rb, &mcp_time[6], 2);


        year = atoi(year_rb);
        month = atoi(month_rb);
        day = atoi(day_rb);

        hour = atoi(hour_rb);
        minute = atoi(minute_rb);
        second = atoi(second_rb);


        if(time_temp < 0)
        {
            tmp_hour = abs(time_temp) / 3600;
            tmp_minute = (abs(time_temp) % 3600) / 60;
            tmp_second = abs(time_temp) % 60;

            second -= tmp_second;
            minute -= tmp_minute;
            hour -= tmp_hour;

            if(second < 0)
            {
                second += 60;
                minute--;
            }
            if(minute < 0)
            {
                minute += 60;
                hour--;
            }
            if(hour < 0)
            {
                hour += 24;
                day--;
            }
            if(day == 0)
            {
                if(month == 1)
                {
                    day = 31;
                    month = 12;
                    year--;
                }
                else if((month == 2) || (month == 4) || (month == 6) || (month == 8) || (month == 9) || (month == 11))
                {
                    day = 31;
                    month--;
                }
                else if((month == 5) || (month == 7) || (month == 10) || (month == 12))
                {
                    day = 30;
                    month--;
                }
                else if(month == 3)
                {
                    if(IsLeapYear(year))
                    {
                        day = 29;
                    }
                    else
                    {
                        day = 28;
                    }
                    month--;
                }
            }
        }
        else
        {
            tmp_hour = time_temp / 3600;
            tmp_minute = (time_temp % 3600) / 60;
            tmp_second = time_temp % 60;

            second += tmp_second;
            minute += tmp_minute;
            hour += tmp_hour;
            if(second > 59)
            {
                second = second % 60;
                minute++;
            }
            if(minute > 59)
            {
                minute = minute % 60;
                hour++;
            }
            if(hour > 23)
            {
                hour = hour % 24;
                day++;
            }
            if((day == 29) && (month == 2) && (IsLeapYear(year) != 1))
            {
                day = 1;
                month++;
            }
            else if((day == 30) && (month == 2) && (IsLeapYear(year) == 1))
            {
                day = 1;
                month++;
            }
            else if(day == 31)
            {
                if((month == 4) || (month == 6) || (month == 9) || (month == 11))
                {
                    day = 1;
                    month++;
                }
            }
            else if(day == 32)
            {
                if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10))
                {
                    day = 1;
                    month++;
                }
                else if(month == 12)
                {
                    day = 1;
                    month = 1;
                    year++;
                }
            }
        }

        /*<TIME YYYY-MM-DD HH:MM:DD;>*/
        memset(mcp_date, '\0', 11);
        memset(mcp_time, '\0', 9);
        sprintf((char *)mcp_date, "%04d-%02d-%02d", year, month, day);
        sprintf((char *)mcp_time, "%02d:%02d:%02d", hour, minute, second);
    }
}




int IsLeapYear(int year)
{
    int LeapYear;
    LeapYear = (((year % 4) == 0) && (year % 100 != 0)) || (year % 400 == 0);
    return LeapYear;
}


void get_data(char slot, unsigned char *data)
{
    int i, num;
    num = (int)(slot - 'a');
    sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
	//
	//!
	//
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 22; i++)
        {
            sprintf((char *)data, "%s%c,%c", data, (char)(0x61 + i), slot_type[i]);
            if((slot_type[i] != 'O') && (i < 18))
            {
                if(i < 14)
                {
                    if((EXT_DRV == slot_type[i]))
                    {
                        sprintf((char *)data, "%s,%s,,",
                                data,
                                ((0 == strlen(gExtCtx.drv[i - 12].drvVer)) ? (char *)"," : (char *)(gExtCtx.drv[i - 12].drvVer)));
                    }
                    else
                    {
                        sprintf((char *)data, "%s,%s", data,
                                (strlen(rpt_content.slot[i].sy_ver) == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot[i].sy_ver));
                    }
                }
                else if(i == 14)
                    sprintf((char *)data, "%s,%s", data,
                            (strlen(rpt_content.slot_o.sy_ver) == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_o.sy_ver));
                else if(i == 15)
                    sprintf((char *)data, "%s,%s", data,
                            (strlen(rpt_content.slot_p.sy_ver) == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_p.sy_ver));
                else if(i == 16)
                    sprintf((char *)data, "%s,%s", data,
                            (strlen(rpt_content.slot_q.sy_ver) == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver));
                else if(i == 17)
                    sprintf((char *)data, "%s,%s", data,
                            (strlen(rpt_content.slot_r.sy_ver) == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver));
            }
			else if((i == 18 )||(i == 19 ))
			{
			    if(slot_type[i] != 'O')
			    	sprintf((char *)data, "%s,,,,", data);
				else
					sprintf((char *)data, "%s", data);
			}
            else if(i == 20)
            {
                sprintf((char *)data, "%s,%s,%s,,", data, (unsigned char *)MCP_VER, fpga_ver);
            }

            sprintf((char *)data, "%s:", data);
        }
		if(slot_type[i] != 'O')
            sprintf((char *)data, "%sw,%c,,,,", data, slot_type[22]);
		else
			sprintf((char *)data, "%sw,%c", data, slot_type[22]);
    }
    else if((slot >= 'a') && (slot < 'o'))
    {
        if(slot_type[num] == 'Y')
        {
            rpt_test(slot, data);
        }
        else if('d' == slot_type[num])
        {
            rpt_drv(slot, data);
        }
        else
        {
            rpt_out(slot, data);
        }
    }
    else if((slot == 'o') || (slot == 'p'))
    {
        if(slot_type[num] == 'Y')
        {
            rpt_test(slot, data);
        }
        else
        {
            rpt_rb(slot, data);
        }
    }
    else if((slot == 'q') || (slot == 'r'))
    {
        if(slot_type[num] == 'Y')
        {
            rpt_test(slot, data);
        }
        else
        {
            rpt_gbtp(slot, data);
        }
    }
    else if((slot == 's') || (slot == 't'))
    {
        if(slot_type[num] != 'O')
        	sprintf((char *)data, "%s%c,%c,,,,", data, slot, slot_type[num]);
		else
			sprintf((char *)data, "%s%c,%c", data, slot, slot_type[num]);
    }
    else if(slot == 'u')
    {
        rpt_mcp(data);
    }
    else if(slot == 'v')
    {
        /*NULL*/
    }
    else if(slot == 'w')
    {
        if(slot_type[num] != 'O')
            sprintf((char *)data, "%s%c,%c,,,,", data, slot, slot_type[num]);
		else
			sprintf((char *)data, "%s%c,%c", data, slot, slot_type[num]);
    }

    sprintf((char *)data, "%s\"%c%c;", data, CR, LF);
}




void get_ext1_eqt(char slot, unsigned char *data)
{
    int i, num;

    num = (int)(slot - 'a');
    sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c,%c", data, (char)('a' + i), gExtCtx.extBid[0][i]);
            if(gExtCtx.extBid[0][i] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[i].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[i].outVer)));
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[i].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[i].outPR)));


            }

            strcat(data, ":");
        }

        sprintf((char *)data, "%s%c,%c", data, 'k', gExtCtx.extBid[0][10]);
        if(gExtCtx.extBid[0][10] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[0].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[0].mgrVer)));
        }
        strcat(data, ":");

        sprintf((char *)data, "%s%c,%c", data, 'l', gExtCtx.extBid[0][11]);
        if(gExtCtx.extBid[0][11] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[1].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[1].mgrVer)));
        }
        strcat(data, ":");
		
        if(gExtCtx.extBid[0][12] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'm', gExtCtx.extBid[0][12]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'm', gExtCtx.extBid[0][12]);
        strcat(data, ":");
		if(gExtCtx.extBid[0][13] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'n', gExtCtx.extBid[0][13]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'n', gExtCtx.extBid[0][13]);
    }
    else
    {
        if(slot >= 'a' && slot <= 'j')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[0][slot - 'a']);
            if(gExtCtx.extBid[0][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[slot - 'a'].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[slot - 'a'].outVer)));
                sprintf((char *)data, "%s,%s,%s",
                        data,
                        gExtCtx.out[slot - 'a'].outSsm,
                        gExtCtx.out[slot - 'a'].outSignal);
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[slot - 'a'].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[slot - 'a'].outPR)));
            }
        }
        else if(slot == 'k' || slot == 'l')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[0][slot - 'a']);
            if(gExtCtx.extBid[0][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,,%d",
                        data,
                        ((0 == strlen(gExtCtx.mgr[slot - 'k'].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[slot - 'k'].mgrVer)),
                        gExtCtx.mgr[slot - 'k'].mgrPR);
            }
        }
        else if(slot == 'm' || slot == 'n')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[0][slot - 'a']);
        }
        else
        {

        }
    }

    sprintf((char *)data, "%s\"%c%c;", data, CR, LF);
}

void get_exta1_eqt(char slot, unsigned char *data)
{
    int i, num;

    num = (int)(slot - 'a');
    sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c,%c", data, (char)('a' + i), gExtCtx.extBid[0][i]);
            if(gExtCtx.extBid[0][i] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[i].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[i].outVer)));
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[i].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[i].outPR)));


            }

            strcat(data, ":");
        }

        sprintf((char *)data, "%s%c,%c", data, 'k', gExtCtx.extBid[0][10]);
        if(gExtCtx.extBid[0][10] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[0].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[0].mgrVer)));
        }
        strcat(data, ":");

        sprintf((char *)data, "%s%c,%c", data, 'l', gExtCtx.extBid[0][11]);
        if(gExtCtx.extBid[0][11] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[1].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[1].mgrVer)));
        }
        strcat(data, ":");
        if(gExtCtx.extBid[0][12] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'm', gExtCtx.extBid[0][12]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'm', gExtCtx.extBid[0][12]);
        strcat(data, ":");

		if(gExtCtx.extBid[0][13] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'n', gExtCtx.extBid[0][13]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'n', gExtCtx.extBid[0][13]);
    }
    else
    {
        if(slot >= 'a' && slot <= 'j')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[0][slot - 'a']);
            if(gExtCtx.extBid[0][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[slot - 'a'].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[slot - 'a'].outVer)));
                sprintf((char *)data, "%s,%s,%s",
                        data,
                        gExtCtx.out[slot - 'a'].outSsm,
                        gExtCtx.out[slot - 'a'].outSignal);
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[slot - 'a'].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[slot - 'a'].outPR)));
            }
        }
        else if(slot == 'k' || slot == 'l')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[0][slot - 'a']);
            if(gExtCtx.extBid[0][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,,%d",
                        data,
                        ((0 == strlen(gExtCtx.mgr[slot - 'k'].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[slot - 'k'].mgrVer)),
                        gExtCtx.mgr[slot - 'k'].mgrPR);
            }
        }
        else if(slot == 'm' || slot == 'n')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[0][slot - 'a']);
        }
        else
        {

        }
    }

    sprintf((char *)data, "%s\"%c%c/%c%c", data, CR, LF, CR, LF);
}






void get_ext2_eqt(char slot, unsigned char *data)
{
    int i, num;

    num = (int)(slot - 'a');
    sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c,%c", data, (char)('a' + i), gExtCtx.extBid[1][i]);
            if(gExtCtx.extBid[1][i] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + i].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[10 + i].outVer)));
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + i].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[10 + i].outPR)));
            }

            strcat(data, ":");
        }

        sprintf((char *)data, "%s%c,%c", data, 'k', gExtCtx.extBid[1][10]);
        if(gExtCtx.extBid[1][10] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[2].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[2].mgrVer)));
        }
        strcat(data, ":");

        sprintf((char *)data, "%s%c,%c", data, 'l', gExtCtx.extBid[1][11]);
        if(gExtCtx.extBid[1][11] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[3].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[3].mgrVer)));
        }
        strcat(data, ":");
        if(gExtCtx.extBid[1][12] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'm', gExtCtx.extBid[1][12]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'm', gExtCtx.extBid[1][12]);
		
        strcat(data, ":");
		if(gExtCtx.extBid[1][13] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'n', gExtCtx.extBid[1][13]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'n', gExtCtx.extBid[1][13]);
    }
    else
    {
        if(slot >= 'a' && slot <= 'j')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[1][slot - 'a']);
            if(gExtCtx.extBid[1][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + slot - 'a'].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[10 + slot - 'a'].outVer)));
                sprintf((char *)data, "%s,%s,%s",
                        data,
                        gExtCtx.out[10 + slot - 'a'].outSsm,
                        gExtCtx.out[10 + slot - 'a'].outSignal);
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + slot - 'a'].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[10 + slot - 'a'].outPR)));
            }
        }
        else if(slot == 'k' || slot == 'l')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[1][slot - 'a']);
            if(gExtCtx.extBid[1][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,,%d",
                        data,
                        ((0 == strlen(gExtCtx.mgr[2 + slot - 'k'].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[2 + slot - 'k'].mgrVer)),
                        gExtCtx.mgr[2 + slot - 'k'].mgrPR);
            }
        }
        else if(slot == 'm' || slot == 'n')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[1][slot - 'a']);
        }
        else
        {

        }
    }

    sprintf((char *)data, "%s\"%c%c;", data, CR, LF);
}



void get_exta2_eqt(char slot, unsigned char *data)
{
    int i, num;
    num = (int)(slot - 'a');
    sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c,%c", data, (char)('a' + i), gExtCtx.extBid[1][i]);
            if(gExtCtx.extBid[1][i] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + i].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[10 + i].outVer)));
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + i].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[10 + i].outPR)));
            }

            strcat(data, ":");
        }

        sprintf((char *)data, "%s%c,%c", data, 'k', gExtCtx.extBid[1][10]);
        if(gExtCtx.extBid[1][10] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[2].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[2].mgrVer)));
        }
        strcat(data, ":");

        sprintf((char *)data, "%s%c,%c", data, 'l', gExtCtx.extBid[1][11]);
        if(gExtCtx.extBid[1][11] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[3].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[3].mgrVer)));
        }
        strcat(data, ":");
		
        if(gExtCtx.extBid[1][12] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'm', gExtCtx.extBid[1][12]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'm', gExtCtx.extBid[1][12]);
		
        strcat(data, ":");
		if(gExtCtx.extBid[1][13] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'n', gExtCtx.extBid[1][13]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'n', gExtCtx.extBid[1][13]);
    }
    else
    {
        if(slot >= 'a' && slot <= 'j')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[1][slot - 'a']);
            if(gExtCtx.extBid[1][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + slot - 'a'].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[10 + slot - 'a'].outVer)));
                sprintf((char *)data, "%s,%s,%s",
                        data,
                        gExtCtx.out[10 + slot - 'a'].outSsm,
                        gExtCtx.out[10 + slot - 'a'].outSignal);
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[10 + slot - 'a'].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[10 + slot - 'a'].outPR)));
            }
        }
        else if(slot == 'k' || slot == 'l')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[1][slot - 'a']);
            if(gExtCtx.extBid[1][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,,%d",
                        data,
                        ((0 == strlen(gExtCtx.mgr[2 + slot - 'k'].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[2 + slot - 'k'].mgrVer)),
                        gExtCtx.mgr[2 + slot - 'k'].mgrPR);
            }
        }
        else if(slot == 'm' || slot == 'n')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[1][slot - 'a']);
        }
        else
        {

        }
    }

    sprintf((char *)data, "%s\"%c%c/%c%c", data, CR, LF, CR, LF);
}




void get_ext3_eqt(char slot, unsigned char *data)
{
    int i, num;

    num = (int)(slot - 'a');
	sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
		
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 10; i++)
        {
            //
            //! 盘类型
            //
            sprintf((char *)data, "%s%c,%c", data, (char)('a' + i), gExtCtx.extBid[2][i]);
            //
            //! 盘在线，上报
            //
			if(gExtCtx.extBid[2][i] != 'O')
            {
                //
                //!版本信息
                //
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + i].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[20 + i].outVer)));
                //
                //!模式和输出状态
                //
				sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + i].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[20 + i].outPR)));
            }

            strcat(data, ":");
        }

		//
		//!MGE单盘在线状态和版本
		//
        sprintf((char *)data, "%s%c,%c", data, 'k', gExtCtx.extBid[2][10]);
        if(gExtCtx.extBid[2][10] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[4].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[4].mgrVer)));
        }
        strcat(data, ":");

        sprintf((char *)data, "%s%c,%c", data, 'l', gExtCtx.extBid[2][11]);
        if(gExtCtx.extBid[2][11] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[5].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[5].mgrVer)));
        }
        strcat(data, ":");
        //
        //!电源单盘在线状态
        //
        if(gExtCtx.extBid[2][12] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'm', gExtCtx.extBid[2][12]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'm', gExtCtx.extBid[2][12]);
		
        strcat(data, ":");

		if(gExtCtx.extBid[2][13] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'n', gExtCtx.extBid[2][13]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'n', gExtCtx.extBid[2][13]);
    }
    else
    {
        //
        //!slot 
        //
        if(slot >= 'a' && slot <= 'j')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[2][slot - 'a']);
            if(gExtCtx.extBid[2][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + slot - 'a'].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[20 + slot - 'a'].outVer)));
                sprintf((char *)data, "%s,%s,%s",
                        data,
                        gExtCtx.out[20 + slot - 'a'].outSsm,
                        gExtCtx.out[20 + slot - 'a'].outSignal);
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + slot - 'a'].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[20 + slot - 'a'].outPR)));
            }
        }
        else if(slot == 'k' || slot == 'l')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[2][slot - 'a']);
            if(gExtCtx.extBid[2][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,,%d",
                        data,
                        ((0 == strlen(gExtCtx.mgr[4 + slot - 'k'].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[4 + slot - 'k'].mgrVer)),
                        gExtCtx.mgr[4 + slot - 'k'].mgrPR);
            }
        }
        else if(slot == 'm' || slot == 'n')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[2][slot - 'a']);
        }
        else
        {

        }
    }

    sprintf((char *)data, "%s\"%c%c;", data, CR, LF);
}

void get_exta3_eqt(char slot, unsigned char *data)
{
    int i, num;

    num = (int)(slot - 'a');
    sprintf((char *)data, "%s%c%c%c\"", data, SP, SP, SP);
    if((slot == ':') || (slot == ' '))
    {
        for(i = 0; i < 10; i++)
        {
            //
            //! 盘类型
            //
            sprintf((char *)data, "%s%c,%c", data, (char)('a' + i), gExtCtx.extBid[2][i]);
            //
            //! 盘在线，上报
            //
			if(gExtCtx.extBid[2][i] != 'O')
            {
                //
                //!版本信息
                //
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + i].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[20 + i].outVer)));
                //
                //!模式和输出状态
                //
				sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + i].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[20 + i].outPR)));
            }

            strcat(data, ":");
        }

		//
		//!MGE单盘在线状态和版本
		//
        sprintf((char *)data, "%s%c,%c", data, 'k', gExtCtx.extBid[2][10]);
        if(gExtCtx.extBid[2][10] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[4].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[4].mgrVer)));
        }
        strcat(data, ":");

        sprintf((char *)data, "%s%c,%c", data, 'l', gExtCtx.extBid[2][11]);
        if(gExtCtx.extBid[2][11] != 'O')
        {
            sprintf((char *)data, "%s,%s,,",
                    data,
                    ((0 == strlen(gExtCtx.mgr[5].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[5].mgrVer)));
        }
        strcat(data, ":");
        //
        //!电源单盘在线状态
        //
        if(gExtCtx.extBid[2][12] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'm', gExtCtx.extBid[2][12]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'm', gExtCtx.extBid[2][12]);
        strcat(data, ":");
		if(gExtCtx.extBid[2][13] == 'O')
        	sprintf((char *)data, "%s%c,%c", data, 'n', gExtCtx.extBid[2][13]);
		else
			sprintf((char *)data, "%s%c,%c,,,,", data, 'n', gExtCtx.extBid[2][13]);
    }
    else
    {
        //
        //!slot 
        //
        if(slot >= 'a' && slot <= 'j')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[2][slot - 'a']);
            if(gExtCtx.extBid[2][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + slot - 'a'].outVer)) ? (char *)(",") : (char *)(gExtCtx.out[20 + slot - 'a'].outVer)));
                sprintf((char *)data, "%s,%s,%s",
                        data,
                        gExtCtx.out[20 + slot - 'a'].outSsm,
                        gExtCtx.out[20 + slot - 'a'].outSignal);
                sprintf((char *)data, "%s,%s",
                        data,
                        ((0 == strlen(gExtCtx.out[20 + slot - 'a'].outPR)) ? (char *)(",") : (char *)(gExtCtx.out[20 + slot - 'a'].outPR)));
            }
        }
        else if(slot == 'k' || slot == 'l')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[2][slot - 'a']);
            if(gExtCtx.extBid[2][slot - 'a'] != 'O')
            {
                sprintf((char *)data, "%s,%s,,,%d",
                        data,
                        ((0 == strlen(gExtCtx.mgr[4 + slot - 'k'].mgrVer)) ? (char *)(",") : (char *)(gExtCtx.mgr[4 + slot - 'k'].mgrVer)),
                        gExtCtx.mgr[4 + slot - 'k'].mgrPR);
            }
        }
        else if(slot == 'm' || slot == 'n')
        {
            sprintf((char *)data, "%s%c,%c", data, slot, gExtCtx.extBid[2][slot - 'a']);
        }
        else
        {

        }
    }

    sprintf((char *)data, "%s\"%c%c;", data, CR, LF);
}









void rpt_rb(char slot, unsigned char *data)
{
    /*“<aid>,<type>,[<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
    <ref>,<priority>,<mask>,<leapmask>,<leap>,<leaptag>,
    [<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],
    <we>,<out>,<type-work>,<msmode>,<trmode>”*/
    int i, flag_1, flag_2;
    unsigned char tmp[3];
	unsigned char sel_ref_mod;
    memset(tmp, 0, 3);
	
    if(slot == RB1_SLOT)
    {
        if(slot_type[14] == 'K' || slot_type[14] == 'R')
        {
            sel_ref_mod = (conf_content.slot_o.sys_ref == '0')? '0':'1';
            flag_1 = strlen(rpt_content.slot_o.sy_ver);
            flag_2 = strlen(rpt_content.slot_o.rf_tzo);

            if(rpt_content.slot_o.rb_ref == '\0' || rpt_content.slot_o.rb_ref == 0xff)
            {
                tmp[0] = ',';
                tmp[1] = '\0';
            }
            else
            {
                tmp[0] = rpt_content.slot_o.rb_ref;
                tmp[1] = ',';
                tmp[2] = '\0';
            }
            sprintf((char *)data, "%s%c,%c,%s,%s",
                    data, slot, slot_type[14], flag_1 == 0 ? (unsigned char *)COMMA_3 : rpt_content.slot_o.sy_ver,
                    tmp);


            sprintf((char *)data, "%s%s,%s,%s,%s,1,",
                    data, rpt_content.slot_o.rb_prio, rpt_content.slot_o.rb_mask,
                    rpt_content.slot_o.rb_leapmask, rpt_content.slot_o.rb_leap
                   );
            for(i = 0; i < 8; i++)
            {
                sprintf((char *)data, "%s%s,", data, rpt_content.slot_o.rf_dey[i]);
            }

            //20140915
            if(flag_alm[14][4] == 0)
            {
                if('g' == rpt_content.slot_o.rb_tl_2mhz)
                {
                    rpt_content.slot_o.rb_tl_2mhz = 'f';
                }
                else
                {
                    ;
                }
            }
            else
            {
                rpt_content.slot_o.rb_tl_2mhz = 'g';
            }

            sprintf((char *)data, "%s%s,%s,%s,%s,%c,%c,%c,%c,%s,%s,%s,%s,%c,0",
                    data, (flag_2 == 0) ? (unsigned char *)COMMA_1 : rpt_content.slot_o.rf_tzo,
                    rpt_content.slot_o.rb_typework,
                    rpt_content.slot_o.rb_msmode,
                    rpt_content.slot_o.rb_trmode,
                    conf_content.slot_u.fb,
                    rpt_content.slot_o.rb_sa,
                    rpt_content.slot_o.rb_tl,
                    rpt_content.slot_o.rb_tl_2mhz,
                    conf_content.slot_o.thresh[0],
                    conf_content.slot_o.thresh[1],
                    rpt_content.slot_o.rb_phase[0],
                    rpt_content.slot_o.rb_phase[1],
                    sel_ref_mod
                    );
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(slot_type[15] == 'K' || slot_type[15] == 'R')
        {
           sel_ref_mod = (conf_content.slot_p.sys_ref == '0')? '0':'1';

            /*	<responsemessage>::=“<aid>,<type>,[<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
            <ref>,<priority>,<mask>,<leapmask>,<leap>,<leaptag>,
            [<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],
            <we>,<out>,<type-work>,<msmode>,<trmode>”*/
            flag_1 = strlen(rpt_content.slot_p.sy_ver);
            flag_2 = strlen(rpt_content.slot_p.rf_tzo);
            if(rpt_content.slot_p.rb_ref == '\0' || rpt_content.slot_p.rb_ref == 0xff)
            {
                tmp[0] = ',';
                tmp[1] = '\0';
            }
            else
            {
                tmp[0] = rpt_content.slot_p.rb_ref;
                tmp[1] = ',';
                tmp[2] = '\0';
            }
            sprintf((char *)data, "%s%c,%c,%s,%s",
                    data, slot, slot_type[15], (flag_1 == 0) ? (unsigned char *)COMMA_3 : rpt_content.slot_p.sy_ver,
                    tmp);
            /*	if(rpt_content.slot_p.rb_leaptag=='\0'||rpt_content.slot_p.rb_leaptag==0xff)
            		{
            			tmp[0]=',';
            			tmp[1]='\0';
            		}
            	else
            		{
            			tmp[0]=rpt_content.slot_o.rb_leaptag;
            			tmp[1]=',';
            			tmp[2]='\0';
            		}*/
            sprintf((char *)data, "%s%s,%s,%s,%s,1,",
                    data, rpt_content.slot_p.rb_prio, rpt_content.slot_p.rb_mask,
                    rpt_content.slot_p.rb_leapmask, rpt_content.slot_p.rb_leap);
            for(i = 0; i < 8; i++)
            {
                sprintf((char *)data, "%s%s,", data, rpt_content.slot_p.rf_dey[i]);
            }

            //20140915
            if(flag_alm[15][4] == 0)
            {
                if('g' == rpt_content.slot_p.rb_tl_2mhz)
                {
                    rpt_content.slot_p.rb_tl_2mhz = 'f';
                }
                else
                {
                    ;
                }
            }
            else
            {
                rpt_content.slot_p.rb_tl_2mhz = 'g';
            }
            sprintf((char *)data, "%s%s,%s,%s,%s,%c,%c,%c,%c,%s,%s,%s,%s,%c,0",
                    data, (flag_2 == 0) ? (unsigned char *)COMMA_1 : rpt_content.slot_p.rf_tzo,
                    rpt_content.slot_p.rb_typework,
                    rpt_content.slot_p.rb_msmode,
                    rpt_content.slot_p.rb_trmode,
                    conf_content.slot_u.fb,
                    rpt_content.slot_p.rb_sa,
                    rpt_content.slot_p.rb_tl,
                    rpt_content.slot_p.rb_tl_2mhz,
                    conf_content.slot_p.thresh[0],
                    conf_content.slot_p.thresh[1],
                    rpt_content.slot_p.rb_phase[0],
                    rpt_content.slot_p.rb_phase[1],
                    sel_ref_mod);
        }
    }

}


void rpt_gbtp(char slot, unsigned char *data)
{
    /* “<aid>,<type>,[<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
    <ppssta>,<sourcetype>,<pos_mode>,<elevation>, <format>, <acmode>,
    <mask>, <qq-fix>,<hhmmss.ss>,<ddmm.mmmmmm>,<ns>,<dddmm.mmmmmm>,<we>,
    <saaaaa.aa>,
    <vvvv>,<tttttttttt>[,<sat>,<qq>,<pp>,<ss>,<h>]*”*/
    int flag_1, flag_2, flag_3, flag_4, flag_5, flag_6, flag_7, flag_8, flag_9, flag_10, flag_11;

    if(slot == GBTP1_SLOT)
    {
        if(slot_type[16] == 'L')
        {
            flag_1 = strlen(rpt_content.slot_q.sy_ver);
            flag_2 = strlen(rpt_content.slot_q.gb_tod);
            flag_3 = strlen(rpt_content.slot_q.gb_ori);
            flag_4 = strlen(rpt_content.slot_q.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_q.gb_rev_b);
            sprintf((char *) data, "%s%c,L,%s,%s,%s,%s,", data, slot,
                     (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                     rpt_content.slot_q.bdzb,
                     (flag_2 == 0) ? (unsigned char *)COMMA_1 : (rpt_content.slot_q.gb_tod),
                     rpt_content.slot_q.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_q.gb_elevation,
                    rpt_content.slot_q.gb_format, rpt_content.slot_q.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_q.gb_mask,
                    (flag_3 == 0) ? (unsigned char *)COMMA_8 : (rpt_content.slot_q.gb_ori),
                    conf_content.slot_u.fb,
                    (flag_4 == 0) ? (unsigned char *)GBTP_G : (rpt_content.slot_q.gb_rev_g),
                    (flag_5 == 0) ? (unsigned char *)GBTP_B : (rpt_content.slot_q.gb_rev_b));
        }
        else if (slot_type[16] == 'P')
        {
            flag_1 = strlen(rpt_content.slot_q.sy_ver);
            flag_2 = strlen(rpt_content.slot_q.gb_tod);
            flag_3 = strlen(rpt_content.slot_q.gb_ori);
            flag_4 = strlen(rpt_content.slot_q.gb_rev_g);
            sprintf((char *) data, "%s%c,P,%s,,%s,%s,", data, slot,
                     (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                     (flag_2 == 0) ? (unsigned char *)COMMA_1 : (rpt_content.slot_q.gb_tod),
                     rpt_content.slot_q.gb_pos_mode);
            sprintf((char *) data, "%s%s,%s,%s,", data, rpt_content.slot_q.gb_elevation,
                     rpt_content.slot_q.gb_format, rpt_content.slot_q.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s", data, rpt_content.slot_q.gb_mask,
                    (flag_3 == 0) ? (unsigned char *)COMMA_8 : (rpt_content.slot_q.gb_ori),
                    conf_content.slot_u.fb,
                    (flag_4 == 0) ? (unsigned char *)GBTP_G : (rpt_content.slot_q.gb_rev_g));
        }
        else if(slot_type[16] == 'e')
        {
            flag_1 = strlen(rpt_content.slot_q.sy_ver);
            flag_2 = strlen(rpt_content.slot_q.gb_tod);
            flag_3 = strlen(rpt_content.slot_q.gb_ori);
            flag_4 = strlen(rpt_content.slot_q.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_q.gb_rev_b);
            sprintf((char *)data, "%s%c,e,%s,%s,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                    rpt_content.slot_q.bdzb,
                    (flag_2 == 0) ? (unsigned char *)COMMA_1 : (rpt_content.slot_q.gb_tod),
                    rpt_content.slot_q.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_q.gb_elevation,
                    rpt_content.slot_q.gb_format, rpt_content.slot_q.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_q.gb_mask,
                    (flag_3 == 0) ? (unsigned char *)COMMA_8 : (rpt_content.slot_q.gb_ori),
                    conf_content.slot_u.fb,
                    (flag_4 == 0) ? (unsigned char *)GBTP_G : (rpt_content.slot_q.gb_rev_g),
                    (flag_5 == 0) ? (unsigned char *)GBTP_B : (rpt_content.slot_q.gb_rev_b));
        }
        else if(slot_type[16] == 'g')
        {
            flag_1 = strlen(rpt_content.slot_q.sy_ver);
            flag_2 = strlen(rpt_content.slot_q.gb_tod);
            flag_3 = strlen(rpt_content.slot_q.gb_ori);
            flag_4 = strlen(rpt_content.slot_q.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_q.gb_rev_b);
            sprintf((char *)data, "%s%c,g,%s,%s,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                    rpt_content.slot_q.bdzb,
                    (flag_2 == 0) ? (unsigned char *)COMMA_1 : (rpt_content.slot_q.gb_tod),
                    rpt_content.slot_q.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_q.gb_elevation,
                    rpt_content.slot_q.gb_format, rpt_content.slot_q.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_q.gb_mask,
                    (flag_3 == 0) ? (unsigned char *)COMMA_8 : (rpt_content.slot_q.gb_ori),
                    conf_content.slot_u.fb,
                    (flag_4 == 0) ? (unsigned char *)GBTP_G : (rpt_content.slot_q.gb_rev_g),
                    (flag_5 == 0) ? (unsigned char *)GBTP_B : (rpt_content.slot_q.gb_rev_b));
        }

        else if(slot_type[16] == 'l')
        {
            flag_1 = strlen(rpt_content.slot_q.sy_ver);
            flag_2 = strlen(rpt_content.slot_q.gb_tod);
            flag_3 = strlen(rpt_content.slot_q.gb_ori);
            flag_4 = strlen(rpt_content.slot_q.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_q.gb_rev_b);
            sprintf((char *)data, "%s%c,l,%s,%s,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                    rpt_content.slot_q.bdzb,
                    (flag_2 == 0) ? (unsigned char *)COMMA_1 : (rpt_content.slot_q.gb_tod),
                    rpt_content.slot_q.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_q.gb_elevation,
                    rpt_content.slot_q.gb_format, rpt_content.slot_q.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_q.gb_mask,
                    (flag_3 == 0) ? (unsigned char *)COMMA_8 : (rpt_content.slot_q.gb_ori),
                    conf_content.slot_u.fb,
                    (flag_4 == 0) ? (unsigned char *)GBTP_G : (rpt_content.slot_q.gb_rev_g),
                    (flag_5 == 0) ? (unsigned char *)GBTP_B : (rpt_content.slot_q.gb_rev_b));
        }
		else if(slot_type[16] == 'v')
		{
			flag_1 = strlen(rpt_content.slot_q.sy_ver);
			flag_2 = (rpt_content.slot_q.gb_sta == '\0') ? 0:1;
			flag_3 = (rpt_content.slot_q.staen == '\0') ? 0:1;
            //flag_4 =  strlen(rpt_content.slot_q.gb_pos_mode);
            //flag_5 =  strlen(rpt_content.slot_q.gb_acmode);
			flag_6 =  rpt_content.slot_q.gb_tod[0] == '\0' ? 0:1;
			flag_7 =  strlen(rpt_content.slot_q.leapalm);
			flag_8 =  strlen(rpt_content.slot_q.leapnum);
			
			flag_9 = strlen(rpt_content.slot_q.gb_ori);
			flag_10 = strlen(rpt_content.slot_q.gb_rev_g);
            flag_11 = strlen(rpt_content.slot_q.gb_rev_b);
			sprintf((char *)data, "%s%c,v,%s,%c,%c,%s,%s,%c,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                    (flag_2 == 0) ? '2' : rpt_content.slot_q.gb_sta, 
                    (flag_3 == 0) ? '1' : rpt_content.slot_q.staen,
                    rpt_content.slot_q.gb_pos_mode,
                    rpt_content.slot_q.gb_acmode,
                    (flag_6 == 0) ? '3' : rpt_content.slot_q.gb_tod[0],
                    (flag_7 == 0) ? "00": (char *)rpt_content.slot_q.leapalm,
                    (flag_8 == 0) ? "00": (char *)rpt_content.slot_q.leapnum);
			
			sprintf((char *)data, "%s%s,%s,%s,%s,%s", data, 
				    ((flag_9 == 0) ? (unsigned char *)COMMA_9 : rpt_content.slot_q.gb_ori),
                    rpt_content.slot_q.rec_type,
                    rpt_content.slot_q.rec_ver,
                    ((flag_10 == 0) ? (unsigned char *)GBTP_GPS : rpt_content.slot_q.gb_rev_g),
                    ((flag_11 == 0) ? (unsigned char *)GBTP_BD : rpt_content.slot_q.gb_rev_b));
		         
		}
		else if(slot_type[16] == 'x')
		{
			flag_1 = strlen(rpt_content.slot_q.sy_ver);
			flag_2 = (rpt_content.slot_q.gb_sta == '\0') ? 0:1;
			flag_3 = (rpt_content.slot_q.staen == '\0') ? 0:1;
            //flag_4 =  strlen(rpt_content.slot_q.gb_pos_mode);
            //flag_5 =  strlen(rpt_content.slot_q.gb_acmode);
            //flag_4 =  strlen(rpt_content.slot_q.delay);
			
			flag_6 =  rpt_content.slot_q.gb_tod[0] == '\0' ? 0:1;
			flag_7 =  strlen(rpt_content.slot_q.leapalm);
			flag_8 =  strlen(rpt_content.slot_q.leapnum);
			
			flag_9 = strlen(rpt_content.slot_q.gb_ori);
			flag_10 = strlen(rpt_content.slot_q.gb_rev_g);
            flag_11 = strlen(rpt_content.slot_q.gb_rev_b);
			flag_4 = strlen(rpt_content.slot_q.gb_rev_glo);
            flag_5 = strlen(rpt_content.slot_q.gb_rev_gal);
			sprintf((char *)data, "%s%c,x,%s,%c,%c,%s,%s,%s,%s,%c,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_q.sy_ver),
                    (flag_2 == 0) ? '2' : rpt_content.slot_q.gb_sta, 
                    (flag_3 == 0) ? '1' : rpt_content.slot_q.staen,
                    rpt_content.slot_q.gb_pos_mode,
                    rpt_content.slot_q.gb_acmode,
                    rpt_content.slot_q.delay,
                     rpt_content.slot_q.pps_out_delay,
                    (flag_6 == 0) ? '3' : rpt_content.slot_q.gb_tod[0],
                    (flag_7 == 0) ? "00": (char *)rpt_content.slot_q.leapalm,
                    (flag_8 == 0) ? "00": (char *)rpt_content.slot_q.leapnum);
			
			sprintf((char *)data, "%s%s,%s,%s,%s,%s,%s,%s", data, 
				    ((flag_9 == 0) ? (unsigned char *)COMMA_9 : rpt_content.slot_q.gb_ori),
                    rpt_content.slot_q.rec_type,
                    rpt_content.slot_q.rec_ver,
                    ((flag_10 == 0) ? (unsigned char *)GBTP_GPS : rpt_content.slot_q.gb_rev_g),
                    ((flag_11 == 0) ? (unsigned char *)GBTP_BD : rpt_content.slot_q.gb_rev_b),
                    ((flag_4 == 0) ? (unsigned char *)GBTP_GLO : rpt_content.slot_q.gb_rev_glo),
                    ((flag_5 == 0) ? (unsigned char *)GBTP_GAL : rpt_content.slot_q.gb_rev_gal));
		         
		}
    }
    else if(slot == GBTP2_SLOT)
    {
        if(slot_type[17] == 'L')
        {
            flag_1 = strlen(rpt_content.slot_r.sy_ver);
            flag_2 = strlen(rpt_content.slot_r.gb_tod);
            flag_3 = strlen(rpt_content.slot_r.gb_ori);
            flag_4 = strlen(rpt_content.slot_r.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_r.gb_rev_b);
            sprintf((char *)data, "%s%c,L,%s,%s,%s,%s,", data, slot,
                    flag_1 == 0 ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    rpt_content.slot_r.bdzb,
                    flag_2 == 0 ? (unsigned char *)COMMA_1 : (rpt_content.slot_r.gb_tod),
                    rpt_content.slot_r.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_r.gb_elevation,
                    rpt_content.slot_r.gb_format, rpt_content.slot_r.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_r.gb_mask,
                    flag_3 == 0 ? (unsigned char *)COMMA_8 : (rpt_content.slot_r.gb_ori),
                    conf_content.slot_u.fb,
                    flag_4 == 0 ? (unsigned char *)GBTP_G : (rpt_content.slot_r.gb_rev_g),
                    flag_5 == 0 ? (unsigned char *)GBTP_B : (rpt_content.slot_r.gb_rev_b));

        }
        else if(slot_type[17] == 'P')
        {
            flag_1 = strlen(rpt_content.slot_r.sy_ver);
            flag_2 = strlen(rpt_content.slot_r.gb_tod);
            flag_3 = strlen(rpt_content.slot_r.gb_ori);
            flag_4 = strlen(rpt_content.slot_r.gb_rev_g);
            sprintf((char *)data, "%s%c,P,%s,,%s,%s,", data, slot,
                    flag_1 == 0 ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    flag_2 == 0 ? (unsigned char *)COMMA_1 : (rpt_content.slot_r.gb_tod),
                    rpt_content.slot_r.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_r.gb_elevation,
                    rpt_content.slot_r.gb_format, rpt_content.slot_r.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s", data, rpt_content.slot_r.gb_mask,
                    flag_3 == 0 ? (unsigned char *)COMMA_8 : (rpt_content.slot_r.gb_ori),
                    conf_content.slot_u.fb,
                    flag_4 == 0 ? (unsigned char *)GBTP_G : (rpt_content.slot_r.gb_rev_g));
        }
        else if(slot_type[17] == 'e')
        {
            flag_1 = strlen(rpt_content.slot_r.sy_ver);
            flag_2 = strlen(rpt_content.slot_r.gb_tod);
            flag_3 = strlen(rpt_content.slot_r.gb_ori);
            flag_4 = strlen(rpt_content.slot_r.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_r.gb_rev_b);
            sprintf((char *)data, "%s%c,e,%s,%s,%s,%s,", data, slot,
                    flag_1 == 0 ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    rpt_content.slot_r.bdzb,
                    flag_2 == 0 ? (unsigned char *)COMMA_1 : (rpt_content.slot_r.gb_tod),
                    rpt_content.slot_r.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_r.gb_elevation,
                    rpt_content.slot_r.gb_format, rpt_content.slot_r.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_r.gb_mask,
                    flag_3 == 0 ? (unsigned char *)COMMA_8 : (rpt_content.slot_r.gb_ori),
                    conf_content.slot_u.fb,
                    flag_4 == 0 ? (unsigned char *)GBTP_G : (rpt_content.slot_r.gb_rev_g),
                    flag_5 == 0 ? (unsigned char *)GBTP_B : (rpt_content.slot_r.gb_rev_b));
        }
        else if(slot_type[17] == 'g')
        {
            flag_1 = strlen(rpt_content.slot_r.sy_ver);
            flag_2 = strlen(rpt_content.slot_r.gb_tod);
            flag_3 = strlen(rpt_content.slot_r.gb_ori);
            flag_4 = strlen(rpt_content.slot_r.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_r.gb_rev_b);
            sprintf((char *)data, "%s%c,g,%s,%s,%s,%s,", data, slot,
                    flag_1 == 0 ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    rpt_content.slot_r.bdzb,
                    flag_2 == 0 ? (unsigned char *)COMMA_1 : (rpt_content.slot_r.gb_tod),
                    rpt_content.slot_r.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_r.gb_elevation,
                    rpt_content.slot_r.gb_format, rpt_content.slot_r.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_r.gb_mask,
                    flag_3 == 0 ? (unsigned char *)COMMA_8 : (rpt_content.slot_r.gb_ori),
                    conf_content.slot_u.fb,
                    flag_4 == 0 ? (unsigned char *)GBTP_G : (rpt_content.slot_r.gb_rev_g),
                    flag_5 == 0 ? (unsigned char *)GBTP_B : (rpt_content.slot_r.gb_rev_b));
        }
        else if(slot_type[17] == 'l')
        {
            flag_1 = strlen(rpt_content.slot_r.sy_ver);
            flag_2 = strlen(rpt_content.slot_r.gb_tod);
            flag_3 = strlen(rpt_content.slot_r.gb_ori);
            flag_4 = strlen(rpt_content.slot_r.gb_rev_g);
            flag_5 = strlen(rpt_content.slot_r.gb_rev_b);
            sprintf((char *)data, "%s%c,l,%s,%s,%s,%s,", data, slot,
                    flag_1 == 0 ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    rpt_content.slot_r.bdzb,
                    flag_2 == 0 ? (unsigned char *)COMMA_1 : (rpt_content.slot_r.gb_tod),
                    rpt_content.slot_r.gb_pos_mode);
            sprintf((char *)data, "%s%s,%s,%s,", data, rpt_content.slot_r.gb_elevation,
                    rpt_content.slot_r.gb_format, rpt_content.slot_r.gb_acmode);
            sprintf((char *)data, "%s%s,%s,%c,%s,%s", data, rpt_content.slot_r.gb_mask,
                    flag_3 == 0 ? (unsigned char *)COMMA_8 : (rpt_content.slot_r.gb_ori),
                    conf_content.slot_u.fb,
                    flag_4 == 0 ? (unsigned char *)GBTP_G : (rpt_content.slot_r.gb_rev_g),
                    flag_5 == 0 ? (unsigned char *)GBTP_B : (rpt_content.slot_r.gb_rev_b));
        }
		else if(slot_type[17] == 'v')
		{
			flag_1 = strlen(rpt_content.slot_r.sy_ver);
			flag_2 = (rpt_content.slot_r.gb_sta == '\0') ? 0:1;
			flag_3 = (rpt_content.slot_r.staen == '\0') ? 0:1;
            //flag_4 =  strlen(rpt_content.slot_q.gb_pos_mode);
            //flag_5 =  strlen(rpt_content.slot_q.gb_acmode);
			flag_6 =  rpt_content.slot_r.gb_tod[0] == '\0' ? 0:1;
			flag_7 =  strlen(rpt_content.slot_r.leapalm);
			flag_8 =  strlen(rpt_content.slot_r.leapnum);
			
			flag_9 = strlen(rpt_content.slot_r.gb_ori);
			flag_10 = strlen(rpt_content.slot_r.gb_rev_g);
            flag_11 = strlen(rpt_content.slot_r.gb_rev_b);
			sprintf((char *)data, "%s%c,v,%s,%c,%c,%s,%s,%c,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    (flag_2 == 0) ? '2' : rpt_content.slot_r.gb_sta, 
                    (flag_3 == 0) ? '1' : rpt_content.slot_r.staen,
                    rpt_content.slot_r.gb_pos_mode,
                    rpt_content.slot_r.gb_acmode,
                    (flag_6 == 0) ? '3' : rpt_content.slot_r.gb_tod[0],
                    (flag_7 == 0) ? "00": (char *)rpt_content.slot_r.leapalm,
                    (flag_8 == 0) ? "00": (char *)rpt_content.slot_r.leapnum);
			
			sprintf((char *)data, "%s%s,%s,%s,%s,%s", data, 
				    ((flag_9 == 0) ? (unsigned char *)COMMA_9 : rpt_content.slot_r.gb_ori),
                    rpt_content.slot_r.rec_type,
                    rpt_content.slot_r.rec_ver,
                    ((flag_10 == 0) ? (unsigned char *)GBTP_GPS : rpt_content.slot_r.gb_rev_g),
                    ((flag_11 == 0) ? (unsigned char *)GBTP_BD : rpt_content.slot_r.gb_rev_b));
		         
		}
		else if(slot_type[17] == 'x')
		{
			flag_1 = strlen(rpt_content.slot_r.sy_ver);
			flag_2 = (rpt_content.slot_r.gb_sta == '\0') ? 0:1;
			flag_3 = (rpt_content.slot_r.staen == '\0') ? 0:1;
            //flag_4 =  strlen(rpt_content.slot_q.gb_pos_mode);
            //flag_5 =  strlen(rpt_content.slot_q.gb_acmode);
            //flag_4 =  strlen(rpt_content.slot_q.delay);
			
			flag_6 =  rpt_content.slot_r.gb_tod[0] == '\0' ? 0:1;
			flag_7 =  strlen(rpt_content.slot_r.leapalm);
			flag_8 =  strlen(rpt_content.slot_r.leapnum);
			
			flag_9 = strlen(rpt_content.slot_r.gb_ori);
			flag_10 = strlen(rpt_content.slot_r.gb_rev_g);
            flag_11 = strlen(rpt_content.slot_r.gb_rev_b);
			flag_4 = strlen(rpt_content.slot_r.gb_rev_glo);
            flag_5 = strlen(rpt_content.slot_r.gb_rev_gal);
			sprintf((char *)data, "%s%c,x,%s,%c,%c,%s,%s,%s,%s,%c,%s,%s,", data, slot,
                    (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot_r.sy_ver),
                    (flag_2 == 0) ? '2' : rpt_content.slot_r.gb_sta, 
                    (flag_3 == 0) ? '1' : rpt_content.slot_r.staen,
                    rpt_content.slot_r.gb_pos_mode,
                    rpt_content.slot_r.gb_acmode,
                    rpt_content.slot_r.delay,
                    rpt_content.slot_r.pps_out_delay,
                    (flag_6 == 0) ? '3' : rpt_content.slot_r.gb_tod[0],
                    (flag_7 == 0) ? "00": (char *)rpt_content.slot_r.leapalm,
                    (flag_8 == 0) ? "00": (char *)rpt_content.slot_r.leapnum);
			
			sprintf((char *)data, "%s%s,%s,%s,%s,%s,%s,%s", data, 
				    ((flag_9 == 0) ? (unsigned char *)COMMA_9 : rpt_content.slot_r.gb_ori),
                    rpt_content.slot_r.rec_type,
                    rpt_content.slot_r.rec_ver,
                    ((flag_10 == 0) ? (unsigned char *)GBTP_GPS : rpt_content.slot_r.gb_rev_g),
                    ((flag_11 == 0) ? (unsigned char *)GBTP_BD : rpt_content.slot_r.gb_rev_b),
                    ((flag_4 == 0) ? (unsigned char *)GBTP_GLO : rpt_content.slot_r.gb_rev_glo),
                    ((flag_5 == 0) ? (unsigned char *)GBTP_GAL : rpt_content.slot_r.gb_rev_gal));
			}
		         
		
    }
}

void get_sta1_ver(unsigned char *data, int num)
{
    //sprintf((char *)data,"%s,%s,%s,,1,JLABAE@C,JLABAE@A,OOOOOO@@,,,",data,slot,"V01.00","V01.00");

	if(NULL != strchr(rpt_content.slot[num].sy_ver,','))
	{
		sprintf((char *)data, "%s,%s1", data, rpt_content.slot[num].sy_ver);
	}
	else
	{
		sprintf((char *)data, "%s,,,,1", data);
	}
    


    if( (( (conf_content3.sta1.ip[0] ) & 0xf0 ) != 0x40) ||
            (((conf_content3.sta1.ip[2] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.ip[4] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.ip[6] ) & 0xf0) != 0x40) )
    {

        memset(conf_content3.sta1.ip, 0, 9);
        memcpy(conf_content3.sta1.ip, "L@JH@AFC", 8);
    }

    if( (((conf_content3.sta1.gate[0] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.gate[2] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.gate[4] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.gate[6] ) & 0xf0) != 0x40) )
    {

        memset(conf_content3.sta1.gate, 0, 9);
        memcpy(conf_content3.sta1.gate, "L@JH@A@A", 8);
    }

    if( (((conf_content3.sta1.mask[0] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.mask[2] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.mask[4] ) & 0xf0) != 0x40) ||
            (((conf_content3.sta1.mask[6] ) & 0xf0) != 0x40) )
    {


        memset(conf_content3.sta1.mask, 0, 9);
        memcpy(conf_content3.sta1.mask, "OOOOOO@@", 8);
    }

    sprintf((char *)data, "%s,%s,%s,%s,,,", data, conf_content3.sta1.ip, conf_content3.sta1.gate, conf_content3.sta1.mask);
    //sprintf((char *)data,"%s,%s,%s,,1,JLABAE@C,JLABAE@A,OOOOOO@@,,,",data,slot,"V01.00","V01.00");
}
void __srtcat(unsigned char *dist,unsigned char *tail)
{
	int res;
	unsigned char comma[2];
	comma[0] = ',';
	comma[1] = 0;
	if(dist == NULL || tail == NULL)
	{
        return ;
    }
	strcat(dist,comma);
	res = strlen(tail);
	if(0 == res)
	{
		//strcat(dist,",");
	}
	else
	{
        strcat(dist,tail);
	}
    
}

void str_2_net(unsigned char net_addr,unsigned char *h_buf,unsigned char *l_buf)
{
	unsigned char h_4bit,l_4bit;
	
	l_4bit = (net_addr & 0x0f) | 0x40;
	h_4bit = ((net_addr & 0xf0) >> 4) | 0x40;
	
	//printf("o:%x\n",net_addr);
	//printf("h:%x,l:%x\n",h_4bit,l_4bit);
	*h_buf = h_4bit;
	*l_buf = l_4bit;
}

void strcat_mac(unsigned char * data,int num,int port)
{
	int i;
	char buf[13];
	memset(buf,0,13);
	for(i=0;i<6;i++)
	{ 
		str_2_net(rpt_content.slot[num].ntp_pmac[port][i],&buf[i*2],&buf[i*2+1]);
	}
	__srtcat(data,buf);
}

void strcat_ip(unsigned char * data,int num,int port)
{
	int i;
	char buf[13];
	memset(buf,0,13);
	for(i=0;i<4;i++)
	{ 
		str_2_net(rpt_content.slot[num].ntp_pip[port][i],&buf[i*2],&buf[i*2+1]);
	}
	__srtcat(data,buf);
}
void strcat_gate(unsigned char * data,int num,int port)
{
	int i;
	char buf[13];
	memset(buf,0,13);
	for(i=0;i<4;i++)
	{ 
		str_2_net(rpt_content.slot[num].ntp_pgateway[port][i],&buf[i*2],&buf[i*2+1]);
	}
	__srtcat(data,buf);
}
void strcat_mask(unsigned char * data,int num,int port)
{
	int i;
	char buf[13];
	memset(buf,0,13);
	for(i=0;i<4;i++)
	{ 
		str_2_net(rpt_content.slot[num].ntp_pnetmask[port][i],&buf[i*2],&buf[i*2+1]);
	}
	__srtcat(data,buf);
}

void rpt_out(char slot, unsigned char *data)
{
    int num, i;
    unsigned char tmp[4];
    unsigned char tmp1[3], tmp2[3], tmp3[3], tmp4[3];
    int flag_1, flag_2;
    unsigned char tmp_buf[4];
    int length = 0;
    num = (int)(slot - 'a');
    if(slot_type[num] == 'I')
    {
        /*	 "<aid>,<type>,
        [<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
        <ppssta>,<sourcetype>,<en>”*/
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        flag_2 = strlen(rpt_content.slot[num].tp16_tod);
        memcpy(tmp_buf, rpt_content.slot[num].tp16_tod, strlen(rpt_content.slot[num].tp16_tod));
		//
        //pps state switch
        //
        if('0' == tmp_buf[0])
        {
            tmp_buf[0] = '1';
        }
        else if('1' == tmp_buf[0])
        {
            tmp_buf[0] = '2';
        }
        else if('2' == tmp_buf[0])
        {
            tmp_buf[0] = '3';
        }
        else if('5' == tmp_buf[0])
        {
            tmp_buf[0] = '4';
        }
        else
        {
            tmp_buf[0] = '3';
        }

        //source type switch
        if('2' == tmp_buf[2])
        {
            tmp_buf[2] = '3';
        }
        else if('3' == tmp_buf[2])
        {
            tmp_buf[2] = '2';
        }
        else
        {
            //do nothing
        }
        //printf("%s\t%s\n",rpt_content.slot[num].tp16_tod,tmp_buf);
        sprintf((char *)data, "%s%c,I,%s,%s,%s", data, slot,
                (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot[num].sy_ver),
                (flag_2 == 0) ? (unsigned char *)COMMA_1 : tmp_buf,
                rpt_content.slot[num].tp16_en);
    }
    else if( (slot_type[num] == 'J') ||
             (slot_type[num] == 'a') ||
             (slot_type[num] == 'b') ||
             (slot_type[num] == 'c') ||
             (slot_type[num] == 'h') ||
             (slot_type[num] == 'i'))
    {
        if((rpt_content.slot[num].out_mode == '0') || (rpt_content.slot[num].out_mode == '1'))
        {}
        else
        {
            rpt_content.slot[num].out_mode = '0';
        }
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,%c,%s,%s,%s,%c,%c", data, slot, slot_type[num],
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver),
                rpt_content.slot[num].out_lev,
                rpt_content.slot[num].out_typ,
                rpt_content.slot[num].out_mode,
                rpt_content.slot[num].out_sta);
    }
    else if(((slot_type[num] > '@') && (slot_type[num] < 'I')) || ((slot_type[num] > 109) && (slot_type[num] < 114)) )//PTP
    {
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,%c,%s", data, slot, slot_type[num],
                (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot[num].sy_ver));
        for(i = 0; i < 4; i++)
        {

            sprintf((char *)data, "%s,%d,%s,%s,%s,%s,%s,%s", data, i + 1,
                    rpt_content.slot[num].ptp_ip[i],
                    rpt_content.slot[num].ptp_mac[i],
                    rpt_content.slot[num].ptp_gate[i],
                    rpt_content.slot[num].ptp_mask[i],
                    rpt_content.slot[num].ptp_dns1[i],
                    rpt_content.slot[num].ptp_dns2[i]);

            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_en[i]);
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_delaytype[i]);
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_multicast[i]);
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_enp[i]);
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_step[i]);
#if 0
            sprintf((char *)data, "%s,%c,%c,%c,%c,%c", data,
                    rpt_content.slot[num].ptp_en[i],
                    rpt_content.slot[num].ptp_delaytype[i],
                    rpt_content.slot[num].ptp_multicast[i],
                    rpt_content.slot[num].ptp_enp[i],
                    rpt_content.slot[num].ptp_step[i]);
#endif
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_sync[i]);
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_announce[i]);
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_delayreq[i]);
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_pdelayreq[i]);
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_delaycom[i]);
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_linkmode[i]);
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_out[i]);
            //2014.12.2
            if(rpt_ptp_mtc.ptp_prio1[num][i][0] != 'A')
            {
                //printf("\nrpt_ptp_mtc.ptp_prio1[%d][%d][0] != A\n",num,i);
                memset(rpt_ptp_mtc.ptp_prio1[num][i], 0, 5);
            }
            if(rpt_ptp_mtc.ptp_prio2[num][i][0] != 'B')
            {
                //printf("\nrpt_ptp_mtc.ptp_prio2[%d][%d][0] != B\n",num,i);
                memset(rpt_ptp_mtc.ptp_prio2[num][i], 0, 5);
            }
            sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_prio1[num][i]);
            sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_prio2[num][i]);
			//sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_dom[num][i]);

#if 0
            sprintf((char *)data, "%s,%s,%s,%s,%s,%s,%c,%c", data,
                    rpt_content.slot[num].ptp_sync[i],
                    rpt_content.slot[num].ptp_announce[i],
                    rpt_content.slot[num].ptp_delayreq[i],
                    rpt_content.slot[num].ptp_pdelayreq[i],
                    rpt_content.slot[num].ptp_delaycom[i],
                    rpt_content.slot[num].ptp_linkmode[i],
                    rpt_content.slot[num].ptp_out[i]);
#endif

        }
		sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ntp_leap);
    }
	else if(slot_type[num] == 'j')
	{
	      flag_1 = strlen(rpt_content.slot[num].sy_ver);
          sprintf((char *)data, "%s%c,%c,%s", data, slot, slot_type[num],
                (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot[num].sy_ver));
		  for(i = 0; i < 4; i++)
          {

            sprintf((char *)data, "%s,%d,%s,%s,%s,%s", data, i + 1,
				    rpt_content.slot[num].ptp_mac[i],
                    rpt_content.slot[num].ptp_ip[i],
                    rpt_content.slot[num].ptp_mask[i],
                    rpt_content.slot[num].ptp_gate[i]
                    );

            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_en[i]);       //PTP Enable
			sprintf((char *)data, "%s,%c", data, rpt_ptp_mtc.esmc_en[num][i]);           //Esmc enable
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_delaytype[i]);//Delay type
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_multicast[i]);//multicast
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_enp[i]);      //enp
            sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_step[i]);     //step
            //
            //!
            //
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_sync[i]);
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_announce[i]);
            sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_delaycom[i]);

            if(rpt_ptp_mtc.ptp_prio1[num][i][0] != 'A')
            {
                //printf("\nrpt_ptp_mtc.ptp_prio1[%d][%d][0] != A\n",num,i);
                memset(rpt_ptp_mtc.ptp_prio1[num][i], 0, 5);
            }
            if(rpt_ptp_mtc.ptp_prio2[num][i][0] != 'B')
            {
                //printf("\nrpt_ptp_mtc.ptp_prio2[%d][%d][0] != B\n",num,i);
                memset(rpt_ptp_mtc.ptp_prio2[num][i], 0, 5);
            }
            sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_prio1[num][i]);
            sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_prio2[num][i]);
			sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_dom[num][i]);  //ptp domain
			sprintf((char *)data, "%s,%s", data, rpt_ptp_mtc.ptp_ver[num][i]);  //pt version
			//
			//!sfp modual .
			//
            sprintf((char *)data, "%s,%s,%s,%s,%s", data, rpt_ptp_mtc.ptp_mtc_ip[num][i],rpt_ptp_mtc.sfp_type[num][i],rpt_ptp_mtc.sfp_oplo[num][i],rpt_ptp_mtc.sfp_ophi[num][i]);  //PTP mtc,sfp type,oplo,ophi
            //
            //!ptp status byte.
            //
            sprintf((char *)data, "%s,%s,%s,%s,%s", data, rpt_ptp_mtc.time_source[num][i],rpt_ptp_mtc.clock_class[num][i],rpt_ptp_mtc.time_acc[num][i],rpt_ptp_mtc.synce_ssm[num][i]);//time source,clock class,time acc,synce
        }
		sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ntp_leap);
		  
	}
	else if(slot_type[num] == 's')
	{
	    flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,%c,%s", data, slot, slot_type[num],
                (flag_1 == 0) ? (unsigned char *)COMMA_3 : (rpt_content.slot[num].sy_ver));
        sprintf((char *)data, "%s,%d,%s,%s,%s", data, 1, 	    rpt_content.slot[num].ptp_ip[0],
                    											rpt_content.slot[num].ptp_gate[0],
                    											rpt_content.slot[num].ptp_mask[0]);
		
        sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_delaytype[0]);
        sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_multicast[0]);
        sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_enp[0]);
        sprintf((char *)data, "%s,%c", data, rpt_content.slot[num].ptp_step[0]);

       
        sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_delayreq[0]);
        sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_pdelayreq[0]);
        sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ptp_clkcls[0]);
		sprintf((char *)data, "%s,%s", data, rpt_content.slot[num].ntp_leap);
     
	}
    else if(slot_type[num] == 'T' || slot_type[num] == 'X')
    {
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,%c,%s,%s,%s", data, slot, slot_type[num],
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver),
                rpt_content.slot[num].rs_en,
                rpt_content.slot[num].rs_tzo);
    }
    else if(slot_type[num] == 'S')
    {
        /*<response message>::= "<aid>,<type>,[<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
        < priority1 >,< priority2>,<mode1>,<port1>,<mode2>,<port2>,
        <tl1>,<tl2>,<tl3>,<tl4>,<tl5>,<tl6>,<tl7>,<tl8>,<ph1 >,
        <ph2>,<ph3 >,<ph4>,<ph5 >,<ph6>,<ph7 >,<ph8>"*/
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,S,%s,", data, slot,
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver));

        flag_1 = strlen(rpt_content.slot[num].ref_mod1);
        flag_2 = strlen(rpt_content.slot[num].ref_mod2);
        sprintf((char *)data, "%s%s,%s,%s,%s,", data,
                rpt_content.slot[num].ref_prio1,
                rpt_content.slot[num].ref_prio2,
                flag_1 == 0 ? (unsigned char *)(COMMA_1) : (rpt_content.slot[num].ref_mod1),
                flag_2 == 0 ? (unsigned char *)(COMMA_1) : (rpt_content.slot[num].ref_mod2));

        flag_1 = strlen(rpt_content.slot[num].ref_ph);
        //	flag_2=strlen(rpt_content.slot[num].ref_tl);
        sprintf((char *)data, "%s%s,%s,%s,%s,%s,%s,%s,%c", data,
                rpt_content.slot[num].ref_typ1,
                rpt_content.slot[num].ref_typ2,
                rpt_content.slot[num].ref_sa,
                rpt_content.slot[num].ref_tl,
                rpt_content.slot[num].ref_en,
                flag_1 == 0 ? (unsigned char *)(COMMA_7) : (rpt_content.slot[num].ref_ph),
                ((0 == strlen(rpt_content.slot[num].ref_ssm_en)) ? (unsigned char *)("00000000") : rpt_content.slot[num].ref_ssm_en),rpt_content.slot[num].ref_2mb_lvlm);
        //		flag_2==0?(unsigned char *)(COMMA_7):(rpt_content.slot[num].ref_tl));
    }
    else if(slot_type[num] == 'U')
    {
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,U,%s,%s", data, slot,
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver),
                rpt_content.slot[num].ppx_mod);
    }
    else if(slot_type[num] == 'V')
    {
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        memset(tmp, 0, 4);
        if((rpt_content.slot[num].tod_br < 0x30 ) || (rpt_content.slot[num].tod_br > 0x37))
        {
            sprintf((char *)tmp, ",%c", rpt_content.slot[num].tod_tzo);
        }
        else
        {
            sprintf((char *)tmp, "%c,%c", rpt_content.slot[num].tod_br, rpt_content.slot[num].tod_tzo);
        }

        sprintf((char *)data, "%s%c,V,%s,%s,%s", data, slot,
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver),
                rpt_content.slot[num].tod_en, tmp);
    }
    else if(slot_type[num] == 'W')
    {
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,W,%s,%s,%s,%s", data, slot,
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver),
                rpt_content.slot[num].igb_en,
                rpt_content.slot[num].igb_rat,
                rpt_content.slot[num].igb_max);
    }
    else if(slot_type[num] == 'Z')
    {
        /*<ip>,<mac>,<gateway>,<netmask>,
        <bcast_en>,<interval>,<tpv_en>,<md5_en>,<ois_en>,
        [<keyid>,<md5key>]+*/
#if 1
        flag_1 = strlen(rpt_content.slot[num].sy_ver);
        sprintf((char *)data, "%s%c,Z,%s,", data, slot,
                flag_1 == 0 ? (unsigned char *)(COMMA_3) : (rpt_content.slot[num].sy_ver));

        if((rpt_content.slot[num].ntp_bcast_en == '1') || (rpt_content.slot[num].ntp_bcast_en == '0'))
        {
            sprintf((char *)tmp1, "%c,", rpt_content.slot[num].ntp_bcast_en);
            tmp1[2] = '\0';
        }
        else
        {
            tmp1[0] = ',';
            tmp1[1] = '\0';
        }
        if((rpt_content.slot[num].ntp_tpv_en >= '@') && (rpt_content.slot[num].ntp_tpv_en < 'H'))
        {
            sprintf((char *)tmp2, "%c,", rpt_content.slot[num].ntp_tpv_en);
            tmp2[2] = '\0';
        }
        else
        {
            tmp2[0] = ',';
            tmp2[1] = '\0';
        }
        if((rpt_content.slot[num].ntp_md5_en == '1') || (rpt_content.slot[num].ntp_md5_en == '0'))
        {
            sprintf((char *)tmp3, "%c,", rpt_content.slot[num].ntp_md5_en);
            tmp3[2] = '\0';
        }
        else
        {
            tmp3[0] = ',';
            tmp3[1] = '\0';
        }
        if((rpt_content.slot[num].ntp_leap[0] >= '0') && (rpt_content.slot[num].ntp_leap[0] <= '9'))
        {
            strncpy((char *)tmp4,rpt_content.slot[num].ntp_leap,2);
            tmp4[2] = '\0';
        }
        else
        {
            tmp4[0] = '\0';
        }

        sprintf((char *)data, "%s%s%s,%s%s%s", data,
                tmp1,
                rpt_content.slot[num].ntp_interval,
                tmp2,
                tmp3,
                tmp4);
		if(rpt_content.slot[num].ntp_mcu[0] < '0' || rpt_content.slot[num].ntp_mcu[0] >'9')
		{
			memset(rpt_content.slot[num].ntp_mcu,0,sizeof(rpt_content.slot[num].ntp_mcu));
		}
		__srtcat(data,rpt_content.slot[num].ntp_mcu);
		__srtcat(data,rpt_content.slot[num].ntp_portEn);
		__srtcat(data,rpt_content.slot[num].ntp_portStatus);
        for(i = 0; i < 12; i++)
        {
			tmp1[0] = i+'1';
			tmp1[1] = 0;
			if(9 == i)
			{
				tmp1[0] = '1';
				tmp1[1] = '0';
			}
			else if(10 == i)
			{
				tmp1[0] = '1';
				tmp1[1] = '1';
			}
			if(11 == i)
			{
				tmp1[0] = '1';
				tmp1[1] = '2';
			}
			__srtcat(data,tmp1);
			strcat_mac(data,num,i);
			strcat_ip(data,num,i);
			strcat_gate(data,num,i);
			strcat_mask(data,num,i);
		}
		for(i = 0; i < 4; i++)
		{
		     length = strlen(data);
			 if(0 == i)
		        sprintf(&data[length],",%s,%s,",rpt_content.slot[num].ntp_keyid[i],rpt_content.slot[num].ntp_key[i]);
			 else if(3 == i)
			 	sprintf(&data[length],"%s,%s",rpt_content.slot[num].ntp_keyid[i],rpt_content.slot[num].ntp_key[i]);
			 else
			 	sprintf(&data[length],"%s,%s,",rpt_content.slot[num].ntp_keyid[i],rpt_content.slot[num].ntp_key[i]);
			 	
				
		}
#else
		sprintf(data,"%s%s",data,"i,Z,V01.04,V02.01,,V00.01,1,0064,D,0,17,9,OO@O,OO@O,1,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,2,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,3,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,4,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,5,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,6,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,7,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,8,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,9,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,10,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,11,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@,12,AABBCCDDEEFF,L@JH@OA@,L@JH@O@A,OOOOOO@@");
		//sprintf(data,"%s%s",data,"j,Z,V01.04,V02.01,,,1,0064,D,0,17,9,,,1,L@JH@OA@,AABBCCDDEEFF,L@JH@O@A,OOOOOO@@");
#endif
    }
    else if('m' == slot_type[num])
    {
        sprintf((char *)data, "%s%c,m", data, slot);
        get_sta1_ver(data, num);
        //sprintf((char *)data,"%s%c,m,%s,%s,,1,JLABAE@C,JLABAE@A,OOOOOO@@,,,",data,slot,"V01.00","V01.00");
    }
}

void rpt_test(char slot, unsigned char *data)
{
    int num = (int)(slot - 'a');
    if((num >= 0) && (num < 14))
    {
        sprintf((char *)data, "%s%c,Y,%s", data, slot, rpt_content.slot[num].sy_ver);
    }
    else if(num == 14)
    {
        sprintf((char *)data, "%s%c,Y,%s", data, slot, rpt_content.slot_o.sy_ver);
    }
    else if(num == 15)
    {
        sprintf((char *)data, "%s%c,Y,%s", data, slot, rpt_content.slot_p.sy_ver);
    }
    else if(num == 16)
    {
        sprintf((char *)data, "%s%c,Y,%s", data, slot, rpt_content.slot_q.sy_ver);
    }
    else if(num == 17)
    {
        sprintf((char *)data, "%s%c,Y,%s", data, slot, rpt_content.slot_r.sy_ver);
    }
}



void rpt_drv(char slot, unsigned char *data)
{
    int num = (int)(slot - 'a');

    if( (12 == num) || (13 == num) )
    {
        sprintf((char *)data, "%s%c,d,%s,,,%d,%d,%d",
                data, slot,
                ((0 == strlen(gExtCtx.drv[num - 12].drvVer)) ? (char *)"," : (char *)(gExtCtx.drv[num - 12].drvVer)),
                gExtCtx.drv[num - 12].drvPR[0],
                gExtCtx.drv[num - 12].drvPR[1],
                gExtCtx.drv[num - 12].drvPR[2]);
    }
}



void rpt_mcp(unsigned char *data)
{
    /*“<aid>,<type>,[<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
    <ip>,<gateway>,<netmask>, < dns1>,< dns2>,<mac>,<source>,<leapnum>”*/

    unsigned char  mcp_ssm_th = 0;
    unsigned char  mcp_ip[16];
    unsigned char  mcp_gateway[16];
    unsigned char  mcp_mask[16];
    unsigned char  mcp_mac[18];
    //int len;
    memset( mcp_ip, '\0', sizeof(unsigned char) * 16);
    memset( mcp_gateway, '\0', sizeof(unsigned char) * 16);
    memset( mcp_mask, '\0', sizeof(unsigned char) * 16);
    memset( mcp_mac, '\0', sizeof(unsigned char) * 18);

    API_Get_McpIp(mcp_ip);
    API_Get_McpGateWay(mcp_gateway);
    //printf("gateway=%s\n",mcp_gateway);
    if(if_a_string_is_a_valid_ipv4_address(mcp_gateway) == 0)
    {}
    else
    {
        memset( mcp_gateway, '\0', sizeof(unsigned char) * 16);
    }
    API_Get_McpMask(mcp_mask);
    API_Get_McpMacAddr(mcp_mac);
    if(if_a_string_is_a_valid_ipv4_address(conf_content.slot_u.dns1) == 0)
    {}
    else
    {
        memset( conf_content.slot_u.dns1, '\0', sizeof(unsigned char) * 16);
    }
    if(if_a_string_is_a_valid_ipv4_address(conf_content.slot_u.dns2) == 0)
    {}
    else
    {
        memset( conf_content.slot_u.dns2, '\0', sizeof(unsigned char) * 16);
    }
    sprintf((char *)data, "%su,N,%s,%s,,,%s,%s,%s,%s,%s,%s", data, (unsigned char *)MCP_VER,
            fpga_ver,
            mcp_ip, mcp_gateway, mcp_mask,
            conf_content.slot_u.dns1,
            conf_content.slot_u.dns2, mcp_mac);
    switch(conf_content.slot_u.out_ssm_oth)
    {
    case 0x02:
        mcp_ssm_th = 'a';
        break;
    case 0x04:
        mcp_ssm_th = 'b';
        break;
    case 0x08:
        mcp_ssm_th = 'c';
        break;
    case 0x0b:
        mcp_ssm_th = 'd';
        break;
    case 0x0f:
        mcp_ssm_th = 'e';
        break;
    case 0x00:
        mcp_ssm_th = 'f';
        break;
    }
#if 0
    sprintf((char *)data, "%s,%c,%s,%c,%s,%c,%c", data,
            conf_content.slot_u.time_source,
            conf_content.slot_u.leap_num,
            conf_content.slot_u.fb,
            conf_content.slot_u.tl,
            conf_content.slot_u.out_ssm_en,
            mcp_ssm_th);
#endif
    //2014-6-19
    sprintf((char *)data, "%s,%c", data, conf_content.slot_u.time_source);
    sprintf((char *)data, "%s,%s", data, conf_content.slot_u.leap_num);
    sprintf((char *)data, "%s,%c", data, conf_content.slot_u.fb);
    sprintf((char *)data, "%s,%s", data, conf_content.slot_u.tl);
    sprintf((char *)data, "%s,%c", data, conf_content.slot_u.out_ssm_en);
    sprintf((char *)data, "%s,%c,%c", data, mcp_ssm_th,conf_content3.mcp_protocol);
	sprintf((char *)data, "%s,%s%c%s", data, mcp_date,SP,mcp_time);
    //2014.12.9
    /*
    if(conf_content3.LeapMod !='0' && conf_content3.LeapMod != '1')
    {
    	conf_content3.LeapMod = '0';
    	save_config();
    }
    sprintf((char *)data,"%s,%c",data,conf_content3.LeapMod);
    */


}



void get_gbtp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    case 0x00:
    case 0x01:
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
    case 0x02:
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x06:
    case 0x07:
        break;
    }
}
void get_btp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    
    case 0x01:
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
	case 0x00:
    case 0x02:
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x06:
    case 0x07:
        break;
    }
}
void get_gtp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{ 
    switch(num)
    {
    case 0x00:
    {
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
    case 0x01:
    case 0x02:
    case 0x03:
    case 0x04:
    case 0x05:
    case 0x06:
    case 0x07:
        break;
    }
}


void get_rb_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:


    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
	case 16:
	case 17:
	case 18:
    {
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
    default:
		break;
    }
}

void get_TP16_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    case 00:
    case 01:
    case 02:
    case 03:
    {
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
    /*case 04:
    case 05:
    case 06:
    case 07:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
	case 20:	
    {
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
        break;
    }
    case 21:
    case 22:
    case 23:
        break;*/
    default:
    break;

    }
}

void get_OUT16_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
	    /*case 00:
	    case 01:
	    case 18:
	    {
	        memcpy(ntfcncde, TYPE_MN, 2);
	        memcpy(almcde, TYPE_MN_A, 2);
	        break;
	    }
	    case 02:
	    case 03:
	    case 04:
	    case 05:
	    case 06:
	    case 07:
	    case 8:
	    case 9:
	    case 10:
	    case 11:
	    case 12:
	    case 13:
	    case 14:
	    case 15:
	    case 16:
	    case 17:
	    {
	        memcpy(ntfcncde, TYPE_MJ, 2);
	        memcpy(almcde, TYPE_MJ_A, 2);
	        break;
	    }
	    case 19:
	    case 20:
	    case 21:
	    case 22:
	    case 23:
	        break;*/
    default:
    break;
    }
}

void get_PTP_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    /*case 00:
    case 01:
    case 02:
    case 03:
    {
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
    case 04:
    case 05:
    case 06:
    case 07:
    case 8:
    case 9:
    case 10:
    case 11:
	case 12:
    {
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
        break;
    }
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
    }
}
/***************************FUNCTION  :get_PGEIN_alm_type********************************/
/***************************PARAMETERS:                  ********************************/
void get_PGEIN_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	{
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
        break;
    }	
    /*case 01:
		
    case 02:
    case 03:
    case 04:
    case 05:
    case 06:
    case 07:
    case 8:
    case 9:
    case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
	break;
  }
}
void get_PGE4V_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	case 01:
	case 02:
    case 03:
    case 04:
    case 05:
    case 06:
    case 07:
	case 8:
    case 9:
   // case 08:
   // case 09:
	case 16:
	case 17:
	case 24:
	case 25:
	case 32:
	case 33:	
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
   
    /*case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
  }
}

void get_GBTPIIV2_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	case 02:
    case 03:
    case 04:
	{
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
		break;
	}
    
	case 01:	
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
   
    /*case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
  }
}

void get_drv_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	case 01:
	case 02:
    
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
   
    /*
    case 03:
    case 04:
    case 05:
    case 06:
    case 07:
    case 08:
    case 09:
	case 16:
	case 17:
	case 24:
	case 25:
	case 32:
	case 33:	
    case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
  }
}
void get_mge_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	case 01:
	case 02:
    case 03:
	case 04:
	case 05:
	case 06:
	case 07:
	case 8:
    case 9:
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
    case 10:
	case 11:
	{
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
        break;
    }
    /*
    case 03:
    case 04:
    case 05:
    case 06:
    case 07:
    case 08:
    case 09:
	case 16:
	case 17:
	case 24:
	case 25:
	case 32:
	case 33:	
    case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
  }
}


void get_tod_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	case 01:
	case 02:
    case 03:
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
   
    /*case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
  }
}

void get_ntp_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
  switch(num)
  {
    case 00:
	case 01:
	case 02:
    case 03:
	case 04:
	case 05:
	case 06:
    case 07:
	case 8:
	case 9:
	case 10:
    case 11:
	case 12:
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
   
    /*case 10:
    case 11:
	case 12:
    case 13:
    case 14:
    case 15:
        break;*/
    default:
    break;
  }
}

void get_MCP_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    case 0:
    case 1:
    case 4:
    case 5:
    {
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
        break;
    }
	case 6:
	{
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }	
    /*case 2:
    case 3:

    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    {
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }*/
    default:
    break;
    }
}

void get_REF_alm_type(unsigned char *ntfcncde, unsigned char *almcde, int num)
{
    switch(num)
    {
    case 00:
    case 01:
    case 02:
    case 03:
    case 12:

    case 04:
    case 05:
    case 06:
    case 07:
    case 8:
    case 9:
    case 10:
    case 11:

    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
    case 31:
    case 32:
    case 33:
    case 34:
    case 35:
    case 36:
    case 37:
    case 38:
    case 39:
    case 40:
    case 41:
    case 42:
    case 43:
    case 44:
    case 45:
    case 46:
    case 47:

    {
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        break;
    }
	default:
		break;
    }
}




int get_format(char *format, unsigned char *ctag)
{
    unsigned char tmp[MAXDATASIZE];
    int len = 0;
    memset(tmp, '\0', MAXDATASIZE);
    get_time_fromnet();
    sprintf((char *)tmp, "%c%c%c%c%c%c%s%c", CR, LF, LF, SP, SP, SP, conf_content.slot_u.tid, SP);
    sprintf((char *)tmp, "%s%s%c%s", tmp, mcp_date, SP, mcp_time);
    sprintf((char *)tmp, "%s%c%cM%c%c%s%cCOMPLD%c%c", tmp, CR, LF, SP, SP, ctag, SP, CR, LF);
    len = strlen(tmp);
    memcpy(format, tmp, len);

    return len;
}




void get_alm(char slot, unsigned char *data)
{
    int num, i;

    num = (int)(slot - 'a');
    //printf("<get_alm>:%d\n",num);
    if( (num >= 0) && (num < 23) )
    {
        if( (num == 18) || (num == 19) ) //电源
        {
            if(slot_type[num] != 'O')
            {
               
                sprintf((char *)data, "%s%c%c%c\"%c,M,\"%c%c", data, SP, SP, SP, slot, CR, LF);
            }
        }
        else if(num == 21)
        {

        }
        else
        {
            if(slot_type[num] != 'O')
            { 
                sprintf((char *)data, "%s%c%c%c%s%c%c", data, SP, SP, SP, alm_sta[num], CR, LF);
            }
			//
			//!reftf alarm : @@@@@@ PPPPPP
			//
            if((slot_type[num] == 'S') && (num == 0))
            {
                sprintf((char *)data, "%s%c%c%c%s%c%c;", data, SP, SP, SP, alm_sta[18], CR, LF);
            }
            else if((slot_type[num] == 'S') && (num == 1))
            {
                sprintf((char *)data, "%s%c%c%c%s%c%c;", data, SP, SP, SP, alm_sta[19], CR, LF);
            }
			//
            //!pge4v2 alarm : @@@@@@ PPPPPP
            //
			else if(slot_type[num] == 'j')
            {
                sprintf((char *)data, "%s%c%c%c%s%c%c;", data, SP, SP, SP, alm_sta[PGE4S_ALM5_START+num], CR, LF);
            }
            else 
            {
                sprintf((char *)data, "%s;", data);
            }
          
			
        }
    }
    else if( (slot == ':') || (slot == ' ') )
    {
        for(i = 0; i < 18; i++)
        {
            if(slot_type[i] != 'O')
            {
                if( (slot_type[i] == 'd' ) && (12 == i || 13 == i) )
                {
                    //20140926改变drv在线告警
                    alm_sta[i][5] &= gExtCtx.save.onlineSta;
                }
                else
                {
                    ;
                }
                if(alm_sta[i][0] != '\0')
                	sprintf((char *)data, "%s%c%c%c%s%c%c", data, SP, SP, SP, alm_sta[i], CR, LF);
            }
			//
			//! reftf @@@@@@ PPPPPP
			//
            if((slot_type[i] == 'S') && (i == 0))
            {
                sprintf((char *)data, "%s%c%c%c%s%c%c", data, SP, SP, SP, alm_sta[18], CR, LF);
            }
            else if((slot_type[i] == 'S') && (i == 1))
            {
                sprintf((char *)data, "%s%c%c%c%s%c%c", data, SP, SP, SP, alm_sta[19], CR, LF);
            }
			//
			//! pge4v2 @@@@@@ PPPPPP
			//
			else if(slot_type[i] == 'j')
            {
                sprintf((char *)data, "%s%c%c%c%s%c%c", data, SP, SP, SP, alm_sta[PGE4S_ALM5_START+i], CR, LF);
            }
        }
        if(slot_type[22] != 'O')
        {
            sprintf((char *)data, "%s%c%c%c%s%c%c", data, SP, SP, SP, alm_sta[20], CR, LF);
            sprintf((char *)data, "%s%c%c%c%s%c%c;", data, SP, SP, SP, alm_sta[22], CR, LF);
        }
        else
        {
            sprintf((char *)data, "%s%c%c%c%s%c%c;", data, SP, SP, SP, alm_sta[20], CR, LF);
        }
    }
}







void get_ext_alm(char slot, unsigned char *data)
{
    int i;
    int opt = 0;

    if( (slot == ':') || (slot == ' ') )
    {
        for(i = 0; i < 10; i++)
        {
            if(	(EXT_NONE != gExtCtx.extBid[0][i]) &&
                    (0 != memcmp(gExtCtx.out[i].outAlm, "@@@@@", 5)))
            {
                opt = 1;
                sprintf((char *)data,
                        "%s%c%c%c\"%c,%c,%s\"\r\n",
                        data,
                        SP, SP, SP,
                        i + 'a',
                        gExtCtx.extBid[0][i],
                        gExtCtx.out[i].outAlm);
            }
        }

        if( (EXT_NONE != gExtCtx.extBid[0][10]) &&
                (0 != memcmp(gExtCtx.mgr[0].mgrAlm, "@@@@@@@", 7)))
        {
            opt = 1;
            sprintf((char *)data,
                    "%s%c%c%c\"%c,%c,%s\"\r\n",
                    data,
                    SP, SP, SP,
                    'k',
                    gExtCtx.extBid[0][10],
                    gExtCtx.mgr[0].mgrAlm);
        }

        if( (EXT_NONE != gExtCtx.extBid[0][11]) &&
                (0 != memcmp(gExtCtx.mgr[1].mgrAlm, "@@@@@@@", 7)))
        {
            opt = 1;
            sprintf((char *)data,
                    "%s%c%c%c\"%c,%c,%s\"\r\n",
                    data,
                    SP, SP, SP,
                    'l',
                    gExtCtx.extBid[0][11],
                    gExtCtx.mgr[1].mgrAlm);
        }

        if(0 == opt)
        {
            strcat(data, "   \"\"\r\n");
        }
        strcat(data, "/\r\n");

        opt = 0;
        for(i = 0; i < 10; i++)
        {
            if( (EXT_NONE != gExtCtx.extBid[1][i]) &&
                    (0 != memcmp(gExtCtx.out[10 + i].outAlm, "@@@@@", 5)))
            {
                opt = 1;
                sprintf((char *)data,
                        "%s%c%c%c\"%c,%c,%s\"\r\n",
                        data, SP, SP, SP,
                        i + 'a',
                        gExtCtx.extBid[1][i],
                        gExtCtx.out[10 + i].outAlm);
            }
        }

        if( (EXT_NONE != gExtCtx.extBid[1][10]) &&
                (0 != memcmp(gExtCtx.mgr[2].mgrAlm, "@@@@@@@", 7)))
        {
            opt = 1;
            sprintf((char *)data,
                    "%s%c%c%c\"%c,%c,%s\"\r\n",
                    data, SP, SP, SP, 'k',
                    gExtCtx.extBid[1][10],
                    gExtCtx.mgr[2].mgrAlm);
        }

        if( (EXT_NONE != gExtCtx.extBid[1][11]) &&
                (0 != memcmp(gExtCtx.mgr[3].mgrAlm, "@@@@@@@", 7)))
        {
            opt = 1;
            sprintf((char *)data,
                    "%s%c%c%c\"%c,%c,%s\"\r\n",
                    data, SP, SP, SP, 'l',
                    gExtCtx.extBid[1][11],
                    gExtCtx.mgr[3].mgrAlm);
        }

        if(0 == opt)
        {
            strcat(data, "   \"\"\r\n");
        }
        strcat(data, "/\r\n");

        opt = 0;
        for(i = 0; i < 10; i++)
        {
            if( (EXT_NONE != gExtCtx.extBid[2][i]) &&
                    (0 != memcmp(gExtCtx.out[20 + i].outAlm, "@@@@@", 5)))
            {
                opt = 1;
                sprintf((char *)data,
                        "%s%c%c%c\"%c,%c,%s\"\r\n",
                        data, SP, SP, SP, i + 'a',
                        gExtCtx.extBid[2][i],
                        gExtCtx.out[20 + i].outAlm);
            }
        }

        if( (EXT_NONE != gExtCtx.extBid[2][10]) &&
                (0 != memcmp(gExtCtx.mgr[4].mgrAlm, "@@@@@@@", 7)))
        {
            opt = 1;
            sprintf((char *)data,
                    "%s%c%c%c\"%c,%c,%s\"\r\n",
                    data, SP, SP, SP, 'k',
                    gExtCtx.extBid[2][10],
                    gExtCtx.mgr[4].mgrAlm);
        }

        if( (EXT_NONE != gExtCtx.extBid[2][11]) &&
                (0 != memcmp(gExtCtx.mgr[5].mgrAlm, "@@@@@@@", 7)))
        {
            opt = 1;
            sprintf((char *)data,
                    "%s%c%c%c\"%c,%c,%s\"\r\n",
                    data, SP, SP, SP, 'l',
                    gExtCtx.extBid[2][11],
                    gExtCtx.mgr[5].mgrAlm);
        }

        if(0 == opt)
        {
            strcat(data, "   \"\"\r\n");
        }
        strcat(data, ";");
    }
}







void get_ext1_alm(char slot, unsigned char *data)
{
    int num, i;

    if( (slot == ':') || (slot == ' ') )
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, i + 'a', gExtCtx.extBid[0][i]);
            if(EXT_NONE != gExtCtx.extBid[0][i])
            {
                sprintf((char *)data, "%s,%s\"%c%c", data, gExtCtx.out[i].outAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n");
            }
        }

        sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, 'k', gExtCtx.extBid[0][10]);
        if(EXT_NONE != gExtCtx.extBid[0][10])
        {
            sprintf((char *)data, "%s,%s\"%c%c", data, gExtCtx.mgr[0].mgrAlm, CR, LF);
        }
        else
        {
            strcat(data, "\"\r\n");
        }

        sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, 'l', gExtCtx.extBid[0][11]);
        if(EXT_NONE != gExtCtx.extBid[0][11])
        {
            sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[1].mgrAlm, CR, LF);
        }
        else
        {
            strcat(data, "\"\r\n;");
        }
    }
    else
    {
        num = (int)(slot - 'a');
        if( (num >= 0) && (num <= 9) )
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[0][num]);
            if(EXT_NONE != gExtCtx.extBid[0][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.out[num].outAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else if(10 == num)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[0][num]);
            if(EXT_NONE != gExtCtx.extBid[0][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[0].mgrAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else if(11 == num)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[0][num]);
            if(EXT_NONE != gExtCtx.extBid[0][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[1].mgrAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else
        {
            //
        }
    }
}







void get_ext2_alm(char slot, unsigned char *data)
{
    int num, i;

    if( (slot == ':') || (slot == ' ') )
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, i + 'a', gExtCtx.extBid[1][i]);
            if(EXT_NONE != gExtCtx.extBid[1][i])
            {
                sprintf((char *)data, "%s,%s\"%c%c", data, gExtCtx.out[10 + i].outAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n");
            }
        }

        sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, 'k', gExtCtx.extBid[1][10]);
        if(EXT_NONE != gExtCtx.extBid[1][10])
        {
            sprintf((char *)data, "%s,%s\"%c%c", data, gExtCtx.mgr[2].mgrAlm, CR, LF);
        }
        else
        {
            strcat(data, "\"\r\n");
        }

        sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, 'l', gExtCtx.extBid[1][11]);
        if(EXT_NONE != gExtCtx.extBid[1][11])
        {
            sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[3].mgrAlm, CR, LF);
        }
        else
        {
            strcat(data, "\"\r\n;");
        }
    }
    else
    {
        num = (int)(slot - 'a');
        if( (num >= 0) && (num <= 9) )
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[1][num]);
            if(EXT_NONE != gExtCtx.extBid[1][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.out[10 + num].outAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else if(10 == num)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[1][num]);
            if(EXT_NONE != gExtCtx.extBid[1][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[2].mgrAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else if(11 == num)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[1][num]);
            if(EXT_NONE != gExtCtx.extBid[1][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[3].mgrAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else
        {
            //
        }
    }
}







void get_ext3_alm(char slot, unsigned char *data)
{
    int num, i;

    if( (slot == ':') || (slot == ' ') )
    {
        for(i = 0; i < 10; i++)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, i + 'a', gExtCtx.extBid[2][i]);
            if(EXT_NONE != gExtCtx.extBid[2][i])
            {
                sprintf((char *)data, "%s,%s\"%c%c", data, gExtCtx.out[20 + i].outAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n");
            }
        }

        sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, 'k', gExtCtx.extBid[2][10]);
        if(EXT_NONE != gExtCtx.extBid[2][10])
        {
            sprintf((char *)data, "%s,%s\"%c%c", data, gExtCtx.mgr[4].mgrAlm, CR, LF);
        }
        else
        {
            strcat(data, "\"\r\n");
        }

        sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, 'l', gExtCtx.extBid[2][11]);
        if(EXT_NONE != gExtCtx.extBid[2][11])
        {
            sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[5].mgrAlm, CR, LF);
        }
        else
        {
            strcat(data, "\"\r\n;");
        }
    }
    else
    {
        num = (int)(slot - 'a');
        if( (num >= 0) && (num <= 9) )
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[2][num]);
            if(EXT_NONE != gExtCtx.extBid[2][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.out[20 + num].outAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else if(10 == num)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[2][num]);
            if(EXT_NONE != gExtCtx.extBid[2][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[4].mgrAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else if(11 == num)
        {
            sprintf((char *)data, "%s%c%c%c\"%c,%c", data, SP, SP, SP, slot, gExtCtx.extBid[2][num]);
            if(EXT_NONE != gExtCtx.extBid[2][num])
            {
                sprintf((char *)data, "%s,%s\"%c%c;", data, gExtCtx.mgr[5].mgrAlm, CR, LF);
            }
            else
            {
                strcat(data, "\"\r\n;");
            }
        }
        else
        {
            //
        }
    }
}




/*********************Function:save_one_alm********************/
/*********************Parmeters: slot      *******************/
/*********************Modify:2017/7/19      *******************/


void save_alm(char slot, unsigned char *data, int flag)
{
    int num;
    num = (int)(slot - 'a');
    if(flag == 1) //REF
    {
        memcpy(alm_sta[num + 18], data, strlen(data));
    }
	else if(flag == 2)
	{
	    memcpy(alm_sta[num + PGE4S_ALM5_START], data, strlen(data));
	}
    else if((num >= 0) && (num < 18))
    {
        /*if(0 == num || 1 == num)
        {
            printf("<ref alm %d>:'0x4':%s\n", num, data);
        }*/  
        memcpy(alm_sta[num], data, strlen(data));
    }
    else if(num == 20)
    {
        memcpy(alm_sta[20], data, strlen(data));
    }
    else if(num == 22)
    {
        memcpy(alm_sta[22], data, strlen(data));
    }
#if DEBUG_NET_INFO
    if(print_switch == 0)
    {
        printf("%s:<%d>:%s;\n", NET_INFO, num + 1, data);
    }
#endif
}

int send_pullout_alm(int num, int flag_cl)
{
    unsigned char alm[3];
    unsigned char tmp[50];
    unsigned char ntfcncde[3];
    unsigned char almcde[3];
    alm[0] = ((num & 0x0f) | 0x50); // 0x6x  di 4 wei

    if(flag_cl == 0)
    {
        alm[1] = (((num & 0x70) >> 3) | 0x50);
        alm[2] = '\0';
        memcpy(ntfcncde, TYPE_MN, 2);
        memcpy(almcde, TYPE_MN_A, 2);
        rpt_alm_framing('u', TYPE_MN, TYPE_MN_A, alm);
    }
    else
    {
        alm[1] = (((num & 0x70) >> 3) | 0x51);
        alm[2] = '\0';
        memcpy(ntfcncde, TYPE_CL, 2);
        memcpy(almcde, TYPE_CL_A, 2);
        rpt_alm_framing('u', ntfcncde, almcde, alm);
    }

    get_time_fromnet();
    sprintf((char *)tmp, "\"u:N,%s,%s,%s,%s,%s,NSA\"%c%c%c%c%c",
            mcp_date, mcp_time, almcde, ntfcncde, alm, CR, LF, SP, SP, SP);
    strncpy(saved_alms[saved_alms_cnt], tmp, strlen(tmp));
    saved_alms_cnt++;
    if(saved_alms_cnt == SAVE_ALM_NUM)
    {
        saved_alms_cnt = 0;
    }
    /******/

    return 0;
}

void  send_alm(char slot, int num, char type, int flag_cl)
{

    unsigned char ntfcncde[3];
    unsigned char almcde[3];
    unsigned char alm[3];
    unsigned char tmp[50];
	//unsigned char act_almbit = 0;
    memset(ntfcncde, '\0', 3);
    memset(almcde, '\0', 3);
    memset(alm, '\0', 3);
    memset(tmp, '\0', 50);
    if(type == 'L' ||'e' == type || 'g' == type )
    {
        get_gbtp_alm_type(ntfcncde, almcde, num);
    }
	else if(  'l' == type)
	{
	    get_btp_alm_type(ntfcncde, almcde, num);
	}
	else if( type == 'P')
	{
	    get_gtp_alm_type(ntfcncde, almcde, num);
	}
    //else if(type=='P')
    //	{
    //	get_gbtp_alm_type(ntfcncde,almcde,num);
    //}
    else if(type == 'K' || type == 'R')
    {
        get_rb_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 'J' || type == 'a' || type == 'h' || type == 'i')
    {
        get_OUT16_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 'I')
    {
        get_TP16_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 'S')
    {
        get_REF_alm_type(ntfcncde, almcde, num);
    }
    else if((type < 'I') && (type > '@'))
    {
        get_PTP_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 'Q')
    {
        memcpy(ntfcncde, TYPE_MJ, 2);
        memcpy(almcde, TYPE_MJ_A, 2);
    }
    else if(type == 'N')
    {
        get_MCP_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 'V')
	{
	    get_tod_alm_type(ntfcncde, almcde, num);
    }
	else if(type == 'Z')
	{
	    get_ntp_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 's')
    {
        get_PGEIN_alm_type(ntfcncde, almcde, num);
    }
    else if(type == 'j')
    {
        get_PGE4V_alm_type(ntfcncde, almcde, num);
    }
	else if(type == 'd')
    {
        get_drv_alm_type(ntfcncde, almcde, num);
    }
	else if(type == 'f')
    {
        get_mge_alm_type(ntfcncde, almcde, num);
    }
	else if(type == 'v')
	{
	    get_GBTPIIV2_alm_type(ntfcncde, almcde, num);
	}
    //num=num+1;  0110 Z3Z2Z1Z0 0110 Z7 Z6Z5Z4
    //@@@@@@@@@@@@@@@@@@
    alm[0] = ((num & 0x0f) | 0x50);
    if(flag_cl == 0)
    {
        alm[1] = (((num & 0x70) >> 3) | 0x50);
        memcpy(ntfcncde, TYPE_CL, 2);
        memcpy(almcde, TYPE_CL_A, 2);
    }
    else
    {
        alm[1] = (((num & 0x70) >> 3) | 0x51);
    }

    alm[2] = '\0';
    /******/
    get_time_fromnet();
    sprintf((char *)tmp, "\"%c:%c,%s,%s,%s,%s,%s,NSA\"%c%c%c%c%c",
            slot, type, mcp_date, mcp_time, almcde, ntfcncde, alm, CR, LF, SP, SP, SP);
    strncpy(saved_alms[saved_alms_cnt], tmp, strlen(tmp));
    saved_alms_cnt++;
    if(saved_alms_cnt == SAVE_ALM_NUM)
    {
        saved_alms_cnt = 0;
    }
    /******/
	if(ntfcncde[0] != '\0')
    rpt_alm_framing(slot, ntfcncde, almcde, alm);


}


void rpt_alm_to_client(char slot, unsigned char *data)
{
    int num, i, j, len;
    char tmp, type_flag, temp;
	int star_ind = 0;
    char errflag = 0;
    len = strlen(data);
    num = (int)(slot - 'a');

    type_flag = slot_type[num];
    //
    //屏蔽掉多余告警
    //
    almMsk_cmdRALM(num, data, len);
	
	if(num == 20)
    {
        num = 18;     //mcp
    }
    else if(num == 22)
    {
        num = 19;     //fan
    }
    //
    //!0x04 第一行报警，0x05 第二行报警, 每行24报警
    //
	for(i = 0; i < len; i++)
    {
        if((data[0] >> 4) == 0x04)
        {
            tmp = (data[i] & 0x0f);
            for(j = 0; j < 4; j++)
            {
                temp = ((tmp >> j) & 0x01);
                if((temp == 0x00) && (flag_alm[num][i * 4 + j] == 0x01))
                {
                    /*清除告警*/
                    send_alm(slot, (i * 4 + j), type_flag, 0);
                }
                else if((temp == 0x01) && (flag_alm[num][i * 4 + j] == 0x00))
                {
                    /*上报告警*/
                    send_alm(slot, (i * 4 + j), type_flag, 1);
                }
                flag_alm[num][i * 4 + j] = temp;
            }
        }
        else if((data[0] >> 4) == 0x05) //ref
        {
            //printf("<rpt_alm_to_client>:'ref num':%d\n",num);
            //! flag_alm 20 ref1 21 ref2 (0x50)
			switch(type_flag)
			{
			  case 'S':
			  {
			  	if((num != 1) || (num != 0))
					errflag = 1;
				else
					star_ind = (int)PGE4S_TALM5_START + num - 2;
			  
			  }
			  break;
			  case 'j':
			  {
			  	if((num < 0) || (num > 13))
					errflag = 1;
				else
					star_ind = (int)PGE4S_TALM5_START + num;
			  }
			  break;
			  default:
			  	errflag = 1;
			  break;
			}

			if(errflag == 1)
				return ;

			tmp = (data[i] & 0x0f);
            for(j = 0; j < 4; j++)
            {
                temp = ((tmp >> j) & 0x01);
                if((temp == 0x00) && (flag_alm[star_ind][i * 4 + j] == 0x01))
                {
                    /*清除告警*/
                    send_alm(slot, (i * 4 + j + 24), type_flag, 0);
                }
                else if((temp == 0x01) && (flag_alm[star_ind][i * 4 + j] == 0x00))
                {
                    /*上报告警*/
                    send_alm(slot, (i * 4 + j + 24), type_flag, 1);
                }
                flag_alm[star_ind][i * 4 + j] = temp;
            }
        }
    }

}







void respond_success(int client, unsigned char *ctag)
{
    int len, send_ok;
    unsigned char  sendbuf[MAXDATASIZE];
    memset(sendbuf, '\0', MAXDATASIZE);
    len = get_format(sendbuf, ctag);
    sprintf((char *)sendbuf, "%s;", sendbuf);
    send_ok = send(client, sendbuf, len + 1, MSG_NOSIGNAL);
    if(send_ok == -1)
    {
        /*	close(client);
        	FD_CLR(client, &allset);
        	client= -1;*/
        printf("timeout_<1>!\n");
    }
    if(print_switch == 0)
    {
        printf("##YES,%s\n", ctag);
    }
}

void respond_fail(int client, unsigned char *ctag, int type)
{
    /*	<cr><lf><lf>
    ^^^<sid>^<date>^<time><cr><lf>
    M^^<catg>^DENY<cr><lf>
    ^^^<error code><cr><lf>;*/
    int send_ok, len;
    unsigned char  sendbuf[MAXDATASIZE];
    memset(sendbuf, '\0', MAXDATASIZE);
    get_time_fromnet();
    sprintf((char *)sendbuf, "%c%c%c%c%c%c%s%c", CR, LF, LF, SP, SP, SP, conf_content.slot_u.tid, SP);
    sprintf((char *)sendbuf, "%s%s%c%s%c%c", sendbuf, mcp_date, SP, mcp_time, CR, LF);
    sprintf((char *)sendbuf, "%sM%c%c%s%cDENY%c%c", sendbuf, SP, SP, ctag, SP, CR, LF);
    sprintf((char *)sendbuf, "%s%c%c%c%s%c%c;", sendbuf, SP, SP, SP, g_cmd_fun_ret[type], CR, LF);
    len = strlen(sendbuf);
    send_ok = send(client, sendbuf, len + 1, MSG_NOSIGNAL);
    if(send_ok == -1)
    {
        /*	close(client);
        	FD_CLR(client, &allset);
        	client= -1;*/
        printf("timeout_<2>!\n");
    }
    if(print_switch == 0)
    {
        printf("##NO,%s\n", ctag);
    }
}




void rpt_event_framing(unsigned char *event,
                       char slot,
                       int flag_na,
                       unsigned char *data,
                       int flag_sa,
                       unsigned char *reason)
{
    unsigned char tmp[1024];
    memset(tmp, '\0', 1024);
    get_time_fromnet();
    sprintf((char *)tmp, "%c%c%c%c%c%c%s%c", CR, LF, LF, SP, SP, SP, conf_content.slot_u.tid, SP);
    sprintf((char *)tmp, "%s%s%c%s%c%c", tmp, mcp_date, SP, mcp_time, CR, LF);
    sprintf((char *)tmp, "%sA%c%c%06d%cREPT%cEVNT%c%s%c%c", tmp, SP, SP, atag, SP, SP, SP, event, CR, LF);

	if((event[0]=='C')&&(event[1]=='0')&&(event[2]=='0'))
	{
	   if(conf_content.slot_u.fb == '0')
	   	{
			if(slot == RB1_SLOT)
				sprintf(tmp,"%s%c%c%c\"%c:%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s:\\\"\\\"\"%c%c;",
					tmp,SP,SP,SP,slot,((flag_na==1)?"NA":"CL"),rpt_content.slot_o.rb_ph[0],
					rpt_content.slot_o.rb_ph[1],rpt_content.slot_o.rb_ph[2],rpt_content.slot_o.rb_ph[3],rpt_content.slot_p.rb_ph[4],rpt_content.slot_o.rb_ph[4],
					rpt_content.slot_p.rb_ph[5],rpt_content.slot_o.rb_ph[5],rpt_content.slot_o.rb_ph[6],
					((flag_sa==1)?"SA":"NSA"),reason,CR,LF);
			else if(slot == RB2_SLOT)
				sprintf(tmp,"%s%c%c%c\"%c:%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s:\\\"\\\"\"%c%c;",
					tmp,SP,SP,SP,slot,((flag_na==1)?"NA":"CL"),rpt_content.slot_p.rb_ph[0],
					rpt_content.slot_p.rb_ph[1],rpt_content.slot_p.rb_ph[2],rpt_content.slot_p.rb_ph[3],rpt_content.slot_p.rb_ph[4],rpt_content.slot_o.rb_ph[4],
					rpt_content.slot_p.rb_ph[5],rpt_content.slot_o.rb_ph[5],rpt_content.slot_p.rb_ph[6],
					((flag_sa==1)?"SA":"NSA"),reason,CR,LF);
	   	}
	   else
	   	{
	   	   if(slot == RB1_SLOT)
				sprintf(tmp,"%s%c%c%c\"%c:%s,%s,%s,%s,%s,%s,%s,,,%s,%s,%s:\\\"\\\"\"%c%c;",
					tmp,SP,SP,SP,slot,((flag_na==1)?"NA":"CL"),rpt_content.slot_o.rb_ph[0],
					rpt_content.slot_o.rb_ph[1],rpt_content.slot_o.rb_ph[2],rpt_content.slot_o.rb_ph[3],rpt_content.slot_o.rb_ph[4],
					rpt_content.slot_o.rb_ph[5],rpt_content.slot_o.rb_ph[6],
					((flag_sa==1)?"SA":"NSA"),reason,CR,LF);
		   else if(slot == RB2_SLOT)
				sprintf(tmp,"%s%c%c%c\"%c:%s,%s,%s,%s,%s,%s,%s,,,%s,%s,%s:\\\"\\\"\"%c%c;",
					tmp,SP,SP,SP,slot,((flag_na==1)?"NA":"CL"),rpt_content.slot_p.rb_ph[0],
					rpt_content.slot_p.rb_ph[1],rpt_content.slot_p.rb_ph[2],rpt_content.slot_p.rb_ph[3],rpt_content.slot_p.rb_ph[4],
					rpt_content.slot_p.rb_ph[5],rpt_content.slot_p.rb_ph[6],
					((flag_sa==1)?"SA":"NSA"),reason,CR,LF);
	   	}
		//printf("send client data,%s\n",tmp);
	}
	else
    sprintf((char *)tmp, "%s%c%c%c\"%c:%s,%s,%s,%s:\\\"\\\"\"%c%c;",
            tmp, SP, SP, SP, slot, ((flag_na == 1) ? "NA" : "CL"), data, ((flag_sa == 1) ? "SA" : "NSA"), reason, CR, LF);

    sendto_all_client(tmp);
    atag++;
    if(atag >= 0xffff)
    {
        atag = 0;
    }
}





/*
<cr><lf><lf>
^^^<sid>^<date>^<time><cr><lf>
<almcde>^<atag>^REPT^EVNT^<event><cr><lf>
^^^”<aid>:<ntfcncde>,<condtype>,<serveff>,<reason>:\”<conddescr>\””<cr><lf>
;
   	<ntfcncde>::=NA|CL
	<serveff>::=SA|NSA
   LT00000000000002 2011-11-19 10:27:41
A  003366 REPT EVNT C91
   "l:NA,,NSA,AUTO:\"\""
;
*/

void rpt_alm_framing(char slot, unsigned char *ntfcncde,
                     unsigned char *almcde, unsigned char *alm)
{
    /*<cr><lf><lf>
    ^^^<sid>^<date>^<time><cr><lf>
    <almcde>^<atag>^REPT^ALRM<cr><lf>
    ^^^”<aid>:<ntfcncde>,<condtype>,<serveff>:\”<conddescr>\””<cr><lf>
    ;*/
    /*<almcde>::=*C|**|*^|A^   对应的<ctfcncde>格式为：
    <ntfcncde>::=CR|MJ|MN|CL
    分别表示严重告警、主要告警、次要告警及告警清除信息；*/
    /*
       LT00000000000002 2011-11-19 10:36:34
    *  003412 REPT ALRM
    "g:MN,co,NSA:\"\""
    ;
    */
    unsigned char tmp[1024];

    memset(tmp, '\0', 1024);
    get_time_fromnet();
    sprintf((char *)tmp, "%c%c%c%c%c%c%s%c", CR, LF, LF, SP, SP, SP, conf_content.slot_u.tid, SP);
    sprintf((char *)tmp, "%s%s%c%s%c%c", tmp, mcp_date, SP, mcp_time, CR, LF);
    sprintf((char *)tmp, "%s%s%c%06d%cREPT%cALRM%c%c%c", tmp, almcde, SP, atag, SP, SP, SP, CR, LF);
    sprintf((char *)tmp, "%s%c%c%c\"%c:%s,%s,NSA:\\\"\\\"\"%c%c;",
            tmp, SP, SP, SP, slot, ntfcncde, alm, CR, LF);

    sendto_all_client(tmp);
    atag++;
    if(atag >= 0xffff)
    {
        atag = 0;
    }

}

void send_online_framing(int num, char type)
{
    /*<cr><lf><lf>
    ^^^<sid>^<date>^<time><cr><lf>
    <almcde>^<atag>^REPT^EVNT^<event><cr><lf>
    ^^^”<aid>:<ntfcncde>,<condtype>,<serveff>,<reason>:\”<conddescr>\””<cr><lf>
    ;

    LT00000000000002 2011-11-19 10:27:45
    A  003367 REPT EVNT C91
    "m:NA,O,NSA,AUTO:\"\""
    ;


    */

    //	unsigned char  *tmp=(unsigned char *) malloc ( sizeof(unsigned char) * 1024 );
    unsigned char tmp[1024];
    if(num > 24 || num < 0)
    {
        return ;
    }
    memset(tmp, '\0', 1024);
    get_time_fromnet();
    sprintf((char *)tmp, "%c%c%c%c%c%c%s%c", CR, LF, LF, SP, SP, SP, conf_content.slot_u.tid, SP);
    sprintf((char *)tmp, "%s%s%c%s%c%c", tmp, mcp_date, SP, mcp_time, CR, LF);
    sprintf((char *)tmp, "%sA%c%c%06d%cREPT%cEVNT%cC91%c%c", tmp, SP, SP, atag, SP, SP, SP, CR, LF);
    sprintf((char *)tmp, "%s%c%c%c\"%c:NA,%c,NSA,AUTO:\\\"\\\"\"%c%c;",
            tmp, SP, SP, SP, (char)(num + 0x61), type, CR, LF);

    sendto_all_client(tmp);
    atag++;
    if(atag >= 0xffff)
    {
        atag = 0;
    }
    //	free(tmp);
    //	tmp=NULL;


}



void sendto_all_client(unsigned char *buf)
{
    int count, len, i, send_ok;
    int client_num[MAX_CLIENT];

    len = strlen_r(buf, ';') + 1;
    count = judge_client_online(client_num);

    for(i = 0; i < count; i++)
    {
        send_ok = send(client_fd[client_num[i]], buf, len, MSG_NOSIGNAL);

        //printf("\n\n%s SEND: %s\n\n", __func__, buf);

        if(send_ok == -1)
        {
            printf("-----------------timeout!\n");
        }
    }
}





void sendtodown(unsigned char *buf, unsigned char slot)
{
    struct mymsgbuf msgbuffer;
    int msglen;

    memset(&msgbuffer, '\0', sizeof(struct mymsgbuf));
    msgbuffer.msgtype = 4;
    msgbuffer.slot = slot - 0x60;
    strcpy(msgbuffer.ctrlstring, buf);
    msgbuffer.len = strlen(msgbuffer.ctrlstring);
    memcpy(msgbuffer.ctag, "000000", 6);
    msglen = sizeof(msgbuffer) - 4;

    if((msgsnd (qid, &msgbuffer, msglen, 0)) == -1)
    {
        perror("msgget error<1>!\n");
        printf("errno=%d\n", errno);
    }
    if(print_switch == 0)
    {
        printf("UPMSG>%s<>%d<>%s<\n", msgbuffer.ctrlstring, msgbuffer.len, msgbuffer.ctag);
    }
}





void sendtodown_cli(unsigned char *buf, unsigned char slot, unsigned char *ctag)
{
    struct mymsgbuf msgbuffer;
    int msglen;

    memset(&msgbuffer, '\0', sizeof(struct mymsgbuf));
    msgbuffer.msgtype = 4;
    msgbuffer.slot = slot - 0x60;
    strcpy(msgbuffer.ctrlstring, buf);
    msgbuffer.len = strlen(msgbuffer.ctrlstring);
    memcpy (msgbuffer.ctag, ctag, 6);
    msglen = sizeof(msgbuffer) - 4;

    if((msgsnd (qid, &msgbuffer, msglen, 0)) == -1)
    {
        perror ("msgget error<2>!\n");
        printf("errno=%d\n", errno);
    }
    if(print_switch == 0)
    {
        printf("UPMSG>%s<>%d<>%s<\n", msgbuffer.ctrlstring, msgbuffer.len, msgbuffer.ctag);
    }
}









/*
  1	成功
  0	失败
*/
int ext_issue_cmd(u8_t *buf, u16_t len, u8_t slot, u8_t *ctag)
{
    struct mymsgbuf msgbuffer;
    int msglen;

    memset(&msgbuffer, '\0', sizeof(struct mymsgbuf));
    msgbuffer.msgtype = 4;
    memcpy(msgbuffer.ctrlstring, buf, len);
    msgbuffer.len = len;
    memcpy(msgbuffer.ctag, ctag, 6);
    msglen = sizeof(msgbuffer) - 4;

    if( (EXT_DRV == slot_type[12]) &&
            (slot >= EXT_1_OUT_LOWER && slot <= EXT_1_OUT_UPPER) &&
            (1 == gExtCtx.drv[0].drvPR[0]) )
    {
        msgbuffer.slot = 13;
        if(-1 == (msgsnd(qid, &msgbuffer, msglen, 0)))
        {
            return 0;
        }
    }

    else if((EXT_DRV == slot_type[12]) &&
            (slot >= EXT_2_OUT_LOWER && slot <= EXT_2_OUT_UPPER) &&
            (1 == gExtCtx.drv[0].drvPR[1]) )
    {
        msgbuffer.slot = 13;
        if(-1 == (msgsnd(qid, &msgbuffer, msglen, 0)))
        {
            return 0;
        }
    }

    else if((EXT_DRV == slot_type[12]) &&
            (slot >= EXT_3_OUT_LOWER && slot <= EXT_3_OUT_UPPER && slot != ':' && slot != ';') &&
            (1 == gExtCtx.drv[0].drvPR[2]) )
    {
        msgbuffer.slot = 13;
        if(-1 == (msgsnd(qid, &msgbuffer, msglen, 0)))
        {
            return 0;
        }
    }

    else if((EXT_DRV == slot_type[13]) &&
            (slot >= EXT_1_OUT_LOWER && slot <= EXT_1_OUT_UPPER) &&
            (1 == gExtCtx.drv[1].drvPR[0]) )
    {
        msgbuffer.slot = 14;
        if(-1 == (msgsnd(qid, &msgbuffer, msglen, 0)))
        {
            return 0;
        }
    }

    else if((EXT_DRV == slot_type[13]) &&
            (slot >= EXT_2_OUT_LOWER && slot <= EXT_2_OUT_UPPER) &&
            (1 == gExtCtx.drv[1].drvPR[1]) )
    {
        msgbuffer.slot = 14;
        if(-1 == (msgsnd(qid, &msgbuffer, msglen, 0)))
        {
            return 0;
        }
    }

    else if((EXT_DRV == slot_type[13]) &&
            (slot >= EXT_3_OUT_LOWER && slot <= EXT_3_OUT_UPPER && slot != ':' && slot != ';') &&
            (1 == gExtCtx.drv[1].drvPR[2]) )
    {
        msgbuffer.slot = 14;
        if(-1 == (msgsnd(qid, &msgbuffer, msglen, 0)))
        {
            return 0;
        }
    }
    else
    {
        //do nothing
    }

    if(print_switch == 0)
    {
        printf("UPMSG>%s<>%d<>%s<\n", msgbuffer.ctrlstring, msgbuffer.len, msgbuffer.ctag);
    }

    return 1;
}









int get_freq_lev(int lev)//快捕频率的时候   降一等级
{
    switch (lev)
    {
    case 0x00:
        lev = 0x00;
        break;
    case 0x02:
        lev = 0x04;
        break;
    case 0x04:
        lev = 0x08;
        break;
    case 0x08:
        lev = 0x0b;
        break;
    case 0x0b:
        lev = 0x0f;
        break;
    case 0x0f:
        lev = 0x0f;
        break;
    default:
        lev = 0x0f;
        break;
    }
    return lev;
}

int get_out_now_lev(unsigned char *parg)
{
    int lev;
    if(strncmp1(parg, "00", 2) == 0)
    {
        lev = 0;
    }
    else if(strncmp1(parg, "02", 2) == 0)
    {
        lev = 0x02;
    }
    else if(strncmp1(parg, "04", 2) == 0)
    {
        lev = 0x04;
    }
    else if(strncmp1(parg, "08", 2) == 0)
    {
        lev = 0x08;
    }
    else if(strncmp1(parg, "0B", 2) == 0)
    {
        lev = 0x0b;
    }
    else if(strncmp1(parg, "0F", 2) == 0)
    {
        lev = 0x0f;
    }
    else
    {
        lev = 0x0f;
    }
    return lev;
}

void get_out_now_lev_char(int lev )
{
    if(lev == 0x00)
    {
        memcpy(conf_content.slot_u.tl, "00", 2);
    }
    else if(lev == 0x02)
    {
        memcpy(conf_content.slot_u.tl, "02", 2);
    }
    else if(lev == 0x04)
    {
        memcpy(conf_content.slot_u.tl, "04", 2);
    }
    else if(lev == 0x08)
    {
        memcpy(conf_content.slot_u.tl, "08", 2);
    }
    else if(lev == 0x0b)
    {
        memcpy(conf_content.slot_u.tl, "0B", 2);
    }
    else if(lev == 0x0f)
    {
        memcpy(conf_content.slot_u.tl, "0F", 2);
    }
    else if(lev == 0xff)
    {
        memcpy(conf_content.slot_u.tl, "FF", 2);
    }
    else
    {
        memcpy(conf_content.slot_u.tl, "00", 2);
    }
}

void get_out_lev(int num, int flag)
{
    int flag_lev = 0;
    //int tmp_lev=0;
    int c_port = 0;
    int i;
    int tmp1, tmp2;
    unsigned char alm[3];
    unsigned short alm_num = 47;
    unsigned char ntfcncde[3];
    unsigned char almcde[3];
	unsigned char rled_en = 0;
	unsigned char yled_en = 0;

    if(strncmp1(rpt_content.slot_o.rb_msmode, "MAIN", 4) == 0)
    {
        if(strncmp1(rpt_content.slot_o.rb_trmode, "FREE", 4) == 0)
        {
            flag_lev = 0x0f;
            rled_en  = 0x01;
        }
        else if(strncmp1(rpt_content.slot_o.rb_trmode, "HOLD", 4) == 0)
        {
            yled_en = 0x01;
   
            if(slot_type[14] == 'K')
            {
                flag_lev = 0x04;
            }
            else if(slot_type[14] == 'R')
            {
                flag_lev = 0x0b;
            }
        }
        else if((strncmp1(rpt_content.slot_o.rb_trmode, "FATR", 4) == 0)
                || (strncmp1(rpt_content.slot_o.rb_trmode, "TRCK", 4) == 0))
        {
            yled_en = 0x01;
            i = rpt_content.slot_o.rb_ref - 0x31;
            if((i >= 0) && (i < 4)) 
            {
                if(strncmp1(rpt_content.slot_o.rb_trmode, "FATR", 4) == 0)
                {
                    flag_lev = 0x04;
                }
                else
                {
                    flag_lev = 0x02;
                }
            }
            else if((i > 3) && (i < 8)) 
            {
                if(conf_content.slot_u.fb == '0')
                {
                    if(i == 4)/* INP-5、INP-6，分别代表RB1、RB2盘的2Mhz输入信号
											INP-7、INP-8，分别代表RB1、RB2盘的2Mbit输入信号
											2hz下发的时钟等级已知，只需转发SSM字节
											*/
                    {
                        //读取寄存器 获取TL


                    }
                    else if(i == 5)
                    {
                        /*NULL*/
                        flag_lev = get_ref_tl(rpt_content.slot_o.rb_tl_2mhz);
                        if(strncmp1(rpt_content.slot_o.rb_trmode, "FATR", 4) == 0)
                        {
                            flag_lev = get_freq_lev(flag_lev);
                        }
                    }
                    else if(i == 6)
                    {

                    }
                    else if(i == 7)
                    {
                        /*NULL*/
                        flag_lev = get_ref_tl(rpt_content.slot_o.rb_tl);
                        if(strncmp1(rpt_content.slot_o.rb_trmode, "FATR", 4) == 0)
                        {
                            flag_lev = get_freq_lev(flag_lev);
                        }
                    }

                }
                else if (conf_content.slot_u.fb == '1') //后面板
                {
                    if(i == 4) //ref1
                    {
                        if((rpt_content.slot[0].ref_mod1[2] > '0') && (rpt_content.slot[0].ref_mod1[2] < '9'))
                        {
                            c_port = rpt_content.slot[0].ref_mod1[2] - 0x31;
                            flag_lev = get_ref_tl(rpt_content.slot[0].ref_tl[c_port]);
                            if(strncmp1(rpt_content.slot_o.rb_trmode, "FATR", 4) == 0)
                            {
                                flag_lev = get_freq_lev(flag_lev);
                            }

                            if('1' == conf_content.slot_u.out_ssm_en)
                            {
                                tmp1 = flag_lev;
                                if(tmp1 == 0)
                                {
                                    tmp1 = 16;
                                }

                                tmp2 = conf_content.slot_u.out_ssm_oth;
                                if(tmp2 == 0)
                                {
                                    tmp2 = 16;
                                }

                                if(tmp1 > tmp2)
                                {
                                    ssm_alm_curr = 1;// 1表示有告警
                                }
                                else
                                {
                                    ssm_alm_curr = 0;
                                }
                                //SSM门限 告警产生
                                if(0 == ssm_alm_last && 1 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x69);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_MN, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_MN_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
									
                                }
                                //SSM门限 告警清除
                                if(1 == ssm_alm_last && 0 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                            else
                            {
                                flag_lev = 0xff;
                                //SSM闭塞时，如果已经产生SSM门限告警，需要清除此告警
                                if(1 == ssm_alm_curr)
                                {
                                    ssm_alm_curr = 0;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                        }
                    }
                    else if(i == 5) //ref2
                    {
                        if((rpt_content.slot[1].ref_mod1[2] > '0') && (rpt_content.slot[1].ref_mod1[2] < '9'))
                        {
                            c_port = rpt_content.slot[1].ref_mod1[2] - 0x31;
                            flag_lev = get_ref_tl(rpt_content.slot[1].ref_tl[c_port]);
                            if(strncmp1(rpt_content.slot_o.rb_trmode, "FATR", 4) == 0)
                            {
                                flag_lev = get_freq_lev(flag_lev);
                            }

                            if('1' == conf_content.slot_u.out_ssm_en)
                            {
                                tmp1 = flag_lev;
                                if(tmp1 == 0)
                                {
                                    tmp1 = 16;
                                }

                                tmp2 = conf_content.slot_u.out_ssm_oth;
                                if(tmp2 == 0)
                                {
                                    tmp2 = 16;
                                }

                                if(tmp1 > tmp2)
                                {
                                    ssm_alm_curr = 1;// 1表示有告警
                                }
                                else
                                {
                                    ssm_alm_curr = 0;
                                }
                                //SSM门限 告警产生
                                if(0 == ssm_alm_last && 1 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x69);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_MN, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_MN_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                                //SSM门限 告警清除
                                if(1 == ssm_alm_last && 0 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                            else
                            {
                                flag_lev = 0xff;
                                //SSM闭塞时，如果已经产生SSM门限告警，需要清除此告警
                                if(1 == ssm_alm_curr)
                                {
                                    ssm_alm_curr = 0;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                printf("ref change %c\n", rpt_content.slot_o.rb_ref);
                return;
            }

        }
		else
		{
		    printf("clock status unknow %s\n",rpt_content.slot_o.rb_trmode);
		    return;
		}

    }
    else if(strncmp1(rpt_content.slot_p.rb_msmode, "MAIN", 4) == 0)
    {
        if(strncmp1(rpt_content.slot_p.rb_trmode, "FREE", 4) == 0)
        {
            rled_en = 0x01;
            flag_lev = 0x0f;
        }
        else if(strncmp1(rpt_content.slot_p.rb_trmode, "HOLD", 4) == 0)
        {
            yled_en = 0x01;
            if(slot_type[15] == 'K')
            {
                flag_lev = 0x04;
            }
            else if(slot_type[15] == 'R')
            {
                flag_lev = 0x0b;
            }
        }
        else if((strncmp1(rpt_content.slot_p.rb_trmode, "FATR", 4) == 0)
                || (strncmp1(rpt_content.slot_p.rb_trmode, "TRCK", 4) == 0))
        {
            yled_en = 0x01;
            i = rpt_content.slot_p.rb_ref - 0x31;
            if((i >= 0) && (i < 4)) //时间
            {
                if(strncmp1(rpt_content.slot_p.rb_trmode, "FATR", 4) == 0)
                {
                    flag_lev = 0x04;
                }
                else
                {
                    flag_lev = 0x02;
                }
            }
            else if((i > 3) && (i < 8)) //pinglv
            {
                if(conf_content.slot_u.fb == '0')
                {
                    if(i == 4)
                    {
                        flag_lev = get_ref_tl(rpt_content.slot_p.rb_tl_2mhz);
                        if(strncmp1(rpt_content.slot_p.rb_trmode, "FATR", 4) == 0)
                        {
                            flag_lev = get_freq_lev(flag_lev);
                        }
                    }
                    else if(i == 5)
                    {

                    }
                    else if(i == 6)
                    {
                        flag_lev = get_ref_tl(rpt_content.slot_p.rb_tl);
                        if(strncmp1(rpt_content.slot_p.rb_trmode, "FATR", 4) == 0)
                        {
                            flag_lev = get_freq_lev(flag_lev);
                        }
                    }
                    else if(i == 7)
                    {

                    }
                }
                else if(conf_content.slot_u.fb == '1') //后面板
                {
                    if(i == 4) //ref1
                    {
                        if((rpt_content.slot[0].ref_mod1[2] > '0') && (rpt_content.slot[0].ref_mod1[2] < '9'))
                        {
                            c_port = rpt_content.slot[0].ref_mod1[2] - 0x31;
                            flag_lev = get_ref_tl(rpt_content.slot[0].ref_tl[c_port]);
                            if(strncmp1(rpt_content.slot_p.rb_trmode, "FATR", 4) == 0)
                            {
                                flag_lev = get_freq_lev(flag_lev);
                            }

                            if('1' == conf_content.slot_u.out_ssm_en)
                            {
                                tmp1 = flag_lev;
                                if(tmp1 == 0)
                                {
                                    tmp1 = 16;
                                }

                                tmp2 = conf_content.slot_u.out_ssm_oth;
                                if(tmp2 == 0)
                                {
                                    tmp2 = 16;
                                }

                                if(tmp1 > tmp2)
                                {
                                    ssm_alm_curr = 1;// 1表示有告警
                                }
                                else
                                {
                                    ssm_alm_curr = 0;
                                }
                                //SSM门限 告警产生
                                if(0 == ssm_alm_last && 1 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x69);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_MN, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_MN_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                                //SSM门限 告警清除
                                if(1 == ssm_alm_last && 0 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                            else
                            {
                                flag_lev = 0xff;
                                //SSM闭塞时，如果已经产生SSM门限告警，需要清除此告警
                                if(1 == ssm_alm_curr)
                                {
                                    ssm_alm_curr = 0;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                        }
                    }
                    else if(i == 5) //ref2
                    {
                        if((rpt_content.slot[1].ref_mod1[2] > '0') && (rpt_content.slot[1].ref_mod1[2] < '9'))
                        {
                            c_port = rpt_content.slot[1].ref_mod1[2] - 0x31;
                            flag_lev = get_ref_tl(rpt_content.slot[1].ref_tl[c_port]);
                            if(strncmp1(rpt_content.slot_p.rb_trmode, "FATR", 4) == 0)
                            {
                                flag_lev = get_freq_lev(flag_lev);
                            }

                            if('1' == conf_content.slot_u.out_ssm_en)
                            {
                                tmp1 = flag_lev;
                                if(tmp1 == 0)
                                {
                                    tmp1 = 16;
                                }

                                tmp2 = conf_content.slot_u.out_ssm_oth;
                                if(tmp2 == 0)
                                {
                                    tmp2 = 16;
                                }

                                if(tmp1 > tmp2)
                                {
                                    ssm_alm_curr = 1;// 1表示有告警
                                }
                                else
                                {
                                    ssm_alm_curr = 0;
                                }
                                //SSM门限 告警产生
                                if(0 == ssm_alm_last && 1 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x69);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_MN, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_MN_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                                //SSM门限 告警清除
                                if(1 == ssm_alm_last && 0 == ssm_alm_curr)
                                {
                                    ssm_alm_last = ssm_alm_curr;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                            else
                            {
                                flag_lev = 0xff;
                                //SSM闭塞时，如果已经产生SSM门限告警，需要清除此告警
                                if(1 == ssm_alm_curr)
                                {
                                    ssm_alm_curr = 0;
                                    alm[0] = ((alm_num & 0x0f) | 0x60);
                                    alm[1] = (((alm_num & 0x30) >> 3) | 0x68);
                                    alm[2] = '\0';
                                    memcpy(ntfcncde, TYPE_CL, 2);
                                    ntfcncde[2] = '\0';
                                    memcpy(almcde, TYPE_CL_A, 2);
                                    almcde[2] = '\0';
                                    rpt_alm_framing('u', ntfcncde, almcde, alm);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                 printf("ref change %c\n", rpt_content.slot_p.rb_ref);
                 return;
          
            }

        }
		else
		{ 
		   printf("clock status unknow %s\n",rpt_content.slot_p.rb_trmode);
		   return;
		}

    }
	else
	{
	  return;
	}

	if(strncmp1(rpt_content.slot_p.rb_msmode, "STBY", 4) == 0)
	{
	     if(strncmp1(rpt_content.slot_p.rb_trmode, "FREE", 4) == 0)
	        rled_en = 0x01;
	     else 
	        yled_en = 0x01;
	
	}
	else if(strncmp1(rpt_content.slot_o.rb_msmode, "STBY", 4) == 0)
	{
	     if(strncmp1(rpt_content.slot_p.rb_trmode, "FREE", 4) == 0)
	        rled_en = 0x01;
	     
	      else
		  	yled_en = 0x01;
	}
	if (rled_en == 0x01)
	{
	   FpgaWrite(ALMControl, 0xfff9);
	   FpgaWrite(IndicateLED,0x0001);
	}
	else
	{
	   FpgaWrite(ALMControl, 0xfffe);
	   FpgaWrite(IndicateLED,0x0002);
	}

    get_out_now_lev_char(flag_lev);

    if(flag == 1) //初始化，单独对一个单盘下发
    {
        send_out_lev(flag_lev, num);
		printf("singal out len slot=%d,lev=%d\n", num, flag_lev);
    }
    else
    {
        if(flag_lev != g_lev)
        {
            g_lev = flag_lev;

            for(i = 0; i < 14; i++)
            {
                //g_lev = get_out_now_lev(rpt_content.slot[i].out_lev);
                if( (slot_type[i] == 'J') ||
                        (slot_type[i] == 'a') ||
                        (slot_type[i] == 'b') ||
                        (slot_type[i] == 'c') ||
                        (slot_type[i] == 'h') ||
                        (slot_type[i] == 'i'))
                {
                    send_out_lev(flag_lev, i);
					printf("all out len slot=%d,lev=%d\n", i, flag_lev);
                }
            }

            //扩展框 时钟等级
            ext_ssm_cfg(conf_content.slot_u.tl);
        }
    }
}



void send_out_lev(int flag_lev, int num)
{
    unsigned char sendbuf[SENDBUFSIZE];
    memset(sendbuf, '\0', SENDBUFSIZE);

    //printf("-------------in send  -------lev=%02x\n",flag_lev);
    switch(flag_lev)
    {
    case 0x00:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::00;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }
    case 0x02:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::02;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }
    case 0x04:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::04;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }
    case 0x08:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::08;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }
    case 0x0b:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::0B;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }
    case 0x0f:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::0F;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }
    case 0xff:
    {
        sprintf((char *)sendbuf, "SET-OUT-LEV:000000::FF;");
        sendtodown(sendbuf, (char)('a' + num));
        break;
    }

    default:
    {
        break;
    }

    }

}

int get_ref_tl(unsigned char c)
{
    int lev = 0;

    if(c == 'a')
    {
        lev = 0x02;
    }
    else if(c == 'b')
    {
        lev = 0x04;
    }
    else if(c == 'c')
    {
        lev = 0x08;
    }
    else if(c == 'd')
    {
        lev = 0x0b;
    }
    else if(c == 'e')
    {
        lev = 0x0f;
    }
    else if(c == 'f')
    {
        lev = 0x00;
    }
    else if(c == 'g')
    {
        lev = 0x00;
    }

    return lev;
}



int send_ref_lev(int num)
{
    int c_port = 0;
    int c_tl = 0;
    int i;
    char ext_ssm[3];

    if((num != 0) && (num != 1))
    {
        return 1;
    }
    if(strncmp1(rpt_content.slot_o.rb_msmode, "MAIN", 4) == 0)
    {
        if((rpt_content.slot_o.rb_ref == '7') && ((num == 0)))
        {
            //先选择跟踪源 路数
            if((rpt_content.slot[0].ref_mod1[2] > '0') && (rpt_content.slot[0].ref_mod1[2] < '9'))
            {
                c_port = rpt_content.slot[0].ref_mod1[2] - 0x31;
                c_tl = get_ref_tl(rpt_content.slot[0].ref_tl[c_port]);
            }
            else
            {
                return 1;
            }
        }
        else if((rpt_content.slot_o.rb_ref == '8') && ((num == 1)))
        {
            if((rpt_content.slot[1].ref_mod1[2] > '0') && (rpt_content.slot[1].ref_mod1[2] < '9'))
            {
                c_port = rpt_content.slot[1].ref_mod1[2] - 0x31;
                c_tl = get_ref_tl(rpt_content.slot[1].ref_tl[c_port]);
            }
            else
            {
                return 1;
            }
        }
        else
        {
            return 1;
        }
    }
    else if(strncmp1(rpt_content.slot_p.rb_msmode, "MAIN", 4) == 0)
    {
        if((rpt_content.slot_p.rb_ref == '7') && ((num == 0)))
        {
            //先选择跟踪源 路数
            if((rpt_content.slot[0].ref_mod1[2] > '0') && (rpt_content.slot[0].ref_mod1[2] < '9'))
            {
                c_port = rpt_content.slot[0].ref_mod1[2] - 0x31;
                c_tl = get_ref_tl(rpt_content.slot[0].ref_tl[c_port]);
            }
            else
            {
                return 1;
            }
        }
        else if((rpt_content.slot_p.rb_ref == '8') && ((num == 1)))
        {
            if((rpt_content.slot[1].ref_mod1[2] > '0') && (rpt_content.slot[1].ref_mod1[2] < '9'))
            {
                c_port = rpt_content.slot[1].ref_mod1[2] - 0x31;
                c_tl = get_ref_tl(rpt_content.slot[1].ref_tl[c_port]);

            }
            else
            {
                return 1;
            }
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 1;
    }

    if(c_tl != g_lev)			//再比较是否改变
    {
        g_lev = c_tl;
        for(i = 0; i < 14; i++)
        {
            if( (slot_type[i] == 'J') ||
                    (slot_type[i] == 'a') ||
                    (slot_type[i] == 'b') ||
                    (slot_type[i] == 'c') ||
                    (slot_type[i] == 'h') ||
                    (slot_type[i] == 'i') )
            {
                send_out_lev(c_tl, i);
            }
        }

        memset(ext_ssm, 0, 3);
        if(0x00 == c_tl)
        {
            memcpy(ext_ssm, "00", 2);
        }
        else if(0x02 == c_tl)
        {
            memcpy(ext_ssm, "02", 2);
        }
        else if(0x04 == c_tl)
        {
            memcpy(ext_ssm, "04", 2);
        }
        else if(0x08 == c_tl)
        {
            memcpy(ext_ssm, "08", 2);
        }
        else if(0x0B == c_tl)
        {
            memcpy(ext_ssm, "0B", 2);
        }
        else if(0x0F == c_tl)
        {
            memcpy(ext_ssm, "0F", 2);
        }
        else if(0xFF == c_tl)
        {
            memcpy(ext_ssm, "FF", 2);
        }
        else
        {
            memcpy(ext_ssm, "00", 2);
        }

        //扩展框 时钟等级
        ext_ssm_cfg(ext_ssm);
    }

    return 0;

}

int file_exists(char *filename)
{
    return (access(filename, 0) == 0);
}

/*
int find_config()
{
	struct dirent *ptr;
	DIR *dir;
	dir=opendir("/usr/");
	while((ptr=readdir(dir))!=NULL)
	{
		if(ptr->d_name[0] == '.')
		continue;
		if(strncmp1(ptr->d_name,"LT",2)==0)
			{
				memset(exist_filename,0,50);
				sprintf((char *)exist_filename,"/usr/%s",ptr->d_name);
				closedir(dir);
				return 1;
			}
	}
	closedir(dir);
	return 0;
}*/







/*
  1	成功
  0	失败
*/
int ext_alm_cmp(u8_t ext_sid, u8_t *ext_new_data, struct extctx *ctx)
{
    u8_t ext_old_alm[16];
    int i, j;

    memset(ext_old_alm, 0, 16);

    if((ext_sid >= EXT_1_OUT_LOWER) && (ext_sid <= EXT_1_OUT_UPPER))
    {
        memcpy(ext_old_alm, ctx->out[ext_sid - EXT_1_OUT_OFFSET].outAlm, 5);
        if(0 != memcmp(ext_old_alm, ext_new_data, 5))
        {
            for(i = 0; i < 5; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('1', ext_sid - EXT_1_OUT_OFFSET + 97, 4 * i + j + 1, "**", "MJ");
						//printf("ext out1 alarm happened %d\r\n",4 * i + j + 1);
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('1', ext_sid - EXT_1_OUT_OFFSET + 97, 4 * i + j + 1, "A ", "CL");
						//printf("ext out1 alarm clear %d\r\n",4 * i + j + 1);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if((ext_sid >= EXT_2_OUT_LOWER) && (ext_sid <= EXT_2_OUT_UPPER))
    {
        memcpy(ext_old_alm, ctx->out[ext_sid - EXT_2_OUT_OFFSET].outAlm, 5);
        if(0 != memcmp(ext_old_alm, ext_new_data, 5))
        {
            for(i = 0; i < 5; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('2', ext_sid - EXT_2_OUT_OFFSET - 10 + 97, 4 * i + j + 1, "**", "MJ");
						//printf("ext out2 alarm happened %d\r\n",4 * i + j + 1);
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('2', ext_sid - EXT_2_OUT_OFFSET - 10 + 97, 4 * i + j + 1, "A ", "CL");
						//printf("ext out2 alarm clear %d\r\n",4 * i + j + 1);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if((ext_sid >= EXT_3_OUT_LOWER) && (ext_sid <= EXT_3_OUT_UPPER))
    {
        if(ext_sid < 58)//except ':;'
        {
            memcpy(ext_old_alm, ctx->out[ext_sid - EXT_3_OUT_OFFSET].outAlm, 5);
            if(0 != memcmp(ext_old_alm, ext_new_data, 5))
            {
                for(i = 0; i < 5; i++)
                {
                    for(j = 0; j < 4; j++)
                    {
                        if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                        {
                            ext_alm_evt('3', ext_sid - EXT_3_OUT_OFFSET - 20 + 97, 4 * i + j + 1, "**", "MJ");
							//printf("ext out3 alarm happened %d\r\n",4 * i + j + 1);
                        }
                        else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                        {
                            ext_alm_evt('3', ext_sid - EXT_3_OUT_OFFSET - 20 + 97, 4 * i + j + 1, "A ", "CL");
							//printf("ext out3 alarm clear %d\r\n",4 * i + j + 1);
                        }
                        else
                        {
                            //do nothing
                        }
                    }
                }
            }
        }
        else
        {
            memcpy(ext_old_alm, ctx->out[ext_sid - EXT_3_OUT_OFFSET - 2].outAlm, 5);
            if(0 != memcmp(ext_old_alm, ext_new_data, 5))
            {
                for(i = 0; i < 5; i++)
                {
                    for(j = 0; j < 4; j++)
                    {
                        if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                        {
                            ext_alm_evt('3', ext_sid - EXT_3_OUT_OFFSET - 22 + 97, 4 * i + j + 1, "**", "MJ");
							//printf("ext out3 alarm happened %d\r\n",4 * i + j + 1);
                        }
                        else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                        {
                            ext_alm_evt('3', ext_sid - EXT_3_OUT_OFFSET - 22 + 97, 4 * i + j + 1, "A ", "CL");
							//printf("ext out3 alarm clear %d\r\n",4 * i + j + 1);
                        }
                        else
                        {
                            //do nothing
                        }
                    }
                }
            }
        }
    }
    else if(EXT_1_MGR_PRI == ext_sid)
    {
        memcpy(ext_old_alm, ctx->mgr[0].mgrAlm, MGE_RALM_NUM);
        if(0 != memcmp(ext_old_alm, ext_new_data, MGE_RALM_NUM))
        {
            for(i = 0; i < MGE_RALM_NUM; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('1', 'k', 4 * i + j + 1, "**", "MJ");
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('1', 'k', 4 * i + j + 1, "A ", "CL");
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if(EXT_1_MGR_RSV == ext_sid)
    {
        memcpy(ext_old_alm, ctx->mgr[1].mgrAlm, MGE_RALM_NUM);
        if(0 != memcmp(ext_old_alm, ext_new_data, MGE_RALM_NUM))
        {
            for(i = 0; i < MGE_RALM_NUM; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('1', 'l', 4 * i + j + 1, "**", "MJ");
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('1', 'l', 4 * i + j + 1, "A ", "CL");
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if(EXT_2_MGR_PRI == ext_sid)
    {
        memcpy(ext_old_alm, ctx->mgr[2].mgrAlm, MGE_RALM_NUM);
        if(0 != memcmp(ext_old_alm, ext_new_data, MGE_RALM_NUM))
        {
            for(i = 0; i < MGE_RALM_NUM; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('2', 'k', 4 * i + j + 1, "**", "MJ");
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('2', 'k', 4 * i + j + 1, "A ", "CL");
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if(EXT_2_MGR_RSV == ext_sid)
    {
        memcpy(ext_old_alm, ctx->mgr[3].mgrAlm, MGE_RALM_NUM);
        if(0 != memcmp(ext_old_alm, ext_new_data, MGE_RALM_NUM))
        {
            for(i = 0; i < MGE_RALM_NUM; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('2', 'l', 4 * i + j + 1, "**", "MJ");
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('2', 'l', 4 * i + j + 1, "A ", "CL");
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if(EXT_3_MGR_PRI == ext_sid)
    {
        memcpy(ext_old_alm, ctx->mgr[4].mgrAlm, MGE_RALM_NUM);
        if(0 != memcmp(ext_old_alm, ext_new_data, MGE_RALM_NUM))
        {
            for(i = 0; i < MGE_RALM_NUM; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('3', 'k', 4 * i + j + 1, "**", "MJ");
						//printf("ext mgr3 alarm happened %d\r\n",4 * i + j + 1);
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('3', 'k', 4 * i + j + 1, "A ", "CL");
						//printf("ext mgr3 alarm clear %d\r\n",4 * i + j + 1);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else if(EXT_3_MGR_RSV == ext_sid)
    {
        memcpy(ext_old_alm, ctx->mgr[5].mgrAlm, MGE_RALM_NUM);
        if(0 != memcmp(ext_old_alm, ext_new_data, MGE_RALM_NUM))
        {
            for(i = 0; i < MGE_RALM_NUM; i++)
            {
                for(j = 0; j < 4; j++)
                {
                    if( (0 == ((ext_old_alm[i])&BIT(j))) && (0 != ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('3', 'l', 4 * i + j + 1, "**", "MJ");
						//printf("ext mge3 alarm happened %d\r\n",4 * i + j + 1);
                    }
                    else if( (0 != ((ext_old_alm[i])&BIT(j))) && (0 == ((ext_new_data[i])&BIT(j))) )
                    {
                        ext_alm_evt('3', 'l', 4 * i + j + 1, "A ", "CL");
						//printf("ext mge3 alarm clear %d\r\n",4 * i + j + 1);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}






/*
  1	成功
  0	失败
*/
int ext_alm_evt(u8_t eid, u8_t sid, u8_t almid, u8_t *agcc1, u8_t *agcc2)
{
    u8_t sendbuf[1024];
    signed char offset;
    u8_t onlinesta;

    offset = eid - '1';
    onlinesta = gExtCtx.save.onlineSta;
    if( offset > 2 || offset < 0)
    {
        return 0;
    }
    else
    {
        if( ((onlinesta >> offset) & 0x01) == 0)
        {
            return 0;
        }
        else
        {
            //lalalalala;
        }
    }


    memset(sendbuf, 0, 1024);
    get_time_fromnet();
    sprintf((char *)sendbuf, "\r\n\n   %s %s %s\r\n%s %06d EXT ALM\r\n   \"%c,%c,%03d,%s\"\r\n;",
            conf_content.slot_u.tid, mcp_date, mcp_time, agcc1, atag,
            eid, sid, almid, agcc2);
    
    sendto_all_client(sendbuf);
	//printf("send ext alm %s\r\n",sendbuf);
    atag++;
    if(atag >= 0xffff)
    {
        atag = 0;
    }

    return 1;
}





/*
  1	成功
  0	失败
*/
int ext_bpp_cfg(u8_t ext_sid, u8_t *ext_new_type, struct extctx *ctx)
{
    u8_t ext_old_type[15];
    int i;
    u8_t tmp_ctag[7];
    u8_t sendbuf[256];
    u8_t slotid;

    memset(ext_old_type, 0, 15);
    memset(tmp_ctag, 0, 7);
    memcpy(tmp_ctag, "000000", 6);

    if((EXT_1_MGR_PRI == ext_sid) || (EXT_1_MGR_RSV == ext_sid))
    {
        memcpy(ext_old_type, ctx->extBid[0], 14);
        for(i = 0; i < 10; i++)
        {
            if( (EXT_NONE == ext_old_type[i]) &&
                    ((EXT_OUT16 == ext_new_type[i]) || (EXT_OUT32 == ext_new_type[i]) || (EXT_OUT16S == ext_new_type[i]) || (EXT_OUT32S == ext_new_type[i])) )
            {
                // lev
                printf("1+%d+%s\n", i + 1, ctx->save.outSsm[i]);
                if(2 == strlen(ctx->save.outSsm[i]))
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                            i + EXT_1_OUT_LOWER,
                            tmp_ctag,
                            ctx->save.outSsm[i]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_1_OUT_LOWER, tmp_ctag);
                }

                //signal
                printf("1+%d+%s\n", i + 1, ctx->save.outSignal[i]);
                if(16 == strlen(ctx->save.outSignal[i]))
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                            i + EXT_1_OUT_LOWER,
                            tmp_ctag,
                            ctx->save.outSignal[i]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_1_OUT_LOWER, tmp_ctag);
                }

                //mod
                printf("1+%d+%s\n", i + 1, ctx->save.outPR[i]);
                if('0' == ctx->save.outPR[i][0] || '1' == ctx->save.outPR[i][0])
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%c;",
                            i + EXT_1_OUT_LOWER,
                            tmp_ctag,
                            ctx->save.outPR[i][0]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_1_OUT_LOWER, tmp_ctag);
                }
            }
        }
    }
    else if((EXT_2_MGR_PRI == ext_sid) || (EXT_2_MGR_RSV == ext_sid))
    {
        memcpy(ext_old_type, ctx->extBid[1], 14);
        for(i = 0; i < 10; i++)
        {
            if( (EXT_NONE == ext_old_type[i]) &&
                    ((EXT_OUT16 == ext_new_type[i]) || (EXT_OUT32 == ext_new_type[i]) || (EXT_OUT16S == ext_new_type[i]) || (EXT_OUT32S == ext_new_type[i])) )
            {
                //lev
                printf("2+%d+%s\n", i + 1, ctx->save.outSsm[10 + i]);
                if(2 == strlen(ctx->save.outSsm[10 + i]))
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                            i + EXT_2_OUT_LOWER,
                            tmp_ctag,
                            ctx->save.outSsm[10 + i]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_2_OUT_LOWER, tmp_ctag);
                }

                //signal
                printf("2+%d+%s\n", i + 1, ctx->save.outSignal[10 + i]);
                if(16 == strlen(ctx->save.outSignal[10 + i]))
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                            i + EXT_2_OUT_LOWER,
                            tmp_ctag,
                            ctx->save.outSignal[10 + i]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_2_OUT_LOWER, tmp_ctag);
                }

                //mod
                printf("2+%d+%s\n", i + 1, ctx->save.outPR[10 + i]);
                if('0' == ctx->save.outPR[10 + i][0] || '1' == ctx->save.outPR[10 + i][0])
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%c;",
                            i + EXT_2_OUT_LOWER,
                            tmp_ctag,
                            ctx->save.outPR[10 + i][0]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_2_OUT_LOWER, tmp_ctag);
                }
            }
        }
    }
    else if((EXT_3_MGR_PRI == ext_sid) || (EXT_3_MGR_RSV == ext_sid))
    {
        memcpy(ext_old_type, ctx->extBid[2], 14);
        for(i = 0; i < 10; i++)
        {
            if( (EXT_NONE == ext_old_type[i]) &&
                    ((EXT_OUT16 == ext_new_type[i]) || (EXT_OUT32 == ext_new_type[i]) || (EXT_OUT16S == ext_new_type[i]) || (EXT_OUT32S == ext_new_type[i])) )
            {
                if((i + EXT_3_OUT_LOWER) >= ':')//except ':;'
                {
                    slotid = i + EXT_3_OUT_LOWER + 2;
                }
                else
                {
                    slotid = i + EXT_3_OUT_LOWER;
                }

                //lev
                printf("3+%d+%s\n", i + 1, ctx->save.outSsm[20 + i]);
                if(2 == strlen(ctx->save.outSsm[20 + i]))
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                            slotid,
                            tmp_ctag,
                            ctx->save.outSsm[20 + i]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, tmp_ctag);
                }

                //signal
                printf("3+%d+%s\n", i + 1, ctx->save.outSignal[20 + i]);
                if(16 == strlen(ctx->save.outSignal[20 + i]))
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-TYP:%c:%s::%s;",
                            slotid,
                            tmp_ctag,
                            ctx->save.outSignal[20 + i]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, tmp_ctag);
                }

                //mod
                printf("3+%d+%s\n", i + 1, ctx->save.outPR[20 + i]);
                if('0' == ctx->save.outPR[20 + i][0] || '1' == ctx->save.outPR[20 + i][0])
                {
                    memset(sendbuf, 0, 256);
                    sprintf((char *)sendbuf, "SET-OUT-MOD:%c:%s::%c;",
                            slotid,
                            tmp_ctag,
                            ctx->save.outPR[20 + i][0]);
                    ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, tmp_ctag);
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return 1;
}






int ethn_down(const char *networkcard)
{
    int sock_fd;
    struct ifreq ifr;

    if ( ( sock_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    {
        close(sock_fd);
        return (0);
    }
    memset( &ifr, 0, sizeof(struct ifreq) );
    strncpy( ifr.ifr_name, networkcard, IFNAMSIZ );
    if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0)
    {
        close(sock_fd);
        return (0);
    }

    ifr.ifr_flags &= (~IFF_UP);
    if ( ioctl ( sock_fd, SIOCSIFFLAGS, &ifr) < 0 )
    {
        close(sock_fd);
        return (0);
    }

    close(sock_fd);
    return (1);
}



int ethn_up(const char *networkcard)
{
    int sock_fd;
    struct ifreq ifr;

    if ( ( sock_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    {
        close(sock_fd);
        return (0);
    }
    memset( &ifr, 0, sizeof(struct ifreq) );
    strncpy( ifr.ifr_name, networkcard, IFNAMSIZ );
    if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0)
    {
        close(sock_fd);
        return (0);
    }

    ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
    if ( ioctl ( sock_fd, SIOCSIFFLAGS, &ifr) < 0 )
    {
        close(sock_fd);
        return (0);
    }

    close(sock_fd);
    return (1);
}



int mcp_get_gateway( char *local_gateway )
{
    char buf[1024];
    int pl = 0, ph = 0;
    int i;
    FILE *readfp;

    memset( buf, 0, sizeof(buf) );
    readfp = popen( "route -n", "r" );
    if ( NULL == readfp )
    {
        printf("route -n is NULL\n");
        pclose( readfp );
        return(0);
    }

    if ( 0 == fread( buf, sizeof(char), sizeof(buf),  readfp) )
    {
        pclose( readfp );
        return (0);
    }

    for( i = 0; i < 1024; i++ )
    {
        if ( buf[i] == 'U' && buf[i + 1] == 'G' && (i + 1) < 1024 )
        {
            pl = i;
            break;
        }
    }
    if( pl == 0 )
    {
        pclose( readfp );
        return(0);
    }

    while ( buf[pl] > '9' || buf[pl] < '0' )
    {
        pl--;
    }
    while ( (buf[pl] >= '0' && buf[pl] <= '9') || buf[pl] == '.')
    {
        pl--;
    }
    while ( buf[pl] > '9' || buf[pl] < '0' )
    {
        pl--;
    }
    buf[pl + 1] = '\0';

    for ( i = pl; ((buf[i] >= '0' && buf[i] <= '9') || buf[i] == '.'); i--);
    ph = i + 1;
    strcpy(local_gateway, &buf[ph]);
    //printf("<mcp_get_gateway>:%s\n",local_gateway);
    pclose( readfp );

    return (1);
}



int mcp_set_gateway( char *networkcard, char *old_gateway, char *new_gateway )
{
    int sock_fd = -1;
    struct rtentry rt;
    struct sockaddr rtsockaddr;
    struct sockaddr_in	*sin = NULL;

    if ( (sock_fd = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
    {
        close(sock_fd);
        return(0);
    }

    memset(&rt, 0, sizeof(struct rtentry));
    memset(&rtsockaddr, 0, sizeof(struct sockaddr));

    sin = (struct sockaddr_in *)&rtsockaddr;
    sin->sin_family = AF_INET;
    sin->sin_addr.s_addr = INADDR_ANY;

    rt.rt_dst = rtsockaddr;		//set the destination address to '0.0.0.0'
    rt.rt_genmask = rtsockaddr;	//set the netmask to '0.0.0.0'

    //Fill in the other fields.
    rt.rt_flags = (RTF_UP | RTF_GATEWAY);
    rt.rt_metric = 0;
    rt.rt_dev = networkcard;

    //delete the current default gateway.
    sin->sin_addr.s_addr = inet_addr(old_gateway);
    rt.rt_gateway = rtsockaddr;

    if (ioctl(sock_fd, SIOCDELRT, &rt) < 0)
    {
        if( errno != ESRCH)
        {
            close(sock_fd);
            return(0);
        }
    }
    //set the new default gateway.
    sin->sin_addr.s_addr = inet_addr(new_gateway);
    rt.rt_gateway = rtsockaddr;

    if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
    {
        close(sock_fd);
        return(0);
    }

    close(sock_fd);
    return(1);
}




void mcpd_reset_network_card(void)
{
    int retval = 0;
    unsigned char gateway[16];

    memset(gateway, 0, sizeof(gateway));

    retval = mcp_get_gateway(gateway);
    if( 1 == retval )
    {
        printf("reset_network_card: 1\n");
        if ( 1 == ethn_down("eth0"))
        {
            printf("eth0 down success!!!\n");
            while ( 0 == ethn_up("eth0") )
            {
                printf("eth0 up failure!!!\n");
            }
            printf("eth0 up success!!!\n");

            mcp_set_gateway(TCP_NETWORK_CARD, gateway, gateway);
        }
        else
        {
            if(print_switch == 0)
            {
                printf("eth0 down failure!!!\n");
            }
        }
    }
    else//2014-1-24添加else语句，未获取成功时，使用flash中保存的网关
    {
        printf("reset_network_card: 0\n");
        memcpy(gateway, conf_content.slot_u.gate, 16);
        if ( 1 == ethn_down("eth0"))
        {
            printf("eth0 down success!!!\n");
            while ( 0 == ethn_up("eth0") )
            {
                printf("eth0 up failure!!!\n");
            }
            printf("eth0 up success!!!\n");

            mcp_set_gateway(TCP_NETWORK_CARD, gateway, gateway);
        }
        else
        {
            if(print_switch == 0)
            {
                printf("eth0 down failure!!!\n");
            }
        }

    }
}






/*
  1	成功
  0	失败
*/
int ext_drv_mgr_evt(struct extctx *ctx)
{
    u8_t sendbuf[1024];
    u8_t aid;

    memset(ctx->new_drv_mgr_sta, 0, 32);
    sprintf((char *) ctx->new_drv_mgr_sta,
             "%d%d%d%d%d%d,%d%d%d%d%d%d",
             ctx->drv[0].drvPR[0], ctx->drv[1].drvPR[0],
             ctx->drv[0].drvPR[1], ctx->drv[1].drvPR[1],
             ctx->drv[0].drvPR[2], ctx->drv[1].drvPR[2],
             ctx->mgr[0].mgrPR, ctx->mgr[1].mgrPR,
             ctx->mgr[2].mgrPR, ctx->mgr[3].mgrPR,
             ctx->mgr[4].mgrPR, ctx->mgr[5].mgrPR );

    if(0 != strcmp(ctx->old_drv_mgr_sta, ctx->new_drv_mgr_sta))
    {
        memcpy(ctx->old_drv_mgr_sta, ctx->new_drv_mgr_sta, 13);
        memset(sendbuf, 0, 1024);
        get_time_fromnet();

        if('d' == slot_type[12])
        {
            aid = 'm';
        }
        else if('d' == slot_type[13])
        {
            aid = 'n';
        }
        else
        {
            aid = 'm';
        }

        sprintf((char *)sendbuf, "\r\n\n   %s %s %s\r\nA  %06d REPT EVNT CDM\r\n   \"%c:NA,%s,NSA,AUTO:\\\"\\\"\"\r\n;",
                conf_content.slot_u.tid, mcp_date, mcp_time,
                atag, aid,
                ctx->new_drv_mgr_sta);

        sendto_all_client(sendbuf);
        atag++;
        if(atag >= 0xffff)
        {
            atag = 0;
        }
    }

    return 1;
}








void ext_bpp_evt(int aid, char bid)
{
    unsigned char tmp[1024];

    memset(tmp, '\0', 1024);
    get_time_fromnet();
    sprintf((char *)tmp, "%c%c%c%c%c%c%s%c", CR, LF, LF, SP, SP, SP, conf_content.slot_u.tid, SP);
    sprintf((char *)tmp, "%s%s%c%s%c%c", tmp, mcp_date, SP, mcp_time, CR, LF);
    sprintf((char *)tmp, "%sA%c%c%06d%cREPT%cEVNT%cC91%c%c", tmp, SP, SP, atag, SP, SP, SP, CR, LF);
    sprintf((char *)tmp, "%s%c%c%c\"%c:NA,%c,NSA,AUTO:\\\"\\\"\"%c%c;",
            tmp, SP, SP, SP, aid, bid, CR, LF);

    sendto_all_client(tmp);
	//printf("Send C91 %s\n",tmp);
    atag++;
    if(atag >= 0xffff)
    {
        atag = 0;
    }
}






/*
  1	成功
  0	失败
*/
int ext_bid_cmp(u8_t ext_sid, u8_t *ext_new_data, struct extctx *ctx)
{
    u8_t ext_old_bid[16];
    u32_t tmp;
    u8_t bppa[8];
    int i;

    memset(ext_old_bid, 0, 16);

    if(EXT_1_MGR_PRI == ext_sid)
    {
        tmp = 0;
        memcpy(ext_old_bid, ctx->extBid[0], 14);
        if(0 != memcmp(ext_old_bid, ext_new_data, 14))
        {
            for(i = 0; i < 14; i++)
            {
                if(ext_old_bid[i] != ext_new_data[i])
                {
                    if(EXT_NONE == ext_old_bid[i] && EXT_NONE != ext_new_data[i])
                    {
                        tmp &= (~BIT(i));
                        ext_alm_evt('1', 'k', 13 + i, "A ", "CL");
                    }
                    else if(EXT_NONE != ext_old_bid[i] && EXT_NONE == ext_new_data[i])
                    {
                        tmp |= BIT(i);
                        ext_alm_evt('1', 'k', 13 + i, "**", "MJ");
                    }
                    else
                    {
                        //do nothing
                    }

                    if(i < 10)
                    {
                        ext_bpp_evt(23 + i, ext_new_data[i]);
                    }
                    else if(10 == i)
                    {
                        ext_bpp_evt(EXT_1_MGR_PRI, ext_new_data[i]);
                    }
                    else if(11 == i)
                    {
                        ext_bpp_evt(EXT_1_MGR_RSV, ext_new_data[i]);
                    }
                    else if(12 == i)
                    {
                        ext_bpp_evt(EXT_1_PWR1, ext_new_data[i]);
                    }
                    else if(13 == i)
                    {
                        ext_bpp_evt(EXT_1_PWR2, ext_new_data[i]);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }

            memset(bppa, 0, 8);
            bppa[0] = 0x40 | (tmp & 0x0000000F);
            bppa[1] = 0x40 | ((tmp >> 4) & 0x0000000F);
            bppa[2] = 0x40 | ((tmp >> 8) & 0x0000000F);
            bppa[3] = 0x40 | ((tmp >> 12) & 0x0000000F);
            memcpy(&ctx->mgr[0].mgrAlm[3], bppa, 4);
        }
    }
    else if(EXT_1_MGR_RSV == ext_sid)
    {
        tmp = 0;
        memcpy(ext_old_bid, ctx->extBid[0], 14);
        if(0 != memcmp(ext_old_bid, ext_new_data, 14))
        {
            for(i = 0; i < 14; i++)
            {
                if(ext_old_bid[i] != ext_new_data[i])
                {
                    if(EXT_NONE == ext_old_bid[i] && EXT_NONE != ext_new_data[i])
                    {
                        tmp &= (~BIT(i));
                        ext_alm_evt('1', 'l', 13 + i, "A ", "CL");
                    }
                    else if(EXT_NONE != ext_old_bid[i] && EXT_NONE == ext_new_data[i])
                    {
                        tmp |= BIT(i);
                        ext_alm_evt('1', 'l', 13 + i, "**", "MJ");
                    }
                    else
                    {
                        //do nothing
                    }

                    if(i < 10)
                    {
                        ext_bpp_evt(23 + i, ext_new_data[i]);
                    }
                    else if(10 == i)
                    {
                        ext_bpp_evt(EXT_1_MGR_PRI, ext_new_data[i]);
                    }
                    else if(11 == i)
                    {
                        ext_bpp_evt(EXT_1_MGR_RSV, ext_new_data[i]);
                    }
                    else if(12 == i)
                    {
                        ext_bpp_evt(EXT_1_PWR1, ext_new_data[i]);
                    }
                    else if(13 == i)
                    {
                        ext_bpp_evt(EXT_1_PWR2, ext_new_data[i]);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }

            memset(bppa, 0, 8);
            bppa[0] = 0x40 | (tmp & 0x0000000F);
            bppa[1] = 0x40 | ((tmp >> 4) & 0x0000000F);
            bppa[2] = 0x40 | ((tmp >> 8) & 0x0000000F);
            bppa[3] = 0x40 | ((tmp >> 12) & 0x0000000F);
            memcpy(&ctx->mgr[1].mgrAlm[3], bppa, 4);
        }
    }
    else if(EXT_2_MGR_PRI == ext_sid)
    {
        tmp = 0;
        memcpy(ext_old_bid, ctx->extBid[1], 14);
        if(0 != memcmp(ext_old_bid, ext_new_data, 14))
        {
            for(i = 0; i < 14; i++)
            {
                if(ext_old_bid[i] != ext_new_data[i])
                {
                    if(EXT_NONE == ext_old_bid[i] && EXT_NONE != ext_new_data[i])
                    {
                        tmp &= (~BIT(i));
                        ext_alm_evt('2', 'k', 13 + i, "A ", "CL");
                    }
                    else if(EXT_NONE != ext_old_bid[i] && EXT_NONE == ext_new_data[i])
                    {
                        tmp |= BIT(i);
                        ext_alm_evt('2', 'k', 13 + i, "**", "MJ");
                    }
                    else
                    {
                        //do nothing
                    }

                    if(i < 10)
                    {
                        ext_bpp_evt(39 + i, ext_new_data[i]);
                    }
                    else if(10 == i)
                    {
                        ext_bpp_evt(EXT_2_MGR_PRI, ext_new_data[i]);
                    }
                    else if(11 == i)
                    {
                        ext_bpp_evt(EXT_2_MGR_RSV, ext_new_data[i]);
                    }
                    else if(12 == i)
                    {
                        ext_bpp_evt(EXT_2_PWR1, ext_new_data[i]);
                    }
                    else if(13 == i)
                    {
                        ext_bpp_evt(EXT_2_PWR2, ext_new_data[i]);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }

            memset(bppa, 0, 8);
            bppa[0] = 0x40 | (tmp & 0x0000000F);
            bppa[1] = 0x40 | ((tmp >> 4) & 0x0000000F);
            bppa[2] = 0x40 | ((tmp >> 8) & 0x0000000F);
            bppa[3] = 0x40 | ((tmp >> 12) & 0x0000000F);
            memcpy(&ctx->mgr[2].mgrAlm[3], bppa, 4);
        }
    }
    else if(EXT_2_MGR_RSV == ext_sid)
    {
        tmp = 0;
        memcpy(ext_old_bid, ctx->extBid[1], 14);
        if(0 != memcmp(ext_old_bid, ext_new_data, 14))
        {
            for(i = 0; i < 14; i++)
            {
                if(ext_old_bid[i] != ext_new_data[i])
                {
                    if(EXT_NONE == ext_old_bid[i] && EXT_NONE != ext_new_data[i])
                    {
                        tmp &= (~BIT(i));
                        ext_alm_evt('2', 'l', 13 + i, "A ", "CL");
                    }
                    else if(EXT_NONE != ext_old_bid[i] && EXT_NONE == ext_new_data[i])
                    {
                        tmp |= BIT(i);
                        ext_alm_evt('2', 'l', 13 + i, "**", "MJ");
                    }
                    else
                    {
                        //do nothing
                    }

                    if(i < 10)
                    {
                        ext_bpp_evt(39 + i, ext_new_data[i]);
                    }
                    else if(10 == i)
                    {
                        ext_bpp_evt(EXT_2_MGR_PRI, ext_new_data[i]);
                    }
                    else if(11 == i)
                    {
                        ext_bpp_evt(EXT_2_MGR_RSV, ext_new_data[i]);
                    }
                    else if(12 == i)
                    {
                        ext_bpp_evt(EXT_2_PWR1, ext_new_data[i]);
                    }
                    else if(13 == i)
                    {
                        ext_bpp_evt(EXT_2_PWR2, ext_new_data[i]);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }

            memset(bppa, 0, 8);
            bppa[0] = 0x40 | (tmp & 0x0000000F);
            bppa[1] = 0x40 | ((tmp >> 4) & 0x0000000F);
            bppa[2] = 0x40 | ((tmp >> 8) & 0x0000000F);
            bppa[3] = 0x40 | ((tmp >> 12) & 0x0000000F);
            memcpy(&ctx->mgr[3].mgrAlm[3], bppa, 4);
        }
    }
    else if(EXT_3_MGR_PRI == ext_sid)
    {
        tmp = 0;
        memcpy(ext_old_bid, ctx->extBid[2], 14);
        if(0 != memcmp(ext_old_bid, ext_new_data, 14))
        {
            for(i = 0; i < 14; i++)
            {
                if(ext_old_bid[i] != ext_new_data[i])
                {
                    if(EXT_NONE == ext_old_bid[i] && EXT_NONE != ext_new_data[i])
                    {
                        tmp &= (~BIT(i));
                        ext_alm_evt('3', 'k', 13 + i, "A ", "CL");
                    }
                    else if(EXT_NONE != ext_old_bid[i] && EXT_NONE == ext_new_data[i])
                    {
                        tmp |= BIT(i);
                        ext_alm_evt('3', 'k', 13 + i, "**", "MJ");
                    }
                    else
                    {
                        //do nothing
                    }

                    if(i < 3)
                    {
                        ext_bpp_evt(55 + i, ext_new_data[i]);
                    }
                    else if(i >= 3 && i < 10)
                    {
                        ext_bpp_evt(57 + i, ext_new_data[i]);
                    }
                    else if(10 == i)
                    {
                        ext_bpp_evt(EXT_3_MGR_PRI, ext_new_data[i]);
                    }
                    else if(11 == i)
                    {
                        ext_bpp_evt(EXT_3_MGR_RSV, ext_new_data[i]);
                    }
                    else if(12 == i)
                    {
                        ext_bpp_evt(EXT_3_PWR1, ext_new_data[i]);
                    }
                    else if(13 == i)
                    {
                        ext_bpp_evt(EXT_3_PWR2, ext_new_data[i]);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }

            memset(bppa, 0, 8);
            bppa[0] = 0x40 | (tmp & 0x0000000F);
            bppa[1] = 0x40 | ((tmp >> 4) & 0x0000000F);
            bppa[2] = 0x40 | ((tmp >> 8) & 0x0000000F);
            bppa[3] = 0x40 | ((tmp >> 12) & 0x0000000F);
            memcpy(&ctx->mgr[4].mgrAlm[3], bppa, 4);
        }
    }
    else if(EXT_3_MGR_RSV == ext_sid)
    {
        tmp = 0;
        memcpy(ext_old_bid, ctx->extBid[2], 14);
        if(0 != memcmp(ext_old_bid, ext_new_data, 14))
        {
            for(i = 0; i < 14; i++)
            {
                if(ext_old_bid[i] != ext_new_data[i])
                {
                    if(EXT_NONE == ext_old_bid[i] && EXT_NONE != ext_new_data[i])
                    {
                        tmp &= (~BIT(i));
                        ext_alm_evt('3', 'l', 13 + i, "A ", "CL");
                    }
                    else if(EXT_NONE != ext_old_bid[i] && EXT_NONE == ext_new_data[i])
                    {
                        tmp |= BIT(i);
                        ext_alm_evt('3', 'l', 13 + i, "**", "MJ");
                    }
                    else
                    {
                        //do nothing
                    }

                    if(i < 3)
                    {
                        ext_bpp_evt(55 + i, ext_new_data[i]);
                    }
                    else if(i >= 3 && i < 10)
                    {
                        ext_bpp_evt(57 + i, ext_new_data[i]);
                    }
                    else if(10 == i)
                    {
                        ext_bpp_evt(EXT_3_MGR_PRI, ext_new_data[i]);
                    }
                    else if(11 == i)
                    {
                        ext_bpp_evt(EXT_3_MGR_RSV, ext_new_data[i]);
                    }
                    else if(12 == i)
                    {
                        ext_bpp_evt(EXT_3_PWR1, ext_new_data[i]);
                    }
                    else if(13 == i)
                    {
                        ext_bpp_evt(EXT_3_PWR2, ext_new_data[i]);
                    }
                    else
                    {
                        //do nothing
                    }
                }
            }

            memset(bppa, 0, 8);
            bppa[0] = 0x40 | (tmp & 0x0000000F);
            bppa[1] = 0x40 | ((tmp >> 4) & 0x0000000F);
            bppa[2] = 0x40 | ((tmp >> 8) & 0x0000000F);
            bppa[3] = 0x40 | ((tmp >> 12) & 0x0000000F);
            memcpy(&ctx->mgr[5].mgrAlm[3], bppa, 4);
        }
    }
    else
    {
        return 0;
    }

    return 1;
}







void ext_ssm_cfg(char *pSsm)
{
    int i;
    u8_t tmp_ctag[7];
    u8_t sendbuf[256];
    u8_t slotid;

    memcpy(tmp_ctag, "000000", 6);

    if(	((EXT_DRV == slot_type[12]) || (EXT_DRV == slot_type[13])) &&
            ((1 == gExtCtx.drv[0].drvPR[0]) || (1 == gExtCtx.drv[1].drvPR[0])) )
    {
        for(i = 0; i < 10; i++)
        {
            if((EXT_OUT16 == gExtCtx.extBid[0][i]) || (EXT_OUT32 == gExtCtx.extBid[0][i]) || (EXT_OUT16S == gExtCtx.extBid[0][i]) || (EXT_OUT32S == gExtCtx.extBid[0][i]))
            {
                // lev
                memset(sendbuf, 0, 256);
                sprintf((char *)	sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                            i + EXT_1_OUT_LOWER,
                            tmp_ctag,
                            pSsm );
                ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_1_OUT_LOWER, tmp_ctag);
            }
        }
    }

    if(	((EXT_DRV == slot_type[12]) || (EXT_DRV == slot_type[13])) &&
            ((1 == gExtCtx.drv[0].drvPR[1]) || (1 == gExtCtx.drv[1].drvPR[1])) )
    {
        for(i = 0; i < 10; i++)
        {
            if((EXT_OUT16 == gExtCtx.extBid[1][i]) || (EXT_OUT32 == gExtCtx.extBid[1][i]) || (EXT_OUT16S == gExtCtx.extBid[1][i]) || (EXT_OUT32S == gExtCtx.extBid[1][i]))
            {
                //lev
                memset(sendbuf, 0, 256);
                sprintf((char *)	sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                            i + EXT_2_OUT_LOWER,
                            tmp_ctag,
                            pSsm );
                ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, i + EXT_2_OUT_LOWER, tmp_ctag);
            }
        }
    }

    if(	((EXT_DRV == slot_type[12]) || (EXT_DRV == slot_type[13])) &&
            ((1 == gExtCtx.drv[0].drvPR[2]) || (1 == gExtCtx.drv[1].drvPR[2])) )
    {
        for(i = 0; i < 10; i++)
        {
            if((EXT_OUT16 == gExtCtx.extBid[2][i]) || (EXT_OUT32 == gExtCtx.extBid[2][i]) || (EXT_OUT16S == gExtCtx.extBid[2][i]) || (EXT_OUT32S == gExtCtx.extBid[2][i]))
            {
                if((i + EXT_3_OUT_LOWER) >= ':')//except ':;'
                {
                    slotid = i + EXT_3_OUT_LOWER + 2;
                }
                else
                {
                    slotid = i + EXT_3_OUT_LOWER;
                }

                //lev
                memset(sendbuf, 0, 256);
                sprintf((char *)	sendbuf, "SET-OUT-LEV:%c:%s::%s;",
                            slotid,
                            tmp_ctag,
                            pSsm	);
                ext_issue_cmd(sendbuf, strlen_r(sendbuf, ';') + 1, slotid, tmp_ctag);
            }
        }
    }

    if((EXT_DRV == slot_type[12]) || (EXT_DRV == slot_type[13]))
    {
        for(i = 0; i < 30; i++)
        {
            memcpy(gExtCtx.save.outSsm[i], pSsm, 2);
            memcpy(gExtCtx.out[i].outSsm, pSsm, 2);
        }

        ext_write_flash(&(gExtCtx.save));
    }
}


int is_freq_input(unsigned char slot)
{
    int i;
    unsigned char port[2];
    if(conf_content.slot_u.fb == '0')
    {
        if(slot == RB1_SLOT)
        {
            for(i = 0; i < 4; i++)
            {
                if(flag_alm[14][i + 4] == 0x00)
                {
                    return 0;
                }
            }
        }
        else if(slot == RB2_SLOT)
        {
            for(i = 0; i < 4; i++)
            {
                if(flag_alm[15][i + 4] == 0x00)
                {
                    return 0;
                }
            }
        }
    }
    else
    {
#if 1
        if(slot_type[0] == 'S')
        {
            memset(port, 0, 2);

            port[0] = alm_sta[0][5] & 0x0f;
            port[1] = alm_sta[0][6] & 0x0f;

            for(i = 9; i < 11; i++) //0x04
            {
                if( (i % 2) != 0 )
                {
                    port[0] |= (alm_sta[0][i] & 0x0f);
                }
                else
                {
                    port[1] |= (alm_sta[0][i] & 0x0f);
                }
            }
            for(i = 5; i < 11; i++) //0x05
            {
                if( (i % 2) != 0 )
                {
                    port[0] |= (alm_sta[18][i] & 0x0f);
                }
                else
                {
                    port[1] |= (alm_sta[18][i] & 0x0f);
                }
            }
            for(i = 0; i < 4; i++)
            {
                if( ((port[0] >> i) & 0x01) == 0x00)
                {
                    return 0;
                }
                if( ((port[1] >> i) & 0x01) == 0x0)
                {
                    return 0;
                }
            }
        }
        else
        {
            //nothing
        }
        if(slot_type[1] == 'S')
        {
            memset(port, 0, 2);
            port[0] = alm_sta[1][5] & 0x0f;
            port[1] = alm_sta[1][6] & 0x0f;
            for(i = 9; i < 11; i++) //0x04
            {
                if( (i % 2) != 0 )
                {
                    port[0] |= (alm_sta[1][i] & 0x0f);
                }
                else
                {
                    port[1] |= (alm_sta[1][i] & 0x0f);
                }
            }
            for(i = 5; i < 11; i++) //0x05
            {
                if( (i % 2) != 0 )
                {
                    port[0] |= (alm_sta[19][i] & 0x0f);
                }
                else
                {
                    port[1] |= (alm_sta[19][i] & 0x0f);
                }
            }
            for(i = 0; i < 4; i++)
            {
                if( ((port[0] >> i) & 0x01) == 0x00)
                {
                    return 0;
                }
                if( ((port[1] >> i) & 0x01) == 0x00)
                {
                    return 0;
                }
            }
        }
#endif
    }

    return 1;
}

//检查是否为2级时钟,小于等于G.812
int timeClock_2(const unsigned char rb)
{
    unsigned char port;
    unsigned char slot = 0;
    if('0' == conf_content.slot_u.fb)
    {
        switch(rpt_content.slot_o.rb_ref)
        {
        case '5':
            return 0;
            break;

        case '6':
            return 0;
            break;

        case '7':
            return 0;
            break;

        case '8':
            return 0;
            break;

        default:
            return 0;
            break;
        }
    }
    else
    {

        //后面板:3--reftf1 时间;4--reftf2时间;5--reftf1频率;6--reftf2频率
        if('o' == rb)//计算reftf槽位号
        {
            if('3' == rpt_content.slot_o.rb_ref || '4' == rpt_content.slot_o.rb_ref)//后面板时间输入
            {
                slot = rpt_content.slot_o.rb_ref - '3';
            }
            else if('5' == rpt_content.slot_o.rb_ref || '6' == rpt_content.slot_o.rb_ref)//后面板频率输入
            {
                slot = rpt_content.slot_o.rb_ref - '5';

            }
            else
            {
                printf("RB up pps_stat error\n");
            }
        }
        else
        {
            if('3' == rpt_content.slot_p.rb_ref || '4' == rpt_content.slot_p.rb_ref)//后面板时间输入
            {
                slot = rpt_content.slot_p.rb_ref - '3';
            }
            else if('5' == rpt_content.slot_p.rb_ref || '6' == rpt_content.slot_p.rb_ref)//后面板频率输入
            {
                slot = rpt_content.slot_p.rb_ref - '5';

            }
            else
            {
                printf("RB up pps_stat error\n");
            }
        }
        //确认端口号
        if('3' == rpt_content.slot_o.rb_ref || '4' == rpt_content.slot_o.rb_ref)
        {

            if( rpt_content.slot[slot].ref_mod2[2] == '\0' || rpt_content.slot[slot].ref_mod2[2] == '0')
            {
                return 0;
            }
            else
            {
                port = rpt_content.slot[slot].ref_mod2[2] - '1';
            }

        }
        else
        {
            if( rpt_content.slot[slot].ref_mod1[2] == '\0' || rpt_content.slot[slot].ref_mod1[2] == '0')
            {
                return 0;
            }
            else
            {
                port = rpt_content.slot[slot].ref_mod1[2] - '1';
            }
        }

        //检查时钟质量等级
        if( rpt_content.slot[slot].ref_tl[port] != 'a')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

}

int get_tod_sta(unsigned char *buf, unsigned char slot)
{
    //int flag_a;
    unsigned char res;
    if(slot == RB1_SLOT)
    {
        if(strncmp1(rpt_content.slot_o.rb_msmode, "MAIN", 4) == 0)
        {

            //flag_a=is_freq_input('o');
            if(buf[0] == '1')
            {
                if ( rpt_content.slot_o.rb_ref == '3' ||
                     rpt_content.slot_o.rb_ref == '4' ||
                     rpt_content.slot_o.rb_ref == '5' ||
                     rpt_content.slot_o.rb_ref == '6' ||
                     rpt_content.slot_o.rb_ref == '7' ||
                     rpt_content.slot_o.rb_ref == '8'   )

                {
					res = timeClock_2('o');
                    if( res && ( '1' == conf_content3.mcp_protocol) )
                    {
                        buf[0] = '5';//二级时钟
                    }
                    else
                    {
                        buf[0] = '1'; //正常跟踪一级时钟
                    }
                }
                else//正常跟踪卫星
                {
                    buf[0] = '0';
                }
            }
            else if(buf[0] == '2')
            {
                buf[0] = '5'; //保持
            }
            else if(buf[0] == '3')
            {
				//不可用
				if( '1' == conf_content3.mcp_protocol )
				{
					buf[0] = '7';
				}
				else
				{
					buf[0] = '2';
				}
               
               
            }
            else if(buf[0] == '4')
            {
				//XO保持
				if( '1' == conf_content3.mcp_protocol )
				{
					buf[0] = '4'; 
				}
				else
				{
					buf[0] = '3';
				}
            }
            else
            {
                return 1;
            }
        }
        else
        {
            return 1;
        }
    }
    else if(slot == RB2_SLOT)
    {
        if(strncmp1(rpt_content.slot_p.rb_msmode, "MAIN", 4) == 0)
        {

            //flag_a=is_freq_input('o');
            if(buf[0] == '1')
            {
                if ( rpt_content.slot_p.rb_ref == '3' ||
                     rpt_content.slot_p.rb_ref == '4' ||
                     rpt_content.slot_p.rb_ref == '5' ||
                     rpt_content.slot_p.rb_ref == '6' ||
                     rpt_content.slot_p.rb_ref == '7' ||
                     rpt_content.slot_p.rb_ref == '8'   )
                {
					res = timeClock_2('p');
                    if( res && ( '1' == conf_content3.mcp_protocol) )
                    {
                        buf[0] = '5';//二级时钟
                    }
                    else
                    {
                        buf[0] = '1'; //正常跟踪一级时钟
                    }
                }
                else//正常跟踪卫星
                {
                    buf[0] = '0';
                }
            }
            else if(buf[0] == '2')
            {
                buf[0] = '5'; //保持
            }
            else if(buf[0] == '3')
            {
				//不可用
				if( '1' == conf_content3.mcp_protocol )
				{
					buf[0] = '7';
				}
				else
				{
					buf[0] = '2';
				}
               
            }
            else if(buf[0] == '4')
            {
				//XO保持
				if( '1' == conf_content3.mcp_protocol )
				{
					buf[0] = '4'; 
				}
				else
				{
					buf[0] = '3';
				}
            }
            else
            {
                return 1;
            }
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 1;
    }
    return 0;
}

int send_tod(unsigned char *buf, unsigned char slot)
{
    unsigned char  sendbuf1[SENDBUFSIZE];
    unsigned char  sendbuf2[SENDBUFSIZE];
    unsigned char  *ctag = "000000";
    int i;
    memset(sendbuf1, '\0', SENDBUFSIZE);
    memset(sendbuf2, '\0', SENDBUFSIZE);

    if(get_tod_sta(buf, slot) == 1)
    {
        return 1;
    }

    //modified by zhanghui 2013-9-10 15:50:12

    if('2' == buf[2])
    {
        buf[2] = '3';
    }
    else if(('3' == buf[2]))
    {
        buf[2] = '2';
    }
    else
    {
        //do nothing
    }

	//buf[0]=TOD_STA;
    sprintf((char *)sendbuf1, "SET-TOD-STA:%s::0,%s;", ctag, buf);
    sprintf((char *)sendbuf2, "SET-PTP-STA:%s::%s;", ctag, buf);
    for(i = 0; i < 14; i++)
    {
        if(slot_type[i] == 'I')
        {
            sendtodown(sendbuf1, (unsigned char)(i + 0x61));
            printf("+++++tod:%s\n",sendbuf1);
        }
        else if(((slot_type[i] < 'I') && (slot_type[i] > '@')) || (slot_type[i] == 'j') )
        {
            sendtodown(sendbuf2, (unsigned char)(i + 0x61));
 			printf("-----tod:%s\n",sendbuf2);
        }
    }

    return 0;
}

void rb_do_nothing(void)
{
}

void rb_16_master(void)
{
    sendtodown("SET-MAIN:000000::MAIN;",'p');
	printf("RB auto master\n");	
}

void rb_16_salve(void)
{
	sendtodown("SET-MAIN:000000::STBY;",'p');
	printf("RB auto STBY\n");
}
void lct_set_rb(void)
{
	if(strncmp(conf_content.slot_p.msmode,"STBY",4) == 0)
	{
		sendtodown("SET-MAIN:000000::STBY;",'p');
	}
	else
	{
		sendtodown("SET-MAIN:000000::MAIN;",'p');
	}
	printf("RB LCT\n");
}

int rb_stat_index(int slot)
{
	unsigned char stat_buf[5];
	
    memset(stat_buf,0,5);
    
	if(slot == 14)
	{
		if('O' == slot_type[14])
	    {
	    	return 0;
	    }
	    else
	    {
	    	memcpy(stat_buf,rpt_content.slot_o.rb_trmode,4);
	    }
	}
	if(slot == 15)
	{
		if('O' == slot_type[15])
	    {
	    	return 0;
	    }
	    else
	    {
	    	memcpy(stat_buf,rpt_content.slot_p.rb_trmode,4);
	    }
	}
	
	if(stat_buf == NULL)
		return -1;
	
	if( strncmp(stat_buf,"FREE",4)==0 )
	{
		return 1;
	}
	else if( strncmp(stat_buf,"FATR",4)==0 )
	{
		return 2;
	}
	else if( strncmp(stat_buf,"TRCK",4)==0 )
	{
		return 3;
	}
	else if( strncmp(stat_buf,"HOLD",4)==0 )
	{
		return 4;
	}
	else
	{
		return -1;
	}
}


/*
16#RB pan state
-------------------------------------------------------------------------------
|   \  16#|                                                                    |
|15# \    |   None         FreeRun       FastTrack       Locked       HoldOver |  
|------------------------------------------------------------------------------|
|None     |   None          Master          Master       Master       Master   |
|         |                                                                    |
|FreeRun  |   None          Master          Master       Master       Master   |
|         |                                                                    |
|FastTrack|   None          slave           LCT          Master       Master   |
|         |                                                                    |
|Locked   |   None          slave           slave        LCT          slave    |
|         |                                                                    |
|HoldOver |   None          slave           slave        Master       LCT      |
-------------------------------------------------------------------------------
*/
typedef void (*Func_Matrix)(void);
void timer_rb_ms(void)
{
	signed char i,j;
	static signed char rb_stat_index_save = 0; 
    Func_Matrix rb_ms_change[5][5] = {
    								  {rb_do_nothing, rb_16_master, rb_16_master, rb_16_master, rb_16_master},
    								  {rb_do_nothing, rb_16_master, rb_16_master, rb_16_master, rb_16_master},
    								  {rb_do_nothing, rb_16_salve , lct_set_rb  , rb_16_master, rb_16_master},
    								  {rb_do_nothing, rb_16_salve , rb_16_salve , lct_set_rb  , rb_16_salve },
    								  {rb_do_nothing, rb_16_salve , rb_16_salve , rb_16_master, lct_set_rb  } 
    								 };

    i = rb_stat_index(14);
    j = rb_stat_index(15);
    
	if(i==-1 || j ==-1)
	{
		return ;
	}
	if( rb_stat_index_save == i*5+j )
	{
	    return ;	
	}
	else
	{
	    rb_stat_index_save = i*5+j;
	}
	rb_ms_change[i][j]();
}

/*
mcp定时检测控制
*/
void timer_mcp(void * loop)
{

   bool_t *loop_flag = loop;

   while(!(*loop_flag)) 
   {
        sleep(120);
        timer_rb_ms();
		
   }
   printf("The RB MasterSlave pthread exit!\n");
   pthread_exit(NULL);
	
}








<file_sep>/MCPU/mcp_save_struct.h
#ifndef _MCP_SAVE_STRUCT_H_
#define _MCP_SAVE_STRUCT_H_

#pragma pack(1)

typedef struct
{
    unsigned char sy_ver[40];
    unsigned char tp16_en[17];
    unsigned char tp16_tod[4];

    unsigned char out_lev[3];
    unsigned char out_typ[17];
    unsigned char out_sta;
    unsigned char out_mode;


    unsigned char ptp_ip[4][16];
    unsigned char ptp_mac[4][18];
    unsigned char ptp_gate[4][16];
    unsigned char ptp_mask[4][16];
    unsigned char ptp_dns1[4][16];
    unsigned char ptp_dns2[4][16];

    /*<eable>,<delaytype>,<multicast>,<enp>,<step>,
    <sync>,<announce>,<delayreq>,<pdelayreq>,
    <delaycom>,<linkmode>
    delete  the port*/
    unsigned char ptp_en[4];
    unsigned char ptp_delaytype[4];
    unsigned char ptp_multicast[4];
    unsigned char ptp_enp[4];
    unsigned char ptp_step[4];
    unsigned char ptp_sync[4][3];
    unsigned char ptp_announce[4][3];
    unsigned char ptp_delayreq[4][3];
    unsigned char ptp_pdelayreq[4][3];
    unsigned char ptp_delaycom[4][9];
    unsigned char ptp_linkmode[4];
    unsigned char ptp_out[4];
	unsigned char ptp_clkcls[4][4];
	//unsigned char ptp_dom[4];

    unsigned char ref_prio1[9];
    unsigned char ref_prio2[9];
    unsigned char ref_mod1[4];
    unsigned char ref_mod2[4];

    unsigned char ref_ph[128];
    unsigned char ref_sa[9];
    unsigned char ref_typ1[9];
    unsigned char ref_typ2[9];
    unsigned char ref_tl[9];
    unsigned char ref_en[9];
    unsigned char ref_ssm_en[9];

    unsigned char rs_en[5];
    unsigned char rs_tzo[5];

    unsigned char ppx_mod[17];

    unsigned char tod_en[17];
    unsigned char tod_br;
    unsigned char tod_tzo;

    unsigned char igb_en[5];
    unsigned char igb_rat[5];
    unsigned char igb_max[5];
    unsigned char igb_tzone;

    unsigned char ntp_ip[9];
    unsigned char ntp_mac[13];
    unsigned char ntp_gate[9];
    unsigned char ntp_msk[9];

    unsigned char ntp_bcast_en;
    unsigned char ntp_tpv_en;
    unsigned char ntp_md5_en;
    unsigned char ntp_ois_en;
    unsigned char ntp_interval[5];
    unsigned char ntp_key_flag[4];

    unsigned char ntp_keyid[4][5];
    unsigned char ntp_key[4][17];
    unsigned char ntp_ms[26];
	unsigned char ntp_leap[4];
	unsigned char ntp_mcu[2];
	unsigned char ntp_pmac[12][6];
	unsigned char ntp_pip[12][5];
	unsigned char ntp_pnetmask[12][4];
	unsigned char ntp_pgateway[12][4];
    unsigned char ntp_portEn[5];
    unsigned char ntp_portStatus[5];
    unsigned char ref_2mb_lvlm;
} rpt_out_content;

typedef struct
{
    /*	<responsemessage>::=“<aid>,<type>,[<version-MCU>],[<version-FPGA>],
    [<version-CPLD>],[<version-PCB>],<ref>,<priority>,<mask>,<leapmask>,<leap>,<leaptag>,
    [<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],[<delay>],
    <we>,<out>,<type-work>,<msmode>,<trmode>”*/
    unsigned char rb_ref;
    unsigned char rb_prio[9];
    unsigned char rb_mask[9];
    unsigned char rb_leapmask[6];
    unsigned char rb_leap[3];
    unsigned char rb_leaptag;
    unsigned char rf_dey[8][9];/*<deley>X8*/
    unsigned char rf_tzo[4];/*<we>,<out>*/
    unsigned char rb_typework[8];
    unsigned char rb_msmode[5];
    unsigned char rb_trmode[5];
    unsigned char sy_ver[40];
    unsigned char rb_inp[10][128];
    unsigned char rb_tl;
    unsigned char rb_sa;
    unsigned char rb_tl_2mhz;

    unsigned char tod_buf[4];
	unsigned char rb_phase[2][9];
	unsigned char rb_ph[7][10];// #17 #18 #1 time #2 time #1 fre #2 fre
	
} rpt_rb_content;

typedef struct
{
    /*<response message >::= “
    <aid>,<type>,[<version-MCU>],[<version-FPGA>], [<version-CPLD>],[<version-PCB>],
    <ppssta>,<sourcetype>,<pos_mode>,<elevation>, <format>, <acmode>, <mask>,
    <qq-fix>,<hhmmss.ss>,<ddmm.mmmmmm>,<ns>,<dddmm.mmmmmm>,<we>,<saaaaa.aa>,
    <vvvv>,<tttttttttt>[,<sat>,<qq>,<pp>,<ss>,<h>]*”*/
    unsigned char sy_ver[40];
    unsigned char gb_sta;
    unsigned char gb_tod[4];
    unsigned char gb_pos_mode[5];
    unsigned char gb_elevation[9];
    unsigned char gb_format[6];
    unsigned char gb_acmode[4];
    unsigned char gb_mask[3];/*<mask>*/
    unsigned char gb_ori[70];/*<qq>,<hhmmss.ss>,<ddmm.mmmmmm>,<ns>,<dddmm.mmmmmm>,<we>,<saaaaa.aa>,
	<vvvv>,<tttttttttt>*/
    //unsigned char gb_rev[256];
    unsigned char gb_rev_g[256];	/*<sat>,<qq>[,<pp>,<ss>,<h>]**/
    unsigned char gb_rev_b[256];
	unsigned char gb_rev_glo[256];	/*<sat>,<qq>[,<pp>,<ss>,<h>]**/
    unsigned char gb_rev_gal[256];
    unsigned char bdzb[8];
	unsigned char staen;
	unsigned char rec_type[12];
	unsigned char rec_ver[40];
	unsigned char leapnum[4];
	unsigned char leapalm[3];
	unsigned char delay[8];
	unsigned char pps_out_delay[16];
	
} rpt_gbtp_content;

typedef struct
{
    rpt_out_content slot[14];
    rpt_rb_content slot_o, slot_p;
    rpt_gbtp_content slot_q, slot_r;
} rpt_all_content;


typedef struct
{
    unsigned char ptp_prio1[14][4][5];//14个槽位，4个端口，5个字节保存信息。
    unsigned char ptp_prio2[14][4][5];
    unsigned char ptp_mtc_ip[14][4][152];//保存单播ip
    unsigned char ptp_dom[14][4][4];//20170616 add
    unsigned char esmc_en[14][4];
	unsigned char ptp_ver[14][4][10];
	unsigned char sfp_type[14][4][3];
	unsigned char sfp_oplo[14][4][7];
	unsigned char sfp_ophi[14][4][7];
	unsigned char time_source[14][4][5];
	unsigned char clock_class[14][4][4];
	unsigned char time_acc[14][4][5];
	unsigned char synce_ssm[14][4][14];
} file_ptp_mtc; //第一部分配置信息由file_content保存，第二部分为扩展框，此为第三部分。


#pragma pack()

rpt_all_content rpt_content;
file_ptp_mtc rpt_ptp_mtc;
//
//define esmc enable,ptp_domain.
//
//unsigned char esmc_en[14][4];
//unsigned char ptp_dom[14][4][4];

void sta1_alm(unsigned char *sendbuf, unsigned char slot);
void sta1_portnum(unsigned char *sendbuf, unsigned char slot);
void sta1_net(unsigned char *sendbuf, unsigned char slot);
void sta1_cpu(unsigned char *sendbuf, unsigned char slot);
void sta1_in_out(unsigned char *sendbuf, unsigned char slot);

void set_sta_ip(unsigned const char *ip, unsigned char solt);
void set_sta_gateway(unsigned char *ip, unsigned char solt);
void set_sta_netmask(unsigned char *ip, unsigned char solt);
//extern int _NUM_REVT; 


#endif

<file_sep>/MCPU/mcp_main.c
#include <sys/resource.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>


//#include <linux/tcp.h>
#include <netinet/tcp.h>


#include "ext_alloc.h"
#include "ext_global.h"
#include "mcp_main.h"
#include "mcp_process.h"
#include "mcp_def.h"
#include "mcp_save_struct.h"
#include "mcp_set_struct.h"
//#include "memwatch.h"
//#include "memwatch.h"




int qid;
int print_switch = 0;
char TOD_STA = '2';
static bool_t Loop_Mark = false;

void log()
{
    printf("****************************************************\n");
    printf("*                                                  *\n");
    printf("*                                                  *\n");
    printf("*                P300 TF EXT                       *\n");
    printf("*                                                  *\n");
    printf("*v%s.%s.%s.%s_%c                        \n",VER_MAJOR,VER_MINOR,VER_REVIS,VER_DATE,VER_STATE);
    printf("*                                                  *\n");
    printf("****************************************************\n");
	
}

void black_door(int sig)
{
	printf("+++++signal is %d\n",sig);
	TOD_STA = sig+'0';
}


int main(int argc , char **argv)
{
    //
    //!Socket Attribute Setting. 
    //
    int opt = 1;
    //
    //!Bind  The  Local IP and Port Parameters.
    //
    struct sockaddr_in server;
    //
    //!Process pid Getting.
    //
    pid_t pid;
	//
	//! Priority Setting.
	//
    int prio;

    int i, j, len_rpt;
    ssize_t len;
    int tmp, temp;
	
    //
    //! Tcp Receive Buffer and Data Swap Buffer. 
    unsigned char recvbuf[MAXRECVDATA];
    unsigned char rpt_buf[MAXDATASIZE];

    //const char chOpt=1;
    //int nErr;
    struct timeval timeout = {0, 100000};

    //int flag_slotOIsMaster =0; //2014-1-27 rb备用板是否为主用状态；1启用备槽
    printf("sizeof(file_content)=%d\n", (int)sizeof(file_content));
	printf("sizeof(file_content3)=%d\n",(int)sizeof(file_content3));
	
	//
    //Get self-process pid. 
    //
    pid = getpid();
	//
	//Set self-process priority.
	//
    if (setpriority(PRIO_PROCESS, pid, -12) != 0)
    {
        printf("%s%s\n", "MCPU", "Can't set ntp's priority!");
    }

	//
	//Record The Last error.
    errno = 0;

	//
	//!Get Self-Process Priority.
    prio = getpriority(PRIO_PROCESS, pid);
    if ( 0 == errno )
    {
        printf("MCPU priority is %d\n", prio);
    }
	
    FpgaWrite( WdControl, 0x00 );
    FpgaWrite( WdControl, 0x01 );
    log();

	
    Init_filesystem();
    initializeContext(&gExtCtx);
    init_var();
    config_enet();
    Open_FpgaDev();
    //
    //Timer 120s Check The Setting or not Return Corrected.
    init_timer();

    qid = msgQ_create(PATH_NAME, PROJ_ID);
	
	//
	//! Create The Thread of deal RB Master/Slave inter 60 s.
	//
	if(Create_Thread(&Loop_Mark) == false)
	{
		printf("<MCPU ERR> %s\n", "Create RB Mastr Slave Thread Failed!");
		return(-1);
	}
#if 0
if(SIG_ERR ==  signal(1,black_door))
	{
		perror("++++++signal1\n");
	}
	if(SIG_ERR ==  signal(2,black_door))
	{
		perror("++++++signal2\n");
	}
	if(SIG_ERR ==  signal(3,black_door))
	{
		perror("++++++signal3\n");
	}
	if(SIG_ERR ==  signal(4,black_door))
	{
		perror("++++++signal4\n");
	}
	if(SIG_ERR ==  signal(5,black_door))
	{
		perror("++++++signal5\n");
	}
	if(SIG_ERR ==  signal(6,black_door))
	{
		perror("++++++signal6\n");
	}
	if(SIG_ERR ==  signal(7,black_door))
	{
		perror("++++++signal7\n");
	}
#endif	
#if DEBUG_NET_INFO
    printf("%s:init completed,create socket...\n", NET_INFO);
#endif


    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("Creating socket failed.");
        exit(1);
    }

    //允许多客户端连接
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));       //设置socket属性
    setsockopt(listenfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
    setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
    
    // if(nErr==-1)
    //{
    // perror("TCP_NODELAY error\n");
    //return ;
    //}

    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    // server.sin_addr.s_addr = inet_addr("192.168.1.10");
    server.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
    {
        //调用bind绑定地址
        perror("Bind error.");
        exit(1);
    }

    if (listen(listenfd, BACKLOG) == -1)
    {
        //调用listen开始监听
        perror("listen() error\n");
        exit(1);
    }

#if DEBUG_NET_INFO
    printf("%s:listening...\n", NET_INFO);
#endif

    //
    //!初始化select
    //
    maxfd = listenfd;
    maxi = -1;
    for(client_num = 0; client_num < MAX_CLIENT; client_num++)
    {
        client_fd[client_num] = -1;
    }
	//
	//!
	//
    FD_ZERO(&allset);           //清空
    FD_SET(listenfd, &allset);  //将监听socket加入select检测的描述符集合
    print_switch = 1;
	
	
    while (1)
    {
        struct sockaddr_in addr;
        rset = allset;
		
		//  int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);				  
		//! timeout == NULL:block the tcp connected.
        nready = select(maxfd + 1, &rset, NULL, NULL, NULL);    //调用select

        //
        //! select error,continue;
		//if(nready <= 0)
        //{
        //    perror("select error\n");    
        //    continue;
        //}
		//else 
		if (FD_ISSET(listenfd, &rset))  						//检测是否有新客户端请求
        {

		   //fprintf(stderr,"f:Accept a connection.\n");
            sin_size = sizeof(struct sockaddr_in);
            if ((connectfd =  accept(listenfd, (struct sockaddr *)&addr, (socklen_t *) & sin_size)) == -1)
            {
                continue;
            }

            //将新客户端的加入数组
            for (client_num = 0; client_num < MAX_CLIENT; client_num++)
            {
                if (client_fd[client_num] < 0)
                {
                    client_fd[client_num] = connectfd; //保存客户端描述符

                    //if(client_num == 0)
                    //{
                        //init_pan();
                    //}
#if DEBUG_NET_INFO
                    if(print_switch == 0)
                    {
                        printf("%s:Got a new Client<%d>\n", NET_INFO, client_num);
                    }
#endif
                 break;
                }
            }

            if (client_num >= MAX_CLIENT)
            {
                printf("Too many clients\n");
				
                close(connectfd);
            }
            else
            {
                FD_SET(connectfd, &allset);
                if (connectfd > maxfd)
                {
                    maxfd = connectfd;    //确认maxfd是最大描述符
                }
                if (client_num > maxi)    //数组最大元素值
                {
                    maxi = client_num;
                }
                if (--nready <= 0)
                {
                    continue;
                }
            }//如果没有新客户端连接，继续循环
        }

        for (client_num = 0; client_num <= maxi; client_num++)
        {
            if ((sockfd = client_fd[client_num]) < 0)    //如果客户端描述符小于0，则没有客户端连接，检测下一个
            {
                continue;
            }

            // 有客户连接，检测是否有数据
            if (FD_ISSET(sockfd, &rset))
            {
                memset(recvbuf, '\0', MAXRECVDATA);
				//len = recv(sockfd, recvbuf, MAXDATASIZE, 0);
                if ((len = recv(sockfd, recvbuf, MAXDATASIZE, 0)) <= 0)
                {				
					close(sockfd);				
					FD_CLR(sockfd, &allset);
					
                    client_fd[client_num] = -1;

#if DEBUG_NET_INFO
                    if(print_switch == 0)
                    {
                        printf("%s:client<%d>closed. \n", NET_INFO, client_num);
                    }
#endif
                }
			//	else if (len < 0)
			//	{

			//	}
				else
                {
#if 1
                   if(len < 8)
                    {
                        /*<OL:xxx;>*/
                        /*@@{   00000000 00000000 00000000*/
                        /*OL:oo?;*/
                        //判断取盘信息
                        if(strncmp1("OL", recvbuf, 2) == 0)
                        {
                            for(i = 0; i < 3; i++)
                            {
                                for(j = 0; j < 8; j++)
                                {
                                    tmp = ((recvbuf[3 + i] >> j) & 0x01);
                                    if(tmp == 0x00) //not online
                                    {
                                        temp = i * 8 + j;
                                        //printf("not online temp:%d\n ",temp);
                                        if(temp < 0 || temp > 23)
                                        {
                                            printf("<not online>temp out of range\n");
                                        }
                                        if(online_sta[i * 8 + j] == 0x01)
                                        {
                                            //send not online
                                            send_online_framing(temp, 'O');

                                            /*取盘后，删除以前上报数据*/
                                            if((temp) < 14)
                                            {
                                                memset(&rpt_content.slot[temp], '\0', sizeof(out_content));
                                            }
                                            else if(temp == 14)
                                            {
                                                memset(&rpt_content.slot_o, '\0', sizeof(rb_content));
                                                //flag_slotOIsMaster = 0;
                                            }
                                            else if(temp == 15)
                                            {
                                                memset(&rpt_content.slot_p, '\0', sizeof(rb_content));

                                            }
                                            else if(temp == 16)
                                            {
                                                memset(&rpt_content.slot_q, '\0', sizeof(gbtp_content));
                                            }
                                            else if(temp == 17)
                                            {
                                                memset(&rpt_content.slot_r, '\0', sizeof(gbtp_content));
                                            }
                                            memset(alm_sta[temp], 0, 14);

                                            send_pullout_alm(temp + 24, 0);
                                        }
                                        online_sta[temp] = tmp;
                                        slot_type[temp] = 'O';
                                    }
                                    else   //online
                                    {
                                        temp = i * 8 + j;
                                        if(temp < 0 || temp > 23)
                                        {
                                            printf("<online>temp out of range\n");
                                        }
                                        if(online_sta[temp] == 0x00)
                                        {
                                            if(temp == 22)
                                            {
                                                send_online_framing(temp, 'Q');
                                                slot_type[temp] = 'Q';
                                            }
                                            else if((temp == 18) || (temp == 19))
                                            {
                                                //send online
                                                send_online_framing(temp, 'M');
                                                slot_type[temp] = 'M';
                                                //init_pan(temp);
                                            }
                                            //else if((temp==20)||(temp==21))
                                            else if(temp == 20)
                                            {
                                                //send online
                                                send_online_framing(temp, 'N');
                                                slot_type[temp] = 'N';
                                                init_pan(20);
                                            }
                                            else if((temp == 16) || (temp == 17))
                                            {
                                                //send online
                                                //send_online_framing(temp,'L');
                                                //slot_type[temp]='L';
                                            }
                                            else if((temp == 14) || (temp == 15))
                                            {
                                                //send online
                                                //	send_online_framing(temp,'K');
                                                //	slot_type[temp]='K';
                                                //init_pan((char)(temp+'a'));
                                                //if(temp==14)
                                                //printf("online\n");
                                            }
                                            if(temp >= 0 && temp < 24)
                                            {
                                                send_pullout_alm(temp + 24, 1);
                                            }

                                        }
                                        online_sta[temp] = tmp;
                                    }
                                }
                            }

                        }
                        /*
                        if(0 == online_sta[15])//2014-1-27 rb主框出问题时启用备用框
                        {
                        	if(0 == flag_slotOIsMaster && slot_type[14]!= 'O')
                        	{
                        		//printf("set-main:main\n");
                        		sendtodown("SET-MAIN:000000::MAIN;",'o');
                        		flag_slotOIsMaster = 1;
                        	}
                        }
                        else
                        {
                        	if(1 == flag_slotOIsMaster && slot_type[15]!= 'O')
                        	{
                        		flag_slotOIsMaster = 0;
                        		sendtodown("SET-MAIN:000000::STBY;",'o');
                        	}
                        }
                        */
                    }

                    else
                    {
                        for(i = 0, j = 0; i < 1024; i++)
                        {
                            if(recvbuf[i] == ';')
                            {
                                len_rpt = i - j + 1;
                                memset(rpt_buf, '\0', sizeof(unsigned char) * MAXDATASIZE);
                                memcpy(rpt_buf, &recvbuf[j], len_rpt);
                                judge_data(rpt_buf, len_rpt, client_fd[client_num]);
                                j = i + 1;
                            }
                        }
                    }
#endif
                }

                if (--nready <= 0)
                {
                    break;
                }
            }
        }

#if 1
        if(1 == reset_network_card)
        {
            if(0 == shake_hand_count)
            {
               //printf("phy reset\r\n");
			   mcpd_reset_network_card();
            }

            shake_hand_count = 0;
            reset_network_card = 0;
        }
#endif
    }

    close(listenfd);            //关闭服务器监听socket
    Close_FpgaDev();
    return 0;
}




<file_sep>/MCPU/ext_cmn.h
#ifndef	__EXT_CMN__
#define	__EXT_CMN__






int strlen_r( char *str , char end);






#endif//__EXT_CMN__

<file_sep>/MCPU/mcp_def.h

#ifndef _MCP_DEF_H_
#define _MCP_DEF_H_


#define	BaseAddr1	(0x03600000)	//write
#define	BaseAddr2	(0x03600000 | 0x04000000)	//read

#define	REG16_FPGA(BaseAddr, Addr)       (BaseAddr + Addr)

#define ALMControl   REG16_FPGA(BaseAddr1,0x0022)

#define FPGA_READ   0x01
#define FPGA_WRITE  0x02

#define WdControl   REG16_FPGA(BaseAddr1,0x0020)
#define IndicateLED   REG16_FPGA(BaseAddr1,0x0030)



#define	TCP_NETWORK_CARD		"eth0"	//网卡


typedef unsigned char	Uint8;
typedef unsigned short 	Uint16;
typedef unsigned long	Ulong32;

typedef short int       Int16;
typedef char            Int8;
typedef long			Long32;

typedef float			Float32;
typedef double		Double64;



unsigned char mcp_date[11];
unsigned char mcp_time[9];
unsigned char password[9];

unsigned short atag;

unsigned char slot_type[24];
unsigned char fpga_ver[7];
int online_sta[24];
//
//! ref pge4s alarm 0x50.
//
int flag_alm[36][26];		  //20:ref1   21:ref2  22-35 PGE4S 
unsigned char alm_sta[37][14];//18:ref1   19:ref2  23-36 PGE4S
int flag_i_o;
int flag_i_p;

int timeout_cnt[MAX_CLIENT];

unsigned char saved_alms[SAVE_ALM_NUM][50];
int saved_alms_cnt;
int time_temp;

fd_set rset, allset;
int client_fd[MAX_CLIENT];
extern int qid;    /*消息队列标识符*/

int listenfd, maxfd, connectfd, sockfd;
int client_num, maxi, nready, count_data;
int sin_size; //地址信息结构体大小
int client_num;

unsigned char event_old[17][128];

int g_DrvLost;
int g_LeapFlag;
extern int _CMD_NODE_MAX;
extern int _NUM_RPT100S;
extern int NUM_REVT;

#endif

