📄 mcget.c
字号:
/*
* 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 "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 "mcget.h"
#include "mcwap.h"
#include "mconfig.h"
#include "mutils.h"
#include "mfieldp.h"
#define MMS_MAX_FSM_GET_INSTANCES 1
#define MMS_GET_HTTP_HEADER "Accept: */*\n"
typedef struct
{
MmsStateMachine requestingFsm;
long fsmInstance;
MmsSignalId returnSig;
MmsRequestId requestId;
UINT32 msgId;
char *uri;
char *notifTransId;
char *retrieveTransId;
UINT32 retries;
unsigned long received;
unsigned long expected;
MmsHttpContent *cont;
MmsRetrieveStatus retrieveStatus;
MmsEncodedText *retrieveText;
} CohGetInstanceData;
static CohGetInstanceData *fsmInstance[MMS_MAX_FSM_GET_INSTANCES];
static char *myHttpHeader;
static void cohGetMain(MmsSignal *sig);
static void deleteInstance(long instance);
static void finalizeData(long instance);
static void getFinished(long instance, MmsResult ret);
static long getInstance(long instance);
static void getMsg(long instance);
static long getMsgInit(const MmsSignal *sig);
static void getRetry(long instance, MmsResult res);
static void handleResponse(long instance, const MmsSignal *sig);
static long selectInstance(void);
void cohGetCancel(MmsMsgId msgId)
{
M_SIGNAL_SENDTO_U( M_FSM_COH_GET, MMS_SIG_COH_GET_CANCEL, msgId);
}
void cohGetFreeResultParam(MmsSigCohGetResultParam *p)
{
if (p != NULL)
{
if (p->retrieveText != NULL)
{
if (p->retrieveText->text != NULL)
{
M_FREE(p->retrieveText->text);
}
M_FREE(p->retrieveText);
}
if (p->retrieveTransId != NULL)
{
M_FREE(p->retrieveTransId);
}
M_FREE(p);
}
}
void cohGetInit(void)
{
int i;
myHttpHeader = NULL;
for (i = 0; i < MMS_MAX_FSM_GET_INSTANCES; ++i)
{
fsmInstance[i] = NULL;
}
mSignalRegisterDst(M_FSM_COH_GET, cohGetMain);
cohGetSetHttpHeader();
MMS_LOG_I(("MMS FSM COH GET: initialized\n"));
}
long cohGetInstance(MmsRequestId id)
{
long i;
for (i = 0L; i < MMS_MAX_FSM_GET_INSTANCES; ++i)
{
if (fsmInstance[i] != NULL && fsmInstance[i]->requestId == id)
{
return i;
}
}
return -1L;
}
static void cohGetMain(MmsSignal *sig)
{
long instance = -1L;
int i;
switch (sig->type)
{
case MMS_SIG_COH_GET_CANCEL :
MMS_LOG_I(("MMS FSM COH GET: MMS_SIG_COH_GET_CANCEL %lu\n", sig->u_param1));
for (i = 0; i < MMS_MAX_FSM_GET_INSTANCES; ++i)
{
if (fsmInstance[i] != NULL)
{
if (fsmInstance[i]->msgId == sig->u_param1)
{
getFinished( i, MMS_RESULT_CANCELLED_BY_USER);
}
}
}
break;
case MMS_SIG_COH_GET_MSG :
MMS_LOG_I(("MMS FSM COH GET: MMS_SIG_COH_GET_MSG\n"));
if ((instance = getMsgInit(sig)) != -1L)
{
M_SIGNAL_SENDTO_IUU( M_FSM_COH_WAP, MMS_SIG_COH_WAP_START, instance,
M_FSM_COH_GET, MMS_SIG_COH_GET_CONNECTED);
}
break;
case MMS_SIG_COH_GET_CONNECTED :
MMS_LOG_I(("MMS FSM COH GET: Received MMS_SIG_COH_GET_CONNECTED\n"));
instance = sig->i_param;
if (instance < 0 || instance >= MMS_MAX_FSM_GET_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_GET_RETRY))
{
M_SIGNAL_SENDTO_IUU( M_FSM_COH_WAP, MMS_SIG_COH_WAP_START,
instance, M_FSM_COH_GET, MMS_SIG_COH_GET_CONNECTED);
}
else
{
getFinished( instance, (MmsResult)sig->u_param1);
}
}
else
{
getMsg(instance);
}
break;
case MMS_SIG_COH_GET_DISCONNECTED :
MMS_LOG_I(("MMS FSM COH GET: Received MMS_SIG_COH_GET_DISCONNECTED\n"));
break;
case MMS_SIG_COH_GET_RSP :
MMS_LOG_I(("MMS FSM COH GET: Received MMS_SIG_COH_GET_RSP\n"));
if ((instance = getInstance(sig->i_param)) != -1)
{
handleResponse( instance, sig);
}
break;
case MMS_SIG_COMMON_TIMER_EXPIRED :
MMS_LOG_I(("MMS FSM COH GET: Received TIMER_EXPIRED\n"));
if ((instance = getInstance(sig->i_param)) != -1)
{
getRetry( instance, MMS_RESULT_COMM_TIMEOUT);
}
break;
default:
MMS_LOG_I(("MMS FSM COH GET: received unknown signal\n"));
break;
}
mSignalDelete(sig);
}
void cohGetSetHttpHeader(void)
{
unsigned size;
char *userAgent = cfgGetStr(MMS_CFG_CLIENT_USER_AGENT);
char *extraHeader = cfgGetStr(MMS_CFG_CLIENT_HTTP_HEADER);
if (myHttpHeader != NULL)
{
M_FREE(myHttpHeader);
myHttpHeader = NULL;
}
size = sizeof(MMS_GET_HTTP_HEADER);
if (userAgent != NULL && strlen(userAgent) > 0)
{
size += sizeof(MMS_USER_AGENT) + strlen(userAgent) + sizeof(MMS_NEWLINE);
}
if (extraHeader != NULL && strlen(extraHeader) > 0)
{
size += strlen(extraHeader) + sizeof(MMS_NEWLINE);
}
myHttpHeader = M_ALLOC(size);
if (myHttpHeader == NULL)
{
MMS_LOG_I(("%s(%d): Out of memory\n", __FILE__, __LINE__));
MMSa_error(MMS_RESULT_RESTART_NEEDED);
return;
}
memset( myHttpHeader, 0, size);
if (userAgent != NULL && strlen(userAgent) > 0)
{
if (strlen(myHttpHeader) > 0)
{
strcat( myHttpHeader, "\n");
}
strcat( myHttpHeader, MMS_USER_AGENT);
strcat( myHttpHeader, userAgent);
}
if (extraHeader != NULL && strlen(extraHeader) > 0)
{
if (strlen(myHttpHeader) > 0)
{
strcat( myHttpHeader, "\n");
}
strcat( myHttpHeader, extraHeader);
}
if (strlen(MMS_GET_HTTP_HEADER) > 0)
{
if (strlen(myHttpHeader) > 0)
{
strcat( myHttpHeader, "\n");
}
strcat( myHttpHeader, MMS_GET_HTTP_HEADER);
}
}
void cohGetTerminate(void)
{
long i;
for (i = 0L; i < MMS_MAX_FSM_GET_INSTANCES; ++i)
{
if (fsmInstance[i] != NULL)
{
MMS_LOG_I(("******* GET FSM active at terminate ******* %s %s %lu\n",
fsmInstance[i]->uri == NULL ?
"NULL" : fsmInstance[i]->uri,
fsmInstance[i]->notifTransId == NULL ?
"NULL" : fsmInstance[i]->notifTransId,
fsmInstance[i]->msgId));
deleteInstance(i);
}
}
if (myHttpHeader != NULL)
{
M_FREE(myHttpHeader);
myHttpHeader = NULL;
}
mSignalDeregister(M_FSM_COH_GET);
MMS_LOG_I(("MMS FSM COH GET: terminated\n"));
}
static void deleteInstance(long instance)
{
CohGetInstanceData *fsm;
fsm = fsmInstance[instance];
if (fsm == NULL)
{
return;
}
M_TIMER_RESET_I( M_FSM_COH_GET, instance);
mmsWapCancelHttpOperation( fsmInstance[instance]->requestId);
mmsWapFreeRequestId(fsmInstance[instance]->requestId);
mmsWapFreeContentParams(fsm->cont);
if (fsm->notifTransId != NULL)
{
M_FREE(fsm->notifTransId);
}
M_FREE(fsm->uri);
M_FREE(fsmInstance[instance]);
fsmInstance[instance] = NULL;
M_SIGNAL_SENDTO_IUU( M_FSM_COH_WAP, MMS_SIG_COH_WAP_STOP, 0,
M_FSM_COH_GET, MMS_SIG_COH_GET_DISCONNECTED);
}
static void finalizeData(long instance)
{
unsigned char *theMsg = NULL;
UINT32 size;
MmsResult res = MMS_RESULT_OK;
MmsHeaderValue read;
UINT32 notUsed = 0;
UINT32 remain;
CMN_BOOL permanentError = FALSE;
CMN_BOOL isDifferentTransactionId;
CohGetInstanceData *fsm;
fsm = fsmInstance[instance];
theMsg = M_ALLOC(MMS_MAX_CHUNK_SIZE);
if (fldrmgrGetMessage( CMN_CLIENT_MMS, fsm->msgId, 0L, MMS_MAX_CHUNK_SIZE,
&size, theMsg) != FM_RESULT_OK)
{
MMS_LOG_I(("%s(%d): Couldn't read msg %lu (%lu, %lu)!\n",
__FILE__, __LINE__, fsm->msgId, size, MMS_MAX_CHUNK_SIZE));
res = MMS_RESULT_MESSAGE_EMPTY;
}
else if ( mmsPduUnrecognized( theMsg, size) )
{
MMS_LOG_I(("%s(%d): Unrecognized PDU!\n", __FILE__, __LINE__));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -