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

📄 ftps_handler.c

📁 CSR蓝牙芯片的ftp程序的服务器端代码 客户端已经上传
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2004

FILE NAME
    ftps_handler.c
    
DESCRIPTION
	Message handler source for the FTP Server library

*/

#include <message.h>
#include <stream.h>
#include <source.h>
#include <panic.h>
#include <print.h>
#include <string.h>

#include <app/bluestack/types.h>
#include <app/bluestack/bluetooth.h>
#include <app/bluestack/sdc_prim.h>
#include <service.h>
#include <connection.h>

#include "ftps.h"
#include "ftps_private.h"

/* Static Strings */

/* Default FTP Service Record */
static const uint8 serviceRecordFTP[] =
    {   
		/* Service class ID list */
		0x09,0x00,0x01,  /* AttrID , ServiceClassIDList */
		0x35,0x03,   /* 3 bytes in total DataElSeq */
		0x19,0x11,0x06,  /* 2 byte UUID, Service class = OBEX_FTP */

		/* protocol descriptor list */
		0x09,0x00,0x04,  /* AttrId ProtocolDescriptorList */
		0x35,0x11,   /* 17 bytes in total DataElSeq */
		0x35,0x03,   /* 3 bytes in DataElSeq */
		0x19,0x01,0x00,  /* 2 byte UUID, Protocol = L2CAP */

		0x35,0x05,   /* 5 bytes in DataElSeq */
		0x19,0x00,0x03,  /* 2 byte UUID Protocol = RFCOMM */
		0x08,0x00,   /* 1 byte UINT - server channel template value 0 - to be filled in by app */

		0x35,0x03,   /* 3 bytes in DataElSeq */
		0x19, 0x00, 0x08, /* 2 byte UUID, Protocol = OBEX */

		/* service name */
		0x09,0x01,0x00,  /* AttrId - Service Name */
		0x25,0x08,   /* 8 byte string - OBEX FTP */
		'O','B','E','X',' ','F','T','P',

		/* profile descriptor list */
		0x09,0x00,0x09,  /* AttrId, ProfileDescriptorList */
		0x35,0x08,   /* DataElSeq wrapper */
		0x35,0x06,   /* 6 bytes in total DataElSeq */
		0x19,0x11,0x06,  /* 2 byte UUID, Service class = OBEX_FTP */
		0x09,0x01,0x00,  /* 2 byte uint, version = 100 */
    };

/* UUID Target String for Obex_FTP */
static const uint8 ftp_targ[]={0xf9,0xec,0x7b,0xc4, 0x95,0x3c, 0x11,0xd2, 0x98,0x4e, 0x52,0x54,0x00,0xdc,0x9e,0x09};

/* Internal Message Handlers */
static void handleIntAcceptConnection(ftpsState *state, FTPS_INT_CONNECTION_RESP_T *msg);

static void handleIntSendFirstPacket(ftpsState *state, FTPS_INT_SEND_FIRST_PACKET_T *msg);
static void handleIntSendNextPacket(ftpsState *state, FTPS_INT_SEND_NEXT_PACKET_T *msg);
static void handleIntSendFirstPacketSrc(ftpsState *state, FTPS_INT_SEND_FIRST_PACKET_SRC_T *msg);
static void handleIntSendNextPacketSrc(ftpsState *state, FTPS_INT_SEND_NEXT_PACKET_SRC_T *msg);

static void handleIntSetPathResult(ftpsState *state, FTPS_INT_SETPATH_RESULT_T *msg);

static void handleIntSendGetReject(ftpsState *state, FTPS_INT_SEND_GET_REJECT_T *msg);
static void handleIntGetNextPacket(ftpsState *state, FTPS_INT_GET_NEXT_PACKET_T *msg);
static void handleIntSendDeleteReply(ftpsState *state, FTPS_INT_SEND_DELETE_REPLY_T *msg);
static void handleIntSendPutReject(ftpsState *state, FTPS_INT_SEND_PUT_REJECT_T *msg);

/* GOEP Library Message Handlers */
static void handleGoepInitCfm(ftpsState *state, GOEP_INIT_CFM_T *msg);
static void handleGoepChannelInd(ftpsState *state, GOEP_CHANNEL_IND_T *msg);
static void handleGoepConnectInd(ftpsState *state, GOEP_CONNECT_IND_T *msg);
static void handleGoepConnectCfm(ftpsState *state, GOEP_CONNECT_CFM_T *msg);
static void handleGoepDisconnectInd(ftpsState *state, GOEP_DISCONNECT_IND_T *msg);


