Fix problem with empty string set as 'manufacturer_id'.
[betaflight.git] / src / main / fc / board_info.c
blobd484d45846617fcbcf51def6f9775e1650a0cedd
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 static bool boardInformationSet = false;
30 static char manufacturerId[MAX_MANUFACTURER_ID_LENGTH + 1];
31 static char boardName[MAX_BOARD_NAME_LENGTH + 1];
32 static bool boardInformationWasUpdated = false;
34 static bool signatureSet = false;
35 static uint8_t signature[SIGNATURE_LENGTH];
37 void initBoardInformation(void)
39 boardInformationSet = boardConfig()->boardInformationSet;
40 if (boardInformationSet) {
41 strncpy(manufacturerId, boardConfig()->manufacturerId, MAX_MANUFACTURER_ID_LENGTH);
42 strncpy(boardName, boardConfig()->boardName, MAX_BOARD_NAME_LENGTH);
45 signatureSet = boardConfig()->signatureSet;
46 if (signatureSet) {
47 memcpy(signature, boardConfig()->signature, SIGNATURE_LENGTH);
51 const char *getManufacturerId(void)
53 return manufacturerId;
56 const char *getBoardName(void)
58 return boardName;
61 bool boardInformationIsSet(void)
63 return boardInformationSet;
66 bool setManufacturerId(const char *newManufacturerId)
68 if (!boardInformationSet || strlen(manufacturerId) == 0) {
69 strncpy(manufacturerId, newManufacturerId, MAX_MANUFACTURER_ID_LENGTH);
71 boardInformationWasUpdated = true;
73 return true;
74 } else {
75 return false;
79 bool setBoardName(const char *newBoardName)
81 if (!boardInformationSet || strlen(boardName) == 0) {
82 strncpy(boardName, newBoardName, MAX_BOARD_NAME_LENGTH);
84 boardInformationWasUpdated = true;
86 return true;
87 } else {
88 return false;
92 bool persistBoardInformation(void)
94 if (boardInformationWasUpdated) {
95 strncpy(boardConfigMutable()->manufacturerId, manufacturerId, MAX_MANUFACTURER_ID_LENGTH);
96 strncpy(boardConfigMutable()->boardName, boardName, MAX_BOARD_NAME_LENGTH);
97 boardConfigMutable()->boardInformationSet = true;
99 initBoardInformation();
101 return true;
102 } else {
103 return false;
107 #if defined(USE_SIGNATURE)
108 const uint8_t *getSignature(void)
110 return signature;
113 bool signatureIsSet(void)
115 return signatureSet;
118 bool setSignature(const uint8_t *newSignature)
120 if (!signatureSet) {
121 memcpy(signature, newSignature, SIGNATURE_LENGTH);
123 return true;
124 } else {
125 return false;
129 bool persistSignature(void)
131 if (!signatureSet) {
132 memcpy(boardConfigMutable()->signature, signature, SIGNATURE_LENGTH);
133 boardConfigMutable()->signatureSet = true;
135 initBoardInformation();
137 return true;
138 } else {
139 return false;
142 #endif
143 #endif // USE_BOARD_INFO