Bake in the board information at build time (#12089)
[betaflight.git] / src / main / fc / board_info.c
blob729b93d84a761e7a1d46d9292e4e50c5edc6971f
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <string.h>
24 #include "platform.h"
26 #if defined(USE_BOARD_INFO)
27 #include "pg/board.h"
29 #if !defined(BOARD_NAME)
30 static bool boardInformationSet = false;
31 static char manufacturerId[MAX_MANUFACTURER_ID_LENGTH + 1];
32 static char boardName[MAX_BOARD_NAME_LENGTH + 1];
33 static bool boardInformationWasUpdated = false;
34 #endif
36 static bool signatureSet = false;
37 static uint8_t signature[SIGNATURE_LENGTH];
39 void initBoardInformation(void)
41 #if !defined(BOARD_NAME)
42 boardInformationSet = boardConfig()->boardInformationSet;
43 if (boardInformationSet) {
44 strncpy(manufacturerId, boardConfig()->manufacturerId, MAX_MANUFACTURER_ID_LENGTH + 1);
45 strncpy(boardName, boardConfig()->boardName, MAX_BOARD_NAME_LENGTH + 1);
47 #endif
49 signatureSet = boardConfig()->signatureSet;
50 if (signatureSet) {
51 memcpy(signature, boardConfig()->signature, SIGNATURE_LENGTH);
55 const char *getManufacturerId(void)
57 #if defined(MANUFACTURER_ID) && defined(BOARD_NAME)
58 return STR(MANUFACTURER_ID);
59 #elif defined(BOARD_NAME)
60 return "----";
61 #else
62 return manufacturerId;
63 #endif
66 const char *getBoardName(void)
68 #if defined(BOARD_NAME)
69 return STR(BOARD_NAME);
70 #else
71 return boardName;
72 #endif
75 bool boardInformationIsSet(void)
77 #if defined(BOARD_NAME)
78 return true;
79 #else
80 return boardInformationSet;
81 #endif
84 bool setManufacturerId(const char *newManufacturerId)
86 #if !defined(BOARD_NAME)
87 if (!boardInformationSet || strlen(manufacturerId) == 0) {
88 strncpy(manufacturerId, newManufacturerId, MAX_MANUFACTURER_ID_LENGTH + 1);
90 boardInformationWasUpdated = true;
92 return true;
93 } else {
94 return false;
96 #else
97 UNUSED(newManufacturerId);
98 return false;
99 #endif
102 bool setBoardName(const char *newBoardName)
104 #if !defined(BOARD_NAME)
105 if (!boardInformationSet || strlen(boardName) == 0) {
106 strncpy(boardName, newBoardName, MAX_BOARD_NAME_LENGTH + 1);
108 boardInformationWasUpdated = true;
110 return true;
111 } else {
112 return false;
114 #else
115 UNUSED(newBoardName);
116 return false;
117 #endif
120 bool persistBoardInformation(void)
122 #if !defined(BOARD_NAME)
123 if (boardInformationWasUpdated) {
124 strncpy(boardConfigMutable()->manufacturerId, manufacturerId, MAX_MANUFACTURER_ID_LENGTH + 1);
125 strncpy(boardConfigMutable()->boardName, boardName, MAX_BOARD_NAME_LENGTH + 1);
126 boardConfigMutable()->boardInformationSet = true;
128 initBoardInformation();
130 return true;
131 } else {
132 return false;
134 #else
135 return false;
136 #endif
139 #if defined(USE_SIGNATURE)
140 const uint8_t *getSignature(void)
142 return signature;
145 bool signatureIsSet(void)
147 return signatureSet;
150 bool setSignature(const uint8_t *newSignature)
152 if (!signatureSet) {
153 memcpy(signature, newSignature, SIGNATURE_LENGTH);
155 return true;
156 } else {
157 return false;
161 bool persistSignature(void)
163 if (!signatureSet) {
164 memcpy(boardConfigMutable()->signature, signature, SIGNATURE_LENGTH);
165 boardConfigMutable()->signatureSet = true;
167 initBoardInformation();
169 return true;
170 } else {
171 return false;
174 #endif
175 #endif // USE_BOARD_INFO