static void handleGoepRemGetStartInd(ftpsState *state, GOEP_REMOTE_GET_START_IND_T *msg);
static void handleGoepRemGetDataInd(ftpsState *state, GOEP_REMOTE_GET_MORE_DATA_REQUEST_IND_T *msg);
static void handleGoepRemGetCompleteInd(ftpsState *state, GOEP_REMOTE_GET_COMPLETE_IND_T *msg);

static void handleGoepSetPathInd(ftpsState *state, GOEP_SET_PATH_IND_T *msg);

static void handleGoepRemPutStartInd(ftpsState *state, GOEP_REMOTE_PUT_START_IND_T *msg);
static void handleGoepRemPutDataInd(ftpsState *state, GOEP_REMOTE_PUT_DATA_IND_T *msg);
static void handleGoepRemPutCompleteInd(ftpsState *state, GOEP_REMOTE_PUT_COMPLETE_IND_T *msg);

static void handleGoepRemPutDeleteInd(ftpsState *state, GOEP_DELETE_IND_T *msg);

static void SendBadRequest(ftpsState *state);

/* Connection Library message handlers */
static void handleSDPRegisterCfm(ftpsState *state, CL_SDP_REGISTER_CFM_T *msg);

/* Find the rfcomm server channel in a service record */
static bool findRfcommServerChannel(const uint8 *ptr, const uint8 *end, Region *value);
/* Insert the rfcomm server channel into a service record */
static uint16 insertRfcommServerChannel(uint8 *ptr, uint8 *end, uint8 chan);


void ftpsHandler(Task task, MessageId id, Message message)
{
	/* Get task control block */
	ftpsState *state = (ftpsState*)task;

    switch(id)
    {
        /* Internal FTP Server messages */
	case FTPS_INT_CONNECTION_RESP:
		handleIntAcceptConnection(state, (FTPS_INT_CONNECTION_RESP_T*)message);
		break;
	case FTPS_INT_SEND_FIRST_PACKET:
		handleIntSendFirstPacket(state, (FTPS_INT_SEND_FIRST_PACKET_T*)message);
		break;
	case FTPS_INT_SEND_NEXT_PACKET:
		handleIntSendNextPacket(state, (FTPS_INT_SEND_NEXT_PACKET_T*)message);
		break;
	case FTPS_INT_SEND_FIRST_PACKET_SRC:
		handleIntSendFirstPacketSrc(state, (FTPS_INT_SEND_FIRST_PACKET_SRC_T*)message);
		break;
	case FTPS_INT_SEND_NEXT_PACKET_SRC:
		handleIntSendNextPacketSrc(state, (FTPS_INT_SEND_NEXT_PACKET_SRC_T*)message);
		break;
	case FTPS_INT_SETPATH_RESULT:
		handleIntSetPathResult(state, (FTPS_INT_SETPATH_RESULT_T*)message);
		break;
	case FTPS_INT_SEND_GET_REJECT:
		handleIntSendGetReject(state, (FTPS_INT_SEND_GET_REJECT_T*)message);
		break;
	case FTPS_INT_GET_NEXT_PACKET:
		handleIntGetNextPacket(state, (FTPS_INT_GET_NEXT_PACKET_T*)message);
		break;
	case FTPS_INT_SEND_DELETE_REPLY:
		handleIntSendDeleteReply(state, (FTPS_INT_SEND_DELETE_REPLY_T*)message);
		break;
	case FTPS_INT_SEND_PUT_REJECT:
		handleIntSendPutReject(state, (FTPS_INT_SEND_PUT_REJECT_T*)message);
		break;
		
        /* Messages from the GOEP Library */
	case GOEP_INIT_CFM:
		handleGoepInitCfm(state, (GOEP_INIT_CFM_T*)message);
		break;
	case GOEP_CHANNEL_IND:
		handleGoepChannelInd(state, (GOEP_CHANNEL_IND_T*)message);
		break;
    case GOEP_CONNECT_IND:
        handleGoepConnectInd(state, (GOEP_CONNECT_IND_T*)message);
        break;
    case GOEP_CONNECT_CFM:
        handleGoepConnectCfm(state, (GOEP_CONNECT_CFM_T*)message);
        break;
    case GOEP_DISCONNECT_IND:
        handleGoepDisconnectInd(state, (GOEP_DISCONNECT_IND_T*)message);
        break;
	case GOEP_REMOTE_GET_START_IND:
		handleGoepRemGetStartInd(state, (GOEP_REMOTE_GET_START_IND_T*)message);
		break;
	case GOEP_REMOTE_GET_MORE_DATA_REQUEST_IND:
		handleGoepRemGetDataInd(state, (GOEP_REMOTE_GET_MORE_DATA_REQUEST_IND_T*)message);
		break;
	case GOEP_REMOTE_GET_COMPLETE_IND:
		handleGoepRemGetCompleteInd(state, (GOEP_REMOTE_GET_COMPLETE_IND_T*)message);
		break;
	case GOEP_SET_PATH_IND:
		handleGoepSetPathInd(state, (GOEP_SET_PATH_IND_T*)message);
		break;
	case GOEP_REMOTE_PUT_START_IND:
		handleGoepRemPutStartInd(state, (GOEP_REMOTE_PUT_START_IND_T*)message);
		break;
	case GOEP_REMOTE_PUT_DATA_IND:
		handleGoepRemPutDataInd(state, (GOEP_REMOTE_PUT_DATA_IND_T*)message);
		break;
	case GOEP_REMOTE_PUT_COMPLETE_IND:
		handleGoepRemPutCompleteInd(state, (GOEP_REMOTE_PUT_COMPLETE_IND_T*)message);
		break;
	case GOEP_DELETE_IND:
		handleGoepRemPutDeleteInd(state, (GOEP_DELETE_IND_T*)message);
		break;
		
		/* Messages from the connection library */
	case CL_SDP_REGISTER_CFM:
		handleSDPRegisterCfm(state, (CL_SDP_REGISTER_CFM_T*)message);
		break;
        
	default:
		PRINT(("FTPS Unhandled Message : %d , 0x%0X\n",id,id) ); 
		break;
	}
}

