summaryrefslogtreecommitdiffstats
path: root/Src/Network/IndustrialStack.h
blob: 8f80d26a15360d5532901a39172d0a5d418362b7 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * 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.
 *
 */

/*----------------------------------------------------------*/
/*! \file
 *  \brief  This file contains the CIndustrialStack class (common part).
 */
/*----------------------------------------------------------*/

#ifndef INDUSTRIAL_STACK_H
#define INDUSTRIAL_STACK_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "SafeVector.h"
#include "IndustrialStack_Types.h"
#include "IndustrialStack_MNS.h"
#include "Console.h"

class CIndustrialStack : IISNetServiceWrapperCB
{
private:
    CISNetServiceWrapper *msnw;
    CSafeVector<CISDeviceQueue *> deviceQueues;
    CSafeVector<CSInternalEvent *> intEventListeners;
    CISDeviceQueue *GetQueue(uint16_t deviceType);
    void HandleStateChange(ISReturn_t state, IISElement * element);
public:
    CIndustrialStack(uint8_t deviceApi, int controlRxHandle, int controlTxHandle);
    virtual ~CIndustrialStack();
    void ServiceStack(uint32_t time);
    void EnqueueElement(uint16_t nodeAddress, IISElement *element);
    bool ElementsPending();
    bool ElementsPending(uint16_t ingoreNodeAddress);
    void ClearAllElements();
    bool SendMostMessage(CISMostMsg *message);
    void Unsynchronize();
    void AddInternalEventListener(CSInternalEvent* callback);
    
    /* Callback from IISNetServiceWrapperCB interface */
    virtual void OnReceivedMostMessage(CISMostMsg *rcvMessage);
    virtual void OnSyncStateChanged(bool isSynced);
    virtual void OnControlReadEnd();
};

class CISWaitElement : public IISElement
{
protected:
    uint32_t startTime;
    uint32_t timeout;
public:
    CISWaitElement() : IISElement(), startTime(0), timeout(2000) 
    { 
        ElementName = "Wait Element";
    }
    
    void SetTimeout(uint32_t timeInMillis)
    {
        timeout = timeInMillis;
    }
    
    virtual ISReturn_t Service(CIndustrialStack *iStack, uint32_t time)
    {
        assert(NULL != iStack);
        if (0 == startTime)
            startTime = time;
        else if ( ( time - startTime >= timeout ) )
            return ISReturn_Success;
        return ISReturn_NoChange;
    }
    
    virtual ISReturn_t OnMostMessage(CIndustrialStack *iStack, CISMostMsg *rcvMessage)
    {
        assert(NULL != iStack);
        return ISReturn_NoChange;
    }
};

class CISWaitForPendingElements : public CISWaitElement
{
private:
    uint16_t ignoreNode;
public:
    CISWaitForPendingElements(uint16_t ingoreNodeAddress) 
        : CISWaitElement(), ignoreNode(ingoreNodeAddress)
    {
    }
        
    virtual ISReturn_t Service(CIndustrialStack *iStack, uint32_t time)
    {
        assert(NULL != iStack);
        if (!iStack->ElementsPending(ignoreNode))
            return ISReturn_Success;
        return (CISWaitElement::Service(iStack, time) == ISReturn_NoChange)
            ? ISReturn_NoChange : ISReturn_Timeout;
    }
};

class CISSendMostMsgElement : public CISWaitElement
{
private:
    uint8_t retryCount;
public:
    CISMostMsg Request;
    CISMostMsg Response;
    bool WaitForResponse;
    CISOpType_t WaitForResponseOpType;
    
    CISSendMostMsgElement() : CISWaitElement(), retryCount(4),
        WaitForResponse(false), 
        WaitForResponseOpType(CISOpType_INVALID)
    {
        timeout = 500;
    }
    
