summaryrefslogtreecommitdiffstats
path: root/Src/ScriptXml.cpp
blob: 5aa2be44cb4e27c022879d52dc84b57ee35ff289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Video On Demand Samples
 *
 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You may also obtain this software under a propriety license from Microchip.
 * Please contact Microchip for further information.
 *
 */

#include "SafeVector.h"
#include "Console.h"
#include "ScriptXml.h"

#define TAG_SCRIPT               ((xmlChar *)"script")
#define TAG_ACTION               ((xmlChar *)"action")
#define TAG_NAME                 ((xmlChar *)"name")
#define TAG_TYPE                 ((xmlChar *)"type")
#define TAG_FBLOCK_ID_HEX        ((xmlChar *)"fblock_id_hex")
#define TAG_FUNCTION_ID_HEX      ((xmlChar *)"function_id_hex")
#define TAG_OP_TYPE_REQUEST_HEX  ((xmlChar *)"op_type_request_hex")
#define TAG_OP_TYPE_RESPONSE_HEX ((xmlChar *)"op_type_response_hex")
#define TAG_PAYLOAD_HEX          ((xmlChar *)"payload_hex")
#define TAG_PAUSE_MS             ((xmlChar *)"pause_ms")

#define VAL_SEND_MCM             ((char*)"SEND_MCM")
#define VAL_PAUSE                ((char*)"PAUSE")

#define XML_STRCMP(x,y)          (strcmp(reinterpret_cast< const char* >(x), reinterpret_cast< const char* >(y)) == 0)

CSciptXml::CSciptXml( const char *szFile ) : CXml( szFile )
{

}

CSciptXml::CSciptXml( const char *szBuffer, uint32_t nBufferLen ) : CXml( szBuffer, nBufferLen )
{

}

