コード例 #1
0
int main(int argc, char **argv)
{
    inquiry_info *ii = NULL;
    int max_rsp, num_rsp;
    int dev_id, sock, len, flags,loglines,socket_fd;
    int i;
    char addr[19] = { 0 };
    char name[248] = { 0 };
    char current_time[50];
    info data;
    pthread_t pid;
    pthread_attr_t attr;
    pthread_mutex_t mutex;
    
    pthread_create(&pid,NULL,relay2server,NULL);
    InitQueue(& infoQ);
    sleep(2);

        char *server_addr = getenv("SERVER_ADDRESS");
        if(!server_addr)
        {
           printf("no remote server address,exit thread");
        }
   socketopen(0,&socket_fd,server_addr);
    dev_id = hci_get_route(NULL);
    sock = hci_open_dev( dev_id );
    if (dev_id < 0 || sock < 0) {
        perror("opening socket");
        exit(1);
    }

   // len  = 7;
    max_rsp = 255;
    loglines = 0;
    flags = IREQ_CACHE_FLUSH;
    ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));

        time_t now;
        struct tm *timenow;

#ifdef LOGFILE_ON
	config(&maxLoglines,&len,&logname);
        FILE *logFile;
        logFile=fopen(logname,"w");
#endif
 
                
    while (1){
        time(&now);
        timenow = localtime(&now);
        sprintf(current_time, "%d%02d%02d%02d%02d%02d",
            timenow->tm_year+1900, timenow->tm_mon+1, timenow->tm_mday, timenow->tm_hour, timenow->tm_min, timenow->tm_sec);

#ifdef TERMINAL_ON      
        printf("inquiry starts: %s",asctime(timenow));
#endif
#ifdef LOGFILE_ON       
        fprintf(logFile,"inquiry starts: %s",asctime(timenow));
#endif
        num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
        if( num_rsp < 0 ) perror("hci_inquiry");
        for (i = 0; i < num_rsp; i++) {
                ba2str(&(ii+i)->bdaddr, addr);       
        	  memcpy(data.addr,addr,sizeof(addr));
		  //memcpy(data.time,current_time,sizeof(current_time));
		  memcpy(data.time,asctime(timenow),sizeof(data.time));
		  int res = EnQueue(infoQ, data);
		  if(!res)
		  {
			printf("The infoQueue is full \n");
		   }			
	
#ifdef TERMINAL_ON      
                        printf("%s  %s\n", addr, name);
#endif
#ifdef LOGFILE_ON      
			loglines++; 
                        fprintf(logFile, "%s  %s\n", addr, name);
	
#endif                  
            }

#ifdef TERMINAL_ON      
                        printf("%d devices found.\n",num_rsp);
                        printf("---------------------------------------\n");
                        fflush(stdout);
#endif
#ifdef LOGFILE_ON       
			   loglines++;
                        fprintf(logFile,"%d devices found.\n",num_rsp);
                        fprintf(logFile,"---------------------------------------\n");
                        fflush(logFile);
			logHandler(&loglines,current_time,&logFile);
			
#endif
                }
    
#ifdef LOGFILE_ON       
                        fclose(logFile);
#endif
    free( ii );
    close( sock );
    return 0;
}
コード例 #2
0
ファイル: testnet.c プロジェクト: Distrotech/gawk
int
devopen(const char *name, const char *mode)
{
	int openfd;
	char *cp;
	char *ptr;
	int flag = 0;
	int len;
	int family;
	int protocol;
	char *hostname;
	char *hostnameslastcharp;
	char *localpname;
	char *localpnamelastcharp;

	flag = str2mode(mode);
	openfd = INVALID_HANDLE;

	/* /inet/protocol/localport/hostname/remoteport */
	len = 6;

	cp = (char *) name + len;
	/* which protocol? */
	if (strncmp(cp, "tcp/", 4) == 0)
		protocol = SOCK_STREAM;
	else if (strncmp(cp, "udp/", 4) == 0)
		protocol = SOCK_DGRAM;
	else {
		protocol = SOCK_STREAM;	/* shut up the compiler */
		fprintf(stderr, _("no (known) protocol supplied in special filename `%s'"), name);
		exit(1);
	}
	cp += 4;

	/* which localport? */
	localpname = cp;
	while (*cp != '/' && *cp != '\0')
		cp++;
	/*                    
	 * Require a port, let them explicitly put 0 if
	 * they don't care.  
	 */
	if (*cp != '/' || cp == localpname) {
		fprintf(stderr, _("special file name `%s' is incomplete"), name);
		exit(1);
	}

	/*
	 * We change the special file name temporarily because we
	 * need a 0-terminated string here for conversion with atoi().
	 * By using atoi() the use of decimal numbers is enforced.
	 */
	*cp = '\0';
	localpnamelastcharp = cp;

	/* which hostname? */
	cp++;
	hostname = cp;
	while (*cp != '/' && *cp != '\0')
		cp++; 
	if (*cp != '/' || cp == hostname) {
		*localpnamelastcharp = '/';
		fprintf(stderr, _("must supply a remote hostname to `/inet'"));
		exit(1);
	}
	*cp = '\0';
	hostnameslastcharp = cp;

	/* which remoteport? */
	cp++;
	/*
	 * The remote port ends the special file name.
	 * This means there already is a '\0' at the end of the string.
	 * Therefore no need to patch any string ending.
	 *
	 * Here too, require a port, let them explicitly put 0 if
	 * they don't care.
	 */
	if (*cp == '\0') {
		*localpnamelastcharp = '/';
		*hostnameslastcharp = '/';
		fprintf(stderr, _("must supply a remote port to `/inet'"));
		exit(1);
	}

	{
#define DEFAULT_RETRIES 20
	static unsigned long def_retries = DEFAULT_RETRIES;
	static bool first_time = true;
	unsigned long retries = 0;
	static long msleep = 1000;

	if (first_time) {
		char *cp, *end;
		unsigned long count = 0;
		char *ms2;
		
		first_time = false;
		if ((cp = getenv("GAWK_SOCK_RETRIES")) != NULL) {
			count = strtoul(cp, & end, 10);
			if (end != cp && count > 0)
				def_retries = count;
		}

		/*
		 * Env var is in milliseconds, paramter to usleep()
		 * is microseconds, make the conversion. Default is
		 * 1 millisecond.
		 */
		if ((ms2 = getenv("GAWK_MSEC_SLEEP")) != NULL) {
			msleep = strtol(ms2, & end, 10);
			if (end == ms2 || msleep < 0)
				msleep = 1000;
			else
				msleep *= 1000;
		}
	}
	retries = def_retries;

	do {
		openfd = socketopen(family, protocol, localpname, cp, hostname);
		retries--;
	} while (openfd == INVALID_HANDLE && retries > 0 && usleep(msleep) == 0);
	}

	*localpnamelastcharp = '/';
	*hostnameslastcharp = '/';

	return openfd;
}