This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtcoap.c
185 lines (139 loc) · 4.08 KB
/
tcoap.c
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
/**
* tcoap.c
*
* Author: Serge Maslyakov, [email protected]
* Copyright 2017 Serge Maslyakov. All rights reserved.
*
*/
#include "tcoap.h"
#include "tcoap_udp.h"
#include "tcoap_tcp.h"
#include "tcoap_utils.h"
static tcoap_error init_coap_driver(tcoap_handle * const handle, const tcoap_request_descriptor * const reqd);
static void deinit_coap_driver(tcoap_handle * handle);
/**
* @brief See description in the header file.
*
*/
void tcoap_debug(tcoap_handle * const handle, const bool enable)
{
if (enable) {
TCOAP_SET_STATUS(handle, TCOAP_DEBUG_ON);
} else {
TCOAP_RESET_STATUS(handle, TCOAP_DEBUG_ON);
}
}
/**
* @brief See description in the header file.
*
*/
tcoap_error tcoap_send_coap_request(tcoap_handle * const handle, const tcoap_request_descriptor * const reqd)
{
tcoap_error err;
if (TCOAP_CHECK_STATUS(handle, TCOAP_SENDING_PACKET)) {
return TCOAP_BUSY_ERROR;
}
TCOAP_SET_STATUS(handle, TCOAP_SENDING_PACKET);
err = init_coap_driver(handle, reqd);
if (err == TCOAP_OK) {
switch (handle->transport) {
case TCOAP_UDP:
err = tcoap_send_coap_request_udp(handle, reqd);
break;
case TCOAP_TCP:
err = tcoap_send_coap_request_tcp(handle, reqd);
break;
case TCOAP_SMS:
default:
/* not supported yet */
err = TCOAP_PARAM_ERROR;
break;
}
}
deinit_coap_driver(handle);
TCOAP_RESET_STATUS(handle, TCOAP_SENDING_PACKET);
tcoap_tx_signal(handle, TCOAP_ROUTINE_PACKET_DID_FINISH);
return err;
}
/**
* @brief See description in the header file.
*
*/
tcoap_error tcoap_rx_byte(tcoap_handle * const handle, const uint8_t byte)
{
if (TCOAP_CHECK_STATUS(handle, TCOAP_WAITING_RESP)) {
if (handle->response.len < TCOAP_MAX_PDU_SIZE) {
handle->response.buf[handle->response.len++] = byte;
tcoap_tx_signal(handle, TCOAP_RESPONSE_BYTE_DID_RECEIVE);
return TCOAP_OK;
}
return TCOAP_RX_BUFF_FULL_ERROR;
}
return TCOAP_WRONG_STATE_ERROR;
}
/**
* @brief See description in the header file.
*
*/
tcoap_error tcoap_rx_packet(tcoap_handle * const handle, const uint8_t * buf, const uint32_t len)
{
if (TCOAP_CHECK_STATUS(handle, TCOAP_WAITING_RESP)) {
mem_copy(handle->response.buf, buf, len < TCOAP_MAX_PDU_SIZE ? len : TCOAP_MAX_PDU_SIZE);
handle->response.len = len;
if (len < TCOAP_MAX_PDU_SIZE) {
tcoap_tx_signal(handle, TCOAP_RESPONSE_DID_RECEIVE);
return TCOAP_OK;
}
return TCOAP_RX_BUFF_FULL_ERROR;
}
return TCOAP_WRONG_STATE_ERROR;
}
/**
* @brief Init CoAP driver
*
* @param handle - coap handle
* @param reqd - descriptor of request
*
* @return status of operation
*/
static tcoap_error init_coap_driver(tcoap_handle * const handle, const tcoap_request_descriptor * const reqd)
{
tcoap_error err;
err = TCOAP_OK;
handle->request.len = 0;
handle->response.len = 0;
if (reqd->code == TCOAP_CODE_EMPTY_MSG && reqd->tkl) {
return TCOAP_PARAM_ERROR;
}
if (handle->request.buf == NULL) {
err = tcoap_alloc_mem_block(&handle->request.buf, TCOAP_MAX_PDU_SIZE);
if (err != TCOAP_OK) {
return err;
}
}
if (reqd->type == TCOAP_MESSAGE_CON || reqd->response_callback != NULL) {
if (handle->response.buf == NULL) {
err = tcoap_alloc_mem_block(&handle->response.buf, TCOAP_MAX_PDU_SIZE);
}
}
return err;
}
/**
* @brief Deinit CoAP driver
*
* @param handle - coap handle
*
*/
static void deinit_coap_driver(tcoap_handle * handle)
{
if (handle->response.buf != NULL) {
tcoap_free_mem_block(handle->response.buf, TCOAP_MAX_PDU_SIZE);
handle->response.buf = NULL;
}
if (handle->request.buf != NULL) {
tcoap_free_mem_block(handle->request.buf, TCOAP_MAX_PDU_SIZE);
handle->request.buf = NULL;
}
handle->request.len = 0;
handle->response.len = 0;
}