-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathMQTTNetworkUtil.h
85 lines (79 loc) · 2.8 KB
/
MQTTNetworkUtil.h
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
/*
* Copyright (c) 2020, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#ifndef _MQTTNETWORK_UTIL_H_
#define _MQTTNETWORK_UTIL_H_
/* MQTT doesn't expect nsapi error values so we translate them */
static int convert_nsapi_error_to_mqtt_error(int nsapi_error)
{
if (nsapi_error == NSAPI_ERROR_WOULD_BLOCK) {
/* MQTT expects 0 on timeout */
return 0;
} else if (nsapi_error == 0) {
/* MQTT expect -1 on closed sockets */
return -1;
}
return nsapi_error;
}
/** Reads data and returns number of bytes read or a translated error that MQTT expects. This will call
* read on the socket multiple times until all data is retrieved.
*
* @tparam SocketType Socket type like TCPSocket.
* @param socket Socket to read data from.
* @param buffer Buffer to store data.
* @param len Length of expected data.
* @param timeout Timeout for the operation.
* @return Always returns the full length if successful or an error.
*/
template<typename SocketType>
static int accumulate_mqtt_read(SocketType socket, unsigned char *buffer, int len, int timeout)
{
/* TODO: Timout should be applied to whole operation not partial recv */
socket->set_timeout(timeout);
/* MQTT Client expects the full packet so we accumulate until we get all bytes */
int remaining = len;
while (remaining) {
int ret = socket->recv(buffer, remaining);
if (ret > 0) {
remaining -= ret;
buffer += ret;
} else {
return convert_nsapi_error_to_mqtt_error(ret);
}
}
return len;
}
/** Sends data and returns number of bytes sent or a translated error that MQTT expects.
*
* @tparam SocketType Socket type like TCPSocket.
* @param socket Socket to send data on.
* @param buffer Data to send.
* @param len Length of data.
* @param timeout Timeout for the operation.
* @return Always returns the full length if successful or an error.
*/
template<typename SocketType>
static int mqtt_write(SocketType socket, unsigned char *buffer, int len, int timeout)
{
socket->set_timeout(timeout);
int ret = socket->send(buffer, len);
if (ret > 0) {
return ret;
} else {
return convert_nsapi_error_to_mqtt_error(ret);
}
}
#endif // _MQTTNETWORK_UTIL_H_