Commit 7a7face2 authored by vladislav's avatar vladislav
Browse files

appkey added

parent 132ea374
#include <gateway_protocol/gateway_protocol.h>
#define GATEWAY_PROTOCOL_APP_KEY_SIZE 8
static uint8_t app_key[GATEWAY_PROTOCOL_APP_KEY_SIZE];
static uint8_t dev_id = 0xFF;
void gateway_protocol_init(const uint8_t *appkey, const uint8_t devid) {
memcpy(app_key, appkey, GATEWAY_PROTOCOL_APP_KEY_SIZE);
dev_id = devid;
}
void gateway_protocol_packet_encode (
const uint8_t dev_id,
const gateway_protocol_packet_type_t packet_type,
const uint8_t payload_length,
const uint8_t *payload,
......@@ -11,6 +19,9 @@ void gateway_protocol_packet_encode (
{
*packet_length = 0;
memcpy(&packet[*packet_length], app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE);
(*packet_length) += GATEWAY_PROTOCOL_APP_KEY_SIZE;
packet[*packet_length] = dev_id;
(*packet_length)++;
......@@ -25,7 +36,6 @@ void gateway_protocol_packet_encode (
}
uint8_t gateway_protocol_packet_decode (
uint8_t *dev_id,
gateway_protocol_packet_type_t *packet_type,
uint8_t *payload_length,
uint8_t *payload,
......@@ -33,8 +43,13 @@ uint8_t gateway_protocol_packet_decode (
const uint8_t *packet)
{
uint8_t p_len = 0;
uint8_t appkey[GATEWAY_PROTOCOL_APP_KEY_SIZE];
uint8_t dev;
memcpy(appkey, &packet[p_len], GATEWAY_PROTOCOL_APP_KEY_SIZE);
p_len += GATEWAY_PROTOCOL_APP_KEY_SIZE;
*dev_id = packet[p_len];
dev = packet[p_len];
p_len++;
*packet_type = (gateway_protocol_packet_type_t) packet[p_len];
......@@ -46,5 +61,7 @@ uint8_t gateway_protocol_packet_decode (
memcpy(payload, &packet[p_len], *payload_length);
p_len += *payload_length;
return (p_len == packet_length);
return (memcmp(appkey, app_key, GATEWAY_PROTOCOL_APP_KEY_SIZE) &&
dev == dev_id &&
p_len == packet_length);
}
\ No newline at end of file
......@@ -26,8 +26,9 @@ typedef enum {
GATEWAY_PROTOCOL_STAT_NACK = 0xFF
} gateway_protocol_stat_t;
void gateway_protocol_init(const uint8_t *appkey, const uint8_t devid);
void gateway_protocol_packet_encode (
const uint8_t dev_id,
const gateway_protocol_packet_type_t packet_type,
const uint8_t payload_length,
const uint8_t *payload,
......@@ -35,7 +36,6 @@ void gateway_protocol_packet_encode (
uint8_t *packet);
uint8_t gateway_protocol_packet_decode (
uint8_t *dev_id,
gateway_protocol_packet_type_t *packet_type,
uint8_t *payload_length,
uint8_t *payload,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment