SmartSnippets DA1459x SDK
svc_types.h
Go to the documentation of this file.
1 
43 #ifndef SVC_TYPES_H_
44 #define SVC_TYPES_H_
45 
46 #include <stdint.h>
47 #include <ble_bufops.h>
48 
49 #define SVC_IEEE11073_SFLOAT_NAN (0x07FF)
50 #define SVC_IEEE11073_SFLOAT_NRES (0x0800)
51 #define SVC_IEEE11073_SFLOAT_PLUS_INFINITY (0x07FE)
52 #define SVC_IEEE11073_SFLOAT_MINUS_INFINITY (0x0802)
53 
54 #define SVC_IEEE11073_FLOAT_NAN (0x007FFFFF)
55 #define SVC_IEEE11073_FLOAT_NRES (0x00800000)
56 #define SVC_IEEE11073_FLOAT_PLUS_INFINITY (0x007FFFFE)
57 #define SVC_IEEE11073_FLOAT_MINUS_INFINITY (0x00800002)
58 
62 typedef struct {
63  uint16_t year;
64  uint8_t month;
65  uint8_t day;
66  uint8_t hours;
67  uint8_t minutes;
68  uint8_t seconds;
70 
74 typedef struct {
75  int8_t exp;
76  int32_t mantissa;
78 
88 __STATIC_INLINE void pack_date_time(const svc_date_time_t *date_time, uint8_t **ptr)
89 {
90  put_u16_inc(ptr, date_time->year);
91  put_u8_inc(ptr, date_time->month);
92  put_u8_inc(ptr, date_time->day);
93  put_u8_inc(ptr, date_time->hours);
94  put_u8_inc(ptr, date_time->minutes);
95  put_u8_inc(ptr, date_time->seconds);
96 }
97 
107 __STATIC_INLINE void unpack_date_time(const uint8_t **ptr, svc_date_time_t *date_time)
108 {
109  date_time->year = get_u16_inc(ptr);
110  date_time->month = get_u8_inc(ptr);
111  date_time->day = get_u8_inc(ptr);
112  date_time->hours = get_u8_inc(ptr);
113  date_time->minutes = get_u8_inc(ptr);
114  date_time->seconds = get_u8_inc(ptr);
115 }
116 
125 __STATIC_INLINE void float_to_ieee11703(float val, int8_t precision, svc_ieee11073_float_t *ieee_val)
126 {
127  ieee_val->exp = -precision;
128 
129  if (precision > 0) {
130  while (precision) {
131  val *= 10;
132  precision--;
133  }
134  }
135 
136  if (precision < 0) {
137  while (precision) {
138  val /= 10;
139  precision++;
140  }
141  }
142 
143  ieee_val->mantissa = (int32_t) val;
144 }
145 
154 __STATIC_INLINE float ieee11703_to_float(const svc_ieee11073_float_t *value)
155 {
156  float float_value;
157  int8_t exp;
158 
159  float_value = value->mantissa;
160  exp = value->exp;
161 
162  while (exp > 0) {
163  float_value *= 10;
164  exp--;
165  }
166 
167  while (exp < 0) {
168  float_value /= 10;
169  exp++;
170  }
171 
172  return float_value;
173 }
174 
183 __STATIC_INLINE uint16_t pack_ieee11703_sfloat(const svc_ieee11073_float_t *val)
184 {
185  /*
186  * ISO/IEEE Std. 11073-20601™-2008 defines SFLOAT-Type data type which consist of 12-bit
187  * mantissa and 4-bit exponent in below order:
188  * Exponent Mantissa
189  * 4 bit 12 bit
190  */
191 
192  /* maximum exponent = 7
193  * minimum exponent = -8 (signed 4 bits)
194  */
195  if (val->exp > 7 || val->exp < -8) {
196  return SVC_IEEE11073_SFLOAT_NRES;
197  }
198 
199  /* maximum mantissa = 2047 - 0x000007FF | +infinity
200  * minimum mantissa = -2048 - 0xFFFFF800 | -infinity
201  */
202  if (val->mantissa > 2047 || val->mantissa < -2048) {
203  return SVC_IEEE11073_SFLOAT_NRES;
204  }
205 
206  return ((val->exp & 0x0F) << 12) | (val->mantissa & 0x0FFF);
207 }
208 
216 __STATIC_INLINE void unpack_ieee11703_sfloat(uint16_t sfloat_val, svc_ieee11073_float_t *val)
217 {
218  /*
219  * ISO/IEEE Std. 11073-20601™-2008 defines SFLOAT-Type data type which consist of 12-bit
220  * mantissa and 4-bit exponent in below order:
221  * Exponent Mantissa
222  * 4 bits 12 bits
223  */
224 
225  /*
226  * mantissa and exponent are signed values so the shifts have to be done
227  * with signed value of sfloat_val.
228  */
229  val->mantissa = ((int16_t) (sfloat_val << 4) >> 4);
230  val->exp = ((int16_t) sfloat_val >> 12);
231 }
232 
241 __STATIC_INLINE uint32_t pack_ieee11703_float(const svc_ieee11073_float_t *val)
242 {
243  /*
244  * ISO/IEEE Std. 11073-20601™-2008 defines FLOAT-Type data type which consist of 24-bit
245  * mantissa and 8-bit exponent in below order:
246  * Exponent Mantissa
247  * 1 octet 3 octets
248  */
249 
250  /* maximum exponent = 127
251  * minimum exponent = -128 (signed 1 octet)
252  */
253  if (val->exp > 127 || val->exp < -128) {
254  return SVC_IEEE11073_FLOAT_NRES;
255  }
256 
257  /* maximum mantissa = 8388607 - 0x007FFFFF | +infinity
258  * minimum mantissa = -8388608 - 0xFF800000 | -infinity
259  */
260  if (val->mantissa > 8388607 || val->mantissa < -8388608) {
261  return SVC_IEEE11073_FLOAT_NRES;
262  }
263 
264  return (val->exp << 24) | (val->mantissa & 0xFFFFFF);
265 }
266 
274 __STATIC_INLINE void unpack_ieee11703_float(uint32_t float_val, svc_ieee11073_float_t *val)
275 {
276  /*
277  * ISO/IEEE Std. 11073-20601™-2008 defines FLOAT-Type data type which consist of 24-bit
278  * mantissa and 8-bit exponent in below order:
279  * Exponent Mantissa
280  * 1 octet 3 octets
281  */
282 
283  /*
284  * mantissa and exponent are signed values so the shifts have to be done
285  * with signed value of float_val.
286  */
287  val->mantissa = ((int32_t) (float_val << 8) >> 8);
288  val->exp = ((int32_t) float_val >> 24);
289 }
290 
291 #endif /* SVC_TYPES_H_ */
292 
unpack_ieee11703_float
__STATIC_INLINE void unpack_ieee11703_float(uint32_t float_val, svc_ieee11073_float_t *val)
Unpack FLOAT-Type to IEEE 11073 value.
Definition: svc_types.h:274
get_u8_inc
__STATIC_INLINE uint8_t get_u8_inc(const uint8_t **buffer)
Get uint8 from buffer and increase pointer.
Definition: ble_bufops.h:92
svc_date_time_t::seconds
uint8_t seconds
Definition: svc_types.h:68
unpack_date_time
__STATIC_INLINE void unpack_date_time(const uint8_t **ptr, svc_date_time_t *date_time)
Unpack date time.
Definition: svc_types.h:107
get_u16_inc
__STATIC_INLINE uint16_t get_u16_inc(const uint8_t **buffer)
Get uint16 from buffer and increase pointer.
Definition: ble_bufops.h:109
pack_ieee11703_float
__STATIC_INLINE uint32_t pack_ieee11703_float(const svc_ieee11073_float_t *val)
Pack IEEE 11073 value to FLOAT-Type.
Definition: svc_types.h:241
svc_date_time_t::minutes
uint8_t minutes
Definition: svc_types.h:67
float_to_ieee11703
__STATIC_INLINE void float_to_ieee11703(float val, int8_t precision, svc_ieee11073_float_t *ieee_val)
Convert float value to ISO/IEEE Std. 11073-20601™-2008 standard.
Definition: svc_types.h:125
pack_date_time
__STATIC_INLINE void pack_date_time(const svc_date_time_t *date_time, uint8_t **ptr)
Pack date time.
Definition: svc_types.h:88
put_u16_inc
__STATIC_INLINE void put_u16_inc(uint8_t **buffer, uint16_t value)
Put uint16 to buffer and increase pointer.
Definition: ble_bufops.h:198
svc_date_time_t
Definition: svc_types.h:62
svc_date_time_t::day
uint8_t day
Definition: svc_types.h:65
svc_ieee11073_float_t
Definition: svc_types.h:74
ieee11703_to_float
__STATIC_INLINE float ieee11703_to_float(const svc_ieee11073_float_t *value)
Convert ISO/IEEE Std. 11073-20601™-2008 standard to float value.
Definition: svc_types.h:154
svc_date_time_t::month
uint8_t month
Definition: svc_types.h:64
svc_date_time_t::year
uint16_t year
Definition: svc_types.h:63
svc_date_time_t::hours
uint8_t hours
Definition: svc_types.h:66
unpack_ieee11703_sfloat
__STATIC_INLINE void unpack_ieee11703_sfloat(uint16_t sfloat_val, svc_ieee11073_float_t *val)
Unpack SFLOAT-Type to IEEE 11073 value.
Definition: svc_types.h:216
put_u8_inc
__STATIC_INLINE void put_u8_inc(uint8_t **buffer, uint8_t value)
Put uint8 to buffer and increase pointer.
Definition: ble_bufops.h:182
pack_ieee11703_sfloat
__STATIC_INLINE uint16_t pack_ieee11703_sfloat(const svc_ieee11073_float_t *val)
Pack IEEE 11073 value to SFLOAT-Type.
Definition: svc_types.h:183
ble_bufops.h
Helpers to put and get data from BLE buffers.