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

📄 btserver.c

📁 linux下bluetooth后台服务程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include "btdefines.h"#include "btutils.h"#include "btttmp.h"#include "bthandler.h"#include "btserver.h"//PLEASECHECK: following include is needed for the hack down there, remove when fixed#include "TTMPConn.h"//this is returned by a TTMP macro if communication error occurs (undefine == return;)#undef TTMP_ERROR#define TTMP_ERRORstatic int serverbusy = 1;static char serverbuf[BTSR_BUFFER_SIZE];static TTMPConnection ttmpcon[BT_MAX_CLIENTS];static int ttmppend[BT_MAX_CLIENTS];static BTConnection btcon[BT_MAX_HANDLES];static int refcon[BT_MAX_HANDLES][BT_MAX_CLIENTS];/* Assume pairing is disabled by default */static int pairingenabled = 0;/* PLEASECHECK: according to Johan, there is no need to disable/enable pagescan anymore */#undef HANDLE_PAGESCAN#ifdef HANDLE_PAGESCANstatic int pagescanenabled = 0;/* PLEASECHECK:  This should be removed when the kernel part is fixed */static int pagescanenablepid = -1;static int pagescandisablepid = -1;#endif /* HANDLE_PAGESCAN */#ifdef ENABLE_DEBUG_LOG#define STATE2STR(state) state2str(state)#define ERROR2STR(error) error2str(error)#include <stdio.h>#include <signal.h>#include <errno.h>#include <ucontext.h>static void sighandler_usr2(){	int i, j, res;	printf("****************** btserver state begin ************************\n");	res = 0;	for (i = 0; i < BT_MAX_CLIENTS; i++)		if (ttmpcon[i])			res++;	printf("The number of active clients: %d\n", res);	res = 0;	for (i = 0; i < BT_MAX_HANDLES; i++)		if (btcon[i])			res++;	printf("The number of active handles: %d\n", res);	printf("Reference map:\n");	printf("Handles/Clients ");	for (i = 0; i < BT_MAX_CLIENTS; i++)		printf("%2d ", i);	printf("State");	printf("\n");	for (i = 0; i < BT_MAX_HANDLES; i++) {		printf("    %2d          ", i);		for (j = 0; j < BT_MAX_CLIENTS; j++)			printf("%2d ", refcon[i][j]);		if (btcon[i])			printf("%s\n", STATE2STR(btcon[i]->state));		else			printf("-\n");	}	printf("\n");	printf("Pairing is %s.\n", pairingenabled?"enabled":"disabled");#ifdef HANDLE_PAGESCAN	printf("Page scan is %s.\n", pagescanenabled?"enabled":"disabled");#endif /* HANDLE_PAGESCAN */	printf("******************* btserver state end *************************\n");}#else //ENABLE_DEBUG_LOG#define STATE2STR(state) "DEBUG DISABLED"#define ERROR2STR(error) "DEBUG DISABLED"#endif //ENABLE_DEBUG_LOGinline int refhandle(int handle){	int i, res = 0;	for (i = 0; i < BT_MAX_CLIENTS; i++)		res += refcon[handle][i];	return res;}void requesthandler(int ittmpcon, TTMPMessage msg){	int i, cmd, handle, res, cmdid;	TTMP_GETFIELDINT(msg, CMD_ID, &cmdid);	TTMP_GETFIELDINT(msg, CMD_PAR_COMMAND, &cmd);	switch(cmd)	{	case CMD_INIT:	{		char *bdaddr, *pin;		int chan, prof, opt;		TTMP_DETACHFIELDSTR(msg, CMD_PAR_ADDRESS, &bdaddr);		TTMP_GETFIELDINT(msg, CMD_PAR_CHANNEL, &chan);		TTMP_GETFIELDINT(msg, CMD_PAR_PROFILE, &prof);		TTMP_GETFIELDINT(msg, CMD_PAR_OPTION, &opt);		TTMP_DETACHFIELDSTR(msg, CMD_PAR_PIN, &pin);		if (!bdaddr || !pin) {			DEBUG_LOG("Failed to retrive bdaddr or pin from TTMP");			handle = -1;			goto init_failed;		}		for (i = 0; i < BT_MAX_HANDLES; i++) {			if (!btcon[i])				continue;			if (BTHandlerCompare(btcon[i], bdaddr, chan, prof, &res) != BT_ERR_NONE)				continue;			if (res == BT_TRUE)				break;		}		if (i < BT_MAX_HANDLES)			handle = i;		else {			for (i = 0; i < BT_MAX_HANDLES; i++)				if (!btcon[i])					break;			if (i == BT_MAX_HANDLES)				handle = -1;			else if (BTHandlerCreate(&btcon[i], bdaddr, chan, prof, opt, pin) != BT_ERR_NONE)				handle = -1;			else				handle = i;		}		if (handle != -1)			refcon[handle][ittmpcon]++;init_failed:		ttmppend[ittmpcon]++;		TTMP_CLEAR(msg);		TTMP_SETFIELDINT(msg, CMD_ID, cmdid);		TTMP_SETFIELDINT(msg, CMD_PAR_HANDLE, handle);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		if (handle != -1) {			DEBUG_LOG3("CMD_INIT: returned client=%d, handle=%d, refcount=%d", ittmpcon, handle, refhandle(handle));		} else {			DEBUG_LOG2("CMD_INIT: returned client=%d, handle=%d", ittmpcon, handle);		}		break;	}	case CMD_CLOSE:	{		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG3("CMD_CLOSE: client=%d, handle=%d, refcount=%d", ittmpcon, handle, refhandle(handle));				refcon[handle][ittmpcon]--;				if (!refhandle(handle))					res = BTHandlerClose(btcon[handle]);				else					res = BT_ERR_NONE;			}			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_RELEASE:	{		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG2("CMD_RELEASE: client=%d, handle=%d", ittmpcon, handle);				res = BTHandlerRelease(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_SEND:	{		int len, sent;		char* buf;		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		TTMP_GETFIELDSIZE(msg, CMD_PAR_DATA, &len);		TTMP_DETACHFIELDBIN(msg, CMD_PAR_DATA, &buf);		if (!buf) {			DEBUG_LOG("Failed to retrive buf from TTMP");			res = BT_ERR_FAILED;			goto send_failed;		}		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {//				DEBUG_LOG3("CMD_SEND: client=%d, handle=%d, len=%d", ittmpcon, handle, len);				res = BTHandlerSend(btcon[handle], buf, len, &sent);			}			else				res = BT_ERR_ARGUMENT;		}		else			res = BT_ERR_ARGUMENT;		if (buf)			free(buf);send_failed:		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_RECEIVE:	{		int len, received;		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		TTMP_GETFIELDINT(msg, CMD_PAR_SIZE, &len);		if (len > BTSR_BUFFER_SIZE)			len = BTSR_BUFFER_SIZE;		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {//				DEBUG_LOG3("CMD_RECEIVE: client=%d, handle=%d, len=%d", ittmpcon, handle, len);				res = BTHandlerReceive(btcon[handle], serverbuf, len, &received);			}			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);		if (res != BT_ERR_NONE)			received = 0;		TTMP_SETFIELDINT(msg, CMD_PAR_SIZE, received);		if (received)			TTMP_SETFIELDBIN(msg, CMD_PAR_DATA, serverbuf, received);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		break;	}	case CMD_GETSTATE:	{		int state;		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {//				DEBUG_LOG2("CMD_GETSTATE: client=%d, handle=%d", ittmpcon, handle);				if (BTHandlerGetState(btcon[handle], &state) != BT_ERR_NONE)					state = BT_STATE_UNINITIALIZED;			}			else				state = BT_STATE_UNINITIALIZED;		}		else			state = BT_STATE_UNINITIALIZED;		ttmppend[ittmpcon]++;		TTMP_CLEAR(msg);		TTMP_SETFIELDINT(msg, CMD_ID, cmdid);		TTMP_SETFIELDINT(msg, CMD_PAR_RESULT, state);		TTMP_SEND(ttmpcon[ittmpcon], msg);		ttmppend[ittmpcon]--;		break;	}	case CMD_GETERROR:	{		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG2("CMD_ERROR: client=%d, handle=%d", ittmpcon, handle);				if (BTHandlerGetError(btcon[handle], &res) != BT_ERR_NONE)					res = BT_ERR_ARGUMENT;			}			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_STARTUSINGCHANNEL:	{		char* buf;		TTMP_GETFIELDINT(msg, CMD_PAR_HANDLE, &handle);		TTMP_DETACHFIELDSTR(msg, CMD_PAR_DATA, &buf);		if (!buf) {			DEBUG_LOG("Failed to retrive buf from TTMP");			res = BT_ERR_FAILED;			goto startusing_failed;		}		if (handle < BT_MAX_HANDLES && handle > -1) {			if (btcon[handle]) {				DEBUG_LOG2("CMD_STARTUSINGCHANNEL: client=%d, handle=%d", ittmpcon, handle);				res = BTHandlerStartApp(btcon[handle], buf);			}			else				res = BT_ERR_ARGUMENT;		}		else			res = BT_ERR_ARGUMENT;		if (buf)			free(buf);startusing_failed:		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_STOPUSINGCHANNEL:	{

⌨️ 快捷键说明

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