flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
clock_utils.c
Go to the documentation of this file.
1#include "clock_utils.h"
2
3#include <stdint.h>
4#include <ctype.h>
5#include <string.h>
6
7#include "ptp_core.h"
8
9#include <flexptp_options.h>
10
12#define S (gPtpCoreState)
14
15// print clock identity
16void ptp_print_clock_identity(uint64_t clockID) {
17 uint8_t *p = (uint8_t*) &clockID;
18 uint8_t i;
19 for (i = 0; i < 8; i++) { // reverse byte order due to Network->Host byte order conversion
20 MSG("%02x", p[7 - i]);
21 }
22}
23
24// create clock identity based on MAC address
25void ptp_create_clock_identity(const uint8_t * hwa) {
26 uint8_t *p = (uint8_t*) &S.hwoptions.clockIdentity;
27 // construct clockIdentity
28 memcpy(p, hwa, 3); // first 3 octets of MAC address
29 p[3] = 0xff;
30 p[4] = 0xfe;
31 memcpy(&p[5], &hwa[3], 3); // last 3 octets of MAC address
32
33 // display ID
34 MSG("Own clock ID: ");
35 ptp_print_clock_identity(S.hwoptions.clockIdentity);
36 MSG("\n");
37}
38
39// convert string clock id to 64-bit number
40uint64_t hextoclkid(const char *str) {
41 size_t len = strlen(str);
42 uint64_t clkid = 0;
43 for (size_t i = 0; i < len; i++) {
44 char digit = tolower(str[i]);
45 if (digit >= '0' && digit <= '9') {
46 digit = digit - '0';
47 } else if (digit >= 'a' && digit <= 'f') {
48 digit = digit - 'a' + 10;
49 } else {
50 break;
51 }
52
53 clkid += (uint64_t) digit * ((uint64_t) 1 << (4 * i));
54 }
55 uint8_t *p = NULL;
56 for (size_t i = 0; i < 8; i++) {
57 p = ((uint8_t*) &clkid) + i;
58 *p = ((*p & 0x0F) << 4) | ((*p & 0xF0) >> 4);
59 }
60
61 return clkid;
62}
void ptp_create_clock_identity(const uint8_t *hwa)
Definition: clock_utils.c:25
void ptp_print_clock_identity(uint64_t clockID)
Definition: clock_utils.c:16
uint64_t hextoclkid(const char *str)
Definition: clock_utils.c:40
This module defines clock identity related operations.
Core of the PTP implementation. Defines functions for message processing, clock tuning,...