Skip to content

Commit

Permalink
[Tizen] Introduce network service discovery
Browse files Browse the repository at this point in the history
This patch introduces network service discovery to Tizen service.
NSD will be used to set ip and port automatically.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Jul 8, 2024
1 parent 80dcd1b commit 4667c9a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 11 deletions.
113 changes: 102 additions & 11 deletions Tizen.web/ImageClassificationOffloadingService/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,144 @@

#include "main.h"
#include <bundle.h>
#include <dns-sd.h>
#include <message_port.h>
#include <service_app.h>
#include <stdlib.h>
#include <tizen.h>

/**
* @brief Service app create callback
*/
static bool _create_cb(void* user_data){
static bool _create_cb(void *user_data) {
dlog_print(DLOG_INFO, LOG_TAG, "Callback create\n");
return true;
}

/**
* @brief Service app terminate callback
*/
static void _terminate_cb(void* user_data){
static void _terminate_cb(void *user_data) {
dlog_print(DLOG_INFO, LOG_TAG, "Callback terminate\n");
}

/**
* @brief Service app app control callback
*/
static void _app_control_cb(app_control_h app_control, void* user_data){
static void _app_control_cb(app_control_h app_control, void *user_data) {
dlog_print(DLOG_INFO, LOG_TAG, "Callback app_control\n");
}

/**
* @brief print dnssd remote service information
*/
void _dnssd_print_found(dnssd_service_h dnssd_remote_service) {
/* Handling the found service */
char *service_name = NULL;
char *service_type = NULL;
char *ip_v4_address = NULL;
char *ip_v6_address = NULL;
int port = 0;
int error_code = dnssd_service_get_name(dnssd_remote_service, &service_name);
if (error_code == DNSSD_ERROR_NONE && service_name != NULL)
dlog_print(DLOG_DEBUG, LOG_TAG, "Service name [%s]", service_name);

error_code = dnssd_service_get_type(dnssd_remote_service, &service_type);
if (error_code == DNSSD_ERROR_NONE && service_type != NULL)
dlog_print(DLOG_DEBUG, LOG_TAG, "Service type [%s]", service_type);

error_code = dnssd_service_get_ip(dnssd_remote_service, &ip_v4_address,
&ip_v6_address);
if (error_code == DNSSD_ERROR_NONE) {
if (ip_v4_address)
dlog_print(DLOG_DEBUG, LOG_TAG, "IPV4 address [%s]", ip_v4_address);
if (ip_v6_address)
dlog_print(DLOG_DEBUG, LOG_TAG, "IPV6 address [%s]", ip_v6_address);
}

error_code = dnssd_service_get_port(dnssd_remote_service, &port);
if (error_code == DNSSD_ERROR_NONE)
dlog_print(DLOG_DEBUG, LOG_TAG, "Service port [%d]", port);

if (service_name)
free(service_name);
if (service_type)
free(service_type);
if (ip_v4_address)
free(ip_v4_address);
if (ip_v6_address)
free(ip_v6_address);
}

/**
* @brief dnssd found callback
*/
void _found_cb(dnssd_service_state_e state,
dnssd_service_h dnssd_remote_service, void *user_data) {
dlog_print(DLOG_DEBUG, LOG_TAG, "Browse Service Callback\n");
dlog_print(DLOG_DEBUG, LOG_TAG, "Handler: %u\n", dnssd_remote_service);
dlog_print(DLOG_DEBUG, LOG_TAG, "State: ");
switch (state) {
case DNSSD_SERVICE_STATE_AVAILABLE:
/* DNS-SD service found */
dlog_print(DLOG_DEBUG, LOG_TAG, "Available\n");
_dnssd_print_found(dnssd_remote_service);
break;
case DNSSD_SERVICE_STATE_UNAVAILABLE:
/* DNS-SD service becomes unavailable */
dlog_print(DLOG_DEBUG, LOG_TAG, "Un-Available\n");
break;
case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED:
/* Browsing failed */
dlog_print(DLOG_DEBUG, LOG_TAG, "Browse Failure\n");
break;
case DNSSD_SERVICE_STATE_HOST_NAME_LOOKUP_FAILED:
/* Resolving service name failed */
dlog_print(DLOG_DEBUG, LOG_TAG, "Resolve Service Name Failure\n");
break;
case DNSSD_SERVICE_STATE_ADDRESS_LOOKUP_FAILED:
/* Resolving service address failed */
dlog_print(DLOG_DEBUG, LOG_TAG, "Resolve Service Address\n");
break;
default:
dlog_print(DLOG_DEBUG, LOG_TAG, "Unknown Browse State\n");
break;
}
}

/**
* @brief Main function.
*/
int main(int argc, char *argv[]) {
service_app_lifecycle_callback_s event_callback = {
.create = _create_cb,
.terminate = _terminate_cb,
.app_control = _app_control_cb
};
dnssd_browser_h browser_handle;
char *target = "_nsd_offloading._tcp";
int ret;
bool found;

service_app_lifecycle_callback_s event_callback = {.create = _create_cb,
.terminate = _terminate_cb,
.app_control =
_app_control_cb};

dlog_print(DLOG_ERROR, LOG_TAG,
"Image classification offloading service start");

bool found;
int ret = message_port_check_remote_port(REMOTE_APP_ID, PORT_NAME, &found);
ret = dnssd_initialize();
if (ret != DNSSD_ERROR_NONE)
dlog_print(DLOG_ERROR, LOG_TAG, "Dnssd initialize failed");

ret = dnssd_start_browsing_service(target, &browser_handle, _found_cb, NULL);
if (ret == DNSSD_ERROR_NONE)
dlog_print(DLOG_DEBUG, LOG_TAG, "Start browsing");

ret = message_port_check_remote_port(REMOTE_APP_ID, PORT_NAME, &found);
if (ret != MESSAGE_PORT_ERROR_NONE || !found) {
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port");
}

/* Todo: Send network information to ui application */
/** Todo: Change Uni-directional to Bi-directional
It takes time to find a remote service
Do not send network information when starting service */
bundle *b = bundle_create();
bundle_add_str(b, "command", "connected");
ret = message_port_send_message(REMOTE_APP_ID, PORT_NAME, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
<icon>imageclassificationoffloadingservice.png</icon>
<label>imageclassificationoffloadingservice</label>
</service-application>
<privileges>
<privilege>http://tizen.org/privilege/internet</privilege>
</privileges>
</manifest>

0 comments on commit 4667c9a

Please sign in to comment.