-
Notifications
You must be signed in to change notification settings - Fork 5
/
af_status_command.c
139 lines (112 loc) · 5.1 KB
/
af_status_command.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright 2018 Afero, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include "af_status_command.h"
#include "af_logger.h"
void af_status_command_initialize(af_status_command_t *af_status_command) {
memset(af_status_command, 0, sizeof(af_status_command_t));
af_status_command->cmd = AF_STATUS_COMMAND_STATUS;
}
void af_status_command_initialize_with_bytes_to_send(af_status_command_t *af_status_command, uint16_t bytes_to_send) {
af_status_command_initialize(af_status_command);
af_status_command->bytes_to_send = bytes_to_send;
}
void af_status_command_cleanup(af_status_command_t *af_status_command) {
memset(af_status_command, 0, sizeof(af_status_command_t));
}
uint16_t af_status_command_get_size(af_status_command_t *af_status_command) {
return sizeof(af_status_command->cmd) + sizeof(af_status_command->bytes_to_send) + sizeof(af_status_command->bytes_to_recv);
}
uint16_t af_status_command_get_bytes(af_status_command_t *af_status_command, uint8_t *bytes) {
int index = 0;
bytes[index++] = (af_status_command->cmd);
bytes[index++] = (af_status_command->bytes_to_send & 0xff);
bytes[index++] = ((af_status_command->bytes_to_send >> 8) & 0xff);
bytes[index++] = (af_status_command->bytes_to_recv & 0xff);
bytes[index++] = ((af_status_command->bytes_to_recv >> 8) & 0xff);
return index;
}
void af_status_command_set_checksum(af_status_command_t *af_status_command, uint8_t checksum) {
af_status_command->checksum = checksum;
}
uint8_t af_status_command_get_checksum(af_status_command_t *af_status_command) {
uint8_t result = 0;
result += (af_status_command->cmd);
result += (af_status_command->bytes_to_send & 0xff);
result += ((af_status_command->bytes_to_send >> 8) & 0xff);
result += (af_status_command->bytes_to_recv & 0xff);
result += ((af_status_command->bytes_to_recv >> 8) & 0xff);
return result;
}
void af_status_command_set_ack(af_status_command_t *af_status_command, bool ack) {
af_status_command->cmd = ack ? AF_STATUS_COMMAND_STATUS_ACK : AF_STATUS_COMMAND_STATUS;
}
void af_status_command_set_bytes_to_send(af_status_command_t *af_status_command, uint16_t bytes_to_send) {
af_status_command->bytes_to_send = bytes_to_send;
}
uint16_t af_status_command_get_bytes_to_send(af_status_command_t *af_status_command) {
return af_status_command->bytes_to_send;
}
void af_status_command_set_bytes_to_recv(af_status_command_t *af_status_command, uint16_t bytes_to_recv) {
af_status_command->bytes_to_recv = bytes_to_recv;
}
uint16_t af_status_command_get_bytes_to_recv(af_status_command_t *af_status_command) {
return af_status_command->bytes_to_recv;
}
bool af_status_command_equals(af_status_command_t *af_status_command_me, af_status_command_t *af_status_command_other) {
return memcmp(af_status_command_me, af_status_command_other, af_status_command_get_size(af_status_command_me)) == 0;
}
bool af_status_command_is_valid(af_status_command_t *af_status_command) {
return (af_status_command->checksum == af_status_command_get_checksum(af_status_command)) && (AF_STATUS_COMMAND_STATUS == af_status_command->cmd || AF_STATUS_COMMAND_STATUS_ACK == af_status_command->cmd);
}
void af_status_command_dump(af_status_command_t *af_status_command) {
af_logger_print_buffer("cmd : ");
af_logger_println_buffer(AF_STATUS_COMMAND_STATUS == af_status_command->cmd ? "STATUS" : "STATUS_ACK");
af_logger_print_buffer("bytes to send : ");
af_logger_println_formatted_value(af_status_command->bytes_to_send, AF_LOGGER_DEC);
af_logger_print_buffer("bytes to receive : ");
af_logger_println_formatted_value(af_status_command->bytes_to_recv, AF_LOGGER_DEC);
}
void af_status_command_dump_bytes(af_status_command_t *af_status_command) {
int len = af_status_command_get_size(af_status_command);
uint8_t *bytes;
int i = 0;
int b;
bytes = (uint8_t *)malloc(len);
if (bytes == NULL) {
af_logger_print_buffer("malloc failed!");
return;
}
af_status_command_get_bytes(af_status_command, bytes);
af_logger_print_buffer("len : ");
af_logger_println_value(len);
af_logger_print_buffer("data : ");
for (i = 0; i < len; i++) {
if (i > 0) {
af_logger_print_buffer(", ");
}
b = bytes[i] & 0xff;
if (b < 0x10) {
af_logger_print_buffer("0x0");
af_logger_print_formatted_value(b, AF_LOGGER_HEX);
} else {
af_logger_print_buffer("0x");
af_logger_print_formatted_value(b, AF_LOGGER_HEX);
}
}
af_logger_println_buffer("");
free(bytes);
}