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

📄 mcpost.c

📁 彩信MMS的全部代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (C) Obigo AB, 2002-2005.
 * All rights reserved.
 *
 * This software is covered by the license agreement between
 * the end user and Obigo AB, and may be 
 * used and copied only in accordance with the terms of the 
 * said agreement.
 *
 * Obigo AB assumes no responsibility or 
 * liability for any errors or inaccuracies in this software, 
 * or any consequential, incidental or indirect damage arising
 * out of the use of the software.
 *
 */

















#include "cansilib.h"   
#include "cmnconf.h"    
#include "aapifile.h"   
#include "cmntypes.h"   
#include "aapicmn.h"    
#include "fldrmgr.h"    
#include "gmem.h"       

#include "mmsconf.h"    
#include "mmstypes.h"   
#include "aapimms.h"    
#include "msig.h"       
#include "mtimer.h"     
#include "mmem.h"       
#include "mcpdu.h"      
#include "mcpost.h"     
#include "mcwap.h"      
#include "mconfig.h"    
#include "mutils.h"      
#include "mfieldp.h"    
#include "mrptr.h"      


#define MMS_MAX_FSM_POST_INSTANCES  5
#define LEN_POST_PORT               5       
#define LEN_TRANSACTION_ID          11      








#define MMS_POST_HTTP_HEADER    "Accept: */*\n" \
                                "Content-Type: " MMS_CONTENT_TYPE_STRING "\n"




typedef struct
{
    MmsStateMachine requestingFsm;  
    long            fsmInstance;    
    MmsSignalId     returnSig;      
    MmsRequestId    requestId;      
    char            *uri;           
    UINT32          retries;        
    unsigned long   msgSize;        
    MmsHttpContent  *cont;          
    MmsHttpContent  *sarBuffer;     
    unsigned long   sent;           
    char            *transactionId; 
    MmsSigCohPostParam param;       
    MmsResponseStatus responseStatus; 
    MmsEncodedText  *responseText;  
} CohPostInstanceData;




















static CohPostInstanceData *fsmInstance[MMS_MAX_FSM_POST_INSTANCES];
static char *myHttpHeader;


static MmsResult checkResponseGeneral( CohPostInstanceData *fsm, void *data, 
    unsigned long length, CMN_BOOL *permanentError);
static MmsResult checkResponsePdu( CohPostInstanceData *fsm, void *data, 
    unsigned long length, CMN_BOOL *permanentError, MmsMessageType pduType);
static void cohPostMain(MmsSignal *sig);
static void deleteInstance(long instance);
static long getInstance(const MmsSignal *sig);
static char *getTransactionId( char *header, unsigned long length);
static void handleResponse(long instance, const MmsSignal *sig);
static CMN_BOOL packetReassemble(CohPostInstanceData *fsm, CMN_BOOL moreData);
static void postFinished(long instance, MmsResult ret);
static void postIt(long instance);
static void postMore(long instance);
static void postMsg(long instance);
static void postMsgAbort(long instance, MmsResult ret);
static long postMsgInit(MmsSignal *sig);
static void postPdu(long instance);
static void postRetry(long instance, MmsResult ret);
static long selectInstance(void);












