/**********************************************************************************
* Copyright Statement:
* -------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of Beijing NetQin Tech. Co., Ltd. (C) 2008
*
***********************************************************************************
*
* Filename :
* ----------------
* nq_soc_core.c
*
* Description :
* ----------------
*
*
* Author :
* ----------------
* yuanzhonghua
*
* Date created :
* ----------------
* 2008/11/20
*
*
*===================================================================
*
* REVISIONS HISTORY
*---------------------------------------------------------------------------------
* Date Username Description of Modification
* -------- ---------- ---------------------------------------------------------
*
*
**********************************************************************************/
#include "nq_soc_types.h"
#include "nq_soc_adp.h"
#include "nq_soc_core.h"
//socket id
#define __NQ_ZH_SET_SOC_ID(socket_index, socketID) do {g_nq_zh_core_soc_cntx_p[socket_index].socketId= (socketID);}while(0)
#define __NQ_ZH_GET_SOC_ID(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].socketId
//socket state
#define __NQ_ZH_SET_SOC_STATE(socket_index, state) do {g_nq_zh_core_soc_cntx_p[socket_index].socStat = (state);}while(0)
#define __NQ_ZH_GET_SOC_STATE(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].socStat
//send
#define __NQ_ZH_SET_SOC_SEND_OFFSET(socket_index, offset) do {g_nq_zh_core_soc_cntx_p[socket_index].send_offset = (offset);}while(0)
#define __NQ_ZH_GET_SOC_SEND_OFFSET(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].send_offset
#define __NQ_ZH_INC_SOC_SEND_OFFSET(socket_index, inc_value) do {g_nq_zh_core_soc_cntx_p[socket_index].send_offset += (inc_value);}while(0)
#define __NQ_ZH_SET_SOC_SEND_BUFFER(socket_index, buffer) do {g_nq_zh_core_soc_cntx_p[socket_index].send_buf = (buffer);}while(0)
#define __NQ_ZH_GET_SOC_SEND_BUFFER(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].send_buf
#define __NQ_ZH_SET_SOC_SEND_LEN(socket_index, len) do{g_nq_zh_core_soc_cntx_p[socket_index].send_len = (len);}while(0)
#define __NQ_ZH_GET_SOC_SEND_LEN(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].send_len
#define __NQ_ZH_GET_SOC_CURR_SEND_POS(socket_index) (__NQ_ZH_GET_SOC_SEND_BUFFER(socket_index) + __NQ_ZH_GET_SOC_SEND_OFFSET(socket_index))
#define __NQ_ZH_GET_SOC_CURR_SEND_REMAIN_LEN(socket_index) (__NQ_ZH_GET_SOC_SEND_LEN(socket_index) - __NQ_ZH_GET_SOC_SEND_OFFSET(socket_index))
//receive
#define __NQ_ZH_SET_SOC_RECV_OFFSET(socket_index, offset) do {g_nq_zh_core_soc_cntx_p[socket_index].recv_offset = (offset);}while(0)
#define __NQ_ZH_GET_SOC_RECV_OFFSET(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].recv_offset
#define __NQ_ZH_INC_SOC_RECV_OFFSET(socket_index, inc_value) do {g_nq_zh_core_soc_cntx_p[socket_index].recv_offset += (inc_value);}while(0)
#define __NQ_ZH_SET_SOC_RECV_BUFFER(socket_index, buffer) do {g_nq_zh_core_soc_cntx_p[socket_index].recv_buf = (buffer);}while(0)
#define __NQ_ZH_GET_SOC_RECV_BUFFER(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].recv_buf
#define __NQ_ZH_SET_SOC_RECV_LEN_MAX(socket_index, len) do{g_nq_zh_core_soc_cntx_p[socket_index].recv_len_max = (len);}while(0)
#define __NQ_ZH_GET_SOC_RECV_LEN_MAX(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].recv_len_max
#define __NQ_ZH_GET_SOC_CURR_RECV_POS(socket_index) (__NQ_ZH_GET_SOC_RECV_BUFFER(socket_index) + __NQ_ZH_GET_SOC_RECV_OFFSET(socket_index))
#define __NQ_ZH_GET_SOC_CURR_RECV_REMAIN_LEN(socket_index) (__NQ_ZH_GET_SOC_RECV_LEN_MAX(socket_index) - __NQ_ZH_GET_SOC_RECV_OFFSET(socket_index))
//socket time id
#define __NQ_ZH_SET_SOC_TIMERID(socket_index, timerid) do{g_nq_zh_core_soc_cntx_p[socket_index].socket_timer = (timerid);}while(0)
#define __NQ_ZH_GET_SOC_TIMERID(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].socket_timer
//socket user callback
#define __NQ_ZH_SET_SOC_FINISHED_FUNC(socket_index, func) do{g_nq_zh_core_soc_cntx_p[socket_index].isFinished= (func);}while(0)
#define __NQ_ZH_GET_SOC_FINISHED_FUNC(socket_index) g_nq_zh_core_soc_cntx_p[socket_index].isFinished
#define __NQ_ZH_CALL_SOC_FINISHED_FUNC(socket_index, buffer, len, flag) g_nq_zh_core_soc_cntx_p[socket_index].isFinished((buffer), (len), (flag))
#define __NQ_ZH_SET_SOC_USERCB_FUNC(socket_index, func) do{g_nq_zh_core_soc_cntx_p[socket_index].userCallback= (func);}while(0)
#define __NQ_ZH_SOC_NOTIFY_USER(socket_index, error_code) \
do \
{ \
nq_zh_core_soc_stop_timer(socket_index); \
if(g_nq_zh_core_soc_cntx_p[socket_index].userCallback) \
{ \
g_nq_zh_core_soc_cntx_p[socket_index].userCallback((error_code)); \
} \
nq_zh_core_destroy(); \
}while(0)
static void nq_zh_core_soc_socket_notify(void* inMsg);
static void nq_zh_core_state_handler(kal_int8 socket_index, kal_int32 err_code, kal_int32 sock_event );
static void nq_zh_core_soc_start_timer(kal_int8 socket_index);
static void nq_zh_core_soc_stop_timer(kal_int8 socket_index);
static nq_zh_soc_context *g_nq_zh_core_soc_cntx_p = NULL;
static kal_bool nq_zh_core_malloc_stat_mem(void)
{
if(NULL == g_nq_zh_core_soc_cntx_p )
{
g_nq_zh_core_soc_cntx_p = (nq_zh_soc_context*)nq_zh_malloc_mem(NQ_ZH_SOC_MAX*sizeof(nq_zh_soc_context));
if(NULL == g_nq_zh_core_soc_cntx_p)
{
return KAL_FALSE;
}
}
return KAL_TRUE;
}
static kal_bool nq_zh_core_free_stat_mem(void)
{
kal_int8 index =0;
if(NULL == g_nq_zh_core_soc_cntx_p )
{
return KAL_FALSE;
}
for(index = 0; index < NQ_ZH_SOC_MAX; index++)
{
if(g_nq_zh_core_soc_cntx_p[index].socketId > NQ_ZH_FAILED)
{
break;
}
}
if(index >= NQ_ZH_SOC_MAX)
{
nq_zh_free_mem(g_nq_zh_core_soc_cntx_p);
g_nq_zh_core_soc_cntx_p = NULL;
nq_zh_soc_set_notify_handler(NULL);
return KAL_TRUE;
}
return KAL_FALSE;
}
static kal_bool nq_zh_core_init(void)
{
kal_int8 index =0;
kal_bool ret = KAL_TRUE;
ret = nq_zh_core_malloc_stat_mem();
if(KAL_FALSE == ret)
{
return KAL_FALSE;
}
for(index = 0; index < NQ_ZH_SOC_MAX; index++)
{
__NQ_ZH_SET_SOC_ID(index, NQ_ZH_FAILED);
__NQ_ZH_SET_SOC_STATE(index, NQ_ZH_SOC_STATE_CLOSE);
__NQ_ZH_SET_SOC_TIMERID(index, nq_zh_soc_get_timer_baseId() + index);
}
return KAL_TRUE;
}
static kal_bool nq_zh_core_destroy(void)
{
return nq_zh_core_free_stat_mem();
}
static kal_int8 nq_zh_core_get_socket_index_by_id(kal_int32 socketId)
{
kal_int8 index =0;
for(index = 0; index < NQ_ZH_SOC_MAX; index++)
{
if(__NQ_ZH_GET_SOC_ID(index) == socketId)
return index;
}
return NQ_ZH_FAILED;
}
static kal_int8 nq_zh_core_get_free_socket_index(void)
{
kal_int8 index = 0;
for(index =0; index < NQ_ZH_SOC_MAX; index++)
{
if(__NQ_ZH_GET_SOC_ID(index) == NQ_ZH_FAILED)
return index;
}
return NQ_ZH_FAILED;
}
void nq_zh_core_deactive_handler(void* inMsg)
{
}
kal_int8 nq_zh_core_createSocket(kal_int8 type)
{
kal_int8 socketid, index, result;
kal_uint8 domain_type, soc_type, protocol_type, val;
kal_uint32 nwk_account_id;
if(g_nq_zh_core_soc_cntx_p == NULL)
{
nq_zh_core_init();
}
index = nq_zh_core_get_free_socket_index();
if(index == NQ_ZH_FAILED)
{
return NQ_ZH_FAILED;
}
nwk_account_id = nq_zh_soc_get_account_id(index);
if(nwk_account_id == 0xffff)
{
return NQ_ZH_FAILED;
}
domain_type = nq_zh_soc_get_domain_type();
soc_type = nq_zh_soc_get_socket_type(type);
protocol_type = 0;
s