-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfalhas.c
More file actions
60 lines (47 loc) · 1.88 KB
/
falhas.c
File metadata and controls
60 lines (47 loc) · 1.88 KB
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
#include "serial.h"
#include <string.h>
#include <stdio.h>
#include <stdint.h>
static uint16_t transaction_id = 0;
void enviarPacoteInvalido(const uint8_t *pdu, uint8_t pdu_len, const char *descricao) {
uint8_t req[260] = {0};
req[0] = (transaction_id >> 8) & 0xFF; // Transaction ID
req[1] = transaction_id & 0xFF;
req[2] = 0x00; // Protocol ID
req[3] = 0x00;
req[4] = 0x00; // Length (high)
req[5] = pdu_len + 1; // Length = Unit ID + PDU
req[6] = 0x01; // Unit ID válido (exceto no teste de endereço)
memcpy(&req[7], pdu, pdu_len);
printf("🚫 %s\n", descricao);
modbusWrite((const char *)req, 7 + pdu_len);
transaction_id = (transaction_id + 1) % 0x10000;
}
void enviarFuncaoInvalida(void) {
uint8_t pdu[] = {0x09, 0x00, 0x00, 0x00, 0x01}; // função 0x09 inexistente
enviarPacoteInvalido(pdu, sizeof(pdu), "Funcao inexistente (0x09)");
}
void enviarRegistradorInvalido(void) {
uint8_t pdu[] = {0x0F, 0xFF, 0xF0, 0x00, 0x10, 0x02, 0x00, 0x00}; // Endereço alto
enviarPacoteInvalido(pdu, sizeof(pdu), "Endereco de registrador invalido");
}
void enviarValorInvalido(void) {
uint8_t pdu[] = {0x0F, 0x00, 0x00, 0x00, 0x10, 0x01, 0xFF}; // ByteCount < esperado
enviarPacoteInvalido(pdu, sizeof(pdu), "Valor de dado invalido");
}
void enviarEnderecoInvalido(void) {
uint8_t req[260] = {0};
uint8_t pdu[] = {0x0F, 0x00, 0x00, 0x00, 0x10, 0x02, 0xFF, 0xFF};
// MBAP + Unit ID inválido (0x09, por ex)
req[0] = (transaction_id >> 8) & 0xFF;
req[1] = transaction_id & 0xFF;
req[2] = 0x00;
req[3] = 0x00;
req[4] = 0x00;
req[5] = sizeof(pdu) + 1;
req[6] = 0x09; // escravo inexistente
memcpy(&req[7], pdu, sizeof(pdu));
printf("🚫 Endereco de escravo inexistente (ID 0x09)\n");
modbusWrite((const char *)req, 7 + sizeof(pdu));
transaction_id = (transaction_id + 1) % 0x10000;
}