bool CSciptXml::GetScripts( CSafeVector<CScript *> &allChannels )
{
    xmlChar szTmp[XML_DEFAULT_VALUE_LEN];
    bool success = false;
    CXml Xml( *( ( CXml * )this ) );
    success = Xml.SetToTopNode();

    while( success && Xml.FindNode( TAG_ACTION ) )
    {
        ConsolePrintf( PRIO_LOW, BLUE"--NEW ACTION--"RESETCOLOR"\n" );
        CScript *script = new CScript();
        if( NULL == script )
            break;

        szTmp[0] = '\0';
        success &= Xml.GetChildValue( TAG_NAME, szTmp, XML_DEFAULT_VALUE_LEN );
        if( success )
        {
            ConsolePrintf( PRIO_LOW, BLUE"Name: '%s'"RESETCOLOR"\n", szTmp );
            int nameLen = sizeof( xmlChar ) * ( xmlStrlen( szTmp ) + 1 );
            if( nameLen >= MAX_SCRIPT_NAME_LENGTH )
                nameLen = MAX_SCRIPT_NAME_LENGTH - 1;
            memcpy( script->scriptName, szTmp, nameLen );
            script->scriptName[nameLen] = '\0'; //Terminate string in any case
        }

        szTmp[0] = '\0';
        success &= Xml.GetChildValue( TAG_TYPE, szTmp );
        if( success )
        {
            ConsolePrintf( PRIO_LOW, BLUE"Type: %s"RESETCOLOR"\n", szTmp );
            if( XML_STRCMP(szTmp, VAL_SEND_MCM) )
            {
                script->scriptType = SCRIPT_MCM_SEND;
            }
            else if( XML_STRCMP(szTmp, VAL_PAUSE) )
            {
                script->scriptType = SCRIPT_PAUSE;
            }
            else
            {
                //Be tolerant to new types
                ConsolePrintf( PRIO_ERROR, YELLOW"CSciptXml: Ignoring unknown TYPE:'%s'"RESETCOLOR"\n", szTmp );
                delete script;
                Xml.FindNextNode();
                continue;
            }
        }

        if( SCRIPT_MCM_SEND == script->scriptType )
        {
            szTmp[0] = '\0';
            success &= Xml.GetChildValue( TAG_FBLOCK_ID_HEX, szTmp, XML_DEFAULT_VALUE_LEN );
            if( success )
            {
                script->fblockId = ConvertToInt( szTmp );
                ConsolePrintf( PRIO_LOW, BLUE"FBlock-ID: '%s=%d'"RESETCOLOR"\n", szTmp, script->fblockId );
            }

            szTmp[0] = '\0';
            success &= Xml.GetChildValue( TAG_FUNCTION_ID_HEX, szTmp, XML_DEFAULT_VALUE_LEN );
            if( success )
            {
                script->functionId = ConvertToInt( szTmp );
                ConsolePrintf( PRIO_LOW, BLUE"Function-ID: '%s=%d'"RESETCOLOR"\n", szTmp, script->functionId );
            }

            szTmp[0] = '\0';
            success &= Xml.GetChildValue( TAG_OP_TYPE_REQUEST_HEX, szTmp,
                XML_DEFAULT_VALUE_LEN );
            if( success )
            {
                script->opTypeRequest = ConvertToInt( szTmp );
                ConsolePrintf( PRIO_LOW, BLUE"OP-Type Request: '%s=%d'"RESETCOLOR"\n", szTmp, script->opTypeRequest );
            }

            szTmp[0] = '\0';
            if( Xml.GetChildValue( TAG_OP_TYPE_RESPONSE_HEX, szTmp,
                XML_DEFAULT_VALUE_LEN ) )
            {
                script->opTypeResponse = ConvertToInt( szTmp );
                ConsolePrintf( PRIO_LOW, BLUE"OP-Type Response: '%s=%d'"RESETCOLOR"\n", szTmp,
                    script->opTypeResponse );
            }
            else
            {
                script->opTypeResponse = 0xFF;
                ConsolePrintf( PRIO_LOW, BLUE"OP-Type Response was not defined"RESETCOLOR"\n" );
            }

            szTmp[0] = '\0';
            //Payload is optional
            if( Xml.GetChildValue( TAG_PAYLOAD_HEX, szTmp, XML_DEFAULT_VALUE_LEN ) )
            {
                ConsolePrintf( PRIO_LOW, BLUE"Payload: '%s'"RESETCOLOR"\n", szTmp );
                StorePayload( szTmp, script );
            }
        }
        else if( SCRIPT_PAUSE == script->scriptType )
        {
            szTmp[0] = '\0';
            success &= Xml.GetChildValue( TAG_PAUSE_MS, szTmp, XML_DEFAULT_VALUE_LEN );
            if( success )
            {
                script->pauseInMillis = strtol( ( char * )szTmp, NULL, 10 );
                ConsolePrintf( PRIO_LOW, BLUE"FBlock-ID: '%s=%d'"RESETCOLOR"\n", szTmp, script->fblockId );
            }
        }
        if( success )
        {
            allChannels.PushBack( script );
        }
        else
        {
            delete script;
        }
        Xml.FindNextNode();
    }
    return success;
}

void CSciptXml::StorePayload( xmlChar *xString, CScript *script )
{
    char *cString = ( char * )xString; //Convertion may be needed..
    script->payloadLength = 0;
    for( uint32_t i = 0; i < strlen( cString ); i++ )
    {
        if( cString[i] >= 65 && cString[i] <= 90 )
        {
            //convert upper case to lower case
            cString[i] += 32;
        }
    }
    char *tkPtr;
    char *token = strtok_r( cString, " ,.-", &tkPtr );
    while( NULL != token )
    {
        if( script->payloadLength >= MAX_PAYLOAD_LENGTH )
        {
            ConsolePrintf( PRIO_ERROR,
                "CSciptXml::ConvertToByteArray, MAX_PAYLOAD_LENGTH is defined too low. Please increase value!" );
            break;
        }
        script->payload[script->payloadLength++] = strtol( token, NULL, 16 );
        token = strtok_r( NULL, " ,.-", &tkPtr );
    }
}