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

📄 zl5011xcetdifftransfer.c

📁 Zalink50114----TDMoIP芯片驱动源码
💻 C
字号:
/*******************************************************************************
*
*  File name:              zl5011xCetDiffTransfer.c
*
*  Version:                4
*
*  Author:                 MRC
*
*  Date created:           01/09/2004
*
*  Copyright 2002, 2003, 2004, 2005, Zarlink Semiconductor Limited.
*  All rights reserved.
*
*  Module Description:
*
*  This module contains functions to open, close, send and receive data over
*  UDP sockets. These functions are used by the CET differential clock recovery
*  algorithm in order to transfer timing information from the Master to Slave
*  devices.
*  These functions are provided to allow flexibility in the connection
*  mechanism between the two nodes.
*
*  Revision History:
*
*  Rev:  Date:       Author:  Comments:
*  1     01/09/2004  MRC      Creation
*  2     20/10/2004  APL      Use memset instead of bzero which is non-ANSI
*  3     10/12/2004  APL      Added explicit cast to avoid compiler warning
*  4     01/04/2005  MRC      Fixed compiler warning
*
*******************************************************************************/

/*****************   INCLUDE FILES                *****************************/

#include "zl5011xApi.h"
#include "zl5011xCetDiffTransfer.h"

/*****************   GLOBAL VARIABLES          ********************************/

/* differential clock recovery settings */
zl5011xBooleanE zl5011xCetDiffTxEnable =  ZL5011X_FALSE;
Sint32T zl5011xCetDiffTxFd;
struct sockaddr_in zl5011xCetDiffTxAddr;

zl5011xBooleanE zl5011xCetDiffRxEnable = ZL5011X_FALSE;
Sint32T zl5011xCetDiffRxFd;
struct sockaddr_in zl5011xCetDiffRxAddr;

/*****************   EXPORTED FUNCTION DEFINTIONS   ***************************/

/*******************************************************************************

 Function:
    zl5011xCetDiffTransferRxInit

 Description:
   Initialises a receive socket for the differential out of band clock recovery
   timing information transfer.

 Inputs:
   rxPortNum      port number to open for receiving data.

 Structure inputs:
   None

 Structure outputs:
   None

 Returns:
   zlStatusE

 Remarks:

*******************************************************************************/

zlStatusE zl5011xCetDiffTransferRxInit(Uint16T rxPortNum)
{
   zlStatusE status = ZL5011X_OK;
   OS_STATUS osStatus;

   /* open the Rx socket */
   zl5011xCetDiffRxFd = socket(AF_INET, SOCK_DGRAM, 0);

   if (zl5011xCetDiffRxFd == OS_ERROR)
   {
      status = ZL5011X_RTOS_SOCKET_FAIL;
   }

   /* Bind it to the required port */
   if (status == ZL5011X_OK)
   {
      memset((char *)&zl5011xCetDiffRxAddr, 0, sizeof(struct sockaddr_in));

      zl5011xCetDiffRxAddr.sin_family = AF_INET;
      zl5011xCetDiffRxAddr.sin_port = htons(rxPortNum);
      zl5011xCetDiffRxAddr.sin_addr.s_addr = htonl(INADDR_ANY);

      osStatus = bind(zl5011xCetDiffRxFd, (struct sockaddr *)&zl5011xCetDiffRxAddr, sizeof(struct sockaddr_in));

      if (osStatus == OS_ERROR)
      {
         status = ZL5011X_RTOS_SOCKET_FAIL;
      }
   }

   if (status == ZL5011X_OK)
   {
      zl5011xCetDiffRxEnable = ZL5011X_TRUE;
   }
   else
   {
      zl5011xCetDiffRxEnable = ZL5011X_FALSE;
   }

   return(status);
}

/*******************************************************************************

 Function:
    zl5011xCetDiffTransferRxDelete

 Description:
   Closes a receive socket for the differential out of band clock recovery
   timing information transfer.

 Inputs:
   None

 Structure inputs:
   None

 Structure outputs:
   None

 Returns:
   zlStatusE

 Remarks:

*******************************************************************************/

zlStatusE zl5011xCetDiffTransferRxDelete(void)
{
   zlStatusE status = ZL5011X_OK;

   if (zl5011xCetDiffRxEnable == ZL5011X_TRUE)
   {
      close(zl5011xCetDiffRxFd);

      zl5011xCetDiffRxEnable = ZL5011X_FALSE;
   }

   return(status);
}

/*******************************************************************************

 Function:
    zl5011xCetDiffTransferTxInit

 Description:
   Initialises a transmit socket for the differential out of band clock recovery
   timing information transfer.

 Inputs:
   None

 Structure inputs:
   None

 Structure outputs:
   None

 Returns:
   zlStatusE

 Remarks:

*******************************************************************************/

zlStatusE zl5011xCetDiffTransferTxInit(void)
{
   zlStatusE status = ZL5011X_OK;

   /* open the Tx socket */
   zl5011xCetDiffTxFd = socket(AF_INET, SOCK_DGRAM, 0);

   if (zl5011xCetDiffTxFd == OS_ERROR)
   {
      status = ZL5011X_RTOS_SOCKET_FAIL;
   }

   /* prepare a Tx header */
   if (status == ZL5011X_OK)
   {
      memset((char *)&zl5011xCetDiffTxAddr, 0, sizeof(struct sockaddr_in));
      zl5011xCetDiffTxAddr.sin_family = AF_INET;
   }

   if (status == ZL5011X_OK)
   {
      zl5011xCetDiffTxEnable = ZL5011X_TRUE;
   }
   else
   {
      zl5011xCetDiffTxEnable = ZL5011X_FALSE;
   }

   return(status);
}

