From: Michal Szczecinski Date: Sat, 19 Dec 2015 11:46:35 +0000 (+0100) Subject: [Files Sharing Server] Added model description. X-Git-Tag: tizen_3.0/TD_SYNC/20161201~220^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9a24d6cae546a67dd790d1dc231b8dca25ed2ede;p=sdk%2Fonline-doc.git [Files Sharing Server] Added model description. Change-Id: I637c9b2ec003d86edd6cece55978e6e556f77f55 Signed-off-by: Michal Szczecinski --- diff --git a/org.tizen.sampledescriptions/html/images/files_sharing_server_model_tasks.png b/org.tizen.sampledescriptions/html/images/files_sharing_server_model_tasks.png new file mode 100644 index 0000000..29f32a3 Binary files /dev/null and b/org.tizen.sampledescriptions/html/images/files_sharing_server_model_tasks.png differ diff --git a/org.tizen.sampledescriptions/html/mobile_n/files_sharing_server_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/files_sharing_server_sd_mn.htm index 8f243f3..35a4e60 100644 --- a/org.tizen.sampledescriptions/html/mobile_n/files_sharing_server_sd_mn.htm +++ b/org.tizen.sampledescriptions/html/mobile_n/files_sharing_server_sd_mn.htm @@ -316,11 +316,187 @@ static void __view_del_file_item_cb(void *data, Evas_Object *obj) } -

Model

+

The application's model consists of three sub-modules: Model, File and the Bluetooth module. The main tasks are presented in the picture below:

+

Model tasks +

The model's data is represented by the model_data_t structure, which is allocated in the model_init() function, called by the controller when the application is created.

+ +
+struct _model_data_t {
+   Eina_List *files_list;
+   int transfer_id;
+};
+typedef struct _model_data_t model_data_t;
+
+ +
+extern model_data_t *model_init(void)
+{
+   model_data_t *md = NULL;
+   md = (model_data_t *)malloc(sizeof(model_data_t));
+   md->files_list = NULL;
+
+   if (!bt_module_init())
+      /*Error handling.*/
+
+   return md;
+}
+
+ +

The model object destructor is called on app_terminate() by the constructor and releases all allocated data.

+
+extern void model_deinit(model_data_t *md)
+{
+   file_t *file = NULL;
+
+   if (!md || !md->files_list)
+      /*Error handling*/
+
+   EINA_LIST_FREE (md->files_list, file)
+      file_delete(file);
+
+   free(md);
+   bt_module_deinit();
+}
+
+ +

Bluetooth initialization is described below:

+ +
+bool bt_module_init(void)
+{
+   int ret = BT_ERROR_NONE;
+   char *directory = NULL;
+
+   /*Set storage directory for received files*/
+   storage_get_directory(0, STORAGE_DIRECTORY_DOWNLOADS, &directory);
+   if(!directory)
+      /*Error handling.*/
+
+   /*Initialize Bluetooth.*/
+   ret = bt_initialize();
+   if (ret != BT_ERROR_NONE)
+      /*Error handling.*/
+
+   /*Initialize the Bluetooth OPP server and set callbacks invoked when connection is requested.*/
+   ret = bt_opp_server_initialize_by_connection_request(directory, __bt_connection_request_cb, NULL);
+   if (ret != BT_ERROR_NONE)
+      /*Error handling.*/
+
+   free(directory);
+   return true;
+}
+
+ +

All initialized modules must be released when they are no longer needed. In case of this sample application, it is called on app_terminate event.

+ +
+void bt_module_deinit(void)
+{
+   int ret = BT_ERROR_NONE;
+
+   ret = bt_opp_server_deinitialize();
+   if (ret != BT_ERROR_NONE)
+      /*Error handling.*/
+
+   ret = bt_deinitialize();
+   if (ret != BT_ERROR_NONE)
+      /*Error handling.*/
+}
+
+ +

If another device starts to send files using the Bluetooth OPP protocol, the request callback is invoked. In this sample application, Ecore Events mechanism is used to inform the controller about actions from other modules. This approach was chosen to avoid static data structures declared in each module.

+ +
+static void __bt_connection_request_cb(const char *remote_addr, void *data)
+{
+   ecore_event_add(app_utils_get_event_type(EVENT_CONNECTION_REQUEST), NULL, NULL, NULL);
+}
+
+ +

After receiving a request event, controller changes the state of the view to enable the accept button. When the user taps it, bt_module_transfer_accept() function is called. It accepts the connection and starts the files transfer. It is also used to set callback functions which inform the application about the progress changes and transfer finished events.

+ +

+void bt_module_transfer_accept(model_data_t *md)
+{
+   int ret = BT_ERROR_NONE;
+
+   if (!md)
+      /*Error handling.*/
+
+   ret = bt_opp_server_accept(__bt_transfer_progress_cb, __bt_transfer_finished_cb,
+            NULL, md, &(md->transfer_id));
+
+   if (ret != BT_ERROR_NONE)
+      /*Error handling.*/
+}
+
+ +

The __bt_transfer_progress_cb() function is used to update the received file progress and distinguish new files transfer. +For that purpose, the transfer_in_progress flag is declared in the static memory. It is reset by the __bt_transfer_finished_cb() callback function.

+ +

+static void __bt_transfer_progress_cb(const char *file, long long size, int percent, void *user_data)
+{
+   model_data_t *md = (model_data_t *)user_data;
+   static file_t *recv_file = NULL;
+
+   if (!md)
+      /*Error handling.*/
+
+   /*Receive new file. In that case EVENT_TRANSFER_STARTED is emitted and it is handled by the controller
+   to add new file item into the files list.*/
+   if (!transfer_in_progres) {
+      /*Create a new file object.*/
+      recv_file = file_create(md, strdup(file), NULL);
+      /*Pass the created file to the controller module. New genlist item is created based on the file.*/
+      ecore_event_add(app_utils_get_event_type(EVENT_TRANSFER_STARTED), recv_file, __bt_transfer_started_end_cb, NULL);
+      /*Change the flag to true. It will be reset when the transfer of the file is completed.*/
+      transfer_in_progres = true;
+   }
+
+   /*Update the progress of the received file.*/
+   file_update_progress(recv_file, percent);
+   /*Pass the received file to the controller. It is used to update the progressbar widget for a specific file in the received files list.*/
+   ecore_event_add(app_utils_get_event_type(EVENT_TRANSFER_IN_PROGRESS), recv_file, __bt_transfer_in_progress_end_cb, NULL);
+}
+
+ +

Transfer finished callback is used to change the value of the transfer_in_progress flag.

+

+static void __bt_transfer_finished_cb(int result, const char *file, long long size, void *user_data)
+{
+   transfer_in_progres = false;
+}
+
+ +

All helper function used in the sample application are implemented in the app_utils.c file. The most important function in this module initializes new Ecore Callbacks used by the controller.

+ +

+typedef enum {
+   EVENT_CONNECTION_REQUEST = 0,
+   EVENT_TRANSFER_IN_PROGRESS,
+   EVENT_ACCEPT_BUTTON_CLICKED,
+   EVENT_REJECT_BUTTON_CLICKED,
+   EVENT_TRANSFER_STARTED,
+} event_type_t;
+
+ +

+extern int app_utils_get_event_type(event_type_t event)
+{
+   int i = 0;
 
+   if (!initialized) {
+      initialized = true;
+      for (i = 0; i < APP_EVENTS_COUNT; i++)
+         arr_custom_events[i] = ecore_event_type_new();
+   }
 
+   return arr_custom_events[event];
+}
+