An access controller for man doors at our facility. It receives Wiegand signals from a keypad/card reader and activates a relay to open the door. Access codes are stored in EEPROM. The active code list is updated from TFTP on a local server.
Dependencies: 24LCxx_I2C CardReader USBHOST
DHCPOptions.h@0:a56239ae90c2, 2017-09-25 (annotated)
- Committer:
- acesrobertm
- Date:
- Mon Sep 25 19:02:40 2017 +0000
- Revision:
- 0:a56239ae90c2
in process of moving networking code to non-blocking format
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acesrobertm | 0:a56239ae90c2 | 1 | |
acesrobertm | 0:a56239ae90c2 | 2 | #ifndef DHCPOPTIONS_H_ |
acesrobertm | 0:a56239ae90c2 | 3 | #define DHCPOPTIONS_H_ |
acesrobertm | 0:a56239ae90c2 | 4 | |
acesrobertm | 0:a56239ae90c2 | 5 | #include <cstdint> |
acesrobertm | 0:a56239ae90c2 | 6 | #include "EthernetInterface.h" |
acesrobertm | 0:a56239ae90c2 | 7 | #include "NetworkStack.h" |
acesrobertm | 0:a56239ae90c2 | 8 | #include "UDPSocket.h" |
acesrobertm | 0:a56239ae90c2 | 9 | #include <inet.h> |
acesrobertm | 0:a56239ae90c2 | 10 | |
acesrobertm | 0:a56239ae90c2 | 11 | using std::uint8_t; |
acesrobertm | 0:a56239ae90c2 | 12 | using std::uint16_t; |
acesrobertm | 0:a56239ae90c2 | 13 | using std::uint32_t; |
acesrobertm | 0:a56239ae90c2 | 14 | |
acesrobertm | 0:a56239ae90c2 | 15 | #define MAC_STRING_LENGTH 17 |
acesrobertm | 0:a56239ae90c2 | 16 | #define DHCP_BROADCAST_ADDRESS "255.255.255.255" |
acesrobertm | 0:a56239ae90c2 | 17 | #define DHCP_SERVER_PORT 67 |
acesrobertm | 0:a56239ae90c2 | 18 | #define DHCP_CLIENT_PORT 68 |
acesrobertm | 0:a56239ae90c2 | 19 | #define DHCP_DEFAULT_TIMEOUT 1000 |
acesrobertm | 0:a56239ae90c2 | 20 | #define DHCP_PACKET_SIZE 576 |
acesrobertm | 0:a56239ae90c2 | 21 | |
acesrobertm | 0:a56239ae90c2 | 22 | #define DHCP_OP_REQUEST 1 // Request op |
acesrobertm | 0:a56239ae90c2 | 23 | #define DHCP_HTYPE 1 // Ethernet |
acesrobertm | 0:a56239ae90c2 | 24 | #define DHCP_HLEN 6 // MAC 6 octet |
acesrobertm | 0:a56239ae90c2 | 25 | #define DHCP_HOPS 0 |
acesrobertm | 0:a56239ae90c2 | 26 | #define DHCP_SECS 0 |
acesrobertm | 0:a56239ae90c2 | 27 | #define DHCP_FLAGS 0 |
acesrobertm | 0:a56239ae90c2 | 28 | #define DHCP_CIADDR 0 |
acesrobertm | 0:a56239ae90c2 | 29 | #define DHCP_YIADDR 0 |
acesrobertm | 0:a56239ae90c2 | 30 | #define DHCP_SIADDR 0 |
acesrobertm | 0:a56239ae90c2 | 31 | #define DHCP_GIADDR 0 |
acesrobertm | 0:a56239ae90c2 | 32 | #define DHCP_MAGIC_COOKIE 0x63825363 // See, it is magic after all! |
acesrobertm | 0:a56239ae90c2 | 33 | #define DHCP_PREQLIST 55 |
acesrobertm | 0:a56239ae90c2 | 34 | #define DHCP_OPTION_TFTPSERVER 66 |
acesrobertm | 0:a56239ae90c2 | 35 | #define DHCP_OPTION_STOP 255 |
acesrobertm | 0:a56239ae90c2 | 36 | |
acesrobertm | 0:a56239ae90c2 | 37 | #define DHCP_CHADDR_SIZE 16 |
acesrobertm | 0:a56239ae90c2 | 38 | #define DHCP_SNAME_SIZE 64 |
acesrobertm | 0:a56239ae90c2 | 39 | #define DHCP_FILE_SIZE 128 |
acesrobertm | 0:a56239ae90c2 | 40 | #define DHCP_OPTIONS_SIZE 312 |
acesrobertm | 0:a56239ae90c2 | 41 | |
acesrobertm | 0:a56239ae90c2 | 42 | class DHCPOptions |
acesrobertm | 0:a56239ae90c2 | 43 | { |
acesrobertm | 0:a56239ae90c2 | 44 | public: |
acesrobertm | 0:a56239ae90c2 | 45 | DHCPOptions(EthernetInterface *stack) : m_eth(stack) {} |
acesrobertm | 0:a56239ae90c2 | 46 | |
acesrobertm | 0:a56239ae90c2 | 47 | /* Get DHCP Options from the server. Blocks until completion. */ |
acesrobertm | 0:a56239ae90c2 | 48 | int getOptions(char* tftp_server, const char* mac_address, const uint32_t timeout = DHCP_DEFAULT_TIMEOUT); //Blocking |
acesrobertm | 0:a56239ae90c2 | 49 | |
acesrobertm | 0:a56239ae90c2 | 50 | private: |
acesrobertm | 0:a56239ae90c2 | 51 | EthernetInterface *m_eth; |
acesrobertm | 0:a56239ae90c2 | 52 | UDPSocket m_sock; |
acesrobertm | 0:a56239ae90c2 | 53 | |
acesrobertm | 0:a56239ae90c2 | 54 | struct DHCPPacket |
acesrobertm | 0:a56239ae90c2 | 55 | { |
acesrobertm | 0:a56239ae90c2 | 56 | uint8_t op; |
acesrobertm | 0:a56239ae90c2 | 57 | uint8_t htype; |
acesrobertm | 0:a56239ae90c2 | 58 | uint8_t hlen; |
acesrobertm | 0:a56239ae90c2 | 59 | uint8_t hops; |
acesrobertm | 0:a56239ae90c2 | 60 | uint32_t xid; |
acesrobertm | 0:a56239ae90c2 | 61 | uint16_t secs; |
acesrobertm | 0:a56239ae90c2 | 62 | uint16_t flags; |
acesrobertm | 0:a56239ae90c2 | 63 | uint32_t ciaddr; |
acesrobertm | 0:a56239ae90c2 | 64 | uint32_t yiaddr; |
acesrobertm | 0:a56239ae90c2 | 65 | uint32_t siaddr; |
acesrobertm | 0:a56239ae90c2 | 66 | uint32_t giaddr; |
acesrobertm | 0:a56239ae90c2 | 67 | char chaddr[DHCP_CHADDR_SIZE]; |
acesrobertm | 0:a56239ae90c2 | 68 | char sname[DHCP_SNAME_SIZE]; |
acesrobertm | 0:a56239ae90c2 | 69 | char file[DHCP_FILE_SIZE]; |
acesrobertm | 0:a56239ae90c2 | 70 | |
acesrobertm | 0:a56239ae90c2 | 71 | //Options |
acesrobertm | 0:a56239ae90c2 | 72 | uint32_t magic; // magic cookie |
acesrobertm | 0:a56239ae90c2 | 73 | uint8_t op_mtype; // message type |
acesrobertm | 0:a56239ae90c2 | 74 | uint8_t mtype_len; |
acesrobertm | 0:a56239ae90c2 | 75 | uint8_t mtype; |
acesrobertm | 0:a56239ae90c2 | 76 | char options[DHCP_OPTIONS_SIZE]; |
acesrobertm | 0:a56239ae90c2 | 77 | |
acesrobertm | 0:a56239ae90c2 | 78 | } __attribute__ ((packed)); |
acesrobertm | 0:a56239ae90c2 | 79 | |
acesrobertm | 0:a56239ae90c2 | 80 | int decodeOptions(char* tftp_server, DHCPPacket& pkt); |
acesrobertm | 0:a56239ae90c2 | 81 | int macStrToBin(char* macBin, const char* macStr); |
acesrobertm | 0:a56239ae90c2 | 82 | unsigned char charToNum(const char input); |
acesrobertm | 0:a56239ae90c2 | 83 | }; |
acesrobertm | 0:a56239ae90c2 | 84 | |
acesrobertm | 0:a56239ae90c2 | 85 | #endif /* DHCPOPTIONS_H_ */ |
acesrobertm | 0:a56239ae90c2 | 86 |