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

📄 ftps_handler.c

📁 CSR蓝牙芯片的ftp程序的服务器端代码 客户端已经上传
💻 C
📖 第 1 页 / 共 2 页
字号:
{
	GoepRemotePutResponse(state->handle, goep_svr_resp_BadRequest);
	FtpsPacketComplete(state);
}

/* GOEP Library Message Handlers */
static void handleGoepInitCfm(ftpsState *state, GOEP_INIT_CFM_T *msg)
{
	PRINT(("handleGoepConnectInd\n"));
	
	if (msg->status == goep_success)
	{
        state->handle=msg->session;
        state->currCom= ftps_com_None;
		
		GoepGetChannel(state->handle);
	}
	else
	{
		ftpsMsgSendInitCfm(NULL, ftps_failure);
		free(state);
	}
}

static void handleGoepChannelInd(ftpsState *state, GOEP_CHANNEL_IND_T *msg)
{
	PRINT(("handleGoepChannelInd\n"));
	
	if (msg->status == goep_success)
	{
		uint8* sdp;
				
		state->rfcChan = msg->channel;
		
		sdp = (uint8 *)PanicUnlessMalloc(sizeof(serviceRecordFTP));
		memcpy(sdp, serviceRecordFTP, sizeof(serviceRecordFTP));

		if (!insertRfcommServerChannel(sdp, sdp + sizeof(serviceRecordFTP), state->rfcChan))
		{
			ftpsMsgSendInitCfm(NULL, ftps_failure);
			free(sdp);
			free(state);
		}
		else
		{
			/* Send the service record to the connection lib to be registered with BlueStack */
			ConnectionRegisterServiceRecord(&state->task, sizeof(serviceRecordFTP),sdp);
		}
	}
	else
	{
		ftpsMsgSendInitCfm(NULL, ftps_failure);
		free(state);
	}
}

static void handleGoepConnectInd(ftpsState *state, GOEP_CONNECT_IND_T *msg)
{
	PRINT(("handleGoepConnectInd\n"));

	if ((msg->size_target>0) && (memcmp(&ftp_targ[0], &msg->target[0], sizeof(ftp_targ))==0))
	{
	   	MAKE_FTPS_MESSAGE(FTPS_CONNECT_IND);
		
		message->session = (FTPS *)state;
		message->bd_addr = msg->bd_addr;
		message->maxPacketLen = msg->maxPacketLen;
        
   		MessageSend(state->theAppTask, FTPS_CONNECT_IND, message);
			
		state->currCom = ftps_com_Connecting;
	}
	else
	{
		/* Reject the connection attempt due to an invalid target header */
		state->currCom = ftps_com_None;
		GoepConnectResponse(state->handle, goep_svr_resp_BadRequest, 0);
	}
}

static void handleGoepConnectCfm(ftpsState *state, GOEP_CONNECT_CFM_T *msg)
{
    PRINT(("handleGoepConnectCfm\n"));
    
    if (msg->status != goep_success)
    { /* Couldn't connect */
		ftpsMsgSendConnectCfm(state, ftps_failure, 0);
		state->currCom= ftps_com_None;
    }
    else
    {
        /* Return to Idle state */
        state->currCom= ftps_com_None;
		
		ftpsMsgSendConnectCfm(state, ftps_success, msg->maxPacketLen);
    }
}

static void handleGoepDisconnectInd(ftpsState *state, GOEP_DISCONNECT_IND_T *msg)
{
    PRINT(("handleGoepDisconnectInd\n"));
    
	FTPS_SEND_IND(state, FTPS_DISCONNECT_IND);
	state->currCom= ftps_com_None;
}

static void handleGoepRemGetStartInd(ftpsState *state, GOEP_REMOTE_GET_START_IND_T *msg)
{
    PRINT(("handleGoepRemGetStartInd\n"));

	if (state->currCom != ftps_com_None)
	{ /* Busy */
		PRINT(("       Busy\n"));
	}
	else
	{
		const uint8 *s = SourceMap(msg->src);

		/* Decide which type of REMOTE_GET is being used */
		if ((msg->typeLength>0) && (memcmp(&typeFolder[0], &s[msg->typeOffset], sizeof(msg->typeLength))==0))
		{ /* Get Folder Listing */
			PRINT(("       Folder Listing\n"));
			FTPS_SEND_IND(state, FTPS_GETFOLDER_START_IND);
			FtpsPacketComplete(state);
			state->currCom = ftps_com_GetFolderListing;
		}
		else
		{ /* Get Object */
    		MAKE_FTPS_MESSAGE(FTPS_GETOBJECT_START_IND);
            
			PRINT(("       Object Get\n"));

			message->session = (FTPS *)state;
			message->src = msg->src;
			message->nameLen = msg->nameLength;
			message->nameOffset = msg->nameOffset;
			message->typeLen = msg->typeLength;
			message->typeOffset = msg->typeOffset;
        
    		MessageSend(state->theAppTask, FTPS_GETOBJECT_START_IND, message);

			state->currCom = ftps_com_GetObject;
		}
	}
}

