libschc
Functions | Variables
compressor.c File Reference
#include <string.h>
#include <stdio.h>
#include "jsmn.h"
#include "picocoap.h"
#include "compressor.h"
#include "schc_config.h"
#include "rules/rule_config.h"

Functions

static void get_node_ip (schc_ipaddr_t *node_ip)
 Get the node its IP address as set during initialization. More...
 
static struct schc_deviceget_device_by_id (uint32_t device_id)
 Get a device by it's id. More...
 
int8_t set_rule_id (struct schc_rule_t *schc_rule, uint8_t *data)
 
struct schc_rule_tget_schc_rule_by_reliability_mode (struct schc_rule_t *schc_rule, reliability_mode mode, uint32_t device_id)
 
static struct schc_rule_tget_schc_rule_by_layer_ids (uint8_t ip_rule_id, uint8_t udp_rule_id, uint8_t coap_rule_id, uint32_t device_id, reliability_mode mode)
 
struct schc_rule_tget_schc_rule_by_rule_id (uint8_t *rule_arr, uint32_t device_id)
 
static uint8_t compress (schc_bitarray_t *dst, schc_bitarray_t *src, const struct schc_layer_rule_t *rule, direction DI)
 The compression mechanism. More...
 
static uint8_t decompress (struct schc_layer_rule_t *rule, schc_bitarray_t *src, schc_bitarray_t *dst, direction DI)
 The decompression mechanism. More...
 
static struct schc_layer_rule_tschc_find_rule_from_header (schc_bitarray_t *src, uint32_t device_id, schc_layer_t layer, direction DI)
 Find a matching rule for a layer. More...
 
static void swap_ipv6_source_and_destination (uint8_t *ptr)
 Swaps the IPv6 source and destination. More...
 
static uint8_t equal (struct schc_field *target_field, unsigned char *field_value, uint16_t field_offset)
 The equal matching operator. More...
 
static uint8_t ignore (struct schc_field *target_field, unsigned char *field_value, uint16_t field_offset)
 The ignore matching operator. More...
 
static uint8_t MSB (struct schc_field *target_field, unsigned char *field_value, uint16_t field_offset)
 The MSB matching operator MSB(x): a match is obtained if the most significant (leftmost) x bits of the packet header field value are equal to the TV in the Rule. More...
 
static uint8_t matchmap (struct schc_field *target_field, unsigned char *field_value, uint16_t field_offset)
 The match-map matching operator match-mapping: With match-mapping, the Target Value is a list of values. More...
 
static void set_node_ip (schc_ipaddr_t *node_ip)
 Notifies the compressor about the node its IP address. More...
 
uint8_t schc_compressor_init (uint8_t src[16])
 Initializes the SCHC compressor. More...
 
int16_t schc_compress (uint8_t *data, uint16_t total_length, schc_bitarray_t *dst, uint32_t device_id, direction dir, struct schc_rule_t **schc_rule)
 Compresses a CoAP/UDP/IP packet. More...
 
static uint16_t compute_length (unsigned char *data, uint16_t data_len)
 Set the packet length for the UDP and IP headers. More...
 
static uint16_t chksum (uint16_t sum, const uint8_t *data, uint16_t len)
 
uint16_t compute_checksum (unsigned char *data)
 Calculates the UDP checksum and sets the appropriate header fields. More...
 
uint16_t schc_decompress (schc_bitarray_t *bit_arr, uint8_t *buf, uint32_t device_id, uint16_t total_length, direction dir)
 Construct the header from the layered set of rules. More...
 

Variables

static schc_ipaddr_t node_ip_6
 
jsmn_parser json_parser
 
jsmntok_t json_token [JSON_TOKENS]
 

Function Documentation

◆ chksum()

static uint16_t chksum ( uint16_t  sum,
const uint8_t *  data,
uint16_t  len 
)
static

◆ compress()

static uint8_t compress ( schc_bitarray_t dst,
schc_bitarray_t src,
const struct schc_layer_rule_t rule,
direction  DI 
)
static

The compression mechanism.

Parameters
dst_arrthe bit array in which to copy the contents to
src_arrthe original header
rulethe rule to match the compression with
Returns
the length length of the compressed header

◆ compute_checksum()

uint16_t compute_checksum ( unsigned char *  data)

Calculates the UDP checksum and sets the appropriate header fields.

Parameters
datapointer to the data packet
Returns
checksum the computed checksum

◆ compute_length()

static uint16_t compute_length ( unsigned char *  data,
uint16_t  data_len 
)
static

Set the packet length for the UDP and IP headers.

Parameters
datapointer to the data packet
data_lenthe length of the total packet
Returns
0

◆ decompress()

static uint8_t decompress ( struct schc_layer_rule_t rule,
schc_bitarray_t src,
schc_bitarray_t dst,
direction  DI 
)
static

The decompression mechanism.

Parameters
rulepointer to the rule to use during the decompression
srcthe received SCHC bit buffer
dstthe buffer to store the decompressed, original packet
Returns
the length of the decompressed header

◆ equal()

static uint8_t equal ( struct schc_field target_field,
unsigned char *  field_value,
uint16_t  field_offset 
)
static

The equal matching operator.

Parameters
target_fieldthe field from the rule
field_valuethe value from the header to compare with the rule value
field_offsetthe offset (in bits), starting from the field value pointer
Returns
1 if the target field matches the field value 0 if the target field doesn't match the field value

◆ get_device_by_id()