static MmsResult checkResponseGeneral( CohPostInstanceData *fsm, void *data, 
    unsigned long length, CMN_BOOL *permanentError)
{
    MmsResult       res = MMS_RESULT_OK;
    MmsHeaderValue  read;
    long            remain;
    UINT32          notUsed = 0;

    *permanentError = FALSE;
    if ( !mmsPduGet( data, length, X_MMS_MESSAGE_TYPE, &read) )
    {
        

    }
    else if (read.messageType == MMS_M_SEND_CONF) 
    {   


        if ( !mmsPduGet( data, length, X_MMS_RESPONSE_STATUS, &read) )
        {
            MMS_LOG_I(("%s(%d): No data X_MMS_RESPONSE_STATUS header tag\n",
                __FILE__, __LINE__));
            res = MMS_RESULT_COMM_ILLEGAL_PDU;
        }
        else if (*(UINT8 *)read.responseStatus == MMS_RSP_STATUS_UNSUPPORTED_MSG)
        {
            MMS_LOG_I(("%s(%d): MMSC didn't recognize PDU, response status (%d)\n", 
                __FILE__, __LINE__, (int)*(UINT8 *)read.responseStatus));

            fsm->responseStatus = (MmsResponseStatus)*(UINT8 *)read.responseStatus;
            *permanentError = TRUE;
            res = translateResponseStatus(
                (MmsResponseStatus)*(UINT8 *)read.responseStatus);

            

            if ( mmsPduGet( data, length, X_MMS_RESPONSE_TEXT, &read) )
            {
                fsm->responseText = M_ALLOCTYPE(MmsEncodedText);
                remain = (long)((char *)data - (char *)read.responseText + (long)length);

                if (remain > 0)
                {
                    parseEncodedStringValue( read.responseText, (UINT32)remain, 
                        &notUsed, fsm->responseText);
                }
                else
                {
                    MMS_LOG_I(("%s(%d): Illegal PDU, %ld\n" 
                        __FILE__, __LINE__, remain));
                    res = MMS_RESULT_COMM_ILLEGAL_PDU;
                } 
            } 
        } 
    } 

    return res;
} 











