Differenze tra le versioni di "Controllare la temperatura dell'hardware mediante le funzioni del API di libsensors"

Da Gambas-it.org - Wikipedia.
 
(7 versioni intermedie di uno stesso utente non sono mostrate)
Riga 8: Riga 8:
 
* tipo di sensore di temperatura (''tempX_type'');
 
* tipo di sensore di temperatura (''tempX_type'');
 
* valori di isteresi (''tempX_max_hyst'' e ''tempX_crit_hyst'').
 
* valori di isteresi (''tempX_max_hyst'' e ''tempX_crit_hyst'').
 
I tipi di sensori termici disponibili sono:
 
<BR>1  ''PII/Celeron Diode''
 
<BR>2  ''3904 transistor''
 
<BR>3  ''thermal diode''
 
<BR>4  ''thermistor''
 
<BR>5  ''AMD AMDSI''
 
<BR>6  ''Intel PECI''
 
 
 
L'<I>Isteresi</i> è la capacità di molti chip di far scattare un allarme quando la temperatura supera il suo limite massimo e critico previsto per quel chip, e di far cessare l'allarme quando la temperatura scende al di sotto di quel limite critico. Quindi , oltre a ''tempX_max'', molti chip hanno una caratteristica secondaria: ''tempX_max_hyst'' e ''tempX_crit_hyst''.
 
L'<I>Isteresi</i> è la capacità di molti chip di far scattare un allarme quando la temperatura supera il suo limite massimo e critico previsto per quel chip, e di far cessare l'allarme quando la temperatura scende al di sotto di quel limite critico. Quindi , oltre a ''tempX_max'', molti chip hanno una caratteristica secondaria: ''tempX_max_hyst'' e ''tempX_crit_hyst''.
  
 
+
Per poter fruire delle risorse esterne della libreria ''libsensors'', si dovrà richiamare l'attuale libreria: "''libsensors.so.5.0.0'' ".
Per poter fruire delle risorse esterne della libreria ''libsensors'', si dovrà richiamare l'attuale libreria: ''libsensors.so.4.3.2'' .
 
 
 
  
 
===Esempio pratico===
 
===Esempio pratico===
 
Di seguito mostriamo un semplice, breve ed essenziale esempio di codice per monitorare la temperatura dell'hardware:
 
Di seguito mostriamo un semplice, breve ed essenziale esempio di codice per monitorare la temperatura dell'hardware:
  Library "libsensors:4.3.2"
+
  Library "libsensors:5.0.0"
 
    
 
    
 
  Public Struct sensors_bus_id
 
  Public Struct sensors_bus_id
Riga 43: Riga 32:
 
   number As Integer
 
   number As Integer
 
   type As Integer
 
   type As Integer
   first_subfeature As Integer   <FONT color=gray>' ''Questi ultimi due campi servono soltanto per un uso interno alla libreria "libsensors"''</font>
+
   first_subfeature As Integer
 
   padding1 As Integer
 
   padding1 As Integer
 
  End Struct
 
  End Struct
Riga 55: Riga 44:
 
  End Struct
 
  End Struct
 
   
 
   
  Private Const SENSORS_MODE_R As Byte = 1
+
  Private Const SENSORS_FEATURE_TEMP As Integer = 2
 +
Private Const SENSORS_SUBFEATURE_TEMP_INPUT As Integer = 512
 
   
 
   
 
  <FONT color=gray>' ''int sensors_init(FILE *input)''
 
  <FONT color=gray>' ''int sensors_init(FILE *input)''
  ' ''Load the configuration file and the detected chips list. If this returns a value unequal to zero, you are in trouble''</font>
+
  ' ''Load the configuration file and the detected chips list.''</font>
  Private Extern sensors_init(filename As File) As Integer
+
  Private Extern sensors_init(input_ As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name *match, int *nr)''
 
  <FONT color=gray>' ''const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name *match, int *nr)''
  ' ''This function returns all detected chips that match a given chip name, one by one. If no chip name is provided, all detected chips are returned.''</font>
+
  ' ''This function returns all detected chips that match a given chip name, one by one.''</font>
  Private Extern sensors_get_detected_chips(scnMatch As Pointer, nrP As Pointer) As Sensors_chip_name
+
Private Extern sensors_get_detected_chips(match_ As Pointer, nr As Pointer) As Pointer
 +
 +
<FONT color=gray>' ''int sensors_snprintf_chip_name(char *str, size_t size, const sensors_chip_name *chip)''
 +
' ''Prints a chip name from its internal representation.''</font>
 +
  Private Extern sensors_snprintf_chip_name(str_ As Pointer, size As Integer, chip As Sensors_chip_name) As Integer
 +
 +
<FONT color=gray>' ''const char* sensors_get_adapter_name (const sensors_bus_id * bus)''
 +
' ''Returns the adapter name of a bus number, as used within the sensors_chip_name structure.''</font>
 +
Private Extern sensors_get_adapter_name(bus As Sensors_bus_id) As String
 
   
 
   
 
  <FONT color=gray>' ''const sensors_feature * sensors_get_features(const sensors_chip_name *name, int *nr)''
 
  <FONT color=gray>' ''const sensors_feature * sensors_get_features(const sensors_chip_name *name, int *nr)''
  ' ''This returns all main features of a specific chip. nr is an internally used variable. Set it to zero to start at the begin of the list. If no more features are found NULL is returned.''</font>
+
  ' ''This returns all main features of a specific chip. nr is an internally used variable.''</font>
  Private Extern sensors_get_features(chip_name As Sensors_chip_name, nrP As Pointer) As Sensors_feature
+
  Private Extern sensors_get_features(name As Sensors_chip_name, nr As Pointer) As Pointer
 
   
 
   
  <FONT color=gray>' ''const sensors_subfeature * sensors_get_all_subfeatures(const sensors_chip_name *name, const sensors_feature *feature, int *nr)''
+
<FONT color=gray>' ''char *sensors_get_label(const sensors_chip_name *name, const sensors_feature *feature)''
  ' ''This returns all subfeatures of a given main feature. nr is an internally used variable. Set it to zero to start at the begin of the list. If no more features are found NULL is returned.''</font>
+
' ''Look up the label for a given feature.''</font>
  Private Extern sensors_get_all_subfeatures(chip_name As Sensors_chip_name, feature As Sensors_feature, nrP As Pointer) As Sensors_subfeature
+
Private Extern sensors_get_label(name As Sensors_chip_name, feature As Sensors_feature) As Pointer
 +
 +
  <FONT color=gray>' ''const sensors_subfeature * sensors_get_subfeature(const sensors_chip_name *name, const sensors_feature *feature, sensors_subfeature_type type)''
 +
  ' ''Returns the subfeature of the given type for a given main feature.''</font>
 +
  Private Extern sensors_get_subfeature(name As Sensors_chip_name, feature As Sensors_feature, type As Integer) As Sensors_subfeature
 
   
 
   
 
  <FONT color=gray>' ''int sensors_get_value(const sensors_chip_name *name, int subfeat_nr, double *value)''
 
  <FONT color=gray>' ''int sensors_get_value(const sensors_chip_name *name, int subfeat_nr, double *value)''
  ' ''Read the value of a subfeature of a certain chip. his function will return 0 on success, and <0 on failure.''</font>
+
  ' ''Read the value of a subfeature of a certain chip.''</font>
  Private Extern sensors_get_value(chip_name As Sensors_chip_name, subfeat_nr As Integer, value As Pointer) As Integer
+
  Private Extern sensors_get_value(name As Sensors_chip_name, subfeat_nr As Integer, value As Pointer) As Integer
 +
 +
<FONT color=gray>' ''void sensors_cleanup(void)''
 +
' ''Cleans everything up.''</font>
 +
Private Extern sensors_cleanup()
 
   
 
   
<FONT color=gray>' ''int sensors_snprintf_chip_name(char *str, size_t size, const sensors_chip_name *chip)''
 
' ''Prints a chip name from its internal representation. Return the number of characters printed on success (same as snprintf), <0 on error.''</font>
 
Private Extern sensors_snprintf_chip_name(strp As Pointer, size As Integer, chip As Sensors_chip_name) As Integer
 
 
   
 
   
  <FONT color=gray>' ''const char* sensors_get_adapter_name (const sensors_bus_id * bus)''
+
Public Sub Main()
  ' ''Returns the adapter name of a bus number, as used within the sensors_chip_name structure. If it could not be found, it returns NULL.''</font>
+
  Private Extern sensors_get_adapter_name(bus_id As Sensors_bus_id) As String
