();
///
/// Initializes IoTCon. Call this API to start IoTCon.
///
///
/// @a filePath point to a file for handling secure virtual resources.
/// The file that is CBOR(Concise Binary Object Representation)-format must already exist
/// in @a filePath. We recommend to use application-local file for @a filePath.
///
///
/// http://tizen.org/privilege/network.get \n
/// http://tizen.org/privilege/internet
///
/// The file path to point to storage for handling secure virtual resources.
///
/// You must call Deinitialize() if IoTCon API is no longer needed.
///
///
///
/// string filePath = "../../res/iotcon-test-svr-db-server.dat";
/// IoTConnectivityServerManager.Initialize(filePath);
///
public static void Initialize(string filePath)
{
int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(filePath);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
///
/// Deinitializes IoTCon.
///
///
/// This API must be called if IoTCon API is no longer needed.
///
///
/// Initialize() should be called to initialize.
///
///
///
/// IoTConnectivityServerManager.Deinitialize();
///
public static void Deinitialize()
{
_resources.Clear();
s_requestId = 1;
s_RequestHandlerCallbackMap.Clear();
Interop.IoTConnectivity.Client.IoTCon.Deinitialize();
}
///
/// Registers a resource in IoTCon server
///
///
/// http://tizen.org/privilege/internet
///
/// The resource to register
///
/// Initialize() should be called to initialize.
///
///
///
///
/// ResourceTypes types = new ResourceTypes(new List(){ "org.tizen.light" });
/// Attributes attributes = new Attributes { { "state", "ON" }};
/// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
/// try {
/// IoTConnectivityServerManager.RegisterResource(res);
/// } catch(Exception ex) {
/// Console.Log("Exception caught : " + ex.Message);
/// }
///
public static void RegisterResource(Resource resource)
{
Log.Info(IoTConnectivityErrorFactory.LogTag, "...");
IntPtr id = IntPtr.Zero;
lock (s_RequestHandlerCallbackMap)
{
id = (IntPtr)s_requestId++;
}
s_RequestHandlerCallbackMap[id] = (IntPtr r_resource, IntPtr request, IntPtr userData) =>
{
int requestId = (int)userData;
Log.Info(IoTConnectivityErrorFactory.LogTag, "Received s_RequestHandlerCallbackMap : " + requestId);
if (request == IntPtr.Zero)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "request is IntPtr.Zero");
return;
}
resource.OnRequest(r_resource, request, userData);
};
IntPtr handle = IntPtr.Zero;
int errorCode = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, s_RequestHandlerCallbackMap[id], id, out handle);
if (errorCode != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource");
lock (s_RequestHandlerCallbackMap)
{
s_RequestHandlerCallbackMap.Remove(id);
}
throw IoTConnectivityErrorFactory.GetException(errorCode);
}
else
{
resource.ResourceHandle = handle;
}
_resources.Add(resource);
}
///
/// Unregisters a resource in IoTCon server
///
///
/// http://tizen.org/privilege/internet
///
/// The resource to unregister
///
/// Initialize() should be called to initialize.
///
///
///
///
/// ResourceTypes types = new ResourceTypes(new List(){ "org.tizen.light" });
/// Attributes attributes = new Attributes { { "state", "ON" }};
/// Resource res = new LiteResource("/room/1", types, ResourcePolicy.Discoverable, attributes);
/// IoTConnectivityServerManager.RegisterResource(res);
/// try {
/// IoTConnectivityServerManager.UnregisterResource(res);
/// } catch(Exception ex) {
/// Console.Log("Exception caught : " + ex.Message);
/// }
///
public static void UnregisterResource(Resource resource)
{
if (resource != null)
{
if (resource.ResourceHandle != IntPtr.Zero)
{
Interop.IoTConnectivity.Server.Resource.Destroy(resource.ResourceHandle);
}
_resources.Remove(resource);
}
}
///
/// Starts presence of a server
///
///
/// Use this API to send server's announcements to clients.
/// Server can call this API when online for the first time or come back from offline to online.\n
/// If @a time is 0, server will set default value as 60 seconds.\n
/// If @a time is very big, server will set maximum value as (60 * 60 * 24) seconds, (24 hours).
///
///
/// http://tizen.org/privilege/internet
///
/// The interval of announcing presence in seconds.
///
/// Initialize() should be called to initialize.
///
///
///
///
///
///
/// try {
/// IoTConnectivityServerManager.StartSendingPresence(120);
/// } catch(Exception ex) {
/// Console.Log("Exception caught : " + ex.Message);
/// }
///
public static void StartSendingPresence(uint time)
{
int ret = Interop.IoTConnectivity.Server.IoTCon.StartPresence(time);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to start presence");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
///
/// Stops presence of a server.
///
///
/// Use this API to stop sending server's announcements to clients.
/// Server can call this API when terminating, entering to offline or out of network.
///
///
/// http://tizen.org/privilege/internet
///
///
/// Initialize() should be called to initialize.
///
///
///
///
///
///
/// IoTConnectivityServerManager.StopSendingPresence();
///
public static void StopSendingPresence()
{
int ret = Interop.IoTConnectivity.Server.IoTCon.StopPresence();
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed cancel presence");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
///
/// Sets the device name
///
///
/// This API sets the name of the local device (the device calling the API).\n
/// If the device name is set, clients can get the name using .
///
/// The device name
///
///
///
///
/// IoTConnectivityServerManager.SetDeviceName("my-tizen");
///
public static void SetDeviceName(string deviceName)
{
int ret = Interop.IoTConnectivity.Server.IoTCon.SetDeviceName(deviceName);
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed set device name");
throw IoTConnectivityErrorFactory.GetException(ret);
}
}
private static List _resources = new List();
}
}