Skip to content

Commit

Permalink
moved from snprintf to sprintf (because sn doesn't exist)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacb committed Dec 16, 2014
1 parent d6b2a58 commit 7a86640
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions user/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void config_execute(void) {
wifi_get_macaddr(SOFTAP_IF, macaddr);
os_strncpy(ap_conf.ssid, AP_SSID, sizeof(ap_conf.ssid));
os_strncpy(ap_conf.password, AP_PASSWORD, sizeof(ap_conf.password));
os_snprintf(&ap_conf.password[strlen(AP_PASSWORD)], sizeof(ap_conf.password) - strlen(AP_PASSWORD), "_%02X%02X%02X", macaddr[3], macaddr[4], macaddr[5]);
//os_snprintf(&ap_conf.password[strlen(AP_PASSWORD)], sizeof(ap_conf.password) - strlen(AP_PASSWORD), "_%02X%02X%02X", macaddr[3], macaddr[4], macaddr[5]);
os_sprintf(&ap_conf.password[strlen(AP_PASSWORD)], "_%02X%02X%02X", macaddr[3], macaddr[4], macaddr[5]);
ap_conf.authmode = AUTH_WPA_PSK;
ap_conf.channel = 6;
ETS_UART_INTR_DISABLE();
Expand All @@ -49,28 +50,40 @@ void config_execute(void) {

#define MSG_OK "OK\r\n"
#define MSG_ERROR "ERROR\r\n"
#define MSG_INVALID_CMD "INVALID COMMAND\r\n"
#define MSG_INVALID_CMD "UNKNOWN COMMAND\r\n"

#define MAX_ARGS 12
#define MSG_BUF_LEN 128

char *my_strdup(char *str) {
size_t len;
char *copy;

len = strlen(str) + 1;
if (!(copy = os_malloc((u_int)len)))
return (NULL);
os_memcpy(copy, str, len);
return (copy);
}

char **config_parse_args(char *buf, uint8_t *argc) {
const char delim[] = " \t";
char *save, *tok;
char **argv = (char **)os_zalloc(sizeof(char *) * MAX_ARGS); // note fixed length
char **argv = (char **)os_malloc(sizeof(char *) * MAX_ARGS); // note fixed length
os_memset(argv, 0, sizeof(char *) * MAX_ARGS);

*argc = 0;
for (; *buf == ' ' || *buf == '\t'; ++buf); // absorb leading spaces
for (tok = strtok_r(buf, delim, &save); tok; tok = strtok_r(NULL, delim, &save)) {
argv[*argc] = strdup(tok);
argv[*argc] = my_strdup(tok);
*argc++;
if (*argc == MAX_ARGS) {
break;
}
}
}

void config_parse_args_free(uint8_t argc, char **argv) {
void config_parse_args_free(uint8_t argc, char *argv[]) {
uint8_t i;
for (i = 0; i <= argc; ++i) {
if (argv[i])
Expand Down Expand Up @@ -130,6 +143,7 @@ void config_cmd_ap(struct espconn *conn, uint8_t argc, char *argv[]) {
struct softap_config ap_conf;

if (argc != 2) {
char buf[MSG_BUF_LEN];
espconn_sent(conn, MSG_ERROR, strlen(MSG_ERROR));
} else {
os_strncpy(ap_conf.ssid, ssid, sizeof(ap_conf.ssid));
Expand All @@ -152,7 +166,7 @@ const config_commands_t config_commands[] = {
};

void config_parse(struct espconn *conn, char *buf, int len) {
char *lbuf = (char *)os_zalloc(len + 1), **argv;
char *lbuf = (char *)os_malloc(len + 1), **argv;
uint8_t i, argc;
// we need a '\0' end of the string
os_memcpy(lbuf, buf, len);
Expand All @@ -171,6 +185,18 @@ void config_parse(struct espconn *conn, char *buf, int len) {
}
// parse out buffer into arguments
argv = config_parse_args(lbuf, &argc);
// debugging
{
uint8_t i;
size_t len;
char buf[MSG_BUF_LEN];
for (i = 0; i < argc; ++i) {
//len = os_snprintf(buf, MSG_BUF_LEN, "argument %d: '%s'\r\n", i, argv[i]);
len = os_sprintf(buf, "argument %d: '%s'\r\n", i, argv[i]);
espconn_sent(conn, buf, len);
}
}
// end debugging
if (argc == 0) {
espconn_sent(conn, MSG_OK, strlen(MSG_OK));
} else {
Expand Down

0 comments on commit 7a86640

Please sign in to comment.