#include <CAManager.h>
#include <net_connection.h>
+#include <wifi-manager.h>
#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
#include "ocprovisioningmanager.h"
#include "ua_client.h"
+#define _WIFI_AUTO_CONNECT_ // WIFI Auto connection :: FOR TEST
+
#define OC_SECURITY_DB_DAT_FILE_NAME "oic_svr_db.dat"
#define OC_RSRVD_PROTOCOL_INDEPENDENT_ID "piid"
#define OC_CONTROLEE_DATA_FILE_PATH "/opt/usr/data/ua_client/"
string g_uid;
string g_accesstoken;
+#ifdef _WIFI_AUTO_CONNECT_
+bool g_wifi_connected = false;
+
+typedef struct {
+ char *ap_name;
+ char *ap_pwd;
+} ua_wifi_info_s;
+
+pthread_mutex_t thd_mutex;
+
+void _thread_mutex_init() {
+ pthread_mutexattr_t mattr;
+ pthread_mutexattr_init(&mattr);
+ pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&thd_mutex, &mattr);
+ pthread_mutexattr_destroy(&mattr);
+}
+
+
+#endif
+
static void _set_device_info(const char *key, const char *value);
static void _get_device_info(ua_device_info_s *device);
static void _exec_update();
snprintf(file_path, sizeof(file_path), "%s/%s", OC_CONTROLEE_DATA_FILE_PATH, "device_info.ini");
// wait for /opt/ mount
- do {
- sleep(5);
- } while(!g_key_file_load_from_file(keyfile, file_path, flags, &error));
-
- if (!g_key_file_load_from_file(keyfile, file_path, flags, &error)) {
- UA_LOG("error=[%s]", error->message);
- } else {
+ while (1) {
+ if (g_key_file_load_from_file(keyfile, file_path, flags, &error))
+ break;
+ else
+ sleep(5);
+ }
- device->manufacturer = g_key_file_get_string(keyfile, "device_info", "manufacturer", NULL);
- device->model_name = g_key_file_get_string(keyfile, "device_info", "model_name", NULL);
- device->firmware_ver = g_key_file_get_string(keyfile, "device_info", "firmware_ver", NULL);
- device->firmware_update_state = g_key_file_get_string(keyfile, "device_info", "firmware_update_state", NULL);
- device->uuid = g_key_file_get_string(keyfile, "device_info", "uuid", NULL);
- device->access_token = g_key_file_get_string(keyfile, "device_info", "access_token", NULL);
- device->content_server = g_key_file_get_string(keyfile, "device_info", "content_server", NULL);
- device->ocf_server = g_key_file_get_string(keyfile, "device_info", "ocf_server", NULL);
+ device->manufacturer = g_key_file_get_string(keyfile, "device_info", "manufacturer", NULL);
+ device->model_name = g_key_file_get_string(keyfile, "device_info", "model_name", NULL);
+ device->firmware_ver = g_key_file_get_string(keyfile, "device_info", "firmware_ver", NULL);
+ device->firmware_update_state = g_key_file_get_string(keyfile, "device_info", "firmware_update_state", NULL);
+ device->uuid = g_key_file_get_string(keyfile, "device_info", "uuid", NULL);
+ device->access_token = g_key_file_get_string(keyfile, "device_info", "access_token", NULL);
+ device->content_server = g_key_file_get_string(keyfile, "device_info", "content_server", NULL);
+ device->ocf_server = g_key_file_get_string(keyfile, "device_info", "ocf_server", NULL);
- g_key_file_unref(keyfile);
- }
+ g_key_file_unref(keyfile);
UA_LOG("manufacturer=[%s]", device->manufacturer);
UA_LOG("model_name=[%s]", device->model_name);
UA_LOG("ocf_server=[%s]", device->ocf_server);
}
+#ifdef _WIFI_AUTO_CONNECT_
+static void _get_wifi_info(ua_wifi_info_s *wifi)
+{
+ GKeyFile *keyfile;
+ GKeyFileFlags flags = G_KEY_FILE_NONE;
+ GError *error = NULL;
+ char file_path[256] = {0,};
+
+ keyfile = g_key_file_new();
+
+ snprintf(file_path, sizeof(file_path), "%s/%s", OC_CONTROLEE_DATA_FILE_PATH, "wifi_info.ini");
+
+ if (!g_key_file_load_from_file(keyfile, file_path, flags, &error)) {
+ UA_LOG("error=[%s]", error->message);
+ } else {
+ wifi->ap_name = g_key_file_get_string(keyfile, "wifi_info", "ap_name", NULL);
+ wifi->ap_pwd = g_key_file_get_string(keyfile, "wifi_info", "ap_pwd", NULL);
+
+ g_key_file_unref(keyfile);
+ }
+
+ UA_LOG("ap_name=[%s]", wifi->ap_name);
+ UA_LOG("ap_pwd=[%s]", wifi->ap_pwd);
+}
+#endif
+
+
void onPublish(const OCRepresentation &, const int &eCode)
{
UA_LOG("Publish resource response received, code: %d", eCode);
OCStackResult SetDeviceInfo()
{
+ UA_LOG("ENTER");
+
OCStackResult result = OC_STACK_ERROR;
OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "TIZEN_RPI3");
+ UA_LOG("setPropertyValue() to set device name, result=[%d]", result);
if (result != OC_STACK_OK)
{
UA_LOG("Failed to set device name");
}
+#ifdef _WIFI_AUTO_CONNECT_
+wifi_manager_h wifi_h;
+
+static const char *__test_convert_error_to_string(wifi_manager_error_e err_type)
+{
+ switch (err_type) {
+ case WIFI_MANAGER_ERROR_NONE:
+ return "NONE";
+ case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
+ return "INVALID_PARAMETER";
+ case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
+ return "OUT_OF_MEMORY";
+ case WIFI_MANAGER_ERROR_INVALID_OPERATION:
+ return "INVALID_OPERATION";
+ case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
+ return "ADDRESS_FAMILY_NOT_SUPPORTED";
+ case WIFI_MANAGER_ERROR_OPERATION_FAILED:
+ return "OPERATION_FAILED";
+ case WIFI_MANAGER_ERROR_NO_CONNECTION:
+ return "NO_CONNECTION";
+ case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
+ return "NOW_IN_PROGRESS";
+ case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
+ return "ALREADY_EXISTS";
+ case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
+ return "OPERATION_ABORTED";
+ case WIFI_MANAGER_ERROR_DHCP_FAILED:
+ return "DHCP_FAILED";
+ case WIFI_MANAGER_ERROR_INVALID_KEY:
+ return "INVALID_KEY";
+ case WIFI_MANAGER_ERROR_NO_REPLY:
+ return "NO_REPLY";
+ case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
+ return "SECURITY_RESTRICTED";
+ case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED:
+ return "ALREADY_INITIALIZED";
+ case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
+ return "PERMISSION_DENIED";
+ case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
+ return "NOT_SUPPORTED";
+ default :
+ return "UNKNOWN";
+ }
+}
+
+void _free_wifi_info(ua_wifi_info_s *wifi_info)
+{
+ g_free(wifi_info->ap_name);
+ g_free(wifi_info->ap_pwd);
+ g_free(wifi_info);
+}
+
+void _wifi_connection_state_changed_cb(wifi_manager_connection_state_e state, wifi_manager_ap_h ap, void *user_data)
+{
+ UA_LOG("wifi connection state = [%d]", state);
+
+ if (state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED) {
+ pthread_mutex_lock(&thd_mutex);
+ g_wifi_connected = true;
+ pthread_mutex_unlock(&thd_mutex);
+
+ wifi_manager_unset_connection_state_changed_cb(wifi_h);
+ }
+}
+
+
+void _wifi_connected_cb(wifi_manager_error_e result, void *user_data)
+{
+ UA_LOG("result = [%s]", __test_convert_error_to_string(result));
+
+ pthread_mutex_lock(&thd_mutex);
+ g_wifi_connected = true;
+ pthread_mutex_unlock(&thd_mutex);
+
+ wifi_manager_unset_background_scan_cb(wifi_h);
+}
+
+bool _connect_ap_cb(wifi_manager_ap_h ap, void *user_data)
+{
+ UA_LOG("_connect_ap_cb()");
+
+ if (g_wifi_connected == true) {
+ UA_LOG("Already wifi connected");
+ return false;
+ }
+
+ int rv = 0;
+ char *ap_name = NULL;
+
+ ua_wifi_info_s *wifi_info = (ua_wifi_info_s *)user_data;
+
+ rv = wifi_manager_ap_get_essid(ap, &ap_name);
+ if (rv != WIFI_MANAGER_ERROR_NONE) {
+ UA_LOG("Fail to get AP name [%s]", __test_convert_error_to_string((wifi_manager_error_e)rv));
+ return false;
+ } else {
+ UA_LOG("AP name = [%s]", ap_name);
+ }
+
+ if (g_strcmp0(ap_name, wifi_info->ap_name) == 0) {
+ bool required = false;
+
+ if (wifi_manager_ap_is_passphrase_required(ap, &required) == WIFI_MANAGER_ERROR_NONE) {
+ UA_LOG("Passphrase required : %s", required ? "TRUE" : "FALSE");
+ } else {
+ UA_LOG("Fail to get Passphrase required");
+ }
+
+ if (required) {
+ rv = wifi_manager_ap_set_passphrase(ap, wifi_info->ap_pwd);
+ if (rv != WIFI_MANAGER_ERROR_NONE) {
+ UA_LOG("wifi_manager_ap_set_passphrase() failed, [%d]", __test_convert_error_to_string((wifi_manager_error_e)rv));
+ g_free(ap_name);
+ return false;
+ }
+ }
+
+ rv = wifi_manager_connect(wifi_h, ap, _wifi_connected_cb, NULL);
+ UA_LOG("wifi_manager_connect() [%s]", __test_convert_error_to_string((wifi_manager_error_e)rv));
+
+ g_free(ap_name);
+ return false;
+ }
+
+ g_free(ap_name);
+ return true;
+}
+
+
+void _wifi_scan_finished_cb(wifi_manager_error_e result, void *user_data)
+{
+ UA_LOG("_wifi_scan_finished_cb() result=[%s]", __test_convert_error_to_string(result));
+
+ wifi_manager_foreach_found_ap(wifi_h, _connect_ap_cb, user_data);
+}
+
+
+void _wifi_activate_cb(wifi_manager_error_e result, void *user_data)
+{
+ UA_LOG("_wifi_activate_cb() result=[%s]", __test_convert_error_to_string(result));
+}
+
+
+void _wifi_auto_connect(ua_wifi_info_s *wifi_info)
+{
+ UA_LOG("Enter");
+
+ wifi_h = NULL;
+ bool wifi_state = false;
+ int rv;
+
+ if (wifi_manager_initialize(&wifi_h) != WIFI_MANAGER_ERROR_NONE) {
+ UA_LOG("wifi_manager_initialize() failed");
+ return;
+ }
+
+ if (wifi_h == NULL) {
+ UA_LOG("wifi_manager handle is NULL!");
+ return;
+ }
+
+ if (wifi_info->ap_name && strlen(wifi_info->ap_name) > 0) {
+ wifi_manager_is_activated(wifi_h, &wifi_state);
+ UA_LOG("Wifi activate state = [%s]", wifi_state?"TRUE":"FALSE");
+
+ if (wifi_state == false) {
+ wifi_manager_set_background_scan_cb(wifi_h, _wifi_scan_finished_cb, (void *)wifi_info);
+
+ rv = wifi_manager_activate(wifi_h, _wifi_activate_cb, NULL);
+ UA_LOG("wifi_manager_activate() return=[%s]", __test_convert_error_to_string((wifi_manager_error_e)rv));
+ } else {
+ wifi_manager_scan(wifi_h, _wifi_scan_finished_cb, (void *)wifi_info);
+ }
+ } else {
+ wifi_manager_connection_state_e connection_state;
+ wifi_manager_get_connection_state(wifi_h, &connection_state);
+ UA_LOG("Wifi connection state = [%d]", connection_state);
+ if (connection_state == WIFI_MANAGER_CONNECTION_STATE_CONNECTED) {
+ pthread_mutex_lock(&thd_mutex);
+ g_wifi_connected = true;
+ pthread_mutex_unlock(&thd_mutex);
+ } else {
+ wifi_manager_set_connection_state_changed_cb(wifi_h, _wifi_connection_state_changed_cb, NULL);
+ }
+ }
+}
+#endif
+
+
connection_cellular_state_e get_cellular_network_state()
{
connection_h connection;
//cellular
int network_state = 0;
- if (get_cellular_network_state() == CONNECTION_CELLULAR_STATE_CONNECTED)
- {
- network_state = true;
- }
-
- // ethernet
- if (get_ethernet_network_state() == CONNECTION_ETHERNET_STATE_CONNECTED)
- {
- network_state = true;
- }
+// if (get_cellular_network_state() == CONNECTION_CELLULAR_STATE_CONNECTED)
+// {
+// UA_LOG("CONNECTION_CELLULAR_STATE_CONNECTED!");
+// network_state = true;
+// }
+//
+// // ethernet
+// if (get_ethernet_network_state() == CONNECTION_ETHERNET_STATE_CONNECTED)
+// {
+// UA_LOG("CONNECTION_ETHERNET_STATE_CONNECTED!");
+// network_state = true;
+// }
//wifi
if (get_wifi_network_state() == CONNECTION_WIFI_STATE_CONNECTED)
{
+ UA_LOG("CONNECTION_WIFI_STATE_CONNECTED!");
network_state = true;
}
{
UA_LOG("");
- ua_device_info_s *device = (ua_device_info_s *)data;
+#ifdef _WIFI_AUTO_CONNECT_
+ while(1) {
+ if (g_wifi_connected == true)
+ break;
+ sleep(1);
+ }
+#endif
+
+ mutex blocker;
+ unique_lock<mutex> lock(blocker);
+
+ OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
+
+ PlatformConfig cfg
+ {
+ ServiceType::InProc,
+ ModeType::Both,
+ "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
+ 0, // Uses randomly available port
+ QualityOfService::LowQos,
+ &ps
+ };
+
+ OCPlatform::Configure(cfg);
+
+ g_host = "coap+tcp://";
- mutex blocker;
- unique_lock<mutex> lock(blocker);
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
+ g_host = "coaps+tcp://";
+#endif
+
+
+ ua_device_info_s *device = (ua_device_info_s *)data;
+
+ string ocf_server_url(device->ocf_server);
+ g_host += ocf_server_url;
+
+ UA_LOG("host address = %s", g_host.c_str());
+
+ OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(g_host,
+ CT_ADAPTER_TCP);
+
+
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
+ UA_LOG("Security Mode");
+ if (CA_STATUS_OK != saveTrustCert())
+ {
+ UA_LOG("saveTrustCert returned an error");
+ }
+
+ uint16_t cipher = MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256;
+ if (CA_STATUS_OK != CASelectCipherSuite(cipher, CA_ADAPTER_TCP))
+ {
+ UA_LOG("CASelectCipherSuite returned an error");
+ }
+#endif
+
+ try {
+ if (device->uuid == NULL || (device->uuid && strlen(device->uuid) == 0)) {
+ UA_LOG("Sign-up...");
+ accountMgr->signUp("tdc", "11223344", &handleSignupCB);
+ g_callbackLock.wait(lock);
+ UA_LOG("Sign-In...");
+ accountMgr->signIn(g_uid, g_accesstoken, &handleSigninCB);
+ g_callbackLock.wait(lock);
+ } else {
+#if 1 // FOR TDC
+ UA_LOG("Sign-up...");
+ accountMgr->signUp("tdc", "11223344", &handleSignupCB);
+ g_callbackLock.wait(lock);
+ UA_LOG("Sign-In...");
+ accountMgr->signIn(g_uid, g_accesstoken, &handleSigninCB);
+ g_callbackLock.wait(lock);
+#else
+ UA_LOG("Sign-In...");
+ string uuid(device->uuid);
+ string access_token(device->access_token);
+ accountMgr->signIn(uuid, access_token, &handleSigninCB);
+ g_callbackLock.wait(lock);
+#endif
+ }
+ }
+ catch (exception& e){
+ UA_LOG("Authentication failed");
+ return NULL;
+ }
UA_LOG("Registering firmware resources to platform...");
cur_firmware_ver, new_firmware_ver, atoi(device->firmware_update_state));
#endif
+
uri = firmware.getResourceUri();
rt = firmware.getResourceType()[0];
itf = firmware.getInterfaces()[0];
- // Time to Live is 30 seconds
- OCPlatform::startPresence(30);
-
result = OCPlatform::registerResource(firmware.m_handle,
uri,
return NULL;
}
- UA_LOG("Publishing resources to cloud");
-
result = SetDeviceInfo();
if (result != OC_STACK_OK) {
UA_LOG("SetDeviceInfo() is failed, [%d]", result);
return NULL;
}
- OCPlatform::stopPresence();
+ UA_LOG("Publishing resources to cloud");
ResourceHandles resourceHandles;
result = RDClient::Instance().publishResourceToRD(g_host, OCConnectivityType::CT_ADAPTER_TCP,
resourceHandles,
&onPublish);
-
UA_LOG(" result: %d, Waiting Publish default resource response from cloud", result);
+ g_callbackLock.wait(lock);
+
resourceHandles.push_back(firmware.m_handle);
result = RDClient::Instance().publishResourceToRD(g_host, OCConnectivityType::CT_ADAPTER_TCP,
resourceHandles,
&onPublish);
-
UA_LOG(" result: %d, Waiting Publish user resource response from cloud", result);
g_callbackLock.wait(lock);
GThread *main_thd = NULL;
- mutex blocker;
- unique_lock<mutex> lock(blocker);
-
-
- OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
-
- PlatformConfig cfg
- {
- ServiceType::InProc,
- ModeType::Both,
- "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
- 0, // Uses randomly available port
- QualityOfService::LowQos,
- &ps
- };
-
- OCPlatform::Configure(cfg);
-
- g_host = "coap+tcp://";
-
-#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
- g_host = "coaps+tcp://";
-#endif
-
+ // wait for network connection
+// while(is_network_connected() == 0){
+// UA_LOG("network not ready");
+// sleep(5);
+// }
-#if 0
- g_host += argv[1];
-#else
ua_device_info_s *device = (ua_device_info_s *)calloc(1, sizeof(ua_device_info_s));
+ _get_device_info(device);
- _get_device_info(device);
-
- string ocf_server_url(device->ocf_server);
- g_host += ocf_server_url;
-#endif
-
- UA_LOG("host address = %s", g_host.c_str());
-
- OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(g_host,
- CT_ADAPTER_TCP);
-
-
-#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
- UA_LOG("Security Mode");
- if (CA_STATUS_OK != saveTrustCert())
- {
- UA_LOG("saveTrustCert returned an error");
- }
-
- uint16_t cipher = MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256;
- if (CA_STATUS_OK != CASelectCipherSuite(cipher, CA_ADAPTER_TCP))
- {
- UA_LOG("CASelectCipherSuite returned an error");
- }
-#endif
-
- // wait for network connection
- while(is_network_connected() == 0){
- UA_LOG("network not ready");
- sleep(5);
- }
-
- try {
- if (device->uuid == NULL || (device->uuid && strlen(device->uuid) == 0)) {
-#if 0
- if (argc != 3)
- {
- UA_LOG("Put \"[authprovider] [authcode]\" for sign-up and sign-in and publish resources!!");
- return 0;
- }
- UA_LOG("Sign-up...");
- accountMgr->signUp(argv[1], argv[2], &handleSignupCB);
-#else
- UA_LOG("Sign-up...");
- accountMgr->signUp("tdc", "11223344", &handleSignupCB);
+#ifdef _WIFI_AUTO_CONNECT_
+ _thread_mutex_init();
+ ua_wifi_info_s *wifi_info = (ua_wifi_info_s *)calloc(1, sizeof(ua_wifi_info_s));
+ _get_wifi_info(wifi_info);
+ _wifi_auto_connect(wifi_info);
#endif
- g_callbackLock.wait(lock);
- UA_LOG("Sign-In...");
- accountMgr->signIn(g_uid, g_accesstoken, &handleSigninCB);
- g_callbackLock.wait(lock);
- } else {
- UA_LOG("Sign-In...");
- string uuid(device->uuid);
- string access_token(device->access_token);
- accountMgr->signIn(uuid, access_token, &handleSigninCB);
- g_callbackLock.wait(lock);
- }
- }
- catch (exception& e){
- UA_LOG("Authentication failed");
- goto _END_OF_PROC;
- }
main_thd = g_thread_new("ua_client", _start_ua_client, (void *)device);
if (main_thd == NULL) {
UA_LOG("Fail to start ua_client");
}
-
-_END_OF_PROC:
g_free(device->manufacturer);
g_free(device->model_name);
g_free(device->firmware_ver);
g_free(device->content_server);
g_free(device->ocf_server);
g_free(device);
-
+#ifdef _WIFI_AUTO_CONNECT_
+ _free_wifi_info(wifi_info);
+#endif
return 0;
}