Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Util.cpp Source File

Util.cpp

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 
00017 #include <stdio.h>
00018 #include <unistd.h>
00019 
00020 #include "LMqttsnClientApp.h"
00021 using namespace std;
00022 /*=====================================
00023         Global functions
00024  ======================================*/
00025 #ifndef CPU_BIGENDIANN
00026 
00027 /*--- For Little endianness ---*/
00028 
00029 uint16_t getUint16(const uint8_t* pos){
00030     uint16_t val = ((uint16_t)*pos++ << 8);
00031     return val += *pos;
00032 }
00033 
00034 void setUint16(uint8_t* pos, uint16_t val){
00035     *pos++ = (val >> 8) & 0xff;
00036     *pos   = val & 0xff;
00037 }
00038 
00039 uint32_t getUint32(const uint8_t* pos){
00040     uint32_t val = uint32_t(*pos++) <<  24;
00041     val += uint32_t(*pos++) << 16;
00042     val += uint32_t(*pos++) <<  8;
00043     return val += *pos++;
00044 }
00045 
00046 void setUint32(uint8_t* pos, uint32_t val){
00047     *pos++ = (val >> 24) & 0xff;
00048     *pos++ = (val >> 16) & 0xff;
00049     *pos++ = (val >>  8) & 0xff;
00050     *pos   =  val & 0xff;
00051 }
00052 
00053 float getFloat32(const uint8_t* pos){
00054     union{
00055         float flt;
00056         uint8_t d[4];
00057     }val;
00058     val.d[3] = *pos++;
00059     val.d[2] = *pos++;
00060     val.d[1] = *pos++;
00061     val.d[0] = *pos;
00062     return val.flt;
00063 }
00064 
00065 void setFloat32(uint8_t* pos, float flt){
00066     union{
00067         float flt;
00068         uint8_t d[4];
00069     }val;
00070     val.flt = flt;
00071     *pos++ = val.d[3];
00072     *pos++ = val.d[2];
00073     *pos++ = val.d[1];
00074     *pos   = val.d[0];
00075 }
00076 
00077 #else
00078 
00079 /*--- For Big endianness ---*/
00080 
00081 uint16_t getUint16(const uint8_t* pos){
00082   uint16_t val = *pos++;
00083   return val += ((uint16_t)*pos++ << 8);
00084 }
00085 
00086 void setUint16(uint8_t* pos, uint16_t val){
00087     *pos++ =  val & 0xff;
00088     *pos   = (val >>  8) & 0xff;
00089 }
00090 
00091 uint32_t getUint32(const uint8_t* pos){
00092     long val = uint32_t(*(pos + 3)) << 24;
00093     val += uint32_t(*(pos + 2)) << 16;
00094     val += uint32_t(*(pos + 1)) <<  8;
00095     return val += *pos;
00096 }
00097 
00098 void setUint32(uint8_t* pos, uint32_t val){
00099     *pos++ =  val & 0xff;
00100     *pos++ = (val >>  8) & 0xff;
00101     *pos++ = (val >> 16) & 0xff;
00102     *pos   = (val >> 24) & 0xff;
00103 }
00104 
00105 float getFloat32(const uint8_t* pos){
00106     union{
00107         float flt;
00108         uint8_t d[4];
00109     }val;
00110 
00111     val.d[0] = *pos++;
00112     val.d[1] = *pos++;
00113     val.d[2] = *pos++;
00114     val.d[3] = *pos;
00115     return val.flt;
00116 }
00117 
00118 void setFloat32(uint8_t* pos, float flt){
00119     union{
00120         float flt;
00121         uint8_t d[4];
00122     }val;
00123     val.flt = flt;
00124     *pos++ = val.d[0];
00125     *pos++ = val.d[1];
00126     *pos++ = val.d[2];
00127     *pos   = val.d[3];
00128 }
00129 
00130 #endif  // CPU_LITTLEENDIANN
00131 
00132 
00133 
00134