-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel_exchange.c
429 lines (349 loc) · 11.3 KB
/
kernel_exchange.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (c) 2016, Cascoda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include "ca821x_api.h"
#include "kernel_exchange.h"
/******************************************************************************/
#define DebugFSMount "/sys/kernel/debug"
#define DriverNode "/ca8210"
#define DriverFilePath (DebugFSMount DriverNode)
#define USE_LOGFILE 1
#define CA8210_IOCTL_HARD_RESET (0)
/******************************************************************************/
static int ca8210_test_int_exchange(
const uint8_t *buf,
size_t len,
uint8_t *response,
void *pDeviceRef
);
/******************************************************************************/
static int DriverFileDescriptor;
static pthread_t rx_thread;
static pthread_mutex_t flag_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t rx_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t tx_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t buf_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t unhandled_sync_cond = PTHREAD_COND_INITIALIZER;
static int unhandled_sync_count = 0;
static uint8_t worker_flag = 0;
static uint8_t initialised = 0;
static fd_set rx_block_fd_set;
#ifdef USE_LOGFILE
static FILE * LogFileDescriptor;
static pthread_mutex_t file_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static kernel_exchange_errorhandler errorcallback;
/******************************************************************************/
struct buffer_queue{
size_t len;
uint8_t * buf;
struct buffer_queue * next;
};
static struct buffer_queue * head_buffer_queue = NULL;
//Add a buffer to the FIFO queue - this is a blocking function
static void add_to_queue(const uint8_t *buf, size_t len);
//Retrieve a buffer into destBuf - this is a nonblocking function and will return 0 if nothing is retrieved from the queue
static size_t pop_from_queue(uint8_t * destBuf, size_t maxlen);
/******************************************************************************/
static void writeTime(FILE * stream){
struct timeval tv;
char timeString[40];
gettimeofday(&tv, NULL);
strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec));
fprintf(stream, "\r\nat: %s.%06d ", timeString, (uint32_t)tv.tv_usec);
}
static void *ca8210_test_int_read_worker(void *arg)
{
uint8_t rx_buf[512];
size_t rx_len;
int i;
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
pthread_mutex_lock(&flag_mutex);
while (worker_flag) {
pthread_mutex_unlock(&flag_mutex);
rx_len = 0;
FD_ZERO(&rx_block_fd_set);
FD_SET(DriverFileDescriptor, &rx_block_fd_set);
//Wait until there is data available to read (or time out after 5 seconds)
select(DriverFileDescriptor + 1, &rx_block_fd_set, NULL, NULL, &timeout);
//try to get fresh data
if(pthread_mutex_trylock(&rx_mutex) == 0){
rx_len = read(DriverFileDescriptor, rx_buf, 0);
#ifdef USE_LOGFILE
if (rx_len > 0) {
pthread_mutex_lock(&file_mutex);
writeTime(LogFileDescriptor);
fputs("\r\nReceived Async:",LogFileDescriptor);
for(i = 0; i < rx_len; i++){
fprintf(LogFileDescriptor, " %02x", rx_buf[i]);
}
fputs("\r\n",LogFileDescriptor);
pthread_mutex_unlock(&file_mutex);
}
#endif
if(rx_len > 0 && (rx_buf[0] & SPI_SYN)){ //Catch unhandled synchronous commands so synchronicity for future commands is not lost
unhandled_sync_count--;
assert(unhandled_sync_count >= 0);
pthread_cond_signal(&unhandled_sync_cond);
}
pthread_mutex_unlock(&rx_mutex);
}
//If nothing was received this cycle, get something from the queue
if(rx_len == 0){
rx_len = pop_from_queue(rx_buf, 512);
}
if (rx_len > 0) {
ca821x_downstream_dispatch(rx_buf, rx_len, &DriverFileDescriptor);
}
pthread_mutex_lock(&flag_mutex);
}
return NULL;
}
int kernel_exchange_init(void){
return kernel_exchange_init_withhandler(NULL);
}
int kernel_exchange_init_withhandler(kernel_exchange_errorhandler callback)
{
int ret;
if(initialised) return 1;
errorcallback = callback;
#ifdef USE_LOGFILE
LogFileDescriptor = fopen("exchange.log", "a");
fputs("\r\n-------------------NEW SESSION-------------------------\r\n",LogFileDescriptor);
#endif
DriverFileDescriptor = open(DriverFilePath, O_RDWR | O_NONBLOCK);
if (DriverFileDescriptor == -1) {
fprintf(stderr, "Couldn't open driver node, errno = %d\n",
errno);
return -1;
}
ca821x_api_downstream = ca8210_test_int_exchange;
//Empty the receive buffer for clean start
pthread_mutex_lock(&rx_mutex);
size_t rx_len;
do{
uint8_t scrap[512];
rx_len = read(DriverFileDescriptor, scrap, 0);
} while (rx_len != 0);
pthread_mutex_unlock(&rx_mutex);
unhandled_sync_count = 0;
pthread_mutex_lock(&flag_mutex);
worker_flag = 1;
pthread_mutex_unlock(&flag_mutex);
ret = pthread_create(&rx_thread, NULL, ca8210_test_int_read_worker, NULL);
if(ret == 0) initialised = 1;
return ret;
}
void kernel_exchange_deinit(void){
int ret;
//Cause the worker thread to terminate
pthread_mutex_lock(&flag_mutex);
worker_flag = 0;
pthread_mutex_unlock(&flag_mutex);
//Lock all mutexes
pthread_mutex_lock(&tx_mutex);
pthread_mutex_lock(&rx_mutex);
pthread_mutex_lock(&buf_queue_mutex);
#ifdef USE_LOGFILE
pthread_mutex_lock(&file_mutex);
do{
ret = fclose(LogFileDescriptor);
} while(ret < 0 && errno == EINTR);
#endif
//close the driver file
do{
ret = close(DriverFileDescriptor);
} while(ret < 0 && errno == EINTR);
initialised = 0;
//unlock all mutexes
#ifdef USE_LOGFILE
pthread_mutex_unlock(&file_mutex);
#endif
pthread_mutex_unlock(&buf_queue_mutex);
pthread_mutex_unlock(&rx_mutex);
pthread_mutex_unlock(&tx_mutex);
}
int ca8210_test_int_reset(unsigned long resettime)
{
return ioctl(DriverFileDescriptor, CA8210_IOCTL_HARD_RESET, resettime);
}
static int ca8210_test_int_write(const uint8_t *buf, size_t len)
{
int returnvalue, remaining = len;
int i, attempts = 0;
pthread_mutex_lock(&tx_mutex);
do {
returnvalue = write(DriverFileDescriptor, buf+len-remaining, remaining);
if (returnvalue > 0)
remaining -= returnvalue;
if(returnvalue == -1){
int error = errno;
if(errno == EAGAIN){ //If the error is that the device is busy, try again after a short wait
if(attempts++ < 5){
struct timespec toSleep;
toSleep.tv_sec = 0;
toSleep.tv_nsec = 50*1000000;
nanosleep(&toSleep, NULL); //Sleep for ~50ms
continue;
}
}
pthread_mutex_unlock(&tx_mutex);
return error;
}
} while (remaining > 0);
#ifdef USE_LOGFILE
pthread_mutex_lock(&file_mutex);
writeTime(LogFileDescriptor);
fputs("\r\nWriting data: ",LogFileDescriptor);
for(i = 0; i < len; i++){
fprintf(LogFileDescriptor, " %02x", buf[i]);
}
fputs("\r\n",LogFileDescriptor);
pthread_mutex_unlock(&file_mutex);
#endif
pthread_mutex_unlock(&tx_mutex);
return 0;
}
static int ca8210_test_int_exchange(
const uint8_t *buf,
size_t len,
uint8_t *response,
void *pDeviceRef
)
{
int Rx_Length, error, i;
const uint8_t isSynchronous = ((buf[0] & SPI_SYN) && response);
if(isSynchronous){
pthread_mutex_lock(&rx_mutex); //Enforce synchronous write then read
while(unhandled_sync_count != 0) {pthread_cond_wait(&unhandled_sync_cond, &rx_mutex);}
}
else if(buf[0] & SPI_SYN){
pthread_mutex_lock(&rx_mutex);
unhandled_sync_count++;
pthread_mutex_unlock(&rx_mutex);
}
error = ca8210_test_int_write(buf, len);
if(error){
//Revert all state changes and release mutexes
if(isSynchronous)pthread_mutex_unlock(&rx_mutex);
else if(buf[0] & SPI_SYN){
pthread_mutex_lock(&rx_mutex);
unhandled_sync_count--;
pthread_mutex_unlock(&rx_mutex);
}
//Call the errorcallback or crash the program
if(errorcallback) errorcallback(error);
else abort();
//Return a failure to the next highest layer
return -1;
}
if (isSynchronous) {
do {
Rx_Length = read(DriverFileDescriptor, response, (size_t) 0);
#ifdef USE_LOGFILE
if (Rx_Length > 0) {
pthread_mutex_lock(&file_mutex);
writeTime(LogFileDescriptor);
fputs("\r\nReceived Sync:",LogFileDescriptor);
for(i = 0; i < Rx_Length; i++){
fprintf(LogFileDescriptor, " %02x", response[i]);
}
fputs("\r\n",LogFileDescriptor);
pthread_mutex_unlock(&file_mutex);
}
#endif
if(Rx_Length > 0 && !(response[0] & SPI_SYN)){
//Unexpected asynchronous response
add_to_queue(response, Rx_Length);
Rx_Length = 0;
}
} while (Rx_Length == 0);
pthread_mutex_unlock(&rx_mutex);
}
return 0;
}
static void add_to_queue(const uint8_t *buf, size_t len){
pthread_mutex_lock(&buf_queue_mutex);
{
struct buffer_queue * nextbuf = head_buffer_queue;
if(nextbuf == NULL){
//queue empty -> start new queue
head_buffer_queue = malloc(sizeof(struct buffer_queue));
memset(head_buffer_queue, 0, sizeof(struct buffer_queue));
nextbuf = head_buffer_queue;
}
else{
while(nextbuf->next != NULL){
nextbuf = nextbuf->next;
}
//allocate new buffer cell
nextbuf->next = malloc(sizeof(struct buffer_queue));
memset(nextbuf->next, 0, sizeof(struct buffer_queue));
nextbuf = nextbuf->next;
}
nextbuf->len = len;
nextbuf->buf = malloc(len);
memcpy(nextbuf->buf, buf, len);
}
pthread_mutex_unlock(&buf_queue_mutex);
}
static size_t pop_from_queue(uint8_t * destBuf, size_t maxlen){
if(pthread_mutex_trylock(&buf_queue_mutex) == 0){
struct buffer_queue * current = head_buffer_queue;
size_t len = 0;
if(head_buffer_queue != NULL){
head_buffer_queue = current->next;
len = current->len;
if(len > maxlen) len = 0;
memcpy(destBuf, current->buf, len);
free(current->buf);
free(current);
}
pthread_mutex_unlock(&buf_queue_mutex);
return len;
}
return 0;
}