/* Internal Message Handlers */

static void handleIntAcceptConnection(ftpsState *state, FTPS_INT_CONNECTION_RESP_T *msg)
{
    PRINT(("handleIntAcceptConnection\n"));
    
    if (state->currCom!= ftps_com_Connecting)
    { /* Not idle, Send Error Message */
		ftpsMsgSendConnectCfm(state, ftps_wrong_state, 0);
    }
    else
    {
		if (msg->accept)
		{ /* Send accept response code */
			GoepConnectResponse(state->handle, goep_svr_resp_OK, msg->pktSize);
			state->serv_type = msg->type;
		}
		else
		{ /* Send reject response code */
			GoepConnectResponse(state->handle, goep_svr_resp_BadRequest, msg->pktSize);
	        state->currCom= ftps_com_None;
		}
    }
}

static void handleIntSendFirstPacket(ftpsState *state, FTPS_INT_SEND_FIRST_PACKET_T *msg)
{
    PRINT(("handleIntSendFirstPacket\n"));
    
    if ((state->currCom!= ftps_com_GetObject) && (state->currCom!= ftps_com_GetFolderListing))
    { /* Not idle, Send Error Message */
		ftpsMsgSendGetObjectCompleteInd(state, ftps_wrong_state);
		PRINT(("       Error\n"));
    }
    else
    {        
		GoepRemoteGetResponse(state->handle, goep_svr_resp_OK, msg->totalLen,
				  msg->nameLen, msg->name,
				  msg->typeLen, msg->type,
				  msg->length, msg->packet, msg->onlyPacket);
    }
}

static void handleIntSendNextPacket(ftpsState *state, FTPS_INT_SEND_NEXT_PACKET_T *msg)
{
    PRINT(("handleIntSendNextPacket\n"));
    
    if ((state->currCom!= ftps_com_GetObject) && (state->currCom!= ftps_com_GetFolderListing))
    { /* Not idle, Send Error Message */
		ftpsMsgSendGetObjectCompleteInd(state, ftps_wrong_state);
		PRINT(("       Error\n"));
    }
    else
    {        
		GoepRemoteGetResponsePkt(state->handle, msg->length, msg->packet, msg->lastPacket);
    }
}

static void handleIntSendFirstPacketSrc(ftpsState *state, FTPS_INT_SEND_FIRST_PACKET_SRC_T *msg)
{
    PRINT(("handleIntSendFirstPacketSrc\n"));
    
    if ((state->currCom!= ftps_com_GetObject) && (state->currCom!= ftps_com_GetFolderListing))
    { /* Not idle, Send Error Message */
		ftpsMsgSendGetObjectCompleteInd(state, ftps_wrong_state);
		PRINT(("       Error\n"));
    }
    else
    {        
		GoepRemoteGetResponseSource(state->handle, goep_svr_resp_OK, msg->totalLen,
				  msg->nameLen, msg->name,
				  msg->typeLen, msg->type,
				  msg->length, msg->src, msg->offset, msg->onlyPacket);
    }
}

static void handleIntSendNextPacketSrc(ftpsState *state, FTPS_INT_SEND_NEXT_PACKET_SRC_T *msg)
{
    PRINT(("handleIntSendNextPacketSrc\n"));
    
    if ((state->currCom!= ftps_com_GetObject) && (state->currCom!= ftps_com_GetFolderListing))
    { /* Not idle, Send Error Message */
		ftpsMsgSendGetObjectCompleteInd(state, ftps_wrong_state);
		PRINT(("       Error\n"));
    }
    else
    {        
		GoepRemoteGetResponsePktSource(state->handle, msg->length, msg->src, msg->offset, msg->lastPacket);
    }
}

