PageRenderTime 72ms CodeModel.GetById 2ms app.highlight 61ms RepoModel.GetById 0ms app.codeStats 1ms

/peek-build/m23/g23m-fad/tcpip/socket_api.c

https://bitbucket.org/C0deMaver1ck/peeklinux
C | 1658 lines | 1121 code | 325 blank | 212 comment | 114 complexity | 283744c86590d751da6a488c017d5ce1 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* 
   2+------------------------------------------------------------------------------
   3|  File:       socket.c
   4+------------------------------------------------------------------------------
   5|  Copyright 2002 Texas Instruments Berlin, AG 
   6|                 All rights reserved. 
   7| 
   8|                 This file is confidential and a trade secret of Texas 
   9|                 Instruments Berlin, AG 
  10|                 The receipt of or possession of this file does not convey 
  11|                 any rights to reproduce or disclose its contents or to 
  12|                 manufacture, use, or sell anything it may describe, in 
  13|                 whole, or in part, without the specific written consent of 
  14|                 Texas Instruments Berlin, AG. 
  15+----------------------------------------------------------------------------- 
  16|  Purpose :  This file implements the socket API functionality.
  17|             For a description of this file read g23m\condat\doc\8462_601.doc
  18+----------------------------------------------------------------------------- 
  19*/ 
  20
  21#ifndef SAP_DCM
  22#define SAP_DCM
  23#endif /* !SAP_DCM */
  24
  25#define ENTITY_TCPIP
  26
  27#include <string.h>
  28#include <stdio.h>
  29#include <stdlib.h>
  30
  31#include "socket_api.h"
  32
  33#include "custom.h"
  34#include "pei.h"   
  35#include "socket_int.h"
  36#include "dcm.h"
  37#include "aci_all.h"
  38
  39
  40LOCAL const int UNUSED = 0;
  41
  42/* this function maps the DCM result codes to the Socket-API result codes */
  43LOCAL T_SOCK_RESULT map_dcm_result_code(S32 result)
  44{
  45  switch(result)
  46  {
  47    case DCM_OK:                return SOCK_RESULT_OK;
  48    case DCM_NOT_READY:         return SOCK_RESULT_NOT_READY;
  49    case DCM_ALREADY_ACTIVATED: return SOCK_RESULT_BEARER_ACTIVE;
  50    case DCM_UNKNOWN_EVENT:     return SOCK_RESULT_INTERNAL_ERROR;
  51    case DCM_INVALID_PARAMETER: return SOCK_RESULT_INVALID_PARAMETER;
  52    case DCM_BUSY:              return SOCK_RESULT_IN_PROGRESS;
  53    case DCM_PS_CONN_BROKEN:    return SOCK_RESULT_NETWORK_LOST;
  54    case DCM_NO_NETWORK:        return SOCK_RESULT_BEARER_NOT_READY;
  55    default:                    return SOCK_RESULT_INTERNAL_ERROR;
  56  }
  57}
  58
  59
  60/*******************************************************************************
  61** Function  :   FindSocketData
  62** Parameter :   T_SOCK_API_INSTANCE_DATA*
  63**               T_SOCK_SOCKET
  64** Description : The function returns a pointer to the data structure of a socket.
  65**               If the socket does not exist or api_data is NULL,
  66**               then it returns NULL.
  67*******************************************************************************/
  68LOCAL T_SOCK_SOCKET_DATA* FindSocketData(T_SOCK_API_INSTANCE_DATA* api_data,
  69                                         T_SOCK_SOCKET tcpip_socket)
  70{
  71  T_SOCK_SOCKET_DATA* socket_data;
  72
  73  TRACE_FUNCTION("FindSocketData()");
  74
  75  /* must be checked whether api_data is NULL or not.*/
  76  if(api_data != NULL)       
  77     socket_data = api_data->socket_data;
  78  else
  79  {
  80    TRACE_ERROR("FindSocketData: api_data = NULL!");
  81     return NULL;
  82  }
  83  
  84  TRACE_EVENT_P2("FindSocketData() cached: %08X, search: %08X", socket_data, tcpip_socket);
  85  while((socket_data != NULL) && (tcpip_socket != socket_data->tcpip_socket))
  86  {
  87    socket_data = socket_data->next_socket_data;
  88    TRACE_EVENT_P2("FindSocketData() next: %08X, search: %08X", socket_data, tcpip_socket);
  89  }
  90
  91  TRACE_EVENT_P2("FindSocketData: tcpip_data=%x, ret=%x",tcpip_socket,socket_data );
  92  return socket_data;
  93} /* FindSocketData */
  94
  95
  96
  97/*******************************************************************************
  98** Function  : ReleaseSocketData
  99** Parameter : T_SOCK_API_INSTANCE_DATA*
 100**             T_SOCK_SOCKET_DATA*
 101** Description : The function releases the specified socket data.
 102*******************************************************************************/
 103static void ReleaseSocketData(T_SOCK_API_INSTANCE_DATA* api_data,
 104                              T_SOCK_SOCKET_DATA* socket_data)
 105{
 106  T_SOCK_SOCKET_DATA* temp_socket_data;
 107
 108  TRACE_FUNCTION("ReleaseSocketData()");
 109
 110  /* take socket data from linked list */
 111  if(api_data->socket_data == socket_data)
 112  {
 113    api_data->socket_data = socket_data->next_socket_data;
 114  }
 115  else
 116  {
 117    temp_socket_data = api_data->socket_data;
 118    while(temp_socket_data->next_socket_data != socket_data)
 119    {
 120      temp_socket_data = temp_socket_data->next_socket_data;
 121    }
 122    temp_socket_data->next_socket_data = socket_data->next_socket_data;
 123  }
 124  
 125  /* release socket data  */
 126  MFREE(socket_data);
 127} /* ReleaseSocketData */
 128
 129
 130/******************************************************************************/
 131BOOL sock_api_initialize(T_SOCK_API_INSTANCE *api_instance,
 132                         T_HANDLE app_handle,
 133                         char* app_name)
 134{
 135  T_SOCK_API_INSTANCE_DATA* api_data_p;
 136  T_HANDLE                  hCommAPP;
 137  T_HANDLE                  hCommTCPIP;
 138  T_HANDLE                  hCommDCM;
 139
 140  TRACE_FUNCTION("[Socket API] sock_api_initialize()");
 141
 142  /* open communication handles */
 143  if((hCommTCPIP = vsi_c_open (app_handle, TCPIP_NAME)) < VSI_OK)
 144  {
 145    TRACE_ERROR( "sock_api_initialize: vsi_c_open(TCP) failed");
 146    return FALSE;
 147  }
 148
 149  if((hCommAPP = vsi_c_open (app_handle, app_name)) < VSI_OK)
 150  {
 151    TRACE_ERROR( "sock_api_initialize: vsi_c_open(APP) failed");
 152    return FALSE;
 153  }
 154  
 155  if((hCommDCM = vsi_c_open (app_handle, DCM_NAME)) < VSI_OK)
 156  {
 157    TRACE_ERROR( "sock_api_initialize: vsi_c_open(DCM) failed");
 158    return FALSE;
 159  }
 160  /* initialization successful */
 161  
 162  /* allocate API Data structure */
 163  MALLOC(api_data_p, sizeof(T_SOCK_API_INSTANCE_DATA));
 164  
 165  /*initialize API Data */
 166  api_data_p->app_handle  = app_handle;
 167  api_data_p->hCommAPP    = hCommAPP;
 168  api_data_p->hCommTCPIP  = hCommTCPIP;
 169  api_data_p->hCommDCM    = hCommDCM;
 170  api_data_p->socket_data = NULL;
 171 
 172  /* derefernce 'api_instance' and save address of 'api_data' */
 173  *api_instance = (T_SOCK_API_INSTANCE)api_data_p;
 174  return TRUE;
 175} /* sock_api_initialize */
 176
 177
 178/******************************************************************************/
 179void sock_api_deinitialize(T_SOCK_API_INSTANCE api_instance)
 180{
 181  T_SOCK_API_INSTANCE_DATA* api_data_p;
 182  T_SOCK_SOCKET_DATA*       socket_data1;
 183  T_SOCK_SOCKET_DATA*       socket_data2;
 184
 185  TRACE_FUNCTION("[Socket API] sock_api_deinitialize()");
 186
 187  /* get the address of 'api_data' */
 188  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 189  
 190  /*close communication channels */
 191  vsi_c_close (api_data_p->app_handle, api_data_p->hCommAPP);
 192  vsi_c_close (api_data_p->app_handle, api_data_p->hCommTCPIP);
 193  vsi_c_close (api_data_p->app_handle, api_data_p->hCommDCM);
 194
 195  /* release all socket data */
 196  socket_data1 = api_data_p->socket_data;
 197
 198  while(socket_data1 != NULL)
 199  {
 200    socket_data2 = socket_data1;
 201    socket_data1 = socket_data1->next_socket_data;
 202    MFREE(socket_data2);
 203  }
 204
 205  /* release API data */
 206  MFREE(api_data_p);
 207} 
 208
 209
 210/******************************************************************************/
 211T_SOCK_RESULT sock_open_bearer(T_SOCK_API_INSTANCE api_instance,
 212                               T_SOCK_BEARER_TYPE bearer_select,
 213                               int profile_number,
 214                               T_SOCK_BEARER_INFO *params,
 215                               T_SOCK_CALLBACK sock_cb,
 216                               void *context)
 217{
 218  T_SOCK_API_INSTANCE_DATA* api_data_p;
 219
 220  TRACE_FUNCTION("[Socket API] sock_open_bearer()");
 221  
 222if(sock_cb EQ NULL)
 223  return SOCK_RESULT_INVALID_PARAMETER;
 224
 225  /* set API data pointer */
 226  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 227  api_data_p->context = context;
 228  api_data_p->callback = sock_cb;
 229  
 230  /* send primitive to DCM */
 231  {
 232    PALLOC(dcm_open_conn_req, DCM_OPEN_CONN_REQ);
 233    dcm_open_conn_req->api_instance = api_instance;
 234    dcm_open_conn_req->bearer_select = bearer_select;
 235    dcm_open_conn_req->profile_number = profile_number;
 236
 237    dcm_open_conn_req->dcm_info_conn.bearer_handle = UNUSED;
 238    dcm_open_conn_req->dcm_info_conn.app_handle = params->app_handle;
 239    dcm_open_conn_req->dcm_info_conn.bearer_type = params->bearer_type;
 240    dcm_open_conn_req->dcm_info_conn.apn_valid = params->apn_valid;
 241    memcpy(dcm_open_conn_req->dcm_info_conn.apn, params->apn, SOCK_MAX_APN_LEN+1);
 242    dcm_open_conn_req->dcm_info_conn.phone_number_valid = params->phone_nr_valid;
 243    memcpy(dcm_open_conn_req->dcm_info_conn.phone_number, params->phone_nr,
 244           SOCK_MAX_PHONENUM_LEN+1);
 245    dcm_open_conn_req->dcm_info_conn.user_id_valid = params->user_id_valid;
 246    memcpy(dcm_open_conn_req->dcm_info_conn.user_id, params->user_id,
 247           SOCK_MAX_USERID_LEN+1);
 248    dcm_open_conn_req->dcm_info_conn.password_valid = params->password_valid;
 249    memcpy(dcm_open_conn_req->dcm_info_conn.password, params->password,
 250           SOCK_MAX_PASSWORD_LEN+1);
 251    dcm_open_conn_req->dcm_info_conn.cid = params->cid;
 252    dcm_open_conn_req->dcm_info_conn.ip_address = params->ip_address;
 253    dcm_open_conn_req->dcm_info_conn.dns1 = params->dns1;
 254    dcm_open_conn_req->dcm_info_conn.dns2 = params->dns2;
 255    dcm_open_conn_req->dcm_info_conn.gateway = params->gateway;
 256    dcm_open_conn_req->dcm_info_conn.auth_type = params->authtype;
 257    dcm_open_conn_req->dcm_info_conn.data_compr = params->data_compr;
 258    dcm_open_conn_req->dcm_info_conn.header_compr = params->header_comp;
 259    dcm_open_conn_req->dcm_info_conn.precedence = params->precedence;
 260    dcm_open_conn_req->dcm_info_conn.delay = params->precedence;
 261    dcm_open_conn_req->dcm_info_conn.reliability = params->reliability;
 262    dcm_open_conn_req->dcm_info_conn.peak_throughput = params->peak_throughput;
 263    dcm_open_conn_req->dcm_info_conn.mean_throughput = params->mean_througput;
 264    dcm_open_conn_req->dcm_info_conn.shareable = params->shareable;
 265
 266    PSEND(api_data_p->hCommDCM, dcm_open_conn_req);
 267  }
 268  return SOCK_RESULT_OK;
 269}
 270
 271/******************************************************************************/
 272LOCAL void sock_open_bearer_cnf(T_DCM_OPEN_CONN_CNF *dcm_open_conn_cnf,
 273                                      T_SOCK_API_INSTANCE api_instance)
 274{
 275  T_SOCK_OPEN_BEARER_CNF sock_open_bearer_cnf;
 276  T_SOCK_API_INSTANCE_DATA *api_data_p;
 277
 278  TRACE_FUNCTION("[Socket API] sock_open_bearer_cnf()");
 279
 280  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 281  
 282  /* fill signal struct */
 283  sock_open_bearer_cnf.result = map_dcm_result_code(dcm_open_conn_cnf->result);
 284  sock_open_bearer_cnf.socket = UNUSED;
 285  sock_open_bearer_cnf.event_type = SOCK_OPEN_BEARER_CNF;
 286  sock_open_bearer_cnf.bearer_handle = dcm_open_conn_cnf->bearer_handle;
 287
 288  /* release primitive */
 289  PFREE(dcm_open_conn_cnf);
 290  
 291  api_data_p->callback((T_SOCK_EVENTSTRUCT *)&sock_open_bearer_cnf,
 292                       api_data_p->context);
 293}
 294
 295/******************************************************************************/
 296T_SOCK_RESULT sock_close_bearer(T_SOCK_API_INSTANCE api_instance,
 297                                T_SOCK_BEARER_HANDLE bearer_handle,
 298                                T_SOCK_CALLBACK sock_cb,
 299                                void *context)
 300{
 301  T_SOCK_API_INSTANCE_DATA* api_data_p;
 302
 303  TRACE_FUNCTION("[Socket API] sock_close_bearer()");
 304
 305  if(sock_cb EQ NULL) {
 306   return SOCK_RESULT_INVALID_PARAMETER;
 307  }
 308
 309  /* set API data pointer */
 310  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 311  api_data_p->callback = sock_cb;
 312  api_data_p->context = context;
 313
 314  /* send primitive to DCM */
 315  {
 316    PALLOC(dcm_close_conn_req, DCM_CLOSE_CONN_REQ);
 317    dcm_close_conn_req->api_instance = api_instance;
 318    dcm_close_conn_req->bearer_handle = bearer_handle;
 319    PSEND(api_data_p->hCommDCM, dcm_close_conn_req);
 320  }
 321  
 322  return SOCK_RESULT_OK;
 323}
 324
 325/******************************************************************************/
 326LOCAL void sock_close_bearer_cnf(T_DCM_CLOSE_CONN_CNF *dcm_close_conn_cnf,
 327                                        T_SOCK_API_INSTANCE api_instance)
 328{
 329  T_SOCK_CLOSE_BEARER_CNF sock_close_bearer_cnf;
 330  T_SOCK_API_INSTANCE_DATA *api_data_p;
 331
 332  TRACE_FUNCTION("[Socket API] dcm_close_conn_cnf()");
 333
 334  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 335
 336  sock_close_bearer_cnf.result = map_dcm_result_code(dcm_close_conn_cnf->result);
 337  sock_close_bearer_cnf.socket = UNUSED;
 338  sock_close_bearer_cnf.event_type = SOCK_CLOSE_BEARER_CNF;
 339
 340  /* release primitive */
 341  PFREE(dcm_close_conn_cnf);
 342
 343  /* call callback function */
 344  api_data_p->callback((T_SOCK_EVENTSTRUCT *)&sock_close_bearer_cnf,
 345                       api_data_p->context);
 346}
 347
 348/******************************************************************************/
 349T_SOCK_RESULT sock_bearer_info(T_SOCK_API_INSTANCE api_instance,
 350                               T_SOCK_BEARER_HANDLE bearer_handle,
 351                               T_SOCK_CALLBACK sock_cb,
 352                               void *context)
 353{
 354  T_SOCK_API_INSTANCE_DATA* api_data_p;
 355
 356  TRACE_FUNCTION("[Socket API] sock_bearer_info()");
 357
 358  if(sock_cb EQ NULL)
 359   return SOCK_RESULT_INVALID_PARAMETER;
 360
 361  /* set API data pointer */
 362  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 363  api_data_p->callback = sock_cb;
 364  api_data_p->context = context;
 365
 366  /* send primitive to DCM */
 367  {
 368    PALLOC(dcm_get_current_conn_req, DCM_GET_CURRENT_CONN_REQ);
 369    dcm_get_current_conn_req->api_instance = api_instance;
 370    dcm_get_current_conn_req->bearer_handle = bearer_handle;
 371    PSEND(api_data_p->hCommDCM, dcm_get_current_conn_req);
 372  }
 373  return SOCK_RESULT_OK;
 374}
 375
 376
 377/******************************************************************************/
 378LOCAL void sock_bearer_info_cnf(T_DCM_GET_CURRENT_CONN_CNF *current_conn_cnf,
 379                                      T_SOCK_API_INSTANCE api_instance)
 380{
 381  T_SOCK_BEARER_INFO_CNF bearer_info;
 382  T_SOCK_API_INSTANCE_DATA *api_data_p;
 383
 384  TRACE_FUNCTION("[Socket API] sock_bearer_info_cnf()");
 385
 386  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 387
 388  bearer_info.result = map_dcm_result_code(current_conn_cnf->result);
 389  bearer_info.socket = UNUSED;
 390  bearer_info.event_type = SOCK_BEARER_INFO_CNF;
 391
 392  bearer_info.bearer_params.bearer_handle = current_conn_cnf->dcm_info_conn.bearer_handle;
 393  bearer_info.bearer_params.app_handle = current_conn_cnf->dcm_info_conn.app_handle;
 394  bearer_info.bearer_params.bearer_type =(T_SOCK_BEARER_TYPE)current_conn_cnf->dcm_info_conn.bearer_type;
 395  bearer_info.bearer_params.apn_valid = current_conn_cnf->dcm_info_conn.apn_valid;
 396  memcpy(bearer_info.bearer_params.apn,
 397         current_conn_cnf->dcm_info_conn.apn, SOCK_MAX_APN_LEN);
 398  bearer_info.bearer_params.phone_nr_valid = current_conn_cnf->dcm_info_conn.phone_number_valid;
 399  memcpy(bearer_info.bearer_params.phone_nr,current_conn_cnf->dcm_info_conn.phone_number,
 400         SOCK_MAX_PHONENUM_LEN);
 401  bearer_info.bearer_params.user_id_valid = current_conn_cnf->dcm_info_conn.user_id_valid;
 402  memcpy(bearer_info.bearer_params.user_id, current_conn_cnf->dcm_info_conn.user_id,
 403         SOCK_MAX_USERID_LEN);
 404  bearer_info.bearer_params.password_valid = current_conn_cnf->dcm_info_conn.password_valid;
 405  memcpy(bearer_info.bearer_params.password, current_conn_cnf->dcm_info_conn.password,
 406         SOCK_MAX_PASSWORD_LEN);
 407  bearer_info.bearer_params.cid = current_conn_cnf->dcm_info_conn.cid;
 408  bearer_info.bearer_params.ip_address = current_conn_cnf->dcm_info_conn.ip_address;
 409  bearer_info.bearer_params.dns1 = current_conn_cnf->dcm_info_conn.dns1;
 410  bearer_info.bearer_params.dns2 = current_conn_cnf->dcm_info_conn.dns2;
 411  bearer_info.bearer_params.gateway = current_conn_cnf->dcm_info_conn.gateway;
 412  bearer_info.bearer_params.authtype = (T_SOCK_AUTHTYPE)current_conn_cnf->dcm_info_conn.auth_type;
 413  bearer_info.bearer_params.data_compr = current_conn_cnf->dcm_info_conn.data_compr;
 414  bearer_info.bearer_params.header_comp = current_conn_cnf->dcm_info_conn.header_compr;
 415  bearer_info.bearer_params.precedence = current_conn_cnf->dcm_info_conn.precedence;
 416  bearer_info.bearer_params.delay = current_conn_cnf->dcm_info_conn.delay;
 417  bearer_info.bearer_params.reliability = current_conn_cnf->dcm_info_conn.reliability;
 418  bearer_info.bearer_params.peak_throughput = current_conn_cnf->dcm_info_conn.peak_throughput;
 419  bearer_info.bearer_params.mean_througput = current_conn_cnf->dcm_info_conn.mean_throughput;
 420  bearer_info.bearer_params.shareable = current_conn_cnf->dcm_info_conn.shareable;
 421
 422  /* release primitive */
 423  PFREE(current_conn_cnf);
 424
 425  /* call callback function */
 426  api_data_p->callback((T_SOCK_EVENTSTRUCT *)&bearer_info, api_data_p->context);
 427}
 428
 429
 430/******************************************************************************/
 431LOCAL void sock_dcm_error_ind(T_DCM_ERROR_IND *dcm_error_ind,
 432                                    T_SOCK_API_INSTANCE api_instance)
 433{
 434  T_SOCK_BAERER_CLOSED_IND sock_bearer_closed_ind;
 435  T_SOCK_API_INSTANCE_DATA *api_data_p;
 436
 437  TRACE_FUNCTION("[Socket API] sock_dcm_error_ind()");
 438
 439  api_data_p = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 440
 441  sock_bearer_closed_ind.event_type = SOCK_BAERER_CLOSED_IND;
 442  sock_bearer_closed_ind.result = map_dcm_result_code(dcm_error_ind->result);
 443  sock_bearer_closed_ind.socket = UNUSED;
 444  sock_bearer_closed_ind.dcm_error = dcm_error_ind->dcm_err;
 445
 446  PFREE(dcm_error_ind);
 447  
 448  if(api_data_p->callback NEQ NULL)
 449  {
 450    api_data_p->callback((T_SOCK_EVENTSTRUCT *)&sock_bearer_closed_ind,
 451                         api_data_p->context);
 452  }
 453}
 454
 455
 456/* ******************* Socket related functions ***************************** */
 457
 458/******************************************************************************/
 459T_SOCK_RESULT sock_create(T_SOCK_API_INSTANCE api_instance,
 460                          T_SOCK_IPPROTO ipproto,
 461                          T_SOCK_CALLBACK callback,
 462                          void* context)
 463{
 464  T_SOCK_API_INSTANCE_DATA* api_data;
 465  T_SOCK_SOCKET_DATA*       socket_data;
 466
 467  TRACE_FUNCTION("[Socket API] sock_create()");
 468  TRACE_EVENT_P1("IP-Protocol=%d",(U32)ipproto);
 469  
 470  /* set api_data  */
 471  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 472  
 473 if(callback EQ NULL)
 474  return SOCK_RESULT_INVALID_PARAMETER;
 475
 476 /* check for correct data */
 477  if((ipproto != SOCK_IPPROTO_UDP) && (ipproto != SOCK_IPPROTO_TCP))
 478  {
 479    TRACE_ERROR("[Socket API] Error: Invalid IP-Protocol");
 480    return SOCK_RESULT_INVALID_PARAMETER;
 481  } 
 482  if( api_data == NULL )
 483  {
 484    TRACE_ERROR("[Socket API] Error: api_instance == NULL!");
 485    return SOCK_RESULT_INVALID_PARAMETER;
 486  }
 487
 488  /* allocate socket data */
 489  MALLOC(socket_data, sizeof(T_SOCK_SOCKET_DATA));
 490  
 491  /* put socket data in linked list */
 492  socket_data->next_socket_data = api_data->socket_data;
 493  api_data->socket_data         = socket_data;
 494  
 495  /* initialize socket data */
 496  socket_data->api_data        = api_data;
 497  socket_data->tcpip_socket    = 0;
 498  socket_data->callback        = callback;
 499  socket_data->context         = context;
 500  socket_data->rx_flow_control = SOCK_FLOW_XON;
 501  socket_data->tx_flow_control = SOCK_FLOW_XON;
 502  socket_data->rx_window       = 1;
 503  socket_data->tx_window       = 1;
 504  
 505  /* send primitive to TCPIP */
 506  {
 507    PALLOC(tcpip_create_req_prim, TCPIP_CREATE_REQ);
 508
 509    tcpip_create_req_prim->app_handle = api_data->hCommAPP;
 510    tcpip_create_req_prim->ipproto    = ipproto;
 511    tcpip_create_req_prim->request_id = (T_SOCK_SOCKET)socket_data;
 512
 513    PSEND(api_data->hCommTCPIP, tcpip_create_req_prim);
 514  }
 515
 516  return SOCK_RESULT_OK;
 517}/* sock_create */
 518
 519
 520/*****************************************************************************************
 521** Function  : sock_create_cnf
 522** Parameter : T_TCPIP_CREATE_CNF *
 523**             T_SOCK_API_INSTANCE ' this value will be ignored in this function
 524**             because you can find socket_data only using request_id
 525*****************************************************************************************/
 526LOCAL void sock_create_cnf(T_TCPIP_CREATE_CNF  *tcpip_create_cnf,
 527                                  T_SOCK_API_INSTANCE api_instance)
 528{
 529  T_SOCK_SOCKET_DATA* socket_data;
 530  T_SOCK_CREATE_CNF   sock_create_cnf;
 531
 532  TRACE_FUNCTION("[Socket API] sock_create_cnf()");
 533
 534  /* set socket data */
 535  socket_data = (T_SOCK_SOCKET_DATA*)tcpip_create_cnf->request_id;
 536  
 537  /* set received values */
 538  socket_data->tcpip_socket = tcpip_create_cnf->socket;
 539  
 540  /* fill confirm struct */
 541  sock_create_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_create_cnf->event_type;
 542  sock_create_cnf.result     = (T_SOCK_RESULT)tcpip_create_cnf->result;
 543  sock_create_cnf.socket     = (T_SOCK_SOCKET)socket_data;
 544  
 545  socket_data->callback((T_SOCK_EVENTSTRUCT *)&sock_create_cnf, socket_data->context);
 546
 547  /* release socket data if context creation was not successful */
 548  if(tcpip_create_cnf->result != TCPIP_RESULT_OK)
 549  {
 550    TRACE_ERROR("[Socket API] Error: sock_create() failed");
 551    ReleaseSocketData(socket_data->api_data, socket_data);
 552  }
 553  
 554  PFREE(tcpip_create_cnf);
 555}/* sock_create_cnf */
 556
 557
 558/******************************************************************************/
 559T_SOCK_RESULT sock_close(T_SOCK_SOCKET socket)
 560{
 561  T_SOCK_API_INSTANCE_DATA*  api_data;
 562  T_SOCK_SOCKET_DATA* sock_data;
 563
 564  TRACE_FUNCTION("[Socket API] sock_close()");
 565
 566  sock_data = (T_SOCK_SOCKET_DATA*)socket;
 567  if(sock_data == NULL)
 568  {
 569    return SOCK_RESULT_INVALID_PARAMETER;
 570  }
 571
 572  api_data = sock_data->api_data;
 573   
 574  /* send primitive to TCPIP */
 575  {
 576    PALLOC(tcpip_close_req_prim, TCPIP_CLOSE_REQ);
 577    tcpip_close_req_prim->app_handle = api_data->hCommAPP;
 578    tcpip_close_req_prim->socket = sock_data->tcpip_socket;
 579    PSEND(api_data->hCommTCPIP, tcpip_close_req_prim);
 580  }
 581  return SOCK_RESULT_OK;
 582}/* sock_close */
 583
 584
 585/*******************************************************************************
 586** Function  : sock_close_cnf
 587** Parameter : T_SOCK_CLOSE_CNF *
 588*******************************************************************************/
 589LOCAL void sock_close_cnf(T_TCPIP_CLOSE_CNF *tcpip_close_cnf,
 590                                 T_SOCK_API_INSTANCE api_instance)
 591{
 592  T_SOCK_SOCKET_DATA* sock_data;
 593  T_SOCK_CLOSE_CNF   sock_close_cnf;
 594  T_SOCK_API_INSTANCE_DATA *api_data;
 595
 596  TRACE_FUNCTION("[Socket API] sock_close_cnf()");
 597
 598  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 599
 600  sock_data =  FindSocketData(api_data ,tcpip_close_cnf->socket);
 601  if(sock_data == NULL)
 602  {
 603    PFREE(tcpip_close_cnf);
 604    return;
 605  }
 606
 607  /* fill confirm struct */
 608  sock_close_cnf.event_type  = (T_SOCK_EVENTTYPE)tcpip_close_cnf->event_type;
 609  sock_close_cnf.result      = (T_SOCK_RESULT)tcpip_close_cnf->result;
 610  sock_close_cnf.socket      = (T_SOCK_SOCKET)sock_data;
 611  
 612  sock_data->callback(&sock_close_cnf, sock_data->context);
 613
 614  /* release socket data if sock closing was successful */
 615  if(tcpip_close_cnf->result == TCPIP_RESULT_OK)
 616  {
 617    ReleaseSocketData(sock_data->api_data, sock_data);
 618  }
 619  
 620  PFREE(tcpip_close_cnf);
 621}/* sock_close_cnf */
 622
 623
 624/******************************************************************************/
 625T_SOCK_RESULT sock_bind(T_SOCK_SOCKET socket, T_SOCK_PORT  port)
 626{
 627  T_SOCK_API_INSTANCE_DATA* api_data;
 628  T_SOCK_SOCKET_DATA *sock_data;
 629
 630  TRACE_FUNCTION("[Socket API] sock_bind()");
 631
 632  sock_data = (T_SOCK_SOCKET_DATA*)socket;
 633  if(sock_data == NULL)
 634  {
 635    return SOCK_RESULT_INVALID_PARAMETER;
 636  }
 637
 638  api_data = sock_data->api_data;
 639  {
 640    PALLOC (tcpip_bind_req_prim, TCPIP_BIND_REQ);
 641    tcpip_bind_req_prim->app_handle = api_data->hCommAPP;
 642    tcpip_bind_req_prim->socket     = sock_data->tcpip_socket;
 643    tcpip_bind_req_prim->port       = port;
 644    PSEND(api_data->hCommTCPIP, tcpip_bind_req_prim);
 645  }
 646  return SOCK_RESULT_OK;
 647}/* sock_bind */
 648
 649
 650/*******************************************************************************
 651** Function  : sock_bind_cnf
 652** Parameter : T_SOCK_BIND_CNF *
 653**                   T_SOCK_API_INSTANCE
 654*******************************************************************************/
 655LOCAL void sock_bind_cnf(T_TCPIP_BIND_CNF  *tcpip_bind_cnf,
 656                                T_SOCK_API_INSTANCE api_instance)
 657{
 658  T_SOCK_SOCKET_DATA *sock_data;
 659  T_SOCK_BIND_CNF sock_bind_cnf;
 660  T_SOCK_API_INSTANCE_DATA *api_data;
 661
 662  TRACE_FUNCTION("[Socket API] sock_bind_cnf()");
 663
 664  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 665  sock_data =  FindSocketData(api_data ,tcpip_bind_cnf->socket);
 666  if(sock_data == NULL)
 667  {
 668    PFREE(tcpip_bind_cnf);
 669    return;
 670  }
 671
 672  /* fill confirm struct */
 673  sock_bind_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_bind_cnf->event_type;
 674  sock_bind_cnf.result     = (T_SOCK_RESULT)tcpip_bind_cnf->result; 
 675  sock_bind_cnf.socket     = (T_SOCK_SOCKET)sock_data;
 676
 677  sock_data->callback(&sock_bind_cnf,sock_data->context);
 678
 679  PFREE(tcpip_bind_cnf);
 680}/* sock_bind_cnf */
 681
 682
 683/******************************************************************************/
 684T_SOCK_RESULT sock_listen(T_SOCK_SOCKET socket )
 685{
 686  T_SOCK_API_INSTANCE_DATA* api_data;
 687  T_SOCK_SOCKET_DATA* sock_data;
 688    
 689  TRACE_FUNCTION("[Socket API] sock_listen()");
 690    
 691  sock_data = (T_SOCK_SOCKET_DATA*)socket;
 692  if(sock_data == NULL)
 693  {
 694    TRACE_ERROR("[Socket API] listen() error: Invalid socket data");
 695    return SOCK_RESULT_INVALID_PARAMETER;
 696  }
 697
 698  api_data   = sock_data->api_data;
 699  {
 700    PALLOC (tcpip_listen_req_prim, TCPIP_LISTEN_REQ);
 701    tcpip_listen_req_prim->app_handle = api_data->hCommAPP;
 702    tcpip_listen_req_prim->socket = sock_data->tcpip_socket;
 703    PSEND(api_data->hCommTCPIP,tcpip_listen_req_prim );
 704  }
 705	return SOCK_RESULT_OK;
 706}/* sock_listen */
 707
 708
 709/*******************************************************************************
 710** Function  : sock_listen_cnf
 711** Parameter : T_SOCK_LISTEN_CNF *
 712**             T_SOCK_API_INSTANCE
 713*******************************************************************************/
 714LOCAL void sock_listen_cnf(T_TCPIP_LISTEN_CNF *tcpip_listen_cnf,
 715                                 T_SOCK_API_INSTANCE api_instance )
 716{
 717  T_SOCK_SOCKET_DATA *sock_data;
 718  T_SOCK_LISTEN_CNF sock_listen_cnf;
 719  T_SOCK_API_INSTANCE_DATA *api_data;
 720
 721  TRACE_FUNCTION("[Socket API] sock_listen_cnf()");
 722
 723  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 724  sock_data = FindSocketData(api_data ,tcpip_listen_cnf->socket);
 725  if(sock_data == NULL)
 726  {
 727    PFREE(tcpip_listen_cnf);
 728    return;
 729  }
 730
 731  /* fill confirm struct */
 732  sock_listen_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_listen_cnf->event_type;
 733  sock_listen_cnf.result     = (T_SOCK_RESULT)tcpip_listen_cnf->result; 
 734  sock_listen_cnf.socket     = (T_SOCK_SOCKET)sock_data;
 735
 736  sock_data->callback(&sock_listen_cnf,sock_data->context);
 737
 738  PFREE(tcpip_listen_cnf);
 739} /* sock_listen_cnf */
 740
 741
 742/******************************************************************************/
 743T_SOCK_RESULT sock_connect(T_SOCK_SOCKET socket,
 744                           T_SOCK_IPADDR ipaddr,
 745                           T_SOCK_PORT port)
 746{
 747  T_SOCK_API_INSTANCE_DATA* api_data;
 748  T_SOCK_SOCKET_DATA* sock_data;
 749
 750  TRACE_FUNCTION("sock_connect()");
 751
 752  sock_data = (T_SOCK_SOCKET_DATA*)socket;
 753  if(sock_data == NULL)
 754  {
 755      TRACE_ERROR("[Socket API] connect error: Invalid socket data");
 756      return SOCK_RESULT_INVALID_PARAMETER;
 757  }
 758
 759  api_data = sock_data->api_data;
 760  {
 761    PALLOC (tcpip_connect_req_prim, TCPIP_CONNECT_REQ);
 762    tcpip_connect_req_prim->app_handle = api_data->hCommAPP;
 763    tcpip_connect_req_prim->socket = sock_data->tcpip_socket;
 764    tcpip_connect_req_prim->ipaddr = ipaddr;
 765    tcpip_connect_req_prim->port   = port;
 766    PSEND(api_data->hCommTCPIP, tcpip_connect_req_prim);
 767  }
 768  return SOCK_RESULT_OK;
 769} /* sock_connect */
 770
 771
 772/*******************************************************************************
 773** Function  : sock_connect_cnf
 774** Parameter : T_SOCK_CONNECT_CNF *
 775**             T_SOCK_API_INSTANCE
 776*******************************************************************************/
 777LOCAL void sock_connect_cnf(T_TCPIP_CONNECT_CNF *tcpip_connect_cnf,
 778                                   T_SOCK_API_INSTANCE api_instance )
 779{
 780  T_SOCK_SOCKET_DATA *sock_data;
 781  T_SOCK_CONNECT_CNF sock_connect_cnf;
 782  T_SOCK_API_INSTANCE_DATA *api_data;
 783
 784  TRACE_FUNCTION("sock_connect_cnf()");
 785
 786  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 787
 788  TRACE_EVENT_P3("api_data %d, socket %d, reslut %d",api_data,
 789                 tcpip_connect_cnf->socket,tcpip_connect_cnf->result);
 790
 791  sock_data =  FindSocketData(api_data ,tcpip_connect_cnf->socket);
 792  if(sock_data == NULL)
 793  {
 794    TRACE_EVENT("sock data NULL !!!");
 795    PFREE(tcpip_connect_cnf);
 796    return;
 797  }
 798
 799  /* fill confirm struct */
 800  sock_connect_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_connect_cnf->event_type;
 801  sock_connect_cnf.result     = (T_SOCK_RESULT)tcpip_connect_cnf->result; 
 802  sock_connect_cnf.socket     = (T_SOCK_SOCKET)sock_data;
 803
 804  sock_data->callback(&sock_connect_cnf,sock_data->context);
 805
 806  PFREE(tcpip_connect_cnf);
 807} /* sock_connect_cnf */
 808
 809
 810/******************************************************************************/
 811T_SOCK_RESULT sock_getsockname(T_SOCK_SOCKET socket)
 812{
 813  T_SOCK_API_INSTANCE_DATA* api_data;
 814  T_SOCK_SOCKET_DATA* sock_data;
 815
 816  TRACE_FUNCTION("sock_getsockname()");
 817
 818  sock_data = (T_SOCK_SOCKET_DATA*)socket;
 819  if(sock_data == NULL)
 820  {
 821    TRACE_ERROR("[Socket API] getsockname() error: Invalid socket data");
 822    return SOCK_RESULT_INVALID_PARAMETER;
 823  }
 824    api_data   = sock_data->api_data;
 825
 826  {
 827    PALLOC(tcpip_sockname_req_prim, TCPIP_SOCKNAME_REQ);
 828    tcpip_sockname_req_prim->app_handle = api_data->hCommAPP;
 829    tcpip_sockname_req_prim->socket = sock_data->tcpip_socket;
 830    PSEND(api_data->hCommTCPIP, tcpip_sockname_req_prim);
 831  }
 832  return SOCK_RESULT_OK;
 833} /* sock_getsockname */
 834
 835
 836
 837/*******************************************************************************
 838** Function  : sock_connect_cnf
 839** Parameter : T_SOCK_SOCKNAME_CNF *
 840**             T_SOCK_API_INSTANCE
 841*******************************************************************************/
 842LOCAL void sock_sockname_cnf(T_TCPIP_SOCKNAME_CNF *tcpip_sockname_cnf,
 843                                    T_SOCK_API_INSTANCE api_instance )
 844{
 845  T_SOCK_SOCKET_DATA *sock_data;
 846  T_SOCK_SOCKNAME_CNF sock_sockname_cnf;
 847  T_SOCK_API_INSTANCE_DATA *api_data;
 848
 849  TRACE_FUNCTION("sock_getsockname_cnf()");
 850
 851  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 852
 853  sock_data =  FindSocketData(api_data , tcpip_sockname_cnf->socket);
 854  if(sock_data == NULL)
 855  {
 856    PFREE(tcpip_sockname_cnf);
 857    return;
 858  }
 859
 860  /* fill confirm struct */
 861  sock_sockname_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_sockname_cnf->event_type;
 862  sock_sockname_cnf.result     = (T_SOCK_RESULT)tcpip_sockname_cnf->result; 
 863  sock_sockname_cnf.socket     = (T_SOCK_SOCKET)sock_data;
 864  sock_sockname_cnf.ipaddr     = tcpip_sockname_cnf->ipaddr;
 865  sock_sockname_cnf.port       = tcpip_sockname_cnf->port;
 866
 867  sock_data->callback((T_SOCK_EVENTSTRUCT*)&sock_sockname_cnf,sock_data->context);
 868
 869  PFREE(tcpip_sockname_cnf);
 870} /* sock_sockname_cnf */
 871
 872
 873/******************************************************************************/
 874T_SOCK_RESULT sock_getpeername(T_SOCK_SOCKET socket)
 875{
 876  T_SOCK_API_INSTANCE_DATA* api_data;
 877  T_SOCK_SOCKET_DATA* sock_data;
 878
 879  TRACE_FUNCTION("sock_getpeername()");
 880
 881  sock_data = (T_SOCK_SOCKET_DATA*)socket;
 882  api_data   = sock_data->api_data;
 883  
 884  if((sock_data == NULL) OR (api_data == NULL))
 885  {
 886    TRACE_ERROR("[Socket API] getpeername() error: Invalid socket data");
 887    return SOCK_RESULT_INVALID_PARAMETER;
 888  }
 889
 890  {
 891    PALLOC (tcpip_peername_req_prim, TCPIP_PEERNAME_REQ);
 892    tcpip_peername_req_prim->app_handle = api_data->hCommAPP;
 893    tcpip_peername_req_prim->socket = sock_data->tcpip_socket;
 894    PSEND(api_data->hCommTCPIP, tcpip_peername_req_prim);
 895  }
 896  return SOCK_RESULT_OK;
 897} /* sock_getpeername */
 898
 899
 900/*******************************************************************************
 901** Function  : sock_peername_cnf
 902** Parameter : T_SOCK_PEERNAME_CNF *
 903**                   T_SOCK_API_INSTANCE
 904*******************************************************************************/
 905LOCAL void sock_peername_cnf(T_TCPIP_PEERNAME_CNF *tcpip_peername_cnf,
 906                                   T_SOCK_API_INSTANCE api_instance )
 907{
 908  T_SOCK_SOCKET_DATA *sock_data;
 909  T_SOCK_PEERNAME_CNF sock_peername_cnf;
 910  T_SOCK_API_INSTANCE_DATA *api_data;
 911
 912  TRACE_FUNCTION("sock_getpeername_cnf()");
 913
 914  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 915
 916  sock_data =  FindSocketData(api_data ,tcpip_peername_cnf->socket);
 917  if(sock_data == NULL)
 918  {
 919    PFREE(tcpip_peername_cnf);
 920    return;
 921  }
 922
 923  sock_peername_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_peername_cnf->event_type;
 924  sock_peername_cnf.result     = (T_SOCK_RESULT)tcpip_peername_cnf->result; 
 925  sock_peername_cnf.socket     = (T_SOCK_SOCKET)sock_data;
 926  sock_peername_cnf.ipaddr     = tcpip_peername_cnf->ipaddr;
 927  sock_peername_cnf.port       = tcpip_peername_cnf->port;
 928   
 929  sock_data->callback((T_SOCK_EVENTSTRUCT*)&sock_peername_cnf,sock_data->context);
 930
 931  PFREE(tcpip_peername_cnf);
 932} /* sock_peername_cnf */
 933
 934
 935/******************************************************************************/
 936T_SOCK_RESULT sock_gethostbyname(T_SOCK_API_INSTANCE api_instance,
 937                                 char* hostname, 
 938                                 T_SOCK_CALLBACK callback,
 939                                 void* context)
 940{
 941  T_SOCK_API_INSTANCE_DATA* api_data;
 942  T_SOCK_SOCKET_DATA*       socket_data;
 943
 944  TRACE_FUNCTION("sock_gethostbyname()");
 945  TRACE_EVENT_P1("hostname: %s",hostname);
 946
 947  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
 948
 949  if(callback EQ NULL)
 950   return SOCK_RESULT_INVALID_PARAMETER;
 951
 952  if(api_data == NULL)
 953  {
 954    TRACE_ERROR("[Socket API] gethostbyname() error: Invalid socket data");
 955    return SOCK_RESULT_INVALID_PARAMETER;
 956  }
 957  
 958  MALLOC(socket_data, sizeof(T_SOCK_SOCKET_DATA));
 959  
 960  /* put socket data in linked list */
 961  socket_data->next_socket_data = api_data->socket_data;
 962  api_data->socket_data         = socket_data;
 963  
 964  /* initialize socket data */
 965  socket_data->api_data        = api_data;
 966  socket_data->tcpip_socket    = 0;
 967  socket_data->callback        = callback;
 968  socket_data->context         = context;
 969  socket_data->rx_flow_control = SOCK_FLOW_XON; 
 970  socket_data->tx_flow_control = SOCK_FLOW_XON;
 971  socket_data->rx_window       = 1;
 972  socket_data->tx_window       = 1;
 973
 974  {
 975    PALLOC (tcpip_hostinfo_req_prim , TCPIP_HOSTINFO_REQ);
 976    tcpip_hostinfo_req_prim->app_handle = api_data->hCommAPP;
 977    tcpip_hostinfo_req_prim->ipaddr = 0;
 978    strcpy((char*)tcpip_hostinfo_req_prim->hostname, hostname);
 979    tcpip_hostinfo_req_prim->request_id = (T_SOCK_SOCKET)socket_data;     
 980    PSEND(api_data->hCommTCPIP,tcpip_hostinfo_req_prim);
 981  }
 982  return SOCK_RESULT_OK;
 983} /* sock_gethostbyname */
 984
 985
 986/*******************************************************************************
 987** Function  : sock_hostinfo_cnf
 988** Parameter : T_SOCK_HOSTINFO_CNF *
 989**             T_SOCK_API_INSTANCE    this value will be ignored ...
 990**                                    because you can find sock_data using request_id.
 991*******************************************************************************/
 992LOCAL void sock_hostinfo_cnf(T_TCPIP_HOSTINFO_CNF* tcpip_hostinfo_cnf , 
 993                                       T_SOCK_API_INSTANCE api_instance )
 994{
 995  T_SOCK_SOCKET_DATA* socket_data;
 996  T_SOCK_HOSTINFO_CNF  sock_hostinfo_cnf;
 997
 998  socket_data = (T_SOCK_SOCKET_DATA*)tcpip_hostinfo_cnf->request_id;
 999  
1000  /* set received values */
1001  socket_data->tcpip_socket = tcpip_hostinfo_cnf->socket;
1002  
1003  /* fill confirm struct */
1004  sock_hostinfo_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_hostinfo_cnf->event_type;
1005  sock_hostinfo_cnf.result     = (T_SOCK_RESULT)tcpip_hostinfo_cnf->result;
1006  sock_hostinfo_cnf.socket     = (T_SOCK_SOCKET)socket_data;
1007  sock_hostinfo_cnf.ipaddr     =  tcpip_hostinfo_cnf->ipaddr;
1008  strcpy(sock_hostinfo_cnf.hostname,(char*)tcpip_hostinfo_cnf->hostname);
1009
1010  socket_data->callback((T_SOCK_EVENTSTRUCT*)&sock_hostinfo_cnf, socket_data->context);
1011
1012  ReleaseSocketData(socket_data->api_data, socket_data);
1013  
1014  PFREE(tcpip_hostinfo_cnf);
1015} /* sock_hostinfo_cnf */
1016
1017
1018/******************************************************************************/
1019T_SOCK_RESULT sock_send(T_SOCK_SOCKET socket, char* buffer, U16 buffer_length)
1020{
1021  T_SOCK_API_INSTANCE_DATA* api_data;
1022  T_SOCK_SOCKET_DATA* sock_data;
1023
1024  TRACE_FUNCTION("[Socket API] sock_send()");
1025
1026  sock_data = (T_SOCK_SOCKET_DATA*)socket;
1027  if(sock_data == NULL)
1028  {
1029    return SOCK_RESULT_INVALID_PARAMETER;
1030  }
1031
1032  api_data   = sock_data->api_data;
1033
1034  if(sock_data->tx_window != 0)
1035  {
1036    PALLOC(tcpip_data_req_prim,TCPIP_DATA_REQ);
1037  	tcpip_data_req_prim->app_handle = api_data->hCommAPP;
1038  	tcpip_data_req_prim->socket     = sock_data->tcpip_socket;
1039  	tcpip_data_req_prim->ipaddr     = 0;
1040  	tcpip_data_req_prim->port       = 0;
1041  	tcpip_data_req_prim->buflen     =  buffer_length;
1042  	tcpip_data_req_prim->data       = (U32) M_ALLOC(buffer_length);
1043
1044  	if(tcpip_data_req_prim->data)
1045  	{
1046      /* copy the user data buffer, 
1047         the prim-data is freed by tcpip_clear_send_buffer() */
1048      memcpy((char *)tcpip_data_req_prim->data,buffer,buffer_length);
1049    }
1050  	else
1051  	{
1052      PFREE(tcpip_data_req_prim);
1053      return SOCK_RESULT_OUT_OF_MEMORY;
1054    }
1055    sock_data->tx_window--;
1056  	PSEND(api_data->hCommTCPIP,tcpip_data_req_prim); 
1057  	return SOCK_RESULT_OK;
1058  }
1059  else /* tx_window = 0 */
1060  {
1061    sock_data->tx_flow_control = SOCK_FLOW_XOFF;
1062    return SOCK_RESULT_NO_BUFSPACE;
1063  }
1064} /* sock_send */
1065
1066
1067/******************************************************************************/
1068T_SOCK_RESULT sock_sendto(T_SOCK_SOCKET socket, char* buffer,U16 buffer_length,
1069                          T_SOCK_IPADDR ipaddr,T_SOCK_PORT port)
1070{
1071  T_SOCK_API_INSTANCE_DATA* api_data;
1072  T_SOCK_SOCKET_DATA* sock_data;
1073
1074  TRACE_FUNCTION("[Socket API] sock_sendto()");
1075
1076  sock_data = (T_SOCK_SOCKET_DATA*)socket;
1077  if(sock_data == NULL)
1078      return SOCK_RESULT_INVALID_PARAMETER;
1079  api_data   = sock_data->api_data;
1080
1081  if(sock_data->tx_window != 0)
1082  {
1083    PALLOC(tcpip_data_req_prim,TCPIP_DATA_REQ);
1084  	tcpip_data_req_prim->app_handle = api_data->hCommAPP;
1085  	tcpip_data_req_prim->socket        = sock_data->tcpip_socket;
1086  	tcpip_data_req_prim->ipaddr        = ipaddr;
1087  	tcpip_data_req_prim->port          = port;
1088  	tcpip_data_req_prim->buflen        =  buffer_length;
1089	  tcpip_data_req_prim->data=(U32) M_ALLOC(buffer_length);
1090  	if(tcpip_data_req_prim->data )
1091  	{
1092      /* copy the user data buffer, 
1093         the prim-data is freed by tcpip_clear_send_buffer() */
1094      memcpy((char *)tcpip_data_req_prim->data,buffer,buffer_length);
1095  	}
1096  	else
1097  	{
1098  	  PFREE(tcpip_data_req_prim);
1099  	  return SOCK_RESULT_OUT_OF_MEMORY;
1100  	}
1101
1102 	  sock_data->tx_window--;
1103 	  PSEND(api_data->hCommTCPIP, tcpip_data_req_prim);
1104    return SOCK_RESULT_OK;
1105  }
1106  else /* tx_window = 0 */
1107  {
1108    sock_data->tx_flow_control = SOCK_FLOW_XOFF;
1109    return SOCK_RESULT_NO_BUFSPACE;
1110  }
1111}
1112
1113  
1114/*******************************************************************************
1115** Function  : sock_mtu_size_cnf
1116** Parameter : T_SOCK_MTU_SIZE_CNF *
1117**             T_SOCK_API_INSTANCE
1118*******************************************************************************/
1119LOCAL void sock_mtu_size_cnf(T_TCPIP_MTU_SIZE_CNF *tcpip_mtu_size_cnf ,
1120                                   T_SOCK_API_INSTANCE api_instance )
1121{
1122  T_SOCK_SOCKET_DATA *sock_data;
1123  T_SOCK_MTU_SIZE_CNF sock_mtu_size_cnf;
1124  T_SOCK_API_INSTANCE_DATA *api_data;
1125
1126  TRACE_FUNCTION("[Socket API] sock_mtu_size_cnf()");
1127
1128  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1129
1130  sock_data =  FindSocketData(api_data ,tcpip_mtu_size_cnf->socket);
1131  if(sock_data == NULL)
1132  {
1133    PFREE(tcpip_mtu_size_cnf);
1134    return;
1135  }
1136
1137  sock_mtu_size_cnf.event_type = (T_SOCK_EVENTTYPE)tcpip_mtu_size_cnf->event_type;
1138  sock_mtu_size_cnf.result     = (T_SOCK_RESULT)tcpip_mtu_size_cnf->result; 
1139  sock_mtu_size_cnf.socket     = (T_SOCK_SOCKET)sock_data;
1140  sock_mtu_size_cnf.mtu_size   =  tcpip_mtu_size_cnf->mtu_size;
1141
1142  sock_data->callback((T_SOCK_EVENTSTRUCT*)&sock_mtu_size_cnf,sock_data->context);
1143
1144  PFREE(tcpip_mtu_size_cnf);
1145} /* sock_mtu_size_cnf */
1146
1147
1148
1149/*******************************************************************************
1150** Function  : sock_connect_ind
1151** Parameter : T_SOCK_CONNECT_IND *
1152**                   T_SOCK_API_INSTANCE
1153** Description : new socket_data will be linked to current socket_data(listening socke_data)
1154*******************************************************************************/
1155LOCAL void sock_connect_ind(T_TCPIP_CONNECT_IND *tcpip_connect_ind , 
1156                                  T_SOCK_API_INSTANCE api_instance )
1157{
1158  T_SOCK_SOCKET_DATA *cur_sock_data;   /* listening socket_data */
1159  T_SOCK_SOCKET_DATA *new_sock_data;   /* connected socket_data */
1160  T_SOCK_CONNECT_IND sock_connect_ind;
1161  T_SOCK_API_INSTANCE_DATA *api_data;
1162
1163  TRACE_FUNCTION("[Socket API] sock_connect_ind()");
1164
1165  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1166
1167  /* find listening socket_data
1168   * tcpip_connent_ind->socket is listening socket descrpitor
1169   */
1170  cur_sock_data =  FindSocketData(api_data ,tcpip_connect_ind->socket);
1171  if(cur_sock_data == NULL)
1172  {
1173    PFREE(tcpip_connect_ind);
1174    return;
1175  }
1176
1177  /* allocate socket data */
1178  MALLOC(new_sock_data, sizeof(T_SOCK_SOCKET_DATA));
1179
1180  /* put socket data in linked list */
1181  new_sock_data->next_socket_data = NULL;     
1182  cur_sock_data->next_socket_data = new_sock_data;
1183
1184  /* initialize socket data */
1185  new_sock_data->api_data        = api_data;
1186  new_sock_data->tcpip_socket    = tcpip_connect_ind->new_socket;
1187  /* server must register callback using socket_get_callback() after receiving */
1188  /* connect ind event.                                                        */
1189  new_sock_data->callback        = NULL; 
1190  new_sock_data->context         = NULL;  
1191  new_sock_data->rx_flow_control = SOCK_FLOW_XON;
1192  new_sock_data->tx_flow_control = SOCK_FLOW_XON;
1193  new_sock_data->rx_window       = 1;
1194  new_sock_data->tx_window       = 1;
1195
1196  /* fill confirm struct */
1197  sock_connect_ind.event_type   = (T_SOCK_EVENTTYPE)tcpip_connect_ind->event_type;
1198  sock_connect_ind.result       = (T_SOCK_RESULT)tcpip_connect_ind->result; 
1199  /* Check !!! why cur_sock data become new_sock_data */
1200  /* sock_connect_ind.socket       = (T_SOCK_SOCKET)cur_sock_data;*/
1201  sock_connect_ind.socket          = (T_SOCK_SOCKET)new_sock_data;   
1202  sock_connect_ind.new_socket   =  tcpip_connect_ind->new_socket;
1203  sock_connect_ind.peer_ipaddr  =  tcpip_connect_ind->ipaddr;
1204  sock_connect_ind.peer_port    =  tcpip_connect_ind->port;
1205
1206  cur_sock_data->callback((T_SOCK_EVENTSTRUCT*)&sock_connect_ind,cur_sock_data->context);
1207
1208  PFREE(tcpip_connect_ind);
1209} /* sock_connect_ind */
1210
1211
1212/*******************************************************************************
1213** Function  : sock_conn_closed_ind
1214** Parameter : T_SOCK_CONN_CLOSED_IND *
1215**             T_SOCK_API_INSTANCE
1216**             Release closed socket data
1217*******************************************************************************/
1218LOCAL void sock_conn_closed_ind(T_TCPIP_CONN_CLOSED_IND *tcpip_conn_closed_ind,
1219                                      T_SOCK_API_INSTANCE api_instance)
1220{
1221  T_SOCK_SOCKET_DATA *sock_data;
1222  T_SOCK_CONN_CLOSED_IND sock_conn_closed_ind;
1223  T_SOCK_API_INSTANCE_DATA *api_data;
1224
1225  TRACE_FUNCTION("[Socket API] sock_closed_ind()");
1226
1227  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1228
1229  sock_data =  FindSocketData(api_data, tcpip_conn_closed_ind->socket);
1230  if(sock_data != NULL)
1231  {
1232    sock_conn_closed_ind.event_type = (T_SOCK_EVENTTYPE)tcpip_conn_closed_ind->event_type; 
1233    sock_conn_closed_ind.result     = (T_SOCK_RESULT)tcpip_conn_closed_ind->result;  
1234    sock_conn_closed_ind.socket     = (T_SOCK_SOCKET)sock_data;
1235
1236    sock_data->callback(&sock_conn_closed_ind,sock_data->context);
1237
1238    ReleaseSocketData(sock_data->api_data,sock_data);
1239  }
1240  else {;}
1241
1242  PFREE(tcpip_conn_closed_ind);
1243} /* sock_conn_closed_ind */
1244
1245
1246/*******************************************************************************
1247** Function  : sock_error_ind
1248** Parameter : T_SOCK_ERROR_IND *
1249**             T_SOCK_API_INSTANCE
1250*******************************************************************************/
1251LOCAL void sock_error_ind(T_TCPIP_ERROR_IND* tcpip_error_ind , 
1252                                T_SOCK_API_INSTANCE api_instance)
1253{
1254  T_SOCK_SOCKET_DATA*   sock_data;
1255  T_SOCK_ERROR_IND sock_error_ind;
1256  T_SOCK_API_INSTANCE_DATA *api_data;
1257
1258  TRACE_FUNCTION("[Socket API] sock_error_ind()");
1259
1260  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1261  
1262  sock_data =  FindSocketData(api_data , tcpip_error_ind->socket);
1263  if(sock_data != NULL)
1264  {
1265    /* fill confirm struct */
1266    sock_error_ind.event_type   = (T_SOCK_EVENTTYPE)tcpip_error_ind->event_type;
1267    sock_error_ind.result       = (T_SOCK_RESULT)tcpip_error_ind->result; 
1268    sock_error_ind.socket       = (T_SOCK_SOCKET)sock_data;
1269
1270    sock_data->callback(&sock_error_ind,sock_data->context);
1271  }
1272  else {;}
1273  
1274  PFREE(tcpip_error_ind);
1275} /* sock_error_ind */
1276
1277
1278/******************************************************************************/
1279T_SOCK_RESULT sock_gethostbyaddr(T_SOCK_API_INSTANCE api_instance,
1280                                 T_SOCK_IPADDR ipaddr, 
1281                                 T_SOCK_CALLBACK callback,
1282                                 void* context )
1283{
1284  T_SOCK_API_INSTANCE_DATA* api_data;
1285  T_SOCK_SOCKET_DATA*     socket_data;
1286
1287  TRACE_FUNCTION("[Socket API] sock_gethostbyaddr()");
1288
1289  if(callback EQ NULL)
1290   return SOCK_RESULT_INVALID_PARAMETER;
1291
1292  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1293  
1294  MALLOC(socket_data, sizeof(T_SOCK_SOCKET_DATA));
1295   
1296  /* put socket data in linked list */
1297  socket_data->next_socket_data = api_data->socket_data;
1298  api_data->socket_data         = socket_data;
1299  
1300  /* initialize socket data */
1301  socket_data->api_data        = api_data;
1302  socket_data->tcpip_socket    = 0;
1303  socket_data->callback        = callback;
1304  socket_data->context         = context;
1305  socket_data->rx_flow_control = SOCK_FLOW_XON;
1306  socket_data->tx_flow_control = SOCK_FLOW_XON;
1307  socket_data->rx_window       = 1;
1308  socket_data->tx_window       = 1;
1309  
1310  {
1311    PALLOC (tcpip_hostinfo_req_prim ,TCPIP_HOSTINFO_REQ);
1312    tcpip_hostinfo_req_prim->app_handle = api_data->hCommAPP;
1313    tcpip_hostinfo_req_prim->ipaddr = ipaddr;
1314    *tcpip_hostinfo_req_prim->hostname = 0;
1315    tcpip_hostinfo_req_prim->request_id = (T_SOCK_SOCKET)socket_data;     
1316    PSEND(api_data->hCommTCPIP, tcpip_hostinfo_req_prim);
1317 }
1318 return SOCK_RESULT_OK;
1319} /* sock_gethostbyaddr */
1320
1321
1322/*******************************************************************************
1323   Function     :  sock_recv_ind
1324   Parameter    :  T_TCPIP_DATA_IND*
1325                   T_SOCK_API_INSTANCE  
1326   Return       : VOID
1327   Return Event : None
1328*******************************************************************************/
1329static void sock_recv_ind(T_TCPIP_DATA_IND* tcpip_data_ind,
1330                                T_SOCK_API_INSTANCE api_instance)
1331{
1332  T_SOCK_SOCKET_DATA*   sock_data;
1333  T_SOCK_RECV_IND sock_receive_ind;
1334  T_SOCK_API_INSTANCE_DATA *api_data;
1335
1336  TRACE_FUNCTION("[Socket API] sock_recv_ind()");
1337
1338  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1339  
1340  sock_data =  FindSocketData(api_data , tcpip_data_ind->socket);
1341  if(sock_data == NULL)
1342  {
1343    MFREE(tcpip_data_ind->data);
1344    PFREE(tcpip_data_ind);
1345    return;
1346  }
1347
1348  sock_data->rx_window--;  /* when data received, window size will be descreased..*/
1349
1350  if(sock_data->rx_flow_control == SOCK_FLOW_XOFF )
1351  {
1352    /* case sock flow xoff, sock_data will be stored and send to the application
1353       with sock_flow_xon() */
1354    sock_data->recv_ind.socket  = tcpip_data_ind->socket;
1355    sock_data->recv_ind.result = (T_SOCK_RESULT)tcpip_data_ind->result;
1356    sock_data->recv_ind.event_type = (T_SOCK_EVENTTYPE)tcpip_data_ind->event_type;
1357    sock_data->recv_ind.data_length = tcpip_data_ind->buflen;
1358    sock_data->recv_ind.data_buffer = (char*)tcpip_data_ind->data;
1359  }
1360  else 
1361  {
1362    /* case sock flow xon , sock_data will be sent to application...*/
1363    sock_receive_ind.event_type   =  (T_SOCK_EVENTTYPE)tcpip_data_ind->event_type;
1364    sock_receive_ind.result       =  (T_SOCK_RESULT)tcpip_data_ind->result;
1365    sock_receive_ind.socket       =  (T_SOCK_SOCKET)sock_data;
1366    sock_receive_ind.data_length  =  tcpip_data_ind->buflen;
1367    sock_receive_ind.data_buffer  =  (char *)tcpip_data_ind->data;
1368
1369    sock_data->callback((T_SOCK_EVENTSTRUCT*)&sock_receive_ind,sock_data->context);
1370
1371    /* send primitive */
1372    if( (sock_data->rx_flow_control != SOCK_FLOW_XOFF) &&
1373        (sock_data->rx_window == 0) )
1374    {
1375      PALLOC(tcpip_data_res_prim,TCPIP_DATA_RES);
1376      tcpip_data_res_prim->app_handle = api_data->hCommAPP;
1377      ACI_ASSERT(sock_data->tcpip_socket == tcpip_data_ind->socket);
1378      tcpip_data_res_prim->socket       =  tcpip_data_ind->socket ;
1379      tcpip_data_res_prim->window       = 1;  
1380      sock_data->rx_window   = 1;
1381      PSEND(api_data->hCommTCPIP, tcpip_data_res_prim);	
1382    }
1383  }
1384  PFREE(tcpip_data_ind);
1385} /* sock_recv_ind */
1386
1387
1388/*******************************************************************************
1389** Function  : sock_send_cnf
1390** Parameter : T_TCPIP_DATA_CNF *
1391**                   T_SOCK_API_INSTANCE
1392*******************************************************************************/
1393static void sock_send_cnf(T_TCPIP_DATA_CNF* tcpip_data_cnf,
1394                                T_SOCK_API_INSTANCE api_instance)
1395{
1396  T_SOCK_SOCKET_DATA *sock_data;
1397  T_SOCK_API_INSTANCE_DATA *api_data;
1398  T_SOCK_FLOW_READY_IND sock_flow_ready_ind;
1399
1400  TRACE_FUNCTION("[Socket API] sock_send_cnf()");
1401
1402  api_data = (T_SOCK_API_INSTANCE_DATA*)api_instance;
1403  sock_data =  FindSocketData(api_data , tcpip_data_cnf->socket);
1404
1405  if(sock_data == NULL)
1406  {
1407    PFREE(tcpip_data_cnf);
1408    return;
1409  }
1410  /* if tcpip_data_cnf->window is 0,*/
1411  /* this value will be 1 because 0 and 1 is the same meaning in the Rnet TCPIP.*/
1412  if( tcpip_data_cnf->window  == 0 )
1413  {
1414    sock_data->tx_window  = 1;
1415  }
1416  else
1417  {
1418    sock_data->tx_window = tcpip_data_cnf->window;
1419  }
1420
1421  sock_flow_ready_ind.event_type = (T_SOCK_EVENTTYPE)tcpip_data_cnf->event_type;
1422  sock_flow_ready_ind.re

Large files files are truncated, but you can click here to view the full file