+
  Dim p As Pointer
 +
  Dim scn As Sensors_chip_name
 +
  Dim chn As Integer
 +
 
 +
  If sensors_init(Null) <> 0 Then
 +
    Chiusura()
 +
    Error.Raise("Errore !")
 +
  Endif
 +
 
 +
  <FONT Color=gray>' ''Ritorna nella variabile "chn" il numero di chip trovati:''</font>
 +
  p = sensors_get_detected_chips(Null, VarPtr(chn))
 +
  If p == 0 Then Error.Raise("Errore !")
 +
 
 +
  While p > 0
 +
  <FONT Color=gray>' ''Usa il Puntatore, per non incorrere in errori riscontrati nei test, e lo assegna alla Struttura:''</font>
 +
    scn = p
 +
    If Mostra(scn) > 0 Then Print
 +
    p = sensors_get_detected_chips(Null, VarPtr(chn))
 +
  Wend
 +
 
 +
<FONT Color=gray>' ''Va in chiusura:''</font>
 +
  Chiusura()
 +
 
 +
End
 +
 +
 +
  Private Procedure Chiusura()
 +
 +
<FONT Color=gray>' ''Libera la memoria precedentemente occupata per la libreria "libsensors":''</font>
 +
  sensors_cleanup()
 
   
 
   
  <FONT color=gray>' ''void sensors_cleanup(void)''
+
  End
' ''Cleans everything up: you can't access anything after this, until the next sensors_init() call!''</font>
 
Private Extern sensors_cleanup()
 
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Private Procedure Mostra(nome As Sensors_chip_name) As Integer
 
   
 
   
   Dim scnm As Sensors_chip_name
+
   Dim buf As New Byte[128]
   Dim sf As Sensors_feature
+
   Dim chip As New String[2]
 +
  Dim adap As String
 +
  Dim p As Pointer
 +
  Dim sft As Sensors_feature
 
   Dim subf As Sensors_subfeature
 
   Dim subf As Sensors_subfeature
   Dim nomeB As New Byte[128]
+
   Dim i, c As Integer
  Dim c, lung, f, s, rc As Integer
+
   Dim f As Float
   Dim valTemp As Float
 
 
 
  If sensors_init(Null) <> 0 Then Error.Raise("Impossibile inizializzare la libreria 'libsensors' !")
 
 
 
  scnm = sensors_get_detected_chips(0, VarPtr(c))
 
 
   
 
   
  lung = sensors_snprintf_chip_name(nomeB.Data, 128, scnm)
+
  sensors_snprintf_chip_name(buf.Data, buf.Count, nome)
  Print nomeB.ToString(0, lung)
+
  chip[0] = String@(buf.Data)
 +
  adap = sensors_get_adapter_name(nome.bus)
 +
  chip[1] = adap
 
    
 
    
  Print "Adapter: "; sensors_get_adapter_name(scnm.bus)
+
  p = sensors_get_features(nome, VarPtr(i))
   
+
  if p == 0 Then Error.Raise("Errore !")
  sf = sensors_get_features(scnm, VarPtr(f))
 
   
 
  Print "Tipo di sensore termico: "; sf_type(sf.type)
 
  Print
 
 
   
 
   
  subf = sensors_get_all_subfeatures(scnm, sf, VarPtr(s))
+
  While p > 0
  If subf.flags And SENSORS_MODE_R Then  
+
<FONT Color=gray>' ''Usa il Puntatore, per non incorrere in errori riscontrati nei test, e lo assegna alla Struttura:''</font>
    rc = sensors_get_value(scnm, subf.number, VarPtr(valTemp))
+
    sft = p
    If rc < 0 Then
+
    If sft.type == SENSORS_FEATURE_TEMP Then  
      Error.Raise("Errore nella lettura del valore !")
+
      subf = sensors_get_subfeature(nome, sft, SENSORS_SUBFEATURE_TEMP_INPUT)
    Else
+
      sensors_get_value(nome, subf.number, VarPtr(f))
      Print String@(sf.name); " = "; valTemp; "°"
+
      If chip.Count > 0 Then
    Endif
+
        Print chip[0]
  Endif
+
        Print "Adapter: " & chip[1]
 
+
        chip.Clear
  Print ".........................................\n"
+
      Endif
   
+
      Print String@(sensors_get_label(nome, sft)); ": "; "\e[1m"; f; "\e[0m"; "°C"
  scnm = sensors_get_detected_chips(0, VarPtr(c))
+
      Inc c
 
+
    Endif  
  lung = sensors_snprintf_chip_name(nomeB.Data, 128, scnm)
+
    p = sensors_get_features(nome, VarPtr(i))
  Print nomeB.ToString(0, lung)