static void handleGoepRemGetDataInd(ftpsState *state, GOEP_REMOTE_GET_MORE_DATA_REQUEST_IND_T *msg)
{
    PRINT(("handleGoepRemGetDataInd\n"));

	if ((state->currCom != ftps_com_GetFolderListing) && (state->currCom != ftps_com_GetObject))
	{ /* Busy */
		PRINT(("       Busy\n"));
		GoepRemoteGetResponse(state->handle, goep_svr_resp_BadRequest, 0, 0, NULL, 0, NULL, 0, NULL, TRUE);
	}
	else
	{
		MessageId id = FTPS_GETOBJECT_DATA_IND;
   		MAKE_FTPS_MESSAGE(FTPS_GETOBJECT_DATA_IND);

		if (state->currCom == ftps_com_GetFolderListing)
			id = FTPS_GETFOLDER_DATA_IND;
		
		message->session = (FTPS *)state;

		MessageSend(state->theAppTask, id, message);
	}
}

static void handleGoepRemGetCompleteInd(ftpsState *state, GOEP_REMOTE_GET_COMPLETE_IND_T *msg)
{
    PRINT(("handleGoepRemGetDataInd\n"));

	if ((state->currCom != ftps_com_GetFolderListing) && (state->currCom != ftps_com_GetObject))
	{ /* Busy */
		PRINT(("       Busy\n"));
		GoepRemoteGetResponse(state->handle, goep_svr_resp_BadRequest, 0, 0, NULL, 0, NULL, 0, NULL, TRUE);
	}
	else
	{
		MessageId id = FTPS_GETOBJECT_COMPLETE_IND;
   		MAKE_FTPS_MESSAGE(FTPS_GETOBJECT_COMPLETE_IND);

		if (state->currCom == ftps_com_GetFolderListing)
			id = FTPS_GETFOLDER_COMPLETE_IND;
		
		message->session = (FTPS *)state;
		switch (msg->status)
		{
		case goep_success:
			message->status = ftps_success;
			break;
		case goep_host_abort:
			message->status = ftps_remote_abort;
			break;
		default:
			message->status = ftps_failure;
			break;
		}

		MessageSend(state->theAppTask, id, message);
		
		state->currCom = ftps_com_None;
	}
}

static void handleGoepSetPathInd(ftpsState *state, GOEP_SET_PATH_IND_T *msg)
{
    PRINT(("handleGoepRemSetPathInd\n"));

	if (state->currCom != ftps_com_None)
	{ /* Error */
		PRINT(("       Error\n"));
		FtpsPacketComplete(state);
		GoepRemoteSetPathResponse(state->handle, goep_svr_resp_BadRequest);
	}
	else
	{
		/* decide on type of set path */
		if (msg->nameLength > 0)
		{ /* goto sub folder */
			MAKE_FTPS_MESSAGE(FTPS_SETPATH_SUBFOLDER_IND);

			message->session = (FTPS *)state;
			message->src = msg->src;
			message->nameOffset = msg->nameOffset;
			message->nameLength = msg->nameLength;
			message->create = ((msg->flags & GOEP_PATH_NOCREATE) == GOEP_PATH_NOCREATE)? FALSE : TRUE;

			MessageSend(state->theAppTask, FTPS_SETPATH_SUBFOLDER_IND, message);
		}
		else if (msg->flags == (GOEP_PATH_PARENT | GOEP_PATH_NOCREATE))
		{ /* goto parent folder */
			FtpsPacketComplete(state);
			FTPS_SEND_IND(state, FTPS_SETPATH_PARENT_IND);
		}
		else
		{ /* goto root folder */
			FtpsPacketComplete(state);
			FTPS_SEND_IND(state, FTPS_SETPATH_ROOT_IND);
		}

		state->currCom = ftps_com_SetPath;
	}
}

static void handleGoepRemPutStartInd(ftpsState *state, GOEP_REMOTE_PUT_START_IND_T *msg)
{
    PRINT(("handleGoepRemPutStartInd\n"));

	if (state->currCom != ftps_com_None)
	{ /* Busy */
		PRINT(("       Busy\n"));
		SendBadRequest(state);
	}
	else
	{
		if (state->serv_type == ftps_serv_readonly)
		{ /* Server is read only, so ignore the request */
			PRINT(("       Server Read Only\n"));			
			GoepRemotePutResponse(state->handle, goep_svr_resp_Forbidden);
			FtpsPacketComplete(state);
		}
		else
		{
			/* create message */
			MAKE_FTPS_MESSAGE(FTPS_PUTOBJECT_START_IND);

			message->session = (FTPS *)state;
			message->src = msg->src;
			message->nameOffset = msg->nameOffset;
			message->nameLen = msg->nameLength;
			message->typeOffset = msg->typeOffset;
			message->typeLen = msg->typeLength;
			message->totalLength = msg->totalLength;
			message->dataOffset = msg->dataOffset;
			message->dataLength = msg->dataLength;
			message->moreData = msg->moreData;
			
			/* send message */
			MessageSend(state->theAppTask, FTPS_PUTOBJECT_START_IND, message);
		}
		state->currCom = ftps_com_PutObject;
	}
}