static MmsResult checkResponsePdu( CohPostInstanceData *fsm, 
    void *data, unsigned long length, CMN_BOOL *permanentError,
    MmsMessageType pduType)
{
    MmsResult       res = MMS_RESULT_OK;
    MmsHeaderValue  read;
    MmsHeaderValue  server;
    UINT32          notUsed = 0;
    long            remain;
    char            *offset;

    *permanentError = FALSE;
    if (data == NULL)
    {
        MMS_LOG_I(("POST handleResponse: No data returned, error (%d)\n", 
            data));

        res = MMS_RESULT_COMM_ILLEGAL_PDU;
    }
    else if ( mmsPduUnrecognized( data, length) )
    {
        MMS_LOG_I(("%s(%d): Unrecognized PDU!\n", __FILE__, __LINE__));
        res = MMS_RESULT_COMM_ILLEGAL_PDU;
    }
    else if ( !mmsPduSanityCheck( data, length) )
    {
        MMS_LOG_I(("%s(%d): Badly formatted PDU!\n", __FILE__, __LINE__));
        res = MMS_RESULT_COMM_ILLEGAL_PDU;
    }
    else if ( !mmsPduGet( data, length, X_MMS_MESSAGE_TYPE, &read) )
    {
        MMS_LOG_I(("%s(%d): No data X_MMS_MESSAGE_TYPE header tag\n",
            __FILE__, __LINE__));
        res = MMS_RESULT_COMM_HEADER_TAG;
    }
    else if (read.messageType != pduType)
    {
        MMS_LOG_I(("%s(%d): Wrong message type (%d). Expected %d.\n", 
            __FILE__, __LINE__, read.messageType, pduType));
        res = MMS_RESULT_COMM_UNEXPECTED_MESSAGE;
    }
    else if ( !mmsPduGet( data, length, X_MMS_TRANSACTION_ID, &read) )
    {
        MMS_LOG_I(("%s(%d): No data X_MMS_TRANSACTION_ID header tag\n",
            __FILE__, __LINE__));
        res = MMS_RESULT_COMM_HEADER_TAG;
    }
    else if ( read.transactionId == NULL || fsm->transactionId == NULL ||
        strcmp( (char *)read.transactionId, (char *)fsm->transactionId) != 0 )
    {
        MMS_LOG_I(("%s(%d): Wrong transaction ID (%s) != (%s)\n", 
            __FILE__, __LINE__,
            read.transactionId == NULL ? "NULL" : read.transactionId,
            fsm->transactionId == NULL ? "NULL" : fsm->transactionId));
        res = MMS_RESULT_COMM_TRANSACTION_ID;
    }
    else if ( !mmsPduGet( data, length, X_MMS_RESPONSE_STATUS, &read) )
    {
        MMS_LOG_I(("%s(%d): No data X_MMS_RESPONSE_STATUS header tag\n",
            __FILE__, __LINE__));
        res = MMS_RESULT_COMM_HEADER_TAG;
    }
    else 
    {
        if (pduType != MMS_M_MBOX_DELETE_CONF)
        {
            fsm->responseStatus = (MmsResponseStatus)*(UINT8 *)read.responseStatus;
        }
        else
        {
            MMS_LOG_I(("%s(%d): MMS 1.2 MMBOX: Only the first of possible "
                "multiple response status codes are examined\n", 
                __FILE__, __LINE__));

            remain = (long)((char *)data - (char *)read.responseStatus + (long)length);
            offset = cnvValueLengthToUint32( read.responseStatus, &notUsed,
                (UINT32)remain);
            remain = (long)((char *)offset - (char *)read.responseStatus + (long)length);

            if (offset == NULL || remain < 1)
            {
                MMS_LOG_I(("%s(%d): Illegal PDU, %ld\n" 
                    __FILE__, __LINE__, remain));
                res = MMS_RESULT_COMM_ILLEGAL_PDU;
            }
            else if (IS_SHORT_INTEGER(*offset))
            {
                ++offset;
                fsm->responseStatus = (MmsResponseStatus)*(UINT8 *)offset;
            }
            else 
            {
                
                remain -= *offset;
                offset += *offset;
                if (remain > 0)
                {
                    fsm->responseStatus = (MmsResponseStatus)*(UINT8 *)offset;
                }
                else
                {
                    MMS_LOG_I(("%s(%d): Illegal PDU, %ld\n" 
                        __FILE__, __LINE__, remain));
                    res = MMS_RESULT_COMM_ILLEGAL_PDU;
                } 
            } 
        } 

        if ( mmsPduGet( data, length, X_MMS_RESPONSE_TEXT, &read) )
        {
            fsm->responseText = M_ALLOCTYPE(MmsEncodedText);
            remain = (long)((char *)data - (char *)read.responseText + (long)length);

            if (remain > 0)
            {
                parseEncodedStringValue( read.responseText, (UINT32)remain, &notUsed, 
                    fsm->responseText);
            }
            else
            {
                MMS_LOG_I(("%s(%d): Illegal PDU, %ld\n" 
                    __FILE__, __LINE__, remain));
                res = MMS_RESULT_COMM_ILLEGAL_PDU;
            } 
        } 

        if (res != MMS_RESULT_OK)
        {
            
        }
        else if (fsm->responseStatus != MMS_RSP_STATUS_OK)
        {
            MMS_LOG_I(("%s(%d): Wrong response status (%d)\n", 
                __FILE__, __LINE__, fsm->responseStatus));
            if (fsm->responseStatus >= MMS_RSP_STATUS_P_FAILURE ||
                fsm->responseStatus <= MMS_RSP_STATUS_UNSUPPORTED_MSG)
            {
                *permanentError = TRUE;
            } 
            
            res = translateResponseStatus(fsm->responseStatus);
        }
        else
        {
            


            if ( mmsPduGet( data, length, MMS_MESSAGE_ID, &server) )
            {
                if (fsm->param.origMsg != 0) 
                {
                    

 
                    (void)mrpthAddMessageId(fsm->param.origMsg, server.messageId);
                }                
            } 
            
            res = MMS_RESULT_OK;
        } 
    } 

    return res;
} 





void cohPostCancel(MmsMsgId msgId)
{
    M_SIGNAL_SENDTO_U( M_FSM_COH_POST, MMS_SIG_COH_POST_CANCEL, msgId);
} 





void cohPostFreeResultParam(MmsSigCohPostResultParam *p)
{
    if (p != NULL)
    {
        if (p->responseText != NULL)
        {
            if (p->responseText->text != NULL)
            {
                M_FREE(p->responseText->text);
            } 

            M_FREE(p->responseText);
        } 

        M_FREE(p);
    } 
} 





