parameters.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include "options.h"
  2. #include <Arduino.h>
  3. #include "parameters.h"
  4. #include <nvs_flash.h>
  5. #include <string.h>
  6. #include "romfs.h"
  7. #include "util.h"
  8. Parameters g;
  9. static nvs_handle handle;
  10. const Parameters::Param Parameters::params[] = {
  11. { "LOCK_LEVEL", Parameters::ParamType::UINT8, (const void*)&g.lock_level, 0, 0, 2 },
  12. { "CAN_NODE", Parameters::ParamType::UINT8, (const void*)&g.can_node, 0, 0, 127 },
  13. { "UAS_TYPE", Parameters::ParamType::UINT8, (const void*)&g.ua_type, 0, 0, 15 },
  14. { "UAS_ID_TYPE", Parameters::ParamType::UINT8, (const void*)&g.id_type, 0, 0, 4 },
  15. { "UAS_ID", Parameters::ParamType::CHAR20, (const void*)&g.uas_id[0], 0, 0, 0 },
  16. { "UAS_TYPE_2", Parameters::ParamType::UINT8, (const void*)&g.ua_type_2, 0, 0, 15 },
  17. { "UAS_ID_TYPE_2", Parameters::ParamType::UINT8, (const void*)&g.id_type_2, 0, 0, 4 },
  18. { "UAS_ID_2", Parameters::ParamType::CHAR20, (const void*)&g.uas_id_2[0], 0, 0, 0 },
  19. { "BAUDRATE", Parameters::ParamType::UINT32, (const void*)&g.baudrate, 57600, 9600, 921600 },
  20. { "WIFI_NAN_RATE", Parameters::ParamType::FLOAT, (const void*)&g.wifi_nan_rate, 0, 0, 5 },
  21. { "WIFI_BCN_RATE", Parameters::ParamType::FLOAT, (const void*)&g.wifi_beacon_rate, 0, 0, 5 },
  22. { "WIFI_POWER", Parameters::ParamType::FLOAT, (const void*)&g.wifi_power, 20, 2, 20 },
  23. { "BT4_RATE", Parameters::ParamType::FLOAT, (const void*)&g.bt4_rate, 1, 0, 5 },
  24. { "BT4_POWER", Parameters::ParamType::FLOAT, (const void*)&g.bt4_power, 18, -27, 18 },
  25. { "BT5_RATE", Parameters::ParamType::FLOAT, (const void*)&g.bt5_rate, 1, 0, 5 },
  26. { "BT5_POWER", Parameters::ParamType::FLOAT, (const void*)&g.bt5_power, 18, -27, 18 },
  27. { "WEBSERVER_EN", Parameters::ParamType::UINT8, (const void*)&g.webserver_enable, 1, 0, 1 },
  28. { "WIFI_SSID", Parameters::ParamType::CHAR20, (const void*)&g.wifi_ssid, },
  29. { "WIFI_PASSWORD", Parameters::ParamType::CHAR20, (const void*)&g.wifi_password, 0, 0, 0, PARAM_FLAG_PASSWORD, 8 },
  30. { "WIFI_CHANNEL", Parameters::ParamType::UINT8, (const void*)&g.wifi_channel, 6, 1, 13 },
  31. { "BCAST_POWERUP", Parameters::ParamType::UINT8, (const void*)&g.bcast_powerup, 1, 0, 1 },
  32. { "PUBLIC_KEY1", Parameters::ParamType::CHAR64, (const void*)&g.public_keys[0], },
  33. { "PUBLIC_KEY2", Parameters::ParamType::CHAR64, (const void*)&g.public_keys[1], },
  34. { "PUBLIC_KEY3", Parameters::ParamType::CHAR64, (const void*)&g.public_keys[2], },
  35. { "PUBLIC_KEY4", Parameters::ParamType::CHAR64, (const void*)&g.public_keys[3], },
  36. { "PUBLIC_KEY5", Parameters::ParamType::CHAR64, (const void*)&g.public_keys[4], },
  37. { "MAVLINK_SYSID", Parameters::ParamType::UINT8, (const void*)&g.mavlink_sysid, 0, 0, 254 },
  38. { "DONE_INIT", Parameters::ParamType::UINT8, (const void*)&g.done_init, 0, 0, 0, PARAM_FLAG_HIDDEN},
  39. { "", Parameters::ParamType::NONE, nullptr, },
  40. };
  41. /*
  42. get count of parameters capable of being converted to load
  43. */
  44. uint16_t Parameters::param_count_float(void)
  45. {
  46. uint16_t count = 0;
  47. for (const auto &p : params) {
  48. if (p.flags & PARAM_FLAG_HIDDEN) {
  49. continue;
  50. }
  51. switch (p.ptype) {
  52. case ParamType::UINT8:
  53. case ParamType::UINT32:
  54. case ParamType::FLOAT:
  55. count++;
  56. break;
  57. }
  58. }
  59. // remove 1 for DONE_INIT
  60. return count-1;
  61. }
  62. /*
  63. get index of parameter counting only those capable of representation as float
  64. */
  65. int16_t Parameters::param_index_float(const Parameters::Param *f)
  66. {
  67. uint16_t count = 0;
  68. for (const auto &p : params) {
  69. if (p.flags & PARAM_FLAG_HIDDEN) {
  70. continue;
  71. }
  72. switch (p.ptype) {
  73. case ParamType::UINT8:
  74. case ParamType::UINT32:
  75. case ParamType::FLOAT:
  76. if (&p == f) {
  77. return count;
  78. }
  79. count++;
  80. break;
  81. }
  82. }
  83. return -1;
  84. }
  85. /*
  86. find by name
  87. */
  88. const Parameters::Param *Parameters::find(const char *name)
  89. {
  90. for (const auto &p : params) {
  91. if (strcmp(name, p.name) == 0) {
  92. return &p;
  93. }
  94. }
  95. return nullptr;
  96. }
  97. /*
  98. find by index
  99. */
  100. const Parameters::Param *Parameters::find_by_index(uint16_t index)
  101. {
  102. if (index >= ARRAY_SIZE(params)-2) {
  103. return nullptr;
  104. }
  105. return &params[index];
  106. }
  107. /*
  108. find by index
  109. */
  110. const Parameters::Param *Parameters::find_by_index_float(uint16_t index)
  111. {
  112. uint16_t count = 0;
  113. for (const auto &p : params) {
  114. if (p.flags & PARAM_FLAG_HIDDEN) {
  115. continue;
  116. }
  117. switch (p.ptype) {
  118. case ParamType::UINT8:
  119. case ParamType::UINT32:
  120. case ParamType::FLOAT:
  121. if (index == count) {
  122. return &p;
  123. }
  124. count++;
  125. break;
  126. }
  127. }
  128. return nullptr;
  129. }
  130. void Parameters::Param::set_uint8(uint8_t v) const
  131. {
  132. auto *p = (uint8_t *)ptr;
  133. *p = v;
  134. nvs_set_u8(handle, name, *p);
  135. }
  136. void Parameters::Param::set_uint32(uint32_t v) const
  137. {
  138. auto *p = (uint32_t *)ptr;
  139. *p = v;
  140. nvs_set_u32(handle, name, *p);
  141. }
  142. void Parameters::Param::set_float(float v) const
  143. {
  144. auto *p = (float *)ptr;
  145. *p = v;
  146. union {
  147. float f;
  148. uint32_t u32;
  149. } u;
  150. u.f = v;
  151. nvs_set_u32(handle, name, u.u32);
  152. }
  153. void Parameters::Param::set_char20(const char *v) const
  154. {
  155. if (min_len > 0 && strlen(v) < min_len) {
  156. return;
  157. }
  158. memset((void*)ptr, 0, 21);
  159. strncpy((char *)ptr, v, 20);
  160. nvs_set_str(handle, name, v);
  161. }
  162. void Parameters::Param::set_char64(const char *v) const
  163. {
  164. if (min_len > 0 && strlen(v) < min_len) {
  165. return;
  166. }
  167. memset((void*)ptr, 0, 65);
  168. strncpy((char *)ptr, v, 64);
  169. nvs_set_str(handle, name, v);
  170. }
  171. uint8_t Parameters::Param::get_uint8() const
  172. {
  173. const auto *p = (const uint8_t *)ptr;
  174. return *p;
  175. }
  176. uint32_t Parameters::Param::get_uint32() const
  177. {
  178. const auto *p = (const uint32_t *)ptr;
  179. return *p;
  180. }
  181. float Parameters::Param::get_float() const
  182. {
  183. const auto *p = (const float *)ptr;
  184. return *p;
  185. }
  186. const char *Parameters::Param::get_char20() const
  187. {
  188. const char *p = (const char *)ptr;
  189. return p;
  190. }
  191. const char *Parameters::Param::get_char64() const
  192. {
  193. const char *p = (const char *)ptr;
  194. return p;
  195. }
  196. /*
  197. get parameter as a float
  198. */
  199. bool Parameters::Param::get_as_float(float &v) const
  200. {
  201. switch (ptype) {
  202. case ParamType::UINT8:
  203. v = float(get_uint8());
  204. break;
  205. case ParamType::UINT32:
  206. v = float(get_uint32());
  207. break;
  208. case ParamType::FLOAT:
  209. v = get_float();
  210. break;
  211. default:
  212. return false;
  213. }
  214. return true;
  215. }
  216. /*
  217. set parameter from a float
  218. */
  219. void Parameters::Param::set_as_float(float v) const
  220. {
  221. switch (ptype) {
  222. case ParamType::UINT8:
  223. set_uint8(uint8_t(v));
  224. break;
  225. case ParamType::UINT32:
  226. set_uint32(uint32_t(v));
  227. break;
  228. case ParamType::FLOAT:
  229. set_float(v);
  230. break;
  231. }
  232. }
  233. /*
  234. load defaults from parameter table
  235. */
  236. void Parameters::load_defaults(void)
  237. {
  238. for (const auto &p : params) {
  239. switch (p.ptype) {
  240. case ParamType::UINT8:
  241. *(uint8_t *)p.ptr = uint8_t(p.default_value);
  242. break;
  243. case ParamType::UINT32:
  244. *(uint32_t *)p.ptr = uint32_t(p.default_value);
  245. break;
  246. case ParamType::FLOAT:
  247. *(float *)p.ptr = p.default_value;
  248. break;
  249. }
  250. }
  251. }
  252. void Parameters::init(void)
  253. {
  254. load_defaults();
  255. if (nvs_flash_init() != ESP_OK ||
  256. nvs_open("storage", NVS_READWRITE, &handle) != ESP_OK) {
  257. Serial.printf("NVS init failed\n");
  258. }
  259. // load values from NVS
  260. for (const auto &p : params) {
  261. switch (p.ptype) {
  262. case ParamType::UINT8:
  263. nvs_get_u8(handle, p.name, (uint8_t *)p.ptr);
  264. break;
  265. case ParamType::UINT32:
  266. nvs_get_u32(handle, p.name, (uint32_t *)p.ptr);
  267. break;
  268. case ParamType::FLOAT:
  269. nvs_get_u32(handle, p.name, (uint32_t *)p.ptr);
  270. break;
  271. case ParamType::CHAR20: {
  272. size_t len = 21;
  273. nvs_get_str(handle, p.name, (char *)p.ptr, &len);
  274. break;
  275. }
  276. case ParamType::CHAR64: {
  277. size_t len = 65;
  278. nvs_get_str(handle, p.name, (char *)p.ptr, &len);
  279. break;
  280. }
  281. }
  282. }
  283. if (strlen(g.wifi_ssid) == 0) {
  284. uint8_t mac[6] {};
  285. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  286. snprintf(wifi_ssid, 20, "RID_%02x%02x%02x%02x%02x%02x",
  287. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  288. }
  289. if (g.done_init == 0) {
  290. set_by_name_uint8("DONE_INIT", 1);
  291. // setup public keys
  292. set_by_name_char64("PUBLIC_KEY1", ROMFS::find_string("public_keys/ArduPilot_public_key1.dat"));
  293. set_by_name_char64("PUBLIC_KEY2", ROMFS::find_string("public_keys/ArduPilot_public_key2.dat"));
  294. #if defined(BOARD_BLUEMARK_DB200) || defined(BOARD_BLUEMARK_DB110)
  295. set_by_name_char64("PUBLIC_KEY3", ROMFS::find_string("public_keys/BlueMark_public_key1.dat"));
  296. #else
  297. set_by_name_char64("PUBLIC_KEY3", ROMFS::find_string("public_keys/ArduPilot_public_key3.dat"));
  298. #endif
  299. }
  300. }
  301. /*
  302. check if BasicID info is filled in with parameters
  303. */
  304. bool Parameters::have_basic_id_info(void) const
  305. {
  306. return strlen(g.uas_id) > 0 && g.id_type > 0 && g.ua_type > 0;
  307. }
  308. bool Parameters::have_basic_id_2_info(void) const
  309. {
  310. return strlen(g.uas_id_2) > 0 && g.id_type_2 > 0 && g.ua_type_2 > 0;
  311. }
  312. bool Parameters::set_by_name_uint8(const char *name, uint8_t v)
  313. {
  314. const auto *f = find(name);
  315. if (!f) {
  316. return false;
  317. }
  318. f->set_uint8(v);
  319. return true;
  320. }
  321. bool Parameters::set_by_name_char64(const char *name, const char *s)
  322. {
  323. const auto *f = find(name);
  324. if (!f) {
  325. return false;
  326. }
  327. f->set_char64(s);
  328. return true;
  329. }
  330. bool Parameters::set_by_name_string(const char *name, const char *s)
  331. {
  332. const auto *f = find(name);
  333. if (!f) {
  334. return false;
  335. }
  336. switch (f->ptype) {
  337. case ParamType::UINT8:
  338. f->set_uint8(uint8_t(strtoul(s, nullptr, 0)));
  339. return true;
  340. case ParamType::UINT32:
  341. f->set_uint32(strtoul(s, nullptr, 0));
  342. return true;
  343. case ParamType::FLOAT:
  344. f->set_float(atof(s));
  345. return true;
  346. case ParamType::CHAR20:
  347. f->set_char20(s);
  348. return true;
  349. case ParamType::CHAR64:
  350. f->set_char64(s);
  351. return true;
  352. }
  353. return false;
  354. }
  355. /*
  356. return a public key
  357. */
  358. bool Parameters::get_public_key(uint8_t i, uint8_t key[32]) const
  359. {
  360. if (i >= MAX_PUBLIC_KEYS) {
  361. return false;
  362. }
  363. const char *b64_key = g.public_keys[i].b64_key;
  364. const char *ktype = "PUBLIC_KEYV1:";
  365. if (strncmp(b64_key, ktype, strlen(ktype)) != 0) {
  366. return false;
  367. }
  368. b64_key += strlen(ktype);
  369. int32_t out_len = base64_decode(b64_key, key, 32);
  370. if (out_len != 32) {
  371. return false;
  372. }
  373. return true;
  374. }
  375. bool Parameters::no_public_keys(void) const
  376. {
  377. for (uint8_t i=0; i<MAX_PUBLIC_KEYS; i++) {
  378. uint8_t key[32];
  379. if (get_public_key(i, key)) {
  380. return false;
  381. }
  382. }
  383. return true;
  384. }
  385. bool Parameters::set_public_key(uint8_t i, const uint8_t key[32])
  386. {
  387. if (i >= MAX_PUBLIC_KEYS) {
  388. return false;
  389. }
  390. char *s = base64_encode(key, PUBLIC_KEY_LEN);
  391. if (s == nullptr) {
  392. return false;
  393. }
  394. char name[] = "PUBLIC_KEYx";
  395. name[strlen(name)-2] = '1'+i;
  396. bool ret = set_by_name_char64(name, s);
  397. delete[] s;
  398. return ret;
  399. }
  400. bool Parameters::remove_public_key(uint8_t i)
  401. {
  402. if (i >= MAX_PUBLIC_KEYS) {
  403. return false;
  404. }
  405. char name[] = "PUBLIC_KEYx";
  406. name[strlen(name)-2] = '1'+i;
  407. return set_by_name_char64(name, "");
  408. }