Virgil IoT KIT
snap-structs.h
Go to the documentation of this file.
1 // Copyright (C) 2015-2020 Virgil Security, Inc.
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // (1) Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // (2) Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in
14 // the documentation and/or other materials provided with the
15 // distribution.
16 //
17 // (3) Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
22 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 // DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
34 
40 #ifndef VS_SNAP_STRUCTS_H
41 #define VS_SNAP_STRUCTS_H
42 
43 #include <stdbool.h>
44 #include <stdint.h>
45 #include <stdlib.h>
47 
48 #ifdef __cplusplus
49 namespace VirgilIoTKit {
50 extern "C" {
51 #endif
52 
53 struct vs_netif_t;
55 
58 typedef uint16_t vs_snap_transaction_id_t;
59 
64 typedef uint32_t vs_snap_service_id_t;
65 
70 typedef uint32_t vs_snap_element_t;
71 
85 typedef vs_status_e (*vs_netif_rx_cb_t)(struct vs_netif_t *netif,
86  const uint8_t *data,
87  const uint16_t data_sz,
88  const uint8_t **packet_data,
89  uint16_t *packet_data_sz);
90 
102 typedef vs_status_e (*vs_netif_process_cb_t)(struct vs_netif_t *netif, const uint8_t *data, const uint16_t data_sz);
103 
116 typedef vs_status_e (*vs_netif_tx_t)(struct vs_netif_t *netif, const uint8_t *data, const uint16_t data_sz);
117 
129 typedef vs_status_e (*vs_netif_mac_t)(const struct vs_netif_t *netif, struct vs_mac_addr_t *mac_addr);
130 
143 typedef vs_status_e (*vs_netif_init_t)(struct vs_netif_t *netif,
144  const vs_netif_rx_cb_t rx_cb,
145  const vs_netif_process_cb_t process_cb);
146 
157 typedef vs_status_e (*vs_netif_deinit_t)(struct vs_netif_t *netif);
158 
175  vs_snap_element_t element_id,
176  const uint8_t *request,
177  const uint16_t request_sz,
178  uint8_t *response,
179  const uint16_t response_buf_sz,
180  uint16_t *response_sz);
181 
196  vs_snap_element_t element_id,
197  bool is_ack,
198  const uint8_t *response,
199  const uint16_t response_sz);
200 
210 
219 
224 typedef enum {
233 
234 #define ETH_ADDR_LEN (6)
235 #define ETH_TYPE_LEN (2)
236 #define ETH_CRC_LEN (4)
237 #define ETH_HEADER_LEN (ETH_ADDR_LEN + ETH_ADDR_LEN + ETH_TYPE_LEN)
238 #define ETH_MIN_LEN (64)
239 #define ETH_MTU (1500)
240 
241 #define VS_ETHERTYPE_VIRGIL (HTONS_IN_COMPILE_TIME(0xABCD))
242 
244 typedef enum {
248 
249 /******************************************************************************/
252 typedef struct __attribute__((__packed__)) vs_mac_addr_t {
253  uint8_t bytes[ETH_ADDR_LEN];
254 } vs_mac_addr_t;
255 
256 /******************************************************************************/
259 typedef struct __attribute__((__packed__)) ethernet_header {
262  uint16_t type;
263 } vs_ethernet_header_t;
264 
265 /******************************************************************************/
268 typedef struct __attribute__((__packed__)) {
271  vs_snap_element_t element_id; // CODEGEN: SKIP
272  uint32_t flags; // CODEGEN: SKIP
273  uint16_t padding;
274  uint16_t content_size;
276 
277 /******************************************************************************/
280 typedef struct __attribute__((__packed__)) {
281  vs_ethernet_header_t eth_header;
283  uint8_t content[];
285 
286 #define VS_NETIF_PACKET_BUF_SIZE (1024)
287 
288 /******************************************************************************/
293 typedef struct vs_netif_t {
294  void *user_data;
296  // Functions
302  // Incoming packet
303  uint8_t packet_buf[VS_NETIF_PACKET_BUF_SIZE];
304  uint16_t packet_buf_filled;
305 } vs_netif_t;
306 
307 /******************************************************************************/
312 typedef struct {
313  void *user_data;
320 
321 /******************************************************************************/
324 typedef struct {
325  uint32_t sent;
326  uint32_t received;
328 
329 #ifdef __cplusplus
330 } // extern "C"
331 } // namespace VirgilIoTKit
332 #endif
333 
334 #endif // VS_SNAP_STRUCTS_H
vs_netif_t::deinit
vs_netif_deinit_t deinit
Destroy.
Definition: snap-structs.h:298
vs_netif_process_cb_t
vs_status_e(* vs_netif_process_cb_t)(struct vs_netif_t *netif, const uint8_t *data, const uint16_t data_sz)
Preprocessed data.
Definition: snap-structs.h:102
vs_snap_packet_t::header
vs_snap_header_t header
Packet header.
Definition: snap-structs.h:282
vs_snap_header_t::service_id
vs_snap_service_id_t service_id
SNAP service.
Definition: snap-structs.h:270
vs_netif_rx_cb_t
vs_status_e(* vs_netif_rx_cb_t)(struct vs_netif_t *netif, const uint8_t *data, const uint16_t data_sz, const uint8_t **packet_data, uint16_t *packet_data_sz)
Received data.
Definition: snap-structs.h:85
vs_netif_t::packet_buf_filled
uint16_t packet_buf_filled
Packet size.
Definition: snap-structs.h:304
vs_snap_service_id_t
uint32_t vs_snap_service_id_t
SNAP service ID.
Definition: snap-structs.h:64
vs_netif_deinit_t
vs_status_e(* vs_netif_deinit_t)(struct vs_netif_t *netif)
Destructor.
Definition: snap-structs.h:157
vs_netif_t::init
vs_netif_init_t init
Initialization.
Definition: snap-structs.h:297
vs_snap_service_t::request_process
vs_snap_service_request_processor_t request_process
Reqeust processing.
Definition: snap-structs.h:315
vs_snap_service_periodical_processor_t
vs_status_e(* vs_snap_service_periodical_processor_t)(void)
SNAP Periodical data.
Definition: snap-structs.h:209
vs_status_e
vs_status_e
Status code.
Definition: status_code.h:77
ETH_ADDR_LEN
#define ETH_ADDR_LEN
Definition: snap-structs.h:234
vs_mac_addr_t
MAC address.
Definition: snap-structs.h:252
vs_snap_service_response_processor_t
vs_status_e(* vs_snap_service_response_processor_t)(const struct vs_netif_t *netif, vs_snap_element_t element_id, bool is_ack, const uint8_t *response, const uint16_t response_sz)
SNAP Service Response Processor.
Definition: snap-structs.h:195
provision-structs.h
Provision interface structures.
vs_snap_header_t::transaction_id
vs_snap_transaction_id_t transaction_id
Transaction ID.
Definition: snap-structs.h:269
vs_snap_element_t
uint32_t vs_snap_element_t
SNAP element ID.
Definition: snap-structs.h:70
ethernet_header::type
uint16_t type
Ethernet packet type.
Definition: snap-structs.h:262
vs_snap_service_t::periodical_process
vs_snap_service_periodical_processor_t periodical_process
Periodical task.
Definition: snap-structs.h:317
vs_netif_mac_t
vs_status_e(* vs_netif_mac_t)(const struct vs_netif_t *netif, struct vs_mac_addr_t *mac_addr)
Get MAC address.
Definition: snap-structs.h:129
vs_snap_stat_t::received
uint32_t received
Receives amount.
Definition: snap-structs.h:326
ethernet_header::src
vs_mac_addr_t src
Source MAC address.
Definition: snap-structs.h:261
vs_netif_t::tx
vs_netif_tx_t tx
Transmit data.
Definition: snap-structs.h:299
vs_snap_header_t::padding
uint16_t padding
Packet padding.
Definition: snap-structs.h:273
vs_snap_header_t
SNAP packet header.
Definition: snap-structs.h:268
VS_SNAP_FLAG_ACK
@ VS_SNAP_FLAG_ACK
Confirmation about receiving a correct packet.
Definition: snap-structs.h:245
vs_netif_tx_t
vs_status_e(* vs_netif_tx_t)(struct vs_netif_t *netif, const uint8_t *data, const uint16_t data_sz)
Send data.
Definition: snap-structs.h:116
vs_snap_header_t::content_size
uint16_t content_size
Packet vs_snap_packet_t content data size.
Definition: snap-structs.h:274
VS_SNAP_DEV_LOGGER
@ VS_SNAP_DEV_LOGGER
Logger role.
Definition: snap-structs.h:228
VS_SNAP_DEV_DEBUGGER
@ VS_SNAP_DEV_DEBUGGER
Debugger role.
Definition: snap-structs.h:230
VS_SNAP_DEV_CONTROL
@ VS_SNAP_DEV_CONTROL
Control role.
Definition: snap-structs.h:227
vs_snap_flags_e
vs_snap_flags_e
SNAP Flags.
Definition: snap-structs.h:244
vs_snap_header_t::flags
uint32_t flags
Packet flags.
Definition: snap-structs.h:272
vs_snap_service_request_processor_t
vs_status_e(* vs_snap_service_request_processor_t)(const struct vs_netif_t *netif, vs_snap_element_t element_id, const uint8_t *request, const uint16_t request_sz, uint8_t *response, const uint16_t response_buf_sz, uint16_t *response_sz)
SNAP Service Request Processor.
Definition: snap-structs.h:174
VS_SNAP_DEV_THING
@ VS_SNAP_DEV_THING
Thing role.
Definition: snap-structs.h:226
vs_netif_t::user_data
void * user_data
User data.
Definition: snap-structs.h:294
vs_snap_service_t
SNAP service descriptor.
Definition: snap-structs.h:312
vs_netif_t
Network interface.
Definition: snap-structs.h:293
vs_snap_service_deinit_t
vs_status_e(* vs_snap_service_deinit_t)(void)
SNAP Service Destructor.
Definition: snap-structs.h:218
HTONL_IN_COMPILE_TIME
#define HTONL_IN_COMPILE_TIME(val)
Definition: provision-structs.h:87
vs_snap_service_t::user_data
void * user_data
User data.
Definition: snap-structs.h:313
VS_SNAP_DEV_GATEWAY
@ VS_SNAP_DEV_GATEWAY
Gateway role.
Definition: snap-structs.h:225
VS_NETIF_PACKET_BUF_SIZE
#define VS_NETIF_PACKET_BUF_SIZE
Definition: snap-structs.h:286
vs_snap_service_t::id
vs_snap_service_id_t id
Service ID.
Definition: snap-structs.h:314
vs_snap_packet_t::eth_header
vs_ethernet_header_t eth_header
Ethernet header.
Definition: snap-structs.h:281
vs_snap_stat_t
SNAP statistics.
Definition: snap-structs.h:324
vs_snap_device_role_e
vs_snap_device_role_e
Device roles.
Definition: snap-structs.h:224
vs_snap_service_t::response_process
vs_snap_service_response_processor_t response_process
Response processing.
Definition: snap-structs.h:316
ethernet_header
Ethernet header.
Definition: snap-structs.h:259
vs_snap_service_t::deinit
vs_snap_service_deinit_t deinit
Destructor call.
Definition: snap-structs.h:318
vs_snap_stat_t::sent
uint32_t sent
Sends amount.
Definition: snap-structs.h:325
vs_snap_packet_t
SNAP packet.
Definition: snap-structs.h:280
vs_snap_transaction_id_t
uint16_t vs_snap_transaction_id_t
SNAP transaction ID.
Definition: snap-structs.h:54
ethernet_header::dest
vs_mac_addr_t dest
Destination MAC address.
Definition: snap-structs.h:260
vs_netif_t::mac_addr
vs_netif_mac_t mac_addr
MAC address.
Definition: snap-structs.h:300
VS_SNAP_FLAG_NACK
@ VS_SNAP_FLAG_NACK
Notification about rejecting a packet.
Definition: snap-structs.h:246
vs_netif_init_t
vs_status_e(* vs_netif_init_t)(struct vs_netif_t *netif, const vs_netif_rx_cb_t rx_cb, const vs_netif_process_cb_t process_cb)
Initializer.
Definition: snap-structs.h:143
vs_snap_header_t::element_id
vs_snap_element_t element_id
Service's command.
Definition: snap-structs.h:271
VS_SNAP_DEV_INITIALIZER
@ VS_SNAP_DEV_INITIALIZER
Initializer role.
Definition: snap-structs.h:231
VS_SNAP_DEV_SNIFFER
@ VS_SNAP_DEV_SNIFFER
Sniffer role.
Definition: snap-structs.h:229