*
*/
#include <glib.h>
+#include <tzplatform_config.h>
#include <stdio.h>
#include <stdlib.h>
#include "mesh-service.h"
#include "mesh-network.h"
+#define MESH_NETWORK_LIST_FILE_PATH tzplatform_mkpath(TZ_SYS_VAR, "/lib/mesh/mesh_network.conf")
+
static void _list_destroy_notify(gpointer data)
{
mesh_network_info_s *info = (mesh_network_info_s*)data;
}
}
+static void _save_current_list_into_storage(GList *saved_network)
+{
+ GKeyFile* storage = NULL;
+ gboolean res = FALSE;
+ GError* error = NULL;
+ mesh_network_info_s *info = NULL;
+
+ GList *iter = NULL;
+ char str_group[16];
+ int group_index = 1;
+
+ storage = g_key_file_new();
+
+ /* Insert list item into storage list */
+ iter = saved_network;
+ while (iter != NULL) {
+ info = (mesh_network_info_s*)iter->data;
+
+ /* Generate item group */
+ snprintf(str_group, 16, "Item%d", group_index++);
+ g_key_file_set_string(storage, str_group, "mesh_id", info->mesh_id);
+ g_key_file_set_integer(storage, str_group, "channel", info->channel);
+ g_key_file_set_integer(storage, str_group, "security", info->security);
+
+ iter = g_list_next(iter);
+ }
+
+ MESH_LOGD("Saving network information list into storage..");
+ res = g_key_file_save_to_file(storage, MESH_NETWORK_LIST_FILE_PATH, &error);
+ if (FALSE == res) {
+ MESH_LOGE("Failed to save mesh network list into storage !");
+ MESH_LOGE(" [%s]", error->message);
+ g_error_free(error);
+ }
+
+ g_key_file_free(storage);
+}
+
+int mesh_network_load_mesh_network(GList **saved_network)
+{
+ GKeyFile* storage;
+ gboolean res = FALSE;
+ GError* error = NULL;
+
+ gsize list_count = 0;
+ gchar** group_list = NULL;
+ mesh_network_info_s *info = NULL;
+ char *mesh_id = NULL;
+ int mesh_channel;
+ int security;
+
+ gsize idx = 0;
+
+ /* Check if stored file exists */
+ res = g_file_test(MESH_NETWORK_LIST_FILE_PATH, G_FILE_TEST_EXISTS);
+ if (FALSE == res) {
+ MESH_LOGD("There is no saved networks on storage");
+ return MESHD_ERROR_NONE;
+ }
+
+ /* Get stored contents */
+ storage = g_key_file_new();
+ res = g_key_file_load_from_file(storage, MESH_NETWORK_LIST_FILE_PATH,
+ G_KEY_FILE_NONE, &error);
+ if (FALSE == res) {
+ MESH_LOGE("Failed to get saved network list from storage !");
+ g_key_file_free(storage);
+ return MESHD_ERROR_IO_ERROR;
+ }
+
+ group_list = g_key_file_get_groups(storage, &list_count);
+ if (group_list) {
+ for(idx = 0; idx < list_count ; idx++) {
+ mesh_id = g_key_file_get_string(storage, group_list[idx],
+ "mesh_id", NULL);
+ mesh_channel = g_key_file_get_integer(storage, group_list[idx],
+ "channel", NULL);
+ security = g_key_file_get_integer(storage, group_list[idx],
+ "security", NULL);
+
+ MESH_LOGD("=========[%d / %d]==========", idx, list_count);
+ MESH_LOGD("[%s] : ", group_list[idx]);
+ MESH_LOGD(" mesh_id : [%s]", mesh_id);
+ MESH_LOGD(" channel : [%d]", mesh_channel);
+ MESH_LOGD(" security : [%d]", security);
+
+ info = g_try_new0(mesh_network_info_s, 1);
+ if (NULL == info) {
+ MESH_LOGE("Failed to allocate memory !");
+ g_key_file_free(storage);
+ g_strfreev(group_list);
+ return MESHD_ERROR_OUT_OF_MEMORY;
+ }
+ info->mesh_id = mesh_id;
+ info->bssid = NULL;
+ info->channel = mesh_channel;
+ info->security = security;
+
+ *saved_network = g_list_prepend(*saved_network, info);
+ }
+ /* Reverse list order to place higher priority sequence */
+ *saved_network = g_list_reverse(*saved_network);
+
+ g_strfreev(group_list);
+ } else {
+ MESH_LOGE("File exists but there is no data");
+ g_key_file_free(storage);
+ return MESHD_ERROR_NO_DATA;
+ }
+
+ g_key_file_free(storage);
+
+ return MESHD_ERROR_NONE;
+}
+
int mesh_network_add_mesh_network(GList **saved_network, const char *mesh_id,
int mesh_channel, int security)
{
*saved_network = g_list_prepend(*saved_network, info);
+ _save_current_list_into_storage(*saved_network);
+
return ret;
}
ret = MESHD_ERROR_NO_DATA;
}
+ _save_current_list_into_storage(*saved_network);
+
return ret;
}
ret = MESHD_ERROR_NO_DATA;
}
+ _save_current_list_into_storage(*saved_network);
+
return ret;
}