Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
mcu_arch.c
Go to the documentation of this file.
1/*
2 *
3 * Copyright (C) 2009-2013 The Paparazzi Team
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
29
30#include "mcu_arch.h"
31#include "mcu.h"
32
33#include <stdio.h>
34#include <string.h>
35#include <dirent.h>
36
38#ifndef LINUX_UID_FILE
39#define LINUX_UID_FILE ""
40#endif
41
45static uint8_t uid_from_file(const char *path, uint8_t *uid, uint8_t max_len)
46{
47 char line[64];
48 if (path[0] == '\0') {
49 return 0;
50 }
51 FILE *f = fopen(path, "r");
52 if (f == NULL) {
53 return 0;
54 }
55 uint8_t len = 0;
56 if (fgets(line, sizeof(line), f) != NULL) {
57 line[strcspn(line, "\r\n")] = '\0';
58 len = Min(strlen(line), max_len);
59 memcpy(uid, line, len);
60 }
61 fclose(f);
62 return len;
63}
64
68static uint8_t uid_from_mac(uint8_t *uid, uint8_t max_len)
69{
70 DIR *dir = opendir("/sys/class/net");
71 if (dir == NULL) {
72 return 0;
73 }
74 char best_name[64] = "";
75 uint8_t best_mac[6];
76 struct dirent *ent;
77 while ((ent = readdir(dir)) != NULL) {
78 char path[300];
79 unsigned int m[6];
80 if (ent->d_name[0] == '.' || strcmp(ent->d_name, "lo") == 0 ||
81 strlen(ent->d_name) >= sizeof(best_name) ||
82 (best_name[0] != '\0' && strcmp(ent->d_name, best_name) >= 0)) {
83 continue;
84 }
85 snprintf(path, sizeof(path), "/sys/class/net/%s/address", ent->d_name);
86 FILE *f = fopen(path, "r");
87 if (f == NULL) {
88 continue;
89 }
90 int n = fscanf(f, "%x:%x:%x:%x:%x:%x", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]);
91 fclose(f);
92 if (n != 6 || (m[0] | m[1] | m[2] | m[3] | m[4] | m[5]) == 0) {
93 continue;
94 }
95 strcpy(best_name, ent->d_name);
96 for (int i = 0; i < 6; i++) {
97 best_mac[i] = m[i];
98 }
99 }
100 closedir(dir);
101 if (best_name[0] == '\0') {
102 return 0;
103 }
104 uint8_t len = Min(6, max_len);
105 memcpy(uid, best_mac, len);
106 return len;
107}
108
115{
116 uint8_t len = uid_from_file(LINUX_UID_FILE, uid, max_len);
117 if (len == 0) {
118 len = uid_from_mac(uid, max_len);
119 }
120 return len;
121}
122
123#if USE_LINUX_SIGNAL
124#include "message_pragmas.h"
125PRINT_CONFIG_MSG("Catching SIGINT. Press CTRL-C twice to stop program.")
126
127/*
128 * Handle linux signals by hand if the program is not launch
129 * by a shell already doing it
130 */
131
132#include <stdlib.h>
133#include <stdio.h>
134#include <signal.h>
135
139static void sig_handler(int signo)
140{
141 static int nb_signal = 0;
142
143 if (signo == SIGINT) {
144 printf("Received SIGINT\n");
145 if (nb_signal == 0) {
146 printf("Press Ctrl-C again to stop the program\n");
147 nb_signal++;
148 } else {
149 printf("Leaving now\n");
150 exit(0);
151 }
152 }
153}
154
155void mcu_arch_init(void)
156{
157 struct sigaction sa;
158 sigemptyset(&sa.sa_mask);
159 sa.sa_flags = 0;
160 sa.sa_handler = &sig_handler;
161 if (sigaction(SIGINT, &sa, NULL) == -1) {
162 printf("Can't catch SIGINT\n");
163 }
164}
165
166#else
167
168void mcu_arch_init(void) { }
169
170#endif
171
void mcu_arch_init(void)
Initialize the specific archittecture functions.
Definition mcu_arch.c:157
#define Min(x, y)
Definition esc_dshot.c:110
uint8_t mcu_get_uid(uint8_t *uid, uint8_t max_len)
Get the 96-bit unique device ID of the STM32.
Definition mcu_arch.c:252
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define LINUX_UID_FILE
Optional file with a unique board serial number (read as text).
Definition mcu_arch.c:39
static uint8_t uid_from_file(const char *path, uint8_t *uid, uint8_t max_len)
Read the first line of a text file as unique ID.
Definition mcu_arch.c:45
static uint8_t uid_from_mac(uint8_t *uid, uint8_t max_len)
Get the MAC address of the first network interface (by name), skipping loopback.
Definition mcu_arch.c:68
linux arch dependant microcontroller initialisation functions.
Arch independent mcu ( Micro Controller Unit ) utilities.
static const float dir[]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup).
Definition wedgebug.c:204