-
Notifications
You must be signed in to change notification settings - Fork 2
/
axu_connector.c
86 lines (73 loc) · 2.4 KB
/
axu_connector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2018 The go-hpb Authors
// This file is part of the go-hpb.
//
// The go-hpb is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-hpb is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-hpb. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "axu_connector.h"
#include "atomic.h"
static uint16_t g_sequence_id = 0;
#define fetch_axu_package_sequence() atomic_fetch_and_add(&g_sequence_id,1)
A_Package* axu_package_new(uint32_t len)
{
if(len > PACKAGE_MAX_SIZE)
{
printf("Package len (%d) is bigger than PACKAGE_MAX_SIZE(%d) \r\n", len, (int)PACKAGE_MAX_SIZE);
return NULL;
}
A_Package * pack = (A_Package*)malloc(len + sizeof(A_Package));
if(pack != NULL)
pack->header.body_length = len;
return pack;
}
int axu_package_free(A_Package *pack)
{
if(pack != NULL)
free(pack);
return 0;
}
int axu_package_len(A_Package *pack)
{
return pack->header.body_length + sizeof(A_Package);
}
void axu_package_init(A_Package *pack, A_Package* req, ACmd cmd)
{
pack->header.magic_aacc = 0xaacc;
pack->header.magic_ccaa = 0xccaa;
pack->header.acmd = cmd;
pack->checksum = 0;
memset(pack->data, 0, pack->header.body_length);
if(req){
// is a response package.
pack->header.package_id = req->header.package_id;
pack->header.q_or_r = AP_RESPONSE;
}else{
pack->header.package_id = fetch_axu_package_sequence();
pack->header.q_or_r = AP_QUERY;
}
}
int axu_set_data(A_Package *pack, int offset, uint8_t *data, int len)
{
if((offset + len) > pack->header.body_length){
printf("axu_set_data offset(%d) + len(%d) > body_length(%d)\n", offset, len, pack->header.body_length);
return 1;
}
memcpy(pack->data + offset, data, len);
return 0;
}
void axu_finish_package(A_Package *pack)
{
pack->checksum = checksum(pack->data, pack->header.body_length);
}