NVProperty generic key value store using the MCU flash area.
Dependents: Turtle_RadioShuttle
NVProperty_Editor.cpp
00001 /* 00002 * This is an unpublished work copyright 00003 * (c) 2019 Helmut Tschemernjak 00004 * 30826 Garbsen (Hannover) Germany 00005 * 00006 * 00007 * Use is granted to registered RadioShuttle licensees only. 00008 * Licensees must own a valid serial number and product code. 00009 * Details see: www.radioshuttle.de 00010 */ 00011 00012 00013 #include <mbed.h> 00014 #include "PinMap.h" 00015 #include "main.h" 00016 #include "arch.h" 00017 00018 #ifdef FEATURE_NVPROPERTYEDITOR 00019 #include <NVPropertyProviderInterface.h> 00020 #include "NVProperty.h" 00021 00022 static const char *getNiceName(int id, int val); 00023 00024 /* 00025 * add custom user defined properties here 00026 */ 00027 class UserProperty : public NVProperty { 00028 public: 00029 enum Properties { 00030 MY_DEVNAME = PRIVATE_RANGE_START, 00031 MY_CITY, 00032 MY_APP_PASSWORD, 00033 }; 00034 }; 00035 00036 // #define ARRAYLEN(arr) (sizeof(arr) / sizeof(0[arr])) 00037 00038 struct propArray { 00039 int id; 00040 NVProperty::NVPType type; 00041 const char *name; 00042 int valueInt; 00043 const char *valueStr; 00044 } propArray[] = { 00045 { NVProperty::RTC_AGING_CAL, NVProperty::T_32BIT, "RTC_AGING_CAL", 0, NULL }, 00046 { NVProperty::ADC_VREF, NVProperty::T_32BIT, "ADC_VREF", 0, NULL }, 00047 { NVProperty::HARDWARE_REV, NVProperty::T_32BIT, "HARDWARE_REV", 0, NULL }, 00048 00049 { NVProperty::LORA_DEVICE_ID, NVProperty::T_32BIT, "LORA_DEVICE_ID", 0, NULL }, 00050 { NVProperty::LORA_CODE_ID, NVProperty::T_32BIT, "LORA_CODE_ID", 0, NULL }, 00051 { NVProperty::LORA_REMOTE_ID, NVProperty::T_32BIT, "LORA_REMOTE_ID", 0, NULL }, 00052 { NVProperty::LORA_REMOTE_ID_ALT, NVProperty::T_32BIT, "LORA_REMOTE_ID_ALT", 0, NULL }, 00053 { NVProperty::LORA_RADIO_TYPE, NVProperty::T_32BIT, "LORA_RADIO_TYPE", 0, NULL }, 00054 { NVProperty::LORA_FREQUENCY, NVProperty::T_32BIT, "LORA_FREQUENCY", 0, NULL }, 00055 { NVProperty::LORA_BANDWIDTH, NVProperty::T_32BIT, "LORA_BANDWIDTH", 0, NULL }, 00056 { NVProperty::LORA_SPREADING_FACTOR, NVProperty::T_32BIT, "LORA_SPREADING_FACTOR", 0, NULL }, 00057 { NVProperty::LORA_TXPOWER, NVProperty::T_32BIT, "LORA_TXPOWER", 0, NULL }, 00058 { NVProperty::LORA_FREQUENCY_OFFSET, NVProperty::T_32BIT, "LORA_FREQUENCY_OFFSET", 0, NULL }, 00059 { NVProperty::LORA_APP_PWD, NVProperty::T_STR, "LORA_APP_PWD", 0, NULL }, 00060 { NVProperty::LORA_APP_PWD_ALT, NVProperty::T_STR, "LORA_APP_PWD_ALT", 0, NULL }, 00061 00062 { NVProperty::LOC_LONGITUDE, NVProperty::T_STR, "LOC_LONGITUDE", 0, NULL }, 00063 { NVProperty::LOC_LATITUDE, NVProperty::T_STR, "LOC_LATITUDE", 0, NULL }, 00064 { NVProperty::LOC_NAME, NVProperty::T_STR, "LOC_NAME", 0, NULL }, 00065 { NVProperty::HOSTNAME, NVProperty::T_STR, "HOSTNAME", 0, NULL }, 00066 00067 { NVProperty::WIFI_SSID, NVProperty::T_STR, "WIFI_SSID", 0, NULL }, 00068 { NVProperty::WIFI_PASSWORD, NVProperty::T_STR, "WIFI_PASSWORD", 0, NULL }, 00069 { NVProperty::WIFI_SSID_ALT, NVProperty::T_STR, "WIFI_SSID_ALT", 0, NULL }, 00070 { NVProperty::WIFI_PASSWORD_ALT, NVProperty::T_STR, "WIFI_PASSWORD_ALT", 0, NULL }, 00071 { NVProperty::USE_DHCP, NVProperty::T_32BIT, "USE_DHCP", 0, NULL }, 00072 { NVProperty::MAC_ADDR, NVProperty::T_STR, "MAC_ADDR", 0, NULL }, 00073 { NVProperty::NET_IP_ADDR, NVProperty::T_STR, "NET_IP_ADDR", 0, NULL }, 00074 { NVProperty::NET_IP_ROUTER, NVProperty::T_STR, "NET_IP_ROUTER", 0, NULL }, 00075 { NVProperty::NET_IP_DNS_SERVER, NVProperty::T_STR, "NET_IP_DNS_SERVER", 0, NULL }, 00076 { NVProperty::NET_IP_DNS_SERVER_ALT, NVProperty::T_STR, "NET_IP_DNS_SERVer_ALT", 0, NULL }, 00077 00078 { NVProperty::NET_NTP_SERVER, NVProperty::T_STR, "NET_NTP_SERVER", 0, NULL }, 00079 { NVProperty::NET_NTP_SERVER_ALT,NVProperty::T_STR, "NET_NTP_SERVER_ALT", 0, NULL }, 00080 { NVProperty::NET_NTP_GMTOFFSET, NVProperty::T_32BIT, "NET_NTP_GMTOFFSET", 0, NULL }, 00081 { NVProperty::NET_NTP_DAYLIGHTOFFSET, NVProperty::T_32BIT, "NET_NTP_DAYLIGHTOFFSET", 0, NULL }, 00082 00083 { NVProperty::MQTT_SERVER, NVProperty::T_STR, "MQTT_SERVER", 0, NULL }, 00084 { NVProperty::MQTT_SERVER_ALT, NVProperty::T_STR, "MQTT_SERVER_ALT", 0, NULL }, 00085 { NVProperty::MQTT_TOPIC_INFO, NVProperty::T_STR, "MQTT_TOPIC_INFO", 0, NULL }, 00086 { NVProperty::MQTT_TOPIC_ALARM, NVProperty::T_STR, "MQTT_TOPIC_ALARM", 0, NULL }, 00087 { NVProperty::MQTT_TOPIC_CONTROL, NVProperty::T_STR, "MQTT_TOPIC_CONTROL", 0, NULL }, 00088 { NVProperty::MQTT_TOPIC_4, NVProperty::T_STR, "MQTT_TOPIC_4", 0, NULL }, 00089 { NVProperty::MQTT_TOPIC_5, NVProperty::T_STR, "MQTT_TOPIC_5", 0, NULL }, 00090 00091 { NVProperty::ALARM_STATUS, NVProperty::T_32BIT, "ALARM_STATUS", 0, NULL }, 00092 00093 /* 00094 * A user defined property 00095 */ 00096 { UserProperty::MY_CITY, NVProperty::T_STR, "MY_CITY", 0, NULL }, 00097 }; 00098 00099 void NVPropertyEditor(void) 00100 { 00101 NVProperty p; 00102 00103 rprintf("\nWelcome to the Property Editor which allows reading/writing/erasing non volatile settings\r\n\r\n"); 00104 const char *help = "Properties cmds are:\r\n l (list properties), s (set e.g. s20=value), d (delete e.g. d20), q (quit)\r\n"; 00105 rprintf(help); 00106 00107 while(true) { 00108 char buf[80]; 00109 00110 memset(buf, 0, sizeof(buf)); 00111 00112 rprintf("$ "); 00113 const char *cmd = ConsoleReadline(buf, (int)sizeof(buf), true); 00114 if (!cmd) 00115 continue; 00116 00117 switch(*cmd) { 00118 case 'l': { 00119 int cnt = ARRAYLEN(propArray); 00120 rprintf("List of Properties:\r\n"); 00121 for (int i = 0; i < cnt; i++) { 00122 char tbuf[128]; 00123 const char *value = "(not set)"; 00124 memset(tbuf, 0, sizeof(tbuf)); 00125 if (propArray[i].type <= NVProperty::T_32BIT) { 00126 int val = p.GetProperty(propArray[i].id, NVProperty::NVP_ENOENT); 00127 if (val != NVProperty::NVP_ENOENT) { 00128 value = tbuf; 00129 snprintf(tbuf, sizeof(tbuf), "%d (0x%x) %s", val, val, getNiceName(propArray[i].id, val)); 00130 } 00131 } else if (propArray[i].type == NVProperty::T_STR) { 00132 const char *s = p.GetProperty(propArray[i].id, (const char *)NULL); 00133 if (s) 00134 value = s; 00135 } 00136 rprintf("%24s: %d=%s\r\n", propArray[i].name, propArray[i].id, value); 00137 wait_ms(2); // to flush buffers 00138 } 00139 rprintf("\r\n"); 00140 } 00141 break; 00142 case 'd': { 00143 int id = strtol(++cmd, NULL, 0); 00144 00145 if (id < 1) { 00146 dprintf("invalid parameter"); 00147 break; 00148 } 00149 int cnt = ARRAYLEN(propArray); 00150 int slot = -1; 00151 for (int i = 0; i < cnt; i++) { 00152 if (propArray[i].id == id) { 00153 slot = i; 00154 break; 00155 } 00156 } 00157 if (slot == -1) 00158 dprintf("Property: %d not found in table", id); 00159 else { 00160 dprintf("Deleted Property: %d %s", id, propArray[slot].name); 00161 p.OpenPropertyStore(true); // enable for write 00162 p.EraseProperty(id); 00163 } 00164 } 00165 break; 00166 case 's': { 00167 char *v = (char *)strchr(++cmd, '='); 00168 if (!v) 00169 continue; 00170 *v++ = 0; 00171 00172 int id = strtol(cmd, NULL, 0); 00173 if (id < 1) { 00174 dprintf("invalid parameter"); 00175 break; 00176 } 00177 00178 int cnt = ARRAYLEN(propArray); 00179 int slot = -1; 00180 for (int i = 0; i < cnt; i++) { 00181 if (propArray[i].id == id) { 00182 slot = i; 00183 break; 00184 } 00185 } 00186 if (slot == -1) 00187 dprintf("Property: %d not found in table", id); 00188 else { 00189 dprintf("Set Property: %s: %d=%s", propArray[slot].name, id, v); 00190 p.OpenPropertyStore(true); // enable for write 00191 if (propArray[slot].type == NVProperty::T_STR) { 00192 p.SetProperty(id, p.T_STR, v, p.S_FLASH); 00193 } else if (propArray[slot].type <= NVProperty::T_32BIT) { 00194 p.SetProperty(id, p.T_32BIT, (int)strtoll(v, NULL, 0), p.S_FLASH); 00195 } 00196 } 00197 } 00198 break; 00199 case 'i': 00200 dump("Flash-Area", (void *)0x803c000, 2048); 00201 break; 00202 case 'r': 00203 dprintf("ReorgProperties"); 00204 p.ReorgProperties(); 00205 break; 00206 case 'q': 00207 return; 00208 default: 00209 rprintf(help); 00210 break; 00211 } 00212 } 00213 } 00214 00215 00216 static const char *getNiceName(int id, int val) 00217 { 00218 const char *name = ""; 00219 00220 switch(id) { 00221 case NVProperty::LORA_RADIO_TYPE: 00222 if (val == 1) 00223 return "RS_Node_Offline"; 00224 else if (val == 2) 00225 return "RS_Node_Checking"; 00226 else if (val == 3) 00227 return "RS_Node_Online"; 00228 else if (val == 4) 00229 return "RS_Station_Basic"; 00230 else if (val == 5) 00231 return "RS_Station_Server"; 00232 break; 00233 default: 00234 break; 00235 } 00236 return name; 00237 } 00238 #endif
Generated on Tue Jul 12 2022 20:44:07 by 1.7.2