Tiny protocol  0.7.0
Tiny communication protocol for microcontrollers
tiny_layer2.h
Go to the documentation of this file.
1 /*
2  Copyright 2016-2019 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  Protocol Library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Protocol Library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #ifndef _TINY_LAYER2_H_
27 #define _TINY_LAYER2_H_
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <stdint.h>
35 
37 typedef enum
38 {
39  TINY_TX_STATE_IDLE,
40  TINY_TX_STATE_START,
41  TINY_TX_STATE_SEND_DATA,
42  TINY_TX_STATE_SEND_CRC,
43  TINY_TX_STATE_END
44 } ETinyTxState;
45 
46 typedef enum
47 {
48  TINY_RX_STATE_IDLE,
49  TINY_RX_STATE_START,
50  TINY_RX_STATE_READ_DATA,
51  TINY_RX_STATE_END
52 } ETinyRxState;
54 
59 typedef struct
60 {
62 
63  int framelen;
65  uint8_t inprogress;
67  fcs_t fcs;
69  uint8_t blockIndex;
70  uint8_t bits;
71  uint8_t prevbyte;
74 
75 
80 typedef struct
81 {
83  int sentbytes;
85  uint8_t *pframe;
87  int framelen;
89  uint8_t inprogress;
91  fcs_t fcs; // fcs field
93  uint8_t blockIndex; // index of the frame block being processed
94  uint8_t prevbyte; // last byte sent
95  uint8_t bits; // index of crc byte being sent
96  uint8_t locked;
99 
100 /*************************************************************
101 *
102 * Tiny defines
103 *
104 **************************************************************/
105 
110 typedef struct
111 {
113  void* pdata;
114 #ifdef CONFIG_ENABLE_STATS
115  STinyStats stat;
117 #endif
118  write_block_cb_t write_func;
126 #ifdef PLATFORM_MUTEX
127  PLATFORM_MUTEX send_mutex; // Mutex for send operation
128 #endif
129 #ifdef PLATFORM_COND
130  PLATFORM_COND send_condition; // Condition is called, when send operation is completed
131 #endif
132  uint8_t fcs_bits;
135 
140 
144  uint8_t uid_support;
145 } STinyData;
146 
147 
167 extern int tiny_init(STinyData *handle,
168  write_block_cb_t write_func,
169  read_block_cb_t read_func,
170  void *pdata);
171 
172 
180 extern int tiny_close(STinyData *handle);
181 
182 
207 extern int tiny_send(STinyData *handle, uint16_t *uid, uint8_t * pbuf, int len, uint8_t flags);
208 
209 
231 extern int tiny_read(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int len, uint8_t flags);
232 
233 
248 extern int tiny_simple_send(STinyData *handle, uint8_t *pbuf, int len);
249 
268 extern int tiny_simple_read(STinyData *handle, uint8_t *pbuf, int len);
269 
289 extern void tiny_enable_uid(STinyData *handle, uint8_t on);
290 
299 extern int tiny_set_fcs_bits(STinyData *handle, uint8_t bits);
300 
301 
323 extern int tiny_on_rx_byte(STinyData * handle, uint8_t *pbuf, int len, uint8_t byte);
324 
350 extern int tiny_send_start(STinyData *handle, uint8_t flags);
351 
386 extern int tiny_send_buffer(STinyData *handle, uint8_t * pbuf, int len, uint8_t flags);
387 
388 
412 extern int tiny_send_end(STinyData *handle, uint8_t flags);
413 
422 extern void tiny_send_terminate(STinyData *handle);
423 
452 extern int tiny_read_start(STinyData * handle, uint8_t flags);
453 
454 
504 extern int tiny_read_buffer(STinyData *handle, uint8_t *pbuf, int len, uint8_t flags);
505 
506 
516 extern void tiny_read_terminate(STinyData *handle);
517 
518 
530 extern int tiny_lock(STinyData *handle, uint8_t flags);
531 
532 
542 extern void tiny_unlock(STinyData *handle);
543 
544 
555 extern int tiny_set_callbacks(STinyData *handle,
556  on_frame_cb_t read_cb,
557  on_frame_cb_t send_cb);
558 
559 
569 extern int tiny_get_callbacks(STinyData *handle,
570  on_frame_cb_t *read_cb,
571  on_frame_cb_t *send_cb);
572 
573 
574 #ifdef CONFIG_ENABLE_STATS
575 
584 extern int tiny_get_stat(STinyData *handle, STinyStats *stat);
585 
586 
595 extern int tiny_clear_stat(STinyData *handle);
596 
597 #endif /* CONFIG_ENABLE_STATS */
598 
604 #ifdef __cplusplus
605 }
606 #endif
607 
608 #endif /* _TINY_PROTOCOL_H_ */
int tiny_on_rx_byte(STinyData *handle, uint8_t *pbuf, int len, uint8_t byte)
The function processes one rx byte. Used in event-based mode. This function processes single received...
Definition: tiny_layer2.c:625
Definition: tiny_layer2.h:80
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_proto_types.h:114
int tiny_close(STinyData *handle)
Definition: tiny_layer2.c:182
int tiny_lock(STinyData *handle, uint8_t flags)
locks Tiny state machine for send operations
Definition: tiny_layer2.c:1077
fcs_t fcs
The field contains calculated checksum and not available in TINY_MINIMAL configuration.
Definition: tiny_layer2.h:91
Definition: tiny_layer2.h:59
uint8_t inprogress
The state of Receive State machine.
Definition: tiny_layer2.h:65
Definition: tiny_proto_types.h:89
Tiny protocol Types.
void tiny_unlock(STinyData *handle)
unlock Tiny state machine for send operations
Definition: tiny_layer2.c:1095
int tiny_send_buffer(STinyData *handle, uint8_t *pbuf, int len, uint8_t flags)
sends user provided data in the body of the frame
Definition: tiny_layer2.c:425
STinyTxStatus tx
Definition: tiny_layer2.h:125
int tiny_set_fcs_bits(STinyData *handle, uint8_t bits)
Definition: tiny_layer2.c:201
int tiny_send_start(STinyData *handle, uint8_t flags)
initiates sending of a new frame
Definition: tiny_layer2.c:376
Definition: tiny_layer2.h:110
int tiny_get_stat(STinyData *handle, STinyStats *stat)
Definition: tiny_layer2.c:1004
int tiny_clear_stat(STinyData *handle)
Definition: tiny_layer2.c:1014
int tiny_simple_read(STinyData *handle, uint8_t *pbuf, int len)
reads frame from the channel in blocking mode.
Definition: tiny_layer2.c:990
void tiny_enable_uid(STinyData *handle, uint8_t on)
The function enables uid support. Enables uid support. The function affects on tiny_on_rx_byte and on...
Definition: tiny_layer2.c:227
int tiny_init(STinyData *handle, write_block_cb_t write_func, read_block_cb_t read_func, void *pdata)
Definition: tiny_layer2.c:145
int tiny_send(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int len, uint8_t flags)
sends frame with user payload to communication channel
Definition: tiny_layer2.c:557
int sentbytes
Number of payload bytes, sent from the frame being sent.
Definition: tiny_layer2.h:83
int framelen
Number of bytes in the frame being received.
Definition: tiny_layer2.h:63
void * pdata
pointer to application defined data, passed during protocol initialization - absent in Arduino versio...
Definition: tiny_layer2.h:113
STinyRxStatus rx
Definition: tiny_layer2.h:123
uint8_t uid_support
flag indicates if uid support is enabled. It is important for tiny_on_rx_byte()
Definition: tiny_layer2.h:144
fcs_t fcs
The field contains calculated checksum and not available in TINY_MINIMAL configuration.
Definition: tiny_layer2.h:67
read_block_cb_t read_func
pointer to platform related read function
Definition: tiny_layer2.h:121
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_proto_types.h:125
int tiny_read_start(STinyData *handle, uint8_t flags)
initiates receiving of a new frame
Definition: tiny_layer2.c:742
void tiny_read_terminate(STinyData *handle)
terminates read operation
Definition: tiny_layer2.c:910
int tiny_simple_send(STinyData *handle, uint8_t *pbuf, int len)
sends frame with user payload to communication channel in blocking mode
Definition: tiny_layer2.c:611
int tiny_get_callbacks(STinyData *handle, on_frame_cb_t *read_cb, on_frame_cb_t *send_cb)
returns callbacks assigned for frame processing The function returns set callbacks.
Definition: tiny_layer2.c:1054
int framelen
Length of the frame passed by a user.
Definition: tiny_layer2.h:87
uint8_t inprogress
Definition: tiny_layer2.h:89
on_frame_cb_t write_cb
pointer to callback function to get data being sent
Definition: tiny_layer2.h:142
void tiny_send_terminate(STinyData *handle)
terminates send operation
Definition: tiny_layer2.c:537
void(* on_frame_cb_t)(void *handle, uint16_t uid, uint8_t *pdata, int size)
Definition: tiny_proto_types.h:138
on_frame_cb_t read_cb
pointer to callback function
Definition: tiny_layer2.h:138
int tiny_set_callbacks(STinyData *handle, on_frame_cb_t read_cb, on_frame_cb_t send_cb)
set callbacks for processing frames The function sets callback procs for specified Tiny channel...
Definition: tiny_layer2.c:1031
int tiny_read(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int len, uint8_t flags)
Definition: tiny_layer2.c:921
int tiny_read_buffer(STinyData *handle, uint8_t *pbuf, int len, uint8_t flags)
reads frame payload to provided buffer
Definition: tiny_layer2.c:790
uint8_t * pframe
Pointer to the frame data being sent.
Definition: tiny_layer2.h:85
int tiny_send_end(STinyData *handle, uint8_t flags)
completes sending of a new frame
Definition: tiny_layer2.c:483