Andrew Lindsay / Mbed 2 deprecated IoTGateway_Basic

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PayloadV2.cpp Source File

PayloadV2.cpp

00001 /** IoT Gateway RFM12B payload V2 format handling
00002  *
00003  * @author Andrew Lindsay
00004  *
00005  * @section LICENSE
00006  *
00007  * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015 
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  * 
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  * 
00027  * @section DESCRIPTION
00028  * 
00029  * 
00030  */
00031 
00032 #include "mbed.h"
00033 #include "PayloadV2.h"
00034 
00035 
00036 // Constructors for a new 21 payload
00037 PayloadV2::PayloadV2( ) : PayloadDef() {}
00038 
00039 PayloadV2::PayloadV2( uint8_t *pptr, short plen ): PayloadDef( pptr, plen ) {
00040     uint8_t *ptr = pptr;
00041     ptr += 4;
00042     // Scan through payload counting readings
00043     while ( ptr < (pptr + plen) ) {
00044         switch ( *ptr & 0xf0 ) {
00045             case 0x00:      // Byte
00046                 readingCount++;
00047                 ptr += 4;
00048                 break;
00049             case 0x10:      // short, 16 bit
00050                 readingCount++;
00051                 ptr += 5;
00052                 break;
00053             case 0x20:      // 32 bit
00054                 readingCount++;
00055                 ptr += 7;
00056                 break;
00057             case 0x30:      // string
00058                 readingCount++;
00059                 ptr += 2;
00060 //                printf("String %d\n", *ptr);
00061                 if ( *ptr <= 40 )
00062                     ptr += *ptr;
00063                 break;
00064         }
00065     }
00066 }
00067 
00068 short PayloadV2::numReadings() {
00069     return readingCount;
00070 }
00071 
00072 uint8_t *PayloadV2::readingPtr( short readingNum ) {
00073     uint8_t *ptr = payloadBuffer;
00074     short currentReading = 0;
00075     
00076 //    for(int i=0; i<payloadLen; i++ ) 
00077 //        printf("%02X ",*ptr++);
00078 //    ptr = payloadBuffer;
00079 //    printf("\n");
00080     ptr += 4;
00081 
00082     // Scan through payload readings
00083     while ( ptr < (payloadBuffer + payloadLen) ) {
00084         switch ( *ptr & 0xf0 ) {
00085             case 0x00:      //
00086                 if ( currentReading == readingNum )
00087                     return ptr;
00088                 currentReading++;
00089                 ptr += 3;
00090                 break;
00091             case 0x10:      //
00092                 if ( currentReading == readingNum )
00093                     return ptr;
00094                 currentReading++;
00095                 ptr += 4;
00096                 break;
00097             case 0x20:      //
00098                 if ( currentReading == readingNum )
00099                     return ptr;
00100                 currentReading++;
00101                 ptr += 6;
00102                 break;
00103             case 0x30:      //
00104                 if ( currentReading == readingNum )
00105                     return ptr;
00106                 currentReading++;
00107                 ptr += 2;
00108                 if ( *ptr <= 40 )
00109                     ptr += *ptr;
00110                 break;
00111             default:
00112                 ptr++;
00113         }
00114     }
00115     return NULL;
00116 }
00117 
00118 int8_t PayloadV2::readingByte( short readingNum ) {
00119     uint8_t *ptr = readingPtr( readingNum );
00120 
00121     payloadv2ByteReading *bptr = (payloadv2ByteReading*)ptr;
00122     return bptr->reading;
00123 }
00124 
00125 short PayloadV2::readingShort( short readingNum ) {
00126     uint8_t *ptr = readingPtr( readingNum );
00127     payloadv2ShortReading *bptr = (payloadv2ShortReading*)ptr;
00128     return bptr->reading;
00129 }
00130 
00131 int PayloadV2::reading( short readingNum ) {
00132     return( readingLong( readingNum ) );
00133 }
00134     
00135 int PayloadV2::readingLong( short readingNum ) {
00136     uint8_t *ptr = readingPtr( readingNum );
00137 
00138     payloadv2LongReading *bptr = (payloadv2LongReading*)ptr;
00139 //    printf("Long Reading %ld\n",bptr->reading);
00140     return bptr->reading;
00141 }
00142 
00143 short PayloadV2::readingType( short readingNum ) {
00144     uint8_t *ptr = readingPtr( readingNum );
00145     payloadv2Header *bptr = (payloadv2Header*)ptr;
00146     return bptr->typeId >>4;
00147 }
00148 
00149 short PayloadV2::sensorId( short readingNum ) {
00150     uint8_t *ptr = readingPtr( readingNum );
00151 
00152     payloadv2Header *bptr = (payloadv2Header*)ptr;
00153     
00154 //    printf("Sensor ID from pkt is %d, values %2x %2x\n", (bptr->typeId & 0x0f << 8) | bptr->id,bptr->typeId, bptr->id );
00155     return (bptr->typeId & 0x0f << 8) | bptr->id;
00156 }