MeterLogger
mqtt_utils.c
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2014, Tuan PM
3 * Email: tuanpm@live.com
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33 #include <string.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <math.h>
37 #include <stddef.h>
38 #include "mqtt_utils.h"
39 
40 
41 uint8_t ICACHE_FLASH_ATTR UTILS_IsIPV4 (int8_t *str)
42 {
43  uint8_t segs = 0; /* Segment count. */
44  uint8_t chcnt = 0; /* Character count within segment. */
45  uint8_t accum = 0; /* Accumulator for segment. */
46  /* Catch NULL pointer. */
47  if (str == 0)
48  return 0;
49  /* Process every character in string. */
50 
51  while (*str != '\0') {
52  /* Segment changeover. */
53 
54  if (*str == '.') {
55  /* Must have some digits in segment. */
56  if (chcnt == 0)
57  return 0;
58  /* Limit number of segments. */
59  if (++segs == 4)
60  return 0;
61  /* Reset segment values and restart loop. */
62  chcnt = accum = 0;
63  str++;
64  continue;
65  }
66 
67  /* Check numeric. */
68  if ((*str < '0') || (*str > '9'))
69  return 0;
70 
71  /* Accumulate and check segment. */
72 
73  if ((accum = accum * 10 + *str - '0') > 255)
74  return 0;
75  /* Advance other segment specific stuff and continue loop. */
76 
77  chcnt++;
78  str++;
79  }
80 
81  /* Check enough segments and enough characters in last segment. */
82 
83  if (segs != 3)
84  return 0;
85  if (chcnt == 0)
86  return 0;
87  /* Address okay. */
88 
89  return 1;
90 }
91 uint8_t ICACHE_FLASH_ATTR UTILS_StrToIP(const int8_t* str, void *ip)
92 {
93 
94  /* The count of the number of bytes processed. */
95  int i;
96  /* A pointer to the next digit to process. */
97  const char * start;
98 
99  start = str;
100  for (i = 0; i < 4; i++) {
101  /* The digit being processed. */
102  char c;
103  /* The value of this byte. */
104  int n = 0;
105  while (1) {
106  c = * start;
107  start++;
108  if (c >= '0' && c <= '9') {
109  n *= 10;
110  n += c - '0';
111  }
112  /* We insist on stopping at "." if we are still parsing
113  the first, second, or third numbers. If we have reached
114  the end of the numbers, we will allow any character. */
115  else if ((i < 3 && c == '.') || i == 3) {
116  break;
117  }
118  else {
119  return 0;
120  }
121  }
122  if (n >= 256) {
123  return 0;
124  }
125  ((uint8_t*)ip)[i] = n;
126  }
127  return 1;
128 
129 }
130 uint32_t ICACHE_FLASH_ATTR UTILS_Atoh(const int8_t *s)
131 {
132  uint32_t value = 0, digit;
133  int8_t c;
134 
135  while((c = *s++)){
136  if('0' <= c && c <= '9')
137  digit = c - '0';
138  else if('A' <= c && c <= 'F')
139  digit = c - 'A' + 10;
140  else if('a' <= c && c<= 'f')
141  digit = c - 'a' + 10;
142  else break;
143 
144  value = (value << 4) | digit;
145  }
146 
147  return value;
148 }
149 
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
uint8_t ICACHE_FLASH_ATTR UTILS_IsIPV4(int8_t *str)
Definition: mqtt_utils.c:41
uint8_t ICACHE_FLASH_ATTR UTILS_StrToIP(const int8_t *str, void *ip)
Definition: mqtt_utils.c:91
uint32_t ICACHE_FLASH_ATTR UTILS_Atoh(const int8_t *s)
Definition: mqtt_utils.c:130