-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathu_port_posix.c
289 lines (248 loc) · 7.76 KB
/
u_port_posix.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright 2024 u-blox
*
* 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.
*/
/** @file
* @brief Posix port example
*
* Can be used for running on Linux.
* The current implementation only support one instance of the AT client.
*/
#include <time.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <errno.h>
#include <string.h>
#include "u_cx_log.h"
#include "u_port.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
typedef struct {
int uartFd;
uCxAtClient_t *pClient;
pthread_t rxThread;
volatile bool terminateRxTask;
} uPortContext_t;
/* ----------------------------------------------------------------
* STATIC PROTOTYPES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* STATIC VARIABLES
* -------------------------------------------------------------- */
static uint64_t gBootTime = 0;
static int gUartFd;
static uCxAtClientConfig_t *gPConfig = NULL;
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
static int32_t getTickTimeMs(void)
{
struct timespec time;
clock_gettime(CLOCK_MONOTONIC_RAW, &time);
uint64_t timeMs = (time.tv_sec * 1000) + (time.tv_nsec / (1000 * 1000));
return (int32_t)(timeMs % (1000 * 60 * 60 * 24));
}
static int openUart(const char *pDevName, int baudRate, bool useFlowControl)
{
int uartFd;
speed_t speed;
if (baudRate == 9600) {
speed = B9600;
} else if (baudRate == 19200) {
speed = B19200;
} else if (baudRate == 38400) {
speed = B38400;
} else if (baudRate == 57600) {
speed = B57600;
} else if (baudRate == 115200) {
speed = B115200;
} else if (baudRate == 230400) {
speed = B230400;
} else if (baudRate == 460800) {
speed = B460800;
} else if (baudRate == 921600) {
speed = B921600;
} else {
return -1;
}
uartFd = open(pDevName, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (uartFd < 0) {
return uartFd;
}
struct termios options;
tcgetattr(uartFd, &options);
cfmakeraw(&options);
cfsetispeed(&options, speed);
cfsetospeed(&options, speed);
if (useFlowControl) {
options.c_cflag |= CRTSCTS;
} else {
options.c_cflag &= ~CRTSCTS;
}
// Set timed read with 100 ms timeout
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 1;
if (tcsetattr(uartFd, TCSANOW, &options) != 0) {
return -1;
}
tcflush(uartFd, TCIOFLUSH);
return uartFd;
}
static void *rxTask(void *pArg)
{
uPortContext_t *pCtx = (uPortContext_t *)pArg;
struct pollfd fds[1];
fds[0].fd = pCtx->uartFd;
fds[0].events = POLLIN;
while (!pCtx->terminateRxTask) {
int pollrc = poll(fds, 1, 100);
if (pollrc < 0) {
perror("poll");
break;
}
if (fds[0].revents & POLLIN) {
uCxAtClientHandleRx(pCtx->pClient);
}
}
U_CX_LOG_LINE_I(U_CX_LOG_CH_DBG, pCtx->pClient->instance, "RX task terminated");
return NULL;
}
static int32_t uartWrite(uCxAtClient_t *pClient, void *pStreamHandle, const void *pData, size_t length)
{
(void)pClient;
int uartFd = *((int *)pStreamHandle);
return (int32_t)write(uartFd, pData, length);
}
static int32_t uartRead(uCxAtClient_t *pClient, void *pStreamHandle, void *pData, size_t length, int32_t timeoutMs)
{
(void)pClient;
int uartFd = *((int *)pStreamHandle);
if (timeoutMs == 0) {
int available = 0;
ioctl(uartFd, FIONREAD, &available);
if (available == 0) {
return 0;
}
}
return (int32_t)read(uartFd, pData, length);
}
// Create a time structure by adding the specified number of
// milliseconds to the current clock time.
static void msToTimeSpec(int32_t ms, struct timespec *pTime, bool fromNow)
{
struct timespec now = {0};
if (fromNow) {
timespec_get(&now, TIME_UTC);
}
pTime->tv_sec = now.tv_sec + ms / 1000;
pTime->tv_nsec = now.tv_nsec + (ms % 1000) * 1000000;
if (pTime->tv_nsec >= 1000000000) {
pTime->tv_nsec -= 1000000000;
pTime->tv_sec++;
}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
int32_t uPortGetTickTimeMs(void)
{
return getTickTimeMs() - gBootTime;
}
void uPortAtInit(uCxAtClient_t *pClient)
{
static uPortContext_t context;
static char rxBuf[1024];
#if U_CX_USE_URC_QUEUE == 1
static char urcBuf[1024];
#endif
static uCxAtClientConfig_t config = {
.pRxBuffer = &rxBuf[0],
.rxBufferLen = sizeof(rxBuf),
#if U_CX_USE_URC_QUEUE == 1
.pUrcBuffer = &urcBuf[0],
.urcBufferLen = sizeof(urcBuf),
#endif
.pStreamHandle = &context,
.write = uartWrite,
.read = uartRead
};
memset(&context, 0, sizeof(context));
context.pClient = pClient;
context.uartFd = -1;
// Current implementation of this port only support one instance
assert(gPConfig == NULL);
gPConfig = &config;
if (gBootTime == 0) {
gBootTime = getTickTimeMs();
}
uCxAtClientInit(&config, pClient);
}
bool uPortAtOpen(uCxAtClient_t *pClient, const char *pDevName, int baudRate, bool useFlowControl)
{
uPortContext_t *pCtx = pClient->pConfig->pStreamHandle;
assert(pClient->pConfig != NULL);
assert(pCtx != NULL);
assert(pCtx->uartFd == -1);
U_CX_LOG_LINE_I(U_CX_LOG_CH_DBG, pClient->instance, "Opening %s at %d with %s flow control",
pDevName, baudRate, useFlowControl ? "CTS/RTS" : "no");
gUartFd = openUart(pDevName, baudRate, useFlowControl);
if (gUartFd < 0) {
U_CX_LOG_LINE_I(U_CX_LOG_CH_ERROR, pClient->instance, "Failed to open UART");
return false;
}
pCtx->uartFd = gUartFd;
pCtx->terminateRxTask = false;
pthread_attr_t attr;
struct sched_param param;
pthread_attr_init(&attr);
pthread_attr_getschedparam(&attr, ¶m);
param.sched_priority = 9;
pthread_attr_setschedparam(&attr, ¶m);
pthread_create(&pCtx->rxThread, &attr, rxTask, pCtx);
return true;
}
void uPortAtClose(uCxAtClient_t *pClient)
{
uPortContext_t *pCtx = pClient->pConfig->pStreamHandle;
assert(pCtx->uartFd != -1);
assert(!pCtx->terminateRxTask);
U_CX_LOG_LINE_I(U_CX_LOG_CH_DBG, pClient->instance, "Closing UART");
// Terminate the RX task
pCtx->terminateRxTask = true;
pthread_join(pCtx->rxThread, NULL);
close(pCtx->uartFd);
pCtx->uartFd = -1;
}
int32_t uPortMutexTryLock(pthread_mutex_t *pMutex, uint32_t timeoutMs)
{
int32_t ret;
if (timeoutMs == 0) {
ret = pthread_mutex_trylock(pMutex);
} else {
struct timespec time;
msToTimeSpec(timeoutMs, &time, true);
ret = pthread_mutex_timedlock(pMutex, &time);
}
return ret;
}