1 #include "blecfg_pub.h"
2
3 #include "aos/kv.h"
4
5 #ifdef AOS_COMP_CLI
6 #include "aos/cli.h"
7
8 extern char g_blecfg_devname[128];
9
10 typedef int (*ble_netCfg_shell_cmd_function_t)(int argc, char *argv[]);
11
12 struct ble_netCfg_shell_cmd {
13 const char *cmd_name;
14 ble_netCfg_shell_cmd_function_t cb;
15 const char *help;
16 const char *desc;
17 };
18
BleCfg_cli_cmd_start(int argc,char * argv[])19 int BleCfg_cli_cmd_start(int argc, char *argv[])
20 {
21 BleCfg_stack_start();
22 return 0;
23 }
24
BleCfg_cli_cmd_stop(int argc,char * argv[])25 int BleCfg_cli_cmd_stop(int argc, char *argv[])
26 {
27 BleCfg_stack_stop();
28 return 0;
29 }
30
BleCfg_cli_cmd_name(int argc,char * argv[])31 int BleCfg_cli_cmd_name(int argc, char *argv[])
32 {
33 uint8_t *bt_addr = NULL;
34
35 printf("netconfig device name %s\r\n", g_blecfg_devname);
36
37 #ifdef CHIP_HAAS1000
38 extern uint8_t *factory_section_get_bt_address(void);
39 bt_addr = factory_section_get_bt_address();
40 printf("BT address %02x-%02x-%02x-%02x-%02x-%02x\r\n",
41 bt_addr[0], bt_addr[1], bt_addr[2], bt_addr[3], bt_addr[4], bt_addr[5]);
42 #endif
43
44 return 0;
45 }
46
47 static const struct ble_netCfg_shell_cmd ble_netCfg_commands[] = {
48 { "start", BleCfg_cli_cmd_start, "None" },
49 { "stop", BleCfg_cli_cmd_stop, "None" },
50 { "name", BleCfg_cli_cmd_name, "None" },
51 { NULL, NULL, NULL }
52 };
53
BleCfg_cli_cmd_handler(int argc,char ** argv)54 static void BleCfg_cli_cmd_handler(int argc, char **argv)
55 {
56 int i = 0;
57 int err;
58
59 if (argc < 2) {
60 BLECFG_LOG_INFO("Ble NetConfig support commands");
61
62 for (i = 0; ble_netCfg_commands[i].cmd_name != NULL; i ++) {
63 BLECFG_LOG_INFO(" %s %s", ble_netCfg_commands[i].cmd_name, ble_netCfg_commands[i].help);
64 }
65 return;
66 }
67
68 for (i = 0; ble_netCfg_commands[i].cmd_name != NULL; i ++) {
69 if (strlen(ble_netCfg_commands[i].cmd_name) == strlen(argv[1]) &&
70 !strncmp(ble_netCfg_commands[i].cmd_name, argv[1], strlen(ble_netCfg_commands[i].cmd_name))) {
71 if (ble_netCfg_commands[i].cb) {
72 err = ble_netCfg_commands[i].cb(argc - 1, &argv[1]);
73
74 if (err) {
75 BLECFG_LOG_INFO("%s execute fail, %d\n", ble_netCfg_commands[i].cmd_name, err);
76 }
77 break;
78 }
79 }
80 }
81 }
82
83 /*
84 kv_init has been called in aos_components_init, but can called repeatedly.
85 #ifdef AOS_COMP_KV
86 kv_init();
87 #endif
88 */
89
BleCfg_kv_clear(int argc,char ** argv)90 static void BleCfg_kv_clear(int argc, char **argv)
91 {
92 int res;
93 int idx;
94 char *key_to_del[] = {"product_key", "device_name", "device_secret", "wifi_ssid", "wifi_password"};
95
96 for (idx = 0; idx < sizeof(key_to_del) / sizeof(key_to_del[0]); idx++)
97 {
98 res = aos_kv_del(key_to_del[idx]);
99 printf("aos_kv_clear '%s' %s\r\n", key_to_del[idx], res == 0 ? "success." : "fail!");
100 }
101 }
102
103 ALIOS_CLI_CMD_REGISTER(BleCfg_kv_clear, kvclear, kv info clear)
104 ALIOS_CLI_CMD_REGISTER(BleCfg_cli_cmd_handler, ble_netCfg, ble netConfig commands)
105
106 #endif
107
108