void client_open(response_t* response,client_t* c,char* name){
   int index;
   pthread_mutex_lock( c->new_account_lock_mutex_ptr);
   if(*c->accounts_no_ptr == MAX_ACCOUNT){
        form_response(response,CANNOT_OPEN,"Database is full.");
        pthread_mutex_unlock(c->new_account_lock_mutex_ptr);
        return;
   }else{
        index = search_account(c->accounts,*c->accounts_no_ptr,name);
        if(index == -1){
            account_open(&c->accounts[0],name);
        }else if(index <= *c->accounts_no_ptr -1){
            form_response(response,CANNOT_OPEN,"Account's already created.");
            pthread_mutex_unlock(c->new_account_lock_mutex_ptr);
            return;
        }else{
            account_open(&c->accounts[index],name);
        }
        (*c->accounts_no_ptr)++;
   }
   pthread_mutex_unlock(c->new_account_lock_mutex_ptr);
   char message [200];
   sprintf(message,"Account %s opened",name);
   form_response(response,SUCCESS,message);
}
void client_start(response_t* response,client_t* c,account_t** cli_acc 
        ,char* name,int client_sock_fd){
    int index;
    account_t* account;
    int accounts_no;

    pthread_mutex_lock(c->new_account_lock_mutex_ptr);
    accounts_no = *c->accounts_no_ptr;
    pthread_mutex_unlock(c->new_account_lock_mutex_ptr);
    
    index = search_account(c->accounts,accounts_no,name);
    if(index == -1 || index == accounts_no){
        form_response(response,CANNOT_START,"Account is not found.");
        return;
    }else{
        account = & c->accounts[index];
        while(pthread_mutex_trylock(&account->account_session_mutex)!=0){
            form_response(response,SUCCESS,"Waiting to start customer session.");
            write(client_sock_fd,response,sizeof(response_t));
            sleep(2);
        } 
    }
    *cli_acc = account;
    account->in_session = IN_SESSION;
    char message [200];
    sprintf(message,"Account %s is in session.",name);
    form_response(response,SUCCESS,message);
}
void client_debit(response_t* response,account_t* account, char* amount){
    char message[100];
    int result = account_debit(account,atof(amount));
    if(result == 0){
        sprintf(message,"Balance reached 0. Your balance is: $%.2f",
                account->balance);
        form_response(response,BALANCE_REACH_ZERO, message);
    }else if(result == 1){
        sprintf(message,"Successfully debit $%s.",amount);
        form_response(response,SUCCESS,message);
    }
}
BOOL parse_firmware( char* mIncoming )
{
	char expression[] = "read (serial number|revision)";

	BOOL is_position_command = strcmp(mIncoming, "get serial number");
	sprintf(Response, "Serial#=%ld", FiveMotorConfigData.serialNumber );	
	form_response(Response);
	BOOL is_revision = strcmp(mIncoming, "revision");	
	if (is_revision)
		sprintf(Response,"Version=%d.%d.%d",
				 FiveMotorConfigData.FirmwareRevA,
				 FiveMotorConfigData.FirmwareRevB,
				 FiveMotorConfigData.FirmwareRevC );
		form_response(Response);
	return is_position_command;
}
示例#5
0
/*
 *   Reply
 * ---------------------------------------------------------------------
 * - function responsible for responding to the message sent by client
 * - creates buffer where the respond is stored, loads data to the buffer
 *   and then writes them to the socket
 */
int reply(int socket_data)
{
  int len = 0;
  ioctl(socket_data, FIONREAD, &len);
  if (len <= 0) return EXIT_FAILURE;

  char * buffer = NULL;
  if ((buffer = (char *) malloc(sizeof(char) * len) ) == NULL)
    return EXIT_FAILURE;
  read(socket_data, buffer, len);

//  printf("Process of PID %d recieved data from client:\n%s\n", getpid(), buffer);

  char * response = NULL;
  if ((response = (char *) calloc(BUFSIZE, sizeof(char))) == NULL)
    return EXIT_FAILURE;

  if (form_response(buffer, response) != EXIT_SUCCESS) return EXIT_FAILURE;

  if (write(socket_data, response, strlen(response) +1 ) < 0)
    { print_err("Server PID(%d): Unable to send data back to client", getpid());
      return EXIT_FAILURE; }

  free(response);
  close(socket_data);
  free(buffer);
  return EXIT_SUCCESS;
}
BOOL parse_use_encoder( char* mIncoming )
{
	char expression[] = "^use (encoder|potentiometer)";
	int match = re_match( 2, Captures, expression, mIncoming );
		
	int use_encoder 	   = strcmp(mIncoming, "use encoder");
	int use_potentiometer  = strcmp(mIncoming, "use potientiometer");			
	if (use_encoder==0) {
			FiveMotorConfigData.use_encoder = 1;
			form_response("ACK: use encoder");
	} else if (use_potentiometer==0) {
			FiveMotorConfigData.use_encoder = 0;
			form_response("ACK using potentiometer");
	}
	return FALSE;
}
示例#7
0
/* 
INPUT:  Single line '\n' terminated.  	
	Login:mtenniswood;Password:Tobart;Device name:motorola XT1565;\n
	
OUTPUT: 
	m_login_name
	m_login_password
*/
bool ServerHandler::parse_credentials( string mCredentials )
{
	bool retval = true;
	SuperString str = mCredentials;
	
	int count = str.split(';');
	str.trim_spaces_in_splits();		
	if (count < 3) {
		form_response( "Sorry there was garbled text in your credentials." );
		return false;		
	}
	//	[0] is "Login;"
	SuperString name   = str.m_split_words[1];	// UserID
	SuperString passwd = str.m_split_words[2];	// Password
	SuperString hostname = str.m_split_words[3];	// Password
		
	name.split(':');
	m_login_name = name.m_split_words[1];
	passwd.split(':'); 
	m_login_password = passwd.m_split_words[1];
			
	hostname.split(':');
	m_login_hostname = hostname.m_split_words[1];
	//printf("\tdevname=%s\n", m_login_hostname.c_str() );
	//printf("Login=%s\t\tpasswd=%s\n", m_login_name.c_str(), m_login_password.c_str() );
	return retval;
}
/************************************************************************
 END Differential Drive Train Commands 
*************************************************************************/
void top_level( char* mIncoming )
{
	bool retval=false;

	retval = parse_use_encoder			( mIncoming );		if (retval) return;
	retval = parse_set_unit				( mIncoming );		if (retval) return;
	retval = parse_zero_encoders		( mIncoming );		if (retval) return;
	retval = parse_read_position		( mIncoming );		if (retval) return;
	retval = parse_measure_travel		( mIncoming );		if (retval) return;
	retval = parse_home_command			( mIncoming );		if (retval) return;

	retval = parse_duty_command			( mIncoming );		if (retval) return;
	retval = parse_position_command 	( mIncoming );		if (retval) return;
	retval = parse_speed_command		( mIncoming );		if (retval) return;

	retval = parse_limits_enable		( mIncoming );		if (retval) return;
	retval = parse_firmware				( mIncoming );		if (retval) return;
	
	retval = parse_set_base_frequency_command( mIncoming );	if (retval) return;
	retval = parse_diff_spin_command		( mIncoming );	if (retval) return;
	retval = parse_diff_move_command		( mIncoming );	if (retval) return;
	
	if (retval==false)
		form_response( "NAK:unknown command." );		
}
void send_motor_status()
{
	char* sstr;
	sprintf(buff, "Motor Status: \r\n");
	send_message(buff);

	for (int i=0; i<5; i++) {	
		sstr = get_status_string( FiveMotorConfigData.motor_status[i] );
		sprintf(buff, "%c:%s  ", motor_letters[i], sstr);
		send_message(buff);
	}
	strcpy(buff, "\r\n");	
	send_message(buff);

	send_limit_states();
		
	sprintf(buff, "Raw Status: %x %x %x %x %x", 
						FiveMotorConfigData.motor_status[0], 
						FiveMotorConfigData.motor_status[1],
						FiveMotorConfigData.motor_status[2],
						FiveMotorConfigData.motor_status[3],
						FiveMotorConfigData.motor_status[4]  );
	strcat(buff, "\r\n");	
	form_response(buff);
}
void send_base_frequency()
{
	char buffer[255];
	float num = get_base_frequency_herz();
	sprintf(buffer,"Base Frequency (hz): %6.2f\n", num );
	form_response(buffer);
	send_message (buffer);	
}
示例#11
0
void send_base_frequency()
{
	float num = get_base_frequency_herz();
	char temp[10];
	dtostrf( num, 4, 1, temp );
	sprintf(buff,"Base Frequency (hz): %s", temp );
	form_response(buff);
}
示例#12
0
文件: ui.c 项目: imgflo/imgflo
// Must not call g_log family functions to avoid recursion
static void
send_response_nodebug(SoupWebsocketConnection *ws,
            const gchar *protocol, const gchar *command, JsonObject *payload)
{
    if (ws) {
        gchar *text = form_response(protocol, command, payload);
        soup_websocket_connection_send_text(ws, text);
        g_free(text);
    }
}
示例#13
0
文件: ui.c 项目: imgflo/imgflo
static void
send_response(SoupWebsocketConnection *ws,
            const gchar *protocol, const gchar *command, JsonObject *payload)
{
    g_return_if_fail(ws);

    gchar *text = form_response(protocol, command, payload);
    imgflo_debug ("SEND: %s\n", text);
    soup_websocket_connection_send_text(ws, text);
    g_free(text);
}
示例#14
0
void send_destinations()
{
	for (int i=0; i<5; i++) {
		if (mot_states[i].direction)
			sprintf(buff,"%c  Directoin=+;  Destination=%8ld\r\n", motor_letters[i], mot_states[i].destination );
		else 
			sprintf(buff,"%c  Directoin=-;  Destination=%8ld\r\n", motor_letters[i], mot_states[i].destination );	
		send_message( buff );		
	}
	sprintf(buff,"Motor destinations.");
	form_response( buff );
}
BOOL parse_set_unit( char* mIncoming )
{
	char expression = "^set unit (inch|meter|mm|feet)";
	int match = re_match( 2, Captures, expression, mIncoming );
	
	int set_unit = strcmp(mIncoming, "set unit ");
	char* ptr = mIncoming+0;
	if ( (strcmp(mIncoming, "meters")==0) )	{
		FiveMotorConfigData.units  = meters;   		// enum eMeasuringUnit		
		form_response( "ACK unit=meters" );
	} if ( (strcmp(mIncoming, "mm")==0) )	{
		FiveMotorConfigData.units  = millimeters;   // enum eMeasuringUnit
		form_response( "ACK unit=mm" );
	} if ( (strcmp(mIncoming, "feet")==0) )	 {
		FiveMotorConfigData.units  = feet;
		form_response( "ACK unit=feet" );
	} if ( (strcmp(mIncoming, "inches")==0) )	{
		FiveMotorConfigData.units  = inches;	
		form_response( "ACK unit=inches" );
	}
	return FALSE;
}
示例#16
0
bool ServerHandler::validate( string mCredentials )
{
	m_credentials_validated = false;

	m_user_id = sql_users.sql_find( m_login_name );
	if (m_user_id)
	{
		printf("Found sql username : %s\t", m_login_name.c_str() );
		m_credentials_validated = sql_users.verify_password( m_login_name, m_login_password );
		if (m_credentials_validated)
		{
			form_response( "Welcome" );
		} else {
			form_response( "Bad password" );
		}
	}
	else {
		printf("NOT found in dbase\n");		
		form_response( "Sorry I can't find that username.  Please register." );
	}
	return m_credentials_validated;
}
示例#17
0
void send_current_readings()
{
	char temp[10];
		
	sprintf(buff, "Current: ");
	for (int i=0; i<5; i++)
	{
		//dtostrf( CurrentSample[i], 3, 1, temp );
		sprintf(temp, " %d ", CurrentSample[i] );
		strcat( buff, temp );
		//strcat( buff, " ");
	}	
	form_response( buff );
}
BOOL parse_speed_command( char* mIncoming )
{
	char expression[] = "^motor speed:([vV]\\d+)? ?([wW]\\d+)? ?([xX]\\d+)? ?([yY]\\d+)? ?([zZ]\\d+)?";
	
	BOOL is_speed_command = strcmp(mIncoming, "motor speed");
	byte which = which_motors(mIncoming);
	float fraction=0.0;
	for (int b=0; b<NUM_MOTORS; b++)
	{
		if (is_in_set(which, b) )
			set_motor_duty( b, fraction );		
	}
	form_response("ACK: speed request");	
	return is_speed_command;
}
BOOL parse_position_command( char* mIncoming )
{
	char expression[] = "^motor position:([vV]\\d+)? ?([wW]\\d+)? ?([xX]\\d+)? ?([yY]\\d+)? ?([zZ]\\d+)?";
	
	BOOL is_position_command = strcmp(mIncoming, "motor position ");
	byte which = which_motors(mIncoming);
	uint32_t pos;
	for (int b=0; b<NUM_MOTORS; b++)
	{
		if (is_in_set(which, b) )
			set_motor_position( b, pos );		
	}	
	form_response("ACK: position request");		
	return is_position_command;
}
BOOL parse_home_command( char* mIncoming )
{
	char expression = "home ([vV])? ([wW])? ([xX])? ([yY])? ([zZ])?";
	int match = re_match( 2, Captures, expression, mIncoming );
		
	BOOL is_speed_command = strcmp(mIncoming, "HOME:");
	byte which = which_motors(mIncoming);
	for (int b=0; b<NUM_MOTORS; b++)
	{
		if (is_in_set(which, b) )
			set_motor_duty( b, HOMING_SPEED );		
	}
	form_response("ACK: homing...");
	return is_speed_command;
}
BOOL parse_zero_encoders( char* mIncoming )
{
	char expression = "zero (position|encoders) (v)?(w)?(x)?(y)?(z)?";
	int match = re_match( 7, Captures, expression, mIncoming );
		
	BOOL is_speed_command = strcmp(mIncoming, "zero position ");

	for (int b=0; b<NUM_MOTORS; b++)
	{
		if (is_in_set(which, b) )
			Encoders[b].Count=0;
	}
	form_response( "ACK zero positioned" );	
	return is_speed_command;
}
示例#22
0
void send_speeds()
{
	char temp[20];
	sprintf(buff,"Speeds (cps): ");
	for (int m=0; m<NUM_MOTORS; m++)
	{
		if (FiveMotorConfigData.use_encoder[m]) {			
			sprintf(temp,"%c=%8ld, ", motor_letters[m], Encoders[m].Speed );
			strcat(buff, temp);
		} else {
			sprintf(temp,"%c=%8d ", motor_letters[m], PotSample[m] );
			strcat(buff, temp);
		}
	}
	form_response( buff );	
}
BOOL parse_measure_travel( char* mIncoming )
{
	char expression = "measure travel";
	int match = re_match( 2, Captures, expression, mIncoming );
	
	BOOL is_speed_command = strcmp(mIncoming, "measure travel");
	byte which = which_motors(mIncoming);
	for (int b=0; b<NUM_MOTORS; b++)
	{
		if (is_in_set(which, b) )
			set_motor_duty( b, HOMING_SPEED );		
	}
	// Don't wait until further limit switches triggered, just ack the cmd:
	form_response( "ACK measuring travel..." );
	return is_speed_command;
}
BOOL parse_limits_enable( char* mIncoming )
{
	char expression[] = "(enable|disable) limits";
	
	BOOL is_position_command = strcmp(mIncoming, "enable limits");
	is_position_command = strcmp(mIncoming, "disable limits");	
	byte which = which_motors(mIncoming);
	uint32_t pos;
	for (int b=0; b<NUM_MOTORS; b++)
	{
		if (is_in_set(which, b) )
			set_motor_position( b, pos );		
	}	
	form_response("ACK: limits enabled");
	return is_position_command;
}
void client(client_t* client_dat,int client_sock_fd){
    // The account of the active client
    account_t* client_account = NULL;

    // Set up parameters for select
    struct timeval timeout  ={ TIME_OUT, 0 }; // Timeout after 5000 seconds
    fd_set read_fd_set;
    FD_ZERO(&read_fd_set); 
    FD_SET(client_sock_fd, &read_fd_set);

    int active_sockets;

    request_t request;
    response_t response;
    
    while((active_sockets = select(client_sock_fd+1,&read_fd_set,NULL,NULL,
                    &timeout))>0){
        int byte_read = listen_request(&request,client_sock_fd);
        if(byte_read == 0){
            // Client closed connection unexpectedly. (not manually)
            if(client_account != NULL){
                set_not_in_session(client_account);
                client_account = NULL;
            }
            break;
        }
    
        if(client_account == NULL || !account_is_in_session(client_account)){
            // Current client did not open the 
            switch(request.code){
                case OPEN:
                   client_open(&response,client_dat,request.message.name);
                   break; 
                case START:
                    client_start(&response,client_dat,&client_account
                           ,request.message.name,client_sock_fd);            
                    break;
                case EXIT:
                    form_response(&response,ACCOUNT_EXIT,"Exit acknowledged.");
                    break;
                default:
                    form_response(&response,CLIENT_NOT_IN_SESSION,"Please "\
                            "start an account before doing the transaction.");
                    break;
            }
        }else{
            switch(request.code){
                case OPEN:
                    form_response(&response,CANNOT_OPEN,"Please finish this"\
                            " account session first.");
                    break;
                case START:
                    form_response(&response,CANNOT_START,"Please finish this"\
                            " account session first.");
                    break;
                case DEBIT:
                    client_debit(&response,client_account,request.message.amount);
                    break;
                case CREDIT:
                    client_credit(&response,client_account,request.message.amount);
                    break;
                case BALANCE:
                    form_response(&response,SUCCESS,"");
                    sprintf(response.message,"Account: %s.\nYour balance is %.2f.",
                            client_account->name,client_account->balance);
                    break;
                case FINISH:
                    set_not_in_session(client_account);
                    form_response(&response,SUCCESS,"Account session closed.");
                    client_account = NULL;
                    break;
                case EXIT:
                    set_not_in_session(client_account);
                    form_response(&response,ACCOUNT_EXIT,"Exit acknowledged and "\
                            "account session closed.");
                    client_account = NULL;
                    break;
                default: 
                    form_response(&response,SUCCESS,"Not a valid request.");
                    break;
            }

        }
        // Send the response to the client without failing!
        while(write(client_sock_fd,&response,sizeof(response_t))<0){
            continue;
        }
        if(request.code == EXIT){
            break;
        }
    }
    if(active_sockets == 0 ){
        if (client_account!=NULL){
            set_not_in_session(client_account);
            client_account = NULL;
        }
        form_response(&response, CONNECTION_TIME_OUT,"Connection time out.");
        write(client_sock_fd, &response,sizeof(response_t));
    }
    exit(EXIT_SUCCESS);
}
void client_credit(response_t* response, account_t* account, char* amount){
    char message[100];
    account_credit(account,atof(amount));
    sprintf(message,"Successfully credit $%s.",amount);
    form_response(response,SUCCESS,message);
}
示例#27
0
void send_robot_info()
{
	char temp[40];
	
	print_encoder_selections();
		
	
	strcpy(buff, "\r\nunit = " );	
	strcat(buff, unit_to_string() );
	strcat(buff, "\r\n");
	send_message( buff );

	sprintf(buff, "Base Frequency = " );	
	dtostrf( FiveMotorConfigData.base_frequency, 3, 1, temp );
	strcat(buff, temp );	
	strcat(buff, "\r\n");
	send_message( buff );

	sprintf(buff, "Wheel diameter= " );	
	dtostrf( FiveMotorConfigData.wheel_diameter, 3, 1, temp );
	strcat(buff, temp );	
	strcat(buff, unit_to_string() );
	strcat(buff, "\r\n");
	send_message( buff );
	
	sprintf(buff, "Wheel separation= " );	
	dtostrf( FiveMotorConfigData.wheel_separation, 3, 1, temp );
	strcat(buff, temp );	
	strcat(buff, unit_to_string() );
	strcat(buff, "\r\n");
	send_message( buff );
	
	sprintf(buff, "Wheel circumference= " );	
	// calc now b/c may have been stored under different unit.
	float circumf = FiveMotorConfigData.wheel_diameter * M_PI;
	dtostrf( circumf, 3, 1, temp );
	strcat(buff, temp );	
	strcat(buff, unit_to_string() );
	strcat(buff, "\r\n");
	send_message( buff );

	// List all unit to counts ratios:
	for (int m=0; m<5; m++)
	{
		sprintf(buff, "\t%c counts_per_unit=%ld %s\r\n", motor_letters[m], 
					FiveMotorConfigData.counts_per_unit[m], 
					unit_to_string() );
		send_message( buff );		
	}
	
	// List all motor status':
/*	sprintf(buff, "Motor Status':\r\n" );
	send_message( buff );	
	for (int m=0; m<5; m++)
	{
		sprintf(buff, "\t%c motor_status=%2x \r\n", motor_letters[m], 
					FiveMotorConfigData.motor_status[m]  );
		send_message( buff );		
	}*/

	// List Limit enable status:
	send_limit_states();

	// Uses the 'v' motor for the gearing.
	sprintf( buff, "Counts per rev= " );
	dtostrf( FiveMotorConfigData.wheel_counts_per_rev, 4, 1, temp );
	strcat ( buff,temp );
	strcat ( buff, "\r\n");	
	send_message( buff );

	print_pid_parameters();
	
	
	sprintf(buff, "\r\n");		// need a response to give the prompt again.
	form_response(buff);
}