static void handleGoepRemPutDataInd(ftpsState *state, GOEP_REMOTE_PUT_DATA_IND_T *msg)
{
    PRINT(("handleGoepRemPutDataInd\n"));

	if (state->currCom != ftps_com_PutObject)
	{ /* Error */
		PRINT(("       Error\n"));
		SendBadRequest(state);
	}
	else
	{
		/* create message */
		MAKE_FTPS_MESSAGE(FTPS_PUTOBJECT_DATA_IND);

		message->session = (FTPS *)state;
		message->src = msg->src;
		message->dataOffset = msg->dataOffset;
		message->dataLength = msg->dataLength;
		message->moreData = msg->moreData;
			
		/* send message */
		MessageSend(state->theAppTask, FTPS_PUTOBJECT_DATA_IND, message);
	}
}

static void handleGoepRemPutCompleteInd(ftpsState *state, GOEP_REMOTE_PUT_COMPLETE_IND_T *msg)
{
    PRINT(("handleGoepRemPutCompleteInd\n"));

	if (state->currCom != ftps_com_PutObject)
	{ /* Error */
		PRINT(("       Error\n"));
	}
	else
	{
		if (state->serv_type == ftps_serv_readonly)
		{ /* Server is read only, request rejected so just drop the data */
			PRINT(("       Server Read Only\n"));
		}
		else
		{
			ftps_lib_status res = ftps_success;
			switch (msg->status)
			{
			case goep_success:
				res = ftps_success;
				break;
			case goep_host_abort:
				res = ftps_remote_abort;
				break;
			default:
				res = ftps_failure;
				break;
			}
			ftpsMsgSendPutObjectCompleteInd(state, res);
		}
		state->currCom= ftps_com_None;
	}
}

static void handleGoepRemPutDeleteInd(ftpsState *state, GOEP_DELETE_IND_T *msg)
{
    PRINT(("handleGoepRemPutDeleteInd\n"));

	if (state->currCom != ftps_com_None)
	{ /* Busy */
		PRINT(("       Busy\n"));
		GoepRemoteDeleteResponse(state->handle, goep_svr_resp_BadRequest);
		FtpsPacketComplete(state);
		state->currCom= ftps_com_None;
	}
	else
	{
		if (state->serv_type == ftps_serv_readonly)
		{ /* Server is read only, so ignore the request */
			PRINT(("       Server Read Only\n"));

			GoepRemoteDeleteResponse(state->handle, goep_svr_resp_Forbidden);
			FtpsPacketComplete(state);
			state->currCom= ftps_com_None;
		}
		else
		{
			/* create message */
			MAKE_FTPS_MESSAGE(FTPS_DELETEOBJECT_IND);

			message->session = (FTPS *)state;
			message->src = msg->src;
			message->nameOffset = msg->nameOffset;
			message->nameLength = msg->nameLength;
			
			/* send message */
			MessageSend(state->theAppTask, FTPS_DELETEOBJECT_IND, message);
			state->currCom = ftps_com_DeleteObject;
		}
	}
}

/* Find the rfcomm server channel in a service record */
static bool findRfcommServerChannel(const uint8 *ptr, const uint8 *end, Region *value)
{
    ServiceDataType type;
    Region record, protocols, protocol;
    record.begin = ptr;
    record.end   = end;
    while(ServiceFindAttribute(&record, saProtocolDescriptorList, &type, &protocols))
	if(type == sdtSequence)
	    while(ServiceGetValue(&protocols, &type, &protocol))
		if(type == sdtSequence
		&& ServiceGetValue(&protocol, &type, value) 
                && type == sdtUUID
		&& RegionMatchesUUID32(value, UUID_RFCOMM)
		&& ServiceGetValue(&protocol, &type, value)
		&& type == sdtUnsignedInteger)
		{
		    /* Hah! found the server channel field */
		    return 1;
		}
    return 0; /* Failed */

}

/* Insert the rfcomm server channel into a service record */
static uint16 insertRfcommServerChannel(uint8 *ptr, uint8 *end, uint8 chan)
{
    Region value;
    if(findRfcommServerChannel(ptr, end, &value) && RegionSize(&value) == 1)
    {
		RegionWriteUnsigned(&value, chan);
		return 1;
    }
    return 0;
}

/* Connection Library message handlers */
static void handleSDPRegisterCfm(ftpsState *state, CL_SDP_REGISTER_CFM_T *msg)
{
	PRINT(("handleSDPRegisterCfm\n"));
	
	if (msg->status == success)
	{
		state->sdpHandle = msg->service_handle;
		ftpsMsgSendInitCfm((FTPS*)state, ftps_success);
	}
	else
	{
		ftpsMsgSendInitCfm(NULL, ftps_failure);
		free(state);
	}
}

⌨️ 快捷键说明

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