void cohPostInit(void)
{
    int i;

    myHttpHeader = NULL;
    for (i = 0; i < MMS_MAX_FSM_POST_INSTANCES; ++i)
    {
        fsmInstance[i] = NULL;
    } 

    mSignalRegisterDst(M_FSM_COH_POST, cohPostMain);
    cohPostSetHttpHeader();
    
    MMS_LOG_I(("MMS FSM COH POST: initialized\n"));
} 













long cohPostInstance(MmsRequestId id)
{
    long i;
    
    
    for (i = 0L; i < MMS_MAX_FSM_POST_INSTANCES; ++i)
    {
        if (fsmInstance[i] != NULL && fsmInstance[i]->requestId == id)
        {
            return i;
        } 
    } 

    return -1L;
} 






static void cohPostMain(MmsSignal *sig)
{
    long instance = -1L;
    int i;

    switch (sig->type)
    {
    case MMS_SIG_COH_POST : 
        MMS_LOG_I(("MMS FSM COH POST: Received MMS_SIG_COH_POST\n"));

        
        if ((instance = postMsgInit(sig)) != -1L)
        {
            
            M_SIGNAL_SENDTO_IUU( M_FSM_COH_WAP, MMS_SIG_COH_WAP_START, instance, 
                M_FSM_COH_POST, MMS_SIG_COH_POST_CONNECTED);
        } 
        break;        
    case MMS_SIG_COH_POST_CONNECTED : 
        MMS_LOG_I(("MMS FSM COH POST: Received MMS_SIG_COH_POST_CONNECTED\n"));
        instance = sig->i_param;
        if (instance < 0 || instance >= MMS_MAX_FSM_POST_INSTANCES ||
            fsmInstance[instance] == NULL)
        {
            MMS_LOG_I(("%s(%d): Wrong instance %ld\n", __FILE__, __LINE__, instance));
        }
        else if ((MmsResult)sig->u_param1 != MMS_RESULT_OK)
        {
            MMS_LOG_I(("%s(%d): Couldn't establish connection %d\n", 
                __FILE__, __LINE__, sig->u_param1));
            if ((MmsResult)sig->u_param1 != MMS_RESULT_CANCELLED_BY_USER &&
                ++fsmInstance[instance]->retries < cfgGetInt(MMS_CFG_POST_RETRY))
            {
                
                M_SIGNAL_SENDTO_IUU( M_FSM_COH_WAP, MMS_SIG_COH_WAP_START, 
                    instance, M_FSM_COH_POST, MMS_SIG_COH_POST_CONNECTED);
            }
            else
            {
                postFinished( instance, (MmsResult)sig->u_param1);
            } 
        }
        else
        {
            postIt(instance);
        } 
        break;        
    case MMS_SIG_COH_POST_DISCONNECTED : 
        MMS_LOG_I(("MMS FSM COH POST: Received MMS_SIG_COH_POST_DISCONNECTED\n"));
        break;        
    case MMS_SIG_COH_POST_CANCEL : 
        MMS_LOG_I(("MMS FSM COH POST: MMS_SIG_COH_POST_CANCEL %lu\n", sig->u_param1));

        for (i = 0; i < MMS_MAX_FSM_POST_INSTANCES; ++i)
        {
            if (fsmInstance[i] != NULL)
            {
                

                if (fsmInstance[i]->param.type == MMS_M_SEND_REQ && 
                    fsmInstance[i]->param.data.msgId == sig->u_param1)
                {
                    

                    postFinished( i, MMS_RESULT_CANCELLED_BY_USER);
                } 
            } 
        } 
        break;
    case MMS_SIG_COH_POST_MORE : 
        MMS_LOG_I(("MMS FSM COH POST: Received MMS_SIG_COH_POST_MORE (%ld)\n", 
            sig->i_param));

⌨️ 快捷键说明

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