DAT290 / kod / USART / Commands.c
Commands.c
Raw
#include <Commands.h>






void ping(short id){
	if(id == 255){
		print("Pinging all peripherals...");
	}else{
		print("Pinging id...");
	}
}


void doorInit(){
	print("Initializing door...");
}

void doorActivate(short active){
	(active == 1) ? print("Activating door...") : print("Deactivating door...");
}

void doorConfig(){
	print("Configuring door...");
}

void motionInit(){
	print("Initializing motion-detecctor...");
}

void motionActivate(short active){
	(active == 1) ? print("Activating motion-detector...") : print("Deactivation motion-detector");
}

void motionConfig(){
	print("Configuring motion-detector...");
}
 
short isNumericValue(char* s){
		short rv = 1;
		while (*s != '\0'){
			if(isdigit(*s) == 0){
				rv = 0;
				break;
			}
			
			*(s++);
		}
			
		return rv;
}

short isCommand(Command command, CommandString* commandString[], short* numberOfCommands, short* numberOfProperties){
	
	if((*numberOfCommands != command.depth) || (*numberOfProperties != command.propertyCount)){
		return 0;
	}
	
	
	short stringLength = strlen(command.command);
	char delim[] = " ";
	short rv = 1;
	
	char *tmp = strtok(command.command, delim);
	short iterations = 0;
	
	while(tmp != 0)
	{
		if(strcmp(tmp, commandString[iterations]->command) != 0){
			rv = 0;
		}
		
		tmp = strtok(0, delim);
		iterations++;
	}
	
	return rv;
}

void printErrorMessage(){
	print("Error - Command does not exist.");
}

 
 void testfunc(char* s){
	print(s);
}
void commandHandler(CommandString* commands[], short numberOfCommands){
		short numberOfActualCommands, numberOfNumericValues = 0;
		for(int i = 0; i < numberOfCommands; i++){
				(isNumericValue(commands[i]->command) == 0) ? numberOfActualCommands++ : numberOfNumericValues++;
		}
		
		
		/*
		 * Command - ping 'id'
		 */
		
		Command pingCommand = {
		.depth = 1, 
		.propertyCount = 1,
		.handler = &ping,
		.command="ping"};
		
		if(isCommand(pingCommand, commands, &numberOfActualCommands, &numberOfNumericValues)){
			pingCommand.handler((short)atoi(commands[1]->command));
			return;
		};
		
		
		/*
		 * Command - ping all
		 */
		
		Command pingAllCommand = {
		.depth = 2, 
		.propertyCount = 0,
		.handler = &ping,
		.command="ping all"};
		
		
		if(isCommand(pingAllCommand, commands, &numberOfActualCommands, &numberOfNumericValues)){
			pingAllCommand.handler(255);
			return;
		};
		
		
		
		
	
		print("Error - Command does not exist.");
}