Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- helper function to deal with wchar_t * to wide_string
- function Value (P : access stddef_h.wchar_t) return Wide_String is
- temp : Wide_String(1 .. 256);
- count : natural := 0;
- -- ugliness to convert pointer types
- type sd_wchar_ptr is access all stddef_h.wchar_t;
- type wchar_array_ptr is access wchar_array;
- Function To_Wchar_Array_Ptr is new Ada.Unchecked_Conversion(sd_wchar_ptr, wchar_array_ptr);
- -- this does NOT create the required wchar_array pointer
- WCP : wchar_array_ptr := To_Wchar_Array_Ptr(sd_wchar_ptr(P));
- begin
- Put_Line("Wide string");
- To_Ada(WCP.all, temp, count);
- Put_Line("Wide string length " & natural'image(count));
- return temp(1..count);
- end Value;
- pragma Ada_2005;
- pragma Style_Checks (Off);
- with Interfaces.C; use Interfaces.C;
- with Interfaces.C.Strings;
- with stddef_h;
- with System;
- package hidapi_hidapi_h is
- -- see source file /usr/include/hidapi/hidapi.h
- type hid_device_info is record
- path : Interfaces.C.Strings.chars_ptr; -- /usr/include/hidapi/hidapi.h:51
- vendor_id : aliased unsigned_short; -- /usr/include/hidapi/hidapi.h:53
- product_id : aliased unsigned_short; -- /usr/include/hidapi/hidapi.h:55
- serial_number : access stddef_h.wchar_t; -- /usr/include/hidapi/hidapi.h:57
- release_number : aliased unsigned_short; -- /usr/include/hidapi/hidapi.h:60
- manufacturer_string : access stddef_h.wchar_t; -- /usr/include/hidapi/hidapi.h:62
- product_string : access stddef_h.wchar_t; -- /usr/include/hidapi/hidapi.h:64
- usage_page : aliased unsigned_short; -- /usr/include/hidapi/hidapi.h:67
- usage : aliased unsigned_short; -- /usr/include/hidapi/hidapi.h:70
- interface_number : aliased int; -- /usr/include/hidapi/hidapi.h:75
- next : access hid_device_info; -- /usr/include/hidapi/hidapi.h:78
- end record;
- pragma Convention (C_Pass_By_Copy, hid_device_info); -- /usr/include/hidapi/hidapi.h:49
- function hid_enumerate (arg1 : unsigned_short; arg2 : unsigned_short) return access hid_device_info; -- /usr/include/hidapi/hidapi.h:132
- pragma Import (C, hid_enumerate, "hid_enumerate");
- end hidapi_hidapi_h;
- pragma Ada_2005;
- pragma Style_Checks (Off);
- with Interfaces.C; use Interfaces.C;
- package stddef_h is
- -- unsupported macro: NULL ((void *)0)
- subtype size_t is unsigned_long; -- /usr/lib/gcc/x86_64-linux-gnu/6/include/stddef.h:216
- subtype wchar_t is int; -- /usr/lib/gcc/x86_64-linux-gnu/6/include/stddef.h:328
- end stddef_h;
- with Ada.Finalization; use Ada.Finalization;
- private with hidapi_hidapi_h;
- private with System;
- package hidapi is
- type id is new natural range 0 .. 2**16 - 1;
- type hid_device is new Limited_Controlled with private;
- -- find first matching devices by enumeration : the RA part of RAII.
- function enumerate (vendor_id, product_id : id) return hid_device;
- -- accessors for device characteristics on enumerated device
- function Serial_No (D : hid_device) return Wide_String;
- function Product_String (D : hid_device) return Wide_String;
- private
- type hid_device is new Limited_Controlled with record
- member : access hidapi_hidapi_h.hid_device_info;
- addr : System.Address;
- end record;
- end hidapi;
- with hidapi_hidapi_h;
- with Interfaces.C; use Interfaces.C;
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Unchecked_Conversion;
- with stddef_h;
- package body hidapi is
- function enumerate (vendor_id, product_id : id) return hid_device is
- use hidapi_hidapi_h;
- first : access hid_device_info;
- begin
- first := hid_enumerate(unsigned_short(vendor_id), unsigned_short(product_id));
- if first /= null then
- return H : hid_device do
- H.member := first;
- H.addr := System.Null_Address;
- end return;
- else raise Program_Error;
- end if;
- end enumerate;
- -- helper function to deal with wchar_t * to wide_string
- function Value (P : access stddef_h.wchar_t) return Wide_String is
- temp : Wide_String(1 .. 256);
- count : natural := 0;
- type sd_wchar_ptr is access all stddef_h.wchar_t;
- type wchar_array_ptr is access wchar_array;
- Function To_Wchar_Array_Ptr is new Ada.Unchecked_Conversion(sd_wchar_ptr, wchar_array_ptr);
- WCP : wchar_array_ptr := To_Wchar_Array_Ptr(sd_wchar_ptr(P));
- begin
- Put_Line("Wide string");
- To_Ada(WCP.all, temp, count);
- Put_Line("Wide string length " & natural'image(count));
- return temp(1..count);
- end Value;
- function Serial_No (D : hid_device) return Wide_String is
- use hidapi_hidapi_h;
- begin
- return Value(D.member.serial_number);
- end Serial_No;
- function Product_String (D : hid_device) return Wide_String is
- use hidapi_hidapi_h;
- begin
- return Value(D.member.product_string);
- end Product_String;
- end hidapi;
- with Hidapi;
- with Ada.Wide_Text_IO;
- procedure Test_Hid is
- usbrelay_vendor_id : constant Hidapi.id := 16#16c0#;
- usbrelay_product_id : constant Hidapi.id := 16#05df#;
- Device : Hidapi.hid_device := Hidapi.Enumerate(usbrelay_vendor_id, usbrelay_product_id);
- begin
- Ada.Wide_Text_IO.Put_Line("Serial : " & Device.Serial_No);
- Ada.Wide_Text_IO.Put_Line("Product : " & Device.Product_String);
- end Test_Hid;
Add Comment
Please, Sign In to add comment