static void handleIntSetPathResult(ftpsState *state, FTPS_INT_SETPATH_RESULT_T *msg)
{
    PRINT(("handleIntSetPathResult\n"));
    
	if (state->currCom != ftps_com_SetPath)
    {
		PRINT(("       Error\n"));
    }
    else
    {
		goep_svr_resp_codes result = goep_svr_resp_OK;

		switch (msg->result)
		{
		case ftps_setpath_OK:
			result = goep_svr_resp_OK;
			break;
		case ftps_setpath_NotFound:
			result = goep_svr_resp_NotFound;
			break;
		case ftps_setpath_NoBrowsing:
			result = goep_svr_resp_Forbidden;
			break;
		case ftps_setpath_AtRoot:
			result = goep_svr_resp_NotFound;
			break;
		case ftps_setpath_ReadOnly:
			result = goep_svr_resp_Unauthorized;
			break;
		}
		GoepRemoteSetPathResponse(state->handle, result);

		state->currCom = ftps_com_None;
    }
}

static void handleIntSendGetReject(ftpsState *state, FTPS_INT_SEND_GET_REJECT_T *msg)
{
    PRINT(("handleIntSendGetReject\n"));
    
    if (state->currCom!= ftps_com_GetObject)
    { /* Not idle, Send Error Message */
		ftpsMsgSendGetObjectCompleteInd(state, ftps_wrong_state);
		PRINT(("       Error\n"));
    }
    else
    {
		goep_svr_resp_codes reason = goep_svr_resp_BadRequest;

		switch (msg->reason)
		{
		case ftps_transfer_NotFound:
			reason = goep_svr_resp_NotFound;
			break;
		case ftps_transfer_Forbidden:
			reason = goep_svr_resp_Forbidden;
			break;
		case ftps_transfer_Unauthorized:
			reason = goep_svr_resp_Unauthorized;
			break;
		case ftps_transfer_BadRequest:
		default:
			reason = goep_svr_resp_BadRequest;
			break;
		};

		GoepRemoteGetResponse(state->handle, reason, 0, 0, NULL, 0, NULL, 0, NULL, TRUE);
    }
}

static void handleIntGetNextPacket(ftpsState *state, FTPS_INT_GET_NEXT_PACKET_T *msg)
{
    PRINT(("handleIntGetNextPacket\n"));
    
	if (state->currCom != ftps_com_PutObject)
    { /* Not receiving data, Send Error Message */
		ftpsMsgSendPutObjectCompleteInd(state, ftps_wrong_state);
    }
    else
    {        
		if (msg->moreData)
			GoepRemotePutResponse(state->handle, goep_svr_resp_Continue);
		else
		{
        	GoepRemotePutResponse(state->handle, goep_svr_resp_OK);
		}
    }
}

static void handleIntSendDeleteReply(ftpsState *state, FTPS_INT_SEND_DELETE_REPLY_T *msg)
{
    PRINT(("handleIntSendDeleteReply\n"));
    
	if (state->currCom != ftps_com_DeleteObject)
    { /* Not receiving data, Send Error Message */
		PRINT(("       Error\n"));
    }
    else
    {        
		if (msg->success)
			GoepRemoteDeleteResponse(state->handle, goep_svr_resp_OK);
		else
        	GoepRemoteDeleteResponse(state->handle, goep_svr_resp_Unauthorized);

		/* Return to Idle state */
		state->currCom = ftps_com_None;
    }
}

static void handleIntSendPutReject(ftpsState *state, FTPS_INT_SEND_PUT_REJECT_T *msg)
{
    PRINT(("handleIntSendPutReject\n"));
    
    if (state->currCom!= ftps_com_GetObject)
    { /* Not idle, Send Error Message */
		ftpsMsgSendPutObjectCompleteInd(state, ftps_wrong_state);
		PRINT(("       Error\n"));
    }
    else
    {
		goep_svr_resp_codes reason = goep_svr_resp_BadRequest;

		switch (msg->reason)
		{
		case ftps_transfer_Forbidden:
		case ftps_transfer_Readonly:
			reason = goep_svr_resp_Forbidden;
			break;
		case ftps_transfer_Unauthorized:
			reason = goep_svr_resp_Unauthorized;
			break;
		case ftps_transfer_BadRequest:
		default:
			reason = goep_svr_resp_BadRequest;
			break;
		};

		GoepRemotePutResponse(state->handle, reason);
    }
}




static void SendBadRequest(ftpsState *state)

⌨️ 快捷键说明

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