|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | /** |
17 | | - * @brief Extends the shell functionality for uniqueid |
| 17 | + * @brief Extends the shell functionality for storage |
18 | 18 | * @author Luis A. Ruiz <luisan00@hotmail.com> |
19 | 19 | * @author Eduardo Azócar <eduazocarv@gmail.com> |
20 | 20 | */ |
21 | 21 |
|
22 | 22 | #include <stdio.h> |
23 | 23 | #include <string.h> |
24 | 24 | #include "storage.h" |
25 | | - |
26 | | -#if MODULE_RADIO |
| 25 | +#include "storage_register.h" |
| 26 | +#include "default_params.h" |
27 | 27 | #include "radio.h" |
28 | | -#endif |
29 | 28 | #if MODULE_RPL_PROTOCOL |
30 | 29 | #include "rpl_protocol.h" |
31 | 30 | #endif |
| 31 | +int8_t stg_save_if_settings(void); |
| 32 | +int8_t stg_del_if_settings(void); |
| 33 | +int8_t stg_load_if_settings(void); |
| 34 | +void build_if_settings(settings_ifaces_t *if_sets, uint8_t num_ifaces); |
32 | 35 |
|
33 | 36 | void storage_usage(void) { |
34 | 37 | puts("Storage tool"); |
35 | 38 | puts("Usage: stg [save|load|show]"); |
| 39 | + puts("\t\t\t->[iface]"); |
| 40 | + printf("%" PRIu16 " Bytes. \n", sizeof(settings_ifaces_t)); |
36 | 41 | puts(""); |
37 | 42 | } |
38 | 43 |
|
39 | 44 | int storage_cmd(int argc, char **argv) { |
40 | 45 | (void)argc; |
41 | 46 | (void)argv; |
42 | | - |
43 | 47 | if ((argc < 2) || (argc > 2) || (strcmp(argv[1], "help") == 0)) { |
44 | | - void storage_usage(void); |
| 48 | + storage_usage(); |
45 | 49 | return 0; |
46 | 50 | } |
47 | | - |
48 | 51 | if (strcmp(argv[1], "save") == 0) { |
49 | | - puts("ToDo: Save all firmware params in mtd storage"); |
| 52 | + stg_save_if_settings(); |
50 | 53 | } else if (strcmp(argv[1], "load") == 0) { |
51 | | - puts("Todo: Load firmware params from mtd storage and are processed"); |
| 54 | + stg_load_if_settings(); |
52 | 55 | } else if (strcmp(argv[1], "show") == 0) { |
53 | 56 | puts("Todo: show all firmware params saved in mtd storage"); |
| 57 | + } else if (strcmp(argv[1], "del") == 0) { |
| 58 | + stg_del_if_settings(); |
54 | 59 | } |
55 | | - |
56 | 60 | return 0; |
57 | 61 | } |
| 62 | + |
| 63 | +int8_t stg_save_if_settings(void) { |
| 64 | + int8_t ret = 0; |
| 65 | + uint8_t ifaces = gnrc_netif_numof(); |
| 66 | + settings_ifaces_t settings_if[ifaces]; |
| 67 | + build_if_settings(settings_if, ifaces); |
| 68 | + mtd_reg_del(IF_KEY, sizeof(settings_if)); |
| 69 | + ret = mtd_save_reg(settings_if, IF_KEY, sizeof(settings_if)); |
| 70 | + if (ret < 0) { |
| 71 | + printf("Failed saving/overwriting the register\n"); |
| 72 | + return ret; |
| 73 | + } |
| 74 | + return ret; |
| 75 | +} |
| 76 | + |
| 77 | +int8_t stg_del_if_settings(void) { |
| 78 | + int8_t ret = 0; |
| 79 | + uint8_t ifaces = gnrc_netif_numof(); |
| 80 | + ret = mtd_reg_del(IF_KEY, ifaces * sizeof(settings_ifaces_t)); |
| 81 | + return ret; |
| 82 | +} |
| 83 | + |
| 84 | +int8_t stg_load_if_settings(void) { |
| 85 | + int8_t ret = 0; |
| 86 | + uint8_t ifaces = gnrc_netif_numof(); |
| 87 | + settings_ifaces_t settings_if[ifaces]; |
| 88 | + ret = mtd_load_reg(settings_if, IF_KEY, sizeof(settings_if)); |
| 89 | + if (ret < 0) { |
| 90 | + printf("The register doesn't exists, first you need to save the interface info\n"); |
| 91 | + return ret; |
| 92 | + } |
| 93 | + for (size_t i = 0; i < ifaces; i++) { |
| 94 | + printf("Netif_id: %" PRId16 "\n", settings_if[i].id); |
| 95 | + if (settings_if[i].type == 1) { |
| 96 | + if (set_netopt_tx_power(settings_if[i].tx_power) < 0) { |
| 97 | + printf("Failed: loading radio settings [tx_power]\n"); |
| 98 | + } |
| 99 | + if (set_netopt_channel(settings_if[i].channel) < 0) { |
| 100 | + printf("Failed: loading radio settings [channel]\n"); |
| 101 | + } |
| 102 | + printf("Type: Radio iface\n"); |
| 103 | + printf("id: %" PRId16 "\n", settings_if[i].id); |
| 104 | + printf("Tx power %" PRId16 "\n", settings_if[i].tx_power); |
| 105 | + printf("Channel: %" PRIu16 "\n", settings_if[i].channel); |
| 106 | + } |
| 107 | + } |
| 108 | + return ret; |
| 109 | +} |
| 110 | + |
| 111 | +void build_if_settings(settings_ifaces_t *if_sets, uint8_t num_ifaces) { |
| 112 | + gnrc_netif_t *netif = NULL; |
| 113 | + for (uint8_t i = 0; i < num_ifaces; i++) { |
| 114 | + netif = gnrc_netif_iter(netif); |
| 115 | + if (netif->ops != NULL) { |
| 116 | + if_sets[i].id = netif->pid; |
| 117 | + } |
| 118 | + switch (netif->device_type) { |
| 119 | + case NETDEV_TYPE_IEEE802154: |
| 120 | + if_sets[i].type = 1; |
| 121 | + if (get_netopt_tx_power(&if_sets[i].tx_power) < 0) { |
| 122 | + puts("Err Loading Tx_power"); |
| 123 | + } |
| 124 | + if (get_netopt_channel(&if_sets[i].channel) < 0) { |
| 125 | + puts("Err Loading "); |
| 126 | + } |
| 127 | + break; |
| 128 | + default: |
| 129 | + if_sets[i].type = 0; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments