static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
    int status;
    uint8_t battery_level;

    switch(state){
        case TC_W4_SERVICE_RESULT:
            switch(hci_event_packet_get_type(packet)){
                case GATT_EVENT_SERVICE_QUERY_RESULT:
                    gatt_event_service_query_result_get_service(packet, &battery_service);
                    dump_service(&battery_service);
                    break;
                case GATT_EVENT_QUERY_COMPLETE:
                    if (packet[4] != 0){
                        printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]);
                        add_to_blacklist(report.address);
                        gap_disconnect(connection_handle);
                        break;  
                    } 
                    state = TC_W4_CHARACTERISTIC_RESULT;
                    printf("\nSearch for battery level characteristic.\n");
                    gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &battery_service, battery_level_characteristic_uuid);
                    break;
                default:
                    break;
            }
            break;
            
        case TC_W4_CHARACTERISTIC_RESULT:
            switch(hci_event_packet_get_type(packet)){
                case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
                    gatt_event_characteristic_query_result_get_characteristic(packet, &config_characteristic);
                    dump_characteristic(&config_characteristic);
                    break;
                case GATT_EVENT_QUERY_COMPLETE:
                    if (packet[4] != 0){
                        printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]);
                        add_to_blacklist(report.address);
                        gap_disconnect(connection_handle);
                        break;  
                    } 
                    state = TC_W4_BATTERY_DATA;
                    printf("\nConfigure battery level characteristic for notify.\n");
                    status = gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &config_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
                    if (status != 0){
                        printf("\nNotification not supported. Query value of characteristic.\n");
                        gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &config_characteristic);
                    }
                    
                    break;
                default:
                    break;
            }
            break;
        case TC_W4_BATTERY_DATA:
            switch(hci_event_packet_get_type(packet)){
                case GATT_EVENT_NOTIFICATION:
                    printf("\nBattery Data:\n");
                    dump_characteristic_value(&packet[8], little_endian_read_16(packet, 6));
                    break;
                case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
                        
                        if (gatt_event_characteristic_value_query_result_get_value_length(packet) < 1) break;
                        battery_level = gatt_event_characteristic_value_query_result_get_value(packet)[0];
                        printf("Battry level %d \n", battery_level);
                    break;

                case GATT_EVENT_QUERY_COMPLETE:
                    if (packet[4] != 0){
                        printf("CHARACTERISTIC_VALUE_QUERY_RESULT - Error status %x.\n", packet[4]);
                        break;  
                    }
                    // Use timer if repeated request is needed and notification is not supperted
                    gap_disconnect(connection_handle);
                    break;
                default:
                    printf("Unknown packet type %x\n", hci_event_packet_get_type(packet));
                    break;
            }
            break;

        default:
            printf("error\n");
            break;
    }
    
}
Exemple #2
0
/*thread function which's job is to carry out the order from the user*/
void *handle_main()
{
	system("clear");
	printf("Welcome to The Chatting program!User name:%s\n1.query the online users\n2.send message to someone\n3.send message to everyone\n4.View messages\n5.add someone to blacklist\n6.remove someone from blacklist\n7.View blacklist!\n(c)cls,(#)exit\n===================================================\n",usrname);
				
	while(true)
	{
		gets(buffer);
		if(strlen(buffer)!=1)
		{
			printf("input error!\n");
		}
		else if(buffer[0]=='1'){ /*query the online users*/
			send_msg.service = 0X02;
			int len=YY_MSG_HEADER_LENGTH;
			stcp_client_send(sockfd,&len,sizeof(int));
			stcp_client_send(sockfd,&send_msg,len);			
			pthread_mutex_lock(&wait_mutex);	
			while(wait!=0x13)
				pthread_cond_wait(&wait_cond,&wait_mutex);
			wait=0x00;
			pthread_mutex_unlock(&wait_mutex);
			unsigned char online_users = (unsigned char)rec_msg.msg_content[0];
			char *users=rec_msg.msg_content+1;
			int i;
			printf("Online users:\n");
			for(i=0;i<online_users;i++)
				printf("User name: %s\n",users+i*20);
		}
		else if(buffer[0]=='2') 
		{	/*send messages to someone!*/
			bool success=false;
			send_msg.service = 0x03;
			printf("Please enter the receive user's name:");
			while(!success)
			{
				gets(buffer);
				if(strlen(buffer)>20)
				{
					printf("The user name inputed is too long!input again:\n");
					continue;
				}
				else
				{
					bool illegal=false;
					int i=0;
					for(i;i<strlen(buffer);i++)
					{
						if(!(('a'<=buffer[i]&& 'z'>=buffer[i]) ||('A'<=buffer[i] && 'Z' >=buffer[i])||('0'<=buffer[i] && '9'>=buffer[i])) )
						{
							printf("The input is illegal!input again:\n");
							illegal=true;
							break;
						}
					}
					if(illegal)
						continue;
				}
				strcpy(send_msg.rec_usr,buffer);
				/*query whether the user is online*/
				int len=YY_MSG_HEADER_LENGTH;
				stcp_client_send(sockfd,&len,sizeof(int));
				stcp_client_send(sockfd,&send_msg,len);
				pthread_mutex_lock(&wait_mutex);
				while(wait!=0x14 && wait!=0x15)
				pthread_cond_wait(&wait_cond,&wait_mutex);
				wait=0x00;
				pthread_mutex_unlock(&wait_mutex);
				if(rec_msg.service == 0x15){
					success=true;
					break;
				}
				else{
					printf("The user is not exist or not online!\n");
					break;
				}
			}
			if(success){
				printf("Iuput the message content:\n");
				gets(send_msg.msg_content);
				send_msg.service = 0x04;
				int len=YY_MSG_HEADER_LENGTH+strlen(send_msg.msg_content);
				stcp_client_send(sockfd,&len,sizeof(int));
				stcp_client_send(sockfd,&send_msg,len);
				printf("The message has been sent out!\n");
			}
		}
		else if(buffer[0]=='3')		
		{	/*send messages to everyone!*/
			send_msg.service = 0x05;
			printf("Iuput the message content:\n");
			gets(send_msg.msg_content);
			strcpy(send_msg.rec_usr,"everyone");
			int len = YY_MSG_HEADER_LENGTH+strlen(send_msg.msg_content);
			stcp_client_send(sockfd,&len,sizeof(int));
			stcp_client_send(sockfd,&send_msg,len);
			printf("The message has been sent out!\n");
		}
		else if(buffer[0]=='4')
		{	/*View the history messages!*/
			if(msg_num==0)
				printf("No history messages!\n");
			else {
				printf("The history messages :\n");
				int i;
				for(i=0;i<msg_num;i++)
				{
					struct YY_MSG *msg=&history_msgs[(i+first_msg)%MAX_MSG_NUM];
					printf("%s(to %s):%s\n",msg->send_usr,msg->rec_usr,msg->msg_content);
				}
			}
		}
		else if(buffer[0]=='5')
		{	/*add someone to blacklist*/
			bool success=false;
			send_msg.service = 0x03;
			printf("Please input the user's name:");
			while(!success)
			{
				gets(buffer);
				if(strlen(buffer)>20)
				{
					printf("The user name inputed is too long!input again:\n");
					continue;
				}
				else
				{
					bool illegal=false;
					int i=0;
					for(i;i<strlen(buffer);i++)
					{
						if(!(('a'<=buffer[i]&& 'z'>=buffer[i]) ||('A'<=buffer[i] && 'Z' >=buffer[i])||('0'<=buffer[i] && '9'>=buffer[i])) )
						{
							printf("The input is illegal!input again:\n");
							illegal=true;
							continue;
						}
					}
					if(illegal)
						continue;
				}
				strcpy(send_msg.rec_usr,buffer);
				int len = YY_MSG_HEADER_LENGTH;
				stcp_client_send(sockfd,&len,sizeof(int));
				stcp_client_send(sockfd,&send_msg,len);
				pthread_mutex_lock(&wait_mutex);
				while(wait!=0x14 && wait!=0x15)
				pthread_cond_wait(&wait_cond,&wait_mutex);
				wait=0x00;
				pthread_mutex_unlock(&wait_mutex);
				if(rec_msg.service == 0x15){
					success=true;
					break;
				}
				else{
					printf("The user is not exist or not online!\n");
					break;
				}
			}
			add_to_blacklist();
		}
		else if(buffer[0]=='6'){
			/*remove someone from blacklist*/
			printf("Please input the user's name:");
			gets(buffer);
			delete_from_blacklist();
		}
		else if(buffer[0]=='7')
		{
			/*View blacklist*/
			if(blacklistnum==0) printf("NO ONE!\n");
			else {
				printf("blacklist:\n");
				int i=0;
				for(i;i<blacklistnum;i++)
					printf("%s",blacklist[i]);
			}
		}
		else if(buffer[0]=='#'){
			exit(0);
		}
		else if(buffer[0]=='c'){
			system("clear");
			printf("Welcome to The Chatting program!User name:%s\n1.query the online users\n2.send message to someone\n3.send message to everyone\n4.View messages\n5.add someone to blacklist\n6.remove someone from blacklist\n7.View blacklist!\n(c)cls,(#)exit\n===================================================\n",usrname);
		}
		else printf("input error!\n");
	}
	pthread_exit(NULL);
}