e9e26c8d6e5d29e0fed5e8db8d8f94f2e7eabeec
[revag-nm.git] / vw-nm-tools.h
1 static char* nm_main_to_string(NM_State state)
2 {
3         switch(state & NM_MAIN_MASK) {
4                 case NM_MAIN_OFF:
5                         return "Off";
6                 case NM_MAIN_ON:
7                         return "Ready";
8                 case NM_MAIN_LOGIN:
9                         return "Login";
10                 case NM_MAIN_LIMPHOME:
11                         return "Limp home";
12                 default:
13                         return "Unknown?";
14         }
15 }
16
17 static char* nm_sleep_to_string(NM_State state)
18 {
19         switch(state & NM_SLEEP_MASK) {
20                 case NM_SLEEP_CANCEL:
21                         return "No";
22                 case NM_SLEEP_REQUEST:
23                         return "Request";
24                 case NM_SLEEP_ACK:
25                         return "Acknowledged";
26                 default:
27                         return "Unknown?";
28         }
29 }
30
31
32
33 static void nm_dump_all(struct NM_Main *nm)
34 {
35         unsigned id;
36
37         printf("\n");
38         printf(" Node | next | Main      | Sleep\n");
39         printf("----------------------------------------\n");
40
41         for (id = 0; id < nm->max_nodes; id++) {
42                 struct NM_Node *node = &nm->nodes[id];
43
44                 if (node->state & NM_MAIN_MASK) {
45                         printf("  %02x     %02x    %9s   %s\n",
46                                 id,
47                                 node->next,
48                                 nm_main_to_string(node->state),
49                                 nm_sleep_to_string(node->state));
50
51                 }
52         }
53
54         printf("\n");
55 }
56
57
58
59
60
61
62 static void can_tx(int socket, struct can_frame *frame)
63 {
64         ssize_t ret;
65
66         ret = write(socket, frame, sizeof(*frame));
67         if (ret != sizeof(*frame)) {
68                 perror("write to CAN socket");
69                 exit(1);
70         }
71 }
72
73
74
75
76
77
78
79
80 static struct NM_Main* nm_alloc(unsigned node_bits, NM_ID my_id, canid_t can_base)
81 {
82         struct NM_Main *nm;
83
84         if (node_bits < 1 || node_bits > 6) {
85                 return NULL;
86         }
87
88         nm = malloc(sizeof(*nm));
89         if (!nm) {
90                 return NULL;
91         }
92
93         nm->max_nodes = 1 << node_bits;
94         nm->nodes = malloc(nm->max_nodes * sizeof(*nm->nodes));
95         if (!nm->nodes) {
96                 free(nm);
97                 return NULL;
98         }
99
100         nm->my_id = my_id;
101         nm->can_base = can_base;
102
103         nm->nodes[nm->my_id].next = nm->my_id;
104         nm->nodes[nm->my_id].state = NM_MAIN_LOGIN;
105
106         nm->tv.tv_sec = 0;
107         nm->tv.tv_usec = NM_USECS_OTHER_TURN;
108
109         return nm;
110 }
111
112
113
114
115 static void nm_free(struct NM_Main *nm)
116 {
117         free(nm->nodes);
118         free(nm);
119 }