gateway_protocol.c 2.34 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
#include "gateway_protocol.h"
2
#include "security_adapter.h"
3
4
5

#define GATEWAY_PROTOCOL_APP_KEY_SIZE       8

6
static gateway_protocol_checkup_callback_t checkup_callback;
7
8

void gateway_protocol_packet_encode (
9
    const gateway_protocol_conf_t *gwp_conf,
10
11
12
13
14
15
16
17
    const gateway_protocol_packet_type_t packet_type,
    const uint8_t payload_length,
    const uint8_t *payload,
    uint8_t *packet_length,
    uint8_t *packet)
{
    *packet_length = 0;

18
    memcpy(&packet[*packet_length], gwp_conf->app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE);
19
20
    (*packet_length) += GATEWAY_PROTOCOL_APP_KEY_SIZE;

21
    packet[*packet_length] = gwp_conf->dev_id;
22
23
24
25
26
27
28
29
30
31
    (*packet_length)++;

    packet[*packet_length] = packet_type;
    (*packet_length)++;

    packet[*packet_length] = payload_length;
    (*packet_length)++;

    memcpy(&packet[*packet_length], payload, payload_length);
    (*packet_length) += payload_length;
32
33
34

    if (gwp_conf->secure) {
	    security_adapter_encrypt(	gwp_conf->secure_key, 
Vladislav Rykov's avatar
Vladislav Rykov committed
35
					&packet[GATEWAY_PROTOCOL_APP_KEY_SIZE], 
36
					(uint16_t *)packet_length,
Vladislav Rykov's avatar
Vladislav Rykov committed
37
38
					&packet[GATEWAY_PROTOCOL_APP_KEY_SIZE], 
					(*packet_length-GATEWAY_PROTOCOL_APP_KEY_SIZE)
39
40
41
	    );
	    (*packet_length) += GATEWAY_PROTOCOL_APP_KEY_SIZE; 
    }
42
43
44
}

uint8_t gateway_protocol_packet_decode (
45
    gateway_protocol_conf_t *gwp_conf,
46
47
48
    gateway_protocol_packet_type_t *packet_type,
    uint8_t *payload_length,
    uint8_t *payload,
Vladislav Rykov's avatar
Vladislav Rykov committed
49
50
    uint8_t packet_length,
    uint8_t *packet)
51
52
53
{
    uint8_t p_len = 0;

54
    memcpy(gwp_conf->app_key, &packet[p_len], GATEWAY_PROTOCOL_APP_KEY_SIZE);
55
56
    p_len += GATEWAY_PROTOCOL_APP_KEY_SIZE;

Vladislav Rykov's avatar
Vladislav Rykov committed
57
    gwp_conf->app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE] = '\0';
58

Vladislav Rykov's avatar
Vladislav Rykov committed
59
    checkup_callback(gwp_conf);
60
61
    if (gwp_conf->secure) {
	    security_adapter_decrypt(	gwp_conf->secure_key, 
Vladislav Rykov's avatar
Vladislav Rykov committed
62
63
64
					&packet[GATEWAY_PROTOCOL_APP_KEY_SIZE], 
					(packet_length-GATEWAY_PROTOCOL_APP_KEY_SIZE),
					&packet[GATEWAY_PROTOCOL_APP_KEY_SIZE], 
65
					(uint16_t *)&packet_length
66
67
68
69
	    );
    }

    gwp_conf->dev_id = packet[p_len];
70
71
72
73
74
    p_len++;

    *packet_type = (gateway_protocol_packet_type_t) packet[p_len];
    p_len++;

75
    *payload_length = packet[p_len];
76
77
78
79
80
    p_len++;

    memcpy(payload, &packet[p_len], *payload_length);
    p_len += *payload_length;

81
82
83
84
85
    return p_len;
}

void gateway_protocol_set_checkup_callback(gateway_protocol_checkup_callback_t callback) {
    checkup_callback = callback;
86
}
87