| 1 | #include "ProtocolEverflourish.h" |
|---|
| 2 | #include <sstream> |
|---|
| 3 | #include <stdio.h> |
|---|
| 4 | #include "ControllerMessage.h" |
|---|
| 5 | |
|---|
| 6 | int ProtocolEverflourish::methods() const { |
|---|
| 7 | return TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_LEARN; |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | std::string ProtocolEverflourish::getStringForMethod(int method, unsigned char, Controller *) { |
|---|
| 11 | unsigned int deviceCode = this->getIntParameter(L"house", 0, 16383); |
|---|
| 12 | unsigned int intCode = this->getIntParameter(L"unit", 1, 4)-1; |
|---|
| 13 | unsigned char action; |
|---|
| 14 | |
|---|
| 15 | if (method == TELLSTICK_TURNON) { |
|---|
| 16 | action = 15; |
|---|
| 17 | } else if (method == TELLSTICK_TURNOFF) { |
|---|
| 18 | action = 0; |
|---|
| 19 | } else if (method == TELLSTICK_LEARN) { |
|---|
| 20 | action = 10; |
|---|
| 21 | } else { |
|---|
| 22 | return ""; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | const char ssss = 85; |
|---|
| 26 | const char sssl = 84; // 0 |
|---|
| 27 | const char slss = 69; // 1 |
|---|
| 28 | |
|---|
| 29 | const char bits[2] = {sssl,slss}; |
|---|
| 30 | int i, check; |
|---|
| 31 | |
|---|
| 32 | std::string strCode; |
|---|
| 33 | |
|---|
| 34 | deviceCode = (deviceCode << 2) | intCode; |
|---|
| 35 | |
|---|
| 36 | check = calculateChecksum(deviceCode); |
|---|
| 37 | |
|---|
| 38 | char preamble[] = {'R', 5, 'T', 114,60,1,1,105,ssss,ssss,0}; |
|---|
| 39 | strCode.append(preamble); |
|---|
| 40 | |
|---|
| 41 | for(i=15;i>=0;i--) { |
|---|
| 42 | strCode.append(1, bits[(deviceCode>>i)&0x01]); |
|---|
| 43 | } |
|---|
| 44 | for(i=3;i>=0;i--) { |
|---|
| 45 | strCode.append(1, bits[(check>>i)&0x01]); |
|---|
| 46 | } |
|---|
| 47 | for(i=3;i>=0;i--) { |
|---|
| 48 | strCode.append(1, bits[(action>>i)&0x01]); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | strCode.append(1, ssss); |
|---|
| 52 | strCode.append(1, '+'); |
|---|
| 53 | |
|---|
| 54 | return strCode; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | // The calculation used in this function is provided by Frank Stevenson |
|---|
| 58 | unsigned int ProtocolEverflourish::calculateChecksum(unsigned int x) { |
|---|
| 59 | unsigned int bits[16] = { |
|---|
| 60 | 0xf ,0xa ,0x7 ,0xe, |
|---|
| 61 | 0xf ,0xd ,0x9 ,0x1, |
|---|
| 62 | 0x1 ,0x2 ,0x4 ,0x8, |
|---|
| 63 | 0x3 ,0x6 ,0xc ,0xb |
|---|
| 64 | }; |
|---|
| 65 | unsigned int bit = 1; |
|---|
| 66 | unsigned int res = 0x5; |
|---|
| 67 | int i; |
|---|
| 68 | unsigned int lo,hi; |
|---|
| 69 | |
|---|
| 70 | if ((x&0x3)==3) { |
|---|
| 71 | lo = x & 0x00ff; |
|---|
| 72 | hi = x & 0xff00; |
|---|
| 73 | lo += 4; |
|---|
| 74 | if (lo>0x100) { |
|---|
| 75 | lo = 0x12; |
|---|
| 76 | } |
|---|
| 77 | x = lo | hi; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | for(i=0;i<16;i++) { |
|---|
| 81 | if (x&bit) { |
|---|
| 82 | res = res ^ bits[i]; |
|---|
| 83 | } |
|---|
| 84 | bit = bit << 1; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | return res; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | std::string ProtocolEverflourish::decodeData(ControllerMessage &dataMsg) |
|---|
| 91 | { |
|---|
| 92 | std::string data = dataMsg.getParameter("data"); |
|---|
| 93 | unsigned int allData; |
|---|
| 94 | unsigned int house = 0; |
|---|
| 95 | unsigned int unit = 0; |
|---|
| 96 | unsigned int method = 0; |
|---|
| 97 | |
|---|
| 98 | sscanf(data.c_str(), "%X", &allData); |
|---|
| 99 | |
|---|
| 100 | house = allData & 0xFFFC00; |
|---|
| 101 | house >>= 10; |
|---|
| 102 | |
|---|
| 103 | unit = allData & 0x300; |
|---|
| 104 | unit >>= 8; |
|---|
| 105 | unit++; //unit from 1 to 4 |
|---|
| 106 | |
|---|
| 107 | method = allData & 0xF; |
|---|
| 108 | |
|---|
| 109 | if(house < 0 || house > 16383 || unit < 1 || unit > 4){ |
|---|
| 110 | //not everflourish |
|---|
| 111 | return ""; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | std::stringstream retString; |
|---|
| 115 | retString << "class:command;protocol:everflourish;model:selflearning;house:" << house << ";unit:" << unit << ";method:"; |
|---|
| 116 | if(method == 0){ |
|---|
| 117 | retString << "turnoff;"; |
|---|
| 118 | } |
|---|
| 119 | else if(method == 15){ |
|---|
| 120 | retString << "turnon;"; |
|---|
| 121 | } |
|---|
| 122 | else if(method == 10){ |
|---|
| 123 | retString << "learn;"; |
|---|
| 124 | } |
|---|
| 125 | else { |
|---|
| 126 | //not everflourish |
|---|
| 127 | return ""; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | return retString.str(); |
|---|
| 131 | } |
|---|