/***********************************************************************************
    Filename: mbus_paket.h
***********************************************************************************/

#ifndef MBUS_PACKET_H
#define MBUS_PACKET_H

#include <stdint.h>
//----------------------------------------------------------------------------------
// Constants
//----------------------------------------------------------------------------------


#define PACKET_C_FIELD  0x44
#define MAN_CODE        0x5133 //Texas Instruments Hong Kong = "TIS"  32*32*2+ 32*9 +19 = 20787
#define MAN_NUMBER      0x11111111	//use for S-mode
//#define MAN_NUMBER      0x22222222		//use for T-mode
//#define MAN_NUMBER      0x33333333

#define MAN_ID          0x01
#define MAN_VER         0x07
#define PACKET_CI_FIELD 0x78


#define DIF_FIELD 0x0B
#define VIF_FIELD 0x13
#define DATA_FIELD_1 0x43
#define DATA_FIELD_2 0x65
#define DATA_FIELD_3 0x87

#define PACKET_OK              0
#define PACKET_CODING_ERROR    1
#define PACKET_CRC_ERROR       2

#define WMBUS_SMODE            1
#define WMBUS_TMODE            2
#define WMBUS_CMODE            3
#define WMBUS_FMODE            4
#define WMBUS_NabefMODE        5
#define WMBUS_NcdMODE          6
#define WMBUS_NgMODE           7
#define WMBUS_C2MODE           8

// Assumes FrmtA	for S and T-modes
// Assumes FrmtB for C, F and all N-modes (shorter telegram, less CRC16 fields (max 2 x CRC16), L-field includes CRC16
// which corresponds to the CC1120 packet engine in FIXED mode

//static const uint8_t WMbus_mode = 8;  //C2-OTHER mode  20.Feb'15 13:00
//static const uint8_t WMbus_mode = 7;  //Ng-mode  20.Feb'15 13:00

//static const uint8_t WMbus_mode = 6;  //Ncd-mode 20.Feb'15 13:00

//static const uint8_t WMbus_mode = 5;  //Nab-mode, OK on 19.Feb'15 15:30

//static const uint8_t WMbus_mode = 4; //F-mode at 433.820 MHz, works fine on 18.Feb 5:20pm
//static const uint8_t WMbus_mode = 3; //C, works fine on Feb.19 11:30am
//static const uint8_t WMbus_mode = 2;  //T-mode OK on 19.Feb'15, 14:45
static const unsigned char WMbus_mode = 1;  //S-mode OK on 18.Feb'15 15:00

//static const uint8_t Format = FrmtA; //S and T
//static const uint8_t Format = FrmtB; //C
//----------------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------------

void encodeTXPacket(uint8_t* pPacket, uint8_t* pData, uint8_t dataSize);

uint16_t decodeTXPacket(uint8_t* pByte, uint8_t* pPacket, uint16_t packetSize);

uint16_t packetSize(uint8_t lField);
uint16_t byteSize(uint8_t Smode, uint8_t transmit, uint16_t packetSize);

void   encodeTXBytesTmode(uint8_t* pByte, uint8_t* pPacket, uint16_t packetSize);
uint16_t decodeRXBytesTmode(uint8_t* pByte, uint8_t* pPacket, uint16_t packetSize);

void   encodeTXBytesSmode(uint8_t* pByte, uint8_t* pPacket, uint16_t packetSize);
uint16_t decodeRXBytesSmode(uint8_t* pByte, uint8_t* pPacket, uint16_t packetSize);

#endif


/***********************************************************************************
  Copyright 2008 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.
***********************************************************************************/

