void read_provision_status(void) { char psm_val[65]; psm_handle_t handle; int ret; if ((ret = psm_open(&handle, "network")) != 0) { wmprintf("open psm network error\r\n"); return; } if (psm_get(&handle, "configured", psm_val, sizeof(psm_val)) != 0) { psm_close(&handle); return; } if (0 == strcmp(psm_val, "1")) g_provisioned.state = APP_NETWORK_PROVISIONED; else g_provisioned.state = APP_NETWORK_NOT_PROVISIONED; psm_close(&handle); }
void psm_cli_get(int argc, char **argv) { psm_handle_t handle; int ret; char value[32]; if (argc != 3) { wmprintf("[psm] Usage: %s <module> <variable>\r\n", argv[0]); wmprintf("[psm] Error: invalid number of arguments\r\n"); return; } ret = psm_open(&handle, argv[1]); if (ret != 0) { psm_e("psm_open failed with: %d " "(Is the module name registered?)", ret); return; } psm_get(&handle, argv[2], value, sizeof(value)); wmprintf("[psm] Value: %s\r\n", value); psm_close(&handle); }
/* This task configures Evrythng client and connects to the Evrythng cloud */ static void evrythng_task() { psm_handle_t handle; int rc; if ((rc = psm_open(&handle, "evrythng")) != 0) { wmprintf("psm_open failed with: %d (Is the module name registered?)\n\r", rc); goto exit; } char api_key[128]; if (psm_get(&handle, "api_key", api_key, 128) == 0) { wmprintf("api_key: %s\n\r", api_key); } else { wmprintf("api_key doesn't exist\n\r"); goto exit; } char thng_id_buf[64]; if (psm_get(&handle, "thng_id", thng_id_buf, 64) == 0) { if (thng_id != NULL) { os_mem_free(thng_id); } thng_id = (char*)os_mem_alloc((strlen(thng_id_buf)+1)*sizeof(char)); strcpy(thng_id, thng_id_buf); wmprintf("thng_id: %s\n\r", thng_id); } else { wmprintf("thng_id doesn't exist\n\r"); goto exit; } char url_buf[64]; if (psm_get(&handle, "url", url_buf, 64) == 0) { wmprintf("Evrythng URL: %s\n\r", url_buf); } else { wmprintf("Evrythng URL doesn't exist\n\r"); goto exit; } psm_close(&handle); EvrythngInitHandle(&evt_handle); EvrythngSetUrl(evt_handle, url_buf); EvrythngSetKey(evt_handle, api_key); EvrythngSetLogCallback(evt_handle, log_callback); EvrythngSetConnectionCallbacks(evt_handle, on_connection_lost, on_connection_restored); while (EvrythngConnect(evt_handle) != EVRYTHNG_SUCCESS) { wmprintf("Retry\n\r"); os_thread_sleep(os_msec_to_ticks(5000)); } wmprintf("Connected\n\r"); os_semaphore_create_counting(&button1_sem, "button1_sem", 1000, 0); os_semaphore_create_counting(&button2_sem, "button1_sem", 1000, 0); EvrythngSubThngAction(evt_handle, thng_id, "_led1", 0, action_led_callback); EvrythngSubThngAction(evt_handle, thng_id, "_led2", 0, action_led_callback); os_thread_create(&button1_thread, "button1_task", button_task, (void*)button_1, &button_stack, OS_PRIO_3); os_thread_create(&button2_thread, "button2_task", button_task, (void*)button_2, &button_stack, OS_PRIO_3); exit: os_thread_self_complete(0); }
int get_stats(char *string, int len, wm_stat_type_t stat_type) { struct wlan_network network; struct in_addr ip; char *psm_val; short rssi; int ret; psm_handle_t handle; if (string == NULL) return -1; switch (stat_type) { case CONN_STATS: wlan_get_current_rssi(&rssi); snprintf(string, len, "%u %u %u %u %u %u %d", g_wm_stats.wm_lloss, g_wm_stats.wm_conn_att, g_wm_stats.wm_conn_succ, g_wm_stats.wm_conn_fail, g_wm_stats.wm_auth_fail, g_wm_stats.wm_nwnt_found, rssi); break; case DHCP_STATS: snprintf(string, len, "%u %u %u %u %u", g_wm_stats.wm_dhcp_succ, g_wm_stats.wm_dhcp_fail, g_wm_stats.wm_leas_succ, g_wm_stats.wm_leas_fail, g_wm_stats.wm_addr_type); break; case HTTP_CLIENT_STATS: snprintf(string, len, "%u %u %u %u %u %u", g_wm_stats.wm_ht_dns_fail, g_wm_stats.wm_ht_sock_fail, g_wm_stats.wm_ht_conn_no_route, g_wm_stats.wm_ht_conn_timeout, g_wm_stats.wm_ht_conn_reset, g_wm_stats.wm_ht_conn_other); break; case CLOUD_STATS: g_wm_stats.wm_cl_total = g_wm_stats.wm_cl_post_succ + g_wm_stats.wm_cl_post_fail; snprintf(string, len, "%u %u %u", g_wm_stats.wm_cl_post_succ, g_wm_stats.wm_cl_post_fail, g_wm_stats.wm_cl_total); break; /* fixme: Currently, cloud cumulative stats are not reported in * diagnostics */ case CLOUD_CUMUL_STATS: psm_val = string; g_wm_stats.wm_cl_cum_total = 0; if ((ret = psm_open(&handle, STATS_MOD_NAME)) == 0) { if (psm_get (&handle, VAR_CLOUD_CUMUL_STATS, psm_val, len) == 0) { g_wm_stats.wm_cl_cum_total = atoi(psm_val); } psm_close(&handle); } else { if (ret == -WM_E_PSM_METADATA_CRC) psm_erase_and_init(); if (ret == -WM_E_CRC) psm_erase_partition (psm_get_partition_id(STATS_MOD_NAME)); } g_wm_stats.wm_cl_cum_total = g_wm_stats.wm_cl_cum_total + g_wm_stats.wm_cl_total; snprintf(string, len, "%u", g_wm_stats.wm_cl_cum_total); break; case HTTPD_STATS: snprintf(string, len, "%u %u %u %s-%s", g_wm_stats.wm_hd_wsgi_call, g_wm_stats.wm_hd_file, g_wm_stats.wm_hd_time, g_wm_stats.wm_hd_useragent.product, g_wm_stats.wm_hd_useragent.version); break; case NET_STATS: net_diag_stats(string, len); break; case IP_ADDR: if (wlan_get_current_network(&network)) { snprintf(string, len, ""); return -1; } ip.s_addr = network.address.ip; snprintf(string, len, "%s", inet_ntoa(ip)); break; case TIME: snprintf(string, len, "%u", wmtime_time_get_posix()); break; case PROV_TYPE: snprintf(string, len, "%u", g_wm_stats.wm_prov_type); break; break; default: snprintf(string, len, "Invalid"); } return 0; }
int diagnostics_read_stats_psm(struct json_str *jptr) { int ret; int json_int; char temp[DIAG_STATS_LENGTH]; psm_handle_t handle; ret = psm_open(&handle, STATS_MOD_NAME); if (ret != WM_SUCCESS) { dbg("Failed to open psm module. Error: %d\r\n", ret); if (ret == -WM_E_PSM_METADATA_CRC) psm_erase_and_init(); if (ret == -WM_E_CRC) psm_erase_partition (psm_get_partition_id(STATS_MOD_NAME)); return ret; } psm_get(&handle, VAR_REBOOT_REASON, temp, sizeof(temp)); json_int = atoi(temp); json_set_val_int(jptr, VAR_REBOOT_REASON, json_int); psm_get(&handle, VAR_EPOCH, temp, sizeof(temp)); json_int = atoi(temp); json_set_val_int(jptr, VAR_EPOCH, json_int); psm_get(&handle, VAR_CONN_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_CONN_STATS, temp); psm_get(&handle, VAR_DHCP_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_DHCP_STATS, temp); psm_get(&handle, VAR_HTTP_CLIENT_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_HTTP_CLIENT_STATS, temp); psm_get(&handle, VAR_CLOUD_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_CLOUD_STATS, temp); /* fixme: Removing cloud cumulative stats from the output */ #if 0 psm_get(&handle, VAR_CLOUD_CUMUL_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_CLOUD_CUMUL_STATS, temp); #endif psm_get(&handle, VAR_HTTPD_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_HTTPD_STATS, temp); psm_get(&handle, VAR_NET_STATS, temp, sizeof(temp)); json_set_val_str(jptr, VAR_NET_STATS, temp); psm_get(&handle, VAR_IP_ADDR, temp, sizeof(temp)); json_set_val_str(jptr, VAR_IP_ADDR, temp); psm_get(&handle, VAR_TIME, temp, sizeof(temp)); json_int = atoi(temp); json_set_val_int(jptr, VAR_TIME, json_int); psm_close(&handle); return 0; }
/**************************************** * generate new token to global token array ****************************************/ static int deal_with_token(bool should_re_gen) { int ret; #define STR_LEN 35 char strval[STR_LEN] = {0}; psm_handle_t handle; ret = psm_open(&handle,"ot_config"); if(ret) { LOG_ERROR("open psm area error \r\n"); return -WM_FAIL; } ret = psm_get(&handle,OT_TOKEN,strval,STR_LEN); if(!ret && !should_re_gen) { //token already exist //read token_string to token array arch_axtobuf_detail(strval, sizeof(strval), token, sizeof(token), NULL); goto exit; } //should regenerator token if(ret) { //which mean first time generate token memset(token,0,sizeof(token)); } //max length is 10 + 16 + 16 +6 = 48 #define GEN_BUF_LEN (52) uint8_t token_generate_buffer[GEN_BUF_LEN]; int n = 0; n = snprintf_safe((char *)token_generate_buffer,GEN_BUF_LEN,"%u",os_ticks_get()); memcpy(token_generate_buffer + n,cfg_struct.key,OT_PROTO_DEVICE_KEY_SIZE); n+= OT_PROTO_DEVICE_KEY_SIZE; memcpy(token_generate_buffer + n,token,sizeof(token)); n += sizeof(token); memcpy(token_generate_buffer + n,cfg_struct.mac_address,6); n += 6; //generate token md5(token_generate_buffer,n,token); snprintf_safe(strval,STR_LEN,"%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", token[0], token[1], token[2], token[3], token[4], token[5], token[6], token[7], token[8], token[9], token[10], token[11], token[12], token[13], token[14], token[15] ); ret = psm_set(&handle,OT_TOKEN,strval); if(ret){ LOG_ERROR("write token fail"); goto exit; } LOG_DEBUG("generate token success \r\n"); exit: psm_close(&handle); return ret; }