예제 #1
0
char* MyMessage::getCustomString(char *buffer) const {
	for (uint8_t i = 0; i < miGetLength(); i++)
	{
		buffer[i * 2] = i2h(data[i] >> 4);
		buffer[(i * 2) + 1] = i2h(data[i]);
	}
	buffer[miGetLength() * 2] = '\0';
	return buffer;
}
예제 #2
0
char* MyMessage::getStream(char *buffer) const {
	uint8_t cmd = miGetCommand();
	if ((cmd == C_STREAM) && (buffer != NULL)) {
		for (uint8_t i = 0; i < miGetLength(); i++)
		{
			buffer[i * 2] = i2h(data[i] >> 4);
			buffer[(i * 2) + 1] = i2h(data[i]);
		}
		buffer[miGetLength() * 2] = '\0';
		return buffer;
	} else {
		return NULL;
static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf, uint8_t sz) {
	static char printBuffer[300];
#ifdef MY_GATEWAY_FEATURE
	// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
	snprintf_P(printBuffer, 299, PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
	MY_SERIALDEVICE.print(printBuffer);
#endif
	for (int i=0; i<sz; i++)
	{
		printBuffer[i * 2] = i2h(buf[i] >> 4);
		printBuffer[(i * 2) + 1] = i2h(buf[i]);
	}
	printBuffer[sz * 2] = '\0';
#ifdef MY_GATEWAY_FEATURE
	// Truncate message if this is gateway node
	printBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1-strlen_P((const char*)str)] = '\0';
#endif
	MY_SERIALDEVICE.print(str);
	if (sz > 0)
	{
		MY_SERIALDEVICE.print(printBuffer);
	}
	MY_SERIALDEVICE.println("");
}
예제 #4
0
파일: login.c 프로젝트: jpocas/libiscsi
static int
iscsi_login_add_chap_response(struct iscsi_context *iscsi, struct iscsi_pdu *pdu)
{
	char str[MAX_STRING_SIZE+1];
	char * strp;
	unsigned char c, cc[2];
	unsigned char digest[16];
	gcry_md_hd_t ctx;
	int i;

	if (iscsi->current_phase != ISCSI_PDU_LOGIN_CSG_SECNEG
	|| iscsi->secneg_phase != ISCSI_LOGIN_SECNEG_PHASE_SEND_RESPONSE) {
		return 0;
	}

	gcry_md_open(&ctx, GCRY_MD_MD5, 0);
	if (!ctx) {
		iscsi_set_error(iscsi, "Cannot create MD5 algorithm");
		return -1;
	}

	if (!iscsi->chap_c[0]) {
		iscsi_set_error(iscsi, "No CHAP challenge found");
		return -1;
	}
	gcry_md_putc(ctx, iscsi->chap_i);
	gcry_md_write(ctx, (unsigned char *)iscsi->passwd, strlen(iscsi->passwd));

	strp = iscsi->chap_c;
	while (*strp != 0) {
		c = (h2i(strp[0]) << 4) | h2i(strp[1]);
		strp += 2;
		gcry_md_putc(ctx, c);
	}
	memcpy(digest, gcry_md_read(ctx, 0), sizeof(digest));
	gcry_md_close(ctx);

	strncpy(str,"CHAP_R=0x",MAX_STRING_SIZE);
	if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str))
	    != 0) {
		iscsi_set_error(iscsi, "Out-of-memory: pdu add data failed.");
		return -1;
	}

	for (i=0; i<16; i++) {
		c = digest[i];
		cc[0] = i2h((c >> 4)&0x0f);
		cc[1] = i2h((c     )&0x0f);
		if (iscsi_pdu_add_data(iscsi, pdu, &cc[0], 2) != 0) {
			iscsi_set_error(iscsi, "Out-of-memory: pdu add data "
				"failed.");
			return -1;
		}
	}
	c = 0;
	if (iscsi_pdu_add_data(iscsi, pdu, &c, 1) != 0) {
		iscsi_set_error(iscsi, "Out-of-memory: pdu add data "
			"failed.");
		return -1;
	}

	return 0;
}
예제 #5
0
파일: vprintf.c 프로젝트: piif/ArduinoLibs
int gvprintf(writeCharFunc writeChar, char *fmt, va_list ap) {
	int len = 0;
	while(*fmt) {
		if (*fmt == '%') {
			uint8_t size = 2; // default size
			uint32_t mask = 0xFFFF;
			char f = 'd'; // default type
			fmt++;

			switch(*fmt) {
			case '1':
				size = 1;
				fmt++;
				break;
			case '2':
				size = 2;
				fmt++;
				break;
			case '4':
				size = 4;
				fmt++;
				break;
			}
			switch(*fmt) {
			case '%':
				outputChar(*fmt);
				break;
			case 's':
				{
					char *s = va_arg (ap, char *);
					while(*s) {
						outputChar(*s++);
					}
				}
				break;
			case 'S':
				{
					char *s = va_arg (ap, char *);
					char c;
					while((c = pgm_read_byte(s)) != '\0') {
						outputChar(c);
						s++;
					}
				}
				break;
			case 'c':
				{
					unsigned char c = (unsigned char)va_arg (ap, int /*char*/);
					outputChar(c);
				}
				break;
			case 'u':
			case 'd':
			case 'x':
			case 'b':
				{
					uint32_t v;
					switch (size) {
					case 1:
						v = va_arg (ap, int/*uint8_t*/);
						break;
					case 2:
						v = va_arg (ap, int/*uint16_t*/);
						break;
					case 4:
						v = va_arg (ap, uint32_t);
						break;
					}
					if (*fmt == 'x') {
						len += i2h(writeChar, v, size);
					} else if (*fmt == 'b') {
						len += i2b(writeChar, v, size);
					} else {
						len += i2d(writeChar, v, size, *fmt == 'd');
					}
				}
				break;
			default:
				outputChar(*fmt);
				break;
			}
			fmt++;
		} else {
예제 #6
0
파일: server.c 프로젝트: ceppa/socket
void performAction(int fd,char *buf)
/*--------------------------------------------
 * esegue operazione richiesta dal client (web o telnet)
 * int fd 	 - descrittore del socket
 * char *buf - stringa con il comando
 * attualmente implementate:

 * 	checkBuonGiorno
 * 	checkBuonaNotte 
 * 	presenze_attiva 
 * 	presenze_disattiva
 * 	irrigazione_attiva id_circuito
 * 	irrigazione_disattiva id_circuito
 * 	bg_attiva
 * 	bn_attiva
 * 	bg_disattiva	
 * 	bn_disattiva
 * 	get
 * 		channels	(restituisce i descrittori dei canali)
 * 		values		(restituisce i valori dei canali)
 * 		latest_values	(restituisce gli ultimi valori eng dei canali)
 * 		outchannels
 * 		outputs
 * 		mm_names
 * 		mm_values
 * 		knx_values
 * 		value adId channel_n
 * 		panel panel_id
 * 		presenze_attivo
 * 		irrigazione_attivo irrigazione_n
 * 		buongiorno_attivo
 * 		buonanotte_attivo
 * 		irrigazione_rt irrigazione_n (real time table)
 * 		irrigazione irrigazione_n (mysql tabel)
 * 		pioggia
 * 		effemeridi
 * 		sottostato inizio_estate inizio_inverno (restituisce antiintrusione,pioggia,giorno,estate)
 * 	set	
 * 		knx_value id_knx value
 * 		output system_n channel_n value 
 * 		output ch_id value
 * 		irrigazione id_irrigazione circuito ora_start ripetitivita tempo_off durata validita
 * 		toggle system_n channel_n
 * 		reset system_n (sends reset signal)
 * 		status
 * 	reload	(spegne e riaccende tutti i sistemi)
 * 		system	(ricarica tabella system)
 * 		analog	(ricarica tabella analog)
 * 		digital	(ricarica tabella digital)
 * 		out		(ricarica tabella digitalOut)
 * 		presenze	(ricarica scenari_presenze)
 * 		bgbn 	(ricarica scenari bgbn)
 * 	dig system_n channel_n	(restituisce corrispondente canale digitale)
 * 	ana system_n channel_n	(restituisce corrispondente canale analogico)
 * 	out system_n channel_n	(restituisce corrispondente uscita digitale)
 * 	update
 * 		analog channel_id
 * 		digital channel_id
 * 		out channel_id
 * 		bg
 * 		bn
 * 		presenze
 * 		irrigazione
 * 	system
 * 		update system_n		(ricarica scheda system_n)
 * 		disable system_n	(disabilita scheda system_n)
 * 		enable system_n		(abilita scheda system_n)
 * 	chn
 * 		disable system_n adId channel_n	(disabilita canale channel_n tipo adId su scheda system_n)
 * 		enable system_n adId channel_n	(abilita canale channel_n tipo adId su scheda system_n)
 *-----------------------------------------*/
{
	char buffer[255];
	int value;
	int i,j,k;
	int state;
	MYSQL *connection=NULL, mysql;
	int systemId;
	int chnId;
	int adId;
	char query[255];
	char *c,*d;
	struct timeval tim;
	double t1;
	int rem_time;
	
	if(strstr(buf,"checkBuonGiorno")==buf)
	{
		*buonGiornoAttivo=checkBuonGiornoAttivo();
		sprintf(buffer,"%d %s",*buonGiornoAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"checkBuonaNotte")==buf)
	{
		*buonaNotteAttivo=checkBuonaNotteAttivo();
		sprintf(buffer,"%d %s",*buonaNotteAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"presenze_attiva")==buf)
	{
		*scenariPresenzeAttivo=1;
		sprintf(buffer,"%d %s",*scenariPresenzeAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"irrigazione_attiva ")==buf)
	{
		i=atoi(&buf[19]);
		j=id_irrigazioneToId(i);

		if(j>=0)
		{
			if(circuiti_attivi(j)>0)
			{
				irrigazioneTable[j].attivo=1;
				irrigazione_attiva(j,irrigazioneTable[j].msg_avvio_manuale);
			}
			sprintf(buffer,"%d %s",irrigazioneTable[j].attivo,TERMINATOR);
			if (send(fd, buffer,strlen(buffer), 0) == -1)
				perror("send");
		}
		else
		{
			sprintf(buffer,"-1 %s",TERMINATOR);
			if (send(fd, buffer,strlen(buffer), 0) == -1)
				perror("send");
		}
		return;
	}
	if(strstr(buf,"bg_attiva")==buf)
	{
		*buonaNotteAttivo=0;
		for(i=0;i<SCENARIBGBNCOUNT;i++)
		{
			if((scenariBgBnTable[i].bg==0)&&(scenariBgBnTable[i].attivo))
			{
				j=id_digitalOutToId(scenariBgBnTable[i].id_digital_out);
				digitalOutTable[j].start_time=0;
				digitalOutTable[j].req_value=digitalOutTable[j].value;
			}
		}

		*buonGiornoAttivo=1;
		for(i=0;i<SCENARIBGBNCOUNT;i++)
		{
			if((scenariBgBnTable[i].bg)&&(scenariBgBnTable[i].attivo))
			{
				j=id_digitalOutToId(scenariBgBnTable[i].id_digital_out);
				digitalOutTable[j].start_time=time(NULL);
				digitalOutTable[j].req_value=digitalOutTable[j].on_value;
				digitalOutTable[j].po_delay=scenariBgBnTable[i].ritardo;
			}
		}
		sprintf(buffer,"%d %s",*buonGiornoAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"bn_attiva")==buf)
	{
		*buonGiornoAttivo=0;
		for(i=0;i<SCENARIBGBNCOUNT;i++)
		{
			if((scenariBgBnTable[i].bg)&&(scenariBgBnTable[i].attivo))
			{
				j=id_digitalOutToId(scenariBgBnTable[i].id_digital_out);
				digitalOutTable[j].start_time=0;
				digitalOutTable[j].req_value=digitalOutTable[j].value;
			}
		}

		*buonaNotteAttivo=1;
		for(i=0;i<SCENARIBGBNCOUNT;i++)
		{
			if((scenariBgBnTable[i].bg==0)&&(scenariBgBnTable[i].attivo))
			{
				j=id_digitalOutToId(scenariBgBnTable[i].id_digital_out);
				digitalOutTable[j].start_time=time(NULL);
				digitalOutTable[j].req_value=1-digitalOutTable[j].on_value;
				digitalOutTable[j].po_delay=scenariBgBnTable[i].ritardo;
			}
		}
		sprintf(buffer,"%d %s",*buonaNotteAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"bg_disattiva")==buf)
	{
		*buonGiornoAttivo=0;
		for(i=0;i<SCENARIBGBNCOUNT;i++)
		{
			if((scenariBgBnTable[i].bg)&&(scenariBgBnTable[i].attivo))
			{
				j=id_digitalOutToId(scenariBgBnTable[i].id_digital_out);
				digitalOutTable[j].start_time=0;
				digitalOutTable[j].req_value=digitalOutTable[j].value;
			}
		}
		sprintf(buffer,"%d %s",*buonGiornoAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"bn_disattiva")==buf)
	{
		*buonaNotteAttivo=0;
		for(i=0;i<SCENARIBGBNCOUNT;i++)
		{
			if((scenariBgBnTable[i].bg==0)&&(scenariBgBnTable[i].attivo))
			{
				j=id_digitalOutToId(scenariBgBnTable[i].id_digital_out);
				digitalOutTable[j].start_time=0;
				digitalOutTable[j].req_value=digitalOutTable[j].value;
			}
		}
		sprintf(buffer,"%d %s",*buonaNotteAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"presenze_disattiva")==buf)
	{
		*scenariPresenzeAttivo=0;
		sprintf(buffer,"%d %s",*scenariPresenzeAttivo,TERMINATOR);
		if (send(fd, buffer,strlen(buffer), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"irrigazione_disattiva ")==buf)
	{
		i=atoi(&buf[21]);
		i=id_irrigazioneToId(i);
		if(i>=0)
		{
			irrigazioneTable[i].attivo=0;
			irrigazione_disattiva(i,irrigazioneTable[i].msg_arresto_manuale);
			
			sprintf(buffer,"%d %s",irrigazioneTable[i].attivo,TERMINATOR);
			if (send(fd, buffer,strlen(buffer), 0) == -1)
				perror("send");
		}
		else
		{
			sprintf(buffer,"-1 %s",TERMINATOR);
			if (send(fd, buffer,strlen(buffer), 0) == -1)
				perror("send");
		}
		return;
	}
	if(strstr(buf,"set")==buf)
	{
		if(strstr(&buf[3]," knx_value ")==&buf[3])
		{
			if(c=strchr(&buf[14],' '))
			{
				*c='\0';
				c++;
				setKnx(atoi(&buf[14]),atof(c));
			}
		}
		if(strstr(&buf[3]," output ")==&buf[3])
		{
			if(c=strchr(&buf[11],' '))
			{
				*c='\0';
				c++;
				if(d=strchr(c,' '))
				{
					*d='\0';
					d++;
					systemId=systemNumToId(atoi(&buf[11]),NUMSYSTEMS);
					chnId=atoi(c)-1;
					printf("%d %d\n",digitalOutTable[deviceToid[2][systemId][chnId]].id_digital_out, atoi(d));
					if(((systemId<NUMSYSTEMS)&&(chnId<systems[systemId].out_ch_d))
							&&(digitalOutTable[deviceToid[2][systemId][chnId]].id_digital_out!=-1))
						setOutput(digitalOutTable[deviceToid[2][systemId][chnId]].id_digital_out,atoi(d));
				}
				else
					setOutput(atoi(&buf[11]),atoi(c));
				return;
			}
		}
		if(strstr(&buf[3]," irrigazione")==&buf[3])
		{
			set_irrigazione(fd,&buf[15]);
			return;
		}
		return;
	}
	if(strstr(buf,"toggle ")==buf)
	{
		if(c=strchr(&buf[7],' '))
		{
			*c='\0';
			c++;
			systemId=systemNumToId(atoi(&buf[7]),NUMSYSTEMS);
			chnId=atoi(c)-1;
			if(((systemId<NUMSYSTEMS)&&(chnId<systems[systemId].out_ch_d))
					&&(digitalOutTable[deviceToid[2][systemId][chnId]].id_digital_out!=-1)
					&&(digitalOutTable[deviceToid[2][systemId][chnId]].value!=-1))
				setOutput(digitalOutTable[deviceToid[2][systemId][chnId]].id_digital_out,
					(1-digitalOutTable[deviceToid[2][systemId][chnId]].value));
		}
		return;
	}
	if(strstr(buf,"reset ")==buf)
	{
		sendResetSignal(systemNumToId(atoi(&buf[6]),NUMSYSTEMS));
		return;
	}
	if(strstr(buf,"status")==buf)
	{
		for(i=0;i<NUMSYSTEMS;i++)
			buffer[i]=systems[i].status;
		for(j=0;j<strlen(TERMINATOR);j++)
			buffer[j+i]=TERMINATOR[j];
		if (send(fd, buffer, NUMSYSTEMS+strlen(TERMINATOR), 0) == -1)
			perror("send");
		return;
	}
	if(strstr(buf,"reload ")==buf)
	{
		if(strstr(&buf[7]," system")==&buf[7])
		{
			loadSystemTable(1);
			return;
		}
		if(strstr(&buf[7]," analog")==&buf[7])
		{
			loadAnalogTable(1);
			return;
		}
		if(strstr(&buf[7]," digital")==&buf[7])
		{
			loadDigitalTable(1);
			return;
		}
		if(strstr(&buf[7]," out")==&buf[7])
		{
			loadDigitalOutTable(1);
			return;
		}
		if(strstr(&buf[7]," presenze")==&buf[7])
		{
			loadScenariPresenzeTable(1);
			return;
		}
		if(strstr(&buf[7]," bgbn")==&buf[7])
		{
			loadScenariBgBnTable(1);
			return;
		}
		return;
	}
	if(strstr(buf,"reload")==buf)
	{
		*reloadCommand=1;
		return;
	}
	if(strstr(buf,"dig ")==buf)
	{
		if(c=strchr(&buf[4],' '))
		{
			*c='\0';
			c++;
			i=deviceToid[1][systemNumToId(atoi(&buf[4]),NUMSYSTEMS)][atoi(c)-1];
			printf("id_digital\tdescription\tdevice_num\tch_num\tenabled\n");
			printf("%d\t%s\t%d\t%d\t%d\n",digitalTable[i].id_digital,
						digitalTable[i].description,digitalTable[i].device_num,
						digitalTable[i].ch_num,digitalTable[i].enabled);
		}
		return;
	}
	if(strstr(buf,"ana ")==buf)
	{
		if(c=strchr(&buf[4],' '))
		{
			*c='\0';
			c++;
			i=deviceToid[0][systemNumToId(atoi(&buf[4]),NUMSYSTEMS)][atoi(c)-1];
			printf("id_analog\tdescription\tdevice_num\tch_num\tenabled\n");
			printf("%d\t%s\t%d\t%d\t%d\n",analogTable[i].id_analog,
						analogTable[i].description,analogTable[i].device_num,
						analogTable[i].ch_num,analogTable[i].enabled);
		}
		return;
	}
	if(strstr(buf,"out ")==buf)
	{
		if(c=strchr(&buf[4],' '))
		{
			*c='\0';
			c++;
			i=deviceToid[2][systemNumToId(atoi(&buf[4]),NUMSYSTEMS)][atoi(c)-1];
			printf("id_digital_out\tdescription\tdevice_num\tch_num\tvalue\tpo_delay\tstart\n");
			printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",digitalOutTable[i].id_digital_out,
						digitalOutTable[i].description,digitalOutTable[i].device_num,
						digitalOutTable[i].ch_num,digitalOutTable[i].value,
						digitalOutTable[i].po_delay,(int)digitalOutTable[i].start_time);
		}
		return;
	}
	
	
	if(strstr(buf,"update")==buf)
	{
		if(strstr(&buf[6]," analog ")==&buf[6])
		{
			updateAnalogChannel(atoi(&buf[14]));
			return;
		}
		if(strstr(&buf[6]," digital ")==&buf[6])
		{
			updateDigitalChannel(atoi(&buf[15]));
			return;
		}
		if(strstr(&buf[6]," out ")==&buf[6])
		{
			updateDigitalOutChannel(fd,atoi(&buf[11]));
			return;
		}
		if(strstr(&buf[6]," bg")==&buf[6])
		{
			updateDayNight(fd);
			return;
		}
		if(strstr(&buf[6]," bn")==&buf[6])
		{
			updateDayNight(fd);
			return;
		}
		if(strstr(&buf[6]," presenze")==&buf[6])
		{
			updatePresenze(fd);
			return;
		}
		if(strstr(&buf[6]," irrigazione")==&buf[6])
		{
			int id_irrigazione=atoi(&buf[18]);
			if(id_irrigazione<1)
				id_irrigazione=-1;
			updateIrrigazioneCircuiti(fd,id_irrigazione);
			return;
		}
		return;
	}
	if(strstr(buf,"get")==buf)
	{
		if(strstr(&buf[3]," channels")==&buf[3])
		{
			for(i=0;i<NUMSYSTEMS;i++)
			{
				if(systems[i].enabled)
				{
					for(j=0;j<systems[i].in_ch_an;j++)
					{
						if(analogTable[deviceToid[0][i][j]].enabled)
						{
							k=deviceToid[0][i][j];
							sprintf(buffer,"0 %d %d %s`",analogTable[k].device_num,
									analogTable[k].ch_num,analogTable[k].description);
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
					for(j=0;j<systems[i].in_ch_d;j++)
					{
						if(digitalTable[deviceToid[1][i][j]].enabled)
						{
							k=deviceToid[1][i][j];
							sprintf(buffer,"1 %d %d %s`",digitalTable[k].device_num,
									digitalTable[k].ch_num,digitalTable[k].description);
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}
			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," values")==&buf[3])
		{
			for(i=0;i<NUMSYSTEMS;i++)
			{
				if(systems[i].enabled)
				{
					for(j=0;j<systems[i].in_ch_an;j++)
					{
						if(analogTable[deviceToid[0][i][j]].enabled)
						{
							k=deviceToid[0][i][j];
							sprintf(buffer,"0 %d %d %f %f %f %f %f %d %d`",
								analogTable[k].device_num,
								analogTable[k].ch_num,
								analogTable[k].value_eng,
								analogTable[k].value_eng1,
								analogTable[k].value_eng2,
								analogTable[k].value_eng3,
								analogTable[k].value_eng4,
								analogTable[k].value,
								(analogTable[k].enabled && systems[i].enabled));

							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
					for(j=0;j<systems[i].in_ch_d;j++)
					{
						if(digitalTable[deviceToid[1][i][j]].enabled)
						{
							k=deviceToid[1][i][j];
							sprintf(buffer,"1 %d %d %d %d %d %d %d %d %d`",
								digitalTable[k].device_num,
								digitalTable[k].ch_num,
								digitalTable[k].value,
								digitalTable[k].value1,
								digitalTable[k].value2,
								digitalTable[k].value3,
								digitalTable[k].value4,
								digitalTable[k].value,
								(digitalTable[k].enabled && systems[i].enabled));
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}
			for(i=0;i<NUMMULTIMETERS;i++)
			{
				if(multimeters[i].enabled)
				{
					for(j=0;j<multimeters[i].out_ch_1+multimeters[i].out_ch_2;j++)
					{
						if(readingTable[deviceToid[3][i][j]].enabled)
						{
							k=deviceToid[3][i][j];
							sprintf(buffer,"3 %d %d %d %d`",
								readingTable[k].multimeter_num,
								readingTable[k].ch_num,
								readingTable[k].value,
								(readingTable[k].enabled && multimeters[i].enabled));
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}

			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," latest_values")==&buf[3])
		{
			for(i=0;i<NUMSYSTEMS;i++)
			{
				if(systems[i].enabled)
				{
					for(j=0;j<systems[i].in_ch_an;j++)
					{
						if(analogTable[deviceToid[0][i][j]].enabled)
						{
							k=deviceToid[0][i][j];
							sprintf(buffer,"0 %d %d %f %d`",
								analogTable[k].device_num,
								analogTable[k].ch_num,
								analogTable[k].value_eng,
								(analogTable[k].enabled && systems[i].enabled));

							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
					for(j=0;j<systems[i].in_ch_d;j++)
					{
						if(digitalTable[deviceToid[1][i][j]].enabled)
						{
							k=deviceToid[1][i][j];
							sprintf(buffer,"1 %d %d %d %d`",
								digitalTable[k].device_num,
								digitalTable[k].ch_num,
								digitalTable[k].value,
								(digitalTable[k].enabled && systems[i].enabled));
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
					for(j=0;j<systems[i].out_ch_d;j++)
					{
						k=deviceToid[2][i][j];
						sprintf(buffer,"2 %d %d %d %d`",
							digitalOutTable[k].device_num,
							digitalOutTable[k].ch_num,
							digitalOutTable[k].value,
							systems[i].enabled);
						if (send(fd, buffer, strlen(buffer), 0) == -1)
							perror("send");
					}
				}
			}
			for(i=0;i<NUMMULTIMETERS;i++)
			{
				if(multimeters[i].enabled)
				{
					for(j=0;j<multimeters[i].out_ch_1+multimeters[i].out_ch_2;j++)
					{
						if(readingTable[deviceToid[3][i][j]].enabled)
						{
							k=deviceToid[3][i][j];
							sprintf(buffer,"3 %d %d %d %d`",
								readingTable[k].multimeter_num,
								readingTable[k].ch_num,
								readingTable[k].value,
								(readingTable[k].enabled && multimeters[i].enabled));
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}

			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}


		if(strstr(&buf[3]," mm_names")==&buf[3])
		{
			for(i=0;i<NUMMULTIMETERS;i++)
			{
				if(multimeters[i].enabled)
				{
					for(j=0;j<multimeters[i].out_ch_1+multimeters[i].out_ch_2;j++)
					{
						if(readingTable[deviceToid[3][i][j]].enabled)
						{
							k=deviceToid[3][i][j];
							sprintf(buffer,"%d %d %s`",readingTable[k].multimeter_num,
									readingTable[k].ch_num,readingTable[k].description);
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}
			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," mm_values")==&buf[3])
		{
			for(i=0;i<NUMMULTIMETERS;i++)
			{
				if(multimeters[i].enabled)
				{
					for(j=0;j<multimeters[i].out_ch_1+multimeters[i].out_ch_2;j++)
					{
						if(readingTable[deviceToid[3][i][j]].enabled)
						{
							k=deviceToid[3][i][j];
							sprintf(buffer,"%d %d %d %d`",
								readingTable[k].multimeter_num,
								readingTable[k].ch_num,
								readingTable[k].value,
								(readingTable[k].enabled && multimeters[i].enabled));

							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}
			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," knx_values")==&buf[3])
		{
			for(i=0;i<NUMKNXCHANNELS;i++)
			{
				j=id_knx_gatewayToId(knxTable[i].id_knx_gateway);
				if((knxTable[i].input_output==1)&&
					(knxGateways[j].enabled))
				{
					if(knxTable[i].data_type=='F')
						sprintf(buffer,"%d %d %f %d`",
							knxTable[i].id_knx_gateway,
							knxTable[i].id_knx_line,
							knxTable[i].value_eng,
							knxTable[i].enabled);
					else
						sprintf(buffer,"%d %d %d %d`",
							knxTable[i].id_knx_gateway,
							knxTable[i].id_knx_line,
							knxTable[i].value,
							knxTable[i].enabled);

					if (send(fd, buffer, strlen(buffer), 0) == -1)
						perror("send");
				}
			}
			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," outchannels")==&buf[3])
		{
			for(i=0;i<NUMSYSTEMS;i++)
			{
				if(systems[i].enabled)
				{
					for(j=0;j<systems[i].out_ch_d;j++)
					{
						if(digitalOutTable[deviceToid[2][i][j]].id_digital_out!=-1)
						{
							k=deviceToid[2][i][j];
							sprintf(buffer,"%d %d %s`",digitalOutTable[k].device_num,
								digitalOutTable[k].ch_num,digitalOutTable[k].description);
							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}
			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," outputs")==&buf[3])
		{
			for(i=0;i<NUMSYSTEMS;i++)
			{
				if(systems[i].enabled)
				{
					for(j=0;j<systems[i].out_ch_d;j++)
					{
						if(digitalOutTable[deviceToid[2][i][j]].id_digital_out!=-1)
						{
							rem_time=digitalOutTable[deviceToid[2][i][j]].po_delay - 
									(time(NULL)-digitalOutTable[deviceToid[2][i][j]].start_time);
							if(rem_time<0)
								rem_time=0;
							
							k=deviceToid[2][i][j];
							sprintf(buffer,"%d %d %d %d`",
								digitalOutTable[k].device_num,
								digitalOutTable[k].ch_num,
								digitalOutTable[k].value,
								rem_time);

							if (send(fd, buffer, strlen(buffer), 0) == -1)
								perror("send");
						}
					}
				}
			}
			if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," value ")==&buf[3])
		{
			if(strlen(&buf[10])>2)
			{
				buf[11]='\0';
				
				adId=atoi(&buf[10]);
				if(adId==0)
				{
					i=id_analogToId(atoi(&buf[12]));
					sprintf(buffer,"%f",analogTable[i].value_eng);
				}
				else
				{
					i=id_digitalToId(atoi(&buf[12]));
					sprintf(buffer,"%d",digitalTable[i].value);
				}
				if (send(fd, buffer, strlen(buffer), 0) == -1)
					perror("send");
				if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
					perror("send");
			}
			return;
		}
		if(strstr(&buf[3]," panel ")==&buf[3])
		{
			if((strlen(&buf[10])>=0)&&(atoi(&buf[10])<NUMPANELS))
			{
				for(i=0;i<16;i++)
				{
					sprintf(buffer,"%d %f`",panels[atoi(&buf[10])][i],
							analogTable[id_analogToId(panels[atoi(&buf[10])][i])].value_eng);
					if (send(fd, buffer, strlen(buffer), 0) == -1)
						perror("send");
				}
				if (send(fd, TERMINATOR, strlen(TERMINATOR), 0) == -1)
					perror("send");
			}
			return;
		}
		if(strstr(&buf[3]," presenze_attivo")==&buf[3])
		{
			sprintf(buffer,"%d %s",*scenariPresenzeAttivo,TERMINATOR);
			if (send(fd, buffer, strlen(buffer), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," irrigazione_attivo ")==&buf[3])
		{
			i=atoi(&buf[23]);
			i=id_irrigazioneToId(i);
			if(i>=0)
			{
				sprintf(buffer,"%d %s",(irrigazioneTable[i].current_circuito>0),TERMINATOR);
				if (send(fd, buffer, strlen(buffer), 0) == -1)
					perror("send");
			}
			else
			{
				sprintf(buffer,"-1 %s",TERMINATOR);
				if (send(fd, buffer,strlen(buffer), 0) == -1)
					perror("send");
			}


			return;
		}
		if(strstr(&buf[3]," buongiorno_attivo")==&buf[3])
		{
			*buonGiornoAttivo=checkBuonGiornoAttivo();
			sprintf(buffer,"%d %s",*buonGiornoAttivo,TERMINATOR);
			if (send(fd, buffer, strlen(buffer), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," buonanotte_attivo")==&buf[3])
		{
			*buonaNotteAttivo=checkBuonaNotteAttivo();
			sprintf(buffer,"%d %s",*buonaNotteAttivo,TERMINATOR);
			if (send(fd, buffer, strlen(buffer), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," irrigazione_rt ")==&buf[3])
		{
			get_irrigazione_rt(fd,&buf[18]);
			return;
		}
		if(strstr(&buf[3]," irrigazione ")==&buf[3])
		{
			get_irrigazione(fd,&buf[15]);
			return;
		}
		if(strstr(&buf[3]," pioggia")==&buf[3])
		{
			int pioggia=-1;
			if(*id_digital_pioggia!=-1)
			{
				if(digitalTable[*id_digital_pioggia].value!=-1)
					pioggia=(digitalTable[*id_digital_pioggia].value==1);
			}
			sprintf(buffer,"%d %s",pioggia,TERMINATOR);
			if (send(fd, buffer, strlen(buffer), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," effemeridi")==&buf[3])
		{
			time_t timer;
			struct tm now;
			char dawn[6];
			char sunset[6];
			timer=time(NULL);
			now=*localtime(&timer);
	
			i2h(dawn,effemeridiTable[now.tm_mon][now.tm_mday - 1].dawn);
			i2h(sunset,effemeridiTable[now.tm_mon][now.tm_mday - 1].sunset);
			sprintf(buffer,"%s %s %s",dawn,sunset,TERMINATOR);
			if (send(fd, buffer, strlen(buffer), 0) == -1)
				perror("send");
			return;
		}
		if(strstr(&buf[3]," sottostato ")==&buf[3])
		{
			get_sottostato(fd,&buf[15]);
			return;
		}
		return;
	}
	if(strstr(buf,"system")==buf)
	{
		if(strstr(&buf[6]," update ")==&buf[6])
		{
			strcpy(buf,&buf[14]);
			while(c=strchr(buf,'|'))
			{
				*c='\0';
				i=atoi(buf);
				updateSystem(i);
				strcpy(buf,c+1);
			}
		}
		if(strstr(&buf[6]," disable")==&buf[6])
		{
			if(strlen(&buf[14]))
			{
				systemId=atoi(&buf[14]);
				mysql_init(&mysql);
				connection = mysql_real_connect(&mysql,"localhost","root", "minair","socket", 0, NULL,0);
				if( connection == NULL ) 
					return;
				sprintf(query,"UPDATE system SET enabled=0 WHERE device_num=%d",systems[systemId].device_num);
				state = mysql_query(connection, query);
				if(state==0)
				{
					systems[systemId].enabled=0;
					systems[systemId].status='d';
				}
				mysql_close(connection);
				return;
			}
		}
		if(strstr(&buf[6]," enable")==&buf[6])
		{
			if(strlen(&buf[13]))
			{
				systemId=atoi(&buf[13]);
				mysql_init(&mysql);
				connection = mysql_real_connect(&mysql,"localhost","root", "minair","socket", 0, NULL,0);
				if( connection == NULL ) 
					return;
				sprintf(query,"UPDATE system SET enabled=1 WHERE device_num=%d",systems[systemId].device_num);
				state = mysql_query(connection, query);
				if(state==0)
					systems[systemId].enabled=1;
				mysql_close(connection);		
				return;
			}
		}
	}
	if(strstr(buf,"chn")==buf)
	{
		if(strstr(&buf[3]," disable ")==&buf[3])
		{
			if(strlen(&buf[12])&&(c=strchr(&buf[12],' '))&&(d=strchr(c+1,' ')))
			{
				*c='\0';
				*d='\0';
				systemId=atoi(&buf[12]);
				adId=atoi(c+1);
				chnId=atoi(d+1);
				mysql_init(&mysql);
				connection = mysql_real_connect(&mysql,"localhost","root", "minair","socket", 0, NULL,0);
				if( connection == NULL ) 
					return;
				if(adId==0)
					sprintf(query,"UPDATE analog SET enabled=0 WHERE device_num=%d AND ch_num=%d",analogTable[deviceToid[0][systemId][chnId]].device_num,analogTable[deviceToid[0][systemId][chnId]].ch_num);
				else
					sprintf(query,"UPDATE digital SET enabled=0 WHERE device_num=%d AND ch_num=%d",digitalTable[deviceToid[1][systemId][chnId]].device_num,digitalTable[deviceToid[1][systemId][chnId]].ch_num);
				state = mysql_query(connection, query);
				printf("%s\n",query);
				if(state==0)
				{
					if(adId==0)
						analogTable[deviceToid[adId][systemId][chnId]].enabled=0;
					else
						digitalTable[deviceToid[adId][systemId][chnId]].enabled=0;
				}
				return;
			}
		}
		if(strstr(&buf[3]," enable ")==&buf[3])
		{
			if(strlen(&buf[11])&&(c=strchr(&buf[11],' '))&&(d=strchr(c+1,' ')))
			{
				*c='\0';
				*d='\0';
				systemId=atoi(&buf[11]);
				adId=atoi(c+1);
				chnId=atoi(d+1);
				mysql_init(&mysql);
				connection = mysql_real_connect(&mysql,"localhost","root", "minair","socket", 0, NULL,0);
				if( connection == NULL ) 
					return;
				if(adId==0)
					sprintf(query,"UPDATE analog SET enabled=1 WHERE device_num=%d AND ch_num=%d",analogTable[deviceToid[0][systemId][chnId]].device_num,analogTable[deviceToid[0][systemId][chnId]].ch_num);
				else
					sprintf(query,"UPDATE digital SET enabled=1 WHERE device_num=%d AND ch_num=%d",digitalTable[deviceToid[1][systemId][chnId]].device_num,digitalTable[deviceToid[1][systemId][chnId]].ch_num);
				state = mysql_query(connection, query);
				printf("%s\n",query);
				if(state==0)
				{
					if(adId==0)
						analogTable[deviceToid[adId][systemId][chnId]].enabled=1;
					else
						digitalTable[deviceToid[adId][systemId][chnId]].enabled=1;
				}
				return;
			}
		}
	}
	return;
}