mbed based IoT Gateway More details http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Committer:
SomeRandomBloke
Date:
Wed May 09 20:29:30 2012 +0000
Revision:
5:0dbc27a7af55
Parent:
0:a29a0225f203
Reduced debug output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:a29a0225f203 1 /** IoT Gateway RFM12B payload V2 format handling
SomeRandomBloke 0:a29a0225f203 2 *
SomeRandomBloke 0:a29a0225f203 3 * @author Andrew Lindsay
SomeRandomBloke 0:a29a0225f203 4 *
SomeRandomBloke 0:a29a0225f203 5 * @section LICENSE
SomeRandomBloke 0:a29a0225f203 6 *
SomeRandomBloke 0:a29a0225f203 7 * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:a29a0225f203 8 *
SomeRandomBloke 0:a29a0225f203 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:a29a0225f203 10 * of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:a29a0225f203 11 * in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:a29a0225f203 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:a29a0225f203 13 * copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:a29a0225f203 14 * furnished to do so, subject to the following conditions:
SomeRandomBloke 0:a29a0225f203 15
SomeRandomBloke 0:a29a0225f203 16 * The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:a29a0225f203 17 * all copies or substantial portions of the Software.
SomeRandomBloke 0:a29a0225f203 18 *
SomeRandomBloke 0:a29a0225f203 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:a29a0225f203 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:a29a0225f203 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:a29a0225f203 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:a29a0225f203 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:a29a0225f203 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:a29a0225f203 25 * THE SOFTWARE.
SomeRandomBloke 0:a29a0225f203 26 *
SomeRandomBloke 0:a29a0225f203 27 * @section DESCRIPTION
SomeRandomBloke 0:a29a0225f203 28 *
SomeRandomBloke 0:a29a0225f203 29 *
SomeRandomBloke 0:a29a0225f203 30 */
SomeRandomBloke 0:a29a0225f203 31
SomeRandomBloke 0:a29a0225f203 32 #include "mbed.h"
SomeRandomBloke 0:a29a0225f203 33 #include "PayloadV2.h"
SomeRandomBloke 0:a29a0225f203 34
SomeRandomBloke 0:a29a0225f203 35
SomeRandomBloke 0:a29a0225f203 36 // Constructors for a new 21 payload
SomeRandomBloke 0:a29a0225f203 37 PayloadV2::PayloadV2( ) : PayloadDef() {}
SomeRandomBloke 0:a29a0225f203 38
SomeRandomBloke 0:a29a0225f203 39 PayloadV2::PayloadV2( uint8_t *pptr, short plen ): PayloadDef( pptr, plen ) {
SomeRandomBloke 0:a29a0225f203 40 uint8_t *ptr = pptr;
SomeRandomBloke 0:a29a0225f203 41 ptr += 4;
SomeRandomBloke 0:a29a0225f203 42 // Scan through payload counting readings
SomeRandomBloke 0:a29a0225f203 43 while ( ptr < (pptr + plen) ) {
SomeRandomBloke 0:a29a0225f203 44 switch ( *ptr & 0xf0 ) {
SomeRandomBloke 0:a29a0225f203 45 case 0x00: // Byte
SomeRandomBloke 0:a29a0225f203 46 readingCount++;
SomeRandomBloke 0:a29a0225f203 47 ptr += 4;
SomeRandomBloke 0:a29a0225f203 48 break;
SomeRandomBloke 0:a29a0225f203 49 case 0x10: // short, 16 bit
SomeRandomBloke 0:a29a0225f203 50 readingCount++;
SomeRandomBloke 0:a29a0225f203 51 ptr += 5;
SomeRandomBloke 0:a29a0225f203 52 break;
SomeRandomBloke 0:a29a0225f203 53 case 0x20: // 32 bit
SomeRandomBloke 0:a29a0225f203 54 readingCount++;
SomeRandomBloke 0:a29a0225f203 55 ptr += 7;
SomeRandomBloke 0:a29a0225f203 56 break;
SomeRandomBloke 0:a29a0225f203 57 case 0x30: // string
SomeRandomBloke 0:a29a0225f203 58 readingCount++;
SomeRandomBloke 0:a29a0225f203 59 ptr += 2;
SomeRandomBloke 0:a29a0225f203 60 // printf("String %d\n", *ptr);
SomeRandomBloke 0:a29a0225f203 61 if ( *ptr <= 40 )
SomeRandomBloke 0:a29a0225f203 62 ptr += *ptr;
SomeRandomBloke 0:a29a0225f203 63 break;
SomeRandomBloke 0:a29a0225f203 64 }
SomeRandomBloke 0:a29a0225f203 65 }
SomeRandomBloke 0:a29a0225f203 66 }
SomeRandomBloke 0:a29a0225f203 67
SomeRandomBloke 0:a29a0225f203 68 short PayloadV2::numReadings() {
SomeRandomBloke 0:a29a0225f203 69 return readingCount;
SomeRandomBloke 0:a29a0225f203 70 }
SomeRandomBloke 0:a29a0225f203 71
SomeRandomBloke 0:a29a0225f203 72 uint8_t *PayloadV2::readingPtr( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 73 uint8_t *ptr = payloadBuffer;
SomeRandomBloke 0:a29a0225f203 74 short currentReading = 0;
SomeRandomBloke 0:a29a0225f203 75
SomeRandomBloke 0:a29a0225f203 76 // for(int i=0; i<payloadLen; i++ )
SomeRandomBloke 0:a29a0225f203 77 // printf("%02X ",*ptr++);
SomeRandomBloke 0:a29a0225f203 78 // ptr = payloadBuffer;
SomeRandomBloke 0:a29a0225f203 79 // printf("\n");
SomeRandomBloke 0:a29a0225f203 80 ptr += 4;
SomeRandomBloke 0:a29a0225f203 81
SomeRandomBloke 0:a29a0225f203 82 // Scan through payload readings
SomeRandomBloke 0:a29a0225f203 83 while ( ptr < (payloadBuffer + payloadLen) ) {
SomeRandomBloke 0:a29a0225f203 84 switch ( *ptr & 0xf0 ) {
SomeRandomBloke 0:a29a0225f203 85 case 0x00: //
SomeRandomBloke 0:a29a0225f203 86 if ( currentReading == readingNum )
SomeRandomBloke 0:a29a0225f203 87 return ptr;
SomeRandomBloke 0:a29a0225f203 88 currentReading++;
SomeRandomBloke 0:a29a0225f203 89 ptr += 3;
SomeRandomBloke 0:a29a0225f203 90 break;
SomeRandomBloke 0:a29a0225f203 91 case 0x10: //
SomeRandomBloke 0:a29a0225f203 92 if ( currentReading == readingNum )
SomeRandomBloke 0:a29a0225f203 93 return ptr;
SomeRandomBloke 0:a29a0225f203 94 currentReading++;
SomeRandomBloke 0:a29a0225f203 95 ptr += 4;
SomeRandomBloke 0:a29a0225f203 96 break;
SomeRandomBloke 0:a29a0225f203 97 case 0x20: //
SomeRandomBloke 0:a29a0225f203 98 if ( currentReading == readingNum )
SomeRandomBloke 0:a29a0225f203 99 return ptr;
SomeRandomBloke 0:a29a0225f203 100 currentReading++;
SomeRandomBloke 0:a29a0225f203 101 ptr += 6;
SomeRandomBloke 0:a29a0225f203 102 break;
SomeRandomBloke 0:a29a0225f203 103 case 0x30: //
SomeRandomBloke 0:a29a0225f203 104 if ( currentReading == readingNum )
SomeRandomBloke 0:a29a0225f203 105 return ptr;
SomeRandomBloke 0:a29a0225f203 106 currentReading++;
SomeRandomBloke 0:a29a0225f203 107 ptr += 2;
SomeRandomBloke 0:a29a0225f203 108 if ( *ptr <= 40 )
SomeRandomBloke 0:a29a0225f203 109 ptr += *ptr;
SomeRandomBloke 0:a29a0225f203 110 break;
SomeRandomBloke 0:a29a0225f203 111 default:
SomeRandomBloke 0:a29a0225f203 112 ptr++;
SomeRandomBloke 0:a29a0225f203 113 }
SomeRandomBloke 0:a29a0225f203 114 }
SomeRandomBloke 0:a29a0225f203 115 return NULL;
SomeRandomBloke 0:a29a0225f203 116 }
SomeRandomBloke 0:a29a0225f203 117
SomeRandomBloke 0:a29a0225f203 118 int8_t PayloadV2::readingByte( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 119 uint8_t *ptr = readingPtr( readingNum );
SomeRandomBloke 0:a29a0225f203 120
SomeRandomBloke 0:a29a0225f203 121 payloadv2ByteReading *bptr = (payloadv2ByteReading*)ptr;
SomeRandomBloke 0:a29a0225f203 122 return bptr->reading;
SomeRandomBloke 0:a29a0225f203 123 }
SomeRandomBloke 0:a29a0225f203 124
SomeRandomBloke 0:a29a0225f203 125 short PayloadV2::readingShort( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 126 uint8_t *ptr = readingPtr( readingNum );
SomeRandomBloke 0:a29a0225f203 127 payloadv2ShortReading *bptr = (payloadv2ShortReading*)ptr;
SomeRandomBloke 0:a29a0225f203 128 return bptr->reading;
SomeRandomBloke 0:a29a0225f203 129 }
SomeRandomBloke 0:a29a0225f203 130
SomeRandomBloke 0:a29a0225f203 131 int PayloadV2::reading( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 132 return( readingLong( readingNum ) );
SomeRandomBloke 0:a29a0225f203 133 }
SomeRandomBloke 0:a29a0225f203 134
SomeRandomBloke 0:a29a0225f203 135 int PayloadV2::readingLong( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 136 uint8_t *ptr = readingPtr( readingNum );
SomeRandomBloke 0:a29a0225f203 137
SomeRandomBloke 0:a29a0225f203 138 payloadv2LongReading *bptr = (payloadv2LongReading*)ptr;
SomeRandomBloke 0:a29a0225f203 139 // printf("Long Reading %ld\n",bptr->reading);
SomeRandomBloke 0:a29a0225f203 140 return bptr->reading;
SomeRandomBloke 0:a29a0225f203 141 }
SomeRandomBloke 0:a29a0225f203 142
SomeRandomBloke 0:a29a0225f203 143 short PayloadV2::readingType( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 144 uint8_t *ptr = readingPtr( readingNum );
SomeRandomBloke 0:a29a0225f203 145 payloadv2Header *bptr = (payloadv2Header*)ptr;
SomeRandomBloke 0:a29a0225f203 146 return bptr->typeId >>4;
SomeRandomBloke 0:a29a0225f203 147 }
SomeRandomBloke 0:a29a0225f203 148
SomeRandomBloke 0:a29a0225f203 149 short PayloadV2::sensorId( short readingNum ) {
SomeRandomBloke 0:a29a0225f203 150 uint8_t *ptr = readingPtr( readingNum );
SomeRandomBloke 0:a29a0225f203 151
SomeRandomBloke 0:a29a0225f203 152 payloadv2Header *bptr = (payloadv2Header*)ptr;
SomeRandomBloke 0:a29a0225f203 153
SomeRandomBloke 0:a29a0225f203 154 // printf("Sensor ID from pkt is %d, values %2x %2x\n", (bptr->typeId & 0x0f << 8) | bptr->id,bptr->typeId, bptr->id );
SomeRandomBloke 0:a29a0225f203 155 return (bptr->typeId & 0x0f << 8) | bptr->id;
SomeRandomBloke 0:a29a0225f203 156 }