static int make_path(IOHIDDeviceRef device, char *buf, size_t len)
{
	int res;
	unsigned short vid, pid;
	char transport[32];

	buf[0] = '\0';

	res = get_string_property_utf8(
		device, CFSTR(kIOHIDTransportKey),
		transport, sizeof(transport));
	
	if (!res)
		return -1;

	vid = get_vendor_id(device);
	pid = get_product_id(device);

	res = snprintf(buf, len, "%s_%04hx_%04hx_%p",
	                   transport, vid, pid, device);
	
	
	buf[len-1] = '\0';
	return res+1;
}
Exemple #2
0
const char*
onlp_sysi_platform_get(void)
{ 
	enum ag7648c_product_id pid = get_product_id();
	
	if (pid == PID_AG7648C)
		return "x86-64-delta-ag7648c";
	else 
		return "unknow";
}
Exemple #3
0
const char*
onlp_sysi_platform_get(void)
{ 
	enum ag6248c_product_id pid = get_product_id();
	
	if (pid == PID_AG6248C_48)
		return "arm-delta-ag6248c";
    else if(pid == PID_AG6248C_48P)
		return "arm-delta-ag6248c-poe";
	else 
		return "unknow";
}
Exemple #4
0
int main(void)
{
	IOHIDManagerRef mgr;
	int i;

	mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
	IOHIDManagerSetDeviceMatching(mgr, NULL);
	IOHIDManagerOpen(mgr, kIOHIDOptionsTypeNone);

	CFSetRef device_set = IOHIDManagerCopyDevices(mgr);
    if (device_set==NULL) {
        return 0;
    }

	CFIndex num_devices = CFSetGetCount(device_set);
	IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
	CFSetGetValues(device_set, (const void **) device_array);

	for (i = 0; i < num_devices; i++) {
		IOHIDDeviceRef dev = device_array[i];
		printf("Device: %p\n", dev);
		printf("  %04hx %04hx\n", get_vendor_id(dev), get_product_id(dev));

		wchar_t serial[256], buf[256];
		char cbuf[256];
		get_serial_number(dev, serial, 256);


		printf("  Serial: %ls\n", serial);
		printf("  Loc: %ld\n", get_location_id(dev));
		get_transport(dev, buf, 256);
		printf("  Trans: %ls\n", buf);
		make_path(dev, cbuf, 256);
		printf("  Path: %s\n", cbuf);

	}

	return 0;
}
struct hid_device_info  HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{
	struct hid_device_info *root = NULL; // return object
	struct hid_device_info *cur_dev = NULL;
	CFIndex num_devices;
	int i;
	
	setlocale(LC_ALL,"");

	/* Set up the HID Manager if it hasn't been done */
	hid_init();
	
	/* Get a list of the Devices */
	CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);

	/* Convert the list into a C array so we can iterate easily. */	
	num_devices = CFSetGetCount(device_set);
	IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
	CFSetGetValues(device_set, (const void **) device_array);

	/* Iterate over each device, making an entry for it. */	
	for (i = 0; i < num_devices; i++) {
		unsigned short dev_vid;
		unsigned short dev_pid;
		#define BUF_LEN 256
		wchar_t buf[BUF_LEN];
		char cbuf[BUF_LEN];

		IOHIDDeviceRef dev = device_array[i];

        if (!dev) {
            continue;
        }
		dev_vid = get_vendor_id(dev);
		dev_pid = get_product_id(dev);

		/* Check the VID/PID against the arguments */
		if ((vendor_id == 0x0 && product_id == 0x0) ||
		    (vendor_id == dev_vid && product_id == dev_pid)) {
			struct hid_device_info *tmp;
			size_t len;

		    	/* VID/PID match. Create the record. */
			tmp = malloc(sizeof(struct hid_device_info));
			if (cur_dev) {
				cur_dev->next = tmp;
			}
			else {
				root = tmp;
			}
			cur_dev = tmp;

			// Get the Usage Page and Usage for this device.
			cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
			cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));

			/* Fill out the record */
			cur_dev->next = NULL;
			len = make_path(dev, cbuf, sizeof(cbuf));
			cur_dev->path = strdup(cbuf);

			/* Serial Number */
			get_serial_number(dev, buf, BUF_LEN);
			cur_dev->serial_number = dup_wcs(buf);

			/* Manufacturer and Product strings */
			get_manufacturer_string(dev, buf, BUF_LEN);
			cur_dev->manufacturer_string = dup_wcs(buf);
			get_product_string(dev, buf, BUF_LEN);
			cur_dev->product_string = dup_wcs(buf);
			
			/* VID/PID */
			cur_dev->vendor_id = dev_vid;
			cur_dev->product_id = dev_pid;

			/* Release Number */
			cur_dev->release_number = get_int_property(dev, CFSTR(kIOHIDVersionNumberKey));

			/* Interface Number (Unsupported on Mac)*/
			cur_dev->interface_number = -1;
		}
	}
	
	free(device_array);
	CFRelease(device_set);
	
	return root;
}
Exemple #6
0
struct hid_device_info  HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
{
	struct hid_device_info *root = NULL; /* return object */
	struct hid_device_info *cur_dev = NULL;
	CFIndex num_devices;
	int i;

	/* Set up the HID Manager if it hasn't been done */
	if (hid_init() < 0)
		return NULL;

	/* give the IOHIDManager a chance to update itself */
	process_pending_events();

	/* Get a list of the Devices */
	IOHIDManagerSetDeviceMatching(hid_mgr, NULL);
	CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);

	/* Convert the list into a C array so we can iterate easily. */
	num_devices = CFSetGetCount(device_set);
	IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
	CFSetGetValues(device_set, (const void **) device_array);

	/* Iterate over each device, making an entry for it. */
	for (i = 0; i < num_devices; i++) {
		unsigned short dev_vid;
		unsigned short dev_pid;
		#define BUF_LEN 256
		wchar_t buf[BUF_LEN];

		IOHIDDeviceRef dev = device_array[i];

        if (!dev) {
            continue;
        }
		dev_vid = get_vendor_id(dev);
		dev_pid = get_product_id(dev);

		/* Check the VID/PID against the arguments */
		if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
		    (product_id == 0x0 || product_id == dev_pid)) {
			struct hid_device_info *tmp;
			io_object_t iokit_dev;
			kern_return_t res;
			io_string_t path;

			/* VID/PID match. Create the record. */
			tmp = malloc(sizeof(struct hid_device_info));
			if (cur_dev) {
				cur_dev->next = tmp;
			}
			else {
				root = tmp;
			}
			cur_dev = tmp;

			/* Get the Usage Page and Usage for this device. */
			cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
			cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));

			/* Fill out the record */
			cur_dev->next = NULL;

			/* Fill in the path (IOService plane) */
			iokit_dev = hidapi_IOHIDDeviceGetService(dev);
			res = IORegistryEntryGetPath(iokit_dev, kIOServicePlane, path);
			if (res == KERN_SUCCESS)
				cur_dev->path = strdup(path);
			else
				cur_dev->path = strdup("");

			/* Serial Number */
			get_serial_number(dev, buf, BUF_LEN);
			cur_dev->serial_number = dup_wcs(buf);

			/* Manufacturer and Product strings */
			get_manufacturer_string(dev, buf, BUF_LEN);
			cur_dev->manufacturer_string = dup_wcs(buf);
			get_product_string(dev, buf, BUF_LEN);
			cur_dev->product_string = dup_wcs(buf);

			/* VID/PID */
			cur_dev->vendor_id = dev_vid;
			cur_dev->product_id = dev_pid;

			/* Release Number */
			cur_dev->release_number = get_int_property(dev, CFSTR(kIOHIDVersionNumberKey));

			/* Interface Number (Unsupported on Mac)*/
			cur_dev->interface_number = -1;
		}
	}

	free(device_array);
	CFRelease(device_set);

	return root;
}
Exemple #7
0
int ShowQueryPage()
{
	struct list *lpublic;	/*解析public.txt文件的链表头*/
	struct list *lcontrol; 	/*解析help.txt文件的链表头*/
	lpublic=get_chain_head("../htdocs/text/public.txt");
    	lcontrol=get_chain_head("../htdocs/text/control.txt"); 	
  	char *encry=(char *)malloc(BUF_LEN);
  	char *str;
  //FILE *fp;
  //char lan[3];
	  char policer_encry[BUF_LEN]; 
	  char addn[N];        
	  char * mode=(char *)malloc(AMOUNT);
	  memset(mode,0,AMOUNT);
	  int i=0;   
	  int cl=1;
  	int retu=0;
  	int select_flag=0;
  	//char menu[21]="menulist";
  	char* i_char=(char *)malloc(10);
  	char * group=(char * )malloc(10);
  	memset(group,0,10);
  	char * QueryID=(char * )malloc(10);
  	memset(QueryID,0,10);
  	char * wrr_weight=(char * )malloc(10);
  	memset(wrr_weight,0,10);
  	int qos_num=0;
  	char * queue_mode=(char * )malloc(10);
  	memset(queue_mode,0,10);
	char * radiobutton=(char *)malloc(10);
	memset(radiobutton,0,10);
	cgiFormString("radiobutton", radiobutton, 10);

  	struct qos_info receive_qos[MAX_QOS_PROFILE];
  	struct query_info query_info[8];
  	int flag[8];
  	for(i=0;i<8;i++)
    	{
    		flag[i]=0;
    	}
	
  	for(i=0;i<MAX_QOS_PROFILE;i++)
	{
		receive_qos[i].profileindex=0;
		receive_qos[i].dp=0;
		receive_qos[i].up=0;
		receive_qos[i].tc=10;
		receive_qos[i].dscp=0;
	}
  	ccgi_dbus_init();
  	memset(encry,0,BUF_LEN);
  	if(cgiFormStringNoNewlines("UN", encry, BUF_LEN)!=cgiFormNotFound )  /*首次进入该页*/
  	{
    		str=dcryption(encry);
    		if(str==NULL)
    		{
	      		ShowErrorPage(search(lpublic,"ill_user")); 	 /*用户非法*/
	      		return 0;
		}
		strcpy(addn,str);
		memset(policer_encry,0,BUF_LEN);                   /*清空临时变量*/
  	}
  	else
  	{
	     cgiFormStringNoNewlines("encry_configqueue",encry,BUF_LEN);
	     cgiFormStringNoNewlines("group",group,10);
	     cgiFormStringNoNewlines("QueryID",QueryID,10);
	     cgiFormStringNoNewlines("wrr_weight",wrr_weight,10);
	     cgiFormStringNoNewlines("queue_mode",queue_mode,10);
	     cgiFormStringNoNewlines("CheckUsr",addn,10);
	}

  cgiHeaderContentType("text/html");
  fprintf(cgiOut,"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>");
  fprintf(cgiOut,"<meta http-equiv=Content-Type content=text/html; charset=gb2312>");
  fprintf(cgiOut,"<title>%s</title>",search(lcontrol,"route_manage"));
  fprintf(cgiOut,"<link rel=stylesheet href=/style.css type=text/css>"\
  	"<style type=text/css>"\
  	  "#div1{ width:62px; height:18px; border:1px solid #666666; background-color:#f9f8f7;}"\
	  "#div2{ width:60px; height:15px; padding-left:5px; padding-top:3px}"\
	  "#link{ text-decoration:none; font-size: 12px}"\
  	".ShowPolicy {overflow-x:hidden;  overflow:auto; width: 580px; height: 236px; clip: rect( ); padding-top: 2px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px} "\
  	"</style>"\
  "</head>"\
    "<script type=\"text/javascript\">"\
			  "function popMenu(objId)"\
			  "{"\
				 "var obj = document.getElementById(objId);"\
				 "if (obj.style.display == 'none')"\
				 "{"\
				   "obj.style.display = 'block';"\
				 "}"\
				 "else"\
				 "{"\
				   "obj.style.display = 'none';"\
				 "}"\
			 "}"\
	"</script>"\
  "<body>");

 
  	 retu=checkuser_group(addn);

   	if(cgiFormSubmitClicked("submit_config_query") == cgiFormSuccess)
  	{
  		int ret=0;
  		if(strcmp(queue_mode,"sp")==0)
  		{
  			set_queue_scheduler(queue_mode);
  		}
  		else if(strcmp(queue_mode,"wrr")==0)
  		{
  			if(strcmp(wrr_weight,"")!=0)
  			{
     				set_queue_scheduler(queue_mode);
				if(strcmp(QueryID,"")!=0)
				{
	     			ret=set_wrr_queue(group,QueryID,wrr_weight,1);	     				
	     				
					if(ret==0)
					{
						ShowAlert(search(lcontrol,"config_queue_succ"));
					}
					else 
					{
						ShowAlert(search(lcontrol,"counterconfigfail"));
					}
				}
				else
				{
					ShowAlert(search(lcontrol,"pls_create_profile"));
				}
  			}
  			else
  			{
				ShowAlert(search(lcontrol,"weight_not_null"));
  			}
  				
  		}
  		else if(strcmp(queue_mode,"sp+wrr")==0)
  		{
  			char * temp=(char * )malloc(10);
  			memset(temp,0,10);
  			strcpy(temp,"hybrid");
			if(strcmp(radiobutton,"val")==0)
			{
	  			if(strcmp(wrr_weight,"")!=0)
	  			{
	     				set_queue_scheduler(temp);
					if(strcmp(QueryID,"")!=0)
					{
		   				ret=set_wrr_queue(group,QueryID,wrr_weight,2);		   				
						if(ret==0)
						{
							ShowAlert(search(lcontrol,"config_queue_succ"));
						}
						else 
						{
							ShowAlert(search(lcontrol,"counterconfigfail"));
						}
					}
					else
					{
						ShowAlert(search(lcontrol,"pls_create_profile"));
					}
				}
				else
				{
					ShowAlert(search(lcontrol,"weight_not_null"));
				}	
			}
			else if(strcmp(radiobutton,"sp")==0)
			{
				set_queue_scheduler(temp);
				if(strcmp(QueryID,"")!=0)
				{
					ret=set_wrr_queue(group,QueryID,"sp",2);					
				    if(ret==0)
					{
						ShowAlert(search(lcontrol,"config_queue_succ"));
					}
					else 
					{
						ShowAlert(search(lcontrol,"counterconfigfail"));
					}

				}
				else
				{
					ShowAlert(search(lcontrol,"pls_create_profile"));
				}
			}
			else
			{
				ShowAlert(search(lcontrol,"select_sp_weight"));
			}
  			free(temp);	
  		}
  	}

	show_qos_mode(mode);
  fprintf(cgiOut,"<form method=post>"\
  "<div align=center>"\
  "<table width=976 border=0 cellpadding=0 cellspacing=0>"\
  "<tr>"\
    "<td width=8 align=left valign=top background=/images/di22.jpg><img src=/images/youce4.jpg width=8 height=30/></td>"\
    "<td width=51 align=left valign=bottom background=/images/di22.jpg><img src=/images/youce33.jpg width=37 height=24/></td>"\
    "<td width=153 align=left valign=bottom background=/images/di22.jpg><font id=titleen>QOS</font><font id=%s> %s</font></td>",search(lpublic,"title_style"),search(lpublic,"management"));
    fprintf(cgiOut,"<td width=690 align=right valign=bottom background=/images/di22.jpg>");
    	  fprintf(cgiOut,"<table width=130 border=0 cellspacing=0 cellpadding=0>"\
          "<tr>"\
          "<td width=62 align=center><input id=but type=submit name=submit_config_query style=background-image:url(/images/%s) value=""></td>",search(lpublic,"img_ok"));		  
          
		  if(cgiFormSubmitClicked("submit_config_query") != cgiFormSuccess)
		  {
            fprintf(cgiOut,"<td width=62 align=left><a href=wp_qosModule.cgi?UN=%s target=mainFrame><img src=/images/%s border=0 width=62 height=20/></a></td>",encry,search(lpublic,"img_cancel"));
           
            }
		  else                                         
     		fprintf(cgiOut,"<td width=62 align=left><a href=wp_qosModule.cgi?UN=%s target=mainFrame><img src=/images/%s border=0 width=62 height=20/></a></td>",encry,search(lpublic,"img_cancel"));
		  fprintf(cgiOut,"</tr>"\
          "</table>");
	fprintf(cgiOut,"</td>"\
    "<td width=74 align=right valign=top background=/images/di22.jpg><img src=/images/youce3.jpg width=31 height=30/></td>"\
  "</tr>"\
  "<tr>"\
    "<td colspan=5 align=center valign=middle><table width=976 border=0 cellpadding=0 cellspacing=0 bgcolor=#f0eff0>"\
      "<tr>"\
        "<td width=12 align=left valign=top background=/images/di888.jpg>&nbsp;</td>"\
        "<td width=948><table width=947 border=0 cellspacing=0 cellpadding=0>"\
            "<tr height=4 valign=bottom>"\
              "<td width=120>&nbsp;</td>"\
              "<td width=827 valign=bottom><img src=/images/bottom_05.gif width=827 height=4/></td>"\
            "</tr>"\
            "<tr>"\
              "<td><table width=120 border=0 cellspacing=0 cellpadding=0>"\
                   "<tr height=25>"\
                    "<td id=tdleft>&nbsp;</td>"\
                  "</tr>");       			
                  
     			fprintf(cgiOut,"<tr height=26>"\
     			"<td align=left id=tdleft background=/images/bottom_bg.gif style=\"border-right:0\"><font id=%s>%s</font></td>",search(lpublic,"menu_san"),search(lcontrol,"config_queue_list"));   /*突出显示*/
     			fprintf(cgiOut,"</tr>");
				int rowsCount=0;
				if(retu==0)
					rowsCount=16;
				else rowsCount=12;
				for(i=0;i<rowsCount+2;i++)
					{
						fprintf(cgiOut,"<tr height=25>"\
						  "<td id=tdleft>&nbsp;</td>"\
						"</tr>");
					}
				  fprintf(cgiOut,"</table>"\
              "</td>"\
              "<td align=left valign=top style=\"background-color:#ffffff; border-right:1px solid #707070; padding-left:30px; padding-top:10px\">"\
						"<table width=640 height=310 border=0 cellspacing=0 cellpadding=0>");
				  fprintf(cgiOut,"<tr height='35'>\n"\
				  				"<td style=\"font-size:14px\"><font color='red'><b>%s</b></font></td>"\
				  			  "</tr>\n",search(lcontrol,mode));
						if(retu==0)
						{
    						fprintf(cgiOut,"<tr>"\
    						"<td id=sec1 style=\"border-bottom:2px solid #53868b;font-size:14px\">%s</td>",search(lcontrol,"config_query_mode"));
    						fprintf(cgiOut,"</tr>"\
    						"<tr>"\
    						"<td><table frame=below rules=rows width=440 height=70 border=1>");
    						show_qos_profile(receive_qos,&qos_num,lcontrol);
    						for(i=0;i<qos_num;i++)
    						{
    							if(receive_qos[i].tc!=10)
    							{
    								flag[receive_qos[i].tc]=1;
								select_flag++;
    							}
    						}
    						fprintf(cgiOut,"<tr height=25 align=left  style=padding-top:8px>");
    						    fprintf(cgiOut,"<td id=td1 width=110>%s:</td>",search(lcontrol,"queue_mode"));
    						    fprintf(cgiOut,"<td  align=left style=padding-left:10px colspan=7><select name=queue_mode onchange=\"javascript:this.form.submit();\">");
    						    for(i=0;i<((get_product_id()==PRODUCT_ID_AU3K_BCM)?2:3);i++)
    							if(strcmp(Queue_Scheduler[i],queue_mode)==0)				/*显示上次选中的*/
    								fprintf(cgiOut,"<option value=%s selected=selected>%s",Queue_Scheduler[i],Queue_Scheduler[i]);
    							else				
    								fprintf(cgiOut,"<option value=%s>%s",Queue_Scheduler[i],Queue_Scheduler[i]);
    						    fprintf(cgiOut,"</select>");
    						    fprintf(cgiOut,"</td>");
    						    int Queue_Schedulerchoice=0;
    							cgiFormSelectSingle("queue_mode", Queue_Scheduler, 3, &Queue_Schedulerchoice, 0);
    						   	fprintf(cgiOut,"</tr>");
    							if(Queue_Schedulerchoice==2 || Queue_Schedulerchoice==1)
    							{
    								fprintf(cgiOut,"<tr height=25 align=left  style=padding-top:8px>");
    								fprintf(cgiOut,"<td  align=left style=padding-left:10px colspan=3>");
								if(get_product_id()!=PRODUCT_ID_AU3K_BCM)
								{
									fprintf(cgiOut,"<select name=group>");
        						    			fprintf(cgiOut,"<option value=%s>%s","group1","group1");
        						    			fprintf(cgiOut,"<option value=%s>%s","group2","group2");
        						    		fprintf(cgiOut,"</select>");
								}
								else
								{
									fprintf(cgiOut,"<input type=hidden name=group value=group1><b>group1</b>");
								}
        						    	fprintf(cgiOut,"</td>");
        						    fprintf(cgiOut,"<td id=td1>%s:</td>",search(lcontrol,"queue_TC"));
    								fprintf(cgiOut,"<td  align=left style=padding-left:10px colspan=3>\n");
								if(select_flag!=0)
								{
									fprintf(cgiOut,"<select name=QueryID>");
    									for(i=0;i<8;i++)
    									{
    										if(flag[i]==1)
    										{
    											fprintf(cgiOut,"<option value=%d>%d",i,i);
    										}
    									}
        						    		fprintf(cgiOut,"</select>");
								}
								else
								{
									fprintf(cgiOut,"<font color='red'>%s</font>",search(lcontrol,"not_create_qos_profile"));
								}
        						    fprintf(cgiOut,"</td>");
//***************************************************changed here**********************************************************
								if(Queue_Schedulerchoice==1)
								{
	    								fprintf(cgiOut,"<td id=td1>%s:</td>",search(lcontrol,"weight"));
	    								fprintf(cgiOut,"<td><input type=text name=wrr_weight size=8></td>");
								}
								else if(Queue_Schedulerchoice==2)
								{
	    								fprintf(cgiOut,"<td id=td1><input type=radio name=radiobutton value='val' checked />%s:</td>",search(lcontrol,"weight"));
	    								fprintf(cgiOut,"<td><input type=text name=wrr_weight size=8></td>");
									fprintf(cgiOut,"<td id=td1><input type=radio name=radiobutton value='sp' />sp</td>");
								}
//**********************************************************************************************************************************************************
    								fprintf(cgiOut,"</tr>");
    							}
    						fprintf(cgiOut,"</table>"\
    						"</td>"\
    						"</tr>");
						}
						fprintf(cgiOut,"<tr style=padding-top:18px>"\
						"<td id=sec1 style=\"border-bottom:2px solid #53868b;font-size:14px\">%s</td>",search(lcontrol,"config_queue_list"));
						fprintf(cgiOut,"</tr>"\
						 "<tr>"\
						   "<td align=left valign=top style=padding-top:18px>"\
						   "<div class=ShowPolicy><table width=320 border=1 frame=below rules=rows bordercolor=#cccccc cellspacing=0 cellpadding=0>");
						   fprintf(cgiOut,"<tr height=30 bgcolor=#eaeff9 style=font-size:14px  id=td1  align=left>"\
							 "<th width=90><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),search(lcontrol,"queue_TC"));
							 fprintf(cgiOut,"<th width=120><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),search(lcontrol,"Scheduling_Group"));
							 fprintf(cgiOut,"<th width=70><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),search(lcontrol,"weight"));
							 fprintf(cgiOut,"</tr>");
							 for(i=0;i<8;i++)
			                              {
			                              	flag[i]=0;
			                              	query_info[i].QID=0;
			                              	query_info[i].Scheduling_group=(char * )malloc(10);
			                              	memset(query_info[i].Scheduling_group,0,10);
			                              	query_info[i].weight=0;
			                              }
							show_queue(query_info);
							for(i=0;i<8;i++)
							{
								/*memset(menu,0,21);
							  	strcpy(menu,"menulist");
							  	sprintf(i_char,"%d",i+1);
							  	strcat(menu,i_char);*/

								fprintf(cgiOut,"<tr height=25 bgcolor=%s align=left>",setclour(cl));
								 fprintf(cgiOut,"<td>%u</td>",query_info[i].QID);
								 fprintf(cgiOut,"<td>%s</td>",query_info[i].Scheduling_group);
								 fprintf(cgiOut,"<td>%u</td>",query_info[i].weight); 
								 fprintf(cgiOut,"</tr>");
								 cl=!cl;
							}
							 
						  	fprintf(cgiOut,"</table></div>"\
							 "</td>"\
						   "</tr>"\
						 
						"<tr>"\
						"<td id=sec1 style=\"border-bottom:2px solid #53868b;font-size:14px\">%s</td>",search(lpublic,"description"));
						fprintf(cgiOut,"</tr>"\
						 "<tr height=25 style=padding-top:2px>"\
			   			"<td style=font-size:14px;color:#FF0000>%s</td>",search(lcontrol,"queue_TC_des"));
			   			fprintf(cgiOut,"</tr>"\
			   			
						/*"<tr>"\
						"<td>"\
						"<table width=430 style=padding-top:2px>"\
						"<tr>");
						sprintf(pageNumCA,"%d",pageNum+1);
						sprintf(pageNumCD,"%d",pageNum-1);
						if(cgiFormSubmitClicked("submit_config_query") != cgiFormSuccess)
							{
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",encry,pageNumCA,"PageDown",search(lcontrol,"page_down"));
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",encry,pageNumCD,"PageUp",search(lcontrol,"page_up"));
							}
						else if(cgiFormSubmitClicked("submit_config_query") == cgiFormSuccess)
							{
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",policer_encry,pageNumCA,"PageDown",search(lcontrol,"page_down"));
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",policer_encry,pageNumCD,"PageUp",search(lcontrol,"page_up"));  
							}
						fprintf(cgiOut,"</tr></table></td>"\
						"</tr>"\*/
						 "<tr>");
						 if(cgiFormSubmitClicked("submit_config_query") != cgiFormSuccess)
						 {
						   fprintf(cgiOut,"<td colspan=6><input type=hidden name=encry_configqueue value=%s></td>",encry);
						   fprintf(cgiOut,"<td><input type=hidden name=CheckUsr value=%s></td>",addn);
						 }
						 else if(cgiFormSubmitClicked("submit_config_query") == cgiFormSuccess)
							 {
							   fprintf(cgiOut,"<td colspan=6><input type=hidden name=encry_configqueue value=%s></td>",encry);
							   fprintf(cgiOut,"<td><input type=hidden name=CheckUsr value=%s></td>",addn);
							 }
						 fprintf(cgiOut,"</tr>"\
					  "</table>"\

              "</td>"\
            "</tr>"\
            "<tr height=4 valign=top>"\
              "<td width=120 height=4 align=right valign=top><img src=/images/bottom_07.gif width=1 height=10/></td>"\
              "<td width=827 height=4 valign=top bgcolor=#FFFFFF><img src=/images/bottom_06.gif width=827 height=15/></td>"\
            "</tr>"\
          "</table>"\
        "</td>"\
        "<td width=15 background=/images/di999.jpg>&nbsp;</td>"\
      "</tr>"\
    "</table></td>"\
  "</tr>"\
  "<tr>"\
    "<td colspan=3 align=left valign=top background=/images/di777.jpg><img src=/images/di555.jpg width=61 height=62/></td>"\
    "<td align=left valign=top background=/images/di777.jpg>&nbsp;</td>"\
    "<td align=left valign=top background=/images/di777.jpg><img src=/images/di666.jpg width=74 height=62/></td>"\
  "</tr>"\
"</table>"\
"</div>"\
"</form>"\
"</body>"\
"</html>");  
free(encry);
free(i_char);
for(i=0;i<8;i++)
{
	free(query_info[i].Scheduling_group);
}
free(group);
free(QueryID);
free(wrr_weight);
free(queue_mode);
release(lpublic);  
release(lcontrol);
free(mode);
return 0;
}
void
gis_page_util_show_factory_dialog (GisPage *page)
{
  g_autoptr(GtkBuilder) builder = NULL;
  GtkButton *poweroff_button;
  GtkButton *testmode_button;
  GtkDialog *factory_dialog;
  GtkImage *serial_image;
  GtkLabel *image_version_label;
  GtkLabel *sdcard_label;
  GtkLabel *serial_label;
  GtkLabel *product_id_label;
  GtkLabel *version_label;
  gboolean have_serial;
  gchar *barcode;
  gchar *barcode_serial, *display_serial;
  gchar *version;
  gchar *image_version;
  gchar *sd_version = NULL;
  gchar *image_version_text;
  gchar *sd_text;
  gchar *product_id_text;

  builder = get_modals_builder ();
  if (builder == NULL) {
    g_warning ("Can't get private builder object for factory mode!");
    return;
  }

  factory_dialog = (GtkDialog *)gtk_builder_get_object (builder, "factory-dialog");
  version_label = (GtkLabel *)gtk_builder_get_object (builder, "software-version");
  product_id_label = (GtkLabel *)gtk_builder_get_object (builder, "product-id");
  image_version_label = (GtkLabel *)gtk_builder_get_object (builder, "image-version");
  sdcard_label = (GtkLabel *)gtk_builder_get_object (builder, "sd-card");
  serial_label = (GtkLabel *)gtk_builder_get_object (builder, "serial-text");
  serial_image = (GtkImage *)gtk_builder_get_object (builder, "serial-barcode");
  poweroff_button = (GtkButton *)gtk_builder_get_object (builder, "poweroff-button");
  testmode_button = (GtkButton *)gtk_builder_get_object (builder, "testmode-button");

  version = get_software_version ();
  gtk_label_set_text (version_label, version);

  product_id_text = get_product_id ();
  if (product_id_text) {
    gtk_label_set_text (product_id_label, product_id_text);
    gtk_widget_set_visible (GTK_WIDGET (product_id_label), TRUE);
    g_free (product_id_text);
  }

  have_serial = get_serial_version (&display_serial, &barcode_serial);

  if (have_serial) {
    gtk_label_set_text (serial_label, display_serial);

    barcode = create_serial_barcode (barcode_serial);
    gtk_image_set_from_file (serial_image, barcode);
  } else {
    gtk_widget_set_visible (GTK_WIDGET (serial_label), FALSE);
    gtk_widget_set_visible (GTK_WIDGET (serial_image), FALSE);
  }

  image_version = gis_page_util_get_image_version (SYSROOT_MOUNT, NULL);
  if (!image_version)
    sd_version = g_strdup (_("Unknown"));

  image_version_text = g_strdup_printf (_("Image: %s"), image_version);
  gtk_label_set_text (image_version_label, image_version_text);
  g_free (image_version);
  g_free (image_version_text);

  if (get_have_sdcard ())
    sd_version = gis_page_util_get_image_version (SD_CARD_MOUNT, NULL);

  if (!sd_version)
    sd_version = g_strdup (_("Disabled"));

  sd_text = g_strdup_printf (_("SD Card: %s"), sd_version);
  gtk_label_set_text (sdcard_label, sd_text);
  g_free (sd_version);
  g_free (sd_text);

  g_signal_connect_swapped (poweroff_button, "clicked",
                            G_CALLBACK (system_poweroff), NULL);
  g_signal_connect (testmode_button, "clicked",
                    G_CALLBACK (system_testmode), factory_dialog);

  gtk_window_set_transient_for (GTK_WINDOW (factory_dialog),
                                GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (page))));
  gtk_window_set_modal (GTK_WINDOW (factory_dialog), TRUE);
  gtk_window_present (GTK_WINDOW (factory_dialog));
  g_signal_connect (factory_dialog, "delete-event",
                    G_CALLBACK (gtk_widget_hide_on_delete), NULL);

  if (have_serial) {
    g_remove (barcode);
    g_free (barcode);
    g_free (barcode_serial);
    g_free (display_serial);
  }

  g_free (version);
}
int ShowPolicyMapPage()
{
	ccgi_dbus_init();       /*调用底层初始化函数*/
	struct list *lpublic;	/*解析public.txt文件的链表头*/
	struct list *lcontrol; 	/*解析help.txt文件的链表头*/
	lpublic=get_chain_head("../htdocs/text/public.txt");
    lcontrol=get_chain_head("../htdocs/text/control.txt"); 	
  char *encry=(char *)malloc(BUF_LEN);
  char *str=NULL;
 // FILE *fp;
  //char lan[3];
  char policymap_encry[BUF_LEN]; 
  char addn[N];         
  int i=0;   
  int retu=0,retz;
  int cl=1;
  int policy_map_num=0;
  char menu[21]="menulist";
  char* i_char=(char *)malloc(10);
  char * deletepolicy=(char * )malloc(10);
  memset(deletepolicy,0,10);
  char * index=(char * )malloc(10);
  memset(index,0,10);

  char * mode=(char *)malloc(AMOUNT);
  memset(mode,0,AMOUNT);
  char * CheckUsr=(char * )malloc(10);
  memset(CheckUsr,0,10);

  
  struct policy_map_info receive_policy_map[Policy_Map_Num];
  
  for(i=0;i<Policy_Map_Num;i++)
	{
    	 receive_policy_map[i].policy_map_index=0;
    	 receive_policy_map[i].droppre=(char *)malloc(20);
    	 memset(receive_policy_map[i].droppre,0,20);
    	 
    	 receive_policy_map[i].trustMem=(char *)malloc(30);
    	 memset(receive_policy_map[i].trustMem,0,30);
    	 
    	 receive_policy_map[i].modiUp=(char *)malloc(20);
    	 memset(receive_policy_map[i].modiUp,0,20);
    	 
    	 receive_policy_map[i].modiDscp=(char *)malloc(20);
    	 memset(receive_policy_map[i].modiDscp,0,20);
    	 
    	 receive_policy_map[i].remaps=(char *)malloc(20);
    	 memset(receive_policy_map[i].remaps,0,20);
    }
  
  if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
  {
	memset(encry,0,BUF_LEN);
    cgiFormStringNoNewlines("UN", encry, BUF_LEN); 
    str=dcryption(encry);
    if(str==NULL)
    {
      ShowErrorPage(search(lpublic,"ill_user")); 	 /*用户非法*/
      return 0;
	}
	strcpy(addn,str);
	memset(policymap_encry,0,BUF_LEN);                   /*清空临时变量*/
  }
  
  cgiFormStringNoNewlines("encry_routelist",policymap_encry,BUF_LEN);
  cgiFormStringNoNewlines("DELRULE",deletepolicy,10);
  cgiFormStringNoNewlines("INDEX",index,10);
  cgiFormStringNoNewlines("CheckUsr",CheckUsr,10);

  if(strcmp(CheckUsr,"")!=0)
  	retu=atoi(CheckUsr);
	show_qos_mode(mode);
  cgiHeaderContentType("text/html");
  fprintf(cgiOut,"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>");
  fprintf(cgiOut,"<meta http-equiv=Content-Type content=text/html; charset=gb2312>");
  fprintf(cgiOut,"<title>%s</title>",search(lcontrol,"route_manage"));
  fprintf(cgiOut,"<link rel=stylesheet href=/style.css type=text/css>"\
  	"<style type=text/css>"\
  	  "#div1{ width:42px; height:18px; border:1px solid #666666; background-color:#f9f8f7;}"\
	  "#div2{ width:40px; height:15px; padding-left:5px; padding-top:3px}"\
	  "#link{ text-decoration:none; font-size: 12px}"\
  	".ShowPolicy {overflow-x:hidden;  overflow:auto; width: 680px; height: 386px; clip: rect( ); padding-top: 2px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px} "\
  	"</style>"\
  "</head>"\
    "<script type=\"text/javascript\">"\
			  "function popMenu(objId)"\
			  "{"\
				 "var obj = document.getElementById(objId);"\
				 "if (obj.style.display == 'none')"\
				 "{"\
				   "obj.style.display = 'block';"\
				 "}"\
				 "else"\
				 "{"\
				   "obj.style.display = 'none';"\
				 "}"\
			 "}"\
	"</script>"\
  "<body>");
  if(strcmp(deletepolicy,"delete")==0)
  {
  	retz=del_policy_map(index,lcontrol);
	switch(retz)
	{
		case 0:
			ShowAlert(search(lpublic,"oper_succ"));
			break;
		case -2:
			ShowAlert(search(lcontrol,"policy_map_not_exist"));
			break;
		case -3:
			ShowAlert(search(lcontrol,"policy_map_bind_port"));
			break;
		case -4:
			ShowAlert(search(lcontrol,"del_policy_map_fail"));
			break;
		default:
			ShowAlert(search(lpublic,"oper_fail"));
			break;
		}
  }
  if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
  {
  	 retu=checkuser_group(str);
  }
  if(cgiFormSubmitClicked("submit_policymaplist") == cgiFormSuccess)
  {
  	 fprintf( cgiOut, "<script type='text/javascript'>\n" );
 	 fprintf( cgiOut, "window.location.href='wp_qosModule.cgi?UN=%s';\n", policymap_encry);
 	 fprintf( cgiOut, "</script>\n" );
  }


  fprintf(cgiOut,"<form method=post>"\
  "<div align=center>"\
  "<table width=976 border=0 cellpadding=0 cellspacing=0>"\
  "<tr>"\
    "<td width=8 align=left valign=top background=/images/di22.jpg><img src=/images/youce4.jpg width=8 height=30/></td>"\
    "<td width=51 align=left valign=bottom background=/images/di22.jpg><img src=/images/youce33.jpg width=37 height=24/></td>"\
    "<td width=153 align=left valign=bottom background=/images/di22.jpg><font id=titleen>QOS</font><font id=%s> %s</font></td>",search(lpublic,"title_style"),search(lpublic,"management"));
    fprintf(cgiOut,"<td width=690 align=right valign=bottom background=/images/di22.jpg>");
    	  fprintf(cgiOut,"<table width=130 border=0 cellspacing=0 cellpadding=0>"\
          "<tr>"\
          "<td width=62 align=center><input id=but type=submit name=submit_policymaplist style=background-image:url(/images/%s) value=""></td>",search(lpublic,"img_ok"));		  
          
		  if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
		  {
            fprintf(cgiOut,"<td width=62 align=left><a href=wp_qosmap.cgi?UN=%s target=mainFrame><img src=/images/%s border=0 width=62 height=20/></a></td>",encry,search(lpublic,"img_cancel"));
           
            }
		  else                                         
     		fprintf(cgiOut,"<td width=62 align=left><a href=wp_qosmap.cgi?UN=%s target=mainFrame><img src=/images/%s border=0 width=62 height=20/></a></td>",policymap_encry,search(lpublic,"img_cancel"));
		  fprintf(cgiOut,"</tr>"\
          "</table>");
	fprintf(cgiOut,"</td>"\
    "<td width=74 align=right valign=top background=/images/di22.jpg><img src=/images/youce3.jpg width=31 height=30/></td>"\
  "</tr>"\
  "<tr>"\
    "<td colspan=5 align=center valign=middle><table width=976 border=0 cellpadding=0 cellspacing=0 bgcolor=#f0eff0>"\
      "<tr>"\
        "<td width=12 align=left valign=top background=/images/di888.jpg>&nbsp;</td>"\
        "<td width=948><table width=947 border=0 cellspacing=0 cellpadding=0>"\
            "<tr height=4 valign=bottom>"\
              "<td width=120>&nbsp;</td>"\
              "<td width=827 valign=bottom><img src=/images/bottom_05.gif width=827 height=4/></td>"\
            "</tr>"\
            "<tr>"\
              "<td><table width=120 border=0 cellspacing=0 cellpadding=0>"\
                   "<tr height=25>"\
                    "<td id=tdleft>&nbsp;</td>"\
                  "</tr>");       			
         		if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
         		{
         			if(retu==0)
         			{
             			fprintf(cgiOut,"<tr height=25>"\
            			"<td align=left id=tdleft><a href=wp_qosmap.cgi?UN=%s target=mainFrame class=top><font id=yingwen_san>QOS </font><font id=%s>%s</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"list"));					   
            		  	fprintf(cgiOut,"</tr>"\
            		  	"<tr height=25>"\
            			"<td align=left id=tdleft><a href=wp_addqos.cgi?UN=%s target=mainFrame class=top><font id=%s>%s</font><font id=yingwen_san>QOS Profile</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"add"));
            		  	fprintf(cgiOut,"</tr>");
             			fprintf(cgiOut,"<tr height=25>"\
             			"<td align=left id=tdleft><a href=wp_addmap.cgi?UN=%s target=mainFrame class=top><font id=%s>%s</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"add_map"));					   
             			fprintf(cgiOut,"</tr>");

				fprintf(cgiOut,"<tr height=25>"\
             			"<td align=left id=tdleft><a href=wp_qosmapinfo.cgi?UN=%s target=mainFrame class=top><font id=%s>QOS %s</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"map_detail"));					   
             			fprintf(cgiOut,"</tr>");
						
             			fprintf(cgiOut,"<tr height=26>"\
             			"<td align=left id=tdleft background=/images/bottom_bg.gif style=\"border-right:0\"><font id=%s>%s</font></td>",search(lpublic,"menu_san"),search(lcontrol,"policy_map"));   /*突出显示*/
             			fprintf(cgiOut,"</tr>");
             			fprintf(cgiOut,"<tr height=25>"\
             			"<td align=left id=tdleft><a href=wp_createpolicy.cgi?UN=%s target=mainFrame class=top><font id=%s>%s</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"add_policy"));					   
             			fprintf(cgiOut,"</tr>");
         			}
         			else
         			{
	         			fprintf(cgiOut,"<tr height=25>"\
	            			"<td align=left id=tdleft><a href=wp_qosmap.cgi?UN=%s target=mainFrame class=top><font id=yingwen_san>QOS </font><font id=%s>%s</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"list"));					   
	            		  	fprintf(cgiOut,"</tr>");

					fprintf(cgiOut,"<tr height=25>"\
	            			"<td align=left id=tdleft><a href=wp_qosmapinfo.cgi?UN=%s target=mainFrame class=top><font id=yingwen_san>QOS </font><font id=%s>%s</font></a></td>",encry,search(lpublic,"menu_san"),search(lcontrol,"map_detail"));					   
	            		  	fprintf(cgiOut,"</tr>");
					
	            		  	fprintf(cgiOut,"<tr height=26>"\
	             			"<td align=left id=tdleft background=/images/bottom_bg.gif style=\"border-right:0\"><font id=%s>%s</font></td>",search(lpublic,"menu_san"),search(lcontrol,"policy_map"));   /*突出显示*/
	             			fprintf(cgiOut,"</tr>");
         			}
         		}
         		else if(cgiFormSubmitClicked("submit_policymaplist") == cgiFormSuccess) 			  
         		{
         			if(retu==0)
	         		{
	             			fprintf(cgiOut,"<tr height=25>"\
	            			"<td align=left id=tdleft><a href=wp_qosmap.cgi?UN=%s target=mainFrame style=color:#000000><font id=yingwen_san>QOS </font><font id=%s>%s</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"list"));						 
	            		  	fprintf(cgiOut,"</tr>"\
	            		  	"<tr height=25>"\
	            			"<td align=left id=tdleft><a href=wp_addqos.cgi?UN=%s target=mainFrame style=color:#000000><font id=%s>%s</font><font id=yingwen_san>QOS Profile</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"add"));
	            		   	fprintf(cgiOut,"</tr>");
	             			fprintf(cgiOut,"<tr height=25>"\
	             			"<td align=left id=tdleft><a href=wp_addmap.cgi?UN=%s target=mainFrame class=top><font id=%s>%s</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"add_map"));					   
	             			fprintf(cgiOut,"</tr>");

					fprintf(cgiOut,"<tr height=25>"\
	             			"<td align=left id=tdleft><a href=wp_qosmapinfo.cgi?UN=%s target=mainFrame class=top><font id=%s>QOS %s</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"map_detail"));					   
	             			fprintf(cgiOut,"</tr>");
					
	             			fprintf(cgiOut,"<tr height=26>"\
	             			"<td align=left id=tdleft background=/images/bottom_bg.gif style=\"border-right:0\"><font id=%s>%s</font></td>",search(lpublic,"menu_san"),search(lcontrol,"policy_map"));   /*突出显示*/
	             			fprintf(cgiOut,"</tr>");
	             			fprintf(cgiOut,"<tr height=25>"\
	             			"<td align=left id=tdleft><a href=wp_createpolicy.cgi?UN=%s target=mainFrame class=top><font id=%s>%s</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"add_policy"));					   
	             			fprintf(cgiOut,"</tr>");
	         		}
         			else
         			{
	         			fprintf(cgiOut,"<tr height=25>"\
	            			"<td align=left id=tdleft><a href=wp_qosmap.cgi?UN=%s target=mainFrame style=color:#000000><font id=yingwen_san>QOS </font><font id=%s>%s</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"list"));						 
	            		  	fprintf(cgiOut,"</tr>");

					fprintf(cgiOut,"<tr height=25>"\
	            			"<td align=left id=tdleft><a href=wp_qosmapinfo.cgi?UN=%s target=mainFrame style=color:#000000><font id=yingwen_san>QOS </font><font id=%s>%s</font></a></td>",policymap_encry,search(lpublic,"menu_san"),search(lcontrol,"map_detail"));						 
	            		  	fprintf(cgiOut,"</tr>");
							
	            		  	fprintf(cgiOut,"<tr height=26>"\
	             			"<td align=left id=tdleft background=/images/bottom_bg.gif style=\"border-right:0\"><font id=%s>%s</font></td>",search(lpublic,"menu_san"),search(lcontrol,"policy_map"));   /*突出显示*/
	             			fprintf(cgiOut,"</tr>");
         			}
         		}
         		int rowsCount=0;
         		if(retu==0)
         			rowsCount=13;
         		else
         			rowsCount=17;
				for(i=0;i<rowsCount;i++)
					{
						fprintf(cgiOut,"<tr height=25>"\
						  "<td id=tdleft>&nbsp;</td>"\
						"</tr>");
					}
				  fprintf(cgiOut,"</table>"\
              "</td>"\
              "<td align=left valign=top style=\"background-color:#ffffff; border-right:1px solid #707070; padding-left:30px; padding-top:10px\">"\
						"<table width=640 height=310 border=0 cellspacing=0 cellpadding=0>"\
						"<tr>\n"\
							"<td id=sec1 style=\"border-bottom:2px solid #53868b;font-size:14px\">\n"\
								"<table width=%s>\n","100%");
									fprintf(cgiOut,"<tr>\n"\
										"<td id=sec1 style=\"font-size:14px\">%s</td>",search(lcontrol,"policy_map_info"));
				  			fprintf(cgiOut,"<td id=sec1 style=\"font-size:14px\" align='right'><font color='red'><b>%s</b></font></td>",search(lcontrol,mode)),
				  		fprintf(cgiOut,"</tr>\n"\
								"</table>\n"\
							"</td>");
						fprintf(cgiOut,"</tr>"\
						 "<tr>"\
						   "<td align=left valign=top style=padding-top:18px>"\
						   "<div class=ShowPolicy><table width=583 border=1 frame=below rules=rows bordercolor=#cccccc cellspacing=0 cellpadding=0>");
						   fprintf(cgiOut,"<tr height=30 bgcolor=#eaeff9 style=font-size:14px  id=td1 align=left>"\
							 "<th width=60><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"Index");
							 fprintf(cgiOut,"<th width=130><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"QoS sub-markers");
							 if(get_product_id()!=PRODUCT_ID_AU3K_BCM)
							 {
							 	fprintf(cgiOut,"<th width=100><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"TrustMode");
							 }
							 fprintf(cgiOut,"<th width=160 align=left><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"Modify UP");
							 fprintf(cgiOut,"<th width=160 align=left><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"Modify DSCP");
							 fprintf(cgiOut,"<th width=160 align=left><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"Remap DSCP");
							 
							 
							 //////////////////////         add by kehao 02/21/2011  15:24                   ////////////////////////////////////////
							 fprintf(cgiOut,"<th width=0 align=left><font id=%s>%s</font></th>", search(lpublic,"menu_thead"),"Binded PORT");
							 
							 /////////////////////////////////////////////////////////////////////////////////////////////////////////
							 
							 fprintf(cgiOut,"<th width=13>&nbsp;</th>");
							 fprintf(cgiOut,"</tr>");
							 show_policy_map(receive_policy_map,&policy_map_num,lcontrol); //显示策略内容
							//fprintf(stderr,"policy_map_num=%d",policy_map_num);
							for(i=0;i<policy_map_num;i++)
							{
								memset(menu,0,21);
							  	strcpy(menu,"menulist");
							  	sprintf(i_char,"%d",i+1);
							  	strcat(menu,i_char);

								 fprintf(cgiOut,"<tr height=25 bgcolor=%s  align=left>",setclour(cl));
								 fprintf(cgiOut,"<td style=font-size:12px  align=left>%u</td>",receive_policy_map[i].policy_map_index);
								 fprintf(cgiOut,"<td style=font-size:12px  align=left>%s</td>",receive_policy_map[i].droppre);
								 if(get_product_id()!=PRODUCT_ID_AU3K_BCM)
								 {
								 	fprintf(cgiOut,"<td style=font-size:12px align=left>%s</td>",receive_policy_map[i].trustMem);
								 }
								 fprintf(cgiOut,"<td style=font-size:12px align=left>%s</td>",receive_policy_map[i].modiUp);
								 fprintf(cgiOut,"<td style=font-size:12px align=left>%s</td>",receive_policy_map[i].modiDscp);
								 fprintf(cgiOut,"<td style=font-size:12px align=left>%s</td>",receive_policy_map[i].remaps);

								 ///////////////////////add by kehao 02/21/2011 17:12///////////////////


                                   fprintf(cgiOut,"<td style=font-size:12px align=left>%d/%d</td>",receive_policy_map[i].slot_no,receive_policy_map[i].port_no);

                                   //fprintf(cgiOut,"<td style=font-size:12px align=left>%d</td>",receive_policy_map[i].port_no);
								  

								 //////////////////////////////////////////////////////////////////////////////////////
								 if(retu==0)
								 {
     								 fprintf(cgiOut,"<td align=left>");
     								 fprintf(cgiOut,"<div style=\"position:relative; z-index:%d\" onmouseover=\"popMenu('%s');\" onmouseout=\"popMenu('%s');\">",(policy_map_num-i),menu,menu);
     																   fprintf(cgiOut,"<img src=/images/detail.gif>"\
     																   "<div id=%s style=\"display:none; position:absolute; top:5px; left:0;\">",menu);
     																   fprintf(cgiOut,"<div id=div1>");
     																   if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
     																   {
     																   		fprintf(cgiOut,"<div id=div2 onmouseover=\"this.style.backgroundColor='#b6bdd2'\" onmouseout=\"this.style.backgroundColor='#f9f8f7'\"><a id=link href=wp_configpolicy.cgi?UN=%s&INDEX=%u target=mainFrame>%s</a></div>",encry,receive_policy_map[i].policy_map_index,search(lpublic,"configure"));
         																   	fprintf(cgiOut,"<div id=div2 onmouseover=\"this.style.backgroundColor='#b6bdd2'\" onmouseout=\"this.style.backgroundColor='#f9f8f7'\"><a id=link href=wp_policymaplist.cgi?UN=%s&INDEX=%u&DELRULE=%s target=mainFrame onclick=\"return confirm('%s')\">%s</a></div>",encry,receive_policy_map[i].policy_map_index,"delete",search(lcontrol,"confirm_delete"),search(lcontrol,"delete"));
     																   	}
     																   else
     																   {
     																		fprintf(cgiOut,"<div id=div2 onmouseover=\"this.style.backgroundColor='#b6bdd2'\" onmouseout=\"this.style.backgroundColor='#f9f8f7'\"><a id=link href=wp_configpolicy.cgi?UN=%s&INDEX=%u target=mainFrame>%s</a></div>",policymap_encry,receive_policy_map[i].policy_map_index,search(lpublic,"configure"));
         																   	fprintf(cgiOut,"<div id=div2 onmouseover=\"this.style.backgroundColor='#b6bdd2'\" onmouseout=\"this.style.backgroundColor='#f9f8f7'\"><a id=link href=wp_policymaplist.cgi?UN=%s&INDEX=%u&DELRULE=%s target=mainFrame onclick=\"return confirm('%s')\">%s</a></div>",policymap_encry,receive_policy_map[i].policy_map_index,"delete",search(lcontrol,"confirm_delete"),search(lcontrol,"delete"));
     																	}
     																   fprintf(cgiOut,"</div>"\
     																   "</div>"\
     																   "</div>");
     																   fprintf(cgiOut,"</td>");
								}
								else fprintf(cgiOut,"<td>&nbsp;</td>");
								 
							 fprintf(cgiOut,"</tr>");
								 cl=!cl;
							}
							 
						  	fprintf(cgiOut,"</table></div>"\
							 "</td>"\
						   "</tr>"\
						 
						 /*"<tr height=25 style=padding-top:2px>"\
			   			"<td style=font-size:14px;color:#FF0000> K - kernel route, C - connected, S - static, R - RIP, O - OSPF,I - ISIS, B - BGP, > - selected route, * - FIB route</td>"\
			   			"</tr>"\*/
			   			
						/*"<tr>"\
						"<td>"\
						"<table width=430 style=padding-top:2px>"\
						"<tr>");
						sprintf(pageNumCA,"%d",pageNum+1);
						sprintf(pageNumCD,"%d",pageNum-1);
						if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
							{
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",encry,pageNumCA,"PageDown",search(lcontrol,"page_down"));
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",encry,pageNumCD,"PageUp",search(lcontrol,"page_up"));
							}
						else if(cgiFormSubmitClicked("submit_policymaplist") == cgiFormSuccess)
							{
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",policymap_encry,pageNumCA,"PageDown",search(lcontrol,"page_down"));
								fprintf(cgiOut,"<td align=center style=padding-top:2px><a href=wp_srouter.cgi?UN=%s&PN=%s&SN=%s>%s</td>",policymap_encry,pageNumCD,"PageUp",search(lcontrol,"page_up"));  
							}
						fprintf(cgiOut,"</tr></table></td>"\
						"</tr>"\*/
						 "<tr>");
						 if(cgiFormSubmitClicked("submit_policymaplist") != cgiFormSuccess)
						 {
						   fprintf(cgiOut,"<td><input type=hidden name=encry_routelist value=%s></td>",encry);
						   fprintf(cgiOut,"<td><input type=hidden name=CheckUsr value=%d></td>",retu);
						 }
						 else if(cgiFormSubmitClicked("submit_policymaplist") == cgiFormSuccess)
							 {
							   fprintf(cgiOut,"<td><input type=hidden name=encry_routelist value=%s></td>",policymap_encry);
							   fprintf(cgiOut,"<td><input type=hidden name=CheckUsr value=%d></td>",retu);
							 }
						 fprintf(cgiOut,"</tr>"\
					  "</table>"\

              "</td>"\
            "</tr>"\
            "<tr height=4 valign=top>"\
              "<td width=120 height=4 align=right valign=top><img src=/images/bottom_07.gif width=1 height=10/></td>"\
              "<td width=827 height=4 valign=top bgcolor=#FFFFFF><img src=/images/bottom_06.gif width=827 height=15/></td>"\
            "</tr>"\
          "</table>"\
        "</td>"\
        "<td width=15 background=/images/di999.jpg>&nbsp;</td>"\
      "</tr>"\
    "</table></td>"\
  "</tr>"\
  "<tr>"\
    "<td colspan=3 align=left valign=top background=/images/di777.jpg><img src=/images/di555.jpg width=61 height=62/></td>"\
    "<td align=left valign=top background=/images/di777.jpg>&nbsp;</td>"\
    "<td align=left valign=top background=/images/di777.jpg><img src=/images/di666.jpg width=74 height=62/></td>"\
  "</tr>"\
"</table>"\
"</div>"\
"</form>"\
"</body>"\
"</html>");  
free(encry);
free(i_char);
for(i=0;i<Policy_Map_Num;i++)
  {

	  	free(receive_policy_map[i].droppre);
	  	free(receive_policy_map[i].trustMem);
	  	free(receive_policy_map[i].modiUp);
	  	free(receive_policy_map[i].modiDscp);
	  	free(receive_policy_map[i].remaps);
 }
free(deletepolicy);
free(CheckUsr);
free(index);
release(lpublic);  
release(lcontrol);
free(mode);
return 0;
}
Exemple #10
0
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Event Function
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void gps_gateway_process(int x, short int y, void *pargs)
{
    unsigned int unFromAddrLen;
    int nByte = 0;
    int nSendByte = 0;
    unsigned char aReqBuffer[512];
    unsigned char aRespBuffer[512];
    struct sockaddr_in stFromAddr;
    bson_oid_t oid;
    bson_t *doc;
    const bson_t *result;

    bson_t *query;
    int i = 0;
    int command = 0;
    int monitor_type = 0;
    char telephone[10];
    char text_message[240];
    int length;

    char strBaseinfo[10];
    AGPS_info * agps;
    cell_info * cell;

    unFromAddrLen = sizeof(stFromAddr);
    memset(aReqBuffer,0x00,512);
    memset(aRespBuffer,0x00,512);

    if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0,
        (struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1)
    {
        printf("error occured while receivingn");
    }

    //proc();
    data_frame_head *msg = (data_frame_head *) aReqBuffer;

    content_gps_up *g = (content_gps_up *)(aReqBuffer + sizeof(data_frame_head));

    //异或校验

    //异或校验
    unsigned char xor = *(aReqBuffer + nByte - 2);

    if( xor != check_xor(aReqBuffer, nByte - 2))
    {
        return ;//drop down BAD check xor.
    }

    switch (msg->cmd)
    {
        case CMD_TERMINAL_GPS_UP  :


            //解析位置信息,得到经纬度
            fprintf(stderr,"protocal head %x \n",msg->data_head[0] );
            fprintf(stderr,"cmd %x \n",       msg->cmd );
            fprintf(stderr,"length %d \n",   ntohs(msg->length) );
            //fprintf(stderr,"product_id %u\n",get_product_id(msg->terminal_id));
            fprintf(stderr,"product_id %u\n",get_product_id(g->terminal_id));
            fprintf(stderr,"flow_id %x \n",   ntohs(g->flow_id) );
            fprintf(stderr,"data_type %x \n",   g->data_type );
            fprintf(stderr," time " );

            for (  i = 0 ; i< 6 ;i++)
                fprintf(stderr," %x \n",   g->base_info.date_time[i] );
            /////////
            //经纬度
            fprintf(stderr," latitude " );
            for (  i = 0 ; i< 4 ;i++)
                fprintf(stderr,"%x \n",   g->base_info.latitude[i] );
            fprintf(stderr," longitude " );
            for (  i = 0 ; i< 4 ;i++)
                fprintf(stderr," %x \n",   g->base_info.longitude[i] );
            /////////
            fprintf(stderr,"speed %u \n", ntohs(g->base_info.speed));
            fprintf(stderr,"fangxiang %u \n", ntohs(g->base_info.direction));
            fprintf(stderr,"gaodu %u\n", ntohs(g->base_info.high));
            fprintf(stderr,"定位状态 %c\n", g->base_info.pos_station);
            fprintf(stderr,"是否应答 %x\n", g->base_info.need_response);

            if (ntohs(msg->length) > sizeof (content_gps_up)) {
                fprintf(stderr,"含有附带信息\n");
                unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up);
                fprintf(stderr,"附带信息类型:%x\n",*attach_info);
                fprintf(stderr,"附带信息长度:%d\n",*(attach_info+1));
                
                switch (*(attach_info))
                {
                    case 0x01:
                        break;
                    case 0x02:
                        fprintf(stderr,"基站信息\n");
                        agps = (AGPS_info * ) ( attach_info + 2);
                        cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info));
                        fprintf(stderr,"\n国家编码");

                        fprintf(stderr," %x ", ntohl(agps->country_code) );
                        fprintf(stderr,"\n运营编码");
                        
                        fprintf(stderr," %x ", ntohl(agps->mobile_code) );
                        fprintf(stderr,"\n运基站定位\n");

                        for (  i = 0 ; i< agps->base_count ;i++){
                            fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) );
                            fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) );
                            fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN );
                            cell++;
                        }
                        break;


                    case 0x0C:
                    case 0x1E:
                    case 0x1F:
                    case 0x28:
                    case 0x40:
                    case 0x41:
                    case 0x42:
                    case 0x43:
                    case 0x44:
                    case 0x45:

                        break;

                }

            }

            if( g->data_type == 0x80 || g->data_type == 0x82 || g->data_type == 0x8E )
            {

                //保存mongodb
                unsigned char timegps[15];
                unsigned char latitude[10];
                unsigned char longitude[10];
                double lat;
                double lng;
                doc = bson_new ();
                bson_oid_init (&oid, NULL);
                BSON_APPEND_OID (doc, "_id", &oid);
                BSON_APPEND_INT32 (doc, "id",get_product_id(g->terminal_id));

                get_gps_time(g->base_info.date_time, timegps );
                
                BSON_APPEND_UTF8 (doc, "timestamp", timegps);
                
                lat = bcd2longitude(g->base_info.latitude, latitude );
                lng = bcd2longitude(g->base_info.longitude, longitude );
               
                BSON_APPEND_UTF8 (doc, "latitude", latitude);
                BSON_APPEND_UTF8 (doc, "longitude", longitude);
                BSON_APPEND_DOUBLE (doc, "lat", lat);
                BSON_APPEND_DOUBLE (doc, "lng", lng);
                BSON_APPEND_INT32 (doc, "speed", ntohs(g->base_info.speed));
                BSON_APPEND_INT32 (doc, "direction", ntohs(g->base_info.direction));
                BSON_APPEND_INT32 (doc, "high", ntohs(g->base_info.high));
                BSON_APPEND_INT32 (doc, "pos_station", g->base_info.pos_station);

               if (ntohs(msg->length) > sizeof (content_gps_up)) {
                    unsigned char *attach_info = aReqBuffer + sizeof(data_frame_head) + sizeof(content_gps_up);
                    
                    switch (*(attach_info))
                    {
                        case 0x01:
                            break;
                        case 0x02:
                            agps = (AGPS_info * ) ( attach_info + 2);
                            cell = (cell_info * ) ( attach_info + 2 + sizeof(AGPS_info));
                            

                            fprintf(stderr,"\n国家编码");

                            fprintf(stderr," %x ", ntohl(agps->country_code) );
                            BSON_APPEND_INT32 (doc, "country_code", ntohl(agps->country_code));

                            fprintf(stderr,"\n运营编码");
                            
                            fprintf(stderr," %x ", ntohl(agps->mobile_code) );
                            BSON_APPEND_INT32 (doc, "mobile_code", ntohl(agps->mobile_code));

                            fprintf(stderr,"\n运基站定位\n");

                            for (  i = 0 ; i< agps->base_count ;i++){
                                fprintf(stderr," 基站 %d 位置区编码 %d\n", i+1, ntohs(cell->lacN) );
                                fprintf(stderr," 基站 %d 小区编码 %d \n", i+1, ntohs(cell->cellN) );
                                fprintf(stderr," 基站 %d 信号强度 %x \n", i+1, cell->signalN );
                                sprintf(strBaseinfo,"lac%d",i);
                                BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->lacN) );
                                sprintf(strBaseinfo,"cell%d",i);
                                BSON_APPEND_INT32 (doc, strBaseinfo, ntohs(cell->cellN) );
                                sprintf(strBaseinfo,"signal%d",i);
                                BSON_APPEND_INT32 (doc, strBaseinfo, cell->signalN );

                                cell++;
                            }
                            break;


                        case 0x0C:
                        case 0x1E:
                        case 0x1F:
                        case 0x28:
                        case 0x40:
                        case 0x41:
                        case 0x42:
                        case 0x43:
                        case 0x44:
                        case 0x45:

                            break;

                    }

                }

                if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
                    printf ("Insert failed: %s\n", error.message);
                }

                bson_destroy (doc);
            }
            else if( g->data_type == 0x85 )
            {

                //delete from mongo
                query = bson_new ();
                BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id));

                if( ! mongoc_collection_remove(collection_cmd, MONGOC_DELETE_NONE, query, NULL, &error))
                {
                    fprintf(stderr,"remove command failed \n"   );
                }
                bson_destroy (query);

            }
            
            if (g->base_info.need_response == 0x01)
            {
                send_22_response( x,  aRespBuffer ,  g->flow_id, xor ,  g->data_type,
                          stFromAddr,  unFromAddrLen );
            }
            

            
            break;

        case CMD_TERMINAL_INFO_UP :
            fprintf(stderr,"CMD_TERMINAL_INFO_UP\n");
            content_info_up * t = (content_info_up *)(aReqBuffer + sizeof(data_frame_head));
            fprintf(stderr,"parameter %x \n",   t->parameter );

            if(t->parameter == 0x01)
            {

                sent_time_response( x, aRespBuffer , g->terminal_id, stFromAddr, unFromAddrLen );
            }

            break;
        case CMD_VERSION_INFO_UP  :
            fprintf(stderr,"CMD_VERSION_INFO_UP");
            break;
        case CMD_VOICE_UP         :
            fprintf(stderr,"CMD_VOICE_UP");
            break;
        default:
            break;
    }


    //get command from mongo
    char *str;
    query = bson_new ();
    BSON_APPEND_INT32 (query, "id",get_product_id(g->terminal_id));



    cursor = mongoc_collection_find (collection_cmd, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

    //while (mongoc_cursor_next (cursor, &doc)) {
    //   mongoc_cursor_next (cursor, &result);
    while (mongoc_cursor_next (cursor, (const  bson_t **) &doc)) {
        bson_iter_t iter;
        bson_iter_t sub_iter;
        str = bson_as_json (doc, NULL);
        fprintf (stderr, "%s\n", str);
        bson_free (str);

        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "cmd", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            command = (int)bson_iter_int32 (&sub_iter);
        }
        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "parameter", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            strcpy (telephone , bson_iter_utf8 (&sub_iter,&length));
        }
        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "monitor_type", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            monitor_type = (int)bson_iter_int32 (&sub_iter);
        }

        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "text_message", &sub_iter)) {
            fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            strcpy (text_message , bson_iter_utf8 (&sub_iter,&length));
        }
    }
    mongoc_cursor_destroy (cursor);

    bson_destroy (query);

    //send command to handring

    if (command == 1)
    {
        send_monitor_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen ,monitor_type, telephone);
    }
    else if (command == 9)
    {
        send_sleep_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen );
    }
    else if (command == 2)
    {
        send_set_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen ,text_message);
    }
    else{
        //为了避免长时间使用网络。在通信6次发送一个休眠指令
        if( (msg->cmd == CMD_TERMINAL_GPS_UP) && (check_state(g->terminal_id) == MAX_STANDBY) )
        {
            send_sleep_cmd( x,  aRespBuffer , g->terminal_id, stFromAddr,  unFromAddrLen );
        }
    }

    //printf("Function called buffer is %sn",aReqBuffer);
    g_count++;

}
Exemple #11
0
int check_state(unsigned char * terminal_id)
{

    bson_oid_t oid;
    bson_t *doc = NULL;
    bson_t *update = NULL;
    bson_t *query = NULL;
    int count;
    int found = 0;

 
    found = mongoc_collection_count (collection_state, MONGOC_QUERY_NONE, query, 0, 0, NULL, &error);

    if (found == 0) {
        doc = bson_new ();
        bson_oid_init (&oid, NULL);
        BSON_APPEND_INT32 (doc, "id", get_product_id(terminal_id));
        BSON_APPEND_INT32 (doc, "count", 0);

        if (!mongoc_collection_insert (collection_state, MONGOC_INSERT_NONE, doc, NULL, &error)) {
            printf ("%s\n", error.message);
        }

    }

    query = bson_new ();
    BSON_APPEND_INT32 (query, "id", get_product_id(terminal_id));

    cursor = mongoc_collection_find (collection_state, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

    //while (mongoc_cursor_next (cursor, &doc)) {
    //   mongoc_cursor_next (cursor, &result);
    while (mongoc_cursor_next (cursor, (const  bson_t **) &doc)) {
        bson_iter_t iter;
        bson_iter_t sub_iter;
        // str = bson_as_json (doc, NULL);
        // fprintf (stderr, "%s\n", str);
        // bson_free (str);

        if (bson_iter_init (&iter, doc) && bson_iter_find_descendant (&iter, "count", &sub_iter)) {
            // fprintf (stderr,"Found key \"%s\" in sub document.\n", bson_iter_key (&sub_iter));
            // printf ("The type of a.b.c.d is: %d\n", (int)bson_iter_type (&sub_iter));
            count = (int)bson_iter_int32 (&sub_iter);
        }
    }
    fprintf (stderr,"Found count %d .\n", count);

    count++;
    
    if (count > MAX_STANDBY) 
    {
        count = 1;
        update = bson_new ();
        BSON_APPEND_INT32 (update, "id",get_product_id(terminal_id));
        BSON_APPEND_INT32 (update, "count", count);


        if (!mongoc_collection_update (collection_state, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {
            printf ("%s\n", error.message);
        }
    }
    else
    {
        update = bson_new ();
        BSON_APPEND_INT32 (update, "id",get_product_id(terminal_id));
        BSON_APPEND_INT32 (update, "count", count);

        if (!mongoc_collection_update (collection_state, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {
            printf ("%s\n", error.message);
        }
    }

    return count;


}