File size: 6,431 Bytes
69c3cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 ###############################################################################
 #
 # Temboo CoAP Edge Device library
 #
 # Copyright (C) 2017, Temboo Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing,
 # software distributed under the License is distributed on an
 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 # either express or implied. See the License for the specific
 # language governing permissions and limitations under the License.
 #
 ###############################################################################
 */

#include "CoapRRLayer.h"

CoapRRLayer::CoapRRLayer(CoapMessageLayer& messageLayer, uint8_t* rxBuffer, uint16_t rxBufferLen) :
    m_messageLayer(messageLayer),
    m_state(STATE_IDLE),
    m_lastResult(NO_ERROR),
    m_token(NULL),
    m_rxBuffer(rxBuffer),
    m_rxBufferLen(rxBufferLen) {
}



CoapRRLayer::Result CoapRRLayer::reliableSend(CoapMsg& msg, char* token, IPAddress addr, uint16_t port) {
    if (m_state != STATE_IDLE) {
        return ERROR_IMPROPER_STATE;
    }
    
    if (CoapMessageLayer::NO_ERROR != m_messageLayer.reliableSend(msg, addr, port)) {
        return ERROR_SENDING_MSG;
    }
    
    m_token = token;
    m_state = STATE_WAITING;
    return NO_ERROR;
}



bool CoapRRLayer::rxTokenMatches(CoapMsg& msg) {
    if (msg.getTokenLen() != strlen(m_token)) {
        return false;
    }
    
    if (0 != memcmp(msg.getToken(), m_token, strlen(m_token))) {
        return false;
    }
    return true;
}



CoapRRLayer::Result CoapRRLayer::loop() {
    
    m_lastResult = NO_ERROR;
    
    switch (m_state) {
            
        case STATE_IDLE:
            
            // Pump the receiver.
            // We're not serving anything, so unless there's an outstanding
            // request (which would mean we would be in STATE_WAITING, not STATE_IDLE),
            // we're going to reject or ignore any incoming traffic.
            switch(m_messageLayer.loop()) {
                case CON_RECEIVED:
                {
                    // Explicitly reject any CON messages so the sender will
                    // quit bugging us with retransmissions.
                    CoapMsg msg(m_rxBuffer, m_rxBufferLen, m_messageLayer.getRXByteCount());
                    m_messageLayer.rejectMsg(msg);
                    break;
                }
                default:
                    // Just ignore anything else.
                    break;
            }
            
            break;
            
        case STATE_WAITING:
            // We're waiting for a response to an earlier request.
            switch(m_messageLayer.loop()) {
                    
                case CoapMessageLayer::NO_ERROR:
                    // Nothing happened. Nothing to do.
                    break;
                    
                case CoapMessageLayer::ACK_RECEIVED:
                {
                    CoapMsg msg(m_rxBuffer, m_rxBufferLen, m_messageLayer.getRXByteCount());
                    
                    // If it wasn't an empty ack, it's a response.
                    // And if the token matches, then it's the response we're waiting for.
                    if (rxTokenMatches(msg)) {
                        m_lastResult = RESPONSE_RECEIVED;
                        m_state = STATE_IDLE;
                    } else {
                        // if ACK is not empty and tokens don't match, an error has occurred
                        if (msg.getTokenLen() != 0) {
                            m_lastResult = ERROR_RECEIVING_RESPONSE;
                            m_state = STATE_IDLE;
                            TEMBOO_TRACE("Error: ");
                            TEMBOO_TRACELN("Msg token did not match");
                        }
                    }
                    break;
                }
                    
                case CoapMessageLayer::RESET_RECEIVED:
                {
                    // If it was a reset, the message should be empty
                    // and there will be no token
                    m_lastResult = RST_RECEIVED;
                    m_state = STATE_IDLE;
                    break;
                }
                    
                case CoapMessageLayer::CON_RECEIVED:
                {
                    // See if this is our response or just some random message.
                    // The message layer has already confirmed it's from the right host.
                    CoapMsg msg(m_rxBuffer, m_rxBufferLen, m_messageLayer.getRXByteCount());
                    
                    // We only accept responses for the current request (i.e. the tokens must match)
                    if (rxTokenMatches(msg)) {
                        m_lastResult = RESPONSE_RECEIVED;
                        m_state = STATE_IDLE;
                    } else {
                        m_messageLayer.rejectMsg(msg);
                        TEMBOO_TRACE("Error: ");
                        TEMBOO_TRACELN("Msg token did not match");
                    }
                    break;
                }
                    
                case CoapMessageLayer::NON_RECEIVED:
                {
                    CoapMsg msg(m_rxBuffer, m_rxBufferLen, m_messageLayer.getRXByteCount());
                    if (rxTokenMatches(msg)) {
                        m_lastResult = RESPONSE_RECEIVED;
                        m_state = STATE_IDLE;
                    } else {
                        // if the token does not match, then an error occurred
                        m_lastResult = ERROR_RECEIVING_RESPONSE;
                        m_state = STATE_IDLE;
                        TEMBOO_TRACE("Error: ");
                        TEMBOO_TRACELN("Msg token did not match");
                    }
                    break;
                }
                    
                default:
                    // Anything else indicates a failure of some sort.  Check
                    // the messageLayer lastResult for specifics.
                    m_lastResult = ERROR_RECEIVING_RESPONSE;
                    m_state = STATE_IDLE;
                    
            }
            break;
    }
    
    return m_lastResult;
}