From f4f6dfe25d13e28d2055c6d8484b1d067e17dd22 Mon Sep 17 00:00:00 2001 From: Jyothi Kumar Sambolu Date: Wed, 11 May 2016 19:21:44 +0530 Subject: [PATCH] [IoTConnectivity] Added Base implementation Change-Id: I458fb1420c9dfa90f6728cf851f867ffd2d59f7f Signed-off-by: Jyothi Kumar Sambolu --- Makefile | 24 + .../Interop/Interop.IoTConnectivity.Client.cs | 224 +++++++ .../Interop/Interop.IoTConnectivity.Common.cs | 360 ++++++++++ .../Interop/Interop.IoTConnectivity.Server.cs | 156 +++++ .../Interop/Interop.Libraries.cs | 17 + .../Properties/AssemblyInfo.cs | 43 ++ .../Tizen.Network.IoTConnectivity.csproj | 106 +++ .../Tizen.Network.IoTConnectivity.snk | Bin 0 -> 596 bytes .../CacheUpdatedEventArgs.cs | 24 + .../DeviceInformationFoundEventArgs.cs | 41 ++ .../Tizen.Network.IoTConnectivity/FindingError.cs | 65 ++ .../FindingErrorOccurredEventArgs.cs | 30 + .../IoTConnectivityClientManager.cs | 589 +++++++++++++++++ .../IoTConnectivityErrorFactory.cs | 137 ++++ .../IoTConnectivityServerManager.cs | 85 +++ .../Tizen.Network.IoTConnectivity/LiteResource.cs | 130 ++++ .../Tizen.Network.IoTConnectivity/ObservePolicy.cs | 25 + .../Tizen.Network.IoTConnectivity/ObserveType.cs | 32 + .../ObserverNotifiedEventArgs.cs | 30 + .../PlatformInformationFoundEventArgs.cs | 76 +++ .../PresenceEventType.cs | 29 + .../PresenceReceivedEventArgs.cs | 42 ++ .../QualityOfService.cs | 25 + .../RemoteResource.cs | 728 +++++++++++++++++++++ .../RemoteResponse.cs | 31 + .../Representation.cs | 226 +++++++ .../Tizen.Network.IoTConnectivity/Request.cs | 66 ++ .../Tizen.Network.IoTConnectivity/Resource.cs | 392 +++++++++++ .../ResourceFoundEventArgs.cs | 30 + .../ResourceInterfaces.cs | 210 ++++++ .../ResourceOptions.cs | 289 ++++++++ .../ResourcePolicy.cs | 48 ++ .../Tizen.Network.IoTConnectivity/ResourceQuery.cs | 365 +++++++++++ .../Tizen.Network.IoTConnectivity/ResourceState.cs | 25 + .../Tizen.Network.IoTConnectivity/ResourceTypes.cs | 178 +++++ .../Tizen.Network.IoTConnectivity/Response.cs | 94 +++ .../Tizen.Network.IoTConnectivity/ResponseCode.cs | 41 ++ .../Tizen.Network.IoTConnectivity/State.cs | 678 +++++++++++++++++++ .../StateChangedEventArgs.cs | 24 + packaging/csapi-network-iotconnectivity.manifest | 5 + packaging/csapi-network-iotconnectivity.pc.in | 5 + packaging/csapi-network-iotconnectivity.spec | 81 +++ 42 files changed, 5806 insertions(+) create mode 100755 Makefile create mode 100755 Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Client.cs create mode 100755 Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Common.cs create mode 100755 Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs create mode 100755 Tizen.Network.IoTConnectivity/Interop/Interop.Libraries.cs create mode 100755 Tizen.Network.IoTConnectivity/Properties/AssemblyInfo.cs create mode 100644 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.csproj create mode 100644 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.snk create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/CacheUpdatedEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/DeviceInformationFoundEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingError.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingErrorOccurredEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityClientManager.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityErrorFactory.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/LiteResource.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObservePolicy.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserveType.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserverNotifiedEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PlatformInformationFoundEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceEventType.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceReceivedEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/QualityOfService.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResource.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResponse.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Request.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Resource.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceFoundEventArgs.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceInterfaces.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceOptions.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourcePolicy.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceQuery.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceState.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceTypes.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Response.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResponseCode.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/State.cs create mode 100755 Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/StateChangedEventArgs.cs create mode 100644 packaging/csapi-network-iotconnectivity.manifest create mode 100644 packaging/csapi-network-iotconnectivity.pc.in create mode 100755 packaging/csapi-network-iotconnectivity.spec diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..8058f34 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +ASM_DIRS := Tizen.Network.IoTConnectivity +ASM_DLLS := $(addsuffix .dll,$(ASM_DIRS)) + +FLAGS := /unsafe + +ALL: $(ASM_DLLS) + +define make-dll +$(eval ASM = $(strip $1)) +$(eval SRC = $(shell find $(ASM) -path $(ASM)/obj -prune -o -name '*.cs' -print)) +$(eval PKG = $(shell echo $2 | tr ' ' ',')) +$(ASM).dll: $(SRC) + @echo "[BUILD] $$@" + @mcs /nologo /out:$$@ /t:library /keyfile:$(ASM)/$(ASM).snk $(addprefix /pkg:,$(PKG)) $(FLAGS) $(SRC) + @echo "[CHECK] $$@" + @RET=`mono-shlib-cop $$@`; \ + CNT=`echo $$$$RET | grep -e '^error:' | wc -l`; \ + if [ $$$$CNT -gt 0 ]; then echo $$$$RET; rm -f $$@ exit 1; fi +endef + +$(eval $(call make-dll, Tizen.Network.IoTConnectivity, csapi-tizen)) + +clean: + @rm -f $(ASM_DLLS) diff --git a/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Client.cs b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Client.cs new file mode 100755 index 0000000..a1195ad --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Client.cs @@ -0,0 +1,224 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class IoTConnectivity + { + internal static partial class Client + { + internal static partial class DeviceInformation + { + internal delegate void DeviceInformationCallback(IntPtr deviceInfoHandle, int result, IntPtr userData); + + internal enum Property + { + Name = 0, + SpecVersion, + Id, + DataModelVersion, + } + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_get_device_info")] + internal static extern int Get(string hostAddress, int connectivityType, DeviceInformationCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_device_info_get_property")] + internal static extern int GetProperty(IntPtr deviceInfoHandle, int property, out string value); + } + + internal static partial class PlatformInformation + { + internal delegate void PlatformInformationCallback(IntPtr platformInfoHandle, int result, IntPtr userData); + + internal enum Propery + { + Id = 0, + MfgName, + MfgUrl, + ModelNumber, + DateOfMfg, + PlatformVer, + OsVer, + HardwareVer, + FirmwareVer, + SupportUrl, + SystemTime + } + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_get_platform_info")] + internal static extern int Get(string hostAddress, int connectivityType, PlatformInformationCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_platform_info_get_property")] + internal static extern int GetProperty(IntPtr platformInfoHandle, int property, out string value); + } + + internal static partial class RemoteResource + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ResponseCallback(IntPtr resource, int err, int requestType, IntPtr response, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ObserveCallback(IntPtr resource, int err, int sequenceNumber, IntPtr response, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CachedRepresentationChangedCallback(IntPtr resource, IntPtr representation, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void StateChangedCallback(IntPtr resource, int state, IntPtr userData); + + internal enum ConnectivityType + { + None = 0, + Ipv4, + Ipv6 + } + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_create")] + internal static extern int Create(string hostAddress, int connectivityType, string uriPath, int properties, IntPtr resourceTypes, IntPtr resourceInterfaces, out IntPtr remoteResource); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_destroy")] + internal static extern void Destroy(IntPtr resource); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_clone")] + internal static extern int Clone(IntPtr src, out IntPtr dest); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_observe_register")] + internal static extern int RegisterObserve(IntPtr resource, int observePolicy, IntPtr query, ObserveCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_observe_deregister")] + internal static extern int DeregisterObserve(IntPtr resource); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get")] + internal static extern int Get(IntPtr resource, IntPtr query, ResponseCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_put")] + internal static extern int Put(IntPtr resource, IntPtr repr, IntPtr query, ResponseCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_post")] + internal static extern int Post(IntPtr resource, IntPtr repr, IntPtr query, ResponseCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_delete")] + internal static extern int Delete(IntPtr resource, ResponseCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_start_caching")] + internal static extern int StartCaching(IntPtr resource, CachedRepresentationChangedCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_stop_caching")] + internal static extern int StopCaching(IntPtr resource); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_start_monitoring")] + internal static extern int StartMonitoring(IntPtr resource, StateChangedCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_stop_monitoring")] + internal static extern int StopMonitoring(IntPtr resource); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_uriPath")] + internal static extern int GetUriPath(IntPtr resource, out string uriPath); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_connectivity_type")] + internal static extern int GetConnectivityType(IntPtr resource, out int connectivityType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_host_address")] + internal static extern int GetHostAddress(IntPtr resource, out string hostAddress); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_device_id")] + internal static extern int GetDeviceId(IntPtr resource, out string deviceId); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_types")] + internal static extern int GetTypes(IntPtr resource, out IntPtr types); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_interfaces")] + internal static extern int GetInterfaces(IntPtr resource, out IntPtr ifaces); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_properties")] + internal static extern int GetProperties(IntPtr resource, out int properties); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_options")] + internal static extern int GetOptions(IntPtr resource, out IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_set_options")] + internal static extern int SetOptions(IntPtr resource, IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_cached_representation")] + internal static extern int GetCachedRepresentation(IntPtr resource, out IntPtr representation); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_get_time_interval")] + internal static extern int GetTimeInterval(IntPtr resource, out int timeInterval); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remote_resource_set_time_interval")] + internal static extern int SetTimeInterval(IntPtr resource, int timeInterval); + } + + internal static partial class IoTCon + { + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_initialize")] + internal static extern int Initialize(); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_deinitialize")] + internal static extern void Deinitialize(); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_get_timeout")] + internal static extern int GetTimeout(out int timeoutSeconds); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_set_timeout")] + internal static extern int SetTimeout(int timeoutSeconds); + } + + internal static partial class ResourceFinder + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FoundResourceCallback(IntPtr remoteResourceHandle, int result, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_find_resource")] + internal static extern int AddResourceFoundCb(string hostAddress, int connectivityType, string resourceType, bool isSecure, FoundResourceCallback cb, IntPtr userData); + } + + internal static partial class Presence + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void PresenceCallback(IntPtr presenceResponseHandle, int err, IntPtr response, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_add_presence_cb")] + internal static extern int AddPresenceCb(string hostAddress, int connectivityType, string resourceType, PresenceCallback cb, IntPtr userData, out IntPtr presenceHandle); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_remove_presence_cb")] + internal static extern int RemovePresenceCb(IntPtr presenceHandle); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_get_host_address")] + internal static extern int GetHostAddress(IntPtr presence, out string hostAddress); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_get_connectivity_type")] + internal static extern int GetConnectivityType(IntPtr presence, out int connectivityType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_get_resource_type")] + internal static extern int GetResourceType(IntPtr presence, out string resourceType); + } + + internal static partial class PresenceResponse + { + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_response_get_result")] + internal static extern int GetResult(IntPtr response, out int result); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_response_get_trigger")] + internal static extern int GetTrigger(IntPtr response, out int trigger); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_response_get_host_address")] + internal static extern int GetHostAddress(IntPtr response, out string hostAddress); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_response_get_connectivity_type")] + internal static extern int GetConnectivityType(IntPtr response, out int connectivityType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_presence_response_get_resource_type")] + internal static extern int GetResourceType(IntPtr response, out string resourceType); + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Common.cs b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Common.cs new file mode 100755 index 0000000..63b6447 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Common.cs @@ -0,0 +1,360 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class IoTConnectivity + { + internal static partial class Common + { + internal enum DataType + { + None = 0, + Int, + Bool, + Double, + String, + ByteStr, + Null, + List, + State + } + + internal static partial class ResourceTypes + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ForeachCallback(string type, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_types_create")] + internal static extern int Create(out IntPtr types); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_types_destroy")] + internal static extern void Destroy(IntPtr types); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_types_add")] + internal static extern int Add(IntPtr types, string type); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_types_remove")] + internal static extern int Remove(IntPtr types, string type); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_types_foreach")] + internal static extern int Foreach(IntPtr types, ForeachCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_types_clone")] + internal static extern int Clone(IntPtr src, out IntPtr dest); + } + + internal static partial class ResourceInterfaces + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ForeachCallback(string iface, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_interfaces_create")] + internal static extern int Create(out IntPtr ifaces); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_interfaces_destroy")] + internal static extern void Destroy(IntPtr ifaces); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_interfaces_add")] + internal static extern int Add(IntPtr ifaces, string iface); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_interfaces_remove")] + internal static extern int Remove(IntPtr ifaces, string iface); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_interfaces_foreach")] + internal static extern int Foreach(IntPtr ifaces, ForeachCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_interfaces_clone")] + internal static extern int Clone(IntPtr src, out IntPtr dest); + } + + internal static partial class State + { + internal delegate bool StateCallback(IntPtr state, string key, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_create")] + internal static extern int Create(out IntPtr state); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_destroy")] + internal static extern void Destroy(IntPtr state); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_clone")] + internal static extern int Clone(IntPtr state, out IntPtr dest); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_int")] + internal static extern int AddInt(IntPtr state, string key, int val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_bool")] + internal static extern int AddBool(IntPtr state, string key, bool val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_double")] + internal static extern int AddDouble(IntPtr state, string key, double val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_str")] + internal static extern int AddStr(IntPtr state, string key, string val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_byte_str")] + internal static extern unsafe int AddByteStr(IntPtr state, string key, byte[] val, int len); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_list")] + internal static extern int AddList(IntPtr state, string key, IntPtr list); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_state")] + internal static extern int AddState(IntPtr dest, string key, IntPtr src); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_add_null")] + internal static extern int AddNull(IntPtr state, string key); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_int")] + internal static extern int GetInt(IntPtr state, string key, out int val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_bool")] + internal static extern int GetBool(IntPtr state, string key, out bool val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_double")] + internal static extern int GetDouble(IntPtr state, string key, out double val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_str")] + internal static extern int GetStr(IntPtr state, string key, out string val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_byte_str")] + internal static extern int GetByteStr(IntPtr state, string key, out IntPtr value, out int size); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_list")] + internal static extern int GetList(IntPtr state, string key, out IntPtr list); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_state")] + internal static extern int GetState(IntPtr src, string key, out IntPtr dest); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_is_null")] + internal static extern int IsNull(IntPtr state, string key, out bool isNull); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_remove")] + internal static extern int Remove(IntPtr state, string key); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_type")] + internal static extern int GetType(IntPtr state, string key, out DataType type); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_foreach")] + internal static extern int Foreach(IntPtr state, StateCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_state_get_keys_count")] + internal static extern int GetKeysCount(IntPtr state, out int count); + } + + internal static partial class Query + { + internal delegate bool QueryCallback(string key, string value, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_create")] + internal static extern int Create(out IntPtr query); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_destroy")] + internal static extern void Destroy(IntPtr query); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_get_resource_type")] + internal static extern int GetResourceType(IntPtr query, out string resourceType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_get_interface")] + internal static extern int GetInterface(IntPtr query, out string resourceInterface); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_set_resource_type")] + internal static extern int SetResourceType(IntPtr query, string resourceType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_set_interface")] + internal static extern int SetInterface(IntPtr query, string resourceInterface); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_add")] + internal static extern int Add(IntPtr query, string key, string value); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_remove")] + internal static extern int Remove(IntPtr query, string key); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_lookup")] + internal static extern int Lookup(IntPtr query, string key, out string data); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_query_foreach")] + internal static extern int Foreach(IntPtr query, QueryCallback cb, IntPtr userData); + } + + internal static partial class Representation + { + internal delegate bool RepresentationChildrenCallback(IntPtr child, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_create")] + internal static extern int Create(out IntPtr repr); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_destroy")] + internal static extern void Destroy(IntPtr repr); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_clone")] + internal static extern int Clone(IntPtr src, out IntPtr dest); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_set_uri_path")] + internal static extern int SetUriPath(IntPtr repr, string uriPath); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_get_uri_path")] + internal static extern int GetUriPath(IntPtr repr, out string uriPath); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_set_resource_types")] + internal static extern int SetResourceTypes(IntPtr repr, IntPtr types); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_get_resource_types")] + internal static extern int GetResourceTypes(IntPtr repr, out IntPtr types); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_set_resource_interfaces")] + internal static extern int SetResourceInterfaces(IntPtr repr, IntPtr ifaces); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_get_resource_interfaces")] + internal static extern int GetResourceInterfaces(IntPtr repr, out IntPtr ifaces); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_set_state")] + internal static extern int SetState(IntPtr repr, IntPtr state); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_get_state")] + internal static extern int GetState(IntPtr repr, out IntPtr state); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_add_child")] + internal static extern int AddChild(IntPtr parent, IntPtr child); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_remove_child")] + internal static extern int RemoveChild(IntPtr parent, IntPtr child); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_foreach_children")] + internal static extern int ForeachChildren(IntPtr parent, RepresentationChildrenCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_get_children_count")] + internal static extern int GetChildrenCount(IntPtr parent, out int count); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_representation_get_nth_child")] + internal static extern int GetNthChild(IntPtr parent, int pos, out IntPtr child); + } + + internal static partial class Options + { + internal delegate bool OptionsCallback(ushort id, string data, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_options_create")] + internal static extern int Create(out IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_options_destroy")] + internal static extern void Destroy(IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_options_add")] + internal static extern int Add(IntPtr options, ushort id, string data); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_options_remove")] + internal static extern int Remove(IntPtr options, ushort id); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_options_lookup")] + internal static extern int Lookup(IntPtr options, ushort id, out string data); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_options_foreach")] + internal static extern int ForEach(IntPtr options, OptionsCallback cb, IntPtr userData); + } + + internal static partial class List + { + internal delegate bool IntCallback(int pos, int value, IntPtr userData); + + internal delegate bool BoolCallback(int pos, bool value, IntPtr userData); + + internal delegate bool DoubleCallback(int pos, double value, IntPtr userData); + + internal delegate bool ByteStrCallback(int pos, byte[] value, int len, IntPtr userData); + + internal delegate bool StrCallback(int pos, string value, IntPtr userData); + + internal delegate bool ListCallback(int pos, IntPtr value, IntPtr userData); + + internal delegate bool StateCallback(int pos, IntPtr value, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_create")] + internal static extern int Create(DataType type, out IntPtr list); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_destroy")] + internal static extern void Destroy(IntPtr list); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_int")] + internal static extern int AddInt(IntPtr list, int val, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_bool")] + internal static extern int AddBool(IntPtr list, bool val, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_double")] + internal static extern int AddDouble(IntPtr list, double val, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_str")] + internal static extern int AddStr(IntPtr list, string val, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_byte_str")] + internal static extern int AddByteStr(IntPtr list, byte[] val, int len, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_list")] + internal static extern int AddList(IntPtr list, IntPtr val, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_add_state")] + internal static extern int AddState(IntPtr list, IntPtr val, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_int")] + internal static extern int GetNthInt(IntPtr list, int pos, out int val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_bool")] + internal static extern int GetNthBool(IntPtr list, int pos, out bool val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_double")] + internal static extern int GetNthDouble(IntPtr list, int pos, out double val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_str")] + internal static extern int GetNthStr(IntPtr list, int pos, out string val); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_byte_str")] + internal static extern int GetNthByteStr(IntPtr list, int pos, out string val, out int len); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_list")] + internal static extern int GetNthList(IntPtr src, int pos, out IntPtr dest); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_nth_state")] + internal static extern int GetNthState(IntPtr list, int pos, out IntPtr state); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_remove_nth")] + internal static extern int RemoveNth(IntPtr list, int pos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_type")] + internal static extern int GetType(IntPtr list, out int type); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_get_length")] + internal static extern int GetLength(IntPtr list, out int length); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_int")] + internal static extern int ForeachInt(IntPtr list, IntCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_bool")] + internal static extern int ForeachBool(IntPtr list, BoolCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_double")] + internal static extern int ForeachDouble(IntPtr list, DoubleCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_byte_str")] + internal static extern int ForeachByteStr(IntPtr list, ByteStrCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_str")] + internal static extern int ForeachStr(IntPtr list, StrCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_list")] + internal static extern int ForeachList(IntPtr list, ListCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_list_foreach_state")] + internal static extern int ForeachState(IntPtr list, StateCallback cb, IntPtr userData); + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs new file mode 100755 index 0000000..8a85141 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Interop/Interop.IoTConnectivity.Server.cs @@ -0,0 +1,156 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class IoTConnectivity + { + internal static partial class Server + { + internal enum RequestType + { + Unknown = 0, + Get = 1, + Put = 2, + Post = 3, + Delete = 4, + } + + internal static partial class IoTCon + { + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_start_presence")] + internal static extern int StartPresence(uint time); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_stop_presence")] + internal static extern int StopPresence(); + } + + internal static partial class Resource + { + internal delegate void RequestHandlerCallback(IntPtr resource, IntPtr request, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_create")] + internal static extern int Create(string uriPath, IntPtr resTypes, IntPtr ifaces, int properties, RequestHandlerCallback cb, IntPtr userData, out IntPtr resourceHandle); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_destroy")] + internal static extern int Destroy(IntPtr resourceHandle); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_bind_interface")] + internal static extern int BindInterface(IntPtr resource, string iface); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_bind_type")] + internal static extern int BindType(IntPtr resourceHandle, string resourceType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_set_request_handler")] + internal static extern int SetRequestHandler(IntPtr resource, RequestHandlerCallback cb, IntPtr userData); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_bind_child_resource")] + internal static extern int BindChildResource(IntPtr parent, IntPtr child); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_unbind_child_resource")] + internal static extern int UnbindChildResource(IntPtr parent, IntPtr child); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_notify")] + internal static extern int Notify(IntPtr resource, IntPtr repr, IntPtr observers, int qos); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_get_number_of_children")] + internal static extern int GetNumberOfChildren(IntPtr resource, out int number); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_get_nth_child")] + internal static extern int GetNthChild(IntPtr parent, int index, out IntPtr child); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_get_uri_path")] + internal static extern int GetUriPath(IntPtr resource, out string uriPpath); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_get_types")] + internal static extern int GetTypes(IntPtr resource, out IntPtr types); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_get_interfaces")] + internal static extern int GetInterfaces(IntPtr resource, out IntPtr ifaces); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_resource_get_properties")] + internal static extern int GetProperties(IntPtr resource, out int properties); + } + + internal static partial class Request + { + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_host_address")] + internal static extern int GetHostAddress(IntPtr request, out string hostAddress); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_connectivity_type")] + internal static extern int GetConnectivityType(IntPtr request, out int connectivityType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_representation")] + internal static extern int GetRepresentation(IntPtr request, out IntPtr repr); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_request_type")] + internal static extern int GetRequestType(IntPtr request, out int type); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_options")] + internal static extern int GetOptions(IntPtr request, out IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_query")] + internal static extern int GetQuery(IntPtr request, out IntPtr query); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_observe_type")] + internal static extern int GetObserveType(IntPtr request, out int observeType); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_request_get_observe_id")] + internal static extern int GetObserveId(IntPtr request, out int observeId); + } + + internal static partial class Response + { + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_create")] + internal static extern int Create(IntPtr request, out IntPtr response); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_destroy")] + internal static extern void Destroy(IntPtr resp); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_get_options")] + internal static extern int GetOptions(IntPtr resp, out IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_get_representation")] + internal static extern int GetRepresentation(IntPtr resp, out IntPtr repr); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_get_result")] + internal static extern int GetResult(IntPtr resp, out int result); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_set_result")] + internal static extern int SetResult(IntPtr resp, int result); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_set_representation")] + internal static extern int SetRepresentation(IntPtr resp, string iface, IntPtr repr); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_set_options")] + internal static extern int SetOptions(IntPtr resp, IntPtr options); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_response_send")] + internal static extern int Send(IntPtr resp); + } + + internal static partial class Observers + { + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_observers_create")] + internal static extern int Create(out IntPtr observers); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_observers_destroy")] + internal static extern void Destroy(IntPtr observers); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_observers_add")] + internal static extern int Add(IntPtr observers, int observeId); + + [DllImport(Libraries.IoTCon, EntryPoint = "iotcon_observers_remove")] + internal static extern int Remove(IntPtr observers, int observeId); + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Interop/Interop.Libraries.cs b/Tizen.Network.IoTConnectivity/Interop/Interop.Libraries.cs new file mode 100755 index 0000000..2ce39e9 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Interop/Interop.Libraries.cs @@ -0,0 +1,17 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string IoTCon = "libiotcon.so.0.0.12"; + public const string Glib = "libglib-2.0.so.0"; + public const string Libc = "libc.so.6"; + } +} diff --git a/Tizen.Network.IoTConnectivity/Properties/AssemblyInfo.cs b/Tizen.Network.IoTConnectivity/Properties/AssemblyInfo.cs new file mode 100755 index 0000000..fc4130c --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Properties/AssemblyInfo.cs @@ -0,0 +1,43 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tizen.Network.IoTConnectivity")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tizen.Network.IoTConnectivity")] +[assembly: AssemblyCopyright("Copyright (c) 2016 Samsung Electronics Co., Ltd")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("3577eae8-b409-4bae-a5da-c6340be1dbc2")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.csproj b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.csproj new file mode 100644 index 0000000..95ad115 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.csproj @@ -0,0 +1,106 @@ + + + + + Debug + AnyCPU + {3577EAE8-B409-4BAE-A5DA-C6340BE1DBC2} + Library + Properties + Tizen.Network.IoTConnectivity + Tizen.Network.IoTConnectivity + v4.5 + 512 + 8.0.30703 + 2.0 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + true + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + + + Tizen.Network.IoTConnectivity.snk + + + + + + + + + + + + ..\..\tizen\Tizen.Internals\bin\Debug\Tizen.Internals.dll + + + ..\..\tizen\Tizen\bin\Debug\Tizen.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.snk b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity.snk new file mode 100644 index 0000000000000000000000000000000000000000..2913977b2ce4ba580ebf80f67c23b7b4b25da063 GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097fJ>iMRV8I|b7`0YL&AaW_Y%sUBr>a=` zv9S7y)ME!Hr)!`MNiq^5n#E@m2BGa81ercjV!+Hj42u)K+&jlcOcvr zZc%`2XgK25`n1BeUPx*#f5?u*jB_pi^R6`>TDJw!L&2V7(YCUYG&@HLVGa0Ah)L%b z7QvSBzkKQW#po5)`Jq2T*$iSP!Tq7nOn6)z2tNfw^zW*Rel-PzZQbvgs+2 z*1K=fal(T(2i;^?86{Aq+)_8(l?R{1s8NGK0WhmQW7&B3~uW(5$1!+1vGm ik4JV&xG4DK*>e_Y%?3nn7U2RI@fG^3;>q)HFOf=a{UnM2 literal 0 HcmV?d00001 diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/CacheUpdatedEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/CacheUpdatedEventArgs.cs new file mode 100755 index 0000000..27cb177 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/CacheUpdatedEventArgs.cs @@ -0,0 +1,24 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// CacheUpdatedEventArgs class. This class is an event arguments of the CacheUpdated event. + /// + public class CacheUpdatedEventArgs : EventArgs + { + /// + /// Representation property. + /// + /// Representation Representation. + public Representation Representation { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/DeviceInformationFoundEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/DeviceInformationFoundEventArgs.cs new file mode 100755 index 0000000..7bda503 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/DeviceInformationFoundEventArgs.cs @@ -0,0 +1,41 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// DeviceInformationFoundEventArgs class. This class is an event arguments of the DeviceInformationFound event. + /// + public class DeviceInformationFoundEventArgs + { + /// + /// The request id + /// + public int RequestId { get; internal set; } + + /// + /// Indicates human friendly name for device + /// + public string Name { get; internal set; } + + /// + /// Indicates human friendly name for device + /// + public string SpecVersion { get; internal set; } + + /// + /// Indicates unique identifier for OIC device + /// + public string DeviceId { get; internal set; } + + /// + /// Indicates version of the specs this device data model is implemented to + /// + public string DataModelVersion { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingError.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingError.cs new file mode 100755 index 0000000..edf55ae --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingError.cs @@ -0,0 +1,65 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Enumeration for resource found errors + /// + internal enum FindingError + { + /// + /// I/O error + /// + Io = 1, + /// + /// Out of memory + /// + OutOfMemory, + /// + /// Permission denied + /// + PermissionDenied, + /// + /// Not supported + /// + NotSupported, + /// + /// Invalid parameter + /// + InvalidParameter, + /// + /// No data available + /// + NoData, + /// + /// Time out + /// + TimeOut, + /// + /// IoTivity errors + /// + Iotivity, + /// + /// Representation errors + /// + Representation, + /// + /// Invalid type + /// + InvalidType, + /// + /// Already + /// + Already, + /// + /// System errors + /// + System + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingErrorOccurredEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingErrorOccurredEventArgs.cs new file mode 100755 index 0000000..587f353 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/FindingErrorOccurredEventArgs.cs @@ -0,0 +1,30 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// FoundErrorEventArgs class. This class is an event arguments of the FoundError event. + /// + public class FindingErrorOccurredEventArgs : EventArgs + { + /// + /// RequestId property. + /// + /// int RequestId. + public int RequestId { get; internal set; } + + /// + /// Result property. + /// + /// FindingError Result. + public Exception Error { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityClientManager.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityClientManager.cs new file mode 100755 index 0000000..6b55115 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityClientManager.cs @@ -0,0 +1,589 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections.Generic; + +namespace Tizen.Network.IoTConnectivity +{ + public static class IoTConnectivityClientManager + { + public const string MulticastAddress = null; + + private static int s_presenceListenerId = 1; + private static Dictionary s_presenceCallbacksMap = new Dictionary(); + private static Dictionary s_presenceHandlesMap = new Dictionary(); + + private static int s_requestId = 1; + private static Dictionary s_resourceFoundCallbacksMap = new Dictionary(); + private static Dictionary s_deviceInformationCallbacksMap = new Dictionary(); + private static Dictionary s_platformInformationCallbacksMap = new Dictionary(); + + /// + /// presence event on the resource + /// + public static event EventHandler PresenceReceived; + + /// + /// Resource found event handler + /// + public static event EventHandler ResourceFound; + + /// + /// PlatformInformationFound event handler + /// + public static event EventHandler PlatformInformationFound; + + /// + /// DeviceInformationFound event handler + /// + public static event EventHandler DeviceInformationFound; + + /// + /// FoundError event handler + /// + public static event EventHandler FindingErrorOccurred; + + /// + /// Timeout property + /// + public static int TimeOut + { + get + { + int timeout; + int ret = Interop.IoTConnectivity.Client.IoTCon.GetTimeout(out timeout); + if (ret != (int)IoTConnectivityError.None) + { + Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get timeout"); + return 0; + } + return timeout; + } + set + { + int ret = Interop.IoTConnectivity.Client.IoTCon.SetTimeout(value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set timeout"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// Connects to the iotcon service + /// + public static void Initialize() + { + int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + /// + /// Disconnects from the iotcon service + /// + public static void Deinitialize() + { + Interop.IoTConnectivity.Client.IoTCon.Deinitialize(); + } + + /// + /// Starts receiving presence events + /// + /// + /// PresenceId + /// + public static int StartReceivingPresence(string hostAddress, string resourceType) + { + Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress); + if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address"); + throw new ArgumentException("Unable to parse host address"); + } + + if (resourceType != null && !ResourceTypes.IsValid(resourceType)) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type"); + throw new ArgumentException("Invalid type"); + } + + IntPtr id = IntPtr.Zero; + lock (s_presenceCallbacksMap) + { + id = (IntPtr)s_presenceListenerId++; + } + s_presenceCallbacksMap[id] = (IntPtr presence, int result, IntPtr presenceResponseHandle, IntPtr userData) => + { + if (presenceResponseHandle != IntPtr.Zero) + { + int presenceId = (int)userData; + if (result == 0) + { + PresenceReceivedEventArgs e = GetPresenceReceivedEventArgs(presenceId, presenceResponseHandle); + if (e == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PresenceReceivedEventArgs"); + return; + } + PresenceReceived?.Invoke(null, e); + } + else + { + FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(presenceId, result); + FindingErrorOccurred?.Invoke(null, e); + } + } + }; + + IntPtr presenceHandle; + int errorCode = Interop.IoTConnectivity.Client.Presence.AddPresenceCb(hostAddress, (int)connectivityType, resourceType, s_presenceCallbacksMap[id], id, out presenceHandle); + if (errorCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register presence event handler"); + lock (s_presenceCallbacksMap) + { + s_presenceCallbacksMap.Remove(id); + } + throw IoTConnectivityErrorFactory.GetException(errorCode); + } + + lock (s_presenceHandlesMap) + { + s_presenceHandlesMap[id] = presenceHandle; + } + return (int)id; + } + + /// + /// Stops receiving presence events + /// + public static void StopReceivingPresence(int presenceId) + { + if (s_presenceHandlesMap.ContainsKey((IntPtr)presenceId)) + { + IntPtr presenceHandle = s_presenceHandlesMap[(IntPtr)presenceId]; + int ret = Interop.IoTConnectivity.Client.Presence.RemovePresenceCb(presenceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister presence event handler"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + lock (s_presenceHandlesMap) + { + s_presenceHandlesMap.Remove((IntPtr)presenceId); + } + } + + if (s_presenceCallbacksMap.ContainsKey((IntPtr)presenceId)) + { + lock (s_presenceCallbacksMap) + { + s_presenceCallbacksMap.Remove((IntPtr)presenceId); + } + } + } + + /// + /// Starts finding resource events + /// + /// + /// RequestId + /// + public static int StartFindingResource(string hostAddress, string resourceType, bool isSecure = false) + { + Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress); + if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address"); + throw new ArgumentException("Unable to parse host address"); + } + + if (resourceType != null && !ResourceTypes.IsValid(resourceType)) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type"); + throw new ArgumentException("Invalid type"); + } + + IntPtr id = IntPtr.Zero; + lock (s_resourceFoundCallbacksMap) + { + id = (IntPtr)s_requestId++; + } + s_resourceFoundCallbacksMap[id] = (IntPtr remoteResourceHandle, int result, IntPtr userData) => + { + if (remoteResourceHandle != IntPtr.Zero) + { + int requestId = (int)userData; + if (result == (int)IoTConnectivityError.None) + { + RemoteResource resource = null; + try + { + resource = new RemoteResource(remoteResourceHandle); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't clone RemoteResource's handle: " + exp.Message); + return; + } + ResourceFoundEventArgs e = new ResourceFoundEventArgs() + { + RequestId = requestId, + Resource = resource + }; + ResourceFound?.Invoke(null, e); + } + else + { + FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result); + FindingErrorOccurred?.Invoke(null, e); + + lock (s_resourceFoundCallbacksMap) + { + s_resourceFoundCallbacksMap.Remove(id); + } + } + } + }; + + int errorCode = Interop.IoTConnectivity.Client.ResourceFinder.AddResourceFoundCb(hostAddress, (int)connectivityType, resourceType, isSecure, s_resourceFoundCallbacksMap[id], id); + if (errorCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register resource found event handler"); + lock (s_resourceFoundCallbacksMap) + { + s_resourceFoundCallbacksMap.Remove(id); + } + throw IoTConnectivityErrorFactory.GetException(errorCode); + } + return (int)id; + } + + /// + /// Starts finding device information events + /// + /// + /// RequestId + /// + public static int StartFindingDeviceInformation(string hostAddress) + { + Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress); + if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address"); + throw new ArgumentException("Unable to parse host address"); + } + + IntPtr id = IntPtr.Zero; + lock (s_deviceInformationCallbacksMap) + { + id = (IntPtr)s_requestId++; + } + s_deviceInformationCallbacksMap[id] = (IntPtr deviceInfoHandle, int result, IntPtr userData) => + { + if (deviceInfoHandle != IntPtr.Zero) + { + int requestId = (int)userData; + if (result == 0) + { + DeviceInformationFoundEventArgs e = GetDeviceInformationFoundEventArgs(requestId, deviceInfoHandle); + if (e == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get DeviceInformationFoundEventArgs"); + return; + } + DeviceInformationFound?.Invoke(null, e); + } + else + { + FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result); + FindingErrorOccurred?.Invoke(null, e); + + lock (s_deviceInformationCallbacksMap) + { + s_deviceInformationCallbacksMap.Remove(id); + } + } + } + }; + + int errorCode = Interop.IoTConnectivity.Client.DeviceInformation.Get(hostAddress, (int)connectivityType, s_deviceInformationCallbacksMap[id], id); + if (errorCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device information"); + lock (s_deviceInformationCallbacksMap) + { + s_deviceInformationCallbacksMap.Remove(id); + } + throw IoTConnectivityErrorFactory.GetException(errorCode); + } + + return (int)id; + } + + /// + /// Starts finding platform information events + /// + /// + /// RequestId + /// + public static int StartFindingPlatformInformation(string hostAddress) + { + Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = RemoteResource.GetConnectivityType(hostAddress); + if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address"); + throw new ArgumentException("Unable to parse host address"); + } + + IntPtr id = IntPtr.Zero; + lock (s_platformInformationCallbacksMap) + { + id = (IntPtr)s_requestId++; + } + s_platformInformationCallbacksMap[id] = (IntPtr platformInfoHandle, int result, IntPtr userData) => + { + if (platformInfoHandle != IntPtr.Zero) + { + int requestId = (int)userData; + if (result == 0) + { + PlatformInformationFoundEventArgs e = GetPlatformInformationFoundEventArgs(requestId, platformInfoHandle); + if (e == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Can't get PlatformInformationFoundEventArgs"); + return; + } + PlatformInformationFound?.Invoke(null, e); + } + else + { + FindingErrorOccurredEventArgs e = GetFindingErrorOccurredEventArgs(requestId, result); + FindingErrorOccurred?.Invoke(null, e); + + lock (s_platformInformationCallbacksMap) + { + s_platformInformationCallbacksMap.Remove(id); + } + } + } + }; + + int errorCode = Interop.IoTConnectivity.Client.PlatformInformation.Get(hostAddress, (int)connectivityType, s_platformInformationCallbacksMap[id], id); + if (errorCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform information"); + lock (s_platformInformationCallbacksMap) + { + s_platformInformationCallbacksMap.Remove(id); + } + throw IoTConnectivityErrorFactory.GetException(errorCode); + } + + return (int)id; + } + + private static PresenceReceivedEventArgs GetPresenceReceivedEventArgs(int presenceId, IntPtr presenceResponseHandle) + { + int trigger; + string host, type; + + int ret = Interop.IoTConnectivity.Client.PresenceResponse.GetHostAddress(presenceResponseHandle, out host); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get host address"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PresenceResponse.GetResourceType(presenceResponseHandle, out type); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource type"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PresenceResponse.GetTrigger(presenceResponseHandle, out trigger); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get event type"); + return null; + } + + PresenceReceivedEventArgs e = new PresenceReceivedEventArgs() + { + PresenceId = presenceId, + HostAddress = host, + Type = type, + EventType = (PresenceEventType)trigger + }; + + return e; + } + + private static DeviceInformationFoundEventArgs GetDeviceInformationFoundEventArgs(int requestId, IntPtr deviceInfoHandle) + { + string name, specVersion, deviceId, dataModelVersion; + + int ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Name, out name); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get name"); + return null; + } + + ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.SpecVersion, out specVersion); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get spec version"); + return null; + } + + ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.Id, out deviceId); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id"); + return null; + } + + ret = Interop.IoTConnectivity.Client.DeviceInformation.GetProperty(deviceInfoHandle, (int)Interop.IoTConnectivity.Client.DeviceInformation.Property.DataModelVersion, out dataModelVersion); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get data model version"); + return null; + } + + DeviceInformationFoundEventArgs e = new DeviceInformationFoundEventArgs() + { + RequestId = requestId, + Name = name, + SpecVersion = specVersion, + DeviceId = deviceId, + DataModelVersion = dataModelVersion + }; + + return e; + } + + private static PlatformInformationFoundEventArgs GetPlatformInformationFoundEventArgs(int requestId, IntPtr platformInfoHandle) + { + string platformId, manufacturerName, manufacturerUrl, modelNumber, dateOfManufacture, platformVersion, osVersion, hardwareVersion, firmwareVersion, supportUrl, systemTime; + + int ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.Id, out platformId); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform id"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgName, out manufacturerName); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer name"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.MfgUrl, out manufacturerUrl); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get manufacturer url"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.ModelNumber, out modelNumber); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get model number"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.DateOfMfg, out dateOfManufacture); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get date of manufacture"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.PlatformVer, out platformVersion); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get platform version"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.OsVer, out osVersion); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to os version"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.HardwareVer, out hardwareVersion); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to hardware version"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.FirmwareVer, out firmwareVersion); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get firmware version"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SupportUrl, out supportUrl); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get support url"); + return null; + } + + ret = Interop.IoTConnectivity.Client.PlatformInformation.GetProperty(platformInfoHandle, (int)Interop.IoTConnectivity.Client.PlatformInformation.Propery.SystemTime, out systemTime); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get system time"); + return null; + } + + PlatformInformationFoundEventArgs e = new PlatformInformationFoundEventArgs() + { + RequestId = requestId, + PlatformId = platformId, + ManufacturerName = manufacturerName, + ManufacturerURL = manufacturerUrl, + DateOfManufacture = dateOfManufacture, + ModelNumber = modelNumber, + PlatformVersion = platformVersion, + OsVersion = osVersion, + HardwareVersion = hardwareVersion, + FirmwareVersion = firmwareVersion, + SupportUrl = supportUrl, + SystemTime = systemTime + }; + + return e; + } + + private static FindingErrorOccurredEventArgs GetFindingErrorOccurredEventArgs(int requestId, int err) + { + FindingErrorOccurredEventArgs e = new FindingErrorOccurredEventArgs() + { + RequestId = requestId, + Error = IoTConnectivityErrorFactory.GetException(err) + }; + return e; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityErrorFactory.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityErrorFactory.cs new file mode 100755 index 0000000..dc29628 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityErrorFactory.cs @@ -0,0 +1,137 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using Tizen.Internals.Errors; + +namespace Tizen.Network.IoTConnectivity +{ + internal enum IoTConnectivityError + { + None = ErrorCode.None, + InvalidParameter = ErrorCode.InvalidParameter, + OutOfMemory = ErrorCode.OutOfMemory, + Io = ErrorCode.IoError, + PermissionDenied = ErrorCode.PermissionDenied, + NotSupported = ErrorCode.NotSupported, + NoData = ErrorCode.NoData, + TimedOut = ErrorCode.TimedOut, + // TODO: Fix IoTivity error mask + Representation = 0x10700000 | 0x01, + InvalidType = 0x10700000 | 0x02, + Already = 0x10700000 | 0x03, + System = 0x10700000 | 0x04, + } + + internal static class IoTConnectivityErrorFactory + { + internal const string LogTag = "Tizen.Network.IoTConnectivity"; + + internal static void ThrowException(int err) + { + IoTConnectivityError error = (IoTConnectivityError)err; + if (error == IoTConnectivityError.OutOfMemory) + { + throw new InvalidOperationException("Out of memory"); + } + else if (error == IoTConnectivityError.InvalidParameter) + { + throw new ArgumentException("Invalid parameter"); + } + else if (error == IoTConnectivityError.Io) + { + throw new ArgumentException("I/O Error"); + } + else if (error == IoTConnectivityError.NoData) + { + throw new ArgumentException("No data found"); + } + else if (error == IoTConnectivityError.TimedOut) + { + throw new ArgumentException("timed out"); + } + else if (error == IoTConnectivityError.PermissionDenied) + { + throw new ArgumentException("Permission denied"); + } + else if (error == IoTConnectivityError.NotSupported) + { + throw new ArgumentException("Not supported"); + } + else if (error == IoTConnectivityError.Representation) + { + throw new ArgumentException("Representation error"); + } + else if (error == IoTConnectivityError.InvalidType) + { + throw new ArgumentException("Invalid type"); + } + else if (error == IoTConnectivityError.Already) + { + throw new ArgumentException("Duplicate"); + } + else if (error == IoTConnectivityError.System) + { + throw new InvalidOperationException("System error"); + } + } + + internal static Exception GetException(int err) + { + IoTConnectivityError error = (IoTConnectivityError)err; + if (error == IoTConnectivityError.OutOfMemory) + { + return new InvalidOperationException("Out of memory"); + } + else if (error == IoTConnectivityError.InvalidParameter) + { + return new ArgumentException("Invalid parameter"); + } + else if (error == IoTConnectivityError.Io) + { + return new ArgumentException("I/O Error"); + } + else if (error == IoTConnectivityError.NoData) + { + return new ArgumentException("No data found"); + } + else if (error == IoTConnectivityError.TimedOut) + { + return new ArgumentException("timed out"); + } + else if (error == IoTConnectivityError.PermissionDenied) + { + return new ArgumentException("Permission denied"); + } + else if (error == IoTConnectivityError.NotSupported) + { + return new ArgumentException("Not supported"); + } + else if (error == IoTConnectivityError.Representation) + { + return new ArgumentException("Representation error"); + } + else if (error == IoTConnectivityError.InvalidType) + { + return new ArgumentException("Invalid type"); + } + else if (error == IoTConnectivityError.Already) + { + return new ArgumentException("Duplicate"); + } + else if (error == IoTConnectivityError.System) + { + return new InvalidOperationException("System error"); + } + else + { + return new InvalidOperationException("Invalid operation"); + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs new file mode 100755 index 0000000..11f3320 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/IoTConnectivityServerManager.cs @@ -0,0 +1,85 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// IoT connectivity server manager. + /// + public static class IoTConnectivityServerManager + { + /// + /// Initializes connection with IoTCon service + /// + public static void Initialize() + { + int ret = Interop.IoTConnectivity.Client.IoTCon.Initialize(); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to initialize"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + /// + /// Disconnects with IoTCon service + /// + public static void Deinitialize() + { + Interop.IoTConnectivity.Client.IoTCon.Deinitialize(); + } + + /// + /// Registers a resource to be available from IoTCon server + /// + /// The resource to register + public static void RegisterResource(Resource resource) + { + IntPtr handle = IntPtr.Zero; + int ret = Interop.IoTConnectivity.Server.Resource.Create(resource.UriPath, resource.Types._resourceTypeHandle, resource.Interfaces.ResourceInterfacesHandle, (int)resource.Policy, resource.OnRequest, IntPtr.Zero, out handle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed create resource"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + else + { + resource.ResourceHandle = handle; + } + } + + /// + /// Starts presence of a server + /// + /// The interval of announcing presence in seconds. + 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. + /// + 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); + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/LiteResource.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/LiteResource.cs new file mode 100755 index 0000000..e55b823 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/LiteResource.cs @@ -0,0 +1,130 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System.Collections.Generic; + +namespace Tizen.Network.IoTConnectivity +{ + public class LiteResource : Resource + { + /// + /// Constructor + /// + /// The uri path of the lite resource + /// Resource type + /// Policy of the resource + /// Optional State of the resource + public LiteResource(string uri, ResourceTypes types, ResourcePolicy policy, State state = null) + : base(uri, types, new ResourceInterfaces(new string[] { ResourceInterfaces.DefaultInterface }), policy) + { + State = state; + } + + /// + /// The state of the lite resource + /// + public State State { get; set; } + + /// + /// The method to accept post request + /// + /// The new state of the lite resource + /// true to accept post request, false to reject it + public virtual bool OnPost(State state) + { + return true; + } + + /// + /// Called on the get event. + /// + /// Request. + public sealed override Response OnGet(Request request) + { + Representation representation = new Representation() + { + UriPath = UriPath, + Interface = Interfaces, + Type = Types, + State = State + }; + + Response response = new Response() + { + Representation = representation, + Result = ResponseCode.Ok + }; + + return response; + } + + /// + /// Called on the put event. + /// + /// Request. + public sealed override Response OnPut(Request request) + { + Response response = new Response(); + response.Result = ResponseCode.Forbidden; + return response; + } + + /// + /// Called on the post event. + /// + /// Request. + public sealed override Response OnPost(Request request) + { + if (OnPost(request.Representation.State)) + { + State = request.Representation.State; + Representation representation = new Representation() { + UriPath = UriPath, + Interface = Interfaces, + Type = Types, + State = State + }; + + Response response = new Response() { + Representation = representation, + Result = ResponseCode.Ok + }; + + Notify(representation, QualityOfService.High); + return response; + } + + return new Response() + { + Result = ResponseCode.Error + }; + } + + /// + /// Called on the delete event. + /// + /// Request. + public sealed override Response OnDelete(Request request) + { + Response response = new Response(); + response.Result = ResponseCode.Forbidden; + return response; + } + + /// + /// Called on the observing event. + /// + /// Request. + /// Observer type + /// Observe identifier. + public sealed override bool OnObserving(Request request, ObserveType observeType, int observeId) + { + return true; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObservePolicy.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObservePolicy.cs new file mode 100755 index 0000000..0d013db --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObservePolicy.cs @@ -0,0 +1,25 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Indicates delete method of request + /// + public enum ObservePolicy + { + /// + /// Indicates observation request for most up-to-date notifications only + /// + IgnoreOutOfOrder = 0, + /// + /// Indicates observation request for all notifications including stale notifications + /// + AcceptOutOfOrder + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserveType.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserveType.cs new file mode 100755 index 0000000..6ff7ec0 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserveType.cs @@ -0,0 +1,32 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Observe type. + /// + public enum ObserveType + { + /// + /// No observe action + /// + NoType = 0, + + /// + /// Indicates action of registering observation + /// + Register = 1, + + /// + /// Indicates action of unregistering observation + /// + Deregister = 2, + } +} + diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserverNotifiedEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserverNotifiedEventArgs.cs new file mode 100755 index 0000000..46b1397 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ObserverNotifiedEventArgs.cs @@ -0,0 +1,30 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// ObserverNotifiedEventArgs class. This class is an event arguments of the ObserverNotified event. + /// + public class ObserverNotifiedEventArgs : EventArgs + { + /// + /// Result property. + /// + /// ResponseCode Result. + public ResponseCode Result { get; internal set; } + + /// + /// Representation property. + /// + /// Representation Representation. + public Representation Representation { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PlatformInformationFoundEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PlatformInformationFoundEventArgs.cs new file mode 100755 index 0000000..b085872 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PlatformInformationFoundEventArgs.cs @@ -0,0 +1,76 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// PlatformInformationFoundEventArgs class. This class is an event arguments of the PlatformInformationFound event. + /// + public class PlatformInformationFoundEventArgs + { + /// + /// The request id + /// + public int RequestId { get; internal set; } + + /// + /// The platform identifier + /// + public string PlatformId { get; internal set; } + + /// + /// The name of manufacturer + /// + public string ManufacturerName { get; internal set; } + + /// + /// URL of the manufacturer + /// + public string ManufacturerURL { get; internal set; } + + /// + /// Model number as designated by manufacturer + /// + public string ModelNumber { get; internal set; } + + /// + /// Manugacturing date of the device + /// + public string DateOfManufacture { get; internal set; } + + /// + /// Version of platfrom defined by manufacturer + /// + public string PlatformVersion { get; internal set; } + + /// + /// Version of platfrom resident OS + /// + public string OsVersion { get; internal set; } + + /// + /// Version of platform Hardware + /// + public string HardwareVersion { get; internal set; } + + /// + /// Version of device firmware + /// + public string FirmwareVersion { get; internal set; } + + /// + /// URL that points to support information from manufacturer + /// + public string SupportUrl { get; internal set; } + + /// + /// Reference time of the device + /// + public string SystemTime { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceEventType.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceEventType.cs new file mode 100755 index 0000000..19d2cc4 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceEventType.cs @@ -0,0 +1,29 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Enumeration for presence events. + /// + public enum PresenceEventType + { + /// + /// Indicates for resource creation operation of server + /// + ResourceCreated = 0, + /// + /// Indicates for resource creation operation of server + /// + ResourceUpdated, + /// + /// Indicates for resource destruction operation of server + /// + ResourceDestroyed + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceReceivedEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceReceivedEventArgs.cs new file mode 100755 index 0000000..053b575 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/PresenceReceivedEventArgs.cs @@ -0,0 +1,42 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// PresenceReceivedEventArgs class. This class is an event arguments of the PresenceReceived event. + /// + public class PresenceReceivedEventArgs : EventArgs + { + /// + /// PresenceId property. + /// + /// int PresenceId. + public int PresenceId { get; internal set; } + + /// + /// EventType property. + /// + /// PresenceEventType EventType. + public PresenceEventType EventType { get; internal set; } + + /// + /// HostAddress property. + /// + /// string HostAddress. + public string HostAddress { get; internal set; } + + /// + /// Type property. + /// + /// string Type. + public string Type { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/QualityOfService.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/QualityOfService.cs new file mode 100755 index 0000000..65624e6 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/QualityOfService.cs @@ -0,0 +1,25 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Indicates observation request for all notifications including stale notifications + /// + public enum QualityOfService + { + /// + /// Indicates low quality of service + /// + Low = 0, + /// + /// Indicates high quality of service + /// + High + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResource.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResource.cs new file mode 100755 index 0000000..fcfc6f0 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResource.cs @@ -0,0 +1,728 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections.Generic; +using System.Net; +using System.Threading.Tasks; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// RemoteResource class + /// + public class RemoteResource : IDisposable + { + internal const int TimeOutMax = 3600; + internal IntPtr _remoteResourceHandle = IntPtr.Zero; + + private bool _disposed = false; + private bool _cacheEnabled = false; + private ResourceOptions _options; + + private int _responseCallbackId = 1; + private static Dictionary _responseCallbacksMap = new Dictionary(); + + private Interop.IoTConnectivity.Client.RemoteResource.CachedRepresentationChangedCallback _cacheUpdatedCallback; + private Interop.IoTConnectivity.Client.RemoteResource.StateChangedCallback _stateChangedCallback; + private Interop.IoTConnectivity.Client.RemoteResource.ObserveCallback _observeCallback; + + private EventHandler _stateChangedEventHandler; + + /// + /// Constructor + /// + public RemoteResource(string hostAddress, string uriPath, ResourcePolicy policy, ResourceTypes resourceTypes, ResourceInterfaces resourceInterfaces) + { + if (hostAddress == null || uriPath == null || resourceTypes == null || resourceInterfaces == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid parameters"); + throw new ArgumentException("Invalid parameter"); + } + + HostAddress = hostAddress; + UriPath = uriPath; + Policy = policy; + Types = new List(resourceTypes); + Interfaces = new List(resourceInterfaces); + DeviceId = null; + + CreateRemoteResource(resourceTypes._resourceTypeHandle, resourceInterfaces.ResourceInterfacesHandle); + } + + internal RemoteResource(IntPtr handleToClone) + { + int ret = Interop.IoTConnectivity.Client.RemoteResource.Clone(handleToClone, out _remoteResourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to clone"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + SetRemoteResource(); + } + + ~RemoteResource() + { + Dispose(false); + } + + /// + /// Event that is called to cache resource attribute's + /// + public event EventHandler CacheUpdated; + + /// + /// Observe event on the resource + /// + public event EventHandler ObserverNotified; + + /// + /// Event that is called when remote resource's state are changed + /// + public event EventHandler StateChanged + { + add + { + if (_stateChangedEventHandler == null) + { + RegisterStateChangedEvent(); + } + _stateChangedEventHandler += value; + } + remove + { + _stateChangedEventHandler -= value; + if (_stateChangedEventHandler == null) + { + UnregisterStateChangedEvent(); + } + } + } + + /// + /// The host address of the resource + /// + public string HostAddress { get; private set; } + + /// + /// The URI path of the resource + /// + public string UriPath { get; private set; } + + /// + /// The resource types of the remote resource + /// + public IEnumerable Types { get; private set; } + + /// + /// The interfaces of the resource + /// + public IEnumerable Interfaces { get; private set; } + + /// + /// The policy of the resource + /// + public ResourcePolicy Policy { get; private set; } + + /// + /// The header options of the resource + /// + public ResourceOptions Options + { + get + { + return _options; + } + set + { + _options = value; + if (value != null) + { + int ret = Interop.IoTConnectivity.Client.RemoteResource.SetOptions(_remoteResourceHandle, value._resourceOptionsHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set options"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + } + + /// + /// Cache enabled property + /// + public bool CacheEnabled + { + get + { + return _cacheEnabled; + } + set + { + if (_cacheEnabled != value) + { + _cacheEnabled = value; + HandleCachePolicyChanged(); + } + } + } + + /// + /// Time interval of monitoring and caching API + /// + public int TimeInterval + { + get + { + int interval; + int ret = Interop.IoTConnectivity.Client.RemoteResource.GetTimeInterval(_remoteResourceHandle, out interval); + if (ret != (int)IoTConnectivityError.None) + { + Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get time interval"); + return 0; + } + return interval; + } + set + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (value < TimeOutMax && value > 0) + { + ret = Interop.IoTConnectivity.Client.RemoteResource.SetTimeInterval(_remoteResourceHandle, value); + } + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set time interval"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// The device id of the resource + /// + public string DeviceId { get; private set; } + + /// + /// Gets cached representation of the remote resource + /// + public Representation CachedRepresentation() + { + IntPtr handle; + int ret = Interop.IoTConnectivity.Client.RemoteResource.GetCachedRepresentation(_remoteResourceHandle, out handle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Warn(IoTConnectivityErrorFactory.LogTag, "Failed to get CachedRepresentation"); + return null; + } + + Representation representation = new Representation(handle); + return representation; + } + + /// + /// Registers the observe callback on the resource + /// + /// The type to specify how client wants to observe + /// The ResourceQuery to send to server + public void StartObserving(ObservePolicy policy, ResourceQuery query = null) + { + _observeCallback = (IntPtr resource, int err, int sequenceNumber, IntPtr response, IntPtr userData) => + { + int result; + IntPtr representationHandle; + int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result"); + return; + } + + ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation"); + return; + } + + Representation repr = null; + try + { + repr = new Representation(representationHandle); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new representation: " + exp.Message); + return; + } + + ObserverNotifiedEventArgs e = new ObserverNotifiedEventArgs() + { + Representation = repr, + Result = (ResponseCode)result + }; + ObserverNotified?.Invoke(null, e); + }; + + IntPtr queryHandle = IntPtr.Zero; + if (query != null) + { + queryHandle = query._resourceQueryHandle; + } + + int errCode = Interop.IoTConnectivity.Client.RemoteResource.RegisterObserve(_remoteResourceHandle, (int)policy, queryHandle, _observeCallback, IntPtr.Zero); + if (errCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to register observe callbacks"); + throw IoTConnectivityErrorFactory.GetException(errCode); + } + } + + /// + /// Deregisters the observe callback on the resource + /// + public void StopObserving() + { + int ret = Interop.IoTConnectivity.Client.RemoteResource.DeregisterObserve(_remoteResourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to deregister observe callbacks"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + /// + /// Gets the attributes of a resource + /// + /// The ResourceQuery to send to server + /// + public async Task GetAsync(ResourceQuery query = null) + { + TaskCompletionSource tcsRemoteResponse = new TaskCompletionSource(); + + IntPtr id = IntPtr.Zero; + lock (_responseCallbacksMap) + { + id = (IntPtr)_responseCallbackId++; + } + _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) => + { + IntPtr responseCallbackId = userData; + lock(_responseCallbacksMap) + { + _responseCallbacksMap.Remove(responseCallbackId); + } + + if (responseHandle != IntPtr.Zero) + { + try + { + tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle)); + } + catch(Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message); + tcsRemoteResponse.TrySetException(exp); + } + } + else + { + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System)); + } + }; + + IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle; + int errCode = Interop.IoTConnectivity.Client.RemoteResource.Get(_remoteResourceHandle, queryHandle, _responseCallbacksMap[id], id); + if (errCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource attributes"); + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode)); + } + return await tcsRemoteResponse.Task; + } + + /// + /// Puts the representation of a resource, asynchronously. + /// + /// Resource representation + /// The ResourceQuery to send to server + /// + public async Task PutAsync(Representation representation, ResourceQuery query = null) + { + TaskCompletionSource tcsRemoteResponse = new TaskCompletionSource(); + + IntPtr id = IntPtr.Zero; + lock (_responseCallbacksMap) + { + id = (IntPtr)_responseCallbackId++; + } + _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) => + { + IntPtr responseCallbackId = userData; + lock (_responseCallbacksMap) + { + _responseCallbacksMap.Remove(responseCallbackId); + } + + if (responseHandle != IntPtr.Zero) + { + try + { + tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle)); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message); + tcsRemoteResponse.TrySetException(exp); + } + } + else + { + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System)); + } + }; + + IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle; + int errCode = Interop.IoTConnectivity.Client.RemoteResource.Put(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id); + if (errCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to put resource representation"); + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode)); + } + return await tcsRemoteResponse.Task; + } + + /// + /// Post request on a resource + /// + /// Resource representation + /// The ResourceQuery to send to server + /// + public async Task PostAsync(Representation representation, ResourceQuery query = null) + { + TaskCompletionSource tcsRemoteResponse = new TaskCompletionSource(); + + IntPtr id = IntPtr.Zero; + lock (_responseCallbacksMap) + { + id = (IntPtr)_responseCallbackId++; + } + _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) => + { + IntPtr responseCallbackId = userData; + lock (_responseCallbacksMap) + { + _responseCallbacksMap.Remove(responseCallbackId); + } + + if (responseHandle != IntPtr.Zero) + { + try + { + tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle)); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message); + tcsRemoteResponse.TrySetException(exp); + } + } + else + { + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System)); + } + }; + + IntPtr queryHandle = (query == null) ? IntPtr.Zero : query._resourceQueryHandle; + int errCode = Interop.IoTConnectivity.Client.RemoteResource.Post(_remoteResourceHandle, representation._representationHandle, queryHandle, _responseCallbacksMap[id], id); + if (errCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to post request"); + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode)); + } + return await tcsRemoteResponse.Task; + } + + /// + /// Delete the resource + /// + /// + public async Task DeleteAsync() + { + TaskCompletionSource tcsRemoteResponse = new TaskCompletionSource(); + + IntPtr id = IntPtr.Zero; + lock (_responseCallbacksMap) + { + id = (IntPtr)_responseCallbackId++; + } + _responseCallbacksMap[id] = (IntPtr resource, int err, int requestType, IntPtr responseHandle, IntPtr userData) => + { + IntPtr responseCallbackId = userData; + lock (_responseCallbacksMap) + { + _responseCallbacksMap.Remove(responseCallbackId); + } + + if (responseHandle != IntPtr.Zero) + { + try + { + tcsRemoteResponse.TrySetResult(GetRemoteResponse(responseHandle)); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get RemoteResponse: ", exp.Message); + tcsRemoteResponse.TrySetException(exp); + } + } + else + { + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.System)); + } + }; + + int errCode = Interop.IoTConnectivity.Client.RemoteResource.Delete(_remoteResourceHandle, _responseCallbacksMap[id], id); + if (errCode != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to delete"); + tcsRemoteResponse.TrySetException(IoTConnectivityErrorFactory.GetException(errCode)); + } + return await tcsRemoteResponse.Task; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + internal static Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType GetConnectivityType(string hostAddress) + { + Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None; + + if (hostAddress == IoTConnectivityClientManager.MulticastAddress) + { + type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4; + } + else + { + IPAddress address; + if (IPAddress.TryParse(hostAddress, out address)) + { + switch (address.AddressFamily) + { + case System.Net.Sockets.AddressFamily.InterNetwork: + type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv4; + break; + case System.Net.Sockets.AddressFamily.InterNetworkV6: + type = Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.Ipv6; + break; + default: + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to parse for Ipv4 or Ipv6"); + break; + } + } + } + return type; + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + } + + Interop.IoTConnectivity.Client.RemoteResource.Destroy(_remoteResourceHandle); + _disposed = true; + } + + private void HandleCachePolicyChanged() + { + if (_cacheEnabled) + { + _cacheUpdatedCallback = (IntPtr resource, IntPtr representation, IntPtr userData) => + { + if (CacheEnabled) + { + Representation repr = null; + try + { + repr = new Representation(representation); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new Representation: " + exp.Message); + return; + } + + CacheUpdatedEventArgs e = new CacheUpdatedEventArgs() + { + Representation = repr + }; + CacheUpdated?.Invoke(null, e); + } + }; + + int ret = Interop.IoTConnectivity.Client.RemoteResource.StartCaching(_remoteResourceHandle, _cacheUpdatedCallback, IntPtr.Zero); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add cache updated event handler"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else + { + int ret = Interop.IoTConnectivity.Client.RemoteResource.StopCaching(_remoteResourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove cache updated event handler"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + private void RegisterStateChangedEvent() + { + _stateChangedCallback = (IntPtr resource, int state, IntPtr userData) => + { + StateChangedEventArgs e = new StateChangedEventArgs() + { + State = (ResourceState)state + }; + _stateChangedEventHandler?.Invoke(null, e); + }; + + int ret = Interop.IoTConnectivity.Client.RemoteResource.StartMonitoring(_remoteResourceHandle, _stateChangedCallback, IntPtr.Zero); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state changed event handler"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + private void UnregisterStateChangedEvent() + { + int ret = Interop.IoTConnectivity.Client.RemoteResource.StopMonitoring(_remoteResourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state changed event handler"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + private void CreateRemoteResource(IntPtr resourceTypeHandle, IntPtr resourceInterfaceHandle) + { + Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType connectivityType = GetConnectivityType(HostAddress); + if (connectivityType == Interop.IoTConnectivity.Client.RemoteResource.ConnectivityType.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Unable to parse host address"); + throw new ArgumentException("Unable to parse host address"); + } + int ret = Interop.IoTConnectivity.Client.RemoteResource.Create(HostAddress, (int)connectivityType, UriPath, (int)Policy, resourceTypeHandle, resourceInterfaceHandle, out _remoteResourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get remote resource"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + private void SetRemoteResource() + { + string hostAddress, uriPath; + int ret = Interop.IoTConnectivity.Client.RemoteResource.GetHostAddress(_remoteResourceHandle, out hostAddress); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get host address"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + ret = Interop.IoTConnectivity.Client.RemoteResource.GetUriPath(_remoteResourceHandle, out uriPath); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + int policy = (int)ResourcePolicy.NoProperty; + ret = Interop.IoTConnectivity.Client.RemoteResource.GetProperties(_remoteResourceHandle, out policy); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Faled to get uri path"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + IntPtr typesHandle, interfacesHandle; + ret = Interop.IoTConnectivity.Client.RemoteResource.GetTypes(_remoteResourceHandle, out typesHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource types"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + ret = Interop.IoTConnectivity.Client.RemoteResource.GetInterfaces(_remoteResourceHandle, out interfacesHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get resource interfaces"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + string deviceId; + ret = Interop.IoTConnectivity.Client.RemoteResource.GetDeviceId(_remoteResourceHandle, out deviceId); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get device id"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + DeviceId = deviceId; + HostAddress = hostAddress; + UriPath = uriPath; + Types = new ResourceTypes(typesHandle); + Interfaces = new ResourceInterfaces(interfacesHandle); + Policy = (ResourcePolicy)policy; + } + + private RemoteResponse GetRemoteResponse(IntPtr response) + { + int result; + IntPtr representationHandle, optionsHandle; + int ret = Interop.IoTConnectivity.Server.Response.GetResult(response, out result); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get result"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + ret = Interop.IoTConnectivity.Server.Response.GetRepresentation(response, out representationHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get representation"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + ret = Interop.IoTConnectivity.Server.Response.GetOptions(response, out optionsHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get options"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + return new RemoteResponse() + { + Result = (ResponseCode)result, + Representation = new Representation(representationHandle), + Options = new ResourceOptions(optionsHandle) + }; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResponse.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResponse.cs new file mode 100755 index 0000000..151ce0f --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/RemoteResponse.cs @@ -0,0 +1,31 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// RemoteResponse Class + /// + public class RemoteResponse + { + /// + /// Indicates the result of the response + /// + public ResponseCode Result { get; internal set; } + + /// + /// Indicates representation of the response + /// + public Representation Representation { get; internal set; } + + /// + /// Indicates header options of the response + /// + public ResourceOptions Options { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs new file mode 100755 index 0000000..7297875 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Representation.cs @@ -0,0 +1,226 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class containing representation of a resource + /// + public class Representation : IDisposable + { + internal IntPtr _representationHandle = IntPtr.Zero; + + private bool _disposed = false; + private ObservableCollection _children = new ObservableCollection(); + + /// + /// Constructor + /// + public Representation() + { + int ret = Interop.IoTConnectivity.Common.Representation.Create(out _representationHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + _children.CollectionChanged += ChildrenCollectionChanged; + } + + // Constructor for cloning native representation object + internal Representation(IntPtr handleToClone) + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (handleToClone != IntPtr.Zero) + { + ret = Interop.IoTConnectivity.Common.Representation.Clone(handleToClone, out _representationHandle); + } + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create representation"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + _children.CollectionChanged += ChildrenCollectionChanged; + } + + ~Representation() + { + Dispose(false); + } + + /// + /// The URI of resource + /// + public string UriPath + { + get + { + string path; + int ret = Interop.IoTConnectivity.Common.Representation.GetUriPath(_representationHandle, out path); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get uri"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + return path; + } + set + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (value != null) + ret = Interop.IoTConnectivity.Common.Representation.SetUriPath(_representationHandle, value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set uri"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// The type of resource + /// + public ResourceTypes Type + { + get + { + IntPtr typeHandle; + int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out typeHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + return new ResourceTypes(typeHandle); + } + set + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (value != null) + ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value._resourceTypeHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// The interface of the resource + /// + public ResourceInterfaces Interface + { + get + { + IntPtr interfaceHandle; + int ret = Interop.IoTConnectivity.Common.Representation.GetResourceTypes(_representationHandle, out interfaceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + return new ResourceInterfaces(interfaceHandle); + } + set + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (value != null) + ret = Interop.IoTConnectivity.Common.Representation.SetResourceTypes(_representationHandle, value.ResourceInterfacesHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// Current state of the resource + /// + public State State + { + get + { + return State; + } + set + { + State = value; + int ret = (int)IoTConnectivityError.InvalidParameter; + if (State != null) + ret = Interop.IoTConnectivity.Common.Representation.SetState(_representationHandle, State._resourceStateHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// List of Child resource representation + /// + public ICollection Children + { + get + { + return _children; + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + Type.Dispose(); + Interface.Dispose(); + State?.Dispose(); + } + + Interop.IoTConnectivity.Common.Representation.Destroy(_representationHandle); + _disposed = true; + } + + private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) + { + foreach (Representation r in e.NewItems) + { + int ret = Interop.IoTConnectivity.Common.Representation.AddChild(_representationHandle, r._representationHandle); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) + { + foreach (Representation r in e.NewItems) + { + int ret = Interop.IoTConnectivity.Common.Representation.RemoveChild(_representationHandle, r._representationHandle); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Request.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Request.cs new file mode 100755 index 0000000..fa46360 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Request.cs @@ -0,0 +1,66 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class respresenting Request to a resource + /// + public class Request: IDisposable + { + private bool _disposed = false; + + ~Request() + { + Dispose(false); + } + + /// + /// The host address of the request + /// + public string HostAddress { get; internal set; } + + /// + /// The representation of the request + /// + public Representation Representation { get; internal set; } + + /// + /// The query of the request + /// + public ResourceQuery Query { get; internal set; } + + /// + /// The options related to the request + /// + public ResourceOptions Options { get; internal set; } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + Representation.Dispose(); + Query.Dispose(); + Options.Dispose(); + } + + _disposed = true; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Resource.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Resource.cs new file mode 100755 index 0000000..6c1b12a --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Resource.cs @@ -0,0 +1,392 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace Tizen.Network.IoTConnectivity +{ + public abstract class Resource : IDisposable + { + private IntPtr _resourceHandle = IntPtr.Zero; + private bool _disposed = false; + private ObservableCollection _children = new ObservableCollection(); + private IntPtr _observerHandle = IntPtr.Zero; + + /// + /// The constructor + /// + /// URI of the resource + /// Resource types + /// Resource interfaces + /// Policy input of the resoruce + public Resource(string uri, ResourceTypes types, ResourceInterfaces interfaces, ResourcePolicy policy) + { + UriPath = uri; + Types = types; + Interfaces = interfaces; + Policy = policy; + + _children.CollectionChanged += ChildrenCollectionChanged; + + int ret = Interop.IoTConnectivity.Server.Observers.Create(out _observerHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create obsever handle"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + ~Resource() + { + Dispose(false); + } + + /// + /// Type details of the resource + /// + public ResourceTypes Types { get; internal set; } + + /// + /// Interface details of the resource + /// + public ResourceInterfaces Interfaces { get; internal set; } + + /// + /// The policy + /// + public ResourcePolicy Policy { get; internal set; } + + /// + /// URI of the resource + /// + public string UriPath { get; internal set; } + + /// + /// List of Child resources + /// + public ICollection Children + { + get + { + return _children; + } + } + + internal IntPtr ResourceHandle + { + get + { + return _resourceHandle; + } + set + { + _resourceHandle = value; + } + } + + /// + /// Notify the specified representation and qos. + /// + /// Representation. + /// Qos. + public void Notify(Representation representation, QualityOfService qos) + { + int ret = (int)IoTConnectivityError.None; + ret = Interop.IoTConnectivity.Server.Resource.Notify(_resourceHandle, representation._representationHandle, _observerHandle, (int)qos); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send notification"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + /// + /// Called on the get event. + /// + /// Request. + public abstract Response OnGet(Request request); + + /// + /// Called on the put event. + /// + /// Request. + public abstract Response OnPut(Request request); + + /// + /// Called on the post event. + /// + /// Request. + public abstract Response OnPost(Request request); + + /// + /// Called on the delete event. + /// + /// Request. + public abstract Response OnDelete(Request request); + + /// + /// Called on the observing event. + /// + /// Request. + /// Policy. + /// Observe identifier. + public abstract bool OnObserving(Request request, ObserveType type, int observeId); + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + Types.Dispose(); + Interfaces.Dispose(); + } + + Interop.IoTConnectivity.Server.Resource.Destroy(_resourceHandle); + Interop.IoTConnectivity.Server.Observers.Destroy(_observerHandle); + _disposed = true; + } + + // This method is used as callback for Resource + internal void OnRequest(IntPtr resourceHandle, IntPtr requestHandle, IntPtr userData) + { + Request request = GetRequest(requestHandle); + Response response = null; + + if (request == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get Request"); + return; + } + + try + { + int observeType; + int ret = Interop.IoTConnectivity.Server.Request.GetObserveType(requestHandle, out observeType); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get observe type"); + return; + } + + if ((ObserveType)observeType != ObserveType.NoType) + { + int observeId; + ret = Interop.IoTConnectivity.Server.Request.GetObserveId(requestHandle, out observeId); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get observe id"); + return; + } + switch ((ObserveType)observeType) + { + case ObserveType.Register: + { + if (OnObserving(request, ObserveType.Register, observeId)) + { + ret = Interop.IoTConnectivity.Server.Observers.Add(_observerHandle, observeId); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add observer id"); + return; + } + break; + } + else + { + // If OnObserving for ObserveType.Register returns false, do not operate for Get operation after Observe operation. + return; + } + } + case ObserveType.Deregister: + { + if (OnObserving(request, ObserveType.Deregister, observeId)) + { + ret = Interop.IoTConnectivity.Server.Observers.Remove(_observerHandle, observeId); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove observer id"); + return; + } + break; + } + else + { + // If OnObserving for ObserveType.Deregister returns false, do not operate for Get operation after Observe operation. + return; + } + } + } + } + + int requestType; + ret = Interop.IoTConnectivity.Server.Request.GetRequestType(requestHandle, out requestType); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get request type"); + return; + } + + switch ((Interop.IoTConnectivity.Server.RequestType)requestType) + { + case Interop.IoTConnectivity.Server.RequestType.Put: + { + response = OnPut(request); + break; + } + case Interop.IoTConnectivity.Server.RequestType.Get: + { + response = OnGet(request); + break; + } + case Interop.IoTConnectivity.Server.RequestType.Post: + { + response = OnPost(request); + break; + } + case Interop.IoTConnectivity.Server.RequestType.Delete: + { + response = OnDelete(request); + break; + } + default: + break; + } + if (response == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send Response"); + return; + } + + if (!response.Send(requestHandle)) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send Response"); + return; + } + } + finally + { + request.Dispose(); + response?.Dispose(); + } + } + + private Request GetRequest(IntPtr requestHandle) + { + string hostAddress; + int ret = Interop.IoTConnectivity.Server.Request.GetHostAddress(requestHandle, out hostAddress); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get host address"); + return null; + } + + IntPtr optionsHandle = IntPtr.Zero; + ret = Interop.IoTConnectivity.Server.Request.GetOptions(requestHandle, out optionsHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get options"); + return null; + } + + IntPtr queryHandle = IntPtr.Zero; + ret = Interop.IoTConnectivity.Server.Request.GetQuery(requestHandle, out queryHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get Query"); + return null; + } + + IntPtr representationHandle = IntPtr.Zero; + ret = Interop.IoTConnectivity.Server.Request.GetRepresentation(requestHandle, out representationHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Get representation"); + return null; + } + + ResourceOptions opts = null; + ResourceQuery query = null; + Representation representation = null; + try + { + opts = (optionsHandle == IntPtr.Zero) ? null : new ResourceOptions(optionsHandle); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new ResourceOptions: " + exp.Message); + return null; + } + + try + { + query = (queryHandle == IntPtr.Zero) ? null : new ResourceQuery(queryHandle); + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new ResourceQuery: " + exp.Message); + return null; + } + + try + { + representation = (representationHandle == IntPtr.Zero) ? null : new Representation(representationHandle); ; + } + catch (Exception exp) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to new Representation: " + exp.Message); + return null; + } + + return new Request() + { + HostAddress = hostAddress, + Options = opts, + Query = query, + Representation = representation + }; + } + + private void ChildrenCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs) + { + if (eventArgs.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) + { + foreach (Resource r in eventArgs.NewItems) + { + int ret = Interop.IoTConnectivity.Server.Resource.BindChildResource(_resourceHandle, r._resourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to bind resource "); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (eventArgs.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) + { + foreach (Resource r in eventArgs.NewItems) + { + int ret = Interop.IoTConnectivity.Server.Resource.UnbindChildResource(_resourceHandle, r._resourceHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to unbind resource"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceFoundEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceFoundEventArgs.cs new file mode 100755 index 0000000..3e204d8 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceFoundEventArgs.cs @@ -0,0 +1,30 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// ResourceFoundEventArgs class. This class is an event arguments of the ResourceFound event. + /// + public class ResourceFoundEventArgs : EventArgs + { + /// + /// RequestId property. + /// + /// int RequestId. + public int RequestId { get; internal set; } + + /// + /// Resource property. + /// + /// RemoteResource Resource. + public RemoteResource Resource { get; internal set; } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceInterfaces.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceInterfaces.cs new file mode 100755 index 0000000..95227ff --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceInterfaces.cs @@ -0,0 +1,210 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class containing resource interfaces + /// + public class ResourceInterfaces : IEnumerable, IDisposable + { + /// + /// Default Interface + /// + public const string DefaultInterface = "oic.if.baseline"; + + /// + /// List Links Interface which is used to list the references to other resources contained in a resource. + /// + public const string LinkInterface = "oic.if.ll"; + + /// + /// Batch Interface which is used to manipulate (GET, PUT, POST, DELETE) on other resource contained in a resource. + /// + public const string BatchInterface = "oic.if.b"; + + /// + /// Group Interface which is used to manipulate (GET, PUT, POST) a group of remote resources. + /// + public const string GroupInterface = "oic.mi.grp"; + + /// + /// Read-Only Interface which is used to limit the methods that can be applied to a resource to GET only. + /// + public const string ReadonlyInterface = "oic.if.r"; + + private readonly IntPtr _resourceInterfacesHandle = IntPtr.Zero; + private const int MaxLength = 61; + private readonly HashSet _resourceInterfaces = new HashSet(); + private bool _disposed = false; + + /// + /// Constructor + /// + public ResourceInterfaces() + { + int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + /// + /// Constructor + /// + /// List of resource interfaces + public ResourceInterfaces(IEnumerable ifaces) + { + int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Create(out _resourceInterfacesHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + foreach (string iface in ifaces) + { + Add(iface); + } + } + + /// + /// Constructor + /// + internal ResourceInterfaces(IntPtr ifacesHandleToClone) + { + int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Clone(ifacesHandleToClone, out _resourceInterfacesHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + Interop.IoTConnectivity.Common.ResourceInterfaces.ForeachCallback cb = (string iface, IntPtr data) => + { + _resourceInterfaces.Add(iface); + return true; + }; + + ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Foreach(ifacesHandleToClone, cb, IntPtr.Zero); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + ~ResourceInterfaces() + { + Dispose(false); + } + + internal IntPtr ResourceInterfacesHandle + { + get + { + return _resourceInterfacesHandle; + } + } + + /// + /// Count of interfaces in the list + /// + public int Count + { + get + { + return _resourceInterfaces.Count; + } + } + + /// + /// Adds a resource interface into the list. + /// + /// Resource interface + public void Add(string item) + { + if (IsValid(item)) + { + int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Add(_resourceInterfacesHandle, item); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + _resourceInterfaces.Add(item); + } + else + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid interface"); + throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); + } + } + + /// + /// Removes a resource interface from the list + /// + /// Resource interface + public void Remove(string item) + { + int ret = Interop.IoTConnectivity.Common.ResourceInterfaces.Remove(_resourceInterfacesHandle, item); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + _resourceInterfaces.Remove(item); + } + + /// + /// Return enumerator for the list of interfaces + /// + /// The enumerator + public IEnumerator GetEnumerator() + { + return _resourceInterfaces.GetEnumerator(); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _resourceInterfaces.GetEnumerator(); + } + + internal static bool IsValid(string type) + { + Regex r = new Regex("^[a-zA-Z0-9.-]+$"); + return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type)); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + } + + Interop.IoTConnectivity.Common.ResourceInterfaces.Destroy(_resourceInterfacesHandle); + _disposed = true; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceOptions.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceOptions.cs new file mode 100755 index 0000000..fa481de --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceOptions.cs @@ -0,0 +1,289 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using Tizen; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class representing Resource options + /// + public class ResourceOptions : IDictionary, IDisposable + { + internal const int MaxSize = 2; + internal const int IdMin = 2048; + internal const int IdMax = 3000; + internal const int DataMax = 15; + + internal IntPtr _resourceOptionsHandle = IntPtr.Zero; + private readonly IDictionary _options = new Dictionary(); + private bool _disposed = false; + + public ResourceOptions() + { + int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + internal ResourceOptions(IntPtr handleToClone) + { + int ret = Interop.IoTConnectivity.Common.Options.Create(out _resourceOptionsHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create options"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + Interop.IoTConnectivity.Common.Options.OptionsCallback forEachCallback = (ushort id, string value, IntPtr userData) => + { + Add(id, value); + return true; + }; + + ret = Interop.IoTConnectivity.Common.Options.ForEach(handleToClone, forEachCallback, IntPtr.Zero); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate options"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + ~ResourceOptions() + { + Dispose(false); + } + + /// + /// Contains all the Option keys + /// + public ICollection Keys + { + get + { + return _options.Keys; + } + } + + /// + /// Contains all the Option values + /// + public ICollection Values + { + get + { + return _options.Values; + } + } + + /// + /// Gets the number of options + /// + public int Count + { + get + { + return _options.Count; + } + } + + /// + /// Represents whether the collection is readonly + /// + public bool IsReadOnly + { + get + { + return _options.IsReadOnly; + } + } + + /// + /// Gets or sets the option + /// + /// The option id to get or set. + /// The option with the specified id. + public string this[ushort key] + { + get + { + return _options[key]; + } + + set + { + Add(key, value); + } + } + + /// + /// Checks the given key exists in Options collection + /// + /// The key to look for + /// + public bool ContainsKey(ushort key) + { + return _options.ContainsKey(key); + } + + /// + /// Adds option key and value + /// + /// Option ID + /// Value coresponding to option + public void Add(ushort key, string value) + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (IsValid(key, value)) + { + ret = Interop.IoTConnectivity.Common.Options.Add(_resourceOptionsHandle, key, value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add option"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + _options.Add(key, value); + } + else + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid options"); + throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); + } + } + + /// + /// Removes an option + /// + /// The option to remvoe + /// + public bool Remove(ushort key) + { + int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + return _options.Remove(key); + } + + /// + /// Gets the value associated with the specified key. + /// + /// The option id + /// value corresponding to option id + /// true if the key exists, false otherwise + public bool TryGetValue(ushort key, out string value) + { + return _options.TryGetValue(key, out value); + } + + /// + /// Adds options key and value as a key value pair + /// + /// The key value pair + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// Clears the Options collection + /// + public void Clear() + { + foreach (ushort key in Keys) + { + int ret = Interop.IoTConnectivity.Common.Options.Remove(_resourceOptionsHandle, key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove option"); + throw IoTConnectivityErrorFactory.GetException(ret); + }; + } + _options.Clear(); + } + + /// + /// Checks if the given option pair exists + /// + /// The key value pair + /// + public bool Contains(KeyValuePair item) + { + return _options.Contains(item); + } + + /// + /// Copies the elements of the options collection to an Array, starting at a particular index. + /// + /// The destination array + /// Index parameter + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + _options.CopyTo(array, arrayIndex); + } + + /// + /// Remove the gien option pair + /// + /// The option pair to remove + /// + public bool Remove(KeyValuePair item) + { + return Remove(item.Key); + } + + /// + /// Get the enumerator to options collection + /// + /// Enumerator to option pairs + public IEnumerator> GetEnumerator() + { + return _options.GetEnumerator(); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _options.GetEnumerator(); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + } + + Interop.IoTConnectivity.Common.Options.Destroy(_resourceOptionsHandle); + _disposed = true; + } + + private bool IsValid(ushort key, string value) + { + return (key > IdMin && key < IdMax && value.Length <= DataMax && _options.Count() < MaxSize); + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourcePolicy.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourcePolicy.cs new file mode 100755 index 0000000..5285072 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourcePolicy.cs @@ -0,0 +1,48 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Enumeration for policy which can be held in a resource. + /// + [Flags] + public enum ResourcePolicy + { + /// + /// Indicates resource uninitialized + /// + NoProperty = 0, + /// + /// Indicates resource that is allowed to be discovered + /// + Discoverable = (1 << 0), + /// + /// Indicates resource that is allowed to be observed + /// + Observable = (1 << 1), + /// + /// Indicates resource initialized and activated + /// + Active = (1 << 2), + /// + /// Indicates resource which takes some delay to respond + /// + Slow = (1 << 3), + /// + /// Indicates secure resource + /// + Secure = (1 << 4), + /// + /// When this bit is set, the resource is allowed to be discovered only if discovery request contains an explicit querystring. + /// + ExplicitDiscoverable = (1 << 5), + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceQuery.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceQuery.cs new file mode 100755 index 0000000..3f0e2bf --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceQuery.cs @@ -0,0 +1,365 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class to manage query of request + /// + public class ResourceQuery : IDictionary, IDisposable + { + internal const int QueryMaxLenth = 64; + internal IntPtr _resourceQueryHandle = IntPtr.Zero; + private readonly IDictionary _query = new Dictionary(); + private bool _disposed = false; + + /// + /// Constructor + /// + public ResourceQuery() + { + int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + internal ResourceQuery(IntPtr resourceQueryHandleToClone) + { + int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + Interop.IoTConnectivity.Common.Query.QueryCallback forEachCallback = (string key, string value, IntPtr userData) => + { + Add(key, value); + return true; + }; + + ret = Interop.IoTConnectivity.Common.Query.Foreach(resourceQueryHandleToClone, forEachCallback, IntPtr.Zero); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + ~ResourceQuery() + { + Dispose(false); + } + + /// + /// Resource type of the query + /// + public string Type + { + get + { + string type; + int ret = Interop.IoTConnectivity.Common.Query.GetResourceType(_resourceQueryHandle, out type); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + return type; + } + set + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (ResourceTypes.IsValid(value)) + ret = Interop.IoTConnectivity.Common.Query.SetResourceType(_resourceQueryHandle, value); + + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// Resource interface of the query + /// + public string Interface + { + get + { + string iface; + int ret = Interop.IoTConnectivity.Common.Query.GetInterface(_resourceQueryHandle, out iface); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + return iface; + } + set + { + int ret = (int)IoTConnectivityError.InvalidParameter; + if (ResourceInterfaces.IsValid(value)) + ret = Interop.IoTConnectivity.Common.Query.SetInterface(_resourceQueryHandle, value); + + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + + /// + /// Contains all keys of Query + /// + public ICollection Keys + { + get + { + return _query.Keys; + } + } + + /// + /// Contains all the values of Query + /// + public ICollection Values + { + get + { + return _query.Values; + } + } + + /// + /// Gets the number of query elements + /// + public int Count + { + get + { + return _query.Count; + } + } + + /// + /// Represents whether Query is readonly + /// + public bool IsReadOnly + { + get + { + return _query.IsReadOnly; + } + } + + /// + /// Gets or sets the Query key.. + /// + /// The key of the Query to get or set. + /// The element with the specified key. + public string this[string key] + { + get + { + return _query[key]; + } + + set + { + Add(key, value); + } + } + + /// + /// Checks the given key exists in Query + /// + /// The Query key + /// true if exists. Otherwise, false + public bool ContainsKey(string key) + { + return _query.ContainsKey(key); + } + + /// + /// Adds Query element + /// + /// The key representing the Query + /// The value representing the Query + public void Add(string key, string value) + { + if (CanAddQuery(key, value)) + { + int ret = Interop.IoTConnectivity.Common.Query.Add(_resourceQueryHandle, key, value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + _query.Add(key, value); + } + else + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Query cannot be added"); + throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); + } + } + + /// + /// Removes a Query element from collection + /// + /// The Query element to remove + /// true if operation is success. Otherwise, false + public bool Remove(string key) + { + int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + return _query.Remove(key); + } + + /// + /// Gets the value associated with the specified key. + /// + /// The key whose value to get. + /// The value associated with the specified key + /// true if the Query contains an element with the specified key; otherwise, false. + public bool TryGetValue(string key, out string value) + { + return _query.TryGetValue(key, out value); + } + + /// + /// Adds Query as a key value pair + /// + /// The key value pair + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// Clears Query + /// + public void Clear() + { + foreach (string key in _query.Keys) + { + int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + _query.Clear(); + } + + /// + /// Checks the given key value pair exists in Query + /// + /// The key value pair + /// true if exists. Otherwise, false + public bool Contains(KeyValuePair item) + { + return _query.Contains(item); + } + + /// + /// Copies the elements of the Query to an Array, starting at a particular index. + /// + /// The destination array + /// The zero-based index in array at which copying begins. + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + _query.CopyTo(array, arrayIndex); + } + + /// + /// Removes a Query element from collection + /// + /// The Query element to remove + /// true if operation is success. Otherwise, false + public bool Remove(KeyValuePair item) + { + int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, item.Key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove query"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + return _query.Remove(item); + } + + /// + /// Returns an enumerator that iterates through the collection. + /// + /// An enumerator that can be used to iterate through the collection. + public IEnumerator> GetEnumerator() + { + return _query.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _query.GetEnumerator(); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + } + + Interop.IoTConnectivity.Common.Query.Destroy(_resourceQueryHandle); + _disposed = true; + } + + private bool CanAddQuery(string newKey, string newValue) + { + int queryLenth = 0; + foreach (string key in Keys) + { + queryLenth += key.Length; + } + foreach (string value in Values) + { + queryLenth += value.Length; + } + + if ((newKey.Length + newValue.Length + queryLenth) < QueryMaxLenth) + return true; + + return false; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceState.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceState.cs new file mode 100755 index 0000000..9563e90 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceState.cs @@ -0,0 +1,25 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Enumeration for states of remote resource. + /// + public enum ResourceState + { + /// + /// Indicates remote resource is alive + /// + Alive = 0, + /// + /// Indicates remote resource is lost + /// + LostSignal + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceTypes.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceTypes.cs new file mode 100755 index 0000000..83e9afa --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResourceTypes.cs @@ -0,0 +1,178 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class containing resource types + /// + public class ResourceTypes : IEnumerable, IDisposable + { + internal const int MaxLength = 61; + internal IntPtr _resourceTypeHandle = IntPtr.Zero; + private readonly HashSet _resourceTypes = new HashSet(); + private bool _disposed = false; + + /// + /// Constructor + /// + public ResourceTypes() + { + int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + /// + /// Constructor + /// + public ResourceTypes(IEnumerable types) + { + int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + foreach (string type in types) + { + Add(type); + } + } + + /// + /// Constructor + /// + internal ResourceTypes(IntPtr typesHandleToClone) + { + int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) => + { + _resourceTypes.Add(type); + return true; + }; + + ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + ~ResourceTypes() + { + Dispose(false); + } + + /// + /// Count of resource types in the list + /// + public int Count + { + get + { + return _resourceTypes.Count; + } + } + + /// + /// Inserts a resource type into the list. + /// + /// The resource type to add + public void Add(string item) + { + if (IsValid(item)) + { + int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + _resourceTypes.Add(item); + } + else + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type"); + throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); + } + } + + /// + /// Removes a resource type from the list + /// + /// The resource type to remove + public void Remove(string item) + { + int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + _resourceTypes.Remove(item); + } + + /// + /// Return enumerator for the list of types + /// + /// Enumerator of the collection + public IEnumerator GetEnumerator() + { + return _resourceTypes.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _resourceTypes.GetEnumerator(); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + internal static bool IsValid(string type) + { + Regex r = new Regex("^[a-zA-Z0-9.-]+$"); + return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type)); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + } + + Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle); + _disposed = true; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Response.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Response.cs new file mode 100755 index 0000000..4120cc7 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/Response.cs @@ -0,0 +1,94 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Class representing Response from a resource + /// + public class Response : IDisposable + { + private bool _disposed = false; + + ~Response() + { + Dispose(false); + } + + /// + /// Result corresponding to a request + /// + public ResponseCode Result { get; set; } + + /// + /// Representation of the resource + /// + public Representation Representation { get; set; } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + internal bool Send(IntPtr requestHandle) + { + IntPtr responseHandle = IntPtr.Zero; + int ret = Interop.IoTConnectivity.Server.Response.Create(requestHandle, out responseHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response"); + return false; + } + + ret = Interop.IoTConnectivity.Server.Response.SetResult(responseHandle, (int)Result); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response"); + Interop.IoTConnectivity.Server.Response.Destroy(responseHandle); + return false; + } + + string iface = "oic.if.baseline"; + // TODO: iface parameter will be removed after native API is changed + ret = Interop.IoTConnectivity.Server.Response.SetRepresentation(responseHandle, iface, Representation._representationHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response"); + Interop.IoTConnectivity.Server.Response.Destroy(responseHandle); + return false; + } + + ret = Interop.IoTConnectivity.Server.Response.Send(responseHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to send response"); + Interop.IoTConnectivity.Server.Response.Destroy(responseHandle); + return false; + } + + Interop.IoTConnectivity.Server.Response.Destroy(responseHandle); + return true; + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + Representation?.Dispose(); + } + + _disposed = true; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResponseCode.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResponseCode.cs new file mode 100755 index 0000000..3bddc37 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/ResponseCode.cs @@ -0,0 +1,41 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// Enumeration for result response + /// + public enum ResponseCode + { + /// + /// Indicates result of response for success + /// + Ok = 0, + /// + /// Indicates result of response for something error + /// + Error, + /// + /// Indicates result of response for resource has created + /// + Created, + /// + /// Indicates result of response for resource has deleted + /// + Deleted, + /// + /// Indicates result of response for slow resource + /// + Slow, + /// + /// Indicates result of response for accessing unauthorized resource + /// + Forbidden + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/State.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/State.cs new file mode 100755 index 0000000..55c3cb2 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/State.cs @@ -0,0 +1,678 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// State represents current state of a resource + /// + public class State : IDictionary, IDisposable + { + internal IntPtr _resourceStateHandle = IntPtr.Zero; + private readonly IDictionary _state = new Dictionary(); + private bool _disposed = false; + + /// + /// Constructor + /// + public State() + { + int ret = Interop.IoTConnectivity.Common.State.Create(out _resourceStateHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create state handle"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + + internal State(IntPtr stateHandleToClone) + { + int ret = Interop.IoTConnectivity.Common.State.Clone(stateHandleToClone, out _resourceStateHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create state handle"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + SetState(stateHandleToClone); + } + + ~State() + { + Dispose(false); + } + + /// + /// Gets the number of status elements + /// + public int Count + { + get + { + return _state.Count; + } + } + + /// + /// Represents whether State is readonly + /// + public bool IsReadOnly + { + get + { + return _state.IsReadOnly; + } + } + + /// + /// Contains all the status keys + /// + public ICollection Keys + { + get + { + return _state.Keys; + } + } + + /// + /// Contains all the status values + /// + public ICollection Values + { + get + { + return _state.Values; + } + } + + /// + /// Gets or sets the status with the specified key. + /// + /// The key of the status to get or set. + /// The element with the specified key. + public object this[string key] + { + get + { + return _state[key]; + } + + set + { + Add(key, value); + } + } + + /// + /// Adds status key and value as a key value pair + /// + /// The item to add + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// Adds status element + /// + /// The key representing the state + /// The value representing the state + public void Add(string key, object value) + { + int ret = 0; + if (value is int) + { + ret = Interop.IoTConnectivity.Common.State.AddInt(_resourceStateHandle, key, (int)value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add int"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (value is State) + { + State state = (State)value; + ret = Interop.IoTConnectivity.Common.State.AddState(_resourceStateHandle, key, state._resourceStateHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (value is string) + { + ret = Interop.IoTConnectivity.Common.State.AddStr(_resourceStateHandle, key, (string)value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add string"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (value is double) + { + ret = Interop.IoTConnectivity.Common.State.AddDouble(_resourceStateHandle, key, (double)value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add double"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (value is bool) + { + ret = Interop.IoTConnectivity.Common.State.AddBool(_resourceStateHandle, key, (bool)value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (value is byte[]) + { + byte[] val = value as byte[]; + if (val == null) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get byte[] val"); + throw new ArgumentException("Invalid Parameter"); + } + ret = Interop.IoTConnectivity.Common.State.AddByteStr(_resourceStateHandle, key, val, val.Length); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add bool"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else if (value is IEnumerable) + { + IntPtr listHandle = List.GetListHandle(value); + ret = Interop.IoTConnectivity.Common.State.AddList(_resourceStateHandle, key, listHandle); + Interop.IoTConnectivity.Common.State.Destroy(listHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + else + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to Add"); + throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); + } + _state.Add(key, value); + } + + /// + /// Clears state collection + /// + public void Clear() + { + foreach (string key in _state.Keys) + { + int ret = Interop.IoTConnectivity.Common.State.Remove(_resourceStateHandle, key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + _state.Clear(); + } + + /// + /// Checks the given key value pair exists in state collection + /// + /// The status key value pair + /// true if exists. Otherwise, false + public bool Contains(KeyValuePair item) + { + return _state.Contains(item); + } + + /// + /// Checks the given key exists in state collection + /// + /// The status key + /// true if exists. Otherwise, false + public bool ContainsKey(string key) + { + return _state.ContainsKey(key); + } + + /// + /// Copies the elements of the state to an Array, starting at a particular index. + /// + /// The destination array + /// The zero-based index in array at which copying begins. + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + _state.CopyTo(array, arrayIndex); + } + + /// + /// Returns an enumerator that iterates through the collection. + /// + /// An enumerator that can be used to iterate through the collection. + public IEnumerator> GetEnumerator() + { + return _state.GetEnumerator(); + } + + /// + /// Removes a state element from collection + /// + /// The state element to remove + /// true if operation is success. Otherwise, false + public bool Remove(KeyValuePair item) + { + int ret = Interop.IoTConnectivity.Common.State.Remove(_resourceStateHandle, item.Key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + return _state.Remove(item); + } + + /// + /// Removes a state element from collection using key + /// + /// The state element to remove + /// true if operation is success. Otherwise, false + public bool Remove(string key) + { + int ret = Interop.IoTConnectivity.Common.State.Remove(_resourceStateHandle, key); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + + return _state.Remove(key); + } + + /// + /// Gets the value associated with the specified key. + /// + /// The key whose value to get. + /// The value associated with the specified key + /// true if the state collection contains an element with the specified key; otherwise, false. + public bool TryGetValue(string key, out object value) + { + return _state.TryGetValue(key, out value); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _state.GetEnumerator(); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Free managed objects + } + + Interop.IoTConnectivity.Common.State.Destroy(_resourceStateHandle); + _disposed = true; + } + + private void SetState(IntPtr stateHandle) + { + Interop.IoTConnectivity.Common.State.StateCallback cb = (IntPtr state, string key, IntPtr userData) => + { + Interop.IoTConnectivity.Common.DataType type = 0; + int ret = Interop.IoTConnectivity.Common.State.GetType(state, key, out type); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + switch (type) + { + case Interop.IoTConnectivity.Common.DataType.Int: + { + int value; + ret = Interop.IoTConnectivity.Common.State.GetInt(state, key, out value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + Add(key, value); + break; + } + case Interop.IoTConnectivity.Common.DataType.Bool: + { + bool value; + ret = Interop.IoTConnectivity.Common.State.GetBool(state, key, out value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + Add(key, value); + break; + } + case Interop.IoTConnectivity.Common.DataType.Double: + { + double value; + ret = Interop.IoTConnectivity.Common.State.GetDouble(state, key, out value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + Add(key, value); + break; + } + case Interop.IoTConnectivity.Common.DataType.String: + { + string value; + ret = Interop.IoTConnectivity.Common.State.GetStr(state, key, out value); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + Add(key, value); + break; + } + case Interop.IoTConnectivity.Common.DataType.ByteStr: + { + IntPtr byteStrPtr; + int byteStrSize; + ret = Interop.IoTConnectivity.Common.State.GetByteStr(state, key, out byteStrPtr, out byteStrSize); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + byte[] byteStr = new byte[byteStrSize]; + Marshal.Copy(byteStrPtr, byteStr, 0, byteStrSize); + Add(key, byteStr); + break; + } + case Interop.IoTConnectivity.Common.DataType.Null: + { + Add(key, null); + break; + } + case Interop.IoTConnectivity.Common.DataType.List: + { + IntPtr listHandle; + ret = Interop.IoTConnectivity.Common.State.GetList(state, key, out listHandle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + Add(key, List.GetList(listHandle)); + break; + } + case Interop.IoTConnectivity.Common.DataType.State: + { + IntPtr stateHndle; + ret = Interop.IoTConnectivity.Common.State.GetState(state, key, out stateHndle); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + Add(key, new State(stateHndle)); + break; + } + default: + break; + } + + return true; + }; + + int res = Interop.IoTConnectivity.Common.State.Foreach(stateHandle, cb, IntPtr.Zero); + if (res != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove state"); + throw IoTConnectivityErrorFactory.GetException(res); + } + } + } + + internal static class List + { + internal static IntPtr GetListHandle(object list) + { + IntPtr listHandle = IntPtr.Zero; + int ret; + int pos = 0; + + if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.List, out listHandle); + pos = 0; + foreach (IEnumerable val in (IEnumerable)list) + { + IntPtr childList = GetListHandle(val); + ret = Interop.IoTConnectivity.Common.List.AddList(listHandle, childList, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + Interop.IoTConnectivity.Common.List.Destroy(childList); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Int, out listHandle); + pos = 0; + foreach (int val in (IEnumerable)list) + { + ret = Interop.IoTConnectivity.Common.List.AddInt(listHandle, val, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.String, out listHandle); + pos = 0; + foreach (string val in (IEnumerable)list) + { + ret = Interop.IoTConnectivity.Common.List.AddStr(listHandle, val, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Double, out listHandle); + pos = 0; + foreach (double val in (IEnumerable)list) + { + ret = Interop.IoTConnectivity.Common.List.AddDouble(listHandle, val, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.Bool, out listHandle); + pos = 0; + foreach (bool val in (IEnumerable)list) + { + ret = Interop.IoTConnectivity.Common.List.AddBool(listHandle, val, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.State, out listHandle); + pos = 0; + foreach (State val in (IEnumerable)list) + { + ret = Interop.IoTConnectivity.Common.List.AddState(listHandle, val._resourceStateHandle, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else if (list is IEnumerable) + { + ret = Interop.IoTConnectivity.Common.List.Create(Interop.IoTConnectivity.Common.DataType.ByteStr, out listHandle); + pos = 0; + foreach (byte[] val in (IEnumerable)list) + { + ret = Interop.IoTConnectivity.Common.List.AddByteStr(listHandle, val, val.Length, pos++); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add byte[]"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + } + } + else + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to GetListHandle"); + throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter); + } + return listHandle; + } + + internal static object GetList(IntPtr listHandle) + { + IList list = null; + int type; + int ret = Interop.IoTConnectivity.Common.List.GetType(listHandle, out type); + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + switch ((Interop.IoTConnectivity.Common.DataType)type) + { + case Interop.IoTConnectivity.Common.DataType.Int: + { + list = new List(); + Interop.IoTConnectivity.Common.List.IntCallback cb = (int pos, int value, IntPtr userData) => + { + list.Add(value); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachInt(listHandle, cb, IntPtr.Zero); + break; + } + case Interop.IoTConnectivity.Common.DataType.Bool: + { + list = new List(); + Interop.IoTConnectivity.Common.List.BoolCallback cb = (int pos, bool value, IntPtr userData) => + { + list.Add(value); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachBool(listHandle, cb, IntPtr.Zero); + break; + } + case Interop.IoTConnectivity.Common.DataType.Double: + { + list = new List(); + Interop.IoTConnectivity.Common.List.DoubleCallback cb = (int pos, double value, IntPtr userData) => + { + list.Add(value); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachDouble(listHandle, cb, IntPtr.Zero); + break; + } + case Interop.IoTConnectivity.Common.DataType.String: + { + list = new List(); + Interop.IoTConnectivity.Common.List.StrCallback cb = (int pos, string value, IntPtr userData) => + { + list.Add(value); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachStr(listHandle, cb, IntPtr.Zero); + break; + } + case Interop.IoTConnectivity.Common.DataType.State: + { + list = new List(); + Interop.IoTConnectivity.Common.List.StateCallback cb = (int pos, IntPtr value, IntPtr userData) => + { + list.Add(new State(value)); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachState(listHandle, cb, IntPtr.Zero); + break; + } + case Interop.IoTConnectivity.Common.DataType.ByteStr: + { + list = new List(); + Interop.IoTConnectivity.Common.List.ByteStrCallback cb = (int pos, byte[] value, int len, IntPtr userData) => + { + list.Add(value); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachByteStr(listHandle, cb, IntPtr.Zero); + break; + } + case Interop.IoTConnectivity.Common.DataType.List: + { + list = new List>(); + Interop.IoTConnectivity.Common.List.ListCallback cb = (int pos, IntPtr value, IntPtr userData) => + { + object childList = GetList(value); + list.Add(childList); + return true; + }; + ret = Interop.IoTConnectivity.Common.List.ForeachList(listHandle, cb, IntPtr.Zero); + break; + } + default: + break; + } + if (ret != (int)IoTConnectivityError.None) + { + Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get state"); + throw IoTConnectivityErrorFactory.GetException(ret); + } + return list; + } + } +} diff --git a/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/StateChangedEventArgs.cs b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/StateChangedEventArgs.cs new file mode 100755 index 0000000..1801353 --- /dev/null +++ b/Tizen.Network.IoTConnectivity/Tizen.Network.IoTConnectivity/StateChangedEventArgs.cs @@ -0,0 +1,24 @@ +/// Copyright 2016 by Samsung Electronics, Inc., +/// +/// This software is the confidential and proprietary information +/// of Samsung Electronics, Inc. ("Confidential Information"). You +/// shall not disclose such Confidential Information and shall use +/// it only in accordance with the terms of the license agreement +/// you entered into with Samsung. + +using System; + +namespace Tizen.Network.IoTConnectivity +{ + /// + /// StateChangedEventArgs class. This class is an event arguments of the StateChanged event. + /// + public class StateChangedEventArgs : EventArgs + { + /// + /// State property. + /// + /// ResourceState State. + public ResourceState State { get; internal set; } + } +} diff --git a/packaging/csapi-network-iotconnectivity.manifest b/packaging/csapi-network-iotconnectivity.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/csapi-network-iotconnectivity.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/csapi-network-iotconnectivity.pc.in b/packaging/csapi-network-iotconnectivity.pc.in new file mode 100644 index 0000000..2849fd8 --- /dev/null +++ b/packaging/csapi-network-iotconnectivity.pc.in @@ -0,0 +1,5 @@ +Name: csapi-network-iotconnectivity +Description: Tizen IoT Connectivity API for C# +Version: @version@ +Libs: -r:@dllpath@/@dllname@ +Requires: diff --git a/packaging/csapi-network-iotconnectivity.spec b/packaging/csapi-network-iotconnectivity.spec new file mode 100755 index 0000000..298ef09 --- /dev/null +++ b/packaging/csapi-network-iotconnectivity.spec @@ -0,0 +1,81 @@ +%define dllpath %{_libdir}/mono/tizen +%define dllname Tizen.Network.IoTConnectivity.dll + +Name: csapi-network-iotconnectivity +Summary: Tizen IoT Connectivity API for C# +Version: 1.0.0 +Release: 1 +Group: Development/Libraries +License: Apache-2.0 +URL: https://www.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest +Source2: %{name}.pc.in + +# TODO: replace mono-compiler, mono-devel to mcs, mono-shlib-cop +BuildRequires: mono-compiler +BuildRequires: mono-devel +# TODO: replace mono-core to gacutil. +# mono-core should provide the symbol 'gacutil' +Requires(post): mono-core +Requires(postun): mono-core + +# P/Invoke Dependencies +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(capi-appfw-application) + +# P/Invoke Runtime Dependencies +# TODO: It should be removed after fix tizen-rpm-config +Requires: glib-2.0 +BuildRequires: pkgconfig(iotcon) +# DLL Dependencies +BuildRequires: pkgconfig(csapi-tizen) +#BuildRequires: ... + +%description +Tizen API for C# + +%package devel +Summary: Development package for %{name} +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Development package for %{name} + +%prep +%setup -q + +cp %{SOURCE1} . + +%build +# build dll +make + +# check p/invoke +if [ -x %{dllname} ]; then + RET=`mono-shlib-cop %{dllname}`; \ + CNT=`echo $RET | grep -E "^error:" | wc -l`; \ + if [ $CNT -gt 0 ]; then exit 1; fi +fi + +%install +# copy dll +mkdir -p %{buildroot}%{dllpath} +install -p -m 644 %{dllname} %{buildroot}%{dllpath} + +# generate pkgconfig +mkdir -p %{buildroot}%{_libdir}/pkgconfig +sed -e "s#@version@#%{version}#g" \ + -e "s#@dllpath@#%{dllpath}#g" \ + -e "s#@dllname@#%{dllname}#g" \ + %{SOURCE2} > %{buildroot}%{_libdir}/pkgconfig/%{name}.pc + +%post +gacutil -i %{dllpath}/%{dllname} + +%files +%{dllpath}/%{dllname} + +%files devel +%{_libdir}/pkgconfig/%{name}.pc -- 2.7.4