Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mib_common.c Source File

mib_common.c

Go to the documentation of this file.
00001 /**
00002  * @file mib_common.c
00003  * @brief Common definitions for MIB modules
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 //Dependencies
00030 #include "core/net.h"
00031 #include "mibs/mib_common.h"
00032 #include "oid.h"
00033 #include "debug.h"
00034 
00035 
00036 /**
00037  * @brief Encode instance identifier (index)
00038  * @param[in] oid Pointer to the object identifier
00039  * @param[in] maxOidLen Maximum number of bytes the OID can hold
00040  * @param[in,out] pos Offset where to write the instance identifier
00041  * @param[in] index Index value
00042  * @return Error code
00043  **/
00044 
00045 error_t mibEncodeIndex(uint8_t *oid, size_t maxOidLen, size_t *pos, uint_t index)
00046 {
00047    //Encode instance identifier
00048    return oidEncodeSubIdentifier(oid, maxOidLen, pos, index);
00049 }
00050 
00051 
00052 /**
00053  * @brief Decode instance identifier (index)
00054  * @param[in] oid Pointer to the object identifier
00055  * @param[in] oidLen Length of the OID, in bytes
00056  * @param[in,out] pos Offset where to read the instance identifier
00057  * @param[out] index Index value
00058  * @return Error code
00059  **/
00060 
00061 error_t mibDecodeIndex(const uint8_t *oid, size_t oidLen, size_t *pos, uint_t *index)
00062 {
00063    error_t error;
00064    uint32_t value;
00065 
00066    //Decode instance identifier
00067    error = oidDecodeSubIdentifier(oid, oidLen, pos, &value);
00068    //Invalid sub-identifier?
00069    if(error)
00070       return error;
00071 
00072    //Save index value
00073    *index = value;
00074 
00075    //Successful processing
00076    return NO_ERROR;
00077 }
00078 
00079 
00080 /**
00081  * @brief Encode instance identifier (IPv4 address)
00082  * @param[in] oid Pointer to the object identifier
00083  * @param[in] maxOidLen Maximum number of bytes the OID can hold
00084  * @param[in,out] pos Offset where to write the instance identifier
00085  * @param[in] ipAddr IPv4 address
00086  * @return Error code
00087  **/
00088 
00089 error_t mibEncodeIpv4Addr(uint8_t *oid, size_t maxOidLen, size_t *pos, Ipv4Addr ipAddr)
00090 {
00091    error_t error;
00092    uint_t i;
00093    uint8_t *p;
00094 
00095    //Cast the IPv4 address as a byte array
00096    p = (uint8_t *) &ipAddr;
00097 
00098    //The address is encoded as 4 subsequent sub-identifiers
00099    for(i = 0; i < 4; i++)
00100    {
00101       //Encode the current byte
00102       error = oidEncodeSubIdentifier(oid, maxOidLen, pos, p[i]);
00103       //Any error to report?
00104       if(error)
00105          return error;
00106    }
00107 
00108    //Successful processing
00109    return NO_ERROR;
00110 }
00111 
00112 
00113 /**
00114  * @brief Decode instance identifier (IPv4 address)
00115  * @param[in] oid Pointer to the object identifier
00116  * @param[in] oidLen Length of the OID, in bytes
00117  * @param[in,out] pos Offset where to read the instance identifier
00118  * @param[out] ipAddr IPv4 address
00119  * @return Error code
00120  **/
00121 
00122 error_t mibDecodeIpv4Addr(const uint8_t *oid, size_t oidLen, size_t *pos, Ipv4Addr *ipAddr)
00123 {
00124    error_t error;
00125    uint_t i;
00126    uint32_t value;
00127    uint8_t *p;
00128 
00129    //Cast the IPv4 address as a byte array
00130    p = (uint8_t *) ipAddr;
00131 
00132    //The address is encoded as 4 subsequent sub-identifiers
00133    for(i = 0; i < 4; i++)
00134    {
00135       //Decode the current sub-identifier
00136       error = oidDecodeSubIdentifier(oid, oidLen, pos, &value);
00137       //Invalid sub-identifier?
00138       if(error)
00139          return error;
00140 
00141       //Each byte of the IPv4 address must be in the range 0-255
00142       if(value > 255)
00143          return ERROR_INSTANCE_NOT_FOUND;
00144 
00145       //Save the current byte
00146       p[i] = value & 0xFF;
00147    }
00148 
00149    //Successful processing
00150    return NO_ERROR;
00151 }
00152 
00153 
00154 /**
00155  * @brief Encode instance identifier (port number)
00156  * @param[in] oid Pointer to the object identifier
00157  * @param[in] maxOidLen Maximum number of bytes the OID can hold
00158  * @param[in,out] pos Offset where to write the instance identifier
00159  * @param[in] port Port number
00160  * @return Error code
00161  **/
00162 
00163 error_t mibEncodePort(uint8_t *oid, size_t maxOidLen, size_t *pos, uint16_t port)
00164 {
00165    //Encode instance identifier
00166    return oidEncodeSubIdentifier(oid, maxOidLen, pos, port);
00167 }
00168 
00169 
00170 /**
00171  * @brief Decode instance identifier (port number)
00172  * @param[in] oid Pointer to the object identifier
00173  * @param[in] oidLen Length of the OID, in bytes
00174  * @param[in,out] pos Offset where to read the instance identifier
00175  * @param[out] port Port number
00176  * @return Error code
00177  **/
00178 
00179 error_t mibDecodePort(const uint8_t *oid, size_t oidLen, size_t *pos, uint16_t *port)
00180 {
00181    error_t error;
00182    uint32_t value;
00183 
00184    //Decode instance identifier
00185    error = oidDecodeSubIdentifier(oid, oidLen, pos, &value);
00186    //Invalid sub-identifier?
00187    if(error)
00188       return error;
00189 
00190    //Port number must be in range 0-65535
00191    if(value > 65535)
00192       return ERROR_INSTANCE_NOT_FOUND;
00193 
00194    //Save port number
00195    *port = value;
00196 
00197    //Successful processing
00198    return NO_ERROR;
00199 }
00200