    virtual ISReturn_t Service(CIndustrialStack *iStack, uint32_t time)
    {
        assert(NULL != iStack);
        if (0 == startTime)
        {
            CISWaitElement::Service(iStack, time);
            if (Request.IsValid == false || Request.FBlock == 0xFFFFFFFF 
                || Request.Func == 0xFFFFFFFF || Request.Inst == 0xFFFFFFFF 
                || Request.OpType == CISOpType_INVALID)
            {
                ConsolePrintf(PRIO_ERROR, 
                    RED"CISSendMostMsgElement %s invalid MOST message to be"\
                    " sent"RESETCOLOR"\n", ElementName);
                return ISReturn_Failure;
            }
            bool success = iStack->SendMostMessage(&Request);
            if (success && !WaitForResponse)
                return ISReturn_Success;
            return success ? ISReturn_NoChange : ISReturn_Failure;
        }
        ISReturn_t waitState = CISWaitElement::Service(iStack, time);
        if (ISReturn_Success == waitState)
        {
            if (0 == --retryCount)
            {
                ConsolePrintf(PRIO_ERROR, RED"All High level retries failed for Request '%s',"\
                    " node 0x%X!"RESETCOLOR"\n",
                    ElementName, Request.TargetAddress);
                return ISReturn_Timeout;
            }
            //Retry sending
            ConsolePrintf(PRIO_HIGH, YELLOW"High level retry after timeout for Request '%s',"\
                    " node 0x%X"RESETCOLOR"\n",
                    ElementName, Request.TargetAddress);
            startTime = time;
            return (iStack->SendMostMessage(&Request)) ? ISReturn_NoChange : ISReturn_Failure;
        }
        return ISReturn_NoChange;
    }
    
    virtual ISReturn_t OnMostMessage(CIndustrialStack *iStack, CISMostMsg *rcvMessage)
    {
        if (!WaitForResponse)
            return ISReturn_NoChange;
        assert(NULL != iStack);
        assert(NULL != rcvMessage);
        assert(Request.IsValid);
        assert(rcvMessage->IsValid);
        
        uint32_t requestAddr = Request.TargetAddress;
        uint32_t responseAddr = rcvMessage->SourceAddress;
        if (requestAddr >= 0x400 && requestAddr <= 0x4FF )
            requestAddr -= 0x300;
        if (responseAddr >= 0x400 && responseAddr <= 0x4FF )
            responseAddr -= 0x300;
        
        if ( ( requestAddr < 0x300 || requestAddr > 0x3FF )
            && ( responseAddr != requestAddr))
            return ISReturn_NoChange;
            
        if ( rcvMessage->FBlock == Request.FBlock
            && rcvMessage->Func == Request.Func)
        {
            if (rcvMessage->OpType == WaitForResponseOpType)
            {
                Response.DeepCopy(rcvMessage);
                return ISReturn_Success;
            }
            else if(0 == --retryCount)
            {
                Response.DeepCopy(rcvMessage);
                return ISReturn_Failure;
            }
            //Retry sending
            ConsolePrintf(PRIO_HIGH, YELLOW"High level retry after wrong result for Request '%s',"\
                    " node 0x%X"RESETCOLOR"\n",
                    ElementName, Request.TargetAddress);
            return (iStack->SendMostMessage(&Request)) ? ISReturn_NoChange : ISReturn_Failure;
        }
        return ISReturn_NoChange;
    }
};

class CSInternalEvent : public IISElement
{
public:
    virtual ISReturn_t Service(CIndustrialStack *iStack, uint32_t time) 
    {
        //Must not be called
        return ISReturn_Failure;
    }
    virtual ISReturn_t OnMostMessage(CIndustrialStack *iStack, CISMostMsg *r) 
    {
        //Must not be called
        return ISReturn_Failure;
    }
    virtual void OnSyncStateChanged(CIndustrialStack *iStack, bool isSynced) {}
    virtual void OnControlReadEnd(CIndustrialStack *iStack) {}
};

#endif //INDUSTRIAL_STACK_H