Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file mib_common.c
Sergunb 0:8918a71cdbe9 3 * @brief Common definitions for MIB modules
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneTCP Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 26 * @version 1.7.6
Sergunb 0:8918a71cdbe9 27 **/
Sergunb 0:8918a71cdbe9 28
Sergunb 0:8918a71cdbe9 29 //Dependencies
Sergunb 0:8918a71cdbe9 30 #include "core/net.h"
Sergunb 0:8918a71cdbe9 31 #include "mibs/mib_common.h"
Sergunb 0:8918a71cdbe9 32 #include "oid.h"
Sergunb 0:8918a71cdbe9 33 #include "debug.h"
Sergunb 0:8918a71cdbe9 34
Sergunb 0:8918a71cdbe9 35
Sergunb 0:8918a71cdbe9 36 /**
Sergunb 0:8918a71cdbe9 37 * @brief Encode instance identifier (index)
Sergunb 0:8918a71cdbe9 38 * @param[in] oid Pointer to the object identifier
Sergunb 0:8918a71cdbe9 39 * @param[in] maxOidLen Maximum number of bytes the OID can hold
Sergunb 0:8918a71cdbe9 40 * @param[in,out] pos Offset where to write the instance identifier
Sergunb 0:8918a71cdbe9 41 * @param[in] index Index value
Sergunb 0:8918a71cdbe9 42 * @return Error code
Sergunb 0:8918a71cdbe9 43 **/
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 error_t mibEncodeIndex(uint8_t *oid, size_t maxOidLen, size_t *pos, uint_t index)
Sergunb 0:8918a71cdbe9 46 {
Sergunb 0:8918a71cdbe9 47 //Encode instance identifier
Sergunb 0:8918a71cdbe9 48 return oidEncodeSubIdentifier(oid, maxOidLen, pos, index);
Sergunb 0:8918a71cdbe9 49 }
Sergunb 0:8918a71cdbe9 50
Sergunb 0:8918a71cdbe9 51
Sergunb 0:8918a71cdbe9 52 /**
Sergunb 0:8918a71cdbe9 53 * @brief Decode instance identifier (index)
Sergunb 0:8918a71cdbe9 54 * @param[in] oid Pointer to the object identifier
Sergunb 0:8918a71cdbe9 55 * @param[in] oidLen Length of the OID, in bytes
Sergunb 0:8918a71cdbe9 56 * @param[in,out] pos Offset where to read the instance identifier
Sergunb 0:8918a71cdbe9 57 * @param[out] index Index value
Sergunb 0:8918a71cdbe9 58 * @return Error code
Sergunb 0:8918a71cdbe9 59 **/
Sergunb 0:8918a71cdbe9 60
Sergunb 0:8918a71cdbe9 61 error_t mibDecodeIndex(const uint8_t *oid, size_t oidLen, size_t *pos, uint_t *index)
Sergunb 0:8918a71cdbe9 62 {
Sergunb 0:8918a71cdbe9 63 error_t error;
Sergunb 0:8918a71cdbe9 64 uint32_t value;
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66 //Decode instance identifier
Sergunb 0:8918a71cdbe9 67 error = oidDecodeSubIdentifier(oid, oidLen, pos, &value);
Sergunb 0:8918a71cdbe9 68 //Invalid sub-identifier?
Sergunb 0:8918a71cdbe9 69 if(error)
Sergunb 0:8918a71cdbe9 70 return error;
Sergunb 0:8918a71cdbe9 71
Sergunb 0:8918a71cdbe9 72 //Save index value
Sergunb 0:8918a71cdbe9 73 *index = value;
Sergunb 0:8918a71cdbe9 74
Sergunb 0:8918a71cdbe9 75 //Successful processing
Sergunb 0:8918a71cdbe9 76 return NO_ERROR;
Sergunb 0:8918a71cdbe9 77 }
Sergunb 0:8918a71cdbe9 78
Sergunb 0:8918a71cdbe9 79
Sergunb 0:8918a71cdbe9 80 /**
Sergunb 0:8918a71cdbe9 81 * @brief Encode instance identifier (IPv4 address)
Sergunb 0:8918a71cdbe9 82 * @param[in] oid Pointer to the object identifier
Sergunb 0:8918a71cdbe9 83 * @param[in] maxOidLen Maximum number of bytes the OID can hold
Sergunb 0:8918a71cdbe9 84 * @param[in,out] pos Offset where to write the instance identifier
Sergunb 0:8918a71cdbe9 85 * @param[in] ipAddr IPv4 address
Sergunb 0:8918a71cdbe9 86 * @return Error code
Sergunb 0:8918a71cdbe9 87 **/
Sergunb 0:8918a71cdbe9 88
Sergunb 0:8918a71cdbe9 89 error_t mibEncodeIpv4Addr(uint8_t *oid, size_t maxOidLen, size_t *pos, Ipv4Addr ipAddr)
Sergunb 0:8918a71cdbe9 90 {
Sergunb 0:8918a71cdbe9 91 error_t error;
Sergunb 0:8918a71cdbe9 92 uint_t i;
Sergunb 0:8918a71cdbe9 93 uint8_t *p;
Sergunb 0:8918a71cdbe9 94
Sergunb 0:8918a71cdbe9 95 //Cast the IPv4 address as a byte array
Sergunb 0:8918a71cdbe9 96 p = (uint8_t *) &ipAddr;
Sergunb 0:8918a71cdbe9 97
Sergunb 0:8918a71cdbe9 98 //The address is encoded as 4 subsequent sub-identifiers
Sergunb 0:8918a71cdbe9 99 for(i = 0; i < 4; i++)
Sergunb 0:8918a71cdbe9 100 {
Sergunb 0:8918a71cdbe9 101 //Encode the current byte
Sergunb 0:8918a71cdbe9 102 error = oidEncodeSubIdentifier(oid, maxOidLen, pos, p[i]);
Sergunb 0:8918a71cdbe9 103 //Any error to report?
Sergunb 0:8918a71cdbe9 104 if(error)
Sergunb 0:8918a71cdbe9 105 return error;
Sergunb 0:8918a71cdbe9 106 }
Sergunb 0:8918a71cdbe9 107
Sergunb 0:8918a71cdbe9 108 //Successful processing
Sergunb 0:8918a71cdbe9 109 return NO_ERROR;
Sergunb 0:8918a71cdbe9 110 }
Sergunb 0:8918a71cdbe9 111
Sergunb 0:8918a71cdbe9 112
Sergunb 0:8918a71cdbe9 113 /**
Sergunb 0:8918a71cdbe9 114 * @brief Decode instance identifier (IPv4 address)
Sergunb 0:8918a71cdbe9 115 * @param[in] oid Pointer to the object identifier
Sergunb 0:8918a71cdbe9 116 * @param[in] oidLen Length of the OID, in bytes
Sergunb 0:8918a71cdbe9 117 * @param[in,out] pos Offset where to read the instance identifier
Sergunb 0:8918a71cdbe9 118 * @param[out] ipAddr IPv4 address
Sergunb 0:8918a71cdbe9 119 * @return Error code
Sergunb 0:8918a71cdbe9 120 **/
Sergunb 0:8918a71cdbe9 121
Sergunb 0:8918a71cdbe9 122 error_t mibDecodeIpv4Addr(const uint8_t *oid, size_t oidLen, size_t *pos, Ipv4Addr *ipAddr)
Sergunb 0:8918a71cdbe9 123 {
Sergunb 0:8918a71cdbe9 124 error_t error;
Sergunb 0:8918a71cdbe9 125 uint_t i;
Sergunb 0:8918a71cdbe9 126 uint32_t value;
Sergunb 0:8918a71cdbe9 127 uint8_t *p;
Sergunb 0:8918a71cdbe9 128
Sergunb 0:8918a71cdbe9 129 //Cast the IPv4 address as a byte array
Sergunb 0:8918a71cdbe9 130 p = (uint8_t *) ipAddr;
Sergunb 0:8918a71cdbe9 131
Sergunb 0:8918a71cdbe9 132 //The address is encoded as 4 subsequent sub-identifiers
Sergunb 0:8918a71cdbe9 133 for(i = 0; i < 4; i++)
Sergunb 0:8918a71cdbe9 134 {
Sergunb 0:8918a71cdbe9 135 //Decode the current sub-identifier
Sergunb 0:8918a71cdbe9 136 error = oidDecodeSubIdentifier(oid, oidLen, pos, &value);
Sergunb 0:8918a71cdbe9 137 //Invalid sub-identifier?
Sergunb 0:8918a71cdbe9 138 if(error)
Sergunb 0:8918a71cdbe9 139 return error;
Sergunb 0:8918a71cdbe9 140
Sergunb 0:8918a71cdbe9 141 //Each byte of the IPv4 address must be in the range 0-255
Sergunb 0:8918a71cdbe9 142 if(value > 255)
Sergunb 0:8918a71cdbe9 143 return ERROR_INSTANCE_NOT_FOUND;
Sergunb 0:8918a71cdbe9 144
Sergunb 0:8918a71cdbe9 145 //Save the current byte
Sergunb 0:8918a71cdbe9 146 p[i] = value & 0xFF;
Sergunb 0:8918a71cdbe9 147 }
Sergunb 0:8918a71cdbe9 148
Sergunb 0:8918a71cdbe9 149 //Successful processing
Sergunb 0:8918a71cdbe9 150 return NO_ERROR;
Sergunb 0:8918a71cdbe9 151 }
Sergunb 0:8918a71cdbe9 152
Sergunb 0:8918a71cdbe9 153
Sergunb 0:8918a71cdbe9 154 /**
Sergunb 0:8918a71cdbe9 155 * @brief Encode instance identifier (port number)
Sergunb 0:8918a71cdbe9 156 * @param[in] oid Pointer to the object identifier
Sergunb 0:8918a71cdbe9 157 * @param[in] maxOidLen Maximum number of bytes the OID can hold
Sergunb 0:8918a71cdbe9 158 * @param[in,out] pos Offset where to write the instance identifier
Sergunb 0:8918a71cdbe9 159 * @param[in] port Port number
Sergunb 0:8918a71cdbe9 160 * @return Error code
Sergunb 0:8918a71cdbe9 161 **/
Sergunb 0:8918a71cdbe9 162
Sergunb 0:8918a71cdbe9 163 error_t mibEncodePort(uint8_t *oid, size_t maxOidLen, size_t *pos, uint16_t port)
Sergunb 0:8918a71cdbe9 164 {
Sergunb 0:8918a71cdbe9 165 //Encode instance identifier
Sergunb 0:8918a71cdbe9 166 return oidEncodeSubIdentifier(oid, maxOidLen, pos, port);
Sergunb 0:8918a71cdbe9 167 }
Sergunb 0:8918a71cdbe9 168
Sergunb 0:8918a71cdbe9 169
Sergunb 0:8918a71cdbe9 170 /**
Sergunb 0:8918a71cdbe9 171 * @brief Decode instance identifier (port number)
Sergunb 0:8918a71cdbe9 172 * @param[in] oid Pointer to the object identifier
Sergunb 0:8918a71cdbe9 173 * @param[in] oidLen Length of the OID, in bytes
Sergunb 0:8918a71cdbe9 174 * @param[in,out] pos Offset where to read the instance identifier
Sergunb 0:8918a71cdbe9 175 * @param[out] port Port number
Sergunb 0:8918a71cdbe9 176 * @return Error code
Sergunb 0:8918a71cdbe9 177 **/
Sergunb 0:8918a71cdbe9 178
Sergunb 0:8918a71cdbe9 179 error_t mibDecodePort(const uint8_t *oid, size_t oidLen, size_t *pos, uint16_t *port)
Sergunb 0:8918a71cdbe9 180 {
Sergunb 0:8918a71cdbe9 181 error_t error;
Sergunb 0:8918a71cdbe9 182 uint32_t value;
Sergunb 0:8918a71cdbe9 183
Sergunb 0:8918a71cdbe9 184 //Decode instance identifier
Sergunb 0:8918a71cdbe9 185 error = oidDecodeSubIdentifier(oid, oidLen, pos, &value);
Sergunb 0:8918a71cdbe9 186 //Invalid sub-identifier?
Sergunb 0:8918a71cdbe9 187 if(error)
Sergunb 0:8918a71cdbe9 188 return error;
Sergunb 0:8918a71cdbe9 189
Sergunb 0:8918a71cdbe9 190 //Port number must be in range 0-65535
Sergunb 0:8918a71cdbe9 191 if(value > 65535)
Sergunb 0:8918a71cdbe9 192 return ERROR_INSTANCE_NOT_FOUND;
Sergunb 0:8918a71cdbe9 193
Sergunb 0:8918a71cdbe9 194 //Save port number
Sergunb 0:8918a71cdbe9 195 *port = value;
Sergunb 0:8918a71cdbe9 196
Sergunb 0:8918a71cdbe9 197 //Successful processing
Sergunb 0:8918a71cdbe9 198 return NO_ERROR;
Sergunb 0:8918a71cdbe9 199 }
Sergunb 0:8918a71cdbe9 200