+
   Wend
  Print "Adapter: "; sensors_get_adapter_name(scnm.bus)
 
  Print "Tipo di sensore termico: "; sf_type(sf.type)
 
  Print
 
 
 
  If subf.flags And SENSORS_MODE_R Then
 
    rc = sensors_get_value(scnm, subf.number, VarPtr(valTemp))
 
    If rc < 0 Then
 
      Error.Raise("Errore nella lettura del valore !")
 
    Else
 
      Print "Temperatura CPU = "; valTemp; "°"
 
    Endif
 
  Endif
 
 
 
  Print "'''''''''''''''''''''''''''''''''''''''''"
 
 
 
  <FONT color=gray>' ''Va in chiusura:''</font>
 
  sensors_cleanup()
 
 
 
'''End'''
 
    
 
 
   
 
   
'''Private''' Function sf_type(numero As Byte) As String
+
  Return c
 
   
 
   
  Dim tipo As String
+
  End
 
 
  Select Case numero
 
    Case 1
 
      tipo = "PII/Celeron Diode"
 
    Case 2
 
      tipo = "3904 transistor"
 
    Case 3
 
      tipo = "thermal diode"
 
    Case 4
 
      tipo = "thermistor"
 
    Case 5
 
      tipo = "AMD AMDSI"
 
    Case 6
 
      tipo = "Intel PECI"
 
  End Select
 
   
 
  Return tipo
 
 
 
  '''End'''
 
 
 
  
  
  
 
=Riferimenti=
 
