dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_fileout.c
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  *
30  * Additional note on redistribution: The copyright and license notices above
31  * must be maintained in each individual source file that is a derivative work
32  * of this source file; otherwise redistribution is prohibited.
33  */
34 
35 
36 /* Project Includes */
37 #include "pios.h"
38 
39 #include <pios_fileout_priv.h>
40 #include "pios_thread.h"
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 
46 /* Provide a COM driver */
47 static void PIOS_FILEOUT_RegisterTxCallback(uintptr_t udp_id, pios_com_callback tx_out_cb, uintptr_t context);
48 static void PIOS_FILEOUT_TxStart(uintptr_t udp_id, uint16_t tx_bytes_avail);
49 
52  .bind_tx_cb = PIOS_FILEOUT_RegisterTxCallback,
53 };
54 
55 
56 static pios_fileout_dev * find_file_dev_by_id(uintptr_t tcp)
57 {
58  return (pios_fileout_dev *) tcp;
59 }
60 
64 int32_t PIOS_FILEOUT_Init(uintptr_t *file_id, const char *filename,
65  const char *mode)
66 {
67  pios_fileout_dev *file_dev = PIOS_malloc(sizeof(pios_fileout_dev));
68 
69  memset(file_dev, 0, sizeof(*file_dev));
70 
71  file_dev->fh = fopen(filename, mode);
72 
73  if (!file_dev->fh) {
74  perror("fopen");
75  return -1;
76  }
77 
78  setbuf(file_dev->fh, NULL);
79 
80  *file_id = (uintptr_t) file_dev;
81 
82  return 0;
83 }
84 
85 static void PIOS_FILEOUT_TxStart(uintptr_t file_id, uint16_t tx_bytes_avail)
86 {
87  pios_fileout_dev *file_dev = find_file_dev_by_id(file_id);
88 
89  PIOS_Assert(file_dev);
90 
91  int32_t length,rem;
92 
96  if (file_dev->tx_out_cb) {
97  while (tx_bytes_avail > 0) {
98  bool tx_need_yield = false;
99  length = (file_dev->tx_out_cb)(file_dev->tx_out_context, file_dev->tx_buffer, PIOS_FILEOUT_TX_BUFFER_SIZE, NULL, &tx_need_yield);
100  rem = length;
101  while (rem > 0) {
102  ssize_t len = 0;
103  len = fwrite(file_dev->tx_buffer, 1, length, file_dev->fh);
104 
105  if (len <= 0) {
106  rem = 0;
107  } else {
108  rem -= len;
109  }
110  }
111  tx_bytes_avail -= length;
112  }
113  }
114 }
115 
116 static void PIOS_FILEOUT_RegisterTxCallback(uintptr_t file_id, pios_com_callback tx_out_cb, uintptr_t context)
117 {
118  pios_fileout_dev *file_dev = find_file_dev_by_id(file_id);
119 
120  PIOS_Assert(file_dev);
121 
122  /*
123  * Order is important in these assignments since ISR uses _cb
124  * field to determine if it's ok to dereference _cb and _context
125  */
126  file_dev->tx_out_context = context;
127  file_dev->tx_out_cb = tx_out_cb;
128 }
129 
uintptr_t tx_out_context
static void PIOS_FILEOUT_TxStart(uintptr_t udp_id, uint16_t tx_bytes_avail)
Definition: pios_fileout.c:85
Main PiOS header to include all the compiled in PiOS options.
static pios_fileout_dev * find_file_dev_by_id(uintptr_t tcp)
Definition: pios_fileout.c:56
#define PIOS_FILEOUT_TX_BUFFER_SIZE
void * PIOS_malloc(size_t size)
Definition: pios_heap.c:125
uint8_t tx_buffer[PIOS_FILEOUT_TX_BUFFER_SIZE]
uint8_t length
static void PIOS_FILEOUT_RegisterTxCallback(uintptr_t udp_id, pios_com_callback tx_out_cb, uintptr_t context)
Definition: pios_fileout.c:116
enum channel_mode mode
Definition: pios_servo.c:58
pios_com_callback tx_out_cb
int32_t PIOS_FILEOUT_Init(uintptr_t *file_id, const char *filename, const char *mode)
Definition: pios_fileout.c:64
const struct pios_com_driver pios_fileout_com_driver
Definition: pios_fileout.c:50
uint32_t file_id
FILEOUT private definitions.
#define PIOS_Assert(test)
Definition: pios_debug.h:52
void(* tx_start)(uintptr_t id, uint16_t tx_bytes_avail)
Definition: pios_com.h:45
uint16_t(* pios_com_callback)(uintptr_t context, uint8_t *buf, uint16_t buf_len, uint16_t *headroom, bool *task_woken)
Definition: pios_com.h:41