/*******************************************************************************

 Function:
    zl5011xCetDiffTransferTxDelete

 Description:
   Closes a transmit socket for the differential out of band clock recovery
   timing information transfer.

 Inputs:
   None

 Structure inputs:
   None

 Structure outputs:
   None

 Returns:
   zlStatusE

 Remarks:

*******************************************************************************/

zlStatusE zl5011xCetDiffTransferTxDelete(void)
{
   zlStatusE status = ZL5011X_OK;

   if (zl5011xCetDiffTxEnable == ZL5011X_TRUE)
   {
      close(zl5011xCetDiffRxFd);

      zl5011xCetDiffTxEnable = ZL5011X_FALSE;
   }

   return(status);
}

/*******************************************************************************

 Function:
   zl5011xCetDiffTransferTxDetails

 Description:
   Used to convert from IP and UDP port numbers to the format required for use
   by the socket interface functions.

 Inputs:
   zl5011xParams   Pointer to the structure for this device instance

 Outputs:
   None

 Returns:
   zlStatusE

 Remarks:
   None

*******************************************************************************/

zlStatusE zl5011xCetDiffTransferTxDetails(Uint32T *sin_addr, Uint16T *sin_port,
      Uint8T *diffTxIpDest, Uint16T diffTxPortNum)
{
   /* set up the Tx address to use for sending packets */
   *sin_addr = htonl((diffTxIpDest[3] << 24) | (diffTxIpDest[2] << 16) |
         (diffTxIpDest[1] << 8) | (diffTxIpDest[0] << 0));
   *sin_port = htons(diffTxPortNum);

   return(ZL5011X_OK);
}

/*****************   STATIC FUNCTION DEFINTIONS   *****************************/

/*******************************************************************************

 Function:
    zl5011xCetDiffTransferRxReceive

 Description:
   Receives an out of band packet containing timing information from a timing
   Master device to recover the timing for a stream.

 Inputs:
   None

 Outputs:
   zl5011xParams   Pointer to the structure for the slave device instance
   stream         stream number on the slave device
   seq            differential timing packet sequence number
   value          timing value from the master device

 Returns:
   None

 Remarks:

*******************************************************************************/

zlStatusE  zl5011xCetDiffTransferRxReceive(zl5011xParamsS **zl5011xParams, Uint8T *stream, Uint16T *seq, Uint32T *value)
{
   zlStatusE  status = ZL5011X_OK;
   Uint32T len;
   zl5011xCetDiffPktTransferS data;

   /* wait for a timing message */
   len = recv(zl5011xCetDiffRxFd, (char *)&data, sizeof(zl5011xCetDiffPktTransferS), 0);

   if (len == (Uint32T)OS_ERROR)
   {
      status = ZL5011X_RTOS_SOCKET_FAIL;
   }

   if (status == ZL5011X_OK)
   {
      /* received a packet so extract the data from it */
      *zl5011xParams = (zl5011xParamsS *)ntohl((Uint32T)data.zl5011xParams);
      *stream = data.stream;
      *seq = ntohs(data.seqNum);
      *value = ntohl(data.prsValue);
   }

   return status;
}

/*******************************************************************************

 Function:
   zl5011xCetDiffTransferTxSend

 Description:
   Sends an out of band packet containing timing information for the slave
   device to recover the timing for a stream.

 Inputs:
   sin_addr       IP address to send packet to - already stored in correct format
   sin_port       UDP port to send packet to - already stored in correct format
   zl5011xParams   Pointer to the structure for the slave device instance
   stream         stream number on the slave device
   seq            differential timing packet sequence number
   value          timing value to transfer to slave device

 Outputs:
   None

 Returns:
   zlStatusE

 Remarks:

*******************************************************************************/

zlStatusE  zl5011xCetDiffTransferTxSend(Uint32T sin_addr, Uint16T sin_port,
      zl5011xParamsS *zl5011xParams, Uint8T stream, Uint16T seq, Uint32T value)
{
   zlStatusE status = ZL5011X_OK;
   OS_STATUS osStatus;
   zl5011xCetDiffPktTransferS data;

   /* prepare the data structure for sending */
   data.zl5011xParams = (zl5011xParamsS *)htonl((Uint32T)zl5011xParams);
   data.stream = stream;
   data.seqNum = htons(seq);
   data.prsValue = htonl(value);

   zl5011xCetDiffTxAddr.sin_addr.s_addr = sin_addr;
   zl5011xCetDiffTxAddr.sin_port = sin_port;

   /* send the packet */
   osStatus = sendto(zl5011xCetDiffTxFd, (char *)&data, sizeof(zl5011xCetDiffPktTransferS), 0,
         (struct sockaddr *)&zl5011xCetDiffTxAddr, sizeof(struct sockaddr_in));

   if (osStatus == OS_ERROR)
   {
      status = ZL5011X_RTOS_SOCKET_FAIL;
   }

   return status;
}

⌨️ 快捷键说明

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