static struct schc_device* get_device_by_id ( uint32_t  device_id)
static

Get a device by it's id.

Parameters
device_idthe id of the device
Returns
schc_device the device which is found NULL if no device was found

◆ get_node_ip()

static void get_node_ip ( schc_ipaddr_t node_ip)
static

Get the node its IP address as set during initialization.

Returns
node_ip_6 the node its IP address

◆ get_schc_rule_by_layer_ids()

static struct schc_rule_t* get_schc_rule_by_layer_ids ( uint8_t  ip_rule_id,
uint8_t  udp_rule_id,
uint8_t  coap_rule_id,
uint32_t  device_id,
reliability_mode  mode 
)
static

◆ get_schc_rule_by_reliability_mode()

struct schc_rule_t* get_schc_rule_by_reliability_mode ( struct schc_rule_t schc_rule,
reliability_mode  mode,
uint32_t  device_id 
)

◆ get_schc_rule_by_rule_id()

struct schc_rule_t* get_schc_rule_by_rule_id ( uint8_t *  rule_arr,
uint32_t  device_id 
)

◆ ignore()

static uint8_t ignore ( struct schc_field target_field,
unsigned char *  field_value,
uint16_t  field_offset 
)
static

The ignore matching operator.

Parameters
target_fieldthe field from the rule
field_valuethe value from the header to compare with the rule value
Returns
1

◆ matchmap()

static uint8_t matchmap ( struct schc_field target_field,
unsigned char *  field_value,
uint16_t  field_offset 
)
static

The match-map matching operator match-mapping: With match-mapping, the Target Value is a list of values.

Each value of the list is identified by an index. Compression is achieved by sending the index instead of the original header field value.

Parameters
target_fieldthe field from the rule
field_valuethe value from the header to compare with the rule value
Returns
1 if the the field value is equal to one of the values found in the mapping array 0 if no matching value is found in the mapping array

◆ MSB()

static uint8_t MSB ( struct schc_field target_field,
unsigned char *  field_value,
uint16_t  field_offset 
)
static

The MSB matching operator MSB(x): a match is obtained if the most significant (leftmost) x bits of the packet header field value are equal to the TV in the Rule.

The x parameter of the MSB MO indicates how many bits are involved in the comparison. If the FL is described as variable, the x parameter must be a multiple of the FL unit. For example, x must be multiple of 8 if the unit of the variable length is bytes.

Parameters
target_fieldthe field from the rule
field_valuethe value from the header to compare with the rule value
Returns
1 if the MSB of the target field matches the MSB of the field value 0 if the MSB of the target field doesn't match the MSB of the field value

◆ schc_compress()

int16_t schc_compress ( uint8_t *  data,
uint16_t  total_length,
schc_bitarray_t dst,
uint32_t  device_id,
direction  dir,
struct schc_rule_t **  schc_rule 
)

Compresses a CoAP/UDP/IP packet.

Parameters
datapointer to the original packet
total_lengththe length of the packet
dstpointer to the bit array object, where the compressed packet will be stored. Can later be passed to fragmenter
device_idthe device id to find a rule for
directionthe direction of the flow UP: LPWAN to IPv6 or DOWN: IPv6 to LPWAN
device_typethe device type: NETWORK_GATEWAY or DEVICE
schc_rulea pointer to a schc rule struct to return the rule that was found
Returns
length the length of the compressed packet -1 on a memory overflow
Note
the compressor will only look for rules configured with the NOT_FRAGMENTED reliability mode

◆ schc_compressor_init()

uint8_t schc_compressor_init ( uint8_t  src[16])

Initializes the SCHC compressor.

Parameters
node_ipa pointer to the source it's ip address
Returns
error error codes on error

◆ schc_decompress()

uint16_t schc_decompress ( schc_bitarray_t bit_arr,
uint8_t *  buf,
uint32_t  device_id,
uint16_t  total_length,
direction  dir 
)

Construct the header from the layered set of rules.

Parameters
datapointer to the received data
bufpointer where to save the decompressed packet
device_idthe device its id
total_lengththe total length of the received data
directionthe direction of the flow (UP: LPWAN to IPv6, DOWN: IPv6 to LPWAN)
Returns
length length of the newly constructed packet 0 the rule was not found

◆ schc_find_rule_from_header()

static struct schc_layer_rule_t* schc_find_rule_from_header ( schc_bitarray_t src,
uint32_t  device_id,
schc_layer_t  layer,
direction  DI 
)
static

Find a matching rule for a layer.

Parameters
schc_bitarraythe bit array as received from the network note: a conversion is required for CoAP to decode the options
device_idthe device to find an IP rule for
schc_layerthe layer for which to find a rule for
Returns
the rule NULL if no rule is found

◆ set_node_ip()

static void set_node_ip ( schc_ipaddr_t node_ip)
static

Notifies the compressor about the node its IP address.

Parameters
node_ippointer to the ip address array
Returns
0

◆ set_rule_id()

int8_t set_rule_id ( struct schc_rule_t schc_rule,
uint8_t *  data 
)

◆ swap_ipv6_source_and_destination()

static void swap_ipv6_source_and_destination ( uint8_t *  ptr)
static

Swaps the IPv6 source and destination.

Parameters
ptrpointer to the ipv6 header

Variable Documentation

◆ json_parser

jsmn_parser json_parser

◆ json_token

jsmntok_t json_token[JSON_TOKENS]

◆ node_ip_6

schc_ipaddr_t node_ip_6
static