EVE 1.0
datapump.h
Go to the documentation of this file.
1 #ifndef EVE_DATAPUMP_H_INCLUDED
2 #define EVE_DATAPUMP_H_INCLUDED
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2016, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * \brief Header file for datapump interface.
36  *
37  * \author SE, Jetro AS
38  */ /******************************************************************/
39 
40 #include <lib/rbuf.h>
41 #include <core/rtos.h>
42 
43 /**
44  * \defgroup datapump Data pump
45  * \ingroup net
46  * \{
47  *
48  */
49 
50 /* Internal datapump flags for task synchronization */
51 #define DATAPUMP_FLAGS_CAN_READ (1 << 0) /*!< Datapump slave pump task can read data */
52 #define DATAPUMP_FLAGS_CAN_WRITE (1 << 1) /*!< Datapump master pump task can write data */
53 #define DATAPUMP_FLAGS_CLOSE (1 << 2) /*!< Request for datapump pump tasks to terminate */
54 #define DATAPUMP_FLAGS_CLOSED (1 << 3) /*!< Datapump slave task has been terminated */
55 
56 /*forward*/ struct datapump_t;
57 
58 /**
59  * A callback which will be called in a task context to open a socket for connection
60  *
61  * @param Pump A socket to be created for this datapump
62  * @return Socket descriptor in CONNECTED state or -1 if connect failed
63  */
64 typedef int (*datapump_connect_task_cb)(struct datapump_t *Pump);
65 
66 /**
67  * Datapump virtual table
68  */
70 {
71  bool (*Handshake)(struct datapump_t *Pump, int Sock);
72  int (*Send)(struct datapump_t *Pump, const uint8_t *Ptr, size_t Len);
73  int (*Recv)(struct datapump_t *Pump, uint8_t *Ptr, size_t Len);
74  void (*Close)(struct datapump_t *Pump);
75 };
76 
77 /**
78  * Pump tasks configuration
79  */
81 {
82  datapump_connect_task_cb ConnectCb; /*!< A callback called by a task to establish connection. */
83  uint32_t ConnectTimeout; /*!< Connect / handshake timeout */
84  uint32_t SendTimeout; /*!< Send timeout */
85  uint32_t ReceiveTimeout; /*!< Receive timeout */
86  uint16_t RbufInSize; /*!< Size of IN part of datapump_conn_t::RbufData ring buffer */
87  uint16_t RbufOutSize; /*!< Size of OUT part of datapump_conn_t::RbufData ring buffer */
88  uint8_t EventOnClosed; /*!< Contiki event ID to be sent to an owner process at socket closing */
89  uint8_t EventOnReceived; /*!< Contiki event ID to be sent to an owner process at data reception */
90 };
91 
92 /**
93  * datapump data pump instance.
94  */
95 struct datapump_t
96 {
97  const struct datapump_cfg_t *PumpCfg; /*!< Pump tasks configuration */
98  const struct datapump_vtbl_t *Vtbl; /*!< Virtual table */
99  struct rbuf_t RbufIn; /*!< IN ring buffer control structure */
100  struct rbuf_t RbufOut; /*!< OUT ring buffer control structure */
101  struct process *ContikiProcess; /*!< Owner Contiki process, will receive datapump_cfg_t::EventOn... events */
102  EventGroupHandle_t EventGroup; /*!< DATAPUMP_FLAGS_xxx waitable bits */
103  TaskHandle_t MasterTask; /*!< Master task, used for connect, handshake and write */
104  TaskHandle_t SlaveTask; /*!< Slave task, used for read */
105  uint8_t RbufData[0]; /*!< Ring buffer data. Must be allocated w.r.t.
106  * datapump_pump_cfg_t::RbufInSize, datapump_pump_cfg_t::RbufOutSize */
107 };
108 
109 /**
110  * @internal
111  */
112 bool DatapumpInitImpl(struct datapump_t *Pump);
113 
114 /**
115  * Initializes a connection and starts data pump tasks
116  *
117  * @param Pump Pointer to an uninitialized data pump instance
118  * @param Vtbl Pointer to a virtual function table
119  * @param PumpCfg Constant pointer to a data pump configuration
120  * @return true if finished successfully, false otherwise
121  */
122 static inline bool DatapumpInit(struct datapump_t *Pump, const struct datapump_vtbl_t *Vtbl, const struct datapump_cfg_t *PumpCfg)
123 {
124  Pump->Vtbl = Vtbl;
125  Pump->PumpCfg = PumpCfg;
126  return DatapumpInitImpl(Pump);
127 }
128 
129 /**
130  * Deallocates resources, associated with a datapump.
131  *
132  * @param Pump Pointer to a data pump instance
133  */
134 void DatapumpFree(struct datapump_t* Pump);
135 
136 /**
137  * Receives data from a datapump connection.
138  *
139  * The function can be called from a Contiki process.
140  * It always read complete data blocks.
141  *
142  * @param Pump Pointer to a datapump instance
143  * @param Buffer A buffer to put data into
144  * @param Length Length of the buffer (number of bytes to read)
145  * @return true if data was read, false otherwise.
146  */
147 bool DatapumpRead(struct datapump_t* Pump, uint8_t* Buffer, size_t Length);
148 
149 /**
150  * Sends data to a datapump connection.
151  *
152  * The function can be called from a Contiki process.
153  * It always read complete data blocks.
154  *
155  * @param Pump Pointer to a datapump instance
156  * @param Buffer A buffer to put data into
157  * @param Length Length of the buffer (number of bytes to read)
158  * @return true if data was read, false otherwise.
159  */
160 bool DatapumpWrite(struct datapump_t* Pump, const uint8_t* Buffer, size_t Length);
161 
162 /**
163  * Returns the number of bytes ready in the incoming buffer.
164  *
165  * @param Pump Pointer to a datapump data pump instance
166  * @return Number of bytes ready in the incoming buffer
167  */
168 size_t DatapumpBytesIn(struct datapump_t* Pump);
169 
170 /** \} */
171 
172 #endif
uint32_t ReceiveTimeout
Definition: datapump.h:85
Header file for ring buffer primitive library.
struct Task_t * TaskHandle_t
Definition: rtos.h:109
struct EventGroup_t * EventGroupHandle_t
Definition: rtos.h:582
int(* datapump_connect_task_cb)(struct datapump_t *Pump)
Definition: datapump.h:64
const struct datapump_vtbl_t * Vtbl
Definition: datapump.h:98
TaskHandle_t MasterTask
Definition: datapump.h:103
struct process * ContikiProcess
Definition: datapump.h:101
uint32_t SendTimeout
Definition: datapump.h:84
datapump_connect_task_cb ConnectCb
Definition: datapump.h:82
size_t DatapumpBytesIn(struct datapump_t *Pump)
const struct datapump_cfg_t * PumpCfg
Definition: datapump.h:97
TaskHandle_t SlaveTask
Definition: datapump.h:104
uint8_t EventOnClosed
Definition: datapump.h:88
Header file for the rtos emulation layer.
Structure that holds the state of a ring buffer.
Definition: rbuf.h:63
bool DatapumpWrite(struct datapump_t *Pump, const uint8_t *Buffer, size_t Length)
bool DatapumpRead(struct datapump_t *Pump, uint8_t *Buffer, size_t Length)
uint8_t EventOnReceived
Definition: datapump.h:89
uint16_t RbufInSize
Definition: datapump.h:86
void DatapumpFree(struct datapump_t *Pump)
static bool DatapumpInit(struct datapump_t *Pump, const struct datapump_vtbl_t *Vtbl, const struct datapump_cfg_t *PumpCfg)
Definition: datapump.h:122
uint16_t RbufOutSize
Definition: datapump.h:87
EventGroupHandle_t EventGroup
Definition: datapump.h:102
uint32_t ConnectTimeout
Definition: datapump.h:83