⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btserver.c

📁 linux下bluetooth后台服务程序
💻 C
📖 第 1 页 / 共 2 页
字号:
		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG2("CMD_STOPUSINGCHANNEL: client=%d, handle=%d", ittmpcon, handle);				res = BTHandlerStopApp(btcon[handle]);			}			else				res = BT_ERR_ARGUMENT;		}		else			res = BT_ERR_ARGUMENT;		ttmppend[ittmpcon]++;		TTMP_CLEAR(msg);		TTMP_SETFIELDINT(msg, CMD_ID, cmdid);		TTMP_SETFIELDINT(msg, CMD_PAR_RESULT, res);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		break;	}	case CMD_GETFLAGS:	{		int flags;		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG2("CMD_GETFLAGS: client=%d, handle=%d", ittmpcon, handle);				if (BTHandlerGetFlags(btcon[handle], &flags) != BT_ERR_NONE)					flags = BT_FLAG_UNDEFINED;			}			else				flags = BT_FLAG_UNDEFINED;		}		else			flags = BT_FLAG_UNDEFINED;		ttmppend[ittmpcon]++;		TTMP_CLEAR(msg);		TTMP_SETFIELDINT(msg, CMD_ID, cmdid);		TTMP_SETFIELDINT(msg, CMD_PAR_RESULT, flags);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		break;	}	case CMD_SETFLAGS:	{		int flags;		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		TTMP_GETFIELDINT(msg, CMD_PAR_OPTION, &flags);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG2("CMD_SETFLAGS: client=%d, handle=%d", ittmpcon, handle);				res = BTHandlerSetFlags(btcon[handle], flags);			}			else				res = BT_ERR_ARGUMENT;		}		else			res = BT_ERR_ARGUMENT;		ttmppend[ittmpcon]++;		TTMP_CLEAR(msg);		TTMP_SETFIELDINT(msg, CMD_ID, cmdid);		TTMP_SETFIELDINT(msg, CMD_PAR_RESULT, res);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		break;	}	case CMD_ISPROFILEACTIVE:	{		int i, profile, active;		TTMP_GETFIELDINT(msg, CMD_PAR_PROFILE, &profile);		DEBUG_LOG2("CMD_ISPROFILEACTIVE: client=%d, handle=%d", ittmpcon, handle);		active = BT_FALSE;		for (i = 0; i < BT_MAX_HANDLES; i++)			if (btcon[i]) {				if (BTHandlerIsProfileActive(btcon[i], profile, &active) == BT_ERR_NONE) {					if (active == BT_TRUE)						break;				} else {					active = BT_FALSE;					break;				}			}		ttmppend[ittmpcon]++;		TTMP_CLEAR(msg);		TTMP_SETFIELDINT(msg, CMD_ID, cmdid);		TTMP_SETFIELDINT(msg, CMD_PAR_RESULT, active);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		break;	}	default:		break;	}}int main(int argc, char** argv){	TTMPServer svr;	TTMPConnection newttmpcon;	TTMPMessage msg;	int i, j, res, state;#ifdef ENABLE_DEBUG_LOG	struct sigaction sigact;#endif //ENABLE_DEBUG_LOG	if (argc > 1) {		printf("Use: %s  (creates socket at [%s])\n", argv[0], SERVER_SOCKET_ADDR);		exit(1);	}	signal(SIGPIPE, SIG_IGN);	unlink(SERVER_SOCKET_ADDR);	res = TTMPServerCreate(&svr, 0, BTSR_VER_MAJOR, BTSR_VER_MINOR);	if (res < TTMP_ERR_NONE) {		DEBUG_LOG1("Unable to create server, error %d", res);		exit(1);	}	res = TTMPServerListen(svr, SERVER_SOCKET_ADDR);	if (res < TTMP_ERR_NONE) {		DEBUG_LOG1("Unable to listen, error %d", res);		TTMPServerDestroy(svr);		exit(1);	}	for (i = 0; i < BT_MAX_CLIENTS; i++) {		ttmpcon[i] = NULL;		ttmppend[i] = 0;	}	for (i = 0; i < BT_MAX_HANDLES; i++) {		btcon[i] = NULL;		for (j = 0; j < BT_MAX_CLIENTS; j++)			refcon[i][j] = 0;	}#ifdef ENABLE_DEBUG_LOG	/* Setup signal handler, used to dump the state on USR1 */	sigact.sa_flags = SA_SIGINFO;	sigact.sa_sigaction = (void *)sighandler_usr2;	if(sigaction (SIGUSR2, &sigact, (struct sigaction *)NULL))		DEBUG_LOG("could not register a signal handler");#endif //ENABLE_DEBUG_LOG	while(serverbusy) {		res = TTMPServerAccept(svr, &newttmpcon);		if (res < TTMP_ERR_NONE) {			if (res != TTMP_ERR_TIMEOUT)				DEBUG_LOG("Incompatible client.");		} else {			//allocate new connection			for( i = 0; i < BT_MAX_CLIENTS; i++)				if (!ttmpcon[i])					break;			if (i == BT_MAX_CLIENTS) {				DEBUG_LOG("Max connections reached");				TTMPConnectionDestroy(newttmpcon);			} else {				DEBUG_LOG1("New client has been added with id %d", i);				ttmpcon[i] = newttmpcon;			}		}		//PLEASECHECK: till ReceiveMulti() is implemented, this		//hack is required :)		//here should be a call to ReceiveMulti(), remove when fixed		//BEGIN HACK (still better than usleep())		{			fd_set fdset;			struct timeval tv;			int maxsock;						FD_ZERO(&fdset);			maxsock = 0;			for (i = 0; i < BT_MAX_CLIENTS; i++) {				if (!ttmpcon[i])					continue;				FD_SET(ttmpcon[i]->socket, &fdset);				if (ttmpcon[i]->socket > maxsock)					maxsock = ttmpcon[i]->socket;			}			tv.tv_sec = 0;			tv.tv_usec = BTSR_RECEIVE_TIMEOUT;			//sleep till something happens on a channel or			//timeout expires			select(maxsock + 1, &fdset, NULL, NULL, &tv);		}		for (i = 0; i < BT_MAX_CLIENTS; i++) {			if (!ttmpcon[i])				continue;			//check for messages			res = TTMPConnectionReceive(ttmpcon[i], &msg);			if (res == TTMP_ERR_TIMEOUT)				continue;			if (res < TTMP_ERR_NONE) {				DEBUG_LOG1("client has been removed with id %d", i);				for (j = 0; j < BT_MAX_HANDLES; j++)					if (refcon[j][i])						refcon[j][i]--;				TTMPConnectionDestroy(ttmpcon[i]);				ttmpcon[i] = NULL;				continue;			}			//handle the received message			requesthandler(i, msg);			TTMPMessageDestroy(msg);		}		//END HACK		/* check if communication to clients was OK */		for (i = 0; i < BT_MAX_CLIENTS; i++)			if (ttmppend[i]) {				DEBUG_LOG1("error happend during communication with a client id %d, removing the client", i);				for (j = 0; j < BT_MAX_HANDLES; j++)					if (refcon[j][i])						refcon[j][i]--;				TTMPConnectionDestroy(ttmpcon[i]);				ttmpcon[i] = NULL;				ttmppend[i] = 0;			}		/* cleanup unused handles */		for (i = 0; i < BT_MAX_HANDLES; i++) {			if (!btcon[i])				continue;			res = refhandle(i);			if (!res) {				res = BTHandlerGetState(btcon[i], &state);				if (res != BT_ERR_NONE || state == BT_STATE_UNINITIALIZED) {					DEBUG_LOG1("all clients left for the handle %d. Destroying...", i);					BTHandlerDestroy(btcon[i]);					btcon[i] = NULL;				} else if (state != BT_STATE_CLOSING) {					DEBUG_LOG1("all clients left for the handle %d. Closing...", i);					BTHandlerClose(btcon[i]);				}			}		}#ifdef HANDLE_PAGESCAN		/* try enable pagescan */		/* PLEASECHECK: All pid stuff should be removed when pagescan enabling/disabling can be non-blocking */		if (pagescandisablepid > 0)			if (waitpid(pagescandisablepid, &res, WNOHANG))				pagescandisablepid = -1;		if (!pagescanenabled && pagescandisablepid < 0) {			for (i = 0; i < BT_MAX_HANDLES; i++)				if (btcon[i])					if (BTHandlerGetState(btcon[i], &res) == BT_ERR_NONE)						if (res == BT_STATE_START_LISTENING || res == BT_STATE_LISTENING)							break;			if (i != BT_MAX_HANDLES) {				DEBUG_LOG("enabling pagescan.");				pagescanenablepid = fork();				if (pagescanenablepid == 0) {					enablepagescan();					exit(0);				} else if (pagescanenablepid < 0) {					DEBUG_LOG("enabling pagescan failed.");				} else {					pagescanenabled = 1;				}			}		}#endif /* HANDLE_PAGESCAN */		/* try enable pairing */		if (!pairingenabled) {			for (i = 0; i < BT_MAX_HANDLES; i++)				if (btcon[i])					if (BTHandlerNeedPair(btcon[i], &res) == BT_ERR_NONE)						if (res)							break;			if (i != BT_MAX_HANDLES) {				DEBUG_LOG("enabling pairing.");				BTSendSig(BT_HCID_NAME, BT_HCID_PAIR_ENABLE);				pairingenabled = 1;			}		}		/* run the active connections */		for (i = 0; i < BT_MAX_HANDLES; i++)			if (btcon[i])				BTHandlerRun(btcon[i]);		/* try disable pairing */		if (pairingenabled) {			for (i = 0; i < BT_MAX_HANDLES; i++)				if (btcon[i])					if (BTHandlerNeedPair(btcon[i], &res) == BT_ERR_NONE)						if (res)							break;			if (i == BT_MAX_HANDLES) {				DEBUG_LOG("disabling pairing.");				BTSendSig(BT_HCID_NAME, BT_HCID_PAIR_DISABLE);				pairingenabled = 0;			}		}#ifdef HANDLE_PAGESCAN		/* try disable page scan */		/* PLEASECHECK: All pid stuff should be removed when pagescan enabling/disabling can be non-blocking */		if (pagescanenablepid > 0)			if (waitpid(pagescanenablepid, &res, WNOHANG))				pagescanenablepid = -1;		if (pagescanenabled && pagescanenablepid < 0) {			for (i = 0; i < BT_MAX_HANDLES; i++)				if (btcon[i])					if (BTHandlerGetState(btcon[i], &res) == BT_ERR_NONE)						if (res == BT_STATE_START_LISTENING || res == BT_STATE_LISTENING)							break;			if (i == BT_MAX_HANDLES) {				DEBUG_LOG("disabling pagescan.");				pagescandisablepid = fork();				if (pagescandisablepid == 0) {					disablepagescan();					exit(0);				} else if (pagescandisablepid < 0) {					DEBUG_LOG("disabling pagescan failed.");				} else {					pagescanenabled = 0;				}			}		}#endif /* HANDLE_PAGESCAN */	}	/* Close all the bluetooth connections */	for (i = 0; i < BT_MAX_HANDLES; i++)		if (btcon[i])			BTHandlerClose(btcon[i]);	/* run the active connections */	for (i = 0; i < BT_MAX_HANDLES; i++)		if (btcon[i]) {			res = BTHandlerGetState(btcon[i], &state);			while (state != BT_STATE_UNINITIALIZED) {				BTHandlerRun(btcon[i]);				usleep(BTSR_RECEIVE_TIMEOUT);			}		}	/* destroy all the bluetooth connections */	for (i = 0; i < BT_MAX_HANDLES; i++)		if (btcon[i])			BTHandlerDestroy(btcon[i]);	/* destroy all the client connections */	for (i = 0; i < BT_MAX_CLIENTS; i++)		if (ttmpcon[i])			TTMPConnectionDestroy(ttmpcon[i]);	TTMPConnectionDestroy(newttmpcon);	TTMPServerDestroy(svr);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -