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

📄 net_ascii.c

📁 ucos的tcpip协议占
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*********************************************************************************************************
*                                              uC/TCP-IP
*                                      The Embedded TCP/IP Suite
*
*                          (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
*               All rights reserved.  Protected by international copyright laws.
*
*               uC/TCP-IP is provided in source form for FREE evaluation, for educational
*               use or peaceful research.  If you plan on using uC/TCP-IP in a commercial
*               product you need to contact Micrium to properly license its use in your
*               product.  We provide ALL the source code for your convenience and to help
*               you experience uC/TCP-IP.  The fact that the source code is provided does
*               NOT mean that you can use it without paying a licensing fee.
*
*               Knowledge of the source code may NOT be used to develop a similar product.
*
*               Please help us continue to provide the Embedded community with the finest
*               software available.  Your honesty is greatly appreciated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                         NETWORK ASCII LIBRARY
*
* Filename      : net_ascii.c
* Version       : V1.87
* Programmer(s) : ITJ
*********************************************************************************************************
*/

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

#define    NET_ASCII_MODULE
#include  <net.h>


/*$PAGE*/
/*
*********************************************************************************************************
*                                            LOCAL DEFINES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                           LOCAL CONSTANTS
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                          LOCAL DATA TYPES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                            LOCAL TABLES
*********************************************************************************************************
*/


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


/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                     LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/


/*$PAGE*/
/*
*********************************************************************************************************
*                                        NetASCII_Str_to_MAC()
*
* Description : Convert a MAC address ASCII string to a MAC address.
*
* Argument(s) : paddr_mac_ascii     Pointer to an ASCII string that contains a MAC address (see Note #1).
*
*               paddr_mac           Pointer to a memory buffer that will receive the converted MAC address
*                                       (see Note #2) :
*
*                                       MAC address represented by ASCII string,         if NO errors.
*
*                                       MAC address cleared to all zeros (see Note #2c), otherwise.
*
*               perr        Pointer to variable that will receive the return error code from this function :
*
*                               NET_ASCII_ERR_NONE                  MAC address successfully converted.
*                               NET_ASCII_ERR_NULL_PTR              Argument 'paddr_mac_ascii'/'paddr_mac'
*                                                                       passed a NULL pointer.
*                               NET_ASCII_ERR_INVALID_LEN           Invalid ASCII string length.
*                               NET_ASCII_ERR_INVALID_CHAR          Invalid ASCII character.
*                               NET_ASCII_ERR_INVALID_CHAR_LEN      Invalid ASCII character length.
*                               NET_ASCII_ERR_INVALID_CHAR_SEQ      Invalid ASCII character sequence.
*
* Return(s)   : none.
*
* Caller(s)   : Application.
*
*               This function is a network protocol suite application interface (API) function & MAY be 
*               called by application function(s).
*
* Note(s)     : (1) (a) The MAC address ASCII string MUST :
*
*                       (1) Include ONLY hexadecimal values & the colon character (':') ; ALL other 
*                           characters are trapped as invalid, including any leading or trailing
*                           characters.
*
*                       (2) (A) Include EXACTLY six hexadecimal values ...
*                           (B) ... separated ...
*                           (C) ... by  EXACTLY five colon characters.
*
*                       (3) Ensure that each hexadecimal value's number of digits does NOT exceed 
*                           the maximum number of digits, NET_ASCII_CHAR_MAX_OCTET_ADDR_MAC (2).
*
*                   (b) In other words, the MAC address ASCII string separates six hexadecimal octet 
*                       values by the colon character (':').  Each hexadecimal value represents one 
*                       octet of the MAC address starting with the most significant octet in network
*                       order.
*
*                           MAC Address Examples :
*
*                                 MAC ADDRESS ASCII STRING     HEXADECIMAL EQUIVALENT
*
*                                   "00:1A:07:AC:22:09"     =     0x001A07AC2209
*                                   "76:4E:01:D2:8C:0B"     =     0x764E01D28C0B
*                                   "80:Db:fE:0b:34:52"     =     0X80DBFE0B3452
*                                    --             --              --        --
*                                    ^               ^              ^          ^
*                                    |               |              |          |
*                                   MSO             LSO            MSO        LSO
*
*                               where
*
*                                   MSO             Most  Significant Octet in MAC Address
*                                   LSO             Least Significant Octet in MAC Address
*
*
*               (2) (a) The size of the memory buffer that will receive the returned MAC address MUST be
*                       greater than or equal to NET_ASCII_NBR_OCTET_ADDR_MAC.
*
*                   (b) MAC address accessed by octets in memory buffer array.
*
*                   (c) MAC address memory array cleared in case of any error(s).
*********************************************************************************************************
*/
/*$PAGE*/
void  NetASCII_Str_to_MAC (CPU_CHAR    *paddr_mac_ascii,
                           CPU_INT08U  *paddr_mac,
                           NET_ERR     *perr)
{
    CPU_CHAR    *pchar_cur;
    CPU_CHAR    *pchar_prev;
    CPU_INT08U  *paddr_cur;
    CPU_INT16U   addr_octet_val;
    CPU_INT16U   addr_octet_val_dig;
    CPU_INT32U   addr_nbr_octet;
    CPU_INT08U   addr_nbr_octet_dig;


                                                                        /* -------------- VALIDATE PTRS --------------- */
    if (paddr_mac == (CPU_INT08U *)0) {
       *perr = NET_ASCII_ERR_NULL_PTR;
        return;
    }

    if (paddr_mac_ascii == (CPU_CHAR *)0) {
        Mem_Clr((void     *)paddr_mac,                                  /* Clr rtn addr on err (see Note #2c).          */
                (CPU_SIZE_T)NET_ASCII_NBR_OCTET_ADDR_MAC);
       *perr = NET_ASCII_ERR_NULL_PTR;
        return;
    }



/*$PAGE*/
    pchar_cur          = (CPU_CHAR   *)paddr_mac_ascii;
    pchar_prev         = (CPU_CHAR   *)0;
    paddr_cur          = (CPU_INT08U *)paddr_mac;
    addr_octet_val     =  0x0000;
    addr_nbr_octet     =  0;
    addr_nbr_octet_dig =  0;

    while ((pchar_cur != (CPU_CHAR *)0) &&                              /* Parse ALL non-NULL chars in ASCII str.       */
          (*pchar_cur != (CPU_CHAR  )0)) {

        switch (*pchar_cur) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
                 addr_nbr_octet_dig++;                                  /* If nbr digs > max (see Note #1a3); ...       */
                 if (addr_nbr_octet_dig > NET_ASCII_CHAR_MAX_OCTET_ADDR_MAC) {
                     Mem_Clr((void     *)paddr_mac,                     /* ... clr rtn addr  (see Note #2c)   ...       */
                             (CPU_SIZE_T)NET_ASCII_NBR_OCTET_ADDR_MAC);
                    *perr = NET_ASCII_ERR_INVALID_CHAR_LEN;             /* ... & rtn err.                               */
                     return;
                 }
                                                                        
                 switch (*pchar_cur) {                                  /* Convert ASCII char into hex val.             */
                     case '0':
                     case '1':
                     case '2':
                     case '3':
                     case '4':
                     case '5':
                     case '6':
                     case '7':
                     case '8':
                     case '9':
                          addr_octet_val_dig = (CPU_INT16U)(*pchar_cur - '0');
                          break;


                     case 'A':
                     case 'B':
                     case 'C':
                     case 'D':
                     case 'E':
                     case 'F':
                          addr_octet_val_dig = (CPU_INT16U)(*pchar_cur - 'A' + 10);
                          break;


                     case 'a':
                     case 'b':
                     case 'c':
                     case 'd':
                     case 'e':
                     case 'f':
                          addr_octet_val_dig = (CPU_INT16U)(*pchar_cur - 'a' + 10);
                          break;

⌨️ 快捷键说明

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