=Riferimenti=
[1] La pagina del API di ''[http://fossies.org/dox/lm_sensors-3.3.4/index.html lm_sensors]'' .
+
* https://github.com/ARM-software/gator/blob/master/daemon/libsensors/sensors.h
 
+
* https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
[2] La pagina relativa al ''[http://linux.die.net/man/5/sensors.conf file sensors.conf]''.
+
* http://fossies.org/dox/lm_sensors-3.3.4/index.html
 +
* http://linux.die.net/man/5/sensors.conf

Versione attuale delle 16:34, 14 gen 2024

La libreria Libsensors mette a disposizione risorse per poter effettuare controlli su alcune parti ed aspetti dell'hardware per Linux. In particolare, ad esempio, questa libreria consente di gestire e visualizzare i dati del sensore della temperatura dell'hardware principale.

In un dato sistema, ci possono essere uno o più chip di controllo hardware. I nomi delle funzioni sono standardizzati. Ogni funzione può a sua volta avere uno o più sub-caratteristiche , ognuna di esse rappresenta un attributo della funzione: valore di ingresso , limite inferiore , limite superiore, limite di allarme, ecc. I nomi delle sub-funzionalità sono anch'essi predefiniti. Le sub-caratteristiche effettivamente esistenti dipendono dal tipo di chip presente nel sistema hardware.

La descrizione chip, restituita dall'apposita funzione esterna sensors_snprintf_chip_name(), è formata da più elementi, separati da trattini. Il primo elemento è il tipo di chip, il secondo elemento è il nome del bus, e il terzo elemento è l'indirizzo esadecimale del chip.

Riguardo alla configurazione della temperatura dei chip essa può avere due specifici sub-caratteristiche:

  • tipo di sensore di temperatura (tempX_type);
  • valori di isteresi (tempX_max_hyst e tempX_crit_hyst).

L'Isteresi è la capacità di molti chip di far scattare un allarme quando la temperatura supera il suo limite massimo e critico previsto per quel chip, e di far cessare l'allarme quando la temperatura scende al di sotto di quel limite critico. Quindi , oltre a tempX_max, molti chip hanno una caratteristica secondaria: tempX_max_hyst e tempX_crit_hyst.

Per poter fruire delle risorse esterne della libreria libsensors, si dovrà richiamare l'attuale libreria: "libsensors.so.5.0.0 ".

Esempio pratico

Di seguito mostriamo un semplice, breve ed essenziale esempio di codice per monitorare la temperatura dell'hardware:

Library "libsensors:5.0.0"
 
Public Struct sensors_bus_id
  type As Short
  nr As Short
End Struct

Public Struct sensors_chip_name
  prefix As Pointer
  bus As Struct Sensors_bus_id
  addr As Integer
  path As Pointer
End Struct

Public Struct sensors_feature
  name As Pointer
  number As Integer
  type As Integer
  first_subfeature As Integer
  padding1 As Integer
End Struct

Public Struct sensors_subfeature
  name As Pointer
  number As Integer
  type As Integer
  mapping As Integer
  flags As Integer
End Struct

Private Const SENSORS_FEATURE_TEMP As Integer = 2
Private Const SENSORS_SUBFEATURE_TEMP_INPUT As Integer = 512

' int sensors_init(FILE *input)
' Load the configuration file and the detected chips list.
Private Extern sensors_init(input_ As Pointer) As Integer

' const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name *match, int *nr)
' This function returns all detected chips that match a given chip name, one by one.
Private Extern sensors_get_detected_chips(match_ As Pointer, nr As Pointer) As Pointer

' int sensors_snprintf_chip_name(char *str, size_t size, const sensors_chip_name *chip)
' Prints a chip name from its internal representation.
Private Extern sensors_snprintf_chip_name(str_ As Pointer, size As Integer, chip As Sensors_chip_name) As Integer

' const char* sensors_get_adapter_name (const sensors_bus_id * bus)
' Returns the adapter name of a bus number, as used within the sensors_chip_name structure.
Private Extern sensors_get_adapter_name(bus As Sensors_bus_id) As String

' const sensors_feature * sensors_get_features(const sensors_chip_name *name, int *nr)
' This returns all main features of a specific chip. nr is an internally used variable.
Private Extern sensors_get_features(name As Sensors_chip_name, nr As Pointer) As Pointer

' char *sensors_get_label(const sensors_chip_name *name, const sensors_feature *feature)
' Look up the label for a given feature.
Private Extern sensors_get_label(name As Sensors_chip_name, feature As Sensors_feature) As Pointer

' const sensors_subfeature * sensors_get_subfeature(const sensors_chip_name *name, const sensors_feature *feature, sensors_subfeature_type type)
' Returns the subfeature of the given type for a given main feature.
Private Extern sensors_get_subfeature(name As Sensors_chip_name, feature As Sensors_feature, type As Integer) As Sensors_subfeature

' int sensors_get_value(const sensors_chip_name *name, int subfeat_nr, double *value)
' Read the value of a subfeature of a certain chip.
Private Extern sensors_get_value(name As Sensors_chip_name, subfeat_nr As Integer, value As Pointer) As Integer

' void sensors_cleanup(void)
' Cleans everything up.
Private Extern sensors_cleanup()


Public Sub Main()

 Dim p As Pointer
 Dim scn As Sensors_chip_name
 Dim chn As Integer
 
 If sensors_init(Null) <> 0 Then
   Chiusura()
   Error.Raise("Errore !")
 Endif
 
' Ritorna nella variabile "chn" il numero di chip trovati:
 p = sensors_get_detected_chips(Null, VarPtr(chn))
 If p == 0 Then Error.Raise("Errore !")
 
 While p > 0
' Usa il Puntatore, per non incorrere in errori riscontrati nei test, e lo assegna alla Struttura:
   scn = p
   If Mostra(scn) > 0 Then Print
   p = sensors_get_detected_chips(Null, VarPtr(chn))
 Wend 
 
' Va in chiusura:
 Chiusura()
 
End


Private Procedure Chiusura()

' Libera la memoria precedentemente occupata per la libreria "libsensors":
 sensors_cleanup()

End


Private Procedure Mostra(nome As Sensors_chip_name) As Integer

 Dim buf As New Byte[128]
 Dim chip As New String[2]
 Dim adap As String
 Dim p As Pointer
 Dim sft As Sensors_feature
 Dim subf As Sensors_subfeature
 Dim i, c As Integer
 Dim f As Float

 sensors_snprintf_chip_name(buf.Data, buf.Count, nome)
 chip[0] = String@(buf.Data)
 adap = sensors_get_adapter_name(nome.bus)
 chip[1] = adap
 
 p = sensors_get_features(nome, VarPtr(i))
 if p == 0 Then Error.Raise("Errore !")

 While p > 0
' Usa il Puntatore, per non incorrere in errori riscontrati nei test, e lo assegna alla Struttura:
   sft = p
   If sft.type == SENSORS_FEATURE_TEMP Then 
     subf = sensors_get_subfeature(nome, sft, SENSORS_SUBFEATURE_TEMP_INPUT) 
     sensors_get_value(nome, subf.number, VarPtr(f))
     If chip.Count > 0 Then
       Print chip[0]
       Print "Adapter: " & chip[1]
       chip.Clear
     Endif
     Print String@(sensors_get_label(nome, sft)); ": "; "\e[1m"; f; "\e[0m"; "°C"
     Inc c
   Endif 
   p = sensors_get_features(nome, VarPtr(i))
 Wend 

 